From 62ce98db99e421e452525a3244bc0a3ee922c032 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] Split scope definitions into smaller files (#56073) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/56073 Changelog: [Internal] Splits `scope.py` into multiple smaller files, each containing a separate defitnition. Reviewed By: cipolleschi Differential Revision: D96284004 fbshipit-source-id: f99275cc9b5f8844f9a14f9d6207ae988aa9ccd7 --- scripts/cxx-api/parser/scope/__init__.py | 27 ++ .../cxx-api/parser/scope/base_scope_kind.py | 36 +++ .../parser/scope/category_scope_kind.py | 37 +++ .../cxx-api/parser/scope/enum_scope_kind.py | 42 +++ .../parser/scope/interface_scope_kind.py | 72 +++++ .../parser/scope/namespace_scope_kind.py | 40 +++ .../parser/scope/protocol_scope_kind.py | 70 ++++ scripts/cxx-api/parser/{ => scope}/scope.py | 304 +----------------- .../parser/scope/struct_like_scope_kind.py | 93 ++++++ .../parser/scope/temporary_scope_kind.py | 21 ++ 10 files changed, 446 insertions(+), 296 deletions(-) create mode 100644 scripts/cxx-api/parser/scope/__init__.py create mode 100644 scripts/cxx-api/parser/scope/base_scope_kind.py create mode 100644 scripts/cxx-api/parser/scope/category_scope_kind.py create mode 100644 scripts/cxx-api/parser/scope/enum_scope_kind.py create mode 100644 scripts/cxx-api/parser/scope/interface_scope_kind.py create mode 100644 scripts/cxx-api/parser/scope/namespace_scope_kind.py create mode 100644 scripts/cxx-api/parser/scope/protocol_scope_kind.py rename scripts/cxx-api/parser/{ => scope}/scope.py (50%) create mode 100644 scripts/cxx-api/parser/scope/struct_like_scope_kind.py create mode 100644 scripts/cxx-api/parser/scope/temporary_scope_kind.py diff --git a/scripts/cxx-api/parser/scope/__init__.py b/scripts/cxx-api/parser/scope/__init__.py new file mode 100644 index 00000000000..350bad569ba --- /dev/null +++ b/scripts/cxx-api/parser/scope/__init__.py @@ -0,0 +1,27 @@ +# Copyright (c) Meta Platforms, Inc. and affiliates. +# +# This source code is licensed under the MIT license found in the +# LICENSE file in the root directory of this source tree. + +from .base_scope_kind import ScopeKind, ScopeKindT +from .category_scope_kind import CategoryScopeKind +from .enum_scope_kind import EnumScopeKind +from .interface_scope_kind import InterfaceScopeKind +from .namespace_scope_kind import NamespaceScopeKind +from .protocol_scope_kind import ProtocolScopeKind +from .scope import Scope +from .struct_like_scope_kind import StructLikeScopeKind +from .temporary_scope_kind import TemporaryScopeKind + +__all__ = [ + "CategoryScopeKind", + "EnumScopeKind", + "InterfaceScopeKind", + "NamespaceScopeKind", + "ProtocolScopeKind", + "Scope", + "ScopeKind", + "ScopeKindT", + "StructLikeScopeKind", + "TemporaryScopeKind", +] diff --git a/scripts/cxx-api/parser/scope/base_scope_kind.py b/scripts/cxx-api/parser/scope/base_scope_kind.py new file mode 100644 index 00000000000..4c3a0943116 --- /dev/null +++ b/scripts/cxx-api/parser/scope/base_scope_kind.py @@ -0,0 +1,36 @@ +# Copyright (c) Meta Platforms, Inc. and affiliates. +# +# This source code is licensed under the MIT license found in the +# LICENSE file in the root directory of this source tree. + +from __future__ import annotations + +from abc import ABC, abstractmethod +from typing import TYPE_CHECKING, TypeVar + +from natsort import natsort_keygen + +if TYPE_CHECKING: + from .scope import Scope + +# Pre-create natsort key function for efficiency +_natsort_key = natsort_keygen() + + +class ScopeKind(ABC): + def __init__(self, name) -> None: + self.name: str = name + + @abstractmethod + def to_string(self, scope: Scope) -> str: + pass + + def close(self, scope: Scope) -> None: + """Called when the scope is closed. Override to perform cleanup.""" + pass + + def print_scope(self, scope: Scope) -> None: + print(self.to_string(scope)) + + +ScopeKindT = TypeVar("ScopeKindT", bound=ScopeKind) diff --git a/scripts/cxx-api/parser/scope/category_scope_kind.py b/scripts/cxx-api/parser/scope/category_scope_kind.py new file mode 100644 index 00000000000..ac62b7e9d4d --- /dev/null +++ b/scripts/cxx-api/parser/scope/category_scope_kind.py @@ -0,0 +1,37 @@ +# Copyright (c) Meta Platforms, Inc. and affiliates. +# +# This source code is licensed under the MIT license found in the +# LICENSE file in the root directory of this source tree. + +from __future__ import annotations + +from typing import TYPE_CHECKING + +from natsort import natsorted + +from .base_scope_kind import ScopeKind + +if TYPE_CHECKING: + from .scope import Scope + + +class CategoryScopeKind(ScopeKind): + def __init__(self, class_name: str, category_name: str) -> None: + super().__init__("category") + self.class_name: str = class_name + self.category_name: str = category_name + + def to_string(self, scope: Scope) -> str: + result = f"{self.name} {self.class_name}({self.category_name}) {{" + + stringified_members = [] + for member in scope.get_members(): + stringified_members.append(member.to_string(2)) + stringified_members = natsorted(stringified_members) + result += ("\n" if len(stringified_members) > 0 else "") + "\n".join( + stringified_members + ) + + result += "\n}" + + return result diff --git a/scripts/cxx-api/parser/scope/enum_scope_kind.py b/scripts/cxx-api/parser/scope/enum_scope_kind.py new file mode 100644 index 00000000000..1e1addb0214 --- /dev/null +++ b/scripts/cxx-api/parser/scope/enum_scope_kind.py @@ -0,0 +1,42 @@ +# Copyright (c) Meta Platforms, Inc. and affiliates. +# +# This source code is licensed under the MIT license found in the +# LICENSE file in the root directory of this source tree. + +from __future__ import annotations + +from typing import TYPE_CHECKING + +from natsort import natsorted + +from .base_scope_kind import ScopeKind + +if TYPE_CHECKING: + from .scope import Scope + + +class EnumScopeKind(ScopeKind): + def __init__(self) -> None: + super().__init__("enum") + self.type: str | None = None + + def to_string(self, scope: Scope) -> str: + result = "" + inheritance_string = f" : {self.type}" if self.type else "" + + result += ( + "\n" + f"{self.name} {scope.get_qualified_name()}{inheritance_string} {{" + ) + + stringified_members = [] + for member in scope.get_members(): + stringified_members.append(member.to_string(2) + ",") + + stringified_members = natsorted(stringified_members) + result += ("\n" if len(stringified_members) > 0 else "") + "\n".join( + stringified_members + ) + + result += "\n}" + + return result diff --git a/scripts/cxx-api/parser/scope/interface_scope_kind.py b/scripts/cxx-api/parser/scope/interface_scope_kind.py new file mode 100644 index 00000000000..a39517de5b2 --- /dev/null +++ b/scripts/cxx-api/parser/scope/interface_scope_kind.py @@ -0,0 +1,72 @@ +# Copyright (c) Meta Platforms, Inc. and affiliates. +# +# This source code is licensed under the MIT license found in the +# LICENSE file in the root directory of this source tree. + +from __future__ import annotations + +from typing import TYPE_CHECKING + +from natsort import natsorted + +from ..utils import qualify_type_str +from .base_scope_kind import ScopeKind + +if TYPE_CHECKING: + from .scope import Scope + + +class InterfaceScopeKind(ScopeKind): + class Base: + def __init__( + self, name: str, protection: str, virtual: bool, refid: str + ) -> None: + self.name: str = name + self.protection: str = protection + self.virtual: bool = virtual + self.refid: str = refid + + def __init__(self) -> None: + super().__init__("interface") + self.base_classes: [InterfaceScopeKind.Base] = [] + + def add_base( + self, base: InterfaceScopeKind.Base | [InterfaceScopeKind.Base] + ) -> None: + if isinstance(base, list): + for b in base: + self.base_classes.append(b) + else: + self.base_classes.append(base) + + def close(self, scope: Scope) -> None: + """Qualify base class names and their template arguments.""" + for base in self.base_classes: + base.name = qualify_type_str(base.name, scope) + + def to_string(self, scope: Scope) -> str: + result = "" + + bases = [] + for base in self.base_classes: + base_text = [base.protection] + if base.virtual: + base_text.append("virtual") + base_text.append(base.name) + bases.append(" ".join(base_text)) + + inheritance_string = " : " + ", ".join(bases) if bases else "" + + result += f"{self.name} {scope.get_qualified_name()}{inheritance_string} {{" + + stringified_members = [] + for member in scope.get_members(): + stringified_members.append(member.to_string(2)) + stringified_members = natsorted(stringified_members) + result += ("\n" if len(stringified_members) > 0 else "") + "\n".join( + stringified_members + ) + + result += "\n}" + + return result diff --git a/scripts/cxx-api/parser/scope/namespace_scope_kind.py b/scripts/cxx-api/parser/scope/namespace_scope_kind.py new file mode 100644 index 00000000000..0d7444be547 --- /dev/null +++ b/scripts/cxx-api/parser/scope/namespace_scope_kind.py @@ -0,0 +1,40 @@ +# Copyright (c) Meta Platforms, Inc. and affiliates. +# +# This source code is licensed under the MIT license found in the +# LICENSE file in the root directory of this source tree. + +from __future__ import annotations + +from typing import TYPE_CHECKING + +from natsort import natsorted + +from ..member import MemberKind +from .base_scope_kind import ScopeKind + +if TYPE_CHECKING: + from .scope import Scope + + +class NamespaceScopeKind(ScopeKind): + def __init__(self) -> None: + super().__init__("namespace") + + def to_string(self, scope: Scope) -> str: + qualification = scope.get_qualified_name() + + # Group members by kind + groups: dict[MemberKind, list[str]] = {kind: [] for kind in MemberKind} + + for member in scope.get_members(): + kind = member.member_kind + stringified = member.to_string(0, qualification, hide_visibility=True) + groups[kind].append(stringified) + + # Sort within each group and combine in kind order + result = [] + for kind in MemberKind: + sorted_group = natsorted(groups[kind]) + result.extend(sorted_group) + + return "\n".join(result) diff --git a/scripts/cxx-api/parser/scope/protocol_scope_kind.py b/scripts/cxx-api/parser/scope/protocol_scope_kind.py new file mode 100644 index 00000000000..0ae304fad7c --- /dev/null +++ b/scripts/cxx-api/parser/scope/protocol_scope_kind.py @@ -0,0 +1,70 @@ +# Copyright (c) Meta Platforms, Inc. and affiliates. +# +# This source code is licensed under the MIT license found in the +# LICENSE file in the root directory of this source tree. + +from __future__ import annotations + +from typing import TYPE_CHECKING + +from natsort import natsorted + +from ..utils import qualify_type_str +from .base_scope_kind import ScopeKind + +if TYPE_CHECKING: + from .scope import Scope + + +class ProtocolScopeKind(ScopeKind): + class Base: + def __init__( + self, name: str, protection: str, virtual: bool, refid: str + ) -> None: + self.name: str = name + self.protection: str = protection + self.virtual: bool = virtual + self.refid: str = refid + + def __init__(self) -> None: + super().__init__("protocol") + self.base_classes: [ProtocolScopeKind.Base] = [] + + def add_base(self, base: ProtocolScopeKind.Base | [ProtocolScopeKind.Base]) -> None: + if isinstance(base, list): + for b in base: + self.base_classes.append(b) + else: + self.base_classes.append(base) + + def close(self, scope: Scope) -> None: + """Qualify base class names and their template arguments.""" + for base in self.base_classes: + base.name = qualify_type_str(base.name, scope) + + def to_string(self, scope: Scope) -> str: + result = "" + + bases = [] + for base in self.base_classes: + base_text = [base.protection] + if base.virtual: + base_text.append("virtual") + base_text.append(base.name) + bases.append(" ".join(base_text)) + + inheritance_string = " : " + ", ".join(bases) if bases else "" + + result += f"{self.name} {scope.get_qualified_name()}{inheritance_string} {{" + + stringified_members = [] + for member in scope.get_members(): + stringified_members.append(member.to_string(2)) + stringified_members = natsorted(stringified_members) + result += ("\n" if len(stringified_members) > 0 else "") + "\n".join( + stringified_members + ) + + result += "\n}" + + return result diff --git a/scripts/cxx-api/parser/scope.py b/scripts/cxx-api/parser/scope/scope.py similarity index 50% rename from scripts/cxx-api/parser/scope.py rename to scripts/cxx-api/parser/scope/scope.py index 615af32a196..6586207ed52 100644 --- a/scripts/cxx-api/parser/scope.py +++ b/scripts/cxx-api/parser/scope/scope.py @@ -5,304 +5,16 @@ from __future__ import annotations -from abc import ABC, abstractmethod -from enum import Enum -from typing import Generic, TypeVar +from typing import Generic -from natsort import natsort_keygen, natsorted +from natsort import natsorted -from .member import FriendMember, Member, MemberKind, TypedefMember -from .template import Template, TemplateList -from .utils import parse_qualified_path, qualify_template_args_only, qualify_type_str - - -# Pre-create natsort key function for efficiency -_natsort_key = natsort_keygen() - - -class ScopeKind(ABC): - def __init__(self, name) -> None: - self.name: str = name - - @abstractmethod - def to_string(self, scope: Scope) -> str: - pass - - def close(self, scope: Scope) -> None: - """Called when the scope is closed. Override to perform cleanup.""" - pass - - def print_scope(self, scope: Scope) -> None: - print(self.to_string(scope)) - - -class StructLikeScopeKind(ScopeKind): - class Base: - def __init__( - self, name: str, protection: str, virtual: bool, refid: str - ) -> None: - self.name: str = name - self.protection: str = protection - self.virtual: bool = virtual - self.refid: str = refid - - class Type(Enum): - CLASS = "class" - STRUCT = "struct" - UNION = "union" - - def __init__(self, type: Type) -> None: - super().__init__(type.value) - - self.base_classes: [StructLikeScopeKind.Base] = [] - self.template_list: TemplateList | None = None - - def add_base( - self, base: StructLikeScopeKind.Base | [StructLikeScopeKind.Base] - ) -> None: - if isinstance(base, list): - for b in base: - self.base_classes.append(b) - else: - self.base_classes.append(base) - - def add_template(self, template: Template | [Template]) -> None: - if template and self.template_list is None: - self.template_list = TemplateList() - - if isinstance(template, list): - for t in template: - self.template_list.add(t) - else: - self.template_list.add(template) - - def close(self, scope: Scope) -> None: - """Qualify base class names and their template arguments.""" - for base in self.base_classes: - base.name = qualify_type_str(base.name, scope) - - def to_string(self, scope: Scope) -> str: - result = "" - - bases = [] - for base in self.base_classes: - base_text = [base.protection] - if base.virtual: - base_text.append("virtual") - base_text.append(base.name) - bases.append(" ".join(base_text)) - - inheritance_string = " : " + ", ".join(bases) if bases else "" - - if self.template_list is not None: - result += "\n" + self.template_list.to_string() + "\n" - result += f"{self.name} {scope.get_qualified_name()}{inheritance_string} {{" - - stringified_members = [] - for member in scope.get_members(): - stringified_members.append(member.to_string(2)) - stringified_members = natsorted(stringified_members) - result += ("\n" if len(stringified_members) > 0 else "") + "\n".join( - stringified_members - ) - - result += "\n}" - - return result - - -class NamespaceScopeKind(ScopeKind): - def __init__(self) -> None: - super().__init__("namespace") - - def to_string(self, scope: Scope) -> str: - qualification = scope.get_qualified_name() - - # Group members by kind - groups: dict[MemberKind, list[str]] = {kind: [] for kind in MemberKind} - - for member in scope.get_members(): - kind = member.member_kind - stringified = member.to_string(0, qualification, hide_visibility=True) - groups[kind].append(stringified) - - # Sort within each group and combine in kind order - result = [] - for kind in MemberKind: - sorted_group = natsorted(groups[kind]) - result.extend(sorted_group) - - return "\n".join(result) - - -class EnumScopeKind(ScopeKind): - def __init__(self) -> None: - super().__init__("enum") - self.type: str | None = None - - def to_string(self, scope: Scope) -> str: - result = "" - inheritance_string = f" : {self.type}" if self.type else "" - - result += ( - "\n" + f"{self.name} {scope.get_qualified_name()}{inheritance_string} {{" - ) - - stringified_members = [] - for member in scope.get_members(): - stringified_members.append(member.to_string(2) + ",") - - stringified_members = natsorted(stringified_members) - result += ("\n" if len(stringified_members) > 0 else "") + "\n".join( - stringified_members - ) - - result += "\n}" - - return result - - -class ProtocolScopeKind(ScopeKind): - class Base: - def __init__( - self, name: str, protection: str, virtual: bool, refid: str - ) -> None: - self.name: str = name - self.protection: str = protection - self.virtual: bool = virtual - self.refid: str = refid - - def __init__(self) -> None: - super().__init__("protocol") - self.base_classes: [ProtocolScopeKind.Base] = [] - - def add_base(self, base: ProtocolScopeKind.Base | [ProtocolScopeKind.Base]) -> None: - if isinstance(base, list): - for b in base: - self.base_classes.append(b) - else: - self.base_classes.append(base) - - def close(self, scope: Scope) -> None: - """Qualify base class names and their template arguments.""" - for base in self.base_classes: - base.name = qualify_type_str(base.name, scope) - - def to_string(self, scope: Scope) -> str: - result = "" - - bases = [] - for base in self.base_classes: - base_text = [base.protection] - if base.virtual: - base_text.append("virtual") - base_text.append(base.name) - bases.append(" ".join(base_text)) - - inheritance_string = " : " + ", ".join(bases) if bases else "" - - result += f"{self.name} {scope.get_qualified_name()}{inheritance_string} {{" - - stringified_members = [] - for member in scope.get_members(): - stringified_members.append(member.to_string(2)) - stringified_members = natsorted(stringified_members) - result += ("\n" if len(stringified_members) > 0 else "") + "\n".join( - stringified_members - ) - - result += "\n}" - - return result - - -class InterfaceScopeKind(ScopeKind): - class Base: - def __init__( - self, name: str, protection: str, virtual: bool, refid: str - ) -> None: - self.name: str = name - self.protection: str = protection - self.virtual: bool = virtual - self.refid: str = refid - - def __init__(self) -> None: - super().__init__("interface") - self.base_classes: [InterfaceScopeKind.Base] = [] - - def add_base( - self, base: InterfaceScopeKind.Base | [InterfaceScopeKind.Base] - ) -> None: - if isinstance(base, list): - for b in base: - self.base_classes.append(b) - else: - self.base_classes.append(base) - - def close(self, scope: Scope) -> None: - """Qualify base class names and their template arguments.""" - for base in self.base_classes: - base.name = qualify_type_str(base.name, scope) - - def to_string(self, scope: Scope) -> str: - result = "" - - bases = [] - for base in self.base_classes: - base_text = [base.protection] - if base.virtual: - base_text.append("virtual") - base_text.append(base.name) - bases.append(" ".join(base_text)) - - inheritance_string = " : " + ", ".join(bases) if bases else "" - - result += f"{self.name} {scope.get_qualified_name()}{inheritance_string} {{" - - stringified_members = [] - for member in scope.get_members(): - stringified_members.append(member.to_string(2)) - stringified_members = natsorted(stringified_members) - result += ("\n" if len(stringified_members) > 0 else "") + "\n".join( - stringified_members - ) - - result += "\n}" - - return result - - -class CategoryScopeKind(ScopeKind): - def __init__(self, class_name: str, category_name: str) -> None: - super().__init__("category") - self.class_name: str = class_name - self.category_name: str = category_name - - def to_string(self, scope: Scope) -> str: - result = f"{self.name} {self.class_name}({self.category_name}) {{" - - stringified_members = [] - for member in scope.get_members(): - stringified_members.append(member.to_string(2)) - stringified_members = natsorted(stringified_members) - result += ("\n" if len(stringified_members) > 0 else "") + "\n".join( - stringified_members - ) - - result += "\n}" - - return result - - -class TemporaryScopeKind(ScopeKind): - def __init__(self) -> None: - super().__init__("temporary") - - def to_string(self, scope: Scope) -> str: - raise RuntimeError("Temporary scope should not be printed") - - -ScopeKindT = TypeVar("ScopeKindT", bound=ScopeKind) +from ..member import FriendMember, Member, TypedefMember +from ..utils import parse_qualified_path, qualify_template_args_only +from .base_scope_kind import _natsort_key, ScopeKindT +from .enum_scope_kind import EnumScopeKind +from .namespace_scope_kind import NamespaceScopeKind +from .struct_like_scope_kind import StructLikeScopeKind class Scope(Generic[ScopeKindT]): diff --git a/scripts/cxx-api/parser/scope/struct_like_scope_kind.py b/scripts/cxx-api/parser/scope/struct_like_scope_kind.py new file mode 100644 index 00000000000..68a7abc5317 --- /dev/null +++ b/scripts/cxx-api/parser/scope/struct_like_scope_kind.py @@ -0,0 +1,93 @@ +# Copyright (c) Meta Platforms, Inc. and affiliates. +# +# This source code is licensed under the MIT license found in the +# LICENSE file in the root directory of this source tree. + +from __future__ import annotations + +from enum import Enum +from typing import TYPE_CHECKING + +from natsort import natsorted + +from ..template import Template, TemplateList +from ..utils import qualify_type_str +from .base_scope_kind import ScopeKind + +if TYPE_CHECKING: + from .scope import Scope + + +class StructLikeScopeKind(ScopeKind): + class Base: + def __init__( + self, name: str, protection: str, virtual: bool, refid: str + ) -> None: + self.name: str = name + self.protection: str = protection + self.virtual: bool = virtual + self.refid: str = refid + + class Type(Enum): + CLASS = "class" + STRUCT = "struct" + UNION = "union" + + def __init__(self, type: Type) -> None: + super().__init__(type.value) + + self.base_classes: [StructLikeScopeKind.Base] = [] + self.template_list: TemplateList | None = None + + def add_base( + self, base: StructLikeScopeKind.Base | [StructLikeScopeKind.Base] + ) -> None: + if isinstance(base, list): + for b in base: + self.base_classes.append(b) + else: + self.base_classes.append(base) + + def add_template(self, template: Template | [Template]) -> None: + if template and self.template_list is None: + self.template_list = TemplateList() + + if isinstance(template, list): + for t in template: + self.template_list.add(t) + else: + self.template_list.add(template) + + def close(self, scope: Scope) -> None: + """Qualify base class names and their template arguments.""" + for base in self.base_classes: + base.name = qualify_type_str(base.name, scope) + + def to_string(self, scope: Scope) -> str: + result = "" + + bases = [] + for base in self.base_classes: + base_text = [base.protection] + if base.virtual: + base_text.append("virtual") + base_text.append(base.name) + bases.append(" ".join(base_text)) + + inheritance_string = " : " + ", ".join(bases) if bases else "" + + if self.template_list is not None: + result += "\n" + self.template_list.to_string() + "\n" + result += f"{self.name} {scope.get_qualified_name()}{inheritance_string} {{" + + stringified_members = [] + for member in scope.get_members(): + stringified_members.append(member.to_string(2)) + stringified_members = natsorted(stringified_members) + result += ("\n" if len(stringified_members) > 0 else "") + "\n".join( + stringified_members + ) + + result += "\n}" + + return result diff --git a/scripts/cxx-api/parser/scope/temporary_scope_kind.py b/scripts/cxx-api/parser/scope/temporary_scope_kind.py new file mode 100644 index 00000000000..04e3c9953a9 --- /dev/null +++ b/scripts/cxx-api/parser/scope/temporary_scope_kind.py @@ -0,0 +1,21 @@ +# Copyright (c) Meta Platforms, Inc. and affiliates. +# +# This source code is licensed under the MIT license found in the +# LICENSE file in the root directory of this source tree. + +from __future__ import annotations + +from typing import TYPE_CHECKING + +from .base_scope_kind import ScopeKind + +if TYPE_CHECKING: + from .scope import Scope + + +class TemporaryScopeKind(ScopeKind): + def __init__(self) -> None: + super().__init__("temporary") + + def to_string(self, scope: Scope) -> str: + raise RuntimeError("Temporary scope should not be printed")