Skip to content

Commit

Permalink
Simplify trampoline class names
Browse files Browse the repository at this point in the history
- Previously the name included the namespace, but now they're in the
  target namespace so it's just redundant information
  • Loading branch information
virtuald committed Dec 18, 2024
1 parent 4efad83 commit 1caea5d
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 31 deletions.
9 changes: 6 additions & 3 deletions robotpy_build/autowrap/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,12 @@ class BaseClassData:
Render data for each base that a class inherits
"""

#: Just the class name
cls_name: str

#: This ends with ::
namespace_: str

#: C++ name, including all known components
full_cpp_name: str # was x_qualname

Expand All @@ -296,9 +302,6 @@ class BaseClassData:
#: turned into underscores.
full_cpp_name_identifier: str # was x_qualname_

#: This ends with ::
namespace_: str

#: C++ name + components, no template parameters
dep_cpp_name: str

Expand Down
41 changes: 23 additions & 18 deletions robotpy_build/autowrap/cxxparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ def _count_and_unwrap(

def _fmt_base_name(
typename: PQName,
) -> typing.Tuple[str, str, str, str, typing.List[str]]:
) -> typing.Tuple[str, str, str, str, str, typing.List[str]]:
all_parts = []
nameonly_parts = []

Expand Down Expand Up @@ -222,6 +222,7 @@ def _fmt_base_name(
tparam_list = []

return (
last_segment.name,
"::".join(most_parts),
"::".join(all_parts),
"::".join(nameonly_parts),
Expand Down Expand Up @@ -292,7 +293,6 @@ class ClassStateData(typing.NamedTuple):
defer_protected_fields: typing.List[Field]

# Needed for trampoline
cls_cpp_identifier: str
template_argument_list: str
base_template_params: str
base_template_args: str
Expand Down Expand Up @@ -756,7 +756,6 @@ def on_class_start(self, state: AWClassBlockState) -> typing.Optional[bool]:
defer_private_virtual_methods=[],
defer_protected_fields=[],
# Trampoline data
cls_cpp_identifier=cls_cpp_identifier,
template_argument_list=template_argument_list,
base_template_args=base_template_args_s,
base_template_params=base_template_params_s,
Expand Down Expand Up @@ -829,33 +828,38 @@ def _process_class_bases(
if base.access == "private":
continue

cpp_name, cpp_name_w_templates, dep_cpp_name, base_ns, tparam_list = (
_fmt_base_name(base.typename)
)
(
cpp_name,
full_cpp_name,
cpp_name_w_templates,
dep_cpp_name,
base_ns,
tparam_list,
) = _fmt_base_name(base.typename)
if ignored_bases.pop(cpp_name_w_templates, None):
continue

# Sometimes, we can't guess all the information about the base, so the
# user needs to specify it explicitly.
user_bqual = class_data.base_qualnames.get(cpp_name)
user_bqual = class_data.base_qualnames.get(full_cpp_name)
if user_bqual:
cpp_name_w_templates = user_bqual
# TODO: sometimes need to add this to pybase_params, but
# that would require parsing this more. Seems sufficiently
# obscure, going to omit it for now.
tp = user_bqual.find("<")
if tp == -1:
cpp_name = user_bqual
full_cpp_name = user_bqual
template_params = ""
else:
cpp_name = user_bqual[:tp]
full_cpp_name = user_bqual[:tp]
template_params = user_bqual[tp + 1 : -1]
dep_cpp_name = cpp_name
ns_idx = cpp_name.rfind("::")
dep_cpp_name = full_cpp_name
ns_idx = full_cpp_name.rfind("::")
if ns_idx == -1:
base_ns = ""
else:
base_ns = cpp_name[:ns_idx]
base_ns = full_cpp_name[:ns_idx]
else:
# TODO: we don't handle nested child classes with templates here
# ... but that has to be rather obscure?
Expand All @@ -869,21 +873,22 @@ def _process_class_bases(
# live in the same namespace as the class
if len(base.typename.segments) == 1:
base_ns = cls_namespace
cpp_name = f"{cls_namespace}::{cpp_name}"
full_cpp_name = f"{cls_namespace}::{full_cpp_name}"
cpp_name_w_templates = f"{cls_namespace}::{cpp_name_w_templates}"
dep_cpp_name = f"{cls_namespace}::{dep_cpp_name}"

base_identifier = cpp_name.translate(_qualname_trans)
base_identifier = full_cpp_name.translate(_qualname_trans)

if base_ns:
base_ns = f"{base_ns}::"

bases.append(
BaseClassData(
full_cpp_name=cpp_name,
cls_name=cpp_name,
namespace_=base_ns,
full_cpp_name=full_cpp_name,
full_cpp_name_w_templates=cpp_name_w_templates,
full_cpp_name_identifier=base_identifier,
namespace_=base_ns,
dep_cpp_name=dep_cpp_name,
template_params=template_params,
)
Expand Down Expand Up @@ -1257,8 +1262,8 @@ def on_class_end(self, state: AWClassBlockState) -> None:
if cdata.template_argument_list:
tmpl = f", {cdata.template_argument_list}"

trampoline_cfg = f"{cdata.ctx.namespace}::PyTrampolineCfg_{cdata.cls_cpp_identifier}<{cdata.template_argument_list}>"
tname = f"{cdata.ctx.namespace}::PyTrampoline_{cdata.cls_cpp_identifier}<typename {ctx.full_cpp_name}{tmpl}, typename {trampoline_cfg}>"
trampoline_cfg = f"{ctx.namespace}::PyTrampolineCfg_{ctx.cpp_name}<{cdata.template_argument_list}>"
tname = f"{ctx.namespace}::PyTrampoline_{ctx.cpp_name}<typename {ctx.full_cpp_name}{tmpl}, typename {trampoline_cfg}>"
tvar = f"{ctx.cpp_name}_Trampoline"

ctx.trampoline = TrampolineData(
Expand Down
20 changes: 10 additions & 10 deletions robotpy_build/autowrap/render_cls_rpy_include.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,20 +124,20 @@ def _render_cls_trampoline(
)

if cls.bases:
r.writeln(f"struct PyTrampolineCfg_{cls.full_cpp_name_identifier} :")
r.writeln(f"struct PyTrampolineCfg_{cls.cpp_name} :")

with r.indent():
for base in cls.bases:
r.writeln(
f"{base.namespace_}PyTrampolineCfg_{base.full_cpp_name_identifier}<{postcomma(base.template_params)}"
f"{base.namespace_}PyTrampolineCfg_{base.cls_name}<{postcomma(base.template_params)}"
)

r.writeln("CfgBase")

for base in cls.bases:
r.writeln(">")
else:
r.writeln(f"struct PyTrampolineCfg_{cls.full_cpp_name_identifier} : CfgBase")
r.writeln(f"struct PyTrampolineCfg_{cls.cpp_name} : CfgBase")

r.writeln("{")

Expand All @@ -162,11 +162,11 @@ def _render_cls_trampoline(
r.writeln(
f"template <typename PyTrampolineBase{precomma(template_parameter_list)}, typename PyTrampolineCfg>"
)
r.writeln(f"using PyTrampolineBase_{cls.full_cpp_name_identifier} =")
r.writeln(f"using PyTrampolineBase_{cls.cpp_name} =")

for base in cls.bases:
r.rel_indent(2)
r.writeln(f"{base.namespace_}PyTrampoline_{base.full_cpp_name_identifier}<")
r.writeln(f"{base.namespace_}PyTrampoline_{base.cls_name}<")

with r.indent():
r.writeln("PyTrampolineBase")
Expand All @@ -182,8 +182,8 @@ def _render_cls_trampoline(
;
template <typename PyTrampolineBase{ precomma(template_parameter_list) }, typename PyTrampolineCfg>
struct PyTrampoline_{ cls.full_cpp_name_identifier } : PyTrampolineBase_{ cls.full_cpp_name_identifier }<PyTrampolineBase{ precomma(template_argument_list) }, PyTrampolineCfg> {{
using PyTrampolineBase_{ cls.full_cpp_name_identifier }<PyTrampolineBase{ precomma(template_argument_list) }, PyTrampolineCfg>::PyTrampolineBase_{ cls.full_cpp_name_identifier };
struct PyTrampoline_{ cls.cpp_name } : PyTrampolineBase_{ cls.cpp_name }<PyTrampolineBase{ precomma(template_argument_list) }, PyTrampolineCfg> {{
using PyTrampolineBase_{ cls.cpp_name }<PyTrampolineBase{ precomma(template_argument_list) }, PyTrampolineCfg>::PyTrampolineBase_{ cls.cpp_name };
"""
)

