1 /* Generate information regarding function declarations and definitions based
2 on information stored in GCC's tree structure. This code implements the
4 Copyright (C) 1989, 91, 94, 95, 97-99, 2000 Free Software Foundation, Inc.
5 Contributed by Ron Guilmette (rfg@segfault.us.com).
7 This file is part of GNU CC.
9 GNU CC is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2, or (at your option)
14 GNU CC is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with GNU CC; see the file COPYING. If not, write to
21 the Free Software Foundation, 59 Temple Place - Suite 330,
22 Boston, MA 02111-1307, USA. */
31 enum formals_style_enum
{
36 typedef enum formals_style_enum formals_style
;
39 static const char *data_type
;
41 static char *affix_data_type
PARAMS ((const char *));
42 static const char *gen_formal_list_for_type
PARAMS ((tree
, formals_style
));
43 static int deserves_ellipsis
PARAMS ((tree
));
44 static const char *gen_formal_list_for_func_def
PARAMS ((tree
, formals_style
));
45 static const char *gen_type
PARAMS ((const char *, tree
, formals_style
));
46 static const char *gen_decl
PARAMS ((tree
, int, formals_style
));
48 /* Given a string representing an entire type or an entire declaration
49 which only lacks the actual "data-type" specifier (at its left end),
50 affix the data-type specifier to the left end of the given type
51 specification or object declaration.
53 Because of C language weirdness, the data-type specifier (which normally
54 goes in at the very left end) may have to be slipped in just to the
55 right of any leading "const" or "volatile" qualifiers (there may be more
56 than one). Actually this may not be strictly necessary because it seems
57 that GCC (at least) accepts `<data-type> const foo;' and treats it the
58 same as `const <data-type> foo;' but people are accustomed to seeing
59 `const char *foo;' and *not* `char const *foo;' so we try to create types
60 that look as expected. */
63 affix_data_type (param
)
66 char *type_or_decl
= (char *) alloca (strlen (param
) + 1);
67 char *p
= type_or_decl
;
68 char *qualifiers_then_data_type
;
71 strcpy (type_or_decl
, param
);
73 /* Skip as many leading const's or volatile's as there are. */
77 if (!strncmp (p
, "volatile ", 9))
82 if (!strncmp (p
, "const ", 6))
90 /* p now points to the place where we can insert the data type. We have to
91 add a blank after the data-type of course. */
93 if (p
== type_or_decl
)
94 return concat (data_type
, " ", type_or_decl
, NULL_PTR
);
98 qualifiers_then_data_type
= concat (type_or_decl
, data_type
, NULL_PTR
);
100 return concat (qualifiers_then_data_type
, " ", p
, NULL_PTR
);
103 /* Given a tree node which represents some "function type", generate the
104 source code version of a formal parameter list (of some given style) for
105 this function type. Return the whole formal parameter list (including
106 a pair of surrounding parens) as a string. Note that if the style
107 we are currently aiming for is non-ansi, then we just return a pair
108 of empty parens here. */
111 gen_formal_list_for_type (fntype
, style
)
115 const char *formal_list
= "";
121 formal_type
= TYPE_ARG_TYPES (fntype
);
122 while (formal_type
&& TREE_VALUE (formal_type
) != void_type_node
)
124 const char *this_type
;
127 formal_list
= concat (formal_list
, ", ", NULL_PTR
);
129 this_type
= gen_type ("", TREE_VALUE (formal_type
), ansi
);
131 = ((strlen (this_type
))
132 ? concat (formal_list
, affix_data_type (this_type
), NULL_PTR
)
133 : concat (formal_list
, data_type
, NULL_PTR
));
135 formal_type
= TREE_CHAIN (formal_type
);
138 /* If we got to here, then we are trying to generate an ANSI style formal
141 New style prototyped ANSI formal parameter lists should in theory always
142 contain some stuff between the opening and closing parens, even if it is
145 The brutal truth though is that there is lots of old K&R code out there
146 which contains declarations of "pointer-to-function" parameters and
147 these almost never have fully specified formal parameter lists associated
148 with them. That is, the pointer-to-function parameters are declared
149 with just empty parameter lists.
151 In cases such as these, protoize should really insert *something* into
152 the vacant parameter lists, but what? It has no basis on which to insert
153 anything in particular.
155 Here, we make life easy for protoize by trying to distinguish between
156 K&R empty parameter lists and new-style prototyped parameter lists
157 that actually contain "void". In the latter case we (obviously) want
158 to output the "void" verbatim, and that what we do. In the former case,
159 we do our best to give protoize something nice to insert.
161 This "something nice" should be something that is still valid (when
162 re-compiled) but something that can clearly indicate to the user that
163 more typing information (for the parameter list) should be added (by
164 hand) at some convenient moment.
166 The string chosen here is a comment with question marks in it. */
170 if (TYPE_ARG_TYPES (fntype
))
171 /* assert (TREE_VALUE (TYPE_ARG_TYPES (fntype)) == void_type_node); */
172 formal_list
= "void";
174 formal_list
= "/* ??? */";
178 /* If there were at least some parameters, and if the formals-types-list
179 petered out to a NULL (i.e. without being terminated by a
180 void_type_node) then we need to tack on an ellipsis. */
182 formal_list
= concat (formal_list
, ", ...", NULL_PTR
);
185 return concat (" (", formal_list
, ")", NULL_PTR
);
188 /* For the generation of an ANSI prototype for a function definition, we have
189 to look at the formal parameter list of the function's own "type" to
190 determine if the function's formal parameter list should end with an
191 ellipsis. Given a tree node, the following function will return non-zero
192 if the "function type" parameter list should end with an ellipsis. */
195 deserves_ellipsis (fntype
)
200 formal_type
= TYPE_ARG_TYPES (fntype
);
201 while (formal_type
&& TREE_VALUE (formal_type
) != void_type_node
)
202 formal_type
= TREE_CHAIN (formal_type
);
204 /* If there were at least some parameters, and if the formals-types-list
205 petered out to a NULL (i.e. without being terminated by a void_type_node)
206 then we need to tack on an ellipsis. */
208 return (!formal_type
&& TYPE_ARG_TYPES (fntype
));
211 /* Generate a parameter list for a function definition (in some given style).
213 Note that this routine has to be separate (and different) from the code that
214 generates the prototype parameter lists for function declarations, because
215 in the case of a function declaration, all we have to go on is a tree node
216 representing the function's own "function type". This can tell us the types
217 of all of the formal parameters for the function, but it cannot tell us the
218 actual *names* of each of the formal parameters. We need to output those
219 parameter names for each function definition.
221 This routine gets a pointer to a tree node which represents the actual
222 declaration of the given function, and this DECL node has a list of formal
223 parameter (variable) declarations attached to it. These formal parameter
224 (variable) declaration nodes give us the actual names of the formal
225 parameters for the given function definition.
227 This routine returns a string which is the source form for the entire
228 function formal parameter list. */
231 gen_formal_list_for_func_def (fndecl
, style
)
235 const char *formal_list
= "";
238 formal_decl
= DECL_ARGUMENTS (fndecl
);
241 const char *this_formal
;
243 if (*formal_list
&& ((style
== ansi
) || (style
== k_and_r_names
)))
244 formal_list
= concat (formal_list
, ", ", NULL_PTR
);
245 this_formal
= gen_decl (formal_decl
, 0, style
);
246 if (style
== k_and_r_decls
)
247 formal_list
= concat (formal_list
, this_formal
, "; ", NULL_PTR
);
249 formal_list
= concat (formal_list
, this_formal
, NULL_PTR
);
250 formal_decl
= TREE_CHAIN (formal_decl
);
254 if (!DECL_ARGUMENTS (fndecl
))
255 formal_list
= concat (formal_list
, "void", NULL_PTR
);
256 if (deserves_ellipsis (TREE_TYPE (fndecl
)))
257 formal_list
= concat (formal_list
, ", ...", NULL_PTR
);
259 if ((style
== ansi
) || (style
== k_and_r_names
))
260 formal_list
= concat (" (", formal_list
, ")", NULL_PTR
);
264 /* Generate a string which is the source code form for a given type (t). This
265 routine is ugly and complex because the C syntax for declarations is ugly
266 and complex. This routine is straightforward so long as *no* pointer types,
267 array types, or function types are involved.
269 In the simple cases, this routine will return the (string) value which was
270 passed in as the "ret_val" argument. Usually, this starts out either as an
271 empty string, or as the name of the declared item (i.e. the formal function
274 This routine will also return with the global variable "data_type" set to
275 some string value which is the "basic" data-type of the given complete type.
276 This "data_type" string can be concatenated onto the front of the returned
277 string after this routine returns to its caller.
279 In complicated cases involving pointer types, array types, or function
280 types, the C declaration syntax requires an "inside out" approach, i.e. if
281 you have a type which is a "pointer-to-function" type, you need to handle
282 the "pointer" part first, but it also has to be "innermost" (relative to
283 the declaration stuff for the "function" type). Thus, is this case, you
284 must prepend a "(*" and append a ")" to the name of the item (i.e. formal
285 variable). Then you must append and prepend the other info for the
286 "function type" part of the overall type.
288 To handle the "innermost precedence" rules of complicated C declarators, we
289 do the following (in this routine). The input parameter called "ret_val"
290 is treated as a "seed". Each time gen_type is called (perhaps recursively)
291 some additional strings may be appended or prepended (or both) to the "seed"
292 string. If yet another (lower) level of the GCC tree exists for the given
293 type (as in the case of a pointer type, an array type, or a function type)
294 then the (wrapped) seed is passed to a (recursive) invocation of gen_type()
295 this recursive invocation may again "wrap" the (new) seed with yet more
296 declarator stuff, by appending, prepending (or both). By the time the
297 recursion bottoms out, the "seed value" at that point will have a value
298 which is (almost) the complete source version of the declarator (except
299 for the data_type info). Thus, this deepest "seed" value is simply passed
300 back up through all of the recursive calls until it is given (as the return
301 value) to the initial caller of the gen_type() routine. All that remains
302 to do at this point is for the initial caller to prepend the "data_type"
303 string onto the returned "seed". */
306 gen_type (ret_val
, t
, style
)
313 /* If there is a typedef name for this type, use it. */
314 if (TYPE_NAME (t
) && TREE_CODE (TYPE_NAME (t
)) == TYPE_DECL
)
315 data_type
= IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (t
)));
318 switch (TREE_CODE (t
))
321 if (TYPE_READONLY (t
))
322 ret_val
= concat ("const ", ret_val
, NULL_PTR
);
323 if (TYPE_VOLATILE (t
))
324 ret_val
= concat ("volatile ", ret_val
, NULL_PTR
);
326 ret_val
= concat ("*", ret_val
, NULL_PTR
);
328 if (TREE_CODE (TREE_TYPE (t
)) == ARRAY_TYPE
|| TREE_CODE (TREE_TYPE (t
)) == FUNCTION_TYPE
)
329 ret_val
= concat ("(", ret_val
, ")", NULL_PTR
);
331 ret_val
= gen_type (ret_val
, TREE_TYPE (t
), style
);
336 if (TYPE_SIZE (t
) == 0 || TREE_CODE (TYPE_SIZE (t
)) != INTEGER_CST
)
337 ret_val
= gen_type (concat (ret_val
, "[]", NULL_PTR
),
338 TREE_TYPE (t
), style
);
339 else if (int_size_in_bytes (t
) == 0)
340 ret_val
= gen_type (concat (ret_val
, "[0]", NULL_PTR
),
341 TREE_TYPE (t
), style
);
344 int size
= (int_size_in_bytes (t
) / int_size_in_bytes (TREE_TYPE (t
)));
346 sprintf (buff
, "[%d]", size
);
347 ret_val
= gen_type (concat (ret_val
, buff
, NULL_PTR
),
348 TREE_TYPE (t
), style
);
353 ret_val
= gen_type (concat (ret_val
,
354 gen_formal_list_for_type (t
, style
),
356 TREE_TYPE (t
), style
);
359 case IDENTIFIER_NODE
:
360 data_type
= IDENTIFIER_POINTER (t
);
363 /* The following three cases are complicated by the fact that a
364 user may do something really stupid, like creating a brand new
365 "anonymous" type specification in a formal argument list (or as
366 part of a function return type specification). For example:
368 int f (enum { red, green, blue } color);
370 In such cases, we have no name that we can put into the prototype
371 to represent the (anonymous) type. Thus, we have to generate the
372 whole darn type specification. Yuck! */
376 data_type
= IDENTIFIER_POINTER (TYPE_NAME (t
));
380 chain_p
= TYPE_FIELDS (t
);
383 data_type
= concat (data_type
, gen_decl (chain_p
, 0, ansi
),
385 chain_p
= TREE_CHAIN (chain_p
);
386 data_type
= concat (data_type
, "; ", NULL_PTR
);
388 data_type
= concat ("{ ", data_type
, "}", NULL_PTR
);
390 data_type
= concat ("struct ", data_type
, NULL_PTR
);
395 data_type
= IDENTIFIER_POINTER (TYPE_NAME (t
));
399 chain_p
= TYPE_FIELDS (t
);
402 data_type
= concat (data_type
, gen_decl (chain_p
, 0, ansi
),
404 chain_p
= TREE_CHAIN (chain_p
);
405 data_type
= concat (data_type
, "; ", NULL_PTR
);
407 data_type
= concat ("{ ", data_type
, "}", NULL_PTR
);
409 data_type
= concat ("union ", data_type
, NULL_PTR
);
414 data_type
= IDENTIFIER_POINTER (TYPE_NAME (t
));
418 chain_p
= TYPE_VALUES (t
);
421 data_type
= concat (data_type
,
422 IDENTIFIER_POINTER (TREE_PURPOSE (chain_p
)), NULL_PTR
);
423 chain_p
= TREE_CHAIN (chain_p
);
425 data_type
= concat (data_type
, ", ", NULL_PTR
);
427 data_type
= concat ("{ ", data_type
, " }", NULL_PTR
);
429 data_type
= concat ("enum ", data_type
, NULL_PTR
);
433 data_type
= IDENTIFIER_POINTER (DECL_NAME (t
));
437 data_type
= IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (t
)));
438 /* Normally, `unsigned' is part of the deal. Not so if it comes
439 with a type qualifier. */
440 if (TREE_UNSIGNED (t
) && TYPE_QUALS (t
))
441 data_type
= concat ("unsigned ", data_type
, NULL_PTR
);
445 data_type
= IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (t
)));
453 data_type
= "[ERROR]";
460 if (TYPE_READONLY (t
))
461 ret_val
= concat ("const ", ret_val
, NULL_PTR
);
462 if (TYPE_VOLATILE (t
))
463 ret_val
= concat ("volatile ", ret_val
, NULL_PTR
);
464 if (TYPE_RESTRICT (t
))
465 ret_val
= concat ("restrict ", ret_val
, NULL_PTR
);
469 /* Generate a string (source) representation of an entire entity declaration
470 (using some particular style for function types).
472 The given entity may be either a variable or a function.
474 If the "is_func_definition" parameter is non-zero, assume that the thing
475 we are generating a declaration for is a FUNCTION_DECL node which is
476 associated with a function definition. In this case, we can assume that
477 an attached list of DECL nodes for function formal arguments is present. */
480 gen_decl (decl
, is_func_definition
, style
)
482 int is_func_definition
;
487 if (DECL_NAME (decl
))
488 ret_val
= IDENTIFIER_POINTER (DECL_NAME (decl
));
492 /* If we are just generating a list of names of formal parameters, we can
493 simply return the formal parameter name (with no typing information
494 attached to it) now. */
496 if (style
== k_and_r_names
)
499 /* Note that for the declaration of some entity (either a function or a
500 data object, like for instance a parameter) if the entity itself was
501 declared as either const or volatile, then const and volatile properties
502 are associated with just the declaration of the entity, and *not* with
503 the `type' of the entity. Thus, for such declared entities, we have to
504 generate the qualifiers here. */
506 if (TREE_THIS_VOLATILE (decl
))
507 ret_val
= concat ("volatile ", ret_val
, NULL_PTR
);
508 if (TREE_READONLY (decl
))
509 ret_val
= concat ("const ", ret_val
, NULL_PTR
);
513 /* For FUNCTION_DECL nodes, there are two possible cases here. First, if
514 this FUNCTION_DECL node was generated from a function "definition", then
515 we will have a list of DECL_NODE's, one for each of the function's formal
516 parameters. In this case, we can print out not only the types of each
517 formal, but also each formal's name. In the second case, this
518 FUNCTION_DECL node came from an actual function declaration (and *not*
519 a definition). In this case, we do nothing here because the formal
520 argument type-list will be output later, when the "type" of the function
521 is added to the string we are building. Note that the ANSI-style formal
522 parameter list is considered to be a (suffix) part of the "type" of the
525 if (TREE_CODE (decl
) == FUNCTION_DECL
&& is_func_definition
)
527 ret_val
= concat (ret_val
, gen_formal_list_for_func_def (decl
, ansi
),
530 /* Since we have already added in the formals list stuff, here we don't
531 add the whole "type" of the function we are considering (which
532 would include its parameter-list info), rather, we only add in
533 the "type" of the "type" of the function, which is really just
534 the return-type of the function (and does not include the parameter
537 ret_val
= gen_type (ret_val
, TREE_TYPE (TREE_TYPE (decl
)), style
);
540 ret_val
= gen_type (ret_val
, TREE_TYPE (decl
), style
);
542 ret_val
= affix_data_type (ret_val
);
544 if (TREE_CODE (decl
) != FUNCTION_DECL
&& DECL_REGISTER (decl
))
545 ret_val
= concat ("register ", ret_val
, NULL_PTR
);
546 if (TREE_PUBLIC (decl
))
547 ret_val
= concat ("extern ", ret_val
, NULL_PTR
);
548 if (TREE_CODE (decl
) == FUNCTION_DECL
&& !TREE_PUBLIC (decl
))
549 ret_val
= concat ("static ", ret_val
, NULL_PTR
);
554 extern FILE *aux_info_file
;
556 /* Generate and write a new line of info to the aux-info (.X) file. This
557 routine is called once for each function declaration, and once for each
558 function definition (even the implicit ones). */
561 gen_aux_info_record (fndecl
, is_definition
, is_implicit
, is_prototyped
)
567 if (flag_gen_aux_info
)
569 static int compiled_from_record
= 0;
571 /* Each output .X file must have a header line. Write one now if we
572 have not yet done so. */
574 if (! compiled_from_record
++)
576 /* The first line tells which directory file names are relative to.
577 Currently, -aux-info works only for files in the working
578 directory, so just use a `.' as a placeholder for now. */
579 fprintf (aux_info_file
, "/* compiled from: . */\n");
582 /* Write the actual line of auxiliary info. */
584 fprintf (aux_info_file
, "/* %s:%d:%c%c */ %s;",
585 DECL_SOURCE_FILE (fndecl
),
586 DECL_SOURCE_LINE (fndecl
),
587 (is_implicit
) ? 'I' : (is_prototyped
) ? 'N' : 'O',
588 (is_definition
) ? 'F' : 'C',
589 gen_decl (fndecl
, is_definition
, ansi
));
591 /* If this is an explicit function declaration, we need to also write
592 out an old-style (i.e. K&R) function header, just in case the user
593 wants to run unprotoize. */
597 fprintf (aux_info_file
, " /*%s %s*/",
598 gen_formal_list_for_func_def (fndecl
, k_and_r_names
),
599 gen_formal_list_for_func_def (fndecl
, k_and_r_decls
));
602 fprintf (aux_info_file
, "\n");