clean up some code

git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/branches/gsoc2009-sploving@11624 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
Baozeng Ding 2009-08-17 14:47:43 +00:00
commit d567ff5806

View file

@ -122,12 +122,12 @@ public:
if (CPlusPlus) if (CPlusPlus)
Printf(f_wrappers, "}\n"); Printf(f_wrappers, "}\n");
/* create the file to generate the module: "builder.sce" */ /* Create the file to generate the module: "builder.sce" */
if(hasfunction_flag) { if(hasfunction_flag) {
Printf(f_builder_code, "];\n"); Printf(f_builder_code, "];\n");
Printf(f_builder_code, "ilib_build(ilib_name,table,files,libs);\n"); Printf(f_builder_code, "ilib_build(ilib_name,table,files,libs);\n");
Printf(f_builder_code, "exit"); Printf(f_builder_code, "exit");
File *f_builder=NewFile(NewStringf("%sbuilder.sce", SWIG_output_directory()), "w", SWIG_output_files()); File *f_builder = NewFile(NewStringf("%sbuilder.sce", SWIG_output_directory()), "w", SWIG_output_files());
Printv(f_builder, f_builder_code, NIL); Printv(f_builder, f_builder_code, NIL);
Close(f_builder); Close(f_builder);
Delete(f_builder); Delete(f_builder);
@ -169,13 +169,12 @@ public:
String *tm; String *tm;
int j; int j;
/* Get the useful information from the node */ /* Determine whether the function is overloaded or not */
String *nodeType = Getattr(n, "nodeType");
int constructor = (!Cmp(nodeType, "constructor"));
// int destructor = (!Cmp(nodeType, "destructor"));
String *storage = Getattr(n, "storage");
bool overloaded = !!Getattr(n, "sym:overloaded"); bool overloaded = !!Getattr(n, "sym:overloaded");
/* Determine whether the function is the last overloaded */
bool last_overload = overloaded && !Getattr(n, "sym:nextSibling"); bool last_overload = overloaded && !Getattr(n, "sym:nextSibling");
String *iname = Getattr(n, "sym:name"); String *iname = Getattr(n, "sym:name");
String *wname = Swig_name_wrapper(iname); String *wname = Swig_name_wrapper(iname);
String *overname = Copy(wname); String *overname = Copy(wname);
@ -201,21 +200,8 @@ public:
int num_arguments = emit_num_arguments(l); int num_arguments = emit_num_arguments(l);
int num_required = emit_num_required(l); int num_required = emit_num_required(l);
/* the number of the output */ /* The number of the output */
int out_required = 0; int out_required = 0;
//int varargs = emit_isvarargs(l);
if (constructor && num_arguments == 1 && num_required == 1) {
if (Cmp(storage, "explicit") == 0) {
Node *parent = Swig_methodclass(n);
if (GetFlag(parent, "feature:implicitconv")) {
String *desc = NewStringf("SWIGTYPE%s", SwigType_manglestr(Getattr(n, "type")));
Printf(f->code, "if (SWIG_CheckImplicit(%s)) SWIG_fail;\n", desc);
Delete(desc);
}
}
}
/* Walk the function parameter list and generate code to get arguments */ /* Walk the function parameter list and generate code to get arguments */
for (j = 0, p = l; j < num_arguments; ++j) { for (j = 0, p = l; j < num_arguments; ++j) {
@ -234,15 +220,18 @@ public:
continue; continue;
} }
String *getargs = NewString(""); String *getargs = NewString("");
/* The paremeter is variable */
if (j >= num_required) if (j >= num_required)
Printf(getargs, "if (Rhs > %d) {\n%s\n}", j, tm); Printf(getargs, "if (Rhs > %d) {\n%s\n}", j, tm);
else else
Printv(getargs, tm, NIL); Printv(getargs, tm, NIL);
Printv(f->code, getargs, "\n", NIL); Printv(f->code, getargs, "\n", NIL);
Delete(getargs); Delete(getargs);
p = Getattr(p, "tmap:in:next"); p = Getattr(p, "tmap:in:next");
continue; continue;
} else { }
else {
Swig_warning(WARN_TYPEMAP_IN_UNDEF, input_file, line_number, "Unable to use type %s as a function argument.\n", SwigType_str(pt, 0)); Swig_warning(WARN_TYPEMAP_IN_UNDEF, input_file, line_number, "Unable to use type %s as a function argument.\n", SwigType_str(pt, 0));
break; break;
} }
@ -259,11 +248,13 @@ public:
if ((tm = Swig_typemap_lookup_out("out", n, "result", f, actioncode))) { if ((tm = Swig_typemap_lookup_out("out", n, "result", f, actioncode))) {
Replaceall(tm, "$result", "result"); Replaceall(tm, "$result", "result");
if(out_required>0)
Printf(f->code,"iOutNum++;\niVarOut++;\n"); /* There are more than one output */
if (out_required > 0)
Printf(f->code, "iOutNum ++;\niVarOut ++;\n");
Printf(f->code, "%s\n", tm); Printf(f->code, "%s\n", tm);
if(strlen(Char(tm))!=0) if (strlen(Char(tm)) != 0)
out_required++; out_required ++;
} }
else { else {
Swig_warning(WARN_TYPEMAP_OUT_UNDEF, input_file, line_number, "Unable to use return type %s in function %s.\n", SwigType_str(d, 0), iname); Swig_warning(WARN_TYPEMAP_OUT_UNDEF, input_file, line_number, "Unable to use return type %s in function %s.\n", SwigType_str(d, 0), iname);
@ -273,11 +264,11 @@ public:
String *outarg = NewString(""); String *outarg = NewString("");
for (p = l; p;) { for (p = l; p;) {
if ((tm = Getattr(p, "tmap:argout"))) { if ((tm = Getattr(p, "tmap:argout"))) {
if(out_required>0) if (out_required > 0)
Printf(f->code,"iOutNum++;\niVarOut++;\n"); Printf(f->code,"iOutNum ++;\niVarOut ++;\n");
Printv(outarg, tm, "\n", NIL); Printv(outarg, tm, "\n", NIL);
p = Getattr(p, "tmap:argout:next"); p = Getattr(p, "tmap:argout:next");
out_required++; out_required ++;
} else { } else {
p = nextSibling(p); p = nextSibling(p);
} }
@ -309,21 +300,24 @@ public:
/* Output cleanup code */ /* Output cleanup code */
Printv(f->code, cleanup, NIL); Printv(f->code, cleanup, NIL);
Delete(cleanup);
/* Insert the code checking for the number of input and output */ /* Insert the code checking for the number of input and output */
int flag; int flag;
if(out_required == 0) { if (out_required == 0) {
out_required = 1; out_required = 1;
flag = 0; flag = 0;
} }
else { else {
flag = 1; flag = 1;
} }
/* Insert the code checking the number of input */
Printf(f->def, "CheckRhs(%d, %d);\n",num_required,num_arguments); Printf(f->def, "CheckRhs(%d, %d);\n",num_required,num_arguments);
Printf(f->def, "CheckLhs(%d, %d);\n",out_required,out_required); Printf(f->def, "CheckLhs(%d, %d);\n",out_required,out_required);
/* Insert the order of output parameters*/ /* Insert the order of output parameters*/
if(flag) if (flag)
Printf(f->def, "\nint iOutNum = 1;\nint iVarOut = Rhs + 1;"); Printf(f->def, "\nint iOutNum = 1;\nint iVarOut = Rhs + 1;");
/* Finish the the code for the function */ /* Finish the the code for the function */
@ -358,22 +352,23 @@ public:
String *iname = Getattr(n, "sym:name"); String *iname = Getattr(n, "sym:name");
String *wname = Swig_name_wrapper(iname); String *wname = Swig_name_wrapper(iname);
int maxargs; int maxargs;
/* Generate the dispatch function */
String *dispatch = Swig_overload_dispatch(n, "return %s(fname, fname_len);", &maxargs); String *dispatch = Swig_overload_dispatch(n, "return %s(fname, fname_len);", &maxargs);
String *tmp = NewString(""); String *tmp = NewString("");
Printv(f->def, "int ", wname, " (char *fname, unsigned long fname_len) {\n", NIL); Printv(f->def, "int ", wname, " (char *fname, unsigned long fname_len) {\n", NIL);
/* Get the number of the parameters */
Wrapper_add_local(f, "argc", "int argc = Rhs;"); Wrapper_add_local(f, "argc", "int argc = Rhs;");
//Printf(tmp, "octave_value_ref argv[%d]={", maxargs);
//for (int j = 0; j < maxargs; ++j) /* Dump the dispatch function */
//Printf(tmp, "%soctave_value_ref(args,%d)", j ? "," : " ", j);
//Printf(tmp, "}");
//Wrapper_add_local(f, "argv", tmp);
Printv(f->code, dispatch, "\n", NIL); Printv(f->code, dispatch, "\n", NIL);
Printf(f->code, "Scierror(999, _(\"No matching function for overload\"));\n"); Printf(f->code, "Scierror(999, _(\"No matching function for overload\"));\n");
Printf(f->code, "return 0;\n"); Printf(f->code, "return 0;\n");
Printv(f->code, "}\n", NIL); Printv(f->code, "}\n", NIL);
Wrapper_print(f, f_wrappers); Wrapper_print(f, f_wrappers);
Delete(tmp); Delete(tmp);
DelWrapper(f); DelWrapper(f);
Delete(dispatch); Delete(dispatch);
@ -396,6 +391,7 @@ public:
if (!addSymbol(iname, n)) if (!addSymbol(iname, n))
return SWIG_ERROR; return SWIG_ERROR;
/* The rows and cols name of the variable */
String *rowname = NewString(""); String *rowname = NewString("");
String *colname = NewString(""); String *colname = NewString("");
String *iscomplexname = NewString(""); String *iscomplexname = NewString("");
@ -403,7 +399,7 @@ public:
Printf(colname, "iCols_%s", iname); Printf(colname, "iCols_%s", iname);
Printf(iscomplexname, "isComplex_%s", iname); Printf(iscomplexname, "isComplex_%s", iname);
/* two wrapper function to get and set the variable */ /* Two wrapper function to get and set the variable */
String *tm; String *tm;
String *globalVar = NewString(""); String *globalVar = NewString("");
Wrapper *getf = NewWrapper(); Wrapper *getf = NewWrapper();
@ -424,10 +420,10 @@ public:
Printf(setf->def, "CheckRhs(1, 1);\n"); Printf(setf->def, "CheckRhs(1, 1);\n");
Printf(setf->def, "CheckLhs(1, 1);\n"); Printf(setf->def, "CheckLhs(1, 1);\n");
/* add the local variable */ /* Add the local variable */
Wrapper_add_local(setf, "piAddrVar", "int *piAddrVar"); Wrapper_add_local(setf, "piAddrVar", "int *piAddrVar");
/* deal with the set function */ /* Deal with the set function */
if (is_assignable(n)) { if (is_assignable(n)) {
Setattr(n, "wrap:name", setname); Setattr(n, "wrap:name", setname);
if (Getattr(n, "unnamedinstance")) if (Getattr(n, "unnamedinstance"))
@ -450,7 +446,7 @@ public:
Wrapper_print(setf, f_wrappers); Wrapper_print(setf, f_wrappers);
Printf(f_builder_code, "\"%s\",\"%s\";", setname, setname); Printf(f_builder_code, "\"%s\",\"%s\";", setname, setname);
/* deal with the get function */ /* Deal with the get function */
Setattr(n, "wrap:name", getname); Setattr(n, "wrap:name", getname);
int addfail = 0; int addfail = 0;
Printv(getf->def, "int ", getname, " (char *fname, unsigned long fname_len){\n", NIL); Printv(getf->def, "int ", getname, " (char *fname, unsigned long fname_len){\n", NIL);
@ -459,7 +455,7 @@ public:
Printf(getf->def, "CheckRhs(0, 0);\n"); Printf(getf->def, "CheckRhs(0, 0);\n");
Printf(getf->def, "CheckLhs(1, 1);\n"); Printf(getf->def, "CheckLhs(1, 1);\n");
/* Insert the order of output parameters*/ /* Insert the order of output parameters */
Printf(getf->def, "\nint iOutNum=1;\nint iVarOut=Rhs+1;"); Printf(getf->def, "\nint iOutNum=1;\nint iVarOut=Rhs+1;");
if ((tm = Swig_typemap_lookup("varout", n, name, 0))) { if ((tm = Swig_typemap_lookup("varout", n, name, 0))) {
@ -474,11 +470,12 @@ public:
Replaceall(tm, "isComplex", iscomplexname); Replaceall(tm, "isComplex", iscomplexname);
addfail = emit_action_code(n, getf->code, tm); addfail = emit_action_code(n, getf->code, tm);
Delete(tm); Delete(tm);
} else { }
else {
Swig_warning(WARN_TYPEMAP_VAROUT_UNDEF, input_file, line_number, "Unable to read variable of type %s\n", SwigType_str(t, 0)); Swig_warning(WARN_TYPEMAP_VAROUT_UNDEF, input_file, line_number, "Unable to read variable of type %s\n", SwigType_str(t, 0));
} }
/*Dump the wrapper function */ /* Dump the wrapper function */
Append(getf->code, "}\n"); Append(getf->code, "}\n");
Wrapper_print(getf, f_wrappers); Wrapper_print(getf, f_wrappers);
Printf(f_header,"%s", globalVar); Printf(f_header,"%s", globalVar);
@ -491,9 +488,8 @@ public:
DelWrapper(setf); DelWrapper(setf);
DelWrapper(getf); DelWrapper(getf);
return SWIG_OK; return SWIG_OK;
} }
/* ----------------------------------------------------------------------- /* -----------------------------------------------------------------------
* constantWrapper() * constantWrapper()
@ -514,7 +510,7 @@ public:
if (!addSymbol(iname, n)) if (!addSymbol(iname, n))
return SWIG_ERROR; return SWIG_ERROR;
/*use the get function to get the constant value */ /* Use the get function to get the constant value */
Wrapper *getf = NewWrapper(); Wrapper *getf = NewWrapper();
String *getname = Swig_name_get(iname); String *getname = Swig_name_get(iname);
Setattr(n, "wrap:name", getname); Setattr(n, "wrap:name", getname);
@ -538,11 +534,13 @@ public:
Swig_warning(WARN_TYPEMAP_VAROUT_UNDEF, input_file, line_number, "Unable to read variable of type %s\n", SwigType_str(type, 0)); Swig_warning(WARN_TYPEMAP_VAROUT_UNDEF, input_file, line_number, "Unable to read variable of type %s\n", SwigType_str(type, 0));
} }
/*Dump the wrapper function */ /* Dump the wrapper function */
Append(getf->code, "}\n"); Append(getf->code, "}\n");
Wrapper_print(getf, f_wrappers); Wrapper_print(getf, f_wrappers);
Printf(f_builder_code, "\"%s\",\"%s\";", getname, getname); Printf(f_builder_code, "\"%s\",\"%s\";", getname, getname);
DelWrapper(getf);
return SWIG_OK; return SWIG_OK;
} }