forked from Kitware/VTK
/
vtkWrapPythonOverload.c
700 lines (605 loc) · 16.6 KB
/
vtkWrapPythonOverload.c
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
/*=========================================================================
Program: Visualization Toolkit
Module: vtkWrapPythonOverload.c
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
/*
When wrapping overloaded C++ methods, it is necessary to provide
hints so that Python can choose which overload to call (see
vtkPythonOverload.cxx for the code that is used to do this).
Where possible, overloads are resolved based on the number of
arguments that are passed. When this isn't possible, the overloads
must be resolved based on argument types. So, for each overload,
we store the parameter types as a string.
The "parameter type" string can start with one of the following:
- (hyphen) marks a method as an explicit constructor
@ placeholder for "self" in a method (i.e. method is not static)
For each parameter, one of the following codes is used:
q bool
c char
b signed char
B unsigned char
h signed short
H unsigned short
i int
I unsigned int
l long
L unsigned long
k long long
K unsigned long long
f float
d double
v void *
z char *
s string
u unicode
F callable object
E enum type
O python object
Q Qt object
V VTK object
W VTK special type
P Pointer to numeric type
A Multi-dimensional array of numeric type
| marks the end of required parameters, following parameters are optional
If the parameter is E, O, Q, V, W, then a type name must follow the type
codes. The type name must be preceded by '*' if the type is a non-const
reference or a pointer. For example,
func(vtkArray *, vtkVariant &, int) -> "VWi *vtkArray &vtkVariant"
If the parameter is P, then the type of the array or pointer must
follow the type codes. For example,
func(int *p, double a[10]) -> "PP *i *d"
If the parameter is A, then both the type and all dimensions after the
first dimension must be provided:
func(double a[3][4]) -> "A *d[4]"
*/
#include "vtkWrapPythonOverload.h"
#include "vtkWrapPythonMethod.h"
#include "vtkWrapPythonTemplate.h"
#include "vtkWrap.h"
#include "vtkWrapText.h"
/* required for VTK_USE_64BIT_IDS */
#include "vtkConfigure.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
/* -------------------------------------------------------------------- */
/* prototypes for utility methods */
/* create a parameter format char for the give type */
static char vtkWrapPython_FormatChar(
unsigned int argtype);
/* create a string for checking arguments against available signatures */
static char *vtkWrapPython_ArgCheckString(
ClassInfo *data, FunctionInfo *currentFunction);
/* -------------------------------------------------------------------- */
/* Get the python format char for the give type, after retrieving the
* base type from the type */
static char vtkWrapPython_FormatChar(unsigned int argtype)
{
char typeChar = 'O';
switch ( (argtype & VTK_PARSE_BASE_TYPE) )
{
case VTK_PARSE_FLOAT:
typeChar = 'f';
break;
case VTK_PARSE_DOUBLE:
typeChar = 'd';
break;
case VTK_PARSE_UNSIGNED_INT:
typeChar = 'I';
break;
case VTK_PARSE_INT:
typeChar = 'i';
break;
case VTK_PARSE_UNSIGNED_SHORT:
typeChar = 'H';
break;
case VTK_PARSE_SHORT:
typeChar = 'h';
break;
case VTK_PARSE_UNSIGNED_LONG:
typeChar = 'L';
break;
case VTK_PARSE_LONG:
typeChar = 'l';
break;
case VTK_PARSE_UNSIGNED_ID_TYPE:
#ifdef VTK_USE_64BIT_IDS
typeChar = 'K';
#else
typeChar = 'I';
#endif
case VTK_PARSE_ID_TYPE:
#ifdef VTK_USE_64BIT_IDS
typeChar = 'k';
#else
typeChar = 'i';
#endif
break;
case VTK_PARSE_SIZE_T:
case VTK_PARSE_UNSIGNED_LONG_LONG:
case VTK_PARSE_UNSIGNED___INT64:
typeChar = 'K';
break;
case VTK_PARSE_SSIZE_T:
case VTK_PARSE_LONG_LONG:
case VTK_PARSE___INT64:
typeChar = 'k';
break;
case VTK_PARSE_SIGNED_CHAR:
typeChar = 'b';
break;
case VTK_PARSE_CHAR:
typeChar = 'c';
break;
case VTK_PARSE_UNSIGNED_CHAR:
typeChar = 'B';
break;
case VTK_PARSE_VOID:
typeChar = 'v';
break;
case VTK_PARSE_BOOL:
typeChar = 'q';
break;
case VTK_PARSE_STRING:
typeChar = 's';
break;
case VTK_PARSE_UNICODE_STRING:
typeChar = 'u';
break;
}
return typeChar;
}
/* -------------------------------------------------------------------- */
/* Create a string to describe the signature of a method. */
static char *vtkWrapPython_ArgCheckString(
ClassInfo *data, FunctionInfo *currentFunction)
{
static char result[2048]; /* max literal string length */
char classname[1024];
size_t currPos = 0;
size_t endPos;
ValueInfo *arg;
unsigned int argtype;
int i, j, k;
int totalArgs, requiredArgs;
char c = '\0';
totalArgs = vtkWrap_CountWrappedParameters(currentFunction);
requiredArgs = vtkWrap_CountRequiredArguments(currentFunction);
if (currentFunction->IsExplicit)
{
/* used to mark constructors as 'explicit' */
result[currPos++] = '-';
}
/* placeholder for 'self' in method calls */
if (!currentFunction->IsStatic)
{
result[currPos++] = '@';
}
/* position for insertion after format chars */
endPos = currPos + totalArgs;
if (totalArgs > requiredArgs)
{
/* add one for the "|" that marks the end of required args */
endPos++;
}
/* create a format character for each argument */
for (i = 0; i < totalArgs; i++)
{
arg = currentFunction->Parameters[i];
argtype = (arg->Type & VTK_PARSE_UNQUALIFIED_TYPE);
if (i == requiredArgs)
{
/* make all following arguments optional */
result[currPos++] = '|';
}
/* will store the classname for objects */
classname[0] = '\0';
if (vtkWrap_IsEnumMember(data, arg))
{
c = 'E';
sprintf(classname, "%.200s.%.200s", data->Name, arg->Class);
}
else if (arg->IsEnum)
{
c = 'E';
vtkWrapText_PythonName(arg->Class, classname);
}
else if (vtkWrap_IsPythonObject(arg))
{
c = 'O';
vtkWrapText_PythonName(arg->Class, classname);
}
else if (vtkWrap_IsVTKObject(arg))
{
c = 'V';
vtkWrapText_PythonName(arg->Class, classname);
}
else if (vtkWrap_IsSpecialObject(arg))
{
c = 'W';
vtkWrapText_PythonName(arg->Class, classname);
}
else if (vtkWrap_IsQtEnum(arg) || vtkWrap_IsQtObject(arg))
{
c = 'Q';
vtkWrapText_PythonName(arg->Class, classname);
}
else if (vtkWrap_IsFunction(arg))
{
c = 'F';
}
else if (vtkWrap_IsVoidPointer(arg))
{
c = 'v';
}
else if (vtkWrap_IsString(arg))
{
c = 's';
if ((argtype & VTK_PARSE_BASE_TYPE) == VTK_PARSE_UNICODE_STRING)
{
c = 'u';
}
}
else if (vtkWrap_IsCharPointer(arg))
{
c = 'z';
}
else if (vtkWrap_IsNumeric(arg) &&
vtkWrap_IsScalar(arg))
{
c = vtkWrapPython_FormatChar(argtype);
}
else if (vtkWrap_IsArray(arg) || vtkWrap_IsPODPointer(arg))
{
c = 'P';
result[endPos++] = ' ';
result[endPos++] = '*';
result[endPos++] = vtkWrapPython_FormatChar(argtype);
}
else if (vtkWrap_IsNArray(arg))
{
c = 'A';
result[endPos++] = ' ';
result[endPos++] = '*';
result[endPos++] = vtkWrapPython_FormatChar(argtype);
if (vtkWrap_IsNArray(arg))
{
for (j = 1; j < arg->NumberOfDimensions; j++)
{
result[endPos++] = '[';
for (k = 0; arg->Dimensions[j][k]; k++)
{
result[endPos++] = arg->Dimensions[j][k];
}
result[endPos++] = ']';
}
}
}
/* add the format char to the string */
result[currPos++] = c;
if (classname[0] != '\0')
{
result[endPos++] = ' ';
if ((argtype == VTK_PARSE_OBJECT_REF ||
argtype == VTK_PARSE_QOBJECT_REF ||
argtype == VTK_PARSE_UNKNOWN_REF) &&
(arg->Type & VTK_PARSE_CONST) == 0)
{
result[endPos++] = '&';
}
else if (argtype == VTK_PARSE_OBJECT_PTR ||
argtype == VTK_PARSE_UNKNOWN_PTR ||
argtype == VTK_PARSE_QOBJECT_PTR)
{
result[endPos++] = '*';
}
strcpy(&result[endPos], classname);
endPos += strlen(classname);
}
}
result[endPos] = '\0';
return result;
}
/* -------------------------------------------------------------------- */
/* Generate an int array that maps arg counts to overloads.
* Each element in the array will either contain the index of the
* overload that it maps to, or -1 if it maps to multiple overloads,
* or zero if it does not map to any. The length of the array is
* returned in "nmax". The value of "overlap" is set to 1 if there
* are some arg counts that map to more than one method. */
int *vtkWrapPython_ArgCountToOverloadMap(
FunctionInfo **wrappedFunctions, int numberOfWrappedFunctions,
int fnum, int is_vtkobject, int *nmax, int *overlap)
{
static int overloadMap[512];
int totalArgs, requiredArgs;
int occ, occCounter;
FunctionInfo *theOccurrence;
FunctionInfo *theFunc;
int mixed_static, any_static;
int i;
*nmax = 0;
*overlap = 0;
theFunc = wrappedFunctions[fnum];
any_static = 0;
mixed_static = 0;
for (i = fnum; i < numberOfWrappedFunctions; i++)
{
if (wrappedFunctions[i]->Name &&
strcmp(wrappedFunctions[i]->Name, theFunc->Name) == 0)
{
if (wrappedFunctions[i]->IsStatic)
{
any_static = 1;
}
else if (any_static)
{
mixed_static = 1;
}
}
}
for (i = 0; i < 100; i++)
{
overloadMap[i] = 0;
}
occCounter = 0;
for (occ = fnum; occ < numberOfWrappedFunctions; occ++)
{
theOccurrence = wrappedFunctions[occ];
if (theOccurrence->Name == 0 ||
strcmp(theOccurrence->Name, theFunc->Name) != 0)
{
continue;
}
occCounter++;
totalArgs = vtkWrap_CountWrappedParameters(theOccurrence);
requiredArgs = vtkWrap_CountRequiredArguments(theOccurrence);
/* vtkobject calls might have an extra "self" arg in front */
if (mixed_static && is_vtkobject &&
!theOccurrence->IsStatic)
{
totalArgs++;
}
if (totalArgs > *nmax)
{
*nmax = totalArgs;
}
for (i = requiredArgs; i <= totalArgs && i < 100; i++)
{
if (overloadMap[i] == 0)
{
overloadMap[i] = occCounter;
}
else
{
overloadMap[i] = -1;
*overlap = 1;
}
}
}
return overloadMap;
}
/* -------------------------------------------------------------------- */
/* output the method table for all overloads of a particular method,
* this is also used to write out all constructors for the class */
void vtkWrapPython_OverloadMethodDef(
FILE *fp, const char *classname, ClassInfo *data, int *overloadMap,
FunctionInfo **wrappedFunctions, int numberOfWrappedFunctions,
int fnum, int numberOfOccurrences, int all_legacy)
{
char occSuffix[8];
int occ, occCounter;
FunctionInfo *theOccurrence;
FunctionInfo *theFunc;
int totalArgs, requiredArgs;
int i;
int putInTable;
theFunc = wrappedFunctions[fnum];
if (all_legacy)
{
fprintf(fp,
"#if !defined(VTK_LEGACY_REMOVE)\n");
}
fprintf(fp,
"static PyMethodDef Py%s_%s_Methods[] = {\n",
classname, theFunc->Name);
occCounter = 0;
for (occ = fnum; occ < numberOfWrappedFunctions; occ++)
{
theOccurrence = wrappedFunctions[occ];
if (theOccurrence->Name == 0 ||
strcmp(theOccurrence->Name, theFunc->Name) != 0)
{
continue;
}
occCounter++;
totalArgs = vtkWrap_CountWrappedParameters(theOccurrence);
requiredArgs = vtkWrap_CountRequiredArguments(theOccurrence);
putInTable = 0;
/* all conversion constructors must go into the table */
if (vtkWrap_IsConstructor(data, theOccurrence) &&
requiredArgs <= 1 && totalArgs >= 1 &&
!theOccurrence->IsExplicit)
{
putInTable = 1;
}
/* all methods that overlap with others must go in the table */
for (i = requiredArgs; i <= totalArgs; i++)
{
if (overloadMap[i] == -1)
{
putInTable = 1;
}
}
if (!putInTable)
{
continue;
}
if (theOccurrence->IsLegacy && !all_legacy)
{
fprintf(fp,
"#if !defined(VTK_LEGACY_REMOVE)\n");
}
/* method suffix to distinguish between signatures */
occSuffix[0] = '\0';
if (numberOfOccurrences > 1)
{
sprintf(occSuffix, "_s%d", occCounter);
}
fprintf(fp,
" {NULL, Py%s_%s%s, METH_VARARGS%s,\n"
" \"%s\"},\n",
classname, theOccurrence->Name,
occSuffix,
theOccurrence->IsStatic ? " | METH_STATIC" : "",
vtkWrapPython_ArgCheckString(data, theOccurrence));
if (theOccurrence->IsLegacy && !all_legacy)
{
fprintf(fp,
"#endif\n");
}
}
fprintf(fp,
" {NULL, NULL, 0, NULL}\n"
"};\n");
if (all_legacy)
{
fprintf(fp,
"#endif\n");
}
fprintf(fp,
"\n");
}
/* -------------------------------------------------------------------- */
/* make a method that will choose which overload to call */
void vtkWrapPython_OverloadMasterMethod(
FILE *fp, const char *classname, int *overloadMap, int maxArgs,
FunctionInfo **wrappedFunctions, int numberOfWrappedFunctions, int fnum,
int is_vtkobject, int all_legacy)
{
FunctionInfo *currentFunction;
FunctionInfo *theOccurrence;
int overlap = 0;
int occ, occCounter;
int i;
int foundOne;
int any_static = 0;
currentFunction = wrappedFunctions[fnum];
for (i = fnum; i < numberOfWrappedFunctions; i++)
{
if (wrappedFunctions[i]->Name &&
strcmp(wrappedFunctions[i]->Name, currentFunction->Name) == 0)
{
if (wrappedFunctions[i]->IsStatic)
{
any_static = 1;
}
}
}
for (i = 0; i <= maxArgs; i++)
{
if (overloadMap[i] == -1)
{
overlap = 1;
}
}
if (all_legacy)
{
fprintf(fp,
"#if !defined(VTK_LEGACY_REMOVE)\n");
}
fprintf(fp,
"static PyObject *\n"
"Py%s_%s(PyObject *self, PyObject *args)\n"
"{\n",
classname, currentFunction->Name);
if (overlap)
{
fprintf(fp,
" PyMethodDef *methods = Py%s_%s_Methods;\n",
classname, currentFunction->Name);
}
fprintf(fp,
" int nargs = vtkPythonArgs::GetArgCount(%sargs);\n"
"\n",
((is_vtkobject && !any_static) ? "self, " : ""));
fprintf(fp,
" switch(nargs)\n"
" {\n");
/* find all occurrences of this method */
occCounter = 0;
for (occ = fnum; occ < numberOfWrappedFunctions; occ++)
{
theOccurrence = wrappedFunctions[occ];
/* is it the same name */
if (theOccurrence->Name &&
strcmp(currentFunction->Name, theOccurrence->Name) == 0)
{
occCounter++;
foundOne = 0;
for (i = 0; i <= maxArgs; i++)
{
if (overloadMap[i] == occCounter)
{
if (!foundOne && theOccurrence->IsLegacy && !all_legacy)
{
fprintf(fp,
"#if !defined(VTK_LEGACY_REMOVE)\n");
}
fprintf(fp,
" case %d:\n",
i);
foundOne = 1;
}
}
if (foundOne)
{
fprintf(fp,
" return Py%s_%s_s%d(self, args);\n",
classname, currentFunction->Name, occCounter);
if (theOccurrence->IsLegacy && !all_legacy)
{
fprintf(fp,
"#endif\n");
}
}
}
}
if (overlap)
{
for (i = 0; i <= maxArgs; i++)
{
if (overloadMap[i] == -1)
{
fprintf(fp,
" case %d:\n",
i);
}
}
fprintf(fp,
" return vtkPythonOverload::CallMethod(methods, self, args);\n");
}
fprintf(fp,
" }\n"
"\n");
fprintf(fp,
" vtkPythonArgs::ArgCountError(nargs, \"%.200s\");\n",
currentFunction->Name);
fprintf(fp,
" return NULL;\n"
"}\n"
"\n");
if (all_legacy)
{
fprintf(fp,
"#endif\n");
}
fprintf(fp,"\n");
}