tyname.c revision 1.7 1 /* $NetBSD: tyname.c,v 1.7 2008/05/01 15:39:33 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.7 2008/05/01 15:39:33 christos Exp $");
39 #endif
40
41 #include <limits.h>
42 #include <stdlib.h>
43 #include <err.h>
44
45 #include PASS
46
47 #ifndef LERROR
48 #define LERROR(a) do { \
49 (void)warnx("%s, %d: %s", __FILE__, __LINE__, (a)); \
50 abort(); \
51 } while (/*CONSTCOND*/0)
52 #endif
53
54 const char *
55 basictyname(tspec_t t)
56 {
57 switch (t) {
58 case BOOL: return "_Bool";
59 case CHAR: return "char";
60 case UCHAR: return "unsigned char";
61 case SCHAR: return "signed char";
62 case SHORT: return "short";
63 case USHORT: return "unsigned short";
64 case INT: return "int";
65 case UINT: return "unsigned int";
66 case LONG: return "long";
67 case ULONG: return "unsigned long";
68 case QUAD: return "long long";
69 case UQUAD: return "unsigned long long";
70 case FLOAT: return "float";
71 case DOUBLE: return "double";
72 case LDOUBLE: return "long double";
73 case VOID: return "void";
74 case PTR: return "pointer";
75 case ENUM: return "enum";
76 case STRUCT: return "struct";
77 case UNION: return "union";
78 case FUNC: return "function";
79 case ARRAY: return "array";
80 case FCOMPLEX: return "float _Complex";
81 case DCOMPLEX: return "double _Complex";
82 case COMPLEX: return "_Complex";
83 default:
84 LERROR("basictyname()");
85 return NULL;
86 }
87 }
88
89 const char *
90 tyname(char *buf, size_t bufsiz, type_t *tp)
91 {
92 tspec_t t;
93 const char *s;
94 char lbuf[64];
95
96 if ((t = tp->t_tspec) == INT && tp->t_isenum)
97 t = ENUM;
98
99 s = basictyname(t);
100
101
102 switch (t) {
103 case BOOL:
104 case CHAR:
105 case UCHAR:
106 case SCHAR:
107 case SHORT:
108 case USHORT:
109 case INT:
110 case UINT:
111 case LONG:
112 case ULONG:
113 case QUAD:
114 case UQUAD:
115 case FLOAT:
116 case DOUBLE:
117 case LDOUBLE:
118 case VOID:
119 case FUNC:
120 case COMPLEX:
121 case FCOMPLEX:
122 case DCOMPLEX:
123 (void)snprintf(buf, bufsiz, "%s", s);
124 break;
125 case PTR:
126 (void)snprintf(buf, bufsiz, "%s to %s", s,
127 tyname(lbuf, sizeof(lbuf), tp->t_subt));
128 break;
129 case ENUM:
130 (void)snprintf(buf, bufsiz, "%s %s", s,
131 #ifdef t_enum
132 tp->t_enum->etag->s_name
133 #else
134 tp->t_isuniqpos ? "*anonymous*" : tp->t_tag->h_name
135 #endif
136 );
137 break;
138 case STRUCT:
139 case UNION:
140 (void)snprintf(buf, bufsiz, "%s %s", s,
141 #ifdef t_str
142 tp->t_str->stag->s_name
143 #else
144 tp->t_isuniqpos ? "*anonymous*" : tp->t_tag->h_name
145 #endif
146 );
147 break;
148 case ARRAY:
149 (void)snprintf(buf, bufsiz, "%s of %s[%d]", s,
150 tyname(lbuf, sizeof(lbuf), tp->t_subt), tp->t_dim);
151 break;
152 default:
153 LERROR("tyname()");
154 }
155 return (buf);
156 }
157