OSDN Git Service

2000-10-28 Neil Booth <neilb@earthling.net>
[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 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 "hconfig.h"
22 #include "system.h"
23 #include "cpplib.h"
24 #include "scan.h"
25
26 static void skip_to_closing_brace PARAMS ((cpp_reader *));
27
28 int brace_nesting = 0;
29
30 /* The first extern_C_braces_length elements of extern_C_braces
31    indicate the (brace nesting levels of) left braces that were
32    prefixed by extern "C".  */
33 int extern_C_braces_length = 0;
34 char extern_C_braces[20];
35 #define in_extern_C_brace (extern_C_braces_length>0)
36
37 /* True if the function declaration currently being scanned is
38    prefixed by extern "C".  */
39 int current_extern_C = 0;
40
41 static void
42 skip_to_closing_brace (pfile)
43      cpp_reader *pfile;
44 {
45   int nesting = 1;
46   for (;;)
47     {
48       cpp_token tok;
49       enum cpp_ttype token;
50
51       cpp_get_token (pfile, &tok);
52       token = tok.type;
53       if (token == CPP_EOF)
54         break;
55       if (token == CPP_OPEN_BRACE)
56         nesting++;
57       if (token == CPP_CLOSE_BRACE && --nesting == 0)
58         break;
59     }
60 }
61
62 /* This function scans a C source file (actually, the output of cpp),
63    reading from FP.  It looks for function declarations, and
64    external variable declarations.  
65
66    The following grammar (as well as some extra stuff) is recognized:
67
68    declaration:
69      (decl-specifier)* declarator ("," declarator)* ";"
70    decl-specifier:
71      identifier
72      keyword
73      extern "C"
74    declarator:
75      (ptr-operator)* dname [ "(" argument-declaration-list ")" ]
76    ptr-operator:
77      ("*" | "&") ("const" | "volatile")*
78    dname:
79      identifier
80
81 Here dname is the actual name being declared.
82 */
83
84 int
85 scan_decls (pfile, argc, argv)
86      cpp_reader *pfile;
87      int argc ATTRIBUTE_UNUSED;
88      char **argv ATTRIBUTE_UNUSED;
89 {
90   int saw_extern, saw_inline;
91   cpp_token token, prev_id;
92
93  new_statement:
94   cpp_get_token (pfile, &token);
95
96  handle_statement:
97   current_extern_C = 0;
98   saw_extern = 0;
99   saw_inline = 0;
100   if (token.type == CPP_OPEN_BRACE)
101     {
102       /* Pop an 'extern "C"' nesting level, if appropriate.  */
103       if (extern_C_braces_length
104           && extern_C_braces[extern_C_braces_length - 1] == brace_nesting)
105         extern_C_braces_length--;
106       brace_nesting--;
107       goto new_statement;
108     }
109   if (token.type == CPP_OPEN_BRACE)
110     {
111       brace_nesting++;
112       goto new_statement;
113     }
114   if (token.type == CPP_EOF)
115     {
116       cpp_pop_buffer (pfile);
117       if (CPP_BUFFER (pfile) == NULL)
118         return 0;
119
120       goto new_statement;
121     }
122   if (token.type == CPP_SEMICOLON)
123     goto new_statement;
124   if (token.type != CPP_NAME)
125     goto new_statement;
126
127   prev_id.type = CPP_EOF;
128   for (;;)
129     {
130       switch (token.type)
131         {
132         default:
133           goto handle_statement;
134         case CPP_MULT:
135         case CPP_AND:
136         case CPP_PLACEMARKER:
137           /* skip */
138           break;
139
140         case CPP_COMMA:
141         case CPP_SEMICOLON:
142           if (prev_id.type != CPP_EOF && saw_extern)
143             {
144               recognized_extern (&prev_id);
145             }
146           if (token.type == CPP_COMMA)
147             break;
148           /* ... fall through ...  */
149         case CPP_OPEN_BRACE:  case CPP_CLOSE_BRACE:
150           goto new_statement;
151           
152         case CPP_EOF:
153           cpp_pop_buffer (pfile);
154           if (CPP_BUFFER (pfile) == NULL)
155             return 0;
156           break;
157
158         case CPP_OPEN_PAREN:
159           /* Looks like this is the start of a formal parameter list.  */
160           if (prev_id.type != CPP_EOF)
161             {
162               int nesting = 1;
163               int have_arg_list = 0;
164               for (;;)
165                 {
166                   cpp_get_token (pfile, &token);
167                   if (token.type == CPP_OPEN_PAREN)
168                     nesting++;
169                   else if (token.type == CPP_CLOSE_PAREN)
170                     {
171                       nesting--;
172                       if (nesting == 0)
173                         break;
174                     }
175                   else if (token.type == CPP_EOF)
176                     break;
177                   else if (token.type == CPP_NAME
178                            || token.type == CPP_ELLIPSIS)
179                     have_arg_list = 1;
180                 }
181               recognized_function (&prev_id, 
182                                    cpp_get_line (pfile)->line,
183                                    (saw_inline ? 'I'
184                                     : in_extern_C_brace || current_extern_C
185                                     ? 'F' : 'f'), have_arg_list,
186                                    CPP_BUFFER (pfile)->nominal_fname);
187               cpp_get_token (pfile, &token);
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               if (token.type == CPP_SEMICOLON)
195                 goto new_statement;
196             }
197           break;
198         case CPP_NAME:
199           /* "inline" and "extern" are recognized but skipped */
200           if (cpp_ideq (&token, "inline"))
201             {
202               saw_inline = 1;
203             }
204           else if (cpp_ideq (&token, "extern"))
205             {
206               saw_extern = 1;
207               cpp_get_token (pfile, &token);
208               if (token.type == CPP_STRING
209                   && token.val.str.len == 1
210                   && token.val.str.text[0] == 'C')
211                 {
212                   current_extern_C = 1;
213                   cpp_get_token (pfile, &token);
214                   if (token.type == CPP_OPEN_BRACE)
215                     {
216                       brace_nesting++;
217                       extern_C_braces[extern_C_braces_length++]
218                         = brace_nesting;
219                       goto new_statement;
220                     }
221                 }
222               else
223                 continue;
224               break;
225             }
226           /* This may be the name of a variable or function.  */
227           prev_id = token;
228           break;
229         }
230       cpp_get_token (pfile, &token);
231     }
232 }