1 /* Generate information regarding function declarations and definitions based
2 on information stored in GCC's tree structure. This code implements the
5 This code was written by Ron Guilmette (rfg@mcc.com).
7 Copyright (C) 1989, 1991 Free Software Foundation, Inc.
9 This file is part of GNU CC.
11 GNU CC is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 2, or (at your option)
16 GNU CC is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with GNU CC; see the file COPYING. If not, write to
23 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
26 #include <sys/param.h>
37 extern char* xmalloc ();
39 enum formals_style_enum {
44 typedef enum formals_style_enum formals_style;
47 static char* data_type;
49 static char * concat ();
50 static char * concat3 ();
51 static char * gen_formal_list_for_type ();
52 static int deserves_ellipsis ();
53 static char * gen_formal_list_for_func_def ();
54 static char * gen_type ();
55 static char * gen_decl ();
56 void gen_aux_info_record ();
58 /* Virtually every UN*X system now in common use (except for pre-4.3-tahoe
59 BSD systems) now provides getcwd as called for by POSIX. Allow for
60 the few exceptions to the general rule here. */
62 #if !(defined (USG) || defined (VMS))
63 extern char *getwd ();
64 #define getcwd(buf,len) getwd(buf)
65 #define GUESSPATHLEN (MAXPATHLEN + 1)
66 #else /* (defined (USG) || defined (VMS)) */
67 extern char *getcwd ();
68 /* We actually use this as a starting point, not a limit. */
69 #define GUESSPATHLEN 100
70 #endif /* (defined (USG) || defined (VMS)) */
72 /* Take two strings and mash them together into a newly allocated area. */
89 ret_val = xmalloc (size1 + size2 + 1);
91 strcpy (&ret_val[size1], s2);
95 /* Take three strings and mash them together into a newly allocated area. */
103 int size1, size2, size3;
116 ret_val = xmalloc (size1 + size2 + size3 + 1);
117 strcpy (ret_val, s1);
118 strcpy (&ret_val[size1], s2);
119 strcpy (&ret_val[size1+size2], s3);
123 /* Given a string representing an entire type or an entire declaration
124 which only lacks the actual "data-type" specifier (at its left end),
125 affix the data-type specifier to the left end of the given type
126 specification or object declaration.
128 Because of C language weirdness, the data-type specifier (which normally
129 goes in at the very left end) may have to be slipped in just to the
130 right of any leading "const" or "volatile" qualifiers (there may be more
131 than one). Actually this may not be strictly necessary because it seems
132 that GCC (at least) accepts `<data-type> const foo;' and treats it the
133 same as `const <data-type> foo;' but people are accustomed to seeing
134 `const char *foo;' and *not* `char const *foo;' so we try to create types
135 that look as expected. */
138 affix_data_type (type_or_decl)
141 char *p = type_or_decl;
142 char *qualifiers_then_data_type;
145 /* Skip as many leading const's or volatile's as there are. */
149 if (!strncmp (p, "volatile", 8))
154 if (!strncmp (p, "const", 5))
162 /* p now points to the place where we can insert the data type. We have to
163 add a blank after the data-type of course. */
165 if (p == type_or_decl)
166 return concat3 (data_type, " ", type_or_decl);
170 qualifiers_then_data_type = concat (type_or_decl, data_type);
172 return concat3 (qualifiers_then_data_type, " ", p);
175 /* Given a tree node which represents some "function type", generate the
176 source code version of a formal parameter list (of some given style) for
177 this function type. Return the whole formal parameter list (including
178 a pair of surrounding parens) as a string. Note that if the style
179 we are currently aiming for is non-ansi, then we just return a pair
180 of empty parens here. */
183 gen_formal_list_for_type (fntype, style)
187 char* formal_list = "";
193 formal_type = TYPE_ARG_TYPES (fntype);
194 while (formal_type && TREE_VALUE (formal_type) != void_type_node)
199 formal_list = concat (formal_list, ", ");
201 this_type = gen_type ("", TREE_VALUE (formal_type), ansi);
204 ? concat (formal_list, affix_data_type (this_type))
205 : concat (formal_list, data_type);
207 formal_type = TREE_CHAIN (formal_type);
210 /* If we got to here, then we are trying to generate an ANSI style formal
213 New style prototyped ANSI formal parameter lists should in theory always
214 contain some stuff between the opening and closing parens, even if it is
217 The brutal truth though is that there is lots of old K&R code out there
218 which contains declarations of "pointer-to-function" parameters and
219 these almost never have fully specified formal parameter lists associated
220 with them. That is, the pointer-to-function parameters are declared
221 with just empty parameter lists.
223 In cases such as these, protoize should really insert *something* into
224 the vacant parameter lists, but what? It has no basis on which to insert
225 anything in particular.
227 Here, we make life easy for protoize by trying to distinguish between
228 K&R empty parameter lists and new-style prototyped parameter lists
229 that actually contain "void". In the latter case we (obviously) want
230 to output the "void" verbatim, and that what we do. In the former case,
231 we do our best to give protoize something nice to insert.
233 This "something nice" should be something that is still legal (when
234 re-compiled) but something that can clearly indicate to the user that
235 more typing information (for the parameter list) should be added (by
236 hand) at some convenient moment.
238 The string chosen here is a comment with question marks in it. */
242 if (TYPE_ARG_TYPES (fntype))
243 /* assert (TREE_VALUE (TYPE_ARG_TYPES (fntype)) == void_type_node); */
244 formal_list = "void";
246 formal_list = "/* ??? */";
250 /* If there were at least some parameters, and if the formals-types-list
251 petered out to a NULL (i.e. without being terminated by a
252 void_type_node) then we need to tack on an ellipsis. */
254 formal_list = concat (formal_list, ", ...");
257 return concat3 (" (", formal_list, ")");
260 /* For the generation of an ANSI prototype for a function definition, we have
261 to look at the formal parameter list of the function's own "type" to
262 determine if the function's formal parameter list should end with an
263 ellipsis. Given a tree node, the following function will return non-zero
264 if the "function type" parameter list should end with an ellipsis. */
267 deserves_ellipsis (fntype)
272 formal_type = TYPE_ARG_TYPES (fntype);
273 while (formal_type && TREE_VALUE (formal_type) != void_type_node)
274 formal_type = TREE_CHAIN (formal_type);
276 /* If there were at least some parameters, and if the formals-types-list
277 petered out to a NULL (i.e. without being terminated by a void_type_node)
278 then we need to tack on an ellipsis. */
280 return (!formal_type && TYPE_ARG_TYPES (fntype));
283 /* Generate a parameter list for a function definition (in some given style).
285 Note that this routine has to be separate (and different) from the code that
286 generates the prototype parameter lists for function declarations, because
287 in the case of a function declaration, all we have to go on is a tree node
288 representing the function's own "function type". This can tell us the types
289 of all of the formal parameters for the function, but it cannot tell us the
290 actual *names* of each of the formal parameters. We need to output those
291 parameter names for each function definition.
293 This routine gets a pointer to a tree node which represents the actual
294 declaration of the given function, and this DECL node has a list of formal
295 parameter (variable) declarations attached to it. These formal parameter
296 (variable) declaration nodes give us the actual names of the formal
297 parameters for the given function definition.
299 This routine returns a string which is the source form for the entire
300 function formal parameter list. */
303 gen_formal_list_for_func_def (fndecl, style)
307 char* formal_list = "";
310 formal_decl = DECL_ARGUMENTS (fndecl);
315 if (*formal_list && ((style == ansi) || (style == k_and_r_names)))
316 formal_list = concat (formal_list, ", ");
317 this_formal = gen_decl (formal_decl, 0, style);
318 if (style == k_and_r_decls)
319 formal_list = concat3 (formal_list, this_formal, "; ");
321 formal_list = concat (formal_list, this_formal);
322 formal_decl = TREE_CHAIN (formal_decl);
326 if (!DECL_ARGUMENTS (fndecl))
327 formal_list = concat (formal_list, "void");
328 if (deserves_ellipsis (TREE_TYPE (fndecl)))
329 formal_list = concat (formal_list, ", ...");
331 if ((style == ansi) || (style == k_and_r_names))
332 formal_list = concat3 (" (", formal_list, ")");
336 /* Generate a string which is the source code form for a given type (t). This
337 routine is ugly and complex because the C syntax for declarations is ugly
338 and complex. This routine is straightforward so long as *no* pointer types,
339 array types, or function types are involved.
341 In the simple cases, this routine will return the (string) value which was
342 passed in as the "ret_val" argument. Usually, this starts out either as an
343 empty string, or as the name of the declared item (i.e. the formal function
346 This routine will also return with the global variable "data_type" set to
347 some string value which is the "basic" data-type of the given complete type.
348 This "data_type" string can be concatenated onto the front of the returned
349 string after this routine returns to its caller.
351 In complicated cases involving pointer types, array types, or function
352 types, the C declaration syntax requires an "inside out" approach, i.e. if
353 you have a type which is a "pointer-to-function" type, you need to handle
354 the "pointer" part first, but it also has to be "innermost" (relative to
355 the declaration stuff for the "function" type). Thus, is this case, you
356 must prepend a "(*" and append a ")" to the name of the item (i.e. formal
357 variable). Then you must append and prepend the other info for the
358 "function type" part of the overall type.
360 To handle the "innermost precedence" rules of complicated C declarators, we
361 do the following (in this routine). The input parameter called "ret_val"
362 is treated as a "seed". Each time gen_type is called (perhaps recursively)
363 some additional strings may be appended or prepended (or both) to the "seed"
364 string. If yet another (lower) level of the GCC tree exists for the given
365 type (as in the case of a pointer type, an array type, or a function type)
366 then the (wrapped) seed is passed to a (recursive) invocation of gen_type()
367 this recursive invocation may again "wrap" the (new) seed with yet more
368 declarator stuff, by appending, prepending (or both). By the time the
369 recursion bottoms out, the "seed value" at that point will have a value
370 which is (almost) the complete source version of the declarator (except
371 for the data_type info). Thus, this deepest "seed" value is simply passed
372 back up through all of the recursive calls until it is given (as the return
373 value) to the initial caller of the gen_type() routine. All that remains
374 to do at this point is for the initial caller to prepend the "data_type"
375 string onto the returned "seed". */
378 gen_type (ret_val, t, style)
385 if (TYPE_NAME (t) && DECL_NAME (TYPE_NAME (t)))
386 data_type = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (t)));
389 switch (TREE_CODE (t))
392 if (TYPE_READONLY (t))
393 ret_val = concat ("const ", ret_val);
394 if (TYPE_VOLATILE (t))
395 ret_val = concat ("volatile ", ret_val);
397 ret_val = concat ("*", ret_val);
399 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE || TREE_CODE (TREE_TYPE (t)) == FUNCTION_TYPE)
400 ret_val = concat3 ("(", ret_val, ")");
402 ret_val = gen_type (ret_val, TREE_TYPE (t), style);
407 ret_val = gen_type (concat (ret_val, "[]"), TREE_TYPE (t), style);
411 ret_val = gen_type (concat (ret_val, gen_formal_list_for_type (t, style)), TREE_TYPE (t), style);
414 case IDENTIFIER_NODE:
415 data_type = IDENTIFIER_POINTER (t);
418 /* The following three cases are complicated by the fact that a
419 user may do something really stupid, like creating a brand new
420 "anonymous" type specification in a formal argument list (or as
421 part of a function return type specification). For example:
423 int f (enum { red, green, blue } color);
425 In such cases, we have no name that we can put into the prototype
426 to represent the (anonymous) type. Thus, we have to generate the
427 whole darn type specification. Yuck! */
431 data_type = IDENTIFIER_POINTER (TYPE_NAME (t));
435 chain_p = TYPE_FIELDS (t);
438 data_type = concat (data_type, gen_decl (chain_p, 0, ansi));
439 chain_p = TREE_CHAIN (chain_p);
440 data_type = concat (data_type, "; ");
442 data_type = concat3 ("{ ", data_type, "}");
444 data_type = concat ("struct ", data_type);
449 data_type = IDENTIFIER_POINTER (TYPE_NAME (t));
453 chain_p = TYPE_FIELDS (t);
456 data_type = concat (data_type, gen_decl (chain_p, 0, ansi));
457 chain_p = TREE_CHAIN (chain_p);
458 data_type = concat (data_type, "; ");
460 data_type = concat3 ("{ ", data_type, "}");
462 data_type = concat ("union ", data_type);
467 data_type = IDENTIFIER_POINTER (TYPE_NAME (t));
471 chain_p = TYPE_VALUES (t);
474 data_type = concat (data_type,
475 IDENTIFIER_POINTER (TREE_PURPOSE (chain_p)));
476 chain_p = TREE_CHAIN (chain_p);
478 data_type = concat (data_type, ", ");
480 data_type = concat3 ("{ ", data_type, " }");
482 data_type = concat ("enum ", data_type);
486 data_type = IDENTIFIER_POINTER (DECL_NAME (t));
490 data_type = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (t)));
491 /* Normally, `unsigned' is part of the deal. Not so if it comes
492 with `const' or `volatile'. */
493 if (TREE_UNSIGNED (t) && (TYPE_READONLY (t) || TYPE_VOLATILE (t)))
494 data_type = concat ("unsigned ", data_type);
498 data_type = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (t)));
509 if (TYPE_READONLY (t))
510 ret_val = concat ("const ", ret_val);
511 if (TYPE_VOLATILE (t))
512 ret_val = concat ("volatile ", ret_val);
516 /* Generate a string (source) representation of an entire entity declaration
517 (using some particular style for function types).
519 The given entity may be either a variable or a function.
521 If the "is_func_definition" parameter is non-zero, assume that the thing
522 we are generating a declaration for is a FUNCTION_DECL node which is
523 associated with a function definition. In this case, we can assume that
524 an attached list of DECL nodes for function formal arguments is present. */
527 gen_decl (decl, is_func_definition, style)
529 int is_func_definition;
533 char* outer_modifier = "";
535 if (DECL_NAME (decl))
536 ret_val = IDENTIFIER_POINTER (DECL_NAME (decl));
540 /* If we are just generating a list of names of formal parameters, we can
541 simply return the formal parameter name (with no typing information
542 attached to it) now. */
544 if (style == k_and_r_names)
547 /* Note that for the declaration of some entity (either a function or a
548 data object, like for instance a parameter) if the entity itself was
549 declared as either const or volatile, then const and volatile properties
550 are associated with just the declaration of the entity, and *not* with
551 the `type' of the entity. Thus, for such declared entities, we have to
552 generate the qualifiers here. */
554 if (TREE_THIS_VOLATILE (decl))
555 ret_val = concat ("volatile ", ret_val);
556 if (TREE_READONLY (decl))
557 ret_val = concat ("const ", ret_val);
561 /* For FUNCTION_DECL nodes, there are two possible cases here. First, if
562 this FUNCTION_DECL node was generated from a function "definition", then
563 we will have a list of DECL_NODE's, one for each of the function's formal
564 parameters. In this case, we can print out not only the types of each
565 formal, but also each formal's name. In the second case, this
566 FUNCTION_DECL node came from an actual function declaration (and *not*
567 a definition). In this case, we do nothing here because the formal
568 argument type-list will be output later, when the "type" of the function
569 is added to the string we are building. Note that the ANSI-style formal
570 parameter list is considered to be a (suffix) part of the "type" of the
573 if (TREE_CODE (decl) == FUNCTION_DECL && is_func_definition)
575 ret_val = concat (ret_val, gen_formal_list_for_func_def (decl, ansi));
577 /* Since we have already added in the formals list stuff, here we don't
578 add the whole "type" of the function we are considering (which
579 would include its parameter-list info), rather, we only add in
580 the "type" of the "type" of the function, which is really just
581 the return-type of the function (and does not include the parameter
584 ret_val = gen_type (ret_val, TREE_TYPE (TREE_TYPE (decl)), style);
587 ret_val = gen_type (ret_val, TREE_TYPE (decl), style);
589 ret_val = affix_data_type (ret_val);
591 if (TREE_REGDECL (decl))
592 ret_val = concat ("register ", ret_val);
593 if (TREE_PUBLIC (decl))
594 ret_val = concat ("extern ", ret_val);
595 if (TREE_CODE (decl) == FUNCTION_DECL && !TREE_PUBLIC (decl))
596 ret_val = concat ("static ", ret_val);
601 extern FILE* aux_info_file;
603 /* Generate and write a new line of info to the aux-info (.X) file. This
604 routine is called once for each function declaration, and once for each
605 function definition (even the implicit ones). */
608 gen_aux_info_record (fndecl, is_definition, is_implicit, is_prototyped)
614 if (flag_gen_aux_info)
616 static int compiled_from_record = 0;
618 /* Each output .X file must have a header line. Write one now if we
619 have not yet done so. */
621 if (! compiled_from_record++)
627 /* Read the working directory, avoiding arbitrary limit. */
631 wd = (char *) xmalloc (size);
632 value = getcwd (wd, size);
633 if (value != 0 || errno != ERANGE)
640 fprintf (aux_info_file, "/* compiled from: %s */\n", wd);
643 /* Write the actual line of auxiliary info. */
645 fprintf (aux_info_file, "/* %s:%d:%c%c */ %s;",
646 DECL_SOURCE_FILE (fndecl),
647 DECL_SOURCE_LINE (fndecl),
648 (is_implicit) ? 'I' : (is_prototyped) ? 'N' : 'O',
649 (is_definition) ? 'F' : 'C',
650 gen_decl (fndecl, is_definition, ansi));
652 /* If this is an explicit function declaration, we need to also write
653 out an old-style (i.e. K&R) function header, just in case the user
654 wants to run unprotoize. */
658 fprintf (aux_info_file, " /*%s %s*/",
659 gen_formal_list_for_func_def (fndecl, k_and_r_names),
660 gen_formal_list_for_func_def (fndecl, k_and_r_decls));
663 fprintf (aux_info_file, "\n");