Home | History | Annotate | Line # | Download | only in common
tyname.c revision 1.40
      1 /*	$NetBSD: tyname.c,v 1.40 2021/04/18 17:47:32 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.40 2021/04/18 17:47:32 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/void */
    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 		for (arg = tp->t_args; arg != NULL; arg = arg->s_next) {
    267 			buf_add(buf, sep), sep = ", ";
    268 			buf_add(buf, type_name(arg->s_type));
    269 		}
    270 #else /* lint2 */
    271 		type_t **argtype;
    272 
    273 		for (argtype = tp->t_args; *argtype != NULL; argtype++) {
    274 			buf_add(buf, sep), sep = ", ";
    275 			buf_add(buf, type_name(*argtype));
    276 		}
    277 #endif
    278 	}
    279 	if (tp->t_vararg) {
    280 		buf_add(buf, sep);
    281 		buf_add(buf, "...");
    282 	}
    283 	buf_add(buf, ") returning ");
    284 	buf_add(buf, type_name(tp->t_subt));
    285 }
    286 
    287 static void
    288 type_name_of_struct_or_union(buffer *buf, const type_t *tp)
    289 {
    290 	buf_add(buf, " ");
    291 #ifdef t_str
    292 	if (tp->t_str->sou_tag->s_name == unnamed &&
    293 	    tp->t_str->sou_first_typedef != NULL) {
    294 		buf_add(buf, "typedef ");
    295 		buf_add(buf, tp->t_str->sou_first_typedef->s_name);
    296 	} else {
    297 		buf_add(buf, tp->t_str->sou_tag->s_name);
    298 	}
    299 #else
    300 	buf_add(buf, tp->t_isuniqpos ? "*anonymous*" : tp->t_tag->h_name);
    301 #endif
    302 }
    303 
    304 static void
    305 type_name_of_enum(buffer *buf, const type_t *tp)
    306 {
    307 	buf_add(buf, " ");
    308 #ifdef t_enum
    309 	if (tp->t_enum->en_tag->s_name == unnamed &&
    310 	    tp->t_enum->en_first_typedef != NULL) {
    311 		buf_add(buf, "typedef ");
    312 		buf_add(buf, tp->t_enum->en_first_typedef->s_name);
    313 	} else {
    314 		buf_add(buf, tp->t_enum->en_tag->s_name);
    315 	}
    316 #else
    317 	buf_add(buf, tp->t_isuniqpos ? "*anonymous*" : tp->t_tag->h_name);
    318 #endif
    319 }
    320 
    321 static void
    322 type_name_of_array(buffer *buf, const type_t *tp)
    323 {
    324 	buf_add(buf, "[");
    325 #ifdef t_str /* lint1 */
    326 	if (tp->t_incomplete_array)
    327 		buf_add(buf, "unknown_size");
    328 	else
    329 		buf_add_int(buf, tp->t_dim);
    330 #else
    331 	buf_add_int(buf, tp->t_dim);
    332 #endif
    333 	buf_add(buf, "]");
    334 	buf_add(buf, " of ");
    335 	buf_add(buf, type_name(tp->t_subt));
    336 }
    337 
    338 const char *
    339 type_name(const type_t *tp)
    340 {
    341 	tspec_t t;
    342 	buffer buf;
    343 	const char *name;
    344 
    345 	if (tp == NULL)
    346 		return "(null)";
    347 
    348 	/*
    349 	 * XXX: Why is this necessary, and in which cases does this apply?
    350 	 * Shouldn't the type be an ENUM from the beginning?
    351 	 */
    352 	if ((t = tp->t_tspec) == INT && tp->t_is_enum)
    353 		t = ENUM;
    354 
    355 	buf_init(&buf);
    356 	if (tp->t_const)
    357 		buf_add(&buf, "const ");
    358 	if (tp->t_volatile)
    359 		buf_add(&buf, "volatile ");
    360 
    361 #ifdef t_str
    362 	if ((t == STRUCT || t == UNION) && tp->t_str->sou_incomplete)
    363 		buf_add(&buf, "incomplete ");
    364 #endif
    365 	buf_add(&buf, tspec_name(t));
    366 
    367 	switch (t) {
    368 	case BOOL:
    369 	case CHAR:
    370 	case UCHAR:
    371 	case SCHAR:
    372 	case SHORT:
    373 	case USHORT:
    374 	case INT:
    375 	case UINT:
    376 	case LONG:
    377 	case ULONG:
    378 	case QUAD:
    379 	case UQUAD:
    380 #ifdef INT128_SIZE
    381 	case INT128:
    382 	case UINT128:
    383 #endif
    384 	case FLOAT:
    385 	case DOUBLE:
    386 	case LDOUBLE:
    387 	case VOID:
    388 	case COMPLEX:
    389 	case FCOMPLEX:
    390 	case DCOMPLEX:
    391 	case LCOMPLEX:
    392 	case SIGNED:
    393 	case UNSIGN:
    394 		break;
    395 	case PTR:
    396 		buf_add(&buf, " to ");
    397 		buf_add(&buf, type_name(tp->t_subt));
    398 		break;
    399 	case ENUM:
    400 		type_name_of_enum(&buf, tp);
    401 		break;
    402 	case STRUCT:
    403 	case UNION:
    404 		type_name_of_struct_or_union(&buf, tp);
    405 		break;
    406 	case ARRAY:
    407 		type_name_of_array(&buf, tp);
    408 		break;
    409 	case FUNC:
    410 		type_name_of_function(&buf, tp);
    411 		break;
    412 	default:
    413 		INTERNAL_ERROR("type_name(%d)", t);
    414 	}
    415 
    416 	name = intern(buf.data);
    417 	buf_done(&buf);
    418 	return name;
    419 }
    420