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