From 97c2eeaf90f5332a3518c239b5803e65db153a28 Mon Sep 17 00:00:00 2001 From: William S Fulton Date: Wed, 20 Oct 2004 20:59:33 +0000 Subject: [PATCH] compact default arguments feature (original default argument wrapping mode) git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk@6449 626c5289-ae23-0410-ae9c-e8d60b6d4f22 --- SWIG/Source/CParse/cparse.h | 1 + SWIG/Source/CParse/cscanner.c | 2 +- SWIG/Source/Modules/emit.cxx | 8 ++++++-- SWIG/Source/Modules/lang.cxx | 3 ++- SWIG/Source/Modules/python.cxx | 2 ++ SWIG/Source/Modules/ruby.cxx | 4 +--- SWIG/Source/Swig/cwrap.c | 36 ++++++++++++++++++++++++---------- SWIG/Source/Swig/parms.c | 28 ++++++++++++++++++++++++++ SWIG/Source/Swig/swig.h | 1 + 9 files changed, 68 insertions(+), 17 deletions(-) diff --git a/SWIG/Source/CParse/cparse.h b/SWIG/Source/CParse/cparse.h index 0d8073360..43170e6bf 100644 --- a/SWIG/Source/CParse/cparse.h +++ b/SWIG/Source/CParse/cparse.h @@ -44,6 +44,7 @@ extern int yylex(); extern SwigType *Swig_cparse_type(String *); extern Node *Swig_cparse(File *); extern Hash *Swig_cparse_features(); +extern void SWIG_cparse_set_compact_default_args(int defargs); /* util.c */ extern void Swig_cparse_replace_descriptor(String *s); diff --git a/SWIG/Source/CParse/cscanner.c b/SWIG/Source/CParse/cscanner.c index 63486a447..8489968a7 100644 --- a/SWIG/Source/CParse/cscanner.c +++ b/SWIG/Source/CParse/cscanner.c @@ -47,7 +47,7 @@ static int num_brace = 0; static int last_brace = 0; static int last_id = 0; static int rename_active = 0; - int cparse_cplusplus; + int cparse_cplusplus = 0; /* ----------------------------------------------------------------------------- * Swig_cparse_cplusplus() diff --git a/SWIG/Source/Modules/emit.cxx b/SWIG/Source/Modules/emit.cxx index c2da9a851..8de4f42d3 100644 --- a/SWIG/Source/Modules/emit.cxx +++ b/SWIG/Source/Modules/emit.cxx @@ -220,22 +220,26 @@ int emit_num_arguments(ParmList *parms) { * * Computes the number of required arguments. This function is safe for * use with multi-valued typemaps and knows how to skip over everything - * properly. Note that it does count parameters which have a default value. + * properly. Note that parameters with default values are counted unless + * the compact default args option is on. * ----------------------------------------------------------------------------- */ int emit_num_required(ParmList *parms) { Parm *p = parms; int nargs = 0; Parm *first_default_arg = 0; + int compactdefargs = ParmList_is_compactdefargs(p); while (p) { if (Getattr(p,"tmap:in") && checkAttribute(p,"tmap:in:numinputs","0")) { p = Getattr(p,"tmap:in:next"); } else { if (Getattr(p,"tmap:default")) break; - if (Getattr(p,"value")) + if (Getattr(p,"value")) { if (!first_default_arg) first_default_arg = p; + if (compactdefargs) break; + } nargs+= GetInt(p,"tmap:in:numinputs"); if (Getattr(p,"tmap:in")) { p = Getattr(p,"tmap:in:next"); diff --git a/SWIG/Source/Modules/lang.cxx b/SWIG/Source/Modules/lang.cxx index 143cfb321..6f5911356 100644 --- a/SWIG/Source/Modules/lang.cxx +++ b/SWIG/Source/Modules/lang.cxx @@ -1089,7 +1089,8 @@ Language::staticmemberfunctionHandler(Node *n) { if (!defaultargs && code) { /* Hmmm. An added static member. We have to create a little wrapper for this */ String *body; - String *tmp = NewStringf("%s(%s)", cname, ParmList_str_defaultargs(parms)); + String *parmstring = CPlusPlus ? ParmList_str_defaultargs(parms) : ParmList_str(parms); + String *tmp = NewStringf("%s(%s)", cname, parmstring); body = SwigType_str(type,tmp); Printv(body,code,"\n",NIL); Setattr(n,"wrap:code",body); diff --git a/SWIG/Source/Modules/python.cxx b/SWIG/Source/Modules/python.cxx index 1282aaf37..6a758ac58 100644 --- a/SWIG/Source/Modules/python.cxx +++ b/SWIG/Source/Modules/python.cxx @@ -12,6 +12,7 @@ char cvsroot_python_cxx[] = "$Header$"; #include "swigmod.h" +#include "cparse.h" #include @@ -131,6 +132,7 @@ public: Swig_mark_arg(i); } else if (strcmp(argv[i],"-keyword") == 0) { use_kw = 1; + SWIG_cparse_set_compact_default_args(1); Swig_mark_arg(i); } else if (strcmp(argv[i],"-classic") == 0) { classic = 1; diff --git a/SWIG/Source/Modules/ruby.cxx b/SWIG/Source/Modules/ruby.cxx index 975c31402..005068181 100644 --- a/SWIG/Source/Modules/ruby.cxx +++ b/SWIG/Source/Modules/ruby.cxx @@ -148,7 +148,6 @@ private: File *f_wrappers; File *f_init; - bool use_kw; bool useGlobalModule; bool multipleInheritance; @@ -186,7 +185,6 @@ public: f_header = 0; f_wrappers = 0; f_init = 0; - use_kw = false; useGlobalModule = false; multipleInheritance = false; } @@ -996,7 +994,7 @@ public: int numarg = emit_num_arguments(l); int numreq = emit_num_required(l); int varargs = emit_isvarargs(l); - bool allow_kwargs = use_kw || Getattr(n,"feature:kwargs"); + bool allow_kwargs = Getattr(n,"feature:kwargs"); bool use_director = (current == CONSTRUCTOR_INITIALIZE && Swig_directorclass(n)); int start = (current == MEMBER_FUNC || current == MEMBER_VAR || use_director) ? 1 : 0; diff --git a/SWIG/Source/Swig/cwrap.c b/SWIG/Source/Swig/cwrap.c index 4aecb239a..d619695d2 100644 --- a/SWIG/Source/Swig/cwrap.c +++ b/SWIG/Source/Swig/cwrap.c @@ -17,6 +17,8 @@ char cvsroot_cwrap_c[] = "$Header$"; #include "swig.h" +extern int cparse_cplusplus; + static Parm *nonvoid_parms(Parm *p) { if (p) { SwigType *t = Getattr(p,"type"); @@ -136,6 +138,8 @@ Swig_wrapped_var_assign(SwigType *t, const String_or_char *name) { * * Emit all of the local variables for a list of parameters. Returns the * number of parameters. + * Default values for the local variables are only emitted if the compact default + * argument behaviour is required. * ----------------------------------------------------------------------------- */ int Swig_cargs(Wrapper *w, ParmList *p) { int i; @@ -147,15 +151,22 @@ int Swig_cargs(Wrapper *w, ParmList *p) { SwigType *altty; String *type; int tycode; + int compactdefargs = ParmList_is_compactdefargs(p); i = 0; + while (p != 0) { lname = Swig_cparm_name(p,i); pt = Getattr(p,"type"); if ((SwigType_type(pt) != T_VOID)) { pname = Getattr(p,"name"); -/* pvalue = Getattr(p,"value");*/ - pvalue = 0; + + /* default values only emitted if in compact default args mode */ + if (compactdefargs) + pvalue = Getattr(p,"value"); + else + pvalue = 0; + type = Getattr(p,"type"); altty = SwigType_alttype(type,0); tycode = SwigType_type(type); @@ -683,11 +694,12 @@ Swig_MethodToFunction(Node *n, String *classname, int flags) { /* See if there is any code that we need to emit */ if (!defaultargs && code) { String *body; - String *tmp = NewStringf("%s(%s)", mangled, ParmList_str_defaultargs(p)); + String *parmstring = cparse_cplusplus ? ParmList_str_defaultargs(p) : ParmList_str(p); + String *tmp = NewStringf("%s(%s)", mangled, parmstring); body = SwigType_str(type,tmp); - Delete(tmp); Printv(body,code,"\n",NIL); Setattr(n,"wrap:code",body); + Delete(tmp); Delete(body); } @@ -844,12 +856,13 @@ Swig_ConstructorToFunction(Node *n, String *classname, /* See if there is any code that we need to emit */ if (!defaultargs && code) { - String *body, *tmp; - tmp = NewStringf("%s(%s)", mangled, ParmList_str_defaultargs(parms)); + String *body; + String *parmstring = cparse_cplusplus ? ParmList_str_defaultargs(parms) : ParmList_str(parms); + String *tmp = NewStringf("%s(%s)", mangled, parmstring); body = SwigType_str(type,tmp); - Delete(tmp); Printv(body,code,"\n",NIL); Setattr(n,"wrap:code",body); + Delete(tmp); Delete(body); } @@ -942,7 +955,8 @@ Swig_DestructorToFunction(Node *n, String *classname, int cplus, int flags) mangled = Swig_name_mangle(membername); code = Getattr(n,"code"); if (code) { - String *s = NewStringf("void %s(%s)", mangled, ParmList_str(p)); + String *parmstring = cparse_cplusplus ? ParmList_str_defaultargs(p) : ParmList_str(p); + String *s = NewStringf("void %s(%s)", mangled, parmstring); Printv(s,code,"\n",NIL); Setattr(n,"wrap:code",s); Delete(s); @@ -1011,7 +1025,8 @@ Swig_MembersetToFunction(Node *n, String *classname, int flags) { if (flags & CWRAP_EXTEND) { String *code = Getattr(n,"code"); if (code) { - String *s = NewStringf("void %s(%s)", mangled, ParmList_str(parms)); + String *parmstring = cparse_cplusplus ? ParmList_str_defaultargs(parms) : ParmList_str(parms); + String *s = NewStringf("void %s(%s)", mangled, parmstring); Printv(s,code,"\n",NIL); Setattr(n,"wrap:code",s); Delete(s); @@ -1068,7 +1083,8 @@ Swig_MembergetToFunction(Node *n, String *classname, int flags) { if (flags & CWRAP_EXTEND) { String *code = Getattr(n,"code"); if (code) { - String *tmp = NewStringf("%s(%s)", mangled, ParmList_str(parms)); + String *parmstring = cparse_cplusplus ? ParmList_str_defaultargs(parms) : ParmList_str(parms); + String *tmp = NewStringf("%s(%s)", mangled, parmstring); String *s = SwigType_str(ty,tmp); Delete(tmp); Printv(s,code,"\n",NIL); diff --git a/SWIG/Source/Swig/parms.c b/SWIG/Source/Swig/parms.c index 3b8993851..f1378a752 100644 --- a/SWIG/Source/Swig/parms.c +++ b/SWIG/Source/Swig/parms.c @@ -48,6 +48,7 @@ Parm *CopyParm(Parm *p) { String *ignore; String *alttype; String *byname; + String *compactdefargs; Parm *np = NewHash(); t = Getattr(p,"type"); @@ -57,6 +58,7 @@ Parm *CopyParm(Parm *p) { ignore = Getattr(p,"ignore"); alttype = Getattr(p,"alttype"); byname = Getattr(p, "arg:byname"); + compactdefargs = Getattr(p, "compactdefargs"); if (t) Setattr(np,"type",Copy(t)); @@ -72,6 +74,8 @@ Parm *CopyParm(Parm *p) { Setattr(np,"alttype", Copy(alttype)); if (byname) Setattr(np, "arg:byname", Copy(byname)); + if (compactdefargs) + Setattr(np, "compactdefargs", Copy(compactdefargs)); Setfile(np,Getfile(p)); Setline(np,Getline(p)); @@ -218,5 +222,29 @@ String *ParmList_protostr(ParmList *p) { return out; } +/* --------------------------------------------------------------------- + * ParmList_is_compactdefargs() + * + * Returns 1 if the parameter list passed in is marked for compact argument + * handling (by the "compactdefargs" attribute). Otherwise returns 0. + * ---------------------------------------------------------------------- */ + +int ParmList_is_compactdefargs(ParmList *p) { + int compactdefargs = 0; + + if (p) { + compactdefargs = Getattr(p,"compactdefargs") ? 1 : 0; + + /* The "compactdefargs" attribute should only be set on the first parameter in the list. + * However, sometimes an extra parameter is inserted at the beginning of the parameter list, + * so we check the 2nd parameter too. */ + if (!compactdefargs) { + Parm *nextparm = nextSibling(p); + compactdefargs = (nextparm && Getattr(nextparm,"compactdefargs")) ? 1 : 0; + } + } + + return compactdefargs; +} diff --git a/SWIG/Source/Swig/swig.h b/SWIG/Source/Swig/swig.h index fdabdab76..38b8fb5b0 100644 --- a/SWIG/Source/Swig/swig.h +++ b/SWIG/Source/Swig/swig.h @@ -334,6 +334,7 @@ extern int ParmList_numrequired(ParmList *); extern String *ParmList_str(ParmList *); extern String *ParmList_str_defaultargs(ParmList *); extern String *ParmList_protostr(ParmList *); +extern int ParmList_is_compactdefargs(ParmList *p); /* --- Parse tree support --- */