]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/gengtype-lex.l
* gengtype-lex.l: Recognize typedef of functions without PARAMS macro.
[thirdparty/gcc.git] / gcc / gengtype-lex.l
1 /* -*- indented-text -*- */
2 /* Process source files and output type information.
3 Copyright (C) 2002, 2003 Free Software Foundation, Inc.
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
11
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA. */
21
22 %{
23 #include "bconfig.h"
24 #include "coretypes.h"
25 #include "system.h"
26
27 #define malloc xmalloc
28 #define realloc xrealloc
29
30 #include "gengtype.h"
31 #include "gengtype-yacc.h"
32
33 #undef YY_USE_PROTOS
34 #define YY_DECL int yylex ()
35
36 static void update_lineno (const char *l, size_t len);
37
38 struct fileloc lexer_line;
39 int lexer_toplevel_done;
40
41 static void
42 update_lineno (const char *l, size_t len)
43 {
44 while (len-- > 0)
45 if (*l++ == '\n')
46 lexer_line.line++;
47 }
48
49 %}
50
51 ID [[:alpha:]_][[:alnum:]_]*
52 WS [[:space:]]+
53 IWORD short|long|(un)?signed|char|int|HOST_WIDE_INT|bool|size_t|CHAR_BITFIELD
54 ITYPE {IWORD}({WS}{IWORD})*
55
56 %x in_struct in_struct_comment in_comment in_yacc_escape
57 %option warn noyywrap nounput nodefault perf-report
58 %option 8bit never-interactive
59 %%
60
61 [^[:alnum:]_]typedef{WS}(struct|union){WS}{ID}{WS}?[*[:space:]]{WS}?{ID}{WS}?";" {
62 char *tagstart;
63 size_t taglen;
64 char *namestart;
65 size_t namelen;
66 int is_pointer = 0;
67 struct type *t;
68 int union_p;
69
70 tagstart = yytext + strlen (" typedef ");
71 while (ISSPACE (*tagstart))
72 tagstart++;
73 union_p = tagstart[0] == 'u';
74 tagstart += strlen ("union ");
75 while (ISSPACE (*tagstart))
76 tagstart++;
77 for (taglen = 1; ISIDNUM (tagstart[taglen]); taglen++)
78 ;
79 for (namestart = tagstart + taglen;
80 ! ISIDNUM (*namestart);
81 namestart++)
82 if (*namestart == '*')
83 is_pointer = 1;
84 for (namelen = 1; ISIDNUM (namestart[namelen]); namelen++)
85 ;
86 t = find_structure (xmemdup (tagstart, taglen, taglen+1), union_p);
87 if (is_pointer)
88 t = create_pointer (t);
89 do_typedef (xmemdup (namestart, namelen, namelen+1), t, &lexer_line);
90 update_lineno (yytext, yyleng);
91 }
92
93 [^[:alnum:]_]typedef{WS}{ITYPE}{WS}{ID}{WS}?";" {
94
95 char *namestart;
96 size_t namelen;
97 struct type *t;
98 char *typestart;
99 size_t typelen;
100
101 for (namestart = yytext + yyleng - 2; ISSPACE (*namestart); namestart--)
102 ;
103 for (namelen = 1; !ISSPACE (namestart[-namelen]); namelen++)
104 ;
105 namestart -= namelen - 1;
106 for (typestart = yytext + strlen (" typedef ");
107 ISSPACE(*typestart);
108 typestart++)
109 ;
110 for (typelen = namestart - typestart;
111 ISSPACE(typestart[typelen-1]);
112 typelen--)
113 ;
114
115 t = create_scalar_type (typestart, typelen);
116 do_typedef (xmemdup (namestart, namelen, namelen+1), t, &lexer_line);
117 update_lineno (yytext, yyleng);
118 }
119
120 [^[:alnum:]_]typedef{WS}{ID}{WS}{ID}{WS}PARAMS {
121 char *namestart;
122 size_t namelen;
123 struct type *t;
124
125 for (namestart = yytext + yyleng - 7; ISSPACE (*namestart); namestart--)
126 ;
127 for (namelen = 1; !ISSPACE (namestart[-namelen]); namelen++)
128 ;
129 namestart -= namelen - 1;
130
131 t = create_scalar_type ("function type", sizeof ("function type")-1);
132 do_typedef (xmemdup (namestart, namelen, namelen+1), t, &lexer_line);
133 update_lineno (yytext, yyleng);
134 }
135
136 [^[:alnum:]_]typedef{WS}{ID}{WS}{ID}{WS}"(" {
137 char *namestart;
138 size_t namelen;
139 struct type *t;
140
141 for (namestart = yytext + yyleng - 2; ISSPACE (*namestart); namestart--)
142 ;
143 for (namelen = 1; !ISSPACE (namestart[-namelen]); namelen++)
144 ;
145 namestart -= namelen - 1;
146
147 t = create_scalar_type ("function type", sizeof ("function type")-1);
148 do_typedef (xmemdup (namestart, namelen, namelen+1), t, &lexer_line);
149 update_lineno (yytext, yyleng);
150 }
151
152 [^[:alnum:]_]typedef{WS}{ID}{WS}?"("{WS}?"*"{WS}?{ID}{WS}?")"{WS}?PARAMS {
153 char *namestart;
154 size_t namelen;
155 struct type *t;
156
157 for (namestart = yytext + yyleng - 7; !ISIDNUM (*namestart); namestart--)
158 ;
159 for (namelen = 1; ISIDNUM (namestart[-namelen]); namelen++)
160 ;
161 namestart -= namelen - 1;
162
163 t = create_scalar_type ("function type", sizeof ("function type")-1);
164 do_typedef (xmemdup (namestart, namelen, namelen+1), t, &lexer_line);
165 update_lineno (yytext, yyleng);
166 }
167
168 [^[:alnum:]_]typedef{WS}{ID}{WS}?"("{WS}?"*"{WS}?{ID}{WS}?")"{WS}?"(" {
169 char *namestart;
170 size_t namelen;
171 struct type *t;
172
173 for (namestart = yytext + yyleng - 2; !ISIDNUM (*namestart); namestart--)
174 ;
175 for (namelen = 1; ISIDNUM (namestart[-namelen]); namelen++)
176 ;
177 namestart -= namelen - 1;
178
179 t = create_scalar_type ("function type", sizeof ("function type")-1);
180 do_typedef (xmemdup (namestart, namelen, namelen+1), t, &lexer_line);
181 update_lineno (yytext, yyleng);
182 }
183
184 [^[:alnum:]_](typedef{WS})?(struct|union){WS}{ID}{WS}/"GTY" {
185 char *tagstart;
186 size_t taglen;
187 int typedef_p;
188 int union_p;
189
190 typedef_p = yytext[1] == 't';
191 if (typedef_p)
192 for (tagstart = yytext + strlen (" typedef ");
193 ISSPACE(*tagstart);
194 tagstart++)
195 ;
196 else
197 tagstart = yytext + 1;
198
199 union_p = tagstart[0] == 'u';
200 tagstart += strlen ("union ");
201 while (ISSPACE (*tagstart))
202 tagstart++;
203 for (taglen = 1; ISIDNUM (tagstart[taglen]); taglen++)
204 ;
205
206 yylval.t = find_structure (xmemdup (tagstart, taglen, taglen + 1), union_p);
207 BEGIN(in_struct);
208 update_lineno (yytext, yyleng);
209 return typedef_p ? ENT_TYPEDEF_STRUCT : ENT_STRUCT;
210 }
211
212 [^[:alnum:]_](extern|static){WS}/"GTY" {
213 BEGIN(in_struct);
214 update_lineno (yytext, yyleng);
215 return ENT_EXTERNSTATIC;
216 }
217
218 ^"%union"{WS}"{"{WS}/"GTY" {
219 BEGIN(in_struct);
220 update_lineno (yytext, yyleng);
221 return ENT_YACCUNION;
222 }
223
224 <in_struct>{
225
226 "/*" { BEGIN(in_struct_comment); }
227
228 ^"%{" { BEGIN(in_yacc_escape); }
229
230 ^"@@".* /* Used for c-parse.in C/ObjC demarcation. */
231
232 {WS} { update_lineno (yytext, yyleng); }
233
234 "const"/[^[:alnum:]_] /* don't care */
235
236 "GTY"/[^[:alnum:]_] { return GTY_TOKEN; }
237 "union"/[^[:alnum:]_] { return UNION; }
238 "struct"/[^[:alnum:]_] { return STRUCT; }
239 "enum"/[^[:alnum:]_] { return ENUM; }
240 "ptr_alias"/[^[:alnum:]_] { return ALIAS; }
241 [0-9]+ { return NUM; }
242 "param"[0-9]*"_is"/[^[:alnum:]_] {
243 yylval.s = xmemdup (yytext, yyleng, yyleng+1);
244 return PARAM_IS;
245 }
246
247 {IWORD}({WS}{IWORD})*/[^[:alnum:]_] |
248 "ENUM_BITFIELD"{WS}?"("{WS}?{ID}{WS}?")" {
249 size_t len;
250
251 for (len = yyleng; ISSPACE (yytext[len-1]); len--)
252 ;
253
254 yylval.t = create_scalar_type (yytext, len);
255 update_lineno (yytext, yyleng);
256 return SCALAR;
257 }
258
259 {ID}/[^[:alnum:]_] {
260 yylval.s = xmemdup (yytext, yyleng, yyleng+1);
261 return ID;
262 }
263
264 \"([^"\\]|\\.)*\" {
265 yylval.s = xmemdup (yytext+1, yyleng-2, yyleng-1);
266 return STRING;
267 }
268 "["[^\[\]]*"]" {
269 yylval.s = xmemdup (yytext+1, yyleng-2, yyleng-1);
270 return ARRAY;
271 }
272 ^"%"{ID} {
273 yylval.s = xmemdup (yytext+1, yyleng-1, yyleng);
274 return PERCENT_ID;
275 }
276 "'"("\\".|[^\\])"'" {
277 yylval.s = xmemdup (yytext+1, yyleng-2, yyleng);
278 return CHAR;
279 }
280
281 [(){},*:<>] { return yytext[0]; }
282
283 [;=] {
284 if (lexer_toplevel_done)
285 {
286 BEGIN(INITIAL);
287 lexer_toplevel_done = 0;
288 }
289 return yytext[0];
290 }
291
292 ^"%%" {
293 BEGIN(INITIAL);
294 return PERCENTPERCENT;
295 }
296
297 . {
298 error_at_line (&lexer_line, "unexpected character `%s'", yytext);
299 }
300 }
301
302 "/*" { BEGIN(in_comment); }
303 \n { lexer_line.line++; }
304 {ID} |
305 "'"("\\".|[^\\])"'" |
306 [^"/\n] /* do nothing */
307 \"([^"\\]|\\.|\\\n)*\" { update_lineno (yytext, yyleng); }
308 "/"/[^*] /* do nothing */
309
310 <in_comment,in_struct_comment>{
311 \n { lexer_line.line++; }
312 [^*\n]{16} |
313 [^*\n] /* do nothing */
314 "*"/[^/] /* do nothing */
315 }
316 <in_comment>"*/" { BEGIN(INITIAL); }
317 <in_struct_comment>"*/" { BEGIN(in_struct); }
318
319 <in_yacc_escape>{
320 \n { lexer_line.line++; }
321 [^%]{16} |
322 [^%] /* do nothing */
323 "%"/[^}] /* do nothing */
324 "%}" { BEGIN(in_struct); }
325 "%" {
326 error_at_line (&lexer_line,
327 "unterminated %%{; unexpected EOF");
328 }
329 }
330
331
332 ["/] |
333 <in_struct_comment,in_comment>"*" {
334 error_at_line (&lexer_line,
335 "unterminated comment or string; unexpected EOF");
336 }
337
338 %%
339
340 void
341 yyerror (const char *s)
342 {
343 error_at_line (&lexer_line, s);
344 }
345
346 void
347 parse_file (const char *fname)
348 {
349 yyin = fopen (fname, "r");
350 lexer_line.file = fname;
351 lexer_line.line = 1;
352 if (yyin == NULL)
353 {
354 perror (fname);
355 exit (1);
356 }
357 if (yyparse() != 0)
358 exit (1);
359 fclose (yyin);
360 }