Skip to content

Commit

Permalink
Merge pull request #1183 from kassane/d-comments
Browse files Browse the repository at this point in the history
Add comments in D bindgen
  • Loading branch information
floooh authored Jan 12, 2025
2 parents ffa3bb6 + 3f01ac0 commit 3b0cf41
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion bindgen/gen_d.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import os
import shutil
import sys
import textwrap

import gen_util as util

Expand Down Expand Up @@ -124,6 +125,12 @@ def l(s):
global out_lines
out_lines += s + '\n'

def c(s, indent=""):
if not s:
return
prefix = f"{indent}/// "
l(textwrap.indent(textwrap.dedent(s), prefix=prefix, predicate=lambda line: True))

def as_d_prim_type(s):
return prim_types[s]

Expand Down Expand Up @@ -333,6 +340,7 @@ def funcdecl_result_d(decl, prefix):
def gen_struct(decl, prefix):
struct_name = check_override(decl['name'])
d_type = as_d_struct_type(struct_name, prefix)
c(decl.get('comment'))
l(f"extern(C)\nstruct {d_type} {{")
for field in decl['fields']:
field_name = check_override(field['name'])
Expand Down Expand Up @@ -399,12 +407,15 @@ def gen_struct(decl, prefix):
l("}")

def gen_consts(decl, prefix):
c(decl.get('comment'))
for item in decl['items']:
item_name = check_override(item['name'])
c(decl.get('comment'))
l(f"enum {util.as_lower_snake_case(item_name, prefix)} = {item['value']};")

def gen_enum(decl, prefix):
enum_name = check_override(decl['name'])
c(decl.get('comment'))
l(f"enum {as_d_enum_type(enum_name, prefix)} {{")
for item in decl['items']:
item_name = as_enum_item_name(check_override(item['name']))
Expand All @@ -416,11 +427,13 @@ def gen_enum(decl, prefix):
l("}")

def gen_func_c(decl, prefix):
c(decl.get('comment'))
l(f"extern(C) {funcdecl_result_c(decl, prefix)} {decl['name']}({funcdecl_args_c(decl, prefix)}) @system @nogc nothrow;")

def gen_func_d(decl, prefix):
c_func_name = decl['name']
d_func_name = util.as_lower_camel_case(check_override(decl['name']), prefix)
c(decl.get('comment'))
if c_func_name in c_callbacks:
# a simple forwarded C callback function
l(f"alias {d_func_name} = {c_func_name};")
Expand Down Expand Up @@ -505,7 +518,7 @@ def gen(c_header_path, c_prefix, dep_c_prefixes):
print(f' {c_header_path} => {module_name}')
reset_globals()
shutil.copyfile(c_header_path, f'sokol-d/src/sokol/c/{os.path.basename(c_header_path)}')
ir = gen_ir.gen(c_header_path, c_source_path, module_name, c_prefix, dep_c_prefixes)
ir = gen_ir.gen(c_header_path, c_source_path, module_name, c_prefix, dep_c_prefixes, with_comments=True)
gen_module(ir, dep_c_prefixes)
output_path = f"sokol-d/src/sokol/{ir['module']}.d"
with open(output_path, 'w', newline='\n') as f_outp:
Expand Down

0 comments on commit 3b0cf41

Please sign in to comment.