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