]>
git.saurik.com Git - wxWidgets.git/blob - wxPython/wxSWIG/SWIG/cplus.cxx
   1 /******************************************************************************* 
   2  * Simplified Wrapper and Interface Generator  (SWIG) 
   4  * Author : David Beazley 
   6  * Department of Computer Science 
   7  * University of Chicago 
  10  * beazley@cs.uchicago.edu 
  12  * Please read the file LICENSE for the copyright and terms by which SWIG 
  13  * can be used and distributed. 
  14  *******************************************************************************/ 
  18 /******************************************************************************* 
  23  * This module defines parser entry points for supporting C++.  Primarily 
  24  * this module is in charge of keeping track of the contents of C++ classes, 
  25  * organizing inheritance, and other things. 
  27  * Eventually this module will be merged with the type handling mechanism 
  28  * in SWIG 2.0 so it's a little messy right now. 
  32  * 1.  The words "simple" and "C++" are rarely used in the same 
  33  *     sentence.   Therefore this module is going to be some sort 
  36  * 2.  I'm using the "Annotated C++ Reference Manual" (ARM) as my 
  37  *     reference for handling certain cases.     Of course, there 
  38  *     is a high probability that I have misinterpreted or overlooked 
  41  * 3.  It is not my intent to write a full C++ compiler. 
  43  *     My goals are simple : 
  44  *           -  Support simple ANSI C-like class member functions and data. 
  45  *           -  Support constructors and destructors. 
  46  *           -  static member functions. 
  47  *           -  basic inheritance. 
  48  *           -  virtual functions. 
  51  *     I do not plan to support the following anytime in the near future 
  52  *           -  Operator overloading 
  57  * The control flow in this module is completely insane.  But here's the 
  60  *       Stage 1 :  SWIG Parsing 
  61  *                  cplus_open_class()        - Open up a new class 
  62  *                  cplus_*                   - Add members to class 
  63  *                  cplus_inherit()           - Inherit from base classes 
  64  *                  cplus_class_close()       - Close class 
  71  *                  ... and so on, until the end of the file 
  73  *       After stage 1, all classes have been read, but nothing has been 
  74  *       sent to the language module yet. 
  76  *       Stage 2 :  Code generation 
  77  *                  For each class we've saved, do this : 
  78  *                       lang->cpp_open_class()     - Open class 
  79  *                       lang->cpp_*                - Emit members 
  80  *                       lang->cpp_inherit()        - Inherit 
  81  *                       lang->cpp_close_class()    - Close class 
  83  * This two-stage approach solves a number of problems related to working 
  84  * with multiple files, mutually referenced classes, add adding methods. 
  85  * Just keep in mind that all C++ code is emitted *after* an entire SWIG 
  86  * file has been parsed. 
  88  * Improved code generation and inheritance of added methods (2/18/97): 
  90  * Each C++ declaration now has an associated function name attached to it. 
  97  * Generates a member function "bar()" with a accessor function named 
  98  * "Foo_bar()".  We will use this recorded accessor function to generate 
  99  * better code for inheritance. For example : 
 101  *           class Foo2 : public Foo { 
 106  * Will create a function called "Foo2_bar()" that is really mapped 
 107  * onto the base-class method "Foo_bar()".   This should improve 
 108  * code generation substantially. 
 111  *        -    Each target language is responsible for naming wrapper 
 114  *******************************************************************************/ 
 116 // Some status variables 
 118 static int    Inherit_mode 
= 0;             // Set if we're inheriting members 
 119 static char   *ccode 
= 0;                   // Set to optional C code (if available) 
 120 static Hash   
*localtypes
;                  // Localtype hash 
 121 static int    abstract 
=0;                  // Status bit set during code generation 
 123 static int    cpp_id 
