Home | History | Annotate | Line # | Download | only in common
tyname.c revision 1.29
      1  1.29    rillig /*	$NetBSD: tyname.c,v 1.29 2021/02/19 22:27:49 rillig Exp $	*/
      2   1.3     skrll 
      3   1.1  christos /*-
      4   1.1  christos  * Copyright (c) 2005 The NetBSD Foundation, Inc.
      5   1.1  christos  * All rights reserved.
      6   1.1  christos  *
      7   1.1  christos  * This code is derived from software contributed to The NetBSD Foundation
      8   1.1  christos  * by Christos Zoulas.
      9   1.1  christos  *
     10   1.1  christos  * Redistribution and use in source and binary forms, with or without
     11   1.1  christos  * modification, are permitted provided that the following conditions
     12   1.1  christos  * are met:
     13   1.1  christos  * 1. Redistributions of source code must retain the above copyright
     14   1.1  christos  *    notice, this list of conditions and the following disclaimer.
     15   1.1  christos  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.1  christos  *    notice, this list of conditions and the following disclaimer in the
     17   1.1  christos  *    documentation and/or other materials provided with the distribution.
     18   1.1  christos  *
     19   1.1  christos  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20   1.1  christos  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21   1.1  christos  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22   1.1  christos  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23   1.1  christos  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24   1.1  christos  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25   1.1  christos  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26   1.1  christos  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27   1.1  christos  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28   1.1  christos  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29   1.1  christos  * POSSIBILITY OF SUCH DAMAGE.
     30   1.1  christos  */
     31   1.1  christos 
     32   1.1  christos #if HAVE_NBTOOL_CONFIG_H
     33   1.1  christos #include "nbtool_config.h"
     34   1.1  christos #endif
     35   1.1  christos 
     36   1.1  christos #include <sys/cdefs.h>
     37   1.1  christos #if defined(__RCSID) && !defined(lint)
     38  1.29    rillig __RCSID("$NetBSD: tyname.c,v 1.29 2021/02/19 22:27:49 rillig Exp $");
     39   1.1  christos #endif
     40   1.1  christos 
     41   1.1  christos #include <limits.h>
     42   1.8  christos #include <string.h>
     43   1.1  christos #include <stdlib.h>
     44   1.1  christos #include <err.h>
     45   1.1  christos 
     46   1.1  christos #include PASS
     47   1.1  christos 
     48   1.1  christos #ifndef LERROR
     49  1.15    rillig #define LERROR(fmt, args...) \
     50  1.15    rillig 	do { \
     51  1.15    rillig 		(void)warnx("%s, %d: " fmt, __FILE__, __LINE__, ##args); \
     52  1.15    rillig 		abort(); \
     53  1.24    rillig 	} while (/*CONSTCOND*/false)
     54   1.1  christos #endif
     55   1.1  christos 
     56  1.20    rillig /* A tree of strings. */
     57  1.20    rillig typedef struct name_tree_node {
     58  1.20    rillig 	char *ntn_name;
     59  1.20    rillig 	struct name_tree_node *ntn_less;
     60  1.20    rillig 	struct name_tree_node *ntn_greater;
     61  1.20    rillig } name_tree_node;
     62  1.20    rillig 
     63  1.20    rillig /* A growable string buffer. */
     64  1.20    rillig typedef struct buffer {
     65  1.20    rillig 	size_t	len;
     66  1.20    rillig 	size_t	cap;
     67  1.20    rillig 	char *	data;
     68  1.20    rillig } buffer;
     69  1.20    rillig 
     70  1.20    rillig static name_tree_node *type_names;
     71  1.20    rillig 
     72  1.20    rillig static name_tree_node *
     73  1.20    rillig new_name_tree_node(const char *name)
     74  1.20    rillig {
     75  1.20    rillig 	name_tree_node *n;
     76  1.20    rillig 
     77  1.20    rillig 	n = xmalloc(sizeof(*n));
     78  1.20    rillig 	n->ntn_name = xstrdup(name);
     79  1.20    rillig 	n->ntn_less = NULL;
     80  1.20    rillig 	n->ntn_greater = NULL;
     81  1.20    rillig 	return n;
     82  1.20    rillig }
     83  1.20    rillig 
     84  1.20    rillig /* Return the canonical instance of the string, with unlimited life time. */
     85  1.26    rillig static const char *
     86  1.20    rillig intern(const char *name)
     87  1.20    rillig {
     88  1.25    rillig 	name_tree_node *n = type_names, **next;
     89  1.20    rillig 	int cmp;
     90  1.20    rillig 
     91  1.20    rillig 	if (n == NULL) {
     92  1.20    rillig 		n = new_name_tree_node(name);
     93  1.20    rillig 		type_names = n;
     94  1.20    rillig 		return n->ntn_name;
     95  1.20    rillig 	}
     96  1.20    rillig 
     97  1.20    rillig 	while ((cmp = strcmp(name, n->ntn_name)) != 0) {
     98  1.25    rillig 		next = cmp < 0 ? &n->ntn_less : &n->ntn_greater;
     99  1.25    rillig 		if (*next == NULL) {
    100  1.25    rillig 			*next = new_name_tree_node(name);
    101  1.25    rillig 			return (*next)->ntn_name;
    102  1.20    rillig 		}
    103  1.25    rillig 		n = *next;
    104  1.20    rillig 	}
    105  1.20    rillig 	return n->ntn_name;
    106  1.20    rillig }
    107  1.20    rillig 
    108  1.20    rillig static void
    109  1.20    rillig buf_init(buffer *buf)
    110  1.20    rillig {
    111  1.20    rillig 	buf->len = 0;
    112  1.20    rillig 	buf->cap = 128;
    113  1.20    rillig 	buf->data = xmalloc(buf->cap);
    114  1.20    rillig 	buf->data[0] = '\0';
    115  1.20    rillig }
    116  1.20    rillig 
    117  1.20    rillig static void
    118  1.20    rillig buf_done(buffer *buf)
    119  1.20    rillig {
    120  1.20    rillig 	free(buf->data);
    121  1.20    rillig }
    122  1.20    rillig 
    123  1.20    rillig static void
    124  1.20    rillig buf_add(buffer *buf, const char *s)
    125  1.20    rillig {
    126  1.20    rillig 	size_t len = strlen(s);
    127  1.20    rillig 
    128  1.20    rillig 	while (buf->len + len + 1 >= buf->cap) {
    129  1.20    rillig 		buf->data = xrealloc(buf->data, 2 * buf->cap);
    130  1.20    rillig 		buf->cap = 2 * buf->cap;
    131  1.20    rillig 	}
    132  1.20    rillig 
    133  1.20    rillig 	memcpy(buf->data + buf->len, s, len + 1);
    134  1.20    rillig 	buf->len += len;
    135  1.20    rillig }
    136  1.20    rillig 
    137  1.20    rillig static void
    138  1.20    rillig buf_add_int(buffer *buf, int n)
    139  1.20    rillig {
    140  1.20    rillig 	char num[1 + sizeof(n) * CHAR_BIT + 1];
    141  1.20    rillig 
    142  1.20    rillig 	snprintf(num, sizeof num, "%d", n);
    143  1.20    rillig 	buf_add(buf, num);
    144  1.20    rillig }
    145  1.20    rillig 
    146   1.1  christos const char *
    147  1.18    rillig tspec_name(tspec_t t)
    148   1.1  christos {
    149   1.1  christos 	switch (t) {
    150  1.19    rillig 	case SIGNED:	return "signed";
    151  1.19    rillig 	case UNSIGN:	return "unsigned";
    152   1.1  christos 	case BOOL:	return "_Bool";
    153   1.1  christos 	case CHAR:	return "char";
    154  1.19    rillig 	case SCHAR:	return "signed char";
    155   1.1  christos 	case UCHAR:	return "unsigned char";
    156   1.1  christos 	case SHORT:	return "short";
    157   1.1  christos 	case USHORT:	return "unsigned short";
    158   1.1  christos 	case INT:	return "int";
    159   1.1  christos 	case UINT:	return "unsigned int";
    160   1.1  christos 	case LONG:	return "long";
    161   1.1  christos 	case ULONG:	return "unsigned long";
    162   1.1  christos 	case QUAD:	return "long long";
    163   1.1  christos 	case UQUAD:	return "unsigned long long";
    164  1.13  christos #ifdef INT128_SIZE
    165  1.13  christos 	case INT128:	return "__int128_t";
    166  1.13  christos 	case UINT128:	return "__uint128_t";
    167  1.13  christos #endif
    168   1.1  christos 	case FLOAT:	return "float";
    169   1.1  christos 	case DOUBLE:	return "double";
    170   1.1  christos 	case LDOUBLE:	return "long double";
    171   1.2       skd 	case VOID:	return "void";
    172   1.1  christos 	case STRUCT:	return "struct";
    173   1.1  christos 	case UNION:	return "union";
    174  1.19    rillig 	case ENUM:	return "enum";
    175  1.19    rillig 	case PTR:	return "pointer";
    176  1.19    rillig 	case ARRAY:	return "array";
    177   1.1  christos 	case FUNC:	return "function";
    178  1.19    rillig 	case COMPLEX:	return "_Complex";
    179   1.5  christos 	case FCOMPLEX:	return "float _Complex";
    180   1.5  christos 	case DCOMPLEX:	return "double _Complex";
    181   1.9      matt 	case LCOMPLEX:	return "long double _Complex";
    182   1.1  christos 	default:
    183  1.18    rillig 		LERROR("tspec_name(%d)", t);
    184   1.1  christos 		return NULL;
    185   1.1  christos 	}
    186   1.1  christos }
    187   1.1  christos 
    188  1.23    rillig bool
    189  1.12  christos sametype(const type_t *t1, const type_t *t2)
    190  1.12  christos {
    191  1.12  christos 	tspec_t	t;
    192  1.12  christos 
    193  1.12  christos 	if (t1->t_tspec != t2->t_tspec)
    194  1.23    rillig 		return false;
    195  1.12  christos 
    196  1.12  christos 	/* Ignore const/void */
    197  1.12  christos 
    198  1.12  christos 	switch (t = t1->t_tspec) {
    199  1.12  christos 	case BOOL:
    200  1.12  christos 	case CHAR:
    201  1.12  christos 	case UCHAR:
    202  1.12  christos 	case SCHAR:
    203  1.12  christos 	case SHORT:
    204  1.12  christos 	case USHORT:
    205  1.12  christos 	case INT:
    206  1.12  christos 	case UINT:
    207  1.12  christos 	case LONG:
    208  1.12  christos 	case ULONG:
    209  1.12  christos 	case QUAD:
    210  1.12  christos 	case UQUAD:
    211  1.13  christos #ifdef INT128_SIZE
    212  1.13  christos 	case INT128:
    213  1.13  christos 	case UINT128:
    214  1.13  christos #endif
    215  1.12  christos 	case FLOAT:
    216  1.12  christos 	case DOUBLE:
    217  1.12  christos 	case LDOUBLE:
    218  1.12  christos 	case VOID:
    219  1.12  christos 	case FUNC:
    220  1.12  christos 	case COMPLEX:
    221  1.12  christos 	case FCOMPLEX:
    222  1.12  christos 	case DCOMPLEX:
    223  1.12  christos 	case LCOMPLEX:
    224  1.24    rillig 		return true;
    225  1.12  christos 	case ARRAY:
    226  1.12  christos 		if (t1->t_dim != t2->t_dim)
    227  1.23    rillig 			return false;
    228  1.12  christos 		/*FALLTHROUGH*/
    229  1.12  christos 	case PTR:
    230  1.12  christos 		return sametype(t1->t_subt, t2->t_subt);
    231  1.12  christos 	case ENUM:
    232  1.12  christos #ifdef t_enum
    233  1.28    rillig 		return strcmp(t1->t_enum->en_tag->s_name,
    234  1.28    rillig 		    t2->t_enum->en_tag->s_name) == 0;
    235  1.12  christos #else
    236  1.23    rillig 		return true;
    237  1.12  christos #endif
    238  1.12  christos 	case STRUCT:
    239  1.12  christos 	case UNION:
    240  1.12  christos #ifdef t_str
    241  1.27    rillig 		return strcmp(t1->t_str->sou_tag->s_name,
    242  1.27    rillig 		    t2->t_str->sou_tag->s_name) == 0;
    243  1.12  christos #else
    244  1.23    rillig 		return true;
    245  1.12  christos #endif
    246  1.12  christos 	default:
    247  1.12  christos 		LERROR("tyname(%d)", t);
    248  1.23    rillig 		return false;
    249  1.12  christos 	}
    250  1.12  christos }
    251  1.12  christos 
    252  1.21    rillig static void
    253  1.21    rillig type_name_of_function(buffer *buf, const type_t *tp)
    254  1.21    rillig {
    255  1.21    rillig 	const char *sep = "";
    256  1.21    rillig 
    257  1.21    rillig 	buf_add(buf, "(");
    258  1.21    rillig 	if (tp->t_proto) {
    259  1.21    rillig #ifdef t_enum /* lint1 */
    260  1.21    rillig 		sym_t *arg;
    261  1.21    rillig 
    262  1.21    rillig 		for (arg = tp->t_args; arg != NULL; arg = arg->s_next) {
    263  1.21    rillig 			buf_add(buf, sep), sep = ", ";
    264  1.21    rillig 			buf_add(buf, type_name(arg->s_type));
    265  1.21    rillig 		}
    266  1.21    rillig #else /* lint2 */
    267  1.21    rillig 		type_t **argtype;
    268  1.21    rillig 
    269  1.21    rillig 		for (argtype = tp->t_args; *argtype != NULL; argtype++) {
    270  1.21    rillig 			buf_add(buf, sep), sep = ", ";
    271  1.21    rillig 			buf_add(buf, type_name(*argtype));
    272  1.21    rillig 		}
    273  1.21    rillig #endif
    274  1.21    rillig 	}
    275  1.21    rillig 	if (tp->t_vararg) {
    276  1.22    rillig 		buf_add(buf, sep);
    277  1.21    rillig 		buf_add(buf, "...");
    278  1.21    rillig 	}
    279  1.21    rillig 	buf_add(buf, ") returning ");
    280  1.21    rillig 	buf_add(buf, type_name(tp->t_subt));
    281  1.21    rillig }
    282  1.21    rillig 
    283   1.1  christos const char *
    284  1.20    rillig type_name(const type_t *tp)
    285   1.1  christos {
    286  1.20    rillig 	tspec_t t;
    287  1.20    rillig 	buffer buf;
    288  1.20    rillig 	const char *name;
    289   1.1  christos 
    290  1.11  christos 	if (tp == NULL)
    291  1.11  christos 		return "(null)";
    292  1.20    rillig 
    293  1.20    rillig 	/*
    294  1.20    rillig 	 * XXX: Why is this necessary, and in which cases does this apply?
    295  1.20    rillig 	 * Shouldn't the type be an ENUM from the beginning?
    296  1.20    rillig 	 */
    297  1.29    rillig 	if ((t = tp->t_tspec) == INT && tp->t_is_enum)
    298   1.1  christos 		t = ENUM;
    299   1.1  christos 
    300  1.20    rillig 	buf_init(&buf);
    301   1.8  christos 	if (tp->t_const)
    302  1.20    rillig 		buf_add(&buf, "const ");
    303   1.8  christos 	if (tp->t_volatile)
    304  1.20    rillig 		buf_add(&buf, "volatile ");
    305  1.20    rillig 	buf_add(&buf, tspec_name(t));
    306   1.1  christos 
    307   1.1  christos 	switch (t) {
    308   1.1  christos 	case BOOL:
    309   1.1  christos 	case CHAR:
    310   1.1  christos 	case UCHAR:
    311   1.1  christos 	case SCHAR:
    312   1.1  christos 	case SHORT:
    313   1.1  christos 	case USHORT:
    314   1.1  christos 	case INT:
    315   1.1  christos 	case UINT:
    316   1.1  christos 	case LONG:
    317   1.1  christos 	case ULONG:
    318   1.1  christos 	case QUAD:
    319   1.1  christos 	case UQUAD:
    320  1.13  christos #ifdef INT128_SIZE
    321  1.13  christos 	case INT128:
    322  1.13  christos 	case UINT128:
    323  1.13  christos #endif
    324   1.1  christos 	case FLOAT:
    325   1.1  christos 	case DOUBLE:
    326   1.1  christos 	case LDOUBLE:
    327   1.2       skd 	case VOID:
    328   1.5  christos 	case COMPLEX:
    329   1.5  christos 	case FCOMPLEX:
    330   1.5  christos 	case DCOMPLEX:
    331  1.10      matt 	case LCOMPLEX:
    332  1.17    rillig 	case SIGNED:
    333  1.17    rillig 	case UNSIGN:
    334   1.1  christos 		break;
    335   1.1  christos 	case PTR:
    336  1.20    rillig 		buf_add(&buf, " to ");
    337  1.20    rillig 		buf_add(&buf, type_name(tp->t_subt));
    338   1.1  christos 		break;
    339   1.1  christos 	case ENUM:
    340  1.20    rillig 		buf_add(&buf, " ");
    341   1.1  christos #ifdef t_enum
    342  1.28    rillig 		buf_add(&buf, tp->t_enum->en_tag->s_name);
    343   1.1  christos #else
    344  1.20    rillig 		buf_add(&buf,
    345  1.20    rillig 		    tp->t_isuniqpos ? "*anonymous*" : tp->t_tag->h_name);
    346   1.1  christos #endif
    347   1.1  christos 		break;
    348   1.1  christos 	case STRUCT:
    349   1.1  christos 	case UNION:
    350  1.20    rillig 		buf_add(&buf, " ");
    351   1.1  christos #ifdef t_str
    352  1.27    rillig 		buf_add(&buf, tp->t_str->sou_tag->s_name);
    353   1.1  christos #else
    354  1.20    rillig 		buf_add(&buf,
    355  1.20    rillig 		    tp->t_isuniqpos ? "*anonymous*" : tp->t_tag->h_name);
    356   1.1  christos #endif
    357   1.1  christos 		break;
    358   1.1  christos 	case ARRAY:
    359  1.20    rillig 		buf_add(&buf, " of ");
    360  1.20    rillig 		buf_add(&buf, type_name(tp->t_subt));
    361  1.20    rillig 		buf_add(&buf, "[");
    362  1.20    rillig 		buf_add_int(&buf, tp->t_dim);
    363  1.20    rillig 		buf_add(&buf, "]");
    364   1.1  christos 		break;
    365  1.21    rillig 	case FUNC:
    366  1.21    rillig 		type_name_of_function(&buf, tp);
    367  1.21    rillig 		break;
    368  1.21    rillig 
    369   1.1  christos 	default:
    370  1.21    rillig 		LERROR("type_name(%d)", t);
    371   1.1  christos 	}
    372  1.20    rillig 
    373  1.20    rillig 	name = intern(buf.data);
    374  1.20    rillig 	buf_done(&buf);
    375  1.20    rillig 	return name;
    376   1.1  christos }
    377