OSDN Git Service

* jcf-write.c (emit_load_or_store): Avoid implicit int arguments.
[pf3gnuchains/gcc-fork.git] / gcc / java / mangle.c
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.
4
5 This file is part of GNU CC.
6
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)
10 any later version.
11
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.
16
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. 
21
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.  */
25
26 /* Written by Per Bothner <bothner@cygnus.com> */
27
28 #include "config.h"
29 #include "system.h"
30 #include "jcf.h"
31 #include "obstack.h"
32
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. */
36
37 int
38 unicode_mangling_length (name, len)
39      char *name; 
40      int len; 
41 {
42   unsigned char *ptr;
43   unsigned char *limit = (unsigned char *)name + len;
44   int need_escapes = 0;
45   int num_chars = 0;
46   int underscores = 0;
47   for (ptr = (unsigned char *) name;  ptr < limit;  )
48     {
49       int ch = UTF8_GET(ptr, limit);
50       if (ch < 0)
51         error ("internal error - invalid Utf8 name");
52       if (ch >= '0' && ch <= '9')
53         need_escapes += num_chars == 0;
54       else if (ch == '_')
55         underscores++;
56       else if ((ch < 'a' || ch > 'z') && (ch < 'A' || ch > 'Z'))
57         need_escapes++;
58       num_chars++;
59     }
60   if (need_escapes)
61     return num_chars + 4 * (need_escapes + underscores);
62   else
63     return 0;
64 }
65
66 /* Assuming (NAME, LEN) is a Utf8-encoding string, emit the string
67    appropriately mangled (with Unicode escapes) to OBSTACK. */
68
69 void
70 emit_unicode_mangled_name (obstack, name, len)
71      struct obstack *obstack;
72      char *name;
73      int len;
74 {
75   unsigned char *ptr;
76   unsigned char *limit = (unsigned char *)name + len;
77   for (ptr = (unsigned char *) name;  ptr < limit;  )
78     {
79       int ch = UTF8_GET(ptr, limit);
80       int emit_escape;
81       if (ch < 0)
82         {
83           error ("internal error - bad Utf8 string");
84           break;
85         }
86       if (ch >= '0' && ch <= '9')
87         emit_escape = (ptr == (unsigned char*) name);
88       else
89         emit_escape = (ch < 'a' || ch > 'z') && (ch < 'A' || ch > 'Z');
90       if (emit_escape)
91         {
92           char buf[6];
93           sprintf (buf, "_%04x", ch);
94           obstack_grow (obstack, buf, 5);
95         }
96       else
97         {
98           obstack_1grow (obstack, ch);
99         }
100     }
101 }
102
103 /* Assuming (NAME, LEN) is a Utf8-encoding string, emit the string
104    appropriately mangled (with Unicode escapes if needed) to OBSTACK. */
105
106 void
107 append_gpp_mangled_name (obstack, name, len)
108      struct obstack *obstack;
109      char *name;
110      int len;
111 {
112   int encoded_len = unicode_mangling_length (name, len);
113   int needs_escapes = encoded_len > 0;
114   char buf[6];
115   if (needs_escapes)
116     {
117       sprintf (buf, "U%d", encoded_len);
118       obstack_grow (obstack, buf, strlen(buf));
119       emit_unicode_mangled_name (obstack, name, len);
120     }
121   else
122     {
123       sprintf (buf, "%d", len);
124       obstack_grow (obstack, buf, strlen(buf));
125       obstack_grow (obstack, name, len);
126     }
127 }
128
129 /* Append the mangled name of a class named CLASSNAME onto OBSTACK. */
130
131 void
132 append_gpp_mangled_classtype (obstack, class_name)
133      struct obstack *obstack;
134      char *class_name;
135 {
136   char *ptr;
137   int qualifications = 0;
138
139   for (ptr = class_name; *ptr != '\0'; ptr++)
140     {
141       if (*ptr == '.')
142         qualifications++;
143     }
144   if (qualifications)
145     {
146       char buf[8];
147       if (qualifications >= 9)
148         sprintf (buf, "Q_%d_", qualifications + 1);
149       else
150         sprintf (buf, "Q%d", qualifications + 1);
151       obstack_grow (obstack, buf, strlen (buf));
152     }
153   for (ptr = class_name; ; ptr++)
154     {
155       if (ptr[0] == '.' || ptr[0] == '\0')
156         {
157           append_gpp_mangled_name (obstack, class_name, ptr - class_name);
158           if (ptr[0] == '\0')
159             break;
160           class_name = ptr + 1;
161         }
162     }
163 }