tyname.c revision 1.32 1 /* $NetBSD: tyname.c,v 1.32 2021/02/28 02:29:28 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.32 2021/02/28 02:29:28 rillig Exp $");
39 #endif
40
41 #include <limits.h>
42 #include <string.h>
43 #include <stdlib.h>
44 #include <err.h>
45
46 #include PASS
47
48 #ifndef LERROR
49 #define LERROR(fmt, args...) \
50 do { \
51 (void)warnx("%s, %d: " fmt, __FILE__, __LINE__, ##args); \
52 abort(); \
53 } while (/*CONSTCOND*/false)
54 #endif
55
56 /* A tree of strings. */
57 typedef struct name_tree_node {
58 char *ntn_name;
59 struct name_tree_node *ntn_less;
60 struct name_tree_node *ntn_greater;
61 } name_tree_node;
62
63 /* A growable string buffer. */
64 typedef struct buffer {
65 size_t len;
66 size_t cap;
67 char * data;
68 } buffer;
69
70 static name_tree_node *type_names;
71
72 static name_tree_node *
73 new_name_tree_node(const char *name)
74 {
75 name_tree_node *n;
76
77 n = xmalloc(sizeof(*n));
78 n->ntn_name = xstrdup(name);
79 n->ntn_less = NULL;
80 n->ntn_greater = NULL;
81 return n;
82 }
83
84 /* Return the canonical instance of the string, with unlimited life time. */
85 static const char *
86 intern(const char *name)
87 {
88 name_tree_node *n = type_names, **next;
89 int cmp;
90
91 if (n == NULL) {
92 n = new_name_tree_node(name);
93 type_names = n;
94 return n->ntn_name;
95 }
96
97 while ((cmp = strcmp(name, n->ntn_name)) != 0) {
98 next = cmp < 0 ? &n->ntn_less : &n->ntn_greater;
99 if (*next == NULL) {
100 *next = new_name_tree_node(name);
101 return (*next)->ntn_name;
102 }
103 n = *next;
104 }
105 return n->ntn_name;
106 }
107
108 static void
109 buf_init(buffer *buf)
110 {
111 buf->len = 0;
112 buf->cap = 128;
113 buf->data = xmalloc(buf->cap);
114 buf->data[0] = '\0';
115 }
116
117 static void
118 buf_done(buffer *buf)
119 {
120 free(buf->data);
121 }
122
123 static void
124 buf_add(buffer *buf, const char *s)
125 {
126 size_t len = strlen(s);
127
128 while (buf->len + len + 1 >= buf->cap) {
129 buf->data = xrealloc(buf->data, 2 * buf->cap);
130 buf->cap = 2 * buf->cap;
131 }
132
133 memcpy(buf->data + buf->len, s, len + 1);
134 buf->len += len;
135 }
136
137 static void
138 buf_add_int(buffer *buf, int n)
139 {
140 char num[1 + sizeof(n) * CHAR_BIT + 1];
141
142 snprintf(num, sizeof num, "%d", n);
143 buf_add(buf, num);
144 }
145
146 const char *
147 tspec_name(tspec_t t)
148 {
149 switch (t) {
150 case SIGNED: return "signed";
151 case UNSIGN: return "unsigned";
152 case BOOL: return "_Bool";
153 case CHAR: return "char";
154 case SCHAR: return "signed char";
155 case UCHAR: return "unsigned char";
156 case SHORT: return "short";
157 case USHORT: return "unsigned short";
158 case INT: return "int";
159 case UINT: return "unsigned int";
160 case LONG: return "long";
161 case ULONG: return "unsigned long";
162 case QUAD: return "long long";
163 case UQUAD: return "unsigned long long";
164 #ifdef INT128_SIZE
165 case INT128: return "__int128_t";
166 case UINT128: return "__uint128_t";
167 #endif
168 case FLOAT: return "float";
169 case DOUBLE: return "double";
170 case LDOUBLE: return "long double";
171 case VOID: return "void";
172 case STRUCT: return "struct";
173 case UNION: return "union";
174 case ENUM: return "enum";
175 case PTR: return "pointer";
176 case ARRAY: return "array";
177 case FUNC: return "function";
178 case COMPLEX: return "_Complex";
179 case FCOMPLEX: return "float _Complex";
180 case DCOMPLEX: return "double _Complex";
181 case LCOMPLEX: return "long double _Complex";
182 default:
183 LERROR("tspec_name(%d)", t);
184 return NULL;
185 }
186 }
187
188 bool
189 sametype(const type_t *t1, const type_t *t2)
190 {
191 tspec_t t;
192
193 if (t1->t_tspec != t2->t_tspec)
194 return false;
195
196 /* Ignore const/void */
197
198 switch (t = t1->t_tspec) {
199 case BOOL:
200 case CHAR:
201 case UCHAR:
202 case SCHAR:
203 case SHORT:
204 case USHORT:
205 case INT:
206 case UINT:
207 case LONG:
208 case ULONG:
209 case QUAD:
210 case UQUAD:
211 #ifdef INT128_SIZE
212 case INT128:
213 case UINT128:
214 #endif
215 case FLOAT:
216 case DOUBLE:
217 case LDOUBLE:
218 case VOID:
219 case FUNC:
220 case COMPLEX:
221 case FCOMPLEX:
222 case DCOMPLEX:
223 case LCOMPLEX:
224 return true;
225 case ARRAY:
226 if (t1->t_dim != t2->t_dim)
227 return false;
228 /*FALLTHROUGH*/
229 case PTR:
230 return sametype(t1->t_subt, t2->t_subt);
231 case ENUM:
232 #ifdef t_enum
233 return strcmp(t1->t_enum->en_tag->s_name,
234 t2->t_enum->en_tag->s_name) == 0;
235 #else
236 return true;
237 #endif
238 case STRUCT:
239 case UNION:
240 #ifdef t_str
241 return strcmp(t1->t_str->sou_tag->s_name,
242 t2->t_str->sou_tag->s_name) == 0;
243 #else
244 return true;
245 #endif
246 default:
247 LERROR("tyname(%d)", t);
248 return false;
249 }
250 }
251
252 static void
253 type_name_of_function(buffer *buf, const type_t *tp)
254 {
255 const char *sep = "";
256
257 buf_add(buf, "(");
258 if (tp->t_proto) {
259 #ifdef t_enum /* lint1 */
260 sym_t *arg;
261
262 for (arg = tp->t_args; arg != NULL; arg = arg->s_next) {
263 buf_add(buf, sep), sep = ", ";
264 buf_add(buf, type_name(arg->s_type));
265 }
266 #else /* lint2 */
267 type_t **argtype;
268
269 for (argtype = tp->t_args; *argtype != NULL; argtype++) {
270 buf_add(buf, sep), sep = ", ";
271 buf_add(buf, type_name(*argtype));
272 }
273 #endif
274 }
275 if (tp->t_vararg) {
276 buf_add(buf, sep);
277 buf_add(buf, "...");
278 }
279 buf_add(buf, ") returning ");
280 buf_add(buf, type_name(tp->t_subt));
281 }
282
283 static void
284 type_name_of_struct_or_union(buffer *buf, const type_t *tp)
285 {
286 buf_add(buf, " ");
287 #ifdef t_str
288 if (tp->t_str->sou_tag->s_name == unnamed &&
289 tp->t_str->sou_first_typedef != NULL) {
290 buf_add(buf, "typedef ");
291 buf_add(buf, tp->t_str->sou_first_typedef->s_name);
292 } else {
293 buf_add(buf, tp->t_str->sou_tag->s_name);
294 }
295 #else
296 buf_add(buf, tp->t_isuniqpos ? "*anonymous*" : tp->t_tag->h_name);
297 #endif
298 }
299
300 static void
301 type_name_of_enum(buffer *buf, const type_t *tp)
302 {
303 buf_add(buf, " ");
304 #ifdef t_enum
305 if (tp->t_enum->en_tag->s_name == unnamed &&
306 tp->t_enum->en_first_typedef != NULL) {
307 buf_add(buf, "typedef ");
308 buf_add(buf, tp->t_enum->en_first_typedef->s_name);
309 } else {
310 buf_add(buf, tp->t_enum->en_tag->s_name);
311 }
312 #else
313 buf_add(buf, tp->t_isuniqpos ? "*anonymous*" : tp->t_tag->h_name);
314 #endif
315 }
316
317 const char *
318 type_name(const type_t *tp)
319 {
320 tspec_t t;
321 buffer buf;
322 const char *name;
323
324 if (tp == NULL)
325 return "(null)";
326
327 /*
328 * XXX: Why is this necessary, and in which cases does this apply?
329 * Shouldn't the type be an ENUM from the beginning?
330 */
331 if ((t = tp->t_tspec) == INT && tp->t_is_enum)
332 t = ENUM;
333
334 buf_init(&buf);
335 if (tp->t_const)
336 buf_add(&buf, "const ");
337 if (tp->t_volatile)
338 buf_add(&buf, "volatile ");
339
340 buf_add(&buf, tspec_name(t));
341
342 switch (t) {
343 case BOOL:
344 case CHAR:
345 case UCHAR:
346 case SCHAR:
347 case SHORT:
348 case USHORT:
349 case INT:
350 case UINT:
351 case LONG:
352 case ULONG:
353 case QUAD:
354 case UQUAD:
355 #ifdef INT128_SIZE
356 case INT128:
357 case UINT128:
358 #endif
359 case FLOAT:
360 case DOUBLE:
361 case LDOUBLE:
362 case VOID:
363 case COMPLEX:
364 case FCOMPLEX:
365 case DCOMPLEX:
366 case LCOMPLEX:
367 case SIGNED:
368 case UNSIGN:
369 break;
370 case PTR:
371 buf_add(&buf, " to ");
372 buf_add(&buf, type_name(tp->t_subt));
373 break;
374 case ENUM:
375 type_name_of_enum(&buf, tp);
376 break;
377 case STRUCT:
378 case UNION:
379 type_name_of_struct_or_union(&buf, tp);
380 break;
381 case ARRAY:
382 buf_add(&buf, " of ");
383 buf_add(&buf, type_name(tp->t_subt));
384 buf_add(&buf, "[");
385 #ifdef t_str /* lint1 */
386 if (tp->t_incomplete_array)
387 buf_add(&buf, "unknown_size");
388 else
389 buf_add_int(&buf, tp->t_dim);
390 #else
391 buf_add_int(&buf, tp->t_dim);
392 #endif
393 buf_add(&buf, "]");
394 break;
395 case FUNC:
396 type_name_of_function(&buf, tp);
397 break;
398
399 default:
400 LERROR("type_name(%d)", t);
401 }
402
403 name = intern(buf.data);
404 buf_done(&buf);
405 return name;
406 }
407