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