Home | History | Annotate | Line # | Download | only in common
tyname.c revision 1.59
      1 /*	$NetBSD: tyname.c,v 1.59 2024/02/01 18:37:06 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)
     38 __RCSID("$NetBSD: tyname.c,v 1.59 2024/02/01 18:37:06 rillig Exp $");
     39 #endif
     40 
     41 #include <assert.h>
     42 #include <limits.h>
     43 #include <string.h>
     44 #include <stdlib.h>
     45 
     46 #if IS_LINT1
     47 #include "lint1.h"
     48 #else
     49 #include "lint2.h"
     50 #endif
     51 
     52 /* A tree of strings. */
     53 typedef struct name_tree_node {
     54 	const char *ntn_name;
     55 	struct name_tree_node *ntn_less;
     56 	struct name_tree_node *ntn_greater;
     57 } name_tree_node;
     58 
     59 static name_tree_node *type_names;
     60 
     61 static name_tree_node *
     62 new_name_tree_node(const char *name)
     63 {
     64 	name_tree_node *n;
     65 
     66 	n = xmalloc(sizeof(*n));
     67 	n->ntn_name = xstrdup(name);
     68 	n->ntn_less = NULL;
     69 	n->ntn_greater = NULL;
     70 	return n;
     71 }
     72 
     73 /* Return the canonical instance of the string, with unlimited lifetime. */
     74 static const char *
     75 intern(const char *name)
     76 {
     77 	name_tree_node *n = type_names, **next;
     78 	int cmp;
     79 
     80 	if (n == NULL) {
     81 		n = new_name_tree_node(name);
     82 		type_names = n;
     83 		return n->ntn_name;
     84 	}
     85 
     86 	while ((cmp = strcmp(name, n->ntn_name)) != 0) {
     87 		next = cmp < 0 ? &n->ntn_less : &n->ntn_greater;
     88 		if (*next == NULL) {
     89 			*next = new_name_tree_node(name);
     90 			return (*next)->ntn_name;
     91 		}
     92 		n = *next;
     93 	}
     94 	return n->ntn_name;
     95 }
     96 
     97 static void
     98 buf_init(buffer *buf)
     99 {
    100 	buf->len = 0;
    101 	buf->cap = 128;
    102 	buf->data = xmalloc(buf->cap);
    103 	buf->data[0] = '\0';
    104 }
    105 
    106 static void
    107 buf_done(buffer *buf)
    108 {
    109 	free(buf->data);
    110 }
    111 
    112 static void
    113 buf_add(buffer *buf, const char *s)
    114 {
    115 	size_t len = strlen(s);
    116 
    117 	while (buf->len + len + 1 >= buf->cap) {
    118 		buf->data = xrealloc(buf->data, 2 * buf->cap);
    119 		buf->cap = 2 * buf->cap;
    120 	}
    121 
    122 	memcpy(buf->data + buf->len, s, len + 1);
    123 	buf->len += len;
    124 }
    125 
    126 static void
    127 buf_add_int(buffer *buf, int n)
    128 {
    129 	char num[1 + sizeof(n) * CHAR_BIT + 1];
    130 
    131 	(void)snprintf(num, sizeof(num), "%d", n);
    132 	buf_add(buf, num);
    133 }
    134 
    135 const char *
    136 tspec_name(tspec_t t)
    137 {
    138 	const char *name = ttab[t].tt_name;
    139 	assert(name != NULL);
    140 	return name;
    141 }
    142 
    143 static void
    144 type_name_of_function(buffer *buf, const type_t *tp)
    145 {
    146 	const char *sep = "";
    147 
    148 	buf_add(buf, "(");
    149 	if (tp->t_proto) {
    150 #if IS_LINT1
    151 		const sym_t *param = tp->t_params;
    152 		if (param == NULL)
    153 			buf_add(buf, "void");
    154 		for (; param != NULL; param = param->s_next) {
    155 			buf_add(buf, sep), sep = ", ";
    156 			buf_add(buf, type_name(param->s_type));
    157 		}
    158 #else
    159 		type_t **argtype;
    160 
    161 		argtype = tp->t_args;
    162 		if (argtype == NULL)
    163 			buf_add(buf, "void");
    164 		for (; *argtype != NULL; argtype++) {
    165 			buf_add(buf, sep), sep = ", ";
    166 			buf_add(buf, type_name(*argtype));
    167 		}
    168 #endif
    169 	}
    170 	if (tp->t_vararg) {
    171 		buf_add(buf, sep);
    172 		buf_add(buf, "...");
    173 	}
    174 	buf_add(buf, ") returning ");
    175 	buf_add(buf, type_name(tp->t_subt));
    176 }
    177 
    178 static void
    179 type_name_of_struct_or_union(buffer *buf, const type_t *tp)
    180 {
    181 	buf_add(buf, " ");
    182 #if IS_LINT1
    183 	if (tp->t_sou->sou_tag->s_name == unnamed &&
    184 	    tp->t_sou->sou_first_typedef != NULL) {
    185 		buf_add(buf, "typedef ");
    186 		buf_add(buf, tp->t_sou->sou_first_typedef->s_name);
    187 	} else {
    188 		buf_add(buf, tp->t_sou->sou_tag->s_name);
    189 	}
    190 #else
    191 	buf_add(buf, tp->t_isuniqpos ? "*anonymous*" : tp->t_tag->h_name);
    192 #endif
    193 }
    194 
    195 static void
    196 type_name_of_enum(buffer *buf, const type_t *tp)
    197 {
    198 	buf_add(buf, " ");
    199 #if IS_LINT1
    200 	if (tp->t_enum->en_tag->s_name == unnamed &&
    201 	    tp->t_enum->en_first_typedef != NULL) {
    202 		buf_add(buf, "typedef ");
    203 		buf_add(buf, tp->t_enum->en_first_typedef->s_name);
    204 	} else {
    205 		buf_add(buf, tp->t_enum->en_tag->s_name);
    206 	}
    207 #else
    208 	buf_add(buf, tp->t_isuniqpos ? "*anonymous*" : tp->t_tag->h_name);
    209 #endif
    210 }
    211 
    212 static void
    213 type_name_of_array(buffer *buf, const type_t *tp)
    214 {
    215 	buf_add(buf, "[");
    216 #if IS_LINT1
    217 	if (tp->t_incomplete_array)
    218 		buf_add(buf, "unknown_size");
    219 	else
    220 		buf_add_int(buf, tp->t_dim);
    221 #else
    222 	buf_add_int(buf, tp->t_dim);
    223 #endif
    224 	buf_add(buf, "]");
    225 	buf_add(buf, " of ");
    226 	buf_add(buf, type_name(tp->t_subt));
    227 }
    228 
    229 const char *
    230 type_name(const type_t *tp)
    231 {
    232 	tspec_t t;
    233 	buffer buf;
    234 	const char *name;
    235 
    236 	if (tp == NULL)
    237 		return "(null)";
    238 
    239 	if ((t = tp->t_tspec) == INT && tp->t_is_enum)
    240 		t = ENUM;
    241 
    242 	buf_init(&buf);
    243 	if (tp->t_const)
    244 		buf_add(&buf, "const ");
    245 	if (tp->t_volatile)
    246 		buf_add(&buf, "volatile ");
    247 
    248 #if IS_LINT1
    249 	if (is_struct_or_union(t) && tp->t_sou->sou_incomplete)
    250 		buf_add(&buf, "incomplete ");
    251 #endif
    252 	buf_add(&buf, tspec_name(t));
    253 
    254 #if IS_LINT1
    255 	if (tp->t_bitfield) {
    256 		buf_add(&buf, ":");
    257 		buf_add_int(&buf, (int)tp->t_bit_field_width);
    258 	}
    259 #endif
    260 
    261 	switch (t) {
    262 	case PTR:
    263 		buf_add(&buf, " to ");
    264 		buf_add(&buf, type_name(tp->t_subt));
    265 		break;
    266 	case ENUM:
    267 		type_name_of_enum(&buf, tp);
    268 		break;
    269 	case STRUCT:
    270 	case UNION:
    271 		type_name_of_struct_or_union(&buf, tp);
    272 		break;
    273 	case ARRAY:
    274 		type_name_of_array(&buf, tp);
    275 		break;
    276 	case FUNC:
    277 		type_name_of_function(&buf, tp);
    278 		break;
    279 	default:
    280 		break;
    281 	}
    282 
    283 	name = intern(buf.data);
    284 	buf_done(&buf);
    285 	return name;
    286 }
    287