Remove numerous hard coded 'result' variable name in generated c/c++ wrappers. The variable name is now defined in just one place, making it possible to change the name easily if a target language so wishes - see cwrap.c.

git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk@12830 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
William S Fulton 2011-10-30 21:51:50 +00:00
commit 24133bacd7
29 changed files with 252 additions and 227 deletions

View file

@ -17,6 +17,7 @@ char cvsroot_cwrap_c[] = "$Id$";
#include "swig.h"
extern int cparse_cplusplus;
static const char *cresult_variable_name = "result";
static Parm *nonvoid_parms(Parm *p) {
if (p) {
@ -28,7 +29,28 @@ static Parm *nonvoid_parms(Parm *p) {
}
/* -----------------------------------------------------------------------------
* Swig_parm_name()
* Swig_cresult_name_set()
*
* Change the name of the variable used to hold the return value from C/C++ wrapper functions
* from the default "result".
* ----------------------------------------------------------------------------- */
void Swig_cresult_name_set(const char *new_name) {
cresult_variable_name = new_name;
}
/* -----------------------------------------------------------------------------
* Swig_cresult_name()
*
* Get the name of the variable used to hold the return value from C/C++ wrapper functions
* ----------------------------------------------------------------------------- */
const char *Swig_cresult_name(void) {
return cresult_variable_name;
}
/* -----------------------------------------------------------------------------
* Swig_cparm_name()
*
* Generates a name for the ith argument in an argument list
* ----------------------------------------------------------------------------- */
@ -880,7 +902,7 @@ int Swig_MethodToFunction(Node *n, const_String_or_char_ptr nspace, String *clas
}
call = Swig_cmethod_call(explicitcall_name ? explicitcall_name : name, p, self, explicit_qualifier, director_type);
cres = Swig_cresult(Getattr(n, "type"), "result", call);
cres = Swig_cresult(Getattr(n, "type"), Swig_cresult_name(), call);
if (pure_virtual && is_director && (flags & CWRAP_DIRECTOR_TWO_CALLS)) {
String *qualifier = SwigType_namestr(Getattr(Getattr(parentNode(n), "typescope"), "qname"));
@ -893,7 +915,7 @@ int Swig_MethodToFunction(Node *n, const_String_or_char_ptr nspace, String *clas
/* Create two method calls, one to call the explicit method, the other a normal polymorphic function call */
String *cres_both_calls = NewStringf("");
String *call_extra = Swig_cmethod_call(name, p, self, 0, director_type);
String *cres_extra = Swig_cresult(Getattr(n, "type"), "result", call_extra);
String *cres_extra = Swig_cresult(Getattr(n, "type"), Swig_cresult_name(), call_extra);
Printv(cres_both_calls, "if (upcall) {\n", cres, "\n", "} else {", cres_extra, "\n}", NIL);
Setattr(n, "wrap:action", cres_both_calls);
Delete(cres_extra);
@ -974,12 +996,12 @@ int Swig_MethodToFunction(Node *n, const_String_or_char_ptr nspace, String *clas
}
}
Append(func, ")");
cres = Swig_cresult(Getattr(n, "type"), "result", func);
cres = Swig_cresult(Getattr(n, "type"), Swig_cresult_name(), func);
Setattr(n, "wrap:action", cres);
Delete(cres);
} else {
String *call = Swig_cfunction_call(mangled, p);
String *cres = Swig_cresult(Getattr(n, "type"), "result", call);
String *cres = Swig_cresult(Getattr(n, "type"), Swig_cresult_name(), call);
Setattr(n, "wrap:action", cres);
Delete(call);
Delete(cres);
@ -1089,7 +1111,7 @@ int Swig_ConstructorToFunction(Node *n, const_String_or_char_ptr nspace, String
}
call = Swig_cfunction_call(mangled, parms);
cres = Swig_cresult(type, "result", call);
cres = Swig_cresult(type, Swig_cresult_name(), call);
Setattr(n, "wrap:action", cres);
Delete(cres);
Delete(call);
@ -1120,7 +1142,7 @@ int Swig_ConstructorToFunction(Node *n, const_String_or_char_ptr nspace, String
* implemented in the target language, calls to those methods will
* generate Swig::DirectorPureVirtualException exceptions.
*/
String *cres = Swig_cresult(type, "result", director_call);
String *cres = Swig_cresult(type, Swig_cresult_name(), director_call);
Append(action, cres);
Delete(cres);
} else {
@ -1135,11 +1157,11 @@ int Swig_ConstructorToFunction(Node *n, const_String_or_char_ptr nspace, String
Append(action, director_ctor);
Replaceall(action, "$comparison", tmp_none_comparison);
cres = Swig_cresult(type, "result", director_call);
cres = Swig_cresult(type, Swig_cresult_name(), director_call);
Replaceall(action, "$director_new", cres);
Delete(cres);
cres = Swig_cresult(type, "result", nodirector_call);
cres = Swig_cresult(type, Swig_cresult_name(), nodirector_call);
Replaceall(action, "$nondirector_new", cres);
Delete(cres);
}
@ -1149,14 +1171,14 @@ int Swig_ConstructorToFunction(Node *n, const_String_or_char_ptr nspace, String
Delete(directorname);
} else {
String *call = Swig_cppconstructor_call(classname, parms);
String *cres = Swig_cresult(type, "result", call);
String *cres = Swig_cresult(type, Swig_cresult_name(), call);
Setattr(n, "wrap:action", cres);
Delete(cres);
Delete(call);
}
} else {
String *call = Swig_cconstructor_call(classname);
String *cres = Swig_cresult(type, "result", call);
String *cres = Swig_cresult(type, Swig_cresult_name(), call);
Setattr(n, "wrap:action", cres);
Delete(cres);
Delete(call);
@ -1371,7 +1393,7 @@ int Swig_MembergetToFunction(Node *n, String *classname, int flags) {
Swig_add_extension_code(n, mangled, parms, ty, code, cparse_cplusplus, "self");
}
call = Swig_cfunction_call(mangled, parms);
cres = Swig_cresult(ty, "result", call);
cres = Swig_cresult(ty, Swig_cresult_name(), call);
Setattr(n, "wrap:action", cres);
Delete(cres);
@ -1381,7 +1403,7 @@ int Swig_MembergetToFunction(Node *n, String *classname, int flags) {
Delete(gname);
} else {
String *call = Swig_cmemberget_call(name, type, self, varcref);
String *cres = Swig_cresult(ty, "result", call);
String *cres = Swig_cresult(ty, Swig_cresult_name(), call);
Setattr(n, "wrap:action", cres);
Delete(call);
Delete(cres);
@ -1472,14 +1494,14 @@ int Swig_VargetToFunction(Node *n, int flags) {
String *sname = Swig_name_get(0, name);
String *mangled = Swig_name_mangle(sname);
call = Swig_cfunction_call(mangled, 0);
cres = Swig_cresult(ty, "result", call);
cres = Swig_cresult(ty, Swig_cresult_name(), call);
Setattr(n, "wrap:action", cres);
Delete(mangled);
Delete(sname);
} else {
String *nname = SwigType_namestr(name);
call = Swig_wrapped_var_assign(type, nname, varcref);
cres = Swig_cresult(ty, "result", call);
cres = Swig_cresult(ty, Swig_cresult_name(), call);
Setattr(n, "wrap:action", cres);
Delete(nname);
}

View file

@ -335,6 +335,8 @@ extern int ParmList_is_compactdefargs(ParmList *p);
extern String *Swig_stringify_with_location(DOH *object);
/* --- C Wrappers --- */
extern void Swig_cresult_name_set(const char *new_name);
extern const char *Swig_cresult_name(void);
extern String *Swig_cparm_name(Parm *p, int i);
extern String *Swig_wrapped_var_type(SwigType *t, int varcref);
extern int Swig_cargs(Wrapper *w, ParmList *l);

View file

@ -1390,6 +1390,7 @@ static String *Swig_typemap_lookup_impl(const_String_or_char_ptr tmap_method, No
* If f and actioncode are NULL, then the caller is just looking to attach the "out" attributes
* ie, not use the typemap code, otherwise both f and actioncode must be non null. */
if (actioncode) {
const String *result_equals = NewStringf("%s = ", Swig_cresult_name());
clname = Copy(actioncode);
/* check that the code in the typemap can be used in this optimal way.
* The code should be in the form "result = ...;\n". We need to extract
@ -1398,8 +1399,8 @@ static String *Swig_typemap_lookup_impl(const_String_or_char_ptr tmap_method, No
* hack and circumvents the normal requirement for a temporary variable
* to hold the result returned from a wrapped function call.
*/
if (Strncmp(clname, "result = ", 9) == 0) {
int numreplacements = Replace(clname, "result = ", "", DOH_REPLACE_ID_BEGIN);
if (Strncmp(clname, result_equals, 9) == 0) {
int numreplacements = Replace(clname, result_equals, "", DOH_REPLACE_ID_BEGIN);
if (numreplacements == 1) {
numreplacements = Replace(clname, ";\n", "", DOH_REPLACE_ID_END);
if (numreplacements == 1) {