Merge branch 'nested' - nested structs/classes support

* nested:
  Deprecation of the 'nestedworkaround' feature
  Ensure -c++out is not used with -c++
  Add missing header to new source file
  Nested C class setters restored in c++out mode for Octave
  Classprefix fixed after private nested classes some comments and spaces added
  Fix template partial specialization detection
  Minor tweaks in Swig_feature_set
  Swig_offset_string moved to misc.c
  nested private classes are discarded while parsing nested relate functions are moved to nested.cxx and renamed accordingly
  out-of-scope template definitions fixed nested_private test disabled again
  fixed out-of-scope nested class definitions, added a test enabled nested C structs assignment (still disabled for Octave), added Java runtime test fixed nested_private test case for Java & C#
  Testcase of private nested class usage causing segfault
  C nested struct passed by value example
  Add in Travis testing for nested branch
  Add C++ nested class example
  Minor code improvements
  Cosmetics/code beautification of nested class support
  Nested classes support
This commit is contained in:
William S Fulton 2013-12-14 15:12:07 +00:00
commit 314fae460b
61 changed files with 2438 additions and 1127 deletions

View file

@ -559,6 +559,8 @@ Allocate():
virtual int classDeclaration(Node *n) {
Symtab *symtab = Swig_symbol_current();
Swig_symbol_setscope(Getattr(n, "symtab"));
Node *oldInclass = inclass;
AccessMode oldAcessMode = cplus_mode;
if (!CPlusPlus) {
/* Always have default constructors/destructors in C */
@ -580,7 +582,6 @@ Allocate():
}
}
}
inclass = n;
String *kind = Getattr(n, "kind");
if (Strcmp(kind, "class") == 0) {
@ -728,7 +729,8 @@ Allocate():
/* Only care about default behavior. Remove temporary values */
Setattr(n, "allocate:visit", "1");
inclass = 0;
inclass = oldInclass;
cplus_mode = oldAcessMode;
Swig_symbol_setscope(symtab);
return SWIG_OK;
}

View file

