/branches/closed/matlab-branch/Source/Modules/allegrocl.cxx
# · C++ · 2053 lines · 1528 code · 278 blank · 247 comment · 422 complexity · 24048bb528dceab98507415ed6250852 MD5 · raw file
Large files are truncated click here to view the full file
- /* -----------------------------------------------------------------------------
- * See the LICENSE file for information on copyright, usage and redistribution
- * of SWIG, and the README file for authors - http://www.swig.org/release.html.
- *
- * allegrocl.cxx
- *
- * ALLEGROCL language module for SWIG.
- * ----------------------------------------------------------------------------- */
- char cvsroot_allegrocl_cxx[] = "$Id: allegrocl.cxx 10453 2008-05-15 21:18:44Z wsfulton $";
- #include "swigmod.h"
- #include "cparse.h"
- #include <ctype.h>
- // #define ALLEGROCL_DEBUG
- // #define ALLEGROCL_WRAP_DEBUG
- // #define ALLEGROCL_TYPE_DEBUG
- // #define ALLEGROCL_CLASS_DEBUG
- static File *f_cl = 0;
- String *f_clhead = NewString("");
- String *f_clwrap = NewString("(swig-in-package ())\n\n");
- static File *f_cxx;
- static File *f_cxx_header = 0;
- static File *f_cxx_wrapper = 0;
- static String *module_name = 0;
- static String *swig_package = 0;
- const char *identifier_converter = "identifier-convert-null";
- static bool CWrap = true; // generate wrapper file for C code by default. most correct.
- static bool Generate_Wrapper = false;
- static bool unique_swig_package = false;
- static String *current_namespace = NewString("");
- static String *current_package = NewString("");
- static Hash *defined_namespace_packages = NewHash();
- static Node *in_class = 0;
- static Node *first_linked_type = 0;
- static Hash *defined_foreign_types = NewHash();
- static Hash *defined_foreign_ltypes = NewHash();
- static String *anon_type_name = NewString("anontype");
- static int anon_type_count = 0;
- // stub
- String *convert_literal(String *num_param, String *type, bool try_to_split = true);
- class ALLEGROCL:public Language {
- public:
- virtual void main(int argc, char *argv[]);
- virtual int top(Node *n);
- virtual int functionWrapper(Node *n);
- virtual int namespaceDeclaration(Node *n);
- virtual int constructorHandler(Node *n);
- virtual int destructorHandler(Node *n);
- virtual int globalvariableHandler(Node *n);
- virtual int variableWrapper(Node *n);
- virtual int constantWrapper(Node *n);
- virtual int memberfunctionHandler(Node *n);
- virtual int membervariableHandler(Node *n);
- virtual int classHandler(Node *n);
- virtual int emit_one(Node *n);
- virtual int enumDeclaration(Node *n);
- virtual int enumvalueDeclaration(Node *n);
- virtual int typedefHandler(Node *n);
- virtual int classforwardDeclaration(Node *n);
- virtual int templateDeclaration(Node *n);
- virtual int validIdentifier(String *s);
- private:
- int emit_defun(Node *n, File *f_cl);
- int emit_dispatch_defun(Node *n);
- int emit_buffered_defuns(Node *n);
- int cClassHandler(Node *n);
- int cppClassHandler(Node *n);
- };
- static ALLEGROCL *allegrocl = 0;
- static String *trim(String *str) {
- char *c = Char(str);
- while (*c != '\0' && isspace((int) *c))
- ++c;
- String *result = NewString(c);
- Chop(result);
- return result;
- }
- int is_integer(String *s) {
- char *c = Char(s);
- if (c[0] == '#' && (c[1] == 'x' || c[1] == 'o'))
- c += 2;
- while (*c) {
- if (!isdigit(*c))
- return 0;
- c++;
- }
- return 1;
- }
- String *class_from_class_or_class_ref(String *type) {
- SwigType *stripped = SwigType_strip_qualifiers(type);
- if (SwigType_isclass(stripped))
- return stripped;
- if (SwigType_ispointer(stripped) || SwigType_isreference(stripped)) {
- // Printf(stderr,"It is a pointer/reference. Is it a class?\n");
- SwigType_pop(stripped);
- if (SwigType_isclass(stripped)) {
- return stripped;
- }
- }
- return 0;
- }
- String *lookup_defined_foreign_type(String *k) {
- #ifdef ALLEGROCL_TYPE_DEBUG
- Printf(stderr, "Looking up defined type '%s'.\n Found: '%s'\n", k, Getattr(defined_foreign_types, k));
- #endif
- return Getattr(defined_foreign_types, k);
- }
- String *listify_namespace(String *namespaze) {
- if (Len(namespaze) == 0)
- return NewString("()");
- String *result = NewStringf("(\"%s\")", namespaze);
- Replaceall(result, "::", "\" \"");
- return result;
- }
- String *namespaced_name(Node *n, String *ns = current_namespace) {
- return NewStringf("%s%s%s", ns, (Len(ns) != 0) ? "::" : "", Getattr(n, "sym:name"));
- }
- // "Namespace::Nested::Class2::Baz" -> "Baz"
- static String *strip_namespaces(String *str) {
- char *result = Char(str);
- String *stripped_one;
- while ((stripped_one = Strstr(result, "::")))
- result = Char(stripped_one) + 2;
- return NewString(result);
- }
- static String *namespace_of(String *str) {
- char *p = Char(str);
- char *start = Char(str);
- char *result = 0;
- String *stripped_one;
- while ((stripped_one = Strstr(p, "::"))) {
- p = Char(stripped_one) + 2;
- }
- if (p > start) {
- int len = p - start - 1;
- result = (char *) malloc(len);
- strncpy(result, start, len - 1);
- result[len - 1] = 0;
- }
- return Char(result);
- }
- void add_linked_type(Node *n) {
- #ifdef ALLEGROCL_CLASS_DEBUG
- Printf(stderr, "Adding linked node of type: %s(%s) %s(%x)\n\n", nodeType(n), Getattr(n, "storage"), Getattr(n, "name"), n);
- #endif
- if (!first_linked_type) {
- first_linked_type = n;
- Setattr(n, "allegrocl:last_linked_type", n);
- } else {
- Node *t = Getattr(first_linked_type, "allegrocl:last_linked_type");
- Setattr(t, "allegrocl:next_linked_type", n);
- Setattr(first_linked_type, "allegrocl:last_linked_type", n);
- }
- }
- void replace_linked_type(Node *old, Node *new_node) {
- Node *prev = Getattr(old, "allegrocl:prev_linked_type");
- Setattr(new_node, "allegrocl:next_linked_type", Getattr(old, "allegrocl:next_linked_type"));
- if (prev)
- Setattr(prev, "allegrocl:next_linked_type", new_node);
- Delattr(old, "allegrocl:next_linked_type");
- Delattr(old, "allegrocl:prev_linked_type");
- // check if we're replacing the first link.
- if (first_linked_type == old) {
- first_linked_type = new_node;
- Setattr(first_linked_type, "allegrocl:last_linked_type", Getattr(old, "allegrocl:last_linked_type"));
- }
- // check if we're replacing the last link.
- if (Getattr(first_linked_type, "allegrocl:last_linked_type") == old)
- Setattr(first_linked_type, "allegrocl:last_linked_type", new_node);
- }
- void insert_linked_type_at(Node *old, Node *new_node, int before = 1) {
- Node *p = 0;
- if (!first_linked_type) {
- add_linked_type(new_node);
- return;
- }
- if (!before) {
- Setattr(new_node, "allegrocl:next_linked_type", Getattr(old, "allegrocl:next_linked_type"));
- Setattr(old, "allegrocl:next_linked_type", new_node);
- if (Getattr(first_linked_type, "allegrocl:last_linked_type") == old)
- Setattr(first_linked_type, "allegrocl:last_linked_type", new_node);
- } else {
- Node *c = first_linked_type;
- while (c) {
- if (c == old) {
- break;
- } else {
- p = c;
- c = Getattr(c, "allegrocl:next_linked_type");
- }
- }
- if (c == old) {
- Setattr(new_node, "allegrocl:next_linked_type", c);
- if (first_linked_type == c) {
- first_linked_type = new_node;
- Setattr(first_linked_type, "allegrocl:last_linked_type", Getattr(c, "allegrocl:last_linked_type"));
- Delattr(c, "allegrocl:last_linked_type");
- }
- if (p)
- Setattr(p, "allegrocl:next_linked_type", new_node);
- }
- }
- }
- Node *find_linked_type_by_name(String *name) {
- Node *p = 0;
- Node *c = first_linked_type;
- // Printf(stderr,"in find_linked_type_by_name '%s'...", name);
- while (c) {
- String *key = Getattr(c, "name");
- if (!Strcmp(key, name)) {
- break;
- } else {
- p = c;
- c = Getattr(c, "allegrocl:next_linked_type");
- }
- }
- // Printf(stderr,"exit find_linked_type_by_name.\n");
- if (p && c)
- Setattr(c, "allegrocl:prev_linked_type", p);
- // Printf(stderr,"find_linked_type_by_name: DONE\n");
- return c;
- }
- Node *get_primary_synonym_of(Node *n) {
- Node *p = Getattr(n, "allegrocl:synonym-of");
- Node *prim = n;
- // Printf(stderr, "getting primary synonym of %x\n", n);
- while (p) {
- // Printf(stderr, " found one! %x\n", p);
- prim = p;
- p = Getattr(p, "allegrocl:synonym-of");
- }
- // Printf(stderr,"get_primary_syn: DONE. returning %s(%x)\n", Getattr(prim,"name"),prim);
- return prim;
- }
- void add_forward_referenced_type(Node *n, int overwrite = 0) {
- String *k = Getattr(n, "name");
- String *name = Getattr(n, "sym:name");
- String *ns = listify_namespace(current_namespace);
- String *val = Getattr(defined_foreign_types, k);
- if (!val || overwrite) {
- #ifdef ALLEGROCL_TYPE_DEBUG
- Printf(stderr, "Adding forward reference for %s (overwrite=%d)\n", k, overwrite);
- #endif
- Setattr(defined_foreign_types, Copy(k), NewString("forward-reference"));
- String *mangled_lname_gen = NewStringf("#.(swig-insert-id \"%s\" %s :type :class)", name, ns);
- Setattr(defined_foreign_ltypes, Copy(k), mangled_lname_gen);
- // Printf(f_cl, ";; forward reference stub\n"
- // "(swig-def-foreign-class \"%s\" (ff:foreign-pointer) (:class ))\n\n"
- // , name);
- #ifdef ALLEGROCL_CLASS_DEBUG
- Printf(stderr, "Linking forward reference type = %s(%x)\n", k, n);
- #endif
- add_linked_type(n);
- }
- }
- void add_defined_foreign_type(Node *n, int overwrite = 0, String *k = 0, String *name = 0, String *ns = current_namespace) {
- String *val;
- String *ns_list = listify_namespace(ns);
- String *templated = n ? Getattr(n, "template") : 0;
- String *cDeclName = n ? Getattr(n, "classDeclaration:name") : 0;
- #ifdef ALLEGROCL_CLASS_DEBUG
- Printf(stderr, "IN A-D-F-T. (n=%x, ow=%d, k=%s, name=%s, ns=%s\n", n, overwrite, k, name, ns);
- Printf(stderr, " templated = '%x', classDecl = '%x'\n", templated, cDeclName);
- #endif
- if (n) {
- if (!name)
- name = Getattr(n, "sym:name");
- if (!name)
- name = strip_namespaces(Getattr(n, "name"));
- if (templated) {
- k = namespaced_name(n);
- } else {
- String *kind_of_type = Getattr(n, "kind");
- /*
- For typedefs of the form:
- typedef __xxx { ... } xxx;
- add_defined_foreign_type will be called once via classHandler
- to define the type for 'struct __xxx', and once via typedefHandler
- to associate xxx with 'struct __xxx'.
- We create the following type to identifier mappings:
- struct __xxx -> (swig-insert-id "xxx") via classHand
- xxx -> (swig-insert-id "xxx") via typedefHand
- and all references to this typedef'd struct will appear in
- generated code as 'xxx'. For non-typedef'd structs, the
- classHand mapping will be
- struct __xxx -> (swig-insert-id "__xxx")
- */
- // Swig_print_node(n);
- String *unnamed = Getattr(n, "unnamed");
- if (kind_of_type && (!Strcmp(kind_of_type, "struct")
- || !Strcmp(kind_of_type, "union")) && cDeclName && !unnamed) {
- k = NewStringf("%s %s", kind_of_type, cDeclName);
- } else {
- if (!Strcmp(nodeType(n), "enum") && unnamed) {
- name = NewStringf("%s%d", anon_type_name, anon_type_count++);
- k = NewStringf("enum %s", name);
- Setattr(n, "allegrocl:name", name);
- } else {
- k = k ? k : Getattr(n, "name");
- }
- }
- }
- // Swig_print_node(n);
- }
- if (SwigType_istemplate(name)) {
- String *temp = strip_namespaces(SwigType_templateprefix(name));
- name = NewStringf("%s%s%s", temp, SwigType_templateargs(name), SwigType_templatesuffix(name));
- }
- val = lookup_defined_foreign_type(k);
- int is_fwd_ref = 0;
- if (val)
- is_fwd_ref = !Strcmp(val, "forward-reference");
- if (!val || overwrite || is_fwd_ref) {
- #ifdef ALLEGROCL_CLASS_DEBUG
- Printf(stderr, "Adding defined type '%s' = '%s' '%s' (overwrite=%d, in-class=%d)\n", k, ns, name, overwrite, in_class);
- #endif
- String *mangled_name_gen = NewStringf("#.(swig-insert-id \"%s\" %s :type :type)", name, ns_list);
- String *mangled_lname_gen = NewStringf("#.(swig-insert-id \"%s\" %s :type :class)", name, ns_list);
- Setattr(defined_foreign_types, Copy(k), Copy(mangled_name_gen));
- Setattr(defined_foreign_ltypes, Copy(k), Copy(mangled_lname_gen));
- if (CPlusPlus) {
- bool cpp_struct = Strstr(k, "struct ") ? true : false;
- bool cpp_union = Strstr(k, "union ") ? true : false;
- String *cpp_type = 0;
- if (cpp_struct) {
- cpp_type = Copy(k);
- Replaceall(cpp_type, "struct ", "");
- } else if (cpp_union) {
- cpp_type = Copy(k);
- Replaceall(cpp_type, "union ", "");
- }
- if (cpp_struct || cpp_union) {
- #ifdef ALLEGROCL_CLASS_DEBUG
- Printf(stderr, " Also adding defined type '%s' = '%s' '%s' (overwrite=%d)\n", cpp_type, ns, name, overwrite);
- #endif
- Setattr(defined_foreign_types, Copy(cpp_type), Copy(mangled_name_gen));
- Setattr(defined_foreign_ltypes, Copy(cpp_type), Copy(mangled_lname_gen));
- }
- }
- #ifdef ALLEGROCL_CLASS_DEBUG
- Printf(stderr, "looking to add %s/%s(%x) to linked_type_list...\n", k, name, n);
- #endif
- if (is_fwd_ref) {
- // Printf(stderr,"*** 1\n");
- add_linked_type(n);
- } else {
- // Printf(stderr,"*** 1-a\n");
- if (SwigType_istemplate(k)) {
- SwigType *resolved = SwigType_typedef_resolve_all(k);
- // Printf(stderr,"*** 1-b\n");
- Node *match = find_linked_type_by_name(resolved);
- Node *new_node = 0;
- // Printf(stderr, "*** temp-1\n");
- if (n) {
- new_node = n;
- } else {
- #ifdef ALLEGROCL_CLASS_DEBUG
- Printf(stderr, "Creating a new templateInst:\n");
- Printf(stderr, " name = %s\n", resolved);
- Printf(stderr, " sym:name = %s\n", name);
- Printf(stderr, " real-name = %s\n", k);
- Printf(stderr, " type = %s\n", resolved);
- Printf(stderr, " ns = %s\n\n", ns);
- #endif
- new_node = NewHash();
- Setattr(new_node, "nodeType", "templateInst");
- Setattr(new_node, "name", Copy(resolved));
- Setattr(new_node, "sym:name", Copy(name));
- Setattr(new_node, "real-name", Copy(k));
- Setattr(new_node, "type", Copy(resolved));
- Setattr(new_node, "allegrocl:namespace", ns);
- Setattr(new_node, "allegrocl:package", ns);
- }
- if (!match) {
- if (!Strcmp(nodeType(new_node), "templateInst") && in_class) {
- /* this is an implicit template instantiation found while
- walking a class. need to insert this into the
- linked_type list before the current class definition */
- #ifdef ALLEGROCL_CLASS_DEBUG
- Printf(stderr, "trying to insert a templateInst before a class\n");
- #endif
- insert_linked_type_at(in_class, new_node);
- #ifdef ALLEGROCL_CLASS_DEBUG
- Printf(stderr, "DID IT!\n");
- #endif
- } else {
- // Printf(stderr,"*** 3\n");
- add_linked_type(new_node);
- }
- Setattr(new_node, "allegrocl:synonym:is-primary", "1");
- } else {
- // a synonym type was found (held in variable 'match')
- // Printf(stderr, "setting primary synonym of %x to %x\n", new_node, match);
- if (new_node == match)
- Printf(stderr, "Hey-4 * - '%s' is a synonym of iteself!\n", Getattr(new_node, "name"));
- Setattr(new_node, "allegrocl:synonym-of", match);
- // Printf(stderr,"*** 4\n");
- add_linked_type(new_node);
- }
- } else {
- Node *match;
- if (!Strcmp(nodeType(n), "cdecl") && !Strcmp(Getattr(n, "storage"), "typedef")) {
- SwigType *type = SwigType_strip_qualifiers(Getattr(n, "type"));
- #ifdef ALLEGROCL_CLASS_DEBUG
- Printf(stderr, "Examining typedef '%s' for class references.\n", type);
- #endif
- if (SwigType_isclass(type)) {
- #ifdef ALLEGROCL_CLASS_DEBUG
- Printf(stderr, "Found typedef of a class '%s'\n", type);
- #endif
- /*
- For the following parsed expression:
- typedef struct __xxx { ... } xxx;
- if n is of kind "class" (defining the class 'struct __xxx'
- then we add n to the linked type list.
- if n is "cdecl" node of storage "typedef" (to note
- that xxx is equivalent to 'struct __xxx' then we don't
- want to add this node to the linked type list.
- */
- String *defined_type = lookup_defined_foreign_type(type);
- String *defined_key_type = lookup_defined_foreign_type(k);
- if ((Strstr(type, "struct ") || Strstr(type, "union "))
- && defined_type && !Strcmp(defined_type, defined_key_type)) {
- // mark as a synonym but don't add to linked_type list
- // Printf(stderr,"*** 4.8\n");
- Setattr(n, "allegrocl:synonym", "1");
- } else {
- SwigType *lookup_type = SwigType_istemplate(type) ? SwigType_typedef_resolve_all(type) : Copy(type);
- match = find_linked_type_by_name(lookup_type);
- if (match) {
- Setattr(n, "allegrocl:synonym", "1");
- Setattr(n, "allegrocl:synonym-of", match);
- Setattr(n, "real-name", Copy(lookup_type));
- // Printf(stderr, "*** pre-5: found match of '%s'(%x)\n", Getattr(match,"name"),match);
- // if(n == match) Printf(stderr, "Hey-5 *** setting synonym of %x to %x\n", n, match);
- // Printf(stderr,"*** 5\n");
- add_linked_type(n);
- } else {
- #ifdef ALLEGROCL_CLASS_DEBUG
- Printf(stderr, "Creating classfoward node for struct stub in typedef.\n");
- #endif
- Node *new_node = NewHash();
- String *symname = Copy(type);
- Replaceall(symname, "struct ", "");
- Setattr(new_node, "nodeType", "classforward");
- Setattr(new_node, "name", Copy(type));
- Setattr(new_node, "sym:name", symname);
- Setattr(new_node, "allegrocl:namespace", ns);
- Setattr(new_node, "allegrocl:package", ns);
- String *mangled_new_name = NewStringf("#.(swig-insert-id \"%s\" %s)", symname, ns_list);
- String *mangled_new_lname = NewStringf("#.(swig-insert-id \"%s\" %s :type :class)", symname, ns_list);
- Setattr(defined_foreign_types, Copy(symname), Copy(mangled_new_name));
- Setattr(defined_foreign_ltypes, Copy(symname), Copy(mangled_new_lname));
- // Printf(stderr,"Weird! Can't find the type!\n");
- add_forward_referenced_type(new_node);
- add_linked_type(new_node);
- Setattr(n, "allegrocl:synonym", "1");
- Setattr(n, "allegrocl:synonym-of", new_node);
- add_linked_type(n);
- }
- Delete(lookup_type);
- }
- } else {
- // check if it's a pointer or reference to a class.
- // Printf(stderr,"Checking if '%s' is a p. or r. to a class\n", type);
- String *class_ref = class_from_class_or_class_ref(type);
- if (class_ref) {
- match = find_linked_type_by_name(class_ref);
- Setattr(n, "allegrocl:synonym", "1");
- Setattr(n, "allegrocl:synonym-of", match);
- add_linked_type(n);
- }
- }
- Delete(type);
- // synonym types have already been added.
- // Printf(stderr,"*** 10\n");
- if (!Getattr(n, "allegrocl:synonym"))
- add_linked_type(n);
- } else if (Getattr(n, "template")) {
- // Printf(stderr, "this is a class template node(%s)\n", nodeType(n));
- String *resolved = SwigType_typedef_resolve_all(Getattr(n, "name"));
- #ifdef ALLEGROCL_CLASS_DEBUG
- Printf(stderr, " looking up %s for linked type match with %s...\n", Getattr(n, "sym:name"), resolved);
- #endif
- match = find_linked_type_by_name(resolved);
- if (!match) {
- #ifdef ALLEGROCL_CLASS_DEBUG
- Printf(stderr, "found no implicit instantiation of %%template node %s(%x)\n", Getattr(n, "name"), n);
- #endif
- add_linked_type(n);
- } else {
- Node *primary = get_primary_synonym_of(match);
- Setattr(n, "allegrocl:synonym:is-primary", "1");
- Delattr(primary, "allegrocl:synonym:is-primary");
- if (n == match)
- Printf(stderr, "Hey-7 * setting synonym of %x to %x\n (match = %x)", primary, n, match);
- Setattr(primary, "allegrocl:synonym-of", n);
- // Printf(stderr,"*** 7\n");
- add_linked_type(n);
- }
- } else {
- #ifdef ALLEGROCL_CLASS_DEBUG
- Printf(stderr, "linking type '%s'(%x)\n", k, n);
- #endif
- // Printf(stderr,"*** 8\n");
- add_linked_type(n);
- }
- }
- }
- Delete(mangled_name_gen);
- Delete(mangled_lname_gen);
- } else {
- Swig_warning(WARN_TYPE_REDEFINED, Getfile(n), Getline(n), "Attempting to store a foreign type that exists: %s (%s)\n", k, val);
- }
- Delete(ns_list);
- #ifdef ALLEGROCL_CLASS_DEBUG
- Printf(stderr, "OUT A-D-F-T\n");
- #endif
- }
- void note_implicit_template_instantiation(SwigType *t) {
- // the namespace of the implicit instantiation is not necessarily
- // current_namespace. Attempt to cull this from the type.
- #ifdef ALLEGROCL_CLASS_DEBUG
- Printf(stderr, "culling namespace of '%s' from '%s'\n", t, SwigType_templateprefix(t));
- #endif
- String *implicit_ns = namespace_of(SwigType_templateprefix(t));
- add_defined_foreign_type(0, 0, t, t, implicit_ns ? implicit_ns : current_namespace);
- }
- String *get_ffi_type(SwigType *ty, const String_or_char *name) {
- /* lookup defined foreign type.
- if it exists, it will return a form suitable for placing
- into lisp code to generate the def-foreign-type name */
- #ifdef ALLEGROCL_TYPE_DEBUG
- Printf(stderr, "inside g_f_t: looking up '%s' '%s'\n", ty, name);
- #endif
- String *found_type = lookup_defined_foreign_type(ty);
- if (found_type) {
- #ifdef ALLEGROCL_TYPE_DEBUG
- Printf(stderr, "found_type '%s'\n", found_type);
- #endif
- return (Strcmp(found_type, "forward-reference") ? Copy(found_type) : NewString(":void"));
- } else {
- Hash *typemap = Swig_typemap_search("ffitype", ty, name, 0);
- if (typemap) {
- String *typespec = Getattr(typemap, "code");
- #ifdef ALLEGROCL_TYPE_DEBUG
- Printf(stderr, "g-f-t: found ffitype typemap '%s'\n%s\n", typespec, typemap);
- #endif
- return NewString(typespec);
- }
- if (SwigType_istemplate(ty)) {
- note_implicit_template_instantiation(ty);
- return Copy(lookup_defined_foreign_type(ty));
- }
- }
- return 0;
- }
- String *lookup_defined_foreign_ltype(String *l) {
- #ifdef ALLEGROCL_TYPE_DEBUG
- Printf(stderr, "Looking up defined ltype '%s'.\n Found: '%s'\n", l, Getattr(defined_foreign_ltypes, l));
- #endif
- return Getattr(defined_foreign_ltypes, l);
- }
- /* walk type and return string containing lisp version.
- recursive. */
- String *internal_compose_foreign_type(SwigType *ty) {
- SwigType *tok;
- String *ffiType = NewString("");
- // for a function type, need to walk the parm list.
- while (Len(ty) != 0) {
- tok = SwigType_pop(ty);
- if (SwigType_isfunction(tok)) {
- // Generate Function wrapper
- Printf(ffiType, "(:function ");
- // walk parm list
- List *pl = SwigType_parmlist(tok);
- Printf(ffiType, "("); // start parm list
- for (Iterator i = First(pl); i.item; i = Next(i)) {
- SwigType *f_arg = SwigType_strip_qualifiers(i.item);
- Printf(ffiType, "%s ", internal_compose_foreign_type(f_arg));
- Delete(f_arg);
- }
- Printf(ffiType, ")"); // end parm list.
- // do function return type.
- Printf(ffiType, " %s)", internal_compose_foreign_type(ty));
- break;
- } else if (SwigType_ispointer(tok) || SwigType_isreference(tok)) {
- Printf(ffiType, "(* %s)", internal_compose_foreign_type(ty));
- } else if (SwigType_isarray(tok)) {
- Printf(ffiType, "(:array %s", internal_compose_foreign_type(ty));
- String *atype = NewString("int");
- String *dim = convert_literal(SwigType_array_getdim(tok, 0), atype);
- Delete(atype);
- if (is_integer(dim)) {
- Printf(ffiType, " %s)", dim);
- } else {
- Printf(ffiType, " #| %s |#)", SwigType_array_getdim(tok, 0));
- }
- } else if (SwigType_ismemberpointer(tok)) {
- // temp
- Printf(ffiType, "(* %s)", internal_compose_foreign_type(ty));
- } else {
- String *res = get_ffi_type(tok, "");
- if (res) {
- Printf(ffiType, "%s", res);
- } else {
- SwigType *resolved_type = SwigType_typedef_resolve(tok);
- if (resolved_type) {
- res = get_ffi_type(resolved_type, "");
- if (res) {
- } else {
- res = internal_compose_foreign_type(resolved_type);
- }
- if (res)
- Printf(ffiType, "%s", res);
- }
- // while(resolved_type) {
- // // the resolved_type may expand into something like p.NS1::NS2::SomeType
- // // for which get_ffi_type will not find any match (due to the p.).
- // // Printf(stderr, "\n in resolved type loop on '%s'\n", resolved_type);
- // res = get_ffi_type(resolved_type, "");
- // if (res) {
- // Printf(ffiType, "%s", res);
- // break;
- // } else {
- // resolved_type = SwigType_typedef_resolve(resolved_type);
- // }
- // }
- if (!res) {
- if (Strstr(tok, "struct ")) {
- Swig_warning(WARN_TYPE_UNDEFINED_CLASS, Getfile(tok), Getline(tok), "Unable to find definition of '%s', assuming forward reference.\n", tok);
- } else {
- Printf(stderr, "Unable to compose foreign type of: '%s'\n", tok);
- }
- Printf(ffiType, "(* :void)");
- }
- }
- }
- }
- return ffiType;
- }
- String *compose_foreign_type(SwigType *ty, String *id = 0) {
- Hash *lookup_res = Swig_typemap_search("ffitype", ty, id, 0);
- #ifdef ALLEGROCL_TYPE_DEBUG
- Printf(stderr, "compose_foreign_type: ENTER (%s)...\n ", ty);
- String *id_ref = SwigType_str(ty, id);
- Printf(stderr, "looking up typemap for %s, found '%s'(%x)\n",
- id_ref, lookup_res ? Getattr(lookup_res, "code") : 0, lookup_res);
- #endif
- /* should we allow named lookups in the typemap here? YES! */
- /* unnamed lookups should be found in get_ffi_type, called
- by internal_compose_foreign_type(), below. */
- if(id && lookup_res) {
- #ifdef ALLEGROCL_TYPE_DEBUG
- Printf(stderr, "compose_foreign_type: EXIT-1 (%s)\n ", Getattr(lookup_res, "code"));
- #endif
- return NewString(Getattr(lookup_res, "code"));
- }
- SwigType *temp = SwigType_strip_qualifiers(ty);
- String *res = internal_compose_foreign_type(temp);
- Delete(temp);
- #ifdef ALLEGROCL_TYPE_DEBUG
- Printf(stderr, "compose_foreign_type: EXIT (%s)\n ", res);
- #endif
- return res;
- }
- void update_package_if_needed(Node *n, File *f = f_clwrap) {
- #ifdef ALLEGROCL_DEBUG
- Printf(stderr, "update_package: ENTER... \n");
- Printf(stderr, " current_package = '%s'\n", current_package);
- Printf(stderr, " node_package = '%s'\n", Getattr(n, "allegrocl:package"));
- Printf(stderr, " node(%x) = '%s'\n", n, Getattr(n, "name"));
- #endif
- String *node_package = Getattr(n, "allegrocl:package");
- if (Strcmp(current_package, node_package)) {
- String *lispy_package = listify_namespace(node_package);
- Delete(current_package);
- current_package = Copy(node_package);
- Printf(f, "\n(swig-in-package %s)\n", lispy_package);
- Delete(lispy_package);
- }
- #ifdef ALLEGROCL_DEBUG
- Printf(stderr, "update_package: EXIT.\n");
- #endif
- }
- static String *mangle_name(Node *n, char const *prefix = "ACL", String *ns = current_namespace) {
- String *suffix = Getattr(n, "sym:overname");
- String *pre_mangled_name = NewStringf("%s_%s__%s%s", prefix, ns, Getattr(n, "sym:name"), suffix);
- String *mangled_name = Swig_name_mangle(pre_mangled_name);
- Delete(pre_mangled_name);
- return mangled_name;
- }
- /* utilities */
- /* remove a pointer from ffitype. non-destructive.
- (* :char) ==> :char
- (* (:array :int 30)) ==> (:array :int 30) */
- String *dereference_ffitype(String *ffitype) {
- char *start;
- char *temp = Char(ffitype);
- String *reduced_type = 0;
- if(temp && temp[0] == '(' && temp[1] == '*') {
- temp += 2;
- // walk past start of pointer references
- while(*temp == ' ') temp++;
- start = temp;
- // temp = Char(reduced_type);
- reduced_type = NewString(start);
- temp = Char(reduced_type);
- // walk to end of string. remove closing paren
- while(*temp != '\0') temp++;
- *(--temp) = '\0';
- }
- return reduced_type ? reduced_type : Copy(ffitype);
- }
- /* returns new string w/ parens stripped */
- String *strip_parens(String *string) {
- string = Copy(string);
- Replaceall(string, "(", "");
- Replaceall(string, ")", "");
- return string;
- }
- int ALLEGROCL::validIdentifier(String *s) {
- char *c = Char(s);
- bool got_dot = false;
- bool only_dots = true;
- /* Check that s is a valid common lisp symbol. There's a lot of leeway here.
- A common lisp symbol is essentially any token that's not a number and
- does not consist of only dots.
- We are expressly not allowing spaces in identifiers here, but spaces
- could be added via the identifier converter. */
- while (*c) {
- if (*c == '.') {
- got_dot = true;
- } else {
- only_dots = false;
- }
- if (!isgraph(*c))
- return 0;
- c++;
- }
- return (got_dot && only_dots) ? 0 : 1;
- }
- String *infix_to_prefix(String *val, char split_op, const String *op, String *type) {
- List *ored = Split(val, split_op, -1);
- // some float hackery
- if (((split_op == '+') || (split_op == '-')) && Len(ored) == 2 &&
- (SwigType_type(type) == T_FLOAT || SwigType_type(type) == T_DOUBLE || SwigType_type(type) == T_LONGDOUBLE)) {
- // check that we're not splitting a float
- String *possible_result = convert_literal(val, type, false);
- if (possible_result)
- return possible_result;
- }
- // try parsing the split results. if any part fails, kick out.
- bool part_failed = false;
- if (Len(ored) > 1) {
- String *result = NewStringf("(%s", op);
- for (Iterator i = First(ored); i.item; i = Next(i)) {
- String *converted = convert_literal(i.item, type);
- if (converted) {
- Printf(result, " %s", converted);
- Delete(converted);
- } else {
- part_failed = true;
- break;
- }
- }
- Printf(result, ")");
- Delete(ored);
- return part_failed ? 0 : result;
- } else {
- Delete(ored);
- }
- return 0;
- }
- /* To be called by code generating the lisp interface
- Will return a containing the literal based on type.
- Will return null if there are problems.
- try_to_split defaults to true (see stub above).
- */
- String *convert_literal(String *literal, String *type, bool try_to_split) {
- String *num_param = Copy(literal);
- String *trimmed = trim(num_param);
- String *num = strip_parens(trimmed), *res = 0;
- char *s = Char(num);
- String *ns = listify_namespace(current_namespace);
- // very basic parsing of infix expressions.
- if (try_to_split) {
- if ((res = infix_to_prefix(num, '|', "logior", type)))
- return res;
- if ((res = infix_to_prefix(num, '&', "logand", type)))
- return res;
- if ((res = infix_to_prefix(num, '^', "logxor", type)))
- return res;
- if ((res = infix_to_prefix(num, '*', "*", type)))
- return res;
- if ((res = infix_to_prefix(num, '/', "/", type)))
- return res;
- if ((res = infix_to_prefix(num, '+', "+", type)))
- return res;
- if ((res = infix_to_prefix(num, '-', "-", type)))
- return res;
- // if( (res = infix_to_prefix(num, '<<', "ash", type)) ) return res;
- }
- if (SwigType_type(type) == T_FLOAT || SwigType_type(type) == T_DOUBLE || SwigType_type(type) == T_LONGDOUBLE) {
- // Use CL syntax for float literals
- String *oldnum = Copy(num);
- // careful. may be a float identifier or float constant.
- char *num_start = Char(num);
- char *num_end = num_start + strlen(num_start) - 1;
- bool is_literal = isdigit(*num_start) || (*num_start == '.');
- String *lisp_exp = 0;
- if (is_literal) {
- if (*num_end == 'f' || *num_end == 'F') {
- lisp_exp = NewString("f");
- } else {
- lisp_exp = NewString("d");
- }
- if (*num_end == 'l' || *num_end == 'L' || *num_end == 'f' || *num_end == 'F') {
- *num_end = '\0';
- num_end--;
- }
- int exponents = Replaceall(num, "e", lisp_exp) + Replaceall(num, "E", lisp_exp);
- if (!exponents)
- Printf(num, "%s0", lisp_exp);
- if (exponents > 1 || (exponents + Replaceall(num, ".", ".") == 0)) {
- // Printf(stderr, "Can't parse '%s' as type '%s'.\n", oldnum, type);
- Delete(num);
- num = 0;
- }
- } else {
- String *id = NewStringf("#.(swig-insert-id \"%s\" %s :type :constant)",
- num, ns);
- Delete(num);
- num = id;
- }
- Delete(oldnum);
- Delete(trimmed);
- Delete(ns);
- return num;
- } else if (SwigType_type(type) == T_CHAR) {
- /* Use CL syntax for character literals */
- Delete(num);
- Delete(trimmed);
- return NewStringf("#\\%s", num_param);
- } else if (SwigType_type(type) == T_STRING) {
- /* Use CL syntax for string literals */
- Delete(num);
- Delete(trimmed);
- return NewStringf("\"%s\"", num_param);
- } else if (Len(num) >= 1 && (isdigit(s[0]) || s[0] == '+' || s[0] == '-')) {
- /* use CL syntax for numbers */
- String *oldnum = Copy(num);
- int usuffixes = Replaceall(num, "u", "") + Replaceall(num, "U", "");
- int lsuffixes = Replaceall(num, "l", "") + Replaceall(num, "L", "");
- if (usuffixes > 1 || lsuffixes > 1) {
- Printf(stderr, "Weird!! number %s looks invalid.\n", oldnum);
- SWIG_exit(EXIT_FAILURE);
- }
- s = Char(num);
- if (s[0] == '0' && Len(num) >= 2) {
- /*octal or hex */
- res = NewStringf("#%c%s", tolower(s[1]) == 'x' ? 'x' : 'o', s + 2);
- Delete(num);
- } else {
- res = num;
- }
- Delete(oldnum);
- Delete(trimmed);
- return res;
- } else if (allegrocl->validIdentifier(num)) {
- /* convert C/C++ identifiers to CL symbols */
- res = NewStringf("#.(swig-insert-id \"%s\" %s :type :constant)", num, ns);
- Delete(num);
- Delete(trimmed);
- Delete(ns);
- return res;
- } else {
- Delete(trimmed);
- return num;
- }
- }
- void emit_stub_class(Node *n) {
- #ifdef ALLEGROCL_WRAP_DEBUG
- Printf(stderr, "emit_stub_class: ENTER... '%s'(%x)\n", Getattr(n, "sym:name"), n);
- #endif
- String *name = Getattr(n, "sym:name");
- if (Getattr(n, "allegrocl:synonym:already-been-stubbed"))
- return;
- if (SwigType_istemplate(name)) {
- String *temp = strip_namespaces(SwigType_templateprefix(name));
- name = NewStringf("%s%s%s", temp, SwigType_templateargs(name), SwigType_templatesuffix(name));
- Delete(temp);
- } else {
- name = strip_namespaces(name);
- }
- // Printf(f_clhead, ";; from emit-stub-class\n");
- update_package_if_needed(n, f_clhead);
- Printf(f_clhead, ";; class template stub.\n");
- Printf(f_clhead, "(swig-def-foreign-stub \"%s\")\n", name);
- Setattr(n, "allegrocl:synonym:already-been-stubbed", "1");
- #ifdef ALLEGROCL_WRAP_DEBUG
- Printf(stderr, "emit_stub_class: EXIT\n");
- #endif
- }
- void emit_synonym(Node *synonym) {
- #ifdef ALLEGROCL_WRAP_DEBUG
- Printf(stderr, "emit_synonym: ENTER... \n");
- #endif
- // Printf(stderr,"in emit_synonym for %s(%x)\n", Getattr(synonym,"name"),synonym);
- int is_tempInst = !Strcmp(nodeType(synonym), "templateInst");
- String *synonym_type;
- Node *of = get_primary_synonym_of(synonym);
- if (is_tempInst) {
- // Printf(stderr, "*** using real-name '%s'\n", Getattr(synonym,"real-name"));
- synonym_type = Getattr(synonym, "real-name");
- } else {
- // Printf(stderr, "*** using name '%s'\n", Getattr(synonym,"name"));
- synonym_type = Getattr(synonym, "name");
- }
- String *synonym_ns = listify_namespace(Getattr(synonym, "allegrocl:namespace"));
- String *syn_ltype, *syn_type, *of_ltype;
- // String *of_cdeclname = Getattr(of,"allegrocl:classDeclarationName");
- String *of_ns = Getattr(of, "allegrocl:namespace");
- String *of_ns_list = listify_namespace(of_ns);
- // String *of_name = of_cdeclname ? NewStringf("struct %s", Getattr(of,"name")) : NewStringf("%s::%s", of_ns, Getattr(of,"sym:name"));
- // String *of_name = NewStringf("%s::%s", of_ns, Getattr(of,"sym:name"));
- String *of_name = namespaced_name(of, of_ns);
- if (CPlusPlus && !Strcmp(nodeType(synonym), "cdecl")) {
- syn_ltype = NewStringf("#.(swig-insert-id \"%s\" %s :type :class)",
- strip_namespaces(Getattr(synonym, "real-name")), synonym_ns);
- syn_type = NewStringf("#.(swig-insert-id \"%s\" %s :type :type)",
- strip_namespaces(Getattr(synonym, "real-name")), synonym_ns);
- } else {
- syn_ltype = lookup_defined_foreign_ltype(synonym_type);
- syn_type = lookup_defined_foreign_type(synonym_type);
- }
- of_ltype = lookup_defined_foreign_ltype(of_name);
- // Printf(f_clhead,";; from emit-synonym\n");
- Printf(f_clhead, "(swig-def-synonym-type %s\n %s\n %s)\n", syn_ltype, of_ltype, syn_type);
- Delete(synonym_ns);
- Delete(of_ns_list);
- Delete(of_name);
- #ifdef ALLEGROCL_WRAP_DEBUG
- Printf(stderr, "emit_synonym: EXIT\n");
- #endif
- }
- void emit_full_class(Node *n) {
- #ifdef ALLEGROCL_WRAP_DEBUG
- Printf(stderr, "emit_full_class: ENTER... \n");
- #endif
- String *name = Getattr(n, "sym:name");
- String *kind = Getattr(n, "kind");
- // Printf(stderr,"in emit_full_class: '%s'(%x).", Getattr(n,"name"),n);
- if (Getattr(n, "allegrocl:synonym-of")) {
- // Printf(stderr,"but it's a synonym of something.\n");
- update_package_if_needed(n, f_clhead);
- emit_synonym(n);
- return;
- }
- // collect superclasses
- String *bases = Getattr(n, "bases");
- String *supers = NewString("(");
- if (bases) {
- int first = 1;
- for (Iterator i = First(bases); i.item; i = Next(i)) {
- if (!first)
- Printf(supers, " ");
- String *s = lookup_defined_foreign_ltype(Getattr(i.item, "name"));
- // String *name = Getattr(i.item,"name");
- if (s) {
- Printf(supers, "%s", s);
- } else {
- #ifdef ALLEGROCL_TYPE_DEBUG
- Printf(stderr, "emit_templ_inst: did not find ltype for base class %s (%s)", Getattr(i.item, "name"), Getattr(n, "allegrocl:namespace"));
- #endif
- }
- }
- } else {
- Printf(supers, "ff:foreign-pointer");
- }
- Printf(supers, ")");
- // Walk children to generate type definition.
- String *slotdefs = NewString(" ");
- #ifdef ALLEGROCL_WRAP_DEBUG
- Printf(stderr, " walking children...\n");
- #endif
- Node *c;
- for (c = firstChild(n); c; c = nextSibling(c)) {
- String *storage_type = Getattr(c, "storage");
- if ((!Strcmp(nodeType(c), "cdecl") && (!storage_type || Strcmp(storage_type, "typedef")))) {
- String *access = Getattr(c, "access");
- // hack. why would decl have a value of "variableHandler" and now "0"?
- String *childDecl = Getattr(c, "decl");
- // Printf(stderr,"childDecl = '%s' (%s)\n", childDecl, Getattr(c,"view"));
- if (!Strcmp(childDecl, "0"))
- childDecl = NewString("");
- SwigType *childType;
- String *cname;
- // don't include types for private slots (yet). spr33959.
- if(access && Strcmp(access,"public")) {
- childType = NewStringf("int");
- cname = NewString("nil");
- } else {
- childType = NewStringf("%s%s", childDecl, Getattr(c, "type"));
- cname = Copy(Getattr(c, "name"));
- }
- if (!SwigType_isfunction(childType)) {
- // Printf(slotdefs, ";;; member functions don't appear as slots.\n ");
- // Printf(slotdefs, ";; ");
- String *ns = listify_namespace(Getattr(n, "allegrocl:package"));
- #ifdef ALLEGROCL_WRAP_DEBUG
- Printf(stderr, "slot name = '%s' ns = '%s' class-of '%s' and type = '%s'\n", cname, ns, name, childType);
- #endif
- Printf(slotdefs, "(#.(swig-insert-id \"%s\" %s :type :slot :class \"%s\") %s)", cname, ns, name, compose_foreign_type(childType));
- Delete(ns);
- if (access && Strcmp(access, "public"))
- Printf(slotdefs, " ;; %s member", access);
- Printf(slotdefs, "\n ");
- }
- Delete(childType);
- Delete(cname);
- }
- }
- String *ns_list = listify_namespace(Getattr(n, "allegrocl:namespace"));
- update_package_if_needed(n, f_clhead);
- Printf(f_clhead, "(swig-def-foreign-class \"%s\"\n %s\n (:%s\n%s))\n\n", name, supers, kind, slotdefs);
- Delete(supers);
- Delete(ns_list);
- Setattr(n, "allegrocl:synonym:already-been-stubbed", "1");
- #ifdef ALLEGROCL_WRAP_DEBUG
- Printf(stderr, "emit_full_class: EXIT\n");
- #endif
- }
- void emit_class(Node *n) {
- #ifdef ALLEGROCL_WRAP_DEBUG
- Printf(stderr, "emit_class: ENTER... '%s'(%x)\n", Getattr(n, "sym:name"), n);
- #endif
- int is_tempInst = !Strcmp(nodeType(n), "templateInst");
- String *ns_list = listify_namespace(Getattr(n, "allegrocl:namespace"));
- String *name = Getattr(n, is_tempInst ? "real-name" : "name");
- if (SwigType_istemplate(name)) {
- String *temp = strip_namespaces(SwigType_templateprefix(name));
- name = NewStringf("%s%s%s", temp, SwigType_templateargs(name), SwigType_templatesuffix(name));
- Delete(temp);
- } else {
- name = strip_namespaces(name);
- }
- if (Getattr(n, "allegrocl:synonym:is-primary")) {
- // Printf(stderr," is primary... ");
- if (is_tempInst) {
- emit_stub_class(n);
- } else {
- emit_full_class(n);
- }
- } else {
- // Node *primary = Getattr(n,"allegrocl:synonym-of");
- Node *primary = get_primary_synonym_of(n);
- if (primary && (primary != n)) {
- // Printf(stderr," emitting synonym... ");
- emit_stub_class(primary);
- update_package_if_needed(n, f_clhead);
- emit_synonym(n);
- } else {
- emit_full_class(n);
- }
- }
- // Printf(stderr,"DONE\n");
- Delete(name);
- Delete(ns_list);
- #ifdef ALLEGROCL_WRAP_DEBUG
- Printf(stderr, "emit_class: EXIT\n");
- #endif
- }
- void emit_typedef(Node *n) {
- #ifdef ALLEGROCL_WRAP_DEBUG
- Printf(stderr, "emit_typedef: ENTER... \n");
- #endif
- String *name;
- String *sym_name = Getattr(n, "sym:name");
- String *type = NewStringf("%s%s", Getattr(n, "decl"), Getattr(n, "type"));
- String *lisp_type = compose_foreign_type(type);
- Delete(type);
- Node *in_class = Getattr(n, "allegrocl:typedef:in-class");
- // Printf(stderr,"in emit_typedef: '%s'(%x).",Getattr(n,"name"),n);
- if (Getattr(n, "allegrocl:synonym-of")) {
- // Printf(stderr," but it's a synonym of something.\n");
- emit_synonym(n);
- return;
- }
- if (in_class) {
- String *class_name = Getattr(in_class, "name");
- if (SwigType_istemplate(class_name)) {
- String *temp = strip_namespaces(SwigType_templateprefix(class_name));
- class_name = NewStringf("%s%s%s", temp, SwigType_templateargs(class_name), SwigType_templatesuffix(class_name));
- Delete(temp);
- }
- name = NewStringf("%s__%s", class_name, sym_name);
- Setattr(n, "allegrocl:in-class", in_class);
- } else {
- name = sym_name ? Copy(sym_name) : Copy(Getattr(n, "name"));
- }
- // leave these in for now. might want to change these to def-foreign-class at some point.
- // Printf(f_clhead, ";; %s\n", SwigType_typedef_resolve_all(lisp_type));
- // Swig_print_node(n);
- Printf(f_clhead, "(swig-def-foreign-type \"%s\"\n %s)\n", name, lisp_type);
- Delete(name);
- #ifdef ALLEGROCL_WRAP_DEBUG
- Printf(stderr, "emit_typedef: EXIT\n");
- #endif
- }
- void emit_enum_type_no_wrap(Node *n) {
- #ifdef ALLEGROCL_WRAP_DEBUG
- Printf(stderr, "emit_enum_type_no_wrap: ENTER... \n");
- #endif
- String *unnamed = Getattr(n, "unnamed");
- String *name;
- // SwigType *enumtype;
- name = unnamed ? Getattr(n, "allegrocl:name") : Getattr(n, "sym:name");
- SwigType *tmp = NewStringf("enum %s", unnamed ? unnamed : name);
- Hash *typemap = Swig_typemap_search("ffitype", tmp, 0, 0);
- String *enumtype = Getattr(typemap, "code");
- // enumtype = compose_foreign_type(tmp);
- Delete(tmp);
- if (name) {
- String *ns = listify_namespace(current_namespace);
- Printf(f_clhead, "(swig-def-foreign-type \"%s\" %s)\n", name, enumtype);
- Delete(ns);
- // walk children.
- Node *c;
- for (c = firstChild(n); c; c = nextSibling(c)) {
- if (!Getattr(c, "error")) {
- String *val = Getattr(c, "enumvalue");
- if (!val)
- val = Getattr(c, "enumvalueex");
- String *converted_val = convert_literal(val, Getattr(c, "type"));
- String *valname = Getattr(c, "sym:name");
- if (converted_val) {
- Printf(f_clhead, "(swig-defconstant \"%s\" %s)\n", valname, converted_val);
- Delete(converted_val);
- } else {
- Swig_warning(WARN_LANG_DISCARD_CONST, Getfile(n), Getline(n), "Unable to parse enum value '%s'. Setting to NIL\n", val);
- Printf(f_clhead, "(swig-defconstant \"%s\" nil #| %s |#)\n", valname, val);
- }
- }
- }
- }
- Printf(f_clhead, "\n");
- #ifdef ALLEGROCL_WRAP_DEBUG
- Printf(stderr, "emit_enum_type_no_wrap: EXIT\n");
- #endif
- }
- void emit_enum_type(Node *n) {
- #ifdef ALLEGROCL_WRAP_DEBUG
- Printf(stderr, "emit_enum_type: ENTER... \n");
- #endif
- if (!Generate_Wrapper) {
- emit_enum_type_no_wrap(n);
- return;
- }
- String *unnamed = Getattr(n, "unnamed");
- String *name;
- // SwigType *enumtype;
- name = unnamed ? Getattr(n, "allegrocl:name") : Getattr(n, "sym:name");
- SwigType *tmp = NewStringf("enum %s", unnamed ? unnamed : name);
- // SwigType *tmp = NewStringf("enum ACL_SWIG_ENUM_NAME");
- Hash *typemap = Swig_typemap_search("ffitype", tmp, 0, 0);
- String *enumtype = Getattr(typemap, "code");
- // enumtype = compose_foreign_type(tmp);
- Delete(tmp);
- if (name) {
- String *ns = listify_namespace(current_namespace);
- Printf(f_clhead, "(swig-def-foreign-type \"%s\" %s)\n", name, enumtype);
- Delete(ns);
- // walk children.
- Node *c;
- for(c = firstChild(n); c; c=nextSibling(c)) {
- String *mangled_name = mangle_name(c, "ACL_ENUM", Getattr(c,"allegrocl:package"));
- Printf(f_clhead, "(swig-defvar \"%s\" \"%s\" :type :constant :ftype :signed-long)\n", Getattr(c, "sym:name"), mangled_name);
- Delete(mangled_name);
- }
- }
- #ifdef ALLEGROCL_WRAP_DEBUG
- Printf(stderr, "emit_enum_type: EXIT\n");
- #endif
- }
- void emit_default_linked_type(Node *n) {
- #ifdef ALLEGROCL_WRAP_DEBUG
- Printf(stderr, "emit_default_linked_type: ENTER... \n");
- #endif
- // catchall for non class types.
- if (!Strcmp(nodeType(n), "classforward")) {
- Printf(f_clhead, ";; forward referenced stub.\n");
- Printf(f_clhead, "(swig-def-foreign-class \"%s\" (ff:foreign-pointer) (:class ))\n\n", Getattr(n, "sym:name"));
- } else if (!Strcmp(nodeType(n), "enum")) {
- emit_enum_type(n);
- } else {
- Printf(stderr, "Don't know how to emit node type '%s' named '%s'\n", nodeType(n), Getattr(n, "name"));
- }
- #ifdef ALLEGROCL_WRAP_DEBUG
- Printf(stderr, "emit_default_linked_type: EXIT\n");
- #endif
- }
- void dump_linked_types(File *f) {
- Node *n = first_linked_type;
- int i = 0;
- while (n) {
- Printf(f, "%d: (%x) node '%s' name '%s'\n", i++, n, nodeType(n), Getattr(n, "sym:name"));
- Node *t = Getattr(n, "allegrocl:synonym-of");
- if (t)
- Printf(f, " synonym-of %s(%x)\n", Getattr(t, "name"), t);
- n = Getattr(n, "allegrocl:next_linked_type");
- }
- }
- void emit_linked_types() {
- #ifdef ALLEGROCL_WRAP_DEBUG
- Printf(stderr, "emit_linked_types: ENTER... ");
- #endif
- Node *n = first_linked_type;
- while (n) {
- String *node_type = nodeType(n);
- // Printf(stderr,"emitting node %s(%x) of type %s.", Getattr(n,"name"),n, nodeType(n));
- if (!Strcmp(node_type, "class") || !Strcmp(node_type, "templateInst")) {
- // may need to emit a stub, so it will update the package itself.
- // Printf(stderr," Passing to emit_class.");
- emit_class(n);
- } else if (!Strcmp(nodeType(n), "cdecl")) {
- // Printf(stderr," Passing to emit_typedef.");
- update_package_if_needed(n, f_clhead);
- emit_typedef(n);
- } else {
- // Printf(stderr," Passing to default_emitter.");
- update_package_if_needed(n, f_clhead);
- emit_default_linked_type(n);
- }
- n = Getattr(n, "allegrocl:next_linked_type");
- // Printf(stderr,"returned.\n");
- }
- #ifdef ALLEGROCL_WRAP_DEBUG
- Printf(stderr, "emit_linked_types: EXIT\n");
- #endif
- }
- extern "C" Language *swig_allegrocl(void) {
- return (allegrocl = new ALLEGROCL());
- }
- void ALLEGROCL::main(int argc, char *argv[]) {
- int i;
- SWIG_library_directory("allegrocl");
- SWIG_config_file("allegrocl.swg");
- for (i = 1; i < argc; i++) {
- if (!strcmp(argv[i], "-identifier-converter")) {
- char *conv = argv[i + 1];
- if (!conv)
- Swig_arg_error();
- Swig_mark_arg(i);
- Swig_mark_arg(i + 1);
- i++;
- /* check for built-ins */
- if (!strcmp(conv, "lispify")) {
- identifier_converter = "identifier-convert-lispify";
- } else if (!strcmp(conv, "null")) {
- identifier_converter = "identifier-convert-null";
- } else {
- /* Must be user defined */
- char *idconv = new char[strlen(conv) + 1];
- strcpy(idconv, conv);
- identifier_converter = idconv;
- }
- } else if (!strcmp(argv[i], "-cwrap")) {
- CWrap = true;
- Swig_mark_arg(i);
- } else if (!strcmp(argv[i], "-nocwrap")) {
- CWrap = false;
- Swig_mark_arg(i);
- } else if (!strcmp(argv[i], "-isolate")) {
- unique_swig_package = true;
- Swig_mark_arg(i);
- }
- if (!strcmp(argv[i], "-help")) {
- fprintf(stdout, "Allegro CL Options (available with -allegrocl)\n");
- fprintf(stdout,
- " -identifier-converter <type or funcname>\n"
- "\tSpecifies the type of conversion to do on C identifiers to convert\n"
- "\tthem to symbols. There are two built-in converters: 'null' and\n"
- "\t 'lispify'. The default is 'null'. If you supply a name other\n"
- "\tthan one of the built-ins, then a function by that name will be\n"
- "\tcalled to convert identifiers to symbols.\n"
- "\n"
- " -[no]cwrap\n"
- "\tTurn on or turn off generation of an intermediate C file when\n" "\tcreating a C interface. By default this is only done for C++ code.\n");
- }
- }
- allow_overloading();
- }
- int ALLEGROCL::top(Node *n) {
- module_name = Getattr(n, "name");
- String *cxx_filename = Getattr(n, "outfile");
- String *cl_filename = NewString("");
- swig_package = unique_swig_package ? NewStringf("swig.%s", module_name) : NewString("swig");
- Printf(cl_filename, "%s%s.cl", SWIG_output_directory(), Swig_file_basename(Getattr(n,"infile")));
- f_cl = NewFile(cl_filename, "w");
- if (!f_cl) {
- Printf(stderr, "Unable to open %s for writing\n", cl_filename);
- SWIG_exit(EXIT_FAILURE);
- }
- Generate_Wrapper = CPlusPlus || CWrap;
- if (Generate_Wrapper) {
- f_cxx = NewFile(cxx_filename, "w");
- if (!f_cxx) {
- Close(f_cl);
- Delete(f_cl);
- Printf(stderr, "Unable to open %s for writing\n", cxx_filename);
- SWIG_exit(EXIT_FAILURE);
- }
- } else
- f_cxx = NewString("");
- f_cxx_header = f_cxx;
- f_cxx_wrapper = NewString("");
- Swig_register_filebyname("header", f_cxx_header);
- Swig_register_filebyname("wrapper", f_cxx_wrapper);
- Swig_register_filebyname("runtime", f_cxx);
- Swig_register_filebyname("lisp", f_clwrap);
- Swig_register_filebyname("lisphead", f_cl);
- Printf(f_cl, ";; This is an automatically generated file. Make changes in\n"
- ";; the definition file, not here.\n\n"
- "(defpackage :%s\n"
- " (:use :common-lisp :ff :excl)\n"
- " (:export #:*swig-identifier-converter* #:*swig-module-name*\n"
- " #:*void* #:*swig-export-list*))\n"
- "(in-package :%s)\n\n"
- "(eval-when (compile load eval)\n"
- " (defparameter *swig-identifier-converter* '%s)\n"
- " (defparameter *swig-module-name* :%s))\n\n", swig_package, swig_package, identifier_converter, module_name);
- Printf(f_cl, "(defpackage :%s\n" " (:use :commo…