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