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, 1998 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. */
30 enum formals_style_enum {
35 typedef enum formals_style_enum formals_style;
38 static char *data_type;
40 static char *affix_data_type PROTO((char *));
41 static char *gen_formal_list_for_type PROTO((tree, formals_style));
42 static int deserves_ellipsis PROTO((tree));
43 static char *gen_formal_list_for_func_def PROTO((tree, formals_style));
44 static char *gen_type PROTO((char *, tree, formals_style));
45 static char *gen_decl PROTO((tree, int, formals_style));
47 /* Concatenate a sequence of strings, returning the result.
49 This function is based on the one in libiberty. */
51 /* This definition will conflict with the one from prefix.c in
52 libcpp.a when linking cc1 and cc1obj. So only provide it if we are
56 concat VPROTO((const char *first, ...))
59 register char *newstr;
61 register const char *arg;
63 #ifndef ANSI_PROTOTYPES
67 /* First compute the size of the result and get sufficient memory. */
69 VA_START (args, first);
70 #ifndef ANSI_PROTOTYPES
71 first = va_arg (args, const char *);
79 length += strlen (arg);
80 arg = va_arg (args, const char *);
83 newstr = (char *) malloc (length + 1);
86 /* Now copy the individual pieces to the result string. */
88 VA_START (args, first);
89 #ifndef ANSI_PROTOTYPES
90 first = va_arg (args, char *);
99 arg = va_arg (args, const char *);
106 #endif /* ! USE_CPPLIB */
108 /* Given a string representing an entire type or an entire declaration
109 which only lacks the actual "data-type" specifier (at its left end),
110 affix the data-type specifier to the left end of the given type
111 specification or object declaration.
113 Because of C language weirdness, the data-type specifier (which normally
114 goes in at the very left end) may have to be slipped in just to the
115 right of any leading "const" or "volatile" qualifiers (there may be more
116 than one). Actually this may not be strictly necessary because it seems
117 that GCC (at least) accepts `<data-type> const foo;' and treats it the
118 same as `const <data-type> foo;' but people are accustomed to seeing
119 `const char *foo;' and *not* `char const *foo;' so we try to create types
120 that look as expected. */
123 affix_data_type (type_or_decl)
126 char *p = type_or_decl;
127 char *qualifiers_then_data_type;
130 /* Skip as many leading const's or volatile's as there are. */
134 if (!strncmp (p, "volatile ", 9))
139 if (!strncmp (p, "const ", 6))
147 /* p now points to the place where we can insert the data type. We have to
148 add a blank after the data-type of course. */
150 if (p == type_or_decl)
151 return concat (data_type, " ", type_or_decl, NULL_PTR);
155 qualifiers_then_data_type = concat (type_or_decl, data_type, NULL_PTR);
157 return concat (qualifiers_then_data_type, " ", p, NULL_PTR);
160 /* Given a tree node which represents some "function type", generate the
161 source code version of a formal parameter list (of some given style) for
162 this function type. Return the whole formal parameter list (including
163 a pair of surrounding parens) as a string. Note that if the style
164 we are currently aiming for is non-ansi, then we just return a pair
165 of empty parens here. */
168 gen_formal_list_for_type (fntype, style)
172 char *formal_list = "";
178 formal_type = TYPE_ARG_TYPES (fntype);
179 while (formal_type && TREE_VALUE (formal_type) != void_type_node)
184 formal_list = concat (formal_list, ", ", NULL_PTR);
186 this_type = gen_type ("", TREE_VALUE (formal_type), ansi);
188 = ((strlen (this_type))
189 ? concat (formal_list, affix_data_type (this_type), NULL_PTR)
190 : concat (formal_list, data_type, NULL_PTR));
192 formal_type = TREE_CHAIN (formal_type);
195 /* If we got to here, then we are trying to generate an ANSI style formal
198 New style prototyped ANSI formal parameter lists should in theory always
199 contain some stuff between the opening and closing parens, even if it is
202 The brutal truth though is that there is lots of old K&R code out there
203 which contains declarations of "pointer-to-function" parameters and
204 these almost never have fully specified formal parameter lists associated
205 with them. That is, the pointer-to-function parameters are declared
206 with just empty parameter lists.
208 In cases such as these, protoize should really insert *something* into
209 the vacant parameter lists, but what? It has no basis on which to insert
210 anything in particular.
212 Here, we make life easy for protoize by trying to distinguish between
213 K&R empty parameter lists and new-style prototyped parameter lists
214 that actually contain "void". In the latter case we (obviously) want
215 to output the "void" verbatim, and that what we do. In the former case,
216 we do our best to give protoize something nice to insert.
218 This "something nice" should be something that is still valid (when
219 re-compiled) but something that can clearly indicate to the user that
220 more typing information (for the parameter list) should be added (by
221 hand) at some convenient moment.
223 The string chosen here is a comment with question marks in it. */
227 if (TYPE_ARG_TYPES (fntype))
228 /* assert (TREE_VALUE (TYPE_ARG_TYPES (fntype)) == void_type_node); */
229 formal_list = "void";
231 formal_list = "/* ??? */";
235 /* If there were at least some parameters, and if the formals-types-list
236 petered out to a NULL (i.e. without being terminated by a
237 void_type_node) then we need to tack on an ellipsis. */
239 formal_list = concat (formal_list, ", ...", NULL_PTR);
242 return concat (" (", formal_list, ")", NULL_PTR);
245 /* For the generation of an ANSI prototype for a function definition, we have
246 to look at the formal parameter list of the function's own "type" to
247 determine if the function's formal parameter list should end with an
248 ellipsis. Given a tree node, the following function will return non-zero
249 if the "function type" parameter list should end with an ellipsis. */
252 deserves_ellipsis (fntype)
257 formal_type = TYPE_ARG_TYPES (fntype);
258 while (formal_type && TREE_VALUE (formal_type) != void_type_node)
259 formal_type = TREE_CHAIN (formal_type);
261 /* If there were at least some parameters, and if the formals-types-list
262 petered out to a NULL (i.e. without being terminated by a void_type_node)
263 then we need to tack on an ellipsis. */
265 return (!formal_type && TYPE_ARG_TYPES (fntype));
268 /* Generate a parameter list for a function definition (in some given style).
270 Note that this routine has to be separate (and different) from the code that
271 generates the prototype parameter lists for function declarations, because
272 in the case of a function declaration, all we have to go on is a tree node
273 representing the function's own "function type". This can tell us the types
274 of all of the formal parameters for the function, but it cannot tell us the
275 actual *names* of each of the formal parameters. We need to output those
276 parameter names for each function definition.
278 This routine gets a pointer to a tree node which represents the actual
279 declaration of the given function, and this DECL node has a list of formal
280 parameter (variable) declarations attached to it. These formal parameter
281 (variable) declaration nodes give us the actual names of the formal
282 parameters for the given function definition.
284 This routine returns a string which is the source form for the entire
285 function formal parameter list. */
288 gen_formal_list_for_func_def (fndecl, style)
292 char *formal_list = "";
295 formal_decl = DECL_ARGUMENTS (fndecl);
300 if (*formal_list && ((style == ansi) || (style == k_and_r_names)))
301 formal_list = concat (formal_list, ", ", NULL_PTR);
302 this_formal = gen_decl (formal_decl, 0, style);
303 if (style == k_and_r_decls)
304 formal_list = concat (formal_list, this_formal, "; ", NULL_PTR);
306 formal_list = concat (formal_list, this_formal, NULL_PTR);
307 formal_decl = TREE_CHAIN (formal_decl);
311 if (!DECL_ARGUMENTS (fndecl))
312 formal_list = concat (formal_list, "void", NULL_PTR);
313 if (deserves_ellipsis (TREE_TYPE (fndecl)))
314 formal_list = concat (formal_list, ", ...", NULL_PTR);
316 if ((style == ansi) || (style == k_and_r_names))
317 formal_list = concat (" (", formal_list, ")", NULL_PTR);
321 /* Generate a string which is the source code form for a given type (t). This
322 routine is ugly and complex because the C syntax for declarations is ugly
323 and complex. This routine is straightforward so long as *no* pointer types,
324 array types, or function types are involved.
326 In the simple cases, this routine will return the (string) value which was
327 passed in as the "ret_val" argument. Usually, this starts out either as an
328 empty string, or as the name of the declared item (i.e. the formal function
331 This routine will also return with the global variable "data_type" set to
332 some string value which is the "basic" data-type of the given complete type.
333 This "data_type" string can be concatenated onto the front of the returned
334 string after this routine returns to its caller.
336 In complicated cases involving pointer types, array types, or function
337 types, the C declaration syntax requires an "inside out" approach, i.e. if
338 you have a type which is a "pointer-to-function" type, you need to handle
339 the "pointer" part first, but it also has to be "innermost" (relative to
340 the declaration stuff for the "function" type). Thus, is this case, you
341 must prepend a "(*" and append a ")" to the name of the item (i.e. formal
342 variable). Then you must append and prepend the other info for the
343 "function type" part of the overall type.
345 To handle the "innermost precedence" rules of complicated C declarators, we
346 do the following (in this routine). The input parameter called "ret_val"
347 is treated as a "seed". Each time gen_type is called (perhaps recursively)
348 some additional strings may be appended or prepended (or both) to the "seed"
349 string. If yet another (lower) level of the GCC tree exists for the given
350 type (as in the case of a pointer type, an array type, or a function type)
351 then the (wrapped) seed is passed to a (recursive) invocation of gen_type()
352 this recursive invocation may again "wrap" the (new) seed with yet more
353 declarator stuff, by appending, prepending (or both). By the time the
354 recursion bottoms out, the "seed value" at that point will have a value
355 which is (almost) the complete source version of the declarator (except
356 for the data_type info). Thus, this deepest "seed" value is simply passed
357 back up through all of the recursive calls until it is given (as the return
358 value) to the initial caller of the gen_type() routine. All that remains
359 to do at this point is for the initial caller to prepend the "data_type"
360 string onto the returned "seed". */
363 gen_type (ret_val, t, style)
370 /* If there is a typedef name for this type, use it. */
371 if (TYPE_NAME (t) && TREE_CODE (TYPE_NAME (t)) == TYPE_DECL)
372 data_type = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (t)));
375 switch (TREE_CODE (t))
378 if (TYPE_READONLY (t))
379 ret_val = concat ("const ", ret_val, NULL_PTR);
380 if (TYPE_VOLATILE (t))
381 ret_val = concat ("volatile ", ret_val, NULL_PTR);
383 ret_val = concat ("*", ret_val, NULL_PTR);
385 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE || TREE_CODE (TREE_TYPE (t)) == FUNCTION_TYPE)
386 ret_val = concat ("(", ret_val, ")", NULL_PTR);
388 ret_val = gen_type (ret_val, TREE_TYPE (t), style);
393 if (TYPE_SIZE (t) == 0 || TREE_CODE (TYPE_SIZE (t)) != INTEGER_CST)
394 ret_val = gen_type (concat (ret_val, "[]", NULL_PTR),
395 TREE_TYPE (t), style);
396 else if (int_size_in_bytes (t) == 0)
397 ret_val = gen_type (concat (ret_val, "[0]", NULL_PTR),
398 TREE_TYPE (t), style);
401 int size = (int_size_in_bytes (t) / int_size_in_bytes (TREE_TYPE (t)));
403 sprintf (buff, "[%d]", size);
404 ret_val = gen_type (concat (ret_val, buff, NULL_PTR),
405 TREE_TYPE (t), style);
410 ret_val = gen_type (concat (ret_val,
411 gen_formal_list_for_type (t, style),
413 TREE_TYPE (t), style);
416 case IDENTIFIER_NODE:
417 data_type = IDENTIFIER_POINTER (t);
420 /* The following three cases are complicated by the fact that a
421 user may do something really stupid, like creating a brand new
422 "anonymous" type specification in a formal argument list (or as
423 part of a function return type specification). For example:
425 int f (enum { red, green, blue } color);
427 In such cases, we have no name that we can put into the prototype
428 to represent the (anonymous) type. Thus, we have to generate the
429 whole darn type specification. Yuck! */
433 data_type = IDENTIFIER_POINTER (TYPE_NAME (t));
437 chain_p = TYPE_FIELDS (t);
440 data_type = concat (data_type, gen_decl (chain_p, 0, ansi),
442 chain_p = TREE_CHAIN (chain_p);
443 data_type = concat (data_type, "; ", NULL_PTR);
445 data_type = concat ("{ ", data_type, "}", NULL_PTR);
447 data_type = concat ("struct ", data_type, NULL_PTR);
452 data_type = IDENTIFIER_POINTER (TYPE_NAME (t));
456 chain_p = TYPE_FIELDS (t);
459 data_type = concat (data_type, gen_decl (chain_p, 0, ansi),
461 chain_p = TREE_CHAIN (chain_p);
462 data_type = concat (data_type, "; ", NULL_PTR);
464 data_type = concat ("{ ", data_type, "}", NULL_PTR);
466 data_type = concat ("union ", data_type, NULL_PTR);
471 data_type = IDENTIFIER_POINTER (TYPE_NAME (t));
475 chain_p = TYPE_VALUES (t);
478 data_type = concat (data_type,
479 IDENTIFIER_POINTER (TREE_PURPOSE (chain_p)), NULL_PTR);
480 chain_p = TREE_CHAIN (chain_p);
482 data_type = concat (data_type, ", ", NULL_PTR);
484 data_type = concat ("{ ", data_type, " }", NULL_PTR);
486 data_type = concat ("enum ", data_type, NULL_PTR);
490 data_type = IDENTIFIER_POINTER (DECL_NAME (t));
494 data_type = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (t)));
495 /* Normally, `unsigned' is part of the deal. Not so if it comes
496 with a type qualifier. */
497 if (TREE_UNSIGNED (t) && TYPE_QUALS (t))
498 data_type = concat ("unsigned ", data_type, NULL_PTR);
502 data_type = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (t)));
510 data_type = "[ERROR]";
517 if (TYPE_READONLY (t))
518 ret_val = concat ("const ", ret_val, NULL_PTR);
519 if (TYPE_VOLATILE (t))
520 ret_val = concat ("volatile ", ret_val, NULL_PTR);
521 if (TYPE_RESTRICT (t))
522 ret_val = concat ("restrict ", ret_val, NULL_PTR);
526 /* Generate a string (source) representation of an entire entity declaration
527 (using some particular style for function types).
529 The given entity may be either a variable or a function.
531 If the "is_func_definition" parameter is non-zero, assume that the thing
532 we are generating a declaration for is a FUNCTION_DECL node which is
533 associated with a function definition. In this case, we can assume that
534 an attached list of DECL nodes for function formal arguments is present. */
537 gen_decl (decl, is_func_definition, style)
539 int is_func_definition;
544 if (DECL_NAME (decl))
545 ret_val = IDENTIFIER_POINTER (DECL_NAME (decl));
549 /* If we are just generating a list of names of formal parameters, we can
550 simply return the formal parameter name (with no typing information
551 attached to it) now. */
553 if (style == k_and_r_names)
556 /* Note that for the declaration of some entity (either a function or a
557 data object, like for instance a parameter) if the entity itself was
558 declared as either const or volatile, then const and volatile properties
559 are associated with just the declaration of the entity, and *not* with
560 the `type' of the entity. Thus, for such declared entities, we have to
561 generate the qualifiers here. */
563 if (TREE_THIS_VOLATILE (decl))
564 ret_val = concat ("volatile ", ret_val, NULL_PTR);
565 if (TREE_READONLY (decl))
566 ret_val = concat ("const ", ret_val, NULL_PTR);
570 /* For FUNCTION_DECL nodes, there are two possible cases here. First, if
571 this FUNCTION_DECL node was generated from a function "definition", then
572 we will have a list of DECL_NODE's, one for each of the function's formal
573 parameters. In this case, we can print out not only the types of each
574 formal, but also each formal's name. In the second case, this
575 FUNCTION_DECL node came from an actual function declaration (and *not*
576 a definition). In this case, we do nothing here because the formal
577 argument type-list will be output later, when the "type" of the function
578 is added to the string we are building. Note that the ANSI-style formal
579 parameter list is considered to be a (suffix) part of the "type" of the
582 if (TREE_CODE (decl) == FUNCTION_DECL && is_func_definition)
584 ret_val = concat (ret_val, gen_formal_list_for_func_def (decl, ansi),
587 /* Since we have already added in the formals list stuff, here we don't
588 add the whole "type" of the function we are considering (which
589 would include its parameter-list info), rather, we only add in
590 the "type" of the "type" of the function, which is really just
591 the return-type of the function (and does not include the parameter
594 ret_val = gen_type (ret_val, TREE_TYPE (TREE_TYPE (decl)), style);
597 ret_val = gen_type (ret_val, TREE_TYPE (decl), style);
599 ret_val = affix_data_type (ret_val);
601 if (TREE_CODE (decl) != FUNCTION_DECL && DECL_REGISTER (decl))
602 ret_val = concat ("register ", ret_val, NULL_PTR);
603 if (TREE_PUBLIC (decl))
604 ret_val = concat ("extern ", ret_val, NULL_PTR);
605 if (TREE_CODE (decl) == FUNCTION_DECL && !TREE_PUBLIC (decl))
606 ret_val = concat ("static ", ret_val, NULL_PTR);
611 extern FILE *aux_info_file;
613 /* Generate and write a new line of info to the aux-info (.X) file. This
614 routine is called once for each function declaration, and once for each
615 function definition (even the implicit ones). */
618 gen_aux_info_record (fndecl, is_definition, is_implicit, is_prototyped)
624 if (flag_gen_aux_info)
626 static int compiled_from_record = 0;
628 /* Each output .X file must have a header line. Write one now if we
629 have not yet done so. */
631 if (! compiled_from_record++)
633 /* The first line tells which directory file names are relative to.
634 Currently, -aux-info works only for files in the working
635 directory, so just use a `.' as a placeholder for now. */
636 fprintf (aux_info_file, "/* compiled from: . */\n");
639 /* Write the actual line of auxiliary info. */
641 fprintf (aux_info_file, "/* %s:%d:%c%c */ %s;",
642 DECL_SOURCE_FILE (fndecl),
643 DECL_SOURCE_LINE (fndecl),
644 (is_implicit) ? 'I' : (is_prototyped) ? 'N' : 'O',
645 (is_definition) ? 'F' : 'C',
646 gen_decl (fndecl, is_definition, ansi));
648 /* If this is an explicit function declaration, we need to also write
649 out an old-style (i.e. K&R) function header, just in case the user
650 wants to run unprotoize. */
654 fprintf (aux_info_file, " /*%s %s*/",
655 gen_formal_list_for_func_def (fndecl, k_and_r_names),
656 gen_formal_list_for_func_def (fndecl, k_and_r_decls));
659 fprintf (aux_info_file, "\n");