source: mainline/uspace/app/sbi/src/ancr.c@ 39e8406

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

Update SBI to rev. 128.

  • Property mode set to 100644
File size: 6.1 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 Ancestry resolver.
30 *
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).
36 *
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.
40 *
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.
44 *
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.
48 */
49
50#include <stdlib.h>
51#include <assert.h>
52#include "list.h"
53#include "mytypes.h"
54#include "stree.h"
55#include "strtab.h"
56#include "symbol.h"
57
58#include "ancr.h"
59
60static void ancr_csi_dfs(stree_program_t *prog, stree_csi_t *csi);
61static void ancr_csi_process(stree_program_t *prog, stree_csi_t *node);
62static void ancr_csi_print_cycle(stree_program_t *prog, stree_csi_t *node);
63
64/** Process ancestry of all CSIs in a module.
65 *
66 * Note that currently we expect there to be exactly one module in the
67 * whole program.
68 */
69void ancr_module_process(stree_program_t *prog, stree_module_t *module)
70{
71 list_node_t *node;
72 stree_modm_t *modm;
73
74 (void) module;
75 node = list_first(&prog->module->members);
76
77 while (node != NULL) {
78 modm = list_node_data(node, stree_modm_t *);
79 assert(modm->mc == mc_csi); /* XXX */
80/* printf("ancr_csi_process() on '%s'\n",
81 strtab_get_str(modm->u.csi->name->sid));*/
82 ancr_csi_dfs(prog, modm->u.csi);
83
84 node = list_next(&prog->module->members, node);
85 }
86}
87
88/** Walk CSI node tree depth-first.
89 *
90 * This is the outer depth-first walk whose purpose is to eventually
91 * process all CSI nodes by calling ancr_csi_process() on them.
92 * (Which causes that and possibly some other nodes to be processed).
93 */
94static void ancr_csi_dfs(stree_program_t *prog, stree_csi_t *csi)
95{
96 list_node_t *node;
97 stree_csimbr_t *csimbr;
98
99 /* Process this node first. */
100 ancr_csi_process(prog, csi);
101
102 /* Now visit all children. */
103 node = list_first(&csi->members);
104 while (node != NULL) {
105 csimbr = list_node_data(node, stree_csimbr_t *);
106 if (csimbr->cc == csimbr_csi)
107 ancr_csi_dfs(prog, csimbr->u.csi);
108
109 node = list_next(&csi->members, node);
110 }
111}
112
113/** Process csi node.
114 *
115 * Fist processes the pre-required nodes (outer CSI and base CSIs),
116 * then processes @a node. This is the core 'outward-and-baseward' walk.
117 */
118static void ancr_csi_process(stree_program_t *prog, stree_csi_t *node)
119{
120 stree_symbol_t *base_sym;
121 stree_csi_t *base_csi, *outer_csi;
122
123 if (node->ancr_state == ws_visited) {
124 /* Node already processed */
125 return;
126 }
127
128 if (node->ancr_state == ws_active) {
129 /* Error, closed reference loop. */
130 printf("Error: Circular class, struct or interface chain: ");
131 ancr_csi_print_cycle(prog, node);
132 printf(".\n");
133 exit(1);
134 }
135
136 node->ancr_state = ws_active;
137
138 outer_csi = csi_to_symbol(node)->outer_csi;
139
140 /* Process outer CSI */
141 if (outer_csi != NULL)
142 ancr_csi_process(prog, outer_csi);
143
144 if (node->base_csi_ref != NULL) {
145 /* Resolve base CSI. */
146 base_sym = symbol_xlookup_in_csi(prog, outer_csi,
147 node->base_csi_ref);
148 base_csi = symbol_to_csi(base_sym);
149 assert(base_csi != NULL);
150
151 /* Process base CSI. */
152 ancr_csi_process(prog, base_csi);
153 } else {
154 base_csi = NULL;
155 }
156
157 /* Store base CSI and update node state. */
158 node->ancr_state = ws_visited;
159 node->base_csi = base_csi;
160}
161
162/** Print loop in CSI ancestry.
163 *
164 * We have detected a loop in CSI ancestry. Traverse it (by following the
165 * nodes in ws_active state and print it.
166 */
167static void ancr_csi_print_cycle(stree_program_t *prog, stree_csi_t *node)
168{
169 stree_csi_t *n;
170 stree_symbol_t *base_sym, *node_sym;
171 stree_csi_t *base_csi, *outer_csi;
172
173 n = node;
174 do {
175 node_sym = csi_to_symbol(node);
176 symbol_print_fqn(node_sym);
177 printf(", ");
178
179 outer_csi = node_sym->outer_csi;
180
181 if (outer_csi != NULL && outer_csi->ancr_state == ws_active) {
182 node = outer_csi;
183 } else if (node->base_csi_ref != NULL) {
184 /* Resolve base CSI. */
185 base_sym = symbol_xlookup_in_csi(prog, outer_csi,
186 node->base_csi_ref);
187 base_csi = symbol_to_csi(base_sym);
188 assert(base_csi != NULL);
189
190 assert(base_csi->ancr_state == ws_active);
191 node = base_csi;
192 } else {
193 assert(b_false);
194 }
195 } while (n != node);
196
197 node_sym = csi_to_symbol(node);
198 symbol_print_fqn(node_sym);
199}
Note: See TracBrowser for help on using the repository browser.