]>
git.saurik.com Git - wxWidgets.git/blob - wxPython/wxSWIG/SWIG/lang.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 *******************************************************************************/
20 // -----------------------------------------------------------------------
25 // This file contains some default methods for the SWIG language class.
26 // Default C++ handling is implemented here as well as a few utility functions.
28 // -----------------------------------------------------------------------
31 // -----------------------------------------------------------------
32 // void Language::set_init(char *iname)
34 // Called to make an initialization function by %init (obsolete)
35 // -----------------------------------------------------------------
37 void Language::set_init(char *iname
) {
41 // -----------------------------------------------------------------
42 // void Language::create_command(char *cname, char *iname
44 // Method for adding a previous wrapped C function.
45 // -----------------------------------------------------------------
47 void Language::create_command(char *, char *) {
48 fprintf(stderr
,"SWIG Warning. No command creation procedure defined.\n");
49 fprintf(stderr
,"C++ inheritance may not work correctly.\n");
52 // -----------------------------------------------------------------
53 // void Language::add_native(char *targetlang, char *funcname)
55 // Method for adding a native function
56 // -----------------------------------------------------------------
59 Language::add_native(char *, char *funcname
) {
61 fprintf(stderr
,"%s : Line %d. Adding native function %s not supported (ignored).\n", input_file
, line_number
, funcname
);
65 static char *ClassName
= 0; // This is the real name of the current class
66 static char *ClassRename
= 0; // This is non-NULL if the class has been renamed
67 static char *ClassType
= 0; // Type of class (ie. union, struct, class)
69 // ---------------------------------------------------------------------------------
70 // void Language::cpp_open_class(char *classname, char *classrename, char *ctype, int strip)
72 // Open a new C++ class.
75 // classname = Real name of the C++ class
76 // classrename = New name of the class (if %name was used)
77 // ctype = Class type (struct, class, union, etc...)
78 // strip = Flag indicating whether we should strip the class type off
80 // This function is in charge of creating a new class. The SWIG parser has
81 // already processed the entire class definition prior to calling this
82 // function (which should simplify things considerably).
84 // ---------------------------------------------------------------------------------
86 void Language::cpp_open_class(char *classname
, char *classrename
, char *ctype
, int strip
) {
88 // Copy the class name
90 if (ClassName
) delete ClassName
;
91 ClassName
= copy_string(classname
);
93 // Copy the class renaming
95 if (ClassRename
) delete ClassRename
;
97 ClassRename
= copy_string(classrename
);
99 ClassRename
= 0; // No renaming
102 // Make the class type
104 if (ClassType
) delete ClassType
;
105 ClassType
= new char[strlen(ctype
)+2];
106 if (strip
) ClassType
[0] = 0;
107 else sprintf(ClassType
,"%s ",ctype
);
110 doc_entry
->usage
= "";
111 doc_entry
->name
= copy_string(classname
);
112 doc_entry
->usage
<< "class ";
113 if (ClassRename
) doc_entry
->usage
<< ClassRename
;
114 else doc_entry
->usage
<< ClassName
;
115 doc_entry
->cinfo
<< "created from " << ctype
120 // ---------------------------------------------------------------------------------
121 // void Language::cpp_close_class()
123 // Close the current class
124 // ---------------------------------------------------------------------------------
126 void Language::cpp_close_class() {
129 // Doesn't really do anything
132 // ---------------------------------------------------------------------------------
133 // void Language::cpp_member_func(char *name, char *iname, DataType *t, ParmList *l)
135 // Method for adding C++ member function
138 // name - name of the member function
139 // iname - renaming (if given)
140 // t - Return datatype
141 // l - Parameter list
143 // By default, we're going to create a function of the form :
145 // Foo_bar(this,args)
147 // Where Foo is the classname, bar is the member name and the this pointer is
148 // explicitly attached to the beginning.
150 // The renaming only applies to the member function part, not the full classname.
152 // ---------------------------------------------------------------------------------
154 void Language::cpp_member_func(char *name
, char *iname
, DataType
*t
, ParmList
*l
) {
155 char cname
[256]; // Name of the C function
159 // Generate the C wrapper function name and interpreter name of this function
161 // Set the classname prefix
163 prefix
= ClassRename
;
168 // Generate the C wrapper name for this method
171 char *bc
= cplus_base_class(name
); // Get base class name of this method
173 strcpy(cname
, name_member(name
,bc
));
175 strcpy(cname
, name_member(name
,ClassName
));
177 strcpy(cname
, name_member(name
,ClassName
));
180 // Create the actual function name
183 strcpy(new_name
, name_member(iname
, prefix
));
185 strcpy(new_name
, name_member(name
,prefix
));
188 // Now do a symbol table lookup on it :
190 if (add_symbol(new_name
, 0,0)) {
191 fprintf(stderr
,"%s : Line %d. Function %s (member %s) multiply defined (2nd definition ignored).\n",
192 input_file
, line_number
, cname
, name
);
196 // Now produce the resulting wrapper function
198 doc_entry
->cinfo
<< "Member : ";
200 cplus_emit_member_func(ClassName
, ClassType
, ClassRename
, name
, iname
, t
, l
, AddMethods
);
203 // ---------------------------------------------------------------------------------
204 // void Language::cpp_constructor(char *name, char *iname, ParmList *l)
206 // Method for adding C++ member constructor
209 // name - Name of the constructor (usually same as classname)
210 // iname - Renamed version
212 // ---------------------------------------------------------------------------------
214 void Language::cpp_constructor(char *name
, char *iname
, ParmList
*l
) {
216 char *prefix
, *cname
;
218 if ((strcmp(name
,ClassName
)) && (!ObjCClass
)) {
219 fprintf(stderr
,"%s : Line %d. Function %s must have a return type.\n",
220 input_file
, line_number
, name
);
227 prefix
= ClassRename
;
232 cname
= name_construct(iname
);
234 cname
= name_construct(prefix
);
236 // Add this function to the SWIG symbol table
238 if (add_symbol(cname
, 0,0)) {
239 fprintf(stderr
,"%s : Line %d. Constructor %s multiply defined (2nd definition ignored).\n",
240 input_file
, line_number
, cname
);
244 // Attach a note to the cinfo field.
247 doc_entry
->cinfo
<< "Constructor: ";
249 // Call our default method
251 cplus_emit_constructor(ClassName
, ClassType
, ClassRename
, name
, iname
, l
, AddMethods
);
255 // ---------------------------------------------------------------------------------
256 // void Language::cpp_destructor(char *name, char *iname)
258 // Method for adding C++ member destructor
261 // name - Name of the destructor (classname)
262 // iname - Renamed destructor
264 // ---------------------------------------------------------------------------------
266 void Language::cpp_destructor(char *name
, char *iname
) {
271 cname
= name_destroy(ClassRename
);
273 cname
= name_destroy(ClassName
);
275 // Add this function to the SWIG symbol table
277 if (add_symbol(cname
, 0,0)) {
278 fprintf(stderr
,"%s : Line %d. Destructor %s multiply defined (2nd definition ignored).\n",
279 input_file
, line_number
, cname
);
284 // Attach a note to the description
287 doc_entry
->cinfo
<< "Destructor: ";
289 // Call our default method
291 cplus_emit_destructor(ClassName
, ClassType
, ClassRename
, name
, iname
, AddMethods
);
295 // ---------------------------------------------------------------------------------
296 // void Language::cleanup()
298 // Perform any necessary cleanup after reaching end of interface file
299 // ---------------------------------------------------------------------------------
301 void Language::cpp_cleanup() {
303 // This doesn't do anything (well, not be default)
307 // ---------------------------------------------------------------------------------
308 // void Language::cpp_inherit(char **baseclass, int mode)
310 // Inherit attributes from given baseclass.
313 // baseclass = NULL terminate list of baseclasses
315 // ---------------------------------------------------------------------------------
317 void Language::cpp_inherit(char **baseclass
, int mode
) {
319 extern void cplus_inherit_members(char *, int);
322 if (!baseclass
) return;
323 while (baseclass
[i
]) {
324 cplus_inherit_members(baseclass
[i
],mode
);
329 // ---------------------------------------------------------------------------------
330 // void Language::cpp_variable(char *name, char *iname, DataType *t)
332 // Wrap a C++ data member
335 // name = Name of the C++ member
336 // iname = Name as used in the interpreter
339 // This creates a pair of functions to set/get the variable of a member.
340 // ---------------------------------------------------------------------------------
342 void Language::cpp_variable(char *name
, char *iname
, DataType
*t
) {
343 char *prefix
, *cname
;
345 // Set the class prefix
348 prefix
= ClassRename
;
354 cname
= name_get(name_member(iname
,prefix
));
356 cname
= name_get(name_member(name
,prefix
));
358 // Check the symbol table
360 if (add_symbol(cname
,(DataType
*) 0,(char *) 0)) {
361 fprintf(stderr
,"%s : Line %d. Variable %s multiply defined (2nd definition ignored).\n", input_file
, line_number
, cname
);
365 // Attach a c descriptor
368 doc_entry
->cinfo
<< "Member data: ";
370 // Create a function to set the value of the variable
372 if (!(Status
& STAT_READONLY
)) {
373 cplus_emit_variable_set(ClassName
, ClassType
, ClassRename
, name
, iname
, t
, AddMethods
);
374 // Add a new line to the documentation entry
375 if (doc_entry
) doc_entry
->usage
<< "\n";
378 // Create a function to get the value of a variable
380 cplus_emit_variable_get(ClassName
,ClassType
, ClassRename
, name
, iname
, t
, AddMethods
);
384 // ---------------------------------------------------------------------------------
385 // void Language::cpp_static_func(char *name, char *iname, DataType *t, ParmList *l)
387 // Wrap a static C++ function
390 // name = Real name of the function
391 // iname = New name in interpreter
392 // t = Return datatype
394 // ---------------------------------------------------------------------------------
396 void Language::cpp_static_func(char *name
, char *iname
, DataType
*t
, ParmList
*l
) {
402 // Set the classname prefix
405 prefix
= ClassRename
;
409 // Set the member function name
416 cname
= name_member(mname
,prefix
);
418 // Now do a symbol table lookup on it :
420 if (add_symbol(cname
, 0,0)) {
422 fprintf(stderr
,"%s : Line %d. class function %s multiply defined (2nd definition ignored).\n",
423 input_file
, line_number
, cname
);
425 fprintf(stderr
,"%s : Line %d. static function %s multiply defined (2nd definition ignored).\n",
426 input_file
, line_number
, cname
);
432 doc_entry
->cinfo
<< "Class method : ";
434 doc_entry
->cinfo
<< "Static member : ";
437 cplus_emit_static_func(ClassName
,ClassType
, ClassRename
, name
, iname
, t
, l
, AddMethods
);
441 // ---------------------------------------------------------------------------------
442 // void Language::cpp_declare_const(char *name, char *iname, DataType *t, char *value)
444 // Create a C++ constant
447 // name = Real name of the constant
450 // value = value as a string
452 // ---------------------------------------------------------------------------------
454 void Language::cpp_declare_const(char *name
, char *iname
, DataType
*type
, char *value
)
462 // Set the classname prefix
465 prefix
= ClassRename
;
470 // Set the constant name
473 cname
= name_member(iname
,prefix
);
475 cname
= name_member(name
,prefix
);
477 // Now do a symbol table lookup on it :
479 if (add_symbol(cname
, 0,0)) {
480 fprintf(stderr
,"%s : Line %d. Constant %s (member %s) multiply defined (2nd definition ignored).\n",
481 input_file
, line_number
, cname
, name
);
485 // Form correct C++ name
487 sprintf(mname
,"%s::%s",ClassName
,name
);
489 // Declare a constant
491 new_value
= new char[strlen(ClassName
)+strlen(name
)+3];
492 sprintf(new_value
,"%s::%s",ClassName
,name
);
497 lang
->declare_const(cname
,cname
,type
, new_value
);
504 // ---------------------------------------------------------------------------------
505 // void Language::cpp_static_var(char *name, char *iname, DataType *t)
507 // Wrap a static C++ variable
510 // name = name of the variable
511 // iname = interpreter name
514 // ---------------------------------------------------------------------------------
516 void Language::cpp_static_var(char *name
, char *iname
, DataType
*t
) {
522 // Set the classname prefix
525 prefix
= ClassRename
;
530 // Create the variable name
533 cname
= name_member(iname
,prefix
);
535 cname
= name_member(name
,prefix
);
537 // Now do a symbol table lookup on it :
539 if (add_symbol(cname
, 0,0)) {
540 fprintf(stderr
,"%s : Line %d. Variable %s (member %s) multiply defined (2nd definition ignored).\n",
541 input_file
, line_number
, cname
, name
);
545 // Form correct C++ name
547 sprintf(mname
,"%s::%s",ClassName
,name
);
550 doc_entry
->cinfo
<< "Static member : ";
552 // Link with this variable
554 lang
->link_variable(mname
,cname
,t
);
557 // ---------------------------------------------------------------------------------
558 // void Language::cpp_class_decl(char *classtype, char *classrename, char *classname)
560 // A forward class declaration
561 // ---------------------------------------------------------------------------------
563 void Language::cpp_class_decl(char *, char *, char *) {
565 // Does nothing by default
569 // -----------------------------------------------------------------------------
570 // void Language::cpp_pragma(Pragma *plist)
572 // Handler C++ pragmas
573 // -----------------------------------------------------------------------------
575 void Language::cpp_pragma(Pragma
*) {
576 // Does nothing by default
579 // ---------------------------------------------------------------------------------
580 // void Language::add_typedef(DataType *t, char *name)
582 // Process a typedef declaration.
583 // ---------------------------------------------------------------------------------
585 void Language::add_typedef(DataType
*, char *) {
590 // ---------------------------------------------------------------------------------
591 // void Language::pragma(char *target, char *var, char *value)
593 // A pragma declaration
594 // ---------------------------------------------------------------------------------
596 void Language::pragma(char *, char *, char *) {
598 // Does nothing by default
602 // ---------------------------------------------------------------------------------
603 // void Language::import(char *filename)
605 // An import directive
606 // ---------------------------------------------------------------------------------
608 void Language::import(char *) {
610 // Does nothing by default