tyname.c revision 1.37
1/* $NetBSD: tyname.c,v 1.37 2021/04/02 12:16:50 rillig 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.37 2021/04/02 12:16:50 rillig 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 INTERNAL_ERROR 49#define INTERNAL_ERROR(fmt, args...) \ 50 do { \ 51 (void)warnx("%s, %d: " fmt, __FILE__, __LINE__, ##args); \ 52 abort(); \ 53 } while (/*CONSTCOND*/false) 54#endif 55 56/* A tree of strings. */ 57typedef struct name_tree_node { 58 char *ntn_name; 59 struct name_tree_node *ntn_less; 60 struct name_tree_node *ntn_greater; 61} name_tree_node; 62 63/* A growable string buffer. */ 64typedef struct buffer { 65 size_t len; 66 size_t cap; 67 char * data; 68} buffer; 69 70static name_tree_node *type_names; 71 72static name_tree_node * 73new_name_tree_node(const char *name) 74{ 75 name_tree_node *n; 76 77 n = xmalloc(sizeof(*n)); 78 n->ntn_name = xstrdup(name); 79 n->ntn_less = NULL; 80 n->ntn_greater = NULL; 81 return n; 82} 83 84/* Return the canonical instance of the string, with unlimited life time. */ 85static const char * 86intern(const char *name) 87{ 88 name_tree_node *n = type_names, **next; 89 int cmp; 90 91 if (n == NULL) { 92 n = new_name_tree_node(name); 93 type_names = n; 94 return n->ntn_name; 95 } 96 97 while ((cmp = strcmp(name, n->ntn_name)) != 0) { 98 next = cmp < 0 ? &n->ntn_less : &n->ntn_greater; 99 if (*next == NULL) { 100 *next = new_name_tree_node(name); 101 return (*next)->ntn_name; 102 } 103 n = *next; 104 } 105 return n->ntn_name; 106} 107 108static void 109buf_init(buffer *buf) 110{ 111 buf->len = 0; 112 buf->cap = 128; 113 buf->data = xmalloc(buf->cap); 114 buf->data[0] = '\0'; 115} 116 117static void 118buf_done(buffer *buf) 119{ 120 free(buf->data); 121} 122 123static void 124buf_add(buffer *buf, const char *s) 125{ 126 size_t len = strlen(s); 127 128 while (buf->len + len + 1 >= buf->cap) { 129 buf->data = xrealloc(buf->data, 2 * buf->cap); 130 buf->cap = 2 * buf->cap; 131 } 132 133 memcpy(buf->data + buf->len, s, len + 1); 134 buf->len += len; 135} 136 137static void 138buf_add_int(buffer *buf, int n) 139{ 140 char num[1 + sizeof(n) * CHAR_BIT + 1]; 141 142 snprintf(num, sizeof(num), "%d", n); 143 buf_add(buf, num); 144} 145 146const char * 147tspec_name(tspec_t t) 148{ 149 switch (t) { 150 case SIGNED: return "signed"; 151 case UNSIGN: return "unsigned"; 152 case BOOL: return "_Bool"; 153 case CHAR: return "char"; 154 case SCHAR: return "signed char"; 155 case UCHAR: return "unsigned char"; 156 case SHORT: return "short"; 157 case USHORT: return "unsigned short"; 158 case INT: return "int"; 159 case UINT: return "unsigned int"; 160 case LONG: return "long"; 161 case ULONG: return "unsigned long"; 162 case QUAD: return "long long"; 163 case UQUAD: return "unsigned long long"; 164#ifdef INT128_SIZE 165 case INT128: return "__int128_t"; 166 case UINT128: return "__uint128_t"; 167#endif 168 case FLOAT: return "float"; 169 case DOUBLE: return "double"; 170 case LDOUBLE: return "long double"; 171 case VOID: return "void"; 172 case STRUCT: return "struct"; 173 case UNION: return "union"; 174 case ENUM: return "enum"; 175 case PTR: return "pointer"; 176 case ARRAY: return "array"; 177 case FUNC: return "function"; 178 case COMPLEX: return "_Complex"; 179 case FCOMPLEX: return "float _Complex"; 180 case DCOMPLEX: return "double _Complex"; 181 case LCOMPLEX: return "long double _Complex"; 182 default: 183 INTERNAL_ERROR("tspec_name(%d)", t); 184 return NULL; 185 } 186} 187 188bool 189sametype(const type_t *t1, const type_t *t2) 190{ 191 tspec_t t; 192 193 if (t1->t_tspec != t2->t_tspec) 194 return false; 195 196 /* Ignore const/void */ 197 198 switch (t = t1->t_tspec) { 199 case BOOL: 200 case CHAR: 201 case UCHAR: 202 case SCHAR: 203 case SHORT: 204 case USHORT: 205 case INT: 206 case UINT: 207 case LONG: 208 case ULONG: 209 case QUAD: 210 case UQUAD: 211#ifdef INT128_SIZE 212 case INT128: 213 case UINT128: 214#endif 215 case FLOAT: 216 case DOUBLE: 217 case LDOUBLE: 218 case VOID: 219 case FUNC: 220 case COMPLEX: 221 case FCOMPLEX: 222 case DCOMPLEX: 223 case LCOMPLEX: 224 return true; 225 case ARRAY: 226 if (t1->t_dim != t2->t_dim) 227 return false; 228 /*FALLTHROUGH*/ 229 case PTR: 230 return sametype(t1->t_subt, t2->t_subt); 231 case ENUM: 232#ifdef t_enum 233 return strcmp(t1->t_enum->en_tag->s_name, 234 t2->t_enum->en_tag->s_name) == 0; 235#else 236 return true; 237#endif 238 case STRUCT: 239 case UNION: 240#ifdef t_str 241 return strcmp(t1->t_str->sou_tag->s_name, 242 t2->t_str->sou_tag->s_name) == 0; 243#else 244 return true; 245#endif 246 default: 247 INTERNAL_ERROR("tyname(%d)", t); 248 return false; 249 } 250} 251 252static void 253type_name_of_function(buffer *buf, const type_t *tp) 254{ 255 const char *sep = ""; 256 257 buf_add(buf, "("); 258 if (tp->t_proto) { 259#ifdef t_enum /* lint1 */ 260 sym_t *arg; 261 262 for (arg = tp->t_args; arg != NULL; arg = arg->s_next) { 263 buf_add(buf, sep), sep = ", "; 264 buf_add(buf, type_name(arg->s_type)); 265 } 266#else /* lint2 */ 267 type_t **argtype; 268 269 for (argtype = tp->t_args; *argtype != NULL; argtype++) { 270 buf_add(buf, sep), sep = ", "; 271 buf_add(buf, type_name(*argtype)); 272 } 273#endif 274 } 275 if (tp->t_vararg) { 276 buf_add(buf, sep); 277 buf_add(buf, "..."); 278 } 279 buf_add(buf, ") returning "); 280 buf_add(buf, type_name(tp->t_subt)); 281} 282 283static void 284type_name_of_struct_or_union(buffer *buf, const type_t *tp) 285{ 286 buf_add(buf, " "); 287#ifdef t_str 288 if (tp->t_str->sou_tag->s_name == unnamed && 289 tp->t_str->sou_first_typedef != NULL) { 290 buf_add(buf, "typedef "); 291 buf_add(buf, tp->t_str->sou_first_typedef->s_name); 292 } else { 293 buf_add(buf, tp->t_str->sou_tag->s_name); 294 } 295#else 296 buf_add(buf, tp->t_isuniqpos ? "*anonymous*" : tp->t_tag->h_name); 297#endif 298} 299 300static void 301type_name_of_enum(buffer *buf, const type_t *tp) 302{ 303 buf_add(buf, " "); 304#ifdef t_enum 305 if (tp->t_enum->en_tag->s_name == unnamed && 306 tp->t_enum->en_first_typedef != NULL) { 307 buf_add(buf, "typedef "); 308 buf_add(buf, tp->t_enum->en_first_typedef->s_name); 309 } else { 310 buf_add(buf, tp->t_enum->en_tag->s_name); 311 } 312#else 313 buf_add(buf, tp->t_isuniqpos ? "*anonymous*" : tp->t_tag->h_name); 314#endif 315} 316 317static void 318type_name_of_array(buffer *buf, const type_t *tp) 319{ 320 buf_add(buf, "["); 321#ifdef t_str /* lint1 */ 322 if (tp->t_incomplete_array) 323 buf_add(buf, "unknown_size"); 324 else 325 buf_add_int(buf, tp->t_dim); 326#else 327 buf_add_int(buf, tp->t_dim); 328#endif 329 buf_add(buf, "]"); 330 buf_add(buf, " of "); 331 buf_add(buf, type_name(tp->t_subt)); 332} 333 334const char * 335type_name(const type_t *tp) 336{ 337 tspec_t t; 338 buffer buf; 339 const char *name; 340 341 if (tp == NULL) 342 return "(null)"; 343 344 /* 345 * XXX: Why is this necessary, and in which cases does this apply? 346 * Shouldn't the type be an ENUM from the beginning? 347 */ 348 if ((t = tp->t_tspec) == INT && tp->t_is_enum) 349 t = ENUM; 350 351 buf_init(&buf); 352 if (tp->t_const) 353 buf_add(&buf, "const "); 354 if (tp->t_volatile) 355 buf_add(&buf, "volatile "); 356 357 buf_add(&buf, tspec_name(t)); 358 359 switch (t) { 360 case BOOL: 361 case CHAR: 362 case UCHAR: 363 case SCHAR: 364 case SHORT: 365 case USHORT: 366 case INT: 367 case UINT: 368 case LONG: 369 case ULONG: 370 case QUAD: 371 case UQUAD: 372#ifdef INT128_SIZE 373 case INT128: 374 case UINT128: 375#endif 376 case FLOAT: 377 case DOUBLE: 378 case LDOUBLE: 379 case VOID: 380 case COMPLEX: 381 case FCOMPLEX: 382 case DCOMPLEX: 383 case LCOMPLEX: 384 case SIGNED: 385 case UNSIGN: 386 break; 387 case PTR: 388 buf_add(&buf, " to "); 389 buf_add(&buf, type_name(tp->t_subt)); 390 break; 391 case ENUM: 392 type_name_of_enum(&buf, tp); 393 break; 394 case STRUCT: 395 case UNION: 396 type_name_of_struct_or_union(&buf, tp); 397 break; 398 case ARRAY: 399 type_name_of_array(&buf, tp); 400 break; 401 case FUNC: 402 type_name_of_function(&buf, tp); 403 break; 404 default: 405 INTERNAL_ERROR("type_name(%d)", t); 406 } 407 408 name = intern(buf.data); 409 buf_done(&buf); 410 return name; 411} 412