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