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