1 //
2 //  Copyright (C) 2011-2019  Nick Gasson
3 //
4 //  This program is free software: you can redistribute it and/or modify
5 //  it under the terms of the GNU General Public License as published by
6 //  the Free Software Foundation, either version 3 of the License, or
7 //  (at your option) any later version.
8 //
9 //  This program is distributed in the hope that it will be useful,
10 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
11 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 //  GNU General Public License for more details.
13 //
14 //  You should have received a copy of the GNU General Public License
15 //  along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 //
17 
18 #ifndef _TYPE_H
19 #define _TYPE_H
20 
21 #include <stdbool.h>
22 
23 #include "ident.h"
24 #include "prim.h"
25 
26 typedef enum type_kind {
27    T_UNRESOLVED,
28    T_SUBTYPE,
29    T_INTEGER,
30    T_REAL,
31    T_ENUM,
32    T_PHYSICAL,
33    T_CARRAY,
34    T_UARRAY,
35    T_RECORD,
36    T_FILE,
37    T_ACCESS,
38    T_FUNC,
39    T_INCOMPLETE,
40    T_PROC,
41    T_NONE,
42    T_PROTECTED,
43 
44    T_LAST_TYPE_KIND
45 } type_kind_t;
46 
47 type_t type_new(type_kind_t kind);
48 
49 type_kind_t type_kind(type_t t);
50 const char *type_kind_str(type_kind_t t);
51 
52 bool type_eq(type_t a, type_t b);
53 bool type_strict_eq(type_t a, type_t b);
54 
55 // See `has_map' in type.c for definition of which fields each type
56 // kind contains
57 
58 ident_t type_ident(type_t t);
59 void type_set_ident(type_t t, ident_t id);
60 bool type_has_ident(type_t t);
61 
62 type_t type_base(type_t t);
63 void type_set_base(type_t t, type_t b);
64 
65 type_t type_elem(type_t t);
66 void type_set_elem(type_t t, type_t e);
67 
68 unsigned type_dims(type_t t);
69 range_t type_dim(type_t t, unsigned n);
70 void type_add_dim(type_t t, range_t r);
71 void type_change_dim(type_t t, unsigned n, range_t r);
72 
73 unsigned type_enum_literals(type_t t);
74 struct tree *type_enum_literal(type_t t, unsigned n);
75 void type_enum_add_literal(type_t t, tree_t lit);
76 
77 unsigned type_units(type_t t);
78 tree_t type_unit(type_t t, unsigned n);
79 void type_add_unit(type_t t, tree_t u);
80 
81 unsigned type_params(type_t t);
82 type_t type_param(type_t t, unsigned n);
83 void type_add_param(type_t t, type_t p);
84 void type_change_param(type_t t, unsigned n, type_t p);
85 
86 type_t type_result(type_t t);
87 void type_set_result(type_t t, type_t r);
88 
89 type_t type_access(type_t t);
90 void type_set_access(type_t t, type_t a);
91 
92 type_t type_file(type_t t);
93 void type_set_file(type_t t, type_t f);
94 
95 unsigned type_index_constrs(type_t t);
96 void type_add_index_constr(type_t t, type_t c);
97 void type_change_index_constr(type_t t, unsigned n, type_t c);
98 type_t type_index_constr(type_t t, unsigned n);
99 
100 void type_set_constraint(type_t t, tree_t c);
101 tree_t type_constraint(type_t t);
102 bool type_has_constraint(type_t t);
103 
104 unsigned type_fields(type_t t);
105 tree_t type_field(type_t t, unsigned n);
106 void type_add_field(type_t t, tree_t e);
107 
108 unsigned type_decls(type_t t);
109 tree_t type_decl(type_t t, unsigned n);
110 void type_add_decl(type_t t, tree_t e);
111 
112 tree_t type_body(type_t t);
113 void type_set_body(type_t t, tree_t b);
114 bool type_has_body(type_t t);
115 
116 void type_replace(type_t t, type_t a);
117 void type_change_kind(type_t t, type_kind_t kind);
118 
119 void type_set_resolution(type_t t, tree_t r);
120 bool type_has_resolution(type_t t);
121 tree_t type_resolution(type_t t);
122 
123 // Pretty printing
124 typedef const char *(*minify_fn_t)(const char *);
125 const char *type_pp(type_t t);
126 const char *type_pp_minify(type_t t, minify_fn_t fn);
127 
128 // Predefined types
129 type_t type_universal_int(void);
130 type_t type_universal_real(void);
131 bool type_is_universal(type_t t);
132 
133 // Type predicates that recurse to base of subtypes
134 bool type_is_array(type_t t);
135 bool type_is_record(type_t t);
136 bool type_is_unconstrained(type_t t);
137 bool type_is_enum(type_t t);
138 bool type_is_integer(type_t t);
139 bool type_is_real(type_t t);
140 bool type_is_scalar(type_t t);
141 bool type_is_file(type_t t);
142 bool type_is_protected(type_t t);
143 bool type_is_access(type_t t);
144 bool type_is_physical(type_t t);
145 bool type_is_discrete(type_t t);
146 bool type_is_subprogram(type_t t);
147 
148 // Helper to find ultimate base type
149 type_t type_base_recur(type_t t);
150 type_kind_t type_base_kind(type_t t);
151 
152 // Helper function to find number of sub-elemets
153 unsigned type_width(type_t type);
154 bool type_known_width(type_t type);
155 
156 #endif  // _TYPE_H
157