Skip to content

Commit b578cee

Browse files
authored
Merge pull request #154 from swig-fortran/rename-long-overloaded
Rename methods that become too long when overloaded
2 parents 70946c5 + 599c139 commit b578cee

File tree

3 files changed

+23
-10
lines changed

3 files changed

+23
-10
lines changed

Examples/test-suite/fortran/fortran_naming_runme.F90

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ program fortran_naming_runme
6262
ASSERT(sixty_four_characters_is_way_too_long_for_fortran_or_punchXAJBV == 65)
6363
ASSERT(leading_underscore_with_sixty_four_characters_is_just_darnIR2OS == 64)
6464
ASSERT(leading_underscore_with_sixty_three_characters_might_be_tricky_ == 63)
65+
ASSERT(this_is_a_very_long_name_but_its_ok_unless_its_overloaded() == 0)
66+
ASSERT(this_is_a_very_long_name_but_its_bad_since_its_overloaded(10) == 10)
67+
ASSERT(this_is_a_very_long_name_but_its_bad_since_its_overloaded(20.0) == 20.0)
6568

6669
! Check elided enum
6770
ASSERT(Zero == 0)

Examples/test-suite/fortran_naming.i

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,13 @@ extern "C" int _0cboundfunc(const int _x) { return _x + 1; }
120120
%constant int _leading_underscore_with_sixty_three_characters_might_be_tricky = 63;
121121
%nofortranconst;
122122

123+
// swigc_ -prefixed name: OK if not overloaded, too long if overloaded
124+
%inline %{
125+
int this_is_a_very_long_name_but_its_ok_unless_its_overloaded() { return 0; }
126+
int this_is_a_very_long_name_but_its_bad_since_its_overloaded(int i) { return i; }
127+
float this_is_a_very_long_name_but_its_bad_since_its_overloaded(float f) { return f; }
128+
%}
129+
123130
// This class is poorly named, but the symname is OK.
124131
%inline %{
125132
template<class T>

Source/Modules/fortran.cxx

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -279,17 +279,17 @@ bool is_valid_identifier(const_String_or_char_ptr name) {
279279
* Requires input to be longer than 63 chars.
280280
* Returns new'd string.
281281
*/
282-
String *shorten_identifier(String *inp, int warning = WARN_NONE) {
283-
assert(Len(inp) > 63);
284-
String *result = NewStringWithSize(inp, 63);
282+
String *shorten_identifier(String *inp, int maxlen, int warning) {
283+
assert(Len(inp) > maxlen);
284+
String *result = NewStringWithSize(inp, maxlen);
285285
unsigned int hash = 5381;
286286
// Hash truncated characters *AND* characters that might be replaced by the hash
287287
// (2**8 / (10 + 26)) =~ 7.1, so backtrack 8 chars
288-
for (const char *src = Char(inp) + 63 - 8; *src != '\0'; ++src) {
288+
for (const char *src = Char(inp) + maxlen - 8; *src != '\0'; ++src) {
289289
hash = (hash * 33 + *src) & 0xffffffffu;
290290
}
291291
// Replace the last chars with the hash encoded into 0-10 + A-Z
292-
char *dst = Char(result) + 63;
292+
char *dst = Char(result) + maxlen;
293293
while (hash > 0) {
294294
unsigned long rem = hash % 36;
295295
hash = hash / 36;
@@ -311,9 +311,9 @@ String *shorten_identifier(String *inp, int warning = WARN_NONE) {
311311
*
312312
* *assumes ownership of input and returns new'd value*
313313
*/
314-
String *ensure_short(String *str, int warning = WARN_NONE) {
315-
if (Len(str) > 63) {
316-
String *shortened = shorten_identifier(str, warning);
314+
String *ensure_short(String *str, int maxlen=63, int warning=WARN_NONE) {
315+
if (Len(str) > maxlen) {
316+
String *shortened = shorten_identifier(str, maxlen, warning);
317317
assert(is_valid_identifier(shortened));
318318
Delete(str);
319319
str = shortened;
@@ -387,7 +387,7 @@ String *make_fname(String *name, int warning = WARN_LANG_IDENTIFIER) {
387387
}
388388

389389
// The beginning of the string is set up; now capture and shorten if too long
390-
result = ensure_short(result ? result : Copy(name), warning);
390+
result = ensure_short(result ? result : Copy(name), 63, warning);
391391

392392
assert(is_valid_identifier(result));
393393
return result;
@@ -1077,10 +1077,12 @@ int FORTRAN::functionWrapper(Node *n) {
10771077
String *fname = NULL; // Fortran proxy function name; null if bind(C)
10781078
String *imname = NULL; // Fortran interface function name
10791079
String *wname = NULL; // SWIG C wrapper function name
1080+
String *overload_ext = Getattr(n, "sym:overloaded") ? Getattr(n, "sym:overname") : NULL;
10801081

10811082
// Generate a unique wrapper name
10821083
wname = Swig_name_wrapper(symname);
1083-
imname = ensure_short(NewStringf("swigc_%s", symname));
1084+
imname = ensure_short(NewStringf("swigc_%s", symname),
1085+
63 - (overload_ext ? Len(overload_ext) : 0));
10841086

10851087
if (String *private_fname = Getattr(n, "fortran:fname")) {
10861088
// Create "private" fortran wrapper function class (swigf_xx) name that will be bound to a class
@@ -1120,6 +1122,7 @@ int FORTRAN::functionWrapper(Node *n) {
11201122
fsymname = fname;
11211123
fname = proxy_name_construct(this->getNSpace(), symname);
11221124
}
1125+
fname = ensure_short(fname, 63 - Len(overload_ext));
11231126
Append(fname, overload_ext);
11241127
generic = true;
11251128
}

0 commit comments

Comments
 (0)