| 1 | /******************************************************************************* |
| 2 | * Simplified Wrapper and Interface Generator (SWIG) |
| 3 | * |
| 4 | * Author : David Beazley |
| 5 | * |
| 6 | * Department of Computer Science |
| 7 | * University of Chicago |
| 8 | * 1100 E 58th Street |
| 9 | * Chicago, IL 60637 |
| 10 | * beazley@cs.uchicago.edu |
| 11 | * |
| 12 | * Please read the file LICENSE for the copyright and terms by which SWIG |
| 13 | * can be used and distributed. |
| 14 | *******************************************************************************/ |
| 15 | |
| 16 | #include "internal.h" |
| 17 | #include <ctype.h> |
| 18 | |
| 19 | |
| 20 | // ----------------------------------------------------------------------- |
| 21 | // $Header$ |
| 22 | // |
| 23 | // lang.cxx |
| 24 | // |
| 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. |
| 27 | // |
| 28 | // ----------------------------------------------------------------------- |
| 29 | |
| 30 | |
| 31 | // ----------------------------------------------------------------- |
| 32 | // void Language::set_init(char *iname) |
| 33 | // |
| 34 | // Called to make an initialization function by %init (obsolete) |
| 35 | // ----------------------------------------------------------------- |
| 36 | |
| 37 | void Language::set_init(char *iname) { |
| 38 | set_module(iname,0); |
| 39 | } |
| 40 | |
| 41 | // ----------------------------------------------------------------- |
| 42 | // void Language::create_command(char *cname, char *iname |
| 43 | // |
| 44 | // Method for adding a previous wrapped C function. |
| 45 | // ----------------------------------------------------------------- |
| 46 | |
| 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"); |
| 50 | } |
| 51 | |
| 52 | // ----------------------------------------------------------------- |
| 53 | // void Language::add_native(char *targetlang, char *funcname) |
| 54 | // |
| 55 | // Method for adding a native function |
| 56 | // ----------------------------------------------------------------- |
| 57 | |
| 58 | void |
| 59 | Language::add_native(char *, char *funcname) { |
| 60 | |
| 61 | fprintf(stderr,"%s : Line %d. Adding native function %s not supported (ignored).\n", input_file, line_number, funcname); |
| 62 | |
| 63 | } |
| 64 | |
| 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) |
| 68 | |
| 69 | // --------------------------------------------------------------------------------- |
| 70 | // void Language::cpp_open_class(char *classname, char *classrename, char *ctype, int strip) |
| 71 | // |
| 72 | // Open a new C++ class. |
| 73 | // |
| 74 | // INPUTS: |
| 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 |
| 79 | // |
| 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). |
| 83 | // |
| 84 | // --------------------------------------------------------------------------------- |
| 85 | |
| 86 | void Language::cpp_open_class(char *classname, char *classrename, char *ctype, int strip) { |
| 87 | |
| 88 | // Copy the class name |
| 89 | |
| 90 | if (ClassName) delete ClassName; |
| 91 | ClassName = copy_string(classname); |
| 92 | |
| 93 | // Copy the class renaming |
| 94 | |
| 95 | if (ClassRename) delete ClassRename; |
| 96 | if (classrename) { |
| 97 | ClassRename = copy_string(classrename); |
| 98 | } else { |
| 99 | ClassRename = 0; // No renaming |
| 100 | } |
| 101 | |
| 102 | // Make the class type |
| 103 | |
| 104 | if (ClassType) delete ClassType; |
| 105 | ClassType = new char[strlen(ctype)+2]; |
| 106 | if (strip) ClassType[0] = 0; |
| 107 | else sprintf(ClassType,"%s ",ctype); |
| 108 | |
| 109 | if (doc_entry) { |
| 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 |
| 116 | << " " << classname; |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | // --------------------------------------------------------------------------------- |
| 121 | // void Language::cpp_close_class() |
| 122 | // |
| 123 | // Close the current class |
| 124 | // --------------------------------------------------------------------------------- |
| 125 | |
| 126 | void Language::cpp_close_class() { |
| 127 | |
| 128 | |
| 129 | // Doesn't really do anything |
| 130 | } |
| 131 | |
| 132 | // --------------------------------------------------------------------------------- |
| 133 | // void Language::cpp_member_func(char *name, char *iname, DataType *t, ParmList *l) |
| 134 | // |
| 135 | // Method for adding C++ member function |
| 136 | // |
| 137 | // INPUTS: |
| 138 | // name - name of the member function |
| 139 | // iname - renaming (if given) |
| 140 | // t - Return datatype |
| 141 | // l - Parameter list |
| 142 | // |
| 143 | // By default, we're going to create a function of the form : |
| 144 | // |
| 145 | // Foo_bar(this,args) |
| 146 | // |
| 147 | // Where Foo is the classname, bar is the member name and the this pointer is |
| 148 | // explicitly attached to the beginning. |
| 149 | // |
| 150 | // The renaming only applies to the member function part, not the full classname. |
| 151 | // |
| 152 | // --------------------------------------------------------------------------------- |
| 153 | |
| 154 | void Language::cpp_member_func(char *name, char *iname, DataType *t, ParmList *l) { |
| 155 | char cname[256]; // Name of the C function |
| 156 | char new_name[256]; |
| 157 | char *prefix; |
| 158 | |
| 159 | // Generate the C wrapper function name and interpreter name of this function |
| 160 | |
| 161 | // Set the classname prefix |
| 162 | if (ClassRename) { |
| 163 | prefix = ClassRename; |
| 164 | } else { |
| 165 | prefix = ClassName; |
| 166 | } |
| 167 | |
| 168 | // Generate the C wrapper name for this method |
| 169 | |
| 170 | if (AddMethods) { |
| 171 | char *bc = cplus_base_class(name); // Get base class name of this method |
| 172 | if (bc) |
| 173 | strcpy(cname, name_member(name,bc)); |
| 174 | else |
| 175 | strcpy(cname, name_member(name,ClassName)); |
| 176 | } else { |
| 177 | strcpy(cname, name_member(name,ClassName)); |
| 178 | } |
| 179 | |
| 180 | // Create the actual function name |
| 181 | |
| 182 | if (iname) { |
| 183 | strcpy(new_name, name_member(iname, prefix)); |
| 184 | } else { |
| 185 | strcpy(new_name, name_member(name,prefix)); |
| 186 | } |
| 187 | |
| 188 | // Now do a symbol table lookup on it : |
| 189 | |
| 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); |
| 193 | return; |
| 194 | } |
| 195 | |
| 196 | // Now produce the resulting wrapper function |
| 197 | if (doc_entry) { |
| 198 | doc_entry->cinfo << "Member : "; |
| 199 | } |
| 200 | cplus_emit_member_func(ClassName, ClassType, ClassRename, name, iname, t, l, AddMethods); |
| 201 | } |
| 202 | |
| 203 | // --------------------------------------------------------------------------------- |
| 204 | // void Language::cpp_constructor(char *name, char *iname, ParmList *l) |
| 205 | // |
| 206 | // Method for adding C++ member constructor |
| 207 | // |
| 208 | // INPUTS: |
| 209 | // name - Name of the constructor (usually same as classname) |
| 210 | // iname - Renamed version |
| 211 | // l - parameters |
| 212 | // --------------------------------------------------------------------------------- |
| 213 | |
| 214 | void Language::cpp_constructor(char *name, char *iname, ParmList *l) { |
| 215 | |
| 216 | char *prefix, *cname; |
| 217 | |
| 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); |
| 221 | return; |
| 222 | } |
| 223 | |
| 224 | // Set the prefix |
| 225 | |
| 226 | if (ClassRename) |
| 227 | prefix = ClassRename; |
| 228 | else |
| 229 | prefix = ClassName; |
| 230 | |
| 231 | if (iname) |
| 232 | cname = name_construct(iname); |
| 233 | else |
| 234 | cname = name_construct(prefix); |
| 235 | |
| 236 | // Add this function to the SWIG symbol table |
| 237 | |
| 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); |
| 241 | return; |
| 242 | } |
| 243 | |
| 244 | // Attach a note to the cinfo field. |
| 245 | |
| 246 | if (doc_entry) |
| 247 | doc_entry->cinfo << "Constructor: "; |
| 248 | |
| 249 | // Call our default method |
| 250 | |
| 251 | cplus_emit_constructor(ClassName, ClassType, ClassRename, name, iname, l, AddMethods); |
| 252 | |
| 253 | } |
| 254 | |
| 255 | // --------------------------------------------------------------------------------- |
| 256 | // void Language::cpp_destructor(char *name, char *iname) |
| 257 | // |
| 258 | // Method for adding C++ member destructor |
| 259 | // |
| 260 | // INPUT: |
| 261 | // name - Name of the destructor (classname) |
| 262 | // iname - Renamed destructor |
| 263 | // |
| 264 | // --------------------------------------------------------------------------------- |
| 265 | |
| 266 | void Language::cpp_destructor(char *name, char *iname) { |
| 267 | |
| 268 | char *cname; |
| 269 | |
| 270 | if (ClassRename) |
| 271 | cname = name_destroy(ClassRename); |
| 272 | else |
| 273 | cname = name_destroy(ClassName); |
| 274 | |
| 275 | // Add this function to the SWIG symbol table |
| 276 | |
| 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); |
| 280 | return; |
| 281 | } |
| 282 | |
| 283 | |
| 284 | // Attach a note to the description |
| 285 | |
| 286 | if (doc_entry) |
| 287 | doc_entry->cinfo << "Destructor: "; |
| 288 | |
| 289 | // Call our default method |
| 290 | |
| 291 | cplus_emit_destructor(ClassName, ClassType, ClassRename, name, iname, AddMethods); |
| 292 | |
| 293 | } |
| 294 | |
| 295 | // --------------------------------------------------------------------------------- |
| 296 | // void Language::cleanup() |
| 297 | // |
| 298 | // Perform any necessary cleanup after reaching end of interface file |
| 299 | // --------------------------------------------------------------------------------- |
| 300 | |
| 301 | void Language::cpp_cleanup() { |
| 302 | |
| 303 | // This doesn't do anything (well, not be default) |
| 304 | |
| 305 | } |
| 306 | |
| 307 | // --------------------------------------------------------------------------------- |
| 308 | // void Language::cpp_inherit(char **baseclass, int mode) |
| 309 | // |
| 310 | // Inherit attributes from given baseclass. |
| 311 | // |
| 312 | // INPUT: |
| 313 | // baseclass = NULL terminate list of baseclasses |
| 314 | // |
| 315 | // --------------------------------------------------------------------------------- |
| 316 | |
| 317 | void Language::cpp_inherit(char **baseclass, int mode) { |
| 318 | |
| 319 | extern void cplus_inherit_members(char *, int); |
| 320 | int i = 0; |
| 321 | |
| 322 | if (!baseclass) return; |
| 323 | while (baseclass[i]) { |
| 324 | cplus_inherit_members(baseclass[i],mode); |
| 325 | i++; |
| 326 | } |
| 327 | } |
| 328 | |
| 329 | // --------------------------------------------------------------------------------- |
| 330 | // void Language::cpp_variable(char *name, char *iname, DataType *t) |
| 331 | // |
| 332 | // Wrap a C++ data member |
| 333 | // |
| 334 | // INPUTS : |
| 335 | // name = Name of the C++ member |
| 336 | // iname = Name as used in the interpreter |
| 337 | // t = Datatype |
| 338 | // |
| 339 | // This creates a pair of functions to set/get the variable of a member. |
| 340 | // --------------------------------------------------------------------------------- |
| 341 | |
| 342 | void Language::cpp_variable(char *name, char *iname, DataType *t) { |
| 343 | char *prefix, *cname; |
| 344 | |
| 345 | // Set the class prefix |
| 346 | |
| 347 | if (ClassRename) { |
| 348 | prefix = ClassRename; |
| 349 | } else { |
| 350 | prefix = ClassName; |
| 351 | } |
| 352 | |
| 353 | if (iname) |
| 354 | cname = name_get(name_member(iname,prefix)); |
| 355 | else |
| 356 | cname = name_get(name_member(name,prefix)); |
| 357 | |
| 358 | // Check the symbol table |
| 359 | |
| 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); |
| 362 | return; |
| 363 | } |
| 364 | |
| 365 | // Attach a c descriptor |
| 366 | |
| 367 | if (doc_entry) |
| 368 | doc_entry->cinfo << "Member data: "; |
| 369 | |
| 370 | // Create a function to set the value of the variable |
| 371 | |
| 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"; |
| 376 | } |
| 377 | |
| 378 | // Create a function to get the value of a variable |
| 379 | |
| 380 | cplus_emit_variable_get(ClassName,ClassType, ClassRename, name, iname, t, AddMethods); |
| 381 | |
| 382 | } |
| 383 | |
| 384 | // --------------------------------------------------------------------------------- |
| 385 | // void Language::cpp_static_func(char *name, char *iname, DataType *t, ParmList *l) |
| 386 | // |
| 387 | // Wrap a static C++ function |
| 388 | // |
| 389 | // INPUTS: |
| 390 | // name = Real name of the function |
| 391 | // iname = New name in interpreter |
| 392 | // t = Return datatype |
| 393 | // l = Parameters |
| 394 | // --------------------------------------------------------------------------------- |
| 395 | |
| 396 | void Language::cpp_static_func(char *name, char *iname, DataType *t, ParmList *l) { |
| 397 | |
| 398 | char *prefix; |
| 399 | char *mname; |
| 400 | char *cname; |
| 401 | |
| 402 | // Set the classname prefix |
| 403 | |
| 404 | if (ClassRename) |
| 405 | prefix = ClassRename; |
| 406 | else |
| 407 | prefix = ClassName; |
| 408 | |
| 409 | // Set the member function name |
| 410 | |
| 411 | if (iname) |
| 412 | mname = iname; |
| 413 | else |
| 414 | mname = name; |
| 415 | |
| 416 | cname = name_member(mname,prefix); |
| 417 | |
| 418 | // Now do a symbol table lookup on it : |
| 419 | |
| 420 | if (add_symbol(cname, 0,0)) { |
| 421 | if (ObjCClass) |
| 422 | fprintf(stderr,"%s : Line %d. class function %s multiply defined (2nd definition ignored).\n", |
| 423 | input_file, line_number, cname); |
| 424 | else |
| 425 | fprintf(stderr,"%s : Line %d. static function %s multiply defined (2nd definition ignored).\n", |
| 426 | input_file, line_number, cname); |
| 427 | return; |
| 428 | } |
| 429 | |
| 430 | if (doc_entry) { |
| 431 | if (ObjCClass) |
| 432 | doc_entry->cinfo << "Class method : "; |
| 433 | else |
| 434 | doc_entry->cinfo << "Static member : "; |
| 435 | } |
| 436 | |
| 437 | cplus_emit_static_func(ClassName,ClassType, ClassRename, name, iname, t, l, AddMethods); |
| 438 | |
| 439 | } |
| 440 | |
| 441 | // --------------------------------------------------------------------------------- |
| 442 | // void Language::cpp_declare_const(char *name, char *iname, DataType *t, char *value) |
| 443 | // |
| 444 | // Create a C++ constant |
| 445 | // |
| 446 | // INPUTS : |
| 447 | // name = Real name of the constant |
| 448 | // iname = new name |
| 449 | // t = Datatype |
| 450 | // value = value as a string |
| 451 | // |
| 452 | // --------------------------------------------------------------------------------- |
| 453 | |
| 454 | void Language::cpp_declare_const(char *name, char *iname, DataType *type, char *value) |
| 455 | { |
| 456 | |
| 457 | char *cname; |
| 458 | char mname[256]; |
| 459 | char *new_value; |
| 460 | char *prefix; |
| 461 | |
| 462 | // Set the classname prefix |
| 463 | |
| 464 | if (ClassRename) { |
| 465 | prefix = ClassRename; |
| 466 | } else { |
| 467 | prefix = ClassName; |
| 468 | } |
| 469 | |
| 470 | // Set the constant name |
| 471 | |
| 472 | if (iname) |
| 473 | cname = name_member(iname,prefix); |
| 474 | else |
| 475 | cname = name_member(name,prefix); |
| 476 | |
| 477 | // Now do a symbol table lookup on it : |
| 478 | |
| 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); |
| 482 | return; |
| 483 | } |
| 484 | |
| 485 | // Form correct C++ name |
| 486 | |
| 487 | sprintf(mname,"%s::%s",ClassName,name); |
| 488 | |
| 489 | // Declare a constant |
| 490 | if (!value) { |
| 491 | new_value = new char[strlen(ClassName)+strlen(name)+3]; |
| 492 | sprintf(new_value,"%s::%s",ClassName,name); |
| 493 | } else { |
| 494 | new_value = value; |
| 495 | } |
| 496 | |
| 497 | lang->declare_const(cname,cname,type, new_value); |
| 498 | |
| 499 | if (!value) { |
| 500 | delete new_value; |
| 501 | } |
| 502 | } |
| 503 | |
| 504 | // --------------------------------------------------------------------------------- |
| 505 | // void Language::cpp_static_var(char *name, char *iname, DataType *t) |
| 506 | // |
| 507 | // Wrap a static C++ variable |
| 508 | // |
| 509 | // INPUT : |
| 510 | // name = name of the variable |
| 511 | // iname = interpreter name |
| 512 | // t = Datatype |
| 513 | // |
| 514 | // --------------------------------------------------------------------------------- |
| 515 | |
| 516 | void Language::cpp_static_var(char *name, char *iname, DataType *t) { |
| 517 | |
| 518 | char *cname; |
| 519 | char mname[256]; |
| 520 | char *prefix; |
| 521 | |
| 522 | // Set the classname prefix |
| 523 | |
| 524 | if (ClassRename) { |
| 525 | prefix = ClassRename; |
| 526 | } else { |
| 527 | prefix = ClassName; |
| 528 | } |
| 529 | |
| 530 | // Create the variable name |
| 531 | |
| 532 | if (iname) |
| 533 | cname = name_member(iname,prefix); |
| 534 | else |
| 535 | cname = name_member(name,prefix); |
| 536 | |
| 537 | // Now do a symbol table lookup on it : |
| 538 | |
| 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); |
| 542 | return; |
| 543 | } |
| 544 | |
| 545 | // Form correct C++ name |
| 546 | |
| 547 | sprintf(mname,"%s::%s",ClassName,name); |
| 548 | |
| 549 | if (doc_entry) |
| 550 | doc_entry->cinfo << "Static member : "; |
| 551 | |
| 552 | // Link with this variable |
| 553 | |
| 554 | lang->link_variable(mname,cname,t); |
| 555 | } |
| 556 | |
| 557 | // --------------------------------------------------------------------------------- |
| 558 | // void Language::cpp_class_decl(char *classtype, char *classrename, char *classname) |
| 559 | // |
| 560 | // A forward class declaration |
| 561 | // --------------------------------------------------------------------------------- |
| 562 | |
| 563 | void Language::cpp_class_decl(char *, char *, char *) { |
| 564 | |
| 565 | // Does nothing by default |
| 566 | |
| 567 | } |
| 568 | |
| 569 | // ----------------------------------------------------------------------------- |
| 570 | // void Language::cpp_pragma(Pragma *plist) |
| 571 | // |
| 572 | // Handler C++ pragmas |
| 573 | // ----------------------------------------------------------------------------- |
| 574 | |
| 575 | void Language::cpp_pragma(Pragma *) { |
| 576 | // Does nothing by default |
| 577 | } |
| 578 | |
| 579 | // --------------------------------------------------------------------------------- |
| 580 | // void Language::add_typedef(DataType *t, char *name) |
| 581 | // |
| 582 | // Process a typedef declaration. |
| 583 | // --------------------------------------------------------------------------------- |
| 584 | |
| 585 | void Language::add_typedef(DataType *, char *) { |
| 586 | |
| 587 | } |
| 588 | |
| 589 | |
| 590 | // --------------------------------------------------------------------------------- |
| 591 | // void Language::pragma(char *target, char *var, char *value) |
| 592 | // |
| 593 | // A pragma declaration |
| 594 | // --------------------------------------------------------------------------------- |
| 595 | |
| 596 | void Language::pragma(char *, char *, char *) { |
| 597 | |
| 598 | // Does nothing by default |
| 599 | |
| 600 | } |
| 601 | |
| 602 | // --------------------------------------------------------------------------------- |
| 603 | // void Language::import(char *filename) |
| 604 | // |
| 605 | // An import directive |
| 606 | // --------------------------------------------------------------------------------- |
| 607 | |
| 608 | void Language::import(char *) { |
| 609 | |
| 610 | // Does nothing by default |
| 611 | |
| 612 | } |
| 613 | |
| 614 | |
| 615 | |
| 616 | |
| 617 | |
| 618 | |
| 619 | |
| 620 | |
| 621 | |