forked from jaeandersson/swig
-
Notifications
You must be signed in to change notification settings - Fork 1
/
matlab.cxx
3033 lines (2642 loc) · 95.9 KB
/
matlab.cxx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* -----------------------------------------------------------------------------
* This file is part of SWIG, which is licensed as a whole under version 3
* (or any later version) of the GNU General Public License. Some additional
* terms also apply to certain portions of SWIG. The full details of the SWIG
* license and copyrights can be found in the LICENSE and COPYRIGHT files
* included with the SWIG source code as distributed by the SWIG developers
* and at http://www.swig.org/legal.html.
*
* matlab.cxx
*
* Matlab language module for SWIG.
* ----------------------------------------------------------------------------- */
#include "swigmod.h"
#include "cparse.h"
static const char *usage = (char *) "\
Matlab Options (available with -matlab)\n\
-opprefix <str> - Set global operator functions prefix to <str> [default: 'op_']\n\
-pkgname <str> - Set package name prefix to <str> [default: '<module>']\n\
-mexname <name> - Set mex function name to <name> [default: '<module>MEX']\n\
\n";
class MATLAB:public Language {
public:
MATLAB();
virtual void main(int argc, char *argv[]);
virtual int top(Node *n);
virtual int functionWrapper(Node *n);
virtual int globalfunctionHandler(Node *n);
virtual int variableWrapper(Node *n);
virtual int constantWrapper(Node *n);
virtual int nativeWrapper(Node *n);
virtual int enumDeclaration(Node *n);
virtual int enumvalueDeclaration(Node *n);
virtual int classHandler(Node *n);
virtual int memberfunctionHandler(Node *n);
virtual int membervariableHandler(Node *n);
virtual int constructorHandler(Node *n);
virtual int destructorHandler(Node *n);
virtual int staticmemberfunctionHandler(Node *n);
virtual int memberconstantHandler(Node *n);
virtual int staticmembervariableHandler(Node *n);
virtual int insertDirective(Node *n);
virtual int importDirective(Node *n);
virtual String *runtimeCode();
virtual String *defaultExternalRuntimeFilename();
int classDirectorMethods(Node *n);
int classDirectorMethod(Node *n, Node *parent, String *super);
int classDirectorConstructor(Node *n);
int classDirectorDefaultConstructor(Node *n);
int classDirectorInit(Node *n);
int classDirectorEnd(Node *n);
int classDirectorDisown(Node *n);
protected:
File *f_wrap_m;
File *f_setup_m;
File *f_gateway;
File *f_constants;
File *f_begin;
File *f_runtime;
File *f_header;
File *f_doc;
File *f_wrappers;
File *f_init;
File *f_initbeforefunc;
File *f_directors;
File *f_directors_h;
File *f_runtime_h;
String *class_name;
String *mex_name;
String *base_init;
String *get_field;
String *set_field;
String *static_methods;
String *wrap_class_m;
// Current constant
int con_id;
// List of function names
List *l_fnames;
// List of constant names
List *l_cnames;
// List of dependent modules
List *l_modules;
bool have_constructor;
bool have_destructor;
//String *constructor_name;
int num_gateway;
int num_constant;
int director_method_index;
// Options
String *op_prefix;
String *pkg_name;
String *pkg_name_fullpath;
bool redirectoutput;
int no_header_file;
// Helper functions
static void nameUnnamedParams(ParmList *parms, bool all);
String *getOverloadedName(Node *n);
void initGateway();
int toGateway(String *fullname, String *wname);
void finalizeGateway();
void initConstant();
int toConstant(String *constname, String *constdef);
void finalizeConstant();
void createSwigRef();
void createSwigMem();
void createSwigGet();
void createSwigStorage();
void autodoc_to_m(File *f, Node *n);
void process_autodoc(Node *n);
void make_autodocParmList(Node *n, String *decl_str, String *args_str);
void addMissingParameterNames(ParmList *plist, int arg_offset);
String *convertValue(String *v, SwigType *t);
const char *get_implicitconv_flag(Node *n);
void dispatchFunction(Node *n);
static String *matlab_escape(String *_s);
void wrapConstructor(int gw_ind, String *symname, String *fullname, Node *n);
void wrapConstructorDirector(int gw_ind, String *symname, String *fullname, Node *n);
int getRangeNumReturns(Node *n, int &max_num_returns, int &min_num_returns);
void checkValidSymName(Node *node);
};
extern "C" Language *swig_matlab(void) {
return new MATLAB();
}
// Only implementations from here on
MATLAB::MATLAB():
f_wrap_m(0),
f_setup_m(0),
f_gateway(0),
f_constants(0),
f_begin(0),
f_runtime(0),
f_header(0),
f_doc(0),
f_wrappers(0),
f_init(0),
f_initbeforefunc(0),
f_directors(0),
f_directors_h(0),
class_name(0),
mex_name(0),
base_init(0),
get_field(0),
set_field(0),
static_methods(0),
wrap_class_m(0),
have_constructor(false),
have_destructor(false),
num_gateway(0),
director_method_index(0),
op_prefix(0),
pkg_name(0),
pkg_name_fullpath(0),
no_header_file(0) {
/* Add code to manage protected constructors and directors */
director_prot_ctor_code = NewString("");
Printv(director_prot_ctor_code,
"if ( argc==1 ) { /* subclassed */\n",
" $director_new \n",
"} else {\n", " mexErrMsgIdAndTxt(\"SWIG:RuntimeError\",\"accessing abstract class or protected constructor\"); \n", " SWIG_fail;\n", "}\n", NIL);
enable_cplus_runtime_mode();
allow_overloading();
director_multiple_inheritance = 1;
director_language = 1;
}
void MATLAB::main(int argc, char *argv[]) {
int cppcast = 1;
redirectoutput = false;
for (int i = 1; i < argc; i++) {
if (argv[i]) {
if (strcmp(argv[i], "-help") == 0) {
fputs(usage, stdout);
} else if (strcmp(argv[i], "-opprefix") == 0) {
if (argv[i + 1]) {
op_prefix = NewString(argv[i + 1]);
Swig_mark_arg(i);
Swig_mark_arg(i + 1);
i++;
} else {
Swig_arg_error();
}
} else if (strcmp(argv[i], "-cppcast") == 0) {
cppcast = 1;
Swig_mark_arg(i);
} else if (strcmp(argv[i], "-nocppcast") == 0) {
cppcast = 0;
Swig_mark_arg(i);
} else if (strcmp(argv[i], "-noh") == 0) {
no_header_file = 1;
Swig_mark_arg(i);
} else if (strcmp(argv[i], "-pkgname") == 0) {
if (argv[i + 1]) {
pkg_name = NewString(argv[i + 1]);
Swig_mark_arg(i);
Swig_mark_arg(i + 1);
i++;
} else {
Swig_arg_error();
}
} else if (strcmp(argv[i], "-mexname") == 0) {
if (argv[i + 1]) {
mex_name = NewString(argv[i + 1]);
Swig_mark_arg(i);
Swig_mark_arg(i + 1);
i++;
} else {
Swig_arg_error();
}
} else if (strcmp(argv[i], "-redirectoutput") == 0) {
redirectoutput = true;
Swig_mark_arg(i);
}
}
}
if (!op_prefix) {
op_prefix = NewString("op_");
}
SWIG_library_directory("matlab");
Preprocessor_define("SWIGMATLAB 1", 0);
if (cppcast) {
Preprocessor_define((DOH *) "SWIG_CPLUSPLUS_CAST", 0);
}
SWIG_config_file("matlab.swg");
SWIG_typemap_lang("matlab");
allow_overloading();
// Matlab API is C++, so output must be C++ compatibile even when wrapping C code
// if (!cparse_cplusplus)
// Swig_cparse_cplusplusout(1);
}
int MATLAB::top(Node *n) {
{
Node *mod = Getattr(n, "module");
if (mod) {
Node *options = Getattr(mod, "options");
if (options) {
int dirprot = 0;
if (Getattr(options, "dirprot")) {
dirprot = 1;
}
if (Getattr(options, "nodirprot")) {
dirprot = 0;
}
if (Getattr(options, "directors")) {
allow_directors();
if (dirprot)
allow_dirprot();
}
// Set package name via module option if not set via command-line
// Otherwise use module name as default (see below)
if (!pkg_name && Getattr(options, "package")) {
pkg_name = Getattr(options, "package");
}
// Set mex name via module option if not set via command-line
// Otherwise use module name as default (see below)
if (!mex_name && Getattr(options, "mexname")) {
mex_name = Getattr(options, "mexname");
}
}
}
}
/* Set comparison with none for ConstructorToFunction */
setSubclassInstanceCheck(NewString("!mxIsNumeric($arg)"));
// Create SwigRef base class
createSwigRef();
// Create SwigMem helper function
createSwigMem();
// Crete SwigGet helper function
createSwigGet();
String *module = Getattr(n, "name");
String *outfile = Getattr(n, "outfile");
/* Initialize all of the output files */
String *outfile_h = !no_header_file ? Getattr(n, "outfile_h") : 0;
// Add default package prefix
if (!pkg_name) {
pkg_name = Copy(module);
}
// List of dependent modules
l_modules = NewList();
// Create subdirectory
String *pkg_directory_name = NewString("+");
Append(pkg_directory_name, pkg_name);
(void) Replace(pkg_directory_name, ".", "/+", DOH_REPLACE_ANY);
pkg_name_fullpath = NewString("");
Printf(pkg_name_fullpath, "%s%s", SWIG_output_directory(), pkg_directory_name);
Swig_new_subdirectory((String *) SWIG_output_directory(), pkg_directory_name);
// Create output (mex) file
f_begin = NewFile(outfile, "w", SWIG_output_files());
if (!f_begin) {
FileErrorDisplay(outfile);
SWIG_exit(EXIT_FAILURE);
}
/* The name of the compiled mex-wrapper is modulenameMEX by default, but this
can be overridden by setting the mexname opion */
if (!mex_name) {
mex_name = NewString(module);
Append(mex_name, "MEX");
}
f_gateway = NewString("");
f_constants = NewString("");
f_runtime = NewString("");
f_header = NewString("");
f_doc = NewString("");
f_wrappers = NewString("");
f_init = NewString("");
f_initbeforefunc = NewString("");
f_directors_h = NewString("");
f_directors = NewString("");
if (directorsEnabled()) {
// Create SwigStorage helper function
createSwigStorage();
if (!no_header_file) {
f_runtime_h = NewFile(outfile_h, "w", SWIG_output_files());
if (!f_runtime_h) {
FileErrorDisplay(outfile_h);
SWIG_exit(EXIT_FAILURE);
}
} else {
f_runtime_h = f_runtime;
}
}
Swig_register_filebyname("gateway", f_gateway);
Swig_register_filebyname("constants", f_constants);
Swig_register_filebyname("begin", f_begin);
Swig_register_filebyname("runtime", f_runtime);
Swig_register_filebyname("header", f_header);
Swig_register_filebyname("doc", f_doc);
Swig_register_filebyname("wrapper", f_wrappers);
Swig_register_filebyname("init", f_init);
Swig_register_filebyname("initbeforefunc", f_initbeforefunc);
Swig_register_filebyname("director", f_directors);
Swig_register_filebyname("director_h", f_directors_h);
Swig_banner(f_begin);
Printf(f_runtime, "\n");
Printf(f_runtime, "#define SWIGMATLAB\n");
Printf(f_runtime, "#define SWIG_name_d \"%s\"\n", module);
Printf(f_runtime, "#define SWIG_name %s\n", module);
Printf(f_runtime, "\n");
Printf(f_runtime, "#define SWIG_op_prefix \"%s\"\n", op_prefix);
Printf(f_runtime, "#define SWIG_pkg_name \"%s\"\n", pkg_name);
Printf(f_runtime, "#define SwigVar_mxArray mxArray*\n");
if (directorsEnabled()) {
Printf(f_runtime, "#define SWIG_DIRECTORS\n");
}
Printf(f_runtime, "\n");
// Constant lookup
initConstant();
// Mex-file gateway
initGateway();
if (directorsEnabled()) {
Swig_banner(f_directors_h);
Printf(f_directors_h, "\n");
Printf(f_directors_h, "#ifndef SWIG_%s_WRAP_H_\n", module);
Printf(f_directors_h, "#define SWIG_%s_WRAP_H_\n\n", module);
if (dirprot_mode()) {
Printf(f_directors_h, "#include <map>\n");
Printf(f_directors_h, "#include <string>\n\n");
}
Printf(f_directors, "\n\n");
Printf(f_directors, "/* ---------------------------------------------------\n");
Printf(f_directors, " * C++ director class methods\n");
Printf(f_directors, " * --------------------------------------------------- */\n\n");
if (outfile_h) {
String *filename = Swig_file_filename(outfile_h);
Printf(f_directors, "#include \"%s\"\n\n", filename);
Delete(filename);
}
}
/* Emit code for children */
Language::top(n);
// Finalize constant lookup
finalizeConstant();
// Finalize Mex-file gate way
finalizeGateway();
// Load dependent modules
Iterator i = First(l_modules);
if (i.item) {
Printf(f_init, "mxArray *id = mxCreateDoubleScalar(double(4));\n");
Printf(f_init, "int error;\n");
Printf(f_init, "if (!id) mexErrMsgIdAndTxt(\"SWIG:RuntimeError\", \"Setup failed\");\n");
for (; i.item; i = Next(i)) {
Printf(f_init, "error = SWIG_Matlab_CallInterp(0, 0, 1, &id, \"%s\");\n", i.item);
Printf(f_init, "if (error) mexErrMsgIdAndTxt(\"SWIG:RuntimeError\", \"Cannot initialize %s\");\n", i.item);
Delete(i.item);
}
Printf(f_init, "mxDestroyArray(id);\n");
}
Delete(l_modules);
/* Load this module */
Printf(f_init, "SWIG_InitializeModule(0);\n\n");
if (directorsEnabled()) {
Swig_insert_file("director.swg", f_runtime);
}
/* Finish off our init function:
we need to close the bracket of the initialisation function (LoadModule).
This was left open such that any dynamic_cast variables are
set inside that function (as those are set via %init in swig.swg, see macro
DYNAMIC_CAST).
*/
Printf(f_init, "}\n");
SwigType_emit_type_table(f_runtime, f_wrappers);
Dump(f_runtime, f_begin);
Dump(f_header, f_begin);
if (directorsEnabled()) {
Dump(f_directors_h, f_runtime_h);
Printf(f_runtime_h, "\n");
Printf(f_runtime_h, "#endif\n");
if (f_runtime_h != f_begin)
Delete(f_runtime_h);
Dump(f_directors, f_begin);
}
Dump(f_doc, f_begin);
Dump(f_wrappers, f_begin);
Dump(f_initbeforefunc, f_begin);
Wrapper_pretty_print(f_init, f_begin);
// Get constant name by index, internal
Printf(f_begin, "\n");
Printf(f_begin, "SWIGINTERN const char* SwigConstantName(int con_id) {\n");
Printf(f_begin, " switch (con_id) {\n");
// Add cases and deallocate list
int c = 0;
for (i = First(l_cnames); i.item; i = Next(i)) {
Printf(f_begin, " case %d: return \"%s\";\n", c++, i.item);
Delete(i.item);
}
Delete(l_cnames);
// Default case
Printf(f_begin, " default: return 0;\n");
Printf(f_begin, " }\n");
Printf(f_begin, "}\n");
// Get constant name by index
Printf(f_begin, "\n");
Printf(f_begin, "SWIGINTERN int swigConstantName(int resc, mxArray *resv[], int argc, mxArray *argv[]) {\n");
// The first argument is always the ID
Printf(f_begin, " if (argc!=1 || !mxIsDouble(*argv) || mxGetNumberOfElements(*argv)!=1) {\n");
Printf(f_begin, " SWIG_Error(SWIG_RuntimeError, \"This mex file should only be called from "
"inside the .m files generated by SWIG. The input should be the constant ID.\");\n");
Printf(f_begin, " return 1;\n");
Printf(f_begin, " }\n");
// Make sure one output
Printf(f_begin, " if (resc!=1) {\n");
Printf(f_begin, " SWIG_Error(SWIG_RuntimeError, \"The function should have one output.\");\n");
Printf(f_begin, " return 1;\n");
Printf(f_begin, " }\n");
// Get the string
Printf(f_begin, " int con_id = (int)mxGetScalar(*argv);\n");
Printf(f_begin, " const char* s = SwigConstantName(con_id);\n");
// Make sure found
Printf(f_begin, " if (s==0) {\n");
Printf(f_begin, " SWIG_Error(SWIG_RuntimeError, \"No such constant ID.\");\n");
Printf(f_begin, " return 1;\n");
Printf(f_begin, " }\n");
// Return string
Printf(f_begin, " *resv = mxCreateString(s);\n");
Printf(f_begin, " return 0;\n");
Printf(f_begin, "}\n\n");
Dump(f_constants, f_begin);
// Get function name by index, internal
Printf(f_begin, "\n");
Printf(f_begin, "SWIGINTERN const char* SwigFunctionName(int fcn_id) {\n");
Printf(f_begin, " switch(fcn_id) {\n");
// Add cases and deallocate list
c = 0;
for (i = First(l_fnames); i.item; i = Next(i)) {
Printf(f_begin, " case %d: return \"%s\";\n", c++, i.item);
Delete(i.item);
}
Delete(l_fnames);
// Default case
Printf(f_begin, " default: return 0;\n");
Printf(f_begin, " }\n");
Printf(f_begin, "}\n");
// Get function name by index
Printf(f_begin, "\n");
Printf(f_begin, "SWIGINTERN int swigFunctionName(int resc, mxArray *resv[], int argc, mxArray *argv[]) {\n");
// The first argument is always the ID
Printf(f_begin, " if (argc!=1 || !mxIsDouble(*argv) || mxGetNumberOfElements(*argv)!=1) {\n");
Printf(f_begin, " SWIG_Error(SWIG_RuntimeError, \"This mex file should only be called from "
"inside the .m files generated by SWIG. The input should be the function ID.\");\n");
Printf(f_begin, " return 1;\n");
Printf(f_begin, " }\n");
// Make sure one output
Printf(f_begin, " if (resc!=1) {\n");
Printf(f_begin, " SWIG_Error(SWIG_RuntimeError, \"The function should have one output.\");\n");
Printf(f_begin, " return 1;\n");
Printf(f_begin, " }\n");
// Get the string
Printf(f_begin, " int fcn_id = (int)mxGetScalar(*argv);\n");
Printf(f_begin, " const char* s = SwigFunctionName(fcn_id);\n");
// Make sure found
Printf(f_begin, " if (s==0) {\n");
Printf(f_begin, " SWIG_Error(SWIG_RuntimeError, \"No such function ID.\");\n");
Printf(f_begin, " return 1;\n");
Printf(f_begin, " }\n");
// Return string
Printf(f_begin, " *resv = mxCreateString(s);\n");
Printf(f_begin, " return 0;\n");
Printf(f_begin, "}\n\n");
// Get memory
Printf(f_begin, "SWIGINTERN int swigThis(int resc, mxArray *resv[], int argc, mxArray *argv[]) {\n");
// Make sure only one input and max one output
Printf(f_begin, " if (argc!=1 || resc!=1) {\n");
Printf(f_begin, " SWIG_Error(SWIG_RuntimeError, \"swigThis should have one input and one output.\");\n");
Printf(f_begin, " return 1;\n");
Printf(f_begin, " }\n");
// Get the SwigPtr object
Printf(f_begin, " SwigPtr* swig_ptr = SWIG_Matlab_getSwigPtr(argv[0]);\n");
Printf(f_begin, " if (!swig_ptr) {\n");
Printf(f_begin, " SWIG_Error(SWIG_RuntimeError, \"The argument should be a SWIG class.\");\n");
Printf(f_begin, " return 1;\n");
Printf(f_begin, " }\n");
// Convert to uint64 and return
Printf(f_begin, " resv[0] = mxCreateNumericMatrix(1, 1, mxUINT64_CLASS, mxREAL);\n");
Printf(f_begin, " *(uint64_T *)mxGetData(resv[0]) = (uint64_T)swig_ptr->ptr;\n");
Printf(f_begin, " return 0;\n");
Printf(f_begin, "}\n\n");
// Touch module
Printf(f_begin, "SWIGINTERN int swigTouch(int resc, mxArray** SWIGUNUSEDPARM(resv), int argc, mxArray** SWIGUNUSEDPARM(argv)) {\n");
// Make sure no inputs or outputs
Printf(f_begin, " if (argc!=0 || resc!=0) {\n");
Printf(f_begin, " SWIG_Error(SWIG_RuntimeError, \"swigTouch should have no inputs or outputs.\");\n");
Printf(f_begin, " return 1;\n");
Printf(f_begin, " }\n");
Printf(f_begin, " return 0;\n");
Printf(f_begin, "}\n\n");
Dump(f_gateway, f_begin);
Delete(f_initbeforefunc);
Delete(f_init);
Delete(f_wrappers);
Delete(f_doc);
Delete(f_header);
Delete(f_directors);
Delete(f_directors_h);
Delete(f_runtime);
Delete(f_begin);
Delete(f_constants);
Delete(f_gateway);
Delete(pkg_name);
Delete(pkg_name_fullpath);
Delete(op_prefix);
Delete(mex_name);
return SWIG_OK;
}
void MATLAB::process_autodoc(Node *n) {
String *str = Getattr(n, "feature:docstring");
bool autodoc_enabled = !Cmp(Getattr(n, "feature:autodoc"), "1");
Setattr(n, "matlab:synopsis", NewString(""));
Setattr(n, "matlab:decl_info", NewString(""));
Setattr(n, "matlab:cdecl_info", NewString(""));
Setattr(n, "matlab:args_info", NewString(""));
String *synopsis = Getattr(n, "matlab:synopsis");
String *decl_info = Getattr(n, "matlab:decl_info");
// String *cdecl_info = Getattr(n, "matlab:cdecl_info");
String *args_info = Getattr(n, "matlab:args_info");
if (autodoc_enabled) {
String *decl_str = NewString("");
String *args_str = NewString("");
make_autodocParmList(n, decl_str, args_str);
Append(decl_info, "Usage: ");
SwigType *type = Getattr(n, "type");
if (type && Strcmp(type, "void")) {
// TODO this is probably wrong. we need sym:name all the time
// (currently reporting C++ name)
Node *nn = classLookup(Getattr(n, "type"));
String *type_str = nn ? Copy(Getattr(nn, "sym:name")) : SwigType_str(type, 0);
Append(decl_info, "retval = ");
Printf(args_str, "%sretval is of type %s. ", args_str, type_str);
Delete(type_str);
}
Append(decl_info, Getattr(n, "sym:name"));
Append(decl_info, " (");
Append(decl_info, decl_str);
Append(decl_info, ")\n");
Append(args_info, args_str);
Delete(decl_str);
Delete(args_str);
}
if (str && Len(str) > 0) {
// strip off {} if necessary
char *t = Char(str);
if (*t == '{') {
Delitem(str, 0);
Delitem(str, DOH_END);
}
// emit into synopsis section
Append(synopsis, str);
}
}
/* ------------------------------------------------------------
* have_matlabprepend()
* Check if there is a %matlabprepend directive and it has text
* ------------------------------------------------------------ */
bool have_matlabprepend(Node *n) {
String *str = Getattr(n, "feature:matlabprepend");
return (str && Len(str) > 0);
}
/* ------------------------------------------------------------
* matlabprepend()
* Get the %matlabprepend code, stripping off {} if neccessary
* ------------------------------------------------------------ */
String *matlabprepend(Node *n) {
String *str = Getattr(n, "feature:matlabprepend");
char *t = Char(str);
if (*t == '{') {
Delitem(str, 0);
Delitem(str, DOH_END);
}
return str;
}
/* ------------------------------------------------------------
* have_matlabappend()
* Check if there is a %matlabappend directive and it has text
* ------------------------------------------------------------ */
bool have_matlabappend(Node *n) {
String *str = Getattr(n, "feature:matlabappend");
if (!str)
str = Getattr(n, "feature:addtofunc");
return (str && Len(str) > 0);
}
/* ------------------------------------------------------------
* matlabappend()
* Get the %matlabappend code, stripping off {} if neccessary
* ------------------------------------------------------------ */
String *matlabappend(Node *n) {
String *str = Getattr(n, "feature:matlabappend");
if (!str)
str = Getattr(n, "feature:addtofunc");
char *t = Char(str);
if (*t == '{') {
Delitem(str, 0);
Delitem(str, DOH_END);
}
return str;
}
int MATLAB::importDirective(Node *n) {
String *modname = Getattr(n, "module");
if (modname) {
// Find the module node for this imported module. It should be the
// first child but search just in case.
Node *mod = firstChild(n);
while (mod && Strcmp(nodeType(mod), "module") != 0)
mod = nextSibling(mod);
// If option mexname set for module, use instead
Node *options = Getattr(mod, "options");
String *import_mexname = options ? Getattr(options, "mexname") : 0;
if (import_mexname == 0) {
import_mexname = Copy(modname);
Append(import_mexname, "MEX");
}
/* Add to list of modules to be imported */
Append(l_modules, import_mexname);
}
return Language::importDirective(n);
}
// const char *get_implicitconv_flag(Node *n) {
// int conv = 0;
// if (n && GetFlag(n, "feature:implicitconv")) {
// conv = 1;
// }
// return conv ? "SWIG_POINTER_IMPLICIT_CONV" : "0";
// }
void MATLAB::addMissingParameterNames(ParmList *plist, int arg_offset) {
Parm *p = plist;
int i = arg_offset;
while (p) {
if (!Getattr(p, "lname")) {
String *pname = Swig_cparm_name(p, i);
Delete(pname);
}
i++;
p = nextSibling(p);
}
}
void MATLAB::make_autodocParmList(Node *n, String *decl_str, String *args_str) {
String *pdocs = 0;
ParmList *plist = CopyParmList(Getattr(n, "parms"));
Parm *p;
Parm *pnext;
int start_arg_num = is_wrapping_class() ? 1 : 0;
addMissingParameterNames(plist, start_arg_num); // for $1_name substitutions done in Swig_typemap_attach_parms
Swig_typemap_attach_parms("in", plist, 0);
Swig_typemap_attach_parms("doc", plist, 0);
for (p = plist; p; p = pnext) {
String *tm = Getattr(p, "tmap:in");
if (tm) {
pnext = Getattr(p, "tmap:in:next");
if (checkAttribute(p, "tmap:in:numinputs", "0")) {
continue;
}
} else {
pnext = nextSibling(p);
}
String *name = 0;
String *type = 0;
String *value = 0;
String *pdoc = Getattr(p, "tmap:doc");
if (pdoc) {
name = Getattr(p, "tmap:doc:name");
type = Getattr(p, "tmap:doc:type");
value = Getattr(p, "tmap:doc:value");
}
// Note: the generated name should be consistent with that in kwnames[]
name = name ? name : Getattr(p, "name");
name = name ? name : Getattr(p, "lname");
name = Swig_name_make(p, 0, name, 0, 0); // rename parameter if a keyword
type = type ? type : Getattr(p, "type");
value = value ? value : Getattr(p, "value");
if (SwigType_isvarargs(type))
break;
String *tex_name = NewString("");
if (name)
Printf(tex_name, "%s", name);
else
Printf(tex_name, "?");
if (Len(decl_str))
Append(decl_str, ", ");
Append(decl_str, tex_name);
if (value) {
String *new_value = convertValue(value, Getattr(p, "type"));
if (new_value) {
value = new_value;
} else {
Node *lookup = Swig_symbol_clookup(value, 0);
if (lookup)
value = Getattr(lookup, "sym:name");
}
Printf(decl_str, " = %s", value);
}
Node *nn = classLookup(Getattr(p, "type"));
String *type_str = nn ? Copy(Getattr(nn, "sym:name")) : SwigType_str(type, 0);
Printf(args_str, "%s is of type %s. ", tex_name, type_str);
Delete(type_str);
Delete(tex_name);
Delete(name);
}
if (pdocs)
Setattr(n, "feature:pdocs", pdocs);
Delete(plist);
}
String *MATLAB::convertValue(String *v, SwigType *t) {
if (v && Len(v) > 0) {
char fc = (Char(v))[0];
if (('0' <= fc && fc <= '9') || '\'' == fc || '"' == fc) {
/* number or string (or maybe NULL pointer) */
if (SwigType_ispointer(t) && Strcmp(v, "0") == 0)
return NewString("None");
else
return v;
}
if (Strcmp(v, "NULL") == 0 || Strcmp(v, "nullptr") == 0)
return SwigType_ispointer(t) ? NewString("nil") : NewString("0");
if (Strcmp(v, "true") == 0 || Strcmp(v, "TRUE") == 0)
return NewString("true");
if (Strcmp(v, "false") == 0 || Strcmp(v, "FALSE") == 0)
return NewString("false");
}
return 0;
}
int MATLAB::functionWrapper(Node *n) {
Parm *p;
String *tm;
int j;
String *nodeType = Getattr(n, "nodeType");
int constructor = (!Cmp(nodeType, "constructor"));
int destructor = (!Cmp(nodeType, "destructor"));
String *storage = Getattr(n, "storage");
bool overloaded = !!Getattr(n, "sym:overloaded");
bool last_overload = overloaded && !Getattr(n, "sym:nextSibling");
String *iname = Getattr(n, "sym:name");
String *wname = Swig_name_wrapper(iname);
String *overname = Copy(wname);
SwigType *d = Getattr(n, "type");
ParmList *l = Getattr(n, "parms");
if (!overloaded && !addSymbol(iname, n))
return SWIG_ERROR;
if (overloaded)
Append(overname, Getattr(n, "sym:overname"));
Wrapper *f = NewWrapper();
Printf(f->def, "int %s(int resc, mxArray *resv[], int argc, mxArray *argv[]) {", overname);
emit_parameter_variables(l, f);
emit_attach_parmmaps(l, f);
Setattr(n, "wrap:parms", l);
int num_arguments = emit_num_arguments(l);
int num_required = emit_num_required(l);
int varargs = emit_isvarargs(l);
char source[64];
if (destructor) {
Append(f->code, "int is_owned;\n");
}
Printf(f->code, "if (!SWIG_check_num_args(\"%s\",argc,%i,%i,%i)) " "{\n SWIG_fail;\n }\n", iname, num_arguments, num_required, varargs);
if (num_arguments == 0 && num_required == 0) {
Printf(f->code, "(void)argv;\n");
} else if (constructor && num_arguments == 1 && num_required == 1) {
if (Cmp(storage, "explicit") == 0) {
Node *parent = Swig_methodclass(n);
if (GetFlag(parent, "feature:implicitconv")) {
String *desc = NewStringf("SWIGTYPE%s", SwigType_manglestr(Getattr(n, "type")));
Printf(f->code, "if (SWIG_CheckImplicit(%s)) SWIG_fail;\n", desc);
Delete(desc);
}
}
}
// Check if owned here, before possibly disowning in the typemaps
if (destructor) {
Append(f->code, "is_owned = SWIG_Matlab_isOwned(argv[0]);\n");
}
for (j = 0, p = l; j < num_arguments; ++j) {
while (checkAttribute(p, "tmap:in:numinputs", "0")) {
p = Getattr(p, "tmap:in:next");
}
SwigType *pt = Getattr(p, "type");
String *tm = Getattr(p, "tmap:in");
if (tm) {
if (!tm || checkAttribute(p, "tmap:in:numinputs", "0")) {
p = nextSibling(p);
continue;
}
sprintf(source, "argv[%d]", j);
Setattr(p, "emit:input", source);
Replaceall(tm, "$source", Getattr(p, "emit:input"));
Replaceall(tm, "$input", Getattr(p, "emit:input"));
Replaceall(tm, "$target", Getattr(p, "lname"));
if (Getattr(p, "wrap:disown") || (Getattr(p, "tmap:in:disown"))) {
Replaceall(tm, "$disown", "SWIG_POINTER_DISOWN");
} else {
Replaceall(tm, "$disown", "0");
}
if (Getattr(p, "tmap:in:implicitconv")) {
const char *convflag = "0";
if (!Getattr(p, "hidden")) {
SwigType *ptype = Getattr(p, "type");
convflag = get_implicitconv_flag(classLookup(ptype));
}
Replaceall(tm, "$implicitconv", convflag);
Setattr(p, "implicitconv", convflag);
}
String *getargs = NewString("");
if (j >= num_required)
Printf(getargs, "if (%d<argc) {\n%s\n}", j, tm);
else
Printv(getargs, tm, NIL);
Printv(f->code, getargs, "\n", NIL);
Delete(getargs);
p = Getattr(p, "tmap:in:next");
continue;
} else {
Swig_warning(WARN_TYPEMAP_IN_UNDEF, input_file, line_number, "Unable to use type %s as a function argument.\n", SwigType_str(pt, 0));
break;
}
}
// Check for trailing varargs
if (varargs) {
if (p && (tm = Getattr(p, "tmap:in"))) {
Replaceall(tm, "$input", "varargs");
Printv(f->code, tm, "\n", NIL);
}
}
// Insert constraint checking code