2 * Copyright (c) 2010 Jiri Svoboda
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
40 static stree_symbol_t
*symbol_search_global(stree_program_t
*prog
,
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.
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
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
;
66 return symbol_lookup_in_csi(prog
, scope
, texpr
->u
.tnameref
->name
);
68 a
= symbol_xlookup_in_csi(prog
, scope
, texpr
->u
.taccess
->arg
);
69 a_csi
= symbol_to_csi(a
);
71 printf("Error: Symbol is not CSI.\n");
74 b
= symbol_search_csi(prog
, a_csi
, texpr
->u
.taccess
->member_name
);
76 printf("Error: CSI '%s' not found\n", strtab_get_str(texpr
->u
.taccess
->member_name
->sid
));
81 return symbol_xlookup_in_csi(prog
, scope
,
82 texpr
->u
.tapply
->gtype
);
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
,
102 stree_symbol_t
*symbol
;
104 /* This CSI node should have been processed. */
105 assert(scope
== NULL
|| scope
->ancr_state
== ws_visited
);
108 while (scope
!= NULL
&& symbol
== NULL
) {
109 symbol
= symbol_search_csi(prog
, scope
, name
);
110 scope
= csi_to_symbol(scope
)->outer_csi
;
114 symbol
= symbol_search_global(prog
, name
);
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
);
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
);
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
)
165 stree_csimbr_t
*csimbr
;
166 stree_ident_t
*mbr_name
;
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
) {
179 return csimbr_to_symbol(csimbr
);
182 node
= list_next(&scope
->members
, node
);
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
,
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. */
212 mbr_name
= modm
->u
.csi
->name
;
215 mbr_name
= modm
->u
.enum_d
->name
;
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. */
229 symbol
= csi_to_symbol(modm
->u
.csi
);
232 symbol
= enum_to_symbol(modm
->u
.enum_d
);
237 node
= list_next(&prog
->module
->members
, node
);
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
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
)
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
);
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
)
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
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
,
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
);
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
)
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
)
327 stree_symbol_t
*entry
, *etmp
;
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
);
338 printf("Error: Duplicate entry point.\n");
344 node
= list_next(&prog
->module
->members
, node
);
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
)
363 stree_csimbr_t
*csimbr
;
364 stree_symbol_t
*entry
, *etmp
;
365 stree_symbol_t
*fun_sym
;
369 node
= list_first(&csi
->members
);
370 while (node
!= NULL
) {
371 csimbr
= list_node_data(node
, stree_csimbr_t
*);
373 switch (csimbr
->cc
) {
375 etmp
= symbol_find_epoint_rec(prog
, name
, csimbr
->u
.csi
);
378 printf("Error: Duplicate entry point.\n");
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
)) {
390 printf("Error: Duplicate entry point.\n");
399 node
= list_next(&csi
->members
, node
);
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
)
422 return symbol
->u
.deleg
;
425 /** Convert delegate to symbol (derived to base).
427 * @param deleg Delegate
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
)
446 return symbol
->u
.enum_d
;
449 /** Convert enum to symbol (derived to base).
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
)
470 return symbol
->u
.csi
;
473 /** Convert CSI to symbol (derived to base).
478 stree_symbol_t
*csi_to_symbol(stree_csi_t
*csi
)
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
)
494 return symbol
->u
.ctor
;
497 /** Convert constructor to symbol (derived to base).
499 * @param ctor Constructor
502 stree_symbol_t
*ctor_to_symbol(stree_ctor_t
*ctor
)
504 assert(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
)
518 return symbol
->u
.fun
;
521 /** Convert function to symbol (derived to base).
523 * @param fun Function
526 stree_symbol_t
*fun_to_symbol(stree_fun_t
*fun
)
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
)
542 return symbol
->u
.var
;
545 /** Convert variable to symbol (derived to base).
547 * @param fun Variable
550 stree_symbol_t
*var_to_symbol(stree_var_t
*var
)
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
)
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
576 stree_symbol_t
*csimbr_to_symbol(stree_csimbr_t
*csimbr
)
578 stree_symbol_t
*symbol
;
580 /* Keep compiler happy. */
584 switch (csimbr
->cc
) {
586 symbol
= csi_to_symbol(csimbr
->u
.csi
);
589 symbol
= ctor_to_symbol(csimbr
->u
.ctor
);
592 symbol
= deleg_to_symbol(csimbr
->u
.deleg
);
595 symbol
= enum_to_symbol(csimbr
->u
.enum_d
);
598 symbol
= fun_to_symbol(csimbr
->u
.fun
);
601 symbol
= var_to_symbol(csimbr
->u
.var
);
604 symbol
= prop_to_symbol(csimbr
->u
.prop
);
611 /** Convert property to symbol (derived to base).
613 * @param fun Property
616 stree_symbol_t
*prop_to_symbol(stree_prop_t
*prop
)
618 assert(prop
->symbol
);
622 /** Print fully qualified name of symbol.
624 * @param symbol Symbol
626 void symbol_print_fqn(stree_symbol_t
*symbol
)
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
);
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. */
653 switch (symbol
->sc
) {
655 ident
= symbol
->u
.csi
->name
;
658 ident
= symbol
->u
.ctor
->name
;
661 ident
= symbol
->u
.deleg
->name
;
664 ident
= symbol
->u
.enum_d
->name
;
667 ident
= symbol
->u
.fun
->name
;
670 ident
= symbol
->u
.var
->name
;
673 ident
= symbol
->u
.prop
->name
;