1 /* $NetBSD: tyname.c,v 1.11 2012/06/20 18:50: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.11 2012/06/20 18:50: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 55 const char * 56 basictyname(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 91 const char * 92 tyname(char *buf, size_t bufsiz, type_t *tp) 93 { 94 tspec_t t; 95 const char *s; 96 char lbuf[64]; 97 char cv[20]; 98 99 if (tp == NULL) 100 return "(null)"; 101 if ((t = tp->t_tspec) == INT && tp->t_isenum) 102 t = ENUM; 103 104 s = basictyname(t); 105 106 cv[0] = '\0'; 107 if (tp->t_const) 108 (void)strcat(cv, "const "); 109 if (tp->t_volatile) 110 (void)strcat(cv, "volatile "); 111 112 switch (t) { 113 case BOOL: 114 case CHAR: 115 case UCHAR: 116 case SCHAR: 117 case SHORT: 118 case USHORT: 119 case INT: 120 case UINT: 121 case LONG: 122 case ULONG: 123 case QUAD: 124 case UQUAD: 125 case FLOAT: 126 case DOUBLE: 127 case LDOUBLE: 128 case VOID: 129 case FUNC: 130 case COMPLEX: 131 case FCOMPLEX: 132 case DCOMPLEX: 133 case LCOMPLEX: 134 (void)snprintf(buf, bufsiz, "%s%s", cv, s); 135 break; 136 case PTR: 137 (void)snprintf(buf, bufsiz, "%s%s to %s", cv, s, 138 tyname(lbuf, sizeof(lbuf), tp->t_subt)); 139 break; 140 case ENUM: 141 (void)snprintf(buf, bufsiz, "%s%s %s", cv, s, 142 #ifdef t_enum 143 tp->t_enum->etag->s_name 144 #else 145 tp->t_isuniqpos ? "*anonymous*" : tp->t_tag->h_name 146 #endif 147 ); 148 break; 149 case STRUCT: 150 case UNION: 151 (void)snprintf(buf, bufsiz, "%s%s %s", cv, s, 152 #ifdef t_str 153 tp->t_str->stag->s_name 154 #else 155 tp->t_isuniqpos ? "*anonymous*" : tp->t_tag->h_name 156 #endif 157 ); 158 break; 159 case ARRAY: 160 (void)snprintf(buf, bufsiz, "%s%s of %s[%d]", cv, s, 161 tyname(lbuf, sizeof(lbuf), tp->t_subt), tp->t_dim); 162 break; 163 default: 164 LERROR("tyname(%d)", t); 165 } 166 return (buf); 167 } 168