Handle templates in the C++ API snapshot (#55693)

Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/55693

Changelog: [Internal]

Adds support for handling template params in the C++ API parser.

Reviewed By: cipolleschi

Differential Revision: D94074045

fbshipit-source-id: b758a8ceb7f95e8a1acf0983afa0c65aeec6b756
This commit is contained in:
Jakub Piasecki authored and meta-codesync[bot] committed 2026-02-25 01:12:30 -08:00
1 parent 2246457065
commit e4ded3b9c0
16 files changed
+221

No files matched your search

+38
View File
@@ -13,6 +13,7 @@ from doxmlparser import compound, index
from .member import EnumMember, FunctionMember, TypedefMember, VariableMember
from .scope import StructLikeScopeKind
from .snapshot import Snapshot
from .template import Template
def resolve_ref_text_name(type_def: compound.refTextType) -> str:
@@ -92,6 +93,38 @@ def get_base_classes(
return base_classes
def get_template_params(
compound_object: compound.CompounddefType,
) -> [Template]:
"""
Get the template parameters of a compound object.
"""
template_params = []
if compound_object.templateparamlist is not None:
for param in compound_object.templateparamlist.param:
template_value = (
resolve_ref_text_name(param.defval) if param.defval else None
)
template_name = param.defname
template_type = resolve_ref_text_name(param.get_type())
if template_name is None:
# Split type string and extract name from the end
# Handles: "typename T", "class T", "int N", etc.
parts = template_type.strip().split()
if len(parts) >= 2:
template_type = " ".join(parts[:-1])
template_name = parts[-1]
elif len(parts) == 1:
# Just a name like "T" with no type keyword
template_name = parts[0]
template_params.append(
Template(template_type, template_name, template_value)
)
return template_params
def get_variable_member(
member_def: compound.MemberdefType,
visibility: str,
@@ -163,6 +196,8 @@ def get_function_member(
is_static,
)
function.add_template(get_template_params(function_def))
return function
@@ -186,6 +221,8 @@ def get_typedef_member(
typedef_keyword,
)
typedef.add_template(get_template_params(typedef_def))
return typedef
@@ -253,6 +290,7 @@ def build_snapshot(xml_dir: str) -> Snapshot:
)
class_scope.kind.add_base(get_base_classes(compound_object))
class_scope.kind.add_template(get_template_params(compound_object))
class_scope.location = compound_object.location.file
for section_def in compound_object.sectiondef:
+19
View File
@@ -10,6 +10,8 @@ from abc import ABC, abstractmethod
from dataclasses import dataclass
from typing import TYPE_CHECKING
from .template import Template, TemplateList
if TYPE_CHECKING:
from .scope import Scope
@@ -18,6 +20,7 @@ class Member(ABC):
def __init__(self, name: str, visibility: str) -> None:
self.name: str = name
self.visibility: str = visibility
self.template_list: TemplateList | None = None
@abstractmethod
def to_string(
@@ -34,6 +37,16 @@ class Member(ABC):
def _get_qualified_name(self, qualification: str | None):
return f"{qualification}::{self.name}" if qualification else self.name
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)
class EnumMember(Member):
def __init__(self, name: str, value: str | None) -> None:
@@ -188,6 +201,9 @@ class FunctionMember(Member):
name = self._get_qualified_name(qualification)
result = ""
if self.template_list is not None:
result += " " * indent + self.template_list.to_string() + "\n"
result += " " * indent
if not hide_visibility:
@@ -577,6 +593,9 @@ class TypedefMember(Member):
name = self._get_qualified_name(qualification)
result = " " * indent
if self.keyword == "using" and self.template_list is not None:
result += self.template_list.to_string() + "\n" + " " * indent
if not hide_visibility:
result += self.visibility + " "
+14
View File
@@ -12,6 +12,7 @@ from typing import Generic, TypeVar
from natsort import natsorted
from .member import Member
from .template import Template, TemplateList
from .utils import parse_qualified_path
@@ -46,6 +47,7 @@ class StructLikeScopeKind(ScopeKind):
super().__init__(type.value)
self.base_classes: [StructLikeScopeKind.Base] = []
self.template_list: TemplateList | None = None
def add_base(
self, base: StructLikeScopeKind.Base | [StructLikeScopeKind.Base]
@@ -56,6 +58,16 @@ class StructLikeScopeKind(ScopeKind):
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 to_string(self, scope: Scope) -> str:
result = ""
@@ -69,6 +81,8 @@ class StructLikeScopeKind(ScopeKind):
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 = []
+28
View File
@@ -0,0 +1,28 @@
# 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.
class Template:
def __init__(self, type: str, name: str, value: str | None) -> None:
self.type: str = type
self.name: str = name
self.value: str | None = value
def to_string(self) -> str:
if self.value is None:
return f"{self.type} {self.name}"
else:
return f"{self.type} {self.name} = {self.value}"
class TemplateList:
def __init__(self) -> None:
self.templates: [Template] = []
def add(self, template: Template) -> None:
self.templates.append(template)
def to_string(self) -> str:
return f"template <{', '.join([template.to_string() for template in self.templates])}>"
@@ -0,0 +1,4 @@
class test::Base {
template <typename T>
public T hello();
}
@@ -0,0 +1,18 @@
/*
* 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.
*/
#pragma once
namespace test {
class Base {
public:
template <typename T>
T hello();
};
} // namespace test
@@ -0,0 +1,4 @@
class test::Clss {
template <typedef T>
public using MyT = T;
}
@@ -0,0 +1,18 @@
/*
* 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.
*/
#pragma once
namespace test {
class Clss {
public:
template <typedef T>
using MyT = T;
};
} // namespace test
@@ -0,0 +1,2 @@
template <typename T>
T test::helloWorld();
@@ -0,0 +1,15 @@
/*
* 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.
*/
#pragma once
namespace test {
template <typename T>
T helloWorld();
} // namespace test
@@ -0,0 +1,2 @@
template <typename T>
using test::MyT = T;
@@ -0,0 +1,15 @@
/*
* 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.
*/
#pragma once
namespace test {
template <typename T>
using MyT = T;
} // namespace test
@@ -0,0 +1,4 @@
template <typename T>
class test::Tmp {
public T field;
}
@@ -0,0 +1,18 @@
/*
* 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.
*/
#pragma once
namespace test {
template <typename T>
class Tmp {
public:
T field;
};
} // namespace test
@@ -0,0 +1,4 @@
template <typename T = int>
class test::Tmp {
public T field;
}
@@ -0,0 +1,18 @@
/*
* 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.
*/
#pragma once
namespace test {
template <typename T = int>
class Tmp {
public:
T field;
};
} // namespace test