Home | History | Annotate | Line # | Download | only in common
tyname.c revision 1.18
      1  1.18    rillig /*	$NetBSD: tyname.c,v 1.18 2021/01/01 01:42:55 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.18    rillig __RCSID("$NetBSD: tyname.c,v 1.18 2021/01/01 01:42:55 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.15    rillig 	} while (/*CONSTCOND*/0)
     54   1.1  christos #endif
     55   1.1  christos 
     56   1.1  christos const char *
     57  1.18    rillig tspec_name(tspec_t t)
     58   1.1  christos {
     59   1.1  christos 	switch (t) {
     60   1.1  christos 	case BOOL:	return "_Bool";
     61   1.1  christos 	case CHAR:	return "char";
     62   1.1  christos 	case UCHAR:	return "unsigned char";
     63   1.1  christos 	case SCHAR:	return "signed char";
     64   1.1  christos 	case SHORT:	return "short";
     65   1.1  christos 	case USHORT:	return "unsigned short";
     66   1.1  christos 	case INT:	return "int";
     67   1.1  christos 	case UINT:	return "unsigned int";
     68   1.1  christos 	case LONG:	return "long";
     69   1.1  christos 	case ULONG:	return "unsigned long";
     70   1.1  christos 	case QUAD:	return "long long";
     71   1.1  christos 	case UQUAD:	return "unsigned long long";
     72  1.13  christos #ifdef INT128_SIZE
     73  1.13  christos 	case INT128:	return "__int128_t";
     74  1.13  christos 	case UINT128:	return "__uint128_t";
     75  1.13  christos #endif
     76   1.1  christos 	case FLOAT:	return "float";
     77   1.1  christos 	case DOUBLE:	return "double";
     78   1.1  christos 	case LDOUBLE:	return "long double";
     79   1.2       skd 	case VOID:	return "void";
     80   1.1  christos 	case PTR:	return "pointer";
     81   1.1  christos 	case ENUM:	return "enum";
     82   1.1  christos 	case STRUCT:	return "struct";
     83   1.1  christos 	case UNION:	return "union";
     84   1.1  christos 	case FUNC:	return "function";
     85   1.1  christos 	case ARRAY:	return "array";
     86   1.5  christos 	case FCOMPLEX:	return "float _Complex";
     87   1.5  christos 	case DCOMPLEX:	return "double _Complex";
     88   1.9      matt 	case LCOMPLEX:	return "long double _Complex";
     89   1.5  christos 	case COMPLEX:	return "_Complex";
     90  1.17    rillig 	case SIGNED:	return "signed";
     91  1.17    rillig 	case UNSIGN:	return "unsigned";
     92   1.1  christos 	default:
     93  1.18    rillig 		LERROR("tspec_name(%d)", t);
     94   1.1  christos 		return NULL;
     95   1.1  christos 	}
     96   1.1  christos }
     97   1.1  christos 
     98  1.12  christos int
     99  1.12  christos sametype(const type_t *t1, const type_t *t2)
    100  1.12  christos {
    101  1.12  christos 	tspec_t	t;
    102  1.12  christos 
    103  1.12  christos 	if (t1->t_tspec != t2->t_tspec)
    104  1.12  christos 		return 0;
    105  1.12  christos 
    106  1.12  christos 	/* Ignore const/void */
    107  1.12  christos 
    108  1.12  christos 	switch (t = t1->t_tspec) {
    109  1.12  christos 	case BOOL:
    110  1.12  christos 	case CHAR:
    111  1.12  christos 	case UCHAR:
    112  1.12  christos 	case SCHAR:
    113  1.12  christos 	case SHORT:
    114  1.12  christos 	case USHORT:
    115  1.12  christos 	case INT:
    116  1.12  christos 	case UINT:
    117  1.12  christos 	case LONG:
    118  1.12  christos 	case ULONG:
    119  1.12  christos 	case QUAD:
    120  1.12  christos 	case UQUAD:
    121  1.13  christos #ifdef INT128_SIZE
    122  1.13  christos 	case INT128:
    123  1.13  christos 	case UINT128:
    124  1.13  christos #endif
    125  1.12  christos 	case FLOAT:
    126  1.12  christos 	case DOUBLE:
    127  1.12  christos 	case LDOUBLE:
    128  1.12  christos 	case VOID:
    129  1.12  christos 	case FUNC:
    130  1.12  christos 	case COMPLEX:
    131  1.12  christos 	case FCOMPLEX:
    132  1.12  christos 	case DCOMPLEX:
    133  1.12  christos 	case LCOMPLEX:
    134  1.12  christos 		return 1;
    135  1.12  christos 	case ARRAY:
    136  1.12  christos 		if (t1->t_dim != t2->t_dim)
    137  1.12  christos 			return 0;
    138  1.12  christos 		/*FALLTHROUGH*/
    139  1.12  christos 	case PTR:
    140  1.12  christos 		return sametype(t1->t_subt, t2->t_subt);
    141  1.12  christos 	case ENUM:
    142  1.12  christos #ifdef t_enum
    143  1.12  christos 		return strcmp(t1->t_enum->etag->s_name,
    144  1.12  christos 		    t2->t_enum->etag->s_name) == 0;
    145  1.12  christos #else
    146  1.12  christos 		return 1;
    147  1.12  christos #endif
    148  1.12  christos 	case STRUCT:
    149  1.12  christos 	case UNION:
    150  1.12  christos #ifdef t_str
    151  1.12  christos 		return strcmp(t1->t_str->stag->s_name,
    152  1.12  christos 		    t2->t_str->stag->s_name) == 0;
    153  1.12  christos #else
    154  1.12  christos 		return 1;
    155  1.12  christos #endif
    156  1.12  christos 	default:
    157  1.12  christos 		LERROR("tyname(%d)", t);
    158  1.12  christos 		return 0;
    159  1.12  christos 	}
    160  1.12  christos }
    161  1.12  christos 
    162   1.1  christos const char *
    163  1.12  christos tyname(char *buf, size_t bufsiz, const type_t *tp)
    164   1.1  christos {
    165   1.1  christos 	tspec_t	t;
    166   1.1  christos 	const	char *s;
    167   1.1  christos 	char lbuf[64];
    168   1.8  christos 	char cv[20];
    169   1.1  christos 
    170  1.11  christos 	if (tp == NULL)
    171  1.11  christos 		return "(null)";
    172   1.1  christos 	if ((t = tp->t_tspec) == INT && tp->t_isenum)
    173   1.1  christos 		t = ENUM;
    174   1.1  christos 
    175  1.18    rillig 	s = tspec_name(t);
    176   1.1  christos 
    177   1.8  christos 	cv[0] = '\0';
    178   1.8  christos 	if (tp->t_const)
    179   1.8  christos 		(void)strcat(cv, "const ");
    180   1.8  christos 	if (tp->t_volatile)
    181   1.8  christos 		(void)strcat(cv, "volatile ");
    182   1.1  christos 
    183   1.1  christos 	switch (t) {
    184   1.1  christos 	case BOOL:
    185   1.1  christos 	case CHAR:
    186   1.1  christos 	case UCHAR:
    187   1.1  christos 	case SCHAR:
    188   1.1  christos 	case SHORT:
    189   1.1  christos 	case USHORT:
    190   1.1  christos 	case INT:
    191   1.1  christos 	case UINT:
    192   1.1  christos 	case LONG:
    193   1.1  christos 	case ULONG:
    194   1.1  christos 	case QUAD:
    195   1.1  christos 	case UQUAD:
    196  1.13  christos #ifdef INT128_SIZE
    197  1.13  christos 	case INT128:
    198  1.13  christos 	case UINT128:
    199  1.13  christos #endif
    200   1.1  christos 	case FLOAT:
    201   1.1  christos 	case DOUBLE:
    202   1.1  christos 	case LDOUBLE:
    203   1.2       skd 	case VOID:
    204   1.1  christos 	case FUNC:
    205   1.5  christos 	case COMPLEX:
    206   1.5  christos 	case FCOMPLEX:
    207   1.5  christos 	case DCOMPLEX:
    208  1.10      matt 	case LCOMPLEX:
    209  1.17    rillig 	case SIGNED:
    210  1.17    rillig 	case UNSIGN:
    211   1.8  christos 		(void)snprintf(buf, bufsiz, "%s%s", cv, s);
    212   1.1  christos 		break;
    213   1.1  christos 	case PTR:
    214   1.8  christos 		(void)snprintf(buf, bufsiz, "%s%s to %s", cv, s,
    215   1.1  christos 		    tyname(lbuf, sizeof(lbuf), tp->t_subt));
    216   1.1  christos 		break;
    217   1.1  christos 	case ENUM:
    218   1.8  christos 		(void)snprintf(buf, bufsiz, "%s%s %s", cv, s,
    219   1.1  christos #ifdef t_enum
    220   1.1  christos 		    tp->t_enum->etag->s_name
    221   1.1  christos #else
    222   1.7  christos 		    tp->t_isuniqpos ? "*anonymous*" : tp->t_tag->h_name
    223   1.1  christos #endif
    224   1.1  christos 		    );
    225   1.1  christos 		break;
    226   1.1  christos 	case STRUCT:
    227   1.1  christos 	case UNION:
    228   1.8  christos 		(void)snprintf(buf, bufsiz, "%s%s %s", cv, s,
    229   1.1  christos #ifdef t_str
    230   1.1  christos 		    tp->t_str->stag->s_name
    231   1.1  christos #else
    232   1.7  christos 		    tp->t_isuniqpos ? "*anonymous*" : tp->t_tag->h_name
    233   1.1  christos #endif
    234   1.1  christos 		    );
    235   1.1  christos 		break;
    236   1.1  christos 	case ARRAY:
    237   1.8  christos 		(void)snprintf(buf, bufsiz, "%s%s of %s[%d]", cv, s,
    238   1.1  christos 		    tyname(lbuf, sizeof(lbuf), tp->t_subt), tp->t_dim);
    239   1.1  christos 		break;
    240   1.1  christos 	default:
    241   1.8  christos 		LERROR("tyname(%d)", t);
    242   1.1  christos 	}
    243  1.14    rillig 	return buf;
    244   1.1  christos }
    245