diff options
Diffstat (limited to 'Source/Swig/stype.c')
-rw-r--r-- | Source/Swig/stype.c | 209 |
1 files changed, 178 insertions, 31 deletions
diff --git a/Source/Swig/stype.c b/Source/Swig/stype.c index dd2aea688..c2c17e515 100644 --- a/Source/Swig/stype.c +++ b/Source/Swig/stype.c @@ -177,7 +177,7 @@ void SwigType_push(SwigType *t, String *cons) { * Testing functions for querying a raw datatype * ----------------------------------------------------------------------------- */ -int SwigType_ispointer_return(SwigType *t) { +int SwigType_ispointer_return(const SwigType *t) { char *c; int idx; if (!t) @@ -190,7 +190,7 @@ int SwigType_ispointer_return(SwigType *t) { return 0; } -int SwigType_isreference_return(SwigType *t) { +int SwigType_isreference_return(const SwigType *t) { char *c; int idx; if (!t) @@ -203,7 +203,7 @@ int SwigType_isreference_return(SwigType *t) { return 0; } -int SwigType_isconst(SwigType *t) { +int SwigType_isconst(const SwigType *t) { char *c; if (!t) return 0; @@ -229,7 +229,7 @@ int SwigType_isconst(SwigType *t) { return 0; } -int SwigType_ismutable(SwigType *t) { +int SwigType_ismutable(const SwigType *t) { int r; SwigType *qt = SwigType_typedef_resolve_all(t); if (SwigType_isreference(qt) || SwigType_isarray(qt)) { @@ -240,7 +240,7 @@ int SwigType_ismutable(SwigType *t) { return r ? 0 : 1; } -int SwigType_isenum(SwigType *t) { +int SwigType_isenum(const SwigType *t) { char *c = Char(t); if (!t) return 0; @@ -250,7 +250,7 @@ int SwigType_isenum(SwigType *t) { return 0; } -int SwigType_issimple(SwigType *t) { +int SwigType_issimple(const SwigType *t) { char *c = Char(t); if (!t) return 0; @@ -308,7 +308,7 @@ int SwigType_issimple(SwigType *t) { * r.q(const).enum SWIGTYPE * ----------------------------------------------------------------------------- */ -SwigType *SwigType_default_create(SwigType *ty) { +SwigType *SwigType_default_create(const SwigType *ty) { SwigType *r = 0; List *l; Iterator it; @@ -366,7 +366,7 @@ SwigType *SwigType_default_create(SwigType *ty) { * SwigType_default_create() before calling this function. * * Example deductions (matching the examples described in SwigType_default_create), - * where the the most specialized matches are highest in the list: + * where the most specialized matches are highest in the list: * * a(ANY).a(ANY).SWIGTYPE * a(ANY).a().SWIGTYPE @@ -387,7 +387,7 @@ SwigType *SwigType_default_create(SwigType *ty) { * SWIGTYPE * ----------------------------------------------------------------------------- */ -SwigType *SwigType_default_deduce(SwigType *t) { +SwigType *SwigType_default_deduce(const SwigType *t) { SwigType *r = NewStringEmpty(); List *l; Iterator it; @@ -511,7 +511,7 @@ String *SwigType_namestr(const SwigType *t) { * Create a C string representation of a datatype. * ----------------------------------------------------------------------------- */ -String *SwigType_str(SwigType *s, const_String_or_char_ptr id) { +String *SwigType_str(const SwigType *s, const_String_or_char_ptr id) { String *result; String *element = 0, *nextelement; List *elements; @@ -606,12 +606,12 @@ String *SwigType_str(SwigType *s, const_String_or_char_ptr id) { } /* ----------------------------------------------------------------------------- - * SwigType_ltype(SwigType *ty) + * SwigType_ltype(const SwigType *ty) * * Create a locally assignable type * ----------------------------------------------------------------------------- */ -SwigType *SwigType_ltype(SwigType *s) { +SwigType *SwigType_ltype(const SwigType *s) { String *result; String *element; SwigType *td, *tc = 0; @@ -709,7 +709,7 @@ SwigType *SwigType_ltype(SwigType *s) { * with an equivalent assignable version. * -------------------------------------------------------------------- */ -String *SwigType_lstr(SwigType *s, const_String_or_char_ptr id) { +String *SwigType_lstr(const SwigType *s, const_String_or_char_ptr id) { String *result; SwigType *tc; @@ -726,10 +726,11 @@ String *SwigType_lstr(SwigType *s, const_String_or_char_ptr id) { * datatype printed by str(). * ----------------------------------------------------------------------------- */ -String *SwigType_rcaststr(SwigType *s, const_String_or_char_ptr name) { +String *SwigType_rcaststr(const SwigType *s, const_String_or_char_ptr name) { String *result, *cast; String *element = 0, *nextelement; - SwigType *td, *rs, *tc = 0; + SwigType *td, *tc = 0; + const SwigType *rs; List *elements; int nelements, i; int clear = 1; @@ -873,7 +874,7 @@ String *SwigType_rcaststr(SwigType *s, const_String_or_char_ptr name) { * Casts a variable from the real type to the local datatype. * ----------------------------------------------------------------------------- */ -String *SwigType_lcaststr(SwigType *s, const_String_or_char_ptr name) { +String *SwigType_lcaststr(const SwigType *s, const_String_or_char_ptr name) { String *result; result = NewStringEmpty(); @@ -899,27 +900,163 @@ String *SwigType_lcaststr(SwigType *s, const_String_or_char_ptr name) { return result; } +#if 0 +/* Alternative implementation for manglestr_default. Mangling is similar to the original + except for a few subtle differences for example in templates: + namespace foo { + template<class T> class bar {}; + typedef int Integer; + void test2(bar<Integer *> *x); + } + Mangling is more consistent and changes from + _p_foo__barT_int_p_t to + _p_foo__barT_p_int_t. +*/ +static void mangle_stringcopy(String *destination, const char *source, int count) { + while (count-- > 0) { + char newc = '_'; + if (!(*source == '.' || *source == ':' || *source == ' ')) + newc = *source; + /* TODO: occasionally '*' or numerics need converting to '_', eg in array dimensions and template expressions */ + Putc(newc, destination); + source++; + } +} + +static void mangle_subtype(String *mangled, SwigType *s); + +/* ----------------------------------------------------------------------------- + * mangle_namestr() + * + * Mangles a type taking care of template expansions. Similar to SwigType_namestr(). + * The type may include a trailing '.', for example "p." + * ----------------------------------------------------------------------------- */ + +static void mangle_namestr(String *mangled, SwigType *t) { + int length = Len(t); + if (SwigType_isqualifier(t)) { + Append(mangled, "q_"); + mangle_stringcopy(mangled, Char(t)+2, length-4); + Append(mangled, "__"); + } else if (SwigType_ismemberpointer(t)) { + Append(mangled, "m_"); + mangle_stringcopy(mangled, Char(t)+2, length-4); + Append(mangled, "__"); + } else if (SwigType_isarray(t)) { + Append(mangled, "a_"); + mangle_stringcopy(mangled, Char(t)+2, length-4); + Append(mangled, "__"); + } else if (SwigType_isfunction(t)) { + List *p = SwigType_parmlist(t); + int sz = Len(p); + int i; + Append(mangled, "f_"); + for (i = 0; i < sz; i++) { + mangle_subtype(mangled, Getitem(p, i)); + Putc('_', mangled); + } + Append(mangled, (sz > 0) ? "_" : "__"); + } else if (SwigType_isvarargs(t)) { + Append(mangled, "___"); + } else { + char *d = Char(t); + char *c = strstr(d, "<("); + if (!c || !strstr(c + 2, ")>")) { + /* not a template type */ + mangle_stringcopy(mangled, Char(t), Len(t)); + } else { + /* a template type */ + String *suffix; + List *p; + int i, sz; + mangle_stringcopy(mangled, d, c-d); + Putc('T', mangled); + Putc('_', mangled); + + p = SwigType_parmlist(c + 1); + sz = Len(p); + for (i = 0; i < sz; i++) { + mangle_subtype(mangled, Getitem(p, i)); + Putc('_', mangled); + } + Putc('t', mangled); + suffix = SwigType_templatesuffix(t); + if (Len(suffix) > 0) { + mangle_namestr(mangled, suffix); + } else { + Append(mangled, suffix); + } + Delete(suffix); + Delete(p); + } + } +} + +static void mangle_subtype(String *mangled, SwigType *s) { + List *elements; + int nelements, i; + + assert(s); + elements = SwigType_split(s); + nelements = Len(elements); + for (i = 0; i < nelements; i++) { + SwigType *element = Getitem(elements, i); + mangle_namestr(mangled, element); + } + Delete(elements); +} + +static String *manglestr_default(const SwigType *s) { + String *mangled = NewString("_"); + SwigType *sr = SwigType_typedef_resolve_all(s); + SwigType *sq = SwigType_typedef_qualified(sr); + SwigType *ss = SwigType_remove_global_scope_prefix(sq); + SwigType *type = ss; + SwigType *lt; + + if (SwigType_istemplate(ss)) { + SwigType *ty = Swig_symbol_template_deftype(ss, 0); + Delete(ss); + ss = ty; + type = ss; + } + + lt = SwigType_ltype(type); -/* keep old mangling since Java codes need it */ -String *SwigType_manglestr_default(SwigType *s) { + Replace(lt, "struct ", "", DOH_REPLACE_ANY); + Replace(lt, "class ", "", DOH_REPLACE_ANY); + Replace(lt, "union ", "", DOH_REPLACE_ANY); + Replace(lt, "enum ", "", DOH_REPLACE_ANY); + + mangle_subtype(mangled, lt); + + Delete(ss); + Delete(sq); + Delete(sr); + + return mangled; +} + +#else + +static String *manglestr_default(const SwigType *s) { char *c; String *result = 0; String *base = 0; SwigType *lt; - SwigType *sr = SwigType_typedef_qualified(s); - SwigType *ss = SwigType_typedef_resolve_all(sr); - - s = ss; + SwigType *sr = SwigType_typedef_resolve_all(s); + SwigType *sq = SwigType_typedef_qualified(sr); + SwigType *ss = SwigType_remove_global_scope_prefix(sq); + SwigType *type = ss; if (SwigType_istemplate(ss)) { SwigType *ty = Swig_symbol_template_deftype(ss, 0); Delete(ss); ss = ty; - s = ss; + type = ss; } - Delete(sr); - lt = SwigType_ltype(s); + lt = SwigType_ltype(type); result = SwigType_prefix(lt); base = SwigType_base(lt); @@ -966,13 +1103,23 @@ String *SwigType_manglestr_default(SwigType *s) { Insert(result, 0, "_"); Delete(lt); Delete(base); - if (ss) - Delete(ss); + Delete(ss); + Delete(sq); + Delete(sr); return result; } - -String *SwigType_manglestr(SwigType *s) { - return SwigType_manglestr_default(s); +#endif + +String *SwigType_manglestr(const SwigType *s) { +#if 0 + /* Debugging checks to ensure a proper SwigType is passed in and not a stringified type */ + String *angle = Strstr(s, "<"); + if (angle && Strncmp(angle, "<(", 2) != 0) + Printf(stderr, "SwigType_manglestr error: %s\n", s); + else if (Strstr(s, "*") || Strstr(s, "&") || Strstr(s, "[")) + Printf(stderr, "SwigType_manglestr error: %s\n", s); +#endif + return manglestr_default(s); } /* ----------------------------------------------------------------------------- @@ -1091,7 +1238,7 @@ SwigType *SwigType_remove_global_scope_prefix(const SwigType *t) { * Checks type declarators for a match * ----------------------------------------------------------------------------- */ -int SwigType_check_decl(SwigType *ty, const SwigType *decl) { +int SwigType_check_decl(const SwigType *ty, const SwigType *decl) { SwigType *t, *t1, *t2; int r; t = SwigType_typedef_resolve_all(ty); |