@@ -235,7 +235,8 @@ def save(self):
235235 f .write (self .buf .getvalue ())
236236
237237
238- def convert_name (name ):
238+ def convert_name (name , is_function = True , create_ocaml_overload_counts = False ,
239+ parent_module = None ):
239240 # strip 'cv.' prefix
240241 if name .startswith ('cv.' ):
241242 name = name [3 :]
@@ -251,14 +252,26 @@ def convert_name(name):
251252 overload_counts [c_name ] += 1
252253 suffix = str (overload_counts [c_name ])
253254 c_name = c_name + suffix
254- ocaml_name = name + suffix
255+ ocaml_name = name if is_function else name + suffix
255256 else :
256257 overload_counts [c_name ] = 0
257258 ocaml_name = name
258259
259260 # convert to camel case
260261 ocaml_name = snake_case (ocaml_name )
261262
263+ # in this case we detect a class method and re-run this function anyway,
264+ # so don't screw up the overload counts
265+ if not ('.' in ocaml_name and is_function ):
266+ overload_key = ocaml_name if parent_module is None \
267+ else parent_module + '.' + ocaml_name
268+ if overload_key in ocaml_overload_counts :
269+ ocaml_overload_counts [overload_key ] += 1
270+ suffix = str (ocaml_overload_counts [overload_key ])
271+ ocaml_name += suffix
272+ elif create_ocaml_overload_counts :
273+ ocaml_overload_counts [overload_key ] = 0
274+
262275 if ocaml_name in ocaml_reserved :
263276 ocaml_name = ocaml_reserved [ocaml_name ]
264277
@@ -344,6 +357,7 @@ def param_sub(match):
344357 defined_enum_consts = set ()
345358 classes = {}
346359 overload_counts = {}
360+ ocaml_overload_counts = {}
347361 draw_functions = []
348362
349363 def add_struct (struct ):
@@ -377,7 +391,7 @@ def build_enum_constr(arg):
377391
378392 def add_class (decl ):
379393 full_class_name = decl [0 ].rsplit (' ' , 1 )[1 ]
380- name , c_name , ocaml_name = convert_name (full_class_name )
394+ name , c_name , ocaml_name = convert_name (full_class_name , is_function = False )
381395 inherits = decl [1 ].rsplit ('::' , 1 )[1 ] if len (decl [1 ]) > 0 else None
382396
383397 cpp_name = name
@@ -432,7 +446,7 @@ def sanitize_param(param):
432446 if cls in classes :
433447 name = name .rsplit ('.' , 1 )[1 ]
434448 c_name = c_name .replace ('.' , '_' )
435- ocaml_name = convert_name (name )[2 ]
449+ ocaml_name = convert_name (name , parent_module = cls )[2 ]
436450
437451 c_params = params [:]
438452
@@ -466,8 +480,8 @@ def sanitize_param(param):
466480 # first pass to collect classes and add types
467481 for decl in decls :
468482 if decl [0 ].startswith ('enum' ):
469- # there's something screwy that happens where sometimes classes show up as enums
470- # this is a really hacky way to make it work
483+ # there's something screwy that happens where sometimes classes
484+ # show up as enums, this is a really hacky way to make it work
471485 if decl [0 ].endswith ('.<unnamed>' ):
472486 decl [0 ] = decl [0 ][:- 10 ]
473487 full_class_name = decl [0 ].rsplit (' ' , 1 )[1 ]
@@ -481,7 +495,26 @@ def sanitize_param(param):
481495 else :
482496 pass
483497
484- # second pass to collect functions and methods
498+ # second pass to identify overloaded OCaml functions
499+ for decl in decls :
500+ if decl [0 ].startswith ('enum' ) or decl [0 ].startswith ('class' ):
501+ pass
502+ else :
503+ name , _ , _ = convert_name (decl [0 ], create_ocaml_overload_counts = True )
504+
505+ if '.' in name :
506+ # class method
507+ cls = name .rsplit ('.' , 1 )[0 ]
508+ if cls in classes :
509+ name = name .rsplit ('.' , 1 )[1 ]
510+ convert_name (name , create_ocaml_overload_counts = True ,
511+ parent_module = cls )
512+
513+ # only add numbers for functions that are actually overloaded
514+ ocaml_overload_counts = \
515+ {name : 0 for name , count in ocaml_overload_counts .items () if count > 0 }
516+
517+ # third pass to collect functions and methods
485518 for decl in decls :
486519 if decl [0 ].startswith ('enum' ):
487520 pass
0 commit comments