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.
29 /** @file Ancestry resolver.
31 * A chicken-and-egg problem is that in order to match identifiers to CSI
32 * definitions we need to know CSI ancestry. To know CSI ancestry we need
33 * to match identifiers to CSI definitions. Thus both must be done at the
34 * same time. Once we know the ancestry of some CSI, we are able to resolve
35 * symbols referenced within the scope of that CSI (but not in nested scopes).
37 * Here lies probably the most complicated (although not so complicated)
38 * algorithm. To process node N we must first process outer(N). This allows
39 * us to find all base(N) nodes and process them.
41 * To ensure all nodes get processed correctly, we use a two-layer walk.
42 * In the lower layer (ancr_csi_process) we follow the dependencies.
43 * ancr_csi_process(N) ensures N (and possibly other nodes) get resolved.
45 * In the second layer we simply do a DFS of the CSI tree, calling
46 * ancr_csi_process() on each node. This ensures that eventually all
47 * nodes get processed.
62 static void ancr_csi_dfs(stree_program_t
*prog
, stree_csi_t
*csi
);
63 static void ancr_csi_process(stree_program_t
*prog
, stree_csi_t
*node
);
64 static stree_csi_t
*ancr_csi_get_pred(stree_program_t
*prog
, stree_csi_t
*csi
,
65 stree_texpr_t
*pred_ref
);
66 static void ancr_csi_print_cycle(stree_program_t
*prog
, stree_csi_t
*node
);
68 /** Process ancestry of all CSIs in a module.
70 * Note that currently we expect there to be exactly one module in the
73 * @param prog Program being processed.
74 * @param module Module to process.
76 void ancr_module_process(stree_program_t
*prog
, stree_module_t
*module
)
82 node
= list_first(&prog
->module
->members
);
84 while (node
!= NULL
) {
85 modm
= list_node_data(node
, stree_modm_t
*);
89 ancr_csi_dfs(prog
, modm
->u
.csi
);
95 node
= list_next(&prog
->module
->members
, node
);
99 /** Walk CSI node tree depth-first.
101 * This is the outer depth-first walk whose purpose is to eventually
102 * process all CSI nodes by calling ancr_csi_process() on them.
103 * (Which causes that and possibly some other nodes to be processed).
105 * @param prog Program being processed.
106 * @param csi CSI node to visit.
108 static void ancr_csi_dfs(stree_program_t
*prog
, stree_csi_t
*csi
)
111 stree_csimbr_t
*csimbr
;
113 /* Process this node first. */
114 ancr_csi_process(prog
, csi
);
116 /* Now visit all children. */
117 node
= list_first(&csi
->members
);
118 while (node
!= NULL
) {
119 csimbr
= list_node_data(node
, stree_csimbr_t
*);
120 if (csimbr
->cc
== csimbr_csi
)
121 ancr_csi_dfs(prog
, csimbr
->u
.csi
);
123 node
= list_next(&csi
->members
, node
);
127 /** Process csi node.
129 * Fist processes the pre-required nodes (outer CSI and base CSIs),
130 * then processes @a node. This is the core 'outward-and-baseward' walk.
132 * @param prog Program being processed.
133 * @param csi CSI node to process.
135 static void ancr_csi_process(stree_program_t
*prog
, stree_csi_t
*csi
)
137 stree_csi_t
*base_csi
, *outer_csi
;
138 stree_csi_t
*gf_class
;
142 stree_csi_t
*pred_csi
;
144 if (csi
->ancr_state
== ws_visited
) {
145 /* Node already processed */
149 if (csi
->ancr_state
== ws_active
) {
150 /* Error, closed reference loop. */
151 printf("Error: Circular class, struct or interface chain: ");
152 ancr_csi_print_cycle(prog
, csi
);
157 csi
->ancr_state
= ws_active
;
159 outer_csi
= csi_to_symbol(csi
)->outer_csi
;
160 gf_class
= builtin_get_gf_class(prog
->builtin
);
162 if (csi
!= gf_class
) {
163 /* Implicit inheritance from grandfather class. */
166 /* Grandfather class has no base class. */
170 /* Process outer CSI */
171 if (outer_csi
!= NULL
)
172 ancr_csi_process(prog
, outer_csi
);
175 * Process inheritance list.
177 pred_n
= list_first(&csi
->inherit
);
179 /* For a class node, the first entry can be a class. */
180 if (csi
->cc
== csi_class
&& pred_n
!= NULL
) {
181 pred
= list_node_data(pred_n
, stree_texpr_t
*);
182 pred_csi
= ancr_csi_get_pred(prog
, csi
, pred
);
183 assert(pred_csi
!= NULL
);
185 if (pred_csi
->cc
== csi_class
) {
186 /* Process base class */
188 ancr_csi_process(prog
, pred_csi
);
190 pred_n
= list_next(&csi
->inherit
, pred_n
);
194 /* Following entires can only be interfaces. */
195 while (pred_n
!= NULL
) {
196 pred
= list_node_data(pred_n
, stree_texpr_t
*);
197 pred_csi
= ancr_csi_get_pred(prog
, csi
, pred
);
198 assert(pred_csi
!= NULL
);
200 /* Process implemented or accumulated interface. */
201 ancr_csi_process(prog
, pred_csi
);
203 switch (pred_csi
->cc
) {
207 cspan_print(csi
->name
->cspan
);
208 printf(" Error: Only the first predecessor "
209 "can be a class. ('");
210 symbol_print_fqn(csi_to_symbol(csi
));
211 printf("' deriving from '");
212 symbol_print_fqn(csi_to_symbol(pred_csi
));
217 assert(b_false
); /* XXX */
220 cspan_print(csi
->name
->cspan
);
221 printf(" Error: Interface predecessor must be "
223 symbol_print_fqn(csi_to_symbol(csi
));
224 printf("' deriving from '");
225 symbol_print_fqn(csi_to_symbol(pred_csi
));
232 assert(b_false
); /* XXX */
238 pred_n
= list_next(&csi
->inherit
, pred_n
);
241 /* Store base CSI and update node state. */
242 csi
->ancr_state
= ws_visited
;
243 csi
->base_csi
= base_csi
;
246 /** Resolve CSI predecessor reference.
248 * Returns the CSI predecessor referenced by @a pred_ref.
249 * If the referenced CSI does not exist, an error is generated.
251 * @param prog Program being processed.
252 * @param csi CSI node to process.
253 * @param pred_ref Type expression referencing the predecessor.
254 * @return Predecessor CSI.
256 static stree_csi_t
*ancr_csi_get_pred(stree_program_t
*prog
, stree_csi_t
*csi
,
257 stree_texpr_t
*pred_ref
)
259 stree_csi_t
*outer_csi
;
260 stree_symbol_t
*pred_sym
;
261 stree_csi_t
*pred_csi
;
263 outer_csi
= csi_to_symbol(csi
)->outer_csi
;
264 pred_sym
= symbol_xlookup_in_csi(prog
, outer_csi
, pred_ref
);
265 pred_csi
= symbol_to_csi(pred_sym
);
266 assert(pred_csi
!= NULL
); /* XXX */
271 /** Print loop in CSI ancestry.
273 * We have detected a loop in CSI ancestry. Traverse it (by following the
274 * nodes in ws_active state and print it.
276 * @param prog Program.
277 * @param node CSI node participating in an ancestry cycle.
279 static void ancr_csi_print_cycle(stree_program_t
*prog
, stree_csi_t
*node
)
282 stree_symbol_t
*pred_sym
, *node_sym
;
283 stree_csi_t
*pred_csi
, *outer_csi
;
289 node_sym
= csi_to_symbol(node
);
290 symbol_print_fqn(node_sym
);
293 outer_csi
= node_sym
->outer_csi
;
295 if (outer_csi
!= NULL
&& outer_csi
->ancr_state
== ws_active
) {
300 pred_n
= list_first(&node
->inherit
);
301 while (pred_n
!= NULL
) {
302 pred
= list_node_data(pred_n
, stree_texpr_t
*);
303 pred_sym
= symbol_xlookup_in_csi(prog
,
305 pred_csi
= symbol_to_csi(pred_sym
);
306 assert(pred_csi
!= NULL
);
308 if (pred_csi
->ancr_state
== ws_active
) {
314 assert(node
!= NULL
);
318 node_sym
= csi_to_symbol(node
);
319 symbol_print_fqn(node_sym
);