tyname.c revision 1.44 1 /* $NetBSD: tyname.c,v 1.44 2021/08/03 17:44:58 rillig Exp $ */
2
3 /*-
4 * Copyright (c) 2005 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Christos Zoulas.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #if HAVE_NBTOOL_CONFIG_H
33 #include "nbtool_config.h"
34 #endif
35
36 #include <sys/cdefs.h>
37 #if defined(__RCSID) && !defined(lint)
38 __RCSID("$NetBSD: tyname.c,v 1.44 2021/08/03 17:44:58 rillig Exp $");
39 #endif
40
41 #include <limits.h>
42 #include <string.h>
43 #include <stdlib.h>
44 #include <err.h>
45
46 #if defined(IS_LINT1)
47 #include "lint1.h"
48 #else
49 #include "lint2.h"
50 #endif
51
52 #ifndef INTERNAL_ERROR
53 #define INTERNAL_ERROR(fmt, args...) \
54 do { \
55 (void)warnx("%s, %d: " fmt, __FILE__, __LINE__, ##args); \
56 abort(); \
57 } while (false)
58 #endif
59
60 /* A tree of strings. */
61 typedef struct name_tree_node {
62 const char *ntn_name;
63 struct name_tree_node *ntn_less;
64 struct name_tree_node *ntn_greater;
65 } name_tree_node;
66
67 /* A growable string buffer. */
68 typedef struct buffer {
69 size_t len;
70 size_t cap;
71 char * data;
72 } buffer;
73
74 static name_tree_node *type_names;
75
76 static name_tree_node *
77 new_name_tree_node(const char *name)
78 {
79 name_tree_node *n;
80
81 n = xmalloc(sizeof(*n));
82 n->ntn_name = xstrdup(name);
83 n->ntn_less = NULL;
84 n->ntn_greater = NULL;
85 return n;
86 }
87
88 /* Return the canonical instance of the string, with unlimited lifetime. */
89 static const char *
90 intern(const char *name)
91 {
92 name_tree_node *n = type_names, **next;
93 int cmp;
94
95 if (n == NULL) {
96 n = new_name_tree_node(name);
97 type_names = n;
98 return n->ntn_name;
99 }
100
101 while ((cmp = strcmp(name, n->ntn_name)) != 0) {
102 next = cmp < 0 ? &n->ntn_less : &n->ntn_greater;
103 if (*next == NULL) {
104 *next = new_name_tree_node(name);
105 return (*next)->ntn_name;
106 }
107 n = *next;
108 }
109 return n->ntn_name;
110 }
111
112 static void
113 buf_init(buffer *buf)
114 {
115 buf->len = 0;
116 buf->cap = 128;
117 buf->data = xmalloc(buf->cap);
118 buf->data[0] = '\0';
119 }
120
121 static void
122 buf_done(buffer *buf)
123 {
124 free(buf->data);
125 }
126
127 static void
128 buf_add(buffer *buf, const char *s)
129 {
130 size_t len = strlen(s);
131
132 while (buf->len + len + 1 >= buf->cap) {
133 buf->data = xrealloc(buf->data, 2 * buf->cap);
134 buf->cap = 2 * buf->cap;
135 }
136
137 memcpy(buf->data + buf->len, s, len + 1);
138 buf->len += len;
139 }
140
141 static void
142 buf_add_int(buffer *buf, int n)
143 {
144 char num[1 + sizeof(n) * CHAR_BIT + 1];
145
146 snprintf(num, sizeof(num), "%d", n);
147 buf_add(buf, num);
148 }
149
150 const char *
151 tspec_name(tspec_t t)
152 {
153 switch (t) {
154 case SIGNED: return "signed";
155 case UNSIGN: return "unsigned";
156 case BOOL: return "_Bool";
157 case CHAR: return "char";
158 case SCHAR: return "signed char";
159 case UCHAR: return "unsigned char";
160 case SHORT: return "short";
161 case USHORT: return "unsigned short";
162 case INT: return "int";
163 case UINT: return "unsigned int";
164 case LONG: return "long";
165 case ULONG: return "unsigned long";
166 case QUAD: return "long long";
167 case UQUAD: return "unsigned long long";
168 #ifdef INT128_SIZE
169 case INT128: return "__int128_t";
170 case UINT128: return "__uint128_t";
171 #endif
172 case FLOAT: return "float";
173 case DOUBLE: return "double";
174 case LDOUBLE: return "long double";
175 case VOID: return "void";
176 case STRUCT: return "struct";
177 case UNION: return "union";
178 case ENUM: return "enum";
179 case PTR: return "pointer";
180 case ARRAY: return "array";
181 case FUNC: return "function";
182 case COMPLEX: return "_Complex";
183 case FCOMPLEX: return "float _Complex";
184 case DCOMPLEX: return "double _Complex";
185 case LCOMPLEX: return "long double _Complex";
186 default:
187 INTERNAL_ERROR("tspec_name(%d)", t);
188 return NULL;
189 }
190 }
191
192 static void
193 type_name_of_function(buffer *buf, const type_t *tp)
194 {
195 const char *sep = "";
196
197 buf_add(buf, "(");
198 if (tp->t_proto) {
199 #ifdef t_enum /* lint1 */
200 sym_t *arg;
201
202 arg = tp->t_args;
203 if (arg == NULL)
204 buf_add(buf, "void");
205 for (; arg != NULL; arg = arg->s_next) {
206 buf_add(buf, sep), sep = ", ";
207 buf_add(buf, type_name(arg->s_type));
208 }
209 #else /* lint2 */
210 type_t **argtype;
211
212 argtype = tp->t_args;
213 if (argtype == NULL)
214 buf_add(buf, "void");
215 for (; *argtype != NULL; argtype++) {
216 buf_add(buf, sep), sep = ", ";
217 buf_add(buf, type_name(*argtype));
218 }
219 #endif
220 }
221 if (tp->t_vararg) {
222 buf_add(buf, sep);
223 buf_add(buf, "...");
224 }
225 buf_add(buf, ") returning ");
226 buf_add(buf, type_name(tp->t_subt));
227 }
228
229 static void
230 type_name_of_struct_or_union(buffer *buf, const type_t *tp)
231 {
232 buf_add(buf, " ");
233 #ifdef t_str
234 if (tp->t_str->sou_tag->s_name == unnamed &&
235 tp->t_str->sou_first_typedef != NULL) {
236 buf_add(buf, "typedef ");
237 buf_add(buf, tp->t_str->sou_first_typedef->s_name);
238 } else {
239 buf_add(buf, tp->t_str->sou_tag->s_name);
240 }
241 #else
242 buf_add(buf, tp->t_isuniqpos ? "*anonymous*" : tp->t_tag->h_name);
243 #endif
244 }
245
246 static void
247 type_name_of_enum(buffer *buf, const type_t *tp)
248 {
249 buf_add(buf, " ");
250 #ifdef t_enum
251 if (tp->t_enum->en_tag->s_name == unnamed &&
252 tp->t_enum->en_first_typedef != NULL) {
253 buf_add(buf, "typedef ");
254 buf_add(buf, tp->t_enum->en_first_typedef->s_name);
255 } else {
256 buf_add(buf, tp->t_enum->en_tag->s_name);
257 }
258 #else
259 buf_add(buf, tp->t_isuniqpos ? "*anonymous*" : tp->t_tag->h_name);
260 #endif
261 }
262
263 static void
264 type_name_of_array(buffer *buf, const type_t *tp)
265 {
266 buf_add(buf, "[");
267 #ifdef t_str /* lint1 */
268 if (tp->t_incomplete_array)
269 buf_add(buf, "unknown_size");
270 else
271 buf_add_int(buf, tp->t_dim);
272 #else
273 buf_add_int(buf, tp->t_dim);
274 #endif
275 buf_add(buf, "]");
276 buf_add(buf, " of ");
277 buf_add(buf, type_name(tp->t_subt));
278 }
279
280 const char *
281 type_name(const type_t *tp)
282 {
283 tspec_t t;
284 buffer buf;
285 const char *name;
286
287 if (tp == NULL)
288 return "(null)";
289
290 if ((t = tp->t_tspec) == INT && tp->t_is_enum)
291 t = ENUM;
292
293 buf_init(&buf);
294 if (tp->t_const)
295 buf_add(&buf, "const ");
296 if (tp->t_volatile)
297 buf_add(&buf, "volatile ");
298
299 #ifdef t_str
300 if ((t == STRUCT || t == UNION) && tp->t_str->sou_incomplete)
301 buf_add(&buf, "incomplete ");
302 #endif
303 buf_add(&buf, tspec_name(t));
304
305 switch (t) {
306 case BOOL:
307 case CHAR:
308 case UCHAR:
309 case SCHAR:
310 case SHORT:
311 case USHORT:
312 case INT:
313 case UINT:
314 case LONG:
315 case ULONG:
316 case QUAD:
317 case UQUAD:
318 #ifdef INT128_SIZE
319 case INT128:
320 case UINT128:
321 #endif
322 case FLOAT:
323 case DOUBLE:
324 case LDOUBLE:
325 case VOID:
326 case COMPLEX:
327 case FCOMPLEX:
328 case DCOMPLEX:
329 case LCOMPLEX:
330 case SIGNED:
331 case UNSIGN:
332 break;
333 case PTR:
334 buf_add(&buf, " to ");
335 buf_add(&buf, type_name(tp->t_subt));
336 break;
337 case ENUM:
338 type_name_of_enum(&buf, tp);
339 break;
340 case STRUCT:
341 case UNION:
342 type_name_of_struct_or_union(&buf, tp);
343 break;
344 case ARRAY:
345 type_name_of_array(&buf, tp);
346 break;
347 case FUNC:
348 type_name_of_function(&buf, tp);
349 break;
350 default:
351 INTERNAL_ERROR("type_name(%d)", t);
352 }
353
354 name = intern(buf.data);
355 buf_done(&buf);
356 return name;
357 }
358