-
Notifications
You must be signed in to change notification settings - Fork 4
/
hdr_parser.py
1059 lines (931 loc) · 41.1 KB
/
hdr_parser.py
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
#!/usr/bin/env python
from __future__ import print_function
import os
import sys
import re
import string
import io
HACK = True
HACK2 = False
HACK3 = False
# the list only for debugging. The real list, used in the real OpenCV build, is specified in CMakeLists.txt
opencv_hdr_list = [
"../../core/include/opencv2/core.hpp",
"../../core/include/opencv2/core/mat.hpp",
"../../core/include/opencv2/core/ocl.hpp",
"../../flann/include/opencv2/flann/miniflann.hpp",
"../../ml/include/opencv2/ml.hpp",
"../../imgproc/include/opencv2/imgproc.hpp",
"../../calib3d/include/opencv2/calib3d.hpp",
"../../features2d/include/opencv2/features2d.hpp",
"../../video/include/opencv2/video/tracking.hpp",
"../../video/include/opencv2/video/background_segm.hpp",
"../../objdetect/include/opencv2/objdetect.hpp",
"../../imgcodecs/include/opencv2/imgcodecs.hpp",
"../../videoio/include/opencv2/videoio.hpp",
"../../highgui/include/opencv2/highgui.hpp",
]
"""
Each declaration is [funcname, return_value_type /* in C, not in Python */, <list_of_modifiers>, <list_of_arguments>, original_return_type, docstring],
where each element of <list_of_arguments> is 4-element list itself:
[argtype, argname, default_value /* or "" if none */, <list_of_modifiers>]
where the list of modifiers is yet another nested list of strings
(currently recognized are "/O" for output argument, "/S" for static (i.e. class) methods
and "/A value" for the plain C arrays with counters)
original_return_type is None if the original_return_type is the same as return_value_type
"""
class CppHeaderParser(object):
def __init__(self, generate_umat_decls=False, generate_gpumat_decls=False):
self._generate_umat_decls = generate_umat_decls
self._generate_gpumat_decls = generate_gpumat_decls
self.BLOCK_TYPE = 0
self.BLOCK_NAME = 1
self.PROCESS_FLAG = 2
self.PUBLIC_SECTION = 3
self.CLASS_DECL = 4
self.namespaces = set()
def batch_replace(self, s, pairs):
for before, after in pairs:
s = s.replace(before, after)
return s
def get_macro_arg(self, arg_str, npos):
npos2 = npos3 = arg_str.find("(", npos)
if npos2 < 0:
print("Error: no arguments for the macro at %d" % (self.lineno,))
sys.exit(-1)
balance = 1
while 1:
t, npos3 = self.find_next_token(arg_str, ['(', ')'], npos3+1)
if npos3 < 0:
print("Error: no matching ')' in the macro call at %d" %
(self.lineno,))
sys.exit(-1)
if t == '(':
balance += 1
if t == ')':
balance -= 1
if balance == 0:
break
return arg_str[npos2+1:npos3].strip(), npos3
def parse_arg(self, arg_str, argno):
"""
Parses <arg_type> [arg_name]
Returns arg_type, arg_name, modlist, argno, where
modlist is the list of wrapper-related modifiers (such as "output argument", "has counter", ...)
and argno is the new index of an anonymous argument.
That is, if no arg_str is just an argument type without argument name, the argument name is set to
"arg" + str(argno), and then argno is incremented.
"""
modlist = []
# pass 0: extracts the modifiers
if "CV_OUT" in arg_str:
modlist.append("/O")
arg_str = arg_str.replace("CV_OUT", "")
if "CV_IN_OUT" in arg_str:
modlist.append("/IO")
arg_str = arg_str.replace("CV_IN_OUT", "")
isarray = False
npos = arg_str.find("CV_CARRAY")
if npos >= 0:
isarray = True
macro_arg, npos3 = self.get_macro_arg(arg_str, npos)
modlist.append("/A " + macro_arg)
arg_str = arg_str[:npos] + arg_str[npos3+1:]
npos = arg_str.find("CV_CUSTOM_CARRAY")
if npos >= 0:
isarray = True
macro_arg, npos3 = self.get_macro_arg(arg_str, npos)
modlist.append("/CA " + macro_arg)
arg_str = arg_str[:npos] + arg_str[npos3+1:]
npos = arg_str.find("const")
if npos >= 0:
modlist.append("/C")
npos = arg_str.find("&")
if npos >= 0:
modlist.append("/Ref")
arg_str = arg_str.strip()
word_start = 0
word_list = []
npos = -1
# print self.lineno, ":\t", arg_str
# pass 1: split argument type into tokens
while 1:
npos += 1
t, npos = self.find_next_token(
arg_str, [" ", "&", "*", "<", ">", ","], npos)
w = arg_str[word_start:npos].strip()
if w == "operator":
word_list.append("operator " + arg_str[npos:].strip())
break
if w not in ["", "const"]:
word_list.append(w)
if t not in ["", " ", "&"]:
word_list.append(t)
if not t:
break
word_start = npos+1
npos = word_start - 1
arg_type = ""
arg_name = ""
angle_stack = []
# print self.lineno, ":\t", word_list
# pass 2: decrypt the list
wi = -1
prev_w = ""
for w in word_list:
wi += 1
if w == "*":
if prev_w == "char" and not isarray:
arg_type = arg_type[:-len("char")] + "c_string"
else:
arg_type += w
continue
elif w == "<":
arg_type += "_"
angle_stack.append(0)
elif w == "," or w == '>':
if not angle_stack:
print("Error at %d: argument contains ',' or '>' not within template arguments" % (
self.lineno,))
sys.exit(-1)
if w == ",":
arg_type += "_and_"
elif w == ">":
if angle_stack[0] == 0:
print("Error at %s:%d: template has no arguments" %
(self.hname, self.lineno))
sys.exit(-1)
if angle_stack[0] > 1:
arg_type += "_end_"
angle_stack[-1:] = []
elif angle_stack:
arg_type += w
angle_stack[-1] += 1
elif arg_type == "struct":
arg_type += " " + w
elif arg_type and arg_type != "~":
arg_name = " ".join(word_list[wi:])
break
else:
arg_type += w
prev_w = w
counter_str = ""
add_star = False
if ("[" in arg_name) and not ("operator" in arg_str):
# print arg_str
p1 = arg_name.find("[")
p2 = arg_name.find("]", p1+1)
if p2 < 0:
print("Error at %d: no closing ]" % (self.lineno,))
sys.exit(-1)
counter_str = arg_name[p1+1:p2].strip()
if counter_str == "":
counter_str = "?"
if not isarray:
modlist.append("/A " + counter_str.strip())
arg_name = arg_name[:p1]
add_star = True
if not arg_name:
if arg_type.startswith("operator"):
arg_type, arg_name = "", arg_type
else:
arg_name = "arg" + str(argno)
argno += 1
while arg_type.endswith("_end_"):
arg_type = arg_type[:-len("_end_")]
if add_star:
arg_type += "*"
arg_type = self.batch_replace(
arg_type, [("std::", ""), ("cv::", ""), ("::", "_")])
return arg_type, arg_name, modlist, argno
def parse_enum(self, decl_str):
l = decl_str
ll = l.split(",")
if ll[-1].strip() == "":
ll = ll[:-1]
prev_val = ""
prev_val_delta = -1
decl = []
for pair in ll:
pv = pair.split("=")
if len(pv) == 1:
prev_val_delta += 1
val = ""
if prev_val:
val = prev_val + "+"
val += str(prev_val_delta)
else:
prev_val_delta = 0
prev_val = val = pv[1].strip()
decl.append(
["const " + self.get_dotted_name(pv[0].strip()), val, [], [], None, ""])
return decl
def parse_class_decl(self, decl_str):
"""
Parses class/struct declaration start in the form:
{class|struct} [CV_EXPORTS] <class_name> [: public <base_class1> [, ...]]
Returns class_name1, <list of base_classes>
"""
l = decl_str
modlist = []
if "CV_EXPORTS_W_MAP" in l:
l = l.replace("CV_EXPORTS_W_MAP", "")
modlist.append("/Map")
if "CV_EXPORTS_W_SIMPLE" in l:
l = l.replace("CV_EXPORTS_W_SIMPLE", "")
modlist.append("/Simple")
npos = l.find("CV_EXPORTS_AS")
if npos >= 0:
macro_arg, npos3 = self.get_macro_arg(l, npos)
modlist.append("=" + macro_arg)
l = l[:npos] + l[npos3+1:]
l = self.batch_replace(l, [("CV_EXPORTS_W", ""), ("CV_EXPORTS", ""), (
"public virtual ", " "), ("public ", " "), ("::", ".")]).strip()
ll = re.split(r'\s+|\s*[,:]\s*', l)
ll = [le for le in ll if le]
classname = ll[1]
bases = ll[2:]
return classname, bases, modlist
def parse_func_decl_no_wrap(self, decl_str, static_method=False, docstring=""):
decl_str = (decl_str or "").strip()
virtual_method = False
explicit_method = False
if decl_str.startswith("explicit"):
decl_str = decl_str[len("explicit"):].lstrip()
explicit_method = True
if decl_str.startswith("virtual"):
decl_str = decl_str[len("virtual"):].lstrip()
virtual_method = True
if decl_str.startswith("static"):
decl_str = decl_str[len("static"):].lstrip()
static_method = True
fdecl = decl_str.replace("CV_OUT", "").replace("CV_IN_OUT", "")
fdecl = fdecl.strip().replace("\t", " ")
while " " in fdecl:
fdecl = fdecl.replace(" ", " ")
fname = fdecl[:fdecl.find("(")].strip()
fnpos = fname.rfind(" ")
if fnpos < 0:
fnpos = 0
fname = fname[fnpos:].strip()
rettype = fdecl[:fnpos].strip()
if rettype.endswith("operator"):
fname = ("operator " + fname).strip()
rettype = rettype[:rettype.rfind("operator")].strip()
if rettype.endswith("::"):
rpos = rettype.rfind(" ")
if rpos >= 0:
fname = rettype[rpos+1:].strip() + fname
rettype = rettype[:rpos].strip()
else:
fname = rettype + fname
rettype = ""
apos = fdecl.find("(")
if fname.endswith("operator"):
fname += " ()"
apos = fdecl.find("(", apos+1)
fname = "cv." + fname.replace("::", ".")
decl = [fname, rettype, [], [], None, docstring]
# inline constructor implementation
implmatch = re.match(
r"(\(.*?\))\s*:\s*(\w+\(.*?\),?\s*)+", fdecl[apos:])
if bool(implmatch):
fdecl = fdecl[:apos] + implmatch.group(1)
args0str = fdecl[apos+1:fdecl.rfind(")")].strip()
if args0str != "" and args0str != "void":
args0str = re.sub(
r"\([^)]*\)", lambda m: m.group(0).replace(',', "@comma@"), args0str)
args0 = args0str.split(",")
args = []
narg = ""
for arg in args0:
narg += arg.strip()
balance_paren = narg.count("(") - narg.count(")")
balance_angle = narg.count("<") - narg.count(">")
if balance_paren == 0 and balance_angle == 0:
args.append(narg.strip())
narg = ""
for arg in args:
dfpos = arg.find("=")
defval = ""
if dfpos >= 0:
defval = arg[dfpos+1:].strip()
else:
dfpos = arg.find("CV_DEFAULT")
if dfpos >= 0:
defval, pos3 = self.get_macro_arg(arg, dfpos)
else:
dfpos = arg.find("CV_WRAP_DEFAULT")
if dfpos >= 0:
defval, pos3 = self.get_macro_arg(arg, dfpos)
if dfpos >= 0:
defval = defval.replace("@comma@", ",")
arg = arg[:dfpos].strip()
pos = len(arg)-1
while pos >= 0 and (arg[pos] in "_[]" or arg[pos].isalpha() or arg[pos].isdigit()):
pos -= 1
if pos >= 0:
aname = arg[pos+1:].strip()
atype = arg[:pos+1].strip()
if aname.endswith("&") or aname.endswith("*") or (aname in ["int", "String", "Mat"]):
atype = (atype + " " + aname).strip()
aname = ""
else:
atype = arg
aname = ""
if aname.endswith("]"):
bidx = aname.find('[')
atype += aname[bidx:]
aname = aname[:bidx]
decl[3].append([atype, aname, defval, []])
if static_method:
decl[2].append("/S")
if virtual_method:
decl[2].append("/V")
if explicit_method:
decl[2].append("/E")
if bool(re.match(r".*\)\s*(const)?\s*=\s*0", decl_str)):
decl[2].append("/A")
if bool(re.match(r".*\)\s*const(\s*=\s*0)?", decl_str)):
decl[2].append("/C")
return decl
def parse_func_decl(self, decl_str, mat="Mat", docstring=""):
"""
Parses the function or method declaration in the form:
[([CV_EXPORTS] <rettype>) | CVAPI(rettype)]
[~]<function_name>
(<arg_type1> <arg_name1>[=<default_value1>] [, <arg_type2> <arg_name2>[=<default_value2>] ...])
[const] {; | <function_body>}
Returns the function declaration entry:
[<func name>, <return value C-type>, <list of modifiers>, <list of arguments>, <original return type>, <docstring>] (see above)
"""
if self.wrap_mode or HACK:
if not (("CV_EXPORTS_AS" in decl_str) or ("CV_EXPORTS_W" in decl_str) or ("CV_WRAP" in decl_str)):
return []
# ignore old API in the documentation check (for now)
if "CVAPI(" in decl_str and (self.wrap_mode or HACK):
return []
top = self.block_stack[-1]
func_modlist = []
npos = decl_str.find("CV_EXPORTS_AS")
if npos >= 0:
arg, npos3 = self.get_macro_arg(decl_str, npos)
func_modlist.append("="+arg)
decl_str = decl_str[:npos] + decl_str[npos3+1:]
npos = decl_str.find("CV_WRAP_AS")
if npos >= 0:
arg, npos3 = self.get_macro_arg(decl_str, npos)
func_modlist.append("="+arg)
decl_str = decl_str[:npos] + decl_str[npos3+1:]
npos = decl_str.find("CV_WRAP_PHANTOM")
if npos >= 0:
decl_str, _ = self.get_macro_arg(decl_str, npos)
func_modlist.append("/phantom")
npos = decl_str.find("CV_WRAP_MAPPABLE")
if npos >= 0:
mappable, npos3 = self.get_macro_arg(decl_str, npos)
func_modlist.append("/mappable="+mappable)
classname = top[1]
return ['.'.join([classname, classname]), None, func_modlist, [], None, None]
virtual_method = False
pure_virtual_method = False
const_method = False
# filter off some common prefixes, which are meaningless for Python wrappers.
# note that we do not strip "static" prefix, which does matter;
# it means class methods, not instance methods
decl_str = self.batch_replace(decl_str, [("static inline", ""), ("inline", ""),
("CV_EXPORTS_W", ""), ("CV_EXPORTS", ""), (
"CV_CDECL", ""), ("CV_WRAP ", " "), ("CV_INLINE", ""),
("CV_DEPRECATED", ""), ("CV_DEPRECATED_EXTERNAL", "")]).strip()
if decl_str.strip().startswith('virtual'):
virtual_method = True
decl_str = decl_str.replace('virtual', '')
end_tokens = decl_str[decl_str.rfind(')'):].split()
const_method = 'const' in end_tokens
pure_virtual_method = '=' in end_tokens and '0' in end_tokens
static_method = False
context = top[0]
if decl_str.startswith("static") and (context == "class" or context == "struct"):
decl_str = decl_str[len("static"):].lstrip()
static_method = True
args_begin = decl_str.find("(")
if decl_str.startswith("CVAPI"):
rtype_end = decl_str.find(")", args_begin+1)
if rtype_end < 0:
print("Error at %d. no terminating ) in CVAPI() macro: %s" %
(self.lineno, decl_str))
sys.exit(-1)
decl_str = decl_str[args_begin+1:rtype_end] + \
" " + decl_str[rtype_end+1:]
args_begin = decl_str.find("(")
if args_begin < 0:
print("Error at %d: no args in '%s'" % (self.lineno, decl_str))
sys.exit(-1)
decl_start = decl_str[:args_begin].strip()
# handle operator () case
if decl_start.endswith("operator"):
args_begin = decl_str.find("(", args_begin+1)
if args_begin < 0:
print("Error at %d: no args in '%s'" % (self.lineno, decl_str))
sys.exit(-1)
decl_start = decl_str[:args_begin].strip()
# TODO: normalize all type of operators
if decl_start.endswith("()"):
decl_start = decl_start[0:-2].rstrip() + " ()"
# constructor/destructor case
if bool(re.match(r'^(\w+::)*(?P<x>\w+)::~?(?P=x)$', decl_start)):
decl_start = "void " + decl_start
rettype, funcname, modlist, argno = self.parse_arg(decl_start, -1)
# determine original return type, hack for return types with underscore
original_type = None
i = decl_start.rfind(funcname)
if i > 0:
original_type = decl_start[:i].replace(
"&", "").replace("const", "").strip()
if argno >= 0:
classname = top[1]
if rettype == classname or rettype == "~" + classname:
rettype, funcname = "", rettype
else:
if bool(re.match('\w+\s+\(\*\w+\)\s*\(.*\)', decl_str)):
return [] # function typedef
elif bool(re.match('\w+\s+\(\w+::\*\w+\)\s*\(.*\)', decl_str)):
return [] # class method typedef
elif bool(re.match('[A-Z_]+', decl_start)):
return [] # it seems to be a macro instantiation
elif "__declspec" == decl_start:
return []
elif bool(re.match(r'\w+\s+\(\*\w+\)\[\d+\]', decl_str)):
return [] # exotic - dynamic 2d array
else:
# print rettype, funcname, modlist, argno
print("Error at %s:%d the function/method name is missing: '%s'" %
(self.hname, self.lineno, decl_start))
sys.exit(-1)
if (self.wrap_mode or HACK) and (("::" in funcname) or funcname.startswith("~")):
# if there is :: in function name (and this is in the header file),
# it means, this is inline implementation of a class method.
# Thus the function has been already declared within the class and we skip this repeated
# declaration.
# Also, skip the destructors, as they are always wrapped
return []
funcname = self.get_dotted_name(funcname)
if not (self.wrap_mode or HACK2):
decl = self.parse_func_decl_no_wrap(
decl_str, static_method, docstring)
decl[0] = funcname
return decl
arg_start = args_begin+1
npos = arg_start-1
balance = 1
angle_balance = 0
# scan the argument list; handle nested parentheses
args_decls = []
args = []
argno = 1
while balance > 0:
npos += 1
t, npos = self.find_next_token(
decl_str, ["(", ")", ",", "<", ">"], npos)
if not t:
print("Error: no closing ')' at %d" % (self.lineno,))
sys.exit(-1)
if t == "<":
angle_balance += 1
if t == ">":
angle_balance -= 1
if t == "(":
balance += 1
if t == ")":
balance -= 1
if (t == "," and balance == 1 and angle_balance == 0) or balance == 0:
# process next function argument
a = decl_str[arg_start:npos].strip()
# print "arg = ", a
arg_start = npos+1
if a:
eqpos = a.find("=")
defval = ""
modlist = []
if eqpos >= 0:
defval = a[eqpos+1:].strip()
else:
eqpos = a.find("CV_DEFAULT")
if eqpos >= 0:
defval, pos3 = self.get_macro_arg(a, eqpos)
else:
eqpos = a.find("CV_WRAP_DEFAULT")
if eqpos >= 0:
defval, pos3 = self.get_macro_arg(a, eqpos)
if defval == "NULL":
defval = "0"
if eqpos >= 0:
a = a[:eqpos].strip()
arg_type, arg_name, modlist, argno = self.parse_arg(
a, argno)
if self.wrap_mode or HACK3:
# TODO: Vectors should contain UMat, but this is not very easy to support and not very needed
vector_mat = "vector_{}".format("Mat")
vector_mat_template = "vector<{}>".format("Mat")
if arg_type == "InputArray":
arg_type = mat
elif arg_type == "InputOutputArray":
arg_type = mat
modlist.append("/IO")
elif arg_type == "OutputArray":
arg_type = mat
modlist.append("/O")
elif arg_type == "InputArrayOfArrays":
arg_type = vector_mat
elif arg_type == "InputOutputArrayOfArrays":
arg_type = vector_mat
modlist.append("/IO")
elif arg_type == "OutputArrayOfArrays":
arg_type = vector_mat
modlist.append("/O")
defval = self.batch_replace(defval, [("InputArrayOfArrays", vector_mat_template),
("InputOutputArrayOfArrays",
vector_mat_template),
("OutputArrayOfArrays",
vector_mat_template),
("InputArray", mat),
("InputOutputArray", mat),
("OutputArray", mat),
("noArray", arg_type)]).strip()
args.append([arg_type, arg_name, defval, modlist])
npos = arg_start-1
if static_method:
func_modlist.append("/S")
if const_method:
func_modlist.append("/C")
if virtual_method:
func_modlist.append("/V")
if pure_virtual_method:
func_modlist.append("/PV")
return [funcname, rettype, func_modlist, args, original_type, docstring]
def get_dotted_name(self, name):
"""
adds the dot-separated container class/namespace names to the bare function/class name, e.g. when we have
namespace cv {
class A {
public:
f(int);
};
}
the function will convert "A" to "cv.A" and "f" to "cv.A.f".
"""
if not self.block_stack:
return name
if name.startswith("cv."):
return name
qualified_name = (("." in name) or ("::" in name))
n = ""
for b in self.block_stack:
block_type, block_name = b[self.BLOCK_TYPE], b[self.BLOCK_NAME]
if block_type in ["file", "enum"]:
continue
if block_type in ["enum struct", "enum class"] and block_name == name:
continue
if block_type not in ["struct", "class", "namespace", "enum struct", "enum class"]:
print("Error at %d: there are non-valid entries in the current block stack %s" %
(self.lineno, self.block_stack))
sys.exit(-1)
if block_name and (block_type == "namespace" or not qualified_name):
n += block_name + "."
n += name.replace("::", ".")
if n.endswith(".Algorithm"):
n = "cv.Algorithm"
return n
def parse_stmt(self, stmt, end_token, mat="Mat", docstring=""):
"""
parses the statement (ending with ';' or '}') or a block head (ending with '{')
The function calls parse_class_decl or parse_func_decl when necessary. It returns
<block_type>, <block_name>, <parse_flag>, <declaration>
where the first 3 values only make sense for blocks (i.e. code blocks, namespaces, classes, enums and such)
"""
stack_top = self.block_stack[-1]
context = stack_top[self.BLOCK_TYPE]
stmt_type = ""
if end_token == "{":
stmt_type = "block"
if context == "block":
print("Error at %d: should not call parse_stmt inside blocks" %
(self.lineno,))
sys.exit(-1)
if context == "class" or context == "struct":
while 1:
colon_pos = stmt.find(":")
if colon_pos < 0:
break
w = stmt[:colon_pos].strip()
if w in ["public", "protected", "private"]:
if w == "public" or (not (self.wrap_mode or HACK) and w == "protected"):
stack_top[self.PUBLIC_SECTION] = True
else:
stack_top[self.PUBLIC_SECTION] = False
stmt = stmt[colon_pos+1:].strip()
break
# do not process hidden class members and template classes/functions
if not stack_top[self.PUBLIC_SECTION] or stmt.startswith("template"):
return stmt_type, "", False, None
if end_token == "{":
if not (self.wrap_mode or HACK) and stmt.startswith("typedef struct"):
stmt_type = "struct"
try:
classname, bases, modlist = self.parse_class_decl(
stmt[len("typedef "):])
except:
print("Error at %s:%d" % (self.hname, self.lineno))
exit(1)
if classname.startswith("_Ipl"):
classname = classname[1:]
decl = [
stmt_type + " " + self.get_dotted_name(classname), "", modlist, [], None, docstring]
if bases:
decl[1] = ": " + \
", ".join([self.get_dotted_name(
b).replace(".", "::") for b in bases])
return stmt_type, classname, True, decl
if stmt.startswith("class") or stmt.startswith("struct"):
stmt_type = stmt.split()[0]
if stmt.strip() != stmt_type:
try:
classname, bases, modlist = self.parse_class_decl(stmt)
except:
print("Error at %s:%d" % (self.hname, self.lineno))
exit(1)
decl = []
# and ("CV_EXPORTS" in stmt)):
if ("CV_EXPORTS_W" in stmt) or ("CV_EXPORTS_AS" in stmt) or (not (self.wrap_mode or HACK)):
decl = [
stmt_type + " " + self.get_dotted_name(classname), "", modlist, [], None, docstring]
if bases:
decl[1] = ": " + ", ".join(
[self.get_dotted_name(b).replace(".", "::") for b in bases])
return stmt_type, classname, True, decl
if stmt.startswith("enum") or stmt.startswith("namespace"):
stmt_list = stmt.rsplit(" ", 1)
if len(stmt_list) < 2:
stmt_list.append("<unnamed>")
return stmt_list[0], stmt_list[1], True, None
if stmt.startswith("extern") and "\"C\"" in stmt:
return "namespace", "", True, None
if end_token == "}" and context.startswith("enum"):
decl = self.parse_enum(stmt)
name = stack_top[self.BLOCK_NAME]
return context, name, False, decl
if end_token == ";" and stmt.startswith("typedef"):
# TODO: handle typedef's more intelligently
return stmt_type, "", False, None
paren_pos = stmt.find("(")
if paren_pos >= 0:
# assume it's function or method declaration,
# since we filtered off the other places where '(' can normally occur:
# - code blocks
# - function pointer typedef's
decl = self.parse_func_decl(stmt, mat=mat, docstring=docstring)
# we return parse_flag == False to prevent the parser to look inside function/method bodies
# (except for tracking the nested blocks)
return stmt_type, "", False, decl
if (context == "struct" or context == "class") and end_token == ";" and stmt:
# looks like it's member declaration; append the members to the class declaration
class_decl = stack_top[self.CLASS_DECL]
# or (class_decl and ("/Map" in class_decl[2])):
if ("CV_PROP" in stmt):
var_modlist = []
if "CV_PROP_RW" in stmt:
var_modlist.append("/RW")
stmt = self.batch_replace(
stmt, [("CV_PROP_RW", ""), ("CV_PROP", "")]).strip()
var_list = stmt.split(",")
var_type, var_name1, modlist, argno = self.parse_arg(
var_list[0], -1)
var_list = [var_name1] + [i.strip() for i in var_list[1:]]
for v in var_list:
class_decl[3].append([var_type, v, "", var_modlist])
return stmt_type, "", False, None
# something unknown
return stmt_type, "", False, None
def find_next_token(self, s, tlist, p=0):
"""
Finds the next token from the 'tlist' in the input 's', starting from position 'p'.
Returns the first occurred token and its position, or ("", len(s)) when no token is found
"""
token = ""
tpos = len(s)
for t in tlist:
pos = s.find(t, p)
if pos < 0:
continue
if pos < tpos:
tpos = pos
token = t
return token, tpos
def parse(self, hname, wmode=True):
"""
The main method. Parses the input file.
Returns the list of declarations (that can be print using print_decls)
"""
self.hname = hname
decls = []
f = io.open(hname, 'rt', encoding='utf-8')
linelist = list(f.readlines())
f.close()
# states:
SCAN = 0 # outside of a comment or preprocessor directive
COMMENT = 1 # inside a multi-line comment
DIRECTIVE = 2 # inside a multi-line preprocessor directive
DOCSTRING = 3 # inside a multi-line docstring
DIRECTIVE_IF_0 = 4 # inside a '#if 0' directive
state = SCAN
self.block_stack = [["file", hname, True, True, None]]
block_head = ""
docstring = ""
self.lineno = 0
self.wrap_mode = wmode
depth_if_0 = 0
for l0 in linelist:
self.lineno += 1
#print(state, self.lineno, l0)
l = l0.strip()
if state == SCAN and l.startswith("#"):
state = DIRECTIVE
# fall through to the if state == DIRECTIVE check
if state == DIRECTIVE:
if l.endswith("\\"):
continue
state = SCAN
l = re.sub(r'//(.+)?', '', l).strip() # drop // comment
if l == '#if 0' or l == '#if defined(__OPENCV_BUILD)' or l == '#ifdef __OPENCV_BUILD':
state = DIRECTIVE_IF_0
depth_if_0 = 1
continue
if state == DIRECTIVE_IF_0:
if l.startswith('#'):
l = l[1:].strip()
if l.startswith("if"):
depth_if_0 += 1
continue
if l.startswith("endif"):
depth_if_0 -= 1
if depth_if_0 == 0:
state = SCAN
else:
# print('---- {:30s}:{:5d}: {}'.format(hname[-30:], self.lineno, l))
pass
continue
if state == COMMENT:
pos = l.find("*/")
if pos < 0:
continue
l = l[pos+2:]
state = SCAN
if state == DOCSTRING:
pos = l.find("*/")
if pos < 0:
docstring += l0
continue
docstring += l[:pos] + "\n"
l = l[pos+2:]
state = SCAN
if l.startswith('CV__') or l.startswith('__CV_'): # just ignore these lines
#print('IGNORE: ' + l)
state = SCAN
continue
if state != SCAN:
print("Error at %d: invalid state = %d" % (self.lineno, state))
sys.exit(-1)
while 1:
token, pos = self.find_next_token(
l, [";", "\"", "{", "}", "//", "/*"])
if not token:
block_head += " " + l
block_head = block_head.strip()
if len(block_head) > 0 and block_head[-1] == ')' and block_head.startswith('CV_ENUM_FLAGS('):
l = ''
token = ';'
else:
break
if token == "//":
block_head += " " + l[:pos]
l = ''
continue
if token == "/*":
block_head += " " + l[:pos]
end_pos = l.find("*/", pos+2)
if len(l) > pos + 2 and l[pos+2] == "*":
# '/**', it's a docstring
if end_pos < 0:
state = DOCSTRING
docstring = l[pos+3:] + "\n"
break
else:
docstring = l[pos+3:end_pos]
elif end_pos < 0:
state = COMMENT
break
l = l[end_pos+2:]
continue
if token == "\"":
pos2 = pos + 1
while 1:
t2, pos2 = self.find_next_token(l, ["\\", "\""], pos2)
if t2 == "":
print("Error at %d: no terminating '\"'" %
(self.lineno,))
sys.exit(-1)
if t2 == "\"":
break
pos2 += 2
block_head += " " + l[:pos2+1]
l = l[pos2+1:]
continue
stmt = (block_head + " " + l[:pos]).strip()
stmt = " ".join(stmt.split()) # normalize the statement
# print(stmt)
stack_top = self.block_stack[-1]
if stmt.startswith("@"):
# Objective C ?
break
decl = None
if stack_top[self.PROCESS_FLAG]:
# even if stack_top[PUBLIC_SECTION] is False, we still try to process the statement,
# since it can start with "public:"
docstring = docstring.strip()
stmt_type, name, parse_flag, decl = self.parse_stmt(
stmt, token, docstring=docstring)
if decl:
if stmt_type.startswith("enum"):
decls.append(
[stmt_type + " " + self.get_dotted_name(name), "", [], decl, None, ""])
else:
decls.append(decl)
if self._generate_gpumat_decls and "cv.cuda" in decl[0]:
# If function takes as one of arguments Mat or vector<Mat> - we want to create the
# same declaration working with GpuMat
args = decl[3]
has_mat = len(
list(filter(lambda x: x[0] in {"Mat", "vector_Mat"}, args))) > 0
if has_mat:
_, _, _, gpumat_decl = self.parse_stmt(
stmt, token, mat="cuda::GpuMat", docstring=docstring)
decls.append(gpumat_decl)
if self._generate_umat_decls:
# If function takes as one of arguments Mat or vector<Mat> - we want to create the
# same declaration working with UMat (this is important for T-Api access)
args = decl[3]
has_mat = len(
list(filter(lambda x: x[0] in {"Mat", "vector_Mat"}, args))) > 0
if has_mat:
_, _, _, umat_decl = self.parse_stmt(