mirror of
https://github.com/facebook/react-native.git
synced 2026-11-18 20:19:07 +00:00
Handle type aliases in C++ API snapshot (#55694)
Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/55694 Changelog: [Internal] Adds support for handling type aliases in the c++ API snapshot Reviewed By: cipolleschi Differential Revision: D94074032 fbshipit-source-id: 8af8bc88e884f4b1b7f7394d1316776cc9d50d6b
This commit is contained in:
1 parent
efc15289be
commit
2246457065
18 files changed
+235
-1
No files matched your search
@@ -10,7 +10,7 @@ from pprint import pprint
|
||||
|
||||
from doxmlparser import compound, index
|
||||
|
||||
from .member import EnumMember, FunctionMember, VariableMember
|
||||
from .member import EnumMember, FunctionMember, TypedefMember, VariableMember
|
||||
from .scope import StructLikeScopeKind
|
||||
from .snapshot import Snapshot
|
||||
|
||||
@@ -166,6 +166,29 @@ def get_function_member(
|
||||
return function
|
||||
|
||||
|
||||
def get_typedef_member(
|
||||
typedef_def: compound.memberdefType, visibility: str
|
||||
) -> TypedefMember:
|
||||
typedef_name = typedef_def.get_name()
|
||||
typedef_type = resolve_ref_text_name(typedef_def.get_type())
|
||||
typedef_argstring = typedef_def.get_argsstring()
|
||||
typedef_definition = typedef_def.definition
|
||||
|
||||
typedef_keyword = "using"
|
||||
if typedef_definition.startswith("typedef"):
|
||||
typedef_keyword = "typedef"
|
||||
|
||||
typedef = TypedefMember(
|
||||
typedef_name,
|
||||
typedef_type,
|
||||
typedef_argstring,
|
||||
visibility,
|
||||
typedef_keyword,
|
||||
)
|
||||
|
||||
return typedef
|
||||
|
||||
|
||||
def create_enum_scope(snapshot: Snapshot, enum_def: compound.EnumdefType):
|
||||
"""
|
||||
Create an enum scope in the snapshot.
|
||||
@@ -261,6 +284,10 @@ def build_snapshot(xml_dir: str) -> Snapshot:
|
||||
for member_def in section_def.memberdef:
|
||||
if member_def.kind == "enum":
|
||||
create_enum_scope(snapshot, member_def)
|
||||
elif member_def.kind == "typedef":
|
||||
class_scope.add_member(
|
||||
get_typedef_member(member_def, visibility)
|
||||
)
|
||||
else:
|
||||
print(
|
||||
f"Unknown section member kind: {member_def.kind} in {compound_object.location.file}"
|
||||
@@ -302,6 +329,11 @@ def build_snapshot(xml_dir: str) -> Snapshot:
|
||||
namespace_scope.add_member(
|
||||
get_function_member(function_def, "public")
|
||||
)
|
||||
elif section_def.kind == "typedef":
|
||||
for typedef_def in section_def.memberdef:
|
||||
namespace_scope.add_member(
|
||||
get_typedef_member(typedef_def, "public")
|
||||
)
|
||||
elif section_def.kind == "enum":
|
||||
for enum_def in section_def.memberdef:
|
||||
create_enum_scope(snapshot, enum_def)
|
||||
|
||||
@@ -549,3 +549,49 @@ class FunctionMember(Member):
|
||||
modifiers = FunctionMember._parse_modifiers(modifiers_str)
|
||||
|
||||
return (arguments, modifiers)
|
||||
|
||||
|
||||
class TypedefMember(Member):
|
||||
def __init__(
|
||||
self, name: str, type: str, argstring: str | None, visibility: str, keyword: str
|
||||
) -> None:
|
||||
super().__init__(name, visibility)
|
||||
self.type: str = type
|
||||
self.keyword: str = keyword
|
||||
self.argstring: str | None = argstring
|
||||
|
||||
def close(self, scope: Scope):
|
||||
# TODO: handle unqualified references
|
||||
pass
|
||||
|
||||
def _is_function_pointer(self) -> bool:
|
||||
"""Check if this typedef is a function pointer type."""
|
||||
return self.argstring is not None and self.argstring.startswith(")(")
|
||||
|
||||
def to_string(
|
||||
self,
|
||||
indent: int = 0,
|
||||
qualification: str | None = None,
|
||||
hide_visibility: bool = False,
|
||||
) -> str:
|
||||
name = self._get_qualified_name(qualification)
|
||||
result = " " * indent
|
||||
|
||||
if not hide_visibility:
|
||||
result += self.visibility + " "
|
||||
|
||||
result += self.keyword
|
||||
|
||||
if self.keyword == "using":
|
||||
result += f" {name} = {self.type};"
|
||||
elif self._is_function_pointer():
|
||||
# Function pointer typedef: "typedef return_type (*name)(args);"
|
||||
# type is e.g. "void(*", argstring is ")(args...)"
|
||||
if "(*" in self.type:
|
||||
result += f" {self.type}{name}{self.argstring};"
|
||||
else:
|
||||
result += f" {self.type}(*{name}{self.argstring};"
|
||||
else:
|
||||
result += f" {self.type} {name};"
|
||||
|
||||
return result
|
||||
@@ -0,0 +1,3 @@
|
||||
class test::Clss {
|
||||
protected typedef int32_t MyInt32;
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
/*
|
||||
* 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 {
|
||||
protected:
|
||||
typedef int32_t MyInt32;
|
||||
};
|
||||
|
||||
} // namespace test
|
||||
@@ -0,0 +1,3 @@
|
||||
class test::Clss {
|
||||
public typedef int32_t MyInt32;
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
/*
|
||||
* 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:
|
||||
typedef int32_t MyInt32;
|
||||
};
|
||||
|
||||
} // namespace test
|
||||
@@ -0,0 +1,3 @@
|
||||
class test::Clss {
|
||||
public using MyInt32 = int32_t;
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
/*
|
||||
* 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:
|
||||
using MyInt32 = int32_t;
|
||||
};
|
||||
|
||||
} // namespace test
|
||||
+4
@@ -0,0 +1,4 @@
|
||||
class test::Clss {
|
||||
public typedef MyInt32 int32_t;
|
||||
public using OtherInt32 = MyInt32;
|
||||
}
|
||||
@@ -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:
|
||||
typedef MyInt32 int32_t;
|
||||
using OtherInt32 = MyInt32;
|
||||
};
|
||||
|
||||
} // namespace test
|
||||
@@ -0,0 +1 @@
|
||||
typedef int32_t test::MyInt32;
|
||||
@@ -0,0 +1,14 @@
|
||||
/*
|
||||
* 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 {
|
||||
|
||||
typedef int32_t MyInt32;
|
||||
|
||||
} // namespace test
|
||||
@@ -0,0 +1 @@
|
||||
typedef void(*test::FnPtr)(int32_t);
|
||||
@@ -0,0 +1,14 @@
|
||||
/*
|
||||
* 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 {
|
||||
|
||||
typedef void (*FnPtr)(int32_t);
|
||||
|
||||
} // namespace test
|
||||
@@ -0,0 +1 @@
|
||||
using test::MyInt32 = int32_t;
|
||||
@@ -0,0 +1,14 @@
|
||||
/*
|
||||
* 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 {
|
||||
|
||||
using MyInt32 = int32_t;
|
||||
|
||||
} // namespace test
|
||||
@@ -0,0 +1,6 @@
|
||||
union test::TypedefUnion {
|
||||
public FloatType floatValue;
|
||||
public ValueType intValue;
|
||||
public typedef float FloatType;
|
||||
public using ValueType = int32_t;
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
/*
|
||||
* 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 <cstdint>
|
||||
|
||||
namespace test {
|
||||
|
||||
union TypedefUnion {
|
||||
public:
|
||||
using ValueType = int32_t;
|
||||
typedef float FloatType;
|
||||
|
||||
ValueType intValue;
|
||||
FloatType floatValue;
|
||||
};
|
||||
|
||||
} // namespace test
|
||||
Reference in new issue
Block a user