]>
git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/java/mangle_name.c
1 /* Shared functions related to mangling names for the GNU compiler
2 for the Java(TM) language.
3 Copyright (C) 2001-2015 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>.
21 Java and all Java-based marks are trademarks or registered trademarks
22 of Sun Microsystems, Inc. in the United States and other countries.
23 The Free Software Foundation is independent of Sun Microsystems, Inc. */
25 /* Written by Alexandre Petit-Bianco <apbianco@cygnus.com> */
29 #include "coretypes.h"
34 #include "double-int.h"
42 #include "java-tree.h"
44 #include "diagnostic-core.h"
46 static void append_unicode_mangled_name (const char *, int);
48 static int unicode_mangling_length (const char *, int);
51 extern struct obstack
*mangle_obstack
;
54 utf8_cmp (const unsigned char *str
, int length
, const char *name
)
56 const unsigned char *limit
= str
+ length
;
59 for (i
= 0; name
[i
]; ++i
)
61 int ch
= UTF8_GET (str
, limit
);
66 return str
== limit
? 0 : 1;
69 /* A sorted list of all C++ keywords. If you change this, be sure
70 also to change the list in
71 libjava/classpath/tools/gnu/classpath/tools/javah/Keywords.java. */
72 static const char *const cxx_keywords
[] =
180 /* Return true if NAME is a C++ keyword. */
182 cxx_keyword_p (const char *name
, int length
)
184 int last
= ARRAY_SIZE (cxx_keywords
);
186 int mid
= (last
+ first
) / 2;
189 for (mid
= (last
+ first
) / 2;
191 old
= mid
, mid
= (last
+ first
) / 2)
193 int kwl
= strlen (cxx_keywords
[mid
]);
194 int min_length
= kwl
> length
? length
: kwl
;
195 int r
= utf8_cmp ((const unsigned char *) name
, min_length
, cxx_keywords
[mid
]);
200 /* We've found a match if all the remaining characters are `$'. */
201 for (i
= min_length
; i
< length
&& name
[i
] == '$'; ++i
)
216 /* If NAME happens to be a C++ keyword, add `$'. */
217 #define MANGLE_CXX_KEYWORDS(NAME, LEN) \
220 if (cxx_keyword_p ((NAME), (LEN))) \
222 char *tmp_buf = (char *)alloca ((LEN)+1); \
223 memcpy (tmp_buf, (NAME), (LEN)); \
232 /* If the assembler doesn't support UTF8 in symbol names, some
233 characters might need to be escaped. */
237 /* Assuming (NAME, LEN) is a Utf8-encoding string, emit the string
238 appropriately mangled (with Unicode escapes if needed) to
239 MANGLE_OBSTACK. Note that `java', `lang' and `Object' are used so
240 frequently that they could be cached. */
243 append_gpp_mangled_name (const char *name
, int len
)
245 int encoded_len
, needs_escapes
;
248 MANGLE_CXX_KEYWORDS (name
, len
);
250 encoded_len
= unicode_mangling_length (name
, len
);
251 needs_escapes
= encoded_len
> 0;
253 sprintf (buf
, "%d", (needs_escapes
? encoded_len
: len
));
254 obstack_grow (mangle_obstack
, buf
, strlen (buf
));
257 append_unicode_mangled_name (name
, len
);
259 obstack_grow (mangle_obstack
, name
, len
);
262 /* Assuming (NAME, LEN) is a Utf8-encoded string, emit the string
263 appropriately mangled (with Unicode escapes) to MANGLE_OBSTACK.
264 Characters needing an escape are encoded `__UNN_' to `__UNNNN_', in
265 which case `__U' will be mangled `__U_'. */
268 append_unicode_mangled_name (const char *name
, int len
)
270 const unsigned char *ptr
;
271 const unsigned char *limit
= (const unsigned char *)name
+ len
;
273 for (ptr
= (const unsigned char *) name
; ptr
< limit
; )
275 int ch
= UTF8_GET(ptr
, limit
);
277 if ((ISALNUM (ch
) && ch
!= 'U') || ch
== '$')
279 obstack_1grow (mangle_obstack
, ch
);
282 /* Everything else needs encoding */
286 if (ch
== '_' || ch
== 'U')
288 /* Prepare to recognize __U */
289 if (ch
== '_' && (uuU
< 3))
292 obstack_1grow (mangle_obstack
, ch
);
294 /* We recognize __U that we wish to encode
295 __U_. Finish the encoding. */
296 else if (ch
== 'U' && (uuU
== 2))
299 obstack_grow (mangle_obstack
, "U_", 2);
301 /* Otherwise, just reset uuU and emit the character we
306 obstack_1grow (mangle_obstack
, ch
);
310 sprintf (buf
, "__U%x_", ch
);
311 obstack_grow (mangle_obstack
, buf
, strlen (buf
));
317 /* Assuming (NAME, LEN) is a Utf8-encoding string, calculate the
318 length of the string as mangled (a la g++) including Unicode
319 escapes. If no escapes are needed, return 0. */
322 unicode_mangling_length (const char *name
, int len
)
324 const unsigned char *ptr
;
325 const unsigned char *limit
= (const unsigned char *)name
+ len
;
326 int need_escapes
= 0; /* Whether we need an escape or not */
327 int num_chars
= 0; /* Number of characters in the mangled name */
328 int uuU
= 0; /* Help us to find __U. 0: '_', 1: '__' */
329 for (ptr
= (const unsigned char *) name
; ptr
< limit
; )
331 int ch
= UTF8_GET(ptr
, limit
);
334 error ("internal error - invalid Utf8 name");
335 if ((ISALNUM (ch
) && ch
!= 'U') || ch
== '$')
340 /* Everything else needs encoding */
343 int encoding_length
= 2;
345 if (ch
== '_' || ch
== 'U')
347 /* It's always at least one character. */
350 /* Prepare to recognize __U */
351 if (ch
== '_' && (uuU
< 3))
354 /* We recognize __U that we wish to encode __U_, we
355 count one more character. */
356 else if (ch
== 'U' && (uuU
== 2))
362 /* Otherwise, just reset uuU */
374 num_chars
+= (4 + encoding_length
);
387 /* The assembler supports UTF8, we don't use escapes. Mangling is
388 simply <N>NAME. <N> is the number of UTF8 encoded characters that
389 are found in NAME. Note that `java', `lang' and `Object' are used
390 so frequently that they could be cached. */
393 append_gpp_mangled_name (const char *name
, int len
)
395 const unsigned char *ptr
;
396 const unsigned char *limit
;
400 MANGLE_CXX_KEYWORDS (name
, len
);
402 limit
= (const unsigned char *)name
+ len
;
404 /* Compute the length of the string we wish to mangle. */
405 for (encoded_len
= 0, ptr
= (const unsigned char *) name
;
406 ptr
< limit
; encoded_len
++)
408 int ch
= UTF8_GET(ptr
, limit
);
411 error ("internal error - invalid Utf8 name");
414 sprintf (buf
, "%d", encoded_len
);
415 obstack_grow (mangle_obstack
, buf
, strlen (buf
));
416 obstack_grow (mangle_obstack
, name
, len
);
419 #endif /* HAVE_AS_UTF8 */