PageRenderTime 122ms CodeModel.GetById 18ms app.highlight 92ms RepoModel.GetById 1ms app.codeStats 0ms

/trunk/Source/Swig/symbol.c

#
C | 2016 lines | 1339 code | 164 blank | 513 comment | 335 complexity | 96bcfc571e5015b213f27a0279366e5e MD5 | raw file

Large files files are truncated, but you can click here to view the full file

   1/* -----------------------------------------------------------------------------
   2 * This file is part of SWIG, which is licensed as a whole under version 3 
   3 * (or any later version) of the GNU General Public License. Some additional
   4 * terms also apply to certain portions of SWIG. The full details of the SWIG
   5 * license and copyrights can be found in the LICENSE and COPYRIGHT files
   6 * included with the SWIG source code as distributed by the SWIG developers
   7 * and at http://www.swig.org/legal.html.
   8 *
   9 * symbol.c
  10 *
  11 * This file implements the SWIG symbol table.  See details below.
  12 * ----------------------------------------------------------------------------- */
  13
  14char cvsroot_symbol_c[] = "$Id: symbol.c 12831 2011-11-01 23:06:53Z wsfulton $";
  15
  16#include "swig.h"
  17#include "swigwarn.h"
  18#include <ctype.h>
  19
  20/* #define SWIG_DEBUG*/
  21/* -----------------------------------------------------------------------------
  22 * Synopsis
  23 *
  24 * This module provides symbol table management for all of SWIG.  In previous
  25 * releases, the management of symbols was rather haphazard.  This module tries
  26 * to correct that.
  27 *
  28 * All symbols are associated with simple identifiers.  For example, here are some
  29 * declarations that generate symbol table entries:
  30 *
  31 *  decl                                    symbol
  32 *  --------------                          ------------
  33 *  void foo(int);                          foo
  34 *  int  x;                                 x
  35 *  typedef int *blah;                      blah
  36 *
  37 * Associated with each symbol is a Hash table that can contain any set of
  38 * attributes that make sense for that object.  For example:
  39 *
  40 *  typedef int *blah;             ---->    "name" : 'blah'
  41 *                                          "type" : 'int'
  42 *                                          "decl" : 'p.'
  43 *                                       "storage" : 'typedef'          
  44 * 
  45 * In some cases, the symbol table needs to manage overloaded entries.  For instance,
  46 * overloaded functions.  In this case, a linked list is built.  The "sym:nextSibling"
  47 * attribute is reserved to hold a link to the next entry.  For example:
  48 *
  49 * int foo(int);            --> "name" : "foo"         "name" : "foo"
  50 * int foo(int,double);         "type" : "int"         "type" : "int" 
  51 *                              "decl" : "f(int)."     "decl" : "f(int,double)."
  52 *                               ...                    ...
  53 *                   "sym:nextSibling" :  --------> "sym:nextSibling": --------> ...
  54 *
  55 * When more than one symbol has the same name, the symbol declarator is 
  56 * used to detect duplicates.  For example, in the above case, foo(int) and
  57 * foo(int,double) are different because their "decl" attribute is different.
  58 * However, if a third declaration "foo(int)" was made, it would generate a 
  59 * conflict (due to having a declarator that matches a previous entry).
  60 *
  61 * Structures and classes:
  62 *
  63 * C/C++ symbol tables are normally managed in a few different spaces.  The
  64 * most visible namespace is reserved for functions, variables, typedef, enum values
  65 * and such.  In C, a separate tag-space is reserved for 'struct name', 'class name',
  66 * and 'union name' declarations.   In SWIG, a single namespace is used for everything
  67 * this means that certain incompatibilities will arise with some C programs. For instance:
  68 *
  69 *        struct Foo {
  70 *             ...
  71 *        }
  72 *
  73 *        int Foo();       // Error. Name clash.  Works in C though 
  74 * 
  75 * Due to the unified namespace for structures, special handling is performed for
  76 * the following:
  77 *
  78 *        typedef struct Foo {
  79 *
  80 *        } Foo;
  81 * 
  82 * In this case, the symbol table contains an entry for the structure itself.  The
  83 * typedef is left out of the symbol table.
  84 *
  85 * Target language vs C:
  86 *
  87 * The symbol tables are normally managed *in the namespace of the target language*.
  88 * This means that name-collisions can be resolved using %rename and related 
  89 * directives.   A quirk of this is that sometimes the symbol tables need to
  90 * be used for C type resolution as well.  To handle this, each symbol table
  91 * also has a C-symbol table lurking behind the scenes.  This is used to locate 
  92 * symbols in the C namespace.  However, this symbol table is not used for error 
  93 * reporting nor is it used for anything else during code generation.
  94 *
  95 * Symbol table structure:
  96 *
  97 * Symbol tables themselves are a special kind of node that is organized just like
  98 * a normal parse tree node.  Symbol tables are organized in a tree that can be
  99 * traversed using the SWIG-DOM API. The following attributes names are reserved.
 100 *
 101 *     name           -- Name of the scope defined by the symbol table (if any)
 102 *                       This name is the C-scope name and is not affected by
 103 *                       %renaming operations
 104 *     symtab         -- Hash table mapping identifiers to nodes.
 105 *     csymtab        -- Hash table mapping C identifiers to nodes.
 106 *
 107 * Reserved attributes on symbol objects:
 108 *
 109 * When a symbol is placed in the symbol table, the following attributes
 110 * are set:
 111 *       
 112 *     sym:name             -- Symbol name
 113 *     sym:nextSibling      -- Next symbol (if overloaded)
 114 *     sym:previousSibling  -- Previous symbol (if overloaded)
 115 *     sym:symtab           -- Symbol table object holding the symbol
 116 *     sym:overloaded       -- Set to the first symbol if overloaded
 117 *
 118 * These names are modeled after XML namespaces.  In particular, every attribute 
 119 * pertaining to symbol table management is prefaced by the "sym:" prefix.   
 120 *
 121 * An example dump of the parse tree showing symbol table entries for the 
 122 * following code should clarify this:
 123 *
 124 *   namespace OuterNamespace {
 125 *       namespace InnerNamespace {
 126 *           class Class {
 127 *           };
 128 *           struct Struct {
 129 *               int Var;
 130 *           };
 131 *       }
 132 *    }
 133 *
 134 *   +++ namespace ----------------------------------------
 135 *   | sym:name     - "OuterNamespace"
 136 *   | symtab       - 0xa064bf0
 137 *   | sym:symtab   - 0xa041690
 138 *   | sym:overname - "__SWIG_0"
 139 *  
 140 *         +++ namespace ----------------------------------------
 141 *         | sym:name     - "InnerNamespace"
 142 *         | symtab       - 0xa064cc0
 143 *         | sym:symtab   - 0xa064bf0
 144 *         | sym:overname - "__SWIG_0"
 145 *  
 146 *               +++ class ----------------------------------------
 147 *               | sym:name     - "Class"
 148 *               | symtab       - 0xa064d80
 149 *               | sym:symtab   - 0xa064cc0
 150 *               | sym:overname - "__SWIG_0"
 151 *               | 
 152 *               +++ class ----------------------------------------
 153 *               | sym:name     - "Struct"
 154 *               | symtab       - 0xa064f00
 155 *               | sym:symtab   - 0xa064cc0
 156 *               | sym:overname - "__SWIG_0"
 157 *  
 158 *                     +++ cdecl ----------------------------------------
 159 *                     | sym:name     - "Var"
 160 *                     | sym:symtab   - 0xa064f00
 161 *                     | sym:overname - "__SWIG_0"
 162 *                     | 
 163 *  
 164 *
 165 * Each class and namespace has its own scope and thus a new symbol table (sym)
 166 * is created. The sym attribute is only set for the first entry in the symbol
 167 * table. The sym:symtab entry points to the symbol table in which the symbol
 168 * exists, so for example, Struct is in the scope OuterNamespace::InnerNamespace
 169 * so sym:symtab points to this symbol table (0xa064cc0).
 170 *
 171 * ----------------------------------------------------------------------------- */
 172
 173static Hash *current = 0;	/* The current symbol table hash */
 174static Hash *ccurrent = 0;	/* The current c symbol table hash */
 175static Hash *current_symtab = 0;	/* Current symbol table node */
 176static Hash *symtabs = 0;	/* Hash of all symbol tables by fully-qualified name */
 177static Hash *global_scope = 0;	/* Global scope */
 178
 179/* common attribute keys, to avoid calling find_key all the times */
 180
 181
 182/* -----------------------------------------------------------------------------
 183 * Swig_symbol_print_tables()
 184 *
 185 * Debug display of symbol tables
 186 * ----------------------------------------------------------------------------- */
 187
 188void Swig_symbol_print_tables(Symtab *symtab) {
 189  if (!symtab)
 190    symtab = current_symtab;
 191
 192  Printf(stdout, "SYMBOL TABLES start  =======================================\n");
 193  Swig_print_tree(symtab);
 194  Printf(stdout, "SYMBOL TABLES finish =======================================\n");
 195}
 196
 197/* -----------------------------------------------------------------------------
 198 * Swig_symbol_print_tables_summary()
 199 *
 200 * Debug summary display of all symbol tables by fully-qualified name 
 201 * ----------------------------------------------------------------------------- */
 202
 203void Swig_symbol_print_tables_summary(void) {
 204  Printf(stdout, "SYMBOL TABLES SUMMARY start  =======================================\n");
 205  Swig_print_node(symtabs);
 206  Printf(stdout, "SYMBOL TABLES SUMMARY finish =======================================\n");
 207}
 208
 209/* -----------------------------------------------------------------------------
 210 * symbol_print_symbols()
 211 * ----------------------------------------------------------------------------- */
 212
 213static void symbol_print_symbols(const char *symboltabletype) {
 214  Node *table = symtabs;
 215  Iterator ki = First(table);
 216  while (ki.key) {
 217    String *k = ki.key;
 218    Printf(stdout, "===================================================\n");
 219    Printf(stdout, "%s -\n", k);
 220    {
 221      Symtab *symtab = Getattr(Getattr(table, k), symboltabletype);
 222      Iterator it = First(symtab);
 223      while (it.key) {
 224	String *symname = it.key;
 225	Printf(stdout, "  %s\n", symname);
 226	/*
 227	Printf(stdout, "  %s - %p (%s)\n", symname, it.item, Getattr(it.item, "name"));
 228	*/
 229	it = Next(it);
 230      }
 231    }
 232    ki = Next(ki);
 233  }
 234}
 235
 236/* -----------------------------------------------------------------------------
 237 * Swig_symbol_print_symbols()
 238 *
 239 * Debug display of all the target language symbols
 240 * ----------------------------------------------------------------------------- */
 241
 242void Swig_symbol_print_symbols(void) {
 243  Printf(stdout, "SYMBOLS start  =======================================\n");
 244  symbol_print_symbols("symtab");
 245  Printf(stdout, "SYMBOLS finish =======================================\n");
 246}
 247
 248/* -----------------------------------------------------------------------------
 249 * Swig_symbol_print_csymbols()
 250 *
 251 * Debug display of all the C symbols
 252 * ----------------------------------------------------------------------------- */
 253
 254void Swig_symbol_print_csymbols(void) {
 255  Printf(stdout, "CSYMBOLS start  =======================================\n");
 256  symbol_print_symbols("csymtab");
 257  Printf(stdout, "CSYMBOLS finish =======================================\n");
 258}
 259
 260/* -----------------------------------------------------------------------------
 261 * Swig_symbol_init()
 262 *
 263 * Create a new symbol table object
 264 * ----------------------------------------------------------------------------- */
 265
 266void Swig_symbol_init(void) {
 267
 268  current = NewHash();
 269  current_symtab = NewHash();
 270  ccurrent = NewHash();
 271  set_nodeType(current_symtab, "symboltable");
 272  Setattr(current_symtab, "symtab", current);
 273  Delete(current);
 274  Setattr(current_symtab, "csymtab", ccurrent);
 275  Delete(ccurrent);
 276
 277  /* Set the global scope */
 278  symtabs = NewHash();
 279  Setattr(symtabs, "", current_symtab);
 280  Delete(current_symtab);
 281  global_scope = current_symtab;
 282}
 283
 284/* -----------------------------------------------------------------------------
 285 * Swig_symbol_setscopename()
 286 *
 287 * Set the C scopename of the current symbol table.
 288 * ----------------------------------------------------------------------------- */
 289
 290void Swig_symbol_setscopename(const_String_or_char_ptr name) {
 291  String *qname;
 292  /* assert(!Getattr(current_symtab,"name")); */
 293  Setattr(current_symtab, "name", name);
 294
 295  /* Set nested scope in parent */
 296
 297  qname = Swig_symbol_qualifiedscopename(current_symtab);
 298
 299  /* Save a reference to this scope */
 300  Setattr(symtabs, qname, current_symtab);
 301  Delete(qname);
 302}
 303
 304/* -----------------------------------------------------------------------------
 305 * Swig_symbol_getscopename()
 306 *
 307 * Get the C scopename of the current symbol table
 308 * ----------------------------------------------------------------------------- */
 309
 310String *Swig_symbol_getscopename(void) {
 311  return Getattr(current_symtab, "name");
 312}
 313
 314/* -----------------------------------------------------------------------------
 315 * Swig_symbol_getscope()
 316 *
 317 * Given a fully qualified C scopename, this function returns a symbol table
 318 * ----------------------------------------------------------------------------- */
 319
 320Symtab *Swig_symbol_getscope(const_String_or_char_ptr name) {
 321  if (!symtabs)
 322    return 0;
 323  if (Equal("::", (const_String_or_char_ptr ) name))
 324    name = "";
 325  return Getattr(symtabs, name);
 326}
 327
 328/* ----------------------------------------------------------------------------- 
 329 * Swig_symbol_qualifiedscopename()
 330 *
 331 * Get the fully qualified C scopename of a symbol table.  Note, this only pertains
 332 * to the C/C++ scope name.  It is not affected by renaming.
 333 * ----------------------------------------------------------------------------- */
 334
 335String *Swig_symbol_qualifiedscopename(Symtab *symtab) {
 336  String *result = 0;
 337  Hash *parent;
 338  String *name;
 339  if (!symtab)
 340    symtab = current_symtab;
 341  parent = Getattr(symtab, "parentNode");
 342  if (parent) {
 343    result = Swig_symbol_qualifiedscopename(parent);
 344  }
 345  name = Getattr(symtab, "name");
 346  if (name) {
 347    if (!result) {
 348      result = NewStringEmpty();
 349    }
 350    if (Len(result)) {
 351      Printv(result, "::", name, NIL);
 352    } else {
 353      Append(result, name);
 354    }
 355  }
 356  return result;
 357}
 358
 359/* ----------------------------------------------------------------------------- 
 360 * Swig_symbol_qualified_language_scopename()
 361 *
 362 * Get the fully qualified C scopename of a symbol table but using a language
 363 * specific separator for the scopenames. Basically the same as
 364 * Swig_symbol_qualifiedscopename() but using the different separator.
 365 * ----------------------------------------------------------------------------- */
 366
 367String *Swig_symbol_qualified_language_scopename(Symtab *n) {
 368  /* TODO: fix for %rename to work */
 369  String *result = Swig_symbol_qualifiedscopename(n);
 370  Replaceall(result, "::", NSPACE_SEPARATOR);
 371  return result;
 372}
 373
 374/* -----------------------------------------------------------------------------
 375 * Swig_symbol_newscope()
 376 *
 377 * Create a new scope.  Returns the newly created scope.
 378 * ----------------------------------------------------------------------------- */
 379
 380Symtab *Swig_symbol_newscope(void) {
 381  Hash *n;
 382  Hash *hsyms, *h;
 383
 384  hsyms = NewHash();
 385  h = NewHash();
 386
 387  set_nodeType(h, "symboltable");
 388  Setattr(h, "symtab", hsyms);
 389  Delete(hsyms);
 390  set_parentNode(h, current_symtab);
 391
 392  n = lastChild(current_symtab);
 393  if (!n) {
 394    set_firstChild(current_symtab, h);
 395  } else {
 396    set_nextSibling(n, h);
 397    Delete(h);
 398  }
 399  set_lastChild(current_symtab, h);
 400  current = hsyms;
 401  ccurrent = NewHash();
 402  Setattr(h, "csymtab", ccurrent);
 403  Delete(ccurrent);
 404  current_symtab = h;
 405  return h;
 406}
 407
 408/* -----------------------------------------------------------------------------
 409 * Swig_symbol_setscope()
 410 *
 411 * Set the current scope.  Returns the previous current scope.
 412 * ----------------------------------------------------------------------------- */
 413
 414Symtab *Swig_symbol_setscope(Symtab *sym) {
 415  Symtab *ret = current_symtab;
 416  current_symtab = sym;
 417  current = Getattr(sym, "symtab");
 418  assert(current);
 419  ccurrent = Getattr(sym, "csymtab");
 420  assert(ccurrent);
 421  return ret;
 422}
 423
 424/* -----------------------------------------------------------------------------
 425 * Swig_symbol_popscope()
 426 *
 427 * Pop out of the current scope.  Returns the popped scope and sets the
 428 * scope to the parent scope.
 429 * ----------------------------------------------------------------------------- */
 430
 431Symtab *Swig_symbol_popscope(void) {
 432  Hash *h = current_symtab;
 433  current_symtab = Getattr(current_symtab, "parentNode");
 434  assert(current_symtab);
 435  current = Getattr(current_symtab, "symtab");
 436  assert(current);
 437  ccurrent = Getattr(current_symtab, "csymtab");
 438  assert(ccurrent);
 439  return h;
 440}
 441
 442/* -----------------------------------------------------------------------------
 443 * Swig_symbol_global_scope()
 444 *
 445 * Return the symbol table for the global scope.
 446 * ----------------------------------------------------------------------------- */
 447
 448Symtab *Swig_symbol_global_scope(void) {
 449  return global_scope;
 450}
 451
 452/* -----------------------------------------------------------------------------
 453 * Swig_symbol_current()
 454 *
 455 * Return the current symbol table.
 456 * ----------------------------------------------------------------------------- */
 457
 458Symtab *Swig_symbol_current(void) {
 459  return current_symtab;
 460}
 461
 462/* -----------------------------------------------------------------------------
 463 * Swig_symbol_alias()
 464 *
 465 * Makes an alias for a symbol in the global symbol table.
 466 * ----------------------------------------------------------------------------- */
 467
 468void Swig_symbol_alias(const_String_or_char_ptr aliasname, Symtab *s) {
 469  String *qname = Swig_symbol_qualifiedscopename(current_symtab);
 470  if (qname) {
 471    Printf(qname, "::%s", aliasname);
 472  } else {
 473    qname = NewString(aliasname);
 474  }
 475  if (!Getattr(symtabs, qname)) {
 476    Setattr(symtabs, qname, s);
 477  }
 478  Delete(qname);
 479}
 480
 481/* -----------------------------------------------------------------------------
 482 * Swig_symbol_inherit()
 483 *
 484 * Inherit symbols from another scope.
 485 * ----------------------------------------------------------------------------- */
 486
 487void Swig_symbol_inherit(Symtab *s) {
 488  int i, ilen;
 489  List *inherit = Getattr(current_symtab, "inherit");
 490  if (!inherit) {
 491    inherit = NewList();
 492    Setattr(current_symtab, "inherit", inherit);
 493    Delete(inherit);
 494  }
 495
 496  if (s == current_symtab) {
 497    Swig_warning(WARN_PARSE_REC_INHERITANCE, Getfile(s), Getline(s), "Recursive scope inheritance of '%s'.\n", Getattr(s, "name"));
 498    return;
 499  }
 500  assert(s != current_symtab);
 501  ilen = Len(inherit);
 502  for (i = 0; i < ilen; i++) {
 503    Node *n = Getitem(inherit, i);
 504    if (n == s)
 505      return;			/* Already inherited */
 506  }
 507  Append(inherit, s);
 508}
 509
 510/* -----------------------------------------------------------------------------
 511 * Swig_symbol_cadd()
 512 *
 513 * Adds a node to the C symbol table only.
 514 * ----------------------------------------------------------------------------- */
 515
 516void Swig_symbol_cadd(const_String_or_char_ptr name, Node *n) {
 517  Node *append = 0;
 518
 519  Node *cn;
 520  /* There are a few options for weak symbols.  A "weak" symbol 
 521     is any symbol that can be replaced by another symbol in the C symbol
 522     table.  An example would be a forward class declaration.  A forward
 523     class sits in the symbol table until a real class declaration comes along.
 524
 525     Certain symbols are marked as "sym:typename".  These are important 
 526     symbols related to the C++ type-system and take precedence in the C
 527     symbol table.  An example might be code like this:
 528
 529     template<class T> T foo(T x);
 530     int foo(int);
 531
 532     In this case, the template is marked with "sym:typename" so that it
 533     stays in the C symbol table (so that it can be expanded using %template).
 534   */
 535
 536  if (!name)
 537    return;
 538  if (SwigType_istemplate(name)) {
 539    String *cname = NewString(name);
 540    String *dname = Swig_symbol_template_deftype(cname, 0);
 541    if (!Equal(dname, name)) {
 542      Swig_symbol_cadd(dname, n);
 543    }
 544    Delete(dname);
 545    Delete(cname);
 546  }
 547#ifdef SWIG_DEBUG
 548  Printf(stderr, "symbol_cadd %s %x\n", name, n);
 549#endif
 550  cn = Getattr(ccurrent, name);
 551
 552  if (cn && (Getattr(cn, "sym:typename"))) {
 553    /* The node in the C symbol table is a typename.  Do nothing */
 554    /* We might append the symbol at the end */
 555    append = n;
 556  } else if (cn && (Getattr(cn, "sym:weak"))) {
 557    /* The node in the symbol table is weak. Replace it */
 558    if (checkAttribute(cn, "nodeType", "template")
 559	&& checkAttribute(cn, "templatetype", "classforward")) {
 560      /* The node is a template classforward declaration, and the
 561         default template parameters here take precedence. */
 562      ParmList *pc = Getattr(cn, "templateparms");
 563      ParmList *pn = Getattr(n, "templateparms");
 564#ifdef SWIG_DEBUG
 565      Printf(stderr, "found template classforward %s\n", Getattr(cn, "name"));
 566#endif
 567      while (pc && pn) {
 568	String *value = Getattr(pc, "value");
 569	if (value) {
 570#ifdef SWIG_DEBUG
 571	  Printf(stderr, "add default template value %s %s\n", Getattr(pc, "name"), value);
 572#endif
 573	  Setattr(pn, "value", value);
 574	}
 575	pc = nextSibling(pc);
 576	pn = nextSibling(pn);
 577      }
 578      Setattr(n, "templateparms", Getattr(cn, "templateparms"));
 579    }
 580    Setattr(ccurrent, name, n);
 581
 582  } else if (cn && (Getattr(n, "sym:weak"))) {
 583    /* The node being added is weak.  Don't worry about it */
 584  } else if (cn && (Getattr(n, "sym:typename"))) {
 585    /* The node being added is a typename.  We definitely add it */
 586    Setattr(ccurrent, name, n);
 587    append = cn;
 588  } else if (cn && (Checkattr(cn, "nodeType", "templateparm"))) {
 589    Swig_error(Getfile(n), Getline(n), "Declaration of '%s' shadows template parameter,\n", name);
 590    Swig_error(Getfile(cn), Getline(cn), "previous template parameter declaration '%s'.\n", name);
 591    return;
 592  } else if (cn) {
 593    append = n;
 594  } else if (!cn) {
 595    /* No conflict. Add the symbol */
 596    Setattr(ccurrent, name, n);
 597  }
 598
 599  /* Multiple entries in the C symbol table.   We append to to the symbol table */
 600  if (append) {
 601    Node *fn, *pn = 0;
 602    cn = Getattr(ccurrent, name);
 603    fn = cn;
 604    while (fn) {
 605      pn = fn;
 606      if (fn == append) {
 607	/* already added. Bail */
 608	return;
 609      }
 610      fn = Getattr(fn, "csym:nextSibling");
 611    }
 612    if (pn) {
 613      Setattr(pn, "csym:nextSibling", append);
 614    }
 615  }
 616
 617  /* Special typedef handling.  When a typedef node is added to the symbol table, we
 618     might have to add a type alias.   This would occur if the typedef mapped to another
 619     scope in the system.  For example:
 620
 621     class Foo {
 622     };
 623
 624     typedef Foo OtherFoo;
 625
 626     In this case, OtherFoo becomes an alias for Foo. */
 627
 628  {
 629    Node *td = n;
 630    while (td && Checkattr(td, "nodeType", "cdecl") && Checkattr(td, "storage", "typedef")) {
 631      SwigType *type;
 632      Node *td1;
 633      type = Copy(Getattr(td, "type"));
 634      SwigType_push(type, Getattr(td, "decl"));
 635      td1 = Swig_symbol_clookup(type, 0);
 636
 637      /* Fix pathetic case #1214313:
 638
 639         class Foo
 640         {
 641         };
 642
 643         typedef Foo FooBar;
 644
 645         class CBaz
 646         {
 647         public:
 648         typedef FooBar Foo;
 649         };
 650
 651         ie, when Foo -> FooBar -> Foo, jump one scope up when possible.
 652
 653       */
 654      if (td1 && Checkattr(td1, "storage", "typedef")) {
 655	String *st = Getattr(td1, "type");
 656	String *sn = Getattr(td, "name");
 657	if (st && sn && Equal(st, sn)) {
 658	  Symtab *sc = Getattr(current_symtab, "parentNode");
 659	  if (sc)
 660	    td1 = Swig_symbol_clookup(type, sc);
 661	}
 662      }
 663
 664      Delete(type);
 665      if (td1 == td)
 666	break;
 667      td = td1;
 668      if (td) {
 669	Symtab *st = Getattr(td, "symtab");
 670	if (st) {
 671	  Swig_symbol_alias(Getattr(n, "name"), st);
 672	  break;
 673	}
 674      }
 675    }
 676  }
 677}
 678
 679/* ----------------------------------------------------------------------------- 
 680 * Swig_symbol_add()
 681 *
 682 * Adds a node to the symbol table.  Returns the node itself if successfully
 683 * added.  Otherwise, it returns the symbol table entry of the conflicting node.
 684 *
 685 * Also places the symbol in a behind-the-scenes C symbol table.  This is needed
 686 * for namespace support, type resolution, and other issues.
 687 * ----------------------------------------------------------------------------- */
 688
 689Node *Swig_symbol_add(const_String_or_char_ptr symname, Node *n) {
 690  Hash *c, *cn, *cl = 0;
 691  SwigType *decl, *ndecl;
 692  String *cstorage, *nstorage;
 693  int nt = 0, ct = 0;
 694  int pn = 0;
 695  int u1 = 0, u2 = 0;
 696  String *name, *overname;
 697
 698  /* See if the node has a name.  If so, we place in the C symbol table for this
 699     scope. We don't worry about overloading here---the primary purpose of this
 700     is to record information for type/name resolution for later. Conflicts
 701     in C namespaces are errors, but these will be caught by the C++ compiler
 702     when compiling the wrapper code */
 703
 704
 705  /* There are a few options for weak symbols.  A "weak" symbol 
 706     is any symbol that can be replaced by another symbol in the C symbol
 707     table.  An example would be a forward class declaration.  A forward
 708     class sits in the symbol table until a real class declaration comes along.
 709
 710     Certain symbols are marked as "sym:typename".  These are important 
 711     symbols related to the C++ type-system and take precedence in the C
 712     symbol table.  An example might be code like this:
 713
 714     template<class T> T foo(T x);
 715     int foo(int);
 716
 717     In this case, the template is marked with "sym:typename" so that it
 718     stays in the C symbol table (so that it can be expanded using %template).
 719   */
 720
 721  name = Getattr(n, "name");
 722  if (name && Len(name)) {
 723    Swig_symbol_cadd(name, n);
 724  }
 725
 726  /* No symbol name defined.  We return. */
 727  if (!symname) {
 728    Setattr(n, "sym:symtab", current_symtab);
 729    return n;
 730  }
 731
 732  /* If node is ignored. We don't proceed any further */
 733  if (GetFlag(n, "feature:ignore"))
 734    return n;
 735
 736  /* See if the symbol already exists in the table */
 737  c = Getattr(current, symname);
 738
 739  /* Check for a weak symbol.  A weak symbol is allowed to be in the
 740     symbol table, but is silently overwritten by other symbols.  An example
 741     would be a forward class declaration.  For instance:
 742
 743     class Foo;
 744
 745     In this case, "Foo" sits in the symbol table.  However, the
 746     definition of Foo would replace the entry if it appeared later. */
 747
 748  if (c && Getattr(c, "sym:weak")) {
 749    c = 0;
 750  }
 751  if (c) {
 752    /* There is a symbol table conflict.  There are a few cases to consider here:
 753       (1) A conflict between a class/enum and a typedef declaration is okay.
 754       In this case, the symbol table entry is set to the class/enum declaration
 755       itself, not the typedef.   
 756
 757       (2) A conflict between namespaces is okay--namespaces are open
 758
 759       (3) Otherwise, overloading is only allowed for functions
 760     */
 761
 762    /* Check for namespaces */
 763    String *ntype = Getattr(n, "nodeType");
 764    if ((Equal(ntype, Getattr(c, "nodeType"))) && ((Equal(ntype, "namespace")))) {
 765      Node *cl, *pcl = 0;
 766      cl = c;
 767      while (cl) {
 768	pcl = cl;
 769	cl = Getattr(cl, "sym:nextSibling");
 770      }
 771      Setattr(pcl, "sym:nextSibling", n);
 772      Setattr(n, "sym:symtab", current_symtab);
 773      Setattr(n, "sym:name", symname);
 774      Setattr(n, "sym:previousSibling", pcl);
 775      return n;
 776    }
 777    if (Getattr(n, "allows_typedef"))
 778      nt = 1;
 779    if (Getattr(c, "allows_typedef"))
 780      ct = 1;
 781    if (nt || ct) {
 782      Node *td, *other;
 783      String *s;
 784      /* At least one of the nodes allows typedef overloading.  Make sure that
 785         both don't--this would be a conflict */
 786
 787      if (nt && ct)
 788	return c;
 789
 790      /* Figure out which node allows the typedef */
 791      if (nt) {
 792	td = n;
 793	other = c;
 794      } else {
 795	td = c;
 796	other = n;
 797      }
 798      /* Make sure the other node is a typedef */
 799      s = Getattr(other, "storage");
 800      if (!s || (!Equal(s, "typedef")))
 801	return c;		/* No.  This is a conflict */
 802
 803      /* Hmmm.  This appears to be okay.  Make sure the symbol table refers to the allow_type node */
 804
 805      if (td != c) {
 806	Setattr(current, symname, td);
 807	Setattr(td, "sym:symtab", current_symtab);
 808	Setattr(td, "sym:name", symname);
 809      }
 810      return n;
 811    }
 812
 813    decl = Getattr(c, "decl");
 814    ndecl = Getattr(n, "decl");
 815
 816    {
 817      String *nt1, *nt2;
 818      nt1 = Getattr(n, "nodeType");
 819      if (Equal(nt1, "template"))
 820	nt1 = Getattr(n, "templatetype");
 821      nt2 = Getattr(c, "nodeType");
 822      if (Equal(nt2, "template"))
 823	nt2 = Getattr(c, "templatetype");
 824      if (Equal(nt1, "using"))
 825	u1 = 1;
 826      if (Equal(nt2, "using"))
 827	u2 = 1;
 828
 829      if ((!Equal(nt1, nt2)) && !(u1 || u2))
 830	return c;
 831    }
 832    if (!(u1 || u2)) {
 833      if ((!SwigType_isfunction(decl)) || (!SwigType_isfunction(ndecl))) {
 834	/* Symbol table conflict */
 835	return c;
 836      }
 837    }
 838
 839    /* Hmmm. Declarator seems to indicate that this is a function */
 840    /* Look at storage class to see if compatible */
 841    cstorage = Getattr(c, "storage");
 842    nstorage = Getattr(n, "storage");
 843
 844    /* If either one is declared as typedef, forget it. We're hosed */
 845    if (Cmp(cstorage, "typedef") == 0) {
 846      return c;
 847    }
 848    if (Cmp(nstorage, "typedef") == 0) {
 849      return c;
 850    }
 851
 852    /* Okay. Walk down the list of symbols and see if we get a declarator match */
 853    {
 854      String *nt = Getattr(n, "nodeType");
 855      int n_template = Equal(nt, "template") && Checkattr(n, "templatetype", "cdecl");
 856      int n_plain_cdecl = Equal(nt, "cdecl");
 857      cn = c;
 858      pn = 0;
 859      while (cn) {
 860	decl = Getattr(cn, "decl");
 861	if (!(u1 || u2)) {
 862	  if (Cmp(ndecl, decl) == 0) {
 863	    /* Declarator conflict */
 864	    /* Now check we don't have a non-templated function overloaded by a templated function with same params,
 865	     * eg void foo(); template<typename> void foo(); */
 866	    String *cnt = Getattr(cn, "nodeType");
 867	    int cn_template = Equal(cnt, "template") && Checkattr(cn, "templatetype", "cdecl");
 868	    int cn_plain_cdecl = Equal(cnt, "cdecl");
 869	    if (!((n_template && cn_plain_cdecl) || (cn_template && n_plain_cdecl))) {
 870	      /* found a conflict */
 871	      return cn;
 872	    }
 873	  }
 874	}
 875	cl = cn;
 876	cn = Getattr(cn, "sym:nextSibling");
 877	pn++;
 878      }
 879    }
 880    /* Well, we made it this far.  Guess we can drop the symbol in place */
 881    Setattr(n, "sym:symtab", current_symtab);
 882    Setattr(n, "sym:name", symname);
 883    /* Printf(stdout,"%s %x\n", Getattr(n,"sym:overname"), current_symtab); */
 884    assert(!Getattr(n, "sym:overname"));
 885    overname = NewStringf("__SWIG_%d", pn);
 886    Setattr(n, "sym:overname", overname);
 887    /*Printf(stdout,"%s %s %s\n", symname, Getattr(n,"decl"), Getattr(n,"sym:overname")); */
 888    Setattr(cl, "sym:nextSibling", n);
 889    Setattr(n, "sym:previousSibling", cl);
 890    Setattr(cl, "sym:overloaded", c);
 891    Setattr(n, "sym:overloaded", c);
 892    Delete(overname);
 893    return n;
 894  }
 895
 896  /* No conflict.  Just add it */
 897  Setattr(n, "sym:symtab", current_symtab);
 898  Setattr(n, "sym:name", symname);
 899  /* Printf(stdout,"%s\n", Getattr(n,"sym:overname")); */
 900  overname = NewStringf("__SWIG_%d", pn);
 901  Setattr(n, "sym:overname", overname);
 902  Delete(overname);
 903  /* Printf(stdout,"%s %s %s\n", symname, Getattr(n,"decl"), Getattr(n,"sym:overname")); */
 904  Setattr(current, symname, n);
 905  return n;
 906}
 907
 908/* -----------------------------------------------------------------------------
 909 * symbol_lookup()
 910 *
 911 * Internal function to handle fully qualified symbol table lookups.  This
 912 * works from the symbol table supplied in symtab and unwinds its way out
 913 * towards the global scope. 
 914 *
 915 * This function operates in the C namespace, not the target namespace.
 916 *
 917 * The check function is an optional callback that can be used to verify a particular
 918 * symbol match.   This is only used in some of the more exotic parts of SWIG. For instance,
 919 * verifying that a class hierarchy implements all pure virtual methods.
 920 * ----------------------------------------------------------------------------- */
 921
 922static Node *_symbol_lookup(const String *name, Symtab *symtab, int (*check) (Node *n)) {
 923  Node *n;
 924  List *inherit;
 925  Hash *sym = Getattr(symtab, "csymtab");
 926  if (Getmark(symtab))
 927    return 0;
 928  Setmark(symtab, 1);
 929
 930
 931  n = Getattr(sym, name);
 932
 933#ifdef SWIG_DEBUG
 934  Printf(stderr, "symbol_look %s %x %x %s\n", name, n, symtab, Getattr(symtab, "name"));
 935#endif
 936
 937  if (n) {
 938    /* if a check-function is defined.  Call it to determine a match */
 939    if (check) {
 940      int c = check(n);
 941      if (c == 1) {
 942	Setmark(symtab, 0);
 943	return n;
 944      }
 945      if (c < 0) {
 946	/* Terminate the search right away */
 947	Setmark(symtab, 0);
 948	return 0;
 949      }
 950    } else {
 951      Setmark(symtab, 0);
 952      return n;
 953    }
 954  }
 955
 956  if (!n && SwigType_istemplate(name)) {
 957    String *dname = 0;
 958    Setmark(symtab, 0);
 959    dname = Swig_symbol_template_deftype(name, symtab);
 960    if (!Equal(dname, name)) {
 961      n = _symbol_lookup(dname, symtab, check);
 962    }
 963    Delete(dname);
 964    if (n)
 965      return n;
 966    Setmark(symtab, 1);
 967  }
 968
 969  inherit = Getattr(symtab, "inherit");
 970  if (inherit) {
 971    int i, len;
 972    len = Len(inherit);
 973    for (i = 0; i < len; i++) {
 974      n = _symbol_lookup(name, Getitem(inherit, i), check);
 975      if (n) {
 976	Setmark(symtab, 0);
 977	return n;
 978      }
 979    }
 980  }
 981
 982  Setmark(symtab, 0);
 983  return 0;
 984}
 985
 986static Node *symbol_lookup(const_String_or_char_ptr name, Symtab *symtab, int (*check) (Node *n)) {
 987  Node *n = 0;
 988  if (DohCheck(name)) {
 989    n = _symbol_lookup(name, symtab, check);
 990  } else {
 991    String *sname = NewString(name);
 992    n = _symbol_lookup(sname, symtab, check);
 993    Delete(sname);
 994  }
 995  return n;
 996}
 997
 998
 999
