Home | History | Annotate | Line # | Download | only in common
tyname.c revision 1.7
      1  1.7  christos /*	$NetBSD: tyname.c,v 1.7 2008/05/01 15:39:33 christos 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  *
     19  1.1  christos  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  1.1  christos  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  1.1  christos  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  1.1  christos  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  1.1  christos  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  1.1  christos  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  1.1  christos  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  1.1  christos  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  1.1  christos  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  1.1  christos  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  1.1  christos  * POSSIBILITY OF SUCH DAMAGE.
     30  1.1  christos  */
     31  1.1  christos 
     32  1.1  christos #if HAVE_NBTOOL_CONFIG_H
     33  1.1  christos #include "nbtool_config.h"
     34  1.1  christos #endif
     35  1.1  christos 
     36  1.1  christos #include <sys/cdefs.h>
     37  1.1  christos #if defined(__RCSID) && !defined(lint)
     38  1.7  christos __RCSID("$NetBSD: tyname.c,v 1.7 2008/05/01 15:39:33 christos Exp $");
     39  1.1  christos #endif
     40  1.1  christos 
     41  1.1  christos #include <limits.h>
     42  1.1  christos #include <stdlib.h>
     43  1.1  christos #include <err.h>
     44  1.1  christos 
     45  1.1  christos #include PASS
     46  1.1  christos 
     47  1.1  christos #ifndef LERROR
     48  1.1  christos #define LERROR(a) 	do { \
     49  1.1  christos     (void)warnx("%s, %d: %s", __FILE__, __LINE__, (a)); \
     50  1.1  christos     abort(); \
     51  1.1  christos } while (/*CONSTCOND*/0)
     52  1.1  christos #endif
     53  1.1  christos 
     54  1.1  christos const char *
     55  1.1  christos basictyname(tspec_t t)
     56  1.1  christos {
     57  1.1  christos 	switch (t) {
     58  1.1  christos 	case BOOL:	return "_Bool";
     59  1.1  christos 	case CHAR:	return "char";
     60  1.1  christos 	case UCHAR:	return "unsigned char";
     61  1.1  christos 	case SCHAR:	return "signed char";
     62  1.1  christos 	case SHORT:	return "short";
     63  1.1  christos 	case USHORT:	return "unsigned short";
     64  1.1  christos 	case INT:	return "int";
     65  1.1  christos 	case UINT:	return "unsigned int";
     66  1.1  christos 	case LONG:	return "long";
     67  1.1  christos 	case ULONG:	return "unsigned long";
     68  1.1  christos 	case QUAD:	return "long long";
     69  1.1  christos 	case UQUAD:	return "unsigned long long";
     70  1.1  christos 	case FLOAT:	return "float";
     71  1.1  christos 	case DOUBLE:	return "double";
     72  1.1  christos 	case LDOUBLE:	return "long double";
     73  1.2       skd 	case VOID:	return "void";
     74  1.1  christos 	case PTR:	return "pointer";
     75  1.1  christos 	case ENUM:	return "enum";
     76  1.1  christos 	case STRUCT:	return "struct";
     77  1.1  christos 	case UNION:	return "union";
     78  1.1  christos 	case FUNC:	return "function";
     79  1.1  christos 	case ARRAY:	return "array";
     80  1.5  christos 	case FCOMPLEX:	return "float _Complex";
     81  1.5  christos 	case DCOMPLEX:	return "double _Complex";
     82  1.5  christos 	case COMPLEX:	return "_Complex";
     83  1.1  christos 	default:
     84  1.1  christos 		LERROR("basictyname()");
     85  1.1  christos 		return NULL;
     86  1.1  christos 	}
     87  1.1  christos }
     88  1.1  christos 
     89  1.1  christos const char *
     90  1.1  christos tyname(char *buf, size_t bufsiz, type_t *tp)
     91  1.1  christos {
     92  1.1  christos 	tspec_t	t;
     93  1.1  christos 	const	char *s;
     94  1.1  christos 	char lbuf[64];
     95  1.1  christos 
     96  1.1  christos 	if ((t = tp->t_tspec) == INT && tp->t_isenum)
     97  1.1  christos 		t = ENUM;
     98  1.1  christos 
     99  1.1  christos 	s = basictyname(t);
    100  1.1  christos 
    101  1.1  christos 
    102  1.1  christos 	switch (t) {
    103  1.1  christos 	case BOOL:
    104  1.1  christos 	case CHAR:
    105  1.1  christos 	case UCHAR:
    106  1.1  christos 	case SCHAR:
    107  1.1  christos 	case SHORT:
    108  1.1  christos 	case USHORT:
    109  1.1  christos 	case INT:
    110  1.1  christos 	case UINT:
    111  1.1  christos 	case LONG:
    112  1.1  christos 	case ULONG:
    113  1.1  christos 	case QUAD:
    114  1.1  christos 	case UQUAD:
    115  1.1  christos 	case FLOAT:
    116  1.1  christos 	case DOUBLE:
    117  1.1  christos 	case LDOUBLE:
    118  1.2       skd 	case VOID:
    119  1.1  christos 	case FUNC:
    120  1.5  christos 	case COMPLEX:
    121  1.5  christos 	case FCOMPLEX:
    122  1.5  christos 	case DCOMPLEX:
    123  1.1  christos 		(void)snprintf(buf, bufsiz, "%s", s);
    124  1.1  christos 		break;
    125  1.1  christos 	case PTR:
    126  1.1  christos 		(void)snprintf(buf, bufsiz, "%s to %s", s,
    127  1.1  christos 		    tyname(lbuf, sizeof(lbuf), tp->t_subt));
    128  1.1  christos 		break;
    129  1.1  christos 	case ENUM:
    130  1.1  christos 		(void)snprintf(buf, bufsiz, "%s %s", s,
    131  1.1  christos #ifdef t_enum
    132  1.1  christos 		    tp->t_enum->etag->s_name
    133  1.1  christos #else
    134  1.7  christos 		    tp->t_isuniqpos ? "*anonymous*" : tp->t_tag->h_name
    135  1.1  christos #endif
    136  1.1  christos 		    );
    137  1.1  christos 		break;
    138  1.1  christos 	case STRUCT:
    139  1.1  christos 	case UNION:
    140  1.1  christos 		(void)snprintf(buf, bufsiz, "%s %s", s,
    141  1.1  christos #ifdef t_str
    142  1.1  christos 		    tp->t_str->stag->s_name
    143  1.1  christos #else
    144  1.7  christos 		    tp->t_isuniqpos ? "*anonymous*" : tp->t_tag->h_name
    145  1.1  christos #endif
    146  1.1  christos 		    );
    147  1.1  christos 		break;
    148  1.1  christos 	case ARRAY:
    149  1.1  christos 		(void)snprintf(buf, bufsiz, "%s of %s[%d]", s,
    150  1.1  christos 		    tyname(lbuf, sizeof(lbuf), tp->t_subt), tp->t_dim);
    151  1.1  christos 		break;
    152  1.1  christos 	default:
    153  1.1  christos 		LERROR("tyname()");
    154  1.1  christos 	}
    155  1.1  christos 	return (buf);
    156  1.1  christos }
    157