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 _TREE_H
19 #define _TREE_H
20 
21 #include "lib.h"
22 #include "ident.h"
23 #include "prim.h"
24 #include "type.h"
25 
26 #include <stdint.h>
27 
28 typedef enum port_mode {
29    PORT_INVALID,
30    PORT_IN,
31    PORT_OUT,
32    PORT_INOUT,
33    PORT_BUFFER,
34    PORT_LINKAGE
35 } port_mode_t;
36 
37 typedef enum class {
38    C_DEFAULT,
39    C_SIGNAL,
40    C_VARIABLE,
41    C_CONSTANT,
42    C_FILE,
43    C_ENTITY,
44    C_COMPONENT,
45    C_CONFIGURATION,
46    C_ARCHITECTURE,
47    C_FUNCTION,
48    C_PACKAGE,
49    C_TYPE,
50    C_SUBTYPE,
51    C_LABEL,
52    C_PROCEDURE,
53    C_LITERAL,
54    C_UNITS,
55    C_LIBRARY,
56    C_ELAB
57 } class_t;
58 
59 typedef enum param_kind {
60    P_POS,
61    P_NAMED
62 } param_kind_t;
63 
64 typedef enum assoc_kind {
65    A_POS,
66    A_NAMED,
67    A_RANGE,
68    A_OTHERS
69 } assoc_kind_t;
70 
71 typedef enum literal_kind {
72    L_INT,
73    L_REAL,
74    L_NULL,
75    L_STRING
76 } literal_kind_t;
77 
78 typedef enum constraint_kind {
79    C_RANGE,
80    C_INDEX,
81    C_COMPUTED
82 } constraint_kind_t;
83 
84 typedef enum tree_kind {
85    T_ENTITY,
86    T_ARCH,
87    T_PORT_DECL,
88    T_FCALL,
89    T_LITERAL,
90    T_SIGNAL_DECL,
91    T_VAR_DECL,
92    T_PROCESS,
93    T_REF,
94    T_WAIT,
95    T_TYPE_DECL,
96    T_VAR_ASSIGN,
97    T_PACKAGE,
98    T_SIGNAL_ASSIGN,
99    T_QUALIFIED,
100    T_ENUM_LIT,
101    T_CONST_DECL,
102    T_FUNC_DECL,
103    T_ELAB,
104    T_AGGREGATE,
105    T_ASSERT,
106    T_ATTR_REF,
107    T_ARRAY_REF,
108    T_ARRAY_SLICE,
109    T_INSTANCE,
110    T_IF,
111    T_NULL,
112    T_PACK_BODY,
113    T_FUNC_BODY,
114    T_RETURN,
115    T_CASSIGN,
116    T_WHILE,
117    T_WAVEFORM,
118    T_ALIAS,
119    T_FOR,
120    T_ATTR_DECL,
121    T_ATTR_SPEC,
122    T_PROC_DECL,
123    T_PROC_BODY,
124    T_EXIT,
125    T_PCALL,
126    T_CASE,
127    T_BLOCK,
128    T_COND,
129    T_CONCAT,
130    T_TYPE_CONV,
131    T_SELECT,
132    T_COMPONENT,
133    T_IF_GENERATE,
134    T_FOR_GENERATE,
135    T_FILE_DECL,
136    T_OPEN,
137    T_FIELD_DECL,
138    T_RECORD_REF,
139    T_ALL,
140    T_NEW,
141    T_CASSERT,
142    T_CPCALL,
143    T_UNIT_DECL,
144    T_NEXT,
145    T_GENVAR,
146    T_PARAM,
147    T_ASSOC,
148    T_USE,
149    T_HIER,
150    T_SPEC,
151    T_BINDING,
152    T_LIBRARY,
153    T_DESIGN_UNIT,
154    T_CONFIGURATION,
155    T_PROT_BODY,
156    T_CONTEXT,
157    T_CTXREF,
158    T_CONSTRAINT,
159    T_BLOCK_CONFIG,
160    T_PRAGMA,
161 
162    T_LAST_TREE_KIND
163 } tree_kind_t;
164 
165 typedef enum {
166    TREE_F_LOCALLY_STATIC  = (1 << 0),
167    TREE_F_GLOBALLY_STATIC = (1 << 1),
168    TREE_F_UNCONSTRAINED   = (1 << 2),
169    TREE_F_IMPURE          = (1 << 3),
170    TREE_F_CONVERSION      = (1 << 4),
171    TREE_F_POSTPONED       = (1 << 5),
172    TREE_F_SHARED          = (1 << 6),
173    TREE_F_REPORT          = (1 << 7),
174    TREE_F_GUARDED         = (1 << 8),
175    TREE_F_ELIDE_BOUNDS    = (1 << 9),
176    TREE_F_LAST_VALUE      = (1 << 10),
177    TREE_F_PACKAGE_SIGNAL  = (1 << 11),
178    TREE_F_SYNTHETIC_NAME  = (1 << 12),
179 } tree_flags_t;
180 
181 tree_t tree_new(tree_kind_t kind);
182 tree_kind_t tree_kind(tree_t t);
183 void tree_change_kind(tree_t t, tree_kind_t kind);
184 const char *tree_kind_str(tree_kind_t t);
185 
186 const loc_t *tree_loc(tree_t t);
187 void tree_set_loc(tree_t t, const loc_t *loc);
188 
189 // See `has_map' in tree.c for definition of which fields each tree
190 // kind contains
191 
192 type_t tree_type(tree_t t);
193 void tree_set_type(tree_t t, type_t ty);
194 bool tree_has_type(tree_t t);
195 
196 ident_t tree_ident(tree_t t);
197 void tree_set_ident(tree_t t, ident_t i);
198 bool tree_has_ident(tree_t t);
199 
200 ident_t tree_ident2(tree_t t);
201 void tree_set_ident2(tree_t t, ident_t i);
202 bool tree_has_ident2(tree_t t);
203 
204 unsigned tree_ports(tree_t t);
205 tree_t tree_port(tree_t t, unsigned n);
206 void tree_add_port(tree_t t, tree_t d);
207 
208 tree_t tree_file_mode(tree_t t);
209 void tree_set_file_mode(tree_t t, tree_t m);
210 
211 unsigned tree_generics(tree_t t);
212 tree_t tree_generic(tree_t t, unsigned n);
213 void tree_add_generic(tree_t t, tree_t d);
214 
215 unsigned tree_genmaps(tree_t t);
216 tree_t tree_genmap(tree_t t, unsigned n);
217 void tree_add_genmap(tree_t t, tree_t e);
218 
219 unsigned tree_params(tree_t t);
220 tree_t tree_param(tree_t t, unsigned n);
221 void tree_add_param(tree_t t, tree_t e);
222 
223 int64_t tree_ival(tree_t t);
224 void tree_set_ival(tree_t t, int64_t i);
225 
226 double tree_dval(tree_t t);
227 void tree_set_dval(tree_t t, double d);
228 
229 bool tree_has_value(tree_t t);
230 tree_t tree_value(tree_t t);
231 void tree_set_value(tree_t t, tree_t v);
232 
233 unsigned tree_waveforms(tree_t t);
234 tree_t tree_waveform(tree_t t, unsigned n);
235 void tree_add_waveform(tree_t t, tree_t w);
236 
237 unsigned tree_decls(tree_t t);
238 tree_t tree_decl(tree_t t, unsigned n);
239 void tree_add_decl(tree_t t, tree_t d);
240 
241 unsigned tree_stmts(tree_t t);
242 tree_t tree_stmt(tree_t t, unsigned n);
243 void tree_add_stmt(tree_t t, tree_t d);
244 
245 unsigned tree_else_stmts(tree_t t);
246 tree_t tree_else_stmt(tree_t t, unsigned n);
247 void tree_add_else_stmt(tree_t t, tree_t d);
248 
249 unsigned tree_conds(tree_t t);
250 tree_t tree_cond(tree_t t, unsigned n);
251 void tree_add_cond(tree_t t, tree_t d);
252 
253 bool tree_has_delay(tree_t t);
254 tree_t tree_delay(tree_t t);
255 void tree_set_delay(tree_t t, tree_t d);
256 
257 unsigned tree_triggers(tree_t t);
258 tree_t tree_trigger(tree_t t, unsigned n);
259 void tree_add_trigger(tree_t t, tree_t s);
260 
261 tree_t tree_target(tree_t t);
262 void tree_set_target(tree_t t, tree_t lhs);
263 
264 char *tree_text(tree_t t);
265 void tree_set_text(tree_t t, const char *text);
266 
267 tree_t tree_ref(tree_t t);
268 bool tree_has_ref(tree_t t);
269 void tree_set_ref(tree_t t, tree_t decl);
270 
271 unsigned tree_contexts(tree_t t);
272 tree_t tree_context(tree_t t, unsigned n);
273 void tree_add_context(tree_t t, tree_t ctx);
274 
275 unsigned tree_assocs(tree_t t);
276 tree_t tree_assoc(tree_t t, unsigned n);
277 void tree_add_assoc(tree_t t, tree_t a);
278 
279 tree_t tree_severity(tree_t t);
280 void tree_set_severity(tree_t t, tree_t s);
281 
282 tree_t tree_message(tree_t t);
283 bool tree_has_message(tree_t t);
284 void tree_set_message(tree_t t, tree_t m);
285 
286 unsigned tree_pos(tree_t t);
287 void tree_set_pos(tree_t t, unsigned pos);
288 
289 unsigned tree_subkind(tree_t t);
290 void tree_set_subkind(tree_t t, unsigned sub);
291 
292 void tree_add_range(tree_t t, range_t r);
293 range_t tree_range(tree_t t, unsigned n);
294 unsigned tree_ranges(tree_t t);
295 void tree_change_range(tree_t t, unsigned n, range_t r);
296 
297 class_t tree_class(tree_t t);
298 void tree_set_class(tree_t t, class_t c);
299 
300 tree_t tree_reject(tree_t t);
301 void tree_set_reject(tree_t t, tree_t r);
302 bool tree_has_reject(tree_t t);
303 
304 tree_t tree_name(tree_t t);
305 void tree_set_name(tree_t t, tree_t n);
306 
307 tree_t tree_spec(tree_t t);
308 bool tree_has_spec(tree_t t);
309 void tree_set_spec(tree_t t, tree_t s);
310 
311 unsigned tree_ops(tree_t t);
312 tree_t tree_op(tree_t t, unsigned n);
313 void tree_add_op(tree_t t, tree_t s);
314 
315 unsigned tree_chars(tree_t t);
316 tree_t tree_char(tree_t t, unsigned n);
317 void tree_add_char(tree_t t, tree_t ref);
318 
319 unsigned tree_nets(tree_t t);
320 netid_t tree_net(tree_t t, unsigned n);
321 void tree_add_net(tree_t t, netid_t n);
322 void tree_change_net(tree_t t, unsigned n, netid_t i);
323 
324 tree_flags_t tree_flags(tree_t t);
325 void tree_set_flag(tree_t t, tree_flags_t mask);
326 
327 void tree_add_attr_str(tree_t t, ident_t name, ident_t str);
328 ident_t tree_attr_str(tree_t t, ident_t name);
329 void tree_add_attr_int(tree_t t, ident_t name, int n);
330 int tree_attr_int(tree_t t, ident_t name, int def);
331 void tree_add_attr_ptr(tree_t t, ident_t name, void *ptr);
332 void *tree_attr_ptr(tree_t t, ident_t name);
333 tree_t tree_attr_tree(tree_t t, ident_t name);
334 void tree_add_attr_tree(tree_t t, ident_t name, tree_t val);
335 void tree_remove_attr(tree_t t, ident_t name);
336 
337 typedef void (*tree_visit_fn_t)(tree_t t, void *context);
338 unsigned tree_visit(tree_t t, tree_visit_fn_t fn, void *context);
339 unsigned tree_visit_only(tree_t t, tree_visit_fn_t fn,
340                          void *context, tree_kind_t kind);
341 
342 typedef tree_t (*tree_rewrite_fn_t)(tree_t t, void *context);
343 tree_t tree_rewrite(tree_t t, tree_rewrite_fn_t fn, void *context);
344 
345 typedef bool (*tree_copy_fn_t)(tree_t t, void *context);
346 tree_t tree_copy(tree_t t, tree_copy_fn_t fn, void *context);
347 
348 void tree_gc(void);
349 
350 tree_wr_ctx_t tree_write_begin(fbuf_t *f);
351 void tree_write(tree_t t, tree_wr_ctx_t ctx);
352 void tree_write_end(tree_wr_ctx_t ctx);
353 
354 tree_rd_ctx_t tree_read_begin(fbuf_t *f, const char *name);
355 tree_t tree_read(tree_rd_ctx_t ctx);
356 void tree_read_end(tree_rd_ctx_t ctx);
357 
358 #endif  // _TREE_H
359