Fix instantiation of variadic class templates

containing parameter pack arguments that are function pointers.

  template <typename... V> struct VariadicParms {
    void ParmsFuncPtrVal(int (*)(V...)) {}
  };

  %template(VariadicParms0) VariadicParms<>;
  %template(VariadicParms1) VariadicParms<A>;
This commit is contained in:
William S Fulton 2022-12-27 19:36:40 +00:00
commit 674abaddbf
6 changed files with 162 additions and 30 deletions

View file

@ -1392,6 +1392,72 @@ void SwigType_typename_replace(SwigType *t, String *pat, String *rep) {
Delete(elem);
}
/* -----------------------------------------------------------------------------
* SwigType_variadic_replace()
*
* Replaces variadic parameter with a list of (zero or more) parameters.
* Needed for variadic templates.
* ----------------------------------------------------------------------------- */
void SwigType_variadic_replace(SwigType *t, Parm *unexpanded_variadic_parm, ParmList *expanded_variadic_parms) {
String *nt;
int i, ilen;
List *elem;
if (!unexpanded_variadic_parm)
return;
if (SwigType_isvariadic(t)) {
/* Based on expand_variadic_parms() but input is single SwigType (t) instead of ParmList */
String *unexpanded_name = Getattr(unexpanded_variadic_parm, "name");
ParmList *expanded = CopyParmList(expanded_variadic_parms);
Parm *ep = expanded;
while (ep) {
SwigType *newtype = Copy(t);
SwigType_del_variadic(newtype);
Replaceid(newtype, unexpanded_name, Getattr(ep, "type"));
Setattr(ep, "type", newtype);
ep = nextSibling(ep);
}
Clear(t);
SwigType *fparms = SwigType_function_parms_only(expanded);
Append(t, fparms);
Delete(expanded);
return;
}
nt = NewStringEmpty();
elem = SwigType_split(t);
ilen = Len(elem);
for (i = 0; i < ilen; i++) {
String *e = Getitem(elem, i);
if (SwigType_isfunction(e)) {
int j, jlen;
List *fparms = SwigType_parmlist(e);
Clear(e);
Append(e, "f(");
jlen = Len(fparms);
for (j = 0; j < jlen; j++) {
SwigType *type = Getitem(fparms, j);
SwigType_variadic_replace(type, unexpanded_variadic_parm, expanded_variadic_parms);
if (Len(type) > 0) {
if (j != 0)
Putc(',', e);
Append(e, type);
} else {
assert(j == jlen - 1); /* A variadic parm was replaced with zero parms, variadic parms are only changed at the end of the list */
}
}
Append(e, ").");
Delete(fparms);
}
Append(nt, e);
}
Clear(t);
Append(t, nt);
Delete(nt);
Delete(elem);
}
/* -----------------------------------------------------------------------------
* SwigType_remove_global_scope_prefix()
*

View file

@ -137,6 +137,7 @@ extern "C" {
extern SwigType *SwigType_add_template(SwigType *t, ParmList *parms);
extern SwigType *SwigType_pop_function(SwigType *t);
extern SwigType *SwigType_pop_function_qualifiers(SwigType *t);
extern SwigType *SwigType_function_parms_only(ParmList *parms);
extern ParmList *SwigType_function_parms(const SwigType *t, Node *file_line_node);
extern List *SwigType_split(const SwigType *t);
extern String *SwigType_pop(SwigType *t);
@ -187,6 +188,7 @@ extern "C" {
extern SwigType *SwigType_default_create(const SwigType *ty);
extern SwigType *SwigType_default_deduce(const SwigType *t);
extern void SwigType_typename_replace(SwigType *t, String *pat, String *rep);
extern void SwigType_variadic_replace(SwigType *t, Parm *unexpanded_variadic_parm, ParmList *expanded_variadic_parms);
extern SwigType *SwigType_remove_global_scope_prefix(const SwigType *t);
extern SwigType *SwigType_alttype(const SwigType *t, int ltmap);

View file

@ -287,6 +287,7 @@ String *SwigType_parm(const SwigType *t) {
* SwigType_split()
*
* Splits a type into its component parts and returns a list of string.
* The component parts of SwigType are split by '.'.
* ----------------------------------------------------------------------------- */
List *SwigType_split(const SwigType *t) {
@ -312,7 +313,7 @@ List *SwigType_split(const SwigType *t) {
/* -----------------------------------------------------------------------------
* SwigType_parmlist()
*
* Splits a comma separated list of parameters into its component parts
* Splits a comma separated list of SwigType * parameters into its component parts
* The input is expected to contain the parameter list within () brackets
* Returns 0 if no argument list in the input, ie there are no round brackets ()
* Returns an empty List if there are no parameters in the () brackets
@ -320,12 +321,12 @@ List *SwigType_split(const SwigType *t) {
*
* Foo(std::string,p.f().Bar<(int,double)>)
*
* returns 2 elements in the list:
* returns 2 SwigType * elements in the list:
* std::string
* p.f().Bar<(int,double)>
* ----------------------------------------------------------------------------- */
List *SwigType_parmlist(const String *p) {
List *SwigType_parmlist(const SwigType *p) {
String *item = 0;
List *list;
char *c;
@ -864,26 +865,42 @@ SwigType *SwigType_array_type(const SwigType *ty) {
* Functions
*
* SwigType_add_function()
* SwigType_function_parms_only()
* SwigType_isfunction()
* SwigType_pop_function()
*
* Add, remove, and test for function types.
* ----------------------------------------------------------------------------- */
/* Returns the function type, t, constructed from the parameters, parms */
SwigType *SwigType_add_function(SwigType *t, ParmList *parms) {
String *pstr;
Parm *p;
/* -----------------------------------------------------------------------------
* SwigType_function_parms_only()
*
* Creates a comma separated list of SwigType strings from parms
* ----------------------------------------------------------------------------- */
Insert(t, 0, ").");
pstr = NewString("f(");
SwigType *SwigType_function_parms_only(ParmList *parms) {
Parm *p;
SwigType *t = NewString("");
for (p = parms; p; p = nextSibling(p)) {
if (p != parms)
Putc(',', pstr);
Append(pstr, Getattr(p, "type"));
Putc(',', t);
Append(t, Getattr(p, "type"));
}
Insert(t, 0, pstr);
Delete(pstr);
return t;
}
/* -----------------------------------------------------------------------------
* SwigType_add_function()
*
* Returns the function type, t, constructed from the parameters, parms
* ----------------------------------------------------------------------------- */
SwigType *SwigType_add_function(SwigType *t, ParmList *parms) {
SwigType *fparms = SwigType_function_parms_only(parms);
Insert(fparms, 0, "f(");
Append(fparms, ").");
Insert(t, 0, fparms);
Delete(fparms);
return t;
}