OSDN Git Service

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