Skip to content

Commit 89cdf8b

Browse files
j-piaseckimeta-codesync[bot]
authored andcommitted
Move inheritance logic to a separate class (facebook#56074)
Summary: Pull Request resolved: facebook#56074 Changelog: [Internal] Extracts the duplicated handling of inheritance and base classes to a separate, reusable class. Differential Revision: D96287394
1 parent dfde6e4 commit 89cdf8b

4 files changed

Lines changed: 54 additions & 91 deletions

File tree

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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+
9+
class Extendable:
10+
class Base:
11+
def __init__(
12+
self, name: str, protection: str, virtual: bool, refid: str
13+
) -> None:
14+
self.name: str = name
15+
self.protection: str = protection
16+
self.virtual: bool = virtual
17+
self.refid: str = refid
18+
19+
def __init__(self) -> None:
20+
self.base_classes = []
21+
22+
def add_base(self, base: Base | list[Base]) -> None:
23+
if isinstance(base, list):
24+
for b in base:
25+
self.base_classes.append(b)
26+
else:
27+
self.base_classes.append(base)
28+
29+
def get_inheritance_string(self) -> str:
30+
bases = []
31+
for base in self.base_classes:
32+
base_text = [base.protection]
33+
if base.virtual:
34+
base_text.append("virtual")
35+
base_text.append(base.name)
36+
bases.append(" ".join(base_text))
37+
38+
return (" : " + ", ".join(bases)) if bases else ""

scripts/cxx-api/parser/scope/interface_scope_kind.py

Lines changed: 5 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -11,33 +11,16 @@
1111

1212
from ..utils import qualify_type_str
1313
from .base_scope_kind import ScopeKind
14+
from .extendable import Extendable
1415

1516
if TYPE_CHECKING:
1617
from .scope import Scope
1718

1819

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-
20+
class InterfaceScopeKind(ScopeKind, Extendable):
2921
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)
22+
ScopeKind.__init__(self, "interface")
23+
Extendable.__init__(self)
4124

4225
def close(self, scope: Scope) -> None:
4326
"""Qualify base class names and their template arguments."""
@@ -47,16 +30,7 @@ def close(self, scope: Scope) -> None:
4730
def to_string(self, scope: Scope) -> str:
4831
result = ""
4932

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-
33+
inheritance_string = self.get_inheritance_string()
6034
result += f"{self.name} {scope.get_qualified_name()}{inheritance_string} {{"
6135

6236
stringified_members = []

scripts/cxx-api/parser/scope/protocol_scope_kind.py

Lines changed: 5 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -11,31 +11,16 @@
1111

1212
from ..utils import qualify_type_str
1313
from .base_scope_kind import ScopeKind
14+
from .extendable import Extendable
1415

1516
if TYPE_CHECKING:
1617
from .scope import Scope
1718

1819

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-
20+
class ProtocolScopeKind(ScopeKind, Extendable):
2921
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)
22+
ScopeKind.__init__(self, "protocol")
23+
Extendable.__init__(self)
3924

4025
def close(self, scope: Scope) -> None:
4126
"""Qualify base class names and their template arguments."""
@@ -45,16 +30,7 @@ def close(self, scope: Scope) -> None:
4530
def to_string(self, scope: Scope) -> str:
4631
result = ""
4732

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-
33+
inheritance_string = self.get_inheritance_string()
5834
result += f"{self.name} {scope.get_qualified_name()}{inheritance_string} {{"
5935

6036
stringified_members = []

scripts/cxx-api/parser/scope/struct_like_scope_kind.py

Lines changed: 6 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -13,41 +13,24 @@
1313
from ..template import Template, TemplateList
1414
from ..utils import qualify_type_str
1515
from .base_scope_kind import ScopeKind
16+
from .extendable import Extendable
1617

1718
if TYPE_CHECKING:
1819
from .scope import Scope
1920

2021

21-
class StructLikeScopeKind(ScopeKind):
22-
class Base:
23-
def __init__(
24-
self, name: str, protection: str, virtual: bool, refid: str
25-
) -> None:
26-
self.name: str = name
27-
self.protection: str = protection
28-
self.virtual: bool = virtual
29-
self.refid: str = refid
30-
22+
class StructLikeScopeKind(ScopeKind, Extendable):
3123
class Type(Enum):
3224
CLASS = "class"
3325
STRUCT = "struct"
3426
UNION = "union"
3527

3628
def __init__(self, type: Type) -> None:
37-
super().__init__(type.value)
29+
ScopeKind.__init__(self, type.value)
30+
Extendable.__init__(self)
3831

39-
self.base_classes: [StructLikeScopeKind.Base] = []
4032
self.template_list: TemplateList | None = None
4133

42-
def add_base(
43-
self, base: StructLikeScopeKind.Base | [StructLikeScopeKind.Base]
44-
) -> None:
45-
if isinstance(base, list):
46-
for b in base:
47-
self.base_classes.append(b)
48-
else:
49-
self.base_classes.append(base)
50-
5134
def add_template(self, template: Template | [Template]) -> None:
5235
if template and self.template_list is None:
5336
self.template_list = TemplateList()
@@ -66,18 +49,10 @@ def close(self, scope: Scope) -> None:
6649
def to_string(self, scope: Scope) -> str:
6750
result = ""
6851

69-
bases = []
70-
for base in self.base_classes:
71-
base_text = [base.protection]
72-
if base.virtual:
73-
base_text.append("virtual")
74-
base_text.append(base.name)
75-
bases.append(" ".join(base_text))
76-
77-
inheritance_string = " : " + ", ".join(bases) if bases else ""
78-
7952
if self.template_list is not None:
8053
result += "\n" + self.template_list.to_string() + "\n"
54+
55+
inheritance_string = self.get_inheritance_string()
8156
result += f"{self.name} {scope.get_qualified_name()}{inheritance_string} {{"
8257

8358
stringified_members = []

0 commit comments

Comments
 (0)