OSDN Git Service

* collect2.c (find_a_file): Use HAVE_DOS_BASED_FILE_SYSTEM in place
[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-98, 1999 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 const char *data_type;
39
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));
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 (param)
124      const char *param;
125 {
126   char *type_or_decl = (char *) alloca (strlen (param) + 1);
127   char *p = type_or_decl;
128   char *qualifiers_then_data_type;
129   char saved;
130
131   strcpy (type_or_decl, param);
132   
133   /* Skip as many leading const's or volatile's as there are.  */
134
135   for (;;)
136     {
137       if (!strncmp (p, "volatile ", 9))
138         {
139           p += 9;
140           continue;
141         }
142       if (!strncmp (p, "const ", 6))
143         {
144           p += 6;
145           continue;
146         }
147       break;
148     }
149
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.  */
152
153   if (p == type_or_decl)
154     return concat (data_type, " ", type_or_decl, NULL_PTR);
155
156   saved = *p;
157   *p = '\0';
158   qualifiers_then_data_type = concat (type_or_decl, data_type, NULL_PTR);
159   *p = saved;
160   return concat (qualifiers_then_data_type, " ", p, NULL_PTR);
161 }
162
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.  */
169
170 static const char *
171 gen_formal_list_for_type (fntype, style)
172      tree fntype;
173      formals_style style;
174 {
175   const char *formal_list = "";
176   tree formal_type;
177
178   if (style != ansi)
179     return "()";
180
181   formal_type = TYPE_ARG_TYPES (fntype);
182   while (formal_type && TREE_VALUE (formal_type) != void_type_node)
183     {
184       const char *this_type;
185
186       if (*formal_list)
187         formal_list = concat (formal_list, ", ", NULL_PTR);
188
189       this_type = gen_type ("", TREE_VALUE (formal_type), ansi);
190       formal_list
191         = ((strlen (this_type))
192            ? concat (formal_list, affix_data_type (this_type), NULL_PTR)
193            : concat (formal_list, data_type, NULL_PTR));
194
195       formal_type = TREE_CHAIN (formal_type);
196     }
197
198   /* If we got to here, then we are trying to generate an ANSI style formal
199      parameters list.
200
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
203      only "void".
204
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.
210
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.
214
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.
220
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.
225
226      The string chosen here is a comment with question marks in it.  */
227
228   if (!*formal_list)
229     {
230       if (TYPE_ARG_TYPES (fntype))
231         /* assert (TREE_VALUE (TYPE_ARG_TYPES (fntype)) == void_type_node);  */
232         formal_list = "void";
233       else
234         formal_list = "/* ??? */";
235     }
236   else
237     {
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.  */
241       if (!formal_type)
242         formal_list = concat (formal_list, ", ...", NULL_PTR);
243     }
244
245   return concat (" (", formal_list, ")", NULL_PTR);
246 }
247
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.  */
253
254 static int
255 deserves_ellipsis (fntype)
256      tree fntype;
257 {
258   tree formal_type;
259
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);
263
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.  */
267
268   return (!formal_type && TYPE_ARG_TYPES (fntype));
269 }
270
271 /* Generate a parameter list for a function definition (in some given style).
272
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.
280
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.
286
287    This routine returns a string which is the source form for the entire
288    function formal parameter list.  */
289
290 static const char *
291 gen_formal_list_for_func_def (fndecl, style)
292      tree fndecl;
293      formals_style style;
294 {
295   const char *formal_list = "";
296   tree formal_decl;
297
298   formal_decl = DECL_ARGUMENTS (fndecl);
299   while (formal_decl)
300     {
301       const char *this_formal;
302
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);
308       else
309         formal_list = concat (formal_list, this_formal, NULL_PTR);
310       formal_decl = TREE_CHAIN (formal_decl);
311     }
312   if (style == ansi)
313     {
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);
318     }
319   if ((style == ansi) || (style == k_and_r_names))
320     formal_list = concat (" (", formal_list, ")", NULL_PTR);
321   return formal_list;
322 }
323
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.
328
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
332    parameter variable).
333
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.
338
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.
347
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".  */
364
365 static const char *
366 gen_type (ret_val, t, style)
367      const char *ret_val;
368      tree t;
369      formals_style style;
370 {
371   tree chain_p;
372
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)));
376   else
377     {
378       switch (TREE_CODE (t))
379         {
380         case POINTER_TYPE:
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);
385
386           ret_val = concat ("*", ret_val, NULL_PTR);
387
388           if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE || TREE_CODE (TREE_TYPE (t)) == FUNCTION_TYPE)
389             ret_val = concat ("(", ret_val, ")", NULL_PTR);
390
391           ret_val = gen_type (ret_val, TREE_TYPE (t), style);
392
393           return ret_val;
394
395         case ARRAY_TYPE:
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);
402           else
403             {
404               int size = (int_size_in_bytes (t) / int_size_in_bytes (TREE_TYPE (t)));
405               char buff[10];
406               sprintf (buff, "[%d]", size);
407               ret_val = gen_type (concat (ret_val, buff, NULL_PTR),
408                                   TREE_TYPE (t), style);
409             }
410           break;
411
412         case FUNCTION_TYPE:
413           ret_val = gen_type (concat (ret_val,
414                                       gen_formal_list_for_type (t, style),
415                                       NULL_PTR),
416                               TREE_TYPE (t), style);
417           break;
418
419         case IDENTIFIER_NODE:
420           data_type = IDENTIFIER_POINTER (t);
421           break;
422
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:
427
428                 int f (enum { red, green, blue } color);
429
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!  */
433
434         case RECORD_TYPE:
435           if (TYPE_NAME (t))
436             data_type = IDENTIFIER_POINTER (TYPE_NAME (t));
437           else
438             {
439               data_type = "";
440               chain_p = TYPE_FIELDS (t);
441               while (chain_p)
442                 {
443                   data_type = concat (data_type, gen_decl (chain_p, 0, ansi),
444                                       NULL_PTR);
445                   chain_p = TREE_CHAIN (chain_p);
446                   data_type = concat (data_type, "; ", NULL_PTR);
447                 }
448               data_type = concat ("{ ", data_type, "}", NULL_PTR);
449             }
450           data_type = concat ("struct ", data_type, NULL_PTR);
451           break;
452
453         case UNION_TYPE:
454           if (TYPE_NAME (t))
455             data_type = IDENTIFIER_POINTER (TYPE_NAME (t));
456           else
457             {
458               data_type = "";
459               chain_p = TYPE_FIELDS (t);
460               while (chain_p)
461                 {
462                   data_type = concat (data_type, gen_decl (chain_p, 0, ansi),
463                                       NULL_PTR);
464                   chain_p = TREE_CHAIN (chain_p);
465                   data_type = concat (data_type, "; ", NULL_PTR);
466                 }
467               data_type = concat ("{ ", data_type, "}", NULL_PTR);
468             }
469           data_type = concat ("union ", data_type, NULL_PTR);
470           break;
471
472         case ENUMERAL_TYPE:
473           if (TYPE_NAME (t))
474             data_type = IDENTIFIER_POINTER (TYPE_NAME (t));
475           else
476             {
477               data_type = "";
478               chain_p = TYPE_VALUES (t);
479               while (chain_p)
480                 {
481                   data_type = concat (data_type,
482                         IDENTIFIER_POINTER (TREE_PURPOSE (chain_p)), NULL_PTR);
483                   chain_p = TREE_CHAIN (chain_p);
484                   if (chain_p)
485                     data_type = concat (data_type, ", ", NULL_PTR);
486                 }
487               data_type = concat ("{ ", data_type, " }", NULL_PTR);
488             }
489           data_type = concat ("enum ", data_type, NULL_PTR);
490           break;
491
492         case TYPE_DECL:
493           data_type = IDENTIFIER_POINTER (DECL_NAME (t));
494           break;
495  
496         case INTEGER_TYPE:
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);
502           break;
503
504         case REAL_TYPE:
505           data_type = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (t)));
506           break;
507
508         case VOID_TYPE:
509           data_type = "void";
510           break;
511
512         case ERROR_MARK:
513           data_type = "[ERROR]";
514           break;
515
516         default:
517           abort ();
518         }
519     }
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);
526   return ret_val;
527 }
528
529 /* Generate a string (source) representation of an entire entity declaration
530    (using some particular style for function types).
531
532    The given entity may be either a variable or a function.
533
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.  */
538
539 static const char *
540 gen_decl (decl, is_func_definition, style)
541      tree decl;
542      int is_func_definition;
543      formals_style style;
544 {
545   const char *ret_val;
546
547   if (DECL_NAME (decl))
548     ret_val = IDENTIFIER_POINTER (DECL_NAME (decl));
549   else
550     ret_val = "";
551
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.  */
555
556   if (style == k_and_r_names)
557     return ret_val;
558
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.  */
565
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);
570
571   data_type = "";
572
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
583      function.  */
584
585   if (TREE_CODE (decl) == FUNCTION_DECL && is_func_definition)
586     {
587       ret_val = concat (ret_val, gen_formal_list_for_func_def (decl, ansi),
588                         NULL_PTR);
589
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
595          list info).  */
596
597       ret_val = gen_type (ret_val, TREE_TYPE (TREE_TYPE (decl)), style);
598     }
599   else
600     ret_val = gen_type (ret_val, TREE_TYPE (decl), style);
601
602   ret_val = affix_data_type (ret_val);
603
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);
610
611   return ret_val;
612 }
613
614 extern FILE *aux_info_file;
615
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).  */
619
620 void
621 gen_aux_info_record (fndecl, is_definition, is_implicit, is_prototyped)
622      tree fndecl;
623      int is_definition;
624      int is_implicit;
625      int is_prototyped;
626 {
627   if (flag_gen_aux_info)
628     {
629       static int compiled_from_record = 0;
630
631       /* Each output .X file must have a header line.  Write one now if we
632          have not yet done so.  */
633
634       if (! compiled_from_record++)
635         {
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");
640         }
641
642       /* Write the actual line of auxiliary info.  */
643
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));
650
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.  */
654
655       if (is_definition)
656         {
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));
660         }
661
662       fprintf (aux_info_file, "\n");
663     }
664 }