Path: blob/main/cddl/contrib/opensolaris/common/ctf/ctf_decl.c
39507 views
/*1* CDDL HEADER START2*3* The contents of this file are subject to the terms of the4* Common Development and Distribution License, Version 1.0 only5* (the "License"). You may not use this file except in compliance6* with the License.7*8* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE9* or http://www.opensolaris.org/os/licensing.10* See the License for the specific language governing permissions11* and limitations under the License.12*13* When distributing Covered Code, include this CDDL HEADER in each14* file and include the License file at usr/src/OPENSOLARIS.LICENSE.15* If applicable, add the following below this CDDL HEADER, with the16* fields enclosed by brackets "[]" replaced with your own identifying17* information: Portions Copyright [yyyy] [name of copyright owner]18*19* CDDL HEADER END20*/21/*22* Copyright 2005 Sun Microsystems, Inc. All rights reserved.23* Use is subject to license terms.24*/2526#pragma ident "%Z%%M% %I% %E% SMI"2728/*29* CTF Declaration Stack30*31* In order to implement ctf_type_name(), we must convert a type graph back32* into a C type declaration. Unfortunately, a type graph represents a storage33* class ordering of the type whereas a type declaration must obey the C rules34* for operator precedence, and the two orderings are frequently in conflict.35* For example, consider these CTF type graphs and their C declarations:36*37* CTF_K_POINTER -> CTF_K_FUNCTION -> CTF_K_INTEGER : int (*)()38* CTF_K_POINTER -> CTF_K_ARRAY -> CTF_K_INTEGER : int (*)[]39*40* In each case, parentheses are used to raise operator * to higher lexical41* precedence, so the string form of the C declaration cannot be constructed by42* walking the type graph links and forming the string from left to right.43*44* The functions in this file build a set of stacks from the type graph nodes45* corresponding to the C operator precedence levels in the appropriate order.46* The code in ctf_type_name() can then iterate over the levels and nodes in47* lexical precedence order and construct the final C declaration string.48*/4950#include <ctf_impl.h>5152void53ctf_decl_init(ctf_decl_t *cd, char *buf, size_t len)54{55int i;5657bzero(cd, sizeof (ctf_decl_t));5859for (i = CTF_PREC_BASE; i < CTF_PREC_MAX; i++)60cd->cd_order[i] = CTF_PREC_BASE - 1;6162cd->cd_qualp = CTF_PREC_BASE;63cd->cd_ordp = CTF_PREC_BASE;6465cd->cd_buf = buf;66cd->cd_ptr = buf;67cd->cd_end = buf + len;68}6970void71ctf_decl_fini(ctf_decl_t *cd)72{73ctf_decl_node_t *cdp, *ndp;74int i;7576for (i = CTF_PREC_BASE; i < CTF_PREC_MAX; i++) {77for (cdp = ctf_list_next(&cd->cd_nodes[i]);78cdp != NULL; cdp = ndp) {79ndp = ctf_list_next(cdp);80ctf_free(cdp, sizeof (ctf_decl_node_t));81}82}83}8485void86ctf_decl_push(ctf_decl_t *cd, ctf_file_t *fp, ctf_id_t type)87{88ctf_decl_node_t *cdp;89ctf_decl_prec_t prec;90uint_t ctype, kind, n = 1;91int is_qual = 0;9293const void *tp;94ctf_arinfo_t ar;9596if ((tp = ctf_lookup_by_id(&fp, type)) == NULL) {97cd->cd_err = fp->ctf_errno;98return;99}100101ctf_get_ctt_info(fp, tp, &kind, NULL, NULL);102ctf_get_ctt_index(fp, tp, NULL, &ctype, NULL);103104switch (kind) {105case CTF_K_ARRAY:106(void) ctf_array_info(fp, type, &ar);107ctf_decl_push(cd, fp, ar.ctr_contents);108n = ar.ctr_nelems;109prec = CTF_PREC_ARRAY;110break;111112case CTF_K_TYPEDEF:113if (ctf_type_rname(fp, tp)[0] == '\0') {114ctf_decl_push(cd, fp, ctype);115return;116}117prec = CTF_PREC_BASE;118break;119120case CTF_K_FUNCTION:121ctf_decl_push(cd, fp, ctype);122prec = CTF_PREC_FUNCTION;123break;124125case CTF_K_POINTER:126ctf_decl_push(cd, fp, ctype);127prec = CTF_PREC_POINTER;128break;129130case CTF_K_VOLATILE:131case CTF_K_CONST:132case CTF_K_RESTRICT:133ctf_decl_push(cd, fp, ctype);134prec = cd->cd_qualp;135is_qual++;136break;137138default:139prec = CTF_PREC_BASE;140}141142if ((cdp = ctf_alloc(sizeof (ctf_decl_node_t))) == NULL) {143cd->cd_err = EAGAIN;144return;145}146147cdp->cd_type = type;148cdp->cd_kind = kind;149cdp->cd_n = n;150151if (ctf_list_next(&cd->cd_nodes[prec]) == NULL)152cd->cd_order[prec] = cd->cd_ordp++;153154/*155* Reset cd_qualp to the highest precedence level that we've seen so156* far that can be qualified (CTF_PREC_BASE or CTF_PREC_POINTER).157*/158if (prec > cd->cd_qualp && prec < CTF_PREC_ARRAY)159cd->cd_qualp = prec;160161/*162* C array declarators are ordered inside out so prepend them. Also by163* convention qualifiers of base types precede the type specifier (e.g.164* const int vs. int const) even though the two forms are equivalent.165*/166if (kind == CTF_K_ARRAY || (is_qual && prec == CTF_PREC_BASE))167ctf_list_prepend(&cd->cd_nodes[prec], cdp);168else169ctf_list_append(&cd->cd_nodes[prec], cdp);170}171172/*PRINTFLIKE2*/173void174ctf_decl_sprintf(ctf_decl_t *cd, const char *format, ...)175{176size_t len = (size_t)(cd->cd_end - cd->cd_ptr);177va_list ap;178size_t n;179180va_start(ap, format);181n = vsnprintf(cd->cd_ptr, len, format, ap);182va_end(ap);183184cd->cd_ptr += MIN(n, len);185cd->cd_len += n;186}187188189