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