Skip to content

Commit

Permalink
Tidied up function naming in various corner-cases
Browse files Browse the repository at this point in the history
Added support for forcing disambiguation to disambiguate all variants of a function (#11)
Added support to tell disambiguation to prioritise certain argument types (#11)
Added function renaming and used it to tidy up ImGui_GetColorU32 and ImGui_IsRectVisible (#11, #25)
  • Loading branch information
ShironekoBen committed Sep 19, 2022
1 parent 5c953e6 commit 6b1e534
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 8 deletions.
3 changes: 3 additions & 0 deletions docs/Changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ Split code_dom into multiple files for tidiness and refactored the modules a bit
Improved the header template system somewhat and added a header to all files (#18, #22)
Made it possible to configure types/names of "trivial" arguments that will not get default helpers generated (#24)
Changed order of Ex functions relative to the original (#23)
Added support for forcing disambiguation to disambiguate all variants of a function (#11)
Added support to tell disambiguation to prioritise certain argument types (#11)
Added function renaming and used it to tidy up ImGui_GetColorU32 and ImGui_IsRectVisible (#11, #25)

--- v0.03

Expand Down
23 changes: 22 additions & 1 deletion main.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ def convert_header(src_file, dest_file_no_ext, template_dir):
'const char*': 'Str',
'char*': 'Str',
'unsigned int': 'Uint',
'unsigned int*': 'UintPtr',
'ImGuiID': 'ID',
'const void*': 'Ptr',
'void*': 'Ptr'},
Expand All @@ -188,7 +189,12 @@ def convert_header(src_file, dest_file_no_ext, template_dir):
"cImFileClose",
"cImFileGetSize",
"cImFileRead",
"cImFileWrite"])
"cImFileWrite"],
functions_to_rename_everything=[
"ImGui_CheckboxFlags" # This makes more sense as IntPtr/UIntPtr variants
],
type_priorities={
})
mod_generate_default_argument_functions.apply(dom_root,
# We ignore functions that don't get called often because in those
# cases the default helper doesn't add much value but does clutter
Expand Down Expand Up @@ -297,6 +303,21 @@ def convert_header(src_file, dest_file_no_ext, template_dir):
'popup_flags'
])

# Do some special-case renaming of functions
mod_rename_functions.apply(dom_root, {
# We want the ImGuiCol version of GetColorU32 to be the primary one, but we can't use type_priorities on
# mod_disambiguate_functions to achieve that because it also has more arguments and thus naturally gets passed
# over. Rather than introducing yet another layer of knobs to try and control _that_, we just do some
# after-the-fact renaming here.
'ImGui_GetColorU32': 'ImGui_GetColorU32ImVec4',
'ImGui_GetColorU32ImGuiCol': 'ImGui_GetColorU32',
'ImGui_GetColorU32ImGuiColEx': 'ImGui_GetColorU32Ex',
# ImGui_IsRectVisible is kinda inobvious as it stands, since the two overrides take the exact same type but
# interpret it differently. Hence do some renaming to make it clearer.
'ImGui_IsRectVisible': 'ImGui_IsRectVisibleBySize',
'ImGui_IsRectVisibleImVec2': 'ImGui_IsRectVisible'
})

# Make all functions use CIMGUI_API
mod_make_all_functions_use_imgui_api.apply(dom_root)
mod_rename_defines.apply(dom_root, {'IMGUI_API': 'CIMGUI_API'})
Expand Down
3 changes: 2 additions & 1 deletion src/modifiers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from . import mod_remove_empty_conditionals
from . import mod_make_all_functions_use_imgui_api
from . import mod_rename_defines
from . import mod_rename_functions
from . import mod_forward_declare_structs
from . import mod_mark_by_value_structs
from . import mod_add_includes
Expand All @@ -36,4 +37,4 @@
from . import mod_mark_internal_members
from . import mod_exclude_defines_from_metadata
from . import mod_wrap_with_extern_c
from . import mod_remove_constexpr
from . import mod_remove_constexpr
31 changes: 25 additions & 6 deletions src/modifiers/mod_disambiguate_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,14 @@


# This modifier finds any overloaded functions with identical names and disambiguates them
def apply(dom_root, name_suffix_remaps, functions_to_ignore):
# name_suffix_remaps gives a dictionary remapping type names for types that have awkward or unwanted names
# functions_to_ignore gives a list of functions that are known not to need disambiguation (but look like they do)
# functions_to_rename_everything gives a list of functions where even the "basic" versions should be renamed
# (normally the most simple version of the function does not get a name change)
# type_priorities provides integer priorities for argument types, used in the event of a tie between argument counts
# to decide which function should not get renamed (highest priority, calculated as the sum of priorities for all
# arguments, wins).
def apply(dom_root, name_suffix_remaps, functions_to_ignore, functions_to_rename_everything, type_priorities):
# Find all functions with name collisions

functions_by_name = {} # Contains lists of functions
Expand Down Expand Up @@ -50,13 +57,23 @@ def apply(dom_root, name_suffix_remaps, functions_to_ignore):
break
num_common_args += 1

# Find the function in the set with the smallest argument count
# Find the function in the set with the smallest argument count, using the sum of the priority of the arguments
# as a tie-breaker if required
lowest_arg_count = sys.maxsize
lowest_arg_priority = -1
lowest_arg_function = None
for function in functions:
if len(function.arguments) < lowest_arg_count:
lowest_arg_count = len(function.arguments)
lowest_arg_function = function
if len(function.arguments) <= lowest_arg_count:
function_priority = 0
for arg in function.arguments:
arg_type = arg.arg_type.to_c_string()
if arg_type in type_priorities:
function_priority += type_priorities[arg_type]

if (len(function.arguments) < lowest_arg_count) or (function_priority > lowest_arg_priority):
lowest_arg_count = len(function.arguments)
lowest_arg_priority = function_priority
lowest_arg_function = function

# Add suffixes based on non-common arguments

Expand All @@ -68,13 +85,15 @@ def apply(dom_root, name_suffix_remaps, functions_to_ignore):
suffixes_by_function[function] = function_suffixes

# Do not alter the name of the function with the fewest arguments
if function == lowest_arg_function:
# (unless this is a function where we want to rename everything)
if (function == lowest_arg_function) and (function.name not in functions_to_rename_everything):
continue

for i in range(num_common_args, len(function.arguments)):
if not function.arguments[i].is_varargs: # Don't try and append a suffix for ... arguments
# Check to see if the full type name is in the remap list, and if so remap it
full_name = function.arguments[i].arg_type.to_c_string()

if full_name in name_suffix_remaps:
suffix_name = name_suffix_remaps[full_name]
else:
Expand Down
9 changes: 9 additions & 0 deletions src/modifiers/mod_rename_functions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from src import code_dom


# This modifier renames functions
# Takes a map mapping old names to new names
def apply(dom_root, name_map):
for function in dom_root.list_all_children_of_type(code_dom.DOMFunctionDeclaration):
if function.name in name_map:
function.name = name_map[function.name]

0 comments on commit 6b1e534

Please sign in to comment.