Home | History | Annotate | Line # | Download | only in common
tyname.c revision 1.47
      1 /*	$NetBSD: tyname.c,v 1.47 2021/09/04 13:45:36 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.47 2021/09/04 13:45:36 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 	(void)snprintf(num, sizeof(num), "%d", n);
    147 	buf_add(buf, num);
    148 }
    149 
    150 /* XXX: at least partly redundant with ttab[t].tt_name */
    151 const char *
    152 tspec_name(tspec_t t)
    153 {
    154 	switch (t) {
    155 	case SIGNED:	return "signed";
    156 	case UNSIGN:	return "unsigned";
    157 	case BOOL:	return "_Bool";
    158 	case CHAR:	return "char";
    159 	case SCHAR:	return "signed char";
    160 	case UCHAR:	return "unsigned char";
    161 	case SHORT:	return "short";
    162 	case USHORT:	return "unsigned short";
    163 	case INT:	return "int";
    164 	case UINT:	return "unsigned int";
    165 	case LONG:	return "long";
    166 	case ULONG:	return "unsigned long";
    167 	case QUAD:	return "long long";
    168 	case UQUAD:	return "unsigned long long";
    169 #ifdef INT128_SIZE
    170 	case INT128:	return "__int128_t";
    171 	case UINT128:	return "__uint128_t";
    172 #endif
    173 	case FLOAT:	return "float";
    174 	case DOUBLE:	return "double";
    175 	case LDOUBLE:	return "long double";
    176 	case VOID:	return "void";
    177 	case STRUCT:	return "struct";
    178 	case UNION:	return "union";
    179 	case ENUM:	return "enum";
    180 	case PTR:	return "pointer";
    181 	case ARRAY:	return "array";
    182 	case FUNC:	return "function";
    183 	case COMPLEX:	return "_Complex";
    184 	case FCOMPLEX:	return "float _Complex";
    185 	case DCOMPLEX:	return "double _Complex";
    186 	case LCOMPLEX:	return "long double _Complex";
    187 	default:
    188 		INTERNAL_ERROR("tspec_name(%d)", t);
    189 		return NULL;
    190 	}
    191 }
    192 
    193 static void
    194 type_name_of_function(buffer *buf, const type_t *tp)
    195 {
    196 	const char *sep = "";
    197 
    198 	buf_add(buf, "(");
    199 	if (tp->t_proto) {
    200 #ifdef t_enum /* lint1 */
    201 		sym_t *arg;
    202 
    203 		arg = tp->t_args;
    204 		if (arg == NULL)
    205 			buf_add(buf, "void");
    206 		for (; arg != NULL; arg = arg->s_next) {
    207 			buf_add(buf, sep), sep = ", ";
    208 			buf_add(buf, type_name(arg->s_type));
    209 		}
    210 #else /* lint2 */
    211 		type_t **argtype;
    212 
    213 		argtype = tp->t_args;
    214 		if (argtype == NULL)
    215 			buf_add(buf, "void");
    216 		for (; *argtype != NULL; argtype++) {
    217 			buf_add(buf, sep), sep = ", ";
    218 			buf_add(buf, type_name(*argtype));
    219 		}
    220 #endif
    221 	}
    222 	if (tp->t_vararg) {
    223 		buf_add(buf, sep);
    224 		buf_add(buf, "...");
    225 	}
    226 	buf_add(buf, ") returning ");
    227 	buf_add(buf, type_name(tp->t_subt));
    228 }
    229 
    230 static void
    231 type_name_of_struct_or_union(buffer *buf, const type_t *tp)
    232 {
    233 	buf_add(buf, " ");
    234 #ifdef t_str
    235 	if (tp->t_str->sou_tag->s_name == unnamed &&
    236 	    tp->t_str->sou_first_typedef != NULL) {
    237 		buf_add(buf, "typedef ");
    238 		buf_add(buf, tp->t_str->sou_first_typedef->s_name);
    239 	} else {
    240 		buf_add(buf, tp->t_str->sou_tag->s_name);
    241 	}
    242 #else
    243 	buf_add(buf, tp->t_isuniqpos ? "*anonymous*" : tp->t_tag->h_name);
    244 #endif
    245 }
    246 
    247 static void
    248 type_name_of_enum(buffer *buf, const type_t *tp)
    249 {
    250 	buf_add(buf, " ");
    251 #ifdef t_enum
    252 	if (tp->t_enum->en_tag->s_name == unnamed &&
    253 	    tp->t_enum->en_first_typedef != NULL) {
    254 		buf_add(buf, "typedef ");
    255 		buf_add(buf, tp->t_enum->en_first_typedef->s_name);
    256 	} else {
    257 		buf_add(buf, tp->t_enum->en_tag->s_name);
    258 	}
    259 #else
    260 	buf_add(buf, tp->t_isuniqpos ? "*anonymous*" : tp->t_tag->h_name);
    261 #endif
    262 }
    263 
    264 static void
    265 type_name_of_array(buffer *buf, const type_t *tp)
    266 {
    267 	buf_add(buf, "[");
    268 #ifdef t_str /* lint1 */
    269 	if (tp->t_incomplete_array)
    270 		buf_add(buf, "unknown_size");
    271 	else
    272 		buf_add_int(buf, tp->t_dim);
    273 #else
    274 	buf_add_int(buf, tp->t_dim);
    275 #endif
    276 	buf_add(buf, "]");
    277 	buf_add(buf, " of ");
    278 	buf_add(buf, type_name(tp->t_subt));
    279 }
    280 
    281 const char *
    282 type_name(const type_t *tp)
    283 {
    284 	tspec_t t;
    285 	buffer buf;
    286 	const char *name;
    287 
    288 	if (tp == NULL)
    289 		return "(null)";
    290 
    291 	if ((t = tp->t_tspec) == INT && tp->t_is_enum)
    292 		t = ENUM;
    293 
    294 	buf_init(&buf);
    295 	if (tp->t_const)
    296 		buf_add(&buf, "const ");
    297 	if (tp->t_volatile)
    298 		buf_add(&buf, "volatile ");
    299 
    300 #ifdef t_str
    301 	if ((t == STRUCT || t == UNION) && tp->t_str->sou_incomplete)
    302 		buf_add(&buf, "incomplete ");
    303 #endif
    304 	buf_add(&buf, tspec_name(t));
    305 
    306 	switch (t) {
    307 	case PTR:
    308 		buf_add(&buf, " to ");
    309 		buf_add(&buf, type_name(tp->t_subt));
    310 		break;
    311 	case ENUM:
    312 		type_name_of_enum(&buf, tp);
    313 		break;
    314 	case STRUCT:
    315 	case UNION:
    316 		type_name_of_struct_or_union(&buf, tp);
    317 		break;
    318 	case ARRAY:
    319 		type_name_of_array(&buf, tp);
    320 		break;
    321 	case FUNC:
    322 		type_name_of_function(&buf, tp);
    323 		break;
    324 	default:
    325 		break;
    326 	}
    327 
    328 	name = intern(buf.data);
    329 	buf_done(&buf);
    330 	return name;
    331 }
    332