Skip to content

Commit 41f601e

Browse files
committed
Fix detection of synthetized constructor
- Modify the synthetize_init_from_pydict() method - Now struct with simply some int, e.g., which ARE trivially default constructible, have a generated constructor, except if they are empty (in which case, we use teh default constructor). - TDB: Add some more tests.
1 parent 52f7795 commit 41f601e

File tree

2 files changed

+4
-3
lines changed

2 files changed

+4
-3
lines changed

src/tools/clair-c2py/codegen/classes.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ clang::Expr const *get_field_initializer(clang::FieldDecl const *f) {
4040
void codegen_synth_constructor(std::ostream &code, cls_info_t const &cls_info) {
4141
auto const *cls = cls_info.ptr;
4242
auto cls_name = clu::get_fully_qualified_name(cls); //cls->getQualifiedNameAsString();
43-
if (cls_info.fields.empty()) return; // Nothing to construct from ?
43+
EXPECTS(!cls_info.fields.empty());
4444

4545
logs.cls_details(fmt::format("Synthesize constructor from pydict", cls_name));
4646
static long counter = 0;
@@ -243,7 +243,7 @@ void codegen_cls(std::ostream &code, str_t const &cls_py_name, cls_info_t const
243243
std::stringstream MethodDecls, MethodTable, MethodDocs;
244244

245245
// ---- constructor
246-
if (cls_info.synthetize_init_from_pydict()) {
246+
if (cls_info.synthetize_init_from_pydict() and not(cls_info.fields.empty())) {
247247
codegen_synth_constructor(MethodDecls, cls_info);
248248
} else
249249
codegen::write_dispatch_constructors(MethodDecls, cls_name, cls_info.constructors);

src/tools/clair-c2py/data.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ struct cls_info_t {
4646

4747
// Do we need to synthesize a constructor, as the class has only a {}
4848
// aggregate initialization
49-
bool synthetize_init_from_pydict() const { return (ptr->isAggregate() and not ptr->hasTrivialDefaultConstructor() and (ptr->getNumBases() == 0)); }
49+
bool synthetize_init_from_pydict() const { return (ptr->isAggregate() and (ptr->getNumBases() == 0)); }
50+
5051
bool synthetize_dict_attribute() const { return synthetize_init_from_pydict(); }
5152
};
5253

0 commit comments

Comments
 (0)