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-98, 1999 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 const char *data_type;
40 static char *affix_data_type PROTO((const char *));
41 static const char *gen_formal_list_for_type PROTO((tree, formals_style));
42 static int deserves_ellipsis PROTO((tree));
43 static const char *gen_formal_list_for_func_def PROTO((tree, formals_style));
44 static const char *gen_type PROTO((const char *, tree, formals_style));
45 static const 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 (param)
126 char *type_or_decl = (char *) alloca (strlen (param) + 1);
127 char *p = type_or_decl;
128 char *qualifiers_then_data_type;
131 strcpy (type_or_decl, param);
133 /* Skip as many leading const's or volatile's as there are. */
137 if (!strncmp (p, "volatile ", 9))
142 if (!strncmp (p, "const ", 6))
150 /* p now points to the place where we can insert the data type. We have to
151 add a blank after the data-type of course. */
153 if (p == type_or_decl)
154 return concat (data_type, " ", type_or_decl, NULL_PTR);
158 qualifiers_then_data_type = concat (type_or_decl, data_type, NULL_PTR);
160 return concat (qualifiers_then_data_type, " ", p, NULL_PTR);
163 /* Given a tree node which represents some "function type", generate the
164 source code version of a formal parameter list (of some given style) for
165 this function type. Return the whole formal parameter list (including
166 a pair of surrounding parens) as a string. Note that if the style
167 we are currently aiming for is non-ansi, then we just return a pair
168 of empty parens here. */
171 gen_formal_list_for_type (fntype, style)
175 const char *formal_list = "";
181 formal_type = TYPE_ARG_TYPES (fntype);
182 while (formal_type && TREE_VALUE (formal_type) != void_type_node)
184 const char *this_type;
187 formal_list = concat (formal_list, ", ", NULL_PTR);
189 this_type = gen_type ("", TREE_VALUE (formal_type), ansi);
191 = ((strlen (this_type))
192 ? concat (formal_list, affix_data_type (this_type), NULL_PTR)
193 : concat (formal_list, data_type, NULL_PTR));
195 formal_type = TREE_CHAIN (formal_type);
198 /* If we got to here, then we are trying to generate an ANSI style formal
201 New style prototyped ANSI formal parameter lists should in theory always
202 contain some stuff between the opening and closing parens, even if it is
205 The brutal truth though is that there is lots of old K&R code out there
206 which contains declarations of "pointer-to-function" parameters and
207 these almost never have fully specified formal parameter lists associated
208 with them. That is, the pointer-to-function parameters are declared
209 with just empty parameter lists.
211 In cases such as these, protoize should really insert *something* into
212 the vacant parameter lists, but what? It has no basis on which to insert
213 anything in particular.
215 Here, we make life easy for protoize by trying to distinguish between
216 K&R empty parameter lists and new-style prototyped parameter lists
217 that actually contain "void". In the latter case we (obviously) want
218 to output the "void" verbatim, and that what we do. In the former case,
219 we do our best to give protoize something nice to insert.
221 This "something nice" should be something that is still valid (when
222 re-compiled) but something that can clearly indicate to the user that
223 more typing information (for the parameter list) should be added (by
224 hand) at some convenient moment.
226 The string chosen here is a comment with question marks in it. */
230 if (TYPE_ARG_TYPES (fntype))
231 /* assert (TREE_VALUE (TYPE_ARG_TYPES (fntype)) == void_type_node); */
232 formal_list = "void";
234 formal_list = "/* ??? */";
238 /* If there were at least some parameters, and if the formals-types-list
239 petered out to a NULL (i.e. without being terminated by a
240 void_type_node) then we need to tack on an ellipsis. */
242 formal_list = concat (formal_list, ", ...", NULL_PTR);
245 return concat (" (", formal_list, ")", NULL_PTR);
248 /* For the generation of an ANSI prototype for a function definition, we have
249 to look at the formal parameter list of the function's own "type" to
250 determine if the function's formal parameter list should end with an
251 ellipsis. Given a tree node, the following function will return non-zero
252 if the "function type" parameter list should end with an ellipsis. */
255 deserves_ellipsis (fntype)
260 formal_type = TYPE_ARG_TYPES (fntype);
261 while (formal_type && TREE_VALUE (formal_type) != void_type_node)
262 formal_type = TREE_CHAIN (formal_type);
264 /* If there were at least some parameters, and if the formals-types-list
265 petered out to a NULL (i.e. without being terminated by a void_type_node)
266 then we need to tack on an ellipsis. */
268 return (!formal_type && TYPE_ARG_TYPES (fntype));
271 /* Generate a parameter list for a function definition (in some given style).
273 Note that this routine has to be separate (and different) from the code that
274 generates the prototype parameter lists for function declarations, because
275 in the case of a function declaration, all we have to go on is a tree node
276 representing the function's own "function type". This can tell us the types
277 of all of the formal parameters for the function, but it cannot tell us the
278 actual *names* of each of the formal parameters. We need to output those
279 parameter names for each function definition.
281 This routine gets a pointer to a tree node which represents the actual
282 declaration of the given function, and this DECL node has a list of formal
283 parameter (variable) declarations attached to it. These formal parameter
284 (variable) declaration nodes give us the actual names of the formal
285 parameters for the given function definition.
287 This routine returns a string which is the source form for the entire
288 function formal parameter list. */
291 gen_formal_list_for_func_def (fndecl, style)
295 const char *formal_list = "";
298 formal_decl = DECL_ARGUMENTS (fndecl);
301 const char *this_formal;
303 if (*formal_list && ((style == ansi) || (style == k_and_r_names)))
304 formal_list = concat (formal_list, ", ", NULL_PTR);
305 this_formal = gen_decl (formal_decl, 0, style);
306 if (style == k_and_r_decls)
307 formal_list = concat (formal_list, this_formal, "; ", NULL_PTR);
309 formal_list = concat (formal_list, this_formal, NULL_PTR);
310 formal_decl = TREE_CHAIN (formal_decl);
314 if (!DECL_ARGUMENTS (fndecl))
315 formal_list = concat (formal_list, "void", NULL_PTR);
316 if (deserves_ellipsis (TREE_TYPE (fndecl)))
317 formal_list = concat (formal_list, ", ...", NULL_PTR);
319 if ((style == ansi) || (style == k_and_r_names))
320 formal_list = concat (" (", formal_list, ")", NULL_PTR);
324 /* Generate a string which is the source code form for a given type (t). This
325 routine is ugly and complex because the C syntax for declarations is ugly
326 and complex. This routine is straightforward so long as *no* pointer types,
327 array types, or function types are involved.
329 In the simple cases, this routine will return the (string) value which was
330 passed in as the "ret_val" argument. Usually, this starts out either as an
331 empty string, or as the name of the declared item (i.e. the formal function
334 This routine will also return with the global variable "data_type" set to
335 some string value which is the "basic" data-type of the given complete type.
336 This "data_type" string can be concatenated onto the front of the returned
337 string after this routine returns to its caller.
339 In complicated cases involving pointer types, array types, or function
340 types, the C declaration syntax requires an "inside out" approach, i.e. if
341 you have a type which is a "pointer-to-function" type, you need to handle
342 the "pointer" part first, but it also has to be "innermost" (relative to
343 the declaration stuff for the "function" type). Thus, is this case, you
344 must prepend a "(*" and append a ")" to the name of the item (i.e. formal
345 variable). Then you must append and prepend the other info for the
346 "function type" part of the overall type.
348 To handle the "innermost precedence" rules of complicated C declarators, we
349 do the following (in this routine). The input parameter called "ret_val"
350 is treated as a "seed". Each time gen_type is called (perhaps recursively)
351 some additional strings may be appended or prepended (or both) to the "seed"
352 string. If yet another (lower) level of the GCC tree exists for the given
353 type (as in the case of a pointer type, an array type, or a function type)
354 then the (wrapped) seed is passed to a (recursive) invocation of gen_type()
355 this recursive invocation may again "wrap" the (new) seed with yet more
356 declarator stuff, by appending, prepending (or both). By the time the
357 recursion bottoms out, the "seed value" at that point will have a value
358 which is (almost) the complete source version of the declarator (except
359 for the data_type info). Thus, this deepest "seed" value is simply passed
360 back up through all of the recursive calls until it is given (as the return
361 value) to the initial caller of the gen_type() routine. All that remains
362 to do at this point is for the initial caller to prepend the "data_type"
363 string onto the returned "seed". */
366 gen_type (ret_val, t, style)
373 /* If there is a typedef name for this type, use it. */
374 if (TYPE_NAME (t) && TREE_CODE (TYPE_NAME (t)) == TYPE_DECL)
375 data_type = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (t)));
378 switch (TREE_CODE (t))
381 if (TYPE_READONLY (t))
382 ret_val = concat ("const ", ret_val, NULL_PTR);
383 if (TYPE_VOLATILE (t))
384 ret_val = concat ("volatile ", ret_val, NULL_PTR);
386 ret_val = concat ("*", ret_val, NULL_PTR);
388 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE || TREE_CODE (TREE_TYPE (t)) == FUNCTION_TYPE)
389 ret_val = concat ("(", ret_val, ")", NULL_PTR);
391 ret_val = gen_type (ret_val, TREE_TYPE (t), style);
396 if (TYPE_SIZE (t) == 0 || TREE_CODE (TYPE_SIZE (t)) != INTEGER_CST)
397 ret_val = gen_type (concat (ret_val, "[]", NULL_PTR),
398 TREE_TYPE (t), style);
399 else if (int_size_in_bytes (t) == 0)
400 ret_val = gen_type (concat (ret_val, "[0]", NULL_PTR),
401 TREE_TYPE (t), style);
404 int size = (int_size_in_bytes (t) / int_size_in_bytes (TREE_TYPE (t)));
406 sprintf (buff, "[%d]", size);
407 ret_val = gen_type (concat (ret_val, buff, NULL_PTR),
408 TREE_TYPE (t), style);
413 ret_val = gen_type (concat (ret_val,
414 gen_formal_list_for_type (t, style),
416 TREE_TYPE (t), style);
419 case IDENTIFIER_NODE:
420 data_type = IDENTIFIER_POINTER (t);
423 /* The following three cases are complicated by the fact that a
424 user may do something really stupid, like creating a brand new
425 "anonymous" type specification in a formal argument list (or as
426 part of a function return type specification). For example:
428 int f (enum { red, green, blue } color);
430 In such cases, we have no name that we can put into the prototype
431 to represent the (anonymous) type. Thus, we have to generate the
432 whole darn type specification. Yuck! */
436 data_type = IDENTIFIER_POINTER (TYPE_NAME (t));
440 chain_p = TYPE_FIELDS (t);
443 data_type = concat (data_type, gen_decl (chain_p, 0, ansi),
445 chain_p = TREE_CHAIN (chain_p);
446 data_type = concat (data_type, "; ", NULL_PTR);
448 data_type = concat ("{ ", data_type, "}", NULL_PTR);
450 data_type = concat ("struct ", data_type, NULL_PTR);
455 data_type = IDENTIFIER_POINTER (TYPE_NAME (t));
459 chain_p = TYPE_FIELDS (t);
462 data_type = concat (data_type, gen_decl (chain_p, 0, ansi),
464 chain_p = TREE_CHAIN (chain_p);
465 data_type = concat (data_type, "; ", NULL_PTR);
467 data_type = concat ("{ ", data_type, "}", NULL_PTR);
469 data_type = concat ("union ", data_type, NULL_PTR);
474 data_type = IDENTIFIER_POINTER (TYPE_NAME (t));
478 chain_p = TYPE_VALUES (t);
481 data_type = concat (data_type,
482 IDENTIFIER_POINTER (TREE_PURPOSE (chain_p)), NULL_PTR);
483 chain_p = TREE_CHAIN (chain_p);
485 data_type = concat (data_type, ", ", NULL_PTR);
487 data_type = concat ("{ ", data_type, " }", NULL_PTR);
489 data_type = concat ("enum ", data_type, NULL_PTR);
493 data_type = IDENTIFIER_POINTER (DECL_NAME (t));
497 data_type = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (t)));
498 /* Normally, `unsigned' is part of the deal. Not so if it comes
499 with a type qualifier. */
500 if (TREE_UNSIGNED (t) && TYPE_QUALS (t))
501 data_type = concat ("unsigned ", data_type, NULL_PTR);
505 data_type = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (t)));
513 data_type = "[ERROR]";
520 if (TYPE_READONLY (t))
521 ret_val = concat ("const ", ret_val, NULL_PTR);
522 if (TYPE_VOLATILE (t))
523 ret_val = concat ("volatile ", ret_val, NULL_PTR);
524 if (TYPE_RESTRICT (t))
525 ret_val = concat ("restrict ", ret_val, NULL_PTR);
529 /* Generate a string (source) representation of an entire entity declaration
530 (using some particular style for function types).
532 The given entity may be either a variable or a function.
534 If the "is_func_definition" parameter is non-zero, assume that the thing
535 we are generating a declaration for is a FUNCTION_DECL node which is
536 associated with a function definition. In this case, we can assume that
537 an attached list of DECL nodes for function formal arguments is present. */
540 gen_decl (decl, is_func_definition, style)
542 int is_func_definition;
547 if (DECL_NAME (decl))
548 ret_val = IDENTIFIER_POINTER (DECL_NAME (decl));
552 /* If we are just generating a list of names of formal parameters, we can
553 simply return the formal parameter name (with no typing information
554 attached to it) now. */
556 if (style == k_and_r_names)
559 /* Note that for the declaration of some entity (either a function or a
560 data object, like for instance a parameter) if the entity itself was
561 declared as either const or volatile, then const and volatile properties
562 are associated with just the declaration of the entity, and *not* with
563 the `type' of the entity. Thus, for such declared entities, we have to
564 generate the qualifiers here. */
566 if (TREE_THIS_VOLATILE (decl))
567 ret_val = concat ("volatile ", ret_val, NULL_PTR);
568 if (TREE_READONLY (decl))
569 ret_val = concat ("const ", ret_val, NULL_PTR);
573 /* For FUNCTION_DECL nodes, there are two possible cases here. First, if
574 this FUNCTION_DECL node was generated from a function "definition", then
575 we will have a list of DECL_NODE's, one for each of the function's formal
576 parameters. In this case, we can print out not only the types of each
577 formal, but also each formal's name. In the second case, this
578 FUNCTION_DECL node came from an actual function declaration (and *not*
579 a definition). In this case, we do nothing here because the formal
580 argument type-list will be output later, when the "type" of the function
581 is added to the string we are building. Note that the ANSI-style formal
582 parameter list is considered to be a (suffix) part of the "type" of the
585 if (TREE_CODE (decl) == FUNCTION_DECL && is_func_definition)
587 ret_val = concat (ret_val, gen_formal_list_for_func_def (decl, ansi),
590 /* Since we have already added in the formals list stuff, here we don't
591 add the whole "type" of the function we are considering (which
592 would include its parameter-list info), rather, we only add in
593 the "type" of the "type" of the function, which is really just
594 the return-type of the function (and does not include the parameter
597 ret_val = gen_type (ret_val, TREE_TYPE (TREE_TYPE (decl)), style);
600 ret_val = gen_type (ret_val, TREE_TYPE (decl), style);
602 ret_val = affix_data_type (ret_val);
604 if (TREE_CODE (decl) != FUNCTION_DECL && DECL_REGISTER (decl))
605 ret_val = concat ("register ", ret_val, NULL_PTR);
606 if (TREE_PUBLIC (decl))
607 ret_val = concat ("extern ", ret_val, NULL_PTR);
608 if (TREE_CODE (decl) == FUNCTION_DECL && !TREE_PUBLIC (decl))
609 ret_val = concat ("static ", ret_val, NULL_PTR);
614 extern FILE *aux_info_file;
616 /* Generate and write a new line of info to the aux-info (.X) file. This
617 routine is called once for each function declaration, and once for each
618 function definition (even the implicit ones). */
621 gen_aux_info_record (fndecl, is_definition, is_implicit, is_prototyped)
627 if (flag_gen_aux_info)
629 static int compiled_from_record = 0;
631 /* Each output .X file must have a header line. Write one now if we
632 have not yet done so. */
634 if (! compiled_from_record++)
636 /* The first line tells which directory file names are relative to.
637 Currently, -aux-info works only for files in the working
638 directory, so just use a `.' as a placeholder for now. */
639 fprintf (aux_info_file, "/* compiled from: . */\n");
642 /* Write the actual line of auxiliary info. */
644 fprintf (aux_info_file, "/* %s:%d:%c%c */ %s;",
645 DECL_SOURCE_FILE (fndecl),
646 DECL_SOURCE_LINE (fndecl),
647 (is_implicit) ? 'I' : (is_prototyped) ? 'N' : 'O',
648 (is_definition) ? 'F' : 'C',
649 gen_decl (fndecl, is_definition, ansi));
651 /* If this is an explicit function declaration, we need to also write
652 out an old-style (i.e. K&R) function header, just in case the user
653 wants to run unprotoize. */
657 fprintf (aux_info_file, " /*%s %s*/",
658 gen_formal_list_for_func_def (fndecl, k_and_r_names),
659 gen_formal_list_for_func_def (fndecl, k_and_r_decls));
662 fprintf (aux_info_file, "\n");