Internal constructor name no longer contains template parameters

This is for consistency with other members.
Fixes csymbol table name for the constructor.

Notable side effects:

- When $name is replaced in a constructor it no longer includes any
  template parameters, so similar to member functions which also
  don't include template parameters (li_boost_shared_ptr testcase)
- Fixes mangled C constructor name when using nested templates
  (nested_in_template, template_nested testcases)
- Fixes some typedef look up when generating templates that have
  default template parameters, to improve generated code so that
  the default parameters are no longer explicitly generated when
  the template type is used (template_default_class_parms_typedef
  testcase)
- For Ruby, better error messages when calling constructors,
  old:
    runme.rb:5:in `initialize': Expected argument 0 of type int, but got String "hi" (TypeError)
	in SWIG method 'Temply<(int)>'
  new:
    runme.rb:5:in `initialize': Expected argument 0 of type int, but got String "hi" (TypeError)
	in SWIG method 'Temply'
- Feature matching of parameters that are template types is now
  consistent for parameters in constructors and methods
  (features testcase)
  Potential incompatibility though:
  old:
    %exception Template<int>::Template(const Template&) "..."
  new:
    %exception Template<int>::Template(const Template<int>&) "..."
This commit is contained in:
William S Fulton 2023-07-07 23:42:21 +01:00
parent a037e2f2e2
commit 9cf049186b
3 changed files with 17 additions and 9 deletions

View File

@ -68,7 +68,7 @@ template<class T> class SimpleTemplate {
// Test 4: Test templates with user supplied constructors and destructor
%exception Template<int>::Template() "$action /*Template<int>::Template<int>*/";
%exception Template<int>::Template(const Template&) "$action /*Template<int>::Template<int>(const Template&)*/";
%exception Template<int>::Template(const Template<int>&) "$action /*Template<int>::Template(const Template&)*/";
%exception Template<int>::~Template() "$action /*Template<int>::~Template*/";
// method tests
%exception Template<int>::foo "$action /*Template<int>::foo*/";
@ -76,6 +76,7 @@ template<class T> class SimpleTemplate {
%exception Template<int>::set(const int &t) "$action /*Template<int>::set(const int &t)*/";
%exception Template<int>::bar(const int &t) "_failed_ /*Template<int>::bar(const int &t) const*/";
%exception Template<int>::bar(const int &t) const "$action /*Template<int>::bar(const int &t) const*/";
%exception Template<int>::spam(const Template<int> &) "$action /*Template<int>::spam(const Template&)*/";
%inline %{
template<class T> class Template {
@ -86,6 +87,7 @@ public:
~Template(){}
void foo(){}
void bar(const int &t) const {}
void spam(const Template &){}
#ifdef SWIG
%extend {
T& get(int i) const {

View File

@ -242,8 +242,6 @@ static void cparse_template_expand(Node *templnode, Node *n, String *tname, Stri
}
if (strchr(Char(name), '<')) {
Append(patchlist, Getattr(n, "name"));
} else {
Append(name, templateargs);
}
name = Getattr(n, "sym:name");
if (name) {

View File

@ -2303,7 +2303,8 @@ static void addCopyConstructor(Node *n) {
String *cname = Getattr(n, "name");
SwigType *type = Copy(cname);
String *name = Swig_scopename_last(cname);
String *lastname = Swig_scopename_last(cname);
String *name = SwigType_templateprefix(lastname);
String *cc = NewStringf("r.q(const).%s", type);
String *decl = NewStringf("f(%s).", cc);
String *oldname = Getattr(n, "sym:name");
@ -2314,15 +2315,15 @@ static void addCopyConstructor(Node *n) {
// renamed, and use its name as oldname.
Node *c;
for (c = firstChild(n); c; c = nextSibling(c)) {
const char *tag = Char(nodeType(c));
if (strcmp(tag, "constructor") == 0) {
String *cname = Getattr(c, "name");
if (Equal(nodeType(c), "constructor")) {
String *csname = Getattr(c, "sym:name");
String *clast = Swig_scopename_last(cname);
String *clast = Swig_scopename_last(Getattr(c, "name"));
if (Equal(csname, clast)) {
oldname = csname;
Delete(clast);
break;
}
Delete(clast);
}
}
}
@ -2335,6 +2336,7 @@ static void addCopyConstructor(Node *n) {
Setattr(cn, "sym:name", symname);
SetFlag(cn, "feature:new");
Setattr(cn, "decl", decl);
Setattr(cn, "ismember", "1");
Setattr(cn, "parentNode", n);
Setattr(cn, "parms", p);
Setattr(cn, "copy_constructor", "1");
@ -2357,6 +2359,7 @@ static void addCopyConstructor(Node *n) {
}
}
Delete(cn);
Delete(lastname);
Delete(name);
Delete(decl);
Delete(symname);
@ -2370,17 +2373,21 @@ static void addDefaultConstructor(Node *n) {
Setline(cn, Getline(n));
String *cname = Getattr(n, "name");
String *name = Swig_scopename_last(cname);
String *lastname = Swig_scopename_last(cname);
String *name = SwigType_templateprefix(lastname);
String *decl = NewString("f().");
String *oldname = Getattr(n, "sym:name");
String *symname = Swig_name_make(cn, cname, name, decl, oldname);
if (Strcmp(symname, "$ignore") != 0) {
Setattr(cn, "name", name);
Setattr(cn, "sym:name", symname);
SetFlag(cn, "feature:new");
Setattr(cn, "decl", decl);
Setattr(cn, "ismember", "1");
Setattr(cn, "parentNode", n);
Setattr(cn, "default_constructor", "1");
Symtab *oldscope = Swig_symbol_setscope(Getattr(n, "symtab"));
Node *on = Swig_symbol_add(symname, cn);
Swig_features_get(Swig_cparse_features(), Swig_symbol_qualifiedscopename(0), name, decl, cn);
@ -2398,6 +2405,7 @@ static void addDefaultConstructor(Node *n) {
}
}
Delete(cn);
Delete(lastname);
Delete(name);
Delete(decl);
Delete(symname);