2 * Copyright (C) 2024 Mikulas Patocka
4 * This file is part of Ajla.
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
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.
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/>.
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;
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
44 var idx := len(ctx.local_types);
45 ctx.local_types_map[r] := idx;
46 ctx.local_types +<= lt;
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),
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);
66 if rec.is_flat_option then
67 return ctx, T_FlatOption;
69 return ctx, T_Undetermined;
71 ] else if typ < 0 and typ <> T_UnknownType then [
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 [
88 if verify_equality(ctx2, get_type_of_var(ctx2, i), bol) then
91 ctx, lt := get_local_type(ctx, i);
92 ctx.variables[i].local_type := lt;