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