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