mirror of
https://github.com/facebook/react-native.git
synced 2026-11-18 20:19:07 +00:00
Handle concepts in the C++ API snapshot (#55695)
Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/55695 Changelog: [Internal] Adds support for handling concepts in the C++ API parser. Reviewed By: NickGerleman Differential Revision: D94074054 fbshipit-source-id: 5e8ef0b0d4d1eeb93554558bb7f80cd67d622a72
This commit is contained in:
1 parent
e4ded3b9c0
commit
f9db851a8d
6 files changed
+226
-1
No files matched your search
@@ -10,10 +10,17 @@ from pprint import pprint
|
||||
|
||||
from doxmlparser import compound, index
|
||||
|
||||
from .member import EnumMember, FunctionMember, TypedefMember, VariableMember
|
||||
from .member import (
|
||||
ConceptMember,
|
||||
EnumMember,
|
||||
FunctionMember,
|
||||
TypedefMember,
|
||||
VariableMember,
|
||||
)
|
||||
from .scope import StructLikeScopeKind
|
||||
from .snapshot import Snapshot
|
||||
from .template import Template
|
||||
from .utils import parse_qualified_path
|
||||
|
||||
|
||||
def resolve_ref_text_name(type_def: compound.refTextType) -> str:
|
||||
@@ -59,6 +66,34 @@ def resolve_linked_text_name(type_def: compound.linkedTextType) -> str:
|
||||
return name.strip()
|
||||
|
||||
|
||||
def resolve_linked_text_full(type_def: compound.linkedTextType) -> str:
|
||||
"""
|
||||
Resolve the full text content of a linkedTextType, including all text
|
||||
fragments and ref elements.
|
||||
|
||||
Unlike resolve_linked_text_name which only gets the first ref or value,
|
||||
this function concatenates all content_ items to reconstruct the full text.
|
||||
"""
|
||||
if not type_def.content_:
|
||||
# Fall back to valueOf_ if no content_ list
|
||||
return type_def.get_valueOf_() or ""
|
||||
|
||||
result = []
|
||||
for item in type_def.content_:
|
||||
if item.category == 1: # MixedContainer.CategoryText
|
||||
result.append(item.value)
|
||||
elif item.category == 3: # MixedContainer.CategoryComplex (ref element)
|
||||
# For ref elements, get the text content
|
||||
if hasattr(item.value, "get_valueOf_"):
|
||||
result.append(item.value.get_valueOf_())
|
||||
elif hasattr(item.value, "valueOf_"):
|
||||
result.append(item.value.valueOf_)
|
||||
else:
|
||||
result.append(str(item.value))
|
||||
|
||||
return "".join(result)
|
||||
|
||||
|
||||
def get_base_classes(
|
||||
compound_object: compound.CompounddefType,
|
||||
) -> [StructLikeScopeKind.Base]:
|
||||
@@ -226,6 +261,33 @@ def get_typedef_member(
|
||||
return typedef
|
||||
|
||||
|
||||
def get_concept_member(
|
||||
concept_def: compound.CompounddefType,
|
||||
) -> ConceptMember:
|
||||
"""
|
||||
Get the concept member from a compound definition.
|
||||
"""
|
||||
concept_name = concept_def.compoundname
|
||||
concept_path = parse_qualified_path(concept_name)
|
||||
unqualified_name = concept_path[-1]
|
||||
|
||||
initializer = concept_def.initializer
|
||||
constraint = ""
|
||||
|
||||
if initializer:
|
||||
# The initializer contains the entire constraind definition.
|
||||
# We want to extract the constraint part after "="
|
||||
initializer_text = resolve_linked_text_full(initializer)
|
||||
eq_pos = initializer_text.find("=")
|
||||
if eq_pos != -1:
|
||||
constraint = initializer_text[eq_pos + 1 :].strip()
|
||||
|
||||
concept = ConceptMember(unqualified_name, constraint)
|
||||
concept.add_template(get_template_params(concept_def))
|
||||
|
||||
return concept
|
||||
|
||||
|
||||
def create_enum_scope(snapshot: Snapshot, enum_def: compound.EnumdefType):
|
||||
"""
|
||||
Create an enum scope in the snapshot.
|
||||
@@ -379,6 +441,18 @@ def build_snapshot(xml_dir: str) -> Snapshot:
|
||||
print(
|
||||
f"Unknown section kind: {section_def.kind} in {compound_object.location.file}"
|
||||
)
|
||||
elif compound_object.kind == "concept":
|
||||
# Concepts belong to a namespace, so we need to find or create the parent namespace
|
||||
concept_name = compound_object.compoundname
|
||||
concept_path = parse_qualified_path(concept_name)
|
||||
namespace_path = "::".join(concept_path[:-1]) if concept_path else ""
|
||||
|
||||
if namespace_path:
|
||||
namespace_scope = snapshot.create_or_get_namespace(namespace_path)
|
||||
else:
|
||||
namespace_scope = snapshot.root_scope
|
||||
|
||||
namespace_scope.add_member(get_concept_member(compound_object))
|
||||
elif compound_object.kind == "file":
|
||||
pass
|
||||
elif compound_object.kind == "dir":
|
||||
|
||||
@@ -614,3 +614,88 @@ class TypedefMember(Member):
|
||||
result += f" {self.type} {name};"
|
||||
|
||||
return result
|
||||
|
||||
|
||||
class ConceptMember(Member):
|
||||
def __init__(
|
||||
self,
|
||||
name: str,
|
||||
constraint: str,
|
||||
) -> None:
|
||||
super().__init__(name, "public")
|
||||
self.constraint: str = self._normalize_constraint(constraint)
|
||||
|
||||
@staticmethod
|
||||
def _normalize_constraint(constraint: str) -> str:
|
||||
"""
|
||||
Normalize the whitespace in a concept constraint expression.
|
||||
|
||||
Doxygen preserves original source indentation, which becomes
|
||||
inconsistent when we flatten namespaces and use qualified names.
|
||||
This method normalizes the indentation by dedenting all lines
|
||||
to the minimum non-empty indentation level.
|
||||
"""
|
||||
if not constraint:
|
||||
return constraint
|
||||
|
||||
lines = constraint.split("\n")
|
||||
if len(lines) <= 1:
|
||||
return constraint.strip()
|
||||
|
||||
# Find minimum indentation (excluding the first line and empty lines)
|
||||
min_indent = float("inf")
|
||||
for line in lines[1:]:
|
||||
stripped = line.lstrip()
|
||||
if stripped: # Skip empty lines
|
||||
indent = len(line) - len(stripped)
|
||||
min_indent = min(min_indent, indent)
|
||||
|
||||
if min_indent == float("inf"):
|
||||
min_indent = 0
|
||||
|
||||
# Dedent all lines by the minimum indentation
|
||||
result_lines = [lines[0].strip()]
|
||||
for line in lines[1:]:
|
||||
if line.strip(): # Non-empty line
|
||||
# Remove the minimum indentation to normalize
|
||||
dedented = (
|
||||
line[int(min_indent) :]
|
||||
if len(line) >= min_indent
|
||||
else line.lstrip()
|
||||
)
|
||||
result_lines.append(dedented.rstrip())
|
||||
else:
|
||||
result_lines.append("")
|
||||
|
||||
# Check if no line is indented
|
||||
if all(not line.startswith(" ") for line in result_lines):
|
||||
# Re-indent all lines but the first by 2 spaces
|
||||
not_indented = result_lines
|
||||
result_lines = [not_indented[0]]
|
||||
for line in not_indented[1:]:
|
||||
if line.strip(): # Non-empty line
|
||||
result_lines.append(" " + line)
|
||||
else:
|
||||
result_lines.append("")
|
||||
|
||||
return "\n".join(result_lines)
|
||||
|
||||
def close(self, scope: Scope):
|
||||
# TODO: handle unqualified references
|
||||
pass
|
||||
|
||||
def to_string(
|
||||
self,
|
||||
indent: int = 0,
|
||||
qualification: str | None = None,
|
||||
hide_visibility: bool = False,
|
||||
) -> str:
|
||||
name = self._get_qualified_name(qualification)
|
||||
result = ""
|
||||
|
||||
if self.template_list is not None:
|
||||
result += " " * indent + self.template_list.to_string() + "\n"
|
||||
|
||||
result += " " * indent + f"concept {name} = {self.constraint};"
|
||||
|
||||
return result
|
||||
@@ -0,0 +1,4 @@
|
||||
template <typename T, typename ReturnT = std::any>
|
||||
concept test::CSSParserSink = requires(CSSSyntaxParser &parser) {
|
||||
{ T::consume(parser) } -> std::convertible_to<ReturnT>;
|
||||
};
|
||||
@@ -0,0 +1,22 @@
|
||||
/*
|
||||
* 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
|
||||
|
||||
#include <any>
|
||||
#include <concepts>
|
||||
|
||||
namespace test {
|
||||
|
||||
class CSSSyntaxParser;
|
||||
|
||||
template <typename T, typename ReturnT = std::any>
|
||||
concept CSSParserSink = requires(CSSSyntaxParser &parser) {
|
||||
{ T::consume(parser) } -> std::convertible_to<ReturnT>;
|
||||
};
|
||||
|
||||
} // namespace test
|
||||
@@ -0,0 +1,9 @@
|
||||
template <typename ReturnT, typename... VisitorsT>
|
||||
concept test::CSSUniqueComponentValueVisitors = CSSSyntaxVisitorReturn<ReturnT> && (CSSComponentValueVisitor<VisitorsT, ReturnT> && ...) &&
|
||||
((CSSFunctionVisitor<VisitorsT, ReturnT> ? 1 : 0) + ... + 0) <= 1 &&
|
||||
((CSSPreservedTokenVisitor<VisitorsT, ReturnT> ? 1 : 0) + ... + 0) <= 1 &&
|
||||
((CSSSimpleBlockVisitor<VisitorsT, ReturnT> ? 1 : 0) + ... + 0) <= 1;
|
||||
template <typename T, typename ReturnT = std::any>
|
||||
concept test::CSSParserSink = requires(CSSSyntaxParser &parser) {
|
||||
{ T::consume(parser) } -> std::convertible_to<ReturnT>;
|
||||
};
|
||||
@@ -0,0 +1,31 @@
|
||||
/*
|
||||
* 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
|
||||
|
||||
#include <any>
|
||||
#include <concepts>
|
||||
|
||||
namespace test {
|
||||
|
||||
class CSSSyntaxParser;
|
||||
|
||||
template <typename T, typename ReturnT = std::any>
|
||||
// clang-format off
|
||||
concept CSSParserSink = requires(CSSSyntaxParser &parser) {
|
||||
{ T::consume(parser) } -> std::convertible_to<ReturnT>;
|
||||
};
|
||||
// clang-format on
|
||||
|
||||
template <typename ReturnT, typename... VisitorsT>
|
||||
concept CSSUniqueComponentValueVisitors =
|
||||
CSSSyntaxVisitorReturn<ReturnT> && (CSSComponentValueVisitor<VisitorsT, ReturnT> && ...) &&
|
||||
((CSSFunctionVisitor<VisitorsT, ReturnT> ? 1 : 0) + ... + 0) <= 1 &&
|
||||
((CSSPreservedTokenVisitor<VisitorsT, ReturnT> ? 1 : 0) + ... + 0) <= 1 &&
|
||||
((CSSSimpleBlockVisitor<VisitorsT, ReturnT> ? 1 : 0) + ... + 0) <= 1;
|
||||
|
||||
} // namespace test
|
||||
Reference in new issue
Block a user