= 0; 
 125 // Forward references 
 127 void cplus_member_func(char *, char *, DataType 
*, ParmList 
*, int); 
 128 void cplus_constructor(char *, char *, ParmList 
*); 
 129 void cplus_destructor(char *, char *); 
 130 void cplus_variable(char *, char *, DataType 
*); 
 131 void cplus_static_func(char *, char *, DataType 
*, ParmList 
*); 
 132 void cplus_declare_const(char *, char *, DataType 
*, char *); 
 133 void cplus_static_var(char *, char *, DataType 
*); 
 134 void cplus_inherit_decl(char **); 
 136 // ----------------------------------------------------------------------------- 
 137 // void add_local_type(char *type, char *classname) 
 138 // void add_local_type(DataType *type, char *classname) 
 140 // Adds a new datatype to the local datatype hash.   This is used to handle 
 141 // datatypes defined within a class. 
 143 // Inputs : Datatype to place in hash 
 147 // Side Effects : Updates localtypes hash. 
 148 // ----------------------------------------------------------------------------- 
 150 static void add_local_type(char *type
, char *classname
) { 
 153   if (!localtypes
) return;        // No hash table initialized, ignore this 
 155   str 
<< classname 
<< "::" << type
; 
 156   localtypes
->add(type
,copy_string(str
)); 
 159 void add_local_type(DataType 
*type
, char *classname
) { 
 160   add_local_type(type
->name
,classname
); 
 163 // ----------------------------------------------------------------------------- 
 164 // void update_local_type(DataType *type) 
 166 // Checks to see whether this datatype is part of a class definition. If so, 
 167 // we update the type-name by appending the class prefix to it.  Uses the 
 168 // name stored in current_class unless unavailable. 
 170 // Inputs : type = Datatype 
 172 // Output : type is updated with a new name. 
 174 // Side Effects : None 
 175 // ----------------------------------------------------------------------------- 
 177 static void update_local_type(DataType 
*type
) { 
 181   // Lookup the datatype in the hash table 
 183   if (!localtypes
) return; 
 185   newname 
= (char *) localtypes
->lookup(type
->name
); 
 187     strcpy(type
->name
, newname
); 
 191 // ----------------------------------------------------------------------------- 
 192 // void update_parms(ParmList *l) 
 194 // Updates all of the parameters in a parameter list with the proper C++ prefix 
 197 // Inputs : l = Parameter list 
 199 // Output : Parameter list l is updated (make sure its a copy!) 
 201 // Side Effects : None 
 202 // ----------------------------------------------------------------------------- 
 204 static void update_parms(ParmList 
*l
) { 
 208     update_local_type(p
->t
); 
 210     // Check for default arguments 
 212     if ((p
->defvalue
) && (localtypes
)) { 
 214       s 
= (char *) localtypes
->lookup(p
->defvalue
); 
 217         p
->defvalue 
= copy_string(s
); 
 224 // ----------------------------------------------------------------------- 
 227 // Base class for various kinds of C++ members 
 228 // ----------------------------------------------------------------------- 
 231   char          *name
;                   // Name of the member 
 232   char          *iname
;                  // Name of member in the interpreter 
 233   int            is_static
;              // Is this a static member? 
 234   int            new_method
;             // Is this a new method (added by SWIG)? 
 235   int            line
;                   // What line number was this member on 
 236   char           *file
;                  // What file was this in? 
 237   char           *code
;                  // Was there any supplied code? 
 238   char           *base
;                  // Base class where this was defined 
 239   int            inherited
;              // Was this member inherited? 
 240   DocEntry       
*de
;                    // Documentation entry 
 241   CPP_member     
*next
;                  // Next member (for building linked lists) 
 242   int            id
;                     // type id when created 
 244   virtual void   inherit(int) { };       // Inheritance rule (optional) 
 245   virtual void   emit() = 0;             // Emit rule 
 248 // ---------------------------------------------------------------------- 
 249 // class CPP_function : public CPP_member 
 251 // Structure for handling a C++ member function 
 252 // ---------------------------------------------------------------------- 
 254 class CPP_function 
: public CPP_member 
{ 
 261   CPP_function(char *n
, char *i
, DataType 
*t
, ParmList 
*l
, int s
, int v 
= 0) { 
 262     name 
= copy_string(n
); 
 263     iname 
= copy_string(i
); 
 264     ret_type 
= new DataType(t
); 
 265     parms 
= new ParmList(l
); 
 268     new_method 
= AddMethods
; 
 269     new_object 
= NewObject
; 
 270     inherited 
= Inherit_mode
; 
 281       if (strlen(CCode
.get())) 
 282         code 
= copy_string(CCode
.get()); 
 289   void inherit(int mode
) { 
 290     doc_entry 
= 0;            // No documentation for an inherited member 
 291     if (mode 
& INHERIT_FUNC
) { 
 292       // Set up the proper addmethods mode and provide C code (if provided) 
 293       int oldaddmethods 
= AddMethods
; 
 294       int oldnewobject 
= NewObject
; 
 295       AddMethods 
= new_method
; 
 296       NewObject 
= new_object
; 
 299         cplus_static_func(name
, iname
, ret_type
, parms
); 
 301         cplus_member_func(name
, iname
, ret_type
, parms
, is_virtual
); 
 303       AddMethods 
= oldaddmethods
; 
 304       NewObject 
= oldnewobject
; 
 311     AddMethods 
= new_method
; 
 312     NewObject 
= new_object
; 
 313     doc_entry 
= de
;            // Restore documentation entry 
 314     line_number 
= line
;        // Restore line and file 
 318     // Make a copy of the parameter list and upgrade its types 
 320     l 
= new ParmList(parms
); 
 321     t 
= new DataType(ret_type
); 
 323     update_local_type(t
); 
 325       lang
->cpp_static_func(name
, iname
, t
, l
); 
 327       lang
->cpp_member_func(name
, iname
, t
, l
); 
 336 // -------------------------------------------------------------------------- 
 337 // class CPP_constructor : public CPP_member 
 339 // Class for holding a C++ constructor definition. 
 340 // -------------------------------------------------------------------------- 
 342 class CPP_constructor 
: public CPP_member 
{ 
 345   CPP_constructor(char *n
, char *i
, ParmList 
*l
) { 
 346     name 
= copy_string(n
); 
 347     iname 
= copy_string(i
); 
 348     parms 
= new ParmList(l
); 
 349     new_method 
= AddMethods
; 
 357       if (strlen(CCode
.get())) 
 358         code 
= copy_string(CCode
.get()); 
 368       AddMethods 
= new_method
; 
 374       // Make a copy of the parameter list and upgrade its types 
 376       l 
= new ParmList(parms
); 
 378       lang
->cpp_constructor(name
,iname
,l
); 
 383         fprintf(stderr
,"%s:%d:  Constructor for abstract base class ignored.\n", 
 391 // -------------------------------------------------------------------------- 
 392 // class CPP_destructor : public CPP_member 
 394 // Class for holding a destructor definition 
 395 // -------------------------------------------------------------------------- 
 397 class CPP_destructor 
: public CPP_member 
{ 
 400   CPP_destructor(char *n
, char *i
) { 
 401     name 
= copy_string(n
); 
 402     iname 
= copy_string(i
); 
 403     new_method 
= AddMethods
; 
 411       if (strlen(CCode
.get())) 
 412         code 
= copy_string(CCode
.get()); 
 421     AddMethods 
= new_method
; 
 426     lang
->cpp_destructor(name
, iname
); 
 430 // ------------------------------------------------------------------------- 
 431 // class CPP_variable : public CPP_member 
 433 // Class for a managing a data member 
 434 // ------------------------------------------------------------------------- 
 436 class CPP_variable 
: public CPP_member 
{ 
 440   CPP_variable(char *n
, char *i
, DataType 
*t
, int s
) { 
 441     name 
= copy_string(n
); 
 442     iname 
= copy_string(i
); 
 443     type 
= new DataType(t
); 
 448     new_method 
= AddMethods
; 
 460   // Emit code for this 
 464     int old_status 
= Status
; 
 466     AddMethods 
= new_method
; 
 472     t 
= new DataType(type
); 
 474       //      if (strcmp(t->qualifier,"const") == 0) Status = Status | STAT_READONLY; 
 476     update_local_type(t
); 
 478       lang
->cpp_variable(name
,iname
,t
); 
 480       lang
->cpp_static_var(name
,iname
,t
); 
 487   // Inherit into another class 
 489   void inherit(int mode
) { 
 490     int oldstatus 
= Status
; 
 493     if (mode 
& INHERIT_VAR
) { 
 495         int oldaddmethods 
= AddMethods
; 
 496         AddMethods 
= new_method
; 
 498         cplus_variable(name
,iname
,type
); 
 499         AddMethods 
= oldaddmethods
; 
 502         cplus_static_var(name
,iname
,type
); 
 509 // ------------------------------------------------------------------------- 
 510 // class CPP_constant : public CPP_member 
 512 // Class for managing constant values 
 513 // ------------------------------------------------------------------------- 
 515 class CPP_constant 
: public CPP_member 
{ 
 519   CPP_constant(char *n
, char *i
, DataType 
*t
, char *v
) { 
 520     name 
= copy_string(n
); 
 521     iname 
= copy_string(i
); 
 522     type 
= new DataType(t
); 
 523     value 
= copy_string(v
); 
 525     new_method 
= AddMethods
; 
 539     AddMethods 
= new_method
; 
 543     lang
->cpp_declare_const(name
,iname
,type
,value
); 
 544     type
->check_defined(); 
 547   void inherit(int mode
) { 
 549     if (mode 
& INHERIT_CONST
) 
 550       cplus_declare_const(name
, iname
, type
, value
); 
 554 // ---------------------------------------------------------------------- 
 557 // Class for managing class members (internally) 
 558 // ---------------------------------------------------------------------- 
 560 static char   *inherit_base_class 
= 0; 
 564   char        *classname
;             // Real class name 
 565   char        *classrename
;           // New name of class (if applicable) 
 566   char        *classtype
;             // class type (struct, union, class) 
 567   int          strip
;                 // Strip off class declarator 
 568   int          wextern
;               // Value of extern wrapper variable for this class 
 569   int          have_constructor
;      // Status bit indicating if we've seen a constructor 
 570   int          have_destructor
;       // Status bit indicating if a destructor has been seen 
 571   int          is_abstract
;           // Status bit indicating if this is an abstract class 
 572   int          generate_default
;      // Generate default constructors 
 573   int          objective_c
;           // Set if this is an objective C class 
 574   int          error
;                 // Set if this class can't be generated 
 575   int          line
;                  // Line number 
 576   char        **baseclass
;            // Base classes (if any) 
 577   Hash        
*local
;                 // Hash table for local types 
 578   Hash        
*scope
;                 // Local scope hash table 
 579   DocEntry    
*de
;                    // Documentation entry of class 
 580   CPP_member  
*members
;               // Linked list of members 
 581   CPP_class   
*next
;                  // Next class 
 582   static CPP_class 
*classlist
;        // List of all classes stored 
 583   Pragma      
*pragmas
;               // Class pragmas 
 585   CPP_class(char *name
, char *ctype
) { 
 587     classname 
= copy_string(name
); 
 588     classtype 
= copy_string(ctype
); 
 592     local 
= new Hash
;                 // Create hash table for storing local datatypes 
 598     // Walk down class list and add to end 
 612     wextern 
= WrapExtern
; 
 613     have_constructor 
= 0; 
 616     generate_default 
= GenerateDefault
; 
 617     objective_c 
= ObjCClass
; 
 620   // ------------------------------------------------------------------------------ 
 621   // Add a new C++ member to this class 
 622   // ------------------------------------------------------------------------------ 
 624   void add_member(CPP_member 
*m
) { 
 627     // Set base class where this was defined 
 628     if (inherit_base_class
) 
 629       m
->base 
= inherit_base_class
; 
 642   // ------------------------------------------------------------------------------ 
 643   // Search for a member with the given name. Returns the member on success, 0 on failure 
 644   // ------------------------------------------------------------------------------ 
 646   CPP_member 
*search_member(char *name
) { 
 651       c 
= m
->iname 
? m
->iname 
: m
->name
; 
 652       if (strcmp(c
,name
) == 0) return m
; 
 658   // ------------------------------------------------------------------------------ 
 659   // Inherit.  Put all the declarations associated with this class into the current 
 660   // ------------------------------------------------------------------------------ 
 662   void inherit_decls(int mode
) { 
 666       inherit_base_class 
= m
->base
; 
 671     inherit_base_class 
= 0; 
 674   // ------------------------------------------------------------------------------ 
 675   // Emit all of the declarations associated with this class 
 676   // ------------------------------------------------------------------------------ 
 679     CPP_member    
*m 
= members
; 
 680     int  last_scope 
= name_scope(0); 
 681     abstract 
= is_abstract
; 
 684       name_scope(cpp_id
);         // Set proper naming scope 
 688     name_scope(last_scope
); 
 691   // ------------------------------------------------------------------------------ 
 692   // Search for a given class in the list 
 693   // ------------------------------------------------------------------------------ 
 695   static CPP_class 
*search(char *name
) { 
 700       if (strcmp(name
,c
->classname
) == 0) return c
; 
 706   // ------------------------------------------------------------------------------ 
 707   // Add default constructors and destructors 
 709   // ------------------------------------------------------------------------------ 
 711   void create_default() { 
 712     if (!generate_default
) return; 
 714     // Try to generate a constructor if not available. 
 717     if ((!have_constructor
) && (1)) { 
 720       doc_entry 
= new DocDecl(classname
,this->de
); 
 721       cplus_constructor(classname
,0,l
); 
 724     if (!have_destructor
) { 
 725       doc_entry 
= new DocDecl(classname
,this->de
); 
 726       cplus_destructor(classname
,0); 
 730   // ------------------------------------------------------------------------------ 
 731   // Dump *all* of the classes saved out to the various 
 732   // language modules (this does what cplus_close_class used to do) 
 733   // ------------------------------------------------------------------------------ 
 734   static void create_all(); 
 737 CPP_class 
*CPP_class::classlist 
= 0; 
 738 static CPP_class     
*current_class
; 
 740 void CPP_class::create_all() { 
 746       localtypes 
= c
->local
; 
 747       if ((!c
->wextern
) && (c
->classtype
)) { 
 748         ObjCClass 
= c
->objective_c
; 
 750         lang
->cpp_open_class(c
->classname
,c
->classrename
,c
->classtype
,c
->strip
); 
 751         lang
->cpp_pragma(c
->pragmas
); 
 754           cplus_inherit_decl(c
->baseclass
); 
 757         lang
->cpp_close_class(); 
 764 // ----------------------------------------------------------------------------- 
 765 // char *cplus_base_class(char *name) 
 767 // Given a member name, return the base class that it belongs to. 
 768 // ----------------------------------------------------------------------------- 
 770 char *cplus_base_class(char *name
) { 
 771   CPP_member 
*m 
= current_class
->search_member(name
); 
 778 // ----------------------------------------------------------------------------- 
 779 // void cplus_open_class(char *name, char *rname, char *ctype) 
 781 // Opens up a new C++ class.   If rname is given, we'll be renaming the 
 782 // class.    This also sets up some basic type equivalence for the 
 786 //          name      = Name of the class 
 787 //          rname     = New name of the class (using %name() directive) 
 788 //          ctype     = Class type ("class","struct", or "union") 
 793 //          Creates a new class obect internally. 
 794 //          Added type-mappings to the SWIG type-checker module. 
 795 //          Sets a number of internal state variables for later use. 
 797 // ----------------------------------------------------------------------------- 
 799 void cplus_open_class(char *name
, char *rname
, char *ctype
) { 
 801   extern void typeeq_derived(char *, char *, char *cast
=0); 
 805   // Add some symbol table management here 
 807   // Search for a previous class definition 
 809   c 
= CPP_class::search(name
); 
 812       // Hmmm. We already seem to have defined this class so we'll 
 813       // create a new class object for whatever reason 
 814       current_class 
= new CPP_class(name
, ctype
); 
 816       // Looks like a reference was made to this class earlier 
 817       // somehow, but no other information is known.  We'll 
 818       // make it our current class and fix it up a bit 
 820       c
->classtype 
= copy_string(ctype
); 
 823     // Create a new class 
 824     current_class 
= new CPP_class(name
, ctype
); 
 825     current_class
->de 
= doc_entry
; 
 828   // Set localtypes hash to our current class 
 830   localtypes 
= current_class
->local
; 
 832   // If renaming the class, set the new name 
 835     current_class
->classrename 
= copy_string(rname
); 
 838   // Make a typedef for both long and short versions of this datatype 
 842       if (strlen(ctype
) > 0 && strcmp(ctype
, "class") != 0) { 
 843         sprintf(temp
,"%s %s", ctype
, name
); 
 844         typeeq_derived(temp
,name
);       // Map "struct foo" to "foo" 
 845         typeeq_derived(name
,temp
);       // Map "foo" to "struct foo" 
 850   AddMethods 
= 0;          // Reset add methods flag 
 854 // ----------------------------------------------------------------------------- 
 855 // DocEntry *cplus_set_class(char *name) 
 857 // This function sets the current class to a given name.   If the class 
 858 // doesn't exist, this function will create one.   If it already exists, 
 859 // we'll just use that. 
 861 // This function is used primarily to add or manipulate an already 
 862 // existing class, but in a different location.  For example : 
 864 //         %include "class.h"        // Grab some classes 
 866 //         %addmethods MyClass {     // Add some members for shadow classes 
 870 // Sounds weird, but returns the documentation entry to class if it exists. 
 871 // The parser needs this so we can generate documentation correctly. 
 873 // Inputs : name   = Name of the class 
 875 // Output : Documentation entry of class or NULL if it doesn't exist. 
 876 //          The parser needs the documentation entry to properly associate 
 880 //          Changes the current class object.  Resets a number of internal 
 881 //          state variables.  Should not be called inside of a open class 
 883 // ----------------------------------------------------------------------------- 
 885 DocEntry 
*cplus_set_class(char *name
) { 
 889   // Look for a previous class definition 
 891   c 
= CPP_class::search(name
); 
 894     localtypes 
= c
->local
; 
 897     fprintf(stderr
,"%s:%d:  Warning class %s undefined.\n",input_file
,line_number
,name
); 
 898     current_class 
= new CPP_class(name
,0); 
 899     localtypes 
= current_class
->local
; 
 904 // This function closes a class open with cplus_set_class() 
 906 void cplus_unset_class() { 
 910 // ----------------------------------------------------------------------------- 
 911 // void cplus_class_close(char *name) 
 913 // Close a C++ class definition.   Up to this point, we've only been collecting 
 914 // member definitions.     This function merely closes the class object and 
 915 // stores it in a list.  All classes are dumped after processing has completed. 
 917 // If name is non-null, it means that the name of the class has actually been 
 918 // set after all of the definitions.  For example : 
 924 // If this is the case, we'll use that as our classname and datatype. 
 925 // Otherwise, we'll just go with the classname and classtype set earlier. 
 927 // Inputs : name   = optional "new name" for the class. 
 931 // Side Effects : Resets internal variables. Saves class in internal list. 
 932 //                Registers the class with the language module, but doesn't 
 934 // ----------------------------------------------------------------------------- 
 936 void cplus_class_close(char *name
) { 
 939     // The name of our class suddenly changed by typedef.  Fix things up 
 940     current_class
->classname 
= copy_string(name
); 
 942     // This flag indicates that the class needs to have it's type stripped off 
 943     current_class
->strip 
= 1; 
 946   // If we're in C++ or Objective-C mode. We're going to drop the class specifier 
 948   if ((CPlusPlus
) || (ObjCClass
)) { 
 949     current_class
->strip 
= 1; 
 952   // Register our class with the target language module, but otherwise 
 953   // don't do anything yet. 
 956   if (current_class
->classrename
) iname 
= current_class
->classrename
; 
 957   else iname 
= current_class
->classname
; 
 959   lang
->cpp_class_decl(current_class
->classname
, iname
, current_class
->classtype
); 
 961   // Clear current class variable and reset 
 967 // ----------------------------------------------------------------------------- 
 968 // void cplus_abort(void) 
 970 // Voids the current class--some kind of unrecoverable parsing error occurred. 
 971 // ----------------------------------------------------------------------------- 
 973 void cplus_abort(void) { 
 974   current_class
->error 
= 1; 
 979 // ----------------------------------------------------------------------------- 
 980 // void cplus_cleanup(void) 
 982 // This function is called after all parsing has been completed.  It dumps all 
 983 // of the stored classes out to the language module. 
 989 // Side Effects : Emits all C++ wrapper code. 
 990 // ----------------------------------------------------------------------------- 
 992 void cplus_cleanup(void) { 
 994   // Dump all classes created at once (yikes!) 
 996   CPP_class::create_all(); 
1000 // ----------------------------------------------------------------------------- 
1001 // void cplus_inherit(int count, char **baseclass) 
1003 // Inherit from a baseclass.  This function only really registers 
1004 // the inheritance, but doesn't do anything with it yet. 
1006 // Inputs : baseclass  = A NULL terminated array of strings with the names 
1007 //                       of baseclasses.   For multiple inheritance, there 
1008 //                       will be multiple entries in this list. 
1012 // Side Effects : Sets the baseclass variable of the current class. 
1013 // ----------------------------------------------------------------------------- 
1015 void cplus_inherit(int count
, char **baseclass
) { 
1018   // printf("Inheriting : count = %d, baseclass = %x\n",count,baseclass); 
1019   // If there are baseclasses, make copy of them 
1021     current_class
->baseclass 
= (char **) new char*[count
+1]; 
1022     for (i 
= 0; i 
< count
; i
++) 
1023       current_class
->baseclass
[i
] = copy_string(baseclass
[i
]); 
1024     current_class
->baseclass
[i
] = 0; 
1030 // ----------------------------------------------------------------------------- 
1031 // cplus_generate_types(char **baseclass) 
1033 // Generates the type-mappings between the current class and any associated 
1034 // base classes.  This is done by performing a depth first search of the 
1035 // class hierarchy.   Functions for performing correct type-casting are 
1036 // generated for each base-derived class pair. 
1038 // Inputs : baseclass     = NULL terminated list of base classes 
1042 // Side Effects : Emits pointer conversion functions.  Registers type-mappings 
1043 //                with the type checking module. 
1045 // ----------------------------------------------------------------------------- 
1047 static Hash convert
;                // Hash table of conversion functions 
1049 void cplus_generate_types(char **baseclass
) { 
1052   String     cfunc
, temp1
, temp2
, temp3
; 
1053   extern void typeeq_derived(char *, char *, char *); 
1059   // Generate type-conversion functions and type-equivalence 
1062   while(baseclass
[i
]) { 
1065     bc 
= CPP_class::search(baseclass
[i
]); 
1067       // Generate a conversion function (but only for C++) 
1069       if (!current_class
->objective_c
) { 
1071         temp3 
<< "Swig" << current_class
->classname 
<< "To" << bc
->classname
; 
1073         if (convert
.add(temp3
,(void *) 1) != -1) { 
1075           // Write a function for casting derived type to parent class 
1077           cfunc 
<< "static void *Swig" << current_class
->classname 
<< "To" << bc
->classname
 
1078                 << "(void *ptr) {\n" 
1079                 << tab4 
<< current_class
->classname 
<< " *src;\n" 
1080                 << tab4 
<< bc
->classname 
<< " *dest;\n" 
1081                 << tab4 
<< "src = (" << current_class
->classname 
<< " *) ptr;\n" 
1082                 << tab4 
<< "dest = (" << bc
->classname 
<< " *) src;\n" 
1083             //      << tab4 << "printf(\"casting...\\n\");\n" 
1084                 << tab4 
<< "return (void *) dest;\n" 
1087           fprintf(f_wrappers
,"%s\n",cfunc
.get()); 
1093       // Make a type-equivalence allowing derived classes to be used in functions of the 
1095       if (strlen(current_class
->classtype
) > 0 && 
1096           strcmp(current_class
->classtype
, "class") != 0) { 
1098         temp1 
<< current_class
->classtype 
<< " " << current_class
->classname
; 
1100         temp2 
<< bc
->classtype 
<< " " << bc
->classname
; 
1101         // Add various equivalences to the pointer table 
1103         typeeq_derived(bc
->classname
, current_class
->classname
,temp3
.get()); 
1104         typeeq_derived(temp2
.get(), current_class
->classname
,temp3
.get()); 
1105         typeeq_derived(temp2
.get(), temp1
.get(),temp3
.get()); 
1106         typeeq_derived(bc
->classname
, temp1
.get(),temp3
.get()); 
1108         typeeq_derived(bc
->classname
, current_class
->classname
,temp3
.get()); 
1110       // Now traverse the hierarchy some more 
1111       cplus_generate_types(bc
->baseclass
); 
1117 // ----------------------------------------------------------------------------- 
1118 // void cplus_inherit_decl(char **baseclass) 
1120 // This function is called internally to handle inheritance between classes. 
1121 // Basically, we're going to generate type-checking information and call 
1122 // out to the target language to handle the inheritance. 
1124 // This function is only called when emitting classes to the language modules 
1125 // (after all parsing has been complete). 
1127 // Inputs : baseclass   = NULL terminated list of base-class names. 
1131 // Side Effects : Generates type-mappings.  Calls the language-specific 
1132 //                inheritance function. 
1133 // ----------------------------------------------------------------------------- 
1135 void cplus_inherit_decl(char **baseclass
) { 
1137   // If not base-classes, bail out 
1139   if (!baseclass
) return; 
1142   lang
->cpp_inherit(baseclass
);        // Pass inheritance onto the various languages 
1145   // Create type-information for class hierarchy 
1147   cplus_generate_types(baseclass
); 
1149 // ----------------------------------------------------------------------------- 
1150 // void cplus_inherit_members(char *baseclass, int mode) 
1152 // Inherits members from a class. This is called by specific language modules 
1153 // to bring in members from base classes.   It may or may  not be called. 
1155 // This function is called with a *single* base-class, not multiple classes 
1156 // like other functions.   To do multiple inheritance, simply call this 
1157 // with each of the associated base classes. 
1160 //          baseclass     = Name of baseclass 
1161 //          mode          = Inheritance handling flags 
1162 //                  INHERIT_FUNC          - Import functions in base class 
1163 //                  INHERIT_VAR           - Import variables in base class 
1164 //                  INHERIT_CONST         - Inherit constants 
1165 //                  INHERIT_ALL           - Inherit everything (grossly inefficient) 
1169 // Side Effects : Imports methods from base-classes into derived classes. 
1171 // ----------------------------------------------------------------------------- 
1173 void cplus_inherit_members(char *baseclass
, int mode
) { 
1176   bc 
= CPP_class::search(baseclass
); 
1178     bc
->inherit_decls(mode
); 
1180     fprintf(stderr
,"%s:%d:  Warning.  Base class %s undefined (ignored).\n", input_file
, current_class
->line
, baseclass
); 
1184 // ----------------------------------------------------------------------------- 
1185 // void cplus_member_func(char *name, char *iname, DataType *type, ParmList *, is_virtual) 
1187 // Parser entry point to creating a C++ member function.   This function primarily 
1188 // just records the function and does a few symbol table checks. 
1191 //          name          = Real name of the member function 
1192 //          iname         = Renamed version (may be NULL) 
1193 //          type          = Return datatype 
1194 //          l             = Parameter list 
1195 //          is_virtual    = Set if this is a pure virtual function (ignored) 
1200 //          Adds member function to current class. 
1201 // ----------------------------------------------------------------------------- 
1203 void cplus_member_func(char *name
, char *iname
, DataType 
*type
, ParmList 
*l
, 
1209   // First, figure out if we're renaming this function or not 
1216   // If we're in inherit mode, we need to check for duplicates. 
1220     if (current_class
->search_member(temp_iname
)) { 
1225   // Add it to our C++ class list 
1227   f 
= new CPP_function(name
,temp_iname
,type
,l
,0,is_virtual
); 
1229   current_class
->add_member(f
); 
1231   // If this is a pure virtual function, the class is abstract 
1234     current_class
->is_abstract 
= 1; 
1238 // ----------------------------------------------------------------------------- 
1239 // void cplus_constructor(char *name, char *iname, ParmList *l) 
1241 // Parser entry point for creating a constructor. 
1244 //          name  = Real name of the constructor (usually the same as the class) 
1245 //          iname = Renamed version (may be NULL) 
1246 //          l     = Parameter list 
1251 //          Adds a constructor to the current class. 
1252 // ----------------------------------------------------------------------------- 
1254 void cplus_constructor(char *name
, char *iname
, ParmList 
*l
) { 
1258     // May want to check the naming scheme here 
1260     c 
= new CPP_constructor(name
,iname
,l
); 
1262     current_class
->add_member(c
); 
1263     current_class
->have_constructor 
= 1; 
1267 // ----------------------------------------------------------------------------- 
1268 // void cplus_destructor(char *name, char *iname) 
1270 // Parser entry point for adding a destructor. 
1273 //          name   = Real name of the destructor (usually same as class name) 
1274 //          iname  = Renamed version (may be NULL) 
1279 //          Adds a destructor to the current class 
1281 // ----------------------------------------------------------------------------- 
1283 void cplus_destructor(char *name
, char *iname
) { 
1287   d 
= new CPP_destructor(name
,iname
); 
1289   current_class
->add_member(d
); 
1290   current_class
->have_destructor 
= 1; 
1293 // ----------------------------------------------------------------------------- 
1294 // void cplus_variable(char *name, char *iname, DataType *t) 
1296 // Parser entry point for creating a new member variable. 
1299 //          name  = name of the variable 
1300 //          iname = Renamed version (may be NULL) 
1306 //          Adds a member variable to the current class 
1307 // ----------------------------------------------------------------------------- 
1309 void cplus_variable(char *name
, char *iname
, DataType 
*t
) { 
1314     // If we're in inherit mode, we need to check for duplicates. 
1322       if (current_class
->search_member(temp_iname
)) { 
1327     v 
= new CPP_variable(name
,iname
,t
,0); 
1329     current_class
->add_member(v
); 
1332 // ----------------------------------------------------------------------------- 
1333 // void cplus_static_func(char *name, char *iname, DataType *type, ParmList *l) 
1335 // Parser entry point for creating a new static member function. 
1338 //          name   = Real name of the function 
1339 //          iname  = Renamed version (may be NULL) 
1340 //          type   = Return datatype 
1341 //          l      = Parameter list 
1346 //          Adds a static function to the current class. 
1348 // ----------------------------------------------------------------------------- 
1350 void cplus_static_func(char *name
, char *iname
, DataType 
*type
, ParmList 
*l
) { 
1354   // If we're in inherit mode, we need to check for duplicates. 
1356   if (iname
) temp_iname 
= iname
; 
1357   else temp_iname 
= name
; 
1360     if (current_class
->search_member(iname
)) { 
1361       // Have a duplication 
1366   CPP_function 
*f 
= new CPP_function(name
, temp_iname
, type
, l
, 1); 
1368   current_class
->add_member(f
); 
1371 // ----------------------------------------------------------------------------- 
1372 // void cplus_declare_const(char *name, char *iname, DataType *type, char *value) 
1374 // Parser entry point for creating a C++ constant (usually contained in an 
1378 //          name   = Real name of the constant 
1379 //          iname  = Renamed constant (may be NULL) 
1380 //          type   = Datatype of the constant 
1381 //          value  = String representation of the value 
1386 //          Adds a constant to the current class. 
1387 // ----------------------------------------------------------------------------- 
1389 void cplus_declare_const(char *name
, char *iname
, DataType 
*type
, char *value
) { 
1393   if (iname
) temp_iname 
= iname
; 
1394   else temp_iname 
= name
; 
1396   // If we're in inherit mode, we need to check for duplicates. 
1397   // Possibly issue a warning or perhaps a remapping 
1400     if (current_class
->search_member(temp_iname
)) { 
1405   CPP_constant 
*c 
=  new CPP_constant(name
, temp_iname
, type
, value
); 
1407   current_class
->add_member(c
); 
1409   // Update this symbol in the symbol table 
1410   update_symbol(name
, type
, value
); 
1412   // Add this symbol to local scope of a class 
1413   add_local_type(name
, current_class
->classname
); 
1416 // ----------------------------------------------------------------------------- 
1417 // void cplus_static_var(char *name, char *iname, DataType *type) 
1419 // Parser entry point for adding a static variable 
1422 //          name  = Name of the member 
1423 //          iname = Renamed version (may be NULL) 
1429 //          Adds a static variable to the current class. 
1430 // ----------------------------------------------------------------------------- 
1432 void cplus_static_var(char *name
, char *iname
, DataType 
*type
) { 
1436   if (iname
) temp_iname 
= iname
; 
1437   else temp_iname 
= name
; 
1439   // If we're in inherit mode, we need to check for duplicates. 
1440   // Possibly issue a warning or perhaps a remapping 
1443     if (current_class
->search_member(temp_iname
)) { 
1448   CPP_variable 
*v 
= new CPP_variable(name
, temp_iname
, type
, 1); 
1450   current_class
->add_member(v
); 
1453 // ----------------------------------------------------------------------------- 
1454 // cplus_add_pragma(char *lang, char *name, char *value) 
1456 // Add a pragma to a class 
1457 // ----------------------------------------------------------------------------- 
1459 void cplus_add_pragma(char *lang
, char *name
, char *value
) 
1462   Pragma 
*p 
= new Pragma
; 
1463   p
->filename 
= input_file
; 
1464   p
->lineno 
= line_number
; 
1469   if (!current_class
->pragmas
) { 
1470     current_class
->pragmas 
= p
; 
1473   pp 
= current_class
->pragmas
; 
1480 // ------------------------------------------------------------------------------ 
1481 // C++/Objective-C code generation functions 
1483 // The following functions are responsible for generating the wrapper functions 
1484 // for C++ and Objective-C methods and variables.  These functions are usually 
1485 // called by specific language modules, but individual language modules can 
1486 // choose to do something else. 
1488 // The C++ module sets a number of internal state variables before emitting various 
1489 // pieces of code.  These variables are often checked implicitly by these 
1490 // procedures even though nothing is passed on the command line. 
1492 // The code generator tries to be somewhat intelligent about what its doing. 
1493 // The member_hash Hash table keeps track of wrapped members and is used for 
1494 // sharing code between base and derived classes. 
1495 // ----------------------------------------------------------------------------- 
1497 static Hash member_hash
;  // Hash wrapping member function wrappers to scripting wrappers 
1499 // ----------------------------------------------------------------------------- 
1500 // void cplus_emit_member_func(char *classname, char *classtype, char *classrename, 
1501 //                             char *mname, char *mrename, DataType *type, 
1502 //                             ParmList *l, int mode) 
1504 // This is a generic function to produce a C wrapper around a C++ member function. 
1505 // This function does the following : 
1507 //          1.     Create a C wrapper function 
1508 //          2.     Wrap the C wrapper function like a normal C function in SWIG 
1509 //          3.     Add the function to the scripting language 
1510 //          4.     Fill in the documentation entry 
1512 // Specific languages can choose to provide a different mechanism, but this 
1513 // function is used to provide a low-level C++ interface. 
1515 // The mode variable determines whether to create a new function or only to 
1516 // add it to the interpreter.   This is used to support the %addmethods directive 
1518 //        mode = 0    : Create a wrapper and add it (the normal mode) 
1519 //        mode = 1    : Assume wrapper was already made and add it to the 
1520 //                      interpreter (%addmethods mode) 
1522 // Wrapper functions are usually created as follows : 
1529 //     Foo_bar(Foo *obj, args) { 
1533 // if %addmethods mode is set AND there is supporting C code detected, make 
1534 // a function from it.  The object is always called 'obj'. 
1536 // Then we wrap Foo_bar().   The name "Foo_bar" is actually contained in the parameter 
1537 // cname.   This is so language modules can provide their own names (possibly for 
1538 // function overloading). 
1540 // This function makes no internal checks of the SWIG symbol table.  This is 
1541 // up to the caller. 
1543 // Objective-C support (added 5/24/97) : 
1545 // If the class member function is part of an objective-C interface, everything 
1546 // works the same except that we change the calling mechanism to issue an 
1547 // Objective-C message. 
1549 // Optimizations (added 12/31/97) : 
1551 // For automatically generated wrapper functions.   We now generate macros such 
1553 //        #define Foo_bar(a,b,c)   (a->bar(b,c)) 
1555 // This should make the wrappers a little faster as well as reducing the amount 
1559 //          classname            = Name of C++ class 
1560 //          classtype            = Type of class (struct, union, class) 
1561 //          classrename          = Renamed class (if any) 
1562 //          mname                = Member name 
1563 //          mrename              = Renamed member 
1564 //          type                 = Return type of the function 
1565 //          l                    = Parameter list 
1566 //          mode                 = addmethods mode 
1571 //          Creates C accessor function in the wrapper file. 
1572 //          Calls the language module to create a wrapper function. 
1573 // ----------------------------------------------------------------------------- 
1575 void cplus_emit_member_func(char *classname
, char *classtype
, char *classrename
, 
1576                             char *mname
, char *mrename
, DataType 
*type
, ParmList 
*l
, 
1586     char     *prev_wrap 
= 0; 
1593     // First generate a proper name for the member function 
1595     // Get the base class of this member 
1596     if (!mrename
) temp_mname 
= mname
; 
1597     else temp_mname 
= mrename
; 
1599     char *bc 
= cplus_base_class(temp_mname
); 
1600     if (!bc
) bc 
= classname
; 
1601     if (strlen(bc
) == 0) bc 
= classname
; 
1603     // Generate the name of the C wrapper function (is always the same, regardless 
1606     cname 
<< name_member(mname
,bc
); 
1608     // Generate the scripting name of this function 
1610       prefix 
= classrename
; 
1615       iname 
<< name_member(mrename
,prefix
); 
1617       iname 
<< name_member(mname
,prefix
); 
1619     // Now check to see if we have already wrapped a function like this. 
1620     // If so, we'll just use the existing wrapper. 
1622     key 
<< cname 
<< "+"; 
1623     l
->print_types(key
); 
1624     //    printf("key = %s\n", (char *) key); 
1625     char *temp 
= copy_string(iname
); 
1626     if ((member_hash
.add(key
,temp
)) == -1) { 
1628       prev_wrap 
= (char *) member_hash
.lookup(key
); 
1631     // Only generate code if an already existing wrapper doesn't exist 
1635       // If mode = 0: Then we go ahead and create a wrapper macro 
1640         wrap 
<< "#define " << cname 
<< "(_swigobj"; 
1642         // Walk down the parameter list and Spit out arguments 
1647           if ((p
->t
->type 
!= T_VOID
) || (p
->t
->is_pointer
)) { 
1648             wrap 
<< ",_swigarg" << i
; 
1657           wrap 
<< "_swigobj->" << mname 
<< "(";         // C++ invocation 
1659           wrap 
<< "[ _swigobj " << mname
;               // Objective C invocation 
1664           if (ObjCClass
) wrap 
<< " " << p
->objc_separator
; 
1665           if ((p
->t
->type 
!= T_VOID
) || (p
->t
->is_pointer
)) { 
1666             wrap 
<< "_swigarg" << i
; 
1670           if ((p 
!= 0) && (!ObjCClass
)) 
1679         fprintf(f_wrappers
,"%s",wrap
.get()); 
1683           if (type
->is_reference
) { 
1686           wrap 
<< type
->print_full(); 
1687           if (type
->is_reference
) { 
1691           wrap 
<< " " << cname 
<< "(" << classtype 
<< classname 
<< " *self"; 
1693           // Walk down the parameter list and Spit out arguments 
1697             if ((p
->t
->type 
!= T_VOID
) || (p
->t
->is_pointer
)) { 
1699               if ((p
->call_type 
& CALL_REFERENCE
) || (p
->t
->is_reference
)) { 
1702               wrap 
<< p
->t
->print_full(); 
1703               if ((p
->call_type 
& CALL_REFERENCE
) || (p
->t
->is_reference
)) { 
1705                    if (p
->t
->is_reference
) 
1708               wrap 
<< " " << p
->name
; 
1713           wrap 
<< ") " << ccode
; 
1714           fprintf(f_wrappers
,"%s\n",wrap
.get()); 
1718       // Now add a parameter to the beginning of the function and call 
1719       // a language specific function to add it. 
1721       newparms 
= new ParmList(l
); 
1723       p
->t 
= new DataType
; 
1724       p
->t
->type 
= T_USER
; 
1725       p
->t
->is_pointer 
= 1; 
1729       sprintf(p
->t
->name
,"%s%s", classtype
,classname
); 
1731       newparms
->insert(p
,0);       // Attach parameter to beginning of list 
1733       // Now wrap the thing.  The name of the function is iname 
1735       lang
->create_function(cname
, iname
, type
, newparms
); 
1738       // Already wrapped this function.   Just patch it up 
1739       lang
->create_command(prev_wrap
, iname
); 
1744 // ----------------------------------------------------------------------------- 
1745 // void cplus_emit_static_func(char *classname, char *classtype, char *classrename, 
1746 //                             char *mname, char *mrename, DataType *type, 
1747 //                             ParmList *l, int mode) 
1749 // This is a generic function to produce a wrapper for a C++ static member function 
1750 // or an Objective-C class method. 
1752 // Specific languages can choose to provide a different mechanism, but this 
1753 // function is used to provide a low-level C++ interface. 
1755 // The mode variable determines whether to create a new function or only to 
1756 // add it to the interpreter.   This is used to support the %addmethods directive 
1758 //        mode = 0    : Create a wrapper and add it (the normal mode) 
1759 //        mode = 1    : Assume wrapper was already made and add it to the 
1760 //                      interpreter (%addmethods mode) 
1762 // Wrapper functions are usually created as follows : 
1765 //       static int bar(args) 
1768 //     becomes a command called Foo_bar() 
1770 // if %addmethods mode is set AND there is supporting C code detected, make 
1771 // a function from it. 
1773 // Then we wrap Foo_bar().   The name "Foo_bar" is actually contained in the parameter 
1774 // cname.   This is so language modules can provide their own names (possibly for 
1775 // function overloading). 
1777 // This function makes no internal checks of the SWIG symbol table.  This is 
1778 // up to the caller. 
1781 //          classname            = Name of C++ class 
1782 //          classtype            = Type of class (struct, union, class) 
1783 //          classrename          = Renamed version of class (optional) 
1784 //          mname                = Member name 
1785 //          mrename              = Renamed member (optional) 
1786 //          type                 = Return type of the function 
1787 //          l                    = Parameter list 
1788 //          mode                 = addmethods mode 
1792 // ----------------------------------------------------------------------------- 
1794 void cplus_emit_static_func(char *classname
, char *, char *classrename
, 
1795                             char *mname
, char *mrename
, DataType 
*type
, ParmList 
*l
, 
1799     String   cname
, iname
, key
; 
1802     char     *prev_wrap 
= 0; 
1809     // Generate a function name for the member function 
1811     if (!mrename
) temp_mname 
= mname
; 
1812     else temp_mname 
= mrename
; 
1813     char *bc 
= cplus_base_class(temp_mname
); 
1814     if (!bc
) bc 
= classname
; 
1815     if (strlen(bc
) == 0) bc 
= classname
; 
1817     // Generate the name of the C wrapper function 
1818     if ((!mode
) && (!ObjCClass
)) { 
1819       cname 
<< bc 
<< "::" << mname
; 
1821       cname 
<< name_member(mname
,bc
); 
1824     // Generate the scripting name of this function 
1826       prefix 
= classrename
; 
1831       iname 
<< name_member(mrename
,prefix
); 
1833       iname 
<< name_member(mname
,prefix
); 
1835     // Perform a hash table lookup to see if we've wrapped anything like this before 
1837     key 
<< cname 
<< "+"; 
1838     l
->print_types(key
); 
1839     char *temp 
= copy_string(iname
); 
1840     if ((member_hash
.add(key
,temp
)) == -1) { 
1842       prev_wrap 
= (char *) member_hash
.lookup(key
); 
1846       if (!((mode
) || (ObjCClass
))) { 
1847         // Not an added method and not objective C, just wrap it 
1848         lang
->create_function(cname
,iname
, type
, l
); 
1850         // This is either an added method or an objective C class function 
1852         // If there is attached code, use it. 
1853         // Otherwise, assume the function has been written already and 
1856         wrap 
<< "static " << type
->print_full() << " " << cname 
<< "("; 
1858         // Walk down the parameter list and Spit out arguments 
1861           if ((p
->t
->type 
!= T_VOID
) || (p
->t
->is_pointer
)) { 
1862             if (p
->t
->is_reference
) { 
1865             wrap 
<< p
->t
->print_full(); 
1866             if (p
->t
->is_reference
) { 
1870             wrap 
<< " " << p
->name
; 
1876         if ((mode
) && (ccode
)) { 
1878         } else if (ObjCClass
) { 
1879           // This is an objective-C method 
1881           wrap 
<< "{\n" << tab4
; 
1883           // Emit the function call. 
1885           if ((type
->type 
!= T_VOID
) || (type
->is_pointer
)) { 
1886             // Declare the return value 
1888             if (type
->is_reference
) { 
1890               wrap 
<< tab4 
<< type
->print_full() << "& _result = "; 
1893               wrap 
<< tab4 
<< type
->print_type() << " _result = " << type
->print_cast(); 
1898           wrap 
<< "[ " << classname 
<< " " << mname
;               // Objective C invocation 
1902             wrap 
<< " " << p
->objc_separator
; 
1903             if ((p
->t
->type 
!= T_VOID
) || (p
->t
->is_pointer
)) { 
1904               if (p
->t
->is_reference
) { 
1914           if ((type
->type 
!= T_VOID
) || (type
->is_pointer
)) { 
1915             if (type
->is_reference
) { 
1916               wrap 
<< tab4 
<< "return " << type
->print_cast() << " &_result;\n"; 
1918               wrap 
<< tab4 
<< "return _result;\n"; 
1923         if (ObjCClass 
|| (mode 
&& ccode
)) 
1924           fprintf(f_wrappers
,"%s\n",wrap
.get()); 
1925         lang
->create_function(cname
,iname
,type
,l
); 
1928       // Already wrapped this function.   Just hook up to it. 
1929       lang
->create_command(prev_wrap
, iname
); 
1933 // ----------------------------------------------------------------------------- 
1934 // void cplus_emit_destructor(char *classname, char *classtype, char *classrename, 
1935 //                            char *mname, char *mrename, int mode) 
1937 // Emit a C wrapper around a C++ destructor. 
1939 // Usually this function is used to do the following : 
1946 //     void delete_Foo(Foo *f) { 
1950 // Then we wrap delete_Foo(). 
1953 //          classname =       Name of the C++ class 
1954 //          classtype =       Type of class (struct,class,union) 
1955 //          classrename =     Renamed class (optional) 
1956 //          mname     =       Name of the destructor 
1957 //          mrename   =       Name of the function in the interpreter 
1958 //          mode      =       addmethods mode (0 or 1) 
1963 //          Creates a destructor function and wraps it. 
1964 // ----------------------------------------------------------------------------- 
1966 void cplus_emit_destructor(char *classname
, char *classtype
, char *classrename
, 
1967                            char *mname
, char *mrename
, int mode
) 
1976     // Construct names for the function 
1979       prefix 
= classrename
; 
1983     cname 
<< name_destroy(classname
); 
1985       iname 
<< name_destroy(mrename
); 
1987       iname 
<< name_destroy(prefix
); 
1990       // Spit out a helper function for this member function 
1991       wrap 
<< "#define " << cname 
<< "(_swigobj) ("; 
1993         wrap 
<< "[_swigobj " << mname 
<< "])\n";   // Name of the member is the destructor 
1994       } else if (CPlusPlus
) 
1995         wrap 
<< "delete _swigobj)\n"; 
1997         wrap 
<< "free ((char *) _swigobj))\n"; 
1998       fprintf(f_wrappers
,"%s", wrap
.get()); 
2001         wrap 
<< "static void " << cname 
<< "(" << classtype 
<< classname 
<< " *self) " << ccode
; 
2002         fprintf(f_wrappers
,"%s\n",wrap
.get()); 
2006     // Make a parameter list for this function 
2010     p
->t 
= new DataType
; 
2011     p
->t
->type 
= T_USER
; 
2012     p
->t
->is_pointer 
= 1; 
2015     sprintf(p
->t
->name
,"%s%s", classtype
, classname
); 
2019     type 
= new DataType
; 
2020     type
->type 
= T_VOID
; 
2021     sprintf(type
->name
,"void"); 
2022     type
->is_pointer 
= 0; 
2025     // iname is the desired name of the function in the target language 
2027     lang
->create_function(cname
,iname
,type
,l
); 
2034 // ----------------------------------------------------------------------------- 
2035 // void cplus_emit_constructor(char *classname, char *classtype, char *classrename, 
2036 //                             char *mname, char *mrename, ParmList *l, int mode) 
2038 // Creates a C wrapper around a C++ constructor 
2041 //          classname    = name of class 
2042 //          classtype    = type of class (struct,class,union) 
2043 //          classrename  = Renamed class (optional) 
2044 //          mname        = Name of constructor 
2045 //          mrename      = Renamed constructor (optional) 
2046 //          l            = Parameter list 
2047 //          mode         = addmethods mode 
2052 //          Creates a C wrapper and calls the language module to wrap it. 
2053 // ----------------------------------------------------------------------------- 
2055 void cplus_emit_constructor(char *classname
, char *classtype
, char *classrename
, 
2056                             char *mname
, char *mrename
, ParmList 
*l
, int mode
) 
2062     String  fcall
,cname
,iname
,argname
; 
2065     // Construct names for the function 
2068       prefix 
= classrename
; 
2072     cname 
<< name_construct(classname
); 
2074       iname 
<< name_construct(mrename
); 
2076       iname 
<< name_construct(prefix
); 
2078     // Create a return type 
2080     type 
= new DataType
; 
2081     type
->type 
= T_USER
; 
2082     sprintf(type
->name
,"%s%s", classtype
,classname
); 
2083     type
->is_pointer 
= 1; 
2087       wrap 
<< "#define " << iname 
<< "("; 
2091         fcall 
<< type
->print_cast() << "[" << classname 
<< " " << mname
; 
2092       } else if (CPlusPlus
) { 
2093         fcall 
<< "new " << classname 
<< "("; 
2095         fcall 
<< type
->print_cast() << " calloc(1,sizeof(" 
2096               << classtype 
<< classname 
<< "))"; 
2099       // Walk down the parameter list and spit out arguments 
2104         if (ObjCClass
) fcall 
<< " " <<  p
->objc_separator
; 
2105         if ((p
->t
->type 
!= T_VOID
) || (p
->t
->is_pointer
)) { 
2106           wrap 
<< "_swigarg" << i
; 
2108           // Emit an argument in the function call if in C++ mode 
2110           if ((CPlusPlus
) || (ObjCClass
)) { 
2111             fcall 
<< "_swigarg" << i
; 
2118           if ((CPlusPlus
) && (!ObjCClass
)) 
2124       if (ObjCClass
) fcall 
<< "]"; 
2125       else if (CPlusPlus
) fcall 
<< ")"; 
2127       wrap 
<< "(" << fcall 
<< ")\n"; 
2128       fprintf(f_wrappers
,"%s",wrap
.get()); 
2131         wrap 
<< "static " << classtype 
<< classname 
<< " *" << cname 
<< "("; 
2133         // Walk down the parameter list and spit out arguments 
2137           if ((p
->t
->type 
!= T_VOID
) || (p
->t
->is_pointer
)) { 
2138             if (p
->call_type 
& CALL_REFERENCE
) { 
2141             wrap 
<< p
->t
->print_real(p
->name
); 
2142             if (p
->call_type 
& CALL_REFERENCE
) { 
2153         wrap 
<< ") " << ccode 
<< "\n"; 
2154         fprintf(f_wrappers
,"%s\n",wrap
.get()); 
2158     // If we had any C++ references, get rid of them now 
2163         //      p->t->is_reference = 0; 
2168     // We've now created a C wrapper.  We're going to add it to the interpreter 
2170     lang
->create_function(cname
, iname
, type
, l
); 
2174 // ----------------------------------------------------------------------------- 
2175 // void cplus_emit_variable_get(char *classname, char *classtype, char *classrename, 
2176 //                              char *mname, char *mrename, DataType *type, int mode) 
2178 // Writes a C wrapper to extract a data member 
2180 // Usually this function works as follows : 
2188 // double Foo_x_get(Foo *obj) { 
2192 // Optimization : 12/31/97 
2194 // Now emits a macro like this : 
2196 //      #define Foo_x_get(obj) (obj->x) 
2199 //          classname     = name of C++ class 
2200 //          classtype     = type of class (struct, class, union) 
2201 //          classrename   = Renamed class 
2202 //          mname         = Member name 
2203 //          mrename       = Renamed member 
2204 //          type          = Datatype of the member 
2205 //          mode          = Addmethods mode 
2210 //          Creates a C accessor function and calls the language module 
2211 //          to make a wrapper around it. 
2212 // ----------------------------------------------------------------------------- 
2214 void cplus_emit_variable_get(char *classname
, char *classtype
, char *classrename
, 
2215                              char *mname
, char *mrename
, DataType 
*type
, int mode
) { 
2220     String    cname
, iname
, key
; 
2225     char     *prev_wrap 
= 0; 
2231     // First generate a proper name for the get function 
2233     // Get the base class of this member 
2234     if (!mrename
) temp_mname 
= mname
; 
2235     else temp_mname 
= mrename
; 
2237     char *bc 
= cplus_base_class(temp_mname
); 
2238     if (!bc
) bc 
= classname
; 
2239     if (strlen(bc
) == 0) bc 
= classname
; 
2241     // Generate the name of the C wrapper function (is always the same, regardless 
2244     cname 
<< name_get(name_member(mname
,bc
)); 
2246     // Generate the scripting name of this function 
2248       prefix 
= classrename
; 
2253       iname 
<< name_get(name_member(mrename
,prefix
)); 
2255       iname 
<< name_get(name_member(mname
,prefix
)); 
2257     // Now check to see if we have already wrapped a variable like this. 
2260     char *temp 
= copy_string(iname
); 
2261     if ((member_hash
.add(key
,temp
)) == -1) { 
2263       prev_wrap 
= (char *) member_hash
.lookup(key
); 
2266     // Only generate code if already existing wrapper doesn't exist 
2269         // Get any sort of typemap that might exist 
2271         source 
<< "obj->" << mname
; 
2273         // Now write a function to get the value of the variable 
2275         tm 
= typemap_lookup("memberout",typemap_lang
,type
,mname
,source
,"result"); 
2277         if ((type
->type 
== T_USER
) && (!type
->is_pointer
)) { 
2280             wrap 
<< "static " << type
->print_type() << " " << cname 
<< "(" 
2281                  << classtype 
<< classname 
<< " *obj) {\n" 
2282                  << tab4 
<< type
->print_type() << " result;\n" 
2284                  << tab4 
<< "return result;\n" 
2287             wrap 
<< "#define " << cname 
<< "(_swigobj) " 
2288                  << "(&_swigobj->" << mname 
<< ")\n"; 
2293             wrap 
<< "static " << type
->print_type() << " " << cname 
<< "(" 
2294                  << classtype 
<< classname 
<< " *obj) {\n" 
2295                  << tab4 
<< type
->print_type() << " result;\n" 
2297                  << tab4 
<< "return result;\n" 
2300             wrap 
<< "#define " << cname 
<< "(_swigobj) ("; 
2301             if (!type
->is_reference
) wrap 
<< type
->print_cast(); 
2304             wrap 
<< " _swigobj->" << mname 
<< ")\n"; 
2307         fprintf(f_wrappers
,"%s",wrap
.get()); 
2310       // Wrap this function 
2314       p
->t 
=  new DataType
; 
2315       p
->t
->type 
= T_USER
; 
2316       p
->t
->is_pointer 
= 1; 
2320       sprintf(p
->t
->name
,"%s%s", classtype
,classname
); 
2323       if ((type
->type 
== T_USER
) && (!type
->is_pointer
)) { 
2325         lang
->create_function(cname
,iname
, type
, l
); 
2328         int is_ref 
= type
->is_reference
; 
2329         type
->is_reference 
= 0; 
2330         lang
->create_function(cname
,iname
, type
, l
); 
2331         type
->is_reference 
= is_ref
; 
2335       // Already wrapped this function.  Just patch it up 
2336       lang
->create_command(prev_wrap
,iname
); 
2341 // ----------------------------------------------------------------------------- 
2342 // void cplus_emit_variable_set(char *classname, char *classtype, char *mname, 
2343 //                              char *cname, char *iname, DataType *type, int mode) 
2345 // Writes a C wrapper to set a data member 
2347 // Usually this function works as follows : 
2355 // double Foo_x_set(Foo *obj, double value) { 
2356 //      return (obj->x = value); 
2359 // Need to handle special cases for char * and for user defined types. 
2363 //     Will free previous contents (if any) and allocate 
2364 //     new storage.   Could be risky, but it's a reasonably 
2365 //     natural thing to do. 
2368 //     Will assign value from a pointer. 
2369 //     Will return a pointer to current value. 
2372 // Optimization,  now defined as a C preprocessor macro 
2375 //          classname     = name of C++ class 
2376 //          classtype     = type of class (struct, class, union) 
2377 //          mname         = Member name 
2378 //          cname         = Name of the C function for this (ie. Foo_bar_get) 
2379 //          iname         = Interpreter name of ths function 
2380 //          type          = Datatype of the member 
2381 //          mode          = Addmethods mode 
2386 //          Creates a C accessor function and calls the language module 
2388 // ----------------------------------------------------------------------------- 
2390 void cplus_emit_variable_set(char *classname
, char *classtype
, char *classrename
, 
2391                              char *mname
, char *mrename
, DataType 
*type
, int mode
) { 
2399     String    cname
, iname
, key
; 
2402     char      *prev_wrap 
= 0; 
2408     // First generate a proper name for the get function 
2410     // Get the base class of this member 
2411     if (!mrename
) temp_mname 
= mname
; 
2412     else temp_mname 
= mrename
; 
2414     char *bc 
= cplus_base_class(temp_mname
); 
2415     if (!bc
) bc 
= classname
; 
2416     if (strlen(bc
) == 0) bc 
= classname
; 
2418     // Generate the name of the C wrapper function (is always the same, regardless 
2421     cname 
<< name_set(name_member(mname
,bc
)); 
2423     // Generate the scripting name of this function 
2425       prefix 
= classrename
; 
2430       iname 
<< name_set(name_member(mrename
,prefix
)); 
2432       iname 
<< name_set(name_member(mname
,prefix
)); 
2434     // Now check to see if we have already wrapped a variable like this. 
2437     char *temp 
= copy_string(iname
); 
2438     if ((member_hash
.add(key
,temp
)) == -1) { 
2440       prev_wrap 
= (char *) member_hash
.lookup(key
); 
2443     // Only generate code if already existing wrapper doesn't exist 
2448         target 
<< "obj->" << mname
; 
2450         // Lookup any typemaps that might exist 
2451         tm 
= typemap_lookup("memberin",typemap_lang
,type
,mname
,"val",target
); 
2453         // First write a function to set the variable 
2456           if ((type
->type 
== T_USER
) && (!type
->is_pointer
)) { 
2460           wrap 
<< "static " << type
->print_type() << " " << cname 
<< "(" 
2461                << classtype 
<< classname 
<< " *obj, " << type
->print_real("val") << ") {\n"; 
2466           // Return the member 
2467           if (is_user
) type
->is_pointer
++; 
2468           wrap 
<< tab4 
<< "return " << type
->print_cast() << " val;\n"; 
2469           if (is_user
) type
->is_pointer
--; 
2473           if ((type
->type 
!= T_VOID
) || (type
->is_pointer
)){ 
2474             if (!type
->is_pointer
) { 
2476               wrap 
<< "#define " << cname 
<< "(_swigobj,_swigval) ("; 
2477               // Have a real value here (ie.  not a pointer). 
2478               // If it's a user defined type, we'll do something special. 
2479               // Otherwise, just assign it. 
2481               if (type
->type 
!= T_USER
) { 
2482                 wrap 
<< "_swigobj->" << mname 
<< " = _swigval"; 
2484                 wrap 
<< "_swigobj->" << mname 
<< " = *(_swigval)"; 
2486               wrap 
<< ",_swigval)\n"; 
2488               // Is a pointer type here.  If string, we do something 
2489               // special.  Otherwise. No problem. 
2490               if ((type
->type 
== T_CHAR
) && (type
->is_pointer 
== 1)) { 
2492                 wrap 
<< "static " << type
->print_type() << " " << cname 
<< "(" 
2493                      << classtype 
<< classname 
<< " *obj, " << type
->print_real("val") << ") {\n"; 
2494                 temp 
<< "obj->" << mname
; 
2496                   wrap 
<< tab4 
<< "if (" << temp 
<< ") delete [] " << temp 
<< ";\n" 
2497                        << tab4 
<< temp 
<< " = new char[strlen(val)+1];\n" 
2498                        << tab4 
<< "strcpy((char *)" << temp 
<< ",val);\n"; 
2500                   wrap 
<< tab4 
<< "if (obj->" << mname 
<< ") free(obj->" << mname 
<< ");\n" 
2501                        << tab4 
<< "obj->" << mname 
<< " = (char *) malloc(strlen(val)+1);\n" 
2502                        << tab4 
<< "strcpy((char *)obj->" << mname 
<< ",val);\n"; 
2504                 wrap 
<< tab4 
<< "return (char *) val;\n"; 
2507                 // A normal pointer type of some sort 
2508                 wrap 
<< "#define " << cname 
<< "(_swigobj,_swigval) ("; 
2509                 if (type
->is_reference
) { 
2510                   wrap 
<< "_swigobj->" << mname 
<< " = *_swigval, _swigval)\n"; 
2512                   wrap 
<< "_swigobj->" << mname 
<< " = _swigval,_swigval)\n"; 
2519       fprintf(f_wrappers
,"%s",wrap
.get()); 
2524       p
->t 
= new DataType(type
); 
2525       p
->t
->is_reference 
= 0; 
2528       if ((type
->type 
== T_USER
) && (!type
->is_pointer
)) p
->t
->is_pointer
++; 
2535       p
->t 
=  new DataType
; 
2536       p
->t
->type 
= T_USER
; 
2538       p
->t
->is_pointer 
= 1; 
2540       sprintf(p
->t
->name
,"%s%s", classtype
,classname
); 
2544       if ((type
->type 
== T_USER
) && (!type
->is_pointer
)) { 
2546         lang
->create_function(cname
,iname
, type
, l
); 
2549         int is_ref 
= type
->is_reference
; 
2550         type
->is_reference 
= 0; 
2551         lang
->create_function(cname
,iname
, type
, l
); 
2552         type
->is_reference 
= is_ref
; 
2556       lang
->create_command(prev_wrap
,iname
); 
2560 // ----------------------------------------------------------------------------- 
2561 // void cplus_support_doc(String &f) 
2563 // This function adds a supporting documentation entry to the 
2564 // end of a class.   This should only be used if there is an 
2565 // alternative interface available or if additional information is needed. 
2567 // doc_entry should be set to the class entry before calling this. Otherwise, 
2568 // who knows where this text is going to end up! 
2570 // Inputs : f  = String with additional text 
2575 //          Adds a text block to the current documentation entry. 
2577 // ----------------------------------------------------------------------------- 
2579 void cplus_support_doc(String 
&f
) { 
2583     de 
= new DocText(f
.get(),doc_entry
); 
2588 // ----------------------------------------------------------------------------- 
2589 // void cplus_register_type(char *typename) 
2591 // Registers a datatype name to be associated with the current class.  This 
2592 // typename is placed into a local hash table for later use.  For example : 
2596 //            enum ENUM { ... }; 
2597 //            typedef double Real; 
2598 //            void bar(ENUM a, Real b); 
2601 // Then we need to access bar using fully qualified type names such as 
2603 //     void wrap_bar(foo::ENUM a, foo::Real b) { 
2607 // Inputs : name of the datatype. 
2611 // Side Effects : Adds datatype to localtypes. 
2612 // ----------------------------------------------------------------------------- 
2614 void cplus_register_type(char *tname
) { 
2616     add_local_type(tname
, current_class
->classname
); 
2619 // ----------------------------------------------------------------------------- 
2620 // void cplus_register_scope(Hash *h) 
2622 // Saves the scope associated with a particular class.  It will be needed 
2623 // later if anything inherits from us. 
2625 // Inputs : Hash table h containing the scope 
2629 // Side Effects : Saves h with current class 
2630 // ----------------------------------------------------------------------------- 
2632 void cplus_register_scope(Hash 
*h
) { 
2633   if (current_class
) { 
2634     current_class
->scope 
= h
; 
2638 // ----------------------------------------------------------------------------- 
2639 // void cplus_inherit_scope(int count, char **baseclass) 
2641 // Given a list of base classes, this function extracts their former scopes 
2642 // and merges them with the current scope.  This is needed to properly handle 
2645 // Inputs : baseclass = NULL terminated array of base-class names 
2649 // Side Effects : Updates current scope with new symbols. 
2651 // Copies any special symbols if needed. 
2652 // ----------------------------------------------------------------------------- 
2654 void cplus_inherit_scope(int count
, char **baseclass
) { 
2660   if (count 
&& current_class
) { 
2661     for (i 
= 0; i 
< count
; i
++) { 
2662        bc 
= CPP_class::search(baseclass
[i
]); 
2665           DataType::merge_scope(bc
->scope
); 
2668           // Copy local symbol table 
2669           key 
= bc
->local
->firstkey(); 
2671             val 
= (char *) bc
->local
->lookup(key
); 
2673             //      str.replace(bc->classname,current_class->classname); 
2674             localtypes
->add(key
,copy_string(str
)); 
2675             key 
= bc
->local
->nextkey();