Skip to content

Commit 36a0457

Browse files
authored
Merge pull request #155 from swig-fortran/simplify-constructors
Remove duplicate private constructors from wrapper code
2 parents b578cee + 968021a commit 36a0457

File tree

1 file changed

+21
-27
lines changed

1 file changed

+21
-27
lines changed

Source/Modules/fortran.cxx

Lines changed: 21 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -617,7 +617,6 @@ class FORTRAN : public Language {
617617
// Current class parameters
618618
String *f_class; //!< Proxy code in currently generated class
619619
Hash *d_method_overloads; //!< Overloaded subroutine -> overload names
620-
List *d_constructors; //!< Overloaded subroutine -> overload names
621620

622621
// Inside of the 'enum' definitions
623622
List *d_enum_public; //!< List of enumerator values
@@ -693,7 +692,7 @@ class FORTRAN : public Language {
693692
* \brief Constructor.
694693
*/
695694
FORTRAN::FORTRAN() :
696-
d_emitted_mangled(NULL), d_callbacks(NULL), d_overloads(NULL), d_private_overloads(NULL), f_class(NULL), d_method_overloads(NULL), d_constructors(NULL),
695+
d_emitted_mangled(NULL), d_callbacks(NULL), d_overloads(NULL), d_private_overloads(NULL), f_class(NULL), d_method_overloads(NULL),
697696
d_enum_public(NULL) {
698697

699698
// Mark this language as supporting directors
@@ -999,14 +998,20 @@ void FORTRAN::write_module(String *filename) {
999998

1000999
// Overloads and renamed module procedures
10011000
for (Iterator kv = First(d_overloads); kv.key; kv = Next(kv)) {
1001+
Iterator proc_name_list = First(kv.item);
1002+
if (!proc_name_list.item) {
1003+
// Skip empty overload list, probably from lack of class constructors
1004+
continue;
1005+
}
1006+
10021007
Printv(out,
10031008
" interface ", kv.key, "\n"
10041009
" module procedure ",
10051010
NULL);
10061011

10071012
// Write overloaded procedure names
10081013
int line_length = 19;
1009-
line_length = print_wrapped_list(out, First(kv.item), line_length);
1014+
line_length = print_wrapped_list(out, proc_name_list, line_length);
10101015
Printv(out, "\n end interface\n", NULL);
10111016
if (!GetFlag(d_private_overloads, kv.key)) {
10121017
Printv(out, " public :: ", kv.key, "\n", NULL);
@@ -2430,12 +2435,15 @@ int FORTRAN::classHandler(Node *n) {
24302435
// Initialize output strings that will be added by 'functionHandler'.
24312436
d_method_overloads = NewHash();
24322437

2433-
// Constructors
2434-
d_constructors = NewList();
2435-
24362438
// Add an assignment function to the class node
24372439
this->add_assignment_operator(n);
24382440

2441+
// Add overload entry for constructors (must do this outside of `add_overload` since the class name already exists as a symbol)
2442+
ASSERT_OR_PRINT_NODE(!Getattr(d_overloads, fsymname), n);
2443+
Setattr(d_overloads, fsymname, NewList());
2444+
// The derived type is already marked as public, so don't add the additional "public" declaration
2445+
SetFlag(d_private_overloads, fsymname);
2446+
24392447
// Assignment operator means we're guaranteed to have at least one method, so it's OK to unconditionally put 'contains'
24402448
Printv(f_class, " contains\n", NULL);
24412449
}
@@ -2475,39 +2483,25 @@ int FORTRAN::classHandler(Node *n) {
24752483
Delete(f_class);
24762484
f_class = NULL;
24772485

2478-
// Print constructor interfaces
2479-
if (d_constructors && (Len(d_constructors) > 0)) {
2480-
Printf(f_fdecl, " interface %s\n", fsymname);
2481-
for (Iterator it = First(d_constructors); it.item; it = Next(it)) {
2482-
Printf(f_fdecl, " module procedure %s\n", it.item);
2483-
}
2484-
Printf(f_fdecl, " end interface\n");
2485-
Setattr(n, "fortran:constructors", d_constructors);
2486-
Delete(d_constructors);
2487-
d_constructors = NULL;
2488-
}
2489-
24902486
return SWIG_OK;
24912487
}
24922488

24932489
/* -------------------------------------------------------------------------
24942490
* \brief Extra stuff for constructors.
24952491
*/
24962492
int FORTRAN::constructorHandler(Node *n) {
2497-
// Add swigf_ to constructor name
2498-
String *fname = proxy_name_construct(this->getNSpace(), "create", Getattr(n, "sym:name"));
2493+
// Set fortran symname of this function to the class symname
2494+
Setattr(n, "fortran:name", Getattr(this->getCurrentClass(), "fortran:name"));
2495+
2496+
// Make constructor method name conform to other interface wrapper names
2497+
String *fname = proxy_name_construct(this->getNSpace(), "new", Getattr(n, "sym:name"));
24992498
Setattr(n, "fortran:fname", fname);
25002499
Delete(fname);
25012500

25022501
// Override the result variable name
25032502
Setattr(n, "wrap:fresult", "self");
2504-
// Don't generate a public interface
2505-
SetFlag(n, "fortran:private");
25062503

25072504
Language::constructorHandler(n);
2508-
if (!GetFlag(n, "fortran:ignore")) {
2509-
Append(d_constructors, Getattr(n, "wrap:fname"));
2510-
}
25112505
return SWIG_OK;
25122506
}
25132507

@@ -2524,8 +2518,8 @@ int FORTRAN::destructorHandler(Node *n) {
25242518
UnsetFlag(n, "feature:except");
25252519
}
25262520

2527-
// Add swigf_ to constructor name
2528-
String *fname = proxy_name_construct(this->getNSpace(), "release", Getattr(n, "sym:name"));
2521+
// Make destructor method name conform to other interface wrapper names
2522+
String *fname = proxy_name_construct(this->getNSpace(), Getattr(n, "sym:name"), "release");
25292523
Setattr(n, "fortran:fname", fname);
25302524
Delete(fname);
25312525

0 commit comments

Comments
 (0)