Add allprotected mode for wrapping protected members when using directors

git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk@10381 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
William S Fulton 2008-04-20 20:41:01 +00:00
commit 9976dc9d75
17 changed files with 468 additions and 181 deletions

View file

@ -274,6 +274,7 @@ public:
if (Getattr(optionsnode, "dirprot")) {
allow_dirprot();
}
allow_allprotected(GetFlag(optionsnode, "allprotected"));
}
/* Initialize all of the output files */
@ -1238,14 +1239,17 @@ public:
// The %csconst feature determines how the constant value is obtained
int const_feature_flag = GetFlag(n, "feature:cs:const");
const String *methodmods = Getattr(n, "feature:cs:methodmodifiers");
methodmods = methodmods ? methodmods : (is_public(n) ? public_string : protected_string);
if ((enum_feature == TypesafeEnum) && Getattr(parentNode(n), "sym:name") && !Getattr(parentNode(n), "unnamedinstance")) {
// Wrap (non-anonymouse) enum using the typesafe enum pattern
if (Getattr(n, "enumvalue")) {
String *value = enumValue(n);
Printf(enum_code, " public static readonly %s %s = new %s(\"%s\", %s);\n", return_type, symname, return_type, symname, value);
Printf(enum_code, " %s static readonly %s %s = new %s(\"%s\", %s);\n", methodmods, return_type, symname, return_type, symname, value);
Delete(value);
} else {
Printf(enum_code, " public static readonly %s %s = new %s(\"%s\");\n", return_type, symname, return_type, symname);
Printf(enum_code, " %s static readonly %s %s = new %s(\"%s\");\n", methodmods, return_type, symname, return_type, symname);
}
} else {
// Simple integer constants
@ -1253,7 +1257,7 @@ public:
// Code generated is the same for SimpleEnum and TypeunsafeEnum -> the class it is generated into is determined later
const char *const_readonly = const_feature_flag ? "const" : "static readonly";
String *value = enumValue(n);
Printf(enum_code, " public %s %s %s = %s;\n", const_readonly, return_type, symname, value);
Printf(enum_code, " %s %s %s %s = %s;\n", methodmods, const_readonly, return_type, symname, value);
Delete(value);
}
}
@ -1336,7 +1340,11 @@ public:
if (outattributes)
Printf(constants_code, " %s\n", outattributes);
const String *itemname = (proxy_flag && wrapping_member_flag) ? variable_name : symname;
Printf(constants_code, " public %s %s %s = ", (const_feature_flag ? "const" : "static readonly"), return_type, itemname);
const String *methodmods = Getattr(n, "feature:cs:methodmodifiers");
methodmods = methodmods ? methodmods : (is_public(n) ? public_string : protected_string);
Printf(constants_code, " %s %s %s %s = ", methodmods, (const_feature_flag ? "const" : "static readonly"), return_type, itemname);
// Check for the %csconstvalue feature
String *value = Getattr(n, "feature:cs:constvalue");
@ -3552,7 +3560,7 @@ public:
Printf(w->code, "}");
// We expose protected methods via an extra public inline method which makes a straight call to the wrapped class' method
// We expose virtual protected methods via an extra public inline method which makes a straight call to the wrapped class' method
String *inline_extra_method = NewString("");
if (dirprot_mode() && !is_public(n) && !pure_virtual) {
Printv(inline_extra_method, declaration, NIL);

View file

@ -415,9 +415,15 @@ void emit_action(Node *n, Wrapper *f) {
action = Getattr(n, "wrap:action");
assert(action != 0);
if (!is_public(n) && (is_member_director(n) || GetFlag(n, "explicitcall"))) {
/* In order to call protected virtual director methods from the target language, we need
* to add an extra dynamic_cast to call the public C++ wrapper in the director class. */
/* In order to call protected virtual director methods from the target language, we need
* to add an extra dynamic_cast to call the public C++ wrapper in the director class.
* Also for non-static protected members when the allprotected option is on. */
// TODO: why is the storage element removed in staticmemberfunctionHandler ??
if (!is_public(n) && (is_member_director(n) || GetFlag(n, "explicitcall")) ||
(is_non_virtual_protected_access(n) && !(checkAttribute(n, "staticmemberfunctionHandler:storage", "static") ||
checkAttribute(n, "storage", "static"))
&& !Equal(nodeType(n), "constructor"))) {
Node *parent = Getattr(n, "parentNode");
String *symname = Getattr(parent, "sym:name");
String *dirname = NewStringf("SwigDirector_%s", symname);

View file

@ -281,6 +281,7 @@ public:
if (Getattr(optionsnode, "dirprot")) {
allow_dirprot();
}
allow_allprotected(GetFlag(optionsnode, "allprotected"));
}
/* Initialize all of the output files */
@ -893,12 +894,6 @@ public:
// Premature garbage collection prevention parameter
if (!is_destructor) {
String *pgc_parameter = prematureGarbageCollectionPreventionParameter(pt, p);
/*
if (!pgc_parameter) {
Printf(stdout, "prematuregcp %s %s [%s]\n", symname, Getattr(n, "sym:overname"), pt);
Swig_print_node(p);
}
*/
if (pgc_parameter) {
Printf(imclass_class_code, ", %s %s_", pgc_parameter, arg);
Printf(f->def, ", jobject %s_", arg);
@ -1294,21 +1289,24 @@ public:
Delete(typemap_lookup_type);
typemap_lookup_type = NULL;
const String *methodmods = Getattr(n, "feature:java:methodmodifiers");
methodmods = methodmods ? methodmods : (is_public(n) ? public_string : protected_string);
if ((enum_feature == TypesafeEnum) && Getattr(parentNode(n), "sym:name") && !Getattr(parentNode(n), "unnamedinstance")) {
// Wrap (non-anonymouse) enum using the typesafe enum pattern
if (Getattr(n, "enumvalue")) {
String *value = enumValue(n);
Printf(enum_code, " public final static %s %s = new %s(\"%s\", %s);\n", return_type, symname, return_type, symname, value);
Printf(enum_code, " %s final static %s %s = new %s(\"%s\", %s);\n", methodmods, return_type, symname, return_type, symname, value);
Delete(value);
} else {
Printf(enum_code, " public final static %s %s = new %s(\"%s\");\n", return_type, symname, return_type, symname);
Printf(enum_code, " %s final static %s %s = new %s(\"%s\");\n", methodmods, return_type, symname, return_type, symname);
}
} else {
// Simple integer constants
// Note these are always generated for anonymous enums, no matter what enum_feature is specified
// Code generated is the same for SimpleEnum and TypeunsafeEnum -> the class it is generated into is determined later
String *value = enumValue(n);
Printf(enum_code, " public final static %s %s = %s;\n", return_type, symname, value);
Printf(enum_code, " %s final static %s %s = %s;\n", methodmods, return_type, symname, value);
Delete(value);
}
}
@ -1385,7 +1383,10 @@ public:
}
const String *itemname = (proxy_flag && wrapping_member_flag) ? variable_name : symname;
Printf(constants_code, " public final static %s %s = ", return_type, itemname);
const String *methodmods = Getattr(n, "feature:java:methodmodifiers");
methodmods = methodmods ? methodmods : (is_public(n) ? public_string : protected_string);
Printf(constants_code, " %s final static %s %s = ", methodmods, return_type, itemname);
// Check for the %javaconstvalue feature
String *value = Getattr(n, "feature:java:constvalue");
@ -3743,7 +3744,7 @@ public:
Printf(w->code, "}");
// We expose protected methods via an extra public inline method which makes a straight call to the wrapped class' method
// We expose virtual protected methods via an extra public inline method which makes a straight call to the wrapped class' method
String *inline_extra_method = NewString("");
if (dirprot_mode() && !is_public(n) && !pure_virtual) {
Printv(inline_extra_method, declaration, NIL);

View file

@ -13,8 +13,10 @@ char cvsroot_lang_cxx[] = "$Id$";
#include "cparse.h"
#include <ctype.h>
static int director_mode = 0; /* set to 0 on default */
static int director_protected_mode = 1; /* set to 1 on default */
/* default mode settings */
static int director_mode = 0;
static int director_protected_mode = 1;
static int all_protected_mode = 0;
static int naturalvar_mode = 0;
/* Set director_protected_mode */
@ -26,6 +28,10 @@ void Wrapper_director_protected_mode_set(int flag) {
director_protected_mode = flag;
}
void Wrapper_all_protected_mode_set(int flag) {
all_protected_mode = flag;
}
void Wrapper_naturalvar_mode_set(int flag) {
naturalvar_mode = flag;
}
@ -34,6 +40,12 @@ extern "C" {
int Swig_director_mode() {
return director_mode;
}
int Swig_director_protected_mode() {
return director_protected_mode;
}
int Swig_all_protected_mode() {
return all_protected_mode;
}
}
/* Some status variables used during parsing */
@ -813,11 +825,13 @@ int Language::cDeclaration(Node *n) {
/* except for friends, they are not affected by access control */
int isfriend = storage && (Cmp(storage, "friend") == 0);
if (!isfriend) {
/* we check what the director needs. If the method is pure virtual,
it is always needed. */
if (!(directorsEnabled() && is_member_director(CurrentClass, n) && need_nonpublic_member(n))) {
return SWIG_NOWRAP;
/* Check what the director needs. If the method is pure virtual, it is always needed.
* Also wrap non-virtual protected members if asked for (allprotected mode). */
if (!(directorsEnabled() && ((is_member_director(CurrentClass, n) && need_nonpublic_member(n)) || is_non_virtual_protected_access(n)))) {
return SWIG_NOWRAP;
}
#if 0
// I don't see why this is needed - WSF
/* prevent wrapping the method twice due to overload */
String *wrapname = NewStringf("nonpublic_%s%s", symname, Getattr(n, "sym:overname"));
if (Getattr(CurrentClass, wrapname)) {
@ -826,6 +840,7 @@ int Language::cDeclaration(Node *n) {
}
SetFlag(CurrentClass, wrapname);
Delete(wrapname);
#endif
}
}
@ -1217,9 +1232,9 @@ int Language::memberfunctionHandler(Node *n) {
}
}
// Set up the type for the cast to this class for use when wrapping const director (virtual) methods.
// Note: protected director methods only.
// Note: protected director methods or when allprotected mode turned on.
String *director_type = 0;
if (!is_public(n) && (is_member_director(CurrentClass, n) || GetFlag(n, "explicitcall"))) {
if (!is_public(n) && (is_member_director(CurrentClass, n) || GetFlag(n, "explicitcall") || is_non_virtual_protected_access(n))) {
director_type = Copy(DirectorClassName);
String *qualifier = Getattr(n, "qualifier");
if (qualifier)
@ -1238,6 +1253,7 @@ int Language::memberfunctionHandler(Node *n) {
Swig_MethodToFunction(n, ClassType, Getattr(n, "template") ? SmartPointer : Extend | SmartPointer | DirectorExtraCall, director_type,
is_member_director(CurrentClass, n));
Setattr(n, "sym:name", fname);
functionWrapper(n);
Delete(director_type);
@ -1263,12 +1279,11 @@ int Language::staticmemberfunctionHandler(Node *n) {
if (!Extend) {
Node *sb = Getattr(n, "cplus:staticbase");
String *sname = sb ? Getattr(sb, "name") : 0;
if (sname) {
String *sname = Getattr(sb, "name");
if (is_non_virtual_protected_access(n))
cname = NewStringf("%s::%s", DirectorClassName, name);
else
cname = NewStringf("%s::%s", sname, name);
} else {
cname = NewStringf("%s::%s", ClassName, name);
}
} else {
String *mname = Swig_name_mangle(ClassName);
cname = Swig_name_member(mname, name);
@ -1346,8 +1361,7 @@ int Language::variableHandler(Node *n) {
SetFlag(n, "feature:immutable");
}
}
if ((Cmp(storage, "static") == 0)
&& !(SmartPointer && Getattr(n, "allocate:smartpointeraccess"))) {
if ((Cmp(storage, "static") == 0) && !(SmartPointer && Getattr(n, "allocate:smartpointeraccess"))) {
staticmembervariableHandler(n);
} else {
membervariableHandler(n);
@ -1421,6 +1435,8 @@ int Language::membervariableHandler(Node *n) {
tm = Swig_typemap_lookup_new("memberin", n, target, 0);
}
int flags = Extend | SmartPointer | use_naturalvar_mode(n);
if (is_non_virtual_protected_access(n))
flags = flags | CWRAP_ALL_PROTECTED_ACCESS;
Swig_MembersetToFunction(n, ClassType, flags);
Setattr(n, "memberset", "1");
@ -1467,6 +1483,8 @@ int Language::membervariableHandler(Node *n) {
/* Emit get function */
{
int flags = Extend | SmartPointer | use_naturalvar_mode(n);
if (is_non_virtual_protected_access(n))
flags = flags | CWRAP_ALL_PROTECTED_ACCESS;
Swig_MembergetToFunction(n, ClassType, flags);
Setattr(n, "sym:name", mrename_get);
Setattr(n, "memberget", "1");
@ -1525,7 +1543,8 @@ int Language::membervariableHandler(Node *n) {
int Language::staticmembervariableHandler(Node *n) {
Swig_require("staticmembervariableHandler", n, "*name", "*sym:name", "*type", "?value", NIL);
String *value = Getattr(n, "value");
String *classname = !SmartPointer ? ClassName : Getattr(CurrentClass, "allocate:smartpointerbase");
String *classname = !SmartPointer ? (is_non_virtual_protected_access(n) ? DirectorClassName : ClassName) : Getattr(CurrentClass, "allocate:smartpointerbase");
if (!value || !Getattr(n, "hasconsttype")) {
String *name = Getattr(n, "name");
String *symname = Getattr(n, "sym:name");
@ -1681,7 +1700,7 @@ int Language::memberconstantHandler(Node *n) {
if (Extend)
new_name = Copy(value);
else
new_name = NewStringf("%s::%s", ClassName, name);
new_name = NewStringf("%s::%s", is_non_virtual_protected_access(n) ? DirectorClassName : ClassName, name);
Setattr(n, "name", new_name);
constantWrapper(n);
@ -2083,6 +2102,20 @@ int Language::classDirector(Node *n) {
List *vtable = NewList();
int virtual_destructor = 0;
unrollVirtualMethods(n, n, vtable, 0, virtual_destructor);
// Emit all the using base::member statements for non virtual members (allprotected mode)
Node *ni;
String *using_protected_members_code = NewString("");
for (ni = Getattr(n, "firstChild"); ni; ni = nextSibling(ni)) {
Node *nodeType = Getattr(ni, "nodeType");
bool cdecl = (Cmp(nodeType, "cdecl") == 0);
if (cdecl && !GetFlag(ni, "feature:ignore")) {
if (is_non_virtual_protected_access(ni)) {
Printf(using_protected_members_code, " using %s::%s;\n", SwigType_namestr(ClassName), Getattr(ni, "name"));
}
}
}
if (virtual_destructor || Len(vtable) > 0) {
if (!virtual_destructor) {
String *classtype = Getattr(n, "classtype");
@ -2096,9 +2129,14 @@ int Language::classDirector(Node *n) {
classDirectorInit(n);
classDirectorConstructors(n);
classDirectorMethods(n);
File *f_directors_h = Swig_filebyname("director_h");
Printv(f_directors_h, using_protected_members_code, NIL);
classDirectorEnd(n);
}
Delete(vtable);
Delete(using_protected_members_code);
return SWIG_OK;
}
@ -3106,12 +3144,20 @@ void Language::allow_dirprot(int val) {
director_protected_mode = val;
}
/* -----------------------------------------------------------------------------
* Language::allow_allprotected()
* ----------------------------------------------------------------------------- */
void Language::allow_allprotected(int val) {
all_protected_mode = val;
}
/* -----------------------------------------------------------------------------
* Language::dirprot_mode()
* ----------------------------------------------------------------------------- */
int Language::dirprot_mode() const {
return directorsEnabled()? director_protected_mode : 0;
return directorsEnabled() ? director_protected_mode : 0;
}
/* -----------------------------------------------------------------------------
@ -3183,8 +3229,7 @@ int Language::need_nonpublic_member(Node *n) {
if (directorsEnabled()) {
if (is_protected(n)) {
if (dirprot_mode()) {
/* when using dirprot mode, the protected members are always
needed. */
/* when using dirprot mode, the protected members are always needed. */
return 1;
} else {
/* if the method is pure virtual, we need it. */

View file

@ -75,7 +75,7 @@ static File *f_pm = 0;
static String *pm; /* Package initialization code */
static String *magic; /* Magic variable wrappers */
static int is_static = 0;
static int staticoption = 0;
/* The following variables are used to manage Perl5 classes */
@ -150,7 +150,7 @@ public:
export_all = 1;
Swig_mark_arg(i);
} else if (strcmp(argv[i], "-static") == 0) {
is_static = 1;
staticoption = 1;
Swig_mark_arg(i);
} else if ((strcmp(argv[i], "-shadow") == 0) || ((strcmp(argv[i], "-proxy") == 0))) {
blessed = 1;
@ -298,7 +298,7 @@ public:
Printf(f_pm, "package %s;\n", fullmodule);
Printf(f_pm, "require Exporter;\n");
if (!is_static) {
if (!staticoption) {
Printf(f_pm, "require DynaLoader;\n");
Printf(f_pm, "@ISA = qw(Exporter DynaLoader);\n");
} else {
@ -378,7 +378,7 @@ public:
Printf(f_pm, "package %s;\n", cmodule);
if (!is_static) {
if (!staticoption) {
Printf(f_pm, "bootstrap %s;\n", fullmodule);
} else {
String *tmp = NewString(fullmodule);

View file

@ -232,6 +232,9 @@ public:
/* Allow director protected members related code generation */
void allow_dirprot(int val = 1);
/* Allow all protected members code generation (for directors) */
void allow_allprotected(int val = 0);
/* Returns the dirprot mode */
int dirprot_mode() const;
@ -349,11 +352,10 @@ int is_private(Node *n);
int is_protected(Node *n);
int is_member_director(Node *parentnode, Node *member);
int is_member_director(Node *member);
int is_non_virtual_protected_access(Node *n); /* Check if the non-virtual protected members are required (for directors) */
int use_naturalvar_mode(Node *n);
void Wrapper_virtual_elimination_mode_set(int);
void Wrapper_director_mode_set(int);
void Wrapper_director_protected_mode_set(int);
void Wrapper_fast_dispatch_mode_set(int);
void Wrapper_cast_dispatch_mode_set(int);
void Wrapper_naturalvar_mode_set(int);

View file

@ -26,17 +26,19 @@ int is_protected(Node *n) {
return access && !Cmp(access, "protected");
}
int is_member_director(Node *parentnode, Node *member) {
int director_mode = Swig_director_mode();
static int is_member_director_helper(Node *parentnode, Node *member) {
int parent_nodirector = GetFlag(parentnode, "feature:nodirector");
if (parent_nodirector)
return 0;
int parent_director = Swig_director_mode() && GetFlag(parentnode, "feature:director");
int cdecl_director = parent_director || GetFlag(member, "feature:director");
int cdecl_nodirector = GetFlag(member, "feature:nodirector");
return cdecl_director && !cdecl_nodirector && !GetFlag(member, "feature:extend");
}
int is_member_director(Node *parentnode, Node *member) {
if (parentnode && checkAttribute(member, "storage", "virtual")) {
int parent_nodirector = GetFlag(parentnode, "feature:nodirector");
if (parent_nodirector)
return 0;
int parent_director = director_mode && GetFlag(parentnode, "feature:director");
int cdecl_director = parent_director || GetFlag(member, "feature:director");
int cdecl_nodirector = GetFlag(member, "feature:nodirector");
return cdecl_director && !cdecl_nodirector && !GetFlag(member, "feature:extend");
return is_member_director_helper(parentnode, member);
} else {
return 0;
}
@ -46,6 +48,17 @@ int is_member_director(Node *member) {
return is_member_director(Getattr(member, "parentNode"), member);
}
// Identifies the additional 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.
int is_non_virtual_protected_access(Node *n) {
int result = 0;
if (Swig_director_mode() && Swig_director_protected_mode() && Swig_all_protected_mode() && is_protected(n) && !checkAttribute(n, "storage", "virtual")) {
if (is_member_director_helper(Getattr(n, "parentNode"), n))
result = 1;
}
return result;
}
/* Clean overloaded list. Removes templates, ignored, and errors */
void clean_overloaded(Node *n) {