Skip to content

Commit d596a3d

Browse files
coadometa-codesync[bot]
authored andcommitted
Deduplicate code in member classes (#56086)
Summary: Pull Request resolved: #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
1 parent f0b51fc commit d596a3d

File tree

4 files changed

+31
-36
lines changed

4 files changed

+31
-36
lines changed

scripts/cxx-api/parser/member/base.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,17 @@ class MemberKind(IntEnum):
3232
FRIEND = 6
3333

3434

35+
def is_function_pointer_argstring(argstring: str | None) -> bool:
36+
"""Check if an argstring indicates a function pointer type."""
37+
return argstring is not None and argstring.startswith(")(")
38+
39+
3540
class Member(ABC):
3641
def __init__(self, name: str, visibility: str) -> None:
3742
self.name: str = name
3843
self.visibility: str = visibility
3944
self.template_list: TemplateList | None = None
45+
self.specialization_args: list[str] | None = None
4046

4147
@property
4248
@abstractmethod
@@ -55,8 +61,19 @@ def to_string(
5561
def close(self, scope: Scope):
5662
pass
5763

58-
def _get_qualified_name(self, qualification: str | None):
59-
return f"{qualification}::{self.name}" if qualification else self.name
64+
def _get_qualified_name(self, qualification: str | None) -> str:
65+
name = self.name
66+
if self.specialization_args is not None:
67+
name = f"{name}<{', '.join(self.specialization_args)}>"
68+
return f"{qualification}::{name}" if qualification else name
69+
70+
def _qualify_specialization_args(self, scope: Scope) -> None:
71+
if self.specialization_args is not None:
72+
from ..utils import qualify_type_str
73+
74+
self.specialization_args = [
75+
qualify_type_str(arg, scope) for arg in self.specialization_args
76+
]
6077

6178
def add_template(self, template: Template | [Template]) -> None:
6279
if template and self.template_list is None:

scripts/cxx-api/parser/member/function_member.py

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -64,16 +64,7 @@ def member_kind(self) -> MemberKind:
6464
def close(self, scope: Scope):
6565
self.type = qualify_type_str(self.type, scope)
6666
self.arguments = qualify_arguments(self.arguments, scope)
67-
if self.specialization_args is not None:
68-
self.specialization_args = [
69-
qualify_type_str(arg, scope) for arg in self.specialization_args
70-
]
71-
72-
def _get_qualified_name(self, qualification: str | None) -> str:
73-
name = self.name
74-
if self.specialization_args is not None:
75-
name = f"{name}<{', '.join(self.specialization_args)}>"
76-
return f"{qualification}::{name}" if qualification else name
67+
self._qualify_specialization_args(scope)
7768

7869
def to_string(
7970
self,

scripts/cxx-api/parser/member/typedef_member.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
qualify_arguments,
1717
qualify_parsed_type,
1818
)
19-
from .base import Member, MemberKind
19+
from .base import is_function_pointer_argstring, Member, MemberKind
2020

2121
if TYPE_CHECKING:
2222
from ..scope import Scope
@@ -48,14 +48,10 @@ def close(self, scope: Scope):
4848
self._fp_arguments = qualify_arguments(self._fp_arguments, scope)
4949
self._parsed_type = qualify_parsed_type(self._parsed_type, scope)
5050

51-
def _is_function_pointer(self) -> bool:
52-
"""Check if this typedef is a function pointer type."""
53-
return self.argstring is not None and self.argstring.startswith(")(")
54-
5551
def get_value(self) -> str:
5652
if self.keyword == "using":
5753
return format_parsed_type(self._parsed_type)
58-
elif self._is_function_pointer():
54+
elif is_function_pointer_argstring(self.argstring):
5955
formatted_args = format_arguments(self._fp_arguments)
6056
qualified_type = format_parsed_type(self._parsed_type)
6157
if "(*" in qualified_type:
@@ -84,7 +80,7 @@ def to_string(
8480

8581
if self.keyword == "using":
8682
result += f" {name} = {format_parsed_type(self._parsed_type)};"
87-
elif self._is_function_pointer():
83+
elif is_function_pointer_argstring(self.argstring):
8884
formatted_args = format_arguments(self._fp_arguments)
8985
qualified_type = format_parsed_type(self._parsed_type)
9086
# Function pointer typedef: "typedef return_type (*name)(args);"

scripts/cxx-api/parser/member/variable_member.py

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,14 @@
1515
parse_type_with_argstrings,
1616
qualify_arguments,
1717
qualify_parsed_type,
18-
qualify_type_str,
1918
split_specialization,
2019
)
21-
from .base import Member, MemberKind, STORE_INITIALIZERS_IN_SNAPSHOT
20+
from .base import (
21+
is_function_pointer_argstring,
22+
Member,
23+
MemberKind,
24+
STORE_INITIALIZERS_IN_SNAPSHOT,
25+
)
2226

2327
if TYPE_CHECKING:
2428
from ..scope import Scope
@@ -65,20 +69,7 @@ def member_kind(self) -> MemberKind:
6569
def close(self, scope: Scope):
6670
self._fp_arguments = qualify_arguments(self._fp_arguments, scope)
6771
self._parsed_type = qualify_parsed_type(self._parsed_type, scope)
68-
if self.specialization_args is not None:
69-
self.specialization_args = [
70-
qualify_type_str(arg, scope) for arg in self.specialization_args
71-
]
72-
73-
def _is_function_pointer(self) -> bool:
74-
"""Check if this variable is a function pointer type."""
75-
return self.argstring is not None and self.argstring.startswith(")(")
76-
77-
def _get_qualified_name(self, qualification: str | None) -> str:
78-
name = self.name
79-
if self.specialization_args is not None:
80-
name = f"{name}<{', '.join(self.specialization_args)}>"
81-
return f"{qualification}::{name}" if qualification else name
72+
self._qualify_specialization_args(scope)
8273

8374
def to_string(
8475
self,
@@ -108,7 +99,7 @@ def to_string(
10899
if self.is_const and not self.is_constexpr:
109100
result += "const "
110101

111-
if self._is_function_pointer():
102+
if is_function_pointer_argstring(self.argstring):
112103
formatted_args = format_arguments(self._fp_arguments)
113104
qualified_type = format_parsed_type(self._parsed_type)
114105
# Function pointer types: argstring is ")(args...)"

0 commit comments

Comments
 (0)