7
7
* Mzscheme language module for SWIG.
8
8
* ----------------------------------------------------------------------------- */
10
char cvsroot_mzscheme_cxx[] = "$Header: /cvsroot/swig/SWIG/Source/Modules/mzscheme.cxx,v 1.20 2006/03/06 22:50:57 wsfulton Exp $";
10
char cvsroot_mzscheme_cxx[] = "$Header: /cvsroot/swig/SWIG/Source/Modules/mzscheme.cxx,v 1.24 2006/11/01 23:54:51 wsfulton Exp $";
12
12
#include "swigmod.h"
16
static const char *usage = (char*)"\
16
static const char *usage = (char *) "\
17
17
Mzscheme Options (available with -mzscheme)\n\
18
-prefix <name> - Set a prefix <name> to be prepended to all names\n\
19
-declaremodule - Create extension that declares a module\n\
20
-noinit - Do not emit scheme_initialize, scheme_reload,\n\
21
scheme_module_name functions\n";
23
static String *fieldnames_tab = 0;
24
static String *convert_tab = 0;
25
static String *convert_proto_tab = 0;
26
static String *struct_name = 0;
27
static String *mangled_struct_name = 0;
29
static char *prefix=0;
18
-prefix <name> - Set a prefix <name> to be prepended to all names\n\
19
-declaremodule - Create extension that declares a module\n\
20
-noinit - Do not emit scheme_initialize, scheme_reload,\n\
21
scheme_module_name functions\n\
22
-dynamic-load <library>,[library,...] - Do not link with these libraries, dynamic load\n\
26
static String *fieldnames_tab = 0;
27
static String *convert_tab = 0;
28
static String *convert_proto_tab = 0;
29
static String *struct_name = 0;
30
static String *mangled_struct_name = 0;
32
static char *prefix = 0;
30
33
static bool declaremodule = false;
31
34
static bool noinit = false;
32
static String *module=0;
33
static char *mzscheme_path=(char*)"mzscheme";
36
static char *load_libraries = NULL;
38
static String *module = 0;
39
static char *mzscheme_path = (char *) "mzscheme";
34
40
static String *init_func_def = 0;
36
static File *f_runtime = 0;
37
static File *f_header = 0;
38
static File *f_wrappers = 0;
39
static File *f_init = 0;
42
static File *f_runtime = 0;
43
static File *f_header = 0;
44
static File *f_wrappers = 0;
45
static File *f_init = 0;
41
47
// Used for garbage collection
42
static int exporting_destructor = 0;
48
static int exporting_destructor = 0;
43
49
static String *swigtype_ptr = 0;
44
50
static String *cls_swigtype = 0;
46
class MZSCHEME : public Language {
52
class MZSCHEME:public Language {
49
55
/* ------------------------------------------------------------
51
57
* ------------------------------------------------------------ */
53
virtual void main (int argc, char *argv[]) {
59
virtual void main(int argc, char *argv[]) {
57
SWIG_library_directory(mzscheme_path);
63
SWIG_library_directory(mzscheme_path);
59
65
// Look for certain command line options
60
66
for (i = 1; i < argc; i++) {
62
if (strcmp (argv[i], "-help") == 0) {
63
fputs (usage, stdout);
65
} else if (strcmp (argv[i], "-prefix") == 0) {
68
if (strcmp(argv[i], "-help") == 0) {
71
} else if (strcmp(argv[i], "-prefix") == 0) {
67
73
prefix = new char[strlen(argv[i + 1]) + 2];
68
74
strcpy(prefix, argv[i + 1]);
70
Swig_mark_arg (i + 1);
75
} else if (strcmp (argv[i], "-declaremodule") == 0) {
78
} else if (strcmp (argv[i], "-noinit") == 0) {
81
} else if (strcmp(argv[i], "-declaremodule") == 0) {
84
} else if (strcmp(argv[i], "-noinit") == 0) {
89
else if (strcmp(argv[i], "-dynamic-load") == 0) {
90
load_libraries = new char[strlen(argv[i + 1]) + 2];
91
strcpy(load_libraries, argv[i + 1]);
85
99
// If a prefix has been specified make sure it ends in a '_'
88
if (prefix[strlen (prefix)] != '_') {
89
prefix[strlen (prefix) + 1] = 0;
90
prefix[strlen (prefix)] = '_';
102
if (prefix[strlen(prefix)] != '_') {
103
prefix[strlen(prefix) + 1] = 0;
104
prefix[strlen(prefix)] = '_';
93
prefix = (char*)"swig_";
107
prefix = (char *) "swig_";
95
109
// Add a symbol for this module
97
Preprocessor_define ("SWIGMZSCHEME 1",0);
111
Preprocessor_define("SWIGMZSCHEME 1", 0);
99
113
// Set name of typemaps
101
115
SWIG_typemap_lang("mzscheme");
103
117
// Read in default typemaps */
210
227
String *cleanup = NewString("");
211
228
String *outarg = NewString("");
212
229
String *build = NewString("");
214
231
int argout_set = 0;
218
235
String *overname = 0;
238
if (load_libraries) {
239
ParmList *parms = Getattr(n, "parms");
240
SwigType *type = Getattr(n, "type");
241
String *name = NewString("caller");
242
Setattr(n, "wrap:action", Swig_cresult(type, "result", Swig_cfunction_call(name, parms)));
220
246
// Make a wrapper name for this
221
247
String *wname = Swig_name_wrapper(iname);
222
if (Getattr(n,"sym:overloaded")) {
223
overname = Getattr(n,"sym:overname");
248
if (Getattr(n, "sym:overloaded")) {
249
overname = Getattr(n, "sym:overname");
225
if (!addSymbol(iname,n)) return SWIG_ERROR;
251
if (!addSymbol(iname, n))
228
255
Append(wname, overname);
230
Setattr(n,"wrap:name",wname);
257
Setattr(n, "wrap:name", wname);
232
259
// Build the name for Scheme.
233
Printv(proc_name, iname,NIL);
260
Printv(proc_name, iname, NIL);
234
261
Replaceall(proc_name, "_", "-");
236
263
// writing the function wrapper function
237
Printv(f->def, "static Scheme_Object *", wname, " (", NIL);
264
Printv(f->def, "static Scheme_Object *", wname, " (", NIL);
238
265
Printv(f->def, "int argc, Scheme_Object **argv", NIL);
239
266
Printv(f->def, ")\n{", NIL);
241
268
/* Define the scheme name in C. This define is used by several
243
270
Printv(f->def, "#define FUNC_NAME \"", proc_name, "\"", NIL);
245
272
// Declare return variable and arguments
246
273
// number of parameters
247
274
// they are called arg0, arg1, ...
248
275
// the return value is called result
250
277
emit_args(d, l, f);
252
279
/* Attach the standard typemaps */
253
emit_attach_parmmaps(l,f);
254
Setattr(n,"wrap:parms",l);
280
emit_attach_parmmaps(l, f);
281
Setattr(n, "wrap:parms", l);
256
283
numargs = emit_num_arguments(l);
257
numreq = emit_num_required(l);
284
numreq = emit_num_required(l);
287
/* Add the holder for the pointer to the function to be opened */
288
if (load_libraries) {
289
Wrapper_add_local(f, "_function_loaded", "static int _function_loaded=(1==0)");
290
Wrapper_add_local(f, "_the_function", "static void *_the_function=NULL");
292
String *parms = ParmList_protostr(l);
293
String *func = NewStringf("(*caller)(%s)", parms);
294
Wrapper_add_local(f, "caller", SwigType_lstr(d, func)); /*"(*caller)()")); */
259
299
// adds local variables
260
300
Wrapper_add_local(f, "lenv", "int lenv = 1");
261
301
Wrapper_add_local(f, "values", "Scheme_Object *values[MAXVALUES]");
304
if (load_libraries) {
305
Printf(f->code, "if (!_function_loaded) { _the_function=mz_load_function(\"%s\");_function_loaded=(1==1); }\n", iname);
306
Printf(f->code, "if (!_the_function) { scheme_signal_error(\"Cannot load C function '%s'\"); }\n", iname);
307
Printf(f->code, "caller=_the_function;\n");
263
311
// Now write code to extract the parameters (this is super ugly)
265
313
for (i = 0, p = l; i < numargs; i++) {
266
314
/* Skip ignored arguments */
268
while (checkAttribute(p,"tmap:in:numinputs","0")) {
269
p = Getattr(p,"tmap:in:next");
316
while (checkAttribute(p, "tmap:in:numinputs", "0")) {
317
p = Getattr(p, "tmap:in:next");
272
SwigType *pt = Getattr(p,"type");
273
String *ln = Getattr(p,"lname");
320
SwigType *pt = Getattr(p, "type");
321
String *ln = Getattr(p, "lname");
275
323
// Produce names of source and target
279
327
Printf(source, "argv[%d]", i);
280
Printf(target, "%s",ln);
281
Printv(arg, Getattr(p,"name"),NIL);
328
Printf(target, "%s", ln);
329
Printv(arg, Getattr(p, "name"), NIL);
283
331
if (i >= numreq) {
284
Printf(f->code,"if (argc > %d) {\n",i);
332
Printf(f->code, "if (argc > %d) {\n", i);
286
334
// Handle parameter types.
287
if ((tm = Getattr(p,"tmap:in"))) {
288
Replaceall(tm,"$source",source);
289
Replaceall(tm,"$target",target);
290
Replaceall(tm,"$input",source);
291
Setattr(p,"emit:input",source);
335
if ((tm = Getattr(p, "tmap:in"))) {
336
Replaceall(tm, "$source", source);
337
Replaceall(tm, "$target", target);
338
Replaceall(tm, "$input", source);
339
Setattr(p, "emit:input", source);
292
340
Printv(f->code, tm, "\n", NIL);
293
p = Getattr(p,"tmap:in:next");
341
p = Getattr(p, "tmap:in:next");
295
343
// no typemap found
296
344
// check if typedef and resolve
297
throw_unhandled_mzscheme_type_error (pt);
345
throw_unhandled_mzscheme_type_error(pt);
298
346
p = nextSibling(p);
300
348
if (i >= numreq) {
301
Printf(f->code,"}\n");
349
Printf(f->code, "}\n");
305
353
/* Insert constraint checking code */
306
354
for (p = l; p;) {
307
if ((tm = Getattr(p,"tmap:check"))) {
308
Replaceall(tm,"$target",Getattr(p,"lname"));
309
Printv(f->code,tm,"\n",NIL);
310
p = Getattr(p,"tmap:check:next");
355
if ((tm = Getattr(p, "tmap:check"))) {
356
Replaceall(tm, "$target", Getattr(p, "lname"));
357
Printv(f->code, tm, "\n", NIL);
358
p = Getattr(p, "tmap:check:next");
312
360
p = nextSibling(p);
316
364
// Pass output arguments back to the caller.
318
366
for (p = l; p;) {
319
if ((tm = Getattr(p,"tmap:argout"))) {
320
Replaceall(tm,"$source",Getattr(p,"emit:input")); /* Deprecated */
321
Replaceall(tm,"$target",Getattr(p,"lname")); /* Deprecated */
322
Replaceall(tm,"$arg",Getattr(p,"emit:input"));
323
Replaceall(tm,"$input",Getattr(p,"emit:input"));
324
Printv(outarg,tm,"\n",NIL);
325
p = Getattr(p,"tmap:argout:next");
367
if ((tm = Getattr(p, "tmap:argout"))) {
368
Replaceall(tm, "$source", Getattr(p, "emit:input")); /* Deprecated */
369
Replaceall(tm, "$target", Getattr(p, "lname")); /* Deprecated */
370
Replaceall(tm, "$arg", Getattr(p, "emit:input"));
371
Replaceall(tm, "$input", Getattr(p, "emit:input"));
372
Printv(outarg, tm, "\n", NIL);
373
p = Getattr(p, "tmap:argout:next");
328
376
p = nextSibling(p);
332
380
// Free up any memory allocated for the arguments.
334
382
/* Insert cleanup code */
335
383
for (p = l; p;) {
336
if ((tm = Getattr(p,"tmap:freearg"))) {
337
Replaceall(tm,"$target",Getattr(p,"lname"));
338
Printv(cleanup,tm,"\n",NIL);
339
p = Getattr(p,"tmap:freearg:next");
384
if ((tm = Getattr(p, "tmap:freearg"))) {
385
Replaceall(tm, "$target", Getattr(p, "lname"));
386
Printv(cleanup, tm, "\n", NIL);
387
p = Getattr(p, "tmap:freearg:next");
341
389
p = nextSibling(p);
345
393
// Now write code to make the function call
349
397
// Now have return value, figure out what to do with it.
351
if ((tm = Swig_typemap_lookup_new("out",n,"result",0))) {
352
Replaceall(tm,"$source","result");
353
Replaceall(tm,"$target","values[0]");
354
Replaceall(tm,"$result","values[0]");
399
if ((tm = Swig_typemap_lookup_new("out", n, "result", 0))) {
400
Replaceall(tm, "$source", "result");
401
Replaceall(tm, "$target", "values[0]");
402
Replaceall(tm, "$result", "values[0]");
355
403
if (GetFlag(n, "feature:new"))
356
Replaceall(tm, "$owner", "1");
404
Replaceall(tm, "$owner", "1");
358
Replaceall(tm, "$owner", "0");
359
Printv(f->code, tm, "\n",NIL);
406
Replaceall(tm, "$owner", "0");
407
Printv(f->code, tm, "\n", NIL);
361
throw_unhandled_mzscheme_type_error (d);
409
throw_unhandled_mzscheme_type_error(d);
364
412
// Dump the argument output code
365
Printv(f->code, Char(outarg),NIL);
413
Printv(f->code, Char(outarg), NIL);
367
415
// Dump the argument cleanup code
368
Printv(f->code, Char(cleanup),NIL);
416
Printv(f->code, Char(cleanup), NIL);
370
418
// Look for any remaining cleanup
372
if (GetFlag(n,"feature:new")) {
373
if ((tm = Swig_typemap_lookup_new("newfree",n,"result",0))) {
374
Replaceall(tm,"$source","result");
375
Printv(f->code, tm, "\n",NIL);
420
if (GetFlag(n, "feature:new")) {
421
if ((tm = Swig_typemap_lookup_new("newfree", n, "result", 0))) {
422
Replaceall(tm, "$source", "result");
423
Printv(f->code, tm, "\n", NIL);
379
426
// Free any memory allocated by the function being wrapped..
381
if ((tm = Swig_typemap_lookup_new("ret",n,"result",0))) {
382
Replaceall(tm,"$source","result");
383
Printv(f->code, tm, "\n",NIL);
428
if ((tm = Swig_typemap_lookup_new("ret", n, "result", 0))) {
429
Replaceall(tm, "$source", "result");
430
Printv(f->code, tm, "\n", NIL);
386
432
// Wrap things up (in a manner of speaking)
388
434
Printv(f->code, tab4, "return SWIG_MzScheme_PackageValues(lenv, values);\n", NIL);
389
435
Printf(f->code, "#undef FUNC_NAME\n");
390
Printv(f->code, "}\n",NIL);
436
Printv(f->code, "}\n", NIL);
438
/* Substitute the function name */
439
Replaceall(f->code, "$symname", iname);
392
441
Wrapper_print(f, f_wrappers);
394
if (!Getattr(n,"sym:overloaded")) {
443
if (!Getattr(n, "sym:overloaded")) {
396
445
// Now register the function
398
447
sprintf(temp, "%d", numargs);
399
448
if (exporting_destructor) {
400
Printf(init_func_def, "SWIG_TypeClientData(SWIGTYPE%s, (void *) %s);\n", swigtype_ptr, wname);
449
Printf(init_func_def, "SWIG_TypeClientData(SWIGTYPE%s, (void *) %s);\n", swigtype_ptr, wname);
402
Printf(init_func_def, "scheme_add_global(\"%s\", scheme_make_prim_w_arity(%s,\"%s\",%d,%d),menv);\n",
403
proc_name, wname, proc_name, numreq, numargs);
451
Printf(init_func_def, "scheme_add_global(\"%s\", scheme_make_prim_w_arity(%s,\"%s\",%d,%d),menv);\n", proc_name, wname, proc_name, numreq, numargs);
406
if (!Getattr(n,"sym:nextSibling")) {
454
if (!Getattr(n, "sym:nextSibling")) {
407
455
/* Emit overloading dispatch function */
410
String *dispatch = Swig_overload_dispatch(n,"return %s(argc,argv);",&maxargs);
458
String *dispatch = Swig_overload_dispatch(n, "return %s(argc,argv);", &maxargs);
412
460
/* Generate a dispatch wrapper for all overloaded functions */
414
Wrapper *df = NewWrapper();
415
String *dname = Swig_name_wrapper(iname);
462
Wrapper *df = NewWrapper();
463
String *dname = Swig_name_wrapper(iname);
418
"static Scheme_Object *\n", dname,
419
"(int argc, Scheme_Object **argv) {",
421
Printv(df->code,dispatch,"\n",NIL);
422
Printf(df->code,"scheme_signal_error(\"No matching function for overloaded '%s'\");\n", iname);
423
Printv(df->code,"}\n",NIL);
424
Wrapper_print(df,f_wrappers);
425
Printf(init_func_def, "scheme_add_global(\"%s\", scheme_make_prim_w_arity(%s,\"%s\",%d,%d),menv);\n",
426
proc_name, dname, proc_name, 0, maxargs);
465
Printv(df->def, "static Scheme_Object *\n", dname, "(int argc, Scheme_Object **argv) {", NIL);
466
Printv(df->code, dispatch, "\n", NIL);
467
Printf(df->code, "scheme_signal_error(\"No matching function for overloaded '%s'\");\n", iname);
468
Printv(df->code, "}\n", NIL);
469
Wrapper_print(df, f_wrappers);
470
Printf(init_func_def, "scheme_add_global(\"%s\", scheme_make_prim_w_arity(%s,\"%s\",%d,%d),menv);\n", proc_name, dname, proc_name, 0, maxargs);
428
472
Delete(dispatch);
433
477
Delete(proc_name);
453
497
* ------------------------------------------------------------ */
455
virtual int variableWrapper(Node *n) {
457
char *name = GetChar(n,"name");
458
char *iname = GetChar(n,"sym:name");
459
SwigType *t = Getattr(n,"type");
499
virtual int variableWrapper(Node *n) {
501
char *name = GetChar(n, "name");
502
char *iname = GetChar(n, "sym:name");
503
SwigType *t = Getattr(n, "type");
461
505
String *proc_name = NewString("");
464
507
String *tm2 = NewString("");;
465
508
String *argnum = NewString("0");
466
509
String *arg = NewString("argv[0]");
469
if (!addSymbol(iname,n)) return SWIG_ERROR;
512
if (!addSymbol(iname, n))
471
515
f = NewWrapper();
473
517
// evaluation function names
475
strcpy(var_name, Char(Swig_name_wrapper(iname)));
518
String *var_name = Swig_name_wrapper(iname);
477
520
// Build the name for scheme.
478
Printv(proc_name, iname,NIL);
521
Printv(proc_name, iname, NIL);
479
522
Replaceall(proc_name, "_", "-");
481
524
if ((SwigType_type(t) != T_USER) || (is_a_pointer(t))) {
483
Printf (f->def, "static Scheme_Object *%s(int argc, Scheme_Object** argv) {\n", var_name);
526
Printf(f->def, "static Scheme_Object *%s(int argc, Scheme_Object** argv) {\n", var_name);
484
527
Printv(f->def, "#define FUNC_NAME \"", proc_name, "\"", NIL);
486
Wrapper_add_local (f, "swig_result", "Scheme_Object *swig_result");
488
if (!GetFlag(n,"feature:immutable")) {
529
Wrapper_add_local(f, "swig_result", "Scheme_Object *swig_result");
531
if (!GetFlag(n, "feature:immutable")) {
489
532
/* Check for a setting of the variable value */
490
Printf (f->code, "if (argc) {\n");
491
if ((tm = Swig_typemap_lookup_new("varin",n,name,0))) {
492
Replaceall(tm,"$source","argv[0]");
493
Replaceall(tm,"$target",name);
494
Replaceall(tm,"$input","argv[0]");
533
Printf(f->code, "if (argc) {\n");
534
if ((tm = Swig_typemap_lookup_new("varin", n, name, 0))) {
535
Replaceall(tm, "$source", "argv[0]");
536
Replaceall(tm, "$target", name);
537
Replaceall(tm, "$input", "argv[0]");
495
538
/* Printv(f->code, tm, "\n",NIL); */
496
539
emit_action_code(n, f, tm);
499
throw_unhandled_mzscheme_type_error (t);
501
Printf (f->code, "}\n");
541
throw_unhandled_mzscheme_type_error(t);
543
Printf(f->code, "}\n");
504
545
// Now return the value of the variable (regardless
505
546
// of evaluating or setting)
507
if ((tm = Swig_typemap_lookup_new("varout",n,name,0))) {
508
Replaceall(tm,"$source",name);
509
Replaceall(tm,"$target","swig_result");
510
Replaceall(tm,"$result","swig_result");
548
if ((tm = Swig_typemap_lookup_new("varout", n, name, 0))) {
549
Replaceall(tm, "$source", name);
550
Replaceall(tm, "$target", "swig_result");
551
Replaceall(tm, "$result", "swig_result");
511
552
/* Printf (f->code, "%s\n", tm); */
512
553
emit_action_code(n, f, tm);
515
throw_unhandled_mzscheme_type_error (t);
517
Printf (f->code, "\nreturn swig_result;\n");
518
Printf (f->code, "#undef FUNC_NAME\n");
519
Printf (f->code, "}\n");
521
Wrapper_print (f, f_wrappers);
555
throw_unhandled_mzscheme_type_error(t);
557
Printf(f->code, "\nreturn swig_result;\n");
558
Printf(f->code, "#undef FUNC_NAME\n");
559
Printf(f->code, "}\n");
561
Wrapper_print(f, f_wrappers);
523
563
// Now add symbol to the MzScheme interpreter
525
565
Printv(init_func_def,
526
"scheme_add_global(\"",
528
"\", scheme_make_prim_w_arity(",
566
"scheme_add_global(\"", proc_name, "\", scheme_make_prim_w_arity(", var_name, ", \"", proc_name, "\", ", "0", ", ", "1", "), menv);\n", NIL);
539
Swig_warning(WARN_TYPEMAP_VAR_UNDEF, input_file, line_number,
540
"Unsupported variable type %s (ignored).\n", SwigType_str(t,0));
569
Swig_warning(WARN_TYPEMAP_VAR_UNDEF, input_file, line_number, "Unsupported variable type %s (ignored).\n", SwigType_str(t, 0));
542
572
Delete(proc_name);
552
582
* ------------------------------------------------------------ */
554
584
virtual int constantWrapper(Node *n) {
555
char *name = GetChar(n,"name");
556
char *iname = GetChar(n,"sym:name");
557
SwigType *type = Getattr(n,"type");
558
String *value = Getattr(n,"value");
585
char *name = GetChar(n, "name");
586
char *iname = GetChar(n, "sym:name");
587
SwigType *type = Getattr(n, "type");
588
String *value = Getattr(n, "value");
560
590
String *var_name = NewString("");
561
591
String *proc_name = NewString("");
562
592
String *rvalue = NewString("");
563
593
String *temp = NewString("");
566
596
// Make a static variable;
568
Printf (var_name, "_wrap_const_%s", Swig_name_mangle(Getattr(n,"sym:name")));
598
Printf(var_name, "_wrap_const_%s", Swig_name_mangle(Getattr(n, "sym:name")));
570
600
// Build the name for scheme.
571
Printv(proc_name, iname,NIL);
601
Printv(proc_name, iname, NIL);
572
602
Replaceall(proc_name, "_", "-");
574
604
if ((SwigType_type(type) == T_USER) && (!is_a_pointer(type))) {
575
Swig_warning(WARN_TYPEMAP_CONST_UNDEF, input_file, line_number,
576
"Unsupported constant value.\n");
605
Swig_warning(WARN_TYPEMAP_CONST_UNDEF, input_file, line_number, "Unsupported constant value.\n");
577
606
return SWIG_NOWRAP;
580
608
// See if there's a typemap
582
Printv(rvalue, value,NIL);
610
Printv(rvalue, value, NIL);
583
611
if ((SwigType_type(type) == T_CHAR) && (is_a_pointer(type) == 1)) {
584
612
temp = Copy(rvalue);
586
Printv(rvalue, "\"", temp, "\"",NIL);
614
Printv(rvalue, "\"", temp, "\"", NIL);
588
616
if ((SwigType_type(type) == T_CHAR) && (is_a_pointer(type) == 0)) {
590
618
temp = Copy(rvalue);
592
Printv(rvalue, "'", temp, "'",NIL);
620
Printv(rvalue, "'", temp, "'", NIL);
594
if ((tm = Swig_typemap_lookup_new("constant",n,name,0))) {
595
Replaceall(tm,"$source",rvalue);
596
Replaceall(tm,"$value",rvalue);
597
Replaceall(tm,"$target",name);
598
Printf (f_init, "%s\n", tm);
622
if ((tm = Swig_typemap_lookup_new("constant", n, name, 0))) {
623
Replaceall(tm, "$source", rvalue);
624
Replaceall(tm, "$value", rvalue);
625
Replaceall(tm, "$target", name);
626
Printf(f_init, "%s\n", tm);
600
628
// Create variable and assign it a value
602
Printf (f_header, "static %s = ", SwigType_lstr(type,var_name));
630
Printf(f_header, "static %s = ", SwigType_lstr(type, var_name));
603
631
if ((SwigType_type(type) == T_STRING)) {
604
Printf (f_header, "\"%s\";\n", value);
632
Printf(f_header, "\"%s\";\n", value);
605
633
} else if (SwigType_type(type) == T_CHAR) {
606
Printf (f_header, "\'%s\';\n", value);
634
Printf(f_header, "\'%s\';\n", value);
608
Printf (f_header, "%s;\n", value);
636
Printf(f_header, "%s;\n", value);
611
639
// Now create a variable declaration
614
642
/* Hack alert: will cleanup later -- Dave */
615
643
Node *n = NewHash();
616
Setattr(n,"name",var_name);
617
Setattr(n,"sym:name",iname);
618
Setattr(n,"type", type);
619
SetFlag(n,"feature:immutable");
644
Setattr(n, "name", var_name);
645
Setattr(n, "sym:name", iname);
646
Setattr(n, "type", type);
647
SetFlag(n, "feature:immutable");
620
648
variableWrapper(n);
639
667
* ------------------------------------------------------------ */
640
668
virtual int classHandler(Node *n) {
641
String *mangled_classname = 0;
642
String *real_classname = 0;
643
String *scm_structname = NewString("");
644
SwigType *ctype_ptr = NewStringf("p.%s", Getattr(n, "classtype"));
646
SwigType *t = NewStringf("p.%s", Getattr(n, "name"));
647
swigtype_ptr = SwigType_manglestr(t);
650
cls_swigtype = SwigType_manglestr(Getattr(n, "name"));
653
fieldnames_tab = NewString("");
654
convert_tab = NewString("");
655
convert_proto_tab = NewString("");
657
struct_name = Getattr(n,"sym:name");
658
mangled_struct_name = Swig_name_mangle(Getattr(n,"sym:name"));
660
Printv(scm_structname, struct_name, NIL);
661
Replaceall(scm_structname, "_", "-");
663
real_classname = Getattr(n,"name");
664
mangled_classname = Swig_name_mangle(real_classname);
666
Printv(fieldnames_tab, "static const char *_swig_struct_",
667
cls_swigtype, "_field_names[] = { \n", NIL);
669
Printv(convert_proto_tab, "static Scheme_Object *_swig_convert_struct_",
670
cls_swigtype, "(", SwigType_str(ctype_ptr, "ptr"), ");\n", NIL);
672
Printv(convert_tab, "static Scheme_Object *_swig_convert_struct_",
673
cls_swigtype, "(", SwigType_str(ctype_ptr, "ptr"), ")\n {\n",
677
tab4, "Scheme_Object *obj;\n",
678
tab4, "Scheme_Object *fields[_swig_struct_", cls_swigtype,
679
"_field_names_cnt];\n",
680
tab4, "int i = 0;\n\n", NIL);
682
/* Generate normal wrappers */
683
Language::classHandler(n);
685
Printv(convert_tab, tab4, "obj = scheme_make_struct_instance(",
686
"_swig_struct_type_", cls_swigtype, ", i, fields);\n", NIL);
687
Printv(convert_tab, tab4, "return obj;\n}\n\n", NIL);
689
Printv(fieldnames_tab, "};\n", NIL);
691
Printv(f_header, "static Scheme_Object *_swig_struct_type_",
692
cls_swigtype, ";\n", NIL);
694
Printv(f_header, fieldnames_tab, NIL);
695
Printv(f_header, "#define _swig_struct_", cls_swigtype,
696
"_field_names_cnt (sizeof(_swig_struct_", cls_swigtype,
697
"_field_names)/sizeof(char*))\n", NIL);
699
Printv(f_header, convert_proto_tab, NIL);
700
Printv(f_wrappers, convert_tab, NIL);
702
Printv(init_func_def, "_swig_struct_type_", cls_swigtype,
703
" = SWIG_MzScheme_new_scheme_struct(menv, \"", scm_structname, "\", ",
704
"_swig_struct_", cls_swigtype, "_field_names_cnt,",
705
"(char**) _swig_struct_", cls_swigtype, "_field_names);\n",
708
Delete(mangled_classname);
709
Delete(swigtype_ptr);
711
Delete(fieldnames_tab);
714
Delete(convert_proto_tab);
716
mangled_struct_name = 0;
717
Delete(cls_swigtype);
669
String *mangled_classname = 0;
670
String *real_classname = 0;
671
String *scm_structname = NewString("");
672
SwigType *ctype_ptr = NewStringf("p.%s", Getattr(n, "classtype"));
674
SwigType *t = NewStringf("p.%s", Getattr(n, "name"));
675
swigtype_ptr = SwigType_manglestr(t);
678
cls_swigtype = SwigType_manglestr(Getattr(n, "name"));
681
fieldnames_tab = NewString("");
682
convert_tab = NewString("");
683
convert_proto_tab = NewString("");
685
struct_name = Getattr(n, "sym:name");
686
mangled_struct_name = Swig_name_mangle(Getattr(n, "sym:name"));
688
Printv(scm_structname, struct_name, NIL);
689
Replaceall(scm_structname, "_", "-");
691
real_classname = Getattr(n, "name");
692
mangled_classname = Swig_name_mangle(real_classname);
694
Printv(fieldnames_tab, "static const char *_swig_struct_", cls_swigtype, "_field_names[] = { \n", NIL);
696
Printv(convert_proto_tab, "static Scheme_Object *_swig_convert_struct_", cls_swigtype, "(", SwigType_str(ctype_ptr, "ptr"), ");\n", NIL);
698
Printv(convert_tab, "static Scheme_Object *_swig_convert_struct_", cls_swigtype, "(", SwigType_str(ctype_ptr, "ptr"), ")\n {\n", NIL);
701
tab4, "Scheme_Object *obj;\n", tab4, "Scheme_Object *fields[_swig_struct_", cls_swigtype, "_field_names_cnt];\n", tab4, "int i = 0;\n\n", NIL);
703
/* Generate normal wrappers */
704
Language::classHandler(n);
706
Printv(convert_tab, tab4, "obj = scheme_make_struct_instance(", "_swig_struct_type_", cls_swigtype, ", i, fields);\n", NIL);
707
Printv(convert_tab, tab4, "return obj;\n}\n\n", NIL);
709
Printv(fieldnames_tab, "};\n", NIL);
711
Printv(f_header, "static Scheme_Object *_swig_struct_type_", cls_swigtype, ";\n", NIL);
713
Printv(f_header, fieldnames_tab, NIL);
714
Printv(f_header, "#define _swig_struct_", cls_swigtype, "_field_names_cnt (sizeof(_swig_struct_", cls_swigtype, "_field_names)/sizeof(char*))\n", NIL);
716
Printv(f_header, convert_proto_tab, NIL);
717
Printv(f_wrappers, convert_tab, NIL);
719
Printv(init_func_def, "_swig_struct_type_", cls_swigtype,
720
" = SWIG_MzScheme_new_scheme_struct(menv, \"", scm_structname, "\", ",
721
"_swig_struct_", cls_swigtype, "_field_names_cnt,", "(char**) _swig_struct_", cls_swigtype, "_field_names);\n", NIL);
723
Delete(mangled_classname);
724
Delete(swigtype_ptr);
726
Delete(fieldnames_tab);
729
Delete(convert_proto_tab);
731
mangled_struct_name = 0;
732
Delete(cls_swigtype);
723
/* ------------------------------------------------------------
724
* membervariableHandler()
725
* ------------------------------------------------------------ */
727
virtual int membervariableHandler(Node *n) {
728
Language::membervariableHandler(n);
730
if (!is_smart_pointer()) {
731
String *symname = Getattr(n, "sym:name");
732
String *name = Getattr(n, "name");
733
SwigType *type = Getattr(n, "type");
734
String *swigtype = SwigType_manglestr(Getattr(n, "type"));
736
String *access_mem = NewString("");
737
SwigType *ctype_ptr = NewStringf("p.%s", Getattr(n, "type"));
739
Printv(fieldnames_tab, tab4, "\"", symname, "\",\n", NIL);
740
Printv(access_mem, "(ptr)->", name, NIL);
741
if ((SwigType_type(type) == T_USER) && (!is_a_pointer(type))) {
742
Printv(convert_tab, tab4, "fields[i++] = ", NIL);
743
Printv(convert_tab, "_swig_convert_struct_", swigtype,
744
"((", SwigType_str(ctype_ptr, ""), ")&((ptr)->",
746
} else if ((tm = Swig_typemap_lookup_new("varout",n,access_mem,0))) {
747
Replaceall(tm,"$result","fields[i++]");
748
Printv(convert_tab, tm, "\n", NIL);
750
Swig_warning(WARN_TYPEMAP_VAR_UNDEF, input_file, line_number,
751
"Unsupported member variable type %s (ignored).\n",
752
SwigType_str(type,0));
738
/* ------------------------------------------------------------
739
* membervariableHandler()
740
* ------------------------------------------------------------ */
742
virtual int membervariableHandler(Node *n) {
743
Language::membervariableHandler(n);
745
if (!is_smart_pointer()) {
746
String *symname = Getattr(n, "sym:name");
747
String *name = Getattr(n, "name");
748
SwigType *type = Getattr(n, "type");
749
String *swigtype = SwigType_manglestr(Getattr(n, "type"));
751
String *access_mem = NewString("");
752
SwigType *ctype_ptr = NewStringf("p.%s", Getattr(n, "type"));
754
Printv(fieldnames_tab, tab4, "\"", symname, "\",\n", NIL);
755
Printv(access_mem, "(ptr)->", name, NIL);
756
if ((SwigType_type(type) == T_USER) && (!is_a_pointer(type))) {
757
Printv(convert_tab, tab4, "fields[i++] = ", NIL);
758
Printv(convert_tab, "_swig_convert_struct_", swigtype, "((", SwigType_str(ctype_ptr, ""), ")&((ptr)->", name, "));\n", NIL);
759
} else if ((tm = Swig_typemap_lookup_new("varout", n, access_mem, 0))) {
760
Replaceall(tm, "$result", "fields[i++]");
761
Printv(convert_tab, tm, "\n", NIL);
763
Swig_warning(WARN_TYPEMAP_VAR_UNDEF, input_file, line_number, "Unsupported member variable type %s (ignored).\n", SwigType_str(type, 0));
760
771
/* ------------------------------------------------------------
761
772
* validIdentifer()
762
773
* ------------------------------------------------------------ */
764
775
virtual int validIdentifier(String *s) {
765
776
char *c = Char(s);
766
/* Check whether we have an R5RS identifier.*/
777
/* Check whether we have an R5RS identifier. */
767
778
/* <identifier> --> <initial> <subsequent>* | <peculiar identifier> */
768
779
/* <initial> --> <letter> | <special initial> */
769
780
if (!(isalpha(*c) || (*c == '!') || (*c == '$') || (*c == '%')