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