source: mainline/uspace/app/sbi/src/tdata.c@ 73401643

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 73401643 was 1433ecda, checked in by Jiri Svoboda <jiri@…>, 7 years ago

Fix cstyle: make ccheck-fix and commit only files where all the changes are good.

  • Property mode set to 100644
File size: 20.3 KB
Line 
1/*
2 * Copyright (c) 2010 Jiri Svoboda
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/** @file Run-time data representation. */
30
31#include <stdlib.h>
32#include <assert.h>
33#include "intmap.h"
34#include "list.h"
35#include "mytypes.h"
36#include "stree.h"
37#include "strtab.h"
38#include "symbol.h"
39
40#include "tdata.h"
41
42static void tdata_item_subst_tprimitive(tdata_primitive_t *torig,
43 tdata_tvv_t *tvv, tdata_item_t **res);
44static void tdata_item_subst_tobject(tdata_object_t *torig, tdata_tvv_t *tvv,
45 tdata_item_t **res);
46static void tdata_item_subst_tarray(tdata_array_t *torig, tdata_tvv_t *tvv,
47 tdata_item_t **res);
48static void tdata_item_subst_tdeleg(tdata_deleg_t *torig,
49 tdata_tvv_t *tvv, tdata_item_t **res);
50static void tdata_item_subst_tebase(tdata_ebase_t *tebase,
51 tdata_tvv_t *tvv, tdata_item_t **res);
52static void tdata_item_subst_tenum(tdata_enum_t *tenum,
53 tdata_tvv_t *tvv, tdata_item_t **res);
54static void tdata_item_subst_tfun(tdata_fun_t *torig,
55 tdata_tvv_t *tvv, tdata_item_t **res);
56static void tdata_item_subst_tvref(tdata_vref_t *tvref, tdata_tvv_t *tvv,
57 tdata_item_t **res);
58
59static void tdata_item_subst_fun_sig(tdata_fun_sig_t *torig, tdata_tvv_t *tvv,
60 tdata_fun_sig_t **res);
61
62static void tdata_tprimitive_print(tdata_primitive_t *tprimitive);
63static void tdata_tobject_print(tdata_object_t *tobject);
64static void tdata_tarray_print(tdata_array_t *tarray);
65static void tdata_tdeleg_print(tdata_deleg_t *tdeleg);
66static void tdata_tebase_print(tdata_ebase_t *tebase);
67static void tdata_tenum_print(tdata_enum_t *tenum);
68static void tdata_tfun_print(tdata_fun_t *tfun);
69static void tdata_tvref_print(tdata_vref_t *tvref);
70
71/** Determine if CSI @a a is derived from CSI described by type item @a tb.
72 *
73 * XXX This won't work with generics.
74 *
75 * @param a Potential derived CSI.
76 * @param tb Type of potentail base CSI.
77 */
78bool_t tdata_is_csi_derived_from_ti(stree_csi_t *a, tdata_item_t *tb)
79{
80 bool_t res;
81
82 switch (tb->tic) {
83 case tic_tobject:
84 res = stree_is_csi_derived_from_csi(a, tb->u.tobject->csi);
85 break;
86 default:
87 printf("Error: Base type is not a CSI.\n");
88 exit(1);
89 }
90
91 return res;
92}
93
94/**
95 * Determine if CSI described by type item @a a is derived from CSI described
96 * by type item @a tb.
97 *
98 * XXX This is somewhat complementary to stype_convert(). It is used for
99 * the explicit @c as conversion. It should only work for objects and only
100 * allow conversion from base to derived types. We might want to scrap this
101 * for a version specific to @c as. The current code does not work with
102 * generics.
103 *
104 * @param a Potential derived CSI.
105 * @param tb Type of potentail base CSI.
106 */
107bool_t tdata_is_ti_derived_from_ti(tdata_item_t *ta, tdata_item_t *tb)
108{
109 bool_t res;
110
111 switch (ta->tic) {
112 case tic_tobject:
113 res = tdata_is_csi_derived_from_ti(ta->u.tobject->csi, tb);
114 break;
115 default:
116 printf("Error: Derived type is not a CSI.\n");
117 exit(1);
118 }
119
120 return res;
121}
122
123/** Determine if two type items are equal (i.e. describe the same type).
124 *
125 * Needed to check compatibility of type arguments in which a parametrized
126 * type is not monotonous.
127 *
128 * @param a Type item
129 * @param b Type item
130 * @return @c b_true if equal, @c b_false if not.
131 */
132bool_t tdata_item_equal(tdata_item_t *a, tdata_item_t *b)
133{
134 /*
135 * Special case: Nil vs. object
136 *
137 * XXX Type of @c Nil should probably be @c object to avoid this
138 * madness.
139 */
140 if (a->tic == tic_tprimitive && a->u.tprimitive->tpc == tpc_nil) {
141 if (b->tic == tic_tobject)
142 return b_true;
143 } else if (b->tic == tic_tprimitive && b->u.tprimitive->tpc == tpc_nil) {
144 if (a->tic == tic_tobject)
145 return b_true;
146 }
147
148 if (a->tic != b->tic)
149 return b_false;
150
151 switch (a->tic) {
152 case tic_tprimitive:
153 /* Check if both have the same tprimitive class. */
154 return (a->u.tprimitive->tpc == b->u.tprimitive->tpc);
155 case tic_tobject:
156 /* Check if both use the same CSI definition. */
157 return (a->u.tobject->csi == b->u.tobject->csi);
158 case tic_tarray:
159 /* Compare rank and base type. */
160 if (a->u.tarray->rank != b->u.tarray->rank)
161 return b_false;
162
163 return tdata_item_equal(a->u.tarray->base_ti,
164 b->u.tarray->base_ti);
165 case tic_tenum:
166 /* Check if both use the same enum definition. */
167 return (a->u.tenum->enum_d == b->u.tenum->enum_d);
168 case tic_tvref:
169 /* Check if both refer to the same type argument. */
170 return (a->u.tvref->targ == b->u.tvref->targ);
171 default:
172 printf("Warning: Unimplemented: Compare types '");
173 tdata_item_print(a);
174 printf("' and '");
175 tdata_item_print(b);
176 printf("'.\n");
177 return b_true;
178 }
179}
180
181/** Substitute type variables in a type item.
182 *
183 * This is the second part of generic type application. In the first part
184 * obtained a TVV using stype_titem_to_tvv() and in this second part we
185 * actually substitute type variables in a type item for their values.
186 * @a tvv must contain all variables referenced in @a ti.
187 *
188 * @param ti Type item to substitute into.
189 * @param tvv Type variable valuation (values of type variables).
190 * @param res Place to store pointer to new type item.
191 */
192void tdata_item_subst(tdata_item_t *ti, tdata_tvv_t *tvv, tdata_item_t **res)
193{
194 switch (ti->tic) {
195 case tic_tprimitive:
196 tdata_item_subst_tprimitive(ti->u.tprimitive, tvv, res);
197 break;
198 case tic_tobject:
199 tdata_item_subst_tobject(ti->u.tobject, tvv, res);
200 break;
201 case tic_tarray:
202 tdata_item_subst_tarray(ti->u.tarray, tvv, res);
203 break;
204 case tic_tdeleg:
205 tdata_item_subst_tdeleg(ti->u.tdeleg, tvv, res);
206 break;
207 case tic_tebase:
208 tdata_item_subst_tebase(ti->u.tebase, tvv, res);
209 break;
210 case tic_tenum:
211 tdata_item_subst_tenum(ti->u.tenum, tvv, res);
212 break;
213 case tic_tfun:
214 tdata_item_subst_tfun(ti->u.tfun, tvv, res);
215 break;
216 case tic_tvref:
217 tdata_item_subst_tvref(ti->u.tvref, tvv, res);
218 break;
219 case tic_ignore:
220 *res = tdata_item_new(tic_ignore);
221 }
222}
223
224/** Substitute type variables in a primitive type item.
225 *
226 * @param torig Type item to substitute into.
227 * @param tvv Type variable valuation (values of type variables).
228 * @param res Place to store pointer to new type item.
229 */
230static void tdata_item_subst_tprimitive(tdata_primitive_t *torig,
231 tdata_tvv_t *tvv, tdata_item_t **res)
232{
233 tdata_primitive_t *tnew;
234
235 (void) tvv;
236
237 /* Plain copy */
238 tnew = tdata_primitive_new(torig->tpc);
239 *res = tdata_item_new(tic_tprimitive);
240 (*res)->u.tprimitive = tnew;
241}
242
243/** Substitute type variables in an object type item.
244 *
245 * @param torig Type item to substitute into.
246 * @param tvv Type variable valuation (values of type variables).
247 * @param res Place to store pointer to new type item.
248 */
249static void tdata_item_subst_tobject(tdata_object_t *torig, tdata_tvv_t *tvv,
250 tdata_item_t **res)
251{
252 tdata_object_t *tnew;
253 list_node_t *targ_n;
254 tdata_item_t *targ;
255 tdata_item_t *new_targ;
256
257 /* Copy static ref flag and base CSI. */
258 tnew = tdata_object_new();
259 tnew->static_ref = torig->static_ref;
260 tnew->csi = torig->csi;
261 list_init(&tnew->targs);
262
263 /* Substitute arguments */
264 targ_n = list_first(&torig->targs);
265 while (targ_n != NULL) {
266 targ = list_node_data(targ_n, tdata_item_t *);
267 tdata_item_subst(targ, tvv, &new_targ);
268 list_append(&tnew->targs, new_targ);
269
270 targ_n = list_next(&torig->targs, targ_n);
271 }
272
273 *res = tdata_item_new(tic_tobject);
274 (*res)->u.tobject = tnew;
275}
276
277/** Substitute type variables in an array type item.
278 *
279 * @param torig Type item to substitute into.
280 * @param tvv Type variable valuation (values of type variables).
281 * @param res Place to store pointer to new type item.
282 */
283static void tdata_item_subst_tarray(tdata_array_t *torig, tdata_tvv_t *tvv,
284 tdata_item_t **res)
285{
286 tdata_array_t *tnew;
287 list_node_t *ext_n;
288 stree_expr_t *extent;
289
290 tnew = tdata_array_new();
291
292 /* Substitute base type */
293 tdata_item_subst(torig->base_ti, tvv, &tnew->base_ti);
294
295 /* Copy rank and extents */
296 tnew->rank = torig->rank;
297 list_init(&tnew->extents);
298
299 ext_n = list_first(&torig->extents);
300 while (ext_n != NULL) {
301 extent = list_node_data(ext_n, stree_expr_t *);
302 list_append(&tnew->extents, extent);
303
304 ext_n = list_next(&tnew->extents, ext_n);
305 }
306
307 *res = tdata_item_new(tic_tarray);
308 (*res)->u.tarray = tnew;
309}
310
311/** Substitute type variables in a delegate type item.
312 *
313 * @param torig Type item to substitute into.
314 * @param tvv Type variable valuation (values of type variables).
315 * @param res Place to store pointer to new type item.
316 */
317static void tdata_item_subst_tdeleg(tdata_deleg_t *torig, tdata_tvv_t *tvv,
318 tdata_item_t **res)
319{
320 tdata_deleg_t *tnew;
321
322 tnew = tdata_deleg_new();
323 tnew->deleg = torig->deleg;
324 tdata_item_subst_fun_sig(torig->tsig, tvv, &tnew->tsig);
325
326 *res = tdata_item_new(tic_tdeleg);
327 (*res)->u.tdeleg = tnew;
328}
329
330/** Substitute type variables in a enum-base type item.
331 *
332 * @param torig Type item to substitute into.
333 * @param tvv Type variable valuation (values of type variables).
334 * @param res Place to store pointer to new type item.
335 */
336static void tdata_item_subst_tebase(tdata_ebase_t *tebase,
337 tdata_tvv_t *tvv, tdata_item_t **res)
338{
339 tdata_ebase_t *tnew;
340
341 (void) tvv;
342
343 /* Plain copy */
344 tnew = tdata_ebase_new();
345 tnew->enum_d = tebase->enum_d;
346
347 *res = tdata_item_new(tic_tebase);
348 (*res)->u.tebase = tnew;
349}
350
351/** Substitute type variables in a enum type item.
352 *
353 * @param torig Type item to substitute into.
354 * @param tvv Type variable valuation (values of type variables).
355 * @param res Place to store pointer to new type item.
356 */
357static void tdata_item_subst_tenum(tdata_enum_t *tenum,
358 tdata_tvv_t *tvv, tdata_item_t **res)
359{
360 tdata_enum_t *tnew;
361
362 (void) tvv;
363
364 /* Plain copy */
365 tnew = tdata_enum_new();
366 tnew->enum_d = tenum->enum_d;
367
368 *res = tdata_item_new(tic_tenum);
369 (*res)->u.tenum = tnew;
370}
371
372/** Substitute type variables in a functional type item.
373 *
374 * @param torig Type item to substitute into.
375 * @param tvv Type variable valuation (values of type variables).
376 * @param res Place to store pointer to new type item.
377 */
378static void tdata_item_subst_tfun(tdata_fun_t *torig, tdata_tvv_t *tvv,
379 tdata_item_t **res)
380{
381 tdata_fun_t *tnew;
382
383 tnew = tdata_fun_new();
384 tdata_item_subst_fun_sig(torig->tsig, tvv, &tnew->tsig);
385
386 *res = tdata_item_new(tic_tfun);
387 (*res)->u.tfun = tnew;
388}
389
390/** Substitute type variables in a type-variable reference item.
391 *
392 * @param torig Type item to substitute into.
393 * @param tvv Type variable valuation (values of type variables).
394 * @param res Place to store pointer to new type item.
395 */
396static void tdata_item_subst_tvref(tdata_vref_t *tvref, tdata_tvv_t *tvv,
397 tdata_item_t **res)
398{
399 tdata_item_t *ti_new;
400
401 ti_new = tdata_tvv_get_val(tvv, tvref->targ->name->sid);
402 assert(ti_new != NULL);
403
404 /* XXX Might be better to clone here. */
405 *res = ti_new;
406}
407
408/** Substitute type variables in a function signature type fragment.
409 *
410 * @param torig Type item to substitute into.
411 * @param tvv Type variable valuation (values of type variables).
412 * @param res Place to store pointer to new type item.
413 */
414static void tdata_item_subst_fun_sig(tdata_fun_sig_t *torig, tdata_tvv_t *tvv,
415 tdata_fun_sig_t **res)
416{
417 tdata_fun_sig_t *tnew;
418 list_node_t *arg_n;
419 tdata_item_t *arg_ti;
420 tdata_item_t *narg_ti = NULL;
421
422 tnew = tdata_fun_sig_new();
423
424 /* Substitute type of each argument */
425 list_init(&tnew->arg_ti);
426 arg_n = list_first(&torig->arg_ti);
427 while (arg_n != NULL) {
428 arg_ti = list_node_data(arg_n, tdata_item_t *);
429
430 /* XXX Because of overloaded Builtin.WriteLine */
431 if (arg_ti == NULL)
432 narg_ti = NULL;
433 else
434 tdata_item_subst(arg_ti, tvv, &narg_ti);
435
436 list_append(&tnew->arg_ti, narg_ti);
437
438 arg_n = list_next(&torig->arg_ti, arg_n);
439 }
440
441 /* Substitute type of variadic argument */
442 if (torig->varg_ti != NULL)
443 tdata_item_subst(torig->varg_ti, tvv, &tnew->varg_ti);
444
445 /* Substitute return type */
446 if (torig->rtype != NULL)
447 tdata_item_subst(torig->rtype, tvv, &tnew->rtype);
448
449 *res = tnew;
450}
451
452
453/** Print type item.
454 *
455 * @param titem Type item
456 */
457void tdata_item_print(tdata_item_t *titem)
458{
459 if (titem == NULL) {
460 printf("none");
461 return;
462 }
463
464 switch (titem->tic) {
465 case tic_tprimitive:
466 tdata_tprimitive_print(titem->u.tprimitive);
467 break;
468 case tic_tobject:
469 tdata_tobject_print(titem->u.tobject);
470 break;
471 case tic_tarray:
472 tdata_tarray_print(titem->u.tarray);
473 break;
474 case tic_tdeleg:
475 tdata_tdeleg_print(titem->u.tdeleg);
476 break;
477 case tic_tebase:
478 tdata_tebase_print(titem->u.tebase);
479 break;
480 case tic_tenum:
481 tdata_tenum_print(titem->u.tenum);
482 break;
483 case tic_tfun:
484 tdata_tfun_print(titem->u.tfun);
485 break;
486 case tic_tvref:
487 tdata_tvref_print(titem->u.tvref);
488 break;
489 case tic_ignore:
490 printf("ignore");
491 break;
492 }
493}
494
495/** Print primitive type item.
496 *
497 * @param tprimitive Primitive type item
498 */
499static void tdata_tprimitive_print(tdata_primitive_t *tprimitive)
500{
501 switch (tprimitive->tpc) {
502 case tpc_bool:
503 printf("bool");
504 break;
505 case tpc_char:
506 printf("char");
507 break;
508 case tpc_int:
509 printf("int");
510 break;
511 case tpc_nil:
512 printf("nil");
513 break;
514 case tpc_string:
515 printf("string");
516 break;
517 case tpc_resource:
518 printf("resource");
519 break;
520 }
521}
522
523/** Print object type item.
524 *
525 * @param tobject Object type item
526 */
527static void tdata_tobject_print(tdata_object_t *tobject)
528{
529 stree_symbol_t *csi_sym;
530 list_node_t *arg_n;
531 tdata_item_t *arg;
532
533 csi_sym = csi_to_symbol(tobject->csi);
534 assert(csi_sym != NULL);
535 symbol_print_fqn(csi_sym);
536
537 arg_n = list_first(&tobject->targs);
538 while (arg_n != NULL) {
539 arg = list_node_data(arg_n, tdata_item_t *);
540 putchar('/');
541 tdata_item_print(arg);
542 arg_n = list_next(&tobject->targs, arg_n);
543 }
544}
545
546/** Print array type item.
547 *
548 * @param tarray Array type item
549 */
550static void tdata_tarray_print(tdata_array_t *tarray)
551{
552 int i;
553
554 tdata_item_print(tarray->base_ti);
555
556 printf("[");
557 for (i = 0; i < tarray->rank - 1; ++i)
558 printf(",");
559 printf("]");
560}
561
562/** Print delegate type item.
563 *
564 * @param tdeleg Delegate type item
565 */
566static void tdata_tdeleg_print(tdata_deleg_t *tdeleg)
567{
568 stree_symbol_t *deleg_sym;
569
570 deleg_sym = deleg_to_symbol(tdeleg->deleg);
571 symbol_print_fqn(deleg_sym);
572}
573
574/** Print enum-base type item.
575 *
576 * @param tebase Enum-base type item
577 */
578static void tdata_tebase_print(tdata_ebase_t *tebase)
579{
580 stree_symbol_t *enum_sym;
581
582 enum_sym = enum_to_symbol(tebase->enum_d);
583
584 printf("typeref(");
585 symbol_print_fqn(enum_sym);
586 printf(")");
587}
588
589/** Print enum type item.
590 *
591 * @param tenum Enum type item
592 */
593static void tdata_tenum_print(tdata_enum_t *tenum)
594{
595 stree_symbol_t *enum_sym;
596
597 enum_sym = enum_to_symbol(tenum->enum_d);
598 symbol_print_fqn(enum_sym);
599}
600
601/** Print function type item.
602 *
603 * @param tfun Function type item
604 */
605static void tdata_tfun_print(tdata_fun_t *tfun)
606{
607 list_node_t *arg_n;
608 tdata_item_t *arg_ti;
609 bool_t first;
610
611 printf("fun(");
612
613 arg_n = list_first(&tfun->tsig->arg_ti);
614 first = b_true;
615 while (arg_n != NULL) {
616 if (first == b_false)
617 printf("; ");
618 else
619 first = b_false;
620
621 arg_ti = list_node_data(arg_n, tdata_item_t *);
622 tdata_item_print(arg_ti);
623
624 arg_n = list_next(&tfun->tsig->arg_ti, arg_n);
625 }
626
627 printf(") : ");
628 tdata_item_print(tfun->tsig->rtype);
629}
630
631/** Print type variable reference type item.
632 *
633 * @param tvref Type variable reference type item
634 */
635static void tdata_tvref_print(tdata_vref_t *tvref)
636{
637 printf("%s", strtab_get_str(tvref->targ->name->sid));
638}
639
640/** Allocate new type item.
641 *
642 * @param tic Type item class
643 * @return New type item
644 */
645tdata_item_t *tdata_item_new(titem_class_t tic)
646{
647 tdata_item_t *titem;
648
649 titem = calloc(1, sizeof(tdata_item_t));
650 if (titem == NULL) {
651 printf("Memory allocation failed.\n");
652 exit(1);
653 }
654
655 titem->tic = tic;
656 return titem;
657}
658
659/** Allocate new array type item.
660 *
661 * @return New array type item
662 */
663tdata_array_t *tdata_array_new(void)
664{
665 tdata_array_t *tarray;
666
667 tarray = calloc(1, sizeof(tdata_array_t));
668 if (tarray == NULL) {
669 printf("Memory allocation failed.\n");
670 exit(1);
671 }
672
673 return tarray;
674}
675
676/** Allocate new object type item.
677 *
678 * @return New object type item
679 */
680tdata_object_t *tdata_object_new(void)
681{
682 tdata_object_t *tobject;
683
684 tobject = calloc(1, sizeof(tdata_object_t));
685 if (tobject == NULL) {
686 printf("Memory allocation failed.\n");
687 exit(1);
688 }
689
690 return tobject;
691}
692
693/** Allocate new primitive type item.
694 *
695 * @return New primitive type item
696 */
697tdata_primitive_t *tdata_primitive_new(tprimitive_class_t tpc)
698{
699 tdata_primitive_t *tprimitive;
700
701 tprimitive = calloc(1, sizeof(tdata_primitive_t));
702 if (tprimitive == NULL) {
703 printf("Memory allocation failed.\n");
704 exit(1);
705 }
706
707 tprimitive->tpc = tpc;
708 return tprimitive;
709}
710
711/** Allocate new delegate type item.
712 *
713 * @return New function type item
714 */
715tdata_deleg_t *tdata_deleg_new(void)
716{
717 tdata_deleg_t *tdeleg;
718
719 tdeleg = calloc(1, sizeof(tdata_deleg_t));
720 if (tdeleg == NULL) {
721 printf("Memory allocation failed.\n");
722 exit(1);
723 }
724
725 return tdeleg;
726}
727
728/** Allocate new enum-base type item.
729 *
730 * @return New enum type item
731 */
732tdata_ebase_t *tdata_ebase_new(void)
733{
734 tdata_ebase_t *tebase;
735
736 tebase = calloc(1, sizeof(tdata_ebase_t));
737 if (tebase == NULL) {
738 printf("Memory allocation failed.\n");
739 exit(1);
740 }
741
742 return tebase;
743}
744
745/** Allocate new enum type item.
746 *
747 * @return New enum type item
748 */
749tdata_enum_t *tdata_enum_new(void)
750{
751 tdata_enum_t *tenum;
752
753 tenum = calloc(1, sizeof(tdata_enum_t));
754 if (tenum == NULL) {
755 printf("Memory allocation failed.\n");
756 exit(1);
757 }
758
759 return tenum;
760}
761
762/** Allocate new functional type item.
763 *
764 * @return New function type item
765 */
766tdata_fun_t *tdata_fun_new(void)
767{
768 tdata_fun_t *tfun;
769
770 tfun = calloc(1, sizeof(tdata_fun_t));
771 if (tfun == NULL) {
772 printf("Memory allocation failed.\n");
773 exit(1);
774 }
775
776 return tfun;
777}
778
779/** Allocate new type variable reference type item.
780 *
781 * @return New type variable reference type item
782 */
783tdata_vref_t *tdata_vref_new(void)
784{
785 tdata_vref_t *tvref;
786
787 tvref = calloc(1, sizeof(tdata_vref_t));
788 if (tvref == NULL) {
789 printf("Memory allocation failed.\n");
790 exit(1);
791 }
792
793 return tvref;
794}
795
796/** Allocate new function signature type fragment.
797 *
798 * @return New function signature type fragment
799 */
800tdata_fun_sig_t *tdata_fun_sig_new(void)
801{
802 tdata_fun_sig_t *tfun_sig;
803
804 tfun_sig = calloc(1, sizeof(tdata_fun_sig_t));
805 if (tfun_sig == NULL) {
806 printf("Memory allocation failed.\n");
807 exit(1);
808 }
809
810 return tfun_sig;
811}
812
813/** Create a new type variable valuation.
814 *
815 * @retrun New type variable valuation
816 */
817tdata_tvv_t *tdata_tvv_new(void)
818{
819 tdata_tvv_t *tvv;
820
821 tvv = calloc(1, sizeof(tdata_tvv_t));
822 if (tvv == NULL) {
823 printf("Memory allocation failed.\n");
824 exit(1);
825 }
826
827 return tvv;
828}
829
830/** Get type variable value.
831 *
832 * Looks up value of the variable with name SID @a name in type
833 * variable valuation @a tvv.
834 *
835 * @param tvv Type variable valuation
836 * @param name Name of the variable (SID)
837 * @return Value of the type variable (type item) or @c NULL
838 * if not defined in @a tvv
839 */
840tdata_item_t *tdata_tvv_get_val(tdata_tvv_t *tvv, sid_t name)
841{
842 return (tdata_item_t *)intmap_get(&tvv->tvv, name);
843}
844
845/** Set type variable value.
846 *
847 * Sets the value of variable with name SID @a name in type variable
848 * valuation @a tvv to the value @a tvalue.
849 *
850 * @param tvv Type variable valuation
851 * @param name Name of the variable (SID)
852 * @param tvalue Value to set (type item) or @c NULL to unset
853 */
854void tdata_tvv_set_val(tdata_tvv_t *tvv, sid_t name, tdata_item_t *tvalue)
855{
856 intmap_set(&tvv->tvv, name, tvalue);
857}
Note: See TracBrowser for help on using the repository browser.