Update GCC and binutils to latest versions
[helenos.git] / uspace / app / sbi / src / symbol.c
blobdfa0ebbc44f971b9dfc4eb43b94d25dce5e0d1b3
1 /*
2 * Copyright (c) 2010 Jiri Svoboda
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.
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.
29 /** @file Symbols. */
31 #include <stdlib.h>
32 #include <assert.h>
33 #include "list.h"
34 #include "mytypes.h"
35 #include "strtab.h"
36 #include "stree.h"
38 #include "symbol.h"
40 static stree_symbol_t *symbol_search_global(stree_program_t *prog,
41 stree_ident_t *name);
42 static stree_symbol_t *symbol_find_epoint_rec(stree_program_t *prog,
43 stree_ident_t *name, stree_csi_t *csi);
44 static stree_ident_t *symbol_get_ident(stree_symbol_t *symbol);
46 /** Lookup symbol in CSI using a type expression.
48 * XXX This should be removed in favor of full type expression evaluation
49 * (run_texpr). This cannot work properly with generics.
51 * @param prog Program
52 * @param scope CSI used as base for relative references
53 * @param texpr Type expression
55 * @return Symbol referenced by type expression or @c NULL
56 * if not found
58 stree_symbol_t *symbol_xlookup_in_csi(stree_program_t *prog,
59 stree_csi_t *scope, stree_texpr_t *texpr)
61 stree_symbol_t *a, *b;
62 stree_csi_t *a_csi;
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);
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);
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);
88 /** Lookup symbol reference in CSI.
90 * XXX These functions should take just an sid, not a full identifier.
91 * Sometimes we search for a name which has no associated cspan.
93 * @param prog Program to look in
94 * @param scope CSI in @a prog which is the base for references
95 * @param name Identifier of the symbol
97 * @return Symbol or @c NULL if symbol not found
99 stree_symbol_t *symbol_lookup_in_csi(stree_program_t *prog, stree_csi_t *scope,
100 stree_ident_t *name)
102 stree_symbol_t *symbol;
104 /* This CSI node should have been processed. */
105 assert(scope == NULL || scope->ancr_state == ws_visited);
107 symbol = NULL;
108 while (scope != NULL && symbol == NULL) {
109 symbol = symbol_search_csi(prog, scope, name);
110 scope = csi_to_symbol(scope)->outer_csi;
113 if (symbol == NULL)
114 symbol = symbol_search_global(prog, name);
116 return symbol;
119 /** Look for symbol strictly in CSI.
121 * Look for symbol in definition of a CSI and its ancestors. (But not
122 * in lexically enclosing CSI.)
124 * @param prog Program to look in
125 * @param scope CSI in which to look
126 * @param name Identifier of the symbol
128 * @return Symbol or @c NULL if symbol not found.
130 stree_symbol_t *symbol_search_csi(stree_program_t *prog,
131 stree_csi_t *scope, stree_ident_t *name)
133 stree_csi_t *base_csi;
134 stree_symbol_t *symbol;
136 /* Look in new members in this class. */
137 symbol = symbol_search_csi_no_base(prog, scope, name);
138 if (symbol != NULL)
139 return symbol;
141 /* Try inherited members. */
142 base_csi = symbol_get_base_class(prog, scope);
143 if (base_csi != NULL)
144 return symbol_search_csi(prog, base_csi, name);
146 /* No match */
147 return NULL;
150 /** Look for symbol strictly in CSI.
152 * Look for symbol in definition of a CSI and its ancestors. (But not
153 * in lexically enclosing CSI or in base CSI.)
155 * @param prog Program to look in
156 * @param scope CSI in which to look
157 * @param name Identifier of the symbol
159 * @return Symbol or @c NULL if symbol not found.
161 stree_symbol_t *symbol_search_csi_no_base(stree_program_t *prog,
162 stree_csi_t *scope, stree_ident_t *name)
164 list_node_t *node;
165 stree_csimbr_t *csimbr;
166 stree_ident_t *mbr_name;
168 (void) prog;
170 /* Look in new members in this class. */
172 node = list_first(&scope->members);
173 while (node != NULL) {
174 csimbr = list_node_data(node, stree_csimbr_t *);
175 mbr_name = stree_csimbr_get_name(csimbr);
177 if (name->sid == mbr_name->sid) {
178 /* Match */
179 return csimbr_to_symbol(csimbr);
182 node = list_next(&scope->members, node);
184 /* No match */
185 return NULL;
188 /** Look for symbol in global scope.
190 * @param prog Program to look in
191 * @param name Identifier of the symbol
193 * @return Symbol or @c NULL if symbol not found.
195 static stree_symbol_t *symbol_search_global(stree_program_t *prog,
196 stree_ident_t *name)
198 list_node_t *node;
199 stree_modm_t *modm;
200 stree_symbol_t *symbol;
201 stree_ident_t *mbr_name;
203 node = list_first(&prog->module->members);
204 while (node != NULL) {
205 modm = list_node_data(node, stree_modm_t *);
207 /* Make compiler happy. */
208 mbr_name = NULL;
210 switch (modm->mc) {
211 case mc_csi:
212 mbr_name = modm->u.csi->name;
213 break;
214 case mc_enum:
215 mbr_name = modm->u.enum_d->name;
216 break;
219 /* The Clang static analyzer is just too picky. */
220 assert(mbr_name != NULL);
222 if (name->sid == mbr_name->sid) {
223 /* Make compiler happy. */
224 symbol = NULL;
226 /* Match */
227 switch (modm->mc) {
228 case mc_csi:
229 symbol = csi_to_symbol(modm->u.csi);
230 break;
231 case mc_enum:
232 symbol = enum_to_symbol(modm->u.enum_d);
233 break;
235 return symbol;
237 node = list_next(&prog->module->members, node);
240 return NULL;
243 /** Get explicit base class for a CSI.
245 * Note that if there is no explicit base class (class is derived implicitly
246 * from @c object, then @c NULL is returned.
248 * @param prog Program to look in
249 * @param csi CSI
251 * @return Base class (CSI) or @c NULL if no explicit base class.
253 stree_csi_t *symbol_get_base_class(stree_program_t *prog, stree_csi_t *csi)
255 list_node_t *pred_n;
256 stree_texpr_t *pred;
257 stree_symbol_t *pred_sym;
258 stree_csi_t *pred_csi;
259 stree_csi_t *outer_csi;
261 outer_csi = csi_to_symbol(csi)->outer_csi;
263 pred_n = list_first(&csi->inherit);
264 if (pred_n == NULL)
265 return NULL;
267 pred = list_node_data(pred_n, stree_texpr_t *);
268 pred_sym = symbol_xlookup_in_csi(prog, outer_csi, pred);
269 pred_csi = symbol_to_csi(pred_sym);
270 assert(pred_csi != NULL); /* XXX! */
272 if (pred_csi->cc == csi_class)
273 return pred_csi;
275 return NULL;
278 /** Get type expression referencing base class for a CSI.
280 * Note that if there is no explicit base class (class is derived implicitly
281 * from @c object, then @c NULL is returned.
283 * @param prog Program to look in
284 * @param csi CSI
286 * @return Type expression or @c NULL if no explicit base class.
288 stree_texpr_t *symbol_get_base_class_ref(stree_program_t *prog,
289 stree_csi_t *csi)
291 list_node_t *pred_n;
292 stree_texpr_t *pred;
293 stree_symbol_t *pred_sym;
294 stree_csi_t *pred_csi;
295 stree_csi_t *outer_csi;
297 outer_csi = csi_to_symbol(csi)->outer_csi;
299 pred_n = list_first(&csi->inherit);
300 if (pred_n == NULL)
301 return NULL;
303 pred = list_node_data(pred_n, stree_texpr_t *);
304 pred_sym = symbol_xlookup_in_csi(prog, outer_csi, pred);
305 pred_csi = symbol_to_csi(pred_sym);
306 assert(pred_csi != NULL); /* XXX! */
308 if (pred_csi->cc == csi_class)
309 return pred;
311 return NULL;
314 /** Find entry point.
316 * Perform a walk of all CSIs and look for a function with the name @a name.
318 * @param prog Program to look in
319 * @param name Name of entry point
321 * @return Symbol or @c NULL if symbol not found.
323 stree_symbol_t *symbol_find_epoint(stree_program_t *prog, stree_ident_t *name)
325 list_node_t *node;
326 stree_modm_t *modm;
327 stree_symbol_t *entry, *etmp;
329 entry = NULL;
331 node = list_first(&prog->module->members);
332 while (node != NULL) {
333 modm = list_node_data(node, stree_modm_t *);
334 if (modm->mc == mc_csi) {
335 etmp = symbol_find_epoint_rec(prog, name, modm->u.csi);
336 if (etmp != NULL) {
337 if (entry != NULL) {
338 printf("Error: Duplicate entry point.\n");
339 exit(1);
341 entry = etmp;
344 node = list_next(&prog->module->members, node);
347 return entry;
350 /** Find entry point under CSI.
352 * Internal part of symbol_find_epoint() that recursively walks CSIs.
354 * @param prog Program to look in
355 * @param name Name of entry point
357 * @return Symbol or @c NULL if symbol not found.
359 static stree_symbol_t *symbol_find_epoint_rec(stree_program_t *prog,
360 stree_ident_t *name, stree_csi_t *csi)
362 list_node_t *node;
363 stree_csimbr_t *csimbr;
364 stree_symbol_t *entry, *etmp;
365 stree_symbol_t *fun_sym;
367 entry = NULL;
369 node = list_first(&csi->members);
370 while (node != NULL) {
371 csimbr = list_node_data(node, stree_csimbr_t *);
373 switch (csimbr->cc) {
374 case csimbr_csi:
375 etmp = symbol_find_epoint_rec(prog, name, csimbr->u.csi);
376 if (etmp != NULL) {
377 if (entry != NULL) {
378 printf("Error: Duplicate entry point.\n");
379 exit(1);
381 entry = etmp;
383 break;
384 case csimbr_fun:
385 fun_sym = fun_to_symbol(csimbr->u.fun);
387 if (csimbr->u.fun->name->sid == name->sid &&
388 stree_symbol_has_attr(fun_sym, sac_static)) {
389 if (entry != NULL) {
390 printf("Error: Duplicate entry point.\n");
391 exit(1);
393 entry = fun_sym;
395 default:
396 break;
399 node = list_next(&csi->members, node);
402 return entry;
406 * The notion of symbol is designed as a common base class for several
407 * types of declarations with global and CSI scope. Here we simulate
408 * conversion from this base class (symbol) to derived classes (CSI,
409 * fun, ..) and vice versa.
412 /** Convert symbol to delegate (base to derived).
414 * @param symbol Symbol
415 * @return Delegate or @c NULL if symbol is not a delegate
417 stree_deleg_t *symbol_to_deleg(stree_symbol_t *symbol)
419 if (symbol->sc != sc_deleg)
420 return NULL;
422 return symbol->u.deleg;
425 /** Convert delegate to symbol (derived to base).
427 * @param deleg Delegate
428 * @return Symbol
430 stree_symbol_t *deleg_to_symbol(stree_deleg_t *deleg)
432 assert(deleg->symbol);
433 return deleg->symbol;
436 /** Convert symbol to enum (base to derived).
438 * @param symbol Symbol
439 * @return Enum or @c NULL if symbol is not a enum
441 stree_enum_t *symbol_to_enum(stree_symbol_t *symbol)
443 if (symbol->sc != sc_enum)
444 return NULL;
446 return symbol->u.enum_d;
449 /** Convert enum to symbol (derived to base).
451 * @param deleg Enum
452 * @return Symbol
454 stree_symbol_t *enum_to_symbol(stree_enum_t *enum_d)
456 assert(enum_d->symbol);
457 return enum_d->symbol;
460 /** Convert symbol to CSI (base to derived).
462 * @param symbol Symbol
463 * @return CSI or @c NULL if symbol is not a CSI
465 stree_csi_t *symbol_to_csi(stree_symbol_t *symbol)
467 if (symbol->sc != sc_csi)
468 return NULL;
470 return symbol->u.csi;
473 /** Convert CSI to symbol (derived to base).
475 * @param csi CSI
476 * @return Symbol
478 stree_symbol_t *csi_to_symbol(stree_csi_t *csi)
480 assert(csi->symbol);
481 return csi->symbol;
484 /** Convert symbol to constructor (base to derived).
486 * @param symbol Symbol
487 * @return Constructor or @c NULL if symbol is not a constructor
489 stree_ctor_t *symbol_to_ctor(stree_symbol_t *symbol)
491 if (symbol->sc != sc_ctor)
492 return NULL;
494 return symbol->u.ctor;
497 /** Convert constructor to symbol (derived to base).
499 * @param ctor Constructor
500 * @return Symbol
502 stree_symbol_t *ctor_to_symbol(stree_ctor_t *ctor)
504 assert(ctor->symbol);
505 return ctor->symbol;
508 /** Convert symbol to function (base to derived).
510 * @param symbol Symbol
511 * @return Function or @c NULL if symbol is not a function
513 stree_fun_t *symbol_to_fun(stree_symbol_t *symbol)
515 if (symbol->sc != sc_fun)
516 return NULL;
518 return symbol->u.fun;
521 /** Convert function to symbol (derived to base).
523 * @param fun Function
524 * @return Symbol
526 stree_symbol_t *fun_to_symbol(stree_fun_t *fun)
528 assert(fun->symbol);
529 return fun->symbol;
532 /** Convert symbol to member variable (base to derived).
534 * @param symbol Symbol
535 * @return Variable or @c NULL if symbol is not a member variable
537 stree_var_t *symbol_to_var(stree_symbol_t *symbol)
539 if (symbol->sc != sc_var)
540 return NULL;
542 return symbol->u.var;
545 /** Convert variable to symbol (derived to base).
547 * @param fun Variable
548 * @return Symbol
550 stree_symbol_t *var_to_symbol(stree_var_t *var)
552 assert(var->symbol);
553 return var->symbol;
556 /** Convert symbol to property (base to derived).
558 * @param symbol Symbol
559 * @return Property or @c NULL if symbol is not a property
561 stree_prop_t *symbol_to_prop(stree_symbol_t *symbol)
563 if (symbol->sc != sc_prop)
564 return NULL;
566 return symbol->u.prop;
569 /** Get symbol from CSI member.
571 * A symbol corresponds to any CSI member. Return it.
573 * @param csimbr CSI member
574 * @return Symbol
576 stree_symbol_t *csimbr_to_symbol(stree_csimbr_t *csimbr)
578 stree_symbol_t *symbol;
580 /* Keep compiler happy. */
581 symbol = NULL;
583 /* Match */
584 switch (csimbr->cc) {
585 case csimbr_csi:
586 symbol = csi_to_symbol(csimbr->u.csi);
587 break;
588 case csimbr_ctor:
589 symbol = ctor_to_symbol(csimbr->u.ctor);
590 break;
591 case csimbr_deleg:
592 symbol = deleg_to_symbol(csimbr->u.deleg);
593 break;
594 case csimbr_enum:
595 symbol = enum_to_symbol(csimbr->u.enum_d);
596 break;
597 case csimbr_fun:
598 symbol = fun_to_symbol(csimbr->u.fun);
599 break;
600 case csimbr_var:
601 symbol = var_to_symbol(csimbr->u.var);
602 break;
603 case csimbr_prop:
604 symbol = prop_to_symbol(csimbr->u.prop);
605 break;
608 return symbol;
611 /** Convert property to symbol (derived to base).
613 * @param fun Property
614 * @return Symbol
616 stree_symbol_t *prop_to_symbol(stree_prop_t *prop)
618 assert(prop->symbol);
619 return prop->symbol;
622 /** Print fully qualified name of symbol.
624 * @param symbol Symbol
626 void symbol_print_fqn(stree_symbol_t *symbol)
628 stree_ident_t *name;
629 stree_symbol_t *outer_sym;
631 if (symbol->outer_csi != NULL) {
632 outer_sym = csi_to_symbol(symbol->outer_csi);
633 symbol_print_fqn(outer_sym);
634 printf(".");
637 name = symbol_get_ident(symbol);
638 printf("%s", strtab_get_str(name->sid));
641 /** Return symbol identifier.
643 * @param symbol Symbol
644 * @return Symbol identifier
646 static stree_ident_t *symbol_get_ident(stree_symbol_t *symbol)
648 stree_ident_t *ident;
650 /* Make compiler happy. */
651 ident = NULL;
653 switch (symbol->sc) {
654 case sc_csi:
655 ident = symbol->u.csi->name;
656 break;
657 case sc_ctor:
658 ident = symbol->u.ctor->name;
659 break;
660 case sc_deleg:
661 ident = symbol->u.deleg->name;
662 break;
663 case sc_enum:
664 ident = symbol->u.enum_d->name;
665 break;
666 case sc_fun:
667 ident = symbol->u.fun->name;
668 break;
669 case sc_var:
670 ident = symbol->u.var->name;
671 break;
672 case sc_prop:
673 ident = symbol->u.prop->name;
674 break;
677 return ident;