From d596a3d2f3aa4889b6019d37341c49c16cf5f5a0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dawid=20Ma=C5=82ecki?= Date: Wed, 18 Mar 2026 09:29:02 -0700 Subject: [PATCH] Deduplicate code in member classes (#56086) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/56086 Changelog: [Internal] Deduplicates specialization_args, qualified names and function pointer check in the member classes. Reviewed By: cipolleschi Differential Revision: D96455789 fbshipit-source-id: 835b73406d4469808b3d7fec7601394f7e42313b --- scripts/cxx-api/parser/member/base.py | 21 ++++++++++++++-- .../cxx-api/parser/member/function_member.py | 11 +------- .../cxx-api/parser/member/typedef_member.py | 10 +++----- .../cxx-api/parser/member/variable_member.py | 25 ++++++------------- 4 files changed, 31 insertions(+), 36 deletions(-) diff --git a/scripts/cxx-api/parser/member/base.py b/scripts/cxx-api/parser/member/base.py index 47ce6990612..4edc05684b3 100644 --- a/scripts/cxx-api/parser/member/base.py +++ b/scripts/cxx-api/parser/member/base.py @@ -32,11 +32,17 @@ class MemberKind(IntEnum): FRIEND = 6 +def is_function_pointer_argstring(argstring: str | None) -> bool: + """Check if an argstring indicates a function pointer type.""" + return argstring is not None and argstring.startswith(")(") + + class Member(ABC): def __init__(self, name: str, visibility: str) -> None: self.name: str = name self.visibility: str = visibility self.template_list: TemplateList | None = None + self.specialization_args: list[str] | None = None @property @abstractmethod @@ -55,8 +61,19 @@ class Member(ABC): def close(self, scope: Scope): pass - def _get_qualified_name(self, qualification: str | None): - return f"{qualification}::{self.name}" if qualification else self.name + def _get_qualified_name(self, qualification: str | None) -> str: + name = self.name + if self.specialization_args is not None: + name = f"{name}<{', '.join(self.specialization_args)}>" + return f"{qualification}::{name}" if qualification else name + + def _qualify_specialization_args(self, scope: Scope) -> None: + if self.specialization_args is not None: + from ..utils import qualify_type_str + + self.specialization_args = [ + qualify_type_str(arg, scope) for arg in self.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/member/function_member.py b/scripts/cxx-api/parser/member/function_member.py index 17650c24372..52929e42771 100644 --- a/scripts/cxx-api/parser/member/function_member.py +++ b/scripts/cxx-api/parser/member/function_member.py @@ -64,16 +64,7 @@ class FunctionMember(Member): def close(self, scope: Scope): self.type = qualify_type_str(self.type, scope) self.arguments = qualify_arguments(self.arguments, scope) - if self.specialization_args is not None: - self.specialization_args = [ - qualify_type_str(arg, scope) for arg in self.specialization_args - ] - - def _get_qualified_name(self, qualification: str | None) -> str: - name = self.name - if self.specialization_args is not None: - name = f"{name}<{', '.join(self.specialization_args)}>" - return f"{qualification}::{name}" if qualification else name + self._qualify_specialization_args(scope) def to_string( self, diff --git a/scripts/cxx-api/parser/member/typedef_member.py b/scripts/cxx-api/parser/member/typedef_member.py index 4e7f09b5d3a..8b6e12f2806 100644 --- a/scripts/cxx-api/parser/member/typedef_member.py +++ b/scripts/cxx-api/parser/member/typedef_member.py @@ -16,7 +16,7 @@ from ..utils import ( qualify_arguments, qualify_parsed_type, ) -from .base import Member, MemberKind +from .base import is_function_pointer_argstring, Member, MemberKind if TYPE_CHECKING: from ..scope import Scope @@ -48,14 +48,10 @@ class TypedefMember(Member): self._fp_arguments = qualify_arguments(self._fp_arguments, scope) self._parsed_type = qualify_parsed_type(self._parsed_type, scope) - def _is_function_pointer(self) -> bool: - """Check if this typedef is a function pointer type.""" - return self.argstring is not None and self.argstring.startswith(")(") - def get_value(self) -> str: if self.keyword == "using": return format_parsed_type(self._parsed_type) - elif self._is_function_pointer(): + elif is_function_pointer_argstring(self.argstring): formatted_args = format_arguments(self._fp_arguments) qualified_type = format_parsed_type(self._parsed_type) if "(*" in qualified_type: @@ -84,7 +80,7 @@ class TypedefMember(Member): if self.keyword == "using": result += f" {name} = {format_parsed_type(self._parsed_type)};" - elif self._is_function_pointer(): + elif is_function_pointer_argstring(self.argstring): formatted_args = format_arguments(self._fp_arguments) qualified_type = format_parsed_type(self._parsed_type) # Function pointer typedef: "typedef return_type (*name)(args);" diff --git a/scripts/cxx-api/parser/member/variable_member.py b/scripts/cxx-api/parser/member/variable_member.py index c69378eb835..8c456f6c1e1 100644 --- a/scripts/cxx-api/parser/member/variable_member.py +++ b/scripts/cxx-api/parser/member/variable_member.py @@ -15,10 +15,14 @@ from ..utils import ( parse_type_with_argstrings, qualify_arguments, qualify_parsed_type, - qualify_type_str, split_specialization, ) -from .base import Member, MemberKind, STORE_INITIALIZERS_IN_SNAPSHOT +from .base import ( + is_function_pointer_argstring, + Member, + MemberKind, + STORE_INITIALIZERS_IN_SNAPSHOT, +) if TYPE_CHECKING: from ..scope import Scope @@ -65,20 +69,7 @@ class VariableMember(Member): def close(self, scope: Scope): self._fp_arguments = qualify_arguments(self._fp_arguments, scope) self._parsed_type = qualify_parsed_type(self._parsed_type, scope) - if self.specialization_args is not None: - self.specialization_args = [ - qualify_type_str(arg, scope) for arg in self.specialization_args - ] - - def _is_function_pointer(self) -> bool: - """Check if this variable is a function pointer type.""" - return self.argstring is not None and self.argstring.startswith(")(") - - def _get_qualified_name(self, qualification: str | None) -> str: - name = self.name - if self.specialization_args is not None: - name = f"{name}<{', '.join(self.specialization_args)}>" - return f"{qualification}::{name}" if qualification else name + self._qualify_specialization_args(scope) def to_string( self, @@ -108,7 +99,7 @@ class VariableMember(Member): if self.is_const and not self.is_constexpr: result += "const " - if self._is_function_pointer(): + if is_function_pointer_argstring(self.argstring): formatted_args = format_arguments(self._fp_arguments) qualified_type = format_parsed_type(self._parsed_type) # Function pointer types: argstring is ")(args...)"