| 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 Symbols. */
|
|---|
| 30 |
|
|---|
| 31 | #include <stdlib.h>
|
|---|
| 32 | #include <assert.h>
|
|---|
| 33 | #include "list.h"
|
|---|
| 34 | #include "mytypes.h"
|
|---|
| 35 | #include "strtab.h"
|
|---|
| 36 | #include "stree.h"
|
|---|
| 37 |
|
|---|
| 38 | #include "symbol.h"
|
|---|
| 39 |
|
|---|
| 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);
|
|---|
| 45 |
|
|---|
| 46 | /** Lookup symbol in CSI using a type expression.
|
|---|
| 47 | *
|
|---|
| 48 | * XXX This should be removed in favor of full type expression evaluation
|
|---|
| 49 | * (run_texpr). This cannot work properly with generics.
|
|---|
| 50 | *
|
|---|
| 51 | * @param prog Program
|
|---|
| 52 | * @param scope CSI used as base for relative references
|
|---|
| 53 | * @param texpr Type expression
|
|---|
| 54 | *
|
|---|
| 55 | * @return Symbol referenced by type expression or @c NULL
|
|---|
| 56 | * if not found
|
|---|
| 57 | */
|
|---|
| 58 | stree_symbol_t *symbol_xlookup_in_csi(stree_program_t *prog,
|
|---|
| 59 | stree_csi_t *scope, stree_texpr_t *texpr)
|
|---|
| 60 | {
|
|---|
| 61 | stree_symbol_t *a, *b;
|
|---|
| 62 | stree_csi_t *a_csi;
|
|---|
| 63 |
|
|---|
| 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);
|
|---|
| 73 | }
|
|---|
| 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);
|
|---|
| 78 | }
|
|---|
| 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);
|
|---|
| 85 | }
|
|---|
| 86 | }
|
|---|
| 87 |
|
|---|
| 88 | /** Lookup symbol reference in CSI.
|
|---|
| 89 | *
|
|---|
| 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.
|
|---|
| 92 | *
|
|---|
| 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
|
|---|
| 96 | *
|
|---|
| 97 | * @return Symbol or @c NULL if symbol not found
|
|---|
| 98 | */
|
|---|
| 99 | stree_symbol_t *symbol_lookup_in_csi(stree_program_t *prog, stree_csi_t *scope,
|
|---|
| 100 | stree_ident_t *name)
|
|---|
| 101 | {
|
|---|
| 102 | stree_symbol_t *symbol;
|
|---|
| 103 |
|
|---|
| 104 | /* This CSI node should have been processed. */
|
|---|
| 105 | assert(scope == NULL || scope->ancr_state == ws_visited);
|
|---|
| 106 |
|
|---|
| 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;
|
|---|
| 111 | }
|
|---|
| 112 |
|
|---|
| 113 | if (symbol == NULL)
|
|---|
| 114 | symbol = symbol_search_global(prog, name);
|
|---|
| 115 |
|
|---|
| 116 | return symbol;
|
|---|
| 117 | }
|
|---|
| 118 |
|
|---|
| 119 | /** Look for symbol strictly in CSI.
|
|---|
| 120 | *
|
|---|
| 121 | * Look for symbol in definition of a CSI and its ancestors. (But not
|
|---|
| 122 | * in lexically enclosing CSI.)
|
|---|
| 123 | *
|
|---|
| 124 | * @param prog Program to look in
|
|---|
| 125 | * @param scope CSI in which to look
|
|---|
| 126 | * @param name Identifier of the symbol
|
|---|
| 127 | *
|
|---|
| 128 | * @return Symbol or @c NULL if symbol not found.
|
|---|
| 129 | */
|
|---|
| 130 | stree_symbol_t *symbol_search_csi(stree_program_t *prog,
|
|---|
| 131 | stree_csi_t *scope, stree_ident_t *name)
|
|---|
| 132 | {
|
|---|
| 133 | stree_csi_t *base_csi;
|
|---|
| 134 | stree_symbol_t *symbol;
|
|---|
| 135 |
|
|---|
| 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;
|
|---|
| 140 |
|
|---|
| 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);
|
|---|
| 145 |
|
|---|
| 146 | /* No match */
|
|---|
| 147 | return NULL;
|
|---|
| 148 | }
|
|---|
| 149 |
|
|---|
| 150 | /** Look for symbol strictly in CSI.
|
|---|
| 151 | *
|
|---|
| 152 | * Look for symbol in definition of a CSI and its ancestors. (But not
|
|---|
| 153 | * in lexically enclosing CSI or in base CSI.)
|
|---|
| 154 | *
|
|---|
| 155 | * @param prog Program to look in
|
|---|
| 156 | * @param scope CSI in which to look
|
|---|
| 157 | * @param name Identifier of the symbol
|
|---|
| 158 | *
|
|---|
| 159 | * @return Symbol or @c NULL if symbol not found.
|
|---|
| 160 | */
|
|---|
| 161 | stree_symbol_t *symbol_search_csi_no_base(stree_program_t *prog,
|
|---|
| 162 | stree_csi_t *scope, stree_ident_t *name)
|
|---|
| 163 | {
|
|---|
| 164 | list_node_t *node;
|
|---|
| 165 | stree_csimbr_t *csimbr;
|
|---|
| 166 | stree_ident_t *mbr_name;
|
|---|
| 167 |
|
|---|
| 168 | (void) prog;
|
|---|
| 169 |
|
|---|
| 170 | /* Look in new members in this class. */
|
|---|
| 171 |
|
|---|
| 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);
|
|---|
| 176 |
|
|---|
| 177 | if (name->sid == mbr_name->sid) {
|
|---|
| 178 | /* Match */
|
|---|
| 179 | return csimbr_to_symbol(csimbr);
|
|---|
| 180 | }
|
|---|
| 181 |
|
|---|
| 182 | node = list_next(&scope->members, node);
|
|---|
| 183 | }
|
|---|
| 184 | /* No match */
|
|---|
| 185 | return NULL;
|
|---|
| 186 | }
|
|---|
| 187 |
|
|---|
| 188 | /** Look for symbol in global scope.
|
|---|
| 189 | *
|
|---|
| 190 | * @param prog Program to look in
|
|---|
| 191 | * @param name Identifier of the symbol
|
|---|
| 192 | *
|
|---|
| 193 | * @return Symbol or @c NULL if symbol not found.
|
|---|
| 194 | */
|
|---|
| 195 | static stree_symbol_t *symbol_search_global(stree_program_t *prog,
|
|---|
| 196 | stree_ident_t *name)
|
|---|
| 197 | {
|
|---|
| 198 | list_node_t *node;
|
|---|
| 199 | stree_modm_t *modm;
|
|---|
| 200 | stree_symbol_t *symbol;
|
|---|
| 201 | stree_ident_t *mbr_name;
|
|---|
| 202 |
|
|---|
| 203 | node = list_first(&prog->module->members);
|
|---|
| 204 | while (node != NULL) {
|
|---|
| 205 | modm = list_node_data(node, stree_modm_t *);
|
|---|
| 206 |
|
|---|
| 207 | /* Make compiler happy. */
|
|---|
| 208 | mbr_name = NULL;
|
|---|
| 209 |
|
|---|
| 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;
|
|---|
| 217 | }
|
|---|
| 218 |
|
|---|
| 219 | /* The Clang static analyzer is just too picky. */
|
|---|
| 220 | assert(mbr_name != NULL);
|
|---|
| 221 |
|
|---|
| 222 | if (name->sid == mbr_name->sid) {
|
|---|
| 223 | /* Make compiler happy. */
|
|---|
| 224 | symbol = NULL;
|
|---|
| 225 |
|
|---|
| 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;
|
|---|
| 234 | }
|
|---|
| 235 | return symbol;
|
|---|
| 236 | }
|
|---|
| 237 | node = list_next(&prog->module->members, node);
|
|---|
| 238 | }
|
|---|
| 239 |
|
|---|
| 240 | return NULL;
|
|---|
| 241 | }
|
|---|
| 242 |
|
|---|
| 243 | /** Get explicit base class for a CSI.
|
|---|
| 244 | *
|
|---|
| 245 | * Note that if there is no explicit base class (class is derived implicitly
|
|---|
| 246 | * from @c object, then @c NULL is returned.
|
|---|
| 247 | *
|
|---|
| 248 | * @param prog Program to look in
|
|---|
| 249 | * @param csi CSI
|
|---|
| 250 | *
|
|---|
| 251 | * @return Base class (CSI) or @c NULL if no explicit base class.
|
|---|
| 252 | */
|
|---|
| 253 | stree_csi_t *symbol_get_base_class(stree_program_t *prog, stree_csi_t *csi)
|
|---|
| 254 | {
|
|---|
| 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;
|
|---|
| 260 |
|
|---|
| 261 | outer_csi = csi_to_symbol(csi)->outer_csi;
|
|---|
| 262 |
|
|---|
| 263 | pred_n = list_first(&csi->inherit);
|
|---|
| 264 | if (pred_n == NULL)
|
|---|
| 265 | return NULL;
|
|---|
| 266 |
|
|---|
| 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! */
|
|---|
| 271 |
|
|---|
| 272 | if (pred_csi->cc == csi_class)
|
|---|
| 273 | return pred_csi;
|
|---|
| 274 |
|
|---|
| 275 | return NULL;
|
|---|
| 276 | }
|
|---|
| 277 |
|
|---|
| 278 | /** Get type expression referencing base class for a CSI.
|
|---|
| 279 | *
|
|---|
| 280 | * Note that if there is no explicit base class (class is derived implicitly
|
|---|
| 281 | * from @c object, then @c NULL is returned.
|
|---|
| 282 | *
|
|---|
| 283 | * @param prog Program to look in
|
|---|
| 284 | * @param csi CSI
|
|---|
| 285 | *
|
|---|
| 286 | * @return Type expression or @c NULL if no explicit base class.
|
|---|
| 287 | */
|
|---|
| 288 | stree_texpr_t *symbol_get_base_class_ref(stree_program_t *prog,
|
|---|
| 289 | stree_csi_t *csi)
|
|---|
| 290 | {
|
|---|
| 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;
|
|---|
| 296 |
|
|---|
| 297 | outer_csi = csi_to_symbol(csi)->outer_csi;
|
|---|
| 298 |
|
|---|
| 299 | pred_n = list_first(&csi->inherit);
|
|---|
| 300 | if (pred_n == NULL)
|
|---|
| 301 | return NULL;
|
|---|
| 302 |
|
|---|
| 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! */
|
|---|
| 307 |
|
|---|
| 308 | if (pred_csi->cc == csi_class)
|
|---|
| 309 | return pred;
|
|---|
| 310 |
|
|---|
| 311 | return NULL;
|
|---|
| 312 | }
|
|---|
| 313 |
|
|---|
| 314 | /** Find entry point.
|
|---|
| 315 | *
|
|---|
| 316 | * Perform a walk of all CSIs and look for a function with the name @a name.
|
|---|
| 317 | *
|
|---|
| 318 | * @param prog Program to look in
|
|---|
| 319 | * @param name Name of entry point
|
|---|
| 320 | *
|
|---|
| 321 | * @return Symbol or @c NULL if symbol not found.
|
|---|
| 322 | */
|
|---|
| 323 | stree_symbol_t *symbol_find_epoint(stree_program_t *prog, stree_ident_t *name)
|
|---|
| 324 | {
|
|---|
| 325 | list_node_t *node;
|
|---|
| 326 | stree_modm_t *modm;
|
|---|
| 327 | stree_symbol_t *entry, *etmp;
|
|---|
| 328 |
|
|---|
| 329 | entry = NULL;
|
|---|
| 330 |
|
|---|
| 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);
|
|---|
| 340 | }
|
|---|
| 341 | entry = etmp;
|
|---|
| 342 | }
|
|---|
| 343 | }
|
|---|
| 344 | node = list_next(&prog->module->members, node);
|
|---|
| 345 | }
|
|---|
| 346 |
|
|---|
| 347 | return entry;
|
|---|
| 348 | }
|
|---|
| 349 |
|
|---|
| 350 | /** Find entry point under CSI.
|
|---|
| 351 | *
|
|---|
| 352 | * Internal part of symbol_find_epoint() that recursively walks CSIs.
|
|---|
| 353 | *
|
|---|
| 354 | * @param prog Program to look in
|
|---|
| 355 | * @param name Name of entry point
|
|---|
| 356 | *
|
|---|
| 357 | * @return Symbol or @c NULL if symbol not found.
|
|---|
| 358 | */
|
|---|
| 359 | static stree_symbol_t *symbol_find_epoint_rec(stree_program_t *prog,
|
|---|
| 360 | stree_ident_t *name, stree_csi_t *csi)
|
|---|
| 361 | {
|
|---|
| 362 | list_node_t *node;
|
|---|
| 363 | stree_csimbr_t *csimbr;
|
|---|
| 364 | stree_symbol_t *entry, *etmp;
|
|---|
| 365 | stree_symbol_t *fun_sym;
|
|---|
| 366 |
|
|---|
| 367 | entry = NULL;
|
|---|
| 368 |
|
|---|
| 369 | node = list_first(&csi->members);
|
|---|
| 370 | while (node != NULL) {
|
|---|
| 371 | csimbr = list_node_data(node, stree_csimbr_t *);
|
|---|
| 372 |
|
|---|
| 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);
|
|---|
| 380 | }
|
|---|
| 381 | entry = etmp;
|
|---|
| 382 | }
|
|---|
| 383 | break;
|
|---|
| 384 | case csimbr_fun:
|
|---|
| 385 | fun_sym = fun_to_symbol(csimbr->u.fun);
|
|---|
| 386 |
|
|---|
| 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);
|
|---|
| 392 | }
|
|---|
| 393 | entry = fun_sym;
|
|---|
| 394 | }
|
|---|
| 395 | default:
|
|---|
| 396 | break;
|
|---|
| 397 | }
|
|---|
| 398 |
|
|---|
| 399 | node = list_next(&csi->members, node);
|
|---|
| 400 | }
|
|---|
| 401 |
|
|---|
| 402 | return entry;
|
|---|
| 403 | }
|
|---|
| 404 |
|
|---|
| 405 | /*
|
|---|
| 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.
|
|---|
| 410 | */
|
|---|
| 411 |
|
|---|
| 412 | /** Convert symbol to delegate (base to derived).
|
|---|
| 413 | *
|
|---|
| 414 | * @param symbol Symbol
|
|---|
| 415 | * @return Delegate or @c NULL if symbol is not a delegate
|
|---|
| 416 | */
|
|---|
| 417 | stree_deleg_t *symbol_to_deleg(stree_symbol_t *symbol)
|
|---|
| 418 | {
|
|---|
| 419 | if (symbol->sc != sc_deleg)
|
|---|
| 420 | return NULL;
|
|---|
| 421 |
|
|---|
| 422 | return symbol->u.deleg;
|
|---|
| 423 | }
|
|---|
| 424 |
|
|---|
| 425 | /** Convert delegate to symbol (derived to base).
|
|---|
| 426 | *
|
|---|
| 427 | * @param deleg Delegate
|
|---|
| 428 | * @return Symbol
|
|---|
| 429 | */
|
|---|
| 430 | stree_symbol_t *deleg_to_symbol(stree_deleg_t *deleg)
|
|---|
| 431 | {
|
|---|
| 432 | assert(deleg->symbol);
|
|---|
| 433 | return deleg->symbol;
|
|---|
| 434 | }
|
|---|
| 435 |
|
|---|
| 436 | /** Convert symbol to enum (base to derived).
|
|---|
| 437 | *
|
|---|
| 438 | * @param symbol Symbol
|
|---|
| 439 | * @return Enum or @c NULL if symbol is not a enum
|
|---|
| 440 | */
|
|---|
| 441 | stree_enum_t *symbol_to_enum(stree_symbol_t *symbol)
|
|---|
| 442 | {
|
|---|
| 443 | if (symbol->sc != sc_enum)
|
|---|
| 444 | return NULL;
|
|---|
| 445 |
|
|---|
| 446 | return symbol->u.enum_d;
|
|---|
| 447 | }
|
|---|
| 448 |
|
|---|
| 449 | /** Convert enum to symbol (derived to base).
|
|---|
| 450 | *
|
|---|
| 451 | * @param deleg Enum
|
|---|
| 452 | * @return Symbol
|
|---|
| 453 | */
|
|---|
| 454 | stree_symbol_t *enum_to_symbol(stree_enum_t *enum_d)
|
|---|
| 455 | {
|
|---|
| 456 | assert(enum_d->symbol);
|
|---|
| 457 | return enum_d->symbol;
|
|---|
| 458 | }
|
|---|
| 459 |
|
|---|
| 460 | /** Convert symbol to CSI (base to derived).
|
|---|
| 461 | *
|
|---|
| 462 | * @param symbol Symbol
|
|---|
| 463 | * @return CSI or @c NULL if symbol is not a CSI
|
|---|
| 464 | */
|
|---|
| 465 | stree_csi_t *symbol_to_csi(stree_symbol_t *symbol)
|
|---|
| 466 | {
|
|---|
| 467 | if (symbol->sc != sc_csi)
|
|---|
| 468 | return NULL;
|
|---|
| 469 |
|
|---|
| 470 | return symbol->u.csi;
|
|---|
| 471 | }
|
|---|
| 472 |
|
|---|
| 473 | /** Convert CSI to symbol (derived to base).
|
|---|
| 474 | *
|
|---|
| 475 | * @param csi CSI
|
|---|
| 476 | * @return Symbol
|
|---|
| 477 | */
|
|---|
| 478 | stree_symbol_t *csi_to_symbol(stree_csi_t *csi)
|
|---|
| 479 | {
|
|---|
| 480 | assert(csi->symbol);
|
|---|
| 481 | return csi->symbol;
|
|---|
| 482 | }
|
|---|
| 483 |
|
|---|
| 484 | /** Convert symbol to constructor (base to derived).
|
|---|
| 485 | *
|
|---|
| 486 | * @param symbol Symbol
|
|---|
| 487 | * @return Constructor or @c NULL if symbol is not a constructor
|
|---|
| 488 | */
|
|---|
| 489 | stree_ctor_t *symbol_to_ctor(stree_symbol_t *symbol)
|
|---|
| 490 | {
|
|---|
| 491 | if (symbol->sc != sc_ctor)
|
|---|
| 492 | return NULL;
|
|---|
| 493 |
|
|---|
| 494 | return symbol->u.ctor;
|
|---|
| 495 | }
|
|---|
| 496 |
|
|---|
| 497 | /** Convert constructor to symbol (derived to base).
|
|---|
| 498 | *
|
|---|
| 499 | * @param ctor Constructor
|
|---|
| 500 | * @return Symbol
|
|---|
| 501 | */
|
|---|
| 502 | stree_symbol_t *ctor_to_symbol(stree_ctor_t *ctor)
|
|---|
| 503 | {
|
|---|
| 504 | assert(ctor->symbol);
|
|---|
| 505 | return ctor->symbol;
|
|---|
| 506 | }
|
|---|
| 507 |
|
|---|
| 508 | /** Convert symbol to function (base to derived).
|
|---|
| 509 | *
|
|---|
| 510 | * @param symbol Symbol
|
|---|
| 511 | * @return Function or @c NULL if symbol is not a function
|
|---|
| 512 | */
|
|---|
| 513 | stree_fun_t *symbol_to_fun(stree_symbol_t *symbol)
|
|---|
| 514 | {
|
|---|
| 515 | if (symbol->sc != sc_fun)
|
|---|
| 516 | return NULL;
|
|---|
| 517 |
|
|---|
| 518 | return symbol->u.fun;
|
|---|
| 519 | }
|
|---|
| 520 |
|
|---|
| 521 | /** Convert function to symbol (derived to base).
|
|---|
| 522 | *
|
|---|
| 523 | * @param fun Function
|
|---|
| 524 | * @return Symbol
|
|---|
| 525 | */
|
|---|
| 526 | stree_symbol_t *fun_to_symbol(stree_fun_t *fun)
|
|---|
| 527 | {
|
|---|
| 528 | assert(fun->symbol);
|
|---|
| 529 | return fun->symbol;
|
|---|
| 530 | }
|
|---|
| 531 |
|
|---|
| 532 | /** Convert symbol to member variable (base to derived).
|
|---|
| 533 | *
|
|---|
| 534 | * @param symbol Symbol
|
|---|
| 535 | * @return Variable or @c NULL if symbol is not a member variable
|
|---|
| 536 | */
|
|---|
| 537 | stree_var_t *symbol_to_var(stree_symbol_t *symbol)
|
|---|
| 538 | {
|
|---|
| 539 | if (symbol->sc != sc_var)
|
|---|
| 540 | return NULL;
|
|---|
| 541 |
|
|---|
| 542 | return symbol->u.var;
|
|---|
| 543 | }
|
|---|
| 544 |
|
|---|
| 545 | /** Convert variable to symbol (derived to base).
|
|---|
| 546 | *
|
|---|
| 547 | * @param fun Variable
|
|---|
| 548 | * @return Symbol
|
|---|
| 549 | */
|
|---|
| 550 | stree_symbol_t *var_to_symbol(stree_var_t *var)
|
|---|
| 551 | {
|
|---|
| 552 | assert(var->symbol);
|
|---|
| 553 | return var->symbol;
|
|---|
| 554 | }
|
|---|
| 555 |
|
|---|
| 556 | /** Convert symbol to property (base to derived).
|
|---|
| 557 | *
|
|---|
| 558 | * @param symbol Symbol
|
|---|
| 559 | * @return Property or @c NULL if symbol is not a property
|
|---|
| 560 | */
|
|---|
| 561 | stree_prop_t *symbol_to_prop(stree_symbol_t *symbol)
|
|---|
| 562 | {
|
|---|
| 563 | if (symbol->sc != sc_prop)
|
|---|
| 564 | return NULL;
|
|---|
| 565 |
|
|---|
| 566 | return symbol->u.prop;
|
|---|
| 567 | }
|
|---|
| 568 |
|
|---|
| 569 | /** Get symbol from CSI member.
|
|---|
| 570 | *
|
|---|
| 571 | * A symbol corresponds to any CSI member. Return it.
|
|---|
| 572 | *
|
|---|
| 573 | * @param csimbr CSI member
|
|---|
| 574 | * @return Symbol
|
|---|
| 575 | */
|
|---|
| 576 | stree_symbol_t *csimbr_to_symbol(stree_csimbr_t *csimbr)
|
|---|
| 577 | {
|
|---|
| 578 | stree_symbol_t *symbol;
|
|---|
| 579 |
|
|---|
| 580 | /* Keep compiler happy. */
|
|---|
| 581 | symbol = NULL;
|
|---|
| 582 |
|
|---|
| 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;
|
|---|
| 606 | }
|
|---|
| 607 |
|
|---|
| 608 | return symbol;
|
|---|
| 609 | }
|
|---|
| 610 |
|
|---|
| 611 | /** Convert property to symbol (derived to base).
|
|---|
| 612 | *
|
|---|
| 613 | * @param fun Property
|
|---|
| 614 | * @return Symbol
|
|---|
| 615 | */
|
|---|
| 616 | stree_symbol_t *prop_to_symbol(stree_prop_t *prop)
|
|---|
| 617 | {
|
|---|
| 618 | assert(prop->symbol);
|
|---|
| 619 | return prop->symbol;
|
|---|
| 620 | }
|
|---|
| 621 |
|
|---|
| 622 | /** Print fully qualified name of symbol.
|
|---|
| 623 | *
|
|---|
| 624 | * @param symbol Symbol
|
|---|
| 625 | */
|
|---|
| 626 | void symbol_print_fqn(stree_symbol_t *symbol)
|
|---|
| 627 | {
|
|---|
| 628 | stree_ident_t *name;
|
|---|
| 629 | stree_symbol_t *outer_sym;
|
|---|
| 630 |
|
|---|
| 631 | if (symbol->outer_csi != NULL) {
|
|---|
| 632 | outer_sym = csi_to_symbol(symbol->outer_csi);
|
|---|
| 633 | symbol_print_fqn(outer_sym);
|
|---|
| 634 | printf(".");
|
|---|
| 635 | }
|
|---|
| 636 |
|
|---|
| 637 | name = symbol_get_ident(symbol);
|
|---|
| 638 | printf("%s", strtab_get_str(name->sid));
|
|---|
| 639 | }
|
|---|
| 640 |
|
|---|
| 641 | /** Return symbol identifier.
|
|---|
| 642 | *
|
|---|
| 643 | * @param symbol Symbol
|
|---|
| 644 | * @return Symbol identifier
|
|---|
| 645 | */
|
|---|
| 646 | static stree_ident_t *symbol_get_ident(stree_symbol_t *symbol)
|
|---|
| 647 | {
|
|---|
| 648 | stree_ident_t *ident;
|
|---|
| 649 |
|
|---|
| 650 | /* Make compiler happy. */
|
|---|
| 651 | ident = NULL;
|
|---|
| 652 |
|
|---|
| 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;
|
|---|
| 675 | }
|
|---|
| 676 |
|
|---|
| 677 | return ident;
|
|---|
| 678 | }
|
|---|