Skip to content

Commit e69a5f1

Browse files
j-piaseckimeta-codesync[bot]
authored andcommitted
Split scope definitions into smaller files (#56073)
Summary: Pull Request resolved: #56073 Changelog: [Internal] Splits `scope.py` into multiple smaller files, each containing a separate defitnition. Differential Revision: D96284004
1 parent 2affee4 commit e69a5f1

File tree

10 files changed

+446
-296
lines changed

10 files changed

+446
-296
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Copyright (c) Meta Platforms, Inc. and affiliates.
2+
#
3+
# This source code is licensed under the MIT license found in the
4+
# LICENSE file in the root directory of this source tree.
5+
6+
from .base_scope_kind import ScopeKind, ScopeKindT
7+
from .category_scope_kind import CategoryScopeKind
8+
from .enum_scope_kind import EnumScopeKind
9+
from .interface_scope_kind import InterfaceScopeKind
10+
from .namespace_scope_kind import NamespaceScopeKind
11+
from .protocol_scope_kind import ProtocolScopeKind
12+
from .scope import Scope
13+
from .struct_like_scope_kind import StructLikeScopeKind
14+
from .temporary_scope_kind import TemporaryScopeKind
15+
16+
__all__ = [
17+
"CategoryScopeKind",
18+
"EnumScopeKind",
19+
"InterfaceScopeKind",
20+
"NamespaceScopeKind",
21+
"ProtocolScopeKind",
22+
"Scope",
23+
"ScopeKind",
24+
"ScopeKindT",
25+
"StructLikeScopeKind",
26+
"TemporaryScopeKind",
27+
]
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Copyright (c) Meta Platforms, Inc. and affiliates.
2+
#
3+
# This source code is licensed under the MIT license found in the
4+
# LICENSE file in the root directory of this source tree.
5+
6+
from __future__ import annotations
7+
8+
from abc import ABC, abstractmethod
9+
from typing import TYPE_CHECKING, TypeVar
10+
11+
from natsort import natsort_keygen
12+
13+
if TYPE_CHECKING:
14+
from .scope import Scope
15+
16+
# Pre-create natsort key function for efficiency
17+
_natsort_key = natsort_keygen()
18+
19+
20+
class ScopeKind(ABC):
21+
def __init__(self, name) -> None:
22+
self.name: str = name
23+
24+
@abstractmethod
25+
def to_string(self, scope: Scope) -> str:
26+
pass
27+
28+
def close(self, scope: Scope) -> None:
29+
"""Called when the scope is closed. Override to perform cleanup."""
30+
pass
31+
32+
def print_scope(self, scope: Scope) -> None:
33+
print(self.to_string(scope))
34+
35+
36+
ScopeKindT = TypeVar("ScopeKindT", bound=ScopeKind)
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Copyright (c) Meta Platforms, Inc. and affiliates.
2+
#
3+
# This source code is licensed under the MIT license found in the
4+
# LICENSE file in the root directory of this source tree.
5+
6+
from __future__ import annotations
7+
8+
from typing import TYPE_CHECKING
9+
10+
from natsort import natsorted
11+
12+
from .base_scope_kind import ScopeKind
13+
14+
if TYPE_CHECKING:
15+
from .scope import Scope
16+
17+
18+
class CategoryScopeKind(ScopeKind):
19+
def __init__(self, class_name: str, category_name: str) -> None:
20+
super().__init__("category")
21+
self.class_name: str = class_name
22+
self.category_name: str = category_name
23+
24+
def to_string(self, scope: Scope) -> str:
25+
result = f"{self.name} {self.class_name}({self.category_name}) {{"
26+
27+
stringified_members = []
28+
for member in scope.get_members():
29+
stringified_members.append(member.to_string(2))
30+
stringified_members = natsorted(stringified_members)
31+
result += ("\n" if len(stringified_members) > 0 else "") + "\n".join(
32+
stringified_members
33+
)
34+
35+
result += "\n}"
36+
37+
return result
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Copyright (c) Meta Platforms, Inc. and affiliates.
2+
#
3+
# This source code is licensed under the MIT license found in the
4+
# LICENSE file in the root directory of this source tree.
5+
6+
from __future__ import annotations
7+
8+
from typing import TYPE_CHECKING
9+
10+
from natsort import natsorted
11+
12+
from .base_scope_kind import ScopeKind
13+
14+
if TYPE_CHECKING:
15+
from .scope import Scope
16+
17+
18+
class EnumScopeKind(ScopeKind):
19+
def __init__(self) -> None:
20+
super().__init__("enum")
21+
self.type: str | None = None
22+
23+
def to_string(self, scope: Scope) -> str:
24+
result = ""
25+
inheritance_string = f" : {self.type}" if self.type else ""
26+
27+
result += (
28+
"\n" + f"{self.name} {scope.get_qualified_name()}{inheritance_string} {{"
29+
)
30+
31+
stringified_members = []
32+
for member in scope.get_members():
33+
stringified_members.append(member.to_string(2) + ",")
34+
35+
stringified_members = natsorted(stringified_members)
36+
result += ("\n" if len(stringified_members) > 0 else "") + "\n".join(
37+
stringified_members
38+
)
39+
40+
result += "\n}"
41+
42+
return result
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# Copyright (c) Meta Platforms, Inc. and affiliates.
2+
#
3+
# This source code is licensed under the MIT license found in the
4+
# LICENSE file in the root directory of this source tree.
5+
6+
from __future__ import annotations
7+
8+
from typing import TYPE_CHECKING
9+
10+
from natsort import natsorted
11+
12+
from ..utils import qualify_type_str
13+
from .base_scope_kind import ScopeKind
14+
15+
if TYPE_CHECKING:
16+
from .scope import Scope
17+
18+
19+
class InterfaceScopeKind(ScopeKind):
20+
class Base:
21+
def __init__(
22+
self, name: str, protection: str, virtual: bool, refid: str
23+
) -> None:
24+
self.name: str = name
25+
self.protection: str = protection
26+
self.virtual: bool = virtual
27+
self.refid: str = refid
28+
29+
def __init__(self) -> None:
30+
super().__init__("interface")
31+
self.base_classes: [InterfaceScopeKind.Base] = []
32+
33+
def add_base(
34+
self, base: InterfaceScopeKind.Base | [InterfaceScopeKind.Base]
35+
) -> None:
36+
if isinstance(base, list):
37+
for b in base:
38+
self.base_classes.append(b)
39+
else:
40+
self.base_classes.append(base)
41+
42+
def close(self, scope: Scope) -> None:
43+
"""Qualify base class names and their template arguments."""
44+
for base in self.base_classes:
45+
base.name = qualify_type_str(base.name, scope)
46+
47+
def to_string(self, scope: Scope) -> str:
48+
result = ""
49+
50+
bases = []
51+
for base in self.base_classes:
52+
base_text = [base.protection]
53+
if base.virtual:
54+
base_text.append("virtual")
55+
base_text.append(base.name)
56+
bases.append(" ".join(base_text))
57+
58+
inheritance_string = " : " + ", ".join(bases) if bases else ""
59+
60+
result += f"{self.name} {scope.get_qualified_name()}{inheritance_string} {{"
61+
62+
stringified_members = []
63+
for member in scope.get_members():
64+
stringified_members.append(member.to_string(2))
65+
stringified_members = natsorted(stringified_members)
66+
result += ("\n" if len(stringified_members) > 0 else "") + "\n".join(
67+
stringified_members
68+
)
69+
70+
result += "\n}"
71+
72+
return result
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Copyright (c) Meta Platforms, Inc. and affiliates.
2+
#
3+
# This source code is licensed under the MIT license found in the
4+
# LICENSE file in the root directory of this source tree.
5+
6+
from __future__ import annotations
7+
8+
from typing import TYPE_CHECKING
9+
10+
from natsort import natsorted
11+
12+
from ..member import MemberKind
13+
from .base_scope_kind import ScopeKind
14+
15+
if TYPE_CHECKING:
16+
from .scope import Scope
17+
18+
19+
class NamespaceScopeKind(ScopeKind):
20+
def __init__(self) -> None:
21+
super().__init__("namespace")
22+
23+
def to_string(self, scope: Scope) -> str:
24+
qualification = scope.get_qualified_name()
25+
26+
# Group members by kind
27+
groups: dict[MemberKind, list[str]] = {kind: [] for kind in MemberKind}
28+
29+
for member in scope.get_members():
30+
kind = member.member_kind
31+
stringified = member.to_string(0, qualification, hide_visibility=True)
32+
groups[kind].append(stringified)
33+
34+
# Sort within each group and combine in kind order
35+
result = []
36+
for kind in MemberKind:
37+
sorted_group = natsorted(groups[kind])
38+
result.extend(sorted_group)
39+
40+
return "\n".join(result)
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# Copyright (c) Meta Platforms, Inc. and affiliates.
2+
#
3+
# This source code is licensed under the MIT license found in the
4+
# LICENSE file in the root directory of this source tree.
5+
6+
from __future__ import annotations
7+
8+
from typing import TYPE_CHECKING
9+
10+
from natsort import natsorted
11+
12+
from ..utils import qualify_type_str
13+
from .base_scope_kind import ScopeKind
14+
15+
if TYPE_CHECKING:
16+
from .scope import Scope
17+
18+
19+
class ProtocolScopeKind(ScopeKind):
20+
class Base:
21+
def __init__(
22+
self, name: str, protection: str, virtual: bool, refid: str
23+
) -> None:
24+
self.name: str = name
25+
self.protection: str = protection
26+
self.virtual: bool = virtual
27+
self.refid: str = refid
28+
29+
def __init__(self) -> None:
30+
super().__init__("protocol")
31+
self.base_classes: [ProtocolScopeKind.Base] = []
32+
33+
def add_base(self, base: ProtocolScopeKind.Base | [ProtocolScopeKind.Base]) -> None:
34+
if isinstance(base, list):
35+
for b in base:
36+
self.base_classes.append(b)
37+
else:
38+
self.base_classes.append(base)
39+
40+
def close(self, scope: Scope) -> None:
41+
"""Qualify base class names and their template arguments."""
42+
for base in self.base_classes:
43+
base.name = qualify_type_str(base.name, scope)
44+
45+
def to_string(self, scope: Scope) -> str:
46+
result = ""
47+
48+
bases = []
49+
for base in self.base_classes:
50+
base_text = [base.protection]
51+
if base.virtual:
52+
base_text.append("virtual")
53+
base_text.append(base.name)
54+
bases.append(" ".join(base_text))
55+
56+
inheritance_string = " : " + ", ".join(bases) if bases else ""
57+
58+
result += f"{self.name} {scope.get_qualified_name()}{inheritance_string} {{"
59+
60+
stringified_members = []
61+
for member in scope.get_members():
62+
stringified_members.append(member.to_string(2))
63+
stringified_members = natsorted(stringified_members)
64+
result += ("\n" if len(stringified_members) > 0 else "") + "\n".join(
65+
stringified_members
66+
)
67+
68+
result += "\n}"
69+
70+
return result

0 commit comments

Comments
 (0)