Makefile.in (stmp-docobjdir): New target; ensure $docobjdir exists.
[official-gcc.git] / gcc / scan-decls.c
blob14f64e8cb2e175f9ca678a410ddaa16e09c38e5f
1 /* scan-decls.c - Extracts declarations from cpp output.
2 Copyright (C) 1993, 1995, 1997, 1998,
3 1999, 2000, 2003 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 "bconfig.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "cpplib.h"
26 #include "scan.h"
28 static void skip_to_closing_brace (cpp_reader *);
29 static const cpp_token *get_a_token (cpp_reader *);
31 int brace_nesting = 0;
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)
40 /* True if the function declaration currently being scanned is
41 prefixed by extern "C". */
42 int current_extern_C = 0;
44 /* Get a token but skip padding. */
45 static const cpp_token *
46 get_a_token (cpp_reader *pfile)
48 for (;;)
50 const cpp_token *result = cpp_get_token (pfile);
51 if (result->type != CPP_PADDING)
52 return result;
56 static void
57 skip_to_closing_brace (cpp_reader *pfile)
59 int nesting = 1;
60 for (;;)
62 enum cpp_ttype token = get_a_token (pfile)->type;
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;
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.
77 The following grammar (as well as some extra stuff) is recognized:
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
92 Here dname is the actual name being declared.
95 int
96 scan_decls (cpp_reader *pfile, int argc ATTRIBUTE_UNUSED,
97 char **argv ATTRIBUTE_UNUSED)
99 int saw_extern, saw_inline;
100 cpp_token prev_id;
101 const cpp_token *token;
103 new_statement:
104 token = get_a_token (pfile);
106 handle_statement:
107 current_extern_C = 0;
108 saw_extern = 0;
109 saw_inline = 0;
110 if (token->type == CPP_OPEN_BRACE)
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;
119 if (token->type == CPP_OPEN_BRACE)
121 brace_nesting++;
122 goto new_statement;
125 if (token->type == CPP_EOF)
126 return 0;
128 if (token->type == CPP_SEMICOLON)
129 goto new_statement;
130 if (token->type != CPP_NAME)
131 goto new_statement;
133 prev_id.type = CPP_EOF;
134 for (;;)
136 switch (token->type)
138 default:
139 goto handle_statement;
140 case CPP_MULT:
141 case CPP_AND:
142 /* skip */
143 break;
145 case CPP_COMMA:
146 case CPP_SEMICOLON:
147 if (prev_id.type != CPP_EOF && saw_extern)
149 recognized_extern (&prev_id);
151 if (token->type == CPP_COMMA)
152 break;
153 /* ... fall through ... */
154 case CPP_OPEN_BRACE: case CPP_CLOSE_BRACE:
155 goto new_statement;
157 case CPP_EOF:
158 return 0;
160 case CPP_OPEN_PAREN:
161 /* Looks like this is the start of a formal parameter list. */
162 if (prev_id.type != CPP_EOF)
164 int nesting = 1;
165 int have_arg_list = 0;
166 for (;;)
168 token = get_a_token (pfile);
169 if (token->type == CPP_OPEN_PAREN)
170 nesting++;
171 else if (token->type == CPP_CLOSE_PAREN)
173 nesting--;
174 if (nesting == 0)
175 break;
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;
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)
190 /* skip body of (normally) inline function */
191 skip_to_closing_brace (pfile);
192 goto new_statement;
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;
201 break;
202 case CPP_NAME:
203 /* "inline" and "extern" are recognized but skipped */
204 if (cpp_ideq (token, "inline"))
206 saw_inline = 1;
208 else if (cpp_ideq (token, "extern"))
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')
216 current_extern_C = 1;
217 token = get_a_token (pfile);
218 if (token->type == CPP_OPEN_BRACE)
220 brace_nesting++;
221 extern_C_braces[extern_C_braces_length++]
222 = brace_nesting;
223 goto new_statement;
226 else
227 continue;
228 break;
230 /* This may be the name of a variable or function. */
231 prev_id = *token;
232 break;
234 token = get_a_token (pfile);