/tags/rel-1.3.35/Source/Swig/symbol.c
C | 1914 lines | 1290 code | 151 blank | 473 comment | 324 complexity | 69bc3ea05f404b26651090f44855d5da MD5 | raw file
Possible License(s): LGPL-2.1, Cube, GPL-3.0, 0BSD, GPL-2.0
Large files files are truncated, but you can click here to view the full file
- /* -----------------------------------------------------------------------------
- * See the LICENSE file for information on copyright, usage and redistribution
- * of SWIG, and the README file for authors - http://www.swig.org/release.html.
- *
- * symbol.c
- *
- * This file implements the SWIG symbol table. See details below.
- * ----------------------------------------------------------------------------- */
- char cvsroot_symbol_c[] = "$Id: symbol.c 10195 2007-12-16 20:55:43Z wsfulton $";
- #include "swig.h"
- #include "swigwarn.h"
- #include <ctype.h>
- /* #define SWIG_DEBUG*/
- /* -----------------------------------------------------------------------------
- * Synopsis
- *
- * This module provides symbol table management for all of SWIG. In previous
- * releases, the management of symbols was rather haphazard. This module tries
- * to correct that.
- *
- * All symbols are associated with simple identifiers. For example, here are some
- * declarations that generate symbol table entries:
- *
- * decl symbol
- * -------------- ------------
- * void foo(int); foo
- * int x; x
- * typedef int *blah; blah
- *
- * Associated with each symbol is a Hash table that can contain any set of
- * attributes that make sense for that object. For example:
- *
- * typedef int *blah; ----> "name" : 'blah'
- * "type" : 'int'
- * "decl" : 'p.'
- * "storage" : 'typedef'
- *
- * In some cases, the symbol table needs to manage overloaded entries. For instance,
- * overloaded functions. In this case, a linked list is built. The "sym:nextSibling"
- * attribute is reserved to hold a link to the next entry. For example:
- *
- * int foo(int); --> "name" : "foo" "name" : "foo"
- * int foo(int,double); "type" : "int" "type" : "int"
- * "decl" : "f(int)." "decl" : "f(int,double)."
- * ... ...
- * "sym:nextSibling" : --------> "sym:nextSibling": --------> ...
- *
- * When more than one symbol has the same name, the symbol declarator is
- * used to detect duplicates. For example, in the above case, foo(int) and
- * foo(int,double) are different because their "decl" attribute is different.
- * However, if a third declaration "foo(int)" was made, it would generate a
- * conflict (due to having a declarator that matches a previous entry).
- *
- * Structures and classes:
- *
- * C/C++ symbol tables are normally managed in a few different spaces. The
- * most visible namespace is reserved for functions, variables, typedef, enum values
- * and such. In C, a separate tag-space is reserved for 'struct name', 'class name',
- * and 'union name' declarations. In SWIG, a single namespace is used for everything
- * this means that certain incompatibilities will arise with some C programs. For instance:
- *
- * struct Foo {
- * ...
- * }
- *
- * int Foo(); // Error. Name clash. Works in C though
- *
- * Due to the unified namespace for structures, special handling is performed for
- * the following:
- *
- * typedef struct Foo {
- *
- * } Foo;
- *
- * In this case, the symbol table contains an entry for the structure itself. The
- * typedef is left out of the symbol table.
- *
- * Target language vs C:
- *
- * The symbol tables are normally managed *in the namespace of the target language*.
- * This means that name-collisions can be resolved using %rename and related
- * directives. A quirk of this is that sometimes the symbol tables need to
- * be used for C type resolution as well. To handle this, each symbol table
- * also has a C-symbol table lurking behind the scenes. This is used to locate
- * symbols in the C namespace. However, this symbol table is not used for error
- * reporting nor is it used for anything else during code generation.
- *
- * Symbol table structure:
- *
- * Symbol tables themselves are a special kind of node that is organized just like
- * a normal parse tree node. Symbol tables are organized in a tree that can be
- * traversed using the SWIG-DOM API. The following attributes names are reserved.
- *
- * name -- Name of the scope defined by the symbol table (if any)
- * This name is the C-scope name and is not affected by
- * %renaming operations
- * symtab -- Hash table mapping identifiers to nodes.
- * csymtab -- Hash table mapping C identifiers to nodes.
- *
- * Reserved attributes on symbol objects:
- *
- * When a symbol is placed in the symbol table, the following attributes
- * are set:
- *
- * sym:name -- Symbol name
- * sym:nextSibling -- Next symbol (if overloaded)
- * sym:previousSibling -- Previous symbol (if overloaded)
- * sym:symtab -- Symbol table object holding the symbol
- * sym:overloaded -- Set to the first symbol if overloaded
- *
- * These names are modeled after XML namespaces. In particular, every attribute
- * pertaining to symbol table management is prefaced by the "sym:" prefix.
- *
- * An example dump of the parse tree showing symbol table entries for the
- * following code should clarify this:
- *
- * namespace OuterNamespace {
- * namespace InnerNamespace {
- * class Class {
- * };
- * struct Struct {
- * int Var;
- * };
- * }
- * }
- *
- * +++ namespace ----------------------------------------
- * | sym:name - "OuterNamespace"
- * | symtab - 0xa064bf0
- * | sym:symtab - 0xa041690
- * | sym:overname - "__SWIG_0"
- *
- * +++ namespace ----------------------------------------
- * | sym:name - "InnerNamespace"
- * | symtab - 0xa064cc0
- * | sym:symtab - 0xa064bf0
- * | sym:overname - "__SWIG_0"
- *
- * +++ class ----------------------------------------
- * | sym:name - "Class"
- * | symtab - 0xa064d80
- * | sym:symtab - 0xa064cc0
- * | sym:overname - "__SWIG_0"
- * |
- * +++ class ----------------------------------------
- * | sym:name - "Struct"
- * | symtab - 0xa064f00
- * | sym:symtab - 0xa064cc0
- * | sym:overname - "__SWIG_0"
- *
- * +++ cdecl ----------------------------------------
- * | sym:name - "Var"
- * | sym:symtab - 0xa064f00
- * | sym:overname - "__SWIG_0"
- * |
- *
- *
- * Each class and namespace has its own scope and thus a new symbol table (sym)
- * is created. The sym attribute is only set for the first entry in the symbol
- * table. The sym:symtab entry points to the symbol table in which the symbol
- * exists, so for example, Struct is in the scope OuterNamespace::InnerNamespace
- * so sym:symtab points to this symbol table (0xa064cc0).
- *
- * ----------------------------------------------------------------------------- */
- static Hash *current = 0; /* The current symbol table hash */
- static Hash *ccurrent = 0; /* The current c symbol table hash */
- static Hash *current_symtab = 0; /* Current symbol table node */
- static Hash *symtabs = 0; /* Hash of all symbol tables by fully-qualified name */
- static Hash *global_scope = 0; /* Global scope */
- /* common attribute keys, to avoid calling find_key all the times */
- #if 0
- void Swig_symbol_dump_symtable() {
- Printf(stdout, "DUMPING SYMTABLE start =======================================\n");
- {
- Hash *cst = Getattr(current_symtab, "csymtab");
- Swig_print_tree(cst);
- /*
- Swig_print_tree(Getattr(cst, "NumSpace"));
- */
- }
- Printf(stdout, "DUMPING SYMTABLE end =======================================\n");
- }
- #endif
- /* -----------------------------------------------------------------------------
- * Swig_symbol_init()
- *
- * Create a new symbol table object
- * ----------------------------------------------------------------------------- */
- void Swig_symbol_init() {
- current = NewHash();
- current_symtab = NewHash();
- ccurrent = NewHash();
- set_nodeType(current_symtab, "symboltable");
- Setattr(current_symtab, "symtab", current);
- Delete(current);
- Setattr(current_symtab, "csymtab", ccurrent);
- Delete(ccurrent);
- /* Set the global scope */
- symtabs = NewHash();
- Setattr(symtabs, "", current_symtab);
- Delete(current_symtab);
- global_scope = current_symtab;
- }
- /* -----------------------------------------------------------------------------
- * Swig_symbol_setscopename()
- *
- * Set the C scopename of the current symbol table.
- * ----------------------------------------------------------------------------- */
- void Swig_symbol_setscopename(const String_or_char *name) {
- String *qname;
- /* assert(!Getattr(current_symtab,"name")); */
- Setattr(current_symtab, "name", name);
- /* Set nested scope in parent */
- qname = Swig_symbol_qualifiedscopename(current_symtab);
- /* Save a reference to this scope */
- Setattr(symtabs, qname, current_symtab);
- Delete(qname);
- }
- /* -----------------------------------------------------------------------------
- * Swig_symbol_getscopename()
- *
- * Get the C scopename of the current symbol table
- * ----------------------------------------------------------------------------- */
- String *Swig_symbol_getscopename() {
- return Getattr(current_symtab, "name");
- }
- /* -----------------------------------------------------------------------------