source: mainline/uspace/app/sbi/src/symbol.c@ 883fedc

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

Update SBI to rev. 207.

  • Property mode set to 100644
File size: 11.6 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 Symbols. */
30
31#include <stdlib.h>
32#include <assert.h>
33#include "list.h"
34#include "mytypes.h"
35#include "strtab.h"
36#include "stree.h"
37
38#include "symbol.h"
39
40static stree_symbol_t *symbol_search_global(stree_program_t *prog,
41 stree_ident_t *name);
42static stree_symbol_t *symbol_find_epoint_rec(stree_program_t *prog,
43 stree_ident_t *name, stree_csi_t *csi);
44static stree_ident_t *symbol_get_ident(stree_symbol_t *symbol);
45
46/** Lookup symbol in CSI using a type expression.
47 *
48 * XXX This should be removed in favor of full type expression evaluation
49 * (run_texpr). This cannot work properly with generics.
50 *
51 * @param prog Program
52 * @param scope CSI used as base for relative references
53 * @param texpr Type expression
54 *
55 * @return Symbol referenced by type expression or @c NULL
56 * if not found
57 */
58stree_symbol_t *symbol_xlookup_in_csi(stree_program_t *prog,
59 stree_csi_t *scope, stree_texpr_t *texpr)
60{
61 stree_symbol_t *a, *b;
62 stree_csi_t *a_csi;
63
64 switch (texpr->tc) {
65 case tc_tnameref:
66 return symbol_lookup_in_csi(prog, scope, texpr->u.tnameref->name);
67 case tc_taccess:
68 a = symbol_xlookup_in_csi(prog, scope, texpr->u.taccess->arg);
69 a_csi = symbol_to_csi(a);
70 if (a_csi == NULL) {
71 printf("Error: Symbol is not CSI.\n");
72 exit(1);
73 }
74 b = symbol_search_csi(prog, a_csi, texpr->u.taccess->member_name);
75 if (b == NULL) {
76 printf("Error: CSI '%s' not found\n", strtab_get_str(texpr->u.taccess->member_name->sid));
77 exit(1);
78 }
79 return b;
80 case tc_tapply:
81 return symbol_xlookup_in_csi(prog, scope,
82 texpr->u.tapply->gtype);
83 default:
84 assert(b_false);
85 }
86}
87
88/** Lookup symbol reference in CSI.
89 *
90 * @param prog Program to look in
91 * @param scope CSI in @a prog which is the base for references
92 * @param name Identifier of the symbol
93 *
94 * @return Symbol or @c NULL if symbol not found
95 */
96stree_symbol_t *symbol_lookup_in_csi(stree_program_t *prog, stree_csi_t *scope,
97 stree_ident_t *name)
98{
99 stree_symbol_t *symbol;
100
101 /* This CSI node should have been processed. */
102 assert(scope == NULL || scope->ancr_state == ws_visited);
103
104 symbol = NULL;
105 while (scope != NULL && symbol == NULL) {
106 symbol = symbol_search_csi(prog, scope, name);
107 scope = csi_to_symbol(scope)->outer_csi;
108 }
109
110 if (symbol == NULL)
111 symbol = symbol_search_global(prog, name);
112
113 return symbol;
114}
115
116/** Look for symbol strictly in CSI.
117 *
118 * Look for symbol in definition of a CSI and its ancestors. (But not
119 * in lexically enclosing CSI.)
120 *
121 * @param prog Program to look in
122 * @param scope CSI in which to look
123 * @param name Identifier of the symbol
124 *
125 * @return Symbol or @c NULL if symbol not found.
126 */
127stree_symbol_t *symbol_search_csi(stree_program_t *prog,
128 stree_csi_t *scope, stree_ident_t *name)
129{
130 list_node_t *node;
131 stree_csimbr_t *csimbr;
132 stree_symbol_t *symbol;
133 stree_ident_t *mbr_name;
134 stree_symbol_t *base_csi_sym;
135 stree_csi_t *base_csi;
136
137 (void) prog;
138
139 /* Look in new members in this class. */
140
141 node = list_first(&scope->members);
142 while (node != NULL) {
143 csimbr = list_node_data(node, stree_csimbr_t *);
144
145 /* Keep compiler happy. */
146 mbr_name = NULL;
147
148 switch (csimbr->cc) {
149 case csimbr_csi: mbr_name = csimbr->u.csi->name; break;
150 case csimbr_deleg: mbr_name = csimbr->u.deleg->name; break;
151 case csimbr_fun: mbr_name = csimbr->u.fun->name; break;
152 case csimbr_var: mbr_name = csimbr->u.var->name; break;
153 case csimbr_prop: mbr_name = csimbr->u.prop->name; break;
154 }
155
156 if (name->sid == mbr_name->sid) {
157 /* Match */
158 switch (csimbr->cc) {
159 case csimbr_csi:
160 symbol = csi_to_symbol(csimbr->u.csi);
161 break;
162 case csimbr_deleg:
163 symbol = deleg_to_symbol(csimbr->u.deleg);
164 break;
165 case csimbr_fun:
166 symbol = fun_to_symbol(csimbr->u.fun);
167 break;
168 case csimbr_var:
169 symbol = var_to_symbol(csimbr->u.var);
170 break;
171 case csimbr_prop:
172 symbol = prop_to_symbol(csimbr->u.prop);
173 break;
174 default:
175 assert(b_false);
176 }
177 return symbol;
178 }
179 node = list_next(&scope->members, node);
180 }
181
182 /* Try inherited members. */
183 if (scope->base_csi_ref != NULL) {
184 base_csi_sym = symbol_xlookup_in_csi(prog,
185 csi_to_symbol(scope)->outer_csi, scope->base_csi_ref);
186 base_csi = symbol_to_csi(base_csi_sym);
187 assert(base_csi != NULL);
188
189 return symbol_search_csi(prog, base_csi, name);
190 }
191
192 /* No match */
193 return NULL;
194}
195
196/** Look for symbol in global scope.
197 *
198 * @param prog Program to look in
199 * @param name Identifier of the symbol
200 *
201 * @return Symbol or @c NULL if symbol not found.
202 */
203static stree_symbol_t *symbol_search_global(stree_program_t *prog,
204 stree_ident_t *name)
205{
206 list_node_t *node;
207 stree_modm_t *modm;
208 stree_symbol_t *symbol;
209
210 node = list_first(&prog->module->members);
211 while (node != NULL) {
212 modm = list_node_data(node, stree_modm_t *);
213 if (name->sid == modm->u.csi->name->sid) {
214 /* Match */
215 switch (modm->mc) {
216 case mc_csi:
217 symbol = csi_to_symbol(modm->u.csi);
218 break;
219 default:
220 assert(b_false);
221 }
222 return symbol;
223 }
224 node = list_next(&prog->module->members, node);
225 }
226
227 return NULL;
228}
229
230/** Find entry point.
231 *
232 * Perform a walk of all CSIs and look for a function with the name @a name.
233 *
234 * @param prog Program to look in
235 * @param name Name of entry point
236 *
237 * @return Symbol or @c NULL if symbol not found.
238 */
239stree_symbol_t *symbol_find_epoint(stree_program_t *prog, stree_ident_t *name)
240{
241 list_node_t *node;
242 stree_modm_t *modm;
243 stree_symbol_t *entry, *etmp;
244
245 entry = NULL;
246
247 node = list_first(&prog->module->members);
248 while (node != NULL) {
249 modm = list_node_data(node, stree_modm_t *);
250 if (modm->mc == mc_csi) {
251 etmp = symbol_find_epoint_rec(prog, name, modm->u.csi);
252 if (etmp != NULL) {
253 if (entry != NULL) {
254 printf("Error: Duplicate entry point.\n");
255 exit(1);
256 }
257 entry = etmp;
258 }
259 }
260 node = list_next(&prog->module->members, node);
261 }
262
263 return entry;
264}
265
266/** Find entry point under CSI.
267 *
268 * Internal part of symbol_find_epoint() that recursively walks CSIs.
269 *
270 * @param prog Program to look in
271 * @param name Name of entry point
272 *
273 * @return Symbol or @c NULL if symbol not found.
274 */
275static stree_symbol_t *symbol_find_epoint_rec(stree_program_t *prog,
276 stree_ident_t *name, stree_csi_t *csi)
277{
278 list_node_t *node;
279 stree_csimbr_t *csimbr;
280 stree_symbol_t *entry, *etmp;
281
282 entry = NULL;
283
284 node = list_first(&csi->members);
285 while (node != NULL) {
286 csimbr = list_node_data(node, stree_csimbr_t *);
287
288 switch (csimbr->cc) {
289 case csimbr_csi:
290 etmp = symbol_find_epoint_rec(prog, name, csimbr->u.csi);
291 if (etmp != NULL) {
292 if (entry != NULL) {
293 printf("Error: Duplicate entry point.\n");
294 exit(1);
295 }
296 entry = etmp;
297 }
298 break;
299 case csimbr_fun:
300 if (csimbr->u.fun->name->sid == name->sid) {
301 if (entry != NULL) {
302 printf("Error: Duplicate entry point.\n");
303 exit(1);
304 }
305 entry = fun_to_symbol(csimbr->u.fun);
306 }
307 default:
308 break;
309 }
310
311 node = list_next(&csi->members, node);
312 }
313
314 return entry;
315}
316
317/*
318 * The notion of symbol is designed as a common base class for several
319 * types of declarations with global and CSI scope. Here we simulate
320 * conversion from this base class (symbol) to derived classes (CSI,
321 * fun, ..) and vice versa.
322 */
323
324/** Convert symbol to delegate (base to derived).
325 *
326 * @param symbol Symbol
327 * @return Delegate or @c NULL if symbol is not a delegate
328 */
329stree_deleg_t *symbol_to_deleg(stree_symbol_t *symbol)
330{
331 if (symbol->sc != sc_deleg)
332 return NULL;
333
334 return symbol->u.deleg;
335}
336
337/** Convert delegate to symbol (derived to base).
338 *
339 * @param deleg Delegate
340 * @return Symbol
341 */
342stree_symbol_t *deleg_to_symbol(stree_deleg_t *deleg)
343{
344 assert(deleg->symbol);
345 return deleg->symbol;
346}
347
348/** Convert symbol to CSI (base to derived).
349 *
350 * @param symbol Symbol
351 * @return CSI or @c NULL if symbol is not a CSI
352 */
353stree_csi_t *symbol_to_csi(stree_symbol_t *symbol)
354{
355 if (symbol->sc != sc_csi)
356 return NULL;
357
358 return symbol->u.csi;
359}
360
361/** Convert CSI to symbol (derived to base).
362 *
363 * @param csi CSI
364 * @return Symbol
365 */
366stree_symbol_t *csi_to_symbol(stree_csi_t *csi)
367{
368 assert(csi->symbol);
369 return csi->symbol;
370}
371
372/** Convert symbol to function (base to derived).
373 *
374 * @param symbol Symbol
375 * @return Function or @c NULL if symbol is not a function
376 */
377stree_fun_t *symbol_to_fun(stree_symbol_t *symbol)
378{
379 if (symbol->sc != sc_fun)
380 return NULL;
381
382 return symbol->u.fun;
383}
384
385/** Convert function to symbol (derived to base).
386 *
387 * @param fun Function
388 * @return Symbol
389 */
390stree_symbol_t *fun_to_symbol(stree_fun_t *fun)
391{
392 assert(fun->symbol);
393 return fun->symbol;
394}
395
396/** Convert symbol to member variable (base to derived).
397 *
398 * @param symbol Symbol
399 * @return Variable or @c NULL if symbol is not a member variable
400 */
401stree_var_t *symbol_to_var(stree_symbol_t *symbol)
402{
403 if (symbol->sc != sc_var)
404 return NULL;
405
406 return symbol->u.var;
407}
408
409/** Convert variable to symbol (derived to base).
410 *
411 * @param fun Variable
412 * @return Symbol
413 */
414stree_symbol_t *var_to_symbol(stree_var_t *var)
415{
416 assert(var->symbol);
417 return var->symbol;
418}
419
420/** Convert symbol to property (base to derived).
421 *
422 * @param symbol Symbol
423 * @return Property or @c NULL if symbol is not a property
424 */
425stree_prop_t *symbol_to_prop(stree_symbol_t *symbol)
426{
427 if (symbol->sc != sc_prop)
428 return NULL;
429
430 return symbol->u.prop;
431}
432
433/** Convert property to symbol (derived to base).
434 *
435 * @param fun Property
436 * @return Symbol
437 */
438stree_symbol_t *prop_to_symbol(stree_prop_t *prop)
439{
440 assert(prop->symbol);
441 return prop->symbol;
442}
443
444/** Print fully qualified name of symbol.
445 *
446 * @param symbol Symbol
447 */
448void symbol_print_fqn(stree_symbol_t *symbol)
449{
450 stree_ident_t *name;
451 stree_symbol_t *outer_sym;
452
453 if (symbol->outer_csi != NULL) {
454 outer_sym = csi_to_symbol(symbol->outer_csi);
455 symbol_print_fqn(outer_sym);
456 printf(".");
457 }
458
459 name = symbol_get_ident(symbol);
460 printf("%s", strtab_get_str(name->sid));
461}
462
463/** Return symbol identifier.
464 *
465 * @param symbol Symbol
466 * @return Symbol identifier
467 */
468static stree_ident_t *symbol_get_ident(stree_symbol_t *symbol)
469{
470 stree_ident_t *ident;
471
472 switch (symbol->sc) {
473 case sc_csi: ident = symbol->u.csi->name; break;
474 case sc_deleg: ident = symbol->u.deleg->name; break;
475 case sc_fun: ident = symbol->u.fun->name; break;
476 case sc_var: ident = symbol->u.var->name; break;
477 case sc_prop: ident = symbol->u.prop->name; break;
478 }
479
480 return ident;
481}
Note: See TracBrowser for help on using the repository browser.