tyname.c revision 1.42 1 /* $NetBSD: tyname.c,v 1.42 2021/06/28 10:29:05 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.42 2021/06/28 10:29:05 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 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 life time. */
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 bool
193 sametype(const type_t *t1, const type_t *t2)
194 {
195 tspec_t t;
196
197 if (t1->t_tspec != t2->t_tspec)
198 return false;
199
200 /* Ignore const/volatile */
201
202 switch (t = t1->t_tspec) {
203 case BOOL:
204 case CHAR:
205 case UCHAR:
206 case SCHAR:
207 case SHORT:
208 case USHORT:
209 case INT:
210 case UINT:
211 case LONG:
212 case ULONG:
213 case QUAD:
214 case UQUAD:
215 #ifdef INT128_SIZE
216 case INT128:
217 case UINT128:
218 #endif
219 case FLOAT:
220 case DOUBLE:
221 case LDOUBLE:
222 case VOID:
223 case FUNC:
224 case COMPLEX:
225 case FCOMPLEX:
226 case DCOMPLEX:
227 case LCOMPLEX:
228 return true;
229 case ARRAY:
230 if (t1->t_dim != t2->t_dim)
231 return false;
232 /*FALLTHROUGH*/
233 case PTR:
234 return sametype(t1->t_subt, t2->t_subt);
235 case ENUM:
236 #ifdef t_enum
237 return strcmp(t1->t_enum->en_tag->s_name,
238 t2->t_enum->en_tag->s_name) == 0;
239 #else
240 return true;
241 #endif
242 case STRUCT:
243 case UNION:
244 #ifdef t_str
245 return strcmp(t1->t_str->sou_tag->s_name,
246 t2->t_str->sou_tag->s_name) == 0;
247 #else
248 return true;
249 #endif
250 default:
251 INTERNAL_ERROR("tyname(%d)", t);
252 return false;
253 }
254 }
255
256 static void
257 type_name_of_function(buffer *buf, const type_t *tp)
258 {
259 const char *sep = "";
260
261 buf_add(buf, "(");
262 if (tp->t_proto) {
263 #ifdef t_enum /* lint1 */
264 sym_t *arg;
265
266 arg = tp->t_args;
267 if (arg == NULL)
268 buf_add(buf, "void");
269 for (; arg != NULL; arg = arg->s_next) {
270 buf_add(buf, sep), sep = ", ";
271 buf_add(buf, type_name(arg->s_type));
272 }
273 #else /* lint2 */
274 type_t **argtype;
275
276 argtype = tp->t_args;
277 if (argtype == NULL)
278 buf_add(buf, "void");
279 for (; *argtype != NULL; argtype++) {
280 buf_add(buf, sep), sep = ", ";
281 buf_add(buf, type_name(*argtype));
282 }
283 #endif
284 }
285 if (tp->t_vararg) {
286 buf_add(buf, sep);
287 buf_add(buf, "...");
288 }
289 buf_add(buf, ") returning ");
290 buf_add(buf, type_name(tp->t_subt));
291 }
292
293 static void
294 type_name_of_struct_or_union(buffer *buf, const type_t *tp)
295 {
296 buf_add(buf, " ");
297 #ifdef t_str
298 if (tp->t_str->sou_tag->s_name == unnamed &&
299 tp->t_str->sou_first_typedef != NULL) {
300 buf_add(buf, "typedef ");
301 buf_add(buf, tp->t_str->sou_first_typedef->s_name);
302 } else {
303 buf_add(buf, tp->t_str->sou_tag->s_name);
304 }
305 #else
306 buf_add(buf, tp->t_isuniqpos ? "*anonymous*" : tp->t_tag->h_name);
307 #endif
308 }
309
310 static void
311 type_name_of_enum(buffer *buf, const type_t *tp)
312 {
313 buf_add(buf, " ");
314 #ifdef t_enum
315 if (tp->t_enum->en_tag->s_name == unnamed &&
316 tp->t_enum->en_first_typedef != NULL) {
317 buf_add(buf, "typedef ");
318 buf_add(buf, tp->t_enum->en_first_typedef->s_name);
319 } else {
320 buf_add(buf, tp->t_enum->en_tag->s_name);
321 }
322 #else
323 buf_add(buf, tp->t_isuniqpos ? "*anonymous*" : tp->t_tag->h_name);
324 #endif
325 }
326
327 static void
328 type_name_of_array(buffer *buf, const type_t *tp)
329 {
330 buf_add(buf, "[");
331 #ifdef t_str /* lint1 */
332 if (tp->t_incomplete_array)
333 buf_add(buf, "unknown_size");
334 else
335 buf_add_int(buf, tp->t_dim);
336 #else
337 buf_add_int(buf, tp->t_dim);
338 #endif
339 buf_add(buf, "]");
340 buf_add(buf, " of ");
341 buf_add(buf, type_name(tp->t_subt));
342 }
343
344 const char *
345 type_name(const type_t *tp)
346 {
347 tspec_t t;
348 buffer buf;
349 const char *name;
350
351 if (tp == NULL)
352 return "(null)";
353
354 /*
355 * XXX: Why is this necessary, and in which cases does this apply?
356 * Shouldn't the type be an ENUM from the beginning?
357 */
358 if ((t = tp->t_tspec) == INT && tp->t_is_enum)
359 t = ENUM;
360
361 buf_init(&buf);
362 if (tp->t_const)
363 buf_add(&buf, "const ");
364 if (tp->t_volatile)
365 buf_add(&buf, "volatile ");
366
367 #ifdef t_str
368 if ((t == STRUCT || t == UNION) && tp->t_str->sou_incomplete)
369 buf_add(&buf, "incomplete ");
370 #endif
371 buf_add(&buf, tspec_name(t));
372
373 switch (t) {
374 case BOOL:
375 case CHAR:
376 case UCHAR:
377 case SCHAR:
378 case SHORT:
379 case USHORT:
380 case INT:
381 case UINT:
382 case LONG:
383 case ULONG:
384 case QUAD:
385 case UQUAD:
386 #ifdef INT128_SIZE
387 case INT128:
388 case UINT128:
389 #endif
390 case FLOAT:
391 case DOUBLE:
392 case LDOUBLE:
393 case VOID:
394 case COMPLEX:
395 case FCOMPLEX:
396 case DCOMPLEX:
397 case LCOMPLEX:
398 case SIGNED:
399 case UNSIGN:
400 break;
401 case PTR:
402 buf_add(&buf, " to ");
403 buf_add(&buf, type_name(tp->t_subt));
404 break;
405 case ENUM:
406 type_name_of_enum(&buf, tp);
407 break;
408 case STRUCT:
409 case UNION:
410 type_name_of_struct_or_union(&buf, tp);
411 break;
412 case ARRAY:
413 type_name_of_array(&buf, tp);
414 break;
415 case FUNC:
416 type_name_of_function(&buf, tp);
417 break;
418 default:
419 INTERNAL_ERROR("type_name(%d)", t);
420 }
421
422 name = intern(buf.data);
423 buf_done(&buf);
424 return name;
425 }
426