Generate C++ wrappers for the global functions too

This ensures that global functions can also use C++ classes, enums etc
for their parameters and return types.

C++ wrappers for a couple of tests had to be disabled, but this is not
really a regression as wrapping global functions just made apparent
problems that were not visible before because the corresponding wrappers
were not created at all.
This commit is contained in:
Vadim Zeitlin 2021-12-07 20:49:15 +01:00
commit 86dbf6bcb5
2 changed files with 27 additions and 0 deletions

View file

@ -920,6 +920,8 @@ public:
bool can_wrap() const { return func_node != NULL; }
// Emit just the function body, including the braces around it.
//
// This helper is used both by our emit() and emit_member_function().
void emit_body(String* wparms) {
String* const wname = Getattr(func_node, "wrap:name");
@ -947,6 +949,23 @@ public:
Append(cxx_wrappers_.sect_impls, "}\n");
}
// Do emit the function wrapper.
void emit() {
// The wrapper function name should be sym:name, but we change it to include the namespace prefix in our own globalvariableHandler(), so now we have to undo
// this by using the value saved there, if available. This is definitely clumsy and it would be better to avoid it, but this would probably need to be done
// by separating C and C++ wrapper generation in two different passes and so would require significantly more changes than this hack.
String* name = Getattr(func_node, "c:globalvariableHandler:sym:name");
if (!name)
name = Getattr(func_node, "sym:name");
Printv(cxx_wrappers_.sect_impls,
"inline ", rtype_desc.type(), " ", name, "(", parms_cxx.get(), ") ",
NIL
);
emit_body(parms_call);
}
cxx_wrappers& cxx_wrappers_;
Node* func_node;
@ -2496,6 +2515,10 @@ public:
if (cxx_class_wrapper_) {
cxx_class_wrapper_->emit_member_function(n);
} else {
cxx_function_wrapper w(cxx_wrappers_, n, Getattr(n, "parms"));
if (w.can_wrap())
w.emit();
}
}