Fix signed integer overflow in data-streamer.c
[official-gcc.git] / gcc / c / c-aux-info.c
blob878807bb58281acbee5c5ded906d568762b30aa0
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-2014 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 "tree.h"
29 #include "c-tree.h"
31 enum formals_style_enum {
32 ansi,
33 k_and_r_names,
34 k_and_r_decls
36 typedef enum formals_style_enum formals_style;
39 static const char *data_type;
41 static char *affix_data_type (const char *) ATTRIBUTE_MALLOC;
42 static const char *gen_formal_list_for_type (tree, formals_style);
43 static const char *gen_formal_list_for_func_def (tree, formals_style);
44 static const char *gen_type (const char *, tree, formals_style);
45 static const char *gen_decl (tree, int, formals_style);
47 /* Given a string representing an entire type or an entire declaration
48 which only lacks the actual "data-type" specifier (at its left end),
49 affix the data-type specifier to the left end of the given type
50 specification or object declaration.
52 Because of C language weirdness, the data-type specifier (which normally
53 goes in at the very left end) may have to be slipped in just to the
54 right of any leading "const" or "volatile" qualifiers (there may be more
55 than one). Actually this may not be strictly necessary because it seems
56 that GCC (at least) accepts `<data-type> const foo;' and treats it the
57 same as `const <data-type> foo;' but people are accustomed to seeing
58 `const char *foo;' and *not* `char const *foo;' so we try to create types
59 that look as expected. */
61 static char *
62 affix_data_type (const char *param)
64 char *const type_or_decl = ASTRDUP (param);
65 char *p = type_or_decl;
66 char *qualifiers_then_data_type;
67 char saved;
69 /* Skip as many leading const's or volatile's as there are. */
71 for (;;)
73 if (!strncmp (p, "volatile ", 9))
75 p += 9;
76 continue;
78 if (!strncmp (p, "const ", 6))
80 p += 6;
81 continue;
83 break;
86 /* p now points to the place where we can insert the data type. We have to
87 add a blank after the data-type of course. */
89 if (p == type_or_decl)
90 return concat (data_type, " ", type_or_decl, NULL);
92 saved = *p;
93 *p = '\0';
94 qualifiers_then_data_type = concat (type_or_decl, data_type, NULL);
95 *p = saved;
96 return reconcat (qualifiers_then_data_type,
97 qualifiers_then_data_type, " ", p, NULL);
100 /* Given a tree node which represents some "function type", generate the
101 source code version of a formal parameter list (of some given style) for
102 this function type. Return the whole formal parameter list (including
103 a pair of surrounding parens) as a string. Note that if the style
104 we are currently aiming for is non-ansi, then we just return a pair
105 of empty parens here. */
107 static const char *
108 gen_formal_list_for_type (tree fntype, formals_style style)
110 const char *formal_list = "";
111 tree formal_type;
113 if (style != ansi)
114 return "()";
116 formal_type = TYPE_ARG_TYPES (fntype);
117 while (formal_type && TREE_VALUE (formal_type) != void_type_node)
119 const char *this_type;
121 if (*formal_list)
122 formal_list = concat (formal_list, ", ", NULL);
124 this_type = gen_type ("", TREE_VALUE (formal_type), ansi);
125 formal_list
126 = ((strlen (this_type))
127 ? concat (formal_list, affix_data_type (this_type), NULL)
128 : concat (formal_list, data_type, NULL));
130 formal_type = TREE_CHAIN (formal_type);
133 /* If we got to here, then we are trying to generate an ANSI style formal
134 parameters list.
136 New style prototyped ANSI formal parameter lists should in theory always
137 contain some stuff between the opening and closing parens, even if it is
138 only "void".
140 The brutal truth though is that there is lots of old K&R code out there
141 which contains declarations of "pointer-to-function" parameters and
142 these almost never have fully specified formal parameter lists associated
143 with them. That is, the pointer-to-function parameters are declared
144 with just empty parameter lists.
146 In cases such as these, protoize should really insert *something* into
147 the vacant parameter lists, but what? It has no basis on which to insert
148 anything in particular.
150 Here, we make life easy for protoize by trying to distinguish between
151 K&R empty parameter lists and new-style prototyped parameter lists
152 that actually contain "void". In the latter case we (obviously) want
153 to output the "void" verbatim, and that what we do. In the former case,
154 we do our best to give protoize something nice to insert.
156 This "something nice" should be something that is still valid (when
157 re-compiled) but something that can clearly indicate to the user that
158 more typing information (for the parameter list) should be added (by
159 hand) at some convenient moment.
161 The string chosen here is a comment with question marks in it. */
163 if (!*formal_list)
165 if (prototype_p (fntype))
166 /* assert (TREE_VALUE (TYPE_ARG_TYPES (fntype)) == void_type_node); */
167 formal_list = "void";
168 else
169 formal_list = "/* ??? */";
171 else
173 /* If there were at least some parameters, and if the formals-types-list
174 petered out to a NULL (i.e. without being terminated by a
175 void_type_node) then we need to tack on an ellipsis. */
176 if (!formal_type)
177 formal_list = concat (formal_list, ", ...", NULL);
180 return concat (" (", formal_list, ")", NULL);
183 /* Generate a parameter list for a function definition (in some given style).
185 Note that this routine has to be separate (and different) from the code that
186 generates the prototype parameter lists for function declarations, because
187 in the case of a function declaration, all we have to go on is a tree node
188 representing the function's own "function type". This can tell us the types
189 of all of the formal parameters for the function, but it cannot tell us the
190 actual *names* of each of the formal parameters. We need to output those
191 parameter names for each function definition.
193 This routine gets a pointer to a tree node which represents the actual
194 declaration of the given function, and this DECL node has a list of formal
195 parameter (variable) declarations attached to it. These formal parameter
196 (variable) declaration nodes give us the actual names of the formal
197 parameters for the given function definition.
199 This routine returns a string which is the source form for the entire
200 function formal parameter list. */
202 static const char *
203 gen_formal_list_for_func_def (tree fndecl, formals_style style)
205 const char *formal_list = "";
206 tree formal_decl;
208 formal_decl = DECL_ARGUMENTS (fndecl);
209 while (formal_decl)
211 const char *this_formal;
213 if (*formal_list && ((style == ansi) || (style == k_and_r_names)))
214 formal_list = concat (formal_list, ", ", NULL);
215 this_formal = gen_decl (formal_decl, 0, style);
216 if (style == k_and_r_decls)
217 formal_list = concat (formal_list, this_formal, "; ", NULL);
218 else
219 formal_list = concat (formal_list, this_formal, NULL);
220 formal_decl = TREE_CHAIN (formal_decl);
222 if (style == ansi)
224 if (!DECL_ARGUMENTS (fndecl))
225 formal_list = concat (formal_list, "void", NULL);
226 if (stdarg_p (TREE_TYPE (fndecl)))
227 formal_list = concat (formal_list, ", ...", NULL);
229 if ((style == ansi) || (style == k_and_r_names))
230 formal_list = concat (" (", formal_list, ")", NULL);
231 return formal_list;
234 /* Generate a string which is the source code form for a given type (t). This
235 routine is ugly and complex because the C syntax for declarations is ugly
236 and complex. This routine is straightforward so long as *no* pointer types,
237 array types, or function types are involved.
239 In the simple cases, this routine will return the (string) value which was
240 passed in as the "ret_val" argument. Usually, this starts out either as an
241 empty string, or as the name of the declared item (i.e. the formal function
242 parameter variable).
244 This routine will also return with the global variable "data_type" set to
245 some string value which is the "basic" data-type of the given complete type.
246 This "data_type" string can be concatenated onto the front of the returned
247 string after this routine returns to its caller.
249 In complicated cases involving pointer types, array types, or function
250 types, the C declaration syntax requires an "inside out" approach, i.e. if
251 you have a type which is a "pointer-to-function" type, you need to handle
252 the "pointer" part first, but it also has to be "innermost" (relative to
253 the declaration stuff for the "function" type). Thus, is this case, you
254 must prepend a "(*" and append a ")" to the name of the item (i.e. formal
255 variable). Then you must append and prepend the other info for the
256 "function type" part of the overall type.
258 To handle the "innermost precedence" rules of complicated C declarators, we
259 do the following (in this routine). The input parameter called "ret_val"
260 is treated as a "seed". Each time gen_type is called (perhaps recursively)
261 some additional strings may be appended or prepended (or both) to the "seed"
262 string. If yet another (lower) level of the GCC tree exists for the given
263 type (as in the case of a pointer type, an array type, or a function type)
264 then the (wrapped) seed is passed to a (recursive) invocation of gen_type()
265 this recursive invocation may again "wrap" the (new) seed with yet more
266 declarator stuff, by appending, prepending (or both). By the time the
267 recursion bottoms out, the "seed value" at that point will have a value
268 which is (almost) the complete source version of the declarator (except
269 for the data_type info). Thus, this deepest "seed" value is simply passed
270 back up through all of the recursive calls until it is given (as the return
271 value) to the initial caller of the gen_type() routine. All that remains
272 to do at this point is for the initial caller to prepend the "data_type"
273 string onto the returned "seed". */
275 static const char *
276 gen_type (const char *ret_val, tree t, formals_style style)
278 tree chain_p;
280 /* If there is a typedef name for this type, use it. */
281 if (TYPE_NAME (t) && TREE_CODE (TYPE_NAME (t)) == TYPE_DECL)
282 data_type = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (t)));
283 else
285 switch (TREE_CODE (t))
287 case POINTER_TYPE:
288 if (TYPE_ATOMIC (t))
289 ret_val = concat ("_Atomic ", ret_val, NULL);
290 if (TYPE_READONLY (t))
291 ret_val = concat ("const ", ret_val, NULL);
292 if (TYPE_VOLATILE (t))
293 ret_val = concat ("volatile ", ret_val, NULL);
295 ret_val = concat ("*", ret_val, NULL);
297 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE || TREE_CODE (TREE_TYPE (t)) == FUNCTION_TYPE)
298 ret_val = concat ("(", ret_val, ")", NULL);
300 ret_val = gen_type (ret_val, TREE_TYPE (t), style);
302 return ret_val;
304 case ARRAY_TYPE:
305 if (!COMPLETE_TYPE_P (t) || TREE_CODE (TYPE_SIZE (t)) != INTEGER_CST)
306 ret_val = gen_type (concat (ret_val, "[]", NULL),
307 TREE_TYPE (t), style);
308 else if (int_size_in_bytes (t) == 0)
309 ret_val = gen_type (concat (ret_val, "[0]", NULL),
310 TREE_TYPE (t), style);
311 else
313 char buff[23];
314 sprintf (buff, "["HOST_WIDE_INT_PRINT_DEC"]",
315 int_size_in_bytes (t)
316 / int_size_in_bytes (TREE_TYPE (t)));
317 ret_val = gen_type (concat (ret_val, buff, NULL),
318 TREE_TYPE (t), style);
320 break;
322 case FUNCTION_TYPE:
323 ret_val = gen_type (concat (ret_val,
324 gen_formal_list_for_type (t, style),
325 NULL),
326 TREE_TYPE (t), style);
327 break;
329 case IDENTIFIER_NODE:
330 data_type = IDENTIFIER_POINTER (t);
331 break;
333 /* The following three cases are complicated by the fact that a
334 user may do something really stupid, like creating a brand new
335 "anonymous" type specification in a formal argument list (or as
336 part of a function return type specification). For example:
338 int f (enum { red, green, blue } color);
340 In such cases, we have no name that we can put into the prototype
341 to represent the (anonymous) type. Thus, we have to generate the
342 whole darn type specification. Yuck! */
344 case RECORD_TYPE:
345 if (TYPE_NAME (t))
346 data_type = IDENTIFIER_POINTER (TYPE_NAME (t));
347 else
349 data_type = "";
350 chain_p = TYPE_FIELDS (t);
351 while (chain_p)
353 data_type = concat (data_type, gen_decl (chain_p, 0, ansi),
354 NULL);
355 chain_p = TREE_CHAIN (chain_p);
356 data_type = concat (data_type, "; ", NULL);
358 data_type = concat ("{ ", data_type, "}", NULL);
360 data_type = concat ("struct ", data_type, NULL);
361 break;
363 case UNION_TYPE:
364 if (TYPE_NAME (t))
365 data_type = IDENTIFIER_POINTER (TYPE_NAME (t));
366 else
368 data_type = "";
369 chain_p = TYPE_FIELDS (t);
370 while (chain_p)
372 data_type = concat (data_type, gen_decl (chain_p, 0, ansi),
373 NULL);
374 chain_p = TREE_CHAIN (chain_p);
375 data_type = concat (data_type, "; ", NULL);
377 data_type = concat ("{ ", data_type, "}", NULL);
379 data_type = concat ("union ", data_type, NULL);
380 break;
382 case ENUMERAL_TYPE:
383 if (TYPE_NAME (t))
384 data_type = IDENTIFIER_POINTER (TYPE_NAME (t));
385 else
387 data_type = "";
388 chain_p = TYPE_VALUES (t);
389 while (chain_p)
391 data_type = concat (data_type,
392 IDENTIFIER_POINTER (TREE_PURPOSE (chain_p)), NULL);
393 chain_p = TREE_CHAIN (chain_p);
394 if (chain_p)
395 data_type = concat (data_type, ", ", NULL);
397 data_type = concat ("{ ", data_type, " }", NULL);
399 data_type = concat ("enum ", data_type, NULL);
400 break;
402 case TYPE_DECL:
403 data_type = IDENTIFIER_POINTER (DECL_NAME (t));
404 break;
406 case INTEGER_TYPE:
407 case FIXED_POINT_TYPE:
408 data_type = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (t)));
409 /* Normally, `unsigned' is part of the deal. Not so if it comes
410 with a type qualifier. */
411 if (TYPE_UNSIGNED (t) && TYPE_QUALS (t))
412 data_type = concat ("unsigned ", data_type, NULL);
413 break;
415 case REAL_TYPE:
416 data_type = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (t)));
417 break;
419 case VOID_TYPE:
420 data_type = "void";
421 break;
423 case ERROR_MARK:
424 data_type = "[ERROR]";
425 break;
427 default:
428 gcc_unreachable ();
431 if (TYPE_ATOMIC (t))
432 ret_val = concat ("_Atomic ", ret_val, NULL);
433 if (TYPE_READONLY (t))
434 ret_val = concat ("const ", ret_val, NULL);
435 if (TYPE_VOLATILE (t))
436 ret_val = concat ("volatile ", ret_val, NULL);
437 if (TYPE_RESTRICT (t))
438 ret_val = concat ("restrict ", ret_val, NULL);
439 return ret_val;
442 /* Generate a string (source) representation of an entire entity declaration
443 (using some particular style for function types).
445 The given entity may be either a variable or a function.
447 If the "is_func_definition" parameter is nonzero, assume that the thing
448 we are generating a declaration for is a FUNCTION_DECL node which is
449 associated with a function definition. In this case, we can assume that
450 an attached list of DECL nodes for function formal arguments is present. */
452 static const char *
453 gen_decl (tree decl, int is_func_definition, formals_style style)
455 const char *ret_val;
457 if (DECL_NAME (decl))
458 ret_val = IDENTIFIER_POINTER (DECL_NAME (decl));
459 else
460 ret_val = "";
462 /* If we are just generating a list of names of formal parameters, we can
463 simply return the formal parameter name (with no typing information
464 attached to it) now. */
466 if (style == k_and_r_names)
467 return ret_val;
469 /* Note that for the declaration of some entity (either a function or a
470 data object, like for instance a parameter) if the entity itself was
471 declared as either const or volatile, then const and volatile properties
472 are associated with just the declaration of the entity, and *not* with
473 the `type' of the entity. Thus, for such declared entities, we have to
474 generate the qualifiers here. */
476 if (TREE_THIS_VOLATILE (decl))
477 ret_val = concat ("volatile ", ret_val, NULL);
478 if (TREE_READONLY (decl))
479 ret_val = concat ("const ", ret_val, NULL);
481 data_type = "";
483 /* For FUNCTION_DECL nodes, there are two possible cases here. First, if
484 this FUNCTION_DECL node was generated from a function "definition", then
485 we will have a list of DECL_NODE's, one for each of the function's formal
486 parameters. In this case, we can print out not only the types of each
487 formal, but also each formal's name. In the second case, this
488 FUNCTION_DECL node came from an actual function declaration (and *not*
489 a definition). In this case, we do nothing here because the formal
490 argument type-list will be output later, when the "type" of the function
491 is added to the string we are building. Note that the ANSI-style formal
492 parameter list is considered to be a (suffix) part of the "type" of the
493 function. */
495 if (TREE_CODE (decl) == FUNCTION_DECL && is_func_definition)
497 ret_val = concat (ret_val, gen_formal_list_for_func_def (decl, ansi),
498 NULL);
500 /* Since we have already added in the formals list stuff, here we don't
501 add the whole "type" of the function we are considering (which
502 would include its parameter-list info), rather, we only add in
503 the "type" of the "type" of the function, which is really just
504 the return-type of the function (and does not include the parameter
505 list info). */
507 ret_val = gen_type (ret_val, TREE_TYPE (TREE_TYPE (decl)), style);
509 else
510 ret_val = gen_type (ret_val, TREE_TYPE (decl), style);
512 ret_val = affix_data_type (ret_val);
514 if (TREE_CODE (decl) != FUNCTION_DECL && C_DECL_REGISTER (decl))
515 ret_val = concat ("register ", ret_val, NULL);
516 if (TREE_PUBLIC (decl))
517 ret_val = concat ("extern ", ret_val, NULL);
518 if (TREE_CODE (decl) == FUNCTION_DECL && !TREE_PUBLIC (decl))
519 ret_val = concat ("static ", ret_val, NULL);
521 return ret_val;
524 extern FILE *aux_info_file;
526 /* Generate and write a new line of info to the aux-info (.X) file. This
527 routine is called once for each function declaration, and once for each
528 function definition (even the implicit ones). */
530 void
531 gen_aux_info_record (tree fndecl, int is_definition, int is_implicit,
532 int is_prototyped)
534 if (flag_gen_aux_info)
536 static int compiled_from_record = 0;
537 expanded_location xloc = expand_location (DECL_SOURCE_LOCATION (fndecl));
539 /* Each output .X file must have a header line. Write one now if we
540 have not yet done so. */
542 if (!compiled_from_record++)
544 /* The first line tells which directory file names are relative to.
545 Currently, -aux-info works only for files in the working
546 directory, so just use a `.' as a placeholder for now. */
547 fprintf (aux_info_file, "/* compiled from: . */\n");
550 /* Write the actual line of auxiliary info. */
552 fprintf (aux_info_file, "/* %s:%d:%c%c */ %s;",
553 xloc.file, xloc.line,
554 (is_implicit) ? 'I' : (is_prototyped) ? 'N' : 'O',
555 (is_definition) ? 'F' : 'C',
556 gen_decl (fndecl, is_definition, ansi));
558 /* If this is an explicit function declaration, we need to also write
559 out an old-style (i.e. K&R) function header, just in case the user
560 wants to run unprotoize. */
562 if (is_definition)
564 fprintf (aux_info_file, " /*%s %s*/",
565 gen_formal_list_for_func_def (fndecl, k_and_r_names),
566 gen_formal_list_for_func_def (fndecl, k_and_r_decls));
569 fprintf (aux_info_file, "\n");