2015-06-25 Zhouyi Zhou <yizhouzhou@ict.ac.cn>
[official-gcc.git] / gcc / c / c-aux-info.c
blob01e81f92510d19007f33e6e7b7bb7cb1cf74585a
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-2015 Free Software Foundation, Inc.
5 Contributed by Ron Guilmette (rfg@segfault.us.com).
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 3, or (at your option) any later
12 version.
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 for more details.
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
23 #include "config.h"
24 #include "system.h"
25 #include "coretypes.h"
26 #include "tm.h"
27 #include "flags.h"
28 #include "symtab.h"
29 #include "alias.h"
30 #include "tree.h"
31 #include "c-tree.h"
33 enum formals_style_enum {
34 ansi,
35 k_and_r_names,
36 k_and_r_decls
38 typedef enum formals_style_enum formals_style;
41 static const char *data_type;
43 static char *affix_data_type (const char *) ATTRIBUTE_MALLOC;
44 static const char *gen_formal_list_for_type (tree, formals_style);
45 static const char *gen_formal_list_for_func_def (tree, formals_style);
46 static const char *gen_type (const char *, tree, formals_style);
47 static const char *gen_decl (tree, int, formals_style);
49 /* Given a string representing an entire type or an entire declaration
50 which only lacks the actual "data-type" specifier (at its left end),
51 affix the data-type specifier to the left end of the given type
52 specification or object declaration.
54 Because of C language weirdness, the data-type specifier (which normally
55 goes in at the very left end) may have to be slipped in just to the
56 right of any leading "const" or "volatile" qualifiers (there may be more
57 than one). Actually this may not be strictly necessary because it seems
58 that GCC (at least) accepts `<data-type> const foo;' and treats it the
59 same as `const <data-type> foo;' but people are accustomed to seeing
60 `const char *foo;' and *not* `char const *foo;' so we try to create types
61 that look as expected. */
63 static char *
64 affix_data_type (const char *param)
66 char *const type_or_decl = ASTRDUP (param);
67 char *p = type_or_decl;
68 char *qualifiers_then_data_type;
69 char saved;
71 /* Skip as many leading const's or volatile's as there are. */
73 for (;;)
75 if (!strncmp (p, "volatile ", 9))
77 p += 9;
78 continue;
80 if (!strncmp (p, "const ", 6))
82 p += 6;
83 continue;
85 break;
88 /* p now points to the place where we can insert the data type. We have to
89 add a blank after the data-type of course. */
91 if (p == type_or_decl)
92 return concat (data_type, " ", type_or_decl, NULL);
94 saved = *p;
95 *p = '\0';
96 qualifiers_then_data_type = concat (type_or_decl, data_type, NULL);
97 *p = saved;
98 return reconcat (qualifiers_then_data_type,
99 qualifiers_then_data_type, " ", p, NULL);
102 /* Given a tree node which represents some "function type", generate the
103 source code version of a formal parameter list (of some given style) for
104 this function type. Return the whole formal parameter list (including
105 a pair of surrounding parens) as a string. Note that if the style
106 we are currently aiming for is non-ansi, then we just return a pair
107 of empty parens here. */
109 static const char *
110 gen_formal_list_for_type (tree fntype, formals_style style)
112 const char *formal_list = "";
113 tree formal_type;
115 if (style != ansi)
116 return "()";
118 formal_type = TYPE_ARG_TYPES (fntype);
119 while (formal_type && TREE_VALUE (formal_type) != void_type_node)
121 const char *this_type;
123 if (*formal_list)
124 formal_list = concat (formal_list, ", ", NULL);
126 this_type = gen_type ("", TREE_VALUE (formal_type), ansi);
127 formal_list
128 = ((strlen (this_type))
129 ? concat (formal_list, affix_data_type (this_type), NULL)
130 : concat (formal_list, data_type, NULL));
132 formal_type = TREE_CHAIN (formal_type);
135 /* If we got to here, then we are trying to generate an ANSI style formal
136 parameters list.
138 New style prototyped ANSI formal parameter lists should in theory always
139 contain some stuff between the opening and closing parens, even if it is
140 only "void".
142 The brutal truth though is that there is lots of old K&R code out there
143 which contains declarations of "pointer-to-function" parameters and
144 these almost never have fully specified formal parameter lists associated
145 with them. That is, the pointer-to-function parameters are declared
146 with just empty parameter lists.
148 In cases such as these, protoize should really insert *something* into
149 the vacant parameter lists, but what? It has no basis on which to insert
150 anything in particular.
152 Here, we make life easy for protoize by trying to distinguish between
153 K&R empty parameter lists and new-style prototyped parameter lists
154 that actually contain "void". In the latter case we (obviously) want
155 to output the "void" verbatim, and that what we do. In the former case,
156 we do our best to give protoize something nice to insert.
158 This "something nice" should be something that is still valid (when
159 re-compiled) but something that can clearly indicate to the user that
160 more typing information (for the parameter list) should be added (by
161 hand) at some convenient moment.
163 The string chosen here is a comment with question marks in it. */
165 if (!*formal_list)
167 if (prototype_p (fntype))
168 /* assert (TREE_VALUE (TYPE_ARG_TYPES (fntype)) == void_type_node); */
169 formal_list = "void";
170 else
171 formal_list = "/* ??? */";
173 else
175 /* If there were at least some parameters, and if the formals-types-list
176 petered out to a NULL (i.e. without being terminated by a
177 void_type_node) then we need to tack on an ellipsis. */
178 if (!formal_type)
179 formal_list = concat (formal_list, ", ...", NULL);
182 return concat (" (", formal_list, ")", NULL);
185 /* Generate a parameter list for a function definition (in some given style).
187 Note that this routine has to be separate (and different) from the code that
188 generates the prototype parameter lists for function declarations, because
189 in the case of a function declaration, all we have to go on is a tree node
190 representing the function's own "function type". This can tell us the types
191 of all of the formal parameters for the function, but it cannot tell us the
192 actual *names* of each of the formal parameters. We need to output those
193 parameter names for each function definition.
195 This routine gets a pointer to a tree node which represents the actual
196 declaration of the given function, and this DECL node has a list of formal
197 parameter (variable) declarations attached to it. These formal parameter
198 (variable) declaration nodes give us the actual names of the formal
199 parameters for the given function definition.
201 This routine returns a string which is the source form for the entire
202 function formal parameter list. */
204 static const char *
205 gen_formal_list_for_func_def (tree fndecl, formals_style style)
207 const char *formal_list = "";
208 tree formal_decl;
210 formal_decl = DECL_ARGUMENTS (fndecl);
211 while (formal_decl)
213 const char *this_formal;
215 if (*formal_list && ((style == ansi) || (style == k_and_r_names)))
216 formal_list = concat (formal_list, ", ", NULL);
217 this_formal = gen_decl (formal_decl, 0, style);
218 if (style == k_and_r_decls)
219 formal_list = concat (formal_list, this_formal, "; ", NULL);
220 else
221 formal_list = concat (formal_list, this_formal, NULL);
222 formal_decl = TREE_CHAIN (formal_decl);
224 if (style == ansi)
226 if (!DECL_ARGUMENTS (fndecl))
227 formal_list = concat (formal_list, "void", NULL);
228 if (stdarg_p (TREE_TYPE (fndecl)))
229 formal_list = concat (formal_list, ", ...", NULL);
231 if ((style == ansi) || (style == k_and_r_names))
232 formal_list = concat (" (", formal_list, ")", NULL);
233 return formal_list;
236 /* Generate a string which is the source code form for a given type (t). This
237 routine is ugly and complex because the C syntax for declarations is ugly
238 and complex. This routine is straightforward so long as *no* pointer types,
239 array types, or function types are involved.
241 In the simple cases, this routine will return the (string) value which was
242 passed in as the "ret_val" argument. Usually, this starts out either as an
243 empty string, or as the name of the declared item (i.e. the formal function
244 parameter variable).
246 This routine will also return with the global variable "data_type" set to
247 some string value which is the "basic" data-type of the given complete type.
248 This "data_type" string can be concatenated onto the front of the returned
249 string after this routine returns to its caller.
251 In complicated cases involving pointer types, array types, or function
252 types, the C declaration syntax requires an "inside out" approach, i.e. if
253 you have a type which is a "pointer-to-function" type, you need to handle
254 the "pointer" part first, but it also has to be "innermost" (relative to
255 the declaration stuff for the "function" type). Thus, is this case, you
256 must prepend a "(*" and append a ")" to the name of the item (i.e. formal
257 variable). Then you must append and prepend the other info for the
258 "function type" part of the overall type.
260 To handle the "innermost precedence" rules of complicated C declarators, we
261 do the following (in this routine). The input parameter called "ret_val"
262 is treated as a "seed". Each time gen_type is called (perhaps recursively)
263 some additional strings may be appended or prepended (or both) to the "seed"
264 string. If yet another (lower) level of the GCC tree exists for the given
265 type (as in the case of a pointer type, an array type, or a function type)
266 then the (wrapped) seed is passed to a (recursive) invocation of gen_type()
267 this recursive invocation may again "wrap" the (new) seed with yet more
268 declarator stuff, by appending, prepending (or both). By the time the
269 recursion bottoms out, the "seed value" at that point will have a value
270 which is (almost) the complete source version of the declarator (except
271 for the data_type info). Thus, this deepest "seed" value is simply passed
272 back up through all of the recursive calls until it is given (as the return
273 value) to the initial caller of the gen_type() routine. All that remains
274 to do at this point is for the initial caller to prepend the "data_type"
275 string onto the returned "seed". */
277 static const char *
278 gen_type (const char *ret_val, tree t, formals_style style)
280 tree chain_p;
282 /* If there is a typedef name for this type, use it. */
283 if (TYPE_NAME (t) && TREE_CODE (TYPE_NAME (t)) == TYPE_DECL)
284 data_type = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (t)));
285 else
287 switch (TREE_CODE (t))
289 case POINTER_TYPE:
290 if (TYPE_ATOMIC (t))
291 ret_val = concat ("_Atomic ", ret_val, NULL);
292 if (TYPE_READONLY (t))
293 ret_val = concat ("const ", ret_val, NULL);
294 if (TYPE_VOLATILE (t))
295 ret_val = concat ("volatile ", ret_val, NULL);
297 ret_val = concat ("*", ret_val, NULL);
299 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE || TREE_CODE (TREE_TYPE (t)) == FUNCTION_TYPE)
300 ret_val = concat ("(", ret_val, ")", NULL);
302 ret_val = gen_type (ret_val, TREE_TYPE (t), style);
304 return ret_val;
306 case ARRAY_TYPE:
307 if (!COMPLETE_TYPE_P (t) || TREE_CODE (TYPE_SIZE (t)) != INTEGER_CST)
308 ret_val = gen_type (concat (ret_val, "[]", NULL),
309 TREE_TYPE (t), style);
310 else if (int_size_in_bytes (t) == 0)
311 ret_val = gen_type (concat (ret_val, "[0]", NULL),
312 TREE_TYPE (t), style);
313 else
315 char buff[23];
316 sprintf (buff, "[" HOST_WIDE_INT_PRINT_DEC"]",
317 int_size_in_bytes (t)
318 / int_size_in_bytes (TREE_TYPE (t)));
319 ret_val = gen_type (concat (ret_val, buff, NULL),
320 TREE_TYPE (t), style);
322 break;
324 case FUNCTION_TYPE:
325 ret_val = gen_type (concat (ret_val,
326 gen_formal_list_for_type (t, style),
327 NULL),
328 TREE_TYPE (t), style);
329 break;
331 case IDENTIFIER_NODE:
332 data_type = IDENTIFIER_POINTER (t);
333 break;
335 /* The following three cases are complicated by the fact that a
336 user may do something really stupid, like creating a brand new
337 "anonymous" type specification in a formal argument list (or as
338 part of a function return type specification). For example:
340 int f (enum { red, green, blue } color);
342 In such cases, we have no name that we can put into the prototype
343 to represent the (anonymous) type. Thus, we have to generate the
344 whole darn type specification. Yuck! */
346 case RECORD_TYPE:
347 if (TYPE_NAME (t))
348 data_type = IDENTIFIER_POINTER (TYPE_NAME (t));
349 else
351 data_type = "";
352 chain_p = TYPE_FIELDS (t);
353 while (chain_p)
355 data_type = concat (data_type, gen_decl (chain_p, 0, ansi),
356 NULL);
357 chain_p = TREE_CHAIN (chain_p);
358 data_type = concat (data_type, "; ", NULL);
360 data_type = concat ("{ ", data_type, "}", NULL);
362 data_type = concat ("struct ", data_type, NULL);
363 break;
365 case UNION_TYPE:
366 if (TYPE_NAME (t))
367 data_type = IDENTIFIER_POINTER (TYPE_NAME (t));
368 else
370 data_type = "";
371 chain_p = TYPE_FIELDS (t);
372 while (chain_p)
374 data_type = concat (data_type, gen_decl (chain_p, 0, ansi),
375 NULL);
376 chain_p = TREE_CHAIN (chain_p);
377 data_type = concat (data_type, "; ", NULL);
379 data_type = concat ("{ ", data_type, "}", NULL);
381 data_type = concat ("union ", data_type, NULL);
382 break;
384 case ENUMERAL_TYPE:
385 if (TYPE_NAME (t))
386 data_type = IDENTIFIER_POINTER (TYPE_NAME (t));
387 else
389 data_type = "";
390 chain_p = TYPE_VALUES (t);
391 while (chain_p)
393 data_type = concat (data_type,
394 IDENTIFIER_POINTER (TREE_PURPOSE (chain_p)), NULL);
395 chain_p = TREE_CHAIN (chain_p);
396 if (chain_p)
397 data_type = concat (data_type, ", ", NULL);
399 data_type = concat ("{ ", data_type, " }", NULL);
401 data_type = concat ("enum ", data_type, NULL);
402 break;
404 case TYPE_DECL:
405 data_type = IDENTIFIER_POINTER (DECL_NAME (t));
406 break;
408 case INTEGER_TYPE:
409 case FIXED_POINT_TYPE:
410 data_type = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (t)));
411 /* Normally, `unsigned' is part of the deal. Not so if it comes
412 with a type qualifier. */
413 if (TYPE_UNSIGNED (t) && TYPE_QUALS (t))
414 data_type = concat ("unsigned ", data_type, NULL);
415 break;
417 case REAL_TYPE:
418 data_type = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (t)));
419 break;
421 case VOID_TYPE:
422 data_type = "void";
423 break;
425 case ERROR_MARK:
426 data_type = "[ERROR]";
427 break;
429 default:
430 gcc_unreachable ();
433 if (TYPE_ATOMIC (t))
434 ret_val = concat ("_Atomic ", ret_val, NULL);
435 if (TYPE_READONLY (t))
436 ret_val = concat ("const ", ret_val, NULL);
437 if (TYPE_VOLATILE (t))
438 ret_val = concat ("volatile ", ret_val, NULL);
439 if (TYPE_RESTRICT (t))
440 ret_val = concat ("restrict ", ret_val, NULL);
441 return ret_val;
444 /* Generate a string (source) representation of an entire entity declaration
445 (using some particular style for function types).
447 The given entity may be either a variable or a function.
449 If the "is_func_definition" parameter is nonzero, assume that the thing
450 we are generating a declaration for is a FUNCTION_DECL node which is
451 associated with a function definition. In this case, we can assume that
452 an attached list of DECL nodes for function formal arguments is present. */
454 static const char *
455 gen_decl (tree decl, int is_func_definition, formals_style style)
457 const char *ret_val;
459 if (DECL_NAME (decl))
460 ret_val = IDENTIFIER_POINTER (DECL_NAME (decl));
461 else
462 ret_val = "";
464 /* If we are just generating a list of names of formal parameters, we can
465 simply return the formal parameter name (with no typing information
466 attached to it) now. */
468 if (style == k_and_r_names)
469 return ret_val;
471 /* Note that for the declaration of some entity (either a function or a
472 data object, like for instance a parameter) if the entity itself was
473 declared as either const or volatile, then const and volatile properties
474 are associated with just the declaration of the entity, and *not* with
475 the `type' of the entity. Thus, for such declared entities, we have to
476 generate the qualifiers here. */
478 if (TREE_THIS_VOLATILE (decl))
479 ret_val = concat ("volatile ", ret_val, NULL);
480 if (TREE_READONLY (decl))
481 ret_val = concat ("const ", ret_val, NULL);
483 data_type = "";
485 /* For FUNCTION_DECL nodes, there are two possible cases here. First, if
486 this FUNCTION_DECL node was generated from a function "definition", then
487 we will have a list of DECL_NODE's, one for each of the function's formal
488 parameters. In this case, we can print out not only the types of each
489 formal, but also each formal's name. In the second case, this
490 FUNCTION_DECL node came from an actual function declaration (and *not*
491 a definition). In this case, we do nothing here because the formal
492 argument type-list will be output later, when the "type" of the function
493 is added to the string we are building. Note that the ANSI-style formal
494 parameter list is considered to be a (suffix) part of the "type" of the
495 function. */
497 if (TREE_CODE (decl) == FUNCTION_DECL && is_func_definition)
499 ret_val = concat (ret_val, gen_formal_list_for_func_def (decl, ansi),
500 NULL);
502 /* Since we have already added in the formals list stuff, here we don't
503 add the whole "type" of the function we are considering (which
504 would include its parameter-list info), rather, we only add in
505 the "type" of the "type" of the function, which is really just
506 the return-type of the function (and does not include the parameter
507 list info). */
509 ret_val = gen_type (ret_val, TREE_TYPE (TREE_TYPE (decl)), style);
511 else
512 ret_val = gen_type (ret_val, TREE_TYPE (decl), style);
514 ret_val = affix_data_type (ret_val);
516 if (TREE_CODE (decl) != FUNCTION_DECL && C_DECL_REGISTER (decl))
517 ret_val = concat ("register ", ret_val, NULL);
518 if (TREE_PUBLIC (decl))
519 ret_val = concat ("extern ", ret_val, NULL);
520 if (TREE_CODE (decl) == FUNCTION_DECL && !TREE_PUBLIC (decl))
521 ret_val = concat ("static ", ret_val, NULL);
523 return ret_val;
526 extern FILE *aux_info_file;
528 /* Generate and write a new line of info to the aux-info (.X) file. This
529 routine is called once for each function declaration, and once for each
530 function definition (even the implicit ones). */
532 void
533 gen_aux_info_record (tree fndecl, int is_definition, int is_implicit,
534 int is_prototyped)
536 if (flag_gen_aux_info)
538 static int compiled_from_record = 0;
539 expanded_location xloc = expand_location (DECL_SOURCE_LOCATION (fndecl));
541 /* Each output .X file must have a header line. Write one now if we
542 have not yet done so. */
544 if (!compiled_from_record++)
546 /* The first line tells which directory file names are relative to.
547 Currently, -aux-info works only for files in the working
548 directory, so just use a `.' as a placeholder for now. */
549 fprintf (aux_info_file, "/* compiled from: . */\n");
552 /* Write the actual line of auxiliary info. */
554 fprintf (aux_info_file, "/* %s:%d:%c%c */ %s;",
555 xloc.file, xloc.line,
556 (is_implicit) ? 'I' : (is_prototyped) ? 'N' : 'O',
557 (is_definition) ? 'F' : 'C',
558 gen_decl (fndecl, is_definition, ansi));
560 /* If this is an explicit function declaration, we need to also write
561 out an old-style (i.e. K&R) function header, just in case the user
562 wants to run unprotoize. */
564 if (is_definition)
566 fprintf (aux_info_file, " /*%s %s*/",
567 gen_formal_list_for_func_def (fndecl, k_and_r_names),
568 gen_formal_list_for_func_def (fndecl, k_and_r_decls));
571 fprintf (aux_info_file, "\n");