OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [gcc/] [c-aux-info.c] - Blame information for rev 729

Go to most recent revision | Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.