xref: /netbsd-src/external/gpl3/binutils.old/dist/libctf/ctf-decl.c (revision c42dbd0ed2e61fe6eda8590caa852ccf34719964)
1867d70fcSchristos /* C declarator syntax glue.
2*c42dbd0eSchristos    Copyright (C) 2019-2022 Free Software Foundation, Inc.
3867d70fcSchristos 
4867d70fcSchristos    This file is part of libctf.
5867d70fcSchristos 
6867d70fcSchristos    libctf is free software; you can redistribute it and/or modify it under
7867d70fcSchristos    the terms of the GNU General Public License as published by the Free
8867d70fcSchristos    Software Foundation; either version 3, or (at your option) any later
9867d70fcSchristos    version.
10867d70fcSchristos 
11867d70fcSchristos    This program is distributed in the hope that it will be useful, but
12867d70fcSchristos    WITHOUT ANY WARRANTY; without even the implied warranty of
13867d70fcSchristos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14867d70fcSchristos    See the GNU General Public License for more details.
15867d70fcSchristos 
16867d70fcSchristos    You should have received a copy of the GNU General Public License
17867d70fcSchristos    along with this program; see the file COPYING.  If not see
18867d70fcSchristos    <http://www.gnu.org/licenses/>.  */
19867d70fcSchristos 
20867d70fcSchristos /* CTF Declaration Stack
21867d70fcSchristos 
22867d70fcSchristos    In order to implement ctf_type_name(), we must convert a type graph back
23867d70fcSchristos    into a C type declaration.  Unfortunately, a type graph represents a storage
24867d70fcSchristos    class ordering of the type whereas a type declaration must obey the C rules
25867d70fcSchristos    for operator precedence, and the two orderings are frequently in conflict.
26867d70fcSchristos    For example, consider these CTF type graphs and their C declarations:
27867d70fcSchristos 
28867d70fcSchristos    CTF_K_POINTER -> CTF_K_FUNCTION -> CTF_K_INTEGER  : int (*)()
29867d70fcSchristos    CTF_K_POINTER -> CTF_K_ARRAY -> CTF_K_INTEGER     : int (*)[]
30867d70fcSchristos 
31867d70fcSchristos    In each case, parentheses are used to raise operator * to higher lexical
32867d70fcSchristos    precedence, so the string form of the C declaration cannot be constructed by
33867d70fcSchristos    walking the type graph links and forming the string from left to right.
34867d70fcSchristos 
35867d70fcSchristos    The functions in this file build a set of stacks from the type graph nodes
36867d70fcSchristos    corresponding to the C operator precedence levels in the appropriate order.
37867d70fcSchristos    The code in ctf_type_name() can then iterate over the levels and nodes in
38867d70fcSchristos    lexical precedence order and construct the final C declaration string.  */
39867d70fcSchristos 
40867d70fcSchristos #include <ctf-impl.h>
41867d70fcSchristos #include <string.h>
42867d70fcSchristos 
43867d70fcSchristos void
ctf_decl_init(ctf_decl_t * cd)44867d70fcSchristos ctf_decl_init (ctf_decl_t *cd)
45867d70fcSchristos {
46867d70fcSchristos   int i;
47867d70fcSchristos 
48867d70fcSchristos   memset (cd, 0, sizeof (ctf_decl_t));
49867d70fcSchristos 
50867d70fcSchristos   for (i = CTF_PREC_BASE; i < CTF_PREC_MAX; i++)
51867d70fcSchristos     cd->cd_order[i] = CTF_PREC_BASE - 1;
52867d70fcSchristos 
53867d70fcSchristos   cd->cd_qualp = CTF_PREC_BASE;
54867d70fcSchristos   cd->cd_ordp = CTF_PREC_BASE;
55867d70fcSchristos }
56867d70fcSchristos 
57867d70fcSchristos void
ctf_decl_fini(ctf_decl_t * cd)58867d70fcSchristos ctf_decl_fini (ctf_decl_t *cd)
59867d70fcSchristos {
60867d70fcSchristos   ctf_decl_node_t *cdp, *ndp;
61867d70fcSchristos   int i;
62867d70fcSchristos 
63867d70fcSchristos   for (i = CTF_PREC_BASE; i < CTF_PREC_MAX; i++)
64867d70fcSchristos     {
65867d70fcSchristos       for (cdp = ctf_list_next (&cd->cd_nodes[i]); cdp != NULL; cdp = ndp)
66867d70fcSchristos 	{
67867d70fcSchristos 	  ndp = ctf_list_next (cdp);
68867d70fcSchristos 	  free (cdp);
69867d70fcSchristos 	}
70867d70fcSchristos     }
71*c42dbd0eSchristos   free (cd->cd_buf);
72867d70fcSchristos }
73867d70fcSchristos 
74867d70fcSchristos void
ctf_decl_push(ctf_decl_t * cd,ctf_dict_t * fp,ctf_id_t type)75*c42dbd0eSchristos ctf_decl_push (ctf_decl_t *cd, ctf_dict_t *fp, ctf_id_t type)
76867d70fcSchristos {
77867d70fcSchristos   ctf_decl_node_t *cdp;
78867d70fcSchristos   ctf_decl_prec_t prec;
79867d70fcSchristos   uint32_t kind, n = 1;
80867d70fcSchristos   int is_qual = 0;
81867d70fcSchristos 
82867d70fcSchristos   const ctf_type_t *tp;
83867d70fcSchristos   ctf_arinfo_t ar;
84867d70fcSchristos 
85867d70fcSchristos   if ((tp = ctf_lookup_by_id (&fp, type)) == NULL)
86867d70fcSchristos     {
87867d70fcSchristos       cd->cd_err = fp->ctf_errno;
88867d70fcSchristos       return;
89867d70fcSchristos     }
90867d70fcSchristos 
91867d70fcSchristos   switch (kind = LCTF_INFO_KIND (fp, tp->ctt_info))
92867d70fcSchristos     {
93867d70fcSchristos     case CTF_K_ARRAY:
94867d70fcSchristos       (void) ctf_array_info (fp, type, &ar);
95867d70fcSchristos       ctf_decl_push (cd, fp, ar.ctr_contents);
96867d70fcSchristos       n = ar.ctr_nelems;
97867d70fcSchristos       prec = CTF_PREC_ARRAY;
98867d70fcSchristos       break;
99867d70fcSchristos 
100867d70fcSchristos     case CTF_K_TYPEDEF:
101867d70fcSchristos       if (ctf_strptr (fp, tp->ctt_name)[0] == '\0')
102867d70fcSchristos 	{
103867d70fcSchristos 	  ctf_decl_push (cd, fp, tp->ctt_type);
104867d70fcSchristos 	  return;
105867d70fcSchristos 	}
106867d70fcSchristos       prec = CTF_PREC_BASE;
107867d70fcSchristos       break;
108867d70fcSchristos 
109867d70fcSchristos     case CTF_K_FUNCTION:
110867d70fcSchristos       ctf_decl_push (cd, fp, tp->ctt_type);
111867d70fcSchristos       prec = CTF_PREC_FUNCTION;
112867d70fcSchristos       break;
113867d70fcSchristos 
114867d70fcSchristos     case CTF_K_POINTER:
115867d70fcSchristos       ctf_decl_push (cd, fp, tp->ctt_type);
116867d70fcSchristos       prec = CTF_PREC_POINTER;
117867d70fcSchristos       break;
118867d70fcSchristos 
119867d70fcSchristos     case CTF_K_SLICE:
120*c42dbd0eSchristos       /* Slices themselves have no print representation and should not appear in
121*c42dbd0eSchristos 	 the decl stack.  */
122867d70fcSchristos       ctf_decl_push (cd, fp, ctf_type_reference (fp, type));
123*c42dbd0eSchristos       return;
124867d70fcSchristos 
125867d70fcSchristos     case CTF_K_VOLATILE:
126867d70fcSchristos     case CTF_K_CONST:
127867d70fcSchristos     case CTF_K_RESTRICT:
128867d70fcSchristos       ctf_decl_push (cd, fp, tp->ctt_type);
129867d70fcSchristos       prec = cd->cd_qualp;
130867d70fcSchristos       is_qual++;
131867d70fcSchristos       break;
132867d70fcSchristos 
133867d70fcSchristos     default:
134867d70fcSchristos       prec = CTF_PREC_BASE;
135867d70fcSchristos     }
136867d70fcSchristos 
137867d70fcSchristos   if ((cdp = malloc (sizeof (ctf_decl_node_t))) == NULL)
138867d70fcSchristos     {
139867d70fcSchristos       cd->cd_err = EAGAIN;
140867d70fcSchristos       return;
141867d70fcSchristos     }
142867d70fcSchristos 
143867d70fcSchristos   cdp->cd_type = type;
144867d70fcSchristos   cdp->cd_kind = kind;
145867d70fcSchristos   cdp->cd_n = n;
146867d70fcSchristos 
147867d70fcSchristos   if (ctf_list_next (&cd->cd_nodes[prec]) == NULL)
148867d70fcSchristos     cd->cd_order[prec] = cd->cd_ordp++;
149867d70fcSchristos 
150867d70fcSchristos   /* Reset cd_qualp to the highest precedence level that we've seen so
151867d70fcSchristos      far that can be qualified (CTF_PREC_BASE or CTF_PREC_POINTER).  */
152867d70fcSchristos 
153867d70fcSchristos   if (prec > cd->cd_qualp && prec < CTF_PREC_ARRAY)
154867d70fcSchristos     cd->cd_qualp = prec;
155867d70fcSchristos 
156*c42dbd0eSchristos   /* By convention qualifiers of base types precede the type specifier (e.g.
157867d70fcSchristos      const int vs. int const) even though the two forms are equivalent.  */
158867d70fcSchristos 
159*c42dbd0eSchristos   if (is_qual && prec == CTF_PREC_BASE)
160867d70fcSchristos     ctf_list_prepend (&cd->cd_nodes[prec], cdp);
161867d70fcSchristos   else
162867d70fcSchristos     ctf_list_append (&cd->cd_nodes[prec], cdp);
163867d70fcSchristos }
164867d70fcSchristos 
165867d70fcSchristos _libctf_printflike_ (2, 3)
ctf_decl_sprintf(ctf_decl_t * cd,const char * format,...)166867d70fcSchristos void ctf_decl_sprintf (ctf_decl_t *cd, const char *format, ...)
167867d70fcSchristos {
168867d70fcSchristos   va_list ap;
169867d70fcSchristos   char *str;
170867d70fcSchristos   int n;
171867d70fcSchristos 
172867d70fcSchristos   if (cd->cd_enomem)
173867d70fcSchristos     return;
174867d70fcSchristos 
175867d70fcSchristos   va_start (ap, format);
176867d70fcSchristos   n = vasprintf (&str, format, ap);
177867d70fcSchristos   va_end (ap);
178867d70fcSchristos 
179867d70fcSchristos   if (n > 0)
180867d70fcSchristos     {
181867d70fcSchristos       char *newbuf;
182867d70fcSchristos       if ((newbuf = ctf_str_append (cd->cd_buf, str)) != NULL)
183867d70fcSchristos 	cd->cd_buf = newbuf;
184867d70fcSchristos     }
185867d70fcSchristos 
186867d70fcSchristos   /* Sticky error condition.  */
187867d70fcSchristos   if (n < 0 || cd->cd_buf == NULL)
188867d70fcSchristos     {
189867d70fcSchristos       free (cd->cd_buf);
190867d70fcSchristos       cd->cd_buf = NULL;
191867d70fcSchristos       cd->cd_enomem = 1;
192867d70fcSchristos     }
193867d70fcSchristos 
194867d70fcSchristos   free (str);
195867d70fcSchristos }
196867d70fcSchristos 
ctf_decl_buf(ctf_decl_t * cd)197867d70fcSchristos char *ctf_decl_buf (ctf_decl_t *cd)
198867d70fcSchristos {
199*c42dbd0eSchristos   char *buf = cd->cd_buf;
200*c42dbd0eSchristos   cd->cd_buf = NULL;
201*c42dbd0eSchristos   return buf;
202867d70fcSchristos }
203