about summary refs log tree commit diff
path: root/users/glittershark/achilles/src/passes/hir/mod.rs
blob: 845bfcb7ab6a1a7c50ad7417d6c47d513a510579 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
use std::collections::HashMap;

use crate::ast::hir::{Binding, Decl, Expr};
use crate::ast::{BinaryOperator, Ident, Literal, UnaryOperator};

pub(crate) mod monomorphize;
pub(crate) mod strip_positive_units;

pub(crate) trait Visitor<'a, 'ast, T: 'ast>: Sized + 'a {
    type Error;

    fn visit_type(&mut self, _type: &mut T) -> Result<(), Self::Error> {
        Ok(())
    }

    fn visit_ident(&mut self, _ident: &mut Ident<'ast>) -> Result<(), Self::Error> {
        Ok(())
    }

    fn visit_literal(&mut self, _literal: &mut Literal<'ast>) -> Result<(), Self::Error> {
        Ok(())
    }

    fn visit_unary_operator(&mut self, _op: &mut UnaryOperator) -> Result<(), Self::Error> {
        Ok(())
    }

    fn visit_binary_operator(&mut self, _op: &mut BinaryOperator) -> Result<(), Self::Error> {
        Ok(())
    }

    fn visit_binding(&mut self, binding: &mut Binding<'ast, T>) -> Result<(), Self::Error> {
        self.visit_ident(&mut binding.ident)?;
        self.visit_type(&mut binding.type_)?;
        self.visit_expr(&mut binding.body)?;
        Ok(())
    }

    fn post_visit_call(
        &mut self,
        _fun: &mut Expr<'ast, T>,
        _type_args: &mut HashMap<Ident<'ast>, T>,
        _args: &mut Vec<Expr<'ast, T>>,
    ) -> Result<(), Self::Error> {
        Ok(())
    }

    fn pre_visit_call(
        &mut self,
        _fun: &mut Expr<'ast, T>,
        _type_args: &mut HashMap<Ident<'ast>, T>,
        _args: &mut Vec<Expr<'ast, T>>,
    ) -> Result<(), Self::Error> {
        Ok(())
    }

    fn pre_visit_expr(&mut self, _expr: &mut Expr<'ast, T>) -> Result<(), Self::Error> {
        Ok(())
    }

    fn visit_expr(&mut self, expr: &mut Expr<'ast, T>) -> Result<(), Self::Error> {
        self.pre_visit_expr(expr)?;
        match expr {
            Expr::Ident(id, t) => {
                self.visit_ident(id)?;
                self.visit_type(t)?;
            }
            Expr::Literal(lit, t) => {
                self.visit_literal(lit)?;
                self.visit_type(t)?;
            }
            Expr::UnaryOp { op, rhs, type_ } => {
                self.visit_unary_operator(op)?;
                self.visit_expr(rhs)?;
                self.visit_type(type_)?;
            }
            Expr::BinaryOp {
                lhs,
                op,
                rhs,
                type_,
            } => {
                self.visit_expr(lhs)?;
                self.visit_binary_operator(op)?;
                self.visit_expr(rhs)?;
                self.visit_type(type_)?;
            }
            Expr::Let {
                bindings,
                body,
                type_,
            } => {
                for binding in bindings.iter_mut() {
                    self.visit_binding(binding)?;
                }
                self.visit_expr(body)?;
                self.visit_type(type_)?;
            }
            Expr::If {
                condition,
                then,
                else_,
                type_,
            } => {
                self.visit_expr(condition)?;
                self.visit_expr(then)?;
                self.visit_expr(else_)?;
                self.visit_type(type_)?;
            }
            Expr::Fun {
                args,
                body,
                type_args,
                type_,
            } => {
                for (ident, t) in args {
                    self.visit_ident(ident)?;
                    self.visit_type(t)?;
                }
                for ta in type_args {
                    self.visit_ident(ta)?;
                }
                self.visit_expr(body)?;
                self.visit_type(type_)?;
            }
            Expr::Call {
                fun,
                args,
                type_args,
                type_,
            } => {
                self.pre_visit_call(fun, type_args, args)?;
                self.visit_expr(fun)?;
                for arg in args.iter_mut() {
                    self.visit_expr(arg)?;
                }
                self.visit_type(type_)?;
                self.post_visit_call(fun, type_args, args)?;
            }
        }

        Ok(())
    }

    fn post_visit_decl(&mut self, decl: &'a Decl<'ast, T>) -> Result<(), Self::Error> {
        Ok(())
    }

    fn post_visit_fun_decl(
        &mut self,
        _name: &mut Ident<'ast>,
        _type_args: &mut Vec<Ident>,
        _args: &mut Vec<(Ident, T)>,
        _body: &mut Box<Expr<T>>,
        _type_: &mut T,
    ) -> Result<(), Self::Error> {
        Ok(())
    }

    fn visit_decl(&mut self, decl: &'a mut Decl<'ast, T>) -> Result<(), Self::Error> {
        match decl {
            Decl::Fun {
                name,
                type_args,
                args,
                body,
                type_,
            } => {
                self.visit_ident(name)?;
                for type_arg in type_args.iter_mut() {
                    self.visit_ident(type_arg)?;
                }
                for (arg, t) in args.iter_mut() {
                    self.visit_ident(arg)?;
                    self.visit_type(t)?;
                }
                self.visit_expr(body)?;
                self.visit_type(type_)?;
                self.post_visit_fun_decl(name, type_args, args, body, type_)?;
            }
            Decl::Extern {
                name,
                arg_types,
                ret_type,
            } => {
                self.visit_ident(name)?;
                for arg_t in arg_types {
                    self.visit_type(arg_t)?;
                }
                self.visit_type(ret_type)?;
            }
        }

        self.post_visit_decl(decl)?;
        Ok(())
    }
}