140
140
Swig_register_filebyname("init", f_init);
141
141
Swig_register_filebyname("classInit", f_classInit);
143
/* Standard stuff for the SWIG runtime section */
143
/* Standard stuff for the SWIG runtime section */
144
144
Swig_banner(f_runtime);
146
146
Printf(f_header, "#define SWIG_init pike_module_init\n");
147
147
Printf(f_header, "#define SWIG_name \"%s\"\n\n", module);
149
149
/* Change naming scheme for constructors and destructors */
150
Swig_name_register("construct","%c_create");
151
Swig_name_register("destroy","%c_destroy");
150
Swig_name_register("construct", "%c_create");
151
Swig_name_register("destroy", "%c_destroy");
153
153
/* Current wrap type */
154
154
current = NO_CPP;
156
156
/* Emit code for children */
157
157
Language::top(n);
159
159
/* Close the initialization function */
160
160
Printf(f_init, "}\n");
161
161
SwigType_emit_type_table(f_runtime, f_wrappers);
163
163
/* Close all of the files */
164
164
Dump(f_header, f_runtime);
165
165
Dump(f_wrappers, f_runtime);
235
237
void add_method(const DOHString_or_char *name, const DOHString_or_char *function, const DOHString_or_char *description) {
236
238
String *rename = NULL;
237
239
switch (current) {
239
rename = NewString(name);
240
Printf(f_init, "ADD_FUNCTION(\"%s\", %s, tFunc(%s), 0);\n", rename, function, description);
244
rename = NewString(name);
245
Printf(f_init, "ADD_FUNCTION(\"%s\", %s, tFunc(%s), 0);\n", rename, function, description);
251
rename = strip(name);
252
Printf(f_classInit, "ADD_FUNCTION(\"%s\", %s, tFunc(%s), 0);\n", rename, function, description);
255
assert(false); // shouldn't have gotten here for CLASS_CONST nodes
257
assert(false); // what is this?
241
rename = NewString(name);
242
Printf(f_init, "ADD_FUNCTION(\"%s\", %s, tFunc(%s), 0);\n", rename, function, description);
246
rename = NewString(name);
247
Printf(f_init, "ADD_FUNCTION(\"%s\", %s, tFunc(%s), 0);\n", rename, function, description);
253
rename = strip(name);
254
Printf(f_classInit, "ADD_FUNCTION(\"%s\", %s, tFunc(%s), 0);\n", rename, function, description);
257
assert(false); // shouldn't have gotten here for CLASS_CONST nodes
259
assert(false); // what is this?
268
270
virtual int functionWrapper(Node *n) {
270
String *name = Getattr(n,"name");
271
String *iname = Getattr(n,"sym:name");
272
SwigType *d = Getattr(n,"type");
273
ParmList *l = Getattr(n,"parms");
272
String *name = Getattr(n, "name");
273
String *iname = Getattr(n, "sym:name");
274
SwigType *d = Getattr(n, "type");
275
ParmList *l = Getattr(n, "parms");
279
281
String *overname = 0;
280
if (Getattr(n,"sym:overloaded")) {
281
overname = Getattr(n,"sym:overname");
282
if (Getattr(n, "sym:overloaded")) {
283
overname = Getattr(n, "sym:overname");
283
if (!addSymbol(iname,n)) return SWIG_ERROR;
285
if (!addSymbol(iname, n))
286
289
Wrapper *f = NewWrapper();
288
291
/* Write code to extract function parameters. */
289
292
emit_args(d, l, f);
291
294
/* Attach the standard typemaps */
292
emit_attach_parmmaps(l,f);
293
Setattr(n,"wrap:parms",l);
295
emit_attach_parmmaps(l, f);
296
Setattr(n, "wrap:parms", l);
295
298
/* Get number of required and total arguments */
296
299
int num_arguments = emit_num_arguments(l);
297
300
int varargs = emit_isvarargs(l);
299
302
/* Which input argument to start with? */
300
303
int start = (current == MEMBER_FUNC || current == MEMBER_VAR || current == DESTRUCTOR) ? 1 : 0;
302
/* Offset to skip over the attribute name */
305
/* Offset to skip over the attribute name */
303
306
// int offset = (current == MEMBER_VAR) ? 1 : 0;
306
309
String *wname = Swig_name_wrapper(iname);
308
Append(wname,overname);
311
Append(wname, overname);
311
314
Printv(f->def, "static void ", wname, "(INT32 args) {", NIL);
313
316
/* Generate code for argument marshalling */
314
317
String *description = NewString("");
316
319
for (i = 0, p = l; i < num_arguments; i++) {
318
while (checkAttribute(p,"tmap:in:numinputs","0")) {
319
p = Getattr(p,"tmap:in:next");
321
while (checkAttribute(p, "tmap:in:numinputs", "0")) {
322
p = Getattr(p, "tmap:in:next");
322
SwigType *pt = Getattr(p,"type");
323
String *ln = Getattr(p,"lname");
325
SwigType *pt = Getattr(p, "type");
326
String *ln = Getattr(p, "lname");
326
String *lstr = SwigType_lstr(pt,0);
327
Printf(f->code, "%s = (%s) THIS;\n", ln, lstr);
329
String *lstr = SwigType_lstr(pt, 0);
330
Printf(f->code, "%s = (%s) THIS;\n", ln, lstr);
330
333
/* Look for an input typemap */
331
sprintf(source, "Pike_sp[%d-args]", i-start+offset);
332
if ((tm = Getattr(p,"tmap:in"))) {
333
Replaceall(tm, "$source", source);
334
sprintf(source, "Pike_sp[%d-args]", i - start + offset);
335
if ((tm = Getattr(p, "tmap:in"))) {
336
Replaceall(tm, "$source", source);
334
337
Replaceall(tm, "$target", ln);
335
338
Replaceall(tm, "$input", source);
336
339
Setattr(p, "emit:input", source);
337
340
Printf(f->code, "%s\n", tm);
338
String *pikedesc = Getattr(p, "tmap:in:pikedesc");
341
String *pikedesc = Getattr(p, "tmap:in:pikedesc");
340
343
Printv(description, pikedesc, " ", NIL);
342
p = Getattr(p,"tmap:in:next");
345
p = Getattr(p, "tmap:in:next");
345
Swig_warning(WARN_TYPEMAP_IN_UNDEF, input_file, line_number,
346
"Unable to use type %s as a function argument.\n",SwigType_str(pt,0));
348
Swig_warning(WARN_TYPEMAP_IN_UNDEF, input_file, line_number, "Unable to use type %s as a function argument.\n", SwigType_str(pt, 0));
353
355
/* Check for trailing varargs */
355
if (p && (tm = Getattr(p,"tmap:in"))) {
356
Replaceall(tm,"$input", "varargs");
357
Printv(f->code,tm,"\n",NIL);
357
if (p && (tm = Getattr(p, "tmap:in"))) {
358
Replaceall(tm, "$input", "varargs");
359
Printv(f->code, tm, "\n", NIL);
361
363
/* Insert constraint checking code */
362
364
for (p = l; p;) {
363
if ((tm = Getattr(p,"tmap:check"))) {
364
Replaceall(tm,"$target",Getattr(p,"lname"));
365
Printv(f->code,tm,"\n",NIL);
366
p = Getattr(p,"tmap:check:next");
365
if ((tm = Getattr(p, "tmap:check"))) {
366
Replaceall(tm, "$target", Getattr(p, "lname"));
367
Printv(f->code, tm, "\n", NIL);
368
p = Getattr(p, "tmap:check:next");
368
370
p = nextSibling(p);
372
374
/* Insert cleanup code */
373
375
String *cleanup = NewString("");
374
376
for (p = l; p;) {
375
if ((tm = Getattr(p,"tmap:freearg"))) {
376
Replaceall(tm,"$source",Getattr(p,"lname"));
377
Printv(cleanup,tm,"\n",NIL);
378
p = Getattr(p,"tmap:freearg:next");
377
if ((tm = Getattr(p, "tmap:freearg"))) {
378
Replaceall(tm, "$source", Getattr(p, "lname"));
379
Printv(cleanup, tm, "\n", NIL);
380
p = Getattr(p, "tmap:freearg:next");
380
382
p = nextSibling(p);
384
386
/* Insert argument output code */
385
387
String *outarg = NewString("");
386
388
for (p = l; p;) {
387
if ((tm = Getattr(p,"tmap:argout"))) {
388
Replaceall(tm,"$source",Getattr(p,"lname"));
389
Replaceall(tm,"$target","resultobj");
390
Replaceall(tm,"$arg",Getattr(p,"emit:input"));
391
Replaceall(tm,"$input",Getattr(p,"emit:input"));
392
Printv(outarg,tm,"\n",NIL);
393
p = Getattr(p,"tmap:argout:next");
389
if ((tm = Getattr(p, "tmap:argout"))) {
390
Replaceall(tm, "$source", Getattr(p, "lname"));
391
Replaceall(tm, "$target", "resultobj");
392
Replaceall(tm, "$arg", Getattr(p, "emit:input"));
393
Replaceall(tm, "$input", Getattr(p, "emit:input"));
394
Printv(outarg, tm, "\n", NIL);
395
p = Getattr(p, "tmap:argout:next");
395
397
p = nextSibling(p);
399
401
/* Emit the function call */
402
404
/* Clear the return stack */
403
405
Printf(f->code, "pop_n_elems(args);\n");
412
414
// Wrapper_add_local(f, "resultobj", "struct object *resultobj");
413
415
Printv(description, ", ", NIL);
414
if ((tm = Swig_typemap_lookup_new("out",n,"result",0))) {
415
Replaceall(tm,"$source", "result");
416
Replaceall(tm,"$target", "resultobj");
417
Replaceall(tm,"$result", "resultobj");
418
if (GetFlag(n,"feature:new")) {
419
Replaceall(tm,"$owner","1");
416
if ((tm = Swig_typemap_lookup_new("out", n, "result", 0))) {
417
Replaceall(tm, "$source", "result");
418
Replaceall(tm, "$target", "resultobj");
419
Replaceall(tm, "$result", "resultobj");
420
if (GetFlag(n, "feature:new")) {
421
Replaceall(tm, "$owner", "1");
421
Replaceall(tm,"$owner","0");
423
Replaceall(tm, "$owner", "0");
423
425
String *pikedesc = Getattr(n, "tmap:out:pikedesc");
425
427
Printv(description, pikedesc, NIL);
427
Printf(f->code,"%s\n", tm);
429
Printf(f->code, "%s\n", tm);
429
Swig_warning(WARN_TYPEMAP_OUT_UNDEF, input_file, line_number,
430
"Unable to use return type %s in function %s.\n", SwigType_str(d,0), name);
431
Swig_warning(WARN_TYPEMAP_OUT_UNDEF, input_file, line_number, "Unable to use return type %s in function %s.\n", SwigType_str(d, 0), name);
434
435
/* Output argument output code */
435
Printv(f->code,outarg,NIL);
436
Printv(f->code, outarg, NIL);
437
438
/* Output cleanup code */
438
Printv(f->code,cleanup,NIL);
439
Printv(f->code, cleanup, NIL);
440
441
/* Look to see if there is any newfree cleanup code */
441
if (GetFlag(n,"feature:new")) {
442
if ((tm = Swig_typemap_lookup_new("newfree",n,"result",0))) {
443
Replaceall(tm,"$source","result");
444
Printf(f->code,"%s\n",tm);
442
if (GetFlag(n, "feature:new")) {
443
if ((tm = Swig_typemap_lookup_new("newfree", n, "result", 0))) {
444
Replaceall(tm, "$source", "result");
445
Printf(f->code, "%s\n", tm);
448
449
/* See if there is any return cleanup code */
449
450
if ((tm = Swig_typemap_lookup_new("ret", n, "result", 0))) {
450
Replaceall(tm,"$source","result");
451
Printf(f->code,"%s\n",tm);
451
Replaceall(tm, "$source", "result");
452
Printf(f->code, "%s\n", tm);
454
455
/* Close the function */
455
456
Printf(f->code, "}\n");
457
458
/* Substitute the cleanup code */
458
Replaceall(f->code,"$cleanup",cleanup);
459
Replaceall(f->code, "$cleanup", cleanup);
460
461
/* Substitute the function name */
461
Replaceall(f->code,"$symname",iname);
462
Replaceall(f->code,"$result","resultobj");
462
Replaceall(f->code, "$symname", iname);
463
Replaceall(f->code, "$result", "resultobj");
464
465
/* Dump the function out */
465
Wrapper_print(f,f_wrappers);
466
Wrapper_print(f, f_wrappers);
467
468
/* Now register the function with the interpreter. */
468
if (!Getattr(n,"sym:overloaded")) {
469
if (!Getattr(n, "sym:overloaded")) {
469
470
add_method(iname, wname, description);
471
Setattr(n,"wrap:name", wname);
472
if (!Getattr(n,"sym:nextSibling")) {
472
Setattr(n, "wrap:name", wname);
473
if (!Getattr(n, "sym:nextSibling")) {
473
474
dispatchFunction(n);
496
497
String *tmp = NewString("");
497
498
String *dispatch = Swig_overload_dispatch(n, "%s(args); return;", &maxargs);
499
500
/* Generate a dispatch wrapper for all overloaded functions */
501
Wrapper *f = NewWrapper();
502
String *symname = Getattr(n,"sym:name");
503
String *wname = Swig_name_wrapper(symname);
502
Wrapper *f = NewWrapper();
503
String *symname = Getattr(n, "sym:name");
504
String *wname = Swig_name_wrapper(symname);
505
506
Printf(f->def, "static void %s(INT32 args) {", wname);
507
508
Wrapper_add_local(f, "argc", "INT32 argc");
508
509
Printf(tmp, "struct svalue argv[%d]", maxargs);
509
510
Wrapper_add_local(f, "argv", tmp);
510
511
Wrapper_add_local(f, "ii", "INT32 ii");
512
513
Printf(f->code, "argc = args;\n");
513
514
Printf(f->code, "for (ii = 0; (ii < argc) && (ii < %d); ii++) {\n", maxargs);
514
515
Printf(f->code, "argv[ii] = Pike_sp[ii-args];\n");
515
516
Printf(f->code, "}\n");
517
518
Replaceall(dispatch, "$args", "self, args");
518
519
Printv(f->code, dispatch, "\n", NIL);
519
520
Printf(f->code, "Pike_error(\"No matching function for overloaded '%s'.\");\n", symname);
520
521
Printv(f->code, "}\n", NIL);
522
523
Wrapper_print(f, f_wrappers);
524
525
String *description = NewString("");
525
526
Printf(description, "tAny,");
526
527
if (current == CONSTRUCTOR || current == DESTRUCTOR) {
632
633
String *symname = Getattr(n, "sym:name");
633
634
if (!addSymbol(symname, n))
634
635
return SWIG_ERROR;
636
637
PrefixPlusUnderscore = NewStringf("%s_", getClassPrefix());
638
639
Printf(f_classInit, "start_new_program();\n");
640
641
/* Handle inheritance */
641
List *baselist = Getattr(n,"bases");
642
List *baselist = Getattr(n, "bases");
642
643
if (baselist && Len(baselist) > 0) {
643
644
Iterator base = First(baselist);
644
645
while (base.item) {
645
String *basename = Getattr(base.item,"name");
646
SwigType *basetype = NewString(basename);
647
SwigType_add_pointer(basetype);
648
SwigType_remember(basetype);
649
String *basemangle = SwigType_manglestr(basetype);
650
Printf(f_classInit, "low_inherit((struct program *) SWIGTYPE%s->clientdata, 0, 0, 0, 0, 0);\n", basemangle);
646
String *basename = Getattr(base.item, "name");
647
SwigType *basetype = NewString(basename);
648
SwigType_add_pointer(basetype);
649
SwigType_remember(basetype);
650
String *basemangle = SwigType_manglestr(basetype);
651
Printf(f_classInit, "low_inherit((struct program *) SWIGTYPE%s->clientdata, 0, 0, 0, 0, 0);\n", basemangle);
651
652
Delete(basemangle);
652
653
Delete(basetype);
656
657
Printf(f_classInit, "ADD_STORAGE(swig_object_wrapper);\n");
659
660
Language::classHandler(n);
661
662
/* Accessors for member variables */
663
List *membervariables = Getattr(n,"membervariables");
664
if (membervariables && Len(membervariables) > 0) {
665
membervariableAccessors(membervariables);
664
List *membervariables = Getattr(n,"membervariables");
665
if (membervariables && Len(membervariables) > 0) {
666
membervariableAccessors(membervariables);
669
670
/* Done, close the class and dump its definition to the init function */
670
671
Printf(f_classInit, "add_program_constant(\"%s\", pr = end_program(), 0);\n", symname);
671
672
Dump(f_classInit, f_init);
672
673
Clear(f_classInit);
674
675
SwigType *tt = NewString(symname);
675
676
SwigType_add_pointer(tt);
676
677
SwigType_remember(tt);