1 /* -----------------------------------------------------------------------------
2 * See the LICENSE file for information on copyright, usage and redistribution
3 * of SWIG, and the README file for authors - http://www.swig.org/release.html.
7 * Tcl8 language module for SWIG.
8 * ----------------------------------------------------------------------------- */
10 char cvsroot_tcl8_cxx[] = "$Id: tcl8.cxx 11518 2009-08-08 22:56:10Z wsfulton $";
14 static int treduce = SWIG_cparse_template_reduce(0);
16 static const char *usage = (char *) "\
17 Tcl 8 Options (available with -tcl)\n\
18 -itcl - Enable ITcl support\n\
19 -nosafe - Leave out SafeInit module function.\n\
20 -prefix <name> - Set a prefix <name> to be prepended to all names\n\
21 -namespace - Build module into a Tcl 8 namespace\n\
22 -pkgversion - Set package version\n\n";
24 static String *cmd_tab = 0; /* Table of command names */
25 static String *var_tab = 0; /* Table of global variables */
26 static String *const_tab = 0; /* Constant table */
27 static String *methods_tab = 0; /* Methods table */
28 static String *attr_tab = 0; /* Attribute table */
29 static String *prefix = 0;
30 static String *module = 0;
31 static int nspace = 0;
32 static String *init_name = 0;
33 static String *ns_name = 0;
34 static int have_constructor;
35 static String *constructor_name;
36 static int have_destructor;
37 static int have_base_classes;
38 static String *destructor_action = 0;
39 static String *version = (String *) "0.0";
40 static String *class_name = 0;
42 static int have_attributes;
43 static int have_methods;
44 static int nosafe = 0;
46 static File *f_header = 0;
47 static File *f_wrappers = 0;
48 static File *f_init = 0;
49 static File *f_begin = 0;
50 static File *f_runtime = 0;
55 static File *f_shadow = 0;
56 static File *f_shadow_stubs = 0;
58 static String *constructor = 0;
59 static String *destructor = 0;
60 static String *base_classes = 0;
61 static String *base_class_init = 0;
62 static String *methods = 0;
63 static String *imethods = 0;
64 static String *attributes = 0;
65 static String *attribute_traces = 0;
66 static String *iattribute_traces = 0;
70 class TCL8:public Language {
73 /* ------------------------------------------------------------
75 * ------------------------------------------------------------ */
77 virtual void main(int argc, char *argv[]) {
80 SWIG_library_directory("tcl");
82 for (int i = 1; i < argc; i++) {
84 if (strcmp(argv[i], "-prefix") == 0) {
86 prefix = NewString(argv[i + 1]);
92 } else if (strcmp(argv[i], "-pkgversion") == 0) {
94 version = NewString(argv[i + 1]);
99 } else if (strcmp(argv[i], "-namespace") == 0) {
102 } else if (strcmp(argv[i], "-itcl") == 0) {
105 } else if (strcmp(argv[i], "-nosafe") == 0) {
108 } else if (strcmp(argv[i], "-cppcast") == 0) {
111 } else if (strcmp(argv[i], "-nocppcast") == 0) {
114 } else if (strcmp(argv[i], "-help") == 0) {
115 fputs(usage, stdout);
121 Preprocessor_define((DOH *) "SWIG_CPLUSPLUS_CAST", 0);
124 Preprocessor_define("SWIGTCL 1", 0);
125 // SWIGTCL8 is deprecated, and no longer documented.
126 Preprocessor_define("SWIGTCL8 1", 0);
127 SWIG_typemap_lang("tcl8");
128 SWIG_config_file("tcl8.swg");
132 /* ------------------------------------------------------------
134 * ------------------------------------------------------------ */
136 virtual int top(Node *n) {
138 /* Initialize all of the output files */
139 String *outfile = Getattr(n, "outfile");
141 f_begin = NewFile(outfile, "w", SWIG_output_files());
143 FileErrorDisplay(outfile);
144 SWIG_exit(EXIT_FAILURE);
146 f_runtime = NewString("");
147 f_init = NewString("");
148 f_header = NewString("");
149 f_wrappers = NewString("");
151 /* Register file targets with the SWIG file handler */
152 Swig_register_filebyname("header", f_header);
153 Swig_register_filebyname("wrapper", f_wrappers);
154 Swig_register_filebyname("begin", f_begin);
155 Swig_register_filebyname("runtime", f_runtime);
156 Swig_register_filebyname("init", f_init);
158 /* Initialize some variables for the object interface */
160 cmd_tab = NewString("");
161 var_tab = NewString("");
162 methods_tab = NewString("");
163 const_tab = NewString("");
165 Swig_banner(f_begin);
167 Printf(f_runtime, "\n");
168 Printf(f_runtime, "#define SWIGTCL\n");
169 Printf(f_runtime, "\n");
171 /* Set the module name, namespace, and prefix */
173 module = NewStringf("%(lower)s", Getattr(n, "name"));
174 init_name = NewStringf("%(title)s_Init", module);
176 ns_name = prefix ? Copy(prefix) : Copy(module);
181 /* If shadow classing is enabled, we're going to change the module name to "_module" */
184 filen = NewStringf("%s%s.itcl", Swig_file_dirname(outfile), module);
186 Insert(module, 0, "_");
188 if ((f_shadow = NewFile(filen, "w", SWIG_output_files())) == 0) {
189 FileErrorDisplay(filen);
190 SWIG_exit(EXIT_FAILURE);
192 f_shadow_stubs = NewString("");
194 Swig_register_filebyname("shadow", f_shadow);
195 Swig_register_filebyname("itcl", f_shadow);
197 Swig_banner_target_lang(f_shadow, "#");
199 Printv(f_shadow, "\npackage require Itcl\n\n", NIL);
203 /* Generate some macros used throughout code generation */
205 Printf(f_header, "#define SWIG_init %s\n", init_name);
206 Printf(f_header, "#define SWIG_name \"%s\"\n", module);
208 Printf(f_header, "#define SWIG_prefix \"%s::\"\n", ns_name);
209 Printf(f_header, "#define SWIG_namespace \"%s\"\n\n", ns_name);
211 Printf(f_header, "#define SWIG_prefix \"%s\"\n", prefix);
213 Printf(f_header, "#define SWIG_version \"%s\"\n", version);
215 Printf(cmd_tab, "\nstatic swig_command_info swig_commands[] = {\n");
216 Printf(var_tab, "\nstatic swig_var_info swig_variables[] = {\n");
217 Printf(const_tab, "\nstatic swig_const_info swig_constants[] = {\n");
219 Printf(f_wrappers, "#ifdef __cplusplus\nextern \"C\" {\n#endif\n");
221 /* Start emitting code */
224 /* Done. Close up the module */
225 Printv(cmd_tab, tab4, "{0, 0, 0}\n", "};\n", NIL);
226 Printv(var_tab, tab4, "{0,0,0,0}\n", "};\n", NIL);
227 Printv(const_tab, tab4, "{0,0,0,0,0,0}\n", "};\n", NIL);
229 Printv(f_wrappers, cmd_tab, var_tab, const_tab, NIL);
231 /* Dump the pointer equivalency table */
232 SwigType_emit_type_table(f_runtime, f_wrappers);
234 Printf(f_wrappers, "#ifdef __cplusplus\n}\n#endif\n");
236 /* Close the init function and quit */
237 Printf(f_init, "return TCL_OK;\n}\n");
240 Printf(f_init, "SWIGEXPORT int %(title)s_SafeInit(Tcl_Interp *interp) {\n", module);
241 Printf(f_init, " return SWIG_init(interp);\n");
242 Printf(f_init, "}\n");
246 Printv(f_shadow, f_shadow_stubs, "\n", NIL);
251 /* Close all of the files */
252 Dump(f_runtime, f_begin);
253 Printv(f_begin, f_header, f_wrappers, NIL);
254 Wrapper_pretty_print(f_init, f_begin);
264 /* ------------------------------------------------------------
266 * ------------------------------------------------------------ */
268 virtual int functionWrapper(Node *n) {
269 String *name = Getattr(n, "name"); /* Like to get rid of this */
270 String *iname = Getattr(n, "sym:name");
271 SwigType *type = Getattr(n, "type");
272 ParmList *parms = Getattr(n, "parms");
273 String *overname = 0;
279 String *incode, *cleanup, *outarg, *argstr, *args;
280 int num_arguments = 0;
281 int num_required = 0;
286 if (Getattr(n, "sym:overloaded")) {
287 overname = Getattr(n, "sym:overname");
289 if (!addSymbol(iname, n))
293 incode = NewString("");
294 cleanup = NewString("");
295 outarg = NewString("");
296 argstr = NewString("\"");
297 args = NewString("");
301 #ifdef SWIG_USE_RESULTOBJ
302 Wrapper_add_local(f, "resultobj", "Tcl_Obj *resultobj = NULL");
306 String *wname = Swig_name_wrapper(iname);
308 Append(wname, overname);
310 Setattr(n, "wrap:name", wname);
312 Printv(f->def, "SWIGINTERN int\n ", wname, "(ClientData clientData SWIGUNUSED, Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[]) {", NIL);
314 // Emit all of the local variables for holding arguments.
315 emit_parameter_variables(parms, f);
317 /* Attach standard typemaps */
318 emit_attach_parmmaps(parms, f);
319 Setattr(n, "wrap:parms", parms);
321 /* Get number of require and total arguments */
322 num_arguments = emit_num_arguments(parms);
323 num_required = emit_num_required(parms);
324 varargs = emit_isvarargs(parms);
326 /* Unmarshal parameters */
328 for (i = 0, p = parms; i < num_arguments; i++) {
329 /* Skip ignored arguments */
331 while (checkAttribute(p, "tmap:in:numinputs", "0")) {
332 p = Getattr(p, "tmap:in:next");
335 SwigType *pt = Getattr(p, "type");
336 String *ln = Getattr(p, "lname");
338 /* Produce string representations of the source and target arguments */
339 sprintf(source, "objv[%d]", i + 1);
341 if (i == num_required)
343 if ((tm = Getattr(p, "tmap:in"))) {
344 String *parse = Getattr(p, "tmap:in:parse");
346 Replaceall(tm, "$target", ln);
347 Replaceall(tm, "$source", source);
348 Replaceall(tm, "$input", source);
349 Setattr(p, "emit:input", source);
351 if (Getattr(p, "wrap:disown") || (Getattr(p, "tmap:in:disown"))) {
352 Replaceall(tm, "$disown", "SWIG_POINTER_DISOWN");
354 Replaceall(tm, "$disown", "0");
358 Printf(args, ",(void *)0");
359 if (i >= num_required) {
360 Printf(incode, "if (objc > %d) {\n", i + 1);
362 Printf(incode, "%s\n", tm);
363 if (i >= num_required) {
364 Printf(incode, "}\n");
367 Printf(argstr, "%s", parse);
368 Printf(args, ",&%s", ln);
369 if (Strcmp(parse, "p") == 0) {
370 SwigType *lt = SwigType_ltype(pt);
371 SwigType_remember(pt);
372 if (Cmp(lt, "p.void") == 0) {
373 Printf(args, ",(void *)0");
375 Printf(args, ",SWIGTYPE%s", SwigType_manglestr(pt));
380 p = Getattr(p, "tmap:in:next");
383 Swig_warning(WARN_TYPEMAP_IN_UNDEF, input_file, line_number, "Unable to use type %s as a function argument.\n", SwigType_str(pt, 0));
392 /* If variable length arguments we need to emit the in typemap here */
393 if (p && (tm = Getattr(p, "tmap:in"))) {
394 sprintf(source, "objv[%d]", i + 1);
395 Printf(incode, "if (objc > %d) {\n", i);
396 Replaceall(tm, "$input", source);
397 Printv(incode, tm, "\n", NIL);
398 Printf(incode, "}\n");
402 Printf(argstr, "%s\"", usage_string(Char(iname), type, parms));
404 Printv(f->code, "if (SWIG_GetArgs(interp, objc, objv,", argstr, args, ") == TCL_ERROR) SWIG_fail;\n", NIL);
406 Printv(f->code, incode, NIL);
408 /* Insert constraint checking code */
409 for (p = parms; p;) {
410 if ((tm = Getattr(p, "tmap:check"))) {
411 Replaceall(tm, "$target", Getattr(p, "lname"));
412 Printv(f->code, tm, "\n", NIL);
413 p = Getattr(p, "tmap:check:next");
419 /* Insert cleanup code */
420 for (i = 0, p = parms; p; i++) {
421 if (!checkAttribute(p, "tmap:in:numinputs", "0")
422 && !Getattr(p, "tmap:in:parse") && (tm = Getattr(p, "tmap:freearg"))) {
424 Replaceall(tm, "$source", Getattr(p, "lname"));
425 Printv(cleanup, tm, "\n", NIL);
427 p = Getattr(p, "tmap:freearg:next");
433 /* Insert argument output code */
434 for (i = 0, p = parms; p; i++) {
435 if ((tm = Getattr(p, "tmap:argout"))) {
436 Replaceall(tm, "$source", Getattr(p, "lname"));
437 #ifdef SWIG_USE_RESULTOBJ
438 Replaceall(tm, "$target", "resultobj");
439 Replaceall(tm, "$result", "resultobj");
441 Replaceall(tm, "$target", "(Tcl_GetObjResult(interp))");
442 Replaceall(tm, "$result", "(Tcl_GetObjResult(interp))");
444 Replaceall(tm, "$arg", Getattr(p, "emit:input"));
445 Replaceall(tm, "$input", Getattr(p, "emit:input"));
446 Printv(outarg, tm, "\n", NIL);
447 p = Getattr(p, "tmap:argout:next");
453 /* Now write code to make the function call */
454 String *actioncode = emit_action(n);
456 /* Need to redo all of this code (eventually) */
458 /* Return value if necessary */
459 if ((tm = Swig_typemap_lookup_out("out", n, "result", f, actioncode))) {
460 Replaceall(tm, "$source", "result");
461 #ifdef SWIG_USE_RESULTOBJ
462 Replaceall(tm, "$target", "resultobj");
463 Replaceall(tm, "$result", "resultobj");
465 Replaceall(tm, "$target", "(Tcl_GetObjResult(interp))");
466 Replaceall(tm, "$result", "(Tcl_GetObjResult(interp))");
468 if (GetFlag(n, "feature:new")) {
469 Replaceall(tm, "$owner", "SWIG_POINTER_OWN");
471 Replaceall(tm, "$owner", "0");
473 Printf(f->code, "%s\n", tm);
475 Swig_warning(WARN_TYPEMAP_OUT_UNDEF, input_file, line_number, "Unable to use return type %s in function %s.\n", SwigType_str(type, 0), name);
477 emit_return_variable(n, type, f);
479 /* Dump output argument code */
480 Printv(f->code, outarg, NIL);
482 /* Dump the argument cleanup code */
483 Printv(f->code, cleanup, NIL);
485 /* Look for any remaining cleanup */
486 if (GetFlag(n, "feature:new")) {
487 if ((tm = Swig_typemap_lookup("newfree", n, "result", 0))) {
488 Replaceall(tm, "$source", "result");
489 Printf(f->code, "%s\n", tm);
493 if ((tm = Swig_typemap_lookup("ret", n, "result", 0))) {
494 Replaceall(tm, "$source", "result");
495 Printf(f->code, "%s\n", tm);
497 #ifdef SWIG_USE_RESULTOBJ
498 Printv(f->code, "if (resultobj) Tcl_SetObjResult(interp, resultobj);\n", NIL);
500 Printv(f->code, "return TCL_OK;\n", NIL);
501 Printv(f->code, "fail:\n", cleanup, "return TCL_ERROR;\n", NIL);
502 Printv(f->code, "}\n", NIL);
504 /* Substitute the cleanup code */
505 Replaceall(f->code, "$cleanup", cleanup);
506 Replaceall(f->code, "$symname", iname);
508 /* Dump out the function */
509 Wrapper_print(f, f_wrappers);
511 if (!Getattr(n, "sym:overloaded")) {
512 /* Register the function with Tcl */
513 Printv(cmd_tab, tab4, "{ SWIG_prefix \"", iname, "\", (swig_wrapper_func) ", Swig_name_wrapper(iname), ", NULL},\n", NIL);
515 if (!Getattr(n, "sym:nextSibling")) {
516 /* Emit overloading dispatch function */
519 String *dispatch = Swig_overload_dispatch(n, "return %s(clientData, interp, objc, argv - 1);", &maxargs);
521 /* Generate a dispatch wrapper for all overloaded functions */
523 Wrapper *df = NewWrapper();
524 String *dname = Swig_name_wrapper(iname);
526 Printv(df->def, "SWIGINTERN int\n", dname, "(ClientData clientData SWIGUNUSED, Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[]) {", NIL);
527 Printf(df->code, "Tcl_Obj *CONST *argv = objv+1;\n");
528 Printf(df->code, "int argc = objc-1;\n");
529 Printv(df->code, dispatch, "\n", NIL);
530 Printf(df->code, "Tcl_SetResult(interp,(char *) \"No matching function for overloaded '%s'\", TCL_STATIC);\n", iname);
531 Printf(df->code, "return TCL_ERROR;\n");
532 Printv(df->code, "}\n", NIL);
533 Wrapper_print(df, f_wrappers);
534 Printv(cmd_tab, tab4, "{ SWIG_prefix \"", iname, "\", (swig_wrapper_func) ", dname, ", NULL},\n", NIL);
550 /* ------------------------------------------------------------
552 * ------------------------------------------------------------ */
554 virtual int variableWrapper(Node *n) {
556 String *name = Getattr(n, "name");
557 String *iname = Getattr(n, "sym:name");
558 SwigType *t = Getattr(n, "type");
561 String *setfname = 0;
562 Wrapper *setf = 0, *getf = 0;
566 if (!addSymbol(iname, n))
569 /* Create a function for getting a variable */
572 String *getname = Swig_name_get(iname);
573 String *getfname = Swig_name_wrapper(getname);
574 Setattr(n, "wrap:name", getfname);
575 Printv(getf->def, "SWIGINTERN const char *", getfname, "(ClientData clientData SWIGUNUSED, Tcl_Interp *interp, char *name1, char *name2, int flags) {", NIL);
576 Wrapper_add_local(getf, "value", "Tcl_Obj *value = 0");
577 if ((tm = Swig_typemap_lookup("varout", n, name, 0))) {
578 Replaceall(tm, "$source", name);
579 Replaceall(tm, "$target", "value");
580 Replaceall(tm, "$result", "value");
581 /* Printf(getf->code, "%s\n",tm); */
582 addfail = emit_action_code(n, getf->code, tm);
583 Printf(getf->code, "if (value) {\n");
584 Printf(getf->code, "Tcl_SetVar2(interp,name1,name2,Tcl_GetStringFromObj(value,NULL), flags);\n");
585 Printf(getf->code, "Tcl_DecrRefCount(value);\n");
586 Printf(getf->code, "}\n");
587 Printf(getf->code, "return NULL;\n");
589 Append(getf->code, "fail:\n");
590 Printf(getf->code, "return \"%s\";\n", iname);
592 Printf(getf->code, "}\n");
593 Wrapper_print(getf, f_wrappers);
595 Swig_warning(WARN_TYPEMAP_VAROUT_UNDEF, input_file, line_number, "Unable to read variable of type %s\n", SwigType_str(t, 0));
601 /* Try to create a function setting a variable */
602 if (is_assignable(n)) {
604 setname = Swig_name_set(iname);
605 setfname = Swig_name_wrapper(setname);
606 Setattr(n, "wrap:name", setfname);
608 Printv(setf->def, "SWIGINTERN const char *", setfname,
609 "(ClientData clientData SWIGUNUSED, Tcl_Interp *interp, char *name1, char *name2 SWIGUNUSED, int flags) {", NIL);
610 Wrapper_add_local(setf, "value", "Tcl_Obj *value = 0");
611 Wrapper_add_local(setf, "name1o", "Tcl_Obj *name1o = 0");
613 if ((tm = Swig_typemap_lookup("varin", n, name, 0))) {
614 Replaceall(tm, "$source", "value");
615 Replaceall(tm, "$target", name);
616 Replaceall(tm, "$input", "value");
617 Printf(setf->code, "name1o = Tcl_NewStringObj(name1,-1);\n");
618 Printf(setf->code, "value = Tcl_ObjGetVar2(interp, name1o, 0, flags);\n");
619 Printf(setf->code, "Tcl_DecrRefCount(name1o);\n");
620 Printf(setf->code, "if (!value) SWIG_fail;\n");
621 /* Printf(setf->code,"%s\n", tm); */
622 emit_action_code(n, setf->code, tm);
623 Printf(setf->code, "return NULL;\n");
624 Printf(setf->code, "fail:\n");
625 Printf(setf->code, "return \"%s\";\n", iname);
626 Printf(setf->code, "}\n");
627 Wrapper_print(setf, f_wrappers);
629 Swig_warning(WARN_TYPEMAP_VARIN_UNDEF, input_file, line_number, "Unable to set variable of type %s.\n", SwigType_str(t, 0));
639 Printv(var_tab, tab4, "{ SWIG_prefix \"", iname, "\", 0, (swig_variable_func) ", getfname, ",", NIL);
641 static int readonlywrap = 0;
643 Wrapper *ro = NewWrapper();
645 "SWIGINTERN const char *swig_readonly(ClientData clientData SWIGUNUSED, Tcl_Interp *interp SWIGUNUSED, char *name1 SWIGUNUSED, char *name2 SWIGUNUSED, int flags SWIGUNUSED) {");
646 Printv(ro->code, "return \"Variable is read-only\";\n", "}\n", NIL);
647 Wrapper_print(ro, f_wrappers);
651 Printf(var_tab, "(swig_variable_func) swig_readonly},\n");
653 Printv(var_tab, "(swig_variable_func) ", setfname, "},\n", NIL);
662 /* ------------------------------------------------------------
664 * ------------------------------------------------------------ */
666 virtual int constantWrapper(Node *n) {
667 String *name = Getattr(n, "name");
668 String *iname = Getattr(n, "sym:name");
669 String *nsname = !nspace ? Copy(iname) : NewStringf("%s::%s", ns_name, iname);
670 SwigType *type = Getattr(n, "type");
671 String *rawval = Getattr(n, "rawval");
672 String *value = rawval ? rawval : Getattr(n, "value");
675 if (!addSymbol(iname, n))
678 Setattr(n, "sym:name", nsname);
680 /* Special hook for member pointer */
681 if (SwigType_type(type) == T_MPOINTER) {
682 String *wname = Swig_name_wrapper(iname);
683 Printf(f_wrappers, "static %s = %s;\n", SwigType_str(type, wname), value);
687 if ((tm = Swig_typemap_lookup("consttab", n, name, 0))) {
688 Replaceall(tm, "$source", value);
689 Replaceall(tm, "$target", name);
690 Replaceall(tm, "$value", value);
691 Replaceall(tm, "$nsname", nsname);
692 Printf(const_tab, "%s,\n", tm);
693 } else if ((tm = Swig_typemap_lookup("constcode", n, name, 0))) {
694 Replaceall(tm, "$source", value);
695 Replaceall(tm, "$target", name);
696 Replaceall(tm, "$value", value);
697 Replaceall(tm, "$nsname", nsname);
698 Printf(f_init, "%s\n", tm);
701 Swig_warning(WARN_TYPEMAP_CONST_UNDEF, input_file, line_number, "Unsupported constant value.\n");
708 /* ------------------------------------------------------------
710 * ------------------------------------------------------------ */
712 virtual int nativeWrapper(Node *n) {
713 String *name = Getattr(n, "sym:name");
714 String *funcname = Getattr(n, "wrap:name");
715 if (!addSymbol(funcname, n))
718 Printf(f_init, "\t Tcl_CreateObjCommand(interp, SWIG_prefix \"%s\", (swig_wrapper_func) %s, (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);\n", name,
723 /* ------------------------------------------------------------
725 * ------------------------------------------------------------ */
727 virtual int classHandler(Node *n) {
728 static Hash *emitted = NewHash();
729 String *mangled_classname = 0;
730 String *real_classname = 0;
732 have_constructor = 0;
734 destructor_action = 0;
737 constructor = NewString("");
738 destructor = NewString("");
739 base_classes = NewString("");
740 base_class_init = NewString("");
741 methods = NewString("");
742 imethods = NewString("");
743 attributes = NewString("");
744 attribute_traces = NewString("");
745 iattribute_traces = NewString("");
747 have_base_classes = 0;
752 class_name = Getattr(n, "sym:name");
753 if (!addSymbol(class_name, n))
756 real_classname = Getattr(n, "name");
757 mangled_classname = Swig_name_mangle(real_classname);
759 if (Getattr(emitted, mangled_classname))
761 Setattr(emitted, mangled_classname, "1");
763 attr_tab = NewString("");
764 Printf(attr_tab, "static swig_attribute swig_");
765 Printv(attr_tab, mangled_classname, "_attributes[] = {\n", NIL);
767 methods_tab = NewStringf("");
768 Printf(methods_tab, "static swig_method swig_");
769 Printv(methods_tab, mangled_classname, "_methods[] = {\n", NIL);
771 /* Generate normal wrappers */
772 Language::classHandler(n);
774 SwigType *t = Copy(Getattr(n, "name"));
775 SwigType_add_pointer(t);
777 // Catch all: eg. a class with only static functions and/or variables will not have 'remembered'
778 // SwigType_remember(t);
779 String *wrap_class = NewStringf("&_wrap_class_%s", mangled_classname);
780 SwigType_remember_clientdata(t, wrap_class);
782 // t = Copy(Getattr(n,"classtype"));
783 // SwigType_add_pointer(t);
785 String *rt = Copy(Getattr(n, "classtype"));
786 SwigType_add_pointer(rt);
788 // Register the class structure with the type checker
789 /* Printf(f_init,"SWIG_TypeClientData(SWIGTYPE%s, (void *) &_wrap_class_%s);\n", SwigType_manglestr(t), mangled_classname); */
790 if (have_destructor) {
791 Printv(f_wrappers, "SWIGINTERN void swig_delete_", class_name, "(void *obj) {\n", NIL);
792 if (destructor_action) {
793 Printv(f_wrappers, SwigType_str(rt, "arg1"), " = (", SwigType_str(rt, 0), ") obj;\n", NIL);
794 Printv(f_wrappers, destructor_action, "\n", NIL);
797 Printv(f_wrappers, " delete (", SwigType_str(rt, 0), ") obj;\n", NIL);
799 Printv(f_wrappers, " free((char *) obj);\n", NIL);
802 Printf(f_wrappers, "}\n");
805 Printf(methods_tab, " {0,0}\n};\n");
806 Printv(f_wrappers, methods_tab, NIL);
808 Printf(attr_tab, " {0,0,0}\n};\n");
809 Printv(f_wrappers, attr_tab, NIL);
811 /* Handle inheritance */
813 String *base_class = NewString("");
814 String *base_class_names = NewString("");
817 base_classes = NewString("");
820 List *baselist = Getattr(n, "bases");
821 if (baselist && Len(baselist)) {
826 String *bname = Getattr(b.item, "name");
827 if ((!bname) || GetFlag(b.item, "feature:ignore") || (!Getattr(b.item, "module"))) {
832 have_base_classes = 1;
833 Printv(base_classes, bname, " ", NIL);
834 Printv(base_class_init, " ", bname, "Ptr::constructor $ptr\n", NIL);
836 String *bmangle = Swig_name_mangle(bname);
837 // Printv(f_wrappers,"extern swig_class _wrap_class_", bmangle, ";\n", NIL);
838 // Printf(base_class,"&_wrap_class_%s",bmangle);
839 Printf(base_class, "0");
840 Printf(base_class_names, "\"%s *\",", SwigType_namestr(bname));
841 /* Put code to register base classes in init function */
843 //Printf(f_init,"/* Register base : %s */\n", bmangle);
844 //Printf(f_init,"swig_%s_bases[%d] = (swig_class *) SWIG_TypeQuery(\"%s *\")->clientdata;\n", mangled_classname, index, SwigType_namestr(bname));
847 Putc(',', base_class);
853 String *ptrclass = NewString("");
855 // First, build the pointer base class
856 Printv(ptrclass, "itcl::class ", class_name, "Ptr {\n", NIL);
857 if (have_base_classes)
858 Printv(ptrclass, " inherit ", base_classes, "\n", NIL);
860 // Define protected variables for SWIG object pointer
861 Printv(ptrclass, " protected variable swigobj\n", " protected variable thisown\n", NIL);
863 // Define public variables
864 if (have_attributes) {
865 Printv(ptrclass, attributes, NIL);
867 // base class swig_getset was being called for complex inheritance trees
870 Printv(ptrclass, " protected method ", class_name, "_swig_getset {var name1 name2 op} {\n", NIL);
873 " switch -exact -- $op {\n",
874 " r {set $var [", ns_name, "::", class_name, "_[set var]_get $swigobj]}\n",
875 " w {", ns_name, "::", class_name, "_${var}_set $swigobj [set $var]}\n", " }\n", " }\n", NIL);
878 " protected method ", class_name, "_swig_getset {var name1 name2 op} {\n",
879 " switch -exact -- $op {\n",
880 " r {set $var [", class_name, "_[set var]_get $swigobj]}\n",
881 " w {", class_name, "_${var}_set $swigobj [set $var]}\n", " }\n", " }\n", NIL);
884 // Add the constructor, which may include
885 // calls to base class class constructors
887 Printv(ptrclass, " constructor { ptr } {\n", NIL);
888 if (have_base_classes) {
889 Printv(ptrclass, base_class_init, NIL);
890 Printv(ptrclass, " } {\n", NIL);
893 Printv(ptrclass, " set swigobj $ptr\n", " set thisown 0\n", NIL);
895 if (have_attributes) {
896 Printv(ptrclass, attribute_traces, NIL);
898 Printv(ptrclass, " }\n", NIL);
902 Printv(ptrclass, " destructor {\n",
903 " set d_func delete_", class_name, "\n",
904 " if { $thisown && ([info command $d_func] != \"\") } {\n" " $d_func $swigobj\n", " }\n", " }\n", NIL);
908 Printv(ptrclass, imethods, NIL);
911 // Close out the pointer class
912 Printv(ptrclass, "}\n\n", NIL);
913 Printv(f_shadow, ptrclass, NIL);
917 // Create the "real" class.
918 Printv(f_shadow, "itcl::class ", class_name, " {\n", NIL);
919 Printv(f_shadow, " inherit ", class_name, "Ptr\n", NIL);
921 // If we have a constructor, then use it.
922 // If not, then we must have an abstract class without
923 // any constructor. So we create a class constructor
924 // which will fail for this class (but not for inherited
925 // classes). Note that the constructor must fail before
926 // calling the ptrclass constructor.
928 if (have_constructor) {
929 Printv(f_shadow, constructor, NIL);
931 Printv(f_shadow, " constructor { } {\n", NIL);
932 Printv(f_shadow, " # This constructor will fail if called directly\n", NIL);
933 Printv(f_shadow, " if { [info class] == \"::", class_name, "\" } {\n", NIL);
934 Printv(f_shadow, " error \"No constructor for class ", class_name, (Getattr(n, "abstract") ? " - class is abstract" : ""), "\"\n", NIL);
935 Printv(f_shadow, " }\n", NIL);
936 Printv(f_shadow, " }\n", NIL);
939 Printv(f_shadow, "}\n\n", NIL);
942 Printv(f_wrappers, "static swig_class *swig_", mangled_classname, "_bases[] = {", base_class, "0};\n", NIL);
943 Printv(f_wrappers, "static const char * swig_", mangled_classname, "_base_names[] = {", base_class_names, "0};\n", NIL);
945 Delete(base_class_names);
947 Printv(f_wrappers, "static swig_class _wrap_class_", mangled_classname, " = { \"", class_name, "\", &SWIGTYPE", SwigType_manglestr(t), ",", NIL);
949 if (have_constructor) {
950 Printf(f_wrappers, "%s", Swig_name_wrapper(Swig_name_construct(constructor_name)));
951 Delete(constructor_name);
952 constructor_name = 0;
954 Printf(f_wrappers, "0");
956 if (have_destructor) {
957 Printv(f_wrappers, ", swig_delete_", class_name, NIL);
959 Printf(f_wrappers, ",0");
961 Printv(f_wrappers, ", swig_", mangled_classname, "_methods, swig_", mangled_classname, "_attributes, swig_", mangled_classname, "_bases,",
962 "swig_", mangled_classname, "_base_names, &swig_module };\n", NIL);
965 Printv(cmd_tab, tab4, "{ SWIG_prefix \"", class_name, "\", (swig_wrapper_func) SWIG_ObjectConstructor, (ClientData)&_wrap_class_", mangled_classname,
970 Delete(mangled_classname);
975 /* ------------------------------------------------------------
976 * memberfunctionHandler()
977 * ------------------------------------------------------------ */
979 virtual int memberfunctionHandler(Node *n) {
980 String *name = Getattr(n, "name");
981 String *iname = GetChar(n, "sym:name");
983 String *realname, *rname;
985 Language::memberfunctionHandler(n);
987 realname = iname ? iname : name;
988 rname = Swig_name_wrapper(Swig_name_member(class_name, realname));
989 if (!Getattr(n, "sym:nextSibling")) {
990 Printv(methods_tab, tab4, "{\"", realname, "\", ", rname, "}, \n", NIL);
994 ParmList *l = Getattr(n, "parms");
996 String *pname = NewString("");
998 // Add this member to our class handler function
999 Printv(imethods, tab2, "method ", realname, " [list ", NIL);
1002 for (p = l; p; p = nextSibling(p)) {
1004 String *pn = Getattr(p, "name");
1005 String *dv = Getattr(p, "value");
1006 SwigType *pt = Getattr(p, "type");
1008 Printv(pname, ",(", pt, ")", NIL);
1011 /* Only print an argument if not void */
1012 if (Cmp(pt, "void") != 0) {
1014 Printv(pname, pn, NIL);
1016 Printf(pname, "p%d", pnum);
1020 String *defval = NewString(dv);
1022 Insert(defval, 0, "::");
1023 Insert(defval, 0, ns_name);
1025 if (Strncmp(dv, "(", 1) == 0) {
1026 Insert(defval, 0, "$");
1027 Replaceall(defval, "(", "");
1028 Replaceall(defval, ")", "");
1030 Printv(imethods, "[list ", pname, " ", defval, "] ", NIL);
1032 Printv(imethods, pname, " ", NIL);
1037 Printv(imethods, "] ", NIL);
1040 Printv(imethods, "{ ", ns_name, "::", class_name, "_", realname, " $swigobj", NIL);
1042 Printv(imethods, "{ ", class_name, "_", realname, " $swigobj", NIL);
1046 for (p = l; p; p = nextSibling(p)) {
1048 String *pn = Getattr(p, "name");
1049 SwigType *pt = Getattr(p, "type");
1052 /* Only print an argument if not void */
1053 if (Cmp(pt, "void") != 0) {
1055 Printv(pname, pn, NIL);
1057 Printf(pname, "p%d", pnum);
1059 Printv(imethods, " $", pname, NIL);
1063 Printv(imethods, " }\n", NIL);
1071 /* ------------------------------------------------------------
1072 * membervariableHandler()
1073 * ------------------------------------------------------------ */
1075 virtual int membervariableHandler(Node *n) {
1076 String *symname = Getattr(n, "sym:name");
1079 Language::membervariableHandler(n);
1080 Printv(attr_tab, tab4, "{ \"-", symname, "\",", NIL);
1081 rname = Swig_name_wrapper(Swig_name_get(Swig_name_member(class_name, symname)));
1082 Printv(attr_tab, rname, ", ", NIL);
1084 if (!GetFlag(n, "feature:immutable")) {
1085 rname = Swig_name_wrapper(Swig_name_set(Swig_name_member(class_name, symname)));
1086 Printv(attr_tab, rname, "},\n", NIL);
1089 Printf(attr_tab, "0 },\n");
1093 Printv(attributes, " public variable ", symname, "\n", NIL);
1095 Printv(attribute_traces, " trace variable ", symname, " rw [list ", class_name, "_swig_getset ", symname, "]\n", NIL);
1096 Printv(attribute_traces, " set ", symname, "\n", NIL);
1098 have_attributes = 1;
1103 /* ------------------------------------------------------------
1104 * constructorHandler()
1105 * ------------------------------------------------------------ */
1107 virtual int constructorHandler(Node *n) {
1108 Language::constructorHandler(n);
1111 String *name = Getattr(n, "name");
1112 String *iname = GetChar(n, "sym:name");
1116 ParmList *l = Getattr(n, "parms");
1119 String *pname = NewString("");
1121 realname = iname ? iname : name;
1123 if (!have_constructor) {
1124 // Add this member to our class handler function
1125 Printf(constructor, " constructor { ");
1127 // Add parameter list
1129 for (p = l; p; p = nextSibling(p)) {
1131 SwigType *pt = Getattr(p, "type");
1132 String *pn = Getattr(p, "name");
1133 String *dv = Getattr(p, "value");
1136 /* Only print an argument if not void */
1137 if (Cmp(pt, "void") != 0) {
1139 Printv(pname, pn, NIL);
1141 Printf(pname, "p%d", pnum);
1145 Printv(constructor, "{", pname, " {", dv, "} } ", NIL);
1147 Printv(constructor, pname, " ", NIL);
1152 Printf(constructor, "} { \n");
1154 // [BRE] 08/17/00 Added test to see if we are instantiating this object
1155 // type, or, if this constructor is being called as part of the itcl
1156 // inheritance hierarchy.
1157 // In the former case, we need to call the C++ constructor, in the
1158 // latter we don't, or we end up with two C++ objects.
1159 // Check to see if we are instantiating a 'realname' or something
1162 Printv(constructor, " if { [string equal -nocase \"", realname, "\" \"[namespace tail [info class]]\" ] } {\n", NIL);
1164 // Call to constructor wrapper and parent Ptr class
1165 // [BRE] add -namespace/-prefix support
1168 Printv(constructor, " ", realname, "Ptr::constructor [", ns_name, "::new_", realname, NIL);
1170 Printv(constructor, " ", realname, "Ptr::constructor [new_", realname, NIL);
1174 for (p = l; p; p = nextSibling(p)) {
1176 SwigType *pt = Getattr(p, "type");
1177 String *pn = Getattr(p, "name");
1180 /* Only print an argument if not void */
1181 if (Cmp(pt, "void") != 0) {
1183 Printv(pname, pn, NIL);
1185 Printf(pname, "p%d", pnum);
1187 Printv(constructor, " $", pname, NIL);
1192 Printv(constructor, "]\n", " }\n", " } {\n", " set thisown 1\n", " }\n", NIL);
1196 constructor_name = NewString(Getattr(n, "sym:name"));
1197 have_constructor = 1;
1201 /* ------------------------------------------------------------
1202 * destructorHandler()
1203 * ------------------------------------------------------------ */
1205 virtual int destructorHandler(Node *n) {
1206 Language::destructorHandler(n);
1207 have_destructor = 1;
1208 destructor_action = Getattr(n, "wrap:action");
1212 /* ------------------------------------------------------------
1214 * ------------------------------------------------------------ */
1216 virtual int validIdentifier(String *s) {
1222 /* ------------------------------------------------------------
1224 * ------------------------------------------------------------ */
1226 char *usage_string(char *iname, SwigType *, ParmList *l) {
1227 static String *temp = 0;
1229 int i, numopt, pcount;
1232 temp = NewString("");
1235 Printf(temp, "%s::%s ", ns_name, iname);
1237 Printf(temp, "%s ", iname);
1239 /* Now go through and print parameters */
1241 pcount = emit_num_arguments(l);
1242 numopt = pcount - emit_num_required(l);
1243 for (p = l; p; p = nextSibling(p)) {
1245 SwigType *pt = Getattr(p, "type");
1246 String *pn = Getattr(p, "name");
1247 /* Only print an argument if not ignored */
1248 if (!checkAttribute(p, "tmap:in:numinputs", "0")) {
1249 if (i >= (pcount - numopt))
1252 Printf(temp, "%s", pn);
1254 Printf(temp, "%s", SwigType_str(pt, 0));
1256 if (i >= (pcount - numopt))
1265 String *runtimeCode() {
1266 String *s = NewString("");
1267 String *serrors = Swig_include_sys("tclerrors.swg");
1269 Printf(stderr, "*** Unable to open 'tclerrors.swg'\n");
1274 String *sapi = Swig_include_sys("tclapi.swg");
1276 Printf(stderr, "*** Unable to open 'tclapi.swg'\n");
1281 String *srun = Swig_include_sys("tclrun.swg");
1283 Printf(stderr, "*** Unable to open 'tclrun.swg'\n");
1292 String *defaultExternalRuntimeFilename() {
1293 return NewString("swigtclrun.h");
1297 /* ----------------------------------------------------------------------
1298 * swig_tcl() - Instantiate module
1299 * ---------------------------------------------------------------------- */
1301 static Language *new_swig_tcl() {
1304 extern "C" Language *swig_tcl(void) {
1305 return new_swig_tcl();