Ajla 0.1.0
[ajla.git] / newlib / compiler / parser / local_type.ajla
blob2a75289151ab0058cdd88be03ff6367d0c94846c
1 {*
2  * Copyright (C) 2024 Mikulas Patocka
3  *
4  * This file is part of Ajla.
5  *
6  * Ajla is free software: you can redistribute it and/or modify it under the
7  * terms of the GNU General Public License as published by the Free Software
8  * Foundation, either version 3 of the License, or (at your option) any later
9  * version.
10  *
11  * Ajla is distributed in the hope that it will be useful, but WITHOUT ANY
12  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
13  * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along with
16  * Ajla. If not, see <https://www.gnu.org/licenses/>.
17  *}
19 private unit compiler.parser.local_type;
21 uses compiler.parser.dict;
22 uses compiler.parser.type;
24 fn get_record_local_type(ctx rec_ctx : function_context) : (function_context, int);
25 fn get_local_type_cc(ctx : function_context, cc : compare_context, typ : int) : (function_context, int);
26 fn generate_local_types(ctx : function_context) : function_context;
28 implementation
30 uses compiler.common.gvn;
31 uses compiler.parser.gen2;
33 fn local_type_to_int(lt : local_type) : int
35         return gvn_encode(list(pcode_t).[ lt.mode ] + lt.args);
38 fn local_type_idx(ctx : function_context, lt : local_type) : (function_context, int)
40         var r := local_type_to_int(lt);
41         var m := ctx.local_types_map[r];
42         if not is_uninitialized(m) then
43                 return ctx, m;
44         var idx := len(ctx.local_types);
45         ctx.local_types_map[r] := idx;
46         ctx.local_types +<= lt;
47         return ctx, idx;
50 fn get_record_local_type(ctx rec_ctx : function_context) : (function_context, int)
52         var lt := local_type.[
53                 mode : Local_Type_Record,
54                 args : generate_function_id(rec_ctx.id),
55         ];
56         return local_type_idx(ctx, lt);
59 fn get_local_type_cc(ctx : function_context, cc : compare_context, typ : int) : (function_context, int)
61         if typ = T_Record then [
62                 var rec := cc.ctx.record_def.j;
63                 if not rec.is_option then [
64                         return get_record_local_type(ctx, cc.ctx);
65                 ] else [
66                         if rec.is_flat_option then
67                                 return ctx, T_FlatOption;
68                         else
69                                 return ctx, T_Undetermined;
70                 ]
71         ] else if typ < 0 and typ <> T_UnknownType then [
72                 return ctx, typ;
73         ]
74         return ctx, T_Undetermined;
77 fn get_local_type(ctx : function_context, i : int) : (function_context, int)
79         var cc, typ := get_deep_type_of_var(ctx, i);
80         return get_local_type_cc(ctx, cc, typ);
83 fn generate_local_types(ctx : function_context) : function_context
85         var ctx2, bol := generate_Bool(ctx);
86         for i := 0 to len(ctx.variables) do [
87                 var lt : int;
88                 if verify_equality(ctx2, get_type_of_var(ctx2, i), bol) then
89                         lt := T_FlatOption;
90                 else
91                         ctx, lt := get_local_type(ctx, i);
92                 ctx.variables[i].local_type := lt;
93         ]
94         return ctx;