From 678139bb955c313a66bcf0ed0bed0ec5b017c33e Mon Sep 17 00:00:00 2001 From: William S Fulton Date: Fri, 9 Apr 2004 19:33:05 +0000 Subject: [PATCH] enum support function for looking up the enum node from the symbol table given any enum string type git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk@5848 626c5289-ae23-0410-ae9c-e8d60b6d4f22 --- SWIG/Source/Modules/lang.cxx | 82 ++++++++++++++++++++++++++++++----- SWIG/Source/Modules/swigmod.h | 10 +++-- 2 files changed, 78 insertions(+), 14 deletions(-) diff --git a/SWIG/Source/Modules/lang.cxx b/SWIG/Source/Modules/lang.cxx index b6c840183..8a9469928 100644 --- a/SWIG/Source/Modules/lang.cxx +++ b/SWIG/Source/Modules/lang.cxx @@ -45,7 +45,7 @@ int ImportMode = 0; int IsVirtual = 0; static String *AttributeFunctionGet = 0; static String *AttributeFunctionSet = 0; -static int cplus_mode = 0; +int cplus_mode = 0; static Node *CurrentClass = 0; int line_number = 0; char *input_file = 0; @@ -226,13 +226,18 @@ int Dispatcher::usingDeclaration(Node *n) { return defaultHandler(n); } int Dispatcher::namespaceDeclaration(Node *n) { return defaultHandler(n); } /* Allocators */ -Language::Language() { - symbols = NewHash(); - classtypes = NewHash(); - none_comparison = NewString("$arg != 0"); +Language::Language() : + none_comparison(NewString("$arg != 0")), + director_ctor_code(NewString("")), + symbols(NewHash()), + classtypes(NewHash()), + enumtypes(NewHash()), + overloading(0), + multiinput(0), + directors(0) +{ argc_template_string = NewString("argc"); argv_template_string = NewString("argv[%d]"); - director_ctor_code = NewString(""); /* Default director constructor code, passed to Swig_ConstructorToFunction */ Printv(director_ctor_code, @@ -242,14 +247,12 @@ Language::Language() { " $nondirector_new \n", "}\n", NIL); - overloading = 0; - multiinput = 0; - directors = 0; } Language::~Language() { Delete(symbols); Delete(classtypes); + Delete(enumtypes); } /* ---------------------------------------------------------------------- @@ -1343,7 +1346,7 @@ int Language::enumDeclaration(Node *n) { int Language::enumvalueDeclaration(Node *n) { if (CurrentClass && (cplus_mode != CPLUS_PUBLIC)) return SWIG_NOWRAP; - Swig_require("enumvalueDeclaratuon",n,"*name", "?value",NIL); + Swig_require("enumvalueDeclaration",n,"*name", "?value",NIL); String *value = Getattr(n,"value"); String *name = Getattr(n,"name"); String *tmpValue; @@ -1930,7 +1933,6 @@ int Language::constructorDeclaration(Node *n) { } } else { if (name && (Cmp(Swig_scopename_last(name),Swig_scopename_last(ClassName))) && !(Getattr(n,"template"))) { - Printf(stdout,"name = '%s', ClassName='%s'\n", name, ClassName); Swig_warning(WARN_LANG_RETURN_TYPE, input_file,line_number,"Function %s must have a return type.\n", name); Swig_restore(n); @@ -2286,6 +2288,64 @@ Language::classLookup(SwigType *s) { return n; } +/* ----------------------------------------------------------------------------- + * Language::enumLookup() + * + * Finds and returns the Node containing the enum declaration for the (enum) + * type passed in. + * ----------------------------------------------------------------------------- */ + +Node * +Language::enumLookup(SwigType *s) { + Node *n = 0; + Symtab *stab = 0; + + /* Look in hash of cached values */ + n = Getattr(enumtypes,s); + if (!n) { + + SwigType *ty1 = SwigType_typedef_resolve_all(s); + SwigType *ty2 = SwigType_strip_qualifiers(ty1); + Delete(ty1); + ty1 = 0; + + Replaceall(ty2,"enum ",""); + String *prefix = SwigType_prefix(ty2); + + /* Look for type in symbol table */ + while (!n) { + Hash *nstab; + n = Swig_symbol_clookup(ty2,stab); + if (!n) break; + if (Strcmp(nodeType(n),"enum") == 0) break; + n = parentNode(n); + if (!n) break; + nstab = Getattr(n,"sym:symtab"); + n = 0; + if ((!nstab) || (nstab == stab)) { + break; + } + stab = nstab; + } + if (n) { + /* Found a match. Look at the prefix. We only allow simple types. */ + if (Len(prefix) == 0) { /* Simple type */ + Setattr(enumtypes,Copy(s),n); + } else { + n = 0; + } + } + Delete(ty2); + Delete(prefix); + + } + if (n && (Getattr(n,"feature:ignore"))) { + n = 0; + } + + return n; +} + /* ----------------------------------------------------------------------------- * Language::allow_overloading() * ----------------------------------------------------------------------------- */ diff --git a/SWIG/Source/Modules/swigmod.h b/SWIG/Source/Modules/swigmod.h index ad508baed..0623f20de 100644 --- a/SWIG/Source/Modules/swigmod.h +++ b/SWIG/Source/Modules/swigmod.h @@ -207,11 +207,11 @@ public: virtual int classDirectorDisown(Node *n); /* Miscellaneous */ - virtual int validIdentifier(String *s); /* valid identifier? */ virtual int addSymbol(const String *s, const Node *n); /* Add symbol */ virtual Node *symbolLookup(String *s); /* Symbol lookup */ virtual Node *classLookup(SwigType *s); /* Class lookup */ + virtual Node *enumLookup(SwigType *s); /* Enum lookup */ virtual int abstractClassTest(Node *n); /* Is class really abstract? */ /* Allow director related code generation */ @@ -247,10 +247,13 @@ public: /* Return the node for the current class */ Node *getCurrentClass() const; - + + /* Return C++ mode */ + int getCPlusMode() const; + /* Return the real name of the current class */ String *getClassName() const; - + /* Return the current class prefix */ String *getClassPrefix() const; @@ -269,6 +272,7 @@ public: private: Hash *symbols; Hash *classtypes; + Hash *enumtypes; int overloading; int multiinput; int directors;