Home | History | Annotate | Line # | Download | only in common
tyname.c revision 1.1
      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.1 2005/04/07 16:28:40 christos 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 
     60 const char *
     61 basictyname(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 PTR:	return "pointer";
     80 	case ENUM:	return "enum";
     81 	case STRUCT:	return "struct";
     82 	case UNION:	return "union";
     83 	case FUNC:	return "function";
     84 	case ARRAY:	return "array";
     85 	default:
     86 		LERROR("basictyname()");
     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 
     98 	if ((t = tp->t_tspec) == INT && tp->t_isenum)
     99 		t = ENUM;
    100 
    101 	s = basictyname(t);
    102 
    103 
    104 	switch (t) {
    105 	case BOOL:
    106 	case CHAR:
    107 	case UCHAR:
    108 	case SCHAR:
    109 	case SHORT:
    110 	case USHORT:
    111 	case INT:
    112 	case UINT:
    113 	case LONG:
    114 	case ULONG:
    115 	case QUAD:
    116 	case UQUAD:
    117 	case FLOAT:
    118 	case DOUBLE:
    119 	case LDOUBLE:
    120 	case FUNC:
    121 		(void)snprintf(buf, bufsiz, "%s", s);
    122 		break;
    123 	case PTR:
    124 		(void)snprintf(buf, bufsiz, "%s to %s", s,
    125 		    tyname(lbuf, sizeof(lbuf), tp->t_subt));
    126 		break;
    127 	case ENUM:
    128 		(void)snprintf(buf, bufsiz, "%s %s", s,
    129 #ifdef t_enum
    130 		    tp->t_enum->etag->s_name
    131 #else
    132 		    tp->t_tag->h_name
    133 #endif
    134 		    );
    135 		break;
    136 	case STRUCT:
    137 	case UNION:
    138 		(void)snprintf(buf, bufsiz, "%s %s", s,
    139 #ifdef t_str
    140 		    tp->t_str->stag->s_name
    141 #else
    142 		    tp->t_tag->h_name
    143 #endif
    144 		    );
    145 		break;
    146 	case ARRAY:
    147 		(void)snprintf(buf, bufsiz, "%s of %s[%d]", s,
    148 		    tyname(lbuf, sizeof(lbuf), tp->t_subt), tp->t_dim);
    149 		break;
    150 	default:
    151 		LERROR("tyname()");
    152 	}
    153 	return (buf);
    154 }
    155