Expand All @@ -192,7 +192,7 @@ def _render_cls_trampoline(
r.write_trim(
f"""
template <typename PyTrampolineBase{ precomma(template_parameter_list) }, typename PyTrampolineCfg>
struct PyTrampoline_{ cls.full_cpp_name_identifier } : PyTrampolineBase {{
struct PyTrampoline_{ cls.cpp_name } : PyTrampolineBase {{
using PyTrampolineBase::PyTrampolineBase;
"""
)
Expand Down Expand Up @@ -232,11 +232,11 @@ def _render_cls_trampoline(
with r.indent():
all_decls = ", ".join(p.decl for p in fn.all_params)
all_names = ", ".join(p.arg_name for p in fn.all_params)
r.writeln(f"PyTrampoline_{cls.full_cpp_name_identifier}({all_decls}) :")
r.writeln(f"PyTrampoline_{cls.cpp_name}({all_decls}) :")

if cls.bases:
r.writeln(
f" PyTrampolineBase_{cls.full_cpp_name_identifier}<PyTrampolineBase{precomma(trampoline.tmpl_args)}, PyTrampolineCfg>({all_names})"
f" PyTrampolineBase_{cls.cpp_name}<PyTrampolineBase{precomma(trampoline.tmpl_args)}, PyTrampolineCfg>({all_names})"
)
else:
r.writeln(f" PyTrampolineBase({all_names})")
Expand Down

0 comments on commit 1caea5d

Please sign in to comment.