From be178afd80184f6a6d4de1bdf44d66888ce63f05 Mon Sep 17 00:00:00 2001 From: Dave Beazley Date: Mon, 18 Dec 2000 02:53:26 +0000 Subject: [PATCH] Few remaining files from SWIG1.1 added. New generate.cxx file added for walking parse trees. git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk/SWIG@957 626c5289-ae23-0410-ae9c-e8d60b6d4f22 --- Source/Modules1.1/Makefile.in | 10 +- Source/Modules1.1/emit.cxx | 254 +++++++++++ Source/Modules1.1/generate.cxx | 760 +++++++++++++++++++++++++++++++++ Source/Modules1.1/guile.cxx | 118 +---- Source/Modules1.1/guile.h | 8 +- Source/Modules1.1/lang.cxx | 475 +++++++++++++++++++++ Source/Modules1.1/main.cxx | 524 +++++++++++++++++++++++ Source/Modules1.1/mod11.h | 11 - Source/Modules1.1/mzscheme.cxx | 112 +---- Source/Modules1.1/mzscheme.h | 8 +- Source/Modules1.1/perl5.cxx | 169 +++----- Source/Modules1.1/perl5.h | 18 +- Source/Modules1.1/python.cxx | 377 ++++++++-------- Source/Modules1.1/python.h | 26 +- Source/Modules1.1/ruby.cxx | 216 ++++------ Source/Modules1.1/ruby.h | 17 +- Source/Modules1.1/swig11.h | 124 ++++++ Source/Modules1.1/swigmain.cxx | 2 +- Source/Modules1.1/tcl8.cxx | 98 ++--- Source/Modules1.1/tcl8.h | 8 +- 20 files changed, 2572 insertions(+), 763 deletions(-) create mode 100644 Source/Modules1.1/emit.cxx create mode 100644 Source/Modules1.1/generate.cxx create mode 100644 Source/Modules1.1/lang.cxx create mode 100644 Source/Modules1.1/main.cxx delete mode 100644 Source/Modules1.1/mod11.h create mode 100644 Source/Modules1.1/swig11.h diff --git a/Source/Modules1.1/Makefile.in b/Source/Modules1.1/Makefile.in index b9f686a2f..1cbcc3bda 100644 --- a/Source/Modules1.1/Makefile.in +++ b/Source/Modules1.1/Makefile.in @@ -13,11 +13,15 @@ AR = @AR@ RANLIB = @RANLIB@ TARGET = libmodules11.a -OBJS = swigmain.o tcl8.o python.o perl5.o guile.o ruby.o mzscheme.o #java.o -SRCS = swigmain.cxx tcl8.cxx python.cxx perl5.cxx guile.cxx ruby.cxx mzscheme.cxx #java.cxx +COREOBJS = main.o emit.o lang.o generate.o +CORESRCS = main.cxx emit.cxx lang.cxx generate.cxx + +OBJS = $(COREOBJS) swigmain.o tcl8.o python.o perl5.o guile.o ruby.o mzscheme.o #java.o +SRCS = $(CORESRCS) swigmain.cxx tcl8.cxx python.cxx perl5.cxx guile.cxx ruby.cxx mzscheme.cxx #java.cxx INCLUDE = -I$(srcdir)/../Include \ - -I$(srcdir)/../SWIG1.1 \ + -I$(srcdir)/../Preprocessor \ + -I$(srcdir)/../LParse \ -I$(srcdir)/../DOH/Include \ -I$(srcdir)/../Swig \ -I../Include diff --git a/Source/Modules1.1/emit.cxx b/Source/Modules1.1/emit.cxx new file mode 100644 index 000000000..735d351f4 --- /dev/null +++ b/Source/Modules1.1/emit.cxx @@ -0,0 +1,254 @@ +/* ----------------------------------------------------------------------------- + * emit.cxx + * + * Useful functions for emitting various pieces of code. + * + * 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. + * ----------------------------------------------------------------------------- */ + +#include "swig11.h" + +static char cvsroot[] = "$Header$"; + +/* ----------------------------------------------------------------------------- + * new_create_function() + * + * Create a new function + * ----------------------------------------------------------------------------- */ + +void new_create_function(char *name, char *iname, SwigType *type, ParmList *l) { + Hash *h; + h = NewHash(); + Setattr(h,"name",name); + Setattr(h,"scriptname",iname); + Setattr(h,"type",type); + Setattr(h,"parms",l); + lang->function(h); + Delete(h); +} + +/* ----------------------------------------------------------------------------- + * emit_args() + * + * 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(DOH *node, Wrapper *f) { + + SwigType *rt; + ParmList *l; + Parm *p; + int i; + char *tm; + SwigType *pt; + DOHString *pvalue; + DOHString *pname; + DOHString *lname; + + rt = Getattr(node,"type"); + l = Getattr(node,"parms"); + + /* Emit function arguments */ + Swig_cargs(f, l); + + i = 0; + p = l; + while (p != 0) { + lname = Getlname(p); + pt = Gettype(p); + pname = Getname(p); + pvalue = Getvalue(p); + + tm = Swig_typemap_lookup((char*)"arginit",pt,pname,(char*)"",lname,f); + if (tm) { + Printv(f,tm,"\n",0); + } + /* Check for ignore or default typemaps */ + tm = Swig_typemap_lookup((char*)"default",pt,pname,(char*)"",lname,f); + if (tm) { + Printv(f,tm,"\n",0); + } + tm = Swig_typemap_lookup((char*)"ignore",pt,pname,(char*)"",lname,f); + if (tm) { + Printv(f,tm,"\n",0); + Setignore(p,1); + } + i++; + p = Getnext(p); + } + 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. + * ----------------------------------------------------------------------------- */ + +static DOH *fcall = 0; + +void emit_set_action(DOHString_or_char *decl) { + if (fcall) Delete (fcall); + fcall = NewString(decl); +} + +void emit_func_call(DOH *node, Wrapper *f) { + char *decl; + SwigType *t; + ParmList *l; + char *tm; + + decl = GetChar(node,"name"); + t = Getattr(node,"type"); + l = Getattr(node,"parms"); + + if ((tm = Swig_typemap_lookup((char*)"except",t,decl,(char*)"result",(char*)"",0))) { + Printv(f,tm,0); + Replace(f,"$name",decl,DOH_REPLACE_ANY); + } else if ((tm = Swig_except_lookup())) { + Printv(f,tm,0); + Replace(f,"$name",decl,DOH_REPLACE_ANY); + } else { + Printv(f,"$function",0); + } + + if (!fcall) fcall = NewString(Swig_cfunction_call(decl,l)); + + if (CPlusPlus) { + Swig_cppresult(f, t, (char*)"result", Char(fcall)); + } else { + Swig_cresult(f, t, (char*)"result", Char(fcall)); + } + Delete(fcall); + fcall = 0; +} + +/* ----------------------------------------------------------------------------- + * void emit_set_get() + * + * 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 *)"\ +if ($target) free($target);\n\ +$target = ($rtype) malloc(strlen($source)+1);\n\ +strcpy((char *)$target,$source);\n\ +return $ltype $target;\n"; + +/* How to assign a C allocated string */ + +static char *cpp_str = (char *)"\ +if ($target) delete [] $target;\n\ +$target = ($rtype) (new char[strlen($source)+1]);\n\ +strcpy((char *)$target,$source);\n\ +return ($ltype) $target;\n;"; + + +void emit_set_get(DOH *node) { + char *name, *iname; + SwigType *t; + Wrapper *w; + DOHString *new_iname; + char *code = 0; + + name = GetChar(node,"name"); + iname = GetChar(node,"iname"); + t = Getattr(node,"type"); + + /* First write a function to set the variable of the variable */ + if (!ReadOnly) { + + if (SwigType_type(t) == T_STRING) { + if (CPlusPlus) + code = cpp_str; + else + code = c_str; + } + w = Swig_cvarset_wrapper(name, t, code); + Printf(f_header,"%s", w); + new_iname = Swig_name_set(iname); + DohIncref(new_iname); + new_create_function(GetChar(w,"name"), Char(new_iname), Gettype(w), Getparms(w)); + Delete(new_iname); + Delete(w); + } + + w = Swig_cvarget_wrapper(name,t,0); + Printf(f_header,"%s", w); + new_iname = Swig_name_get(iname); + DohIncref(new_iname); + new_create_function(GetChar(w,"name"), Char(new_iname), Gettype(w), Getparms(w)); + Delete(new_iname); + Delete(w); +} + +/* ------------------------------------------------------------------ + * int check_numopt() + * + * Gets the number of optional arguments for a ParmList. + * ------------------------------------------------------------------ */ + +int check_numopt(ParmList *p) { + int n = 0; + int i = 0; + int state = 0; + + for (;p; p = Getnext(p),i++) { + SwigType *pt = Gettype(p); + String *pn = Getname(p); + if (Getvalue(p)) { + n++; + state = 1; + } else if (Swig_typemap_search((char*)"default",pt,pn)) { + n++; + state = 1; + } else if (Swig_typemap_search((char*)"ignore",pt,pn)) { + n++; + } else { + if (state) { + Printf(stderr,"%s:%d. Argument %d must have a default value!\n", Getfile(p), Getline(p),i+1); + } + } + } + return n; +} + + + + + + + + diff --git a/Source/Modules1.1/generate.cxx b/Source/Modules1.1/generate.cxx new file mode 100644 index 000000000..3dee2d9bb --- /dev/null +++ b/Source/Modules1.1/generate.cxx @@ -0,0 +1,760 @@ +/* ----------------------------------------------------------------------------- + * generate.cxx + * + * This file manages the code generation process and serves as a bridge between + * the new SWIG parser and the old set of C++-based language modules. + * + * Author(s) : David Beazley (beazley@cs.uchicago.edu) + * + * Copyright (C) 1999-2000. The University of Chicago + * See the file LICENSE for information on usage and redistribution. + * ----------------------------------------------------------------------------- */ + +#include "swig11.h" + +static char cvstag[] = "$Header$"; + +int ReadOnly = 0; +int WrapExtern = 0; + +/* Access permissions: public, private, protected */ +enum { PUBLIC, PRIVATE, PROTECTED }; +int Access = PUBLIC; + +/* Miscellaneous modes */ +int Native = 0; + +/* This function tries to locate the module name within the parse tree */ +static String *find_module(DOH *node) { + DOH *n; + + if (!node) return 0; + n = node; + while (n) { + if (Swig_tag_check(n,"module")) { + return Getname(n); + } + if (Swig_tag_check(n,"file")) { + String *ty; + ty = Getattr(n,"type"); + if (Cmp(ty,"include") == 0) { + DOH *m; + /* Might be in an include file */ + m = find_module(Getchild(n)); + if (m) return m; + } + } + n = Getnext(n); + } + return find_module(Getchild(node)); +} + +/* This helper function emits external function declarations */ +static +void emit_extern_func(DOH *node, File *f) { + Parm *p; + SwigType *tc; + char *c; + String *storage; + storage = Getattr(node,"storage"); + if (!storage) return; + c = Char(storage); + if (strncmp(c,"extern",6) == 0) { + List *tl = NewList(); + p = Getparms(node); + while (p) { + Append(tl,Gettype(p)); + p = Getnext(p); + } + tc = Copy(Gettype(node)); + SwigType_add_function(tc,tl); + Printf(f,"%s %s;\n", storage, SwigType_str(tc,Getname(node))); + Delete(tc); + Delete(tl); + } +} + +/* Test if static */ +static int +check_static(DOH *node) { + String *storage = Getattr(node,"storage"); + if (!storage) return 0; + if (Cmp(storage,"static") == 0) return 1; + return 0; +} + +/* Test if extern */ +static int +check_extern(DOH *node) { + String *storage = Getattr(node,"storage"); + if (!storage) return 0; + if (strncmp(Char(storage),"extern",6) == 0) return 1; + return 0; +} + +static String *new_name = 0; + +/* Handle renaming */ +static +void set_scriptname(DOH *node) { + if (new_name) { + Setattr(node,"scriptname",new_name); + } else { + String *aname = Getattr(node,"altname"); + if (aname) { + Setattr(node,"scriptname",aname); + } else { + Setattr(node,"scriptname", Getname(node)); + } + } + new_name = 0; +} + +/* ----------------------------------------------------------------------------- + * C++ Support + * ----------------------------------------------------------------------------- */ + +static DOH *class_hash = 0; /* Hash table of classes that have been seen so far */ +static DOH *current_class = 0; /* Set when wrapping a class */ +static DOH *class_name = 0; /* Real name of current class */ +static DOH *class_types = 0; /* Types defined within this class */ +static String *construct_name = 0; /* Expected name of a constructor */ +int AddMethods = 0; /* Set when in addmethods mode */ +int Abstract = 0; /* Set when the class is determined to be abstract */ + +/* Check for abstract classes */ + +int cplus_check_abstract(DOH *node) { + while (node) { + if (Getattr(node,"abstract")) return 1; + node = Getnext(node); + } + return 0; +} + +/* Given a class object, this function builds an internal symbol table */ +void cplus_build_symbols(DOH *node) { + Hash *sym; + DOH *c; + sym = Getattr(node,"symbols"); + if (!sym) { + sym = NewHash(); + Setattr(node,"symbols",sym); + } + c = Getchild(node); + while (c) { + String *name = Getname(c); + String *tag = Gettag(c); + if (Cmp(tag,"c:destructor") == 0) { + name = NewStringf("~%s",name); + } + if (name) { + DOH *pnode = Getattr(sym,name); + if (pnode) { + Printf(stderr,"%s:%d. '%s' redefined. Previous definition at %s:%d.\n", + Getfile(c),Getline(c), name, Getfile(pnode), Getline(pnode)); + } else { + Setattr(sym,name,c); + } + } + c = Getnext(c); + } + return; +} + +/* Add a class type */ +static void +class_addtype(String *name) { + String *s = NewStringf("%s::%s", class_name, name); + if (!class_types) class_types = NewHash(); + Setattr(class_types,name,s); +} + +/* Updates a type with a fully qualified version */ +static void +class_update_type(String *type) { + String *base, *rep; + base = SwigType_base(type); + if (!class_types) return; + rep = Getattr(class_types,base); + if (rep) { + SwigType_setbase(type,rep); + /* Printf(stdout,"updated type = '%s'\n", type); */ + } +} + +/* Updates a list of parms with fully qualified names */ +static void +class_update_parms(ParmList *p) { + while (p) { + class_update_type(Gettype(p)); + p = Getnext(p); + } +} + +extern "C" { +int swig11_unknown(DOH *node, void *clientdata) { + Printf(stdout,"::: Unknown tag - '%s'\n", Getattr(node,"tag")); + return 0; +} + +int swig11_nil(DOH *node, void *clientdata) { + return 0; +} + +/* ----------------------------------------------------------------------------- + * swig11_file() + * + * File inclusion directives. %include, %extern, and %import. + * ----------------------------------------------------------------------------- */ + +int swig11_file(DOH *node, void *clientdata) { + DOH *c; + String *type; + int old_we = WrapExtern; + + type = Getattr(node,"type"); + if ((Cmp(type, "extern") == 0) || (Cmp(type,"import") == 0)) { + WrapExtern = 1; + } + c = Getchild(node); + if (Cmp(type,"import") == 0) { + /* If importing a module, we try to find the module name and pass it to + the language modules */ + String *modname; + modname = find_module(c); + if (modname) { + lang->import(modname); + } + } + Swig_emit_all(c,clientdata); + WrapExtern = old_we; + return 0; +} +/* ----------------------------------------------------------------------------- + * swig11_scope() + * + * Handle the %scope directive. This is a new feature not present in SWIG1.1. + * Creates a new typemap scope and has other side effects. + * ----------------------------------------------------------------------------- */ + +int swig11_scope(DOH *node, void *clientdata) { + DOH *c; + c = Getchild(node); + Swig_typemap_new_scope(); + Swig_emit_all(c,clientdata); + Swig_typemap_pop_scope(); + return 0; +} + +/* ----------------------------------------------------------------------------- + * swig11_insert() + * + * Code insertion with various %{ %} directives. + * ----------------------------------------------------------------------------- */ + +int swig11_insert(DOH *node, void *clientdata) { + String *section; + String *filename; + String *code; + File *out; + + if (WrapExtern) return 0; + + section = Getattr(node,"section"); + if (!section) { + section = (void *) "header"; + } + out = Swig_filebyname(section); + if (!out) { + Printf(stderr,"%s:%d. Can't insert code into unknown section '%s'\n", Getfile(node), Getline(node), section); + return 0; + } + filename = Getattr(node,"filename"); + if (filename) { + /* The user is inserting the contents of a file */ + if (Swig_insert_file(filename,out) < 0) { + Printf(stderr,"%s:%d. File '%s' not found.\n", Getfile(node), Getline(node), filename); + } + return 0; + } + code = Getattr(node,"code"); + if (code) { + Printf(out,"%s",code); + } + return 0; +} + +/* ----------------------------------------------------------------------------- + * swig11_pragma() + * + * %pragma directive. + * ----------------------------------------------------------------------------- */ + +int swig11_pragma(DOH *node, void *clientdata) { + String *name; + String *value; + + if (WrapExtern) return 0; + + name = Getname(node); + value = Getvalue(node); + + Printf(stdout,"::: Pragma\n"); + Printf(stdout," name = '%s'\n", name); + Printf(stdout," value = '%s'\n", value); + + if (Cmp(name,"readonly") == 0) { + ReadOnly = 1; + } else if (Cmp(name,"readwrite") == 0) { + ReadOnly = 0; + } + lang->pragma(node); + return 0; +} + +/* ----------------------------------------------------------------------------- + * swig11_typemap() + * + * Handle the %typemap directive. + * ----------------------------------------------------------------------------- */ + +int swig11_typemap(DOH *node, void *clientdata) { + String *name; + SwigType *type; + String *code; + DOH *parms; + String *method; + + if (WrapExtern) return 0; + method = Getattr(node,"method"); + name = Getname(node); + type = Gettype(node); + code = Getattr(node,"code"); + parms = Getparms(node); + + if (code) { + Swig_typemap_register(method,type,name,code,parms); + } else { + String *srcname; + String *srctype; + srcname = Getattr(node,"srcname"); + srctype = Getattr(node,"srctype"); + if (srcname && srctype) { + Swig_typemap_copy(method,srctype,srcname,type,name); + } else { + Swig_typemap_clear(method,type,name); + } + } + return 0; +} + +/* ----------------------------------------------------------------------------- + * swig11_apply() + * + * The %apply directive. + * ----------------------------------------------------------------------------- */ + +int swig11_apply(DOH *node, void *clientdata) { + DOH *parms; + String *name; + SwigType *type; + + if (WrapExtern) return 0; + + name = Getname(node); + type = Gettype(node); + parms = Getparms(node); + + while (parms) { + Swig_typemap_apply(type,name,Gettype(parms),Getname(parms)); + parms = Getnext(parms); + } + return 0; +} + +/* ----------------------------------------------------------------------------- + * swig11_exception() + * + * The %except directive. + * ----------------------------------------------------------------------------- */ + +int swig11_exception(DOH *node, void *clientdata) { + String *code = Getattr(node,"code"); + if (WrapExtern) return 0; + if (code) { + Swig_except_register(code); + } else { + Swig_except_clear(); + } + return 0; +} + +/* ----------------------------------------------------------------------------- + * swig11_clear() + * + * The %clear directive. + * ----------------------------------------------------------------------------- */ + +int swig11_clear(DOH *node, void *clientdata) { + DOH *parms = Getattr(node,"parms"); + if (WrapExtern) return 0; + while (parms) { + Swig_typemap_clear_apply(Gettype(parms),Getname(parms)); + parms = Getnext(parms); + } + return 0; +} + +/* ----------------------------------------------------------------------------- + * swig11_constant + * + * The %constant directive + * ----------------------------------------------------------------------------- */ + +int swig11_constant(DOH *node, void *clientdata) { + if (WrapExtern) return 0; + if (Access != PUBLIC) return 0; + + set_scriptname(node); + lang->constant(node); + return 0; +} + +/* ----------------------------------------------------------------------------- + * swig11_function() + * + * Emit a wrapper function. + * ----------------------------------------------------------------------------- */ + +int swig11_function(DOH *node, void *clientdata) { + int is_static; + if (WrapExtern) return 0; + if (Access != PUBLIC) return 0; + + is_static = check_static(node); + set_scriptname(node); + + /* Will need some kind of class check in here */ + + if (current_class) { + /* Function has been declared inside a class definition. */ + class_update_parms(Getparms(node)); + String *name = Getname(node); + if (Cmp(name,construct_name) == 0) { + if (!Abstract) + lang->cpp_constructor(node); + } else { + if (is_static) lang->cpp_staticfunction(node); + else lang->cpp_memberfunction(node); + } + } else { + + /* Can't wrap a static function. Oh well. */ + if (is_static) return 0; + emit_extern_func(node,f_header); + lang->function(node); + } + return 0; +} + +/* ----------------------------------------------------------------------------- + * swig11_variable() + * + * Wrap a variable. + * ----------------------------------------------------------------------------- */ + +int swig11_variable(DOH *node, void *clientdata) { + int is_static; + SwigType *type; + + if (WrapExtern) return 0; + if (Access != PUBLIC) return 0; + + type = Gettype(node); + + is_static = check_static(node); + set_scriptname(node); + + if (current_class) { + /* Inside a class definition */ + if (is_static) { + lang->cpp_staticvariable(node); + } else { + lang->cpp_variable(node); + } + } else { + if (check_extern(node)) { + Printf(f_header,"extern %s;\n", SwigType_str(type, Getname(node))); + } + if (is_static) return 0; + + if (SwigType_isconst(type)) { + swig11_constant(node,clientdata); + } else { + lang->variable(node); + } + } + return 0; +} + +/* ----------------------------------------------------------------------------- + * swig11_typedef() + * + * Handle a typedef declaration. + * ----------------------------------------------------------------------------- */ + +int swig11_typedef(DOH *node, void *clientdata) { + String *name; + SwigType *type; + + type = Gettype(node); + name = Getname(node); + SwigType_typedef(type,name); + + lang->add_typedef(type, Char(name)); + + return 0; +} + +/* ----------------------------------------------------------------------------- + * swig11_enum() + * + * Support for enumerations. + * ----------------------------------------------------------------------------- */ + +int swig11_enum(DOH *node, void *clientdata) { + DOH *c; + String *name; + if (WrapExtern) return 0; + if (Access != PUBLIC) return 0; + + name = Getname(node); + if (name && CPlusPlus) { + /* Add a typedef */ + String *t = NewStringf("enum %s", name); + SwigType_typedef(t,name); + class_addtype(name); + Delete(t); + } + c = Getchild(node); + Swig_emit_all(c,clientdata); + return 0; +} + +/* ----------------------------------------------------------------------------- + * swig11_enumvalue() + * + * Create a constant corresponding to an enum value. + * ----------------------------------------------------------------------------- */ +int swig11_enumvalue(DOH *node, void *clientdata) { + set_scriptname(node); + Setattr(node,"type","int"); /* Enums wrapped as ints */ + if (!Getvalue(node)) { /* If no value, use the name itself */ + if (class_name) { + Setvalue(node,NewStringf("%s::%s",class_name, Getname(node))); + } else { + Setvalue(node,Getname(node)); + } + } + if (current_class) { + lang->cpp_constant(node); + } else { + swig11_constant(node,clientdata); + } + return 0; +} + +/* ----------------------------------------------------------------------------- + * swig11_class() + * + * Wrapping of C++ classes + * ----------------------------------------------------------------------------- */ + +int swig11_class(DOH *node, void *clientdata) { + DOH *c; + + /* Save the class */ + String *name = Getname(node); + Setattr(class_hash,name,node); + if (WrapExtern) return 0; + + set_scriptname(node); + class_name = name; + + /* Create a new type scope for this class */ + + SwigType_new_scope(); + if (name) { + SwigType_set_scope_name(name); + } + class_types = 0; + cplus_build_symbols(node); + lang->cpp_open_class(node); + current_class = node; + + construct_name = Getname(node); + if (!CPlusPlus) { + String *altname = Getattr(node,"altname"); + if (altname) construct_name = altname; + } + + c = Getchild(node); + Abstract = cplus_check_abstract(c); + + Swig_emit_all(c,clientdata); + + List *bases = Getattr(node,"bases"); + if (bases) { + lang->cpp_inherit(bases,INHERIT_ALL); + } + lang->cpp_close_class(); + + /* Pop the type scope and save with the class */ + Hash *scp = SwigType_pop_scope(); + Setattr(node,"typescope",scp); + + Setattr(node,"types",class_types); + current_class = 0; + construct_name = 0; + class_name = 0; + return 0; +} + +/* ----------------------------------------------------------------------------- + * swig11_classdecl() + * + * Empty class declaration. Used to register classes with language modules. + * ----------------------------------------------------------------------------- */ + +int swig11_classdecl(DOH *node, void *clientdata) { + if (WrapExtern) return 0; + set_scriptname(node); + + lang->cpp_class_decl(node); + return 0; +} + +int swig11_addmethods(DOH *node, void *clientdata) { + DOH *c; + int oldaddmethods = AddMethods; + if (WrapExtern) return 0; + if (!current_class) { + Printf(stderr,"%s:%d. %%addmethods ignored (does not appear inside a class).\n", Getfile(node),Getline(node)); + return 0; + } + AddMethods = 1; + c = Getchild(node); + Swig_emit_all(c,clientdata); + AddMethods = oldaddmethods;; + return 0; +} + +/* ----------------------------------------------------------------------------- + * swig11_destructor() + * + * C++ Destructor + * ----------------------------------------------------------------------------- */ + +int swig11_destructor(DOH *node, void *clientdata) { + if (WrapExtern) return 0; + if (Access != PUBLIC) return 0; + if (!current_class) return 0; + + set_scriptname(node); + lang->cpp_destructor(node); + return 0; +} + +/* ----------------------------------------------------------------------------- + * swig11_access() + * + * Handle an access specifier (public, private, protected) + * ----------------------------------------------------------------------------- */ + +int swig11_access(DOH *node, void *clientdata) { + String *name = Getname(node); + if (Cmp(name,"public") == 0) Access = PUBLIC; + else if (Cmp(name,"private") == 0) Access = PRIVATE; + else if (Cmp(name,"protected") == 0) Access = PROTECTED; + return 0; +} + +/* ----------------------------------------------------------------------------- + * swig11_types() + * + * Handle the types directive. + * ----------------------------------------------------------------------------- */ + +int swig11_types(DOH *node, void *clientdata) { + Parm *p; + p = Getparms(node); + while (p) { + SwigType *t = Gettype(p); + SwigType_remember(t); + p = Getnext(p); + } + return 0; +} + +static SwigRule rules[] = { + { "file", swig11_file}, + { "scope", swig11_scope}, + { "insert", swig11_insert}, + { "pragma", swig11_pragma}, + { "typemap", swig11_typemap}, + { "apply", swig11_apply}, + { "exception", swig11_exception}, + { "clear", swig11_clear}, + { "addmethods", swig11_addmethods}, + { "constant", swig11_constant}, + { "function", swig11_function}, + { "variable", swig11_variable}, + { "typedef", swig11_typedef}, + { "enum", swig11_enum}, + { "enumvalue", swig11_enumvalue}, + { "class", swig11_class}, + { "classdecl", swig11_classdecl}, + { "destructor", swig11_destructor}, + { "access", swig11_access}, + { "types", swig11_types}, + { "module", swig11_nil}, + { "*", swig11_unknown}, + { 0 } +}; + +} + +/* ----------------------------------------------------------------------------- + * generate() + * + * Called by the SWIG1.1 system to emit code + * ----------------------------------------------------------------------------- */ + +void generate(DOH *node) { + DOH *c; + extern String *swig_module; + + /* Initialize globals */ + class_hash = NewHash(); + + Swig_add_rules(rules); + c = Getattr(node,"child"); + + /* Find the module name */ + if (!swig_module) { + swig_module = find_module(c); + } + if (!swig_module) { + Printf(stderr,"SWIG: No module name specified! Please use %%module or -module.\n"); + SWIG_exit(EXIT_FAILURE); + } + + lang->initialize(swig_module); + Swig_emit_all(c,0); + lang->close(); + + Swig_dump_tags(node,0); + +} + diff --git a/Source/Modules1.1/guile.cxx b/Source/Modules1.1/guile.cxx index c76dcd582..934f141e5 100644 --- a/Source/Modules1.1/guile.cxx +++ b/Source/Modules1.1/guile.cxx @@ -23,7 +23,7 @@ static char cvsroot[] = "$Header$"; * Definitions for adding functions to Guile ***********************************************************************/ -#include "mod11.h" +#include "swig11.h" #include "guile.h" static char *guile_usage = (char*)"\ @@ -48,9 +48,6 @@ Guile Options (available with -guile)\n\ GUILE::GUILE () { - // Set global vars - - typemap_lang = (char*)"guile"; // Set class vars @@ -105,16 +102,6 @@ GUILE::parse_args (int argc, char *argv[]) Swig_arg_error(); } } - else if (strcmp (argv[i], "-module") == 0) { - if (argv[i + 1]) { - set_module (argv[i + 1]); - Swig_mark_arg (i); - Swig_mark_arg (i + 1); - ++i; - } else { - Swig_arg_error(); - } - } /* Bogus upcase requirement due to top-level parsing not respecting language specification. Top-level should stop when it sees "-guile" or other languages. */ @@ -166,70 +153,17 @@ GUILE::parse_args (int argc, char *argv[]) } // -------------------------------------------------------------------- -// GUILE::parse() +// GUILE::initialize() // -// Parse the input file -// -------------------------------------------------------------------- +// Output initialization code that registers functions with the +// interface. +// --------------------------------------------------------------------- void -GUILE::parse () +GUILE::initialize (String *modname) { printf ("Generating wrappers for Guile\n"); - // Print out GUILE specific headers - - headers(); - - // Run the parser - - yyparse(); -} - -// --------------------------------------------------------------------- -// GUILE::set_module(char *mod_name) -// -// Sets the module name. -// Does nothing if it's already set (so it can be overridden as a command -// line option). -// -//---------------------------------------------------------------------- - -void -GUILE::set_module (char *mod_name) -{ - if (module) { - printf ("module already set (%s), returning\n", module); - return; - } - - module = new char [strlen (mod_name) + 1]; - strcpy (module, mod_name); -} - -// --------------------------------------------------------------------- -// GUILE::set_init(char *iname) -// -// Sets the initialization function name. -// Does nothing if it's already set -// -//---------------------------------------------------------------------- - -void -GUILE::set_init (char *iname) -{ - abort (); // for now -ttn - set_module (iname); -} - -// --------------------------------------------------------------------- -// GUILE::headers(void) -// -// Generate the appropriate header files for GUILE interface. -// ---------------------------------------------------------------------- - -void -GUILE::headers (void) -{ Printf(f_runtime, "/* -*- buffer-read-only: t -*- vi: set ro: */\n"); Swig_banner (f_runtime); @@ -240,18 +174,12 @@ GUILE::headers (void) if (NoInclude) { Printf(f_runtime, "#define SWIG_NOINCLUDE\n"); } -} -// -------------------------------------------------------------------- -// GUILE::initialize() -// -// Output initialization code that registers functions with the -// interface. -// --------------------------------------------------------------------- + if (!module) { + module = new char[Len(modname)+1]; + strcpy(module, Char(modname)); + } -void -GUILE::initialize (void) -{ switch (linkage) { case GUILE_LSTYLE_SIMPLE: /* Simple linkage; we have to export the SWIG_init function. The user can @@ -456,8 +384,7 @@ guile_do_doc_typemap(DOHFile *file, const char *op, static void throw_unhandled_guile_type_error (SwigType *d) { - Printf (stderr, "%s : Line %d. Unable to handle type %s.\n",input_file, line_number, SwigType_str(d,0)); - error_count++; + Printf (stderr, "%s:%d. Unable to handle type %s.\n",Getfile(d), Getline(d), SwigType_str(d,0)); } // ---------------------------------------------------------------------- @@ -518,7 +445,6 @@ GUILE::function (DOH *node) { Printf(f,"SCM s_%d", i); } Printf(f,")\n{\n"); - Printf(f,"$locals\n"); /* Define the scheme name in C. This define is used by several Guile macros. */ @@ -773,7 +699,7 @@ GUILE::variable (DOH *node) Printf (f_wrappers, "SCM %s(SCM s_0) {\n", var_name); - if (!(Status & STAT_READONLY) && SwigType_type(t) == T_STRING) { + if (!(ReadOnly) && SwigType_type(t) == T_STRING) { Printf (f_wrappers, "\t char *_temp;\n"); Printf (f_wrappers, "\t int _len;\n"); } @@ -785,7 +711,7 @@ GUILE::variable (DOH *node) // Yup. Extract the type from s_0 and set variable value - if (Status & STAT_READONLY) { + if (ReadOnly) { Printf (f_wrappers, "\t\t scm_misc_error(\"%s\", " "\"Unable to set %s. Variable is read only.\", SCM_EOL);\n", proc_name, proc_name); @@ -846,7 +772,7 @@ GUILE::variable (DOH *node) // Now add symbol to the Guile interpreter if (!emit_setters - || Status & STAT_READONLY) { + || ReadOnly) { /* Read-only variables become a simple procedure returning the value. */ Printf (f_init, "\t gh_new_procedure(\"%s\", %s, 0, 1, 0);\n", @@ -865,7 +791,7 @@ GUILE::variable (DOH *node) /* Compute documentation */ String *signature = NewString(""); - if (Status & STAT_READONLY) { + if (ReadOnly) { Printv(signature, "(", proc_name, ")\n", 0); Printv(signature, "Returns constant ", 0); guile_do_doc_typemap(signature, "varoutdoc", t, NULL, @@ -890,9 +816,9 @@ GUILE::variable (DOH *node) } } else { - Printf (stderr, "%s : Line %d. ** Warning. Unable to link with " + Printf (stderr, "%s:%d. ** Warning. Unable to link with " " type %s (ignored).\n", - input_file, line_number, SwigType_str(t,0)); + Getfile(node), Getline(node), SwigType_str(t,0)); } Delete(proc_name); Delete(f); @@ -911,7 +837,7 @@ GUILE::constant(DOH *node) char *name; SwigType *type; char *value; - int OldStatus = Status; // Save old status flags + int OldStatus = ReadOnly; // Save old status flags DOHString *proc_name; char var_name[256]; DOHString *rvalue; @@ -923,7 +849,7 @@ GUILE::constant(DOH *node) value = GetChar(node,"value"); f = NewWrapper(); - Status = STAT_READONLY; // Enable readonly mode. + ReadOnly = 1; // Enable readonly mode. // Make a static variable; @@ -934,8 +860,8 @@ GUILE::constant(DOH *node) Replace(proc_name,"_", "-", DOH_REPLACE_ANY); if ((SwigType_type(type) == T_USER) && (!is_a_pointer(type))) { - Printf (stderr, "%s : Line %d. Unsupported constant value.\n", - input_file, line_number); + Printf (stderr, "%s:%d. Unsupported constant value.\n", + Getfile(node), Getline(node)); return; } @@ -968,7 +894,7 @@ GUILE::constant(DOH *node) Setattr(nnode,"name",var_name); variable(nnode); Delete(nnode); - Status = OldStatus; + ReadOnly = OldStatus; } Delete(proc_name); Delete(rvalue); diff --git a/Source/Modules1.1/guile.h b/Source/Modules1.1/guile.h index a2b36f52f..a9aef3374 100644 --- a/Source/Modules1.1/guile.h +++ b/Source/Modules1.1/guile.h @@ -43,16 +43,12 @@ private: public : GUILE (); void parse_args (int, char *argv[]); - void parse (); + void initialize(String *module); void function (DOH *node); void variable (DOH *node); void constant (DOH *node); - void initialize (); - void headers (void); void close (void); - void set_module(char *); - void set_init (char *); - void create_command (char *, char *) { }; + void create_command (String *, String *) { }; void cpp_variable(DOH *node); }; diff --git a/Source/Modules1.1/lang.cxx b/Source/Modules1.1/lang.cxx new file mode 100644 index 000000000..76468a47f --- /dev/null +++ b/Source/Modules1.1/lang.cxx @@ -0,0 +1,475 @@ +/* ----------------------------------------------------------------------------- + * lang.cxx + * + * Language base class functions. Default C++ handling is also implemented here. + * + * 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 "swig11.h" + +/* ----------------------------------------------------------------- + * Language::create_command() + * ----------------------------------------------------------------- */ + +void Language::create_command(String *, String *) { + Printf(stderr,"SWIG Warning. No command creation procedure defined.\n"); + Printf(stderr,"C++ inheritance may not work correctly.\n"); +} + +/* ----------------------------------------------------------------- + * Language::nativefunction() + * ----------------------------------------------------------------- */ + +void +Language::nativefunction(DOH *node) { + Printf(stderr,"%s:%d. Adding native function %s not supported (ignored).\n", Getfile(node), Getline(node), Getattr(node,"scriptname")); +} + + +/* The following strings are used during code generation and contain various + permutations of the class name: + + ClassName - This is the name of the class as it appears in C/C++ + source code. It does not include a type specifier + such as "struct" or "class" + + ClassRename - If non-NULL, the class has been renamed. + ClassType - The type of the class (struct, class, union) + ClassFullname - The full name of the class including its type specifier. + For example, "class Foo" or "struct Vector". + ClassPrefix - The class prefix as used in the scripting language + interface. This is either ClassName or ClassRename. +*/ + +static String *ClassName = 0; +static String *ClassRename = 0; +static String *ClassType = 0; +static String *ClassFullname = 0; +static String *ClassPrefix = 0; + +/* ----------------------------------------------------------------------------- + * Language::cpp_open_class() + * ----------------------------------------------------------------------------- */ + +void Language::cpp_open_class(DOH *node) { + String *classname; + String *classrename; + String *ctype; + String *altname; + int strip = CPlusPlus; + + classname = Getname(node); + classrename = Getattr(node,"scriptname"); + ctype = Getattr(node,"classtype"); + + altname = Getattr(node,"altname"); + if (altname) { + strip = 1; + classname = altname; /* Use the alt name instead of the class name */ + } + + if (strip) SetInt(node,"strip",1); + + /* Copy the class name */ + if (ClassName) Delete(ClassName); + ClassName = NewString(classname); + + /* Copy the class renaming */ + + if (ClassRename) Delete(ClassRename); + ClassRename = NewString(classrename); + + if (ClassType) Delete(ClassType); + ClassType = strip ? 0 : NewString(ctype); + + if (ClassFullname) Delete(ClassFullname); + + ClassFullname = ClassType ? NewStringf("%s %s", ClassType, ClassName) : NewString(ClassName); + ClassPrefix = ClassRename; +} + +/* ----------------------------------------------------------------------------- + * Language::cpp_close_class() + * ----------------------------------------------------------------------------- */ + +void Language::cpp_close_class() { + + /* Doesn't really do anything */ +} + +/* ----------------------------------------------------------------------------- + * Language::cpp_memberfunction() + * ----------------------------------------------------------------------------- */ + +void Language::cpp_memberfunction(DOH *node) { + String *name; + String *iname; + String *ccode; + String *script_name; + SwigType *type; + ParmList *parms; + Wrapper *w; + + name = Getattr(node,"name"); + iname = Getattr(node,"scriptname"); + type = Getattr(node,"type"); + parms = Getattr(node,"parms"); + ccode = Getattr(node,"code"); + + /* Generate the C wrapper function name and interpreter name of this function*/ + /* Create the actual function name */ + + script_name = Swig_name_member(ClassPrefix, iname ? iname : name); + + /* Now do a symbol table lookup on it */ + + /* Create the C wrapper function for this */ + w = Swig_cmethod_wrapper(ClassFullname, name, type, parms, ccode); + if (AddMethods && ccode) { + /* Dump the C wrappers */ + Printf(f_wrappers,"%s",w); + } else if (!AddMethods) { + /* Just create a string that does what we want */ + emit_set_action(Swig_cmethod_call(name, Getparms(w))); + } + Setattr(w,"scriptname",script_name); + lang->function(w); + Delete(w); +} + +/* ----------------------------------------------------------------------------- + * Language::cpp_constructor() + * ----------------------------------------------------------------------------- */ + +void Language::cpp_constructor(DOH *node) { + String *name; + String *iname; + String *cname; + String *ccode; + ParmList *parms; + Wrapper *w; + + name = Getattr(node,"name"); + iname = Getattr(node,"scriptname"); + parms = Getattr(node,"parms"); + ccode = Getattr(node,"code"); + + if ((Cmp(name,ClassName))) { + Printf(stderr,"%s:%d. Function %s must have a return type.\n", + Getfile(node), Getline(node), name); + return; + } + + cname = Swig_name_construct(iname ? iname : ClassPrefix); + + /* Add this function to the SWIG symbol table */ + + if (CPlusPlus) { + w = Swig_cppconstructor_wrapper(ClassFullname, parms, ccode); + } else { + w = Swig_cconstructor_wrapper(ClassFullname, parms, ccode); + } + Setattr(w,"scriptname", cname); + + if (!AddMethods) { + if (CPlusPlus) { + emit_set_action(Swig_cppconstructor_call(ClassFullname, parms)); + } else { + emit_set_action(Swig_cconstructor_call(ClassFullname)); + } + } else { + if (ccode) { + Printf(f_wrappers,"%s",w); + } + } + lang->function(w); + Delete(w); + + /* Call our default method */ + /* cplus_emit_constructor(Char(ClassName), Char(ClassType), Char(ClassRename), name, iname, l, AddMethods); */ + +} + +/* ----------------------------------------------------------------------------- + * Language::cpp_destructor() + * ----------------------------------------------------------------------------- */ + +void Language::cpp_destructor(DOH *node) { + String *name; + String *iname; + String *cname; + String *ccode; + Wrapper *w; + + name = Getattr(node,"name"); + iname = Getattr(node,"scriptname"); + cname = Swig_name_destroy(ClassRename ? ClassRename : ClassName); + ccode = Getattr(node,"code"); + + /* Add this function to the SWIG symbol table */ + + if (CPlusPlus) { + w = Swig_cppdestructor_wrapper(ClassFullname,ccode); + } else { + w = Swig_cdestructor_wrapper(ClassFullname, ccode); + } + Setattr(w,"scriptname",cname); + if (AddMethods && ccode) { + Printf(f_wrappers,"%s", w); + lang->function(w); + } else if (AddMethods) { + lang->function(w); + } else { + if (CPlusPlus) + emit_set_action(Swig_cppdestructor_call()); + else + emit_set_action(Swig_cdestructor_call()); + lang->function(w); + } + Delete(w); +} + +/* ----------------------------------------------------------------------------- + * Language::cpp_inherit() + * ----------------------------------------------------------------------------- */ + +void Language::cpp_inherit(List *bases, int mode) { + + if (!bases) return; + /* + while (baseclass[i]) { + // cplus_inherit_members(baseclass[i],mode); + i++; + } + */ +} + +/* ----------------------------------------------------------------------------- + * Language::cpp_variable() + * ----------------------------------------------------------------------------- */ + +void Language::cpp_variable(DOH *node) { + String *name; + String *iname; + String *cname; + String *setcode, *getcode; + SwigType *type; + Wrapper *w; + + name = Getattr(node,"name"); + iname = Getattr(node,"scriptname"); + type = Getattr(node,"type"); + setcode = Getattr(node,"setcode"); + getcode = Getattr(node,"getcode"); + + /* Set the class prefix */ + + cname = Swig_name_get(Swig_name_member(ClassPrefix, iname ? iname : name)); + + /* Check the symbol table */ + + /* Create a function to set the value of the variable */ + if (!ReadOnly) { + w = Swig_cmemberset_wrapper(ClassFullname, name, type, setcode); + Setattr(w,"scriptname",Swig_name_set(Swig_name_member(ClassPrefix,iname ? iname : name))); + if (AddMethods && setcode) { + Printf(f_wrappers,"%s",w); + } else if (!AddMethods) { + /* Check for a member in typemap here */ + String *target = NewStringf("%s->%s", Swig_cparm_name(0,0),name); + char *tm = Swig_typemap_lookup("memberin",type,name,Swig_cparm_name(0,1),target,0); + if (!tm) + emit_set_action(Swig_cmemberset_call(name,type)); + else + emit_set_action(tm); + Delete(target); + } + lang->function(w); + Delete(w); + } + w = Swig_cmemberget_wrapper(ClassFullname, name, type, getcode); + Setattr(w,"scriptname", Swig_name_get(Swig_name_member(ClassPrefix, iname ? iname : name))); + if (AddMethods && getcode) { + Printf(f_wrappers,"%s",w); + } else if (!AddMethods) { + emit_set_action(Swig_cmemberget_call(name,type)); + } + lang->function(w); + Delete(w); +} + +/* ----------------------------------------------------------------------------- + * Language::cpp_static_func() + * ----------------------------------------------------------------------------- */ + +void Language::cpp_staticfunction(DOH *node) { + String *name; + String *iname; + SwigType *type; + ParmList *parms; + String *ccode; + String *script_name; + String *real_name; + DOH *nnode; + Wrapper *w; + + name = Getattr(node,"name"); + iname = Getattr(node,"scriptname"); + type = Getattr(node,"type"); + parms = Getattr(node,"parms"); + ccode = Getattr(node,"code"); + + /* Set the member function name */ + script_name = Swig_name_member(ClassPrefix,iname ? iname : name); + + /* Now do a symbol table lookup on it : */ + + /* Figure out the name of the function */ + if (AddMethods) { + real_name = Swig_name_member(ClassName, name); + if (!ccode) { + nnode = Copy(node); + Setattr(nnode,"name", real_name); + Setattr(nnode,"scriptname", script_name); + lang->function(nnode); + Delete(nnode); + } else { + w = Swig_cfunction_wrapper(real_name,type,parms,ccode); + Printf(f_wrappers,"%s",w); + Setattr(w,"scriptname",script_name); + lang->function(w); + Delete(w); + } + } else { + nnode = Copy(node); + real_name = NewStringf("%s::%s", ClassName, name); + Setattr(nnode,"name", real_name); + Setattr(nnode,"scriptname", script_name); + lang->function(nnode); + Delete(nnode); + Delete(real_name); + } +} + +/* ----------------------------------------------------------------------------- + * Language::cpp_constant() + * ----------------------------------------------------------------------------- */ + +void Language::cpp_constant(DOH *node) +{ + String *name; + String *iname; + String *value; + SwigType *type; + String *cname; + String *mname; + String *new_value; + + name = Getattr(node,"name"); + iname = Getattr(node,"scriptname"); + value = Getattr(node,"value"); + type = Getattr(node,"type"); + + /* Set the constant name */ + + cname = Swig_name_member(ClassPrefix, iname ? iname : name); + + /* Now do a symbol table lookup on it : */ + + /* Form correct C++ name */ + mname = NewStringf("%s::%s", ClassName,name); + + /* Declare a constant */ + if (!value) { + new_value = NewStringf("%s::%s", ClassName, name); + } else { + new_value = NewString(value); + } + Hash *n; + n = NewHash(); + Setattr(n,"name",cname); + Setattr(n,"scriptname",cname); + Setattr(n,"type",type); + Setattr(n,"value",new_value); + lang->constant(n); + Delete(n); + Delete(new_value); + Delete(mname); +} + +/* ----------------------------------------------------------------------------- + * Language::cpp_staticvariable() + * ----------------------------------------------------------------------------- */ + +void Language::cpp_staticvariable(DOH *node) { + char *name, *iname; + SwigType *t; + char *cname; + char mname[256]; + + name = GetChar(node,"name"); + iname = GetChar(node,"scriptname"); + t = Getattr(node,"type"); + + /* Create the variable name */ + + cname = Char(Swig_name_member(ClassPrefix, iname ? iname : name)); + + /* Now do a symbol table lookup on it : */ + + /* Form correct C++ name */ + sprintf(mname,"%s::%s",Char(ClassName),name); + + /* Link with this variable */ + + Hash *n = NewHash(); + Setattr(n,"name",mname); + Setattr(n,"scriptname", cname); + Setattr(n,"type",t); + lang->variable(n); + Delete(n); +} + +/* ----------------------------------------------------------------------------- + * Language::cpp_class_decl() + * ----------------------------------------------------------------------------- */ + +void Language::cpp_class_decl(DOH *) { + /* Does nothing by default */ +} + +/* ----------------------------------------------------------------------------- + * Language::add_typedef() + * ----------------------------------------------------------------------------- */ + +void Language::add_typedef(SwigType *, String *) { + /* Does nothing by default */ +} + +/* ----------------------------------------------------------------------------- + * Language::pragma() + * ----------------------------------------------------------------------------- */ + +void Language::pragma(DOH *node) { + /* Does nothing by default */ +} + +/* ----------------------------------------------------------------------------- + * Language::import() + * ----------------------------------------------------------------------------- */ + +void Language::import(String *) { + /* Does nothing by default */ +} + + + diff --git a/Source/Modules1.1/main.cxx b/Source/Modules1.1/main.cxx new file mode 100644 index 000000000..d487e42d1 --- /dev/null +++ b/Source/Modules1.1/main.cxx @@ -0,0 +1,524 @@ +/* ----------------------------------------------------------------------------- + * main.cxx + * + * Main entry point to the SWIG core. + * + * 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 "swig11.h" +#ifndef MACSWIG +#include "swigconfig.h" +#endif + +#include +#include +#include +#include +extern "C" { +#include "preprocessor.h" +#include "lparse.h" +} + +#ifndef SWIG_LIB +#define SWIG_LIB "/usr/local/lib/swig1.3" +#endif + +#ifndef SWIG_CC +#define SWIG_CC "CC" +#endif + +// Global variables + + FILE *f_runtime; + DOH *f_header; // Some commonly used + DOH *f_wrappers; // FILE pointers + DOH *f_init; + FILE *f_input; + char LibDir[512]; // Library directory + Language *lang; // Language method + int CPlusPlus = 0; + int NewObject = 0; // NewObject flag + int ForceExtern = 0; // Force extern mode + int GenerateDefault = 0; // Generate default constructors + char *Config = 0; + int NoInclude = 0; + int Verbose = 0; + String *swig_module = 0; + +class SwigException {}; + +static char *usage = (char*)"\ +\nGeneral Options\n\ + -c - Produce raw wrapper code (omit support code)\n\ + -c++ - Enable C++ processing\n\ + -co - Check a file out of the SWIG library\n\ + -Dsymbol - Define a symbol (for conditional compilation)\n\ + -I - Look for SWIG files in \n\ + -includeall - Follow all #include statements\n\ + -l - Include SWIG library file.\n\ + -make_default - Create default constructors/destructors\n\ + -o outfile - Set name of the output file.\n\ + -swiglib - Report location of SWIG library and exit\n\ + -v - Run in verbose mode\n\ + -version - Print SWIG version number\n\ + -help - This output.\n\n"; + + + +// ----------------------------------------------------------------------------- +// check_suffix(char *name) +// +// Checks the suffix of a file to see if we should emit extern declarations. +// ----------------------------------------------------------------------------- + +int +check_suffix(char *name) { + char *c; + if (!name) return 0; + if (strlen(name) == 0) return 0; + c = name+strlen(name)-1; + while (c != name) { + if (*c == '.') break; + c--; + } + if (c == name) return 0; + if ((strcmp(c,".c") == 0) || + (strcmp(c,".C") == 0) || + (strcmp(c,".cc") == 0) || + (strcmp(c,".cxx") == 0) || + (strcmp(c,".c++") == 0) || + (strcmp(c,".cpp") == 0)) { + return 1; + } + return 0; +} + +//----------------------------------------------------------------- +// main() +// +// Main program. Initializes the files and starts the parser. +//----------------------------------------------------------------- + +char infilename[256]; +char filename[256]; +char output_dir[512]; +char fn_runtime[256]; + +#ifdef MACSWIG +FILE *swig_log; +#endif + +char *SwigLib; +static int freeze = 0; + +static String *lang_config = 0; + +/* This function sets the name of the configuration file */ + +void SWIG_config_file(const String_or_char *filename) { + lang_config = NewString(filename); +} + +int SWIG_main(int argc, char *argv[], Language *l) { + int i; + char *c; + char temp[512]; + char infile[512]; + char *outfile_name = 0; + int help = 0; + int checkout = 0; + int cpp_only = 0; + int tm_debug = 0; + int tree_debug = 0; + char *includefiles[256]; + int includecount = 0; + extern int check_suffix(char *); + extern void generate(DOH *top); + DOH *libfiles = 0; + DOH *cpps = 0; + char *input_file = 0; + + /* Initialize the SWIG core */ + Swig_init(); + +#ifdef MACSWIG + try { +#endif + + // Initialize the preprocessor + Preprocessor_init(); + + f_wrappers = 0; + f_init = 0; + f_header = 0; + + lang = l; + + // Set up some default symbols (available in both SWIG interface files + // and C files) + + Preprocessor_define((DOH *) "SWIG 1", 0); + Preprocessor_define((DOH *) "__STDC__", 0); +#ifdef MACSWIG + Preprocessor_define((DOH *) "SWIGMAC 1", 0); +#endif +#ifdef SWIGWIN32 + Preprocessor_define((DOH *) "SWIGWIN32 1", 0); +#endif + + // Check for SWIG_LIB environment variable + + if ((c = getenv("SWIG_LIB")) == (char *) 0) { + sprintf(LibDir,"%s",SWIG_LIB); // Build up search paths + } else { + strcpy(LibDir,c); + } + + SwigLib = Swig_copy_string(LibDir); // Make a copy of the real library location + +#ifdef MACSWIG + sprintf(temp,"%s:config", LibDir); + Swig_add_directory((DOH *) ":swig_lib:config"); + Swig_add_directory((DOH *) ":swig_lib"); +#else + sprintf(temp,"%s/config", LibDir); + Swig_add_directory((DOH *) "./swig_lib/config"); + Swig_add_directory((DOH *) "./swig_lib"); +#endif + Swig_add_directory((DOH *) temp); + Swig_add_directory((DOH *) LibDir); + + libfiles = NewList(); + + // Get options + for (i = 1; i < argc; i++) { + if (argv[i]) { + if (strncmp(argv[i],"-I",2) == 0) { + // Add a new directory search path + 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); + Replace(d,(char*)"=",(char*)" ", DOH_REPLACE_ANY | DOH_REPLACE_FIRST); + Preprocessor_define((DOH *) d,0); + // Create a symbol + Swig_mark_arg(i); + } else if (strcmp(argv[i],"-E") == 0) { + cpp_only = 1; + Swig_mark_arg(i); + } else if ((strcmp(argv[i],"-verbose") == 0) || + (strcmp(argv[i],"-v") == 0)) { + Verbose = 1; + Swig_mark_arg(i); + } else if (strcmp(argv[i],"-c++") == 0) { + CPlusPlus=1; + Swig_mark_arg(i); + } else if (strcmp(argv[i],"-c") == 0) { + NoInclude=1; + Preprocessor_define((DOH *) "SWIG_NOINCLUDE 1", 0); + Swig_mark_arg(i); + } else if (strcmp(argv[i],"-make_default") == 0) { + GenerateDefault = 1; + Swig_mark_arg(i); + } else if (strcmp(argv[i],"-swiglib") == 0) { + printf("%s\n", LibDir); + SWIG_exit (EXIT_SUCCESS); + } else if (strcmp(argv[i],"-o") == 0) { + Swig_mark_arg(i); + if (argv[i+1]) { + outfile_name = Swig_copy_string(argv[i+1]); + Swig_mark_arg(i+1); + i++; + } else { + Swig_arg_error(); + } + } else if (strcmp(argv[i],"-version") == 0) { + fprintf(stderr,"\nSWIG Version %s %s\n", + SWIG_VERSION, SWIG_SPIN); + fprintf(stderr,"Copyright (c) 1995-98\n"); + fprintf(stderr,"University of Utah and the Regents of the University of California\n"); + fprintf(stderr,"\nCompiled with %s\n", SWIG_CC); + SWIG_exit (EXIT_SUCCESS); + } else if (strncmp(argv[i],"-l",2) == 0) { + // Add a new directory search path + Append(libfiles,argv[i]+2); + Swig_mark_arg(i); + } else if (strcmp(argv[i],"-co") == 0) { + checkout = 1; + Swig_mark_arg(i); + } else if (strcmp(argv[i],"-freeze") == 0) { + freeze = 1; + Swig_mark_arg(i); + } else if (strcmp(argv[i],"-includeall") == 0) { + Preprocessor_include_all(1); + Swig_mark_arg(i); + } else if (strcmp(argv[i],"-tm_debug") == 0) { + tm_debug = 1; + Swig_mark_arg(i); + } else if(strcmp(argv[i],"-module") == 0) { + if (argv[i+1]) { + swig_module = NewString(argv[i+1]); + Swig_mark_arg(i); + Swig_mark_arg(i+1); + i++; + } else { + Swig_arg_error(); + } + } else if (strcmp(argv[i],"-tree") == 0) { + Swig_mark_arg(i); + tree_debug = 1; + } else if (strcmp(argv[i],"-help") == 0) { + fputs(usage,stderr); + Swig_mark_arg(i); + help = 1; + } + } + } + if (Verbose) + printf ("LibDir: %s\n", LibDir); + + while (includecount > 0) { + Swig_add_directory((DOH *) includefiles[--includecount]); + } + + // Parse language dependent options + lang->parse_args(argc,argv); + + if (help) SWIG_exit (EXIT_SUCCESS); // Exit if we're in help mode + + // Check all of the options to make sure we're cool. + Swig_check_options(); + + // Add language dependent directory to the search path + { + DOH *rl = NewString(""); +#ifdef MACSWIG + Printf(r1,"%s:%s", SwigLib,LibDir); +#else + Printf(rl,"%s/%s", SwigLib,LibDir); +#endif + Swig_add_directory(rl); + } + + // Create names of temporary files that are created + + sprintf(infilename,"%s", argv[argc-1]); + input_file = new char[strlen(infilename)+1]; + strcpy(input_file, infilename); + + // If the user has requested to check out a file, handle that + if (checkout) { + DOH *s; + char *outfile = input_file; + if (outfile_name) + outfile = outfile_name; + + if (Verbose) + printf ("Handling checkout...\n"); + + s = Swig_include(input_file); + if (!s) { + fprintf(stderr,"Unable to locate '%s' in the SWIG library.\n", input_file); + } else { + FILE *f = fopen(outfile,"r"); + if (f) { + fclose(f); + fprintf(stderr,"File '%s' already exists. Checkout aborted.\n", outfile); + } else { + f = fopen(outfile,"w"); + if (!f) { + fprintf(stderr,"Unable to create file '%s'\n", outfile); + } else { + fprintf(stderr,"'%s' checked out from the SWIG library.\n", input_file); + fputs(Char(s),f); + fclose(f); + } + } + } + } else { + // Check the suffix for a .c file. If so, we're going to + // declare everything we see as "extern" + + ForceExtern = check_suffix(infilename); + // Strip off suffix + + c = infilename + strlen(infilename); + while (c != infilename) { + if (*c == '.') { + *c = 0; + break; + } else { + c--; + } + } + if (!outfile_name) { + char *cc = infilename + strlen(infilename); + while (cc != infilename) { + if (*cc == '/') { + cc++; + break; + } + cc--; + } + sprintf(fn_runtime,"%s_wrap.c",infilename); + strcpy(infile,infilename); + outfile_name = fn_runtime; + } else { + sprintf(fn_runtime,"%s",outfile_name); + } + { + // Try to identify the output directory + char *cc = outfile_name; + char *lastc = outfile_name; + while (*cc) { + if (*cc == '/') lastc = cc+1; + cc++; + } + cc = outfile_name; + char *dd = output_dir; + while (cc != lastc) { + *dd = *cc; + dd++; + cc++; + } + *dd = 0; + // Patch up the input filename + cc = infilename + strlen(infilename); + while (cc != infilename) { + if (*cc == '/') { + cc++; + break; + } + cc--; + } + strcpy(infile,cc); + } + + // Define the __cplusplus symbol + if (CPlusPlus) + Preprocessor_define((DOH *) "__cplusplus 1", 0); + + // Run the preprocessor + if (Verbose) + printf ("Preprocessing...\n"); + { + int i; + String *fs = NewString("%include \"swig.swg\"\n"); + String *ds = Swig_include(input_file); + if (!ds) { + Printf(stderr,"Unable to find '%s'\n", input_file); + SWIG_exit (EXIT_FAILURE); + } + + if (lang_config) { + Printf(fs,"\n%%include \"%s\"\n", lang_config); + } + Printf(fs,"\n%%file(\"include\") \"%s\" {\n", Swig_last_file()); + Append(fs, ds); + Append(fs,"\n}\n"); + Delete(ds); + for (i = 0; i < Len(libfiles); i++) { + Printf(fs,"\n%%include \"%s\"\n", Getitem(libfiles,i)); + } + Seek(fs,0,SEEK_SET); + cpps = Preprocessor_parse(fs); + if (cpp_only) { + Printf(stdout,"%s", cpps); + while (freeze); + SWIG_exit (EXIT_SUCCESS); + } + } + if ((f_runtime = fopen(fn_runtime,"w")) == 0) { + fprintf(stderr,"Unable to open %s\n", fn_runtime); + exit(0); + } + f_header = NewString(""); + f_wrappers = NewString(""); + f_init = NewString(""); + + Swig_register_filebyname("header",f_header); + Swig_register_filebyname("runtime", f_runtime); + Swig_register_filebyname("wrapper", f_wrappers); + Swig_register_filebyname("init", f_init); + + // Set up the typemap for handling new return strings + { + if (CPlusPlus) + Swig_typemap_register((char*)"newfree",(char*)"p.char",(char*)"",(char*)"delete [] $source;\n",0); + else + Swig_typemap_register((char*)"newfree",(char*)"p.char",(char*)"",(char*)"free($source);\n",0); + } + + // Pass control over to the specific language interpreter + + if (Verbose) { + fprintf (stdout, "Starting language-specific parse...\n"); + fflush (stdout); + } + // parser_init(); + { + DOH *top; + Seek(cpps,0,SEEK_SET); + top = LParse_parse(cpps); + if (tree_debug) Swig_dump_tree(top); + generate(top); + } + // lang->parse(); + if (Verbose) { + fprintf (stdout, "Finished language-specific parse.\n"); + fflush (stdout); + } + Dump(f_header,f_runtime); + Dump(f_wrappers, f_runtime); + Wrapper_pretty_print(f_init,f_runtime); + fclose(f_runtime); + if (checkout) { + // File was checked out from the SWIG library. Remove it now + remove(input_file); + } + } + if (tm_debug) Swig_typemap_debug(); + while (freeze); + return 0; +} + +// -------------------------------------------------------------------------- +// SWIG_exit(int exit_code) +// +// Cleanup and either freeze or exit +// -------------------------------------------------------------------------- + +void SWIG_exit(int exit_code) { + if (f_runtime) { + fclose(f_runtime); + remove(fn_runtime); + } + while (freeze); + exit (exit_code); +} + + +// -------------------------------------------------------------------------- +// swig_pragma(char *name, char *value) +// +// Handle pragma directives. Not many supported right now +// -------------------------------------------------------------------------- + +void swig_pragma(char *name, char *value) { + + if (strcmp(name,"make_default") == 0) { + GenerateDefault = 1; + } + if (strcmp(name,"no_default") == 0) { + GenerateDefault = 0; + } +} diff --git a/Source/Modules1.1/mod11.h b/Source/Modules1.1/mod11.h deleted file mode 100644 index 376e6fc70..000000000 --- a/Source/Modules1.1/mod11.h +++ /dev/null @@ -1,11 +0,0 @@ - -/* Include the proper SWIG core header */ -/* Feel free to change this when compiling against experiment cores */ -extern "C" { -#include "doh.h" -} -#include "swig11.h" - -extern "C" { -#include "swig.h" -} diff --git a/Source/Modules1.1/mzscheme.cxx b/Source/Modules1.1/mzscheme.cxx index c6a05c43a..51b98db89 100644 --- a/Source/Modules1.1/mzscheme.cxx +++ b/Source/Modules1.1/mzscheme.cxx @@ -23,7 +23,7 @@ static char cvsroot[] = "$Header$"; * Definitions for adding functions to Mzscheme 101 ***********************************************************************/ -#include "mod11.h" +#include "swig11.h" #include "mzscheme.h" static char *mzscheme_usage = (char*)"\ @@ -71,16 +71,6 @@ MZSCHEME::parse_args (int argc, char *argv[]) Swig_arg_error(); } } - else if (strcmp (argv[i], "-module") == 0) { - if (argv[i + 1]) { - set_module (argv[i + 1]); - Swig_mark_arg (i); - Swig_mark_arg (i + 1); - ++i; - } else { - Swig_arg_error(); - } - } } } @@ -97,81 +87,22 @@ MZSCHEME::parse_args (int argc, char *argv[]) // Add a symbol for this module Preprocessor_define ((void *) "SWIGMZSCHEME",0); - - // Set name of typemaps - - typemap_lang = (char*)"mzscheme"; } // -------------------------------------------------------------------- -// MZSCHEME::parse() +// MZSCHEME::initialize() // -// Parse the input file -// -------------------------------------------------------------------- +// Output initialization code that registers functions with the +// interface. +// --------------------------------------------------------------------- void -MZSCHEME::parse () +MZSCHEME::initialize (String *modname) { init_func_def = NewString(""); printf ("Generating wrappers for Mzscheme\n"); - init_func_def = NewString(""); - // Print out MZSCHEME specific headers - - headers(); - - // Run the parser - - yyparse(); - -} - -// --------------------------------------------------------------------- -// MZSCHEME::set_module(char *mod_name) -// -// Sets the module name. -// Does nothing if it's already set (so it can be overridden as a command -// line option). -// -//---------------------------------------------------------------------- - -void -MZSCHEME::set_module (char *mod_name) -{ - if (module) { - printf ("module already set (%s), returning\n", module); - return; - } - - module = new char [strlen (mod_name) + 1]; - strcpy (module, mod_name); -} - -// --------------------------------------------------------------------- -// MZSCHEME::set_init(char *iname) -// -// Sets the initialization function name. -// Does nothing if it's already set -// -//---------------------------------------------------------------------- - -void -MZSCHEME::set_init (char *iname) -{ - abort (); // for now -ttn - set_module (iname); -} - -// --------------------------------------------------------------------- -// MZSCHEME::headers(void) -// -// Generate the appropriate header files for MZSCHEME interface. -// ---------------------------------------------------------------------- - -void -MZSCHEME::headers (void) -{ Swig_banner (f_header); Printf (f_header, "/* Implementation : MZSCHEME */\n\n"); @@ -188,18 +119,12 @@ MZSCHEME::headers (void) SWIG_exit (1); } } -} -// -------------------------------------------------------------------- -// MZSCHEME::initialize() -// -// Output initialization code that registers functions with the -// interface. -// --------------------------------------------------------------------- + if (!module) { + module = new char[Len(modname)+1]; + strcpy(module, Char(modname)); + } -void -MZSCHEME::initialize (void) -{ Printf (f_init, "static void\nSWIG_init (void)\n{\n"); } @@ -305,7 +230,6 @@ MZSCHEME::function(DOH *node) Printv(f, "static Scheme_Object *", wname, " (", 0); Printv(f, "int argc, Scheme_Object **argv", 0); Printv(f, ")\n{\n", 0); - Printf(f, "$locals\n"); // Declare return variable and arguments // number of parameters @@ -538,7 +462,7 @@ MZSCHEME::variable (DOH *node) // Printf (f_wrappers, "\t\t GSWIG_ASSERT(0,\"Unable to set %s. " // "Variable is read only.\", argv[0]);\n", iname); // } - if (Status & STAT_READONLY) { + if (ReadOnly) { Printf (f_wrappers, "\t\t scheme_signal_error(\"Unable to set %s. " "Variable is read only.\");\n", iname); } @@ -607,9 +531,9 @@ MZSCHEME::variable (DOH *node) "), env);\n",0); } else { - Printf (stderr, "%s : Line %d. ** Warning. Unable to link with " + Printf (stderr, "%s:%d. ** Warning. Unable to link with " " type %s (ignored).\n", - input_file, line_number, SwigType_manglestr(t)); + Getfile(node), Getline(node), SwigType_manglestr(t)); } Delete(proc_name); Delete(argnum); @@ -631,7 +555,7 @@ MZSCHEME::constant(DOH *node) SwigType *type; char *value; - int OldStatus = Status; // Save old status flags + int OldStatus = ReadOnly; // Save old status flags char var_name[256]; String *proc_name = NewString(""); String *rvalue = NewString(""); @@ -642,7 +566,7 @@ MZSCHEME::constant(DOH *node) type = Getattr(node,"type"); value = GetChar(node,"value"); - Status = STAT_READONLY; // Enable readonly mode. + ReadOnly = 1; // Make a static variable; @@ -653,8 +577,8 @@ MZSCHEME::constant(DOH *node) Replace(proc_name, "_", "-", DOH_REPLACE_ANY); if ((SwigType_type(type) == T_USER) && (!SwigType_ispointer(type))) { - fprintf (stderr, "%s : Line %d. Unsupported constant value.\n", - input_file, line_number); + fprintf (stderr, "%s:%d. Unsupported constant value.\n", + Getfile(node), Getline(node)); return; } @@ -692,7 +616,7 @@ MZSCHEME::constant(DOH *node) Setattr(nnode,"name",var_name); variable (nnode); Delete(nnode); - Status = OldStatus; + ReadOnly = OldStatus; } Delete(proc_name); Delete(rvalue); diff --git a/Source/Modules1.1/mzscheme.h b/Source/Modules1.1/mzscheme.h index fa0946357..8aa38ebcb 100644 --- a/Source/Modules1.1/mzscheme.h +++ b/Source/Modules1.1/mzscheme.h @@ -34,14 +34,10 @@ private: public : void parse_args (int, char *argv[]); - void parse (); + void initialize(String *module); void function (DOH *node); void variable (DOH *node); void constant (DOH *node); - void initialize (); - void headers (void); void close (void); - void set_module (char *); - void set_init (char *); - void create_command (char *, char *) { }; + void create_command (String *, String *) { }; }; diff --git a/Source/Modules1.1/perl5.cxx b/Source/Modules1.1/perl5.cxx index b1aaba458..e82f2037d 100644 --- a/Source/Modules1.1/perl5.cxx +++ b/Source/Modules1.1/perl5.cxx @@ -17,7 +17,7 @@ static char cvsroot[] = "$Header$"; -#include "mod11.h" +#include "swig11.h" #include "perl5.h" static char *usage = (char*)"\ @@ -29,7 +29,6 @@ Perl5 Options (available with -perl5)\n\ -shadow - Create shadow classes.\n\ -compat - Compatibility mode.\n\n"; -static String *import_file = 0; static String *smodule = 0; static int compat = 0; @@ -109,17 +108,6 @@ PERL5::parse_args(int argc, char *argv[]) { } else { Swig_arg_error(); } - } else if (strcmp(argv[i],"-module") == 0) { - if (argv[i+1]) { - module = NewString(argv[i+1]); - Append(cmodule,module); - Replace(cmodule,":","_",DOH_REPLACE_ANY); - Swig_mark_arg(i); - Swig_mark_arg(i+1); - i++; - } else { - Swig_arg_error(); - } } else if (strcmp(argv[i],"-exportall") == 0) { export_all = 1; Swig_mark_arg(i); @@ -140,14 +128,15 @@ PERL5::parse_args(int argc, char *argv[]) { Preprocessor_define((void *) "SWIGPERL 1", 0); Preprocessor_define((void *) "SWIGPERL5 1", 0); - typemap_lang = (char*)"perl5"; } /* ----------------------------------------------------------------------------- - * PERL5::parse() + * PERL5::initialize() * ----------------------------------------------------------------------------- */ void -PERL5::parse() { +PERL5::initialize(String *modname) +{ + char filen[256]; classes = NewHash(); symbols = NewHash(); @@ -175,55 +164,19 @@ PERL5::parse() { Printf(stderr,"SWIG : Fatal error. Unable to locate 'perl5.swg' in SWIG library.\n"); SWIG_exit (EXIT_FAILURE); } - yyparse(); -} - -/* ----------------------------------------------------------------------------- - * PERL5::set_module() - * ----------------------------------------------------------------------------- */ -void -PERL5::set_module(char *mod_name) { - if (import_file) { - if (!(Cmp(import_file,input_file+strlen(input_file)-Len(import_file)))) { - if (blessed) { - Printf(f_pm,"require %s;\n", mod_name); - } - Delete(import_file); - import_file = 0; - } - } - - if (module) return; - - module = NewString(mod_name); + if (!module) module = NewString(modname); /* Create a C module name and put it in 'cmodule' */ - Clear(cmodule); Append(cmodule,module); Replace(cmodule,":","_",DOH_REPLACE_ANY); -} - -/* ----------------------------------------------------------------------------- - * PERL5::initialize() - * ----------------------------------------------------------------------------- */ -void -PERL5::initialize() -{ - char filen[256]; - - if (!module){ - Printf(stderr,"*** Error. No module name specified.\n"); - SWIG_exit (EXIT_FAILURE); - } if (!package) { package = NewString(module); } /* If we're in blessed mode, change the package name to "packagec" */ - if (blessed) { realpackage = package; package = interface ? interface : NewStringf("%sc",package); @@ -328,9 +281,10 @@ PERL5::initialize() * PERL5::import() * ----------------------------------------------------------------------------- */ void -PERL5::import(char *filename) { - if (import_file) Delete(import_file); - import_file = NewString(filename); +PERL5::import(String *modname) { + if (blessed) { + Printf(f_pm,"require %s;\n", modname); + } } /* ----------------------------------------------------------------------------- @@ -487,7 +441,7 @@ get_pointer(char *iname, char *srcname, char *src, char *dest, * PERL5::create_command() * ----------------------------------------------------------------------------- */ void -PERL5::create_command(char *cname, char *iname) { +PERL5::create_command(String *cname, String *iname) { Printf(f_init,"\t newXS(\"%s::%s\", %s, file);\n", package, iname, Swig_name_wrapper(cname)); if (export_all) { Printf(exported,"%s ",iname); @@ -522,7 +476,6 @@ PERL5::function(DOH *node) outarg = NewString(""); Printv(f, "XS(", Swig_name_wrapper(iname), ") {\n", 0); - Printf(f, "$locals\n"); pcount = emit_args(node, f); numopt = check_numopt(l); @@ -600,7 +553,7 @@ PERL5::function(DOH *node) break; default : - Printf(stderr,"%s : Line %d. Unable to use type %s as a function argument.\n",input_file, line_number, SwigType_str(pt,0)); + Printf(stderr,"%s:%d. Unable to use type %s as a function argument.\n",Getfile(node), Getline(node), SwigType_str(pt,0)); break; } } @@ -696,7 +649,7 @@ PERL5::function(DOH *node) break; default : - Printf(stderr,"%s: Line %d. Unable to use return type %s in function %s.\n", input_file, line_number, SwigType_str(d,0), name); + Printf(stderr,"%s:%d. Unable to use return type %s in function %s.\n", Getfile(node), Getline(node), SwigType_str(d,0), name); break; } } @@ -878,9 +831,8 @@ void PERL5::variable(DOH *node) { /* Create a Perl function for setting the variable value */ - if (!(Status & STAT_READONLY)) { + if (!ReadOnly) { Printf(setf,"SWIGCLASS_STATIC int %s(SV* sv, MAGIC *mg) {\n", set_name); - Printf(setf,"$locals\n"); Printv(setf, tab4, "MAGIC_PPERL\n", tab4, "mg = mg;\n", @@ -961,7 +913,7 @@ void PERL5::variable(DOH *node) { break; default : - Printf(stderr,"%s : Line %d. Unable to link with datatype %s (ignored).\n", input_file, line_number, SwigType_str(t,0)); + Printf(stderr,"%s:%d. Unable to link with datatype %s (ignored).\n", Getfile(node), Getline(node), SwigType_str(t,0)); return; } } @@ -974,7 +926,6 @@ void PERL5::variable(DOH *node) { /* Now write a function to evaluate the variable */ Printf(getf,"SWIGCLASS_STATIC int %s(SV *sv, MAGIC *mg) {\n", val_name); - Printf(getf,"$locals\n"); Printv(getf, tab4, "MAGIC_PPERL\n", tab4, "mg = mg;\n", @@ -1059,7 +1010,7 @@ void PERL5::variable(DOH *node) { Printf(magic,"%s", getf); /* Now add symbol to the PERL interpreter */ - if ((Status & STAT_READONLY) || (!setable)) { + if ((ReadOnly) || (!setable)) { Printv(vinit, tab4, "swig_create_magic(sv,\"", package, "::", iname, "\",MAGIC_CAST MAGIC_CLASS swig_magic_readonly, MAGIC_CAST MAGIC_CLASS ", val_name, ");\n",0); } else { Printv(vinit, tab4, "swig_create_magic(sv,\"", package, "::", iname, "\", MAGIC_CAST MAGIC_CLASS ", set_name, ", MAGIC_CAST MAGIC_CLASS ", val_name, ");\n",0); @@ -1211,7 +1162,7 @@ PERL5::constant(DOH *node) break; default: - Printf(stderr,"%s : Line %d. Unsupported constant value.\n", input_file, line_number); + Printf(stderr,"%s:%d. Unsupported constant value.\n", Getfile(node), Getline(node)); break; } } @@ -1335,9 +1286,14 @@ PERL5::nativefunction(DOH *node) { * PERL5::cpp_open_class() * ----------------------------------------------------------------------------- */ void -PERL5::cpp_open_class(char *classname, char *rname, char *ctype, int strip) { +PERL5::cpp_open_class(DOH *node) { + + this->Language::cpp_open_class(node); + + char *classname = GetChar(node,"name"); + char *rname = GetChar(node,"scriptname"); + char *ctype = GetChar(node,"classtype"); - this->Language::cpp_open_class(classname, rname, ctype, strip); if (blessed) { have_constructor = 0; have_destructor = 0; @@ -1373,7 +1329,11 @@ PERL5::cpp_open_class(char *classname, char *rname, char *ctype, int strip) { member_keys = NewString(""); /* Add some symbols to the hash tables */ - cpp_class_decl(Char(classname),Char(fullclassname),Char(ctype)); + Hash *nnode = NewHash(); + Setattr(nnode,"name", classname); + Setattr(nnode,"scriptname", fullclassname); + Setattr(nnode,"classtype", ctype); + cpp_class_decl(nnode); } } @@ -1736,7 +1696,7 @@ PERL5::cpp_constructor(DOH *node) { return; } Setattr(symbols,cname, cname); - if ((Cmp(realname,class_name) == 0) || ((!iname) && (ObjCClass)) ){ + if ((Cmp(realname,class_name) == 0)) { /* Emit a blessed constructor */ @@ -1851,25 +1811,26 @@ PERL5::cpp_staticfunction(DOH *node) { * PERL5::cpp_inherit() * ------------------------------------------------------------------------------ */ void -PERL5::cpp_inherit(char **baseclass, int) { +PERL5::cpp_inherit(List *bases, int) { + String *base; char *bc; - int i = 0, have_first = 0; + int have_first = 0; if (!blessed) { - this->Language::cpp_inherit(baseclass); + this->Language::cpp_inherit(bases); return; } /* Inherit variables and constants from base classes, but not functions (since Perl can handle that okay). */ - this->Language::cpp_inherit(baseclass, INHERIT_CONST | INHERIT_VAR); + this->Language::cpp_inherit(bases, INHERIT_CONST | INHERIT_VAR); /* Now tell the Perl5 module that we're inheriting from base classes */ base_class = NewString(""); - while (baseclass[i]) { + for (base = Firstitem(bases); base; base = Nextitem(bases)) { /* See if this is a class we know about */ - String *b = NewString(baseclass[i]); + String *b = NewString(base); bc = Char(is_shadow(b)); Delete(b); if (bc) { @@ -1877,7 +1838,6 @@ PERL5::cpp_inherit(char **baseclass, int) { Printf(base_class,bc); have_first = 1; } - i++; } if (!have_first) { Delete(base_class); @@ -1923,15 +1883,18 @@ PERL5::cpp_constant(DOH *node) { * PERL5::cpp_class_decl() * ----------------------------------------------------------------------------- */ void -PERL5::cpp_class_decl(char *name, char *rename, char *type) { +PERL5::cpp_class_decl(DOH *node) { + String *name = Getname(node); + String *rename = Getattr(node,"scriptname"); + String *ctype = Getattr(node,"classtype"); String *stype; if (blessed) { stype = NewString(name); SwigType_add_pointer(stype); Setattr(classes,stype,rename); Delete(stype); - if (strlen(type) > 0) { - stype = NewStringf("%s %s",type,name); + if (Len(ctype) > 0) { + stype = NewStringf("%s %s",ctype,name); SwigType_add_pointer(stype); Setattr(classes,stype,rename); Delete(stype); @@ -1943,11 +1906,15 @@ PERL5::cpp_class_decl(char *name, char *rename, char *type) { * PERL5::add_typedef() * ----------------------------------------------------------------------------- */ void -PERL5::add_typedef(SwigType *t, char *name) { +PERL5::add_typedef(SwigType *t, String *name) { if (!blessed) return; if (is_shadow(t)) { - cpp_class_decl(name,Char(is_shadow(t)),""); + DOH *node = NewHash(); + Setattr(node,"name",name); + Setattr(node,"scriptname", is_shadow(t)); + Setattr(node,"classtype",""); + cpp_class_decl(node); } } @@ -1961,28 +1928,26 @@ PERL5::add_typedef(SwigType *t, char *name) { * %pragma(perl5) include="file.pl" # Includes a file in the .pm file * ----------------------------------------------------------------------------- */ -void PERL5::pragma(char *lang, char *code, char *value) { - if (strcmp(lang,"perl5") == 0) { - if (strcmp(code,"code") == 0) { - /* Dump the value string into the .pm file */ - if (value) { - Printf(pragma_include, "%s\n", value); - } - } else if (strcmp(code,"include") == 0) { - /* Include a file into the .pm file */ - if (value) { - FILE *f = Swig_open(value); - if (!f) { - Printf(stderr,"%s : Line %d. Unable to locate file %s\n", input_file, line_number,value); - } else { - char buffer[4096]; - while (fgets(buffer,4095,f)) { - Printf(pragma_include,"%s",buffer); - } +void PERL5::pragma(DOH *node) { + String *name = Getattr(node,"name"); + String *value = Getattr(node,"value"); + if (Cmp(name,"code") == 0) { + /* Dump the value string into the .pm file */ + if (value) { + Printf(pragma_include, "%s\n", value); + } + } else if (Cmp(name,"include") == 0) { + /* Include a file into the .pm file */ + if (value) { + FILE *f = Swig_open(value); + if (!f) { + Printf(stderr,"%s:%d. Unable to locate file %s\n", Getfile(node), Getline(node),value); + } else { + char buffer[4096]; + while (fgets(buffer,4095,f)) { + Printf(pragma_include,"%s",buffer); } } - } else { - Printf(stderr,"%s : Line %d. Unrecognized pragma.\n", input_file,line_number); } } } diff --git a/Source/Modules1.1/perl5.h b/Source/Modules1.1/perl5.h index 8d78784b4..2d48a8125 100644 --- a/Source/Modules1.1/perl5.h +++ b/Source/Modules1.1/perl5.h @@ -24,31 +24,29 @@ private: char *usage_func(char *, SwigType *, ParmList *); public : virtual void parse_args(int, char *argv[]); - virtual void parse(); + virtual void initialize(String *modname); virtual void function(DOH *node); virtual void variable(DOH *node); virtual void constant(DOH *node); - virtual void initialize(void); virtual void close(void); - virtual void set_module(char *); virtual void nativefunction(DOH *); - virtual void create_command(char *, char *); + virtual void create_command(String *, String *); // Support for blessed perl thingies.... - virtual void cpp_open_class(char *classname, char *rename, char *ctype, int strip); + virtual void cpp_open_class(DOH *); virtual void cpp_close_class(); virtual void cpp_memberfunction(DOH *); virtual void cpp_staticfunction(DOH *); virtual void cpp_variable(DOH *); virtual void cpp_constructor(DOH *); virtual void cpp_destructor(DOH *); - virtual void cpp_inherit(char **baseclass, int mode = INHERIT_ALL); + virtual void cpp_inherit(List *bases, int mode = INHERIT_ALL); virtual void cpp_constant(DOH *); - virtual void cpp_class_decl(char *, char *, char *); - virtual void add_typedef(SwigType *t, char *name); - virtual void pragma(char *, char *, char *); - virtual void import(char *filename); + virtual void cpp_class_decl(DOH *); + virtual void add_typedef(SwigType *t, String *name); + virtual void pragma(DOH *node); + virtual void import(String *filename); }; diff --git a/Source/Modules1.1/python.cxx b/Source/Modules1.1/python.cxx index c4d9a37e5..95347b2f8 100644 --- a/Source/Modules1.1/python.cxx +++ b/Source/Modules1.1/python.cxx @@ -11,7 +11,7 @@ static char cvsroot[] = "$Header$"; -#include "mod11.h" +#include "swig11.h" #include "python.h" static String *const_code = 0; @@ -31,7 +31,6 @@ static String *classes; static String *func; static String *vars; static String *pragma_include = 0; -static String *import_file = 0; static String *methods; static char *class_name; @@ -64,18 +63,8 @@ PYTHON::parse_args(int argc, char *argv[]) { for (i = 1; i < argc; i++) { if (argv[i]) { - if(strcmp(argv[i],"-module") == 0) { - if (argv[i+1]) { - module = NewString(argv[i+1]); - Swig_mark_arg(i); - Swig_mark_arg(i+1); - i++; - } else { - Swig_arg_error(); - } - /* Added edz@bsn.com */ - } else if(strcmp(argv[i],"-interface") == 0) { + if(strcmp(argv[i],"-interface") == 0) { if (argv[i+1]) { interface = NewString(argv[i+1]); Swig_mark_arg(i); @@ -113,14 +102,35 @@ PYTHON::parse_args(int argc, char *argv[]) { } if (!global_name) global_name = NewString("cvar"); Preprocessor_define((void *) "SWIGPYTHON", 0); - typemap_lang = (char*)"python"; } /* ----------------------------------------------------------------------------- - * PYTHON::parse() + * PYTHON::import() * ----------------------------------------------------------------------------- */ void -PYTHON::parse() { +PYTHON::import(String *modname) { + if (shadow) { + Printf(f_shadow,"\nfrom %s import *\n", modname); + } +} + +/* ----------------------------------------------------------------------------- + * PYTHON::add_method() + * ----------------------------------------------------------------------------- */ +void +PYTHON::add_method(String *name, String *function, int kw) { + if (!kw) + Printf(methods,"\t { \"%s\", %s, METH_VARARGS },\n", name, function); + else + Printf(methods,"\t { \"%s\", (PyCFunction) %s, METH_VARARGS | METH_KEYWORDS },\n", name, function); +} + +/* ----------------------------------------------------------------------------- + * PYTHON::initialize() + * ----------------------------------------------------------------------------- */ +void +PYTHON::initialize(String *modname) { + char filen[256]; hash = NewHash(); symbols = NewHash(); @@ -146,62 +156,9 @@ PYTHON::parse() { Printf(stderr,"SWIG : Fatal error. Unable to locate python.swg. (Possible installation problem).\n"); SWIG_exit (EXIT_FAILURE); } - yyparse(); -} + + if (!module) module = NewString(modname); -/* ----------------------------------------------------------------------------- - * PYTHON::set_module() - * ----------------------------------------------------------------------------- */ -void -PYTHON::set_module(char *mod_name) { - - /* If an "import" method has been set and we're in shadow class mode, - output a python command to load the module */ - - if (import_file) { - if (!(strcmp(Char(import_file),input_file+strlen(input_file)-strlen(Char(import_file))))) { - if (shadow) { - Printf(f_shadow,"\nfrom %s import *\n", mod_name); - } - Delete(import_file); - import_file = 0; - } - } - if (module) return; - module = NewString(mod_name); -} - -/* ----------------------------------------------------------------------------- - * PYTHON::import(char *filename) - * ----------------------------------------------------------------------------- */ -void -PYTHON::import(char *filename) { - if (import_file) Delete(import_file); - import_file = NewString(filename); -} - -/* ----------------------------------------------------------------------------- - * PYTHON::add_method() - * ----------------------------------------------------------------------------- */ -void -PYTHON::add_method(char *name, char *function, int kw) { - if (!kw) - Printf(methods,"\t { \"%s\", %s, METH_VARARGS },\n", name, function); - else - Printf(methods,"\t { \"%s\", (PyCFunction) %s, METH_VARARGS | METH_KEYWORDS },\n", name, function); -} - -/* ----------------------------------------------------------------------------- - * PYTHON::initialize() - * ----------------------------------------------------------------------------- */ -void -PYTHON::initialize(void) { - char filen[256]; - - if (!module) { - Printf(stderr,"*** Error. No module name specified.\n"); - SWIG_exit (EXIT_FAILURE); - } /* If shadow classing is enabled, we're going to change the module name to "modulec" */ if (shadow) { @@ -314,8 +271,8 @@ PYTHON::get_pointer(char *src, char *dest, SwigType *t, String *f, char *ret) { * PYTHON::create_command() * ----------------------------------------------------------------------------- */ void -PYTHON::create_command(char *cname, char *iname) { - add_method(iname, Char(Swig_name_wrapper(cname)), use_kw); +PYTHON::create_command(String *cname, String *iname) { + add_method(iname, Swig_name_wrapper(cname), use_kw); } /* ----------------------------------------------------------------------------- @@ -371,7 +328,6 @@ PYTHON::function(DOH *node) { "(PyObject *self, PyObject *args, PyObject *kwargs) {", 0); } - Printf(f,"$locals\n"); Wrapper_add_local(f,"resultobj", "PyObject *resultobj"); /* Get the function usage string for later use */ @@ -505,7 +461,7 @@ PYTHON::function(DOH *node) { break; default : - Printf(stderr,"%s : Line %d. Unable to use type %s as a function argument.\n",input_file, line_number, SwigType_str(pt,0)); + Printf(stderr,"%s:%d. Unable to use type %s as a function argument.\n",Getfile(node), Getline(node), SwigType_str(pt,0)); break; } @@ -589,7 +545,7 @@ PYTHON::function(DOH *node) { Printf(f," resultobj = Py_None;\n"); break; default : - Printf(stderr,"%s: Line %d. Unable to use return type %s in function %s.\n", input_file, line_number, SwigType_str(d,0), name); + Printf(stderr,"%s:%d. Unable to use return type %s in function %s.\n", Getfile(node), Getline(node), SwigType_str(d,0), name); break; } } @@ -713,8 +669,7 @@ PYTHON::variable(DOH *node) { /* Create a function for setting the value of the variable */ Printf(setf,"static int %s_set(PyObject *val) {\n", wname); - Printf(setf,"$locals\n"); - if (!(Status & STAT_READONLY)) { + if (!ReadOnly) { if ((tm = Swig_typemap_lookup((char*)"varin",t,name,(char*)"val",name,0))) { Printf(setf,"%s\n",tm); Replace(setf,"$name",iname, DOH_REPLACE_ANY); @@ -726,39 +681,39 @@ PYTHON::variable(DOH *node) { case T_SCHAR: case T_UCHAR: case T_BOOL: Wrapper_add_localv(setf,"tval",SwigType_lstr(t,0),"tval",0); Printv(setf, - tab4, "tval = (", SwigType_lstr(t,0), ") PyInt_AsLong(val);\n", - tab4, "if (PyErr_Occurred()) {\n", - tab8, "PyErr_SetString(PyExc_TypeError,\"C variable '", + "tval = (", SwigType_lstr(t,0), ") PyInt_AsLong(val);\n", + "if (PyErr_Occurred()) {\n", + "PyErr_SetString(PyExc_TypeError,\"C variable '", iname, "'(", SwigType_str(t,0), ")\");\n", - tab8, "return 1; \n", - tab4, "}\n", - tab4, name, " = tval;\n", + "return 1; \n", + "}\n", + name, " = tval;\n", 0); break; case T_FLOAT: case T_DOUBLE: Wrapper_add_localv(setf,"tval",SwigType_lstr(t,0), "tval",0); Printv(setf, - tab4, "tval = (", SwigType_lstr(t,0), ") PyFloat_AsDouble(val);\n", - tab4, "if (PyErr_Occurred()) {\n", - tab8, "PyErr_SetString(PyExc_TypeError,\"C variable '", + "tval = (", SwigType_lstr(t,0), ") PyFloat_AsDouble(val);\n", + "if (PyErr_Occurred()) {\n", + "PyErr_SetString(PyExc_TypeError,\"C variable '", iname, "'(", SwigType_str(t,0), ")\");\n", - tab8, "return 1; \n", - tab4, "}\n", - tab4, name, " = tval;\n", + "return 1; \n", + "}\n", + name, " = tval;\n", 0); break; case T_CHAR: Wrapper_add_local(setf,"tval","char * tval"); Printv(setf, - tab4, "tval = (char *) PyString_AsString(val);\n", - tab4, "if (PyErr_Occurred()) {\n", - tab8, "PyErr_SetString(PyExc_TypeError,\"C variable '", + "tval = (char *) PyString_AsString(val);\n", + "if (PyErr_Occurred()) {\n", + "PyErr_SetString(PyExc_TypeError,\"C variable '", iname, "'(", SwigType_str(t,0), ")\");\n", - tab8, "return 1; \n", - tab4, "}\n", - tab4, name, " = *tval;\n", + "return 1; \n", + "}\n", + name, " = *tval;\n", 0); break; @@ -766,32 +721,32 @@ PYTHON::variable(DOH *node) { SwigType_add_pointer(t); Wrapper_add_localv(setf,"temp",SwigType_lstr(t,0),"temp",0); get_pointer((char*)"val",(char*)"temp",t,setf,(char*)"1"); - Printv(setf, tab4, name, " = *temp;\n", 0); + Printv(setf, name, " = *temp;\n", 0); SwigType_del_pointer(t); break; case T_STRING: Wrapper_add_local(setf,"tval","char * tval"); Printv(setf, - tab4, "tval = (char *) PyString_AsString(val);\n", - tab4, "if (PyErr_Occurred()) {\n", - tab8, "PyErr_SetString(PyExc_TypeError,\"C variable '", + "tval = (char *) PyString_AsString(val);\n", + "if (PyErr_Occurred()) {\n", + "PyErr_SetString(PyExc_TypeError,\"C variable '", iname, "'(", SwigType_str(t,0), ")\");\n", - tab8, "return 1; \n", - tab4, "}\n", + "return 1; \n", + "}\n", 0); if (CPlusPlus) { Printv(setf, - tab4, "if (", name, ") delete [] ", name, ";\n", - tab4, name, " = new char[strlen(tval)+1];\n", - tab4, "strcpy((char *)", name, ",tval);\n", + "if (", name, ") delete [] ", name, ";\n", + name, " = new char[strlen(tval)+1];\n", + "strcpy((char *)", name, ",tval);\n", 0); } else { Printv(setf, - tab4, "if (", name, ") free((char*)", name, ");\n", - tab4, name, " = (char *) malloc(strlen(tval)+1);\n", - tab4, "strcpy((char *)", name, ",tval);\n", + "if (", name, ") free((char*)", name, ");\n", + name, " = (char *) malloc(strlen(tval)+1);\n", + "strcpy((char *)", name, ",tval);\n", 0); } break; @@ -811,9 +766,9 @@ PYTHON::variable(DOH *node) { } if (!setable) { Printv(setf, - tab4, "PyErr_SetString(PyExc_TypeError,\"Variable ", iname, + "PyErr_SetString(PyExc_TypeError,\"Variable ", iname, " is read-only.\");\n", - tab4, "return 1;\n", + "return 1;\n", 0); } Delete(ta); @@ -824,20 +779,20 @@ PYTHON::variable(DOH *node) { case T_POINTER: case T_REFERENCE: Wrapper_add_localv(setf,"temp", SwigType_lstr(t,0), "temp",0); get_pointer((char*)"val",(char*)"temp",t,setf,(char*)"1"); - Printv(setf,tab4, name, " = temp;\n", 0); + Printv(setf, name, " = temp;\n", 0); break; default: - Printf(stderr,"%s : Line %d. Unable to link with type %s.\n", input_file, line_number, SwigType_str(t,0)); + Printf(stderr,"%s:%d. Unable to link with type %s.\n", Getfile(node), Getline(node), SwigType_str(t,0)); } } Printf(setf," return 0;\n"); } else { /* Is a readonly variable. Issue an error */ Printv(setf, - tab4, "PyErr_SetString(PyExc_TypeError,\"Variable ", iname, + "PyErr_SetString(PyExc_TypeError,\"Variable ", iname, " is read-only.\");\n", - tab4, "return 1;\n", + "return 1;\n", 0); } @@ -846,7 +801,6 @@ PYTHON::variable(DOH *node) { /* Create a function for getting the value of a variable */ Printf(getf,"static PyObject *%s_get() {\n", wname); - Printf(getf,"$locals\n"); Wrapper_add_local(getf,"pyobj", "PyObject *pyobj"); if ((tm = Swig_typemap_lookup((char*)"varout",t,name,name,(char*)"pyobj",0))) { Printf(getf,"%s\n",tm); @@ -860,42 +814,51 @@ PYTHON::variable(DOH *node) { case T_SHORT: case T_USHORT: case T_LONG: case T_ULONG: case T_SCHAR: case T_UCHAR: case T_BOOL: - Printv(getf, tab4, "pyobj = PyInt_FromLong((long) ", name, ");\n", 0); + Printv(getf, "pyobj = PyInt_FromLong((long) ", name, ");\n", 0); break; case T_FLOAT: case T_DOUBLE: - Printv(getf, tab4, "pyobj = PyFloat_FromDouble((double) ", name, ");\n", 0); + Printv(getf, "pyobj = PyFloat_FromDouble((double) ", name, ");\n", 0); break; case T_CHAR: Wrapper_add_local(getf,"ptemp","char ptemp[2]"); Printv(getf, - tab4, "ptemp[0] = ", name, ";\n", - tab4, "ptemp[1] = 0;\n", - tab4, "pyobj = PyString_FromString(ptemp);\n", + "ptemp[0] = ", name, ";\n", + "ptemp[1] = 0;\n", + "pyobj = PyString_FromString(ptemp);\n", 0); break; case T_USER: SwigType_add_pointer(t); SwigType_remember(t); Printv(getf, - tab4, "pyobj = SWIG_NewPointerObj((void *) &", name , + "pyobj = SWIG_NewPointerObj((void *) &", name , ", SWIGTYPE", SwigType_manglestr(t), ");\n", 0); SwigType_del_pointer(t); break; case T_STRING: Printv(getf, - tab4, "if (", name, ")\n", - tab8, "pyobj = PyString_FromString(", name, ");\n", - tab4, "else pyobj = PyString_FromString(\"(NULL)\");\n", + "if (", name, ")\n", + "pyobj = PyString_FromString(", name, ");\n", + "else pyobj = PyString_FromString(\"(NULL)\");\n", 0); break; case T_POINTER: case T_ARRAY: case T_REFERENCE: - SwigType_remember(t); - Printv(getf, - tab4, "pyobj = SWIG_NewPointerObj((void *)", name, - ", SWIGTYPE", SwigType_manglestr(t), ");\n", - 0); + { + SwigType *ta = Copy(t); + SwigType_pop(ta); + if (SwigType_type(ta) == T_CHAR) { + Printv(getf,"pyobj = PyString_FromString(", name, ");\n", 0); + } else { + SwigType_remember(t); + Printv(getf, + "pyobj = SWIG_NewPointerObj((void *)", name, + ", SWIGTYPE", SwigType_manglestr(t), ");\n", + 0); + } + Delete(ta); + } break; default: @@ -946,24 +909,24 @@ PYTHON::constant(DOH *node) { case T_SHORT: case T_USHORT: case T_LONG: case T_ULONG: case T_SCHAR: case T_UCHAR: - Printv(const_code, tab4, "{ SWIG_PY_INT, \"", name, "\", (long) ", value, ", 0, 0, 0},\n", 0); + Printv(const_code, "{ SWIG_PY_INT, \"", name, "\", (long) ", value, ", 0, 0, 0},\n", 0); break; case T_DOUBLE: case T_FLOAT: - Printv(const_code, tab4, "{ SWIG_PY_FLOAT, \"", name, "\", 0, (double) ", value, ", 0,0},\n", 0); + Printv(const_code, "{ SWIG_PY_FLOAT, \"", name, "\", 0, (double) ", value, ", 0,0},\n", 0); break; case T_CHAR : - Printf(const_code," { SWIG_PY_STRING, \"%s\", 0, 0, (void *) \"%s\", 0 }, \n", name, value); + Printf(const_code, "{ SWIG_PY_STRING, \"%s\", 0, 0, (void *) \"%s\", 0 }, \n", name, value); break; case T_STRING: - Printf(const_code," { SWIG_PY_STRING, \"%s\", 0, 0, (void *) \"%s\", 0 }, \n", name, value); + Printf(const_code,"{ SWIG_PY_STRING, \"%s\", 0, 0, (void *) \"%s\", 0 }, \n", name, value); break; case T_POINTER: case T_ARRAY: case T_REFERENCE: SwigType_remember(type); - Printv(const_code, tab4, "{ SWIG_PY_POINTER, \"", name, "\", 0, 0, (void *) ", value, ", &SWIGTYPE", SwigType_manglestr(type), "}, \n", 0); + Printv(const_code, "{ SWIG_PY_POINTER, \"", name, "\", 0, 0, (void *) ", value, ", &SWIGTYPE", SwigType_manglestr(type), "}, \n", 0); break; default: - Printf(stderr,"%s : Line %d. Unsupported constant value.\n", input_file, line_number); + Printf(stderr,"%s:%d. Unsupported constant value. %s, %d\n", Getfile(node), Getline(node), name, SwigType_type(type)); return; break; } @@ -1039,76 +1002,63 @@ PYTHON::nativefunction(DOH *node) { * PYTHON::cpp_class_decl() - Register a class definition * ----------------------------------------------------------------------------- */ void -PYTHON::cpp_class_decl(char *name, char *rename, char *type) { - String *stype; - if (shadow) { - stype = NewString(name); - SwigType_add_pointer(stype); - Setattr(hash,stype,rename); - Delete(stype); - /* Add full name of datatype to the hash table */ - if (strlen(type) > 0) { - stype = NewStringf("%s %s", type, name); +PYTHON::cpp_class_decl(DOH *node) { + String *name = Getname(node); + String *rename = Getattr(node,"scriptname"); + String *ctype = Getattr(node,"classtype"); + String *stype; + if (shadow) { + stype = NewString(name); + SwigType_add_pointer(stype); + Setattr(hash,stype,rename); + Delete(stype); + /* Add full name of datatype to the hash table */ + if (Len(ctype) > 0) { + stype = NewStringf("%s %s", ctype, name); SwigType_add_pointer(stype); Setattr(hash,stype,rename); Delete(stype); - } } + } } /* ----------------------------------------------------------------------------- * PYTHON::pragma() * ----------------------------------------------------------------------------- */ void -PYTHON::pragma(char *lang, char *cmd, char *value) { +PYTHON::pragma(DOH *node) { + String *name; + String *value; + name = Getattr(node,"name"); + value = Getattr(node,"value"); - if (strcmp(lang,(char*)"python") == 0) { - if (strcmp(cmd,"CODE") == 0) { - if (shadow) { - Printf(f_shadow,"%s\n",value); - } - } else if (strcmp(cmd,"code") == 0) { - if (shadow) { - Printf(f_shadow,"%s\n",value); - } - } else if (strcmp(cmd,"include") == 0) { - if (shadow) { - if (value) { - FILE *f = Swig_open(value); - if (!f) { - Printf(stderr,"%s : Line %d. Unable to locate file %s\n", input_file, line_number,value); - } else { - char buffer[4096]; - while (fgets(buffer,4095,f)) { - Printv(pragma_include,buffer,0); - } - } - } - } - } else { - Printf(stderr,"%s : Line %d. Unrecognized pragma.\n", input_file, line_number); - } + if (Cmp(name,"code") == 0) { + if (shadow) { + Printf(f_shadow,"%s\n",value); } + } else if (Cmp(name,"include") == 0) { + if (shadow) { + if (value) { + FILE *f = Swig_open(value); + if (!f) { + Printf(stderr,"%s:%d. Unable to locate file %s\n", Getfile(node), Getline(node),value); + } else { + char buffer[4096]; + while (fgets(buffer,4095,f)) { + Printv(pragma_include,buffer,0); + } + } + } + } + } } -struct PyPragma { - String *m_method; - String *m_text; - PyPragma *next; - PyPragma(char *method, char *text) { - m_method = NewString(method); - m_text = NewString(text); - next = 0; - } - ~PyPragma() { - Delete(m_method); - Delete(m_text); - if (next) delete next; - } -}; - -static PyPragma *pragmas = 0; - +#ifdef OLD +/* C++ pragmas are no longer handled as a special case. The generic %pragma + directive may used in or outside of a class. Therefore, this functionality + should be moved into the pragma method, not handled here. -- Dave. (12/14/00). +*/ + /* ----------------------------------------------------------------------------- * PYTHON::cpp_pragma() - Handle C++ pragmas * ----------------------------------------------------------------------------- */ @@ -1176,6 +1126,8 @@ PYTHON::emitAddPragmas(String *output, char* name, char* spacing) { } } +#endif + /* C++ Support + Shadow Classes */ static String *setattr = 0; @@ -1201,9 +1153,13 @@ static int class_renamed = 0; * PYTHON::cpp_open_class() * ----------------------------------------------------------------------------- */ void -PYTHON::cpp_open_class(char *classname, char *rname, char *ctype, int strip) { - this->Language::cpp_open_class(classname, rname, ctype, strip); +PYTHON::cpp_open_class(DOH *node) { + this->Language::cpp_open_class(node); + char *classname = GetChar(node,"name"); + char *rname = GetChar(node,"scriptname"); + char *ctype = GetChar(node,"classtype"); + if (shadow) { /* Create new strings for building up a wrapper function */ setattr = NewString(""); @@ -1233,7 +1189,7 @@ PYTHON::cpp_open_class(char *classname, char *rname, char *ctype, int strip) { real_classname = Swig_copy_string(classname); class_type = Swig_copy_string(ctype); - cpp_class_decl(real_classname,class_name,class_type); + cpp_class_decl(node); if (shadow) { Printv(setattr, @@ -1353,7 +1309,7 @@ PYTHON::cpp_constructor(DOH *node) { else Printv(construct, tab8, "self.this = apply(", module, ".", Swig_name_construct(realname), ",args)\n", 0); Printv(construct, tab8, "self.thisown = 1\n", 0); - emitAddPragmas(construct,(char*)"__init__",(char*)tab8); + // emitAddPragmas(construct,(char*)"__init__",(char*)tab8); have_constructor = 1; } else { /* Hmmm. We seem to be creating a different constructor. We're just going to create a @@ -1394,7 +1350,7 @@ PYTHON::cpp_destructor(DOH *node) { else realname = class_renamed ? class_name : name; Printv(pyclass, tab4, "def __del__(self,", module, "=", module, "):\n", 0); - emitAddPragmas(pyclass,(char*)"__del__",(char*)tab8); + // emitAddPragmas(pyclass,(char*)"__del__",(char*)tab8); Printv(pyclass, tab8, "if self.thisown == 1 :\n", tab8, tab4, module, ".", Swig_name_destroy(realname), "(self)\n", 0); @@ -1452,7 +1408,7 @@ PYTHON::cpp_close_class() { 0); Printv(classes,repr,0); - emitAddPragmas(classes,(char*)"__class__",(char*)tab4); + // emitAddPragmas(classes,(char*)"__class__",(char*)tab4); } /* Now build the real class with a normal constructor */ @@ -1480,24 +1436,26 @@ PYTHON::cpp_close_class() { * PYTHON::cpp_inherit() - Handle inheritance * ----------------------------------------------------------------------------- */ void -PYTHON::cpp_inherit(char **baseclass,int) { +PYTHON::cpp_inherit(List *bases,int) { char *bc; - int i = 0, first_base = 0; + String *base; + int first_base = 0; if (!shadow) { - this->Language::cpp_inherit(baseclass); + this->Language::cpp_inherit(bases); return; } /* We'll inherit variables and constants, but not methods */ - this->Language::cpp_inherit(baseclass, INHERIT_VAR); + this->Language::cpp_inherit(bases, INHERIT_VAR); - if (!baseclass) return; + if (!bases) return; base_class = NewString(""); /* Now tell the Python module that we're inheriting from a base class */ - while (baseclass[i]) { - String *bs = NewString(baseclass[i]); + + for (base = Firstitem(bases); base; base = Nextitem(bases)) { + String *bs = NewString(base); SwigType_add_pointer(bs); bc = GetChar(hash,bs); if (bc) { @@ -1506,7 +1464,6 @@ PYTHON::cpp_inherit(char **baseclass,int) { first_base = 1; } Delete(bs); - i++; } if (!first_base) { Delete(base_class); @@ -1547,7 +1504,7 @@ PYTHON::cpp_variable(DOH *node) { /* Figure out if we've seen this datatype before */ if (is_shadow(t)) inhash = 1; - if (Status & STAT_READONLY) { + if (ReadOnly) { /* *setattr << tab8 << tab4 << "raise RuntimeError, \'Member is read-only\'\n"; */ } else { Printv(csetattr, tab8, "\"", realname, "\" : ", module, ".", Swig_name_set(Swig_name_member(class_name,realname)), ",\n", 0); @@ -1594,9 +1551,13 @@ PYTHON::cpp_constant(DOH *node) { * PYTHON::add_typedef() - Manage typedef's for shadow classes * ----------------------------------------------------------------------------- */ void -PYTHON::add_typedef(SwigType *t, char *name) { +PYTHON::add_typedef(SwigType *t, String *name) { if (!shadow) return; if (is_shadow(t)) { - cpp_class_decl(name,Char(is_shadow(t)),""); + DOH *node = NewHash(); + Setname(node,name); + Setattr(node,"scriptname", is_shadow(t)); + Setattr(node,"classtype",""); + cpp_class_decl(node); } } diff --git a/Source/Modules1.1/python.h b/Source/Modules1.1/python.h index 213ed55fa..d56070dfc 100644 --- a/Source/Modules1.1/python.h +++ b/Source/Modules1.1/python.h @@ -24,41 +24,35 @@ class PYTHON : public Language { protected: void get_pointer(char *src, char *dest, SwigType *t, String *f, char *ret); - - void add_method(char *name, char *function, int kw); + void add_method(String *name, String *function, int kw); char *usage_func(char *, SwigType *, ParmList *); - void emitAddPragmas(String *output, char* name, char* spacing); + public : // Don't change any of this virtual void parse_args(int, char *argv[]); - virtual void parse(); + virtual void initialize(String *); virtual void function(DOH *node); virtual void variable(DOH *node); virtual void constant(DOH *node); virtual void nativefunction(DOH *); - - virtual void initialize(void); virtual void close(void); - virtual void set_module(char *); - - virtual void create_command(char *, char *); - virtual void import(char *); + virtual void create_command(String *, String *); + virtual void import(String *modname); // C++ extensions---for creating shadow classes virtual void cpp_memberfunction(DOH *); virtual void cpp_constructor(DOH *); virtual void cpp_destructor(DOH *); - virtual void cpp_open_class(char *classname, char *rname, char *ctype, int strip); + virtual void cpp_open_class(DOH *); virtual void cpp_close_class(); - virtual void cpp_inherit(char **baseclass, int mode = INHERIT_ALL); + virtual void cpp_inherit(List *bases, int mode = INHERIT_ALL); virtual void cpp_variable(DOH *); virtual void cpp_constant(DOH *); - virtual void cpp_class_decl(char *, char *,char *); - virtual void pragma(char *, char *, char *); - virtual void cpp_pragma(Pragma *); - virtual void add_typedef(SwigType *t, char *name); + virtual void cpp_class_decl(DOH *); + virtual void pragma(DOH *node); + virtual void add_typedef(SwigType *t, String *name); }; #define PYSHADOW_MEMBER 0x2 diff --git a/Source/Modules1.1/ruby.cxx b/Source/Modules1.1/ruby.cxx index b9586a3a4..9b9af1d42 100644 --- a/Source/Modules1.1/ruby.cxx +++ b/Source/Modules1.1/ruby.cxx @@ -12,7 +12,7 @@ static char cvsroot[] = "$Header$"; -#include "mod11.h" +#include "swig11.h" #include "ruby.h" #include @@ -133,16 +133,7 @@ void RUBY::parse_args(int argc, char *argv[]) { /* Look for certain command line options */ for (int i = 1; i < argc; i++) { if (argv[i]) { - if (strcmp(argv[i],"-module") == 0) { - if (argv[i+1]) { - set_module(argv[i+1]); - Swig_mark_arg(i); - Swig_mark_arg(i+1); - i++; - } else { - Swig_arg_error(); - } - } else if (strcmp(argv[i],"-feature") == 0) { + if (strcmp(argv[i],"-feature") == 0) { if (argv[i+1]) { char *name = argv[i+1]; feature = new char [strlen(name)+1]; @@ -163,10 +154,6 @@ void RUBY::parse_args(int argc, char *argv[]) { /* Add a symbol to the parser for conditional compilation */ Preprocessor_define((void *) "SWIGRUBY", 0); - - /* Add typemap definitions */ - typemap_lang = (char*)"ruby"; - SWIG_config_file("ruby.i"); } @@ -182,12 +169,13 @@ static void insert_file(char *filename, File *file) { } /* --------------------------------------------------------------------- - * RUBY::parse() + * RUBY::initialize() * - * Start parsing an interface file. + * Produces an initialization function. Assumes that the module + * name has already been specified. * --------------------------------------------------------------------- */ -void RUBY::parse() { +void RUBY::initialize(String *modname) { import_file = 0; current = NO_CPP; klass = 0; @@ -248,56 +236,26 @@ void RUBY::parse() { SwigType_setbase(value,(char*)"void"); SwigType_add_pointer(value); SwigType_typedef(value,(char*)"VALUE"); - - yyparse(); /* Run the SWIG parser */ Delete(value); -} -/* --------------------------------------------------------------------- - * RUBY::set_module(char *mod_name) - * - * Sets the module name. Does nothing if it's already set (so it can - * be overridden as a command line option). - *---------------------------------------------------------------------- */ + /* Old set_module code */ - -void RUBY::set_module(char *mod_name) { - if (import_file) { - Printf(f_init, "%srb_f_require(Qnil, rb_str_new2(\"%s\"));\n", tab4, mod_name); - free(import_file); /* Note: was allocated from C */ - import_file = 0; + if (!module) { + module = new char[Len(modname)+1]; + strcpy(module, Char(modname)); } - - if (module) return; - if (!feature) { - feature = new char[strlen(mod_name)+1]; - strcpy(feature, mod_name); + feature = new char[strlen(module)+1]; + strcpy(feature, module); } - - module = new char[strlen(mod_name)+1]; - strcpy(module, mod_name); /* module name must be a constant. */ module[0] = toupper(module[0]); modvar = new char[1+strlen(module)+1]; modvar[0] = 'm'; strcpy(modvar+1, module); -} - -/* --------------------------------------------------------------------- - * RUBY::initialize(void) - * - * Produces an initialization function. Assumes that the module - * name has already been specified. - * --------------------------------------------------------------------- */ - -void RUBY::initialize() { - if (!module) { - Printf(stderr,"SWIG : *** Warning. No module name specified.\n"); - set_module((char*)"swig"); /* Pick a default name */ - } + /* Old init code */ Printf(f_header,"#define SWIG_init Init_%s\n", feature); Printf(f_header,"#define SWIG_name \"%s\"\n\n", module); @@ -348,7 +306,7 @@ void RUBY::close(void) { * -------------------------------------------------------------------------- */ void RUBY::nativefunction(DOH *node) { - Printf(stderr,"%s : Line %d. Adding native function %s not supported (ignored).\n", input_file, line_number, Getattr(node,"scriptname")); + Printf(stderr,"%s:%d. Adding native function %s not supported (ignored).\n", Getfile(node), Getline(node), Getattr(node,"scriptname")); } /* --------------------------------------------------------------------- @@ -374,7 +332,13 @@ String *RUBY::make_wrapper_name(char *iname) { * argc = Number of arguments * --------------------------------------------------------------------- */ -void RUBY::create_command(char *cname, char *iname, int argc) { +static int create_argc = 0; + +void RUBY::create_command(String *scname, String *siname) { + char *cname, *iname; + int argc = create_argc; + cname = Char(scname); + iname = Char(siname); String *wname = make_wrapper_name(iname); if (CPlusPlus) { Insert(wname,0,"VALUEFUNC("); @@ -516,7 +480,6 @@ void RUBY::function(DOH *node) { } } Printf(f,") {\n"); - Printf(f,"$locals\n"); /* Emit all of the local variables for holding arguments. */ if (vararg) { @@ -582,8 +545,8 @@ void RUBY::function(DOH *node) { Replace(f, "$arg", source, DOH_REPLACE_ANY); Delete(s); } else { - Printf(stderr,"%s : Line %d. No typemapping for datatype %s\n", - input_file,line_number, SwigType_str(pt,0)); + Printf(stderr,"%s:%d. No typemapping for datatype %s\n", + Getfile(node), Getline(node), SwigType_str(pt,0)); } if (j >= (pcount-numopt)) Printv(f, tab4, "} \n", 0); @@ -634,8 +597,8 @@ void RUBY::function(DOH *node) { Printv(f, s, 0); Delete(s); } else { - Printf(stderr,"%s : Line %d. No return typemap for datatype %s\n", - input_file,line_number,SwigType_str(t,0)); + Printf(stderr,"%s:%d. No return typemap for datatype %s\n", + Getfile(node), Getline(node),SwigType_str(t,0)); } } } @@ -683,7 +646,8 @@ void RUBY::function(DOH *node) { Printf(f_wrappers,"%s", f); /* Now register the function with the language */ - create_command(name, iname, (vararg ? -1 : numarg)); + create_argc = vararg ? -1 : numarg; + create_command(name, iname); Delete(cleanup); Delete(outarg); Delete(f); @@ -717,7 +681,6 @@ void RUBY::variable(DOH *node) { Printv(getf, "static VALUE\n", getfname, "(", 0); Printf(getf, "VALUE self"); Printf(getf, ") {\n"); - Printf(getf, "$locals\n"); Wrapper_add_local(getf,"_val","VALUE _val"); if (SwigType_type(t) == T_USER) { @@ -737,13 +700,13 @@ void RUBY::variable(DOH *node) { Printv(getf,s, 0); Delete(s); } else { - Printf(stderr,"%s: Line %d. Unable to link with variable type %s\n", - input_file,line_number,SwigType_str(t,0)); + Printf(stderr,"%s:%d. Unable to link with variable type %s\n", + Getfile(node), Getline(node),SwigType_str(t,0)); } Printv(getf, tab4, "return _val;\n}\n", 0); Printf(f_wrappers,"%s", getf); - if (Status & STAT_READONLY) { + if (ReadOnly) { setfname = NewString("NULL"); } else { /* create setter */ @@ -753,7 +716,6 @@ void RUBY::variable(DOH *node) { Replace(setfname,"::", "_", DOH_REPLACE_ANY); /* FIXME: Swig_name_get bug? */ Printv(setf, "static VALUE\n", setfname, "(VALUE self, ", 0); Printf(setf, "VALUE _val) {\n"); - Printf(setf, "$locals\n"); if (SwigType_type(t) == T_USER) { SwigType_add_pointer(t); @@ -772,8 +734,8 @@ void RUBY::variable(DOH *node) { Printv(setf,s,0); Delete(s); } else { - Printf(stderr,"%s: Line %d. Unable to link with variable type %s\n", - input_file,line_number,SwigType_str(t,0)); + Printf(stderr,"%s:%d. Unable to link with variable type %s\n", + Getfile(node), Getline(node),SwigType_str(t,0)); } if (SwigType_type(t) == T_USER) { Printv(setf, name, " = *temp;\n",0); @@ -799,7 +761,7 @@ void RUBY::variable(DOH *node) { tab4, "rb_define_singleton_method(", klass->vname, ", \"", klass->strip(iname), "\", ", getfname, ", 0);\n", 0); - if (!(Status & STAT_READONLY)) { + if (!ReadOnly) { Printv(s, tab4, "rb_define_singleton_method(", klass->vname, ", \"", klass->strip(iname), "=\", ", setfname, ", 1);\n", @@ -814,7 +776,7 @@ void RUBY::variable(DOH *node) { tab4, "rb_define_singleton_method(", modvar, ", \"", iname, "\", ", getfname, ", 0);\n", 0); - if (!(Status & STAT_READONLY)) { + if (!ReadOnly) { Printv(s, tab4, "rb_define_singleton_method(", modvar, ", \"", iname, "=\", ", setfname, ", 1);\n", @@ -846,13 +808,14 @@ char *RUBY::validate_const_name(char *name) { if (islower(name[0])) { name[0] = toupper(name[0]); - Printf(stderr,"%s : Line %d. Wrong constant/class/module name " - "(corrected to `%s')\n", input_file, line_number, name); + /* Printf(stderr,"%s:%d. Wrong constant/class/module name " + "(corrected to `%s')\n", Getfile(node), Getline(node), name); */ + + Printf(stderr,"?:?. Wrong constant/class/module name (corrected to '%s')\n", name); return name; } - Printf(stderr,"%s : Line %d. Wrong constant/class/module name\n", - input_file, line_number); + Printf(stderr,"?:?. Wrong constant/class/module name\n"); return name; } @@ -889,8 +852,8 @@ void RUBY::constant(DOH *node) { } Delete(str); } else { - Printf(stderr,"%s : Line %d. Unable to create constant %s = %s\n", - input_file, line_number, SwigType_str(type,0), value); + Printf(stderr,"%s:%d. Unable to create constant %s = %s\n", + Getfile(node), Getline(node), SwigType_str(type,0), value); } } @@ -1169,8 +1132,13 @@ int RUBY::from_VALUE(SwigType *type, char *value, char *target, String *str) { * * ---------------------------------------------------------------------- */ -void RUBY::cpp_open_class(char *cname, char *rename, char *ctype, int strip) { - this->Language::cpp_open_class(cname, rename, ctype, strip); +void RUBY::cpp_open_class(DOH *node) { + this->Language::cpp_open_class(node); + + char *cname = GetChar(node,"name"); + char *rename = GetChar(node,"scriptname"); + char *ctype = GetChar(node,"classtype"); + int strip = GetInt(node,"strip"); klass = RCLASS(classes, cname); @@ -1252,12 +1220,11 @@ void RUBY::cpp_close_class() { * --------------------------------------------------------------------- */ -void RUBY::cpp_inherit(char **baseclass, int mode) { - if (!baseclass) - return; - - for (int i = 0; baseclass[i]; i++) { - RClass *super = RCLASS(classes, baseclass[i]); +void RUBY::cpp_inherit(List *bases, int mode) { + if (!bases) return; + String *base; + for (base = Firstitem(bases); base; base = Nextitem(bases)) { + RClass *super = RCLASS(classes, Char(base)); if (super) { Replace(klass->init,"$super", super->vname, DOH_REPLACE_ANY); break; /* ignore multiple inheritance */ @@ -1446,14 +1413,17 @@ void RUBY::cpp_staticvariable(DOH *node) { * A forward class declaration * ----------------------------------------------------------------------- */ -void RUBY::cpp_class_decl(char *cname, char *rename, char *type) { +void RUBY::cpp_class_decl(DOH *node) { + String *cname = Getattr(node,"name"); + String *rename = Getattr(node,"scriptname"); + String *ctype = Getattr(node,"classtype"); String *valid_name = NewString((rename ? rename : cname)); validate_const_name(Char(valid_name)); - klass->set_name(cname, rename, Char(valid_name)); - SET_RCLASS(classes, cname, klass); - if (type && strlen(type) > 0) { + klass->set_name(Char(cname), Char(rename), Char(valid_name)); + SET_RCLASS(classes, Char(cname), klass); + if (ctype && Len(ctype) > 0) { char temp[256]; - sprintf(temp,"%s %s", type, cname); + sprintf(temp,"%s %s", Char(ctype), Char(cname)); SET_RCLASS(classes, temp, klass); } /* @@ -1471,65 +1441,47 @@ void RUBY::cpp_class_decl(char *cname, char *rename, char *type) { * A pragma declaration * -------------------------------------------------------------------- */ -void RUBY::pragma(char *lang, char *cmd, char *value) { - if (strcmp(lang, "ruby") != 0) - return; - - if (strcmp(cmd, "free") == 0) { +void RUBY::pragma(DOH *node) { + String *cmd = Getattr(node,"name"); + String *value = Getattr(node,"value"); + if (Cmp(cmd, "free") == 0) { char name[64]; - if (sscanf(value, " %s ", name) != 1) { - Printf(stderr, "%s : Line %d. Invalid free pragma.\n", - input_file, line_number); + if (sscanf(Char(value), " %s ", name) != 1) { + Printf(stderr, "%s:%d. Invalid free pragma.\n", + Getfile(node), Getline(node)); return; } Setattr(klass->freemethods, name, name); - } else if (strcmp(cmd, "include") == 0) { + } else if (Cmp(cmd, "include") == 0) { char name[64]; - if (sscanf(value, " %s ", name) != 1) { - Printf(stderr, "%s : Line %d. Invalid include pragma.\n", - input_file, line_number); + if (sscanf(Char(value), " %s ", name) != 1) { + Printf(stderr, "%s:%d. Invalid include pragma.\n", + Getfile(node), Getline(node)); return; } Printv(klass->includes,tab4, "rb_include_module($class, ", "rb_eval_string(\"", name, "\"));\n", 0); - } else if (strcmp(cmd, "alias") == 0) { + } else if (Cmp(cmd, "alias") == 0) { char alias[64], name[64]; - if (sscanf(value, " %s %s ", alias, name) != 2) { - Printf(stderr, "%s : Line %d. Invalid alias pragma.\n", - input_file, line_number); + if (sscanf(Char(value), " %s %s ", alias, name) != 2) { + Printf(stderr, "%s:%d. Invalid alias pragma.\n", + Getfile(node), Getline(node)); return; } Printv(klass->aliases, tab4, "rb_define_alias($class, ", "\"", alias, "\", \"", name, "\");\n", 0); - } else if (strcmp(cmd, "pred") == 0) { + } else if (Cmp(cmd, "pred") == 0) { char *tok; - tok = strtok(value, " \t"); + tok = strtok(Char(value), " \t"); while (tok) { Setattr(klass->predmethods, tok, tok); tok = strtok(0, " \t"); } - } else if (strcmp(cmd, "debug") == 0) { + } else if (Cmp(cmd, "debug") == 0) { Printf(f_header, "/* %s */\n", value); Printf(f_wrappers, "/* %s */\n", value); Printf(f_init, "/* %s */\n", value); Printf(stderr, "%s\n", value); - } else { - Printf(stderr, "%s : Line %d. Unrecognized pragma.\n", - input_file, line_number); - } -} - -/* ----------------------------------------------------------------------------- - * RUBY::cpp_pragma(Pragma *plist) - * - * Handle C++ pragmas - * ----------------------------------------------------------------------------- */ - - -void RUBY::cpp_pragma(Pragma *plist) { - while (plist) { - pragma(Char(plist->lang), Char(plist->name), Char(plist->value)); - plist = plist->next; } } @@ -1540,9 +1492,8 @@ void RUBY::cpp_pragma(Pragma *plist) { *---------------------------------------------------------------------- */ -void RUBY::import(char *filename) { - if (import_file) free(import_file); - import_file = Swig_copy_string(filename); +void RUBY::import(String *modname) { + Printf(f_init, "rb_f_require(Qnil, rb_str_new2(\"%s\"));\n", modname); } /* @@ -1550,3 +1501,8 @@ void RUBY::import(char *filename) { * c-basic-offset: 2 * End: */ + + + + + diff --git a/Source/Modules1.1/ruby.h b/Source/Modules1.1/ruby.h index 83908ea81..bd1b026df 100644 --- a/Source/Modules1.1/ruby.h +++ b/Source/Modules1.1/ruby.h @@ -20,35 +20,32 @@ class RUBY : public Language { public: /* Virtual functions required by the SWIG parser */ virtual void parse_args(int, char *argv[]); - virtual void parse(); + virtual void initialize(String *module); virtual void function(DOH *node); virtual void variable(DOH *node); virtual void constant(DOH *node); - virtual void initialize(void); virtual void close(void); - virtual void set_module(char *); virtual void nativefunction(DOH *); - virtual void create_command(char *, char *, int); - virtual void import(char *); + virtual void create_command(String *, String *); + virtual void import(String *); /* C++ language extensions. */ virtual void cpp_memberfunction(DOH *); virtual void cpp_constructor(DOH *); virtual void cpp_destructor(DOH *); - virtual void cpp_open_class(char *classname, char *rname, char *ctype, int strip); + virtual void cpp_open_class(DOH *); virtual void cpp_close_class(); - virtual void cpp_inherit(char **baseclass, int mode = INHERIT_ALL); + virtual void cpp_inherit(List *bases, int mode = INHERIT_ALL); virtual void cpp_variable(DOH *); virtual void cpp_staticfunction(DOH *); virtual void cpp_constant(DOH *); virtual void cpp_staticvariable(DOH *); /* Declaration of a class, but not a full definition */ - virtual void cpp_class_decl(char *, char *, char *); + virtual void cpp_class_decl(DOH *); /* Pragma directive */ - virtual void pragma(char *, char *, char *); - virtual void cpp_pragma(Pragma *); + virtual void pragma(DOH *node); }; /* diff --git a/Source/Modules1.1/swig11.h b/Source/Modules1.1/swig11.h new file mode 100644 index 000000000..522be91c9 --- /dev/null +++ b/Source/Modules1.1/swig11.h @@ -0,0 +1,124 @@ +/* ----------------------------------------------------------------------------- + * swig11.h + * + * Main header file for the SWIG1.1 core. + * + * 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. + * + * $Header$ + * ----------------------------------------------------------------------------- */ + +#include +#include +#include + +#include "swigver.h" + +extern "C" { +#include "swig.h" +} + +/* Global variables. */ + +extern FILE *f_runtime; // Runtime code +extern DOH *f_header; // Headers +extern DOH *f_wrappers; // Wrappers +extern DOH *f_init; // Initialization code +extern FILE *f_input; +extern char LibDir[512]; // Library directory +extern int CPlusPlus; // C++ mode +extern int AddMethods; // AddMethods mode +extern int NewObject; // NewObject mode +extern int NoInclude; // NoInclude flag +extern char output_dir[512]; // Output directory +extern int Verbose; +extern int ReadOnly; // Read only mode +extern int Native; // Native mode + +/* Miscellaneous stuff */ + +#define tab4 " " +#define tab8 " " + +// Modes for different types of inheritance + +#define INHERIT_FUNC 0x1 +#define INHERIT_VAR 0x2 +#define INHERIT_CONST 0x4 +#define INHERIT_ALL (INHERIT_FUNC | INHERIT_VAR | INHERIT_CONST) + +/* Language Class */ +class Language { +public: + virtual void parse_args(int argc, char *argv[]) = 0; + virtual void initialize(String *modname) = 0; + virtual void close(void) = 0; + virtual void import(String *modname); + + /* Basic function, variable, constant API (required) */ + virtual void function(DOH *node) = 0; + virtual void variable(DOH *node) = 0; + virtual void constant(DOH *node) = 0; + virtual void nativefunction(DOH *node); + virtual void create_command(String *cname, String *iname); + + /* Optional C++ handling */ + virtual void cpp_open_class(DOH *node); + virtual void cpp_memberfunction(DOH *node); + virtual void cpp_constructor(DOH *node); + virtual void cpp_destructor(DOH *node); + virtual void cpp_variable(DOH *node); + virtual void cpp_staticfunction(DOH *node); + virtual void cpp_constant(DOH *node); + virtual void cpp_staticvariable(DOH *node); + virtual void cpp_close_class(); + + virtual void cpp_class_decl(DOH *node); + virtual void cpp_inherit(List *bases, int mode = INHERIT_ALL); + + /* Miscellaneous features */ + + virtual void add_typedef(SwigType *t, String *name); + virtual void pragma(DOH *node); + +}; + +/* Emit functions */ + +extern void new_create_function(char *, char *, SwigType *, ParmList *); +extern void emit_set_get(DOH *node); +extern void emit_set_action(DOHString_or_char *decl); + +extern int SWIG_main(int, char **, Language *); + +/* These are in the new core */ +extern "C" void *Preprocessor_define(void *, int); + +// Misc + +extern int emit_args(DOH *node, Wrapper *f); +extern void emit_func_call(DOH *node, Wrapper *f); +extern void SWIG_exit(int); /* use EXIT_{SUCCESS,FAILURE} */ +extern int check_numopt(ParmList *); +extern void SWIG_config_file(const String_or_char *); + +/* C++ utility functions */ +extern int cplus_check_abstract(DOH *node); + +extern Language *lang; + +/* swig11.h ends here */ + + + + + + + + diff --git a/Source/Modules1.1/swigmain.cxx b/Source/Modules1.1/swigmain.cxx index d4d977260..3c4e30af2 100644 --- a/Source/Modules1.1/swigmain.cxx +++ b/Source/Modules1.1/swigmain.cxx @@ -27,7 +27,7 @@ static char cvsroot[] = "$Header$"; #ifndef MACSWIG #include "swigconfig.h" #endif -#include "mod11.h" +#include "swig11.h" #include "tcl8.h" #include "python.h" diff --git a/Source/Modules1.1/tcl8.cxx b/Source/Modules1.1/tcl8.cxx index 7b536aa5e..9615c8a4c 100644 --- a/Source/Modules1.1/tcl8.cxx +++ b/Source/Modules1.1/tcl8.cxx @@ -11,7 +11,7 @@ static char cvsroot[] = "$Header$"; -#include "mod11.h" +#include "swig11.h" #include "tcl8.h" #include @@ -61,13 +61,6 @@ TCL8::parse_args(int argc, char *argv[]) { Swig_mark_arg(i+1); i++; } else Swig_arg_error(); - } else if (strcmp(argv[i],"-module") == 0) { - if (argv[i+1]) { - set_module(argv[i+1]); - Swig_mark_arg(i); - Swig_mark_arg(i+1); - i++; - } else Swig_arg_error(); } else if (strcmp(argv[i],"-namespace") == 0) { nspace = 1; Swig_mark_arg(i); @@ -91,16 +84,15 @@ TCL8::parse_args(int argc, char *argv[]) { Preprocessor_define((void *) "SWIGTCL 1",0); Preprocessor_define((void *) "SWIGTCL8 1", 0); - typemap_lang = (char*)"tcl8"; SWIG_config_file("tcl8.swg"); } /* ----------------------------------------------------------------------------- - * TCL8::parse() + * TCL8::initialize() * ----------------------------------------------------------------------------- */ void -TCL8::parse() { +TCL8::initialize(String *modname) { mod_init = NewString(""); cmd_info = NewString(""); @@ -115,7 +107,7 @@ TCL8::parse() { if (NoInclude) { Printf(f_runtime,"#define SWIG_NOINCLUDE\n"); } - /* if (Swig_insert_file("common.swg",f_runtime) == -1) { + if (Swig_insert_file("common.swg",f_runtime) == -1) { Printf(stderr,"SWIG : Fatal error. Unable to locate 'common.swg' in SWIG library.\n"); SWIG_exit (EXIT_FAILURE); } @@ -123,24 +115,14 @@ TCL8::parse() { Printf(stderr,"SWIG : Fatal error. Unable to locate 'swigtcl8.swg' in SWIG library.\n"); SWIG_exit (EXIT_FAILURE); } - */ - yyparse(); -} - -/* ----------------------------------------------------------------------------- - * TCL8::set_module() - * ----------------------------------------------------------------------------- */ - -void -TCL8::set_module(char *mod_name) { - char *c; - - if (module) return; - module = NewString(mod_name); + if (!module) module = NewString(modname); /* Fix capitalization for Tcl */ - for (c = Char(module); *c; c++) *c = (char) tolower(*c); - + char *c = Char(module); + while (c && *c) { + *c = (char) tolower(*c); + c++; + } /* Now create an initialization function */ init_name = NewStringf("%s_Init",module); c = Char(init_name); @@ -154,23 +136,7 @@ TCL8::set_module(char *mod_name) { } else { prefix = NewString(""); } -} -/* ----------------------------------------------------------------------------- - * TCL8::initialize() - * ----------------------------------------------------------------------------- */ - -void -TCL8::initialize() { - - if ((!ns_name) && (nspace)) { - Printf(stderr,"Tcl error. Must specify a namespace.\n"); - SWIG_exit (EXIT_FAILURE); - } - if (!init_name) { - Printf(stderr,"*** Error. No module name specified.\n"); - SWIG_exit (EXIT_FAILURE); - } Printf(f_header,"#define SWIG_init %s\n", init_name); if (!module) module = NewString("swig"); Printf(f_header,"#define SWIG_name \"%s\"\n", module); @@ -248,7 +214,7 @@ TCL8::close(void) { * ----------------------------------------------------------------------------- */ void -TCL8::create_command(char *cname, char *iname) { +TCL8::create_command(String *cname, String *iname) { String *wname = Swig_name_wrapper(cname); Printv(cmd_info, tab4, "{ SWIG_prefix \"", iname, "\", ", wname, ", NULL},\n", 0); @@ -290,7 +256,6 @@ TCL8::function(DOH *node) { Printv(f, "static int\n ", Swig_name_wrapper(iname), "(ClientData clientData, Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[]) {\n", 0); - Printf(f,"$locals\n"); /* Print out variables for storing arguments. */ pcount = emit_args(node, f); @@ -404,8 +369,8 @@ TCL8::function(DOH *node) { break; } default : - Printf(stderr,"%s : Line %d: Unable to use type %s as a function argument.\n", - input_file, line_number, SwigType_str(pt,0)); + Printf(stderr,"%s:%d: Unable to use type %s as a function argument.\n", + Getfile(node), Getline(node), SwigType_str(pt,0)); break; } } @@ -496,8 +461,8 @@ TCL8::function(DOH *node) { break; default : - Printf(stderr,"%s : Line %d: Unable to use return type %s in function %s.\n", - input_file, line_number, SwigType_str(d,0), name); + Printf(stderr,"%s:%d. Unable to use return type %s in function %s.\n", + Getfile(node), Getline(node), SwigType_str(d,0), name); break; } } @@ -567,11 +532,11 @@ TCL8::variable(DOH *node) { /* See if there were any typemaps */ if (Swig_typemap_search((char *)"varin",t,name) || (Swig_typemap_search((char*)"varout",t,name))) { - Printf(stderr,"%s : Line %d. Warning. varin/varout typemap methods not supported.", - input_file, line_number); + Printf(stderr,"%s:%d. Warning. varin/varout typemap methods not supported.", + Getfile(node), Getline(node)); } - if (Status & STAT_READONLY) readonly = 1; + if (ReadOnly) readonly = 1; isarray = SwigType_isarray(t); tc = SwigType_type(t); setname = Getattr(setf,t); @@ -586,9 +551,7 @@ TCL8::variable(DOH *node) { get = NewWrapper(); set = NewWrapper(); Printv(set, "static char *", setname, "(ClientData clientData, Tcl_Interp *interp, char *name1, char *name2, int flags) {\n",0); - Printf(set,"$locals\n"); Printv(get, "static char *", getname, "(ClientData clientData, Tcl_Interp *interp, char *name1, char *name2, int flags) {\n",0); - Printf(get,"$locals\n"); SwigType *lt = Swig_clocal_type(t); if ((tc != T_USER) && (!isarray)) @@ -698,11 +661,11 @@ TCL8::variable(DOH *node) { if (dim && Len(dim)) { Printf(set, "strncpy(addr,value,%s);\n", dim); setable = 1; - readonly = Status & STAT_READONLY; + readonly = ReadOnly; } Printv(get, "Tcl_SetVar2(interp,name1,name2,addr, flags);\n",0); } else { - Printf(stderr,"%s:%d: Array variable '%s' will be read-only.\n", input_file, line_number, name); + Printf(stderr,"%s:%d: Array variable '%s' will be read-only.\n", Getfile(node), Getline(node), name); Wrapper_add_local(get,"value","Tcl_Obj *value"); SwigType_remember(t); Printv(get, @@ -760,7 +723,6 @@ TCL8::variable(DOH *node) { if (!readonlywrap) { Wrapper *ro = NewWrapper(); Printf(ro, "static char *swig_readonly(ClientData clientData, Tcl_Interp *interp, char *name1, char *name2, int flags) {\n"); - Printf(ro,"$locals\n"); Printv(ro, "return \"Variable is read-only\";\n", "}\n", 0); Printf(f_wrappers,"%s",ro); readonlywrap = 1; @@ -781,12 +743,12 @@ TCL8::constant(DOH *node) { char *name; SwigType *type; char *value; - int OldStatus = Status; + int OldStatus = ReadOnly; SwigType *t; char var_name[256]; char *tm; String *rvalue; - Status = STAT_READONLY; + ReadOnly = 1; DOH *nnode; name = GetChar(node,"name"); @@ -898,13 +860,13 @@ TCL8::constant(DOH *node) { break; default: - Printf(stderr,"%s : Line %d. Unsupported constant value.\n", input_file, line_number); + Printf(stderr,"%s:%d. Unsupported constant value.\n", Getfile(node), Getline(node)); break; } } Delete(rvalue); Delete(nnode); - Status = OldStatus; + ReadOnly = OldStatus; } /* ----------------------------------------------------------------------------- @@ -972,8 +934,14 @@ TCL8::nativefunction(DOH *node) { * ----------------------------------------------------------------------------- */ void -TCL8::cpp_open_class(char *classname, char *rename, char *ctype, int strip) { - this->Language::cpp_open_class(classname,rename,ctype,strip); +TCL8::cpp_open_class(DOH *node) { + this->Language::cpp_open_class(node); + + char *classname = GetChar(node,"name"); + char *rename = GetChar(node,"scriptname"); + char *ctype = GetChar(node,"classtype"); + int strip = GetInt(node,"strip"); + if (shadow) { static int included_object = 0; if (!included_object) { @@ -1093,7 +1061,7 @@ void TCL8::cpp_variable(DOH *node) { if (!rname) rname = Swig_name_wrapper(temp); Printv(attributes, rname, ", ", 0); - if (!(Status & STAT_READONLY)) { + if (!ReadOnly) { strcpy(temp, Char(Swig_name_set(Swig_name_member(class_name,realname)))); rname = Getattr(repeatcmd,temp); if (!rname) rname = Swig_name_wrapper(temp); diff --git a/Source/Modules1.1/tcl8.h b/Source/Modules1.1/tcl8.h index 76a6e6161..8e5701c79 100644 --- a/Source/Modules1.1/tcl8.h +++ b/Source/Modules1.1/tcl8.h @@ -26,19 +26,17 @@ private: public : virtual void parse_args(int, char *argv[]); - virtual void parse(); + virtual void initialize(String *); virtual void function(DOH *node); virtual void variable(DOH *node); virtual void constant(DOH *node); - virtual void initialize(void); virtual void close(void); - virtual void set_module(char *); virtual void nativefunction(DOH *); - virtual void create_command(char *, char *); + virtual void create_command(String *, String *); // Stubs for processing C++ classes in Tcl - virtual void cpp_open_class(char *classname, char *rename, char *ctype, int strip); + virtual void cpp_open_class(DOH *node); virtual void cpp_close_class(); virtual void cpp_memberfunction(DOH *); virtual void cpp_variable(DOH *);