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.
7 * This file implements the SWIG symbol table. See details below.
8 * ----------------------------------------------------------------------------- */
10 char cvsroot_symbol_c[] = "$Id: symbol.c 11097 2009-01-30 10:27:37Z bhy $";
16 /* #define SWIG_DEBUG*/
17 /* -----------------------------------------------------------------------------
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
24 * All symbols are associated with simple identifiers. For example, here are some
25 * declarations that generate symbol table entries:
28 * -------------- ------------
31 * typedef int *blah; blah
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:
36 * typedef int *blah; ----> "name" : 'blah'
39 * "storage" : 'typedef'
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:
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)."
49 * "sym:nextSibling" : --------> "sym:nextSibling": --------> ...
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).
57 * Structures and classes:
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:
69 * int Foo(); // Error. Name clash. Works in C though
71 * Due to the unified namespace for structures, special handling is performed for
74 * typedef struct Foo {
78 * In this case, the symbol table contains an entry for the structure itself. The
79 * typedef is left out of the symbol table.
81 * Target language vs C:
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.
91 * Symbol table structure:
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.
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.
103 * Reserved attributes on symbol objects:
105 * When a symbol is placed in the symbol table, the following attributes
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
114 * These names are modeled after XML namespaces. In particular, every attribute
115 * pertaining to symbol table management is prefaced by the "sym:" prefix.
117 * An example dump of the parse tree showing symbol table entries for the
118 * following code should clarify this:
120 * namespace OuterNamespace {
121 * namespace InnerNamespace {
130 * +++ namespace ----------------------------------------
131 * | sym:name - "OuterNamespace"
132 * | symtab - 0xa064bf0
133 * | sym:symtab - 0xa041690
134 * | sym:overname - "__SWIG_0"
136 * +++ namespace ----------------------------------------
137 * | sym:name - "InnerNamespace"
138 * | symtab - 0xa064cc0
139 * | sym:symtab - 0xa064bf0
140 * | sym:overname - "__SWIG_0"
142 * +++ class ----------------------------------------
143 * | sym:name - "Class"
144 * | symtab - 0xa064d80
145 * | sym:symtab - 0xa064cc0
146 * | sym:overname - "__SWIG_0"
148 * +++ class ----------------------------------------
149 * | sym:name - "Struct"
150 * | symtab - 0xa064f00
151 * | sym:symtab - 0xa064cc0
152 * | sym:overname - "__SWIG_0"
154 * +++ cdecl ----------------------------------------
156 * | sym:symtab - 0xa064f00
157 * | sym:overname - "__SWIG_0"
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).
167 * ----------------------------------------------------------------------------- */
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 */
175 /* common attribute keys, to avoid calling find_key all the times */
180 void Swig_symbol_dump_symtable() {
181 Printf(stdout, "DUMPING SYMTABLE start =======================================\n");
183 Hash *cst = Getattr(current_symtab, "csymtab");
184 Swig_print_tree(cst);
186 Swig_print_tree(Getattr(cst, "NumSpace"));
189 Printf(stdout, "DUMPING SYMTABLE end =======================================\n");
193 /* -----------------------------------------------------------------------------
196 * Create a new symbol table object
197 * ----------------------------------------------------------------------------- */
199 void Swig_symbol_init() {
202 current_symtab = NewHash();
203 ccurrent = NewHash();
204 set_nodeType(current_symtab, "symboltable");
205 Setattr(current_symtab, "symtab", current);
207 Setattr(current_symtab, "csymtab", ccurrent);
210 /* Set the global scope */
212 Setattr(symtabs, "", current_symtab);
213 Delete(current_symtab);
214 global_scope = current_symtab;
217 /* -----------------------------------------------------------------------------
218 * Swig_symbol_setscopename()
220 * Set the C scopename of the current symbol table.
221 * ----------------------------------------------------------------------------- */
223 void Swig_symbol_setscopename(const_String_or_char_ptr name) {
225 /* assert(!Getattr(current_symtab,"name")); */
226 Setattr(current_symtab, "name", name);
228 /* Set nested scope in parent */
230 qname = Swig_symbol_qualifiedscopename(current_symtab);
232 /* Save a reference to this scope */
233 Setattr(symtabs, qname, current_symtab);
237 /* -----------------------------------------------------------------------------
238 * Swig_symbol_getscopename()
240 * Get the C scopename of the current symbol table
241 * ----------------------------------------------------------------------------- */
243 String *Swig_symbol_getscopename() {
244 return Getattr(current_symtab, "name");
247 /* -----------------------------------------------------------------------------
248 * Swig_symbol_getscope()
250 * Given a fully qualified C scopename, this function returns a symbol table
251 * ----------------------------------------------------------------------------- */
253 Symtab *Swig_symbol_getscope(const_String_or_char_ptr name) {
256 if (Equal("::", (const_String_or_char_ptr ) name))
258 return Getattr(symtabs, name);
261 /* -----------------------------------------------------------------------------
262 * Swig_symbol_qualifiedscopename()
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 * ----------------------------------------------------------------------------- */
268 String *Swig_symbol_qualifiedscopename(Symtab *symtab) {
273 symtab = current_symtab;
274 parent = Getattr(symtab, "parentNode");
276 result = Swig_symbol_qualifiedscopename(parent);
278 name = Getattr(symtab, "name");
281 result = NewStringEmpty();
284 Printv(result, "::", name, NIL);
286 Append(result, name);
292 /* -----------------------------------------------------------------------------
293 * Swig_symbol_newscope()
295 * Create a new scope. Returns the newly created scope.
296 * ----------------------------------------------------------------------------- */
298 Symtab *Swig_symbol_newscope() {
305 set_nodeType(h, "symboltable");
306 Setattr(h, "symtab", hsyms);
308 set_parentNode(h, current_symtab);
310 n = lastChild(current_symtab);
312 set_firstChild(current_symtab, h);
314 set_nextSibling(n, h);
317 set_lastChild(current_symtab, h);
319 ccurrent = NewHash();
320 Setattr(h, "csymtab", ccurrent);
326 /* -----------------------------------------------------------------------------
327 * Swig_symbol_setscope()
329 * Set the current scope. Returns the previous current scope.
330 * ----------------------------------------------------------------------------- */
332 Symtab *Swig_symbol_setscope(Symtab *sym) {
333 Symtab *ret = current_symtab;
334 current_symtab = sym;
335 current = Getattr(sym, "symtab");
337 ccurrent = Getattr(sym, "csymtab");
342 /* -----------------------------------------------------------------------------
343 * Swig_symbol_popscope()
345 * Pop out of the current scope. Returns the popped scope and sets the
346 * scope to the parent scope.
347 * ----------------------------------------------------------------------------- */
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");
355 ccurrent = Getattr(current_symtab, "csymtab");
360 /* -----------------------------------------------------------------------------
361 * Swig_symbol_current()
363 * Return the current symbol table.
364 * ----------------------------------------------------------------------------- */
366 Symtab *Swig_symbol_current() {
367 return current_symtab;
370 /* -----------------------------------------------------------------------------
371 * Swig_symbol_alias()
373 * Makes an alias for a symbol in the global symbol table.
374 * ----------------------------------------------------------------------------- */
376 void Swig_symbol_alias(const_String_or_char_ptr aliasname, Symtab *s) {
377 String *qname = Swig_symbol_qualifiedscopename(current_symtab);
379 Printf(qname, "::%s", aliasname);
381 qname = NewString(aliasname);
383 if (!Getattr(symtabs, qname)) {
384 Setattr(symtabs, qname, s);
389 /* -----------------------------------------------------------------------------
390 * Swig_symbol_inherit()
392 * Inherit symbols from another scope.
393 * ----------------------------------------------------------------------------- */
395 void Swig_symbol_inherit(Symtab *s) {
397 List *inherit = Getattr(current_symtab, "inherit");
400 Setattr(current_symtab, "inherit", inherit);
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"));
408 assert(s != current_symtab);
410 for (i = 0; i < ilen; i++) {
411 Node *n = Getitem(inherit, i);
413 return; /* Already inherited */
418 /* -----------------------------------------------------------------------------
421 * Adds a node to the C symbol table only.
422 * ----------------------------------------------------------------------------- */
424 void Swig_symbol_cadd(const_String_or_char_ptr name, Node *n) {
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.
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:
437 template<class T> T foo(T x);
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).
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);
456 Printf(stderr, "symbol_cadd %s %x\n", name, n);
458 cn = Getattr(ccurrent, name);
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 */
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");
473 Printf(stderr, "found template classforward %s\n", Getattr(cn, "name"));
476 String *value = Getattr(pc, "value");
479 Printf(stderr, "add default template value %s %s\n", Getattr(pc, "name"), value);
481 Setattr(pn, "value", value);
483 pc = nextSibling(pc);
484 pn = nextSibling(pn);
486 Setattr(n, "templateparms", Getattr(cn, "templateparms"));
488 Setattr(ccurrent, name, n);
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);
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);
503 /* No conflict. Add the symbol */
504 Setattr(ccurrent, name, n);
507 /* Multiple entries in the C symbol table. We append to to the symbol table */
510 cn = Getattr(ccurrent, name);
515 /* already added. Bail */
518 fn = Getattr(fn, "csym:nextSibling");
521 Setattr(pn, "csym:nextSibling", append);
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:
532 typedef Foo OtherFoo;
534 In this case, OtherFoo becomes an alias for Foo. */
538 while (td && Checkattr(td, "nodeType", "cdecl") && Checkattr(td, "storage", "typedef")) {
541 type = Copy(Getattr(td, "type"));
542 SwigType_push(type, Getattr(td, "decl"));
543 td1 = Swig_symbol_clookup(type, 0);
545 /* Fix pathetic case #1214313:
559 ie, when Foo -> FooBar -> Foo, jump one scope up when possible.
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");
568 td1 = Swig_symbol_clookup(type, sc);
577 Symtab *st = Getattr(td, "symtab");
579 Swig_symbol_alias(Getattr(n, "name"), st);
587 /* -----------------------------------------------------------------------------
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.
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 * ----------------------------------------------------------------------------- */
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;
604 String *name, *overname;
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 */
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.
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:
622 template<class T> T foo(T x);
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).
629 name = Getattr(n, "name");
630 if (name && Len(name)) {
631 Swig_symbol_cadd(name, n);
634 /* No symbol name defined. We return. */
636 Setattr(n, "sym:symtab", current_symtab);
640 /* If node is ignored. We don't proceed any further */
641 if (GetFlag(n, "feature:ignore"))
644 /* See if the symbol already exists in the table */
645 c = Getattr(current, symname);
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:
653 In this case, "Foo" sits in the symbol table. However, the
654 definition of Foo would replace the entry if it appeared later. */
656 if (c && Getattr(c, "sym:weak")) {
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.
665 (2) A conflict between namespaces is okay--namespaces are open
667 (3) Otherwise, overloading is only allowed for functions
670 /* Check for namespaces */
671 String *ntype = Getattr(n, "nodeType");
672 if ((Equal(ntype, Getattr(c, "nodeType"))) && ((Equal(ntype, "namespace")))) {
677 cl = Getattr(cl, "sym:nextSibling");
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);
685 if (Getattr(n, "allows_typedef"))
687 if (Getattr(c, "allows_typedef"))
692 /* At least one of the nodes allows typedef overloading. Make sure that
693 both don't--this would be a conflict */
698 /* Figure out which node allows the typedef */
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 */
711 /* Hmmm. This appears to be okay. Make sure the symbol table refers to the allow_type node */
714 Setattr(current, symname, td);
715 Setattr(td, "sym:symtab", current_symtab);
716 Setattr(td, "sym:name", symname);
721 decl = Getattr(c, "decl");
722 ndecl = Getattr(n, "decl");
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"))
734 if (Equal(nt2, "using"))
737 if ((!Equal(nt1, nt2)) && !(u1 || u2))
741 if ((!SwigType_isfunction(decl)) || (!SwigType_isfunction(ndecl))) {
742 /* Symbol table conflict */
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");
752 /* If either one is declared as typedef, forget it. We're hosed */
753 if (Cmp(cstorage, "typedef") == 0) {
756 if (Cmp(nstorage, "typedef") == 0) {
760 /* Okay. Walk down the list of symbols and see if we get a declarator match */
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");
768 decl = Getattr(cn, "decl");
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 */
784 cn = Getattr(cn, "sym:nextSibling");
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);
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);
811 /* Printf(stdout,"%s %s %s\n", symname, Getattr(n,"decl"), Getattr(n,"sym:overname")); */
812 Setattr(current, symname, n);
816 /* -----------------------------------------------------------------------------
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.
823 * This function operates in the C namespace, not the target namespace.
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 * ----------------------------------------------------------------------------- */
830 static Node *_symbol_lookup(const String *name, Symtab *symtab, int (*check) (Node *n)) {
833 Hash *sym = Getattr(symtab, "csymtab");
839 n = Getattr(sym, name);
842 Printf(stderr, "symbol_look %s %x %x %s\n", name, n, symtab, Getattr(symtab, "name"));
846 /* if a check-function is defined. Call it to determine a match */
854 /* Terminate the search right away */
864 if (!n && SwigType_istemplate(name)) {
867 dname = Swig_symbol_template_deftype(name, symtab);
868 if (!Equal(dname, name)) {
869 n = _symbol_lookup(dname, symtab, check);
876 inherit = Getattr(symtab, "inherit");
880 for (i = 0; i < len; i++) {
881 n = _symbol_lookup(name, Getitem(inherit, i), check);
893 static Node *symbol_lookup(const_String_or_char_ptr name, Symtab *symtab, int (*check) (Node *n)) {
895 if (DohCheck(name)) {
896 n = _symbol_lookup(name, symtab, check);
898 String *sname = NewString(name);
899 n = _symbol_lookup(sname, symtab, check);
907 /* -----------------------------------------------------------------------------
908 * symbol_lookup_qualified()
909 * ----------------------------------------------------------------------------- */
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 */
920 Swig_scopename_split(name, &prefix, &bname);
921 n = symbol_lookup_qualified(bname, symtab, prefix, local, checkfunc);
928 /* Make qualified name of current scope */
930 String *qname = Swig_symbol_qualifiedscopename(symtab);
931 const String *cqname;
934 if (prefix && Len(prefix)) {
935 Printv(qname, "::", prefix, NIL);
938 Append(qname, prefix);
945 st = Getattr(symtabs, cqname);
946 /* Found a scope match */
953 n = symbol_lookup(name, st, checkfunc);
960 Node *pn = Getattr(symtab, "parentNode");
962 n = symbol_lookup_qualified(name, pn, prefix, local, checkfunc);
971 /* -----------------------------------------------------------------------------
972 * Swig_symbol_clookup()
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 * ----------------------------------------------------------------------------- */
979 Node *Swig_symbol_clookup(const_String_or_char_ptr name, Symtab *n) {
984 hsym = current_symtab;
986 if (!Checkattr(n, "nodeType", "symboltable")) {
987 n = Getattr(n, "sym:symtab");
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);
1004 String *prefix = Swig_scopename_prefix(name);
1006 s = symbol_lookup_qualified(name, hsym, 0, 0, 0);
1016 s = symbol_lookup(name, hsym, 0);
1019 hsym = Getattr(hsym, "parentNode");
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 */
1034 Swig_warning(WARN_PARSE_USING_UNDEF, Getfile(s), Getline(s), "Nothing known about '%s'.\n", Getattr(s, "uname"));
1041 /* -----------------------------------------------------------------------------
1042 * Swig_symbol_clookup_check()
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 * ----------------------------------------------------------------------------- */
1051 Node *Swig_symbol_clookup_check(const_String_or_char_ptr name, Symtab *n, int (*checkfunc) (Node *n)) {
1056 hsym = current_symtab;
1058 if (!Checkattr(n, "nodeType", "symboltable")) {
1059 n = Getattr(n, "sym:symtab");
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);
1076 String *prefix = Swig_scopename_prefix(name);
1078 s = symbol_lookup_qualified(name, hsym, 0, 0, checkfunc);
1088 s = symbol_lookup(name, hsym, checkfunc);
1091 hsym = Getattr(hsym, "parentNode");
1099 /* Check if s is a 'using' node */
1100 while (s && Checkattr(s, "nodeType", "using")) {
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"));
1111 /* -----------------------------------------------------------------------------
1112 * Swig_symbol_clookup_local()
1113 * ----------------------------------------------------------------------------- */
1115 Node *Swig_symbol_clookup_local(const_String_or_char_ptr name, Symtab *n) {
1120 hsym = current_symtab;
1123 if (!Checkattr(n, "nodeType", "symboltable")) {
1124 n = Getattr(n, "sym:symtab");
1128 h = Getattr(n, "csymtab");
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);
1140 s = symbol_lookup_qualified(name, hsym, 0, 0, 0);
1144 s = symbol_lookup(name, hsym, 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"));
1152 Swig_warning(WARN_PARSE_USING_UNDEF, Getfile(s), Getline(s), "Nothing known about '%s'.\n", Getattr(s, "uname"));
1159 /* -----------------------------------------------------------------------------
1160 * Swig_symbol_clookup_local_check()
1161 * ----------------------------------------------------------------------------- */
1163 Node *Swig_symbol_clookup_local_check(const_String_or_char_ptr name, Symtab *n, int (*checkfunc) (Node *)) {
1168 hsym = current_symtab;
1171 if (!Checkattr(n, "nodeType", "symboltable")) {
1172 n = Getattr(n, "sym:symtab");
1176 h = Getattr(n, "csymtab");
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);
1188 s = symbol_lookup_qualified(name, hsym, 0, 0, checkfunc);
1192 s = symbol_lookup(name, hsym, checkfunc);
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"));
1208 /* -----------------------------------------------------------------------------
1209 * Swig_symbol_cscope()
1211 * Look up a scope name.
1212 * ----------------------------------------------------------------------------- */
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);
1221 /* -----------------------------------------------------------------------------
1222 * Swig_symbol_remove()
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 * ----------------------------------------------------------------------------- */
1229 void Swig_symbol_remove(Node *n) {
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");
1242 /* If previous symbol, just fix the links */
1245 Setattr(symprev, "sym:nextSibling", symnext);
1246 fixovername = symprev; /* fix as symbol to remove is somewhere in the middle of the linked list */
1248 Delattr(symprev, "sym:nextSibling");
1251 /* If no previous symbol, see if there is a next symbol */
1253 Setattr(symtab, symname, symnext);
1254 fixovername = symnext; /* fix as symbol to remove is at head of linked list */
1256 Delattr(symtab, symname);
1261 Setattr(symnext, "sym:previousSibling", symprev);
1263 Delattr(symnext, "sym:previousSibling");
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");
1276 Node *nn = fixovername;
1277 Node *head = fixovername;
1280 /* find head of linked list */
1283 nn = Getattr(nn, "sym:previousSibling");
1286 /* adjust all the sym:overname strings to start from 0 and increment by one */
1289 assert(Getattr(nn, "sym:overname"));
1290 Delattr(nn, "sym:overname");
1291 overname = NewStringf("__SWIG_%d", pn);
1292 Setattr(nn, "sym:overname", overname);
1295 nn = Getattr(nn, "sym:nextSibling");
1300 /* -----------------------------------------------------------------------------
1301 * Swig_symbol_qualified()
1303 * Return the qualified name of a symbol
1304 * ----------------------------------------------------------------------------- */
1306 String *Swig_symbol_qualified(Node *n) {
1308 if (Checkattr(n, "nodeType", "symboltable")) {
1311 symtab = Getattr(n, "sym:symtab");
1314 return NewStringEmpty();
1316 Printf(stderr, "symbol_qscope %s %x %s\n", Getattr(n, "name"), symtab, Getattr(symtab, "name"));
1318 return Swig_symbol_qualifiedscopename(symtab);
1321 /* -----------------------------------------------------------------------------
1322 * Swig_symbol_isoverloaded()
1324 * Check if a symbol is overloaded. Returns the first symbol if so.
1325 * ----------------------------------------------------------------------------- */
1327 Node *Swig_symbol_isoverloaded(Node *n) {
1328 return Getattr(n, "sym:overloaded");
1331 /* -----------------------------------------------------------------------------
1332 * Swig_symbol_type_qualify()
1334 * Create a fully qualified type name
1335 * ----------------------------------------------------------------------------- */
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;
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();
1354 String *cres = Getattr(qualify_cache, scopetype);
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;) {
1371 String *qparm = Swig_symbol_type_qualify(ti.item, st);
1372 if (tscope && (tscope != st)) {
1373 String *ty = Swig_symbol_type_qualify(qparm, tscope);
1378 vparm = Swig_symbol_template_param_eval(qparm, st);
1379 Append(qprefix, vparm);
1387 Append(qprefix, ")>");
1388 Append(qprefix, tsuffix);
1393 Printf(stderr, "symbol_temp_qual %s %s\n", e, qprefix);
1395 #ifdef SWIG_TEMPLATE_QUALIFY_CACHE
1396 Setattr(qualify_cache, scopetype, qprefix);
1404 static int no_constructor(Node *n) {
1405 return !Checkattr(n, "nodeType", "constructor");
1408 SwigType *Swig_symbol_type_qualify(const SwigType *t, Symtab *st) {
1410 String *result = NewStringEmpty();
1413 if (strncmp(c, "::", 2) == 0) {
1418 elements = SwigType_split(t);
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);
1426 String *name = Getattr(n, "name");
1430 Printf(stderr, "symbol_qual_ei %d %s %s %x\n", i, name, e, st);
1432 if (!Swig_scopename_check(name)) {
1433 String *qname = Swig_symbol_qualified(n);
1434 if (qname && Len(qname)) {
1436 Insert(e, 0, qname);
1439 Printf(stderr, "symbol_qual_sc %d %s %s %x\n", i, qname, e, st);
1443 } else if (SwigType_istemplate(e)) {
1444 SwigType *ty = Swig_symbol_template_qualify(e, st);
1449 if (strncmp(Char(e), "::", 2) == 0) {
1454 } else if (SwigType_isfunction(e)) {
1455 List *parms = SwigType_parmlist(e);
1456 String *s = NewString("f(");
1457 Iterator pi = First(parms);
1459 String *pf = Swig_symbol_type_qualify(pi.item, st);
1477 Printf(stderr, "symbol_qualify %s %s %x %s\n", t, result, st, st ? Getattr(st, "name") : 0);
1483 /* -----------------------------------------------------------------------------
1484 * Swig_symbol_template_reduce()
1485 * Resolves template parameter types
1488 * typedef Int Integer;
1490 * Foo<(Int,Integer)>
1493 * ----------------------------------------------------------------------------- */
1496 SwigType *Swig_symbol_template_reduce(SwigType *qt, Symtab *ntab) {
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)) {
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);
1511 String *qual = Swig_symbol_qualified(n);
1512 np = Copy(Getattr(n, "name"));
1515 if (qual && Len(qual)) {
1516 Insert(np, 0, "::");
1517 Insert(np, 0, qual);
1523 Append(qprefix, np);
1526 Append(qprefix, ",");
1531 Append(qprefix, ")>");
1532 Append(qprefix, tsuffix);
1536 Delete(templateargs);
1541 /* -----------------------------------------------------------------------------
1542 * Swig_symbol_typedef_reduce()
1544 * Chase a typedef through symbol tables looking for a match.
1545 * ----------------------------------------------------------------------------- */
1547 SwigType *Swig_symbol_typedef_reduce(SwigType *ty, Symtab *tab) {
1548 SwigType *prefix, *base;
1552 base = SwigType_base(ty);
1553 prefix = SwigType_prefix(ty);
1555 n = Swig_symbol_clookup(base, tab);
1557 if (SwigType_istemplate(ty)) {
1558 SwigType *qt = Swig_symbol_template_reduce(base, tab);
1562 Printf(stderr, "symbol_reduce (a) %s %s\n", ty, prefix);
1569 Printf(stderr, "symbol_reduce (b) %s %s\n", ty, ty);
1574 nt = Getattr(n, "nodeType");
1575 if (Equal(nt, "using")) {
1576 String *uname = Getattr(n, "uname");
1578 n = Swig_symbol_clookup(base, Getattr(n, "sym:symtab"));
1583 Printf(stderr, "symbol_reduce (c) %s %s\n", ty, ty);
1589 if (Equal(nt, "cdecl")) {
1590 String *storage = Getattr(n, "storage");
1591 if (storage && (Equal(storage, "typedef"))) {
1596 SwigType *nt = Copy(Getattr(n, "type"));
1598 /* Fix for case 'typedef struct Hello hello;' */
1600 const char *dclass[3] = { "struct ", "union ", "class " };
1603 for (i = 0; i < 3; i++) {
1604 if (strstr(c, dclass[i]) == c) {
1605 Replace(nt, dclass[i], "", DOH_REPLACE_FIRST);
1609 decl = Getattr(n, "decl");
1611 SwigType_push(nt, decl);
1613 SwigType_push(nt, 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);
1627 Printf(stderr, "symbol_reduce (d) %s %s\n", qt, ty);
1635 Printf(stderr, "symbol_reduce (e) %s %s\n", ty, ty);
1640 /* -----------------------------------------------------------------------------
1641 * Swig_symbol_string_qualify()
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,
1647 * ----------------------------------------------------------------------------- */
1649 String *Swig_symbol_string_qualify(String *s, Symtab *st) {
1651 String *id = NewStringEmpty();
1652 String *r = NewStringEmpty();
1655 if (isalpha((int) *c) || (*c == '_') || (*c == ':')) {
1660 String *qid = Swig_symbol_type_qualify(id, st);
1671 String *qid = Swig_symbol_type_qualify(id, st);
1680 /* -----------------------------------------------------------------------------
1681 * Swig_symbol_template_defargs()
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 * ----------------------------------------------------------------------------- */
1690 ParmList *Swig_symbol_template_defargs(Parm *parms, Parm *targs, Symtab *tscope, Symtab *tsdecl) {
1691 ParmList *expandedparms = parms;
1692 if (Len(parms) < Len(targs)) {
1698 tp = nextSibling(tp);
1703 String *value = Getattr(tp, "value");
1708 SwigType *nt = Swig_symbol_string_qualify(value, tsdecl);
1711 Printf(stderr, "value %s %s %s\n", value, nt, tsdecl ? Getattr(tsdecl, "name") : tsdecl);
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);
1721 ta = nextSibling(ta);
1724 ntq = Swig_symbol_type_qualify(nt, tsdecl);
1725 if (SwigType_istemplate(ntq)) {
1726 String *ty = Swig_symbol_template_deftype(ntq, tscope);
1730 /* Printf(stderr,"value %s %s %s\n",value,ntr,ntq); */
1731 cp = NewParm(ntq, 0);
1733 set_nextSibling(lp, cp);
1735 expandedparms = CopyParm(cp);
1737 tp = nextSibling(tp);
1746 return expandedparms;
1749 /* -----------------------------------------------------------------------------
1750 * Swig_symbol_template_deftype()
1752 * Apply default args to generic template type
1753 * ----------------------------------------------------------------------------- */
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);
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();
1769 String *cres = Getattr(deftype_cache, scopetype);
1771 Append(result, cres);
1779 Printf(stderr, "finding deftype %s\n", type);
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);
1789 String *pf = SwigType_istemplate(e) ? Swig_symbol_template_deftype(pi.item, tscope)
1790 : Swig_symbol_type_qualify(pi.item, tscope);
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);
1814 Printf(stderr, "deftype type %s %s %d\n", e, tprefix, (long) tempn);
1817 ParmList *tnargs = Getattr(tempn, "templateparms");
1818 ParmList *expandedparms;
1820 Symtab *tsdecl = Getattr(tempn, "sym:symtab");
1823 Printf(stderr, "deftype type %s %s %s\n", tprefix, targs, tsuffix);
1825 Append(tprefix, "<(");
1826 expandedparms = Swig_symbol_template_defargs(tparms, tnargs, tscope, tsdecl);
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);
1835 Printf(stderr, "arg type %s\n", ttq);
1837 if (SwigType_istemplate(ttq)) {
1838 SwigType *ttd = Swig_symbol_template_deftype(ttq, tscope);
1842 Printf(stderr, "arg deftype %s\n", ttq);
1845 Append(tprefix, ttq);
1852 Append(tprefix, ")>");
1853 Append(tprefix, tsuffix);
1854 Append(prefix, tprefix);
1856 Printf(stderr, "deftype %s %s \n", type, tprefix);
1858 Append(result, prefix);
1873 #ifdef SWIG_TEMPLATE_DEFTYPE_CACHE
1874 Setattr(deftype_cache, scopetype, result);
1881 SwigType *Swig_symbol_template_param_eval(const SwigType *p, Symtab *symtab) {
1882 String *value = Copy(p);
1885 Node *n = Swig_symbol_clookup(value, symtab);
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)) {
1896 Append(qn, Getattr(n, "name"));
1904 } else if ((Equal(nt, "cdecl"))) {
1905 String *nv = Getattr(n, "value");