Major reworking of the Wrapper object. It is now a DOH object.

git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk/SWIG@948 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
Dave Beazley 2000-11-22 23:14:01 +00:00
commit a543ad49f7
14 changed files with 717 additions and 653 deletions

View file

@ -19,42 +19,25 @@ static char cvsroot[] = "$Header$";
#include "swig.h"
#include <ctype.h>
/* -----------------------------------------------------------------------------
* NewWrapper()
*
* Create a new wrapper function object.
* ----------------------------------------------------------------------------- */
#include "dohobj.h"
Wrapper *
NewWrapper() {
Wrapper *w;
w = (Wrapper *) malloc(sizeof(Wrapper));
w->localh = NewHash();
w->locals = NewString("");
w->code = NewString("");
w->def = NewString("");
w->_type = 0;
w->_parms = 0;
w->_name = 0;
return w;
}
typedef struct {
Hash *attr; /* Attributes */
Hash *localh; /* Hash of local variable names */
String *code; /* Code string */
} WrapObj;
/* -----------------------------------------------------------------------------
* DelWrapper()
*
* Delete a wrapper function object.
* ----------------------------------------------------------------------------- */
void
DelWrapper(Wrapper *w) {
static void
DelWrapper(DOH *wo) {
WrapObj *w = (WrapObj *) ObjData(wo);
Delete(w->localh);
Delete(w->locals);
Delete(w->code);
Delete(w->def);
Delete(w->_type);
Delete(w->_parms);
Delete(w->_name);
free(w);
Delete(w->attr);
DohFree(w);
}
/* -----------------------------------------------------------------------------
@ -139,20 +122,37 @@ Wrapper_pretty_print(String *str, File *f) {
/* -----------------------------------------------------------------------------
* Wrapper_print()
* Wrapper_str()
*
* Print out a wrapper function. Does pretty printing as well.
* Create a string representation of the wrapper function.
* ----------------------------------------------------------------------------- */
void
Wrapper_print(Wrapper *w, File *f) {
String *str;
static String *
Wrapper_str(DOH *wo) {
String *s, *s1;
WrapObj *w = (WrapObj *) ObjData(wo);
s = NewString(w->code);
s1 = NewString("");
Replace(s,"$locals", Getattr(w->attr,"locals"), DOH_REPLACE_ANY);
Wrapper_pretty_print(s,s1);
Delete(s);
return s1;
}
str = NewString("");
Printf(str,"%s\n", w->def);
Printf(str,"%s\n", w->locals);
Printf(str,"%s\n", w->code);
Wrapper_pretty_print(str,f);
/* -----------------------------------------------------------------------------
* Wrapper_dump()
*
* Serialize on out
* ----------------------------------------------------------------------------- */
static int
Wrapper_dump(DOH *wo, DOH *out) {
String *s;
int len;
s = Wrapper_str(wo);
len = Dump(s,out);
Delete(s);
return len;
}
/* -----------------------------------------------------------------------------
@ -163,13 +163,14 @@ Wrapper_print(Wrapper *w, File *f) {
* ----------------------------------------------------------------------------- */
int
Wrapper_add_local(Wrapper *w, const String_or_char *name, const String_or_char *decl) {
Wrapper_add_local(Wrapper *wo, const String_or_char *name, const String_or_char *decl) {
WrapObj *w = (WrapObj *) ObjData(wo);
/* See if the local has already been declared */
if (Getattr(w->localh,name)) {
return -1;
}
Setattr(w->localh,name,decl);
Printf(w->locals,"%s;\n", decl);
Printf(Getattr(w->attr,"locals"),"%s;\n", decl);
return 0;
}
@ -182,11 +183,12 @@ Wrapper_add_local(Wrapper *w, const String_or_char *name, const String_or_char *
* ----------------------------------------------------------------------------- */
int
Wrapper_add_localv(Wrapper *w, const String_or_char *name, ...) {
Wrapper_add_localv(Wrapper *wo, const String_or_char *name, ...) {
va_list ap;
int ret;
String *decl;
DOH *obj;
WrapObj *w = (WrapObj *) ObjData(wo);
decl = NewString("");
va_start(ap,name);
@ -198,7 +200,7 @@ Wrapper_add_localv(Wrapper *w, const String_or_char *name, ...) {
}
va_end(ap);
ret = Wrapper_add_local(w,name,decl);
ret = Wrapper_add_local(wo,name,decl);
Delete(decl);
return ret;
}
@ -210,7 +212,8 @@ Wrapper_add_localv(Wrapper *w, const String_or_char *name, ...) {
* ----------------------------------------------------------------------------- */
int
Wrapper_check_local(Wrapper *w, const String_or_char *name) {
Wrapper_check_local(Wrapper *wo, const String_or_char *name) {
WrapObj *w = (WrapObj *) ObjData(wo);
if (Getattr(w->localh,name)) {
return 1;
}
@ -225,12 +228,12 @@ Wrapper_check_local(Wrapper *w, const String_or_char *name) {
* ----------------------------------------------------------------------------- */
char *
Wrapper_new_local(Wrapper *w, const String_or_char *name, const String_or_char *decl) {
Wrapper_new_local(Wrapper *wo, const String_or_char *name, const String_or_char *decl) {
int i;
char *ret;
String *nname = NewString(name);
String *ndecl = NewString(decl);
char *ret;
WrapObj *w = (WrapObj *) ObjData(wo);
i = 0;
while (Wrapper_check_local(w,nname)) {
@ -240,7 +243,7 @@ Wrapper_new_local(Wrapper *w, const String_or_char *name, const String_or_char *
}
Replace(ndecl, name, nname, DOH_REPLACE_ID);
Setattr(w->localh,nname,ndecl);
Printf(w->locals,"%s;\n", ndecl);
Printf(Getattr(w->attr,"locals"),"%s;\n", ndecl);
ret = Char(nname);
Delete(nname);
Delete(ndecl);
@ -257,11 +260,12 @@ Wrapper_new_local(Wrapper *w, const String_or_char *name, const String_or_char *
* ----------------------------------------------------------------------------- */
char *
Wrapper_new_localv(Wrapper *w, const String_or_char *name, ...) {
Wrapper_new_localv(Wrapper *wo, const String_or_char *name, ...) {
va_list ap;
char *ret;
String *decl;
DOH *obj;
WrapObj *w = (WrapObj *) ObjData(wo);
decl = NewString("");
va_start(ap,name);
@ -273,71 +277,188 @@ Wrapper_new_localv(Wrapper *w, const String_or_char *name, ...) {
}
va_end(ap);
ret = Wrapper_new_local(w,name,decl);
ret = Wrapper_new_local(wo,name,decl);
Delete(decl);
return ret;
}
/* -----------------------------------------------------------------------------
* Wrapper_Gettype()
* Wrapper_Getattr()
* ----------------------------------------------------------------------------- */
SwigType *
Wrapper_Gettype(Wrapper *w) {
return w->_type;
static DOH *
Wrapper_getattr(Wrapper *wo, DOH *k) {
WrapObj *w = (WrapObj *) ObjData(wo);
return Getattr(w->attr,k);
}
/* -----------------------------------------------------------------------------
* Wrapper_Settype()
* Wrapper_Delattr()
* ----------------------------------------------------------------------------- */
void
Wrapper_Settype(Wrapper *w, SwigType *t) {
Delete(w->_type);
w->_type = NewString(t);
static int
Wrapper_delattr(Wrapper *wo, DOH *k) {
WrapObj *w = (WrapObj *) ObjData(wo);
Delattr(w->attr,k);
return 0;
}
/* -----------------------------------------------------------------------------
* Wrapper_Getparms()
* Wrapper_Setattr()
* ----------------------------------------------------------------------------- */
ParmList *
Wrapper_Getparms(Wrapper *w) {
return w->_parms;
static int
Wrapper_setattr(Wrapper *wo, DOH *k, DOH *obj) {
WrapObj *w = (WrapObj *) ObjData(wo);
return Setattr(w->attr,k,obj);
}
/* -----------------------------------------------------------------------------
* Wrapper_Setparms()
* Wrapper_firstkey()
* ----------------------------------------------------------------------------- */
void
Wrapper_Setparms(Wrapper *w, ParmList *l) {
Delete(w->_parms);
w->_parms = l;
DohIncref(l);
static DOH *
Wrapper_firstkey(Wrapper *wo) {
WrapObj *w = (WrapObj *) ObjData(wo);
return Firstkey(w->attr);
}
/* -----------------------------------------------------------------------------
* Wrapper_Getname()
* Wrapper_firstkey()
* ----------------------------------------------------------------------------- */
char *
Wrapper_Getname(Wrapper *w) {
return Char(w->_name);
static DOH *
Wrapper_nextkey(Wrapper *wo) {
WrapObj *w = (WrapObj *) ObjData(wo);
return Nextkey(w->attr);
}
/* File methods. These simply operate on the code string */
static int
Wrapper_read(Wrapper *wo, void *buffer, int nbytes) {
WrapObj *w = (WrapObj *) ObjData(wo);
return Read(w->code,buffer,nbytes);
}
static int
Wrapper_write(Wrapper *wo, void *buffer, int nbytes) {
WrapObj *w = (WrapObj *) ObjData(wo);
return Write(w->code,buffer,nbytes);
}
static int
Wrapper_putc(Wrapper *wo, int ch) {
WrapObj *w = (WrapObj *) ObjData(wo);
return Putc(ch, w->code);
}
static int
Wrapper_getc(Wrapper *wo) {
WrapObj *w = (WrapObj *) ObjData(wo);
return Getc(w->code);
}
static int
Wrapper_ungetc(Wrapper *wo, int ch) {
WrapObj *w = (WrapObj *) ObjData(wo);
return Ungetc(ch, w->code);
}
static int
Wrapper_seek(Wrapper *wo, long offset, int whence) {
WrapObj *w = (WrapObj *) ObjData(wo);
return Seek(w->code, offset, whence);
}
static long
Wrapper_tell(Wrapper *wo) {
WrapObj *w = (WrapObj *) ObjData(wo);
return Tell(w->code);
}
/* String method */
static int Wrapper_replace(DOH *wo, DOH *tok, DOH *rep, int flags) {
WrapObj *w = (WrapObj *) ObjData(wo);
return Replace(w->code, tok, rep, flags);
}
/* -----------------------------------------------------------------------------
* Wrapper_Setname()
* type information
* ----------------------------------------------------------------------------- */
void
Wrapper_Setname(Wrapper *w, String_or_char *n) {
Delete(w->_name);
w->_name = NewString(n);
static DohHashMethods WrapperHashMethods = {
Wrapper_getattr,
Wrapper_setattr,
Wrapper_delattr,
Wrapper_firstkey,
Wrapper_nextkey,
};
static DohFileMethods WrapperFileMethods = {
Wrapper_read,
Wrapper_write,
Wrapper_putc,
Wrapper_getc,
Wrapper_ungetc,
Wrapper_seek,
Wrapper_tell,
0, /* close */
};
static DohStringMethods WrapperStringMethods = {
Wrapper_replace,
0,
};
static DohObjInfo WrapperType = {
"Wrapper", /* objname */
DelWrapper, /* doh_del */
0, /* doh_copy */
0, /* doh_clear */
Wrapper_str, /* doh_str */
0, /* doh_data */
0, /* doh_dump */
0, /* doh_len */
0, /* doh_hash */
0, /* doh_cmp */
0, /* doh_setfile */
0, /* doh_getfile */
0, /* doh_setline */
0, /* doh_getline */
&WrapperHashMethods, /* doh_mapping */
0, /* doh_sequence */
&WrapperFileMethods, /* doh_file */
&WrapperStringMethods, /* doh_string */
0, /* doh_positional */
0,
};
/* -----------------------------------------------------------------------------
* NewWrapper()
*
* Create a new wrapper function object.
* ----------------------------------------------------------------------------- */
#define DOHTYPE_WRAPPER 0xa
Wrapper *
NewWrapper() {
WrapObj *w;
static int init = 0;
if (!init) {
DohRegisterType(DOHTYPE_WRAPPER, &WrapperType);
init = 1;
}
w = (WrapObj *) DohMalloc(sizeof(WrapObj));
w->localh = NewHash();
w->code = NewString("");
w->attr= NewHash();
Setattr(w->attr,"locals","");
Setattr(w->attr,"wrapcode", w->code);
return DohObjMalloc(DOHTYPE_WRAPPER, w);
}