]> gcc.gnu.org Git - gcc.git/blame - gcc/c-aux-info.c
Initial revision
[gcc.git] / gcc / c-aux-info.c
CommitLineData
2bbd3819
RS
1/* Generate information regarding function declarations and definitions based
2 on information stored in GCC's tree structure. This code implements the
3 -fgen-aux-info option.
4
5 This code was written by Ron Guilmette (rfg@mcc.com).
6
7 Copyright (C) 1989, 1991 Free Software Foundation, Inc.
8
9This file is part of GNU CC.
10
11GNU CC is free software; you can redistribute it and/or modify
12it under the terms of the GNU General Public License as published by
13the Free Software Foundation; either version 2, or (at your option)
14any later version.
15
16GNU CC is distributed in the hope that it will be useful,
17but WITHOUT ANY WARRANTY; without even the implied warranty of
18MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19GNU General Public License for more details.
20
21You should have received a copy of the GNU General Public License
22along with GNU CC; see the file COPYING. If not, write to
23the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
24
25#include <stdio.h>
26#include <sys/param.h>
27#include <errno.h>
28#include "config.h"
29#include "flags.h"
30#include "tree.h"
31#include "c-tree.h"
32
33extern char* xmalloc ();
34
35enum formals_style_enum {
36 ansi,
37 k_and_r_names,
38 k_and_r_decls
39};
40typedef enum formals_style_enum formals_style;
41
42
43static char* data_type;
44
45static char * concat ();
46static char * concat3 ();
47static char * gen_formal_list_for_type ();
48static int deserves_ellipsis ();
49static char * gen_formal_list_for_func_def ();
50static char * gen_type ();
51static char * gen_decl ();
52void gen_aux_info_record ();
53
54/* Virtually every UN*X system now in common use (except for pre-4.3-tahoe
55 BSD systems) now provides getcwd as called for by POSIX. Allow for
56 the few exceptions to the general rule here. */
57
58#if !(defined (USG) || defined (VMS))
59extern char *getwd ();
60#define getcwd(buf,len) getwd(buf)
61#define GUESSPATHLEN (MAXPATHLEN + 1)
62#else /* (defined (USG) || defined (VMS)) */
63extern char *getcwd ();
64/* We actually use this as a starting point, not a limit. */
65#define GUESSPATHLEN 100
66#endif /* (defined (USG) || defined (VMS)) */
67\f
68/* Take two strings and mash them together into a newly allocated area. */
69
70static char*
71concat (s1, s2)
72 char* s1;
73 char* s2;
74{
75 int size1, size2;
76 char* ret_val;
77
78 if (!s1)
79 s1 = "";
80 if (!s2)
81 s2 = "";
82
83 size1 = strlen (s1);
84 size2 = strlen (s2);
85 ret_val = xmalloc (size1 + size2 + 1);
86 strcpy (ret_val, s1);
87 strcpy (&ret_val[size1], s2);
88 return ret_val;
89}
90
91/* Take three strings and mash them together into a newly allocated area. */
92
93static char*
94concat3 (s1, s2, s3)
95 char* s1;
96 char* s2;
97 char* s3;
98{
99 int size1, size2, size3;
100 char* ret_val;
101
102 if (!s1)
103 s1 = "";
104 if (!s2)
105 s2 = "";
106 if (!s3)
107 s3 = "";
108
109 size1 = strlen (s1);
110 size2 = strlen (s2);
111 size3 = strlen (s3);
112 ret_val = xmalloc (size1 + size2 + size3 + 1);
113 strcpy (ret_val, s1);
114 strcpy (&ret_val[size1], s2);
115 strcpy (&ret_val[size1+size2], s3);
116 return ret_val;
117}
118
119/* Given a string representing an entire type or an entire declaration
120 which only lacks the actual "data-type" specifier (at its left end),
121 affix the data-type specifier to the left end of the given type
122 specification or object declaration.
123
124 Because of C language weirdness, the data-type specifier (which normally
125 goes in at the very left end) may have to be slipped in just to the
126 right of any leading "const" or "volatile" qualifiers (there may be more
127 than one). Actually this may not be strictly necessary because it seems
128 that GCC (at least) accepts `<data-type> const foo;' and treats it the
129 same as `const <data-type> foo;' but people are accustomed to seeing
130 `const char *foo;' and *not* `char const *foo;' so we try to create types
131 that look as expected. */
132
133static char*
134affix_data_type (type_or_decl)
135 char *type_or_decl;
136{
137 char *p = type_or_decl;
138 char *qualifiers_then_data_type;
139 char saved;
140
141 /* Skip as many leading const's or volatile's as there are. */
142
143 for (;;)
144 {
145 if (!strncmp (p, "volatile", 8))
146 {
147 p += 9;
148 continue;
149 }
150 if (!strncmp (p, "const", 5))
151 {
152 p += 6;
153 continue;
154 }
155 break;
156 }
157
158 /* p now points to the place where we can insert the data type. We have to
159 add a blank after the data-type of course. */
160
161 if (p == type_or_decl)
162 return concat3 (data_type, " ", type_or_decl);
163
164 saved = *p;
165 *p = '\0';
166 qualifiers_then_data_type = concat (type_or_decl, data_type);
167 *p = saved;
168 return concat3 (qualifiers_then_data_type, " ", p);
169}
170
171/* Given a tree node which represents some "function type", generate the
172 source code version of a formal parameter list (of some given style) for
173 this function type. Return the whole formal parameter list (including
174 a pair of surrounding parens) as a string. Note that if the style
175 we are currently aiming for is non-ansi, then we just return a pair
176 of empty parens here. */
177
178static char*
179gen_formal_list_for_type (fntype, style)
180 tree fntype;
181 formals_style style;
182{
183 char* formal_list = "";
184 tree formal_type;
185
186 if (style != ansi)
187 return "()";
188
189 formal_type = TYPE_ARG_TYPES (fntype);
190 while (formal_type && TREE_VALUE (formal_type) != void_type_node)
191 {
192 char* this_type;
193
194 if (*formal_list)
195 formal_list = concat (formal_list, ", ");
196
197 this_type = gen_type ("", TREE_VALUE (formal_type), ansi);
198 formal_list =
199 (strlen (this_type))
200 ? concat (formal_list, affix_data_type (this_type))
201 : concat (formal_list, data_type);
202
203 formal_type = TREE_CHAIN (formal_type);
204 }
205
206 /* If we got to here, then we are trying to generate an ANSI style formal
207 parameters list.
208
209 New style prototyped ANSI formal parameter lists should in theory always
210 contain some stuff between the opening and closing parens, even if it is
211 only "void".
212
213 The brutal truth though is that there is lots of old K&R code out there
214 which contains declarations of "pointer-to-function" parameters and
215 these almost never have fully specified formal parameter lists associated
216 with them. That is, the pointer-to-function parameters are declared
217 with just empty parameter lists.
218
219 In cases such as these, protoize should really insert *something* into
220 the vacant parameter lists, but what? It has no basis on which to insert
221 anything in particular.
222
223 Here, we make life easy for protoize by trying to distinguish between
224 K&R empty parameter lists and new-style prototyped parameter lists
225 that actually contain "void". In the latter case we (obviously) want
226 to output the "void" verbatim, and that what we do. In the former case,
227 we do our best to give protoize something nice to insert.
228
229 This "something nice" should be something that is still legal (when
230 re-compiled) but something that can clearly indicate to the user that
231 more typing information (for the parameter list) should be added (by
232 hand) at some convenient moment.
233
234 The string chozen here is a comment with question marks in it. */
235
236 if (!*formal_list)
237 {
238 if (TYPE_ARG_TYPES (fntype))
239 /* assert (TREE_VALUE (TYPE_ARG_TYPES (fntype)) == void_type_node); */
240 formal_list = "void";
241 else
242 formal_list = "/* ??? */";
243 }
244 else
245 {
246 /* If there were at least some parameters, and if the formals-types-list
247 petered out to a NULL (i.e. without being terminated by a
248 void_type_node) then we need to tack on an ellipsis. */
249 if (!formal_type)
250 formal_list = concat (formal_list, ", ...");
251 }
252
253 return concat3 (" (", formal_list, ")");
254}
255
256/* For the generation of an ANSI prototype for a function definition, we have
257 to look at the formal parameter list of the function's own "type" to
258 determine if the function's formal parameter list should end with an
259 ellipsis. Given a tree node, the following function will return non-zero
260 if the "function type" parameter list should end with an ellipsis. */
261
262static int
263deserves_ellipsis (fntype)
264 tree fntype;
265{
266 tree formal_type;
267
268 formal_type = TYPE_ARG_TYPES (fntype);
269 while (formal_type && TREE_VALUE (formal_type) != void_type_node)
270 formal_type = TREE_CHAIN (formal_type);
271
272 /* If there were at least some parameters, and if the formals-types-list
273 petered out to a NULL (i.e. without being terminated by a void_type_node)
274 then we need to tack on an ellipsis. */
275
276 return (!formal_type && TYPE_ARG_TYPES (fntype));
277}
278
279/* Generate a parameter list for a function definition (in some given style).
280
281 Note that this routine has to be separate (and different) from the code that
282 generates the prototype parameter lists for function declarations, because
283 in the case of a function declaration, all we have to go on is a tree node
284 representing the function's own "function type". This can tell us the types
285 of all of the formal parameters for the function, but it cannot tell us the
286 actual *names* of each of the formal parameters. We need to output those
287 parameter names for each function definition.
288
289 This routine gets a pointer to a tree node which represents the actual
290 declaration of the given function, and this DECL node has a list of formal
291 parameter (variable) declarations attached to it. These formal parameter
292 (variable) declaration nodes give us the actual names of the formal
293 parameters for the given function definition.
294
295 This routine returns a string which is the source form for the entire
296 function formal parameter list. */
297
298static char*
299gen_formal_list_for_func_def (fndecl, style)
300 tree fndecl;
301 formals_style style;
302{
303 char* formal_list = "";
304 tree formal_decl;
305
306 formal_decl = DECL_ARGUMENTS (fndecl);
307 while (formal_decl)
308 {
309 char *this_formal;
310
311 if (*formal_list && ((style == ansi) || (style == k_and_r_names)))
312 formal_list = concat (formal_list, ", ");
313 this_formal = gen_decl (formal_decl, 0, style);
314 if (style == k_and_r_decls)
315 formal_list = concat3 (formal_list, this_formal, "; ");
316 else
317 formal_list = concat (formal_list, this_formal);
318 formal_decl = TREE_CHAIN (formal_decl);
319 }
320 if (style == ansi)
321 {
322 if (!DECL_ARGUMENTS (fndecl))
323 formal_list = concat (formal_list, "void");
324 if (deserves_ellipsis (TREE_TYPE (fndecl)))
325 formal_list = concat (formal_list, ", ...");
326 }
327 if ((style == ansi) || (style == k_and_r_names))
328 formal_list = concat3 (" (", formal_list, ")");
329 return formal_list;
330}
331
332/* Generate a string which is the source code form for a given type (t). This
333 routine is ugly and complex because the C syntax for declarations is ugly
334 and complex. This routine is straightforward so long as *no* pointer types,
335 array types, or function types are involved.
336
337 In the simple cases, this routine will return the (string) value which was
338 passed in as the "ret_val" argument. Usually, this starts out either as an
339 empty string, or as the name of the declared item (i.e. the formal function
340 parameter variable).
341
342 This routine will also return with the global variable "data_type" set to
343 some string value which is the "basic" data-type of the given complete type.
344 This "data_type" string can be concatenated onto the front of the returned
345 string after this routine returns to its caller.
346
347 In complicated cases involving pointer types, array types, or function
348 types, the C declaration syntax requires an "inside out" approach, i.e. if
349 you have a type which is a "pointer-to-function" type, you need to handle
350 the "pointer" part first, but it also has to be "innermost" (relative to
351 the declaration stuff for the "function" type). Thus, is this case, you
352 must prepend a "(*" and append a ")" to the name of the item (i.e. formal
353 variable). Then you must append and prepend the other info for the
354 "function type" part of the overall type.
355
356 To handle the "innermost precedence" rules of complicated C declarators, we
357 do the following (in this routine). The input parameter called "ret_val"
358 is treated as a "seed". Each time gen_type is called (perhaps recursively)
359 some additional strings may be appended or prepended (or both) to the "seed"
360 string. If yet another (lower) level of the GCC tree exists for the given
361 type (as in the case of a pointer type, an array type, or a function type)
362 then the (wrapped) seed is passed to a (recursive) invocation of gen_type()
363 this recursive invocation may again "wrap" the (new) seed with yet more
364 declarator stuff, by appending, prepending (or both). By the time the
365 recursion bottoms out, the "seed value" at that point will have a value
366 which is (almost) the complete source version of the declarator (except
367 for the data_type info). Thus, this deepest "seed" value is simply passed
368 back up through all of the recursive calls until it is given (as the return
369 value) to the initial caller of the gen_type() routine. All that remains
370 to do at this point is for the initial caller to prepend the "data_type"
371 string onto the returned "seed". */
372
373static char*
374gen_type (ret_val, t, style)
375 char* ret_val;
376 tree t;
377 formals_style style;
378{
379 tree chain_p;
380
381 if (TYPE_NAME (t) && DECL_NAME (TYPE_NAME (t)))
382 data_type = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (t)));
383 else
384 {
385 switch (TREE_CODE (t))
386 {
387 case POINTER_TYPE:
388 if (TYPE_READONLY (t))
389 ret_val = concat ("const ", ret_val);
390 if (TYPE_VOLATILE (t))
391 ret_val = concat ("volatile ", ret_val);
392
393 ret_val = concat ("*", ret_val);
394
395 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE || TREE_CODE (TREE_TYPE (t)) == FUNCTION_TYPE)
396 ret_val = concat3 ("(", ret_val, ")");
397
398 ret_val = gen_type (ret_val, TREE_TYPE (t), style);
399
400 return ret_val;
401
402 case ARRAY_TYPE:
403 ret_val = gen_type (concat (ret_val, "[]"), TREE_TYPE (t), style);
404 break;
405
406 case FUNCTION_TYPE:
407 ret_val = gen_type (concat (ret_val, gen_formal_list_for_type (t, style)), TREE_TYPE (t), style);
408 break;
409
410 case IDENTIFIER_NODE:
411 data_type = IDENTIFIER_POINTER (t);
412 break;
413
414 /* The following three cases are complicated by the fact that a
415 user may do something really stupid, like creating a brand new
416 "anonymous" type specification in a formal argument list (or as
417 part of a function return type specification). For example:
418
419 int f (enum { red, green, blue } color);
420
421 In such cases, we have no name that we can put into the prototype
422 to represent the (anonymous) type. Thus, we have to generate the
423 whole darn type specification. Yuck! */
424
425 case RECORD_TYPE:
426 if (TYPE_NAME (t))
427 data_type = IDENTIFIER_POINTER (TYPE_NAME (t));
428 else
429 {
430 data_type = "";
431 chain_p = TYPE_FIELDS (t);
432 while (chain_p)
433 {
434 data_type = concat (data_type, gen_decl (chain_p, 0, ansi));
435 chain_p = TREE_CHAIN (chain_p);
436 data_type = concat (data_type, "; ");
437 }
438 data_type = concat3 ("{ ", data_type, "}");
439 }
440 data_type = concat ("struct ", data_type);
441 break;
442
443 case UNION_TYPE:
444 if (TYPE_NAME (t))
445 data_type = IDENTIFIER_POINTER (TYPE_NAME (t));
446 else
447 {
448 data_type = "";
449 chain_p = TYPE_FIELDS (t);
450 while (chain_p)
451 {
452 data_type = concat (data_type, gen_decl (chain_p, 0, ansi));
453 chain_p = TREE_CHAIN (chain_p);
454 data_type = concat (data_type, "; ");
455 }
456 data_type = concat3 ("{ ", data_type, "}");
457 }
458 data_type = concat ("union ", data_type);
459 break;
460
461 case ENUMERAL_TYPE:
462 if (TYPE_NAME (t))
463 data_type = IDENTIFIER_POINTER (TYPE_NAME (t));
464 else
465 {
466 data_type = "";
467 chain_p = TYPE_VALUES (t);
468 while (chain_p)
469 {
470 data_type = concat (data_type,
471 IDENTIFIER_POINTER (TREE_PURPOSE (chain_p)));
472 chain_p = TREE_CHAIN (chain_p);
473 if (chain_p)
474 data_type = concat (data_type, ", ");
475 }
476 data_type = concat3 ("{ ", data_type, " }");
477 }
478 data_type = concat ("enum ", data_type);
479 break;
480
481 case TYPE_DECL:
482 data_type = IDENTIFIER_POINTER (DECL_NAME (t));
483 break;
484
485 case INTEGER_TYPE:
486 data_type = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (t)));
487 /* Normally, `unsigned' is part of the deal. Not so if it comes
488 with `const' or `volatile'. */
489 if (TREE_UNSIGNED (t) && (TYPE_READONLY (t) || TYPE_VOLATILE (t)))
490 data_type = concat ("unsigned ", data_type);
491 break;
492
493 case REAL_TYPE:
494 data_type = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (t)));
495 break;
496
497 case VOID_TYPE:
498 data_type = "void";
499 break;
500
501 default:
502 abort ();
503 }
504 }
505 if (TYPE_READONLY (t))
506 ret_val = concat ("const ", ret_val);
507 if (TYPE_VOLATILE (t))
508 ret_val = concat ("volatile ", ret_val);
509 return ret_val;
510}
511
512/* Generate a string (source) representation of an entire entity declaration
513 (using some particular style for function types).
514
515 The given entity may be either a variable or a function.
516
517 If the "is_func_definition" parameter is non-zero, assume that the thing
518 we are generating a declaration for is a FUNCTION_DECL node which is
519 associated with a function definition. In this case, we can assume that
520 an attached list of DECL nodes for function formal arguments is present. */
521
522static char*
523gen_decl (decl, is_func_definition, style)
524 tree decl;
525 int is_func_definition;
526 formals_style style;
527{
528 char* ret_val;
529 char* outer_modifier = "";
530
531 if (DECL_NAME (decl))
532 ret_val = IDENTIFIER_POINTER (DECL_NAME (decl));
533 else
534 ret_val = "";
535
536 /* If we are just generating a list of names of formal parameters, we can
537 simply return the formal parameter name (with no typing information
538 attached to it) now. */
539
540 if (style == k_and_r_names)
541 return ret_val;
542
543 /* Note that for the declaration of some entity (either a function or a
544 data object, like for instance a parameter) if the entity itself was
545 declared as either const or volatile, then const and volatile properties
546 are associated with just the declaration of the entity, and *not* with
547 the `type' of the entity. Thus, for such declared entities, we have to
548 generate the qualifiers here. */
549
550 if (TREE_THIS_VOLATILE (decl))
551 ret_val = concat ("volatile ", ret_val);
552 if (TREE_READONLY (decl))
553 ret_val = concat ("const ", ret_val);
554
555 data_type = "";
556
557 /* For FUNCTION_DECL nodes, there are two possible cases here. First, if
558 this FUNCTION_DECL node was generated from a function "definition", then
559 we will have a list of DECL_NODE's, one for each of the function's formal
560 parameters. In this case, we can print out not only the types of each
561 formal, but also each formal's name. In the second case, this
562 FUNCTION_DECL node came from an actual function declaration (and *not*
563 a definition). In this case, we do nothing here because the formal
564 argument type-list will be output later, when the "type" of the function
565 is added to the string we are building. Note that the ANSI-style formal
566 parameter list is considered to be a (suffix) part of the "type" of the
567 function. */
568
569 if (TREE_CODE (decl) == FUNCTION_DECL && is_func_definition)
570 {
571 ret_val = concat (ret_val, gen_formal_list_for_func_def (decl, ansi));
572
573 /* Since we have already added in the formals list stuff, here we don't
574 add the whole "type" of the function we are considering (which
575 would include its parameter-list info), rather, we only add in
576 the "type" of the "type" of the function, which is really just
577 the return-type of the function (and does not include the parameter
578 list info). */
579
580 ret_val = gen_type (ret_val, TREE_TYPE (TREE_TYPE (decl)), style);
581 }
582 else
583 ret_val = gen_type (ret_val, TREE_TYPE (decl), style);
584
585 ret_val = affix_data_type (ret_val);
586
587 if (TREE_REGDECL (decl))
588 ret_val = concat ("register ", ret_val);
589 if (TREE_PUBLIC (decl))
590 ret_val = concat ("extern ", ret_val);
591 if (TREE_CODE (decl) == FUNCTION_DECL && !TREE_PUBLIC (decl))
592 ret_val = concat ("static ", ret_val);
593
594 return ret_val;
595}
596
597extern FILE* aux_info_file;
598
599/* Generate and write a new line of info to the aux-info (.X) file. This
600 routine is called once for each function declaration, and once for each
601 function definition (even the implicit ones). */
602
603void
604gen_aux_info_record (fndecl, is_definition, is_implicit, is_prototyped)
605 tree fndecl;
606 int is_definition;
607 int is_implicit;
608 int is_prototyped;
609{
610 if (flag_gen_aux_info)
611 {
612 static int compiled_from_record = 0;
613
614 /* Each output .X file must have a header line. Write one now if we
615 have not yet done so. */
616
617 if (! compiled_from_record++)
618 {
619 int size;
620 char *wd;
621 char *value;
622
623 /* Read the working directory, avoiding arbitrary limit. */
624 size = GUESSPATHLEN;
625 while (1)
626 {
627 wd = (char *) xmalloc (size);
628 value = getcwd (wd, size);
629 if (value != 0 || errno != ERANGE)
630 break;
631 free (wd);
632 size *= 2;
633 }
634
635 if (value != 0)
636 fprintf (aux_info_file, "/* compiled from: %s */\n", wd);
637 }
638
639 /* Write the actual line of auxilliary 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}
This page took 0.099786 seconds and 5 git commands to generate.