mirror of
https://github.com/facebook/react-native.git
synced 2026-11-18 20:19:07 +00:00
Extract class specializations from name (#56075)
Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/56075 Changelog: [Internal] Updates the parser to extract the template specializations from name string in order to avoid repeated operations on the name string. Reviewed By: cipolleschi Differential Revision: D96303732 fbshipit-source-id: 08ff8ba34522426adccd53129590c5a7d59e0438
This commit is contained in:
1 parent
4391a5f4b0
commit
140509f7d4
6 files changed
+59
-14
No files matched your search
@@ -38,6 +38,7 @@ from .utils import (
|
||||
normalize_pointer_spacing,
|
||||
parse_qualified_path,
|
||||
resolve_linked_text_name,
|
||||
split_specialization,
|
||||
)
|
||||
from .utils.argument_parsing import _find_matching_angle, _split_arguments
|
||||
|
||||
@@ -124,8 +125,7 @@ def _fix_inherited_constructor_name(
|
||||
return
|
||||
|
||||
class_unqualified_name = parse_qualified_path(compound_name)[-1]
|
||||
# Strip template args for comparison
|
||||
class_base_name = class_unqualified_name.split("<")[0]
|
||||
class_base_name, _ = split_specialization(class_unqualified_name)
|
||||
|
||||
if func_member.name != class_base_name:
|
||||
func_member.name = class_unqualified_name
|
||||
|
||||
@@ -10,7 +10,7 @@ from typing import Generic
|
||||
from natsort import natsorted
|
||||
|
||||
from ..member import FriendMember, Member, TypedefMember
|
||||
from ..utils import parse_qualified_path, qualify_template_args_only
|
||||
from ..utils import parse_qualified_path, qualify_type_str
|
||||
from .base_scope_kind import _natsort_key, ScopeKindT
|
||||
from .enum_scope_kind import EnumScopeKind
|
||||
from .namespace_scope_kind import NamespaceScopeKind
|
||||
@@ -35,10 +35,14 @@ class Scope(Generic[ScopeKindT]):
|
||||
current_scope = self
|
||||
while current_scope is not None:
|
||||
if current_scope.name is not None:
|
||||
# Qualify template arguments in the scope name if it has any
|
||||
name = current_scope.name
|
||||
if "<" in name and current_scope.parent_scope is not None:
|
||||
name = qualify_template_args_only(name, current_scope.parent_scope)
|
||||
if (
|
||||
isinstance(current_scope.kind, StructLikeScopeKind)
|
||||
and current_scope.kind.specialization_args is not None
|
||||
):
|
||||
name = (
|
||||
f"{name}<{', '.join(current_scope.kind.specialization_args)}>"
|
||||
)
|
||||
path.append(name)
|
||||
current_scope = current_scope.parent_scope
|
||||
path.reverse()
|
||||
@@ -174,6 +178,18 @@ class Scope(Generic[ScopeKindT]):
|
||||
"""
|
||||
Close the scope by setting the kind of all temporary scopes.
|
||||
"""
|
||||
# Qualify specialization args early so that members and inner scopes
|
||||
# see the fully-qualified name when they call get_qualified_name().
|
||||
if (
|
||||
isinstance(self.kind, StructLikeScopeKind)
|
||||
and self.kind.specialization_args is not None
|
||||
and self.parent_scope is not None
|
||||
):
|
||||
self.kind.specialization_args = [
|
||||
qualify_type_str(arg, self.parent_scope)
|
||||
for arg in self.kind.specialization_args
|
||||
]
|
||||
|
||||
for typedef in self._private_typedefs.values():
|
||||
typedef.close(self)
|
||||
|
||||
|
||||
@@ -25,11 +25,14 @@ class StructLikeScopeKind(ScopeKind, Extendable):
|
||||
STRUCT = "struct"
|
||||
UNION = "union"
|
||||
|
||||
def __init__(self, type: Type) -> None:
|
||||
def __init__(
|
||||
self, type: Type, specialization_args: list[str] | None = None
|
||||
) -> None:
|
||||
ScopeKind.__init__(self, type.value)
|
||||
Extendable.__init__(self)
|
||||
|
||||
self.template_list: TemplateList | None = None
|
||||
self.specialization_args = specialization_args
|
||||
|
||||
def add_template(self, template: Template | [Template]) -> None:
|
||||
if template and self.template_list is None:
|
||||
|
||||
@@ -15,7 +15,7 @@ from .scope import (
|
||||
StructLikeScopeKind,
|
||||
TemporaryScopeKind,
|
||||
)
|
||||
from .utils import parse_qualified_path
|
||||
from .utils import parse_qualified_path, split_specialization
|
||||
|
||||
|
||||
class Snapshot:
|
||||
@@ -46,19 +46,26 @@ class Snapshot:
|
||||
scope_name = path[-1]
|
||||
current_scope = self.ensure_scope(scope_path)
|
||||
|
||||
if scope_name in current_scope.inner_scopes:
|
||||
scope = current_scope.inner_scopes[scope_name]
|
||||
base_name, specialization_args = split_specialization(scope_name)
|
||||
|
||||
# Use the full name (including specialization) as the dict key so that
|
||||
# base templates and their specializations are distinct entries.
|
||||
scope_key = scope_name
|
||||
|
||||
if scope_key in current_scope.inner_scopes:
|
||||
scope = current_scope.inner_scopes[scope_key]
|
||||
if scope.kind.name == "temporary":
|
||||
scope.kind = StructLikeScopeKind(type)
|
||||
scope.kind = StructLikeScopeKind(type, specialization_args)
|
||||
scope.name = base_name
|
||||
else:
|
||||
raise RuntimeError(
|
||||
f"Identifier {scope_name} already exists in scope {current_scope.name}"
|
||||
f"Identifier {scope_key} already exists in scope {current_scope.name}"
|
||||
)
|
||||
return scope
|
||||
else:
|
||||
new_scope = Scope(StructLikeScopeKind(type), scope_name)
|
||||
new_scope = Scope(StructLikeScopeKind(type, specialization_args), base_name)
|
||||
new_scope.parent_scope = current_scope
|
||||
current_scope.inner_scopes[scope_name] = new_scope
|
||||
current_scope.inner_scopes[scope_key] = new_scope
|
||||
return new_scope
|
||||
|
||||
def create_or_get_namespace(self, qualified_name: str) -> Scope[NamespaceScopeKind]:
|
||||
|
||||
@@ -12,6 +12,7 @@ from .argument_parsing import (
|
||||
parse_arg_string,
|
||||
parse_function_pointer_argstring,
|
||||
parse_type_with_argstrings,
|
||||
split_specialization,
|
||||
)
|
||||
from .qualified_path import parse_qualified_path
|
||||
from .text_resolution import (
|
||||
@@ -37,6 +38,7 @@ __all__ = [
|
||||
"FunctionModifiers",
|
||||
"InitializerType",
|
||||
"normalize_angle_brackets",
|
||||
"normalize_pointer_spacing",
|
||||
"parse_arg_string",
|
||||
"parse_function_pointer_argstring",
|
||||
"parse_qualified_path",
|
||||
@@ -46,4 +48,5 @@ __all__ = [
|
||||
"qualify_template_args_only",
|
||||
"qualify_type_str",
|
||||
"resolve_linked_text_name",
|
||||
"split_specialization",
|
||||
]
|
||||
@@ -166,6 +166,22 @@ def _split_arguments(args_str: str) -> list[str]:
|
||||
return [arg for arg in result if arg]
|
||||
|
||||
|
||||
def split_specialization(name: str) -> tuple[str, list[str] | None]:
|
||||
"""Split a potentially specialized name into base name and specialization args.
|
||||
|
||||
Examples:
|
||||
"Foo" -> ("Foo", None)
|
||||
"Foo<int>" -> ("Foo", ["int"])
|
||||
"Foo<int, float>" -> ("Foo", ["int", "float"])
|
||||
"Foo<Bar<int>>" -> ("Foo", ["Bar<int>"])
|
||||
"""
|
||||
angle_start = name.find("<")
|
||||
if angle_start == -1 or not name.endswith(">"):
|
||||
return (name, None)
|
||||
args = _split_arguments(name[angle_start + 1 : -1])
|
||||
return (name[:angle_start], args if args else None)
|
||||
|
||||
|
||||
def _prefix_is_all_qualifiers(prefix: str) -> bool:
|
||||
"""Check if all tokens in the prefix are type qualifiers/specifiers.
|
||||
|
||||
|
||||
Reference in new issue
Block a user