1000/* -----------------------------------------------------------------------------
1001 * symbol_lookup_qualified()
1002 * ----------------------------------------------------------------------------- */
1003
1004static Node *symbol_lookup_qualified(const_String_or_char_ptr name, Symtab *symtab, const String *prefix, int local, int (*checkfunc) (Node *n)) {
1005  /* This is a little funky, we search by fully qualified names */
1006
1007  if (!symtab)
1008    return 0;
1009  if (!prefix) {
1010    Node *n;
1011    String *bname;
1012    String *prefix;
1013    Swig_scopename_split(name, &prefix, &bname);
1014    n = symbol_lookup_qualified(bname, symtab, prefix, local, checkfunc);
1015    Delete(bname);
1016    Delete(prefix);
1017    return n;
1018  } else {
1019    Symtab *st;
1020    Node *n = 0;
1021    /* Make qualified name of current scope */
1022    String *qalloc = 0;
1023    String *qname = Swig_symbol_qualifiedscopename(symtab);
1024    const String *cqname;
1025    if (qname) {
1026      if (Len(qname)) {
1027	if (prefix && Len(prefix)) {
1028	  Printv(qname, "::", prefix, NIL);
1029	}
1030      } else {
1031	Append(qname, prefix);
1032      }
1033      qalloc = qname;
1034      cqname = qname;
1035    } else {
1036      cqname = prefix;
1037    }
1038    st = Getattr(symtabs, cqname);
1039    /* Found a scope match */
1040    if (st) {
1041      if (!name) {
1042	if (qalloc)
1043	  Delete(qalloc);
1044	return st;
1045      }
1046      n = symbol_lookup(name, st, checkfunc);
1047    }
1048    if (qalloc)
1049      Delete(qalloc);
1050
1051    if (!n) {
1052      if (!local) {
1053	Node *pn = Getattr(symtab, "parentNode");
1054	if (pn)
1055	  n = symbol_lookup_qualified(name, pn, prefix, local, checkfunc);
1056      } else {
1057	n = 0;
1058      }
1059    }
1060    return n;
1061  }
1062}
1063
1064/* -----------------------------------------------------------------------------
1065 * Swig_symbol_clookup()
1066 *
1067 * Look up a symbol in the symbol table.   This uses the C name, not scripting
1068 * names.   Note: If we come across a using a directive, we follow it to
1069 * to get the real node.
1070 * ----------------------------------------------------------------------------- */
1071
1072Node *Swig_symbol_clookup(const_String_or_char_ptr name, Symtab *n) {
1073  Hash *hsym = 0;
1074  Node *s = 0;
1075
1076  if (!n) {
1077    hsym = current_symtab;
1078  } else {
1079    if (!Checkattr(n, "nodeType", "symboltable")) {
1080      n = Getattr(n, "sym:symtab");
1081    }
1082    assert(n);
1083    if (n) {
1084      hsym = n;
1085    }
1086  }
1087
1088  if (Swig_scopename_check(name)) {
1089    char *cname = Char(name);
1090    if (strncmp(cname, "::", 2) == 0) {
1091      String *nname = NewString(cname + 2);
1092      if (Swig_scopename_check(nname)) {
1093	s = symbol_lookup_qualified(nname, global_scope, 0, 0, 0);
1094      } else {
1095	s = symbol_lookup(nname, global_scope, 0);
1096      }
1097      Delete(nname);
1098    } else {
1099      String *prefix = Swig_scopename_prefix(name);
1100      if (prefix) {
1101	s = symbol_lookup_qualified(name, hsym, 0, 0, 0);
1102	Delete(prefix);
1103	if (!s) {
1104	  return 0;
1105	}
1106      }
1107    }
1108  }
1109  if (!s) {
1110    while (hsym) {
1111      s = symbol_lookup(name, hsym, 0);
1112      if (s)
1113	break;
1114      hsym = Getattr(hsym, "parentNode");
1115      if (!hsym)
1116	break;
1117    }
1118  }
1119
1120  if (!s) {
1121    return 0;
1122  }
1123  /* Check if s is a 'using' node */
1124  while (s && Checkattr(s, "nodeType", "using")) {
1125    String *uname = Getattr(s, "uname");
1126    Symtab *un = Getattr(s, "sym:symtab");
1127    Node *ss = (!Equal(name, uname) || (un != n)) ? Swig_symbol_clookup(uname, un) : 0;	/* avoid infinity loop */
1128    if (!ss) {
1129      Swig_warning(WARN_PARSE_USING_UNDEF, Getfile(s), Getline(s), "Nothing known about '%s'.\n", Getattr(s, "uname"));
1130    }
1131    s = ss;
1132  }
1133  return s;
1134}
1135
1136/* -----------------------------------------------------------------------------
1137 * Swig_symbol_clookup_check()
1138 *
1139 * This function is identical to Swig_symbol_clookup() except that it
1140 * accepts a callback function that is invoked to determine a symbol match.
1141 * The purpose of this function is to support complicated algorithms that need
1142 * to examine multiple definitions of the same symbol that might appear in an
1143 * inheritance hierarchy. 
1144 * ----------------------------------------------------------------------------- */
1145
1146Node *Swig_symbol_clookup_check(const_String_or_char_ptr name, Symtab *n, int (*checkfunc) (Node *n)) {
1147  Hash *hsym = 0;
1148  Node *s = 0;
1149
1150  if (!n) {
1151    hsym = current_symtab;
1152  } else {
1153    if (!Checkattr(n, "nodeType", "symboltable")) {
1154      n = Getattr(n, "sym:symtab");
1155    }
1156    assert(n);
1157    if (n) {
1158      hsym = n;
1159    }
1160  }
1161
1162  if (Swig_scopename_check(name)) {
1163    char *cname = Char(name);
1164    if (strncmp(cname, "::", 2) == 0) {
1165      String *nname = NewString(cname + 2);
1166      if (Swig_scopename_check(nname)) {
1167	s = symbol_lookup_qualified(nname, global_scope, 0, 0, checkfunc);
1168      } else {
1169	s = symbol_lookup(nname, global_scope, checkfunc);
1170      }
1171      Delete(nname);
1172    } else {
1173      String *prefix = Swig_scopename_prefix(name);
1174      if (prefix) {
1175	s = symbol_lookup_qualified(name, hsym, 0, 0, checkfunc);
1176	Delete(prefix);
1177	if (!s) {
1178	  return 0;
1179	}
1180      }
1181    }
1182  }
1183  if (!s) {
1184    while (hsym) {
1185      s = symbol_lookup(name, hsym, checkfunc);
1186      if (s)
1187	break;
1188      hsym = Getattr(hsym, "parentNode");
1189      if (!hsym)
1190	break;
1191    }
1192  }
1193  if (!s) {
1194    return 0;
1195  }
1196  /* Check if s is a 'using' node */
1197  while (s && Checkattr(s, "nodeType", "using")) {
1198    Node *ss;
1199    ss = Swig_symbol_clookup(Getattr(s, "uname"), Getattr(s, "sym:symtab"));
1200    if (!ss && !checkfunc) {
1201      Swig_warning(WARN_PARSE_USING_UNDEF, Getfile(s), Getline(s), "Nothing known about '%s'.\n", Getattr(s, "uname"));
1202    }
1203    s = ss;
1204  }
1205  return s;
1206}
1207
1208/* -----------------------------------------------------------------------------
1209 * Swig_symbol_clookup_local()
1210 * ----------------------------------------------------------------------------- */
1211
1212Node *Swig_symbol_clookup_local(const_String_or_char_ptr name, Symtab *n) {
1213  Hash *hsym;
1214  Node *s = 0;
1215
1216  if (!n) {
1217    hsym = current_symtab;
1218  } else {
1219    if (!Checkattr(n, "nodeType", "symboltable")) {
1220      n = Getattr(n, "sym:symtab");
1221    }
1222    assert(n);
1223    hsym = n;
1224  }
1225
1226  if (Swig_scopename_check(name)) {
1227    char *cname = Char(name);
1228    if (strncmp(cname, "::", 2) == 0) {
1229      String *nname = NewString(cname + 2);
1230      if (Swig_scopename_check(nname)) {
1231	s = symbol_lookup_qualified(nname, global_scope, 0, 0, 0);
1232      } else {
1233	s = symbol_lookup(nname, global_scope, 0);
1234      }
1235      Delete(nname);
1236    } else {
1237      s = symbol_lookup_qualified(name, hsym, 0, 0, 0);
1238    }
1239  }
1240  if (!s) {
1241    s = symbol_lookup(name, hsym, 0);
1242  }
1243  if (!s)
1244    return 0;
1245  /* Check if s is a 'using' node */
1246  while (s && Checkattr(s, "nodeType", "using")) {
1247    Node *ss = Swig_symbol_clookup_local(Getattr(s, "uname"), Getattr(s, "sym:symtab"));
1248    if (!ss) {
1249      Swig_warning(WARN_PARSE_USING_UNDEF, Getfile(s), Getline(s), "Nothing known about '%s'.\n", Getattr(s, "uname"));
1250    }
1251    s = ss;
1252  }
1253  return s;
1254}
1255
1256/* -----------------------------------------------------------------------------
1257 * Swig_symbol_clookup_local_check()
1258 * ----------------------------------------------------------------------------- */
1259
1260Node *Swig_symbol_clookup_local_check(const_String_or_char_ptr name, Symtab *n, int (*checkfunc) (Node *)) {
1261  Hash *hsym;
1262  Node *s = 0;
1263
1264  if (!n) {
1265    hsym = current_symtab;
1266  } else {
1267    if (!Checkattr(n, "nodeType", "symboltable")) {
1268      n = Getattr(n, "sym:symtab");
1269    }
1270    assert(n);
1271    hsym = n;
1272  }
1273
1274  if (Swig_scopename_check(name)) {
1275    char *cname = Char(name);
1276    if (strncmp(cname, "::", 2) == 0) {
1277      String *nname = NewString(cname + 2);
1278      if (Swig_scopename_check(nname)) {
1279	s = symbol_lookup_qualified(nname, global_scope, 0, 0, checkfunc);
1280      } else {
1281	s = symbol_lookup(nname, global_scope, checkfunc);
1282      }
1283      Delete(nname);
1284    } else {
1285      s = symbol_lookup_qualified(name, hsym, 0, 0, checkfunc);
1286    }
1287  }
1288  if (!s) {
1289    s = symbol_lookup(name, hsym, checkfunc);
1290  }
1291  if (!s)
1292    return 0;
1293  /* Check if s is a 'using' node */
1294  while (s && Checkattr(s, "nodeType", "using")) {
1295    Node *ss = Swig_symbol_clookup_local_check(Getattr(s, "uname"), Getattr(s, "sym:symtab"), checkfunc);
1296    if (!ss && !checkfunc) {
1297      Swig_warning(WARN_PARSE_USING_UNDEF, Getfile(s), Getline(s), "Nothing known about '%s'.\n", Getattr(s, "uname"));
1298    }
1299    s = ss;
1300  }
1301  return s;
1302}
1303
1304
1305/* -----------------------------------------------------------------------------
1306 * Swig_symbol_cscope()
1307 *
1308 * Look up a scope name.
1309 * ----------------------------------------------------------------------------- */
1310
1311Symtab *Swig_symbol_cscope(const_String_or_char_ptr name, Symtab *symtab) {
1312  char *cname = Char(name);
1313  if (strncmp(cname, "::", 2) == 0)
1314    return symbol_lookup_qualified(0, global_scope, name, 0, 0);
1315  return symbol_lookup_qualified(0, symtab, name, 0, 0);
1316}
1317
1318/* -----------------------------------------------------------------------------
1319 * Swig_symbol_remove()
1320 *
1321 * Remove a symbol. If the symbol is an overloaded function and the symbol removed
1322 * is not the last in the list of overloaded functions, then the overloaded
1323 * names (sym:overname attribute) are changed to start from zero, eg __SWIG_0.
1324 * ----------------------------------------------------------------------------- */
1325
1326void Swig_symbol_remove(Node *n) {
1327  Symtab *symtab;
1328  String *symname;
1329  String *overname;
1330  Node *symprev;
1331  Node *symnext;
1332  Node *fixovername = 0;
1333  symtab = Getattr(n, "sym:symtab");	/* Get symbol table object */
1334  symtab = Getattr(symtab, "symtab");	/* Get actual hash table of symbols */
1335  symname = Getattr(n, "sym:name");
1336  symprev = Getattr(n, "sym:previousSibling");
1337  symnext = Getattr(n, "sym:nextSibling");
1338
1339  /* If previous symbol, just fix the links */
1340  if (symprev) {
1341    if (symnext) {
1342      Setattr(symprev, "sym:nextSibling", symnext);
1343      fixovername = symprev;	/* fix as symbol to remove is somewhere in the middle of the linked list */
1344    } else {
1345      Delattr(symprev, "sym:nextSibling");
1346    }
1347  } else {
1348    /* If no previous symbol, see if there is a next symbol */
1349    if (symnext) {
1350      Setattr(symtab, symname, symnext);
1351      fixovername = symnext;	/* fix as symbol to remove is at head of linked list */
1352    } else {
1353      if (symname)
1354	Delattr(symtab, symname);
1355    }
1356  }
1357  if (symnext) {
1358    if (symprev) {
1359      Setattr(symnext, "sym:previousSibling", symprev);
1360    } else {
1361      Delattr(symnext, "sym:previousSibling");
1362    }
1363  }
1364  Delattr(n, "sym:symtab");
1365  Delattr(n, "sym:previousSibling");
1366  Delattr(n, "sym:nextSibling");
1367  Delattr(n, "csym:nextSibling");
1368  Delattr(n, "sym:overname");
1369  Delattr(n, "csym:previousSibling");
1370  Delattr(n, "sym:overloaded");
1371  n = 0;
1372
1373  if (fixovername) {
1374    Node *nn = fixovername;
1375    Node *head = fixovername;
1376    int pn = 0;
1377
1378    /* find head of linked list */
1379    while (nn) {
1380      head = nn;
1381      nn = Getattr(nn, "sym:previousSibling");
1382    }
1383
1384    /* adjust all the sym:overname strings to start from 0 and increment by one */
1385    nn = head;
1386    while (nn) {
1387      assert(Getattr(nn, "sym:overname"));
1388      Delattr(nn, "sym:overname");
1389      overname = NewStringf("__SWIG_%d", pn);
1390      Setattr(nn, "sym:overname", overname);
1391      Delete(overname);
1392      pn++;
1393      nn = Getattr(nn, "sym:nextSibling");
1394    }
1395  }
1396}
1397
1398/* -----------------------------------------------------------------------------
1399 * Swig_symbol_qualified()
1400 *
1401 * Return the qualified name of a symbol
1402 * ----------------------------------------------------------------------------- */
1403
1404String *Swig_symbol_qualified(Node *n) {
1405  Hash *symtab;
1406  if (Checkattr(n, "nodeType", "symboltable")) {
1407    symtab = n;
1408  } else {
1409    symtab = Getattr(n, "sym:symtab");
1410  }
1411  if (!symtab)
1412    return NewStringEmpty();
1413#ifdef SWIG_DEBUG
1414  Printf(stderr, "symbol_qscope %s %x %s\n", Getattr(n, "name"), symtab, Getattr(symtab, "name"));
1415#endif
1416  return Swig_symbol_qualifiedscopename(symtab);
1417}
1418
1419/* -----------------------------------------------------------------------------
1420 * Swig_symbol_isoverloaded()
1421 * 
1422 * Check if a symbol is overloaded.  Returns the first symbol if so.
1423 * ----------------------------------------------------------------------------- */
1424
1425Node *Swig_symbol_isoverloaded(Node *n) {
1426  return Getattr(n, "sym:overloaded");
1427}
1428
1429/* -----------------------------------------------------------------------------
1430 * Swig_symbol_type_qualify()
1431 *
1432 * Create a fully qualified type name
1433 * ----------------------------------------------------------------------------- */
1434
1435/* This cache produces problems with OSS, don't active it */
1436/* #define SWIG_TEMPLATE_QUALIFY_CACHE */
1437static SwigType *Swig_symbol_template_qualify(const SwigType *e, Symtab *st) {
1438  String *tprefix, *tsuffix;
1439  SwigType *qprefix;
1440  List *targs;
1441  Node *tempn;
1442  Symtab *tscope;
1443  Iterator ti;
1444#ifdef SWIG_TEMPLATE_QUALIFY_CACHE
1445  static Hash *qualify_cache = 0;
1446  String *scopetype = st ? NewStringf("%s::%s", Getattr(st, "name"), e)
1447      : NewStringf("%s::%s", Swig_symbol_getscopename(), e);
1448  if (!qualify_cache) {
1449    qualify_cache = NewHash();
1450  }
1451  if (scopetype) {
1452    String *cres = Getattr(qualify_cache, scopetype);
1453    if (cres) {
1454      Delete(scopetype);
1455      return Copy(cres);
1456    }
1457  }
1458#endif
1459
1460  tprefix = SwigType_templateprefix(e);
1461  tsuffix = SwigType_templatesuffix(e);
1462  qprefix = Swig_symbol_type_qualify(tprefix, st);
1463  targs = SwigType_parmlist(e);
1464  tempn = Swig_symbol_clookup_local(tprefix, st);
1465  tscope = tempn ? Getattr(tempn, "sym:symtab") : 0;
1466  Append(qprefix, "<(");
1467  for (ti = First(targs); ti.item;) {
1468    String *vparm;
1469    String *qparm = Swig_symbol_type_qualify(ti.item, st);
1470    if (tscope && (tscope != st)) {
1471      String *ty = Swig_symbol_type_qualify(qparm, tscope);
1472      Delete(qparm);
1473      qparm = ty;
1474    }
1475
1476    vparm = Swig_symbol_template_param_eval(qparm, st);
1477    Append(qprefix, vparm);
1478    ti = Next(ti);
1479    if (ti.item) {
1480      Putc(',', qprefix);
1481    }
1482    Delete(qparm);
1483    Delete(vparm);
1484  }
1485  Append(qprefix, ")>");
1486  Append(qprefix, tsuffix);
1487  Delete(tprefix);
1488  Delete(tsuffix);
1489  Delete(targs);
1490#ifdef SWIG_DEBUG
1491  Printf(stderr, "symbol_temp_qual %s %s\n", e, qprefix);
1492#endif
1493#ifdef SWIG_TEMPLATE_QUALIFY_CACHE
1494  Setattr(qualify_cache, scopetype, qprefix);
1495  Delete(scopetype);
1496#endif
1497
1498  return qprefix;
1499}
1500
1501
1502static int no_constructor(Node *n) {
1503  return !Checkattr(n, "nodeType", "constructor");
1504}
1505
1506SwigType *Swig_symbol_type_qualify(const SwigType *t, Symtab *st) {
1507  List *elements;
1508  String *result = NewStringEmpty();
1509  int i, len;
1510  char *c = Char(t);
1511  if (strncmp(c, "::", 2) == 0) {
1512    Append(result, t);
1513    return result;
1514  }
1515
1516  elements = SwigType_split(t);
1517
1518  len = Len(elements);
1519  for (i = 0; i < len; i++) {
1520    String *e = Getitem(elements, i);
1521    if (SwigType_issimple(e)) {
1522      Node *n = Swig_symbol_clookup_check(e, st, no_constructor);
1523      if (n) {
1524	String *name = Getattr(n, "name");
1525	Clear(e);
1526	Append(e, name);
1527#ifdef SWIG_DEBUG
1528	Printf(stderr, "symbol_qual_ei %d %s %s %x\n", i, name, e, st);
1529#endif
1530	if (!Swig_scopename_check(name)) {
1531	  String *qname = Swig_symbol_qualified(n);
1532	  if (qname && Len(qname)) {
1533	    Insert(e, 0, "::");
1534	    Insert(e, 0, qname);
1535	  }
1536#ifdef SWIG_DEBUG
1537	  Printf(stderr, "symbol_qual_sc %d %s %s %x\n", i, qname, e, st);
1538#endif
1539	  Delete(qname);
1540	}
1541      } else if (SwigType_istemplate(e)) {
1542	SwigType *ty = Swig_symbol_template_qualify(e, st);
1543	Clear(e);
1544	Append(e, ty);
1545	Delete(ty);
1546      }
1547      if (strncmp(Char(e), "::", 2) == 0) {
1548	Delitem(e, 0);
1549	Delitem(e, 0);
1550      }
1551      Append(result, e);
1552    } else if (SwigType_isfunction(e)) {
1553      List *parms = SwigType_parmlist(e);
1554      String *s = NewString("f(");
1555      Iterator pi = First(parms);
1556      while (pi.item) {
1557	String *pf = Swig_symbol_type_qualify(pi.item, st);
1558	Append(s, pf);
1559	pi = Next(pi);
1560	if (pi.item) {
1561	  Append(s, ",");
1562	}
1563	Delete(pf);
1564      }
1565      Append(s, ").");
1566      Append(result, s);
1567      Delete(parms);
1568      Delete(s);
1569    } else {
1570      Append(result, e);
1571    }
1572  }
1573  Delete(elements);
1574#ifdef SWIG_DEBUG
1575  Printf(stderr, "symbol_qualify %s %s %x %s\n", t, result, st, st ? Getattr(st, "name") : 0);
1576#endif
1577
1578  return result;
1579}
1580
1581/* -----------------------------------------------------------------------------
1582 * Swig_symbol_template_reduce()
1583 * Resolves template parameter types
1584 * For example:
1585 *   typedef int Int;
1586 *   typedef Int Integer;
1587 * with input:
1588 *   Foo<(Int,Integer)> 
1589 * returns:
1590 *   Foo<(int,int)>
1591 * ----------------------------------------------------------------------------- */
1592
1593static
1594SwigType *Swig_symbol_template_reduce(SwigType *qt, Symtab *ntab) {
1595  Parm *p;
1596  String *templateargs = SwigType_templateargs(qt);
1597  List *parms = SwigType_parmlist(templateargs);
1598  Iterator pi = First(parms);
1599  String *tprefix = SwigType_templateprefix(qt);
1600  String *tsuffix = SwigType_templatesuffix(qt);
1601  String *qprefix = SwigType_typedef_qualified(tprefix);
1602  Append(qprefix, "<(");
1603  while ((p = pi.item)) {
1604    String *np;
1605    String *tp = Swig_symbol_typedef_reduce(p, ntab);
1606    String *qp = Swig_symbol_type_qualify(tp

Large files files are truncated, but you can click here to view the full file