2001-08-28 Alexandre Petit-Bianco <apbianco@redhat.com>
[official-gcc.git] / gcc / scan-decls.c
blob2fe570e3ec409daaf22cda955d6f322c75254834
1 /* scan-decls.c - Extracts declarations from cpp output.
2 Copyright (C) 1993, 1995, 1997, 1998,
3 1999, 2000 Free Software Foundation, Inc.
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.
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.
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.
19 Written by Per Bothner <bothner@cygnus.com>, July 1993. */
21 #include "hconfig.h"
22 #include "system.h"
23 #include "cpplib.h"
24 #include "scan.h"
26 static void skip_to_closing_brace PARAMS ((cpp_reader *));
28 int brace_nesting = 0;
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)
37 /* True if the function declaration currently being scanned is
38 prefixed by extern "C". */
39 int current_extern_C = 0;
41 static void
42 skip_to_closing_brace (pfile)
43 cpp_reader *pfile;
45 int nesting = 1;
46 for (;;)
48 cpp_token tok;
49 enum cpp_ttype token;
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;
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.
66 The following grammar (as well as some extra stuff) is recognized:
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
81 Here dname is the actual name being declared.
84 int
85 scan_decls (pfile, argc, argv)
86 cpp_reader *pfile;
87 int argc ATTRIBUTE_UNUSED;
88 char **argv ATTRIBUTE_UNUSED;
90 int saw_extern, saw_inline;
91 cpp_token token, prev_id;
93 new_statement:
94 cpp_get_token (pfile, &token);
96 handle_statement:
97 current_extern_C = 0;
98 saw_extern = 0;
99 saw_inline = 0;
100 if (token.type == CPP_OPEN_BRACE)
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;
109 if (token.type == CPP_OPEN_BRACE)
111 brace_nesting++;
112 goto new_statement;
115 if (token.type == CPP_EOF)
116 return 0;
118 if (token.type == CPP_SEMICOLON)
119 goto new_statement;
120 if (token.type != CPP_NAME)
121 goto new_statement;
123 prev_id.type = CPP_EOF;
124 for (;;)
126 switch (token.type)
128 default:
129 goto handle_statement;
130 case CPP_MULT:
131 case CPP_AND:
132 /* skip */
133 break;
135 case CPP_COMMA:
136 case CPP_SEMICOLON:
137 if (prev_id.type != CPP_EOF && saw_extern)
139 recognized_extern (&prev_id);
141 if (token.type == CPP_COMMA)
142 break;
143 /* ... fall through ... */
144 case CPP_OPEN_BRACE: case CPP_CLOSE_BRACE:
145 goto new_statement;
147 case CPP_EOF:
148 return 0;
150 case CPP_OPEN_PAREN:
151 /* Looks like this is the start of a formal parameter list. */
152 if (prev_id.type != CPP_EOF)
154 int nesting = 1;
155 int have_arg_list = 0;
156 for (;;)
158 cpp_get_token (pfile, &token);
159 if (token.type == CPP_OPEN_PAREN)
160 nesting++;
161 else if (token.type == CPP_CLOSE_PAREN)
163 nesting--;
164 if (nesting == 0)
165 break;
167 else if (token.type == CPP_EOF)
168 break;
169 else if (token.type == CPP_NAME
170 || token.type == CPP_ELLIPSIS)
171 have_arg_list = 1;
173 recognized_function (&prev_id,
174 cpp_get_line (pfile)->line,
175 (saw_inline ? 'I'
176 : in_extern_C_brace || current_extern_C
177 ? 'F' : 'f'), have_arg_list);
178 cpp_get_token (pfile, &token);
179 if (token.type == CPP_OPEN_BRACE)
181 /* skip body of (normally) inline function */
182 skip_to_closing_brace (pfile);
183 goto new_statement;
186 /* skip a possible __attribute__ or throw expression after the
187 parameter list */
188 while (token.type != CPP_SEMICOLON && token.type != CPP_EOF)
189 cpp_get_token (pfile, &token);
190 goto new_statement;
192 break;
193 case CPP_NAME:
194 /* "inline" and "extern" are recognized but skipped */
195 if (cpp_ideq (&token, "inline"))
197 saw_inline = 1;
199 else if (cpp_ideq (&token, "extern"))
201 saw_extern = 1;
202 cpp_get_token (pfile, &token);
203 if (token.type == CPP_STRING
204 && token.val.str.len == 1
205 && token.val.str.text[0] == 'C')
207 current_extern_C = 1;
208 cpp_get_token (pfile, &token);
209 if (token.type == CPP_OPEN_BRACE)
211 brace_nesting++;
212 extern_C_braces[extern_C_braces_length++]
213 = brace_nesting;
214 goto new_statement;
217 else
218 continue;
219 break;
221 /* This may be the name of a variable or function. */
222 prev_id = token;
223 break;
225 cpp_get_token (pfile, &token);