Skip to content

Commit 465f45d

Browse files
committed
AST: modify pretty printing of function pointer types
1 parent 29fcc82 commit 465f45d

File tree

3 files changed

+52
-13
lines changed

3 files changed

+52
-13
lines changed

chb/ast/ASTCPrettyPrinter.py

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ def write_local_declarations(self, referenced: Set[str]) -> None:
151151
and not self.localsymboltable.is_formal(vinfo.vname)):
152152
self.ccode.newline(indent=self.indent)
153153
if vinfo.vtype is None:
154-
self.ccode.write("? " + vinfo.vname)
154+
self.ccode.write("? " + vinfo.vname + ";")
155155
continue
156156

157157
if vinfo.vtype.is_array:
@@ -185,14 +185,19 @@ def write_global_declarations(self) -> None:
185185
continue
186186
self.ccode.newline(indent=self.indent)
187187
if vinfo.vtype is None:
188-
self.ccode.write("? " + vinfo.vname)
188+
self.ccode.write("? " + vinfo.vname + ";")
189189
continue
190190

191191
if vinfo.vtype.is_function:
192192
ftype = cast(AST.ASTTypFun, vinfo.vtype)
193-
ftype.returntyp.accept(self)
194-
self.ccode.write(" ")
195-
self.ccode.write(vinfo.vname)
193+
if ftype.returntyp.is_function_pointer:
194+
returntyp = cast(AST.ASTTypPtr, ftype.returntyp)
195+
fnptr = cast(AST.ASTTypFun, returntyp.tgttyp)
196+
self.funtypptr_with_name(fnptr, vinfo.vname)
197+
else:
198+
ftype.returntyp.accept(self)
199+
self.ccode.write(" ")
200+
self.ccode.write(vinfo.vname)
196201
self.ccode.write("(")
197202
if ftype.argtypes is not None:
198203
ftype.argtypes.accept(self)
@@ -608,20 +613,43 @@ def visit_fun_typ(self, t: AST.ASTTypFun) -> None:
608613
t.argtypes.accept(self)
609614
self.ccode.write(")")
610615

616+
def funtypptr_with_name(self, t: AST.ASTTypFun, name: str) -> None:
617+
if t.returntyp.is_function_pointer:
618+
returntyp = cast(AST.ASTTypPtr, t.returntyp)
619+
retty = cast(AST.ASTTypFun, returntyp.tgttyp)
620+
self.funtypptr_with_name(retty, name)
621+
self.ccode.write("(")
622+
if t.argtypes is not None:
623+
t.argtypes.accept(self)
624+
self.ccode.write(")")
625+
else:
626+
t.returntyp.accept(self)
627+
self.ccode.write(" (*")
628+
self.ccode.write(name)
629+
self.ccode.write(")(")
630+
if t.argtypes is not None:
631+
t.argtypes.accept(self)
632+
self.ccode.write(")")
633+
611634
def visit_funargs(self, funargs: AST.ASTFunArgs) -> None:
612635
args = funargs.funargs
613636
if len(args) == 0:
614-
pass
637+
self.ccode.write("void")
615638
else:
616639
for arg in args[:-1]:
617640
arg.accept(self)
618641
self.ccode.write(", ")
619642
args[-1].accept(self)
620643

621644
def visit_funarg(self, funarg: AST.ASTFunArg) -> None:
622-
funarg.argtyp.accept(self)
623-
self.ccode.write(" ")
624-
self.ccode.write(funarg.argname)
645+
if funarg.argtyp.is_function_pointer:
646+
argtyp = cast(AST.ASTTypPtr, funarg.argtyp)
647+
fnptr = cast(AST.ASTTypFun, argtyp.tgttyp)
648+
self.funtypptr_with_name(fnptr, funarg.argname)
649+
else:
650+
funarg.argtyp.accept(self)
651+
self.ccode.write(" ")
652+
self.ccode.write(funarg.argname)
625653

626654
def visit_named_typ(self, t: AST.ASTTypNamed) -> None:
627655
self.ccode.write("typedef ")
@@ -649,6 +677,11 @@ def visit_compinfo(self, cinfo: AST.ASTCompInfo) -> None:
649677
if atype.size_expr is not None:
650678
atype.size_expr.accept(self)
651679
self.ccode.write("];")
680+
elif finfo.fieldtype.is_function_pointer:
681+
fieldtyp = cast(AST.ASTTypPtr, finfo.fieldtype)
682+
funtyp = cast(AST.ASTTypFun, fieldtyp.tgttyp)
683+
self.funtypptr_with_name(funtyp, finfo.fieldname)
684+
self.ccode.write(";")
652685
else:
653686
finfo.fieldtype.accept(self)
654687
self.ccode.write(" ")

chb/ast/ASTNode.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2242,6 +2242,10 @@ def is_pointer(self) -> bool:
22422242
def is_void_pointer(self) -> bool:
22432243
return False
22442244

2245+
@property
2246+
def is_function_pointer(self) -> bool:
2247+
return False
2248+
22452249
@property
22462250
def is_scalar(self) -> bool:
22472251
return (
@@ -2394,6 +2398,10 @@ def is_pointer(self) -> bool:
23942398
def is_void_pointer(self) -> bool:
23952399
return self.tgttyp.is_void
23962400

2401+
@property
2402+
def is_function_pointer(self) -> bool:
2403+
return self.tgttyp.is_function
2404+
23972405
def accept(self, visitor: "ASTVisitor") -> None:
23982406
return visitor.visit_pointer_typ(self)
23992407

chb/ast/astutil.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
# ------------------------------------------------------------------------------
55
# The MIT License (MIT)
66
#
7-
# Copyright (c) 2023 Aarno Labs LLC
7+
# Copyright (c) 2023-2025 Aarno Labs LLC
88
#
99
# Permission is hereby granted, free of charge, to any person obtaining a copy
1010
# of this software and associated documentation files (the "Software"), to deal
@@ -358,7 +358,7 @@ def parsecmd(args: argparse.Namespace) -> NoReturn:
358358
parsemanager.parse_ifile(ifile)
359359

360360
for (faddr, dfn) in functions.items():
361-
fname = faddr.replace("0x", "sub_")
361+
fname = faddr
362362
xpath = os.path.join(cname, "functions")
363363
xpath = os.path.join(xpath, fname)
364364
xfile = os.path.join(xpath, cname + "_" + fname + "_cfun.xml")
@@ -381,6 +381,4 @@ def parsecmd(args: argparse.Namespace) -> NoReturn:
381381

382382
xsbody = rootnode.find("sbody")
383383

384-
385-
386384
exit(0)

0 commit comments

Comments
 (0)