Skip to content

Commit 564a918

Browse files
committed
Refactor upcasts code
In preparation for possible improvement in usage of SwigType_typedef_resolve_all - a SwigType* should be used, not a readable name in a String*.
1 parent 0425a61 commit 564a918

File tree

3 files changed

+86
-79
lines changed

3 files changed

+86
-79
lines changed

Source/Modules/csharp.cxx

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1709,11 +1709,11 @@ class CSHARP:public Language {
17091709
* addInterfaceNameAndUpcasts()
17101710
* ----------------------------------------------------------------------------- */
17111711

1712-
void addInterfaceNameAndUpcasts(SwigType *smart, String *interface_list, String *interface_upcasts, Hash *base_list, String *c_classname) {
1712+
void addInterfaceNameAndUpcasts(SwigType *smart, String *interface_list, String *interface_upcasts, Hash *base_list, SwigType *c_classname) {
17131713
List *keys = Keys(base_list);
17141714
for (Iterator it = First(keys); it.item; it = Next(it)) {
17151715
Node *base = Getattr(base_list, it.item);
1716-
String *c_baseclass = SwigType_namestr(Getattr(base, "name"));
1716+
SwigType *c_baseclassname = Getattr(base, "name");
17171717
String *interface_name = Getattr(base, "interface:name");
17181718
if (Len(interface_list))
17191719
Append(interface_list, ", ");
@@ -1733,12 +1733,11 @@ class CSHARP:public Language {
17331733
Replaceall(cptr_method_name, "$interfacename", interface_name);
17341734

17351735
String *upcast_method_name = Swig_name_member(getNSpace(), getClassPrefix(), cptr_method_name);
1736-
upcastsCode(smart, upcast_method_name, c_classname, c_baseclass);
1736+
upcastsCode(smart, upcast_method_name, c_classname, c_baseclassname);
17371737

17381738
Delete(upcast_method_name);
17391739
Delete(cptr_method_name);
17401740
Delete(interface_code);
1741-
Delete(c_baseclass);
17421741
}
17431742
Delete(keys);
17441743
}
@@ -1749,37 +1748,42 @@ class CSHARP:public Language {
17491748
* Add code for C++ casting to base class
17501749
* ----------------------------------------------------------------------------- */
17511750

1752-
void upcastsCode(SwigType *smart, String *upcast_method_name, String *c_classname, String *c_baseclass) {
1751+
void upcastsCode(SwigType *smart, String *upcast_method_name, SwigType *c_classname, SwigType *c_baseclassname) {
17531752
String *wname = Swig_name_wrapper(upcast_method_name);
17541753

17551754
Printv(imclass_cppcasts_code, "\n [global::System.Runtime.InteropServices.DllImport(\"", dllimport, "\", EntryPoint=\"", wname, "\")]\n", NIL);
17561755
Printf(imclass_cppcasts_code, " public static extern global::System.IntPtr %s(global::System.IntPtr jarg1);\n", upcast_method_name);
17571756

17581757
Replaceall(imclass_cppcasts_code, "$csclassname", proxy_class_name);
17591758

1759+
String *classname = SwigType_namestr(c_classname);
1760+
String *baseclassname = SwigType_namestr(c_baseclassname);
17601761
if (smart) {
17611762
String *smartnamestr = SwigType_namestr(smart);
17621763
String *bsmartnamestr = SwigType_namestr(smart);
17631764

1764-
SwigType *rclassname = SwigType_typedef_resolve_all(c_classname);
1765-
SwigType *rbaseclass = SwigType_typedef_resolve_all(c_baseclass);
1766-
Replaceall(bsmartnamestr, rclassname, rbaseclass);
1767-
1768-
Delete(rclassname);
1769-
Delete(rbaseclass);
1765+
SwigType *rclassname = SwigType_typedef_resolve_all(classname);
1766+
SwigType *rbaseclassname = SwigType_typedef_resolve_all(baseclassname);
1767+
Replaceall(bsmartnamestr, rclassname, rbaseclassname);
17701768

17711769
Printv(upcasts_code,
17721770
"SWIGEXPORT ", bsmartnamestr, " * SWIGSTDCALL ", wname, "(", smartnamestr, " *jarg1) {\n",
17731771
" return jarg1 ? new ", bsmartnamestr, "(*jarg1) : 0;\n"
17741772
"}\n", "\n", NIL);
1773+
1774+
Delete(rbaseclassname);
1775+
Delete(rclassname);
17751776
Delete(bsmartnamestr);
17761777
Delete(smartnamestr);
17771778
} else {
17781779
Printv(upcasts_code,
1779-
"SWIGEXPORT ", c_baseclass, " * SWIGSTDCALL ", wname, "(", c_classname, " *jarg1) {\n",
1780-
" return (", c_baseclass, " *)jarg1;\n"
1780+
"SWIGEXPORT ", baseclassname, " * SWIGSTDCALL ", wname, "(", classname, " *jarg1) {\n",
1781+
" return (", baseclassname, " *)jarg1;\n"
17811782
"}\n", "\n", NIL);
17821783
}
1784+
1785+
Delete(baseclassname);
1786+
Delete(classname);
17831787
Delete(wname);
17841788
}
17851789

@@ -1788,10 +1792,9 @@ class CSHARP:public Language {
17881792
* ----------------------------------------------------------------------------- */
17891793

17901794
void emitProxyClassDefAndCPPCasts(Node *n) {
1791-
String *c_classname = SwigType_namestr(Getattr(n, "name"));
1792-
String *c_baseclass = NULL;
1795+
SwigType *c_classname = Getattr(n, "name");
1796+
SwigType *c_baseclassname = NULL;
17931797
String *baseclass = NULL;
1794-
String *c_baseclassname = NULL;
17951798
String *interface_list = NewStringEmpty();
17961799
String *interface_upcasts = NewStringEmpty();
17971800
SwigType *typemap_lookup_type = Getattr(n, "classtypeobj");
@@ -1813,12 +1816,13 @@ class CSHARP:public Language {
18131816
Iterator base = First(baselist);
18141817
while (base.item) {
18151818
if (!(GetFlag(base.item, "feature:ignore") || Getattr(base.item, "feature:interface"))) {
1816-
String *baseclassname = Getattr(base.item, "name");
1819+
SwigType *baseclassname = Getattr(base.item, "name");
18171820
if (!c_baseclassname) {
1818-
c_baseclassname = baseclassname;
1819-
baseclass = Copy(getProxyName(baseclassname));
1820-
if (baseclass)
1821-
c_baseclass = SwigType_namestr(baseclassname);
1821+
String *name = getProxyName(baseclassname);
1822+
if (name) {
1823+
c_baseclassname = baseclassname;
1824+
baseclass = name;
1825+
}
18221826
} else {
18231827
/* Warn about multiple inheritance for additional base class(es) */
18241828
String *proxyclassname = Getattr(n, "classtypeobj");
@@ -1834,15 +1838,14 @@ class CSHARP:public Language {
18341838
if (interface_bases)
18351839
addInterfaceNameAndUpcasts(smart, interface_list, interface_upcasts, interface_bases, c_classname);
18361840

1837-
bool derived = baseclass && getProxyName(c_baseclassname);
1841+
bool derived = baseclass != 0;
18381842
if (derived && purebase_notderived)
18391843
pure_baseclass = empty_string;
18401844
const String *wanted_base = baseclass ? baseclass : pure_baseclass;
18411845

18421846
if (purebase_replace) {
18431847
wanted_base = pure_baseclass;
18441848
derived = false;
1845-
Delete(baseclass);
18461849
baseclass = NULL;
18471850
if (purebase_notderived)
18481851
Swig_error(Getfile(n), Getline(n), "The csbase typemap for proxy %s must contain just one of the 'replace' or 'notderived' attributes.\n", typemap_lookup_type);
@@ -2033,12 +2036,11 @@ class CSHARP:public Language {
20332036

20342037
if (derived) {
20352038
String *upcast_method_name = Swig_name_member(getNSpace(), getClassPrefix(), smart != 0 ? "SWIGSmartPtrUpcast" : "SWIGUpcast");
2036-
upcastsCode(smart, upcast_method_name, c_classname, c_baseclass);
2039+
upcastsCode(smart, upcast_method_name, c_classname, c_baseclassname);
20372040
Delete(upcast_method_name);
20382041
}
20392042

20402043
Delete(smart);
2041-
Delete(baseclass);
20422044
}
20432045

20442046
/* ----------------------------------------------------------------------

Source/Modules/d.cxx

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3140,11 +3140,10 @@ class D : public Language {
31403140
* Handle inheriting from D and C++ classes.
31413141
*/
31423142

3143-
String *c_classname = SwigType_namestr(Getattr(n, "name"));
3144-
String *c_baseclass = NULL;
3145-
Node *basenode = NULL;
3146-
String *basename = NULL;
3143+
String *c_classname = Getattr(n, "name");
31473144
String *c_baseclassname = NULL;
3145+
Node *basenode = NULL;
3146+
String *baseclass = NULL;
31483147

31493148
// Inheritance from pure D classes.
31503149
Node *attributes = NewHash();
@@ -3161,13 +3160,14 @@ class D : public Language {
31613160
Iterator base = First(baselist);
31623161
while (base.item) {
31633162
if (!GetFlag(base.item, "feature:ignore")) {
3164-
String *baseclassname = Getattr(base.item, "name");
3163+
SwigType *baseclassname = Getattr(base.item, "name");
31653164
if (!c_baseclassname) {
31663165
basenode = base.item;
3167-
c_baseclassname = baseclassname;
3168-
basename = createProxyName(c_baseclassname);
3169-
if (basename)
3170-
c_baseclass = SwigType_namestr(baseclassname);
3166+
String *name = createProxyName(baseclassname);
3167+
if (name) {
3168+
c_baseclassname = baseclassname;
3169+
baseclass = name;
3170+
}
31713171
} else {
31723172
/* Warn about multiple inheritance for additional base class(es) */
31733173
String *proxyclassname = Getattr(n, "classtypeobj");
@@ -3180,33 +3180,32 @@ class D : public Language {
31803180
}
31813181
}
31823182

3183-
bool derived = (basename != NULL);
3183+
bool derived = baseclass != NULL;
31843184

31853185
if (derived && purebase_notderived) {
31863186
pure_baseclass = empty_string;
31873187
}
3188-
const String *wanted_base = basename ? basename : pure_baseclass;
3188+
const String *wanted_base = baseclass ? baseclass : pure_baseclass;
31893189

31903190
if (purebase_replace) {
31913191
wanted_base = pure_baseclass;
31923192
derived = false;
31933193
basenode = NULL;
3194-
Delete(basename);
3195-
basename = NULL;
3194+
baseclass = NULL;
31963195
if (purebase_notderived) {
31973196
Swig_error(Getfile(n), Getline(n),
31983197
"The dbase typemap for proxy %s must contain just one of the 'replace' or 'notderived' attributes.\n",
31993198
typemap_lookup_type);
32003199
}
3201-
} else if (basename && Len(pure_baseclass) > 0) {
3200+
} else if (baseclass && Len(pure_baseclass) > 0) {
32023201
Swig_warning(WARN_D_MULTIPLE_INHERITANCE, Getfile(n), Getline(n),
32033202
"Warning for %s, base class %s ignored. Multiple inheritance is not supported in D. "
32043203
"Perhaps you need one of the 'replace' or 'notderived' attributes in the dbase typemap?\n", typemap_lookup_type, pure_baseclass);
32053204
}
32063205

32073206
// Add code to do C++ casting to base class (only for classes in an inheritance hierarchy)
32083207
if (derived) {
3209-
writeClassUpcast(n, proxy_class_name, c_classname, c_baseclass);
3208+
writeClassUpcast(n, proxy_class_name, c_classname, c_baseclassname);
32103209
}
32113210

32123211
/*
@@ -3354,8 +3353,7 @@ class D : public Language {
33543353
// Write the class body and the curly bracket closing the class definition
33553354
// to the proxy module.
33563355
indentCode(body);
3357-
Replaceall(body, "$dbaseclass", basename);
3358-
Delete(basename);
3356+
Replaceall(body, "$dbaseclass", baseclass);
33593357

33603358
Printv(proxy_class_code, body, "\n}\n", NIL);
33613359
Delete(body);
@@ -3368,7 +3366,7 @@ class D : public Language {
33683366
/* ---------------------------------------------------------------------------
33693367
* D::writeClassUpcast()
33703368
* --------------------------------------------------------------------------- */
3371-
void writeClassUpcast(Node *n, const String* d_class_name, String* c_class_name, String* c_base_name) {
3369+
void writeClassUpcast(Node *n, const String* d_class_name, SwigType* c_classname, SwigType* c_baseclassname) {
33723370

33733371
SwigType *smart = Swig_cparse_smartptr(n);
33743372
String *upcast_name = Swig_name_member(getNSpace(), d_class_name, (smart != 0 ? "SmartPtrUpcast" : "Upcast"));
@@ -3377,37 +3375,41 @@ class D : public Language {
33773375
writeImDModuleFunction(upcast_name, "void*", "(void* objectRef)",
33783376
upcast_wrapper_name);
33793377

3378+
String *classname = SwigType_namestr(c_classname);
3379+
String *baseclassname = SwigType_namestr(c_baseclassname);
33803380
if (smart) {
33813381
String *smartnamestr = SwigType_namestr(smart);
33823382
String *bsmartnamestr = SwigType_namestr(smart);
33833383

3384-
SwigType *rclassname = SwigType_typedef_resolve_all(c_class_name);
3385-
SwigType *rbaseclass = SwigType_typedef_resolve_all(c_base_name);
3386-
Replaceall(bsmartnamestr, rclassname, rbaseclass);
3387-
3388-
Delete(rclassname);
3389-
Delete(rbaseclass);
3384+
SwigType *rclassname = SwigType_typedef_resolve_all(classname);
3385+
SwigType *rbaseclassname = SwigType_typedef_resolve_all(baseclassname);
3386+
Replaceall(bsmartnamestr, rclassname, rbaseclassname);
33903387

33913388
Printv(upcasts_code,
33923389
"SWIGEXPORT ", bsmartnamestr, " * ", upcast_wrapper_name,
33933390
"(", smartnamestr, " *objectRef) {\n",
33943391
" return objectRef ? new ", bsmartnamestr, "(*objectRef) : 0;\n"
33953392
"}\n",
33963393
"\n", NIL);
3394+
3395+
Delete(rbaseclassname);
3396+
Delete(rclassname);
33973397
Delete(bsmartnamestr);
33983398
Delete(smartnamestr);
33993399
} else {
34003400
Printv(upcasts_code,
3401-
"SWIGEXPORT ", c_base_name, " * ", upcast_wrapper_name,
3402-
"(", c_base_name, " *objectRef) {\n",
3403-
" return (", c_base_name, " *)objectRef;\n"
3401+
"SWIGEXPORT ", baseclassname, " * ", upcast_wrapper_name,
3402+
"(", baseclassname, " *objectRef) {\n",
3403+
" return (", baseclassname, " *)objectRef;\n"
34043404
"}\n",
34053405
"\n", NIL);
34063406
}
34073407

3408-
Replaceall(upcasts_code, "$cclass", c_class_name);
3409-
Replaceall(upcasts_code, "$cbaseclass", c_base_name);
3408+
Replaceall(upcasts_code, "$cclass", classname);
3409+
Replaceall(upcasts_code, "$cbaseclass", baseclassname);
34103410

3411+
Delete(baseclassname);
3412+
Delete(classname);
34113413
Delete(upcast_name);
34123414
Delete(upcast_wrapper_name);
34133415
Delete(smart);

0 commit comments

Comments
 (0)