From 91ff20631b199fb042f99e09dae10477258f3d08 Mon Sep 17 00:00:00 2001 From: Dave Beazley Date: Mon, 24 Jul 2000 03:57:26 +0000 Subject: [PATCH] Rewrote all of the low-level C/C++ code generators. Continued destruction of old C++ code. git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk@601 626c5289-ae23-0410-ae9c-e8d60b6d4f22 --- SWIG/Source/SWIG1.1/Makefile.in | 4 +- SWIG/Source/SWIG1.1/cplus.cxx | 808 ++++++-------------------------- SWIG/Source/SWIG1.1/emit.cxx | 308 ++++-------- SWIG/Source/SWIG1.1/internal.h | 1 + SWIG/Source/SWIG1.1/lang.cxx | 16 +- SWIG/Source/SWIG1.1/main.cxx | 11 +- SWIG/Source/SWIG1.1/parser.yxx | 349 +++++++++----- SWIG/Source/SWIG1.1/scanner.cxx | 58 +-- SWIG/Source/SWIG1.1/sstring.cxx | 106 ----- SWIG/Source/SWIG1.1/swig11.h | 4 +- SWIG/Source/SWIG1.1/typemap.cxx | 19 +- 11 files changed, 530 insertions(+), 1154 deletions(-) delete mode 100644 SWIG/Source/SWIG1.1/sstring.cxx diff --git a/SWIG/Source/SWIG1.1/Makefile.in b/SWIG/Source/SWIG1.1/Makefile.in index 90887cf48..4e4fb07f7 100644 --- a/SWIG/Source/SWIG1.1/Makefile.in +++ b/SWIG/Source/SWIG1.1/Makefile.in @@ -15,11 +15,11 @@ RANLIB = @RANLIB@ TARGET = libswig11.a OBJS = parser.o main.o scanner.o \ - emit.o cplus.o lang.o sstring.o typemap.o + emit.o cplus.o lang.o typemap.o SRCS = main.cxx scanner.cxx \ emit.cxx cplus.cxx lang.cxx \ - sstring.cxx typemap.cxx + typemap.cxx PARSER = $(srcdir)/parser.yxx INCLUDE = -I$(srcdir)/../Include \ diff --git a/SWIG/Source/SWIG1.1/cplus.cxx b/SWIG/Source/SWIG1.1/cplus.cxx index 84192278e..47cb5df9c 100644 --- a/SWIG/Source/SWIG1.1/cplus.cxx +++ b/SWIG/Source/SWIG1.1/cplus.cxx @@ -210,8 +210,8 @@ public: int is_virtual; CPP_function(char *n, char *i, DataType *t, ParmList *l, int s, int v = 0) { - name = copy_string(n); - iname = copy_string(i); + name = Swig_copy_string(n); + iname = Swig_copy_string(i); ret_type = CopyDataType(t); parms = CopyParmList(l); is_static = s; @@ -229,7 +229,7 @@ public: } if (AddMethods) { if (strlen(Char(CCode))) - code = copy_string(Char(CCode)); + code = Swig_copy_string(Char(CCode)); else code = 0; } else { @@ -292,8 +292,8 @@ class CPP_constructor : public CPP_member { public: ParmList *parms; CPP_constructor(char *n, char *i, ParmList *l) { - name = copy_string(n); - iname = copy_string(i); + name = Swig_copy_string(n); + iname = Swig_copy_string(i); parms = CopyParmList(l); new_method = AddMethods; inherited = 0; @@ -303,7 +303,7 @@ public: id = type_id; if (AddMethods) { if (strlen(Char(CCode))) - code = copy_string(Char(CCode)); + code = Swig_copy_string(Char(CCode)); else code = 0; } else { @@ -339,8 +339,8 @@ class CPP_destructor : public CPP_member { public: CPP_destructor(char *n, char *i) { - name = copy_string(n); - iname = copy_string(i); + name = Swig_copy_string(n); + iname = Swig_copy_string(i); new_method = AddMethods; next = 0; inherited = 0; @@ -349,7 +349,7 @@ public: id = type_id; if (AddMethods) { if (strlen(Char(CCode))) - code = copy_string(Char(CCode)); + code = Swig_copy_string(Char(CCode)); else code = 0; } else { @@ -377,8 +377,8 @@ public: DataType *type; int status; CPP_variable(char *n, char *i, DataType *t, int s) { - name = copy_string(n); - iname = copy_string(i); + name = Swig_copy_string(n); + iname = Swig_copy_string(i); type = CopyDataType(t); is_static = s; status = Status; @@ -450,10 +450,10 @@ public: char *value; DataType *type; CPP_constant(char *n, char *i, DataType *t, char *v) { - name = copy_string(n); - iname = copy_string(i); + name = Swig_copy_string(n); + iname = Swig_copy_string(i); type = CopyDataType(t); - value = copy_string(v); + value = Swig_copy_string(v); new_method = AddMethods; next = 0; line = line_number; @@ -512,8 +512,8 @@ public: CPP_class(char *name, char *ctype) { CPP_class *c; - classname = copy_string(name); - classtype = copy_string(ctype); + classname = Swig_copy_string(name); + classtype = Swig_copy_string(ctype); classrename = 0; baseclass = 0; local = NewHash(); // Create hash table for storing local datatypes @@ -734,7 +734,7 @@ void cplus_open_class(char *name, char *rname, char *ctype) { // somehow, but no other information is known. We'll // make it our current class and fix it up a bit current_class = c; - c->classtype = copy_string(ctype); + c->classtype = Swig_copy_string(ctype); } } else { // Create a new class @@ -748,7 +748,7 @@ void cplus_open_class(char *name, char *rname, char *ctype) { // If renaming the class, set the new name if (rname) { - current_class->classrename = copy_string(rname); + current_class->classrename = Swig_copy_string(rname); } // Make a typedef for both long and short versions of this datatype @@ -838,7 +838,7 @@ void cplus_class_close(char *name) { if (name) { // The name of our class suddenly changed by typedef. Fix things up - current_class->classname = copy_string(name); + current_class->classname = Swig_copy_string(name); // This flag indicates that the class needs to have it's type stripped off current_class->strip = 1; @@ -921,7 +921,7 @@ void cplus_inherit(int count, char **baseclass) { if (count) { current_class->baseclass = (char **) new char*[count+1]; for (i = 0; i < count; i++) - current_class->baseclass[i] = copy_string(baseclass[i]); + current_class->baseclass[i] = Swig_copy_string(baseclass[i]); current_class->baseclass[i] = 0; } else { baseclass = 0; @@ -1475,164 +1475,58 @@ static DOHHash *member_hash = 0; // Hash wrapping member function wrappers to s void cplus_emit_member_func(char *classname, char *classtype, char *classrename, char *mname, char *mrename, DataType *type, ParmList *l, int mode) { - Parm *p; - ParmList *newparms; - int i; - DOHString *wrap; - char cname[512],iname[512]; - DOHString *key; - char *prefix; - char *prev_wrap = 0; - char *temp_mname; - wrap = NewString(""); + Wrapper *w; + char *code = 0; + char iname[512]; + char fullname[512]; + DOHString *key; + char *prev_wrap = 0; - key = NewString(""); - // First generate a proper name for the member function + key = NewString(""); - // Get the base class of this member - if (!mrename) temp_mname = mname; - else temp_mname = mrename; + if (classtype) { + sprintf(fullname,"%s%s", classtype, classname); + } else { + strcpy(fullname, classname); + } - char *bc = cplus_base_class(temp_mname); - if (!bc) bc = classname; - if (strlen(bc) == 0) bc = classname; + w = Swig_cmethod_wrapper(fullname, mname, type, l, code); - // Generate the name of the C wrapper function (is always the same, regardless - // of renaming). + if (!classrename) classrename = classname; + if (!mrename) mrename = mname; - strcpy(cname, Swig_name_member(bc,mname)); + strcpy(iname, Swig_name_member(classrename, mrename)); - // Generate the scripting name of this function - if (classrename) - prefix = classrename; - else - prefix = classname; + char *bc = cplus_base_class(mrename); + if (!bc) bc = classname; + if (strlen(bc) == 0) bc = classname; - if (mrename) - strcpy(iname,Swig_name_member(prefix,mrename)); - else - strcpy(iname,Swig_name_member(prefix,mname)); + Printf(key,"%s+",Wrapper_Getname(w)); + ParmList_print_types(l,key); + if (!member_hash) member_hash = NewHash(); + if (Getattr(member_hash,key)) { + prev_wrap = GetChar(member_hash,key); + } else { + Setattr(member_hash,key,iname); + } - // Now check to see if we have already wrapped a function like this. - // If so, we'll just use the existing wrapper. - - Printf(key,"%s+",cname); - ParmList_print_types(l,key); - char *temp = copy_string(iname); - if (!member_hash) member_hash = NewHash(); - if (Getattr(member_hash,key)) { - delete [] temp; - prev_wrap = GetChar(member_hash,key); - } else { - Setattr(member_hash,key,temp); - delete [] temp; + if (!prev_wrap) { + if (mode && code) { + /* Produce an actual C wrapper */ + Wrapper_print(w,f_wrappers); + } else if (!mode) { + /* Nah. Just produce a string that does the work for us */ + emit_set_action(Swig_cmethod_call(mname, Wrapper_Getparms(w))); } - - // Only generate code if an already existing wrapper doesn't exist - - if (!prev_wrap) { - - // If mode = 0: Then we go ahead and create a wrapper macro - - if (!mode) { - strcpy(cname,iname); - Printv(wrap, "#define ", cname, "(_swigobj", 0); - - // Walk down the parameter list and Spit out arguments - - i = 0; - p = ParmList_first(l); - while (p != 0) { - DataType *pt = Parm_Gettype(p); - if ((pt->type != T_VOID) || (pt->is_pointer)) { - Printf(wrap,",_swigarg%d",i); - i++; - } - p = ParmList_next(l); - } - Printf(wrap,") ("); - - if (!ObjCClass) { - Printv(wrap, "_swigobj->", mname, "(", 0); // C++ invocation - } else { - Printv(wrap, "[ _swigobj ", mname, 0); // Objective C invocation - } - i = 0; - p = ParmList_first(l); - while(p != 0) { - DataType *pt = Parm_Gettype(p); - /* if (ObjCClass) Printf(wrap," %s", p->objc_separator); */ - if ((pt->type != T_VOID) || (pt->is_pointer)) { - Printf(wrap,"_swigarg%d",i); - i++; - } - p = ParmList_next(l); - if ((p != 0) && (!ObjCClass)) - Putc(',',wrap); - } - if (!ObjCClass) - Printf(wrap,"))\n"); - else - Printf(wrap,"])\n"); - - // Emit it - fprintf(f_wrappers,"%s",Char(wrap)); - } else { - if (ccode) { - Printf(wrap,"static "); - if (type->is_reference) { - type->is_pointer--; - } - Printf(wrap,"%s", DataType_str(type,0)); - Printv(wrap, " ", cname, "(", classtype, classname, " *self", 0); - - // Walk down the parameter list and Spit out arguments - - p = ParmList_first(l); - while (p != 0) { - DataType *pt = Parm_Gettype(p); - if ((pt->type != T_VOID) || (pt->is_pointer)) { - Printf(wrap,","); - Printf(wrap, DataType_str(pt, Parm_Getname(p))); - } - p = ParmList_next(l); - } - Printf(wrap,") %s", ccode); - fprintf(f_wrappers,"%s\n",Char(wrap)); - } - } - - // Now add a parameter to the beginning of the function and call - // a language specific function to add it. - - newparms = CopyParmList(l); - p = NewParm(0,0); - { - DataType *pt = NewDataType(0); - pt->type = T_USER; - sprintf(pt->name,"%s%s", classtype,classname); - pt->is_pointer = 1; - pt->id = cpp_id; - Parm_Settype(p,pt); - DelDataType(pt); - } - Parm_Setname(p,(char*)"self"); - ParmList_insert(newparms,p,0); // Attach parameter to beginning of list - - // Now wrap the thing. The name of the function is iname - - lang->create_function(cname, iname, type, newparms); - DelParmList(newparms); - } else { - // Already wrapped this function. Just patch it up - lang->create_command(prev_wrap, iname); - } - Delete(key); - Delete(wrap); + lang->create_function(Wrapper_Getname(w), iname, Wrapper_Gettype(w), Wrapper_Getparms(w)); + DelWrapper(w); + } else { + lang->create_command(prev_wrap, iname); + } + Delete(key); } - // ----------------------------------------------------------------------------- // void cplus_emit_static_func(char *classname, char *classtype, char *classrename, // char *mname, char *mrename, DataType *type, @@ -1686,23 +1580,18 @@ void cplus_emit_member_func(char *classname, char *classtype, char *classrename, void cplus_emit_static_func(char *classname, char *, char *classrename, char *mname, char *mrename, DataType *type, ParmList *l, int mode) { - Parm *p; - DOHString *wrap; + char cname[512], iname[512]; DOHString *key; - int i; - char *prefix; char *prev_wrap = 0; - char *temp_mname; key = NewString(""); - wrap = NewString(""); // Generate a function name for the member function - if (!mrename) temp_mname = mname; - else temp_mname = mrename; - char *bc = cplus_base_class(temp_mname); + if (!mrename) mrename = mname; + char *bc = cplus_base_class(mname); + if (!bc) bc = classname; if (strlen(bc) == 0) bc = classname; @@ -1714,105 +1603,38 @@ void cplus_emit_static_func(char *classname, char *, char *classrename, } // Generate the scripting name of this function - if (classrename) - prefix = classrename; - else - prefix = classname; - - if (mrename) - strcpy(iname,Swig_name_member(prefix,mrename)); - else - strcpy(iname,Swig_name_member(prefix,mname)); + if (!classrename) classrename = classname; + strcpy(iname,Swig_name_member(classrename, mrename)); // Perform a hash table lookup to see if we've wrapped anything like this before - - Printf(key,"%s+",cname); - ParmList_print_types(l,key); - char *temp = copy_string(iname); + Printf(key,"%s+%s",cname, ParmList_str(l)); if (!member_hash) member_hash = NewHash(); if (Getattr(member_hash,key)) { - delete [] temp; prev_wrap = GetChar(member_hash,key); } else { - Setattr(member_hash,key,temp); - delete [] temp; + Setattr(member_hash,key,iname); } if (!prev_wrap) { - if (!((mode) || (ObjCClass))) { + if (!mode) { // Not an added method and not objective C, just wrap it lang->create_function(cname,iname, type, l); } else { - // This is either an added method or an objective C class function - // - // If there is attached code, use it. - // Otherwise, assume the function has been written already and - // wrap it. - - Printv(wrap,"static ", DataType_str(type,0), " ", cname, "(", 0); - - // Walk down the parameter list and Spit out arguments - p = ParmList_first(l); - while (p != 0) { - DataType *pt = Parm_Gettype(p); - if ((pt->type != T_VOID) || (pt->is_pointer)) { - Printf(wrap, DataType_str(pt,Parm_Getname(p))); - } - p = ParmList_next(l); - if (p) Printf(wrap, ","); + if (!ccode) { + strcpy(cname, Swig_name_member(classname,mname)); + lang->create_function(cname,iname, type, l); + } else { + Wrapper *w = Swig_cfunction_wrapper(cname, type, l, ccode); + Wrapper_print(w,f_wrappers); + lang->create_function(cname,iname, Wrapper_Gettype(w), Wrapper_Getparms(w)); + DelWrapper(w); } - Printf(wrap, ") "); - if ((mode) && (ccode)) { - Printf(wrap,"%s",ccode); - } else if (ObjCClass) { - // This is an objective-C method - Printf(wrap, "{\n"); - - // Emit the function call. - - if ((type->type != T_VOID) || (type->is_pointer)) { - // Declare the return value - Printv(wrap, tab4, DataType_str(type,"_result"), " = ", 0); - } else { - Printf(wrap,tab4); - } - Printv(wrap, "[ ", classname, " ", mname,0); // Objective C invocation - i = 0; - p = ParmList_first(l); - while(p != 0) { - DataType *pt = Parm_Gettype(p); - /*Printf(wrap," %s", p->objc_separator); */ - Printf(wrap," "); - if ((pt->type != T_VOID) || (pt->is_pointer)) { - if (pt->is_reference) { - Printf(wrap,"*"); - } - Printf(wrap,Parm_Getname(p)); - i++; - } - p = ParmList_next(l); - } - Printf(wrap,"];\n"); - - if ((type->type != T_VOID) || (type->is_pointer)) { - if (type->is_reference) { - Printv(wrap, tab4, "return &_result;\n",0); - } else { - Printv(wrap, tab4, "return _result;\n",0); - } - } - Printf(wrap,"}\n"); - } - if (ObjCClass || (mode && ccode)) - fprintf(f_wrappers,"%s\n",Char(wrap)); - lang->create_function(cname,iname,type,l); } } else { // Already wrapped this function. Just hook up to it. lang->create_command(prev_wrap, iname); } Delete(key); - Delete(wrap); } // ----------------------------------------------------------------------------- @@ -1851,74 +1673,37 @@ void cplus_emit_static_func(char *classname, char *, char *classrename, void cplus_emit_destructor(char *classname, char *classtype, char *classrename, char *mname, char *mrename, int mode) { - Parm *p; - DataType *type; - ParmList *l; - DOHString *wrap; + Wrapper *w; char cname[512],iname[512]; - char *prefix; + char fclassname[512]; - // Construct names for the function + if (!classrename) classrename = classname; - wrap = NewString(""); - - if (classrename) - prefix = classrename; - else - prefix = classname; - strcpy(cname,Swig_name_destroy(classname)); if (mrename) strcpy(iname, Swig_name_destroy(mrename)); else - strcpy(iname, Swig_name_destroy(prefix)); + strcpy(iname, Swig_name_destroy(classrename)); - if (!mode) { - // Spit out a helper function for this member function - Printv(wrap, "#define ", cname, "(_swigobj) (",0); - if (ObjCClass) { - Printv(wrap, "[_swigobj ", mname, "])\n", 0); // Name of the member is the destructor - } else if (CPlusPlus) - Printv(wrap, "delete _swigobj)\n",0); - else - Printv(wrap, "free ((char *) _swigobj))\n", 0); - fprintf(f_wrappers,"%s", Char(wrap)); + sprintf(fclassname,"%s%s", classtype, classname); + if (CPlusPlus) { + w = Swig_cppdestructor_wrapper(fclassname,ccode); } else { - if (ccode) { - Printv(wrap, "static void ", cname, "(", classtype, classname, " *self) ", ccode,0); - fprintf(f_wrappers,"%s\n",Char(wrap)); - } + w = Swig_cdestructor_wrapper(fclassname, ccode); } - - // Make a parameter list for this function - - l = NewParmList(); - p = NewParm(0,0); - { - DataType *pt = NewDataType(0); - pt->type = T_USER; - pt->is_pointer = 1; - pt->id = cpp_id; - sprintf(pt->name,"%s%s", classtype, classname); - Parm_Settype(p,pt); - DelDataType(pt); + if (mode && ccode) { + Wrapper_print(w,f_wrappers); + lang->create_function(Wrapper_Getname(w),iname,Wrapper_Gettype(w), Wrapper_Getparms(w)); + } else if (mode) { + lang->create_function(Wrapper_Getname(w),iname,Wrapper_Gettype(w), Wrapper_Getparms(w)); + } else { + if (CPlusPlus) + emit_set_action(Swig_cppdestructor_call(fclassname)); + else + emit_set_action(Swig_cdestructor_call(fclassname)); + lang->create_function(cname, iname, Wrapper_Gettype(w), Wrapper_Getparms(w)); } - Parm_Setname(p,(char*)"self"); - ParmList_insert(l,p,0); - - type = NewDataType(0); - type->type = T_VOID; - sprintf(type->name,"void"); - type->is_pointer = 0; - type->id = cpp_id; - - // iname is the desired name of the function in the target language - - lang->create_function(cname,iname,type,l); - - DelDataType(type); - DelParmList(l); - Delete(wrap); + DelWrapper(w); } // ----------------------------------------------------------------------------- @@ -1945,119 +1730,39 @@ void cplus_emit_destructor(char *classname, char *classtype, char *classrename, void cplus_emit_constructor(char *classname, char *classtype, char *classrename, char *mname, char *mrename, ParmList *l, int mode) { - Parm *p; - int i; - DataType *type; - DOHString *fcall, *wrap; char cname[512],iname[512]; - char *prefix; - - fcall = NewString(""); - wrap = NewString(""); - + char fclassname[512]; + Wrapper *w; // Construct names for the function - if (classrename) - prefix = classrename; - else - prefix = classname; - + if (!classrename) classrename = classname; strcpy(cname,Swig_name_construct(classname)); if (mrename) strcpy(iname, Swig_name_construct(mrename)); else - strcpy(iname, Swig_name_construct(prefix)); + strcpy(iname, Swig_name_construct(classrename)); - // Create a return type - - type = NewDataType(0); - type->type = T_USER; - sprintf(type->name,"%s%s", classtype,classname); - type->is_pointer = 1; - type->id = cpp_id; + sprintf(fclassname,"%s%s", classtype, classname); + if (CPlusPlus) { + w = Swig_cppconstructor_wrapper(fclassname, l, ccode); + } else { + w = Swig_cconstructor_wrapper(fclassname, l, ccode); + } + if (!mode) { - Printv(wrap, "#define ", iname, "(", 0); - strcpy(cname,iname); - if (ObjCClass) { - Printv(fcall, "(", DataType_lstr(type,0), ") [", classname, " ", mname,0); - } else if (CPlusPlus) { - Printv(fcall, "new ", classname, "(", 0); + if (CPlusPlus) { + emit_set_action(Swig_cppconstructor_call(fclassname, l)); } else { - Printv(fcall, "(", DataType_lstr(type,0), ") calloc(1,sizeof(", classtype, classname, "))", 0); + emit_set_action(Swig_cconstructor_call(fclassname)); } - - // Walk down the parameter list and spit out arguments - - i = 0; - p = ParmList_first(l); - while (p != 0) { - DataType *pt = Parm_Gettype(p); - /* if (ObjCClass) Printf(fcall," %s", p->objc_separator); */ - if ((pt->type != T_VOID) || (pt->is_pointer)) { - Printf(wrap,"_swigarg%d",i); - - // Emit an argument in the function call if in C++ mode - - if ((CPlusPlus) || (ObjCClass)) { - Printf(fcall,"_swigarg%d",i); - } - } - i++; - p = ParmList_next(l); - if (p) { - Printf(wrap,","); - if ((CPlusPlus) && (!ObjCClass)) - Printf(fcall,","); - } - } - Printf(wrap,") "); - if (ObjCClass) Printf(fcall,"]"); - else if (CPlusPlus) Printf(fcall,")"); - - Printf(wrap,"(%s)", fcall); - fprintf(f_wrappers,"%s\n",Char(wrap)); } else { if (ccode) { - Printv(wrap, "static ", classtype, classname, " *", cname, "(", 0); - - // Walk down the parameter list and spit out arguments - - p = ParmList_first(l); - while (p != 0) { - DataType *pt = Parm_Gettype(p); - - if ((pt->type != T_VOID) || (pt->is_pointer)) { - Printf(wrap, DataType_str(pt,Parm_Getname(p))); - p = ParmList_next(l); - if (p) { - Printf(wrap,","); - } - } else { - p = ParmList_next(l); - } - } - Printv(wrap,") ", ccode, "\n", 0); - fprintf(f_wrappers,"%s\n",Char(wrap)); - } + Wrapper_print(w,f_wrappers); + } } - - // If we had any C++ references, get rid of them now - - if (!mode) { - p = ParmList_first(l); - while (p) { - // p->t->is_reference = 0; - p = ParmList_next(l); - } - } - - // We've now created a C wrapper. We're going to add it to the interpreter - - lang->create_function(cname, iname, type, l); - DelDataType(type); - Delete(wrap); - Delete(fcall); + lang->create_function(Wrapper_Getname(w), iname, Wrapper_Gettype(w), Wrapper_Getparms(w)); + DelWrapper(w); } // ----------------------------------------------------------------------------- @@ -2103,26 +1808,17 @@ void cplus_emit_constructor(char *classname, char *classtype, char *classrename, void cplus_emit_variable_get(char *classname, char *classtype, char *classrename, char *mname, char *mrename, DataType *type, int mode) { - Parm *p; - ParmList *l; - DOHString *wrap; - char cname[512],iname[512]; + char cname[512],iname[512], fclassname[512]; char key[512]; - char *prefix; - char *tm; - char source[512]; - char *temp_mname; char *prev_wrap = 0; - - wrap = NewString(""); + Wrapper *w; // First generate a proper name for the get function // Get the base class of this member - if (!mrename) temp_mname = mname; - else temp_mname = mrename; + if (!mrename) mrename = mname; - char *bc = cplus_base_class(temp_mname); + char *bc = cplus_base_class(mrename); if (!bc) bc = classname; if (strlen(bc) == 0) bc = classname; @@ -2132,110 +1828,35 @@ void cplus_emit_variable_get(char *classname, char *classtype, char *classrename strcpy(cname, Swig_name_get(Swig_name_member(bc,mname))); // Generate the scripting name of this function - if (classrename) - prefix = classrename; - else - prefix = classname; - - if (mrename) - strcpy(iname, Swig_name_get(Swig_name_member(prefix,mrename))); - else - strcpy(iname, Swig_name_get(Swig_name_member(prefix,mname))); + if (!classrename) classrename = classname; + strcpy(iname, Swig_name_get(Swig_name_member(classrename,mrename))); // Now check to see if we have already wrapped a variable like this. strcpy(key,cname); - char *temp = copy_string(iname); if (!member_hash) member_hash = NewHash(); if (Getattr(member_hash,key)) { - delete [] temp; prev_wrap = GetChar(member_hash,key); } else { - Setattr(member_hash,key,temp); - delete [] temp; + Setattr(member_hash,key,iname); } + sprintf(fclassname,"%s%s", classtype, classname); + w = Swig_cmemberget_wrapper(fclassname,mname,type,ccode); + // Only generate code if already existing wrapper doesn't exist if (!prev_wrap) { - if (!mode) { - // Get any sort of typemap that might exist - - sprintf(source,"obj->%s",mname); - - // Now write a function to get the value of the variable - - tm = typemap_lookup((char*)"memberout",typemap_lang,type,mname,source,(char*)"result"); - - if ((type->type == T_USER) && (!type->is_pointer)) { - type->is_pointer++; - if (tm) { - Printv(wrap, "static ", DataType_str(type,0), " ", cname, "(", - classtype, classname, " *obj) {\n", - tab4, DataType_str(type,0), " result;\n", - tm, "\n", - tab4, "return result;\n", - "}\n", - 0); - } else { - Printv(wrap, "#define ", cname, "(_swigobj) ", - "(&_swigobj->", mname, ")\n", - 0); - } - type->is_pointer--; - } else { - if (tm) { - Printv(wrap, "static ", DataType_str(type,0), " ", cname, "(", - classtype, classname, " *obj) {\n", - tab4, DataType_str(type,0), " result;\n", - tm, "\n", - tab4, "return result;\n", - "}\n", - 0); - } else { - Printv(wrap, "#define ", cname, "(_swigobj) (",0); - if (!type->is_reference) Printf(wrap,"(%s)", DataType_lstr(type,0)); - else - Printf(wrap,"&"); - Printv(wrap, " _swigobj->", mname, ")\n",0); - } - } - fprintf(f_wrappers,"%s",Char(wrap)); + if ((mode) && (ccode)) { + Wrapper_print(w,f_wrappers); + } else if (!mode) { + emit_set_action(Swig_cmemberget_call(mname, type)); } - - // Wrap this function - - l = NewParmList(); - p = NewParm(0,0); - { - DataType *pt = NewDataType(0); - pt->type = T_USER; - pt->is_pointer = 1; - pt->id = cpp_id; - sprintf(pt->name,"%s%s", classtype,classname); - Parm_Settype(p,pt); - DelDataType(pt); - } - Parm_Setname(p,(char*)"self"); - - ParmList_insert(l,p,0); - - if ((type->type == T_USER) && (!type->is_pointer)) { - type->is_pointer++; - lang->create_function(cname,iname, type, l); - type->is_pointer--; - } else { - int is_ref = type->is_reference; - type->is_reference = 0; - lang->create_function(cname,iname, type, l); - type->is_reference = is_ref; - } - DelParmList(l); + lang->create_function(Wrapper_Getname(w),iname, Wrapper_Gettype(w), Wrapper_Getparms(w)); } else { // Already wrapped this function. Just patch it up lang->create_command(prev_wrap,iname); } - Delete(wrap); - + DelWrapper(w); } // ----------------------------------------------------------------------------- @@ -2290,27 +1911,15 @@ void cplus_emit_variable_get(char *classname, char *classtype, char *classrename void cplus_emit_variable_set(char *classname, char *classtype, char *classrename, char *mname, char *mrename, DataType *type, int mode) { - Parm *p; - ParmList *l; - DOHString *wrap; - int is_user = 0; - char *tm; - char target[512]; char cname[512], iname[512]; char key[512]; - char *temp_mname; - char *prefix; + char fclassname[512]; char *prev_wrap = 0; - - wrap = NewString(""); - - // First generate a proper name for the get function - + Wrapper *w; // Get the base class of this member - if (!mrename) temp_mname = mname; - else temp_mname = mrename; + if (!mrename) mrename = mname; - char *bc = cplus_base_class(temp_mname); + char *bc = cplus_base_class(mrename); if (!bc) bc = classname; if (strlen(bc) == 0) bc = classname; @@ -2319,158 +1928,35 @@ void cplus_emit_variable_set(char *classname, char *classtype, char *classrename strcpy(cname, Swig_name_set(Swig_name_member(bc,mname))); - // Generate the scripting name of this function - if (classrename) - prefix = classrename; - else - prefix = classname; - - if (mrename) - strcpy(iname, Swig_name_set(Swig_name_member(prefix,mrename))); - else - strcpy(iname, Swig_name_set(Swig_name_member(prefix,mname))); + if (!classrename) classrename = classname; + strcpy(iname, Swig_name_set(Swig_name_member(classrename, mrename))); // Now check to see if we have already wrapped a variable like this. strcpy(key,cname); - char *temp = copy_string(iname); if (!member_hash) member_hash = NewHash(); if (Getattr(member_hash,key)) { - delete [] temp; prev_wrap = GetChar(member_hash,key); } else { - Setattr(member_hash,key,temp); - delete [] temp; + Setattr(member_hash,key,iname); } + sprintf(fclassname,"%s%s",classtype,classname); + w = Swig_cmemberset_wrapper(fclassname,mname,type,ccode); + // Only generate code if already existing wrapper doesn't exist if (!prev_wrap) { - if (!mode) { - - sprintf(target,"obj->%s",mname); - - // Lookup any typemaps that might exist - tm = typemap_lookup((char*)"memberin",typemap_lang,type,mname,(char*)"val",target); - - // First write a function to set the variable - - if (tm) { - if ((type->type == T_USER) && (!type->is_pointer)) { - type->is_pointer++; - is_user = 1; - } - Printv(wrap, - "static ", DataType_str(type,0), " ", cname, "(", - classtype, classname, " *obj, ", DataType_str(type,(char*)"val"), ") {\n", - 0); - if (is_user) { - type->is_pointer--; - } - Printv(wrap,tm,"\n",0); - // Return the member - if (is_user) type->is_pointer++; - Printv(wrap, tab4, "return val;\n",0); - if (is_user) type->is_pointer--; - Printf(wrap,"}\n"); - } else { - if ((type->type != T_VOID) || (type->is_pointer)){ - if (!type->is_pointer) { - - Printv(wrap, "#define ", cname, "(_swigobj,_swigval) (",0); - // Have a real value here (ie. not a pointer). - // If it's a user defined type, we'll do something special. - // Otherwise, just assign it. - - if (type->type != T_USER) { - Printv(wrap,"_swigobj->",mname," = _swigval",0); - } else { - Printv(wrap,"_swigobj->",mname, " = *(_swigval)",0); - } - Printf(wrap,",_swigval)\n"); - } else { - // Is a pointer type here. If string, we do something - // special. Otherwise. No problem. - if ((type->type == T_CHAR) && (type->is_pointer == 1)) { - char temp[512]; - Printv(wrap, - "static ", DataType_str(type,0), " ", cname, "(", - classtype, classname, " *obj, ", DataType_str(type,(char*)"val"), ") {\n", - 0); - sprintf(temp,"obj->%s",mname); - if (CPlusPlus) { - Printv(wrap, - tab4, "if (", temp, ") delete [] ", temp, ";\n", - tab4, temp, " = new char[strlen(val)+1];\n", - tab4, "strcpy((char *)", temp, ",val);\n", - 0); - } else { - Printv(wrap, - tab4, "if (obj->", mname, ") free(obj->", mname, ");\n", - tab4, "obj->", mname, " = (char *) malloc(strlen(val)+1);\n", - tab4, "strcpy((char *)obj->", mname, ",val);\n", - 0); - } - Printv(wrap,tab4, "return (char *) val;\n","}\n",0); - } else { - // A normal pointer type of some sort - Printv(wrap, "#define ", cname, "(_swigobj,_swigval) (",0); - if (type->is_reference) { - Printv(wrap, "_swigobj->", mname, " = *_swigval, _swigval)\n",0); - } else { - Printv(wrap, "_swigobj->", mname, " = _swigval,_swigval)\n",0); - } - } - } - } - } + if ((mode) && (ccode)) { + Wrapper_print(w,f_wrappers); + } else if (!mode) { + emit_set_action(Swig_cmemberset_call(mname,type)); } - fprintf(f_wrappers,"%s",Char(wrap)); - // Now wrap it. - - l = NewParmList(); - p = NewParm(0,0); - { - DataType *pt = CopyDataType(type); - pt->is_reference = 0; - pt->id = cpp_id; - if ((type->type == T_USER) && (!type->is_pointer)) pt->is_pointer++; - Parm_Settype(p,pt); - DelDataType(pt); - } - if (mrename) - Parm_Setname(p,mrename); - else - Parm_Setname(p,mname); - ParmList_insert(l,p,0); - p = NewParm(0,0); - { - DataType *pt = NewDataType(0); - pt->type = T_USER; - pt->is_pointer = 1; - pt->id = cpp_id; - sprintf(pt->name,"%s%s", classtype,classname); - Parm_Settype(p,pt); - DelDataType(pt); - } - Parm_Setname(p,(char*)"self"); - ParmList_insert(l,p,0); - - if ((type->type == T_USER) && (!type->is_pointer)) { - type->is_pointer++; - lang->create_function(cname,iname, type, l); - type->is_pointer--; - } else { - int is_ref = type->is_reference; - type->is_reference = 0; - lang->create_function(cname,iname, type, l); - type->is_reference = is_ref; - } - DelParmList(l); + lang->create_function(Wrapper_Getname(w),iname, Wrapper_Gettype(w), Wrapper_Getparms(w)); } else { lang->create_command(prev_wrap,iname); } - Delete(wrap); + DelWrapper(w); } // ----------------------------------------------------------------------------- diff --git a/SWIG/Source/SWIG1.1/emit.cxx b/SWIG/Source/SWIG1.1/emit.cxx index 70efc657b..43e74d747 100644 --- a/SWIG/Source/SWIG1.1/emit.cxx +++ b/SWIG/Source/SWIG1.1/emit.cxx @@ -19,97 +19,15 @@ extern "C" { static char cvsroot[] = "$Header$"; -// ----------------------------------------------------------------------------- -// emit_extern_var(char *decl, DataType *t, int extern_type, FILE *f) -// -// Emits an external variables declaration. Extern_type defines the -// type of external declaration. Currently, only C/C++ declarations -// are allowed, but this might be extended to allow Fortran linkage -// someday -// -// Inputs : -// decl = Name of the declaration -// t = Datatype -// extern_type = Numeric code indicating type of extern -// 0 - No "extern" -// 1,2 - Normal extern (C/C++) -// f = FILE handle -// ----------------------------------------------------------------------------- - -void emit_extern_var(char *decl, DataType *t, int extern_type, FILE *f) { - switch(extern_type) { - case 0: - // No extern. Just a forward reference - fprintf(f,"%s;\n", DataType_str(t, decl)); - break; - case 1: case 2: - fprintf(f,"extern %s;\n", DataType_str(t,decl)); - break; - default: - break; - } -} - -// ----------------------------------------------------------------------------- -// emit_extern_func(char *decl, DataType *t, ParmList *L, int extern_type, -// FILE *f) -// -// Emits an external function declaration (similiar to emit_extern_var). -// -// Inputs : -// decl = Name of declaration -// t = Return datatype -// L = parameter list -// extern_type = Type of extern -// 0 - No "extern" -// 1 - extern -// 2 - extern "C" -// 3 - Function declaration (with arg names) -// f = FILE Handle -// ----------------------------------------------------------------------------- - -void emit_extern_func(char *decl, DataType *t, ParmList *L, int extern_type, FILE *f) { - - switch(extern_type) { - case 0: - fprintf(f,"%s", DataType_str(t,0)); - fprintf(f,"%s(", decl); - ParmList_print_types(L,f); - fprintf(f,");\n"); - break; - case 1: - fprintf(f,"extern %s", DataType_str(t,0)); - fprintf(f,"%s(", decl); - ParmList_print_types(L,f); - fprintf(f,");\n"); - break; - case 2: - // A C++ --- > C Extern - fprintf(f,"extern \"C\" %s", DataType_str(t,0)); - fprintf(f,"%s(", decl); - ParmList_print_types(L,f); - fprintf(f,");\n"); - break; - case 3: - fprintf(f,"%s", DataType_str(t,0)); - fprintf(f,"%s(", decl); - ParmList_print_args(L,f); - fprintf(f,")\n"); - break; - default: - break; - } -} - -// ----------------------------------------------------------------------------- -// int emit_args(char *d, DataType *rt, ParmList *l, Wrapper *f) -// -// Creates a list of variable declarations for both the return value -// and function parameters. -// -// The return value is always called result and arguments arg0, arg1, arg2, etc... -// Returns the number of parameters associated with a function. -// ----------------------------------------------------------------------------- +/* ----------------------------------------------------------------------------- + * int emit_args(char *d, DataType *rt, ParmList *l, Wrapper *f) + * + * Creates a list of variable declarations for both the return value + * and function parameters. + * + * The return value is always called result and arguments arg0, arg1, arg2, etc... + * Returns the number of parameters associated with a function. + * ----------------------------------------------------------------------------- */ int emit_args(DataType *rt, ParmList *l, Wrapper *f) { @@ -119,10 +37,9 @@ int emit_args(DataType *rt, ParmList *l, Wrapper *f) { DataType *pt; char *pvalue; char *pname; - char *local; char *lname; - // Emit function arguments + /* Emit function arguments */ Swig_cargs(f, l); i = 0; @@ -145,7 +62,7 @@ int emit_args(DataType *rt, ParmList *l, Wrapper *f) { tm = typemap_lookup((char*)"ignore",typemap_lang,pt,pname,(char*)"",lname,f); if (tm) { Printv(f->code,tm,"\n",0); - p->ignore = 1; + Parm_Setignore(p,1); } i++; p = ParmList_next(l); @@ -153,23 +70,29 @@ int emit_args(DataType *rt, ParmList *l, Wrapper *f) { return(i); } -// ----------------------------------------------------------------------------- -// int emit_func_call(char *decl, DataType *t, ParmList *l, Wrapper*f) -// -// Emits code for a function call (new version). -// -// Exception handling support : -// -// - This function checks to see if any sort of exception mechanism -// has been defined. If so, we emit the function call in an exception -// handling block. -// ----------------------------------------------------------------------------- +/* ----------------------------------------------------------------------------- + * int emit_func_call(char *decl, DataType *t, ParmList *l, Wrapper*f) + * + * Emits code for a function call (new version). + * + * Exception handling support : + * + * - This function checks to see if any sort of exception mechanism + * has been defined. If so, we emit the function call in an exception + * handling block. + * ----------------------------------------------------------------------------- */ + +static DOH *fcall = 0; + +void emit_set_action(DOHString_or_char *decl) { + if (fcall) Delete (fcall); + fcall = NewString(decl); +} void emit_func_call(char *decl, DataType *t, ParmList *l, Wrapper *f) { char *tm; if ((tm = typemap_lookup((char*)"except",typemap_lang,t,decl,(char*)"result",(char*)""))) { - // Found a type-specific mapping Printv(f->code,tm,0); Replace(f->code,"$name",decl,DOH_REPLACE_ANY); } else if ((tm = fragment_lookup((char*)"except",typemap_lang, t->id))) { @@ -178,117 +101,88 @@ void emit_func_call(char *decl, DataType *t, ParmList *l, Wrapper *f) { } else { Printv(f->code,"$function",0); } + + if (!fcall) fcall = NewString(Swig_cfunction_call(decl,l)); + if (CPlusPlus) { - Swig_cppresult(f, t, "result", Swig_cfunction(decl,l)); + Swig_cppresult(f, t, "result", Char(fcall)); } else { - Swig_cresult(f, t, "result", Swig_cfunction(decl,l)); + Swig_cresult(f, t, "result", Char(fcall)); } + Delete(fcall); + fcall = 0; } -// ----------------------------------------------------------------------------- -// void emit_set_get(char *name, char *iname, DataType *type) -// -// Emits a pair of functions to set/get the value of a variable. -// This should be used as backup in case the target language can't -// provide variable linking. -// -// double foo; -// -// Gets translated into the following : -// -// double foo_set(double x) { -// return foo = x; -// } -// -// double foo_get() { -// return foo; -// } -// -// Need to handle special cases for char * and for user -// defined types. -// -// 1. char * -// -// Will free previous contents (if any) and allocate -// new storage. Could be risky, but it's a reasonably -// natural thing to do. -// -// 2. User_Defined -// Will assign value from a pointer. -// Will return a pointer to current value. -// ----------------------------------------------------------------------------- +/* ----------------------------------------------------------------------------- + * void emit_set_get(char *name, char *iname, DataType *type) + * + * Emits a pair of functions to set/get the value of a variable. This is + * only used in the event the target language can't provide variable linking + * on its own. + * + * double foo; + * + * Gets translated into the following : + * + * double foo_set(double x) { + * return foo = x; + * } + * + * double foo_get() { + * return foo; + * } + * ----------------------------------------------------------------------------- */ + +/* How to assign a C allocated string */ + +static char *c_str = (char *)" {\n\ +if ($target) free($target);\n\ +$target = ($rtype) malloc(strlen($source)+1);\n\ +strcpy((char *)$target,$source);\n\ +return $ltype $target;\n\ +}\n"; + +/* How to assign a C allocated string */ + +static char *cpp_str = (char *)" {\n\ +if ($target) delete [] $target;\n\ +$target = ($rtype) (new char[strlen($source)+1]);\n\ +strcpy((char *)$target,$source);\n\ +return ($ltype) $target;\n\ +}\n"; void emit_set_get(char *name, char *iname, DataType *t) { - Parm *p; - ParmList *l; - char *new_name = 0, *new_iname = 0; + Wrapper *w; + char new_iname[256]; + char *code = 0; - DataType *lt; + /* First write a function to set the variable of the variable */ + if (!(Status & STAT_READONLY)) { - // Figure out the assignable local type for this variable - - lt = Swig_clocal_type(t); - - // First write a function to set the variable of the variable - if (!(Status & STAT_READONLY)) { - fprintf(f_header,"static %s %s(%s) {\n", - DataType_lstr(lt,0), Swig_name_set(name), DataType_lstr(lt,"val")); - if (!t->is_pointer) { - fprintf(f_header,"\t %s = %s;\n", name, Swig_clocal_deref(t,"val")); - fprintf(f_header,"\t return %s;\n", Swig_clocal_assign(t,name)); - } else { - // Is a pointer type here. If string, we do something - // special. Otherwise. No problem. - if ((t->type == T_CHAR) && (t->is_pointer == 1)) { - if (CPlusPlus) { - fprintf(f_header,"\t if (%s) delete %s;\n", name,name); - fprintf(f_header,"\t %s = new char[strlen(val)+1];\n",name); - fprintf(f_header,"\t strcpy(%s,val);\n", name); - fprintf(f_header,"\t return %s;\n", Swig_clocal_assign(t,name)); - } else { - fprintf(f_header,"\t if (%s) free(%s);\n", name,name); - fprintf(f_header,"\t %s = (char *) malloc(strlen(val)+1);\n",name); - fprintf(f_header,"\t strcpy(%s,val);\n", name); - fprintf(f_header,"\t return %s;\n", Swig_clocal_assign(t,name)); - } - } else { - fprintf(f_header,"\t %s = %s;\n", name, Swig_clocal_deref(t,"val")); - fprintf(f_header,"\t return %s;\n", Swig_clocal_assign(t,name)); - } - } - fprintf(f_header,"}\n"); - - // Now wrap it. - l = NewParmList(); - p = NewParm(lt,0); - Parm_Setname(p,(char*)""); - ParmList_append(l,p); - - new_name = copy_string(Swig_name_set(name)); - new_iname = copy_string(Swig_name_set(iname)); - lang->create_function(new_name, new_iname, lt, l); - DelParmList(l); - DelParm(p); + if ((t->type == T_CHAR) && (t->is_pointer == 1)) { + if (CPlusPlus) + code = cpp_str; + else + code = c_str; + } + w = Swig_cvarset_wrapper(name, t, code); + Wrapper_print(w,f_header); + strcpy(new_iname, Swig_name_set(iname)); + lang->create_function(Wrapper_Getname(w), new_iname, Wrapper_Gettype(w), Wrapper_Getparms(w)); + DelWrapper(w); + } - // Now write a function to get the value of the variable - fprintf(f_header,"static %s %s() { \n", - DataType_lstr(lt,0), Swig_name_get(name)); - fprintf(f_header,"\t return %s;\n", Swig_clocal_assign(t,name)); - fprintf(f_header,"}\n"); - - // Wrap this function - - l = NewParmList(); - if (new_name) delete [] new_name; - if (new_iname) delete [] new_iname; - - new_name = copy_string(Swig_name_get(name)); - new_iname = copy_string(Swig_name_get(iname)); - lang->create_function(new_name, new_iname, lt, l); - DelParmList(l); - DelDataType(lt); - delete [] new_name; - delete [] new_iname; + w = Swig_cvarget_wrapper(name,t,0); + Wrapper_print(w,f_header); + strcpy(new_iname, Swig_name_get(iname)); + lang->create_function(Wrapper_Getname(w), new_iname, Wrapper_Gettype(w), Wrapper_Getparms(w)); + DelWrapper(w); } + + + + + + diff --git a/SWIG/Source/SWIG1.1/internal.h b/SWIG/Source/SWIG1.1/internal.h index 1106b5ed6..f3babbc54 100644 --- a/SWIG/Source/SWIG1.1/internal.h +++ b/SWIG/Source/SWIG1.1/internal.h @@ -38,3 +38,4 @@ struct TMParm { /* Number of initialization names that can be used */ #define NI_NAMES 512 + diff --git a/SWIG/Source/SWIG1.1/lang.cxx b/SWIG/Source/SWIG1.1/lang.cxx index 2b915c038..3d77ec755 100644 --- a/SWIG/Source/SWIG1.1/lang.cxx +++ b/SWIG/Source/SWIG1.1/lang.cxx @@ -81,22 +81,22 @@ void Language::cpp_open_class(char *classname, char *classrename, char *ctype, i // Copy the class name - if (ClassName) delete ClassName; - ClassName = copy_string(classname); + if (ClassName) free(ClassName); + ClassName = Swig_copy_string(classname); // Copy the class renaming - if (ClassRename) delete ClassRename; + if (ClassRename) free(ClassRename); if (classrename) { - ClassRename = copy_string(classrename); + ClassRename = Swig_copy_string(classrename); } else { ClassRename = 0; // No renaming } // Make the class type - if (ClassType) delete ClassType; - ClassType = new char[strlen(ctype)+2]; + if (ClassType) free(ClassType); + ClassType = (char *) malloc(strlen(ctype)+2); if (strip) ClassType[0] = 0; else sprintf(ClassType,"%s ",ctype); } @@ -442,7 +442,7 @@ void Language::cpp_declare_const(char *name, char *iname, DataType *type, char * // Declare a constant if (!value) { - new_value = new char[strlen(ClassName)+strlen(name)+3]; + new_value = (char *) malloc(strlen(ClassName)+strlen(name)+3); sprintf(new_value,"%s::%s",ClassName,name); } else { new_value = value; @@ -451,7 +451,7 @@ void Language::cpp_declare_const(char *name, char *iname, DataType *type, char * lang->declare_const(cname,cname,type, new_value); if (!value) { - delete new_value; + free(new_value); } } diff --git a/SWIG/Source/SWIG1.1/main.cxx b/SWIG/Source/SWIG1.1/main.cxx index 08badfecf..5fc2afc3e 100644 --- a/SWIG/Source/SWIG1.1/main.cxx +++ b/SWIG/Source/SWIG1.1/main.cxx @@ -143,7 +143,6 @@ int SWIG_main(int argc, char *argv[], Language *l) { int checkout = 0; int cpp_only = 0; - char *typemap_file = 0; char *includefiles[256]; int includecount = 0; extern int check_suffix(char *); @@ -189,7 +188,7 @@ int SWIG_main(int argc, char *argv[], Language *l) { strcpy(LibDir,c); } - SwigLib = copy_string(LibDir); // Make a copy of the real library location + SwigLib = Swig_copy_string(LibDir); // Make a copy of the real library location sprintf(temp,"%s/config", LibDir); Swig_add_directory((DOH *) temp); @@ -205,7 +204,7 @@ int SWIG_main(int argc, char *argv[], Language *l) { if (argv[i]) { if (strncmp(argv[i],"-I",2) == 0) { // Add a new directory search path - includefiles[includecount++] = copy_string(argv[i]+2); + includefiles[includecount++] = Swig_copy_string(argv[i]+2); Swig_mark_arg(i); } else if (strncmp(argv[i],"-D",2) == 0) { DOH *d = NewString(argv[i]+2); @@ -238,7 +237,7 @@ int SWIG_main(int argc, char *argv[], Language *l) { } else if (strcmp(argv[i],"-o") == 0) { Swig_mark_arg(i); if (argv[i+1]) { - outfile_name = copy_string(argv[i+1]); + outfile_name = Swig_copy_string(argv[i+1]); Swig_mark_arg(i+1); i++; } else { @@ -564,9 +563,9 @@ void swig_pragma(char *name, char *value) { GenerateDefault = 0; } if (strcmp(name,"objc_new") == 0) { - objc_construct = copy_string(value); + objc_construct = Swig_copy_string(value); } if (strcmp(name,"objc_delete") == 0) { - objc_destruct = copy_string(value); + objc_destruct = Swig_copy_string(value); } } diff --git a/SWIG/Source/SWIG1.1/parser.yxx b/SWIG/Source/SWIG1.1/parser.yxx index 55d970211..63b04e9ed 100644 --- a/SWIG/Source/SWIG1.1/parser.yxx +++ b/SWIG/Source/SWIG1.1/parser.yxx @@ -33,7 +33,6 @@ extern void scanner_check_typedef(void); extern void scanner_ignore_typedef(void); extern void scanner_clear_start(void); extern void start_inline(char *, int); -extern void format_string(char *); extern void swig_pragma(char *, char *); #include "internal.h" @@ -85,7 +84,7 @@ static DOHHash *symbols = 0; /* Some macros for building constants */ #define E_BINARY(TARGET, SRC1, SRC2, OP) \ - TARGET = new char[strlen(SRC1) + strlen(SRC2) +strlen(OP)+1];\ + TARGET = (char *) malloc(strlen(SRC1) + strlen(SRC2) +strlen(OP)+1);\ sprintf(TARGET,"%s%s%s",SRC1,OP,SRC2); /* C++ modes */ @@ -122,6 +121,112 @@ void parser_init() { temp_type = NewDataType(0); } +static TMParm *NewTMParm() { + TMParm *tmp = (TMParm *) malloc(sizeof(TMParm)); + tmp->next = 0; + return tmp; +} + +//----------------------------------------------------------------------- +// void format_string(char *str) +// +// Replace all of the escape sequences in the string str. It is +// assumed that the new string is smaller than the original! +//----------------------------------------------------------------------- + +static void format_string(char *str) { + char *newstr, *c,*c1; + int state; + if (!str) return; + newstr = Swig_copy_string(str); + c = newstr; + c1 = str; + state = 0; + while (*c) { + switch(state) { + case 0: + if (*c == '\\') + state = 1; + else { + *(c1++) = *c; + state = 0; + } + break; + case 1: + // We're in a simple escape sequence figure out what to do + switch(*c) { + case 'n': + *(c1++) = '\n'; + break; + case 'f': + *(c1++) = '\f'; + break; + case 'r': + *(c1++) = '\r'; + break; + case 't': + *(c1++) = '\t'; + break; + case '\\': + *(c1++) = '\\'; + break; + case '\"': + *(c1++) = '\"'; + break; + case '\'': + *(c1++) = '\''; + break; + default: + *(c1++) = '\\'; + *(c1++) = *c; + } + state = 0; + break; + default: + *(c1++) = *c; + state = 0; + } + c++; + } + *c1 = 0; + free(newstr); +} + +// Emit an external function declaration + +static void emit_extern_func(char *decl, DataType *t, ParmList *L, int extern_type, FILE *f) { + + switch(extern_type) { + case 0: + fprintf(f,"%s", DataType_str(t,0)); + fprintf(f,"%s(", decl); + ParmList_print_types(L,f); + fprintf(f,");\n"); + break; + case 1: + fprintf(f,"extern %s", DataType_str(t,0)); + fprintf(f,"%s(", decl); + ParmList_print_types(L,f); + fprintf(f,");\n"); + break; + case 2: + /* A C++ --- > C Extern */ + fprintf(f,"extern \"C\" %s", DataType_str(t,0)); + fprintf(f,"%s(", decl); + ParmList_print_types(L,f); + fprintf(f,");\n"); + break; + case 3: + fprintf(f,"%s", DataType_str(t,0)); + fprintf(f,"%s(", decl); + ParmList_print_args(L,f); + fprintf(f,")\n"); + break; + default: + break; + } +} + // ---------------------------------------------------------------------- // static init_language() // @@ -257,10 +362,8 @@ void create_variable(int ext, char *name, DataType *t) { // If externed, output an external declaration - if (ext) - emit_extern_var(name, t, ext, f_header); - else if (ForceExtern) { - emit_extern_var(name, t, 1, f_header); + if (ext || ForceExtern) { + Printf(f_header,"extern %s;\n", DataType_str(t,name)); } // If variable datatype is read-only, we'll force it to be readonly @@ -294,7 +397,7 @@ void create_constant(char *name, DataType *type, char *value) { if ((type->type == T_CHAR) && (!type->is_pointer)) type->is_pointer++; - if (!value) value = copy_string(name); + if (!value) value = Swig_copy_string(name); sprintf(temp_name,"const:%s", name); if (add_symbol(temp_name)) { fprintf(stderr,"%s : Line %d. Constant %s multiply defined. (2nd definition ignored)\n", @@ -368,7 +471,7 @@ static void dump_nested(char *parent) { n1 = n->next; Delete(n->code); - delete n; + free(n); n = n1; } nested_list = 0; @@ -489,9 +592,9 @@ command : command statement { ; statement : INCLUDE STRING LBRACE { - $1.filename = copy_string(input_file); + $1.filename = Swig_copy_string(input_file); $1.line = line_number; - input_file = copy_string($2); + input_file = Swig_copy_string($2); line_number = 0; } command RBRACE { input_file = $1.filename; @@ -503,9 +606,9 @@ statement : INCLUDE STRING LBRACE { | WEXTERN STRING LBRACE { $1.flag = WrapExtern; WrapExtern = 1; - $1.filename = copy_string(input_file); + $1.filename = Swig_copy_string(input_file); $1.line = line_number; - input_file = copy_string($2); + input_file = Swig_copy_string($2); line_number = 0; } command RBRACE { input_file = $1.filename; @@ -518,9 +621,9 @@ statement : INCLUDE STRING LBRACE { | IMPORT STRING LBRACE { $1.flag = WrapExtern; WrapExtern = 1; - $1.filename = copy_string(input_file); + $1.filename = Swig_copy_string(input_file); $1.line = line_number; - input_file = copy_string($2); + input_file = Swig_copy_string($2); line_number = 0; lang->import($2); } command RBRACE { @@ -530,9 +633,9 @@ statement : INCLUDE STRING LBRACE { } | SWIGMACRO ID COMMA STRING COMMA NUM_INT LBRACE { - $1.filename = copy_string(input_file); + $1.filename = Swig_copy_string(input_file); $1.line = line_number; - input_file = copy_string($4); + input_file = Swig_copy_string($4); line_number = atoi($6) - 1; } command RBRACE { input_file = $1.filename; @@ -562,13 +665,15 @@ statement : INCLUDE STRING LBRACE { FatalError(); } else { if (DataType_qualifier($2)) { - if ((strcmp(DataType_qualifier($2),"const") == 0)) { - if ($5.type != T_ERROR) + if (!($2->is_pointer) && (strcmp(DataType_qualifier($2),"const") == 0)) { + if ($5.type != T_ERROR) create_constant($3.id, $2, $5.id); - } else + } else { create_variable($1,$3.id,$2); - } else + } + } else { create_variable($1,$3.id,$2); + } } DelDataType($2); } stail { } @@ -865,8 +970,8 @@ statement : INCLUDE STRING LBRACE { input_file,line_number); } for (i = 0; i < $3.count; i++) - if ($3.names[i]) delete [] $3.names[i]; - delete [] $3.names; + if ($3.names[i]) free($3.names[i]); + free($3.names); } /* Module directive */ @@ -878,8 +983,8 @@ statement : INCLUDE STRING LBRACE { module_init = 1; init_language(); for (i = 0; i < $3.count; i++) - if ($3.names[i]) delete [] $3.names[i]; - delete [] $3.names; + if ($3.names[i]) free($3.names[i]); + free($3.names); } /* constant directive */ @@ -942,8 +1047,8 @@ statement : INCLUDE STRING LBRACE { typemap_register($5,$3,Parm_Gettype(p->p),Parm_Getname(p->p),Char(CCode),p->args); p = p->next; } - delete $3; - delete $5; + free($3); + free($5); } /* Create a new typemap in current language */ @@ -961,7 +1066,7 @@ statement : INCLUDE STRING LBRACE { p = p->next; } } - delete $3; + free($3); } /* Clear a typemap */ @@ -973,8 +1078,8 @@ statement : INCLUDE STRING LBRACE { typemap_clear($5,$3,Parm_Gettype(p->p),Parm_Getname(p->p)); p = p->next; } - delete $3; - delete $5; + free($3); + free($5); } /* Clear a typemap in current language */ @@ -991,7 +1096,7 @@ statement : INCLUDE STRING LBRACE { p = p->next; } } - delete $3; + free($3); } /* Copy a typemap */ @@ -1003,10 +1108,10 @@ statement : INCLUDE STRING LBRACE { typemap_copy($5,$3,Parm_Gettype($9->p),Parm_Getname($9->p),Parm_Gettype(p->p),Parm_Getname(p->p)); p = p->next; } - delete $3; - delete $5; + free($3); + free($5); DelParm($9->p); - delete $9; + free($9); } /* Copy typemap in current language */ @@ -1024,9 +1129,9 @@ statement : INCLUDE STRING LBRACE { p = p->next; } } - delete $3; + free($3); DelParm($7->p); - delete $7; + free($7); } /* ----------------------------------------------------------------- apply and clear support (for typemaps) @@ -1039,9 +1144,9 @@ statement : INCLUDE STRING LBRACE { typemap_apply(Parm_Gettype($2->p),Parm_Getname($2->p),Parm_Gettype(p->p),Parm_Getname(p->p)); p = p->next; } - delete $4; + free($4); DelParmList($2->args); - delete $2; + free($2); } | CLEAR tm_list SEMI { TMParm *p; @@ -1063,7 +1168,7 @@ statement : INCLUDE STRING LBRACE { | EXCEPT LPAREN ID RPAREN LBRACE { skip_brace(); fragment_register((char *)"except",$3, Char(CCode)); - delete $3; + free($3); } /* A Generic Exception (no language specified */ @@ -1169,7 +1274,7 @@ typedef_decl : TYPEDEF type declaration { lang->add_typedef($2,$5); cplus_register_type($5); DelDataType($2); - delete $5; + free($5); DelParmList($8); } @@ -1192,7 +1297,7 @@ typedef_decl : TYPEDEF type declaration { lang->add_typedef($2,$6); cplus_register_type($6); DelDataType($2); - delete $6; + free($6); DelParmList($9); } @@ -1353,7 +1458,7 @@ ptail : COMMA parm ptail { parm : parm_type { $$ = $1; if (typemap_check((char *)"ignore",typemap_lang,Parm_Gettype($$),Parm_Getname($$))) - $$->ignore = 1; + Parm_Setignore($$,1); } parm_type : type pname { @@ -1365,7 +1470,7 @@ parm_type : type pname { $$ = NewParm($1,$2); Parm_Setvalue($$,DefArg); DelDataType($1); - delete $2; + free($2); } | type stars pname { @@ -1378,7 +1483,7 @@ parm_type : type pname { DataType_set_arraystr(Parm_Gettype($$), Char(ArrayString)); } DelDataType($1); - delete $3; + free($3); } | type AND pname { @@ -1391,7 +1496,7 @@ parm_type : type pname { fprintf(stderr,"%s : Line %d. Warning. Use of C++ Reference detected. Use the -c++ option.\n", input_file, line_number); } DelDataType($1); - delete $3; + free($3); } | type LPAREN stars pname RPAREN LPAREN parms RPAREN { fprintf(stderr,"%s : Line %d. Error. Function pointer not allowed (remap with typedef).\n", input_file, line_number); @@ -1402,7 +1507,7 @@ parm_type : type pname { Parm_Setname($$,$4); strcpy(pt->name,""); DelDataType($1); - delete $4; + free($4); DelParmList($7); } | PERIOD PERIOD PERIOD { @@ -1423,7 +1528,7 @@ pname : ID def_args { DefArg = Swig_copy_string(ConstChar); else DefArg = Swig_copy_string($2.id); - if ($2.id) delete $2.id; + if ($2.id) free($2.id); } | ID array { $$ = $1; @@ -1431,12 +1536,12 @@ pname : ID def_args { DefArg = 0; } | array { - $$ = new char[1]; + $$ = (char *) malloc(1); $$[0] = 0; InArray = $1; DefArg = 0; } - | empty { $$ = new char[1]; + | empty { $$ = (char *) malloc(1); $$[0] = 0; InArray = 0; DefArg = 0; @@ -1445,7 +1550,7 @@ pname : ID def_args { def_args : EQUAL definetype { $$ = $2; } | EQUAL AND ID { - $$.id = new char[strlen($3)+2]; + $$.id = (char *) malloc(strlen($3)+2); $$.id[0] = '&'; strcpy(&$$.id[1], $3); $$.type = T_USER; @@ -1715,21 +1820,21 @@ opt_int : TYPE_INT { } definetype : { scanner_check_typedef(); } expr { $$ = $2; scanner_ignore_typedef(); - if (ConstChar) delete ConstChar; + if (ConstChar) free(ConstChar); ConstChar = 0; } | STRING { $$.id = $1; $$.type = T_CHAR; - if (ConstChar) delete ConstChar; - ConstChar = new char[strlen($1)+3]; + if (ConstChar) free(ConstChar); + ConstChar = (char *) malloc(strlen($1)+3); sprintf(ConstChar,"\"%s\"",$1); } | CHARCONST { $$.id = $1; $$.type = T_CHAR; - if (ConstChar) delete ConstChar; - ConstChar = new char[strlen($1)+3]; + if (ConstChar) free(ConstChar); + ConstChar = (char *) malloc(strlen($1)+3); sprintf(ConstChar,"'%s'",$1); } ; @@ -1739,12 +1844,12 @@ definetype : { scanner_check_typedef(); } expr { initlist : initlist COMMA ID { $$ = $1; - $$.names[$$.count] = copy_string($3); + $$.names[$$.count] = Swig_copy_string($3); $$.count++; $$.names[$$.count] = (char *) 0; } | empty { - $$.names = new char *[NI_NAMES]; + $$.names = (char **) malloc(NI_NAMES*sizeof(char*)); $$.count = 0; for (i = 0; i < NI_NAMES; i++) $$.names[i] = (char *) 0; @@ -1828,12 +1933,12 @@ expr : NUM_INT { $$.type = T_ULONG; } | SIZEOF LPAREN type RPAREN { - $$.id = new char[strlen($3->name)+9]; + $$.id = (char *) malloc(strlen($3->name)+9); sprintf($$.id,"sizeof(%s)", $3->name); $$.type = T_INT; } | LPAREN strict_type RPAREN expr %prec UMINUS { - $$.id = new char[strlen($4.id)+strlen($2->name)+3]; + $$.id = (char *) malloc(strlen($4.id)+strlen($2->name)+3); sprintf($$.id,"(%s)%s",$2->name,$4.id); $$.type = $2->type; } @@ -1842,36 +1947,36 @@ expr : NUM_INT { $$.type = T_INT; } | ID DCOLON ID { - $$.id = new char[strlen($1)+strlen($3)+3]; + $$.id = (char *) malloc(strlen($1)+strlen($3)+3); sprintf($$.id,"%s::%s",$1,$3); $$.type = T_INT; - delete $1; - delete $3; + free($1); + free($3); } | expr PLUS expr { E_BINARY($$.id,$1.id,$3.id,"+"); $$.type = promote($1.type,$3.type); - delete $1.id; - delete $3.id; + free($1.id); + free($3.id); } | expr MINUS expr { E_BINARY($$.id,$1.id,$3.id,"-"); $$.type = promote($1.type,$3.type); - delete $1.id; - delete $3.id; + free($1.id); + free($3.id); } | expr STAR expr { E_BINARY($$.id,$1.id,$3.id,"*"); $$.type = promote($1.type,$3.type); - delete $1.id; - delete $3.id; + free($1.id); + free($3.id); } | expr SLASH expr { E_BINARY($$.id,$1.id,$3.id,"/"); $$.type = promote($1.type,$3.type); - delete $1.id; - delete $3.id; + free($1.id); + free($3.id); } | expr AND expr { @@ -1881,8 +1986,8 @@ expr : NUM_INT { fprintf(stderr,"%s : Line %d. Type error in constant expression (expecting integers).\n", input_file, line_number); FatalError(); } - delete $1.id; - delete $3.id; + free($1.id); + free($3.id); } | expr OR expr { @@ -1893,8 +1998,8 @@ expr : NUM_INT { FatalError(); } $$.type = T_INT; - delete $1.id; - delete $3.id; + free($1.id); + free($3.id); } | expr XOR expr { @@ -1905,8 +2010,8 @@ expr : NUM_INT { FatalError(); } $$.type = T_INT; - delete $1.id; - delete $3.id; + free($1.id); + free($3.id); } | expr LSHIFT expr { @@ -1917,8 +2022,8 @@ expr : NUM_INT { FatalError(); } $$.type = T_INT; - delete $1.id; - delete $3.id; + free($1.id); + free($3.id); } | expr RSHIFT expr { @@ -1929,32 +2034,32 @@ expr : NUM_INT { FatalError(); } $$.type = T_INT; - delete $1.id; - delete $3.id; + free($1.id); + free($3.id); } | MINUS expr %prec UMINUS { - $$.id = new char[strlen($2.id)+2]; + $$.id = (char *) malloc(strlen($2.id)+2); sprintf($$.id,"-%s",$2.id); $$.type = $2.type; - delete $2.id; + free($2.id); } | NOT expr { - $$.id = new char[strlen($2.id)+2]; + $$.id = (char *) malloc(strlen($2.id)+2); sprintf($$.id,"~%s",$2.id); if ($2.type == T_DOUBLE) { fprintf(stderr,"%s : Line %d. Type error in constant expression (expecting integers).\n", input_file, line_number); FatalError(); } $$.type = $2.type; - delete $2.id; + free($2.id); } | LPAREN expr RPAREN { - $$.id = new char[strlen($2.id)+3]; + $$.id = (char *) malloc(strlen($2.id)+3); sprintf($$.id,"(%s)", $2.id); $$.type = $2.type; - delete $2.id; + free($2.id); } ; /****************************************************************/ @@ -2007,9 +2112,9 @@ cpp_class : if ($4.names) { int j; for (j = 0; j < $4.count; j++) { - if ($4.names[j]) delete [] $4.names[j]; + if ($4.names[j]) free($4.names[j]); } - delete [] $4.names; + free($4.names); } // Dumped nested declarations (if applicable) @@ -2071,9 +2176,9 @@ cpp_class : if ($4.names) { int j; for (j = 0; j < $4.count; j++) { - if ($4.names[j]) delete [] $4.names[j]; + if ($4.names[j]) free($4.names[j]); } - delete [] $4.names; + free($4.names); } if ($9.is_pointer > 0) { @@ -2558,11 +2663,11 @@ cpp_pragma : PRAGMA ID stylearg { fprintf(stderr,"%s : Line %d. Warning. Nested classes not currently supported (ignored).\n", input_file, line_number); /* Generate some code for a new class */ } else { - Nested *n = new Nested; + Nested *n = (Nested *) malloc(sizeof(Nested)); n->code = NewString(""); Printv(n->code, "typedef ", $1, " ", Char(CCode), " $classname_", $5.id, ";\n", 0); - n->name = copy_string($5.id); + n->name = Swig_copy_string($5.id); n->line = start_line; n->type = NewDataType(0); n->type->type = T_USER; @@ -2584,11 +2689,11 @@ cpp_pragma : PRAGMA ID stylearg { } else { /* Generate some code for a new class */ - Nested *n = new Nested; + Nested *n = (Nested *) malloc(sizeof(Nested)); n->code = NewString(""); Printv(n->code, "typedef ", $1, " " , Char(CCode), " $classname_", $4.id, ";\n",0); - n->name = copy_string($4.id); + n->name = Swig_copy_string($4.id); n->line = start_line; n->type = NewDataType(0); n->type->type = T_USER; @@ -2739,13 +2844,13 @@ inherit : COLON base_list { base_list : base_specifier { int i; - $$.names = new char *[NI_NAMES]; + $$.names = (char **) malloc(NI_NAMES*sizeof(char *)); $$.count = 0; for (i = 0; i < NI_NAMES; i++){ $$.names[i] = (char *) 0; } if ($1) { - $$.names[$$.count] = copy_string($1); + $$.names[$$.count] = Swig_copy_string($1); $$.count++; } } @@ -2753,7 +2858,7 @@ base_list : base_specifier { | base_list COMMA base_specifier { $$ = $1; if ($3) { - $$.names[$$.count] = copy_string($3); + $$.names[$$.count] = Swig_copy_string($3); $$.count++; } } @@ -2864,8 +2969,8 @@ objective_c : OC_INTERFACE ID objc_inherit { cplus_class_close($2); cplus_mode = CPLUS_PUBLIC; ObjCClass = 0; - delete $2; - delete $3; + free($2); + free($3); } /* An obj-c category declaration */ | OC_INTERFACE ID LPAREN ID RPAREN objc_protolist { @@ -2887,10 +2992,10 @@ objective_c : OC_INTERFACE ID objc_inherit { if ($3.names[i]) { iname = make_name($3.names[i]); lang->cpp_class_decl($3.names[i],iname,(char*)""); - delete [] $3.names[i]; + free($3.names[i]); } } - delete [] $3.names; + free($3.names); } ; @@ -3075,7 +3180,7 @@ objc_method : MINUS objc_ret_type ID objc_args objc_end { cplus_member_func($3,iname,$2,$4,0); scanner_clear_start(); DelDataType($2); - delete $3; + free($3); DelParmList($4); } } @@ -3093,7 +3198,7 @@ objc_method : MINUS objc_ret_type ID objc_args objc_end { } scanner_clear_start(); DelDataType($2); - delete $3; + free($3); DelParmList($4); } ; @@ -3140,11 +3245,11 @@ objc_args : objc_args objc_separator objc_arg_type ID { } ; -objc_separator : COLON { $$ = copy_string((char*)":"); } - | ID COLON { $$ = new char[strlen($1)+2]; +objc_separator : COLON { $$ = Swig_copy_string((char*)":"); } + | ID COLON { $$ = (char *) malloc(strlen($1)+2); strcpy($$,$1); strcat($$,":"); - delete $1; + free($1); } ; @@ -3154,8 +3259,8 @@ objc_separator : COLON { $$ = copy_string((char*)":"); } stylelist : ID stylearg styletail { $$ = $3; - $$.names[$$.count] = copy_string($1); - $$.values[$$.count] = copy_string($2); + $$.names[$$.count] = Swig_copy_string($1); + $$.values[$$.count] = Swig_copy_string($2); format_string($$.values[$$.count]); $$.count++; } @@ -3164,14 +3269,14 @@ stylelist : ID stylearg styletail { styletail : styletail COMMA ID stylearg { $$ = $1; - $$.names[$$.count] = copy_string($3); - $$.values[$$.count] = copy_string($4); + $$.names[$$.count] = Swig_copy_string($3); + $$.values[$$.count] = Swig_copy_string($4); format_string($$.values[$$.count]); $$.count++; } | empty { - $$.names = new char *[NI_NAMES]; - $$.values = new char *[NI_NAMES]; + $$.names = (char **) malloc(NI_NAMES*sizeof(char *)); + $$.values = (char **) malloc(NI_NAMES*sizeof(char *)); $$.count = 0; } ; @@ -3196,7 +3301,7 @@ tm_method : ID { $$ = $1; } | CONST { - $$ = copy_string((char*)"const"); + $$ = Swig_copy_string((char*)"const"); } ; @@ -3218,15 +3323,15 @@ typemap_parm : type typemap_name { $1->is_pointer++; DataType_set_arraystr($1,Char(ArrayString)); } - $$ = new TMParm; + $$ = NewTMParm(); $$->p = NewParm($1,$2); $$->args = tm_parm; DelDataType($1); - delete $2; + free($2); } | type stars typemap_name { - $$ = new TMParm; + $$ = NewTMParm(); $$->p = NewParm($1,$3); DataType *pt = Parm_Gettype($$->p); pt->is_pointer += $2; @@ -3236,11 +3341,11 @@ typemap_parm : type typemap_name { } $$->args = tm_parm; DelDataType($1); - delete $3; + free($3); } | type AND typemap_name { - $$ = new TMParm; + $$ = NewTMParm(); $$->p = NewParm($1,$3); DataType *pt = Parm_Gettype($$->p); pt->is_reference = 1; @@ -3250,12 +3355,12 @@ typemap_parm : type typemap_name { } $$->args = tm_parm; DelDataType($1); - delete $3; + free($3); } | type LPAREN stars typemap_name RPAREN LPAREN parms RPAREN { fprintf(stderr,"%s : Line %d. Error. Function pointer not allowed (remap with typedef).\n", input_file, line_number); FatalError(); - $$ = new TMParm; + $$ = NewTMParm(); $$->p = NewParm($1,$4); DataType *pt = Parm_Gettype($$->p); pt->type = T_ERROR; @@ -3263,7 +3368,7 @@ typemap_parm : type typemap_name { strcpy(pt->name,""); $$->args = tm_parm; DelDataType($1); - delete $4; + free($4); DelParmList($7); } ; @@ -3284,14 +3389,14 @@ typemap_name : ID typemap_args { | array { ArrayBackup = Copy(ArrayString); } typemap_args { - $$ = new char[1]; + $$ = (char *) malloc(1); $$[0] = 0; InArray = $1; Clear(ArrayString); Append(ArrayString,ArrayBackup); Delete(ArrayBackup); } - | typemap_args { $$ = new char[1]; + | typemap_args { $$ = (char *) malloc(1); $$[0] = 0; InArray = 0; } diff --git a/SWIG/Source/SWIG1.1/scanner.cxx b/SWIG/Source/SWIG1.1/scanner.cxx index 016cc3fb2..b50366698 100644 --- a/SWIG/Source/SWIG1.1/scanner.cxx +++ b/SWIG/Source/SWIG1.1/scanner.cxx @@ -100,7 +100,7 @@ void scanner_init() { void scanner_file(FILE *f) { InFile *in; - in = new InFile; + in = (InFile *) malloc(sizeof(InFile)); in->f = f; in->in_file = input_file; in->extern_mode = WrapExtern; @@ -134,7 +134,7 @@ void scanner_close() { } else { LEX_in = NULL; } - delete in_head; + free(in_head); in_head = p; } @@ -154,7 +154,7 @@ char nextchar() { if (inline_lex_pos >= inline_lex_len) { // Done with inlined code. Check to see if we have any // new code fragments. If so, switch to them. - delete inline_yybuffer; + free(inline_yybuffer); if (fragments) { CodeFragment *f; inline_yybuffer = fragments->text; @@ -162,7 +162,7 @@ char nextchar() { inline_lex_len = strlen(fragments->text); line_number = fragments->line_number; f = fragments->next; - delete fragments; + free(fragments); fragments = f; c = inline_yybuffer[0]; } else { @@ -260,8 +260,8 @@ void start_inline(char *text, int line) { CodeFragment *f,*f1; // Add a new code fragment to our list - f = new CodeFragment; - f->text = copy_string(text); + f = (CodeFragment *) malloc(sizeof(CodeFragment)); + f->text = Swig_copy_string(text); f->line_number = line; f->next = 0; if (!fragments) fragments = f; @@ -275,7 +275,7 @@ void start_inline(char *text, int line) { // Switch our scanner over to process text from a string. // Save current line number and other information however. - inline_yybuffer = copy_string(text); + inline_yybuffer = Swig_copy_string(text); inline_lex_len = strlen(text); inline_lex_pos = 0; inline_line_number = line_number; // Make copy of old line number @@ -476,7 +476,7 @@ int skip_cond(int inthen) { int start_line; char *file; - file = copy_string(input_file); + file = Swig_copy_string(input_file); start_line = line_number; yylen = 0; @@ -545,7 +545,7 @@ int skip_cond(int inthen) { state = 0; } else { yylen = 0; - delete file; + free(file); return 1; } } else { @@ -555,7 +555,7 @@ int skip_cond(int inthen) { } else if ((strcmp(yytext,"#endif") == 0) || (strcmp(yytext,"%endif") == 0)) { if (level <= 0) { /* Found matching endif. exit */ yylen = 0; - delete file; + free(file); return 0; } else { state = 0; @@ -566,7 +566,7 @@ int skip_cond(int inthen) { if (level <= 0) { // If we come across this, we pop it back onto the input queue and return retract(6); - delete file; + free(file); return 0; } else { yylen = 0; @@ -778,7 +778,7 @@ int yylook(void) { } if (c == '\"') { yytext[yylen-1] = 0; - yylval.id = copy_string(yytext+1); + yylval.id = Swig_copy_string(yytext+1); return(STRING); } else if (c == '\\') { state = 21; /* Possibly an escape sequence. */ @@ -902,7 +902,7 @@ int yylook(void) { else if ((c == 'e') || (c == 'E')) {state = 86;} else if ((c == 'f') || (c == 'F')) { yytext[yylen] = 0; - yylval.id = copy_string(yytext); + yylval.id = Swig_copy_string(yytext); return(NUM_FLOAT); } else if (isdigit(c)) { state = 8;} @@ -913,7 +913,7 @@ int yylook(void) { } else { retract(1); yytext[yylen] = 0; - yylval.id = copy_string(yytext); + yylval.id = Swig_copy_string(yytext); return(NUM_INT); } break; @@ -923,12 +923,12 @@ int yylook(void) { else if ((c == 'e') || (c == 'E')) state = 82; else if ((c == 'f') || (c == 'F') || (c == 'l') || (c == 'L')) { yytext[yylen] = 0; - yylval.id = copy_string(yytext); + yylval.id = Swig_copy_string(yytext); return(NUM_FLOAT); } else { retract(1); yytext[yylen] = 0; - yylval.id = copy_string(yytext); + yylval.id = Swig_copy_string(yytext); return(NUM_FLOAT); } break; @@ -938,7 +938,7 @@ int yylook(void) { else { retract(2); yytext[yylen-1] = 0; - yylval.id = copy_string(yytext); + yylval.id = Swig_copy_string(yytext); return(NUM_INT); } break; @@ -955,7 +955,7 @@ int yylook(void) { } else { retract(1); yytext[yylen] = 0; - yylval.id = copy_string(yytext); + yylval.id = Swig_copy_string(yytext); return(NUM_INT); } break; @@ -970,7 +970,7 @@ int yylook(void) { } else { retract(1); yytext[yylen] = 0; - yylval.id = copy_string(yytext); + yylval.id = Swig_copy_string(yytext); return(NUM_INT); } break; @@ -989,7 +989,7 @@ int yylook(void) { } else { retract(1); yytext[yylen] = 0; - yylval.id = copy_string(yytext); + yylval.id = Swig_copy_string(yytext); return(NUM_INT); } break; @@ -1001,12 +1001,12 @@ int yylook(void) { if (isdigit(c)) state = 86; else if ((c == 'f') || (c == 'F') || (c == 'l') || (c == 'L')) { yytext[yylen] = 0; - yylval.id = copy_string(yytext); + yylval.id = Swig_copy_string(yytext); return(NUM_FLOAT); } else { retract(1); yytext[yylen] = 0; - yylval.id = copy_string(yytext); + yylval.id = Swig_copy_string(yytext); return(NUM_FLOAT); } /* Parse a character constant. ie. 'a' */ @@ -1017,12 +1017,12 @@ int yylook(void) { if ((c = nextchar()) == 0) return (0); if ((c == 'u') || (c == 'U')) { yytext[yylen] = 0; - yylval.id = copy_string(yytext); + yylval.id = Swig_copy_string(yytext); return(NUM_ULONG); } else { retract(1); yytext[yylen] = 0; - yylval.id = copy_string(yytext); + yylval.id = Swig_copy_string(yytext); return(NUM_LONG); } @@ -1031,12 +1031,12 @@ int yylook(void) { if ((c = nextchar()) == 0) return (0); if ((c == 'l') || (c == 'L')) { yytext[yylen] = 0; - yylval.id = copy_string(yytext); + yylval.id = Swig_copy_string(yytext); return(NUM_ULONG); } else { retract(1); yytext[yylen] = 0; - yylval.id = copy_string(yytext); + yylval.id = Swig_copy_string(yytext); return(NUM_UNSIGNED); } @@ -1045,7 +1045,7 @@ int yylook(void) { if (c == '\\') state = 91; else if (c == '\'') { yytext[yylen-1] = 0; - yylval.id = copy_string(yytext+1); + yylval.id = Swig_copy_string(yytext+1); return(CHARCONST); } break; @@ -1171,7 +1171,7 @@ extern "C" int yylex(void) { } if (strcmp(yytext,"signed") == 0) { yylval.type = NewDataType(0); - yylval.type->type = T_SINT; + yylval.type->type = T_INT; strcpy(yylval.type->name,yytext); return(TYPE_SIGNED); } @@ -1308,7 +1308,7 @@ extern "C" int yylex(void) { } } - yylval.id = copy_string(yytext); + yylval.id = Swig_copy_string(yytext); return(ID); default: return(l); diff --git a/SWIG/Source/SWIG1.1/sstring.cxx b/SWIG/Source/SWIG1.1/sstring.cxx deleted file mode 100644 index e0473f0f6..000000000 --- a/SWIG/Source/SWIG1.1/sstring.cxx +++ /dev/null @@ -1,106 +0,0 @@ -/* ----------------------------------------------------------------------------- - * sstring.cxx - * - * SWIG string object. - * - * Note: 6/27/2000 - This module is now just a wrapper around a DOHString. - * - * Author(s) : David Beazley (beazley@cs.uchicago.edu) - * - * Copyright (C) 1998-2000. The University of Chicago - * Copyright (C) 1995-1998. The University of Utah and The Regents of the - * University of California. - * - * See the file LICENSE for information on usage and redistribution. - * ----------------------------------------------------------------------------- */ - -static char cvsroot[] = "$Header$"; - -#include "internal.h" -#include -extern "C" { -#include "doh.h" -} - -//----------------------------------------------------------------------- -// char *copy_string(char *str) -// -// Makes a copy of string str. Returns a pointer to it. -//----------------------------------------------------------------------- - -char *copy_string(char *str) { - char *res = 0; - if (str) { - res = new char[strlen(str)+1]; - strcpy(res,str); - } - return res; -} - -//----------------------------------------------------------------------- -// void format_string(char *str) -// -// Replace all of the escape sequences in the string str. It is -// assumed that the new string is smaller than the original! -//----------------------------------------------------------------------- - -void format_string(char *str) { - char *newstr, *c,*c1; - int state; - if (!str) return; - newstr = copy_string(str); - c = newstr; - c1 = str; - state = 0; - while (*c) { - switch(state) { - case 0: - if (*c == '\\') - state = 1; - else { - *(c1++) = *c; - state = 0; - } - break; - case 1: - // We're in a simple escape sequence figure out what to do - switch(*c) { - case 'n': - *(c1++) = '\n'; - break; - case 'f': - *(c1++) = '\f'; - break; - case 'r': - *(c1++) = '\r'; - break; - case 't': - *(c1++) = '\t'; - break; - case '\\': - *(c1++) = '\\'; - break; - case '\"': - *(c1++) = '\"'; - break; - case '\'': - *(c1++) = '\''; - break; - default: - *(c1++) = '\\'; - *(c1++) = *c; - } - state = 0; - break; - default: - *(c1++) = *c; - state = 0; - } - c++; - } - *c1 = 0; - delete newstr; -} - - - diff --git a/SWIG/Source/SWIG1.1/swig11.h b/SWIG/Source/SWIG1.1/swig11.h index a68c70454..487640ff7 100644 --- a/SWIG/Source/SWIG1.1/swig11.h +++ b/SWIG/Source/SWIG1.1/swig11.h @@ -55,7 +55,6 @@ extern int Inline; // Inline mode extern int NoInclude; // NoInclude flag extern char *typemap_lang; // Current language name extern int error_count; -extern char *copy_string(char *); extern char output_dir[512]; // Output directory extern int Verbose; extern int IsVirtual; @@ -219,10 +218,9 @@ public: /* Emit functions */ -extern void emit_extern_var(char *, DataType *, int, FILE *); -extern void emit_extern_func(char *, DataType *, ParmList *, int, FILE *); extern void emit_func_call(char *, DataType *, ParmList *, FILE *); extern void emit_set_get(char *, char *, DataType *); +extern void emit_set_action(DOHString_or_char *decl); extern int SWIG_main(int, char **, Language *); diff --git a/SWIG/Source/SWIG1.1/typemap.cxx b/SWIG/Source/SWIG1.1/typemap.cxx index 235f5635a..241a3bc0d 100644 --- a/SWIG/Source/SWIG1.1/typemap.cxx +++ b/SWIG/Source/SWIG1.1/typemap.cxx @@ -76,7 +76,7 @@ struct TypeMap { ParmList *args; // Local variables (if any) TypeMap(char *l, DataType *t, char *c, ParmList *p = 0) { - lang = copy_string(l); + lang = Swig_copy_string(l); type = CopyDataType(t); code = NewString(c); first = type_id; @@ -90,7 +90,7 @@ struct TypeMap { } } TypeMap(char *l, char *c) { - lang = copy_string(l); + lang = Swig_copy_string(l); type = 0; code = NewString(c); first = type_id; @@ -100,7 +100,7 @@ struct TypeMap { args = 0; } TypeMap(TypeMap *t) { - lang = copy_string(t->lang); + lang = Swig_copy_string(t->lang); type = CopyDataType(t->type); code = Copy(t->code); first = type_id; @@ -126,7 +126,7 @@ struct TmMethod { DataType *type; // Typemap type TmMethod *next; // Next method TmMethod(char *n, DataType *t, TmMethod *m = 0) { - if (n) name = copy_string(n); + if (n) name = Swig_copy_string(n); else name = 0; if (t) { type = CopyDataType(t); @@ -310,7 +310,6 @@ void typemap_register(char *op, char *lang, DataType *type, char *pname, Parm *p; p = ParmList_first(tm->args); while (p) { - DataType *pt = Parm_Gettype(p); char *pn = Parm_Getname(p); if (pn) { // printf(" %s %s\n", pt,pn); @@ -711,7 +710,7 @@ char *typemap_lookup(char *op, char *lang, DataType *type, char *pname, char *so // Copy old array string (just in case) if (DataType_arraystr(m->type)) - oldary = copy_string(DataType_arraystr(m->type)); + oldary = Swig_copy_string(DataType_arraystr(m->type)); else oldary = 0; @@ -738,7 +737,7 @@ char *typemap_lookup(char *op, char *lang, DataType *type, char *pname, char *so result = typemap_lookup_internal(op,lang,m->type,ppname,source,target,f); DataType_set_arraystr(m->type,oldary); if (oldary) - delete oldary; + free(oldary); m->type->is_pointer -= drop_pointer; if (result) { type->is_pointer += drop_pointer; @@ -827,7 +826,7 @@ char *typemap_check_internal(char *op, char *lang, DataType *type, char *pname) Printf(str,"%s",tm->code); // Return character string - Char(str); + return Char(str); } // Function for checking with applications @@ -871,7 +870,7 @@ char *typemap_check(char *op, char *lang, DataType *type, char *pname) { else ppname = pname; m->type->is_pointer += drop_pointer; if (DataType_arraystr(m->type)) - oldary = copy_string(DataType_arraystr(m->type)); + oldary = Swig_copy_string(DataType_arraystr(m->type)); else oldary = 0; @@ -895,7 +894,7 @@ char *typemap_check(char *op, char *lang, DataType *type, char *pname) { } result = typemap_check_internal(op,lang,m->type,ppname); DataType_set_arraystr(m->type,oldary); - if (oldary) delete oldary; + if (oldary) free(oldary); m->type->is_pointer -= drop_pointer; if (result) { type->is_pointer += drop_pointer;