1 /* Functions related to mangling class names for the GNU compiler
2 for the Java(TM) language.
3 Copyright (C) 1998 Free Software Foundation, Inc.
5 This file is part of GNU CC.
7 GNU CC 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 2, or (at your option)
12 GNU CC 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 GNU CC; see the file COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA.
22 Java and all Java-based marks are trademarks or registered trademarks
23 of Sun Microsystems, Inc. in the United States and other countries.
24 The Free Software Foundation is independent of Sun Microsystems, Inc. */
26 /* Written by Per Bothner <bothner@cygnus.com> */
33 /* Assuming (NAME, LEN) is a Utf8-encoding string, calculate
34 the length of the string as mangled (a la g++) including Unicode escapes.
35 If no escapes are needed, return 0. */
38 unicode_mangling_length (name, len)
43 unsigned char *limit = (unsigned char *)name + len;
47 for (ptr = (unsigned char *) name; ptr < limit; )
49 int ch = UTF8_GET(ptr, limit);
51 error ("internal error - invalid Utf8 name");
52 if (ch >= '0' && ch <= '9')
53 need_escapes += num_chars == 0;
56 else if ((ch < 'a' || ch > 'z') && (ch < 'A' || ch > 'Z'))
61 return num_chars + 4 * (need_escapes + underscores);
66 /* Assuming (NAME, LEN) is a Utf8-encoding string, emit the string
67 appropriately mangled (with Unicode escapes) to OBSTACK. */
70 emit_unicode_mangled_name (obstack, name, len)
71 struct obstack *obstack;
75 unsigned char *limit = (unsigned char *)name + len;
76 for (ptr = (unsigned char *) name; ptr < limit; )
78 int ch = UTF8_GET(ptr, limit);
82 error ("internal error - bad Utf8 string");
85 if (ch >= '0' && ch <= '9')
86 emit_escape = (ptr == (unsigned char*) name);
88 emit_escape = (ch < 'a' || ch > 'z') && (ch < 'A' || ch > 'Z');
92 sprintf (buf, "_%04x", ch);
93 obstack_grow (obstack, buf, 5);
97 obstack_1grow (obstack, ch);
102 /* Assuming (NAME, LEN) is a Utf8-encoding string, emit the string
103 appropriately mangled (with Unicode escapes if needed) to OBSTACK. */
106 append_gpp_mangled_name (obstack, name, len)
107 struct obstack *obstack;
111 int encoded_len = unicode_mangling_length (name, len);
112 int needs_escapes = encoded_len > 0;
116 sprintf (buf, "U%d", encoded_len);
117 obstack_grow (obstack, buf, strlen(buf));
118 emit_unicode_mangled_name (obstack, name, len);
122 sprintf (buf, "%d", len);
123 obstack_grow (obstack, buf, strlen(buf));
124 obstack_grow (obstack, name, len);
128 /* Append the mangled name of a class named CLASSNAME onto OBSTACK. */
131 append_gpp_mangled_classtype (obstack, class_name)
132 struct obstack *obstack;
136 int qualifications = 0;
138 for (ptr = class_name; *ptr != '\0'; ptr++)
146 if (qualifications >= 9)
147 sprintf (buf, "Q_%d_", qualifications + 1);
149 sprintf (buf, "Q%d", qualifications + 1);
150 obstack_grow (obstack, buf, strlen (buf));
152 for (ptr = class_name; ; ptr++)
154 if (ptr[0] == '.' || ptr[0] == '\0')
156 append_gpp_mangled_name (obstack, class_name, ptr - class_name);
159 class_name = ptr + 1;