[09ababb7] | 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);
|
---|
[fa36f29] | 42 | static stree_symbol_t *symbol_find_epoint_rec(stree_program_t *prog,
|
---|
| 43 | stree_ident_t *name, stree_csi_t *csi);
|
---|
[09ababb7] | 44 | static stree_ident_t *symbol_get_ident(stree_symbol_t *symbol);
|
---|
| 45 |
|
---|
[38aaacc2] | 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 | */
|
---|
[09ababb7] | 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:
|
---|
[074444f] | 81 | return symbol_xlookup_in_csi(prog, scope,
|
---|
| 82 | texpr->u.tapply->gtype);
|
---|
[09ababb7] | 83 | default:
|
---|
| 84 | assert(b_false);
|
---|
| 85 | }
|
---|
| 86 | }
|
---|
| 87 |
|
---|
[1ebc1a62] | 88 | /** Lookup symbol reference in CSI.
|
---|
[051bc69a] | 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.
|
---|
[1ebc1a62] | 92 | *
|
---|
[38aaacc2] | 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
|
---|
[1ebc1a62] | 96 | *
|
---|
[38aaacc2] | 97 | * @return Symbol or @c NULL if symbol not found
|
---|
[1ebc1a62] | 98 | */
|
---|
[09ababb7] | 99 | stree_symbol_t *symbol_lookup_in_csi(stree_program_t *prog, stree_csi_t *scope,
|
---|
[1433ecda] | 100 | stree_ident_t *name)
|
---|
[09ababb7] | 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.)
|
---|
[38aaacc2] | 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.
|
---|
[09ababb7] | 129 | */
|
---|
| 130 | stree_symbol_t *symbol_search_csi(stree_program_t *prog,
|
---|
| 131 | stree_csi_t *scope, stree_ident_t *name)
|
---|
[051bc69a] | 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. */
|
---|
[c5cb943d] | 142 | base_csi = symbol_get_base_class(prog, scope);
|
---|
| 143 | if (base_csi != NULL)
|
---|
[051bc69a] | 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)
|
---|
[09ababb7] | 163 | {
|
---|
| 164 | list_node_t *node;
|
---|
| 165 | stree_csimbr_t *csimbr;
|
---|
| 166 | stree_ident_t *mbr_name;
|
---|
[fa36f29] | 167 |
|
---|
| 168 | (void) prog;
|
---|
| 169 |
|
---|
| 170 | /* Look in new members in this class. */
|
---|
[09ababb7] | 171 |
|
---|
| 172 | node = list_first(&scope->members);
|
---|
| 173 | while (node != NULL) {
|
---|
| 174 | csimbr = list_node_data(node, stree_csimbr_t *);
|
---|
[051bc69a] | 175 | mbr_name = stree_csimbr_get_name(csimbr);
|
---|
[09ababb7] | 176 |
|
---|
| 177 | if (name->sid == mbr_name->sid) {
|
---|
| 178 | /* Match */
|
---|
[051bc69a] | 179 | return csimbr_to_symbol(csimbr);
|
---|
[09ababb7] | 180 | }
|
---|
| 181 |
|
---|
[051bc69a] | 182 | node = list_next(&scope->members, node);
|
---|
[fa36f29] | 183 | }
|
---|
| 184 | /* No match */
|
---|
[09ababb7] | 185 | return NULL;
|
---|
| 186 | }
|
---|
| 187 |
|
---|
[38aaacc2] | 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 | */
|
---|
[09ababb7] | 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;
|
---|
[051bc69a] | 201 | stree_ident_t *mbr_name;
|
---|
[09ababb7] | 202 |
|
---|
| 203 | node = list_first(&prog->module->members);
|
---|
| 204 | while (node != NULL) {
|
---|
| 205 | modm = list_node_data(node, stree_modm_t *);
|
---|
[051bc69a] | 206 |
|
---|
[c5cb943d] | 207 | /* Make compiler happy. */
|
---|
| 208 | mbr_name = NULL;
|
---|
| 209 |
|
---|
[051bc69a] | 210 | switch (modm->mc) {
|
---|
[1433ecda] | 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;
|
---|
[051bc69a] | 217 | }
|
---|
| 218 |
|
---|
[c5cb943d] | 219 | /* The Clang static analyzer is just too picky. */
|
---|
| 220 | assert(mbr_name != NULL);
|
---|
| 221 |
|
---|
[051bc69a] | 222 | if (name->sid == mbr_name->sid) {
|
---|
[c5cb943d] | 223 | /* Make compiler happy. */
|
---|
| 224 | symbol = NULL;
|
---|
| 225 |
|
---|
[09ababb7] | 226 | /* Match */
|
---|
| 227 | switch (modm->mc) {
|
---|
| 228 | case mc_csi:
|
---|
| 229 | symbol = csi_to_symbol(modm->u.csi);
|
---|
| 230 | break;
|
---|
[051bc69a] | 231 | case mc_enum:
|
---|
| 232 | symbol = enum_to_symbol(modm->u.enum_d);
|
---|
| 233 | break;
|
---|
[09ababb7] | 234 | }
|
---|
| 235 | return symbol;
|
---|
| 236 | }
|
---|
| 237 | node = list_next(&prog->module->members, node);
|
---|
| 238 | }
|
---|
| 239 |
|
---|
| 240 | return NULL;
|
---|
| 241 | }
|
---|
| 242 |
|
---|
[c5cb943d] | 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 |
|
---|
[38aaacc2] | 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 | */
|
---|
[fa36f29] | 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 | }
|
---|
[1433ecda] | 344 | node = list_next(&prog->module->members, node);
|
---|
[fa36f29] | 345 | }
|
---|
| 346 |
|
---|
| 347 | return entry;
|
---|
| 348 | }
|
---|
| 349 |
|
---|
[38aaacc2] | 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 | */
|
---|
[fa36f29] | 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;
|
---|
[c5cb943d] | 365 | stree_symbol_t *fun_sym;
|
---|
[fa36f29] | 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:
|
---|
[c5cb943d] | 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)) {
|
---|
[fa36f29] | 389 | if (entry != NULL) {
|
---|
| 390 | printf("Error: Duplicate entry point.\n");
|
---|
| 391 | exit(1);
|
---|
| 392 | }
|
---|
[c5cb943d] | 393 | entry = fun_sym;
|
---|
[fa36f29] | 394 | }
|
---|
| 395 | default:
|
---|
| 396 | break;
|
---|
| 397 | }
|
---|
| 398 |
|
---|
[1433ecda] | 399 | node = list_next(&csi->members, node);
|
---|
[fa36f29] | 400 | }
|
---|
| 401 |
|
---|
| 402 | return entry;
|
---|
| 403 | }
|
---|
| 404 |
|
---|
[38aaacc2] | 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 |
|
---|
[051bc69a] | 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 |
|
---|
[38aaacc2] | 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 | */
|
---|
[09ababb7] | 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 |
|
---|
[38aaacc2] | 473 | /** Convert CSI to symbol (derived to base).
|
---|
| 474 | *
|
---|
| 475 | * @param csi CSI
|
---|
| 476 | * @return Symbol
|
---|
| 477 | */
|
---|
[09ababb7] | 478 | stree_symbol_t *csi_to_symbol(stree_csi_t *csi)
|
---|
| 479 | {
|
---|
| 480 | assert(csi->symbol);
|
---|
| 481 | return csi->symbol;
|
---|
| 482 | }
|
---|
| 483 |
|
---|
[051bc69a] | 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 |
|
---|
[38aaacc2] | 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 | */
|
---|
[09ababb7] | 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 |
|
---|
[38aaacc2] | 521 | /** Convert function to symbol (derived to base).
|
---|
| 522 | *
|
---|
| 523 | * @param fun Function
|
---|
| 524 | * @return Symbol
|
---|
| 525 | */
|
---|
[09ababb7] | 526 | stree_symbol_t *fun_to_symbol(stree_fun_t *fun)
|
---|
| 527 | {
|
---|
| 528 | assert(fun->symbol);
|
---|
| 529 | return fun->symbol;
|
---|
| 530 | }
|
---|
| 531 |
|
---|
[38aaacc2] | 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 | */
|
---|
[09ababb7] | 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 |
|
---|
[38aaacc2] | 545 | /** Convert variable to symbol (derived to base).
|
---|
| 546 | *
|
---|
| 547 | * @param fun Variable
|
---|
| 548 | * @return Symbol
|
---|
| 549 | */
|
---|
[09ababb7] | 550 | stree_symbol_t *var_to_symbol(stree_var_t *var)
|
---|
| 551 | {
|
---|
| 552 | assert(var->symbol);
|
---|
| 553 | return var->symbol;
|
---|
| 554 | }
|
---|
| 555 |
|
---|
[38aaacc2] | 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 | */
|
---|
[09ababb7] | 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 |
|
---|
[051bc69a] | 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 | */
|
---|
[c5cb943d] | 576 | stree_symbol_t *csimbr_to_symbol(stree_csimbr_t *csimbr)
|
---|
[051bc69a] | 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 |
|
---|
[38aaacc2] | 611 | /** Convert property to symbol (derived to base).
|
---|
| 612 | *
|
---|
| 613 | * @param fun Property
|
---|
| 614 | * @return Symbol
|
---|
| 615 | */
|
---|
[09ababb7] | 616 | stree_symbol_t *prop_to_symbol(stree_prop_t *prop)
|
---|
| 617 | {
|
---|
| 618 | assert(prop->symbol);
|
---|
| 619 | return prop->symbol;
|
---|
| 620 | }
|
---|
| 621 |
|
---|
[38aaacc2] | 622 | /** Print fully qualified name of symbol.
|
---|
| 623 | *
|
---|
| 624 | * @param symbol Symbol
|
---|
| 625 | */
|
---|
[39e8406] | 626 | void symbol_print_fqn(stree_symbol_t *symbol)
|
---|
[09ababb7] | 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);
|
---|
[37f527b] | 633 | symbol_print_fqn(outer_sym);
|
---|
[09ababb7] | 634 | printf(".");
|
---|
| 635 | }
|
---|
| 636 |
|
---|
| 637 | name = symbol_get_ident(symbol);
|
---|
| 638 | printf("%s", strtab_get_str(name->sid));
|
---|
| 639 | }
|
---|
| 640 |
|
---|
[38aaacc2] | 641 | /** Return symbol identifier.
|
---|
| 642 | *
|
---|
| 643 | * @param symbol Symbol
|
---|
| 644 | * @return Symbol identifier
|
---|
| 645 | */
|
---|
[09ababb7] | 646 | static stree_ident_t *symbol_get_ident(stree_symbol_t *symbol)
|
---|
| 647 | {
|
---|
| 648 | stree_ident_t *ident;
|
---|
| 649 |
|
---|
[c5cb943d] | 650 | /* Make compiler happy. */
|
---|
| 651 | ident = NULL;
|
---|
| 652 |
|
---|
[09ababb7] | 653 | switch (symbol->sc) {
|
---|
[1433ecda] | 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;
|
---|
[09ababb7] | 675 | }
|
---|
| 676 |
|
---|
| 677 | return ident;
|
---|
| 678 | }
|
---|