This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Patch: java -vs- c++ keyword mangling


Yesterday it occurred to me that our scheme for pre-mangling the names
of Java methods and fields that conflict with C++ keywords (we want to
do this for CNI) isn't sufficient.  Currently we just add a `$' -- but
that might conflict with an existing name.

Consider this unlikely program:

    public class d
    {
      public static final int register = 7;
      public static final int register$ = 8;
    }

This won't work correctly with the current gcj.

This patch changes gcj to add a `$' if the method or field is the same
as a C++ keyword with any number of `$' appended.  While this is not
likely to be required in practice, I think it is best to plan ahead.

Ok to commit?

2000-11-03  Tom Tromey  <tromey@cygnus.com>

	* class.c (cxx_keyword_p): Accept keywords with trailing `$'s.
	* gjavah.c (cxx_keyword_subst): Handle any number of trailing
	`$'.

Tom

Index: class.c
===================================================================
RCS file: /cvs/gcc/egcs/gcc/java/class.c,v
retrieving revision 1.79
diff -u -r1.79 class.c
--- class.c	2000/10/13 06:26:45	1.79
+++ class.c	2000/11/04 01:22:04
@@ -1961,11 +1961,23 @@
        mid != old;
        old = mid, mid = (last + first) / 2)
     {
-      int r = utf8_cmp (name, length, cxx_keywords[mid]);
+      int kwl = strlen (cxx_keywords[mid]);
+      int min_length = kwl > length ? length : kwl;
+      int r = utf8_cmp (name, min_length, cxx_keywords[mid]);
 
       if (r == 0)
-	return 1;
-      else if (r < 0)
+	{
+	  int i;
+	  /* We've found a match if all the remaining characters are
+	     `$'.  */
+	  for (i = min_length; i < length && name[i] == '$'; ++i)
+	    ;
+	  if (i == length)
+	    return 1;
+	  r = 1;
+	}
+
+      if (r < 0)
 	last = mid;
       else
 	first = mid;
Index: gjavah.c
===================================================================
RCS file: /cvs/gcc/egcs/gcc/java/gjavah.c,v
retrieving revision 1.63
diff -u -r1.63 gjavah.c
--- gjavah.c	2000/10/21 15:10:38	1.63
+++ gjavah.c	2000/11/04 01:22:05
@@ -440,16 +440,32 @@
        mid != old;
        old = mid, mid = (last + first) / 2)
     {
-      int r = utf8_cmp (str, length, cxx_keywords[mid]);
+      int kwl = strlen (cxx_keywords[mid]);
+      int min_length = kwl > length ? length : kwl;
+      int r = utf8_cmp (str, min_length, cxx_keywords[mid]);
 
       if (r == 0)
 	{
-	  char *str = xmalloc (2 + strlen (cxx_keywords[mid]));
-	  strcpy (str, cxx_keywords[mid]);
-	  strcat (str, "$");
-	  return str;
+	  int i;
+
+	  /* Skip all trailing `$'.  */
+	  for (i = min_length; i < length && str[i] == '$'; ++i)
+	    ;
+	  /* We've only found a match if all the remaining characters
+	     are `$'.  */
+	  if (i == length)
+	    {
+	      char *dup = xmalloc (2 + length - min_length + kwl);
+	      strcpy (dup, cxx_keywords[mid]);
+	      for (i = kwl; i < length + 1; ++i)
+		dup[i] = '$';
+	      dup[i] = '\0';
+	      return dup;
+	    }
+	  r = 1;
 	}
-      else if (r < 0)
+	
+      if (r < 0)
 	last = mid;
       else
 	first = mid;

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]