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

Last change on this file was 09ab0a9a, checked in by Jiri Svoboda <jiri@…>, 7 years ago

Fix vertical spacing with new Ccheck revision.

  • 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/** Print type item.
453 *
454 * @param titem Type item
455 */
456void tdata_item_print(tdata_item_t *titem)
457{
458 if (titem == NULL) {
459 printf("none");
460 return;
461 }
462
463 switch (titem->tic) {
464 case tic_tprimitive:
465 tdata_tprimitive_print(titem->u.tprimitive);
466 break;
467 case tic_tobject:
468 tdata_tobject_print(titem->u.tobject);
469 break;
470 case tic_tarray:
471 tdata_tarray_print(titem->u.tarray);
472 break;
473 case tic_tdeleg:
474 tdata_tdeleg_print(titem->u.tdeleg);
475 break;
476 case tic_tebase:
477 tdata_tebase_print(titem->u.tebase);
478 break;
479 case tic_tenum:
480 tdata_tenum_print(titem->u.tenum);
481 break;
482 case tic_tfun:
483 tdata_tfun_print(titem->u.tfun);
484 break;
485 case tic_tvref:
486 tdata_tvref_print(titem->u.tvref);
487 break;
488 case tic_ignore:
489 printf("ignore");
490 break;
491 }
492}
493
494/** Print primitive type item.
495 *
496 * @param tprimitive Primitive type item
497 */
498static void tdata_tprimitive_print(tdata_primitive_t *tprimitive)
499{
500 switch (tprimitive->tpc) {
501 case tpc_bool:
502 printf("bool");
503 break;
504 case tpc_char:
505 printf("char");
506 break;
507 case tpc_int:
508 printf("int");
509 break;
510 case tpc_nil:
511 printf("nil");
512 break;
513 case tpc_string:
514 printf("string");
515 break;
516 case tpc_resource:
517 printf("resource");
518 break;
519 }
520}
521
522/** Print object type item.
523 *
524 * @param tobject Object type item
525 */
526static void tdata_tobject_print(tdata_object_t *tobject)
527{
528 stree_symbol_t *csi_sym;
529 list_node_t *arg_n;
530 tdata_item_t *arg;
531
532 csi_sym = csi_to_symbol(tobject->csi);
533 assert(csi_sym != NULL);
534 symbol_print_fqn(csi_sym);
535
536 arg_n = list_first(&tobject->targs);
537 while (arg_n != NULL) {
538 arg = list_node_data(arg_n, tdata_item_t *);
539 putchar('/');
540 tdata_item_print(arg);
541 arg_n = list_next(&tobject->targs, arg_n);
542 }
543}
544
545/** Print array type item.
546 *
547 * @param tarray Array type item
548 */
549static void tdata_tarray_print(tdata_array_t *tarray)
550{
551 int i;
552
553 tdata_item_print(tarray->base_ti);
554
555 printf("[");
556 for (i = 0; i < tarray->rank - 1; ++i)
557 printf(",");
558 printf("]");
559}
560
561/** Print delegate type item.
562 *
563 * @param tdeleg Delegate type item
564 */
565static void tdata_tdeleg_print(tdata_deleg_t *tdeleg)
566{
567 stree_symbol_t *deleg_sym;
568
569 deleg_sym = deleg_to_symbol(tdeleg->deleg);
570 symbol_print_fqn(deleg_sym);
571}
572
573/** Print enum-base type item.
574 *
575 * @param tebase Enum-base type item
576 */
577static void tdata_tebase_print(tdata_ebase_t *tebase)
578{
579 stree_symbol_t *enum_sym;
580
581 enum_sym = enum_to_symbol(tebase->enum_d);
582
583 printf("typeref(");
584 symbol_print_fqn(enum_sym);
585 printf(")");
586}
587
588/** Print enum type item.
589 *
590 * @param tenum Enum type item
591 */
592static void tdata_tenum_print(tdata_enum_t *tenum)
593{
594 stree_symbol_t *enum_sym;
595
596 enum_sym = enum_to_symbol(tenum->enum_d);
597 symbol_print_fqn(enum_sym);
598}
599
600/** Print function type item.
601 *
602 * @param tfun Function type item
603 */
604static void tdata_tfun_print(tdata_fun_t *tfun)
605{
606 list_node_t *arg_n;
607 tdata_item_t *arg_ti;
608 bool_t first;
609
610 printf("fun(");
611
612 arg_n = list_first(&tfun->tsig->arg_ti);
613 first = b_true;
614 while (arg_n != NULL) {
615 if (first == b_false)
616 printf("; ");
617 else
618 first = b_false;
619
620 arg_ti = list_node_data(arg_n, tdata_item_t *);
621 tdata_item_print(arg_ti);
622
623 arg_n = list_next(&tfun->tsig->arg_ti, arg_n);
624 }
625
626 printf(") : ");
627 tdata_item_print(tfun->tsig->rtype);
628}
629
630/** Print type variable reference type item.
631 *
632 * @param tvref Type variable reference type item
633 */
634static void tdata_tvref_print(tdata_vref_t *tvref)
635{
636 printf("%s", strtab_get_str(tvref->targ->name->sid));
637}
638
639/** Allocate new type item.
640 *
641 * @param tic Type item class
642 * @return New type item
643 */
644tdata_item_t *tdata_item_new(titem_class_t tic)
645{
646 tdata_item_t *titem;
647
648 titem = calloc(1, sizeof(tdata_item_t));
649 if (titem == NULL) {
650 printf("Memory allocation failed.\n");
651 exit(1);
652 }
653
654 titem->tic = tic;
655 return titem;
656}
657
658/** Allocate new array type item.
659 *
660 * @return New array type item
661 */
662tdata_array_t *tdata_array_new(void)
663{
664 tdata_array_t *tarray;
665
666 tarray = calloc(1, sizeof(tdata_array_t));
667 if (tarray == NULL) {
668 printf("Memory allocation failed.\n");
669 exit(1);
670 }
671
672 return tarray;
673}
674
675/** Allocate new object type item.
676 *
677 * @return New object type item
678 */
679tdata_object_t *tdata_object_new(void)
680{
681 tdata_object_t *tobject;
682
683 tobject = calloc(1, sizeof(tdata_object_t));
684 if (tobject == NULL) {
685 printf("Memory allocation failed.\n");
686 exit(1);
687 }
688
689 return tobject;
690}
691
692/** Allocate new primitive type item.
693 *
694 * @return New primitive type item
695 */
696tdata_primitive_t *tdata_primitive_new(tprimitive_class_t tpc)
697{
698 tdata_primitive_t *tprimitive;
699
700 tprimitive = calloc(1, sizeof(tdata_primitive_t));
701 if (tprimitive == NULL) {
702 printf("Memory allocation failed.\n");
703 exit(1);
704 }
705
706 tprimitive->tpc = tpc;
707 return tprimitive;
708}
709
710/** Allocate new delegate type item.
711 *
712 * @return New function type item
713 */
714tdata_deleg_t *tdata_deleg_new(void)
715{
716 tdata_deleg_t *tdeleg;
717
718 tdeleg = calloc(1, sizeof(tdata_deleg_t));
719 if (tdeleg == NULL) {
720 printf("Memory allocation failed.\n");
721 exit(1);
722 }
723
724 return tdeleg;
725}
726
727/** Allocate new enum-base type item.
728 *
729 * @return New enum type item
730 */
731tdata_ebase_t *tdata_ebase_new(void)
732{
733 tdata_ebase_t *tebase;
734
735 tebase = calloc(1, sizeof(tdata_ebase_t));
736 if (tebase == NULL) {
737 printf("Memory allocation failed.\n");
738 exit(1);
739 }
740
741 return tebase;
742}
743
744/** Allocate new enum type item.
745 *
746 * @return New enum type item
747 */
748tdata_enum_t *tdata_enum_new(void)
749{
750 tdata_enum_t *tenum;
751
752 tenum = calloc(1, sizeof(tdata_enum_t));
753 if (tenum == NULL) {
754 printf("Memory allocation failed.\n");
755 exit(1);
756 }
757
758 return tenum;
759}
760
761/** Allocate new functional type item.
762 *
763 * @return New function type item
764 */
765tdata_fun_t *tdata_fun_new(void)
766{
767 tdata_fun_t *tfun;
768
769 tfun = calloc(1, sizeof(tdata_fun_t));
770 if (tfun == NULL) {
771 printf("Memory allocation failed.\n");
772 exit(1);
773 }
774
775 return tfun;
776}
777
778/** Allocate new type variable reference type item.
779 *
780 * @return New type variable reference type item
781 */
782tdata_vref_t *tdata_vref_new(void)
783{
784 tdata_vref_t *tvref;
785
786 tvref = calloc(1, sizeof(tdata_vref_t));
787 if (tvref == NULL) {
788 printf("Memory allocation failed.\n");
789 exit(1);
790 }
791
792 return tvref;
793}
794
795/** Allocate new function signature type fragment.
796 *
797 * @return New function signature type fragment
798 */
799tdata_fun_sig_t *tdata_fun_sig_new(void)
800{
801 tdata_fun_sig_t *tfun_sig;
802
803 tfun_sig = calloc(1, sizeof(tdata_fun_sig_t));
804 if (tfun_sig == NULL) {
805 printf("Memory allocation failed.\n");
806 exit(1);
807 }
808
809 return tfun_sig;
810}
811
812/** Create a new type variable valuation.
813 *
814 * @retrun New type variable valuation
815 */
816tdata_tvv_t *tdata_tvv_new(void)
817{
818 tdata_tvv_t *tvv;
819
820 tvv = calloc(1, sizeof(tdata_tvv_t));
821 if (tvv == NULL) {
822 printf("Memory allocation failed.\n");
823 exit(1);
824 }
825
826 return tvv;
827}
828
829/** Get type variable value.
830 *
831 * Looks up value of the variable with name SID @a name in type
832 * variable valuation @a tvv.
833 *
834 * @param tvv Type variable valuation
835 * @param name Name of the variable (SID)
836 * @return Value of the type variable (type item) or @c NULL
837 * if not defined in @a tvv
838 */
839tdata_item_t *tdata_tvv_get_val(tdata_tvv_t *tvv, sid_t name)
840{
841 return (tdata_item_t *)intmap_get(&tvv->tvv, name);
842}
843
844/** Set type variable value.
845 *
846 * Sets the value of variable with name SID @a name in type variable
847 * valuation @a tvv to the value @a tvalue.
848 *
849 * @param tvv Type variable valuation
850 * @param name Name of the variable (SID)
851 * @param tvalue Value to set (type item) or @c NULL to unset
852 */
853void tdata_tvv_set_val(tdata_tvv_t *tvv, sid_t name, tdata_item_t *tvalue)
854{
855 intmap_set(&tvv->tvv, name, tvalue);
856}
Note: See TracBrowser for help on using the repository browser.