OSDN Git Service

* xtensa-config.h: Undef all macros before defining them.
[pf3gnuchains/gcc-fork.git] / gcc / scan-decls.c
1 /* scan-decls.c - Extracts declarations from cpp output.
2    Copyright (C) 1993, 1995, 1997, 1998,
3    1999, 2000, 2003 Free Software Foundation, Inc.
4
5 This program is free software; you can redistribute it and/or modify it
6 under the terms of the GNU General Public License as published by the
7 Free Software Foundation; either version 2, or (at your option) any
8 later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18
19    Written by Per Bothner <bothner@cygnus.com>, July 1993.  */
20
21 #include "bconfig.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "cpplib.h"
26 #include "scan.h"
27
28 static void skip_to_closing_brace (cpp_reader *);
29 static const cpp_token *get_a_token (cpp_reader *);
30
31 int brace_nesting = 0;
32
33 /* The first extern_C_braces_length elements of extern_C_braces
34    indicate the (brace nesting levels of) left braces that were
35    prefixed by extern "C".  */
36 int extern_C_braces_length = 0;
37 char extern_C_braces[20];
38 #define in_extern_C_brace (extern_C_braces_length>0)
39
40 /* True if the function declaration currently being scanned is
41    prefixed by extern "C".  */
42 int current_extern_C = 0;
43
44 /* Get a token but skip padding.  */
45 static const cpp_token *
46 get_a_token (cpp_reader *pfile)
47 {
48   for (;;)
49     {
50       const cpp_token *result = cpp_get_token (pfile);
51       if (result->type != CPP_PADDING)
52         return result;
53     }
54 }
55
56 static void
57 skip_to_closing_brace (cpp_reader *pfile)
58 {
59   int nesting = 1;
60   for (;;)
61     {
62       enum cpp_ttype token = get_a_token (pfile)->type;
63
64       if (token == CPP_EOF)
65         break;
66       if (token == CPP_OPEN_BRACE)
67         nesting++;
68       if (token == CPP_CLOSE_BRACE && --nesting == 0)
69         break;
70     }
71 }
72
73 /* This function scans a C source file (actually, the output of cpp),
74    reading from FP.  It looks for function declarations, and
75    external variable declarations.
76
77    The following grammar (as well as some extra stuff) is recognized:
78
79    declaration:
80      (decl-specifier)* declarator ("," declarator)* ";"
81    decl-specifier:
82      identifier
83      keyword
84      extern "C"
85    declarator:
86      (ptr-operator)* dname [ "(" argument-declaration-list ")" ]
87    ptr-operator:
88      ("*" | "&") ("const" | "volatile")*
89    dname:
90      identifier
91
92 Here dname is the actual name being declared.
93 */
94
95 int
96 scan_decls (cpp_reader *pfile, int argc ATTRIBUTE_UNUSED,
97             char **argv ATTRIBUTE_UNUSED)
98 {
99   int saw_extern, saw_inline;
100   cpp_token prev_id;
101   const cpp_token *token;
102
103  new_statement:
104   token = get_a_token (pfile);
105
106  handle_statement:
107   current_extern_C = 0;
108   saw_extern = 0;
109   saw_inline = 0;
110   if (token->type == CPP_OPEN_BRACE)
111     {
112       /* Pop an 'extern "C"' nesting level, if appropriate.  */
113       if (extern_C_braces_length
114           && extern_C_braces[extern_C_braces_length - 1] == brace_nesting)
115         extern_C_braces_length--;
116       brace_nesting--;
117       goto new_statement;
118     }
119   if (token->type == CPP_OPEN_BRACE)
120     {
121       brace_nesting++;
122       goto new_statement;
123     }
124
125   if (token->type == CPP_EOF)
126     return 0;
127
128   if (token->type == CPP_SEMICOLON)
129     goto new_statement;
130   if (token->type != CPP_NAME)
131     goto new_statement;
132
133   prev_id.type = CPP_EOF;
134   for (;;)
135     {
136       switch (token->type)
137         {
138         default:
139           goto handle_statement;
140         case CPP_MULT:
141         case CPP_AND:
142           /* skip */
143           break;
144
145         case CPP_COMMA:
146         case CPP_SEMICOLON:
147           if (prev_id.type != CPP_EOF && saw_extern)
148             {
149               recognized_extern (&prev_id);
150             }
151           if (token->type == CPP_COMMA)
152             break;
153           /* ... fall through ...  */
154         case CPP_OPEN_BRACE:  case CPP_CLOSE_BRACE:
155           goto new_statement;
156
157         case CPP_EOF:
158           return 0;
159
160         case CPP_OPEN_PAREN:
161           /* Looks like this is the start of a formal parameter list.  */
162           if (prev_id.type != CPP_EOF)
163             {
164               int nesting = 1;
165               int have_arg_list = 0;
166               for (;;)
167                 {
168                   token = get_a_token (pfile);
169                   if (token->type == CPP_OPEN_PAREN)
170                     nesting++;
171                   else if (token->type == CPP_CLOSE_PAREN)
172                     {
173                       nesting--;
174                       if (nesting == 0)
175                         break;
176                     }
177                   else if (token->type == CPP_EOF)
178                     break;
179                   else if (token->type == CPP_NAME
180                            || token->type == CPP_ELLIPSIS)
181                     have_arg_list = 1;
182                 }
183               recognized_function (&prev_id, token->line,
184                                    (saw_inline ? 'I'
185                                     : in_extern_C_brace || current_extern_C
186                                     ? 'F' : 'f'), have_arg_list);
187               token = get_a_token (pfile);
188               if (token->type == CPP_OPEN_BRACE)
189                 {
190                   /* skip body of (normally) inline function */
191                   skip_to_closing_brace (pfile);
192                   goto new_statement;
193                 }
194
195               /* skip a possible __attribute__ or throw expression after the
196                  parameter list */
197               while (token->type != CPP_SEMICOLON && token->type != CPP_EOF)
198                 token = get_a_token (pfile);
199               goto new_statement;
200             }
201           break;
202         case CPP_NAME:
203           /* "inline" and "extern" are recognized but skipped */
204           if (cpp_ideq (token, "inline"))
205             {
206               saw_inline = 1;
207             }
208           else if (cpp_ideq (token, "extern"))
209             {
210               saw_extern = 1;
211               token = get_a_token (pfile);
212               if (token->type == CPP_STRING
213                   && token->val.str.len == 1
214                   && token->val.str.text[0] == 'C')
215                 {
216                   current_extern_C = 1;
217                   token = get_a_token (pfile);
218                   if (token->type == CPP_OPEN_BRACE)
219                     {
220                       brace_nesting++;
221                       extern_C_braces[extern_C_braces_length++]
222                         = brace_nesting;
223                       goto new_statement;
224                     }
225                 }
226               else
227                 continue;
228               break;
229             }
230           /* This may be the name of a variable or function.  */
231           prev_id = *token;
232           break;
233         }
234       token = get_a_token (pfile);
235     }
236 }