1*e4b17023SJohn Marino /* Generate information regarding function declarations and definitions based
2*e4b17023SJohn Marino on information stored in GCC's tree structure. This code implements the
3*e4b17023SJohn Marino -aux-info option.
4*e4b17023SJohn Marino Copyright (C) 1989, 1991, 1994, 1995, 1997, 1998,
5*e4b17023SJohn Marino 1999, 2000, 2003, 2004, 2007, 2010 Free Software Foundation, Inc.
6*e4b17023SJohn Marino Contributed by Ron Guilmette (rfg@segfault.us.com).
7*e4b17023SJohn Marino
8*e4b17023SJohn Marino This file is part of GCC.
9*e4b17023SJohn Marino
10*e4b17023SJohn Marino GCC is free software; you can redistribute it and/or modify it under
11*e4b17023SJohn Marino the terms of the GNU General Public License as published by the Free
12*e4b17023SJohn Marino Software Foundation; either version 3, or (at your option) any later
13*e4b17023SJohn Marino version.
14*e4b17023SJohn Marino
15*e4b17023SJohn Marino GCC is distributed in the hope that it will be useful, but WITHOUT ANY
16*e4b17023SJohn Marino WARRANTY; without even the implied warranty of MERCHANTABILITY or
17*e4b17023SJohn Marino FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18*e4b17023SJohn Marino for more details.
19*e4b17023SJohn Marino
20*e4b17023SJohn Marino You should have received a copy of the GNU General Public License
21*e4b17023SJohn Marino along with GCC; see the file COPYING3. If not see
22*e4b17023SJohn Marino <http://www.gnu.org/licenses/>. */
23*e4b17023SJohn Marino
24*e4b17023SJohn Marino #include "config.h"
25*e4b17023SJohn Marino #include "system.h"
26*e4b17023SJohn Marino #include "coretypes.h"
27*e4b17023SJohn Marino #include "tm.h"
28*e4b17023SJohn Marino #include "flags.h"
29*e4b17023SJohn Marino #include "tree.h"
30*e4b17023SJohn Marino #include "c-tree.h"
31*e4b17023SJohn Marino
32*e4b17023SJohn Marino enum formals_style_enum {
33*e4b17023SJohn Marino ansi,
34*e4b17023SJohn Marino k_and_r_names,
35*e4b17023SJohn Marino k_and_r_decls
36*e4b17023SJohn Marino };
37*e4b17023SJohn Marino typedef enum formals_style_enum formals_style;
38*e4b17023SJohn Marino
39*e4b17023SJohn Marino
40*e4b17023SJohn Marino static const char *data_type;
41*e4b17023SJohn Marino
42*e4b17023SJohn Marino static char *affix_data_type (const char *) ATTRIBUTE_MALLOC;
43*e4b17023SJohn Marino static const char *gen_formal_list_for_type (tree, formals_style);
44*e4b17023SJohn Marino static const char *gen_formal_list_for_func_def (tree, formals_style);
45*e4b17023SJohn Marino static const char *gen_type (const char *, tree, formals_style);
46*e4b17023SJohn Marino static const char *gen_decl (tree, int, formals_style);
47*e4b17023SJohn Marino
48*e4b17023SJohn Marino /* Given a string representing an entire type or an entire declaration
49*e4b17023SJohn Marino which only lacks the actual "data-type" specifier (at its left end),
50*e4b17023SJohn Marino affix the data-type specifier to the left end of the given type
51*e4b17023SJohn Marino specification or object declaration.
52*e4b17023SJohn Marino
53*e4b17023SJohn Marino Because of C language weirdness, the data-type specifier (which normally
54*e4b17023SJohn Marino goes in at the very left end) may have to be slipped in just to the
55*e4b17023SJohn Marino right of any leading "const" or "volatile" qualifiers (there may be more
56*e4b17023SJohn Marino than one). Actually this may not be strictly necessary because it seems
57*e4b17023SJohn Marino that GCC (at least) accepts `<data-type> const foo;' and treats it the
58*e4b17023SJohn Marino same as `const <data-type> foo;' but people are accustomed to seeing
59*e4b17023SJohn Marino `const char *foo;' and *not* `char const *foo;' so we try to create types
60*e4b17023SJohn Marino that look as expected. */
61*e4b17023SJohn Marino
62*e4b17023SJohn Marino static char *
affix_data_type(const char * param)63*e4b17023SJohn Marino affix_data_type (const char *param)
64*e4b17023SJohn Marino {
65*e4b17023SJohn Marino char *const type_or_decl = ASTRDUP (param);
66*e4b17023SJohn Marino char *p = type_or_decl;
67*e4b17023SJohn Marino char *qualifiers_then_data_type;
68*e4b17023SJohn Marino char saved;
69*e4b17023SJohn Marino
70*e4b17023SJohn Marino /* Skip as many leading const's or volatile's as there are. */
71*e4b17023SJohn Marino
72*e4b17023SJohn Marino for (;;)
73*e4b17023SJohn Marino {
74*e4b17023SJohn Marino if (!strncmp (p, "volatile ", 9))
75*e4b17023SJohn Marino {
76*e4b17023SJohn Marino p += 9;
77*e4b17023SJohn Marino continue;
78*e4b17023SJohn Marino }
79*e4b17023SJohn Marino if (!strncmp (p, "const ", 6))
80*e4b17023SJohn Marino {
81*e4b17023SJohn Marino p += 6;
82*e4b17023SJohn Marino continue;
83*e4b17023SJohn Marino }
84*e4b17023SJohn Marino break;
85*e4b17023SJohn Marino }
86*e4b17023SJohn Marino
87*e4b17023SJohn Marino /* p now points to the place where we can insert the data type. We have to
88*e4b17023SJohn Marino add a blank after the data-type of course. */
89*e4b17023SJohn Marino
90*e4b17023SJohn Marino if (p == type_or_decl)
91*e4b17023SJohn Marino return concat (data_type, " ", type_or_decl, NULL);
92*e4b17023SJohn Marino
93*e4b17023SJohn Marino saved = *p;
94*e4b17023SJohn Marino *p = '\0';
95*e4b17023SJohn Marino qualifiers_then_data_type = concat (type_or_decl, data_type, NULL);
96*e4b17023SJohn Marino *p = saved;
97*e4b17023SJohn Marino return reconcat (qualifiers_then_data_type,
98*e4b17023SJohn Marino qualifiers_then_data_type, " ", p, NULL);
99*e4b17023SJohn Marino }
100*e4b17023SJohn Marino
101*e4b17023SJohn Marino /* Given a tree node which represents some "function type", generate the
102*e4b17023SJohn Marino source code version of a formal parameter list (of some given style) for
103*e4b17023SJohn Marino this function type. Return the whole formal parameter list (including
104*e4b17023SJohn Marino a pair of surrounding parens) as a string. Note that if the style
105*e4b17023SJohn Marino we are currently aiming for is non-ansi, then we just return a pair
106*e4b17023SJohn Marino of empty parens here. */
107*e4b17023SJohn Marino
108*e4b17023SJohn Marino static const char *
gen_formal_list_for_type(tree fntype,formals_style style)109*e4b17023SJohn Marino gen_formal_list_for_type (tree fntype, formals_style style)
110*e4b17023SJohn Marino {
111*e4b17023SJohn Marino const char *formal_list = "";
112*e4b17023SJohn Marino tree formal_type;
113*e4b17023SJohn Marino
114*e4b17023SJohn Marino if (style != ansi)
115*e4b17023SJohn Marino return "()";
116*e4b17023SJohn Marino
117*e4b17023SJohn Marino formal_type = TYPE_ARG_TYPES (fntype);
118*e4b17023SJohn Marino while (formal_type && TREE_VALUE (formal_type) != void_type_node)
119*e4b17023SJohn Marino {
120*e4b17023SJohn Marino const char *this_type;
121*e4b17023SJohn Marino
122*e4b17023SJohn Marino if (*formal_list)
123*e4b17023SJohn Marino formal_list = concat (formal_list, ", ", NULL);
124*e4b17023SJohn Marino
125*e4b17023SJohn Marino this_type = gen_type ("", TREE_VALUE (formal_type), ansi);
126*e4b17023SJohn Marino formal_list
127*e4b17023SJohn Marino = ((strlen (this_type))
128*e4b17023SJohn Marino ? concat (formal_list, affix_data_type (this_type), NULL)
129*e4b17023SJohn Marino : concat (formal_list, data_type, NULL));
130*e4b17023SJohn Marino
131*e4b17023SJohn Marino formal_type = TREE_CHAIN (formal_type);
132*e4b17023SJohn Marino }
133*e4b17023SJohn Marino
134*e4b17023SJohn Marino /* If we got to here, then we are trying to generate an ANSI style formal
135*e4b17023SJohn Marino parameters list.
136*e4b17023SJohn Marino
137*e4b17023SJohn Marino New style prototyped ANSI formal parameter lists should in theory always
138*e4b17023SJohn Marino contain some stuff between the opening and closing parens, even if it is
139*e4b17023SJohn Marino only "void".
140*e4b17023SJohn Marino
141*e4b17023SJohn Marino The brutal truth though is that there is lots of old K&R code out there
142*e4b17023SJohn Marino which contains declarations of "pointer-to-function" parameters and
143*e4b17023SJohn Marino these almost never have fully specified formal parameter lists associated
144*e4b17023SJohn Marino with them. That is, the pointer-to-function parameters are declared
145*e4b17023SJohn Marino with just empty parameter lists.
146*e4b17023SJohn Marino
147*e4b17023SJohn Marino In cases such as these, protoize should really insert *something* into
148*e4b17023SJohn Marino the vacant parameter lists, but what? It has no basis on which to insert
149*e4b17023SJohn Marino anything in particular.
150*e4b17023SJohn Marino
151*e4b17023SJohn Marino Here, we make life easy for protoize by trying to distinguish between
152*e4b17023SJohn Marino K&R empty parameter lists and new-style prototyped parameter lists
153*e4b17023SJohn Marino that actually contain "void". In the latter case we (obviously) want
154*e4b17023SJohn Marino to output the "void" verbatim, and that what we do. In the former case,
155*e4b17023SJohn Marino we do our best to give protoize something nice to insert.
156*e4b17023SJohn Marino
157*e4b17023SJohn Marino This "something nice" should be something that is still valid (when
158*e4b17023SJohn Marino re-compiled) but something that can clearly indicate to the user that
159*e4b17023SJohn Marino more typing information (for the parameter list) should be added (by
160*e4b17023SJohn Marino hand) at some convenient moment.
161*e4b17023SJohn Marino
162*e4b17023SJohn Marino The string chosen here is a comment with question marks in it. */
163*e4b17023SJohn Marino
164*e4b17023SJohn Marino if (!*formal_list)
165*e4b17023SJohn Marino {
166*e4b17023SJohn Marino if (prototype_p (fntype))
167*e4b17023SJohn Marino /* assert (TREE_VALUE (TYPE_ARG_TYPES (fntype)) == void_type_node); */
168*e4b17023SJohn Marino formal_list = "void";
169*e4b17023SJohn Marino else
170*e4b17023SJohn Marino formal_list = "/* ??? */";
171*e4b17023SJohn Marino }
172*e4b17023SJohn Marino else
173*e4b17023SJohn Marino {
174*e4b17023SJohn Marino /* If there were at least some parameters, and if the formals-types-list
175*e4b17023SJohn Marino petered out to a NULL (i.e. without being terminated by a
176*e4b17023SJohn Marino void_type_node) then we need to tack on an ellipsis. */
177*e4b17023SJohn Marino if (!formal_type)
178*e4b17023SJohn Marino formal_list = concat (formal_list, ", ...", NULL);
179*e4b17023SJohn Marino }
180*e4b17023SJohn Marino
181*e4b17023SJohn Marino return concat (" (", formal_list, ")", NULL);
182*e4b17023SJohn Marino }
183*e4b17023SJohn Marino
184*e4b17023SJohn Marino /* Generate a parameter list for a function definition (in some given style).
185*e4b17023SJohn Marino
186*e4b17023SJohn Marino Note that this routine has to be separate (and different) from the code that
187*e4b17023SJohn Marino generates the prototype parameter lists for function declarations, because
188*e4b17023SJohn Marino in the case of a function declaration, all we have to go on is a tree node
189*e4b17023SJohn Marino representing the function's own "function type". This can tell us the types
190*e4b17023SJohn Marino of all of the formal parameters for the function, but it cannot tell us the
191*e4b17023SJohn Marino actual *names* of each of the formal parameters. We need to output those
192*e4b17023SJohn Marino parameter names for each function definition.
193*e4b17023SJohn Marino
194*e4b17023SJohn Marino This routine gets a pointer to a tree node which represents the actual
195*e4b17023SJohn Marino declaration of the given function, and this DECL node has a list of formal
196*e4b17023SJohn Marino parameter (variable) declarations attached to it. These formal parameter
197*e4b17023SJohn Marino (variable) declaration nodes give us the actual names of the formal
198*e4b17023SJohn Marino parameters for the given function definition.
199*e4b17023SJohn Marino
200*e4b17023SJohn Marino This routine returns a string which is the source form for the entire
201*e4b17023SJohn Marino function formal parameter list. */
202*e4b17023SJohn Marino
203*e4b17023SJohn Marino static const char *
gen_formal_list_for_func_def(tree fndecl,formals_style style)204*e4b17023SJohn Marino gen_formal_list_for_func_def (tree fndecl, formals_style style)
205*e4b17023SJohn Marino {
206*e4b17023SJohn Marino const char *formal_list = "";
207*e4b17023SJohn Marino tree formal_decl;
208*e4b17023SJohn Marino
209*e4b17023SJohn Marino formal_decl = DECL_ARGUMENTS (fndecl);
210*e4b17023SJohn Marino while (formal_decl)
211*e4b17023SJohn Marino {
212*e4b17023SJohn Marino const char *this_formal;
213*e4b17023SJohn Marino
214*e4b17023SJohn Marino if (*formal_list && ((style == ansi) || (style == k_and_r_names)))
215*e4b17023SJohn Marino formal_list = concat (formal_list, ", ", NULL);
216*e4b17023SJohn Marino this_formal = gen_decl (formal_decl, 0, style);
217*e4b17023SJohn Marino if (style == k_and_r_decls)
218*e4b17023SJohn Marino formal_list = concat (formal_list, this_formal, "; ", NULL);
219*e4b17023SJohn Marino else
220*e4b17023SJohn Marino formal_list = concat (formal_list, this_formal, NULL);
221*e4b17023SJohn Marino formal_decl = TREE_CHAIN (formal_decl);
222*e4b17023SJohn Marino }
223*e4b17023SJohn Marino if (style == ansi)
224*e4b17023SJohn Marino {
225*e4b17023SJohn Marino if (!DECL_ARGUMENTS (fndecl))
226*e4b17023SJohn Marino formal_list = concat (formal_list, "void", NULL);
227*e4b17023SJohn Marino if (stdarg_p (TREE_TYPE (fndecl)))
228*e4b17023SJohn Marino formal_list = concat (formal_list, ", ...", NULL);
229*e4b17023SJohn Marino }
230*e4b17023SJohn Marino if ((style == ansi) || (style == k_and_r_names))
231*e4b17023SJohn Marino formal_list = concat (" (", formal_list, ")", NULL);
232*e4b17023SJohn Marino return formal_list;
233*e4b17023SJohn Marino }
234*e4b17023SJohn Marino
235*e4b17023SJohn Marino /* Generate a string which is the source code form for a given type (t). This
236*e4b17023SJohn Marino routine is ugly and complex because the C syntax for declarations is ugly
237*e4b17023SJohn Marino and complex. This routine is straightforward so long as *no* pointer types,
238*e4b17023SJohn Marino array types, or function types are involved.
239*e4b17023SJohn Marino
240*e4b17023SJohn Marino In the simple cases, this routine will return the (string) value which was
241*e4b17023SJohn Marino passed in as the "ret_val" argument. Usually, this starts out either as an
242*e4b17023SJohn Marino empty string, or as the name of the declared item (i.e. the formal function
243*e4b17023SJohn Marino parameter variable).
244*e4b17023SJohn Marino
245*e4b17023SJohn Marino This routine will also return with the global variable "data_type" set to
246*e4b17023SJohn Marino some string value which is the "basic" data-type of the given complete type.
247*e4b17023SJohn Marino This "data_type" string can be concatenated onto the front of the returned
248*e4b17023SJohn Marino string after this routine returns to its caller.
249*e4b17023SJohn Marino
250*e4b17023SJohn Marino In complicated cases involving pointer types, array types, or function
251*e4b17023SJohn Marino types, the C declaration syntax requires an "inside out" approach, i.e. if
252*e4b17023SJohn Marino you have a type which is a "pointer-to-function" type, you need to handle
253*e4b17023SJohn Marino the "pointer" part first, but it also has to be "innermost" (relative to
254*e4b17023SJohn Marino the declaration stuff for the "function" type). Thus, is this case, you
255*e4b17023SJohn Marino must prepend a "(*" and append a ")" to the name of the item (i.e. formal
256*e4b17023SJohn Marino variable). Then you must append and prepend the other info for the
257*e4b17023SJohn Marino "function type" part of the overall type.
258*e4b17023SJohn Marino
259*e4b17023SJohn Marino To handle the "innermost precedence" rules of complicated C declarators, we
260*e4b17023SJohn Marino do the following (in this routine). The input parameter called "ret_val"
261*e4b17023SJohn Marino is treated as a "seed". Each time gen_type is called (perhaps recursively)
262*e4b17023SJohn Marino some additional strings may be appended or prepended (or both) to the "seed"
263*e4b17023SJohn Marino string. If yet another (lower) level of the GCC tree exists for the given
264*e4b17023SJohn Marino type (as in the case of a pointer type, an array type, or a function type)
265*e4b17023SJohn Marino then the (wrapped) seed is passed to a (recursive) invocation of gen_type()
266*e4b17023SJohn Marino this recursive invocation may again "wrap" the (new) seed with yet more
267*e4b17023SJohn Marino declarator stuff, by appending, prepending (or both). By the time the
268*e4b17023SJohn Marino recursion bottoms out, the "seed value" at that point will have a value
269*e4b17023SJohn Marino which is (almost) the complete source version of the declarator (except
270*e4b17023SJohn Marino for the data_type info). Thus, this deepest "seed" value is simply passed
271*e4b17023SJohn Marino back up through all of the recursive calls until it is given (as the return
272*e4b17023SJohn Marino value) to the initial caller of the gen_type() routine. All that remains
273*e4b17023SJohn Marino to do at this point is for the initial caller to prepend the "data_type"
274*e4b17023SJohn Marino string onto the returned "seed". */
275*e4b17023SJohn Marino
276*e4b17023SJohn Marino static const char *
gen_type(const char * ret_val,tree t,formals_style style)277*e4b17023SJohn Marino gen_type (const char *ret_val, tree t, formals_style style)
278*e4b17023SJohn Marino {
279*e4b17023SJohn Marino tree chain_p;
280*e4b17023SJohn Marino
281*e4b17023SJohn Marino /* If there is a typedef name for this type, use it. */
282*e4b17023SJohn Marino if (TYPE_NAME (t) && TREE_CODE (TYPE_NAME (t)) == TYPE_DECL)
283*e4b17023SJohn Marino data_type = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (t)));
284*e4b17023SJohn Marino else
285*e4b17023SJohn Marino {
286*e4b17023SJohn Marino switch (TREE_CODE (t))
287*e4b17023SJohn Marino {
288*e4b17023SJohn Marino case POINTER_TYPE:
289*e4b17023SJohn Marino if (TYPE_READONLY (t))
290*e4b17023SJohn Marino ret_val = concat ("const ", ret_val, NULL);
291*e4b17023SJohn Marino if (TYPE_VOLATILE (t))
292*e4b17023SJohn Marino ret_val = concat ("volatile ", ret_val, NULL);
293*e4b17023SJohn Marino
294*e4b17023SJohn Marino ret_val = concat ("*", ret_val, NULL);
295*e4b17023SJohn Marino
296*e4b17023SJohn Marino if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE || TREE_CODE (TREE_TYPE (t)) == FUNCTION_TYPE)
297*e4b17023SJohn Marino ret_val = concat ("(", ret_val, ")", NULL);
298*e4b17023SJohn Marino
299*e4b17023SJohn Marino ret_val = gen_type (ret_val, TREE_TYPE (t), style);
300*e4b17023SJohn Marino
301*e4b17023SJohn Marino return ret_val;
302*e4b17023SJohn Marino
303*e4b17023SJohn Marino case ARRAY_TYPE:
304*e4b17023SJohn Marino if (!COMPLETE_TYPE_P (t) || TREE_CODE (TYPE_SIZE (t)) != INTEGER_CST)
305*e4b17023SJohn Marino ret_val = gen_type (concat (ret_val, "[]", NULL),
306*e4b17023SJohn Marino TREE_TYPE (t), style);
307*e4b17023SJohn Marino else if (int_size_in_bytes (t) == 0)
308*e4b17023SJohn Marino ret_val = gen_type (concat (ret_val, "[0]", NULL),
309*e4b17023SJohn Marino TREE_TYPE (t), style);
310*e4b17023SJohn Marino else
311*e4b17023SJohn Marino {
312*e4b17023SJohn Marino int size = (int_size_in_bytes (t) / int_size_in_bytes (TREE_TYPE (t)));
313*e4b17023SJohn Marino char buff[10];
314*e4b17023SJohn Marino sprintf (buff, "[%d]", size);
315*e4b17023SJohn Marino ret_val = gen_type (concat (ret_val, buff, NULL),
316*e4b17023SJohn Marino TREE_TYPE (t), style);
317*e4b17023SJohn Marino }
318*e4b17023SJohn Marino break;
319*e4b17023SJohn Marino
320*e4b17023SJohn Marino case FUNCTION_TYPE:
321*e4b17023SJohn Marino ret_val = gen_type (concat (ret_val,
322*e4b17023SJohn Marino gen_formal_list_for_type (t, style),
323*e4b17023SJohn Marino NULL),
324*e4b17023SJohn Marino TREE_TYPE (t), style);
325*e4b17023SJohn Marino break;
326*e4b17023SJohn Marino
327*e4b17023SJohn Marino case IDENTIFIER_NODE:
328*e4b17023SJohn Marino data_type = IDENTIFIER_POINTER (t);
329*e4b17023SJohn Marino break;
330*e4b17023SJohn Marino
331*e4b17023SJohn Marino /* The following three cases are complicated by the fact that a
332*e4b17023SJohn Marino user may do something really stupid, like creating a brand new
333*e4b17023SJohn Marino "anonymous" type specification in a formal argument list (or as
334*e4b17023SJohn Marino part of a function return type specification). For example:
335*e4b17023SJohn Marino
336*e4b17023SJohn Marino int f (enum { red, green, blue } color);
337*e4b17023SJohn Marino
338*e4b17023SJohn Marino In such cases, we have no name that we can put into the prototype
339*e4b17023SJohn Marino to represent the (anonymous) type. Thus, we have to generate the
340*e4b17023SJohn Marino whole darn type specification. Yuck! */
341*e4b17023SJohn Marino
342*e4b17023SJohn Marino case RECORD_TYPE:
343*e4b17023SJohn Marino if (TYPE_NAME (t))
344*e4b17023SJohn Marino data_type = IDENTIFIER_POINTER (TYPE_NAME (t));
345*e4b17023SJohn Marino else
346*e4b17023SJohn Marino {
347*e4b17023SJohn Marino data_type = "";
348*e4b17023SJohn Marino chain_p = TYPE_FIELDS (t);
349*e4b17023SJohn Marino while (chain_p)
350*e4b17023SJohn Marino {
351*e4b17023SJohn Marino data_type = concat (data_type, gen_decl (chain_p, 0, ansi),
352*e4b17023SJohn Marino NULL);
353*e4b17023SJohn Marino chain_p = TREE_CHAIN (chain_p);
354*e4b17023SJohn Marino data_type = concat (data_type, "; ", NULL);
355*e4b17023SJohn Marino }
356*e4b17023SJohn Marino data_type = concat ("{ ", data_type, "}", NULL);
357*e4b17023SJohn Marino }
358*e4b17023SJohn Marino data_type = concat ("struct ", data_type, NULL);
359*e4b17023SJohn Marino break;
360*e4b17023SJohn Marino
361*e4b17023SJohn Marino case UNION_TYPE:
362*e4b17023SJohn Marino if (TYPE_NAME (t))
363*e4b17023SJohn Marino data_type = IDENTIFIER_POINTER (TYPE_NAME (t));
364*e4b17023SJohn Marino else
365*e4b17023SJohn Marino {
366*e4b17023SJohn Marino data_type = "";
367*e4b17023SJohn Marino chain_p = TYPE_FIELDS (t);
368*e4b17023SJohn Marino while (chain_p)
369*e4b17023SJohn Marino {
370*e4b17023SJohn Marino data_type = concat (data_type, gen_decl (chain_p, 0, ansi),
371*e4b17023SJohn Marino NULL);
372*e4b17023SJohn Marino chain_p = TREE_CHAIN (chain_p);
373*e4b17023SJohn Marino data_type = concat (data_type, "; ", NULL);
374*e4b17023SJohn Marino }
375*e4b17023SJohn Marino data_type = concat ("{ ", data_type, "}", NULL);
376*e4b17023SJohn Marino }
377*e4b17023SJohn Marino data_type = concat ("union ", data_type, NULL);
378*e4b17023SJohn Marino break;
379*e4b17023SJohn Marino
380*e4b17023SJohn Marino case ENUMERAL_TYPE:
381*e4b17023SJohn Marino if (TYPE_NAME (t))
382*e4b17023SJohn Marino data_type = IDENTIFIER_POINTER (TYPE_NAME (t));
383*e4b17023SJohn Marino else
384*e4b17023SJohn Marino {
385*e4b17023SJohn Marino data_type = "";
386*e4b17023SJohn Marino chain_p = TYPE_VALUES (t);
387*e4b17023SJohn Marino while (chain_p)
388*e4b17023SJohn Marino {
389*e4b17023SJohn Marino data_type = concat (data_type,
390*e4b17023SJohn Marino IDENTIFIER_POINTER (TREE_PURPOSE (chain_p)), NULL);
391*e4b17023SJohn Marino chain_p = TREE_CHAIN (chain_p);
392*e4b17023SJohn Marino if (chain_p)
393*e4b17023SJohn Marino data_type = concat (data_type, ", ", NULL);
394*e4b17023SJohn Marino }
395*e4b17023SJohn Marino data_type = concat ("{ ", data_type, " }", NULL);
396*e4b17023SJohn Marino }
397*e4b17023SJohn Marino data_type = concat ("enum ", data_type, NULL);
398*e4b17023SJohn Marino break;
399*e4b17023SJohn Marino
400*e4b17023SJohn Marino case TYPE_DECL:
401*e4b17023SJohn Marino data_type = IDENTIFIER_POINTER (DECL_NAME (t));
402*e4b17023SJohn Marino break;
403*e4b17023SJohn Marino
404*e4b17023SJohn Marino case INTEGER_TYPE:
405*e4b17023SJohn Marino case FIXED_POINT_TYPE:
406*e4b17023SJohn Marino data_type = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (t)));
407*e4b17023SJohn Marino /* Normally, `unsigned' is part of the deal. Not so if it comes
408*e4b17023SJohn Marino with a type qualifier. */
409*e4b17023SJohn Marino if (TYPE_UNSIGNED (t) && TYPE_QUALS (t))
410*e4b17023SJohn Marino data_type = concat ("unsigned ", data_type, NULL);
411*e4b17023SJohn Marino break;
412*e4b17023SJohn Marino
413*e4b17023SJohn Marino case REAL_TYPE:
414*e4b17023SJohn Marino data_type = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (t)));
415*e4b17023SJohn Marino break;
416*e4b17023SJohn Marino
417*e4b17023SJohn Marino case VOID_TYPE:
418*e4b17023SJohn Marino data_type = "void";
419*e4b17023SJohn Marino break;
420*e4b17023SJohn Marino
421*e4b17023SJohn Marino case ERROR_MARK:
422*e4b17023SJohn Marino data_type = "[ERROR]";
423*e4b17023SJohn Marino break;
424*e4b17023SJohn Marino
425*e4b17023SJohn Marino default:
426*e4b17023SJohn Marino gcc_unreachable ();
427*e4b17023SJohn Marino }
428*e4b17023SJohn Marino }
429*e4b17023SJohn Marino if (TYPE_READONLY (t))
430*e4b17023SJohn Marino ret_val = concat ("const ", ret_val, NULL);
431*e4b17023SJohn Marino if (TYPE_VOLATILE (t))
432*e4b17023SJohn Marino ret_val = concat ("volatile ", ret_val, NULL);
433*e4b17023SJohn Marino if (TYPE_RESTRICT (t))
434*e4b17023SJohn Marino ret_val = concat ("restrict ", ret_val, NULL);
435*e4b17023SJohn Marino return ret_val;
436*e4b17023SJohn Marino }
437*e4b17023SJohn Marino
438*e4b17023SJohn Marino /* Generate a string (source) representation of an entire entity declaration
439*e4b17023SJohn Marino (using some particular style for function types).
440*e4b17023SJohn Marino
441*e4b17023SJohn Marino The given entity may be either a variable or a function.
442*e4b17023SJohn Marino
443*e4b17023SJohn Marino If the "is_func_definition" parameter is nonzero, assume that the thing
444*e4b17023SJohn Marino we are generating a declaration for is a FUNCTION_DECL node which is
445*e4b17023SJohn Marino associated with a function definition. In this case, we can assume that
446*e4b17023SJohn Marino an attached list of DECL nodes for function formal arguments is present. */
447*e4b17023SJohn Marino
448*e4b17023SJohn Marino static const char *
gen_decl(tree decl,int is_func_definition,formals_style style)449*e4b17023SJohn Marino gen_decl (tree decl, int is_func_definition, formals_style style)
450*e4b17023SJohn Marino {
451*e4b17023SJohn Marino const char *ret_val;
452*e4b17023SJohn Marino
453*e4b17023SJohn Marino if (DECL_NAME (decl))
454*e4b17023SJohn Marino ret_val = IDENTIFIER_POINTER (DECL_NAME (decl));
455*e4b17023SJohn Marino else
456*e4b17023SJohn Marino ret_val = "";
457*e4b17023SJohn Marino
458*e4b17023SJohn Marino /* If we are just generating a list of names of formal parameters, we can
459*e4b17023SJohn Marino simply return the formal parameter name (with no typing information
460*e4b17023SJohn Marino attached to it) now. */
461*e4b17023SJohn Marino
462*e4b17023SJohn Marino if (style == k_and_r_names)
463*e4b17023SJohn Marino return ret_val;
464*e4b17023SJohn Marino
465*e4b17023SJohn Marino /* Note that for the declaration of some entity (either a function or a
466*e4b17023SJohn Marino data object, like for instance a parameter) if the entity itself was
467*e4b17023SJohn Marino declared as either const or volatile, then const and volatile properties
468*e4b17023SJohn Marino are associated with just the declaration of the entity, and *not* with
469*e4b17023SJohn Marino the `type' of the entity. Thus, for such declared entities, we have to
470*e4b17023SJohn Marino generate the qualifiers here. */
471*e4b17023SJohn Marino
472*e4b17023SJohn Marino if (TREE_THIS_VOLATILE (decl))
473*e4b17023SJohn Marino ret_val = concat ("volatile ", ret_val, NULL);
474*e4b17023SJohn Marino if (TREE_READONLY (decl))
475*e4b17023SJohn Marino ret_val = concat ("const ", ret_val, NULL);
476*e4b17023SJohn Marino
477*e4b17023SJohn Marino data_type = "";
478*e4b17023SJohn Marino
479*e4b17023SJohn Marino /* For FUNCTION_DECL nodes, there are two possible cases here. First, if
480*e4b17023SJohn Marino this FUNCTION_DECL node was generated from a function "definition", then
481*e4b17023SJohn Marino we will have a list of DECL_NODE's, one for each of the function's formal
482*e4b17023SJohn Marino parameters. In this case, we can print out not only the types of each
483*e4b17023SJohn Marino formal, but also each formal's name. In the second case, this
484*e4b17023SJohn Marino FUNCTION_DECL node came from an actual function declaration (and *not*
485*e4b17023SJohn Marino a definition). In this case, we do nothing here because the formal
486*e4b17023SJohn Marino argument type-list will be output later, when the "type" of the function
487*e4b17023SJohn Marino is added to the string we are building. Note that the ANSI-style formal
488*e4b17023SJohn Marino parameter list is considered to be a (suffix) part of the "type" of the
489*e4b17023SJohn Marino function. */
490*e4b17023SJohn Marino
491*e4b17023SJohn Marino if (TREE_CODE (decl) == FUNCTION_DECL && is_func_definition)
492*e4b17023SJohn Marino {
493*e4b17023SJohn Marino ret_val = concat (ret_val, gen_formal_list_for_func_def (decl, ansi),
494*e4b17023SJohn Marino NULL);
495*e4b17023SJohn Marino
496*e4b17023SJohn Marino /* Since we have already added in the formals list stuff, here we don't
497*e4b17023SJohn Marino add the whole "type" of the function we are considering (which
498*e4b17023SJohn Marino would include its parameter-list info), rather, we only add in
499*e4b17023SJohn Marino the "type" of the "type" of the function, which is really just
500*e4b17023SJohn Marino the return-type of the function (and does not include the parameter
501*e4b17023SJohn Marino list info). */
502*e4b17023SJohn Marino
503*e4b17023SJohn Marino ret_val = gen_type (ret_val, TREE_TYPE (TREE_TYPE (decl)), style);
504*e4b17023SJohn Marino }
505*e4b17023SJohn Marino else
506*e4b17023SJohn Marino ret_val = gen_type (ret_val, TREE_TYPE (decl), style);
507*e4b17023SJohn Marino
508*e4b17023SJohn Marino ret_val = affix_data_type (ret_val);
509*e4b17023SJohn Marino
510*e4b17023SJohn Marino if (TREE_CODE (decl) != FUNCTION_DECL && C_DECL_REGISTER (decl))
511*e4b17023SJohn Marino ret_val = concat ("register ", ret_val, NULL);
512*e4b17023SJohn Marino if (TREE_PUBLIC (decl))
513*e4b17023SJohn Marino ret_val = concat ("extern ", ret_val, NULL);
514*e4b17023SJohn Marino if (TREE_CODE (decl) == FUNCTION_DECL && !TREE_PUBLIC (decl))
515*e4b17023SJohn Marino ret_val = concat ("static ", ret_val, NULL);
516*e4b17023SJohn Marino
517*e4b17023SJohn Marino return ret_val;
518*e4b17023SJohn Marino }
519*e4b17023SJohn Marino
520*e4b17023SJohn Marino extern FILE *aux_info_file;
521*e4b17023SJohn Marino
522*e4b17023SJohn Marino /* Generate and write a new line of info to the aux-info (.X) file. This
523*e4b17023SJohn Marino routine is called once for each function declaration, and once for each
524*e4b17023SJohn Marino function definition (even the implicit ones). */
525*e4b17023SJohn Marino
526*e4b17023SJohn Marino void
gen_aux_info_record(tree fndecl,int is_definition,int is_implicit,int is_prototyped)527*e4b17023SJohn Marino gen_aux_info_record (tree fndecl, int is_definition, int is_implicit,
528*e4b17023SJohn Marino int is_prototyped)
529*e4b17023SJohn Marino {
530*e4b17023SJohn Marino if (flag_gen_aux_info)
531*e4b17023SJohn Marino {
532*e4b17023SJohn Marino static int compiled_from_record = 0;
533*e4b17023SJohn Marino expanded_location xloc = expand_location (DECL_SOURCE_LOCATION (fndecl));
534*e4b17023SJohn Marino
535*e4b17023SJohn Marino /* Each output .X file must have a header line. Write one now if we
536*e4b17023SJohn Marino have not yet done so. */
537*e4b17023SJohn Marino
538*e4b17023SJohn Marino if (!compiled_from_record++)
539*e4b17023SJohn Marino {
540*e4b17023SJohn Marino /* The first line tells which directory file names are relative to.
541*e4b17023SJohn Marino Currently, -aux-info works only for files in the working
542*e4b17023SJohn Marino directory, so just use a `.' as a placeholder for now. */
543*e4b17023SJohn Marino fprintf (aux_info_file, "/* compiled from: . */\n");
544*e4b17023SJohn Marino }
545*e4b17023SJohn Marino
546*e4b17023SJohn Marino /* Write the actual line of auxiliary info. */
547*e4b17023SJohn Marino
548*e4b17023SJohn Marino fprintf (aux_info_file, "/* %s:%d:%c%c */ %s;",
549*e4b17023SJohn Marino xloc.file, xloc.line,
550*e4b17023SJohn Marino (is_implicit) ? 'I' : (is_prototyped) ? 'N' : 'O',
551*e4b17023SJohn Marino (is_definition) ? 'F' : 'C',
552*e4b17023SJohn Marino gen_decl (fndecl, is_definition, ansi));
553*e4b17023SJohn Marino
554*e4b17023SJohn Marino /* If this is an explicit function declaration, we need to also write
555*e4b17023SJohn Marino out an old-style (i.e. K&R) function header, just in case the user
556*e4b17023SJohn Marino wants to run unprotoize. */
557*e4b17023SJohn Marino
558*e4b17023SJohn Marino if (is_definition)
559*e4b17023SJohn Marino {
560*e4b17023SJohn Marino fprintf (aux_info_file, " /*%s %s*/",
561*e4b17023SJohn Marino gen_formal_list_for_func_def (fndecl, k_and_r_names),
562*e4b17023SJohn Marino gen_formal_list_for_func_def (fndecl, k_and_r_decls));
563*e4b17023SJohn Marino }
564*e4b17023SJohn Marino
565*e4b17023SJohn Marino fprintf (aux_info_file, "\n");
566*e4b17023SJohn Marino }
567*e4b17023SJohn Marino }
568