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

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

Update SBI to rev. 184.

  • Property mode set to 100644
File size: 6.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 "list.h"
34#include "mytypes.h"
35#include "stree.h"
36#include "symbol.h"
37
38#include "tdata.h"
39
40static void tdata_tprimitive_print(tdata_primitive_t *tprimitive);
41static void tdata_tobject_print(tdata_object_t *tobject);
42static void tdata_tarray_print(tdata_array_t *tarray);
43static void tdata_tfun_print(tdata_fun_t *tfun);
44
45/** Determine if CSI @a a is derived from CSI described by type item @a tb. */
46bool_t tdata_is_csi_derived_from_ti(stree_csi_t *a, tdata_item_t *tb)
47{
48 bool_t res;
49
50 switch (tb->tic) {
51 case tic_tobject:
52 res = stree_is_csi_derived_from_csi(a, tb->u.tobject->csi);
53 break;
54 default:
55 printf("Error: Base type is not a CSI.\n");
56 exit(1);
57 }
58
59 return res;
60}
61
62/**
63 * Determine if CSI described by type item @a a is derived from CSI described
64 * by type item @a tb.
65 */
66bool_t tdata_is_ti_derived_from_ti(tdata_item_t *ta, tdata_item_t *tb)
67{
68 bool_t res;
69
70 switch (ta->tic) {
71 case tic_tobject:
72 res = tdata_is_csi_derived_from_ti(ta->u.tobject->csi, tb);
73 break;
74 default:
75 printf("Error: Derived type is not a CSI.\n");
76 exit(1);
77 }
78
79 return res;
80}
81
82/** Determine if two type items are equal (i.e. describe the same type). */
83bool_t tdata_item_equal(tdata_item_t *a, tdata_item_t *b)
84{
85 /*
86 * Special case: Nil vs. object
87 *
88 * XXX Type of @c Nil should probably be @c object to avoid this
89 * madness.
90 */
91 if (a->tic == tic_tprimitive && a->u.tprimitive->tpc == tpc_nil) {
92 if (b->tic == tic_tobject)
93 return b_true;
94 } else if (b->tic == tic_tprimitive && b->u.tprimitive->tpc == tpc_nil) {
95 if (a->tic == tic_tobject)
96 return b_true;
97 }
98
99 if (a->tic != b->tic)
100 return b_false;
101
102 switch (a->tic) {
103 case tic_tprimitive:
104 /* Check if both have the same tprimitive class. */
105 return (a->u.tprimitive->tpc == b->u.tprimitive->tpc);
106 case tic_tobject:
107 /* Check if both use the same CSI definition. */
108 return (a->u.tobject->csi == b->u.tobject->csi);
109 case tic_tarray:
110 /* Compare rank and base type. */
111 if (a->u.tarray->rank != b->u.tarray->rank)
112 return b_false;
113
114 return tdata_item_equal(a->u.tarray->base_ti,
115 b->u.tarray->base_ti);
116 default:
117 printf("Warning: Unimplemented: Compare types '");
118 tdata_item_print(a);
119 printf("' and '");
120 tdata_item_print(b);
121 printf("'.\n");
122 return b_true;
123 }
124}
125
126/** Print type item. */
127void tdata_item_print(tdata_item_t *titem)
128{
129 if (titem == NULL) {
130 printf("none");
131 return;
132 }
133
134 switch (titem->tic) {
135 case tic_tprimitive:
136 tdata_tprimitive_print(titem->u.tprimitive);
137 break;
138 case tic_tobject:
139 tdata_tobject_print(titem->u.tobject);
140 break;
141 case tic_tarray:
142 tdata_tarray_print(titem->u.tarray);
143 break;
144 case tic_tfun:
145 tdata_tfun_print(titem->u.tfun);
146 break;
147 case tic_ignore:
148 printf("ignore");
149 break;
150 }
151}
152
153static void tdata_tprimitive_print(tdata_primitive_t *tprimitive)
154{
155 switch (tprimitive->tpc) {
156 case tpc_bool: printf("bool"); break;
157 case tpc_char: printf("char"); break;
158 case tpc_int: printf("int"); break;
159 case tpc_nil: printf("nil"); break;
160 case tpc_string: printf("string"); break;
161 case tpc_resource: printf("resource"); break;
162 }
163}
164
165static void tdata_tobject_print(tdata_object_t *tobject)
166{
167 stree_symbol_t *csi_sym;
168 list_node_t *arg_n;
169 tdata_item_t *arg;
170
171 csi_sym = csi_to_symbol(tobject->csi);
172 assert(csi_sym != NULL);
173 symbol_print_fqn(csi_sym);
174
175 arg_n = list_first(&tobject->targs);
176 while (arg_n != NULL) {
177 arg = list_node_data(arg_n, tdata_item_t *);
178 putchar('/');
179 tdata_item_print(arg);
180 arg_n = list_next(&tobject->targs, arg_n);
181 }
182}
183
184static void tdata_tarray_print(tdata_array_t *tarray)
185{
186 int i;
187
188 tdata_item_print(tarray->base_ti);
189
190 printf("[");
191 for (i = 0; i < tarray->rank - 1; ++i)
192 printf(",");
193 printf("]");
194}
195
196static void tdata_tfun_print(tdata_fun_t *tfun)
197{
198 (void) tfun;
199 printf("unimplemented(fun)");
200}
201
202tdata_item_t *tdata_item_new(titem_class_t tic)
203{
204 tdata_item_t *titem;
205
206 titem = calloc(1, sizeof(tdata_item_t));
207 if (titem == NULL) {
208 printf("Memory allocation failed.\n");
209 exit(1);
210 }
211
212 titem->tic = tic;
213 return titem;
214}
215
216tdata_array_t *tdata_array_new(void)
217{
218 tdata_array_t *tarray;
219
220 tarray = calloc(1, sizeof(tdata_array_t));
221 if (tarray == NULL) {
222 printf("Memory allocation failed.\n");
223 exit(1);
224 }
225
226 return tarray;
227}
228
229tdata_object_t *tdata_object_new(void)
230{
231 tdata_object_t *tobject;
232
233 tobject = calloc(1, sizeof(tdata_object_t));
234 if (tobject == NULL) {
235 printf("Memory allocation failed.\n");
236 exit(1);
237 }
238
239 return tobject;
240}
241
242tdata_primitive_t *tdata_primitive_new(tprimitive_class_t tpc)
243{
244 tdata_primitive_t *tprimitive;
245
246 tprimitive = calloc(1, sizeof(tdata_primitive_t));
247 if (tprimitive == NULL) {
248 printf("Memory allocation failed.\n");
249 exit(1);
250 }
251
252 tprimitive->tpc = tpc;
253 return tprimitive;
254}
255
256tdata_fun_t *tdata_fun_new(void)
257{
258 tdata_fun_t *tfun;
259
260 tfun = calloc(1, sizeof(tdata_fun_t));
261 if (tfun == NULL) {
262 printf("Memory allocation failed.\n");
263 exit(1);
264 }
265
266 return tfun;
267}
Note: See TracBrowser for help on using the repository browser.