diff --git a/scripts/cxx-api/parser/builders.py b/scripts/cxx-api/parser/builders.py index f72c62f45e2..c1a74950956 100644 --- a/scripts/cxx-api/parser/builders.py +++ b/scripts/cxx-api/parser/builders.py @@ -38,6 +38,7 @@ from .utils import ( normalize_pointer_spacing, parse_qualified_path, resolve_linked_text_name, + split_specialization, ) from .utils.argument_parsing import _find_matching_angle, _split_arguments @@ -124,8 +125,7 @@ def _fix_inherited_constructor_name( return class_unqualified_name = parse_qualified_path(compound_name)[-1] - # Strip template args for comparison - class_base_name = class_unqualified_name.split("<")[0] + class_base_name, _ = split_specialization(class_unqualified_name) if func_member.name != class_base_name: func_member.name = class_unqualified_name diff --git a/scripts/cxx-api/parser/scope/scope.py b/scripts/cxx-api/parser/scope/scope.py index 6586207ed52..b7703683da4 100644 --- a/scripts/cxx-api/parser/scope/scope.py +++ b/scripts/cxx-api/parser/scope/scope.py @@ -10,7 +10,7 @@ from typing import Generic from natsort import natsorted from ..member import FriendMember, Member, TypedefMember -from ..utils import parse_qualified_path, qualify_template_args_only +from ..utils import parse_qualified_path, qualify_type_str from .base_scope_kind import _natsort_key, ScopeKindT from .enum_scope_kind import EnumScopeKind from .namespace_scope_kind import NamespaceScopeKind @@ -35,10 +35,14 @@ class Scope(Generic[ScopeKindT]): current_scope = self while current_scope is not None: if current_scope.name is not None: - # Qualify template arguments in the scope name if it has any name = current_scope.name - if "<" in name and current_scope.parent_scope is not None: - name = qualify_template_args_only(name, current_scope.parent_scope) + if ( + isinstance(current_scope.kind, StructLikeScopeKind) + and current_scope.kind.specialization_args is not None + ): + name = ( + f"{name}<{', '.join(current_scope.kind.specialization_args)}>" + ) path.append(name) current_scope = current_scope.parent_scope path.reverse() @@ -174,6 +178,18 @@ class Scope(Generic[ScopeKindT]): """ Close the scope by setting the kind of all temporary scopes. """ + # Qualify specialization args early so that members and inner scopes + # see the fully-qualified name when they call get_qualified_name(). + if ( + isinstance(self.kind, StructLikeScopeKind) + and self.kind.specialization_args is not None + and self.parent_scope is not None + ): + self.kind.specialization_args = [ + qualify_type_str(arg, self.parent_scope) + for arg in self.kind.specialization_args + ] + for typedef in self._private_typedefs.values(): typedef.close(self) diff --git a/scripts/cxx-api/parser/scope/struct_like_scope_kind.py b/scripts/cxx-api/parser/scope/struct_like_scope_kind.py index 11c0db3e929..0a06d7e00d1 100644 --- a/scripts/cxx-api/parser/scope/struct_like_scope_kind.py +++ b/scripts/cxx-api/parser/scope/struct_like_scope_kind.py @@ -25,11 +25,14 @@ class StructLikeScopeKind(ScopeKind, Extendable): STRUCT = "struct" UNION = "union" - def __init__(self, type: Type) -> None: + def __init__( + self, type: Type, specialization_args: list[str] | None = None + ) -> None: ScopeKind.__init__(self, type.value) Extendable.__init__(self) self.template_list: TemplateList | None = None + self.specialization_args = specialization_args def add_template(self, template: Template | [Template]) -> None: if template and self.template_list is None: diff --git a/scripts/cxx-api/parser/snapshot.py b/scripts/cxx-api/parser/snapshot.py index 0ffb11df1ed..eca55a0bcb2 100644 --- a/scripts/cxx-api/parser/snapshot.py +++ b/scripts/cxx-api/parser/snapshot.py @@ -15,7 +15,7 @@ from .scope import ( StructLikeScopeKind, TemporaryScopeKind, ) -from .utils import parse_qualified_path +from .utils import parse_qualified_path, split_specialization class Snapshot: @@ -46,19 +46,26 @@ class Snapshot: scope_name = path[-1] current_scope = self.ensure_scope(scope_path) - if scope_name in current_scope.inner_scopes: - scope = current_scope.inner_scopes[scope_name] + base_name, specialization_args = split_specialization(scope_name) + + # Use the full name (including specialization) as the dict key so that + # base templates and their specializations are distinct entries. + scope_key = scope_name + + if scope_key in current_scope.inner_scopes: + scope = current_scope.inner_scopes[scope_key] if scope.kind.name == "temporary": - scope.kind = StructLikeScopeKind(type) + scope.kind = StructLikeScopeKind(type, specialization_args) + scope.name = base_name else: raise RuntimeError( - f"Identifier {scope_name} already exists in scope {current_scope.name}" + f"Identifier {scope_key} already exists in scope {current_scope.name}" ) return scope else: - new_scope = Scope(StructLikeScopeKind(type), scope_name) + new_scope = Scope(StructLikeScopeKind(type, specialization_args), base_name) new_scope.parent_scope = current_scope - current_scope.inner_scopes[scope_name] = new_scope + current_scope.inner_scopes[scope_key] = new_scope return new_scope def create_or_get_namespace(self, qualified_name: str) -> Scope[NamespaceScopeKind]: diff --git a/scripts/cxx-api/parser/utils/__init__.py b/scripts/cxx-api/parser/utils/__init__.py index 2a9b7536adf..bf8858fce2b 100644 --- a/scripts/cxx-api/parser/utils/__init__.py +++ b/scripts/cxx-api/parser/utils/__init__.py @@ -12,6 +12,7 @@ from .argument_parsing import ( parse_arg_string, parse_function_pointer_argstring, parse_type_with_argstrings, + split_specialization, ) from .qualified_path import parse_qualified_path from .text_resolution import ( @@ -37,6 +38,7 @@ __all__ = [ "FunctionModifiers", "InitializerType", "normalize_angle_brackets", + "normalize_pointer_spacing", "parse_arg_string", "parse_function_pointer_argstring", "parse_qualified_path", @@ -46,4 +48,5 @@ __all__ = [ "qualify_template_args_only", "qualify_type_str", "resolve_linked_text_name", + "split_specialization", ] diff --git a/scripts/cxx-api/parser/utils/argument_parsing.py b/scripts/cxx-api/parser/utils/argument_parsing.py index 5a540d3974b..01f4018a2fa 100644 --- a/scripts/cxx-api/parser/utils/argument_parsing.py +++ b/scripts/cxx-api/parser/utils/argument_parsing.py @@ -166,6 +166,22 @@ def _split_arguments(args_str: str) -> list[str]: return [arg for arg in result if arg] +def split_specialization(name: str) -> tuple[str, list[str] | None]: + """Split a potentially specialized name into base name and specialization args. + + Examples: + "Foo" -> ("Foo", None) + "Foo" -> ("Foo", ["int"]) + "Foo" -> ("Foo", ["int", "float"]) + "Foo>" -> ("Foo", ["Bar"]) + """ + angle_start = name.find("<") + if angle_start == -1 or not name.endswith(">"): + return (name, None) + args = _split_arguments(name[angle_start + 1 : -1]) + return (name[:angle_start], args if args else None) + + def _prefix_is_all_qualifiers(prefix: str) -> bool: """Check if all tokens in the prefix are type qualifiers/specifiers.