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