tyname.c revision 1.4 1 /* $NetBSD: tyname.c,v 1.4 2007/02/07 14:20:58 hubertf 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 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 #if HAVE_NBTOOL_CONFIG_H
40 #include "nbtool_config.h"
41 #endif
42
43 #include <sys/cdefs.h>
44 #if defined(__RCSID) && !defined(lint)
45 __RCSID("$NetBSD: tyname.c,v 1.4 2007/02/07 14:20:58 hubertf Exp $");
46 #endif
47
48 #include <limits.h>
49 #include <stdlib.h>
50 #include <err.h>
51
52 #include PASS
53
54 #ifndef LERROR
55 #define LERROR(a) do { \
56 (void)warnx("%s, %d: %s", __FILE__, __LINE__, (a)); \
57 abort(); \
58 } while (/*CONSTCOND*/0)
59 #endif
60
61 const char *
62 basictyname(tspec_t t)
63 {
64 switch (t) {
65 case BOOL: return "_Bool";
66 case CHAR: return "char";
67 case UCHAR: return "unsigned char";
68 case SCHAR: return "signed char";
69 case SHORT: return "short";
70 case USHORT: return "unsigned short";
71 case INT: return "int";
72 case UINT: return "unsigned int";
73 case LONG: return "long";
74 case ULONG: return "unsigned long";
75 case QUAD: return "long long";
76 case UQUAD: return "unsigned long long";
77 case FLOAT: return "float";
78 case DOUBLE: return "double";
79 case LDOUBLE: return "long double";
80 case VOID: return "void";
81 case PTR: return "pointer";
82 case ENUM: return "enum";
83 case STRUCT: return "struct";
84 case UNION: return "union";
85 case FUNC: return "function";
86 case ARRAY: return "array";
87 default:
88 LERROR("basictyname()");
89 return NULL;
90 }
91 }
92
93 const char *
94 tyname(char *buf, size_t bufsiz, type_t *tp)
95 {
96 tspec_t t;
97 const char *s;
98 char lbuf[64];
99
100 if ((t = tp->t_tspec) == INT && tp->t_isenum)
101 t = ENUM;
102
103 s = basictyname(t);
104
105
106 switch (t) {
107 case BOOL:
108 case CHAR:
109 case UCHAR:
110 case SCHAR:
111 case SHORT:
112 case USHORT:
113 case INT:
114 case UINT:
115 case LONG:
116 case ULONG:
117 case QUAD:
118 case UQUAD:
119 case FLOAT:
120 case DOUBLE:
121 case LDOUBLE:
122 case VOID:
123 case FUNC:
124 (void)snprintf(buf, bufsiz, "%s", s);
125 break;
126 case PTR:
127 (void)snprintf(buf, bufsiz, "%s to %s", s,
128 tyname(lbuf, sizeof(lbuf), tp->t_subt));
129 break;
130 case ENUM:
131 (void)snprintf(buf, bufsiz, "%s %s", s,
132 #ifdef t_enum
133 tp->t_enum->etag->s_name
134 #else
135 tp->t_tag->h_name
136 #endif
137 );
138 break;
139 case STRUCT:
140 case UNION:
141 (void)snprintf(buf, bufsiz, "%s %s", s,
142 #ifdef t_str
143 tp->t_str->stag->s_name
144 #else
145 tp->t_tag->h_name
146 #endif
147 );
148 break;
149 case ARRAY:
150 (void)snprintf(buf, bufsiz, "%s of %s[%d]", s,
151 tyname(lbuf, sizeof(lbuf), tp->t_subt), tp->t_dim);
152 break;
153 default:
154 LERROR("tyname()");
155 }
156 return (buf);
157 }
158