OSDN Git Service

new
[pf3gnuchains/gcc-fork.git] / gcc / c-aux-info.c
1 /* Generate information regarding function declarations and definitions based
2    on information stored in GCC's tree structure.  This code implements the
3    -aux-info option.
4    Copyright (C) 1989, 91, 94, 95, 97, 1998 Free Software Foundation, Inc.
5    Contributed by Ron Guilmette (rfg@segfault.us.com).
6
7 This file is part of GNU CC.
8
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)
12 any later version.
13
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.
18
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.  */
23
24 #include "config.h"
25 #include "system.h"
26 #include "flags.h"
27 #include "tree.h"
28 #include "c-tree.h"
29
30 enum formals_style_enum {
31   ansi,
32   k_and_r_names,
33   k_and_r_decls
34 };
35 typedef enum formals_style_enum formals_style;
36
37
38 static char *data_type;
39
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));
46 \f
47 /* Concatenate a sequence of strings, returning the result.
48
49    This function is based on the one in libiberty.  */
50
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
53    not using libcpp.a */
54 #ifndef USE_CPPLIB
55 char *
56 concat VPROTO((const char *first, ...))
57 {
58   register int length;
59   register char *newstr;
60   register char *end;
61   register const char *arg;
62   va_list args;
63 #ifndef ANSI_PROTOTYPES
64   const char *first;
65 #endif
66
67   /* First compute the size of the result and get sufficient memory.  */
68
69   VA_START (args, first);
70 #ifndef ANSI_PROTOTYPES
71   first = va_arg (args, const char *);
72 #endif
73
74   arg = first;
75   length = 0;
76
77   while (arg != 0)
78     {
79       length += strlen (arg);
80       arg = va_arg (args, const char *);
81     }
82
83   newstr = (char *) malloc (length + 1);
84   va_end (args);
85
86   /* Now copy the individual pieces to the result string.  */
87
88   VA_START (args, first);
89 #ifndef ANSI_PROTOTYPES
90   first = va_arg (args, char *);
91 #endif
92
93   end = newstr;
94   arg = first;
95   while (arg != 0)
96     {
97       while (*arg)
98         *end++ = *arg++;
99       arg = va_arg (args, const char *);
100     }
101   *end = '\000';
102   va_end (args);
103
104   return (newstr);
105 }
106 #endif /* ! USE_CPPLIB */
107
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.
112
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.  */
121
122 static char *
123 affix_data_type (type_or_decl)
124      char *type_or_decl;
125 {
126   char *p = type_or_decl;
127   char *qualifiers_then_data_type;
128   char saved;
129
130   /* Skip as many leading const's or volatile's as there are.  */
131
132   for (;;)
133     {
134       if (!strncmp (p, "volatile ", 9))
135         {
136           p += 9;
137           continue;
138         }
139       if (!strncmp (p, "const ", 6))
140         {
141           p += 6;
142           continue;
143         }
144       break;
145     }
146
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.  */
149
150   if (p == type_or_decl)
151     return concat (data_type, " ", type_or_decl, NULL_PTR);
152
153   saved = *p;
154   *p = '\0';
155   qualifiers_then_data_type = concat (type_or_decl, data_type, NULL_PTR);
156   *p = saved;
157   return concat (qualifiers_then_data_type, " ", p, NULL_PTR);
158 }
159
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.  */
166
167 static char *
168 gen_formal_list_for_type (fntype, style)
169      tree fntype;
170      formals_style style;
171 {
172   char *formal_list = "";
173   tree formal_type;
174
175   if (style != ansi)
176     return "()";
177
178   formal_type = TYPE_ARG_TYPES (fntype);
179   while (formal_type && TREE_VALUE (formal_type) != void_type_node)
180     {
181       char *this_type;
182
183       if (*formal_list)
184         formal_list = concat (formal_list, ", ", NULL_PTR);
185
186       this_type = gen_type ("", TREE_VALUE (formal_type), ansi);
187       formal_list
188         = ((strlen (this_type))
189            ? concat (formal_list, affix_data_type (this_type), NULL_PTR)
190            : concat (formal_list, data_type, NULL_PTR));
191
192       formal_type = TREE_CHAIN (formal_type);
193     }
194
195   /* If we got to here, then we are trying to generate an ANSI style formal
196      parameters list.
197
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
200      only "void".
201
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.
207
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.
211
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.
217
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.
222
223      The string chosen here is a comment with question marks in it.  */
224
225   if (!*formal_list)
226     {
227       if (TYPE_ARG_TYPES (fntype))
228         /* assert (TREE_VALUE (TYPE_ARG_TYPES (fntype)) == void_type_node);  */
229         formal_list = "void";
230       else
231         formal_list = "/* ??? */";
232     }
233   else
234     {
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.  */
238       if (!formal_type)
239         formal_list = concat (formal_list, ", ...", NULL_PTR);
240     }
241
242   return concat (" (", formal_list, ")", NULL_PTR);
243 }
244
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.  */
250
251 static int
252 deserves_ellipsis (fntype)
253      tree fntype;
254 {
255   tree formal_type;
256
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);
260
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.  */
264
265   return (!formal_type && TYPE_ARG_TYPES (fntype));
266 }
267
268 /* Generate a parameter list for a function definition (in some given style).
269
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.
277
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.
283
284    This routine returns a string which is the source form for the entire
285    function formal parameter list.  */
286
287 static char *
288 gen_formal_list_for_func_def (fndecl, style)
289      tree fndecl;
290      formals_style style;
291 {
292   char *formal_list = "";
293   tree formal_decl;
294
295   formal_decl = DECL_ARGUMENTS (fndecl);
296   while (formal_decl)
297     {
298       char *this_formal;
299
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);
305       else
306         formal_list = concat (formal_list, this_formal, NULL_PTR);
307       formal_decl = TREE_CHAIN (formal_decl);
308     }
309   if (style == ansi)
310     {
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);
315     }
316   if ((style == ansi) || (style == k_and_r_names))
317     formal_list = concat (" (", formal_list, ")", NULL_PTR);
318   return formal_list;
319 }
320
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.
325
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
329    parameter variable).
330
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.
335
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.
344
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".  */
361
362 static char *
363 gen_type (ret_val, t, style)
364      char *ret_val;
365      tree t;
366      formals_style style;
367 {
368   tree chain_p;
369
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)));
373   else
374     {
375       switch (TREE_CODE (t))
376         {
377         case POINTER_TYPE:
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);
382
383           ret_val = concat ("*", ret_val, NULL_PTR);
384
385           if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE || TREE_CODE (TREE_TYPE (t)) == FUNCTION_TYPE)
386             ret_val = concat ("(", ret_val, ")", NULL_PTR);
387
388           ret_val = gen_type (ret_val, TREE_TYPE (t), style);
389
390           return ret_val;
391
392         case ARRAY_TYPE:
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);
399           else
400             {
401               int size = (int_size_in_bytes (t) / int_size_in_bytes (TREE_TYPE (t)));
402               char buff[10];
403               sprintf (buff, "[%d]", size);
404               ret_val = gen_type (concat (ret_val, buff, NULL_PTR),
405                                   TREE_TYPE (t), style);
406             }
407           break;
408
409         case FUNCTION_TYPE:
410           ret_val = gen_type (concat (ret_val,
411                                       gen_formal_list_for_type (t, style),
412                                       NULL_PTR),
413                               TREE_TYPE (t), style);
414           break;
415
416         case IDENTIFIER_NODE:
417           data_type = IDENTIFIER_POINTER (t);
418           break;
419
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:
424
425                 int f (enum { red, green, blue } color);
426
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!  */
430
431         case RECORD_TYPE:
432           if (TYPE_NAME (t))
433             data_type = IDENTIFIER_POINTER (TYPE_NAME (t));
434           else
435             {
436               data_type = "";
437               chain_p = TYPE_FIELDS (t);
438               while (chain_p)
439                 {
440                   data_type = concat (data_type, gen_decl (chain_p, 0, ansi),
441                                       NULL_PTR);
442                   chain_p = TREE_CHAIN (chain_p);
443                   data_type = concat (data_type, "; ", NULL_PTR);
444                 }
445               data_type = concat ("{ ", data_type, "}", NULL_PTR);
446             }
447           data_type = concat ("struct ", data_type, NULL_PTR);
448           break;
449
450         case UNION_TYPE:
451           if (TYPE_NAME (t))
452             data_type = IDENTIFIER_POINTER (TYPE_NAME (t));
453           else
454             {
455               data_type = "";
456               chain_p = TYPE_FIELDS (t);
457               while (chain_p)
458                 {
459                   data_type = concat (data_type, gen_decl (chain_p, 0, ansi),
460                                       NULL_PTR);
461                   chain_p = TREE_CHAIN (chain_p);
462                   data_type = concat (data_type, "; ", NULL_PTR);
463                 }
464               data_type = concat ("{ ", data_type, "}", NULL_PTR);
465             }
466           data_type = concat ("union ", data_type, NULL_PTR);
467           break;
468
469         case ENUMERAL_TYPE:
470           if (TYPE_NAME (t))
471             data_type = IDENTIFIER_POINTER (TYPE_NAME (t));
472           else
473             {
474               data_type = "";
475               chain_p = TYPE_VALUES (t);
476               while (chain_p)
477                 {
478                   data_type = concat (data_type,
479                         IDENTIFIER_POINTER (TREE_PURPOSE (chain_p)), NULL_PTR);
480                   chain_p = TREE_CHAIN (chain_p);
481                   if (chain_p)
482                     data_type = concat (data_type, ", ", NULL_PTR);
483                 }
484               data_type = concat ("{ ", data_type, " }", NULL_PTR);
485             }
486           data_type = concat ("enum ", data_type, NULL_PTR);
487           break;
488
489         case TYPE_DECL:
490           data_type = IDENTIFIER_POINTER (DECL_NAME (t));
491           break;
492  
493         case INTEGER_TYPE:
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);
499           break;
500
501         case REAL_TYPE:
502           data_type = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (t)));
503           break;
504
505         case VOID_TYPE:
506           data_type = "void";
507           break;
508
509         case ERROR_MARK:
510           data_type = "[ERROR]";
511           break;
512
513         default:
514           abort ();
515         }
516     }
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);
523   return ret_val;
524 }
525
526 /* Generate a string (source) representation of an entire entity declaration
527    (using some particular style for function types).
528
529    The given entity may be either a variable or a function.
530
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.  */
535
536 static char *
537 gen_decl (decl, is_func_definition, style)
538      tree decl;
539      int is_func_definition;
540      formals_style style;
541 {
542   char *ret_val;
543
544   if (DECL_NAME (decl))
545     ret_val = IDENTIFIER_POINTER (DECL_NAME (decl));
546   else
547     ret_val = "";
548
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.  */
552
553   if (style == k_and_r_names)
554     return ret_val;
555
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.  */
562
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);
567
568   data_type = "";
569
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
580      function.  */
581
582   if (TREE_CODE (decl) == FUNCTION_DECL && is_func_definition)
583     {
584       ret_val = concat (ret_val, gen_formal_list_for_func_def (decl, ansi),
585                         NULL_PTR);
586
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
592          list info).  */
593
594       ret_val = gen_type (ret_val, TREE_TYPE (TREE_TYPE (decl)), style);
595     }
596   else
597     ret_val = gen_type (ret_val, TREE_TYPE (decl), style);
598
599   ret_val = affix_data_type (ret_val);
600
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);
607
608   return ret_val;
609 }
610
611 extern FILE *aux_info_file;
612
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).  */
616
617 void
618 gen_aux_info_record (fndecl, is_definition, is_implicit, is_prototyped)
619      tree fndecl;
620      int is_definition;
621      int is_implicit;
622      int is_prototyped;
623 {
624   if (flag_gen_aux_info)
625     {
626       static int compiled_from_record = 0;
627
628       /* Each output .X file must have a header line.  Write one now if we
629          have not yet done so.  */
630
631       if (! compiled_from_record++)
632         {
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");
637         }
638
639       /* Write the actual line of auxiliary info.  */
640
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));
647
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.  */
651
652       if (is_definition)
653         {
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));
657         }
658
659       fprintf (aux_info_file, "\n");
660     }
661 }