@ -342,11 +342,13 @@ int Contracts::namespaceDeclaration(Node *n) {
int Contracts::classDeclaration(Node *n) {
int ret = SWIG_OK;
int oldInClass = InClass;
Node *oldClass = CurrentClass;
InClass = 1;
CurrentClass = n;
emit_children(n);
InClass = 0;
CurrentClass = 0;
InClass = oldInClass;
CurrentClass = oldClass;
return ret;
}

View file

@ -53,7 +53,6 @@ class CSHARP:public Language {
String *proxy_class_code;
String *module_class_code;
String *proxy_class_name; // proxy class name
String *full_proxy_class_name;// fully qualified proxy class name when using nspace feature, otherwise same as proxy_class_name
String *full_imclass_name; // fully qualified intermediary class name when using nspace feature, otherwise same as imclass_name
String *variable_name; //Name of a variable being wrapped
String *proxy_class_constants_code;
@ -87,6 +86,7 @@ class CSHARP:public Language {
int n_directors;
int first_class_dmethod;
int curr_class_dmethod;
int nesting_depth;
enum EnumFeature { SimpleEnum, TypeunsafeEnum, TypesafeEnum, ProperEnum };
@ -125,7 +125,6 @@ public:
proxy_class_code(NULL),
module_class_code(NULL),
proxy_class_name(NULL),
full_proxy_class_name(NULL),
full_imclass_name(NULL),
variable_name(NULL),
proxy_class_constants_code(NULL),
@ -156,7 +155,8 @@ public:
n_dmethods(0),
n_directors(0),
first_class_dmethod(0),
curr_class_dmethod(0) {
curr_class_dmethod(0),
nesting_depth(0){
/* for now, multiple inheritance in directors is disabled, this
should be easy to implement though */
director_multiple_inheritance = 0;
@ -179,7 +179,13 @@ public:
proxyname = Getattr(n, "proxyname");
if (!proxyname) {
String *nspace = Getattr(n, "sym:nspace");
String *symname = Getattr(n, "sym:name");
String *symname = Copy(Getattr(n, "sym:name"));
if (symname && !GetFlag(n, "feature:flatnested")) {
for (Node *outer_class = Getattr(n, "nested:outer"); outer_class; outer_class = Getattr(outer_class, "nested:outer")) {
Push(symname, ".");
Push(symname, Getattr(outer_class, "sym:name"));
}
}
if (nspace) {
if (namespce)
proxyname = NewStringf("%s.%s.%s", namespce, nspace, symname);
@ -190,12 +196,33 @@ public:
}
Setattr(n, "proxyname", proxyname);
Delete(proxyname);
Delete(symname);
}
}
}
return proxyname;
}
/* -----------------------------------------------------------------------------
* directorClassName()
* ----------------------------------------------------------------------------- */
String *directorClassName(Node *n) {
String *dirclassname;
const char *attrib = "director:classname";
if (!(dirclassname = Getattr(n, attrib))) {
String *classname = getClassPrefix();
dirclassname = NewStringf("SwigDirector_%s", classname);
Setattr(n, attrib, dirclassname);
}
else
dirclassname = Copy(dirclassname);
return dirclassname;
}
/* ------------------------------------------------------------
* main()
* ------------------------------------------------------------ */
@ -1025,7 +1052,7 @@ public:
*/
if (proxy_flag && wrapping_member_flag && !enum_constant_flag) {
// Capitalize the first letter in the variable in the getter/setter function name
bool getter_flag = Cmp(symname, Swig_name_set(getNSpace(), Swig_name_member(0, proxy_class_name, variable_name))) != 0;
bool getter_flag = Cmp(symname, Swig_name_set(getNSpace(), Swig_name_member(0, getClassPrefix(), variable_name))) != 0;
String *getter_setter_name = NewString("");
if (!getter_flag)
@ -1589,6 +1616,7 @@ public:
String *c_baseclassname = NULL;
SwigType *typemap_lookup_type = Getattr(n, "classtypeobj");
bool feature_director = Swig_directorclass(n) ? true : false;
bool has_outerclass = Getattr(n, "nested:outer") != 0 && !GetFlag(n, "feature:flatnested");
// Inheritance from pure C# classes
Node *attributes = NewHash();
@ -1648,7 +1676,8 @@ public:
// Pure C# interfaces
const String *pure_interfaces = typemapLookup(n, derived ? "csinterfaces_derived" : "csinterfaces", typemap_lookup_type, WARN_NONE);
// Start writing the proxy class
Printv(proxy_class_def, typemapLookup(n, "csimports", typemap_lookup_type, WARN_NONE), // Import statements
if (!has_outerclass)
Printv(proxy_class_def, typemapLookup(n, "csimports", typemap_lookup_type, WARN_NONE), // Import statements
"\n", NIL);
// Class attributes
@ -1719,7 +1748,7 @@ public:
Printf(proxy_class_code, " if (SwigDerivedClassHasMethod(\"%s\", swigMethodTypes%s))\n", method, methid);
Printf(proxy_class_code, " swigDelegate%s = new SwigDelegate%s_%s(SwigDirector%s);\n", methid, proxy_class_name, methid, overname);
}
String *director_connect_method_name = Swig_name_member(getNSpace(), proxy_class_name, "director_connect");
String *director_connect_method_name = Swig_name_member(getNSpace(), getClassPrefix(), "director_connect");
Printf(proxy_class_code, " %s.%s(swigCPtr", imclass_name, director_connect_method_name);
for (i = first_class_dmethod; i < curr_class_dmethod; ++i) {
UpcallData *udata = Getitem(dmethods_seq, i);
@ -1795,7 +1824,7 @@ public:
// Add code to do C++ casting to base class (only for classes in an inheritance hierarchy)
if (derived) {
String *smartptr = Getattr(n, "feature:smartptr");
String *upcast_method = Swig_name_member(getNSpace(), proxy_class_name, smartptr != 0 ? "SWIGSmartPtrUpcast" : "SWIGUpcast");
String *upcast_method = Swig_name_member(getNSpace(), getClassPrefix(), smartptr != 0 ? "SWIGSmartPtrUpcast" : "SWIGUpcast");
String *wname = Swig_name_wrapper(upcast_method);
Printv(imclass_cppcasts_code, "\n [global::System.Runtime.InteropServices.DllImport(\"", dllimport, "\", EntryPoint=\"", wname, "\")]\n", NIL);
@ -1846,11 +1875,35 @@ public:
String *nspace = getNSpace();
File *f_proxy = NULL;
// save class local variables
String *old_proxy_class_name = proxy_class_name;
String *old_full_imclass_name = full_imclass_name;
String *old_destructor_call = destructor_call;
String *old_proxy_class_constants_code = proxy_class_constants_code;
String *old_proxy_class_def = proxy_class_def;
String *old_proxy_class_code = proxy_class_code;
if (proxy_flag) {
proxy_class_name = NewString(Getattr(n, "sym:name"));
if (Node *outer = Getattr(n, "nested:outer")) {
String *outerClassesPrefix = Copy(Getattr(outer, "sym:name"));
for (outer = Getattr(outer, "nested:outer"); outer != 0; outer = Getattr(outer, "nested:outer")) {
Push(outerClassesPrefix, "::");
Push(outerClassesPrefix, Getattr(outer, "sym:name"));
}
String *fnspace = nspace ? NewStringf("%s::%s", nspace, outerClassesPrefix) : outerClassesPrefix;
if (!addSymbol(proxy_class_name, n, fnspace))
return SWIG_ERROR;
if (nspace)
Delete(fnspace);
Delete(outerClassesPrefix);
}
else {
if (!addSymbol(proxy_class_name, n, nspace))
return SWIG_ERROR;
}
if (!nspace) {
full_proxy_class_name = NewStringf("%s", proxy_class_name);
full_imclass_name = NewStringf("%s", imclass_name);
if (Cmp(proxy_class_name, imclass_name) == 0) {
Printf(stderr, "Class name cannot be equal to intermediary class name: %s\n", proxy_class_name);
@ -1863,36 +1916,34 @@ public:
}
} else {
if (namespce) {
full_proxy_class_name = NewStringf("%s.%s.%s", namespce, nspace, proxy_class_name);
full_imclass_name = NewStringf("%s.%s", namespce, imclass_name);
} else {
full_proxy_class_name = NewStringf("%s.%s", nspace, proxy_class_name);
full_imclass_name = NewStringf("%s", imclass_name);
}
}
if (!addSymbol(proxy_class_name, n, nspace))
return SWIG_ERROR;
// inner class doesn't need this prologue
if (!Getattr(n, "nested:outer")) {
String *output_directory = outputDirectory(nspace);
String *filen = NewStringf("%s%s.cs", output_directory, proxy_class_name);
f_proxy = NewFile(filen, "w", SWIG_output_files());
if (!f_proxy) {
FileErrorDisplay(filen);
SWIG_exit(EXIT_FAILURE);
}
Append(filenames_list, Copy(filen));
Delete(filen);
filen = NULL;
String *output_directory = outputDirectory(nspace);
String *filen = NewStringf("%s%s.cs", output_directory, proxy_class_name);
f_proxy = NewFile(filen, "w", SWIG_output_files());
if (!f_proxy) {
FileErrorDisplay(filen);
SWIG_exit(EXIT_FAILURE);
// Start writing out the proxy class file
emitBanner(f_proxy);
addOpenNamespace(nspace, f_proxy);
}
Append(filenames_list, Copy(filen));
Delete(filen);
filen = NULL;
// Start writing out the proxy class file
emitBanner(f_proxy);
addOpenNamespace(nspace, f_proxy);
Clear(proxy_class_def);
Clear(proxy_class_code);
else
++nesting_depth;
proxy_class_def = NewString("");
proxy_class_code = NewString("");
destructor_call = NewString("");
proxy_class_constants_code = NewString("");
}
@ -1903,7 +1954,7 @@ public:
emitProxyClassDefAndCPPCasts(n);
String *csclazzname = Swig_name_member(getNSpace(), proxy_class_name, ""); // mangled full proxy class name
String *csclazzname = Swig_name_member(getNSpace(), getClassPrefix(), ""); // mangled full proxy class name
Replaceall(proxy_class_def, "$csclassname", proxy_class_name);
Replaceall(proxy_class_code, "$csclassname", proxy_class_name);
@ -1924,17 +1975,36 @@ public:
Replaceall(proxy_class_def, "$dllimport", dllimport);
Replaceall(proxy_class_code, "$dllimport", dllimport);
Replaceall(proxy_class_constants_code, "$dllimport", dllimport);
Printv(f_proxy, proxy_class_def, proxy_class_code, NIL);
bool has_outerclass = Getattr(n, "nested:outer") != 0 && !GetFlag(n, "feature:flatnested");
if (!has_outerclass)
Printv(f_proxy, proxy_class_def, proxy_class_code, NIL);
else {
Swig_offset_string(proxy_class_def, nesting_depth);
Append(old_proxy_class_code, proxy_class_def);
Swig_offset_string(proxy_class_code, nesting_depth);
Append(old_proxy_class_code, proxy_class_code);
}
// Write out all the constants
if (Len(proxy_class_constants_code) != 0)
Printv(f_proxy, proxy_class_constants_code, NIL);
Printf(f_proxy, "}\n");
addCloseNamespace(nspace, f_proxy);
Delete(f_proxy);
f_proxy = NULL;
if (Len(proxy_class_constants_code) != 0) {
if (!has_outerclass)
Printv(f_proxy, proxy_class_constants_code, NIL);
else {
Swig_offset_string(proxy_class_constants_code, nesting_depth);
Append(old_proxy_class_code, proxy_class_constants_code);
}
}
if (!has_outerclass) {
Printf(f_proxy, "}\n");
addCloseNamespace(nspace, f_proxy);
Delete(f_proxy);
f_proxy = NULL;
} else {
for (int i = 0; i < nesting_depth; ++i)
Append(old_proxy_class_code, " ");
Append(old_proxy_class_code, "}\n\n");
--nesting_depth;
}
/* Output the downcast method, if necessary. Note: There's no other really
good place to put this code, since Abstract Base Classes (ABCs) can and should have
@ -1971,17 +2041,18 @@ public:
Delete(csclazzname);
Delete(proxy_class_name);
proxy_class_name = NULL;
Delete(full_proxy_class_name);
full_proxy_class_name = NULL;
proxy_class_name = old_proxy_class_name;
Delete(full_imclass_name);
full_imclass_name = NULL;
full_imclass_name = old_full_imclass_name;
Delete(destructor_call);
destructor_call = NULL;
destructor_call = old_destructor_call;
Delete(proxy_class_constants_code);
proxy_class_constants_code = NULL;
proxy_class_constants_code = old_proxy_class_constants_code;
Delete(proxy_class_def);
proxy_class_def = old_proxy_class_def;
Delete(proxy_class_code);
proxy_class_code = old_proxy_class_code;
}
return SWIG_OK;
}
@ -1994,7 +2065,7 @@ public:
if (proxy_flag) {
String *overloaded_name = getOverloadedName(n);
String *intermediary_function_name = Swig_name_member(getNSpace(), proxy_class_name, overloaded_name);
String *intermediary_function_name = Swig_name_member(getNSpace(), getClassPrefix(), overloaded_name);
Setattr(n, "proxyfuncname", Getattr(n, "sym:name"));
Setattr(n, "imfuncname", intermediary_function_name);
proxyClassFunctionHandler(n);
@ -2014,7 +2085,7 @@ public:
if (proxy_flag) {
String *overloaded_name = getOverloadedName(n);
String *intermediary_function_name = Swig_name_member(getNSpace(), proxy_class_name, overloaded_name);
String *intermediary_function_name = Swig_name_member(getNSpace(), getClassPrefix(), overloaded_name);
Setattr(n, "proxyfuncname", Getattr(n, "sym:name"));
Setattr(n, "imfuncname", intermediary_function_name);
proxyClassFunctionHandler(n);
@ -2094,7 +2165,7 @@ public:
if (wrapping_member_flag && !enum_constant_flag) {
// Properties
setter_flag = (Cmp(Getattr(n, "sym:name"), Swig_name_set(getNSpace(), Swig_name_member(0, proxy_class_name, variable_name))) == 0);
setter_flag = (Cmp(Getattr(n, "sym:name"), Swig_name_set(getNSpace(), Swig_name_member(0, getClassPrefix(), variable_name))) == 0);
if (setter_flag)
Swig_typemap_attach_parms("csvarin", l, NULL);
}
@ -2264,7 +2335,7 @@ public:
Node *explicit_n = Getattr(n, "explicitcallnode");
if (explicit_n) {
String *ex_overloaded_name = getOverloadedName(explicit_n);
String *ex_intermediary_function_name = Swig_name_member(getNSpace(), proxy_class_name, ex_overloaded_name);
String *ex_intermediary_function_name = Swig_name_member(getNSpace(), getClassPrefix(), ex_overloaded_name);
String *ex_imcall = Copy(imcall);
Replaceall(ex_imcall, "$imfuncname", ex_intermediary_function_name);
@ -3376,14 +3447,21 @@ public:
// Output the director connect method:
String *norm_name = SwigType_namestr(Getattr(n, "name"));
String *swig_director_connect = Swig_name_member(getNSpace(), proxy_class_name, "director_connect");
String *dirclassname = directorClassName(n);
String *swig_director_connect = Swig_name_member(getNSpace(), getClassPrefix(), "director_connect");
String *wname = Swig_name_wrapper(swig_director_connect);
String *sym_name = Getattr(n, "sym:name");
String *qualified_classname = Copy(sym_name);
String *nspace = getNSpace();
String *dirClassName = directorClassName(n);
String *smartptr = Getattr(n, "feature:smartptr");
if (!GetFlag(n, "feature:flatnested")) {
for (Node *outer_class = Getattr(n, "nested:outer"); outer_class; outer_class = Getattr(outer_class, "nested:outer")) {
Push(qualified_classname, ".");
Push(qualified_classname, Getattr(outer_class, "sym:name"));
}
}
if (nspace)
Insert(qualified_classname, 0, NewStringf("%s.", nspace));
@ -3415,7 +3493,7 @@ public:
Printf(code_wrap->def, ", ");
if (i != first_class_dmethod)
Printf(code_wrap->code, ", ");
Printf(code_wrap->def, "%s::SWIG_Callback%s_t callback%s", dirClassName, methid, methid);
Printf(code_wrap->def, "%s::SWIG_Callback%s_t callback%s", dirclassname, methid, methid);
Printf(code_wrap->code, "callback%s", methid);
Printf(imclass_class_code, ", %s.SwigDelegate%s_%s delegate%s", qualified_classname, sym_name, methid, methid);
}
@ -3432,7 +3510,7 @@ public:
Delete(wname);
Delete(swig_director_connect);
Delete(qualified_classname);
Delete(dirClassName);
Delete(dirclassname);
}
/* ---------------------------------------------------------------
@ -3485,7 +3563,7 @@ public:
// we're consistent with the sym:overload name in functionWrapper. (?? when
// does the overloaded method name get set?)
imclass_dmethod = NewStringf("SwigDirector_%s", Swig_name_member(getNSpace(), classname, overloaded_name));
imclass_dmethod = NewStringf("SwigDirector_%s", Swig_name_member(getNSpace(), getClassPrefix(), overloaded_name));
qualified_return = SwigType_rcaststr(returntype, "c_result");
@ -3931,6 +4009,7 @@ public:
Delete(proxy_method_types);
Delete(callback_def);
Delete(callback_code);
Delete(dirclassname);
DelWrapper(w);
return status;
@ -3970,13 +4049,11 @@ public:
String *basetype = Getattr(parent, "classtype");
String *target = Swig_method_decl(0, decl, dirclassname, parms, 0, 0);
String *call = Swig_csuperclass_call(0, basetype, superparms);
String *classtype = SwigType_namestr(Getattr(n, "name"));
Printf(f_directors, "%s::%s : %s, %s {\n", dirclassname, target, call, Getattr(parent, "director:ctor"));
Printf(f_directors, " swig_init_callbacks();\n");
Printf(f_directors, "}\n\n");
Delete(classtype);
Delete(target);
Delete(call);
}
@ -4047,6 +4124,29 @@ public:
return Language::classDirectorInit(n);
}
int classDeclaration(Node *n) {
String *old_director_callback_typedefs = director_callback_typedefs;
String *old_director_callbacks = director_callbacks;
String *old_director_delegate_callback = director_delegate_callback;
String *old_director_delegate_definitions = director_delegate_definitions;
String *old_director_delegate_instances = director_delegate_instances;
String *old_director_method_types = director_method_types;
String *old_director_connect_parms = director_connect_parms;
int ret = Language::classDeclaration(n);
// these variables are deleted in emitProxyClassDefAndCPPCasts, hence no Delete here
director_callback_typedefs = old_director_callback_typedefs;
director_callbacks = old_director_callbacks;
director_delegate_callback = old_director_delegate_callback;
director_delegate_definitions = old_director_delegate_definitions;
director_delegate_instances = old_director_delegate_instances;
director_method_types = old_director_method_types;
director_connect_parms = old_director_connect_parms;
return ret;
}
/* ----------------------------------------------------------------------
* classDirectorDestructor()
* ---------------------------------------------------------------------- */
@ -4079,7 +4179,7 @@ public:
int classDirectorEnd(Node *n) {
int i;
String *director_classname = directorClassName(n);
String *dirclassname = directorClassName(n);
Wrapper *w = NewWrapper();
@ -4089,7 +4189,7 @@ public:
Printf(f_directors_h, " void swig_connect_director(");
Printf(w->def, "void %s::swig_connect_director(", director_classname);
Printf(w->def, "void %s::swig_connect_director(", dirclassname);
for (i = first_class_dmethod; i < curr_class_dmethod; ++i) {
UpcallData *udata = Getitem(dmethods_seq, i);
@ -4116,7 +4216,7 @@ public:
Printf(f_directors_h, "};\n\n");
Printf(w->code, "}\n\n");
Printf(w->code, "void %s::swig_init_callbacks() {\n", director_classname);
Printf(w->code, "void %s::swig_init_callbacks() {\n", dirclassname);
for (i = first_class_dmethod; i < curr_class_dmethod; ++i) {
UpcallData *udata = Getitem(dmethods_seq, i);
String *overname = Getattr(udata, "overname");
@ -4127,6 +4227,7 @@ public:
Wrapper_print(w, f_directors);
DelWrapper(w);
Delete(dirclassname);
return Language::classDirectorEnd(n);
}
@ -4159,8 +4260,8 @@ public:
String *base = Getattr(n, "classtype");
String *class_ctor = NewString("Swig::Director()");
String *directorname = directorClassName(n);
String *declaration = Swig_class_declaration(n, directorname);
String *dirclassname = directorClassName(n);
String *declaration = Swig_class_declaration(n, dirclassname);
Printf(declaration, " : public %s, public Swig::Director", base);
@ -4168,9 +4269,12 @@ public:
Setattr(n, "director:decl", declaration);
Setattr(n, "director:ctor", class_ctor);
Delete(directorname);
Delete(dirclassname);
}
bool nestedClassesSupported() const {
return true;
}
}; /* class CSHARP */
/* -----------------------------------------------------------------------------

View file

@ -86,6 +86,7 @@ class JAVA:public Language {
int n_directors;
int first_class_dmethod;
int curr_class_dmethod;
int nesting_depth;
enum EnumFeature { SimpleEnum, TypeunsafeEnum, TypesafeEnum, ProperEnum };
@ -154,7 +155,8 @@ public:
n_dmethods(0),
n_directors(0),
first_class_dmethod(0),
curr_class_dmethod(0) {
curr_class_dmethod(0),
nesting_depth(0){
/* for now, multiple inheritance in directors is disabled, this
should be easy to implement though */
director_multiple_inheritance = 0;
@ -204,7 +206,13 @@ public:
proxyname = Getattr(n, "proxyname");
if (!proxyname || jnidescriptor) {
String *nspace = Getattr(n, "sym:nspace");
String *symname = Getattr(n, "sym:name");
String *symname = Copy(Getattr(n, "sym:name"));
if (symname && !GetFlag(n, "feature:flatnested")) {
for (Node *outer_class = Getattr(n, "nested:outer"); outer_class; outer_class = Getattr(outer_class, "nested:outer")) {
Push(symname, ".");
Push(symname, Getattr(outer_class, "sym:name"));
}
}
if (nspace) {
if (package && !jnidescriptor)
proxyname = NewStringf("%s.%s.%s", package, nspace, symname);
@ -217,6 +225,7 @@ public:
Setattr(n, "proxyname", proxyname); // Cache it
Delete(proxyname);
}
Delete(symname);
}
}
}
@ -1134,7 +1143,7 @@ public:
*/
if (proxy_flag && wrapping_member_flag && !enum_constant_flag) {
// Capitalize the first letter in the variable to create a JavaBean type getter/setter function name
bool getter_flag = Cmp(symname, Swig_name_set(getNSpace(), Swig_name_member(0, proxy_class_name, variable_name))) != 0;
bool getter_flag = Cmp(symname, Swig_name_set(getNSpace(), Swig_name_member(0, getClassPrefix(), variable_name))) != 0;
String *getter_setter_name = NewString("");
if (!getter_flag)
@ -1713,6 +1722,7 @@ public:
String *c_baseclassname = NULL;
SwigType *typemap_lookup_type = Getattr(n, "classtypeobj");
bool feature_director = Swig_directorclass(n) ? true : false;
bool has_outerclass = Getattr(n, "nested:outer") != 0 && !GetFlag(n, "feature:flatnested");
// Inheritance from pure Java classes
Node *attributes = NewHash();
@ -1773,8 +1783,11 @@ public:
const String *pure_interfaces = typemapLookup(n, "javainterfaces", typemap_lookup_type, WARN_NONE);
// Start writing the proxy class
Printv(proxy_class_def, typemapLookup(n, "javaimports", typemap_lookup_type, WARN_NONE), // Import statements
"\n", typemapLookup(n, "javaclassmodifiers", typemap_lookup_type, WARN_JAVA_TYPEMAP_CLASSMOD_UNDEF), // Class modifiers
if (!has_outerclass) // Import statements
Printv(proxy_class_def, typemapLookup(n, "javaimports", typemap_lookup_type, WARN_NONE),"\n", NIL);
else
Printv(proxy_class_def, "static ", NIL); // C++ nested classes correspond to static java classes
Printv(proxy_class_def, typemapLookup(n, "javaclassmodifiers", typemap_lookup_type, WARN_JAVA_TYPEMAP_CLASSMOD_UNDEF), // Class modifiers
" $javaclassname", // Class name and bases
(*Char(wanted_base)) ? " extends " : "", wanted_base, *Char(pure_interfaces) ? // Pure Java interfaces
" implements " : "", pure_interfaces, " {", derived ? typemapLookup(n, "javabody_derived", typemap_lookup_type, WARN_JAVA_TYPEMAP_JAVABODY_UNDEF) : // main body of class
@ -1825,7 +1838,7 @@ public:
/* Also insert the swigTakeOwnership and swigReleaseOwnership methods */
if (feature_director) {
String *destruct_jnicall, *release_jnicall, *take_jnicall;
String *changeown_method_name = Swig_name_member(getNSpace(), proxy_class_name, "change_ownership");
String *changeown_method_name = Swig_name_member(getNSpace(), getClassPrefix(), "change_ownership");
destruct_jnicall = NewStringf("%s()", destruct_methodname);
release_jnicall = NewStringf("%s.%s(this, swigCPtr, false)", full_imclass_name, changeown_method_name);
@ -1851,7 +1864,7 @@ public:
// Add code to do C++ casting to base class (only for classes in an inheritance hierarchy)
if (derived) {
String *smartptr = Getattr(n, "feature:smartptr");
String *upcast_method = Swig_name_member(getNSpace(), proxy_class_name, smartptr != 0 ? "SWIGSmartPtrUpcast" : "SWIGUpcast");
String *upcast_method = Swig_name_member(getNSpace(), getClassPrefix(), smartptr != 0 ? "SWIGSmartPtrUpcast" : "SWIGUpcast");
String *jniname = makeValidJniName(upcast_method);
String *wname = Swig_name_wrapper(jniname);
Printf(imclass_cppcasts_code, " public final static native long %s(long jarg1);\n", upcast_method);
@ -1908,13 +1921,29 @@ public:
virtual int classHandler(Node *n) {
File *f_proxy = NULL;
String *old_proxy_class_name = proxy_class_name;
String *old_full_proxy_class_name = full_proxy_class_name;
String *old_full_imclass_name = full_imclass_name;
String *old_destructor_call = destructor_call;
String *old_destructor_throws_clause = destructor_throws_clause;
String *old_proxy_class_constants_code = proxy_class_constants_code;
String *old_proxy_class_def = proxy_class_def;
String *old_proxy_class_code = proxy_class_code;
if (proxy_flag) {
proxy_class_name = NewString(Getattr(n, "sym:name"));
String *nspace = getNSpace();
constructIntermediateClassName(n);
String *outerClassesPrefix = 0;
if (Node *outer = Getattr(n, "nested:outer")) {
outerClassesPrefix = Copy(Getattr(outer, "sym:name"));
for (outer = Getattr(outer, "nested:outer"); outer != 0; outer = Getattr(outer, "nested:outer")) {
Push(outerClassesPrefix, ".");
Push(outerClassesPrefix, Getattr(outer, "sym:name"));
}
}
if (!nspace) {
full_proxy_class_name = NewStringf("%s", proxy_class_name);
full_proxy_class_name = outerClassesPrefix ? NewStringf("%s.%s", outerClassesPrefix, proxy_class_name) : NewStringf("%s", proxy_class_name);
if (Cmp(proxy_class_name, imclass_name) == 0) {
Printf(stderr, "Class name cannot be equal to intermediary class name: %s\n", proxy_class_name);
@ -1926,54 +1955,73 @@ public:
SWIG_exit(EXIT_FAILURE);
}
} else {
if (package)
full_proxy_class_name = NewStringf("%s.%s.%s", package, nspace, proxy_class_name);
else
full_proxy_class_name = NewStringf("%s.%s", nspace, proxy_class_name);
if (outerClassesPrefix) {
if (package)
full_proxy_class_name = NewStringf("%s.%s.%s.%s", package, nspace, outerClassesPrefix, proxy_class_name);
else
full_proxy_class_name = NewStringf("%s.%s.%s", nspace, outerClassesPrefix, proxy_class_name);
} else {
if (package)
full_proxy_class_name = NewStringf("%s.%s.%s", package, nspace, proxy_class_name);
else
full_proxy_class_name = NewStringf("%s.%s", nspace, proxy_class_name);
}
}
if (!addSymbol(proxy_class_name, n, nspace))
return SWIG_ERROR;
String *output_directory = outputDirectory(nspace);
String *filen = NewStringf("%s%s.java", output_directory, proxy_class_name);
f_proxy = NewFile(filen, "w", SWIG_output_files());
if (!f_proxy) {
FileErrorDisplay(filen);
SWIG_exit(EXIT_FAILURE);
}
Append(filenames_list, Copy(filen));
Delete(filen);
filen = NULL;
// Start writing out the proxy class file
emitBanner(f_proxy);
if (package || nspace) {
Printf(f_proxy, "package ");
if (package)
Printv(f_proxy, package, nspace ? "." : "", NIL);
if (outerClassesPrefix) {
Replaceall(outerClassesPrefix, ".", "::");
String *fnspace = nspace ? NewStringf("%s::%s", nspace, outerClassesPrefix) : outerClassesPrefix;
if (!addSymbol(proxy_class_name, n, fnspace))
return SWIG_ERROR;
if (nspace)
Printv(f_proxy, nspace, NIL);
Printf(f_proxy, ";\n");
Delete(fnspace);
Delete(outerClassesPrefix);
}
else {
if (!addSymbol(proxy_class_name, n, nspace))
return SWIG_ERROR;
}
Clear(proxy_class_def);
Clear(proxy_class_code);
if (!Getattr(n, "nested:outer")) {
String *output_directory = outputDirectory(nspace);
String *filen = NewStringf("%s%s.java", output_directory, proxy_class_name);
f_proxy = NewFile(filen, "w", SWIG_output_files());
if (!f_proxy) {
FileErrorDisplay(filen);
SWIG_exit(EXIT_FAILURE);
}
Append(filenames_list, Copy(filen));
Delete(filen);
Delete(output_directory);
// Start writing out the proxy class file
emitBanner(f_proxy);
if (package || nspace) {
Printf(f_proxy, "package ");
if (package)
Printv(f_proxy, package, nspace ? "." : "", NIL);
if (nspace)
Printv(f_proxy, nspace, NIL);
Printf(f_proxy, ";\n");
}
}
else
++nesting_depth;
proxy_class_def = NewString("");
proxy_class_code = NewString("");
destructor_call = NewString("");
destructor_throws_clause = NewString("");
proxy_class_constants_code = NewString("");
Delete(output_directory);
}
Language::classHandler(n);
if (proxy_flag) {
emitProxyClassDefAndCPPCasts(n);
String *javaclazzname = Swig_name_member(getNSpace(), proxy_class_name, ""); // mangled full proxy class name
String *javaclazzname = Swig_name_member(getNSpace(), getClassPrefix(), ""); // mangled full proxy class name
Replaceall(proxy_class_def, "$javaclassname", proxy_class_name);
Replaceall(proxy_class_code, "$javaclassname", proxy_class_name);
@ -1991,22 +2039,43 @@ public:
Replaceall(proxy_class_code, "$imclassname", full_imclass_name);
Replaceall(proxy_class_constants_code, "$imclassname", full_imclass_name);
Printv(f_proxy, proxy_class_def, proxy_class_code, NIL);
bool has_outerclass = Getattr(n, "nested:outer") != 0 && !GetFlag(n, "feature:flatnested");
if (!has_outerclass)
Printv(f_proxy, proxy_class_def, proxy_class_code, NIL);
else {
Swig_offset_string(proxy_class_def, nesting_depth);
Append(old_proxy_class_code, proxy_class_def);
Swig_offset_string(proxy_class_code, nesting_depth);
Append(old_proxy_class_code, proxy_class_code);
}
// Write out all the constants
if (Len(proxy_class_constants_code) != 0)
Printv(f_proxy, proxy_class_constants_code, NIL);
if (Len(proxy_class_constants_code) != 0) {
if (!has_outerclass)
Printv(f_proxy, proxy_class_constants_code, NIL);
else {
Swig_offset_string(proxy_class_constants_code, nesting_depth);
Append(old_proxy_class_code, proxy_class_constants_code);
}
}
Printf(f_proxy, "}\n");
Delete(f_proxy);
f_proxy = NULL;
if (!has_outerclass) {
Printf(f_proxy, "}\n");
Delete(f_proxy);
f_proxy = NULL;
} else {
for (int i = 0; i < nesting_depth; ++i)
Append(old_proxy_class_code, " ");
Append(old_proxy_class_code, "}\n\n");
--nesting_depth;
}
/* Output the downcast method, if necessary. Note: There's no other really
good place to put this code, since Abstract Base Classes (ABCs) can and should have
downcasts, making the constructorHandler() a bad place (because ABCs don't get to
have constructors emitted.) */
if (GetFlag(n, "feature:javadowncast")) {
String *downcast_method = Swig_name_member(getNSpace(), proxy_class_name, "SWIGDowncast");
String *downcast_method = Swig_name_member(getNSpace(), getClassPrefix(), "SWIGDowncast");
String *jniname = makeValidJniName(downcast_method);
String *wname = Swig_name_wrapper(jniname);
@ -2038,17 +2107,21 @@ public:
Delete(javaclazzname);
Delete(proxy_class_name);
proxy_class_name = NULL;
proxy_class_name = old_proxy_class_name;
Delete(full_proxy_class_name);
full_proxy_class_name = NULL;
full_proxy_class_name = old_full_proxy_class_name;
Delete(full_imclass_name);
full_imclass_name = NULL;
full_imclass_name = old_full_imclass_name;
Delete(destructor_call);
destructor_call = NULL;
destructor_call = old_destructor_call;
Delete(destructor_throws_clause);
destructor_throws_clause = NULL;
destructor_throws_clause = old_destructor_throws_clause;
Delete(proxy_class_constants_code);
proxy_class_constants_code = NULL;
proxy_class_constants_code = old_proxy_class_constants_code;
Delete(proxy_class_def);
proxy_class_def = old_proxy_class_def;
Delete(proxy_class_code);
proxy_class_code = old_proxy_class_code;
}
return SWIG_OK;
@ -2064,7 +2137,7 @@ public:
if (proxy_flag) {
String *overloaded_name = getOverloadedName(n);
String *intermediary_function_name = Swig_name_member(getNSpace(), proxy_class_name, overloaded_name);
String *intermediary_function_name = Swig_name_member(getNSpace(), getClassPrefix(), overloaded_name);
Setattr(n, "proxyfuncname", Getattr(n, "sym:name"));
Setattr(n, "imfuncname", intermediary_function_name);
proxyClassFunctionHandler(n);
@ -2086,7 +2159,7 @@ public:
if (proxy_flag) {
String *overloaded_name = getOverloadedName(n);
String *intermediary_function_name = Swig_name_member(getNSpace(), proxy_class_name, overloaded_name);
String *intermediary_function_name = Swig_name_member(getNSpace(), getClassPrefix(), overloaded_name);
Setattr(n, "proxyfuncname", Getattr(n, "sym:name"));
Setattr(n, "imfuncname", intermediary_function_name);
proxyClassFunctionHandler(n);
@ -2162,7 +2235,7 @@ public:
if (wrapping_member_flag && !enum_constant_flag) {
// For wrapping member variables (Javabean setter)
setter_flag = (Cmp(Getattr(n, "sym:name"), Swig_name_set(getNSpace(), Swig_name_member(0, proxy_class_name, variable_name))) == 0);
setter_flag = (Cmp(Getattr(n, "sym:name"), Swig_name_set(getNSpace(), Swig_name_member(0, getClassPrefix(), variable_name))) == 0);
}
/* Start generating the proxy function */
@ -2316,7 +2389,7 @@ public:
Node *explicit_n = Getattr(n, "explicitcallnode");
if (explicit_n) {
String *ex_overloaded_name = getOverloadedName(explicit_n);
String *ex_intermediary_function_name = Swig_name_member(getNSpace(), proxy_class_name, ex_overloaded_name);
String *ex_intermediary_function_name = Swig_name_member(getNSpace(), getClassPrefix(), ex_overloaded_name);
String *ex_imcall = Copy(imcall);
Replaceall(ex_imcall, "$imfuncname", ex_intermediary_function_name);
@ -3399,7 +3472,7 @@ public:
// Output the director connect method:
String *jni_imclass_name = makeValidJniName(imclass_name);
String *norm_name = SwigType_namestr(Getattr(n, "name"));
String *swig_director_connect = Swig_name_member(getNSpace(), proxy_class_name, "director_connect");
String *swig_director_connect = Swig_name_member(getNSpace(), getClassPrefix(), "director_connect");
String *swig_director_connect_jni = makeValidJniName(swig_director_connect);
String *smartptr = Getattr(n, "feature:smartptr");
String *dirClassName = directorClassName(n);
@ -3439,7 +3512,7 @@ public:
Delete(swig_director_connect);
// Output the swigReleaseOwnership, swigTakeOwnership methods:
String *changeown_method_name = Swig_name_member(getNSpace(), proxy_class_name, "change_ownership");
String *changeown_method_name = Swig_name_member(getNSpace(), getClassPrefix(), "change_ownership");
String *changeown_jnimethod_name = makeValidJniName(changeown_method_name);
Printf(imclass_class_code, " public final static native void %s(%s obj, long cptr, boolean take_or_release);\n", changeown_method_name, full_proxy_class_name);
@ -4519,6 +4592,9 @@ public:
Setattr(n, "director:ctor", class_ctor);
}
bool nestedClassesSupported() const {
return true;
}
}; /* class JAVA */
/* -----------------------------------------------------------------------------

View file

@ -20,7 +20,7 @@ static int director_mode = 0;
static int director_protected_mode = 1;
static int all_protected_mode = 0;
static int naturalvar_mode = 0;
Language* Language::this_ = 0;
Language *Language::this_ = 0;
/* Set director_protected_mode */
void Wrapper_director_mode_set(int flag) {
@ -355,7 +355,7 @@ Language::~Language() {
String *dirclassname;
String *nspace = NewString(Getattr(n, "sym:nspace"));
const char *attrib = "director:classname";
String *classname = Getattr(n, "sym:name");
String *classname = getClassPrefix();
Replace(nspace, NSPACE_SEPARATOR, "_", DOH_REPLACE_ANY);
if (Len(nspace) > 0)
@ -1015,8 +1015,6 @@ int Language::cDeclaration(Node *n) {
/* Some kind of variable declaration */
String *declaration = Copy(decl);
Delattr(n, "decl");
if (Getattr(n, "nested"))
SetFlag(n, "feature:immutable");
if (!CurrentClass) {
if (Swig_storage_isextern(n) || ForceExtern) {
if (AddExtern) {
@ -2362,6 +2360,15 @@ int Language::classDeclaration(Node *n) {
return SWIG_NOWRAP;
}
// save class local variables for nested classes support
int oldInClass = InClass;
String *oldClassType = ClassType;
String *oldClassPrefix = ClassPrefix;
String *oldClassName = ClassName;
String *oldDirectorClassName = DirectorClassName;
String *oldNSpace = NSpace;
Node *oldCurrentClass = CurrentClass;
String *kind = Getattr(n, "kind");
String *name = Getattr(n, "name");
String *tdname = Getattr(n, "tdname");
@ -2370,6 +2377,8 @@ int Language::classDeclaration(Node *n) {
int strip = CPlusPlus ? 1 : unnamed && tdname;
if (cplus_mode != PUBLIC)
return SWIG_NOWRAP;
if (!name) {
Swig_warning(WARN_LANG_CLASS_UNNAMED, input_file, line_number, "Can't generate wrappers for unnamed struct/class.\n");
return SWIG_NOWRAP;
@ -2380,15 +2389,21 @@ int Language::classDeclaration(Node *n) {
Swig_warning(WARN_LANG_IDENTIFIER, input_file, line_number, "Can't wrap class %s unless renamed to a valid identifier.\n", SwigType_namestr(symname));
return SWIG_NOWRAP;
}
AccessMode oldAccessMode = cplus_mode;
Node *outerClass = Getattr(n, "nested:outer");
if (outerClass && oldAccessMode != PUBLIC)
return SWIG_NOWRAP;
ClassName = Copy(name);
ClassPrefix = Copy(symname);
if (Cmp(kind, "class") == 0) {
cplus_mode = PRIVATE;
} else {
cplus_mode = PUBLIC;
}
ClassName = Copy(name);
ClassPrefix = Copy(symname);
for (; outerClass; outerClass = Getattr(outerClass, "nested:outer")) {
Push(ClassPrefix, "_");
Push(ClassPrefix, Getattr(outerClass, "sym:name"));
}
if (strip) {
ClassType = Copy(name);
} else {
@ -2399,9 +2414,8 @@ int Language::classDeclaration(Node *n) {
InClass = 1;
CurrentClass = n;
String *oldNSpace = NSpace;
NSpace = Getattr(n, "sym:nspace");
int oldAbstract = Abstract;
/* Call classHandler() here */
if (!ImportMode) {
@ -2443,25 +2457,27 @@ int Language::classDeclaration(Node *n) {
classDirector(n);
}
/* check for abstract after resolving directors */
Abstract = abstractClassTest(n);
Abstract = abstractClassTest(n);
classHandler(n);
} else {
Abstract = abstractClassTest(n);
Language::classHandler(n);
}
Abstract = oldAbstract;
cplus_mode = oldAccessMode;
NSpace = oldNSpace;
InClass = 0;
CurrentClass = 0;
InClass = oldInClass;
CurrentClass = oldCurrentClass;
Delete(ClassType);
ClassType = 0;
ClassType = oldClassType;
Delete(ClassPrefix);
ClassPrefix = 0;
ClassPrefix = oldClassPrefix;
Delete(ClassName);
ClassName = 0;
ClassName = oldClassName;
Delete(DirectorClassName);
DirectorClassName = 0;
DirectorClassName = oldDirectorClassName;
return SWIG_OK;
}
@ -2640,7 +2656,7 @@ int Language::constructorDeclaration(Node *n) {
String *scope = Swig_scopename_check(ClassName) ? Swig_scopename_prefix(ClassName) : 0;
String *actual_name = scope ? NewStringf("%s::%s", scope, name) : NewString(name);
Delete(scope);
if (!Equal(actual_name, expected_name) && !(Getattr(n, "template"))) {
if (!Equal(actual_name, expected_name) && !SwigType_istemplate(expected_name)) {
bool illegal_name = true;
if (Extend) {
// Check for typedef names used as a constructor name in %extend. This is deprecated except for anonymous
@ -3426,6 +3442,9 @@ bool Language::extraDirectorProtectedCPPMethodsRequired() const {
return true;
}
bool Language::nestedClassesSupported() const {
return false;
}
/* -----------------------------------------------------------------------------
* Language::is_wrapping_class()
* ----------------------------------------------------------------------------- */
@ -3612,3 +3631,4 @@ Language *Language::instance() {
Hash *Language::getClassHash() const {
return classhash;
}

View file

@ -483,6 +483,10 @@ void SWIG_getoptions(int argc, char *argv[]) {
Preprocessor_define((DOH *) "__cplusplus __cplusplus", 0);
Swig_cparse_cplusplus(1);
Swig_mark_arg(i);
} else if (strcmp(argv[i], "-c++out") == 0) {
// Undocumented
Swig_cparse_cplusplusout(1);
Swig_mark_arg(i);
} else if (strcmp(argv[i], "-fcompact") == 0) {
Wrapper_compact_print_mode_set(1);
Swig_mark_arg(i);
@ -855,8 +859,9 @@ void SWIG_getoptions(int argc, char *argv[]) {
}
}
static void flatten_nested() {
Swig_feature_set(Swig_cparse_features(), "", 0, "feature:flatnested", "1", 0);
}
int SWIG_main(int argc, char *argv[], Language *l) {
@ -947,6 +952,11 @@ int SWIG_main(int argc, char *argv[], Language *l) {
// Don't check for an input file if -external-runtime is passed
Swig_check_options(external_runtime ? 0 : 1);
if (CPlusPlus && cparse_cplusplusout) {
Printf(stderr, "The -c++out option is for C input but C++ input has been requested via -c++\n");
SWIG_exit(EXIT_FAILURE);
}
install_opts(argc, argv);
// Add language dependent directory to the search path
@ -1151,6 +1161,10 @@ int SWIG_main(int argc, char *argv[], Language *l) {
fflush(stdout);
}
// add "ignore" directive if nested classes are not supported
if (!lang->nestedClassesSupported())
flatten_nested();
Node *top = Swig_cparse(cpps);
if (dump_top & STAGE1) {
@ -1161,6 +1175,11 @@ int SWIG_main(int argc, char *argv[], Language *l) {
Printf(stdout, "debug-module stage 1\n");
Swig_print_tree(Getattr(top, "module"));
}
if (!CPlusPlus) {
if (Verbose)
Printf(stdout, "Processing unnamed structs...\n");
Swig_nested_name_unnamed_c_structs(top);
}
if (Verbose) {
Printf(stdout, "Processing types...\n");
@ -1181,6 +1200,12 @@ int SWIG_main(int argc, char *argv[], Language *l) {
}
Swig_default_allocators(top);
if (CPlusPlus) {
if (Verbose)
Printf(stdout, "Processing nested classes...\n");
Swig_nested_process_classes(top);
}
if (dump_top & STAGE3) {
Printf(stdout, "debug-top stage 3\n");
Swig_print_tree(top);

444
Source/Modules/nested.cxx Normal file
View file

@ -0,0 +1,444 @@
/* -----------------------------------------------------------------------------
* This file is part of SWIG, which is licensed as a whole under version 3
* (or any later version) of the GNU General Public License. Some additional
* terms also apply to certain portions of SWIG. The full details of the SWIG
* license and copyrights can be found in the LICENSE and COPYRIGHT files
* included with the SWIG source code as distributed by the SWIG developers
* and at http://www.swig.org/legal.html.
*
* nested.cxx
*
* Nested structs support
* ----------------------------------------------------------------------------- */
#include "swigmod.h"
#include "cparse.h"
// Nested classes processing section
static Hash *classhash = 0;
static String *make_name(Node *n, String *name, SwigType *decl) {
int destructor = name && (*(Char(name)) == '~');
if (String *yyrename = Getattr(n, "class_rename")) {
String *s = NewString(yyrename);
Delattr(n, "class_rename");
if (destructor && (*(Char(s)) != '~')) {
Insert(s, 0, "~");
}
return s;
}
if (!name)
return 0;
return Swig_name_make(n, 0, name, decl, 0);
}
// C version of add_symbols()
static void add_symbols_c(Node *n) {
String *decl;
String *wrn = 0;
String *symname = 0;
int iscdecl = Cmp(nodeType(n), "cdecl") == 0;
Setattr(n, "ismember", "1");
Setattr(n, "access", "public");
if (Getattr(n, "sym:name"))
return;
decl = Getattr(n, "decl");
if (!SwigType_isfunction(decl)) {
String *name = Getattr(n, "name");
String *makename = Getattr(n, "parser:makename");
if (iscdecl) {
String *storage = Getattr(n, "storage");
if (Cmp(storage, "typedef") == 0) {
Setattr(n, "kind", "typedef");
} else {
SwigType *type = Getattr(n, "type");
String *value = Getattr(n, "value");
Setattr(n, "kind", "variable");
if (value && Len(value)) {
Setattr(n, "hasvalue", "1");
}
if (type) {
SwigType *ty;
SwigType *tmp = 0;
if (decl) {
ty = tmp = Copy(type);
SwigType_push(ty, decl);
} else {
ty = type;
}
if (!SwigType_ismutable(ty)) {
SetFlag(n, "hasconsttype");
SetFlag(n, "feature:immutable");
}
if (tmp)
Delete(tmp);
}
if (!type) {
Printf(stderr, "notype name %s\n", name);
}
}
}
Swig_features_get(Swig_cparse_features(), 0, name, 0, n);
if (makename) {
symname = make_name(n, makename, 0);
Delattr(n, "parser:makename"); /* temporary information, don't leave it hanging around */
} else {
makename = name;
symname = make_name(n, makename, 0);
}
if (!symname) {
symname = Copy(Getattr(n, "unnamed"));
}
if (symname) {
wrn = Swig_name_warning(n, 0, symname, 0);
}
} else {
String *name = Getattr(n, "name");
SwigType *fdecl = Copy(decl);
SwigType *fun = SwigType_pop_function(fdecl);
if (iscdecl) {
Setattr(n, "kind", "function");
}
Swig_features_get(Swig_cparse_features(), 0, name, fun, n);
symname = make_name(n, name, fun);
wrn = Swig_name_warning(n, 0, symname, fun);
Delete(fdecl);
Delete(fun);
}
if (!symname)
return;
if (GetFlag(n, "feature:ignore")) {
/* Only add to C symbol table and continue */
Swig_symbol_add(0, n);
} else if (strncmp(Char(symname), "$ignore", 7) == 0) {
char *c = Char(symname) + 7;
SetFlag(n, "feature:ignore");
if (strlen(c)) {
SWIG_WARN_NODE_BEGIN(n);
Swig_warning(0, Getfile(n), Getline(n), "%s\n", c + 1);
SWIG_WARN_NODE_END(n);
}
Swig_symbol_add(0, n);
} else {
Node *c;
if ((wrn) && (Len(wrn))) {
String *metaname = symname;
if (!Getmeta(metaname, "already_warned")) {
SWIG_WARN_NODE_BEGIN(n);
Swig_warning(0, Getfile(n), Getline(n), "%s\n", wrn);
SWIG_WARN_NODE_END(n);
Setmeta(metaname, "already_warned", "1");
}
}
c = Swig_symbol_add(symname, n);
if (c != n) {
/* symbol conflict attempting to add in the new symbol */
if (Getattr(n, "sym:weak")) {
Setattr(n, "sym:name", symname);
} else {
String *e = NewStringEmpty();
String *en = NewStringEmpty();
String *ec = NewStringEmpty();
int redefined = Swig_need_redefined_warn(n, c, true);
if (redefined) {
Printf(en, "Identifier '%s' redefined (ignored)", symname);
Printf(ec, "previous definition of '%s'", symname);
} else {
Printf(en, "Redundant redeclaration of '%s'", symname);
Printf(ec, "previous declaration of '%s'", symname);
}
if (Cmp(symname, Getattr(n, "name"))) {
Printf(en, " (Renamed from '%s')", SwigType_namestr(Getattr(n, "name")));
}
Printf(en, ",");
if (Cmp(symname, Getattr(c, "name"))) {
Printf(ec, " (Renamed from '%s')", SwigType_namestr(Getattr(c, "name")));
}
Printf(ec, ".");
SWIG_WARN_NODE_BEGIN(n);
if (redefined) {
Swig_warning(WARN_PARSE_REDEFINED, Getfile(n), Getline(n), "%s\n", en);
Swig_warning(WARN_PARSE_REDEFINED, Getfile(c), Getline(c), "%s\n", ec);
} else {
Swig_warning(WARN_PARSE_REDUNDANT, Getfile(n), Getline(n), "%s\n", en);
Swig_warning(WARN_PARSE_REDUNDANT, Getfile(c), Getline(c), "%s\n", ec);
}
SWIG_WARN_NODE_END(n);
Printf(e, "%s:%d:%s\n%s:%d:%s\n", Getfile(n), Getline(n), en, Getfile(c), Getline(c), ec);
Setattr(n, "error", e);
Delete(e);
Delete(en);
Delete(ec);
}
}
}
Delete(symname);
}
/* Strips C-style and C++-style comments from string in-place. */
static void strip_comments(char *string) {
int state = 0;
/*
* 0 - not in comment
* 1 - in c-style comment
* 2 - in c++-style comment
* 3 - in string
* 4 - after reading / not in comments
* 5 - after reading * in c-style comments
* 6 - after reading \ in strings
*/
char *c = string;
while (*c) {
switch (state) {
case 0:
if (*c == '\"')
state = 3;
else if (*c == '/')
state = 4;
break;
case 1:
if (*c == '*')
state = 5;
*c = ' ';
break;
case 2:
if (*c == '\n')
state = 0;
else
*c = ' ';
break;
case 3:
if (*c == '\"')
state = 0;
else if (*c == '\\')
state = 6;
break;
case 4:
if (*c == '/') {
*(c - 1) = ' ';
*c = ' ';
state = 2;
} else if (*c == '*') {
*(c - 1) = ' ';
*c = ' ';
state = 1;
} else
state = 0;
break;
case 5:
if (*c == '/')
state = 0;
else
state = 1;
*c = ' ';
break;
case 6:
state = 3;
break;
}
++c;
}
}
// Create a %insert with a typedef to make a new name visible to C
static Node *create_insert(Node *n, bool noTypedef = false) {
// format a typedef
String *ccode = Getattr(n, "code");
Push(ccode, " ");
if (noTypedef) {
Push(ccode, Getattr(n, "name"));
Push(ccode, " ");
Push(ccode, Getattr(n, "kind"));
} else {
Push(ccode, Getattr(n, "kind"));
Push(ccode, "typedef ");
Append(ccode, " ");
Append(ccode, Getattr(n, "tdname"));
}
Append(ccode, ";");
/* Strip comments - further code may break in presence of comments. */
strip_comments(Char(ccode));
/* Make all SWIG created typedef structs/unions/classes unnamed else
redefinition errors occur - nasty hack alert. */
if (!noTypedef) {
const char *types_array[3] = { "struct", "union", "class" };
for (int i = 0; i < 3; i++) {
char *code_ptr = Char(ccode);
while (code_ptr) {
/* Replace struct name (as in 'struct name {...}' ) with whitespace
name will be between struct and opening brace */
code_ptr = strstr(code_ptr, types_array[i]);
if (code_ptr) {
char *open_bracket_pos;
code_ptr += strlen(types_array[i]);
open_bracket_pos = strchr(code_ptr, '{');
if (open_bracket_pos) {
/* Make sure we don't have something like struct A a; */
char *semi_colon_pos = strchr(code_ptr, ';');
if (!(semi_colon_pos && (semi_colon_pos < open_bracket_pos)))
while (code_ptr < open_bracket_pos)
*code_ptr++ = ' ';
}
}
}
}
}
{
/* Remove SWIG directive %constant which may be left in the SWIG created typedefs */
char *code_ptr = Char(ccode);
while (code_ptr) {
code_ptr = strstr(code_ptr, "%constant");
if (code_ptr) {
char *directive_end_pos = strchr(code_ptr, ';');
if (directive_end_pos) {
while (code_ptr <= directive_end_pos)
*code_ptr++ = ' ';
}
}
}
}
Node *newnode = NewHash();
set_nodeType(newnode, "insert");
Setfile(newnode, Getfile(n));
Setline(newnode, Getline(n));
String *code = NewStringEmpty();
Wrapper_pretty_print(ccode, code);
Setattr(newnode, "code", code);
Delete(code);
Delattr(n, "code");
return newnode;
}
static void insertNodeAfter(Node *n, Node *c) {
Node *g = parentNode(n);
set_parentNode(c, g);
Node *ns = nextSibling(n);
if (Node *outer = Getattr(c, "nested:outer")) {
while (ns && outer == Getattr(ns, "nested:outer")) {
n = ns;
ns = nextSibling(n);
}
}
if (!ns) {
set_lastChild(g, c);
} else {
set_nextSibling(c, ns);
set_previousSibling(ns, c);
}
set_nextSibling(n, c);
set_previousSibling(c, n);
}
void Swig_nested_name_unnamed_c_structs(Node *n) {
if (!classhash)
classhash = Getattr(n, "classes");
Node *c = firstChild(n);
while (c) {
Node *next = nextSibling(c);
if (String *declName = Getattr(c, "nested:unnamed")) {
if (Node *outer = Getattr(c, "nested:outer")) {
// generate a name
String *name = NewStringf("%s_%s", Getattr(outer, "name"), declName);
Delattr(c, "nested:unnamed");
// set the name to the class and symbol table
Setattr(c, "tdname", name);
Setattr(c, "name", name);
Swig_symbol_setscope(Getattr(c, "symtab"));
Swig_symbol_setscopename(name);
// now that we have a name - gather base symbols
if (List *publicBases = Getattr(c, "baselist")) {
List *bases = Swig_make_inherit_list(name, publicBases, 0);
Swig_inherit_base_symbols(bases);
Delete(bases);
}
Setattr(classhash, name, c);
Swig_symbol_popscope();
// process declarations following this type (assign correct new type)
SwigType *ty = Copy(name);
Node *decl = nextSibling(c);
List *declList = NewList();
while (decl && Getattr(decl, "nested:unnamedtype") == c) {
Setattr(decl, "type", ty);
Append(declList, decl);
Delattr(decl, "nested:unnamedtype");
SetFlag(decl, "feature:immutable");
add_symbols_c(decl);
decl = nextSibling(decl);
}
Delete(ty);
// Check for extensions
/* // TODO: we can save extensions hash like class hash and move check_extensions() after nesting processing
if (extendhash) {
if (Node *am = Getattr(extendhash, name)) {
// Merge the extension into the symbol table
merge_extensions(c, am);
append_previous_extension(c, am);
Delattr(extendhash, clsname);
}
}*/
Swig_symbol_setscope(Swig_symbol_global_scope());
add_symbols_c(c);
Node *ins = create_insert(c);
insertNodeAfter(c, ins);
removeNode(c);
insertNodeAfter(n, c);
Delete(ins);
Delattr(c, "nested:outer");
} else {
// global unnamed struct - ignore it
c = next;
continue;
}
} else if (cparse_cplusplusout) {
if (Getattr(c, "nested:outer")) {
Node *ins = create_insert(c, true);
insertNodeAfter(c, ins);
Delete(ins);
Delattr(c, "nested:outer");
}
}
// process children
Swig_nested_name_unnamed_c_structs(c);
c = next;
}
}
static void remove_outer_class_reference(Node *n) {
for (Node *c = firstChild(n); c; c = nextSibling(c)) {
if (GetFlag(c, "feature:flatnested")) {
Delattr(c, "nested:outer");
remove_outer_class_reference(c);
}
}
}
void Swig_nested_process_classes(Node *n) {
Node *c = firstChild(n);
while (c) {
Node *next = nextSibling(c);
if (!Getattr(c, "templatetype")) {
if (GetFlag(c, "nested") && GetFlag(c, "feature:flatnested")) {
removeNode(c);
if (!checkAttribute(c, "access", "public"))
SetFlag(c, "feature:ignore");
else
insertNodeAfter(n, c);
}
Swig_nested_process_classes(c);
}
c = next;
}
remove_outer_class_reference(n);
}

View file

@ -12,6 +12,7 @@
* ----------------------------------------------------------------------------- */
#include "swigmod.h"
#include "cparse.h"
static String *global_name = 0;
static String *op_prefix = 0;
@ -132,6 +133,10 @@ public:
SWIG_config_file("octave.swg");
SWIG_typemap_lang("octave");
allow_overloading();
// Octave API is C++, so output must be C++ compatibile even when wrapping C code
if (!cparse_cplusplus)
Swig_cparse_cplusplusout(1);
}
virtual int top(Node *n) {

View file

@ -297,6 +297,16 @@ protected:
/* Some language modules require additional wrappers for virtual methods not declared in sub-classes */
virtual bool extraDirectorProtectedCPPMethodsRequired() const;
public:
/* Does target language support nested classes? Default is 'false'. If 'false' is returned, then
%rename("$ignore", %$isnested) statement will be issued at the top, and the nested classes
will be ignored. Note that even if the target language does not support the notion of class
nesting, the language module may nevertheless return true from this function, and use
%feature "flatnested" to move nested classes to the global scope, instead of ignoring them.
*/
virtual bool nestedClassesSupported() const;
protected:
/* Identifies if a protected members that are generated when the allprotected option is used.
This does not include protected virtual methods as they are turned on with the dirprot option. */
bool isNonVirtualProtectedAccess(Node *n) const;
@ -410,5 +420,7 @@ int Swig_contract_mode_get();
void Swig_browser(Node *n, int);
void Swig_default_allocators(Node *n);
void Swig_process_types(Node *n);
void Swig_nested_process_classes(Node *n);
void Swig_nested_name_unnamed_c_structs(Node *n);
#endif

View file

@ -178,6 +178,20 @@ class TypePass:private Dispatcher {
}
continue;
}
// A case when both outer and nested classes inherit from the same parent. Constructor may be found instead of the class itself.
} else if (GetFlag(cls, "nested") && checkAttribute(bcls, "nodeType", "constructor")) {
bcls = Getattr(bcls, "parentNode");
if (Getattr(bcls, "typepass:visit")) {
if (!Getattr(bcls, "feature:onlychildren")) {
if (!ilist)
ilist = alist = NewList();
Append(ilist, bcls);
} else {
Swig_warning(WARN_TYPE_UNDEFINED_CLASS, Getfile(bname), Getline(bname), "Base class '%s' has no name as it is an empty template instantiated with '%%template()'. Ignored.\n", SwigType_namestr(bname));
Swig_warning(WARN_TYPE_UNDEFINED_CLASS, Getfile(bcls), Getline(bcls), "The %%template directive must be written before '%s' is used as a base class and be declared with a name.\n", SwigType_namestr(bname));
}
}
break;
}
if (Strcmp(nodeType(bcls), "classforward") != 0) {
Swig_error(Getfile(bname), Getline(bname), "'%s' is not a valid base class.\n", SwigType_namestr(bname));
@ -462,6 +476,17 @@ class TypePass:private Dispatcher {
if (unnamed && tdname && (Cmp(storage, "typedef") == 0)) {
SwigType_typedef(unnamed, tdname);
}
// name of the outer class should already be patched to contain it's outer classes names, but not to contain namespaces
// namespace name (if present) is added after processing child nodes
if (Getattr(n, "nested:outer") && name) {
String *outerName = Getattr(Getattr(n, "nested:outer"), "name");
name = NewStringf("%s::%s", outerName, name);
Setattr(n, "name", name);
if (tdname) {
tdname = NewStringf("%s::%s", outerName, tdname);
Setattr(n, "tdname", tdname);
}
}
if (nsname && name) {
nname = NewStringf("%s::%s", nsname, name);
@ -479,7 +504,7 @@ class TypePass:private Dispatcher {
SwigType_attach_symtab(Getattr(n, "symtab"));
/* Inherit type definitions into the class */
if (name) {
if (name && !(GetFlag(n, "nested") && GetFlag(n, "feature:flatnested") && !checkAttribute(n, "access", "public"))) {
cplus_inherit_types(n, 0, nname ? nname : (fname ? fname : name));
}
@ -653,7 +678,7 @@ class TypePass:private Dispatcher {
if (GetFlag(n, "conversion_operator")) {
/* The call to the operator in the generated wrapper must be fully qualified in order to compile */
SwigType *name = Getattr(n, "name");
SwigType *qualifiedname = Swig_symbol_string_qualify(name,0);
SwigType *qualifiedname = Swig_symbol_string_qualify(name, 0);
Clear(name);
Append(name, qualifiedname);
Delete(qualifiedname);
@ -1090,8 +1115,7 @@ class TypePass:private Dispatcher {
* list of overloaded methods we have just added in as child nodes to the "using" node.
* The node will still exist, it is just the symbol table linked list of overloaded methods
* which is hacked. */
if (Getattr(n, "sym:overloaded"))
{
if (Getattr(n, "sym:overloaded")) {
int cnt = 0;
#ifdef DEBUG_OVERLOADED
Node *debugnode = n;
@ -1154,7 +1178,7 @@ class TypePass:private Dispatcher {
#ifdef DEBUG_OVERLOADED
show_overloaded(debugnode);
#endif
clean_overloaded(n); // Needed?
clean_overloaded(n); // Needed?
}
}
}
@ -1258,3 +1282,4 @@ void Swig_process_types(Node *n) {
return;
TypePass::pass(n);
}