Skip to content

Commit 48a5a41

Browse files
rwgkwangxf123456
authored andcommitted
clif/pybind11/classes.py: generate implicit_upcast_bases
This change was enabled by pybind/pybind11#4612 TGP-tested via TGP-head 2023-04-06 (cl/522266833) re-run with this change: * https://fusion2.corp.google.com/presubmit/tap/522266833/OCL:522266833:BASE:522968984:1681064677276:5304c616;groups=Passing/targets * 116 Passing Example generated code: ``` UnspecDerived_class.def( "as_clif_testing_unspecified_inheritance_UnspecBase", [](::clif_testing::unspecified_inheritance::UnspecDerived* self) { return py::capsule(static_cast<void *>(self)); } ); ``` PiperOrigin-RevId: 523233130
1 parent f409194 commit 48a5a41

File tree

6 files changed

+147
-3
lines changed

6 files changed

+147
-3
lines changed

clif/pybind11/classes.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
from clif.pybind11 import function_lib
2323
from clif.pybind11 import utils
2424
from clif.pybind11 import variables
25+
from clif.python import clif_types as types as legacy_types
2526

2627
I = utils.I
2728

@@ -51,11 +52,14 @@ def generate_from(
5152
yield I + I + f'using namespace {namespace};'
5253
class_name = f'{class_decl.name.native}_class'
5354
definition = f'py::classh<{class_decl.name.cpp_name}'
55+
implicit_upcast_bases = []
5456
if not class_decl.suppress_upcasts:
5557
for base in class_decl.bases:
56-
if (base.HasField('cpp_canonical_type') and
57-
base.cpp_canonical_type in codegen_info.registered_types):
58-
definition += f', {base.cpp_canonical_type}'
58+
if base.HasField('cpp_canonical_type'):
59+
if base.cpp_canonical_type in codegen_info.registered_types:
60+
definition += f', {base.cpp_canonical_type}'
61+
else:
62+
implicit_upcast_bases.append(base)
5963
trampoline_class_name = utils.trampoline_name(class_decl)
6064
if trampoline_class_name in trampoline_class_names:
6165
definition += f', {trampoline_class_name}'
@@ -133,6 +137,16 @@ def generate_from(
133137
(not class_decl.cpp_abstract or trampoline_generated)):
134138
yield I + I + f'{class_name}.def(py::init<>());'
135139

140+
for base in implicit_upcast_bases:
141+
mangled = legacy_types.Mangle(base.cpp_canonical_type)
142+
I2 = I + I # pylint: disable=invalid-name
143+
yield I2 + f'{class_name}.def('
144+
yield I2 + I2 + f'"as_{mangled}",'
145+
yield I2 + I2 + f'[]({class_decl.name.cpp_name}* self) {{'
146+
yield I2 + I2 + I2 + 'return py::capsule(static_cast<void *>(self));'
147+
yield I2 + I2 + '}'
148+
yield I2 + ');'
149+
136150
if not reduce_or_reduce_ex_defined:
137151
yield I + I + (f'{class_name}.def("__reduce_ex__",' +
138152
' ::clif_pybind11::ReduceExImpl, py::arg("protocol")=-1);')
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Copyright 2023 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
from "clif/testing/unspecified_inheritance/unspec_base.h":
16+
namespace `clif_testing::unspecified_inheritance`:
17+
class UnspecBase:
18+
def Get(self) -> int
19+
20+
def PassUnspecBase(sb: UnspecBase) -> int
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Copyright 2023 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
from "clif/testing/unspecified_inheritance/unspec_derived.h":
16+
namespace `clif_testing::unspecified_inheritance`:
17+
class UnspecDerived:
18+
def Get(self) -> int
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Copyright 2023 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
from absl.testing import absltest
16+
17+
from clif.testing.unspecified_inheritance.python import unspec_base
18+
from clif.testing.unspecified_inheritance.python import unspec_derived
19+
20+
21+
class InheritanceCorrectnessTest(absltest.TestCase):
22+
23+
def testIt(self):
24+
sd = unspec_derived.UnspecDerived()
25+
self.assertEqual(unspec_base.PassUnspecBase(sd), 230)
26+
27+
28+
if __name__ == '__main__':
29+
absltest.main()
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Copyright 2023 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
#ifndef CLIF_TESTING_UNSPECIFIED_INHERITANCE_UNSPEC_BASE_H_
17+
#define CLIF_TESTING_UNSPECIFIED_INHERITANCE_UNSPEC_BASE_H_
18+
19+
namespace clif_testing {
20+
namespace unspecified_inheritance {
21+
22+
struct UnspecBase {
23+
virtual ~UnspecBase() = default;
24+
virtual int Get() const { return 100; }
25+
};
26+
27+
inline int PassUnspecBase(const UnspecBase& sb) { return sb.Get() + 30; }
28+
29+
} // namespace unspecified_inheritance
30+
} // namespace clif_testing
31+
32+
#endif // CLIF_TESTING_UNSPECIFIED_INHERITANCE_UNSPEC_BASE_H_
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Copyright 2023 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
#ifndef CLIF_TESTING_UNSPECIFIED_INHERITANCE_UNSPEC_DERIVED_H_
17+
#define CLIF_TESTING_UNSPECIFIED_INHERITANCE_UNSPEC_DERIVED_H_
18+
19+
#include "clif/testing/unspecified_inheritance/unspec_base.h"
20+
21+
namespace clif_testing {
22+
namespace unspecified_inheritance {
23+
24+
struct UnspecDerived : UnspecBase {
25+
int Get() const override { return 200; }
26+
};
27+
28+
} // namespace unspecified_inheritance
29+
} // namespace clif_testing
30+
31+
#endif // CLIF_TESTING_UNSPECIFIED_INHERITANCE_UNSPEC_DERIVED_H_

0 commit comments

Comments
 (0)