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

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

Update SBI to rev. 174.

  • Property mode set to 100644
File size: 6.2 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 "mytypes.h"
34#include "stree.h"
35#include "symbol.h"
36
37#include "tdata.h"
38
39static void tdata_tprimitive_print(tdata_primitive_t *tprimitive);
40static void tdata_tobject_print(tdata_object_t *tobject);
41static void tdata_tarray_print(tdata_array_t *tarray);
42static void tdata_tgeneric_print(tdata_generic_t *tgeneric);
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_tgeneric:
145 tdata_tgeneric_print(titem->u.tgeneric);
146 break;
147 case tic_tfun:
148 tdata_tfun_print(titem->u.tfun);
149 break;
150 case tic_ignore:
151 printf("ignore");
152 break;
153 }
154}
155
156static void tdata_tprimitive_print(tdata_primitive_t *tprimitive)
157{
158 switch (tprimitive->tpc) {
159 case tpc_int: printf("int"); break;
160 case tpc_nil: printf("nil"); break;
161 case tpc_string: printf("string"); break;
162 case tpc_resource: printf("resource"); break;
163 }
164}
165
166static void tdata_tobject_print(tdata_object_t *tobject)
167{
168 stree_symbol_t *csi_sym;
169
170 csi_sym = csi_to_symbol(tobject->csi);
171 assert(csi_sym != NULL);
172 symbol_print_fqn(csi_sym);
173}
174
175static void tdata_tarray_print(tdata_array_t *tarray)
176{
177 int i;
178
179 tdata_item_print(tarray->base_ti);
180
181 printf("[");
182 for (i = 0; i < tarray->rank - 1; ++i)
183 printf(",");
184 printf("]");
185}
186
187static void tdata_tgeneric_print(tdata_generic_t *tgeneric)
188{
189 (void) tgeneric;
190 printf("unimplemented(generic)");
191}
192
193static void tdata_tfun_print(tdata_fun_t *tfun)
194{
195 (void) tfun;
196 printf("unimplemented(fun)");
197}
198
199tdata_item_t *tdata_item_new(titem_class_t tic)
200{
201 tdata_item_t *titem;
202
203 titem = calloc(1, sizeof(tdata_item_t));
204 if (titem == NULL) {
205 printf("Memory allocation failed.\n");
206 exit(1);
207 }
208
209 titem->tic = tic;
210 return titem;
211}
212
213tdata_array_t *tdata_array_new(void)
214{
215 tdata_array_t *tarray;
216
217 tarray = calloc(1, sizeof(tdata_array_t));
218 if (tarray == NULL) {
219 printf("Memory allocation failed.\n");
220 exit(1);
221 }
222
223 return tarray;
224}
225
226tdata_object_t *tdata_object_new(void)
227{
228 tdata_object_t *tobject;
229
230 tobject = calloc(1, sizeof(tdata_object_t));
231 if (tobject == NULL) {
232 printf("Memory allocation failed.\n");
233 exit(1);
234 }
235
236 return tobject;
237}
238
239tdata_primitive_t *tdata_primitive_new(tprimitive_class_t tpc)
240{
241 tdata_primitive_t *tprimitive;
242
243 tprimitive = calloc(1, sizeof(tdata_primitive_t));
244 if (tprimitive == NULL) {
245 printf("Memory allocation failed.\n");
246 exit(1);
247 }
248
249 tprimitive->tpc = tpc;
250 return tprimitive;
251}
252
253tdata_fun_t *tdata_fun_new(void)
254{
255 tdata_fun_t *tfun;
256
257 tfun = calloc(1, sizeof(tdata_fun_t));
258 if (tfun == NULL) {
259 printf("Memory allocation failed.\n");
260 exit(1);
261 }
262
263 return tfun;
264}
Note: See TracBrowser for help on using the repository browser.