]>
Commit | Line | Data |
---|---|---|
27bf414c JM |
1 | /* Parser for C and Objective-C. |
2 | Copyright (C) 1987, 1988, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998, | |
3 | 1999, 2000, 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc. | |
4 | ||
5 | Parser actions based on the old Bison parser; structure somewhat | |
6 | influenced by and fragments based on the C++ parser. | |
7 | ||
8 | This file is part of GCC. | |
9 | ||
10 | GCC is free software; you can redistribute it and/or modify it under | |
11 | the terms of the GNU General Public License as published by the Free | |
12 | Software Foundation; either version 2, or (at your option) any later | |
13 | version. | |
14 | ||
15 | GCC is distributed in the hope that it will be useful, but WITHOUT ANY | |
16 | WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
17 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
18 | for more details. | |
19 | ||
20 | You should have received a copy of the GNU General Public License | |
21 | along with GCC; see the file COPYING. If not, write to the Free | |
22 | Software Foundation, 59 Temple Place - Suite 330, Boston, MA | |
23 | 02111-1307, USA. */ | |
24 | ||
25 | /* TODO: | |
26 | ||
27 | Make sure all relevant comments, and all relevant code from all | |
28 | actions, brought over from old parser. Verify exact correspondence | |
29 | of syntax accepted. | |
30 | ||
31 | Add testcases covering every input symbol in every state in old and | |
32 | new parsers. | |
33 | ||
34 | Include full syntax for GNU C, including erroneous cases accepted | |
35 | with error messages, in syntax productions in comments. | |
36 | ||
37 | Make more diagnostics in the front end generally take an explicit | |
38 | location rather than implicitly using input_location. */ | |
39 | ||
40 | #include "config.h" | |
41 | #include "system.h" | |
42 | #include "coretypes.h" | |
43 | #include "tm.h" | |
44 | #include "tree.h" | |
45 | #include "langhooks.h" | |
46 | #include "input.h" | |
47 | #include "cpplib.h" | |
48 | #include "timevar.h" | |
49 | #include "c-pragma.h" | |
50 | #include "c-tree.h" | |
51 | #include "flags.h" | |
52 | #include "output.h" | |
53 | #include "toplev.h" | |
54 | #include "ggc.h" | |
55 | #include "c-common.h" | |
56 | ||
57 | \f | |
58 | /* Miscellaneous data and functions needed for the parser. */ | |
59 | ||
60 | int yydebug; | |
61 | ||
62 | /* Objective-C specific parser/lexer information. */ | |
63 | ||
64 | static int objc_pq_context = 0; | |
65 | ||
66 | /* The following flag is needed to contextualize Objective-C lexical | |
67 | analysis. In some cases (e.g., 'int NSObject;'), it is undesirable | |
68 | to bind an identifier to an Objective-C class, even if a class with | |
69 | that name exists. */ | |
70 | static int objc_need_raw_identifier = 0; | |
71 | #define OBJC_NEED_RAW_IDENTIFIER(VAL) \ | |
72 | do { \ | |
73 | if (c_dialect_objc ()) \ | |
74 | objc_need_raw_identifier = VAL; \ | |
75 | } while (0) | |
76 | ||
77 | /* The reserved keyword table. */ | |
78 | struct resword | |
79 | { | |
80 | const char *word; | |
81 | ENUM_BITFIELD(rid) rid : 16; | |
82 | unsigned int disable : 16; | |
83 | }; | |
84 | ||
85 | /* Disable mask. Keywords are disabled if (reswords[i].disable & | |
86 | mask) is _true_. */ | |
87 | #define D_C89 0x01 /* not in C89 */ | |
88 | #define D_EXT 0x02 /* GCC extension */ | |
89 | #define D_EXT89 0x04 /* GCC extension incorporated in C99 */ | |
90 | #define D_OBJC 0x08 /* Objective C only */ | |
91 | ||
92 | static const struct resword reswords[] = | |
93 | { | |
94 | { "_Bool", RID_BOOL, 0 }, | |
95 | { "_Complex", RID_COMPLEX, 0 }, | |
96 | { "__FUNCTION__", RID_FUNCTION_NAME, 0 }, | |
97 | { "__PRETTY_FUNCTION__", RID_PRETTY_FUNCTION_NAME, 0 }, | |
98 | { "__alignof", RID_ALIGNOF, 0 }, | |
99 | { "__alignof__", RID_ALIGNOF, 0 }, | |
100 | { "__asm", RID_ASM, 0 }, | |
101 | { "__asm__", RID_ASM, 0 }, | |
102 | { "__attribute", RID_ATTRIBUTE, 0 }, | |
103 | { "__attribute__", RID_ATTRIBUTE, 0 }, | |
104 | { "__builtin_choose_expr", RID_CHOOSE_EXPR, 0 }, | |
105 | { "__builtin_offsetof", RID_OFFSETOF, 0 }, | |
106 | { "__builtin_types_compatible_p", RID_TYPES_COMPATIBLE_P, 0 }, | |
107 | { "__builtin_va_arg", RID_VA_ARG, 0 }, | |
108 | { "__complex", RID_COMPLEX, 0 }, | |
109 | { "__complex__", RID_COMPLEX, 0 }, | |
110 | { "__const", RID_CONST, 0 }, | |
111 | { "__const__", RID_CONST, 0 }, | |
112 | { "__extension__", RID_EXTENSION, 0 }, | |
113 | { "__func__", RID_C99_FUNCTION_NAME, 0 }, | |
114 | { "__imag", RID_IMAGPART, 0 }, | |
115 | { "__imag__", RID_IMAGPART, 0 }, | |
116 | { "__inline", RID_INLINE, 0 }, | |
117 | { "__inline__", RID_INLINE, 0 }, | |
118 | { "__label__", RID_LABEL, 0 }, | |
119 | { "__real", RID_REALPART, 0 }, | |
120 | { "__real__", RID_REALPART, 0 }, | |
121 | { "__restrict", RID_RESTRICT, 0 }, | |
122 | { "__restrict__", RID_RESTRICT, 0 }, | |
123 | { "__signed", RID_SIGNED, 0 }, | |
124 | { "__signed__", RID_SIGNED, 0 }, | |
125 | { "__thread", RID_THREAD, 0 }, | |
126 | { "__typeof", RID_TYPEOF, 0 }, | |
127 | { "__typeof__", RID_TYPEOF, 0 }, | |
128 | { "__volatile", RID_VOLATILE, 0 }, | |
129 | { "__volatile__", RID_VOLATILE, 0 }, | |
130 | { "asm", RID_ASM, D_EXT }, | |
131 | { "auto", RID_AUTO, 0 }, | |
132 | { "break", RID_BREAK, 0 }, | |
133 | { "case", RID_CASE, 0 }, | |
134 | { "char", RID_CHAR, 0 }, | |
135 | { "const", RID_CONST, 0 }, | |
136 | { "continue", RID_CONTINUE, 0 }, | |
137 | { "default", RID_DEFAULT, 0 }, | |
138 | { "do", RID_DO, 0 }, | |
139 | { "double", RID_DOUBLE, 0 }, | |
140 | { "else", RID_ELSE, 0 }, | |
141 | { "enum", RID_ENUM, 0 }, | |
142 | { "extern", RID_EXTERN, 0 }, | |
143 | { "float", RID_FLOAT, 0 }, | |
144 | { "for", RID_FOR, 0 }, | |
145 | { "goto", RID_GOTO, 0 }, | |
146 | { "if", RID_IF, 0 }, | |
147 | { "inline", RID_INLINE, D_EXT89 }, | |
148 | { "int", RID_INT, 0 }, | |
149 | { "long", RID_LONG, 0 }, | |
150 | { "register", RID_REGISTER, 0 }, | |
151 | { "restrict", RID_RESTRICT, D_C89 }, | |
152 | { "return", RID_RETURN, 0 }, | |
153 | { "short", RID_SHORT, 0 }, | |
154 | { "signed", RID_SIGNED, 0 }, | |
155 | { "sizeof", RID_SIZEOF, 0 }, | |
156 | { "static", RID_STATIC, 0 }, | |
157 | { "struct", RID_STRUCT, 0 }, | |
158 | { "switch", RID_SWITCH, 0 }, | |
159 | { "typedef", RID_TYPEDEF, 0 }, | |
160 | { "typeof", RID_TYPEOF, D_EXT }, | |
161 | { "union", RID_UNION, 0 }, | |
162 | { "unsigned", RID_UNSIGNED, 0 }, | |
163 | { "void", RID_VOID, 0 }, | |
164 | { "volatile", RID_VOLATILE, 0 }, | |
165 | { "while", RID_WHILE, 0 }, | |
166 | /* These Objective-C keywords are recognized only immediately after | |
167 | an '@'. */ | |
168 | { "class", RID_AT_CLASS, D_OBJC }, | |
169 | { "compatibility_alias", RID_AT_ALIAS, D_OBJC }, | |
170 | { "defs", RID_AT_DEFS, D_OBJC }, | |
171 | { "encode", RID_AT_ENCODE, D_OBJC }, | |
172 | { "end", RID_AT_END, D_OBJC }, | |
173 | { "implementation", RID_AT_IMPLEMENTATION, D_OBJC }, | |
174 | { "interface", RID_AT_INTERFACE, D_OBJC }, | |
175 | { "private", RID_AT_PRIVATE, D_OBJC }, | |
176 | { "protected", RID_AT_PROTECTED, D_OBJC }, | |
177 | { "protocol", RID_AT_PROTOCOL, D_OBJC }, | |
178 | { "public", RID_AT_PUBLIC, D_OBJC }, | |
179 | { "selector", RID_AT_SELECTOR, D_OBJC }, | |
180 | { "throw", RID_AT_THROW, D_OBJC }, | |
181 | { "try", RID_AT_TRY, D_OBJC }, | |
182 | { "catch", RID_AT_CATCH, D_OBJC }, | |
183 | { "finally", RID_AT_FINALLY, D_OBJC }, | |
184 | { "synchronized", RID_AT_SYNCHRONIZED, D_OBJC }, | |
185 | /* These are recognized only in protocol-qualifier context | |
186 | (see above) */ | |
187 | { "bycopy", RID_BYCOPY, D_OBJC }, | |
188 | { "byref", RID_BYREF, D_OBJC }, | |
189 | { "in", RID_IN, D_OBJC }, | |
190 | { "inout", RID_INOUT, D_OBJC }, | |
191 | { "oneway", RID_ONEWAY, D_OBJC }, | |
192 | { "out", RID_OUT, D_OBJC }, | |
193 | }; | |
194 | #define N_reswords (sizeof reswords / sizeof (struct resword)) | |
195 | ||
196 | /* Initialization routine for this file. */ | |
197 | ||
198 | void | |
199 | c_parse_init (void) | |
200 | { | |
201 | /* The only initialization required is of the reserved word | |
202 | identifiers. */ | |
203 | unsigned int i; | |
204 | tree id; | |
205 | int mask = (flag_isoc99 ? 0 : D_C89) | |
206 | | (flag_no_asm ? (flag_isoc99 ? D_EXT : D_EXT|D_EXT89) : 0); | |
207 | ||
208 | if (!c_dialect_objc ()) | |
209 | mask |= D_OBJC; | |
210 | ||
211 | ridpointers = GGC_CNEWVEC (tree, (int) RID_MAX); | |
212 | for (i = 0; i < N_reswords; i++) | |
213 | { | |
214 | /* If a keyword is disabled, do not enter it into the table | |
215 | and so create a canonical spelling that isn't a keyword. */ | |
216 | if (reswords[i].disable & mask) | |
217 | continue; | |
218 | ||
219 | id = get_identifier (reswords[i].word); | |
220 | C_RID_CODE (id) = reswords[i].rid; | |
221 | C_IS_RESERVED_WORD (id) = 1; | |
222 | ridpointers [(int) reswords[i].rid] = id; | |
223 | } | |
224 | } | |
225 | \f | |
226 | /* The C lexer intermediates between the lexer in cpplib and c-lex.c | |
227 | and the C parser. Unlike the C++ lexer, the parser structure | |
228 | stores the lexer information instead of using a separate structure. | |
229 | Identifiers are separated into ordinary identifiers, type names, | |
230 | keywords and some other Objective-C types of identifiers, and some | |
231 | look-ahead is maintained. | |
232 | ||
233 | ??? It might be a good idea to lex the whole file up front (as for | |
234 | C++). It would then be possible to share more of the C and C++ | |
235 | lexer code, if desired. */ | |
236 | ||
237 | /* The following local token type is used. */ | |
238 | ||
239 | /* A keyword. */ | |
240 | #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1)) | |
241 | ||
27bf414c JM |
242 | /* More information about the type of a CPP_NAME token. */ |
243 | typedef enum c_id_kind { | |
244 | /* An ordinary identifier. */ | |
245 | C_ID_ID, | |
246 | /* An identifier declared as a typedef name. */ | |
247 | C_ID_TYPENAME, | |
248 | /* An identifier declared as an Objective-C class name. */ | |
249 | C_ID_CLASSNAME, | |
250 | /* Not an identifier. */ | |
251 | C_ID_NONE | |
252 | } c_id_kind; | |
253 | ||
254 | /* A single C token after string literal concatenation and conversion | |
255 | of preprocessing tokens to tokens. */ | |
256 | typedef struct c_token GTY (()) | |
257 | { | |
258 | /* The kind of token. */ | |
259 | ENUM_BITFIELD (cpp_ttype) type : 8; | |
260 | /* If this token is a CPP_NAME, this value indicates whether also | |
261 | declared as some kind of type. Otherwise, it is C_ID_NONE. */ | |
262 | ENUM_BITFIELD (c_id_kind) id_kind : 8; | |
263 | /* If this token is a keyword, this value indicates which keyword. | |
264 | Otherwise, this value is RID_MAX. */ | |
265 | ENUM_BITFIELD (rid) keyword : 8; | |
266 | /* True if this token is from a system header. */ | |
267 | BOOL_BITFIELD in_system_header : 1; | |
268 | /* The value associated with this token, if any. */ | |
269 | tree value; | |
270 | /* The location at which this token was found. */ | |
271 | location_t location; | |
272 | } c_token; | |
273 | ||
274 | /* A parser structure recording information about the state and | |
275 | context of parsing. Includes lexer information with up to two | |
276 | tokens of look-ahead; more are not needed for C. */ | |
277 | typedef struct c_parser GTY(()) | |
278 | { | |
279 | /* The look-ahead tokens. */ | |
280 | c_token tokens[2]; | |
281 | /* How many look-ahead tokens are available (0, 1 or 2). */ | |
282 | short tokens_avail; | |
283 | /* True if a syntax error is being recovered from; false otherwise. | |
284 | c_parser_error sets this flag. It should clear this flag when | |
285 | enough tokens have been consumed to recover from the error. */ | |
286 | BOOL_BITFIELD error : 1; | |
287 | } c_parser; | |
288 | ||
289 | /* Read in and lex a single token, storing it in *TOKEN. */ | |
290 | ||
291 | static void | |
292 | c_lex_one_token (c_token *token) | |
293 | { | |
294 | timevar_push (TV_LEX); | |
6037d88d | 295 | token->type = c_lex_with_flags (&token->value, &token->location, NULL); |
27bf414c JM |
296 | token->in_system_header = in_system_header; |
297 | switch (token->type) | |
298 | { | |
299 | case CPP_NAME: | |
300 | token->id_kind = C_ID_NONE; | |
301 | token->keyword = RID_MAX; | |
302 | { | |
303 | tree decl; | |
304 | ||
305 | int objc_force_identifier = objc_need_raw_identifier; | |
306 | OBJC_NEED_RAW_IDENTIFIER (0); | |
307 | ||
308 | if (C_IS_RESERVED_WORD (token->value)) | |
309 | { | |
310 | enum rid rid_code = C_RID_CODE (token->value); | |
311 | ||
312 | if (c_dialect_objc ()) | |
313 | { | |
314 | if (!OBJC_IS_AT_KEYWORD (rid_code) | |
315 | && (!OBJC_IS_PQ_KEYWORD (rid_code) || objc_pq_context)) | |
316 | { | |
317 | /* Return the canonical spelling for this keyword. */ | |
318 | token->value = ridpointers[(int) rid_code]; | |
319 | token->type = CPP_KEYWORD; | |
320 | token->keyword = rid_code; | |
321 | break; | |
322 | } | |
323 | } | |
324 | else | |
325 | { | |
326 | /* Return the canonical spelling for this keyword. */ | |
327 | token->value = ridpointers[(int) rid_code]; | |
328 | token->type = CPP_KEYWORD; | |
329 | token->keyword = rid_code; | |
330 | break; | |
331 | } | |
332 | } | |
333 | ||
334 | decl = lookup_name (token->value); | |
335 | if (decl) | |
336 | { | |
337 | if (TREE_CODE (decl) == TYPE_DECL) | |
338 | { | |
339 | token->id_kind = C_ID_TYPENAME; | |
340 | break; | |
341 | } | |
342 | } | |
343 | else if (c_dialect_objc ()) | |
344 | { | |
345 | tree objc_interface_decl = objc_is_class_name (token->value); | |
346 | /* Objective-C class names are in the same namespace as | |
347 | variables and typedefs, and hence are shadowed by local | |
348 | declarations. */ | |
349 | if (objc_interface_decl | |
350 | && (global_bindings_p () | |
351 | || (!objc_force_identifier && !decl))) | |
352 | { | |
353 | token->value = objc_interface_decl; | |
354 | token->id_kind = C_ID_CLASSNAME; | |
355 | break; | |
356 | } | |
357 | } | |
358 | } | |
359 | token->id_kind = C_ID_ID; | |
360 | break; | |
361 | case CPP_AT_NAME: | |
362 | /* This only happens in Objective-C; it must be a keyword. */ | |
363 | token->type = CPP_KEYWORD; | |
364 | token->id_kind = C_ID_NONE; | |
365 | token->keyword = C_RID_CODE (token->value); | |
366 | break; | |
367 | case CPP_COLON: | |
368 | case CPP_COMMA: | |
369 | case CPP_CLOSE_PAREN: | |
370 | case CPP_SEMICOLON: | |
371 | /* These tokens may affect the interpretation of any identifiers | |
372 | following, if doing Objective-C. */ | |
373 | OBJC_NEED_RAW_IDENTIFIER (0); | |
374 | token->id_kind = C_ID_NONE; | |
375 | token->keyword = RID_MAX; | |
376 | break; | |
377 | default: | |
378 | token->id_kind = C_ID_NONE; | |
379 | token->keyword = RID_MAX; | |
380 | break; | |
381 | } | |
382 | timevar_pop (TV_LEX); | |
383 | } | |
384 | ||
385 | /* Return a pointer to the next token from PARSER, reading it in if | |
386 | necessary. */ | |
387 | ||
388 | static inline c_token * | |
389 | c_parser_peek_token (c_parser *parser) | |
390 | { | |
391 | if (parser->tokens_avail == 0) | |
392 | { | |
393 | c_lex_one_token (&parser->tokens[0]); | |
394 | parser->tokens_avail = 1; | |
395 | } | |
396 | return &parser->tokens[0]; | |
397 | } | |
398 | ||
399 | /* Return true if the next token from PARSER has the indicated | |
400 | TYPE. */ | |
401 | ||
402 | static inline bool | |
403 | c_parser_next_token_is (c_parser *parser, enum cpp_ttype type) | |
404 | { | |
405 | return c_parser_peek_token (parser)->type == type; | |
406 | } | |
407 | ||
408 | /* Return true if the next token from PARSER does not have the | |
409 | indicated TYPE. */ | |
410 | ||
411 | static inline bool | |
412 | c_parser_next_token_is_not (c_parser *parser, enum cpp_ttype type) | |
413 | { | |
414 | return !c_parser_next_token_is (parser, type); | |
415 | } | |
416 | ||
417 | /* Return true if the next token from PARSER is the indicated | |
418 | KEYWORD. */ | |
419 | ||
420 | static inline bool | |
421 | c_parser_next_token_is_keyword (c_parser *parser, enum rid keyword) | |
422 | { | |
423 | c_token *token; | |
424 | ||
425 | /* Peek at the next token. */ | |
426 | token = c_parser_peek_token (parser); | |
427 | /* Check to see if it is the indicated keyword. */ | |
428 | return token->keyword == keyword; | |
429 | } | |
430 | ||
431 | /* Return true if TOKEN can start a type name, | |
432 | false otherwise. */ | |
433 | static bool | |
434 | c_token_starts_typename (c_token *token) | |
435 | { | |
436 | switch (token->type) | |
437 | { | |
438 | case CPP_NAME: | |
439 | switch (token->id_kind) | |
440 | { | |
441 | case C_ID_ID: | |
442 | return false; | |
443 | case C_ID_TYPENAME: | |
444 | return true; | |
445 | case C_ID_CLASSNAME: | |
446 | gcc_assert (c_dialect_objc ()); | |
447 | return true; | |
448 | default: | |
449 | gcc_unreachable (); | |
450 | } | |
451 | case CPP_KEYWORD: | |
452 | switch (token->keyword) | |
453 | { | |
454 | case RID_UNSIGNED: | |
455 | case RID_LONG: | |
456 | case RID_SHORT: | |
457 | case RID_SIGNED: | |
458 | case RID_COMPLEX: | |
459 | case RID_INT: | |
460 | case RID_CHAR: | |
461 | case RID_FLOAT: | |
462 | case RID_DOUBLE: | |
463 | case RID_VOID: | |
464 | case RID_BOOL: | |
465 | case RID_ENUM: | |
466 | case RID_STRUCT: | |
467 | case RID_UNION: | |
468 | case RID_TYPEOF: | |
469 | case RID_CONST: | |
470 | case RID_VOLATILE: | |
471 | case RID_RESTRICT: | |
472 | case RID_ATTRIBUTE: | |
473 | return true; | |
474 | default: | |
475 | return false; | |
476 | } | |
477 | case CPP_LESS: | |
478 | if (c_dialect_objc ()) | |
479 | return true; | |
480 | return false; | |
481 | default: | |
482 | return false; | |
483 | } | |
484 | } | |
485 | ||
486 | /* Return true if the next token from PARSER can start a type name, | |
487 | false otherwise. */ | |
488 | static inline bool | |
489 | c_parser_next_token_starts_typename (c_parser *parser) | |
490 | { | |
491 | c_token *token = c_parser_peek_token (parser); | |
492 | return c_token_starts_typename (token); | |
493 | } | |
494 | ||
495 | /* Return true if TOKEN can start declaration specifiers, false | |
496 | otherwise. */ | |
497 | static bool | |
498 | c_token_starts_declspecs (c_token *token) | |
499 | { | |
500 | switch (token->type) | |
501 | { | |
502 | case CPP_NAME: | |
503 | switch (token->id_kind) | |
504 | { | |
505 | case C_ID_ID: | |
506 | return false; | |
507 | case C_ID_TYPENAME: | |
508 | return true; | |
509 | case C_ID_CLASSNAME: | |
510 | gcc_assert (c_dialect_objc ()); | |
511 | return true; | |
512 | default: | |
513 | gcc_unreachable (); | |
514 | } | |
515 | case CPP_KEYWORD: | |
516 | switch (token->keyword) | |
517 | { | |
518 | case RID_STATIC: | |
519 | case RID_EXTERN: | |
520 | case RID_REGISTER: | |
521 | case RID_TYPEDEF: | |
522 | case RID_INLINE: | |
523 | case RID_AUTO: | |
524 | case RID_THREAD: | |
525 | case RID_UNSIGNED: | |
526 | case RID_LONG: | |
527 | case RID_SHORT: | |
528 | case RID_SIGNED: | |
529 | case RID_COMPLEX: | |
530 | case RID_INT: | |
531 | case RID_CHAR: | |
532 | case RID_FLOAT: | |
533 | case RID_DOUBLE: | |
534 | case RID_VOID: | |
535 | case RID_BOOL: | |
536 | case RID_ENUM: | |
537 | case RID_STRUCT: | |
538 | case RID_UNION: | |
539 | case RID_TYPEOF: | |
540 | case RID_CONST: | |
541 | case RID_VOLATILE: | |
542 | case RID_RESTRICT: | |
543 | case RID_ATTRIBUTE: | |
544 | return true; | |
545 | default: | |
546 | return false; | |
547 | } | |
548 | case CPP_LESS: | |
549 | if (c_dialect_objc ()) | |
550 | return true; | |
551 | return false; | |
552 | default: | |
553 | return false; | |
554 | } | |
555 | } | |
556 | ||
557 | /* Return true if the next token from PARSER can start declaration | |
558 | specifiers, false otherwise. */ | |
559 | static inline bool | |
560 | c_parser_next_token_starts_declspecs (c_parser *parser) | |
561 | { | |
562 | c_token *token = c_parser_peek_token (parser); | |
563 | return c_token_starts_declspecs (token); | |
564 | } | |
565 | ||
566 | /* Return a pointer to the next-but-one token from PARSER, reading it | |
567 | in if necessary. The next token is already read in. */ | |
568 | ||
569 | static c_token * | |
570 | c_parser_peek_2nd_token (c_parser *parser) | |
571 | { | |
572 | if (parser->tokens_avail >= 2) | |
573 | return &parser->tokens[1]; | |
574 | gcc_assert (parser->tokens_avail == 1); | |
575 | gcc_assert (parser->tokens[0].type != CPP_EOF); | |
576 | c_lex_one_token (&parser->tokens[1]); | |
577 | parser->tokens_avail = 2; | |
578 | return &parser->tokens[1]; | |
579 | } | |
580 | ||
581 | /* Consume the next token from PARSER. */ | |
582 | ||
583 | static void | |
584 | c_parser_consume_token (c_parser *parser) | |
585 | { | |
586 | if (parser->tokens_avail == 2) | |
587 | parser->tokens[0] = parser->tokens[1]; | |
588 | else | |
589 | { | |
590 | gcc_assert (parser->tokens_avail == 1); | |
591 | gcc_assert (parser->tokens[0].type != CPP_EOF); | |
592 | } | |
593 | parser->tokens_avail--; | |
594 | } | |
595 | ||
596 | /* Update the globals input_location and in_system_header from | |
597 | TOKEN. */ | |
598 | static inline void | |
599 | c_parser_set_source_position_from_token (c_token *token) | |
600 | { | |
601 | if (token->type != CPP_EOF) | |
602 | { | |
603 | input_location = token->location; | |
604 | in_system_header = token->in_system_header; | |
605 | } | |
606 | } | |
607 | ||
608 | /* Allocate a new parser. */ | |
609 | ||
610 | static c_parser * | |
611 | c_parser_new (void) | |
612 | { | |
613 | /* Use local storage to lex the first token because loading a PCH | |
614 | file may cause garbage collection. */ | |
615 | c_parser tparser; | |
616 | c_parser *ret; | |
617 | memset (&tparser, 0, sizeof tparser); | |
618 | c_lex_one_token (&tparser.tokens[0]); | |
619 | tparser.tokens_avail = 1; | |
620 | ret = GGC_NEW (c_parser); | |
621 | memcpy (ret, &tparser, sizeof tparser); | |
622 | return ret; | |
623 | } | |
624 | ||
625 | /* Issue a diagnostic of the form | |
626 | FILE:LINE: MESSAGE before TOKEN | |
627 | where TOKEN is the next token in the input stream of PARSER. | |
628 | MESSAGE (specified by the caller) is usually of the form "expected | |
629 | OTHER-TOKEN". | |
630 | ||
631 | Do not issue a diagnostic if still recovering from an error. | |
632 | ||
633 | ??? This is taken from the C++ parser, but building up messages in | |
634 | this way is not i18n-friendly and some other approach should be | |
635 | used. */ | |
636 | ||
637 | static void | |
638 | c_parser_error (c_parser *parser, const char *msgid) | |
639 | { | |
640 | c_token *token = c_parser_peek_token (parser); | |
641 | if (parser->error) | |
642 | return; | |
643 | parser->error = true; | |
644 | if (!msgid) | |
645 | return; | |
646 | /* This diagnostic makes more sense if it is tagged to the line of | |
647 | the token we just peeked at. */ | |
648 | c_parser_set_source_position_from_token (token); | |
649 | c_parse_error (msgid, | |
650 | /* Because c_parse_error does not understand | |
651 | CPP_KEYWORD, keywords are treated like | |
652 | identifiers. */ | |
653 | (token->type == CPP_KEYWORD ? CPP_NAME : token->type), | |
654 | token->value); | |
655 | } | |
656 | ||
657 | /* If the next token is of the indicated TYPE, consume it. Otherwise, | |
658 | issue the error MSGID. If MSGID is NULL then a message has already | |
659 | been produced and no message will be produced this time. Returns | |
660 | true if found, false otherwise. */ | |
661 | ||
662 | static bool | |
663 | c_parser_require (c_parser *parser, | |
664 | enum cpp_ttype type, | |
665 | const char *msgid) | |
666 | { | |
667 | if (c_parser_next_token_is (parser, type)) | |
668 | { | |
669 | c_parser_consume_token (parser); | |
670 | return true; | |
671 | } | |
672 | else | |
673 | { | |
674 | c_parser_error (parser, msgid); | |
675 | return false; | |
676 | } | |
677 | } | |
678 | ||
679 | /* If the next token is the indicated keyword, consume it. Otherwise, | |
680 | issue the error MSGID. Returns true if found, false otherwise. */ | |
681 | ||
682 | static bool | |
683 | c_parser_require_keyword (c_parser *parser, | |
684 | enum rid keyword, | |
685 | const char *msgid) | |
686 | { | |
687 | if (c_parser_next_token_is_keyword (parser, keyword)) | |
688 | { | |
689 | c_parser_consume_token (parser); | |
690 | return true; | |
691 | } | |
692 | else | |
693 | { | |
694 | c_parser_error (parser, msgid); | |
695 | return false; | |
696 | } | |
697 | } | |
698 | ||
699 | /* Like c_parser_require, except that tokens will be skipped until the | |
700 | desired token is found. An error message is still produced if the | |
701 | next token is not as expected. If MSGID is NULL then a message has | |
702 | already been produced and no message will be produced this | |
703 | time. */ | |
704 | ||
705 | static void | |
706 | c_parser_skip_until_found (c_parser *parser, | |
707 | enum cpp_ttype type, | |
708 | const char *msgid) | |
709 | { | |
710 | unsigned nesting_depth = 0; | |
711 | ||
712 | if (c_parser_require (parser, type, msgid)) | |
713 | return; | |
714 | ||
715 | /* Skip tokens until the desired token is found. */ | |
716 | while (true) | |
717 | { | |
718 | /* Peek at the next token. */ | |
719 | c_token *token = c_parser_peek_token (parser); | |
720 | /* If we've reached the token we want, consume it and stop. */ | |
721 | if (token->type == type && !nesting_depth) | |
722 | { | |
723 | c_parser_consume_token (parser); | |
724 | break; | |
725 | } | |
726 | /* If we've run out of tokens, stop. */ | |
727 | if (token->type == CPP_EOF) | |
728 | return; | |
729 | if (token->type == CPP_OPEN_BRACE | |
730 | || token->type == CPP_OPEN_PAREN | |
731 | || token->type == CPP_OPEN_SQUARE) | |
732 | ++nesting_depth; | |
733 | else if (token->type == CPP_CLOSE_BRACE | |
734 | || token->type == CPP_CLOSE_PAREN | |
735 | || token->type == CPP_CLOSE_SQUARE) | |
736 | { | |
737 | if (nesting_depth-- == 0) | |
738 | break; | |
739 | } | |
740 | /* Consume this token. */ | |
741 | c_parser_consume_token (parser); | |
742 | } | |
743 | parser->error = false; | |
744 | } | |
745 | ||
746 | /* Skip tokens until the end of a parameter is found, but do not | |
747 | consume the comma, semicolon or closing delimiter. */ | |
748 | ||
749 | static void | |
750 | c_parser_skip_to_end_of_parameter (c_parser *parser) | |
751 | { | |
752 | unsigned nesting_depth = 0; | |
753 | ||
754 | while (true) | |
755 | { | |
756 | c_token *token = c_parser_peek_token (parser); | |
757 | if ((token->type == CPP_COMMA || token->type == CPP_SEMICOLON) | |
758 | && !nesting_depth) | |
759 | break; | |
760 | /* If we've run out of tokens, stop. */ | |
761 | if (token->type == CPP_EOF) | |
762 | return; | |
763 | if (token->type == CPP_OPEN_BRACE | |
764 | || token->type == CPP_OPEN_PAREN | |
765 | || token->type == CPP_OPEN_SQUARE) | |
766 | ++nesting_depth; | |
767 | else if (token->type == CPP_CLOSE_BRACE | |
768 | || token->type == CPP_CLOSE_PAREN | |
769 | || token->type == CPP_CLOSE_SQUARE) | |
770 | { | |
771 | if (nesting_depth-- == 0) | |
772 | break; | |
773 | } | |
774 | /* Consume this token. */ | |
775 | c_parser_consume_token (parser); | |
776 | } | |
777 | parser->error = false; | |
778 | } | |
779 | ||
780 | /* Skip tokens until we have consumed an entire block, or until we | |
781 | have consumed a non-nested ';'. */ | |
782 | ||
783 | static void | |
784 | c_parser_skip_to_end_of_block_or_statement (c_parser *parser) | |
785 | { | |
786 | unsigned nesting_depth = 0; | |
787 | ||
788 | while (true) | |
789 | { | |
790 | c_token *token; | |
791 | ||
792 | /* Peek at the next token. */ | |
793 | token = c_parser_peek_token (parser); | |
794 | /* If we've run out of tokens, stop. */ | |
795 | if (token->type == CPP_EOF) | |
796 | return; | |
797 | /* If the next token is a ';', we have reached the end of the | |
798 | statement. */ | |
799 | if (token->type == CPP_SEMICOLON && !nesting_depth) | |
800 | { | |
801 | /* Consume the ';'. */ | |
802 | c_parser_consume_token (parser); | |
803 | break; | |
804 | } | |
805 | /* If the next token is a non-nested '}', then we have reached | |
806 | the end of the current block. */ | |
807 | if (token->type == CPP_CLOSE_BRACE | |
808 | && (nesting_depth == 0 || --nesting_depth == 0)) | |
809 | { | |
810 | c_parser_consume_token (parser); | |
811 | break; | |
812 | } | |
813 | /* If it the next token is a '{', then we are entering a new | |
814 | block. Consume the entire block. */ | |
815 | if (token->type == CPP_OPEN_BRACE) | |
816 | ++nesting_depth; | |
817 | c_parser_consume_token (parser); | |
818 | } | |
819 | parser->error = false; | |
820 | } | |
821 | ||
822 | ||
823 | /* Save the warning flags which are controlled by __extension__. */ | |
824 | ||
825 | static inline int | |
826 | disable_extension_diagnostics (void) | |
827 | { | |
828 | int ret = (pedantic | |
829 | | (warn_pointer_arith << 1) | |
830 | | (warn_traditional << 2) | |
831 | | (flag_iso << 3)); | |
832 | pedantic = 0; | |
833 | warn_pointer_arith = 0; | |
834 | warn_traditional = 0; | |
835 | flag_iso = 0; | |
836 | return ret; | |
837 | } | |
838 | ||
839 | /* Restore the warning flags which are controlled by __extension__. | |
840 | FLAGS is the return value from disable_extension_diagnostics. */ | |
841 | ||
842 | static inline void | |
843 | restore_extension_diagnostics (int flags) | |
844 | { | |
845 | pedantic = flags & 1; | |
846 | warn_pointer_arith = (flags >> 1) & 1; | |
847 | warn_traditional = (flags >> 2) & 1; | |
848 | flag_iso = (flags >> 3) & 1; | |
849 | } | |
850 | ||
851 | /* Possibly kinds of declarator to parse. */ | |
852 | typedef enum c_dtr_syn { | |
853 | /* A normal declarator with an identifier. */ | |
854 | C_DTR_NORMAL, | |
855 | /* An abstract declarator (maybe empty). */ | |
856 | C_DTR_ABSTRACT, | |
857 | /* A parameter declarator: may be either, but after a type name does | |
858 | not redeclare a typedef name as an identifier if it can | |
859 | alternatively be interpreted as a typedef name; see DR#009, | |
860 | applied in C90 TC1, omitted from C99 and reapplied in C99 TC2 | |
861 | following DR#249. For example, given a typedef T, "int T" and | |
862 | "int *T" are valid parameter declarations redeclaring T, while | |
863 | "int (T)" and "int * (T)" and "int (T[])" and "int (T (int))" are | |
864 | abstract declarators rather than involving redundant parentheses; | |
865 | the same applies with attributes inside the parentheses before | |
866 | "T". */ | |
867 | C_DTR_PARM | |
868 | } c_dtr_syn; | |
869 | ||
870 | static void c_parser_external_declaration (c_parser *); | |
871 | static void c_parser_asm_definition (c_parser *); | |
872 | static void c_parser_declaration_or_fndef (c_parser *, bool, bool, bool, bool); | |
873 | static void c_parser_declspecs (c_parser *, struct c_declspecs *, bool, bool, | |
874 | bool); | |
875 | static struct c_typespec c_parser_enum_specifier (c_parser *); | |
876 | static struct c_typespec c_parser_struct_or_union_specifier (c_parser *); | |
877 | static tree c_parser_struct_declaration (c_parser *); | |
878 | static struct c_typespec c_parser_typeof_specifier (c_parser *); | |
879 | static struct c_declarator *c_parser_declarator (c_parser *, bool, c_dtr_syn, | |
880 | bool *); | |
881 | static struct c_declarator *c_parser_direct_declarator (c_parser *, bool, | |
882 | c_dtr_syn, bool *); | |
883 | static struct c_declarator *c_parser_direct_declarator_inner (c_parser *, | |
884 | bool, | |
885 | struct c_declarator *); | |
886 | static struct c_arg_info *c_parser_parms_declarator (c_parser *, bool, tree); | |
887 | static struct c_arg_info *c_parser_parms_list_declarator (c_parser *, tree); | |
888 | static struct c_parm *c_parser_parameter_declaration (c_parser *, tree); | |
889 | static tree c_parser_simple_asm_expr (c_parser *); | |
890 | static tree c_parser_attributes (c_parser *); | |
891 | static struct c_type_name *c_parser_type_name (c_parser *); | |
892 | static struct c_expr c_parser_initializer (c_parser *); | |
893 | static struct c_expr c_parser_braced_init (c_parser *, tree, bool); | |
894 | static void c_parser_initelt (c_parser *); | |
895 | static void c_parser_initval (c_parser *, struct c_expr *); | |
896 | static tree c_parser_compound_statement (c_parser *); | |
897 | static void c_parser_compound_statement_nostart (c_parser *); | |
898 | static void c_parser_label (c_parser *); | |
899 | static void c_parser_statement (c_parser *); | |
900 | static void c_parser_statement_after_labels (c_parser *); | |
901 | static void c_parser_if_statement (c_parser *); | |
902 | static void c_parser_switch_statement (c_parser *); | |
903 | static void c_parser_while_statement (c_parser *); | |
904 | static void c_parser_do_statement (c_parser *); | |
905 | static void c_parser_for_statement (c_parser *); | |
906 | static tree c_parser_asm_statement (c_parser *); | |
907 | static tree c_parser_asm_operands (c_parser *); | |
908 | static tree c_parser_asm_clobbers (c_parser *); | |
909 | static struct c_expr c_parser_expr_no_commas (c_parser *, struct c_expr *); | |
910 | static struct c_expr c_parser_conditional_expression (c_parser *, | |
911 | struct c_expr *); | |
912 | static struct c_expr c_parser_binary_expression (c_parser *, struct c_expr *); | |
913 | static struct c_expr c_parser_cast_expression (c_parser *, struct c_expr *); | |
914 | static struct c_expr c_parser_unary_expression (c_parser *); | |
915 | static struct c_expr c_parser_sizeof_expression (c_parser *); | |
916 | static struct c_expr c_parser_alignof_expression (c_parser *); | |
917 | static struct c_expr c_parser_postfix_expression (c_parser *); | |
918 | static struct c_expr c_parser_postfix_expression_after_paren_type (c_parser *, | |
919 | struct c_type_name *); | |
920 | static struct c_expr c_parser_postfix_expression_after_primary (c_parser *, | |
921 | struct c_expr); | |
922 | static struct c_expr c_parser_expression (c_parser *); | |
923 | static tree c_parser_expr_list (c_parser *); | |
924 | ||
925 | /* These Objective-C parser functions are only ever called when | |
926 | compiling Objective-C. */ | |
927 | static void c_parser_objc_class_definition (c_parser *); | |
928 | static void c_parser_objc_class_instance_variables (c_parser *); | |
929 | static void c_parser_objc_class_declaration (c_parser *); | |
930 | static void c_parser_objc_alias_declaration (c_parser *); | |
931 | static void c_parser_objc_protocol_definition (c_parser *); | |
932 | static enum tree_code c_parser_objc_method_type (c_parser *); | |
933 | static void c_parser_objc_method_definition (c_parser *); | |
934 | static void c_parser_objc_methodprotolist (c_parser *); | |
935 | static void c_parser_objc_methodproto (c_parser *); | |
936 | static tree c_parser_objc_method_decl (c_parser *); | |
937 | static tree c_parser_objc_type_name (c_parser *); | |
938 | static tree c_parser_objc_protocol_refs (c_parser *); | |
939 | static void c_parser_objc_try_catch_statement (c_parser *); | |
940 | static void c_parser_objc_synchronized_statement (c_parser *); | |
941 | static tree c_parser_objc_selector (c_parser *); | |
942 | static tree c_parser_objc_selector_arg (c_parser *); | |
943 | static tree c_parser_objc_receiver (c_parser *); | |
944 | static tree c_parser_objc_message_args (c_parser *); | |
945 | static tree c_parser_objc_keywordexpr (c_parser *); | |
946 | ||
947 | /* Parse a translation unit (C90 6.7, C99 6.9). | |
948 | ||
949 | translation-unit: | |
950 | external-declarations | |
951 | ||
952 | external-declarations: | |
953 | external-declaration | |
954 | external-declarations external-declaration | |
955 | ||
956 | GNU extensions: | |
957 | ||
958 | translation-unit: | |
959 | empty | |
960 | */ | |
961 | ||
962 | static void | |
963 | c_parser_translation_unit (c_parser *parser) | |
964 | { | |
965 | if (c_parser_next_token_is (parser, CPP_EOF)) | |
966 | { | |
967 | if (pedantic) | |
968 | pedwarn ("ISO C forbids an empty source file"); | |
969 | } | |
970 | else | |
971 | { | |
972 | void *obstack_position = obstack_alloc (&parser_obstack, 0); | |
973 | do | |
974 | { | |
975 | ggc_collect (); | |
976 | c_parser_external_declaration (parser); | |
977 | obstack_free (&parser_obstack, obstack_position); | |
978 | } | |
979 | while (c_parser_next_token_is_not (parser, CPP_EOF)); | |
980 | } | |
981 | } | |
982 | ||
983 | /* Parse an external declaration (C90 6.7, C99 6.9). | |
984 | ||
985 | external-declaration: | |
986 | function-definition | |
987 | declaration | |
988 | ||
989 | GNU extensions: | |
990 | ||
991 | external-declaration: | |
992 | asm-definition | |
993 | ; | |
994 | __extension__ external-declaration | |
995 | ||
996 | Objective-C: | |
997 | ||
998 | external-declaration: | |
999 | objc-class-definition | |
1000 | objc-class-declaration | |
1001 | objc-alias-declaration | |
1002 | objc-protocol-definition | |
1003 | objc-method-definition | |
1004 | @end | |
1005 | */ | |
1006 | ||
1007 | static void | |
1008 | c_parser_external_declaration (c_parser *parser) | |
1009 | { | |
1010 | int ext; | |
1011 | switch (c_parser_peek_token (parser)->type) | |
1012 | { | |
1013 | case CPP_KEYWORD: | |
1014 | switch (c_parser_peek_token (parser)->keyword) | |
1015 | { | |
1016 | case RID_EXTENSION: | |
1017 | ext = disable_extension_diagnostics (); | |
1018 | c_parser_consume_token (parser); | |
1019 | c_parser_external_declaration (parser); | |
1020 | restore_extension_diagnostics (ext); | |
1021 | break; | |
1022 | case RID_ASM: | |
1023 | c_parser_asm_definition (parser); | |
1024 | break; | |
1025 | case RID_AT_INTERFACE: | |
1026 | case RID_AT_IMPLEMENTATION: | |
1027 | gcc_assert (c_dialect_objc ()); | |
1028 | c_parser_objc_class_definition (parser); | |
1029 | break; | |
1030 | case RID_AT_CLASS: | |
1031 | gcc_assert (c_dialect_objc ()); | |
1032 | c_parser_objc_class_declaration (parser); | |
1033 | break; | |
1034 | case RID_AT_ALIAS: | |
1035 | gcc_assert (c_dialect_objc ()); | |
1036 | c_parser_objc_alias_declaration (parser); | |
1037 | break; | |
1038 | case RID_AT_PROTOCOL: | |
1039 | gcc_assert (c_dialect_objc ()); | |
1040 | c_parser_objc_protocol_definition (parser); | |
1041 | break; | |
1042 | case RID_AT_END: | |
1043 | gcc_assert (c_dialect_objc ()); | |
1044 | c_parser_consume_token (parser); | |
1045 | objc_finish_implementation (); | |
1046 | break; | |
1047 | default: | |
1048 | goto decl_or_fndef; | |
1049 | } | |
1050 | break; | |
1051 | case CPP_SEMICOLON: | |
1052 | if (pedantic) | |
1053 | pedwarn ("ISO C does not allow extra %<;%> outside of a function"); | |
1054 | c_parser_consume_token (parser); | |
1055 | break; | |
1056 | case CPP_PLUS: | |
1057 | case CPP_MINUS: | |
1058 | if (c_dialect_objc ()) | |
1059 | { | |
1060 | c_parser_objc_method_definition (parser); | |
1061 | break; | |
1062 | } | |
1063 | /* Else fall through, and yield a syntax error trying to parse | |
1064 | as a declaration or function definition. */ | |
1065 | default: | |
1066 | decl_or_fndef: | |
1067 | /* A declaration or a function definition. We can only tell | |
1068 | which after parsing the declaration specifiers, if any, and | |
1069 | the first declarator. */ | |
1070 | c_parser_declaration_or_fndef (parser, true, true, false, true); | |
1071 | break; | |
1072 | } | |
1073 | } | |
1074 | ||
1075 | /* Parse a declaration or function definition (C90 6.5, 6.7.1, C99 | |
1076 | 6.7, 6.9.1). If FNDEF_OK is true, a function definition is | |
1077 | accepted; otherwise (old-style parameter declarations) only other | |
1078 | declarations are accepted. If NESTED is true, we are inside a | |
1079 | function or parsing old-style parameter declarations; any functions | |
1080 | encountered are nested functions and declaration specifiers are | |
1081 | required; otherwise we are at top level and functions are normal | |
1082 | functions and declaration specifiers may be optional. If EMPTY_OK | |
1083 | is true, empty declarations are OK (subject to all other | |
1084 | constraints); otherwise (old-style parameter declarations) they are | |
1085 | diagnosed. If START_ATTR_OK is true, the declaration specifiers | |
1086 | may start with attributes; otherwise they may not. | |
1087 | ||
1088 | declaration: | |
1089 | declaration-specifiers init-declarator-list[opt] ; | |
1090 | ||
1091 | function-definition: | |
1092 | declaration-specifiers[opt] declarator declaration-list[opt] | |
1093 | compound-statement | |
1094 | ||
1095 | declaration-list: | |
1096 | declaration | |
1097 | declaration-list declaration | |
1098 | ||
1099 | init-declarator-list: | |
1100 | init-declarator | |
1101 | init-declarator-list , init-declarator | |
1102 | ||
1103 | init-declarator: | |
1104 | declarator simple-asm-expr[opt] attributes[opt] | |
1105 | declarator simple-asm-expr[opt] attributes[opt] = initializer | |
1106 | ||
1107 | GNU extensions: | |
1108 | ||
1109 | nested-function-definition: | |
1110 | declaration-specifiers declarator declaration-list[opt] | |
1111 | compound-statement | |
1112 | ||
1113 | The simple-asm-expr and attributes are GNU extensions. | |
1114 | ||
1115 | This function does not handle __extension__; that is handled in its | |
1116 | callers. ??? Following the old parser, __extension__ may start | |
1117 | external declarations, declarations in functions and declarations | |
1118 | at the start of "for" loops, but not old-style parameter | |
1119 | declarations. | |
1120 | ||
1121 | C99 requires declaration specifiers in a function definition; the | |
1122 | absence is diagnosed through the diagnosis of implicit int. In GNU | |
1123 | C we also allow but diagnose declarations without declaration | |
1124 | specifiers, but only at top level (elsewhere they conflict with | |
1125 | other syntax). */ | |
1126 | ||
1127 | static void | |
1128 | c_parser_declaration_or_fndef (c_parser *parser, bool fndef_ok, bool empty_ok, | |
1129 | bool nested, bool start_attr_ok) | |
1130 | { | |
1131 | struct c_declspecs *specs; | |
1132 | tree prefix_attrs; | |
1133 | tree all_prefix_attrs; | |
1134 | bool diagnosed_no_specs = false; | |
1135 | specs = build_null_declspecs (); | |
1136 | c_parser_declspecs (parser, specs, true, true, start_attr_ok); | |
1137 | if (parser->error) | |
1138 | { | |
1139 | c_parser_skip_to_end_of_block_or_statement (parser); | |
1140 | return; | |
1141 | } | |
1142 | if (nested && !specs->declspecs_seen_p) | |
1143 | { | |
1144 | c_parser_error (parser, "expected declaration specifiers"); | |
1145 | c_parser_skip_to_end_of_block_or_statement (parser); | |
1146 | return; | |
1147 | } | |
1148 | finish_declspecs (specs); | |
1149 | if (c_parser_next_token_is (parser, CPP_SEMICOLON)) | |
1150 | { | |
1151 | if (empty_ok) | |
1152 | shadow_tag (specs); | |
1153 | else | |
1154 | { | |
1155 | shadow_tag_warned (specs, 1); | |
1156 | pedwarn ("empty declaration"); | |
1157 | } | |
1158 | c_parser_consume_token (parser); | |
1159 | return; | |
1160 | } | |
1161 | pending_xref_error (); | |
1162 | prefix_attrs = specs->attrs; | |
1163 | all_prefix_attrs = prefix_attrs; | |
1164 | specs->attrs = NULL_TREE; | |
1165 | while (true) | |
1166 | { | |
1167 | struct c_declarator *declarator; | |
1168 | bool dummy = false; | |
1169 | tree fnbody; | |
1170 | /* Declaring either one or more declarators (in which case we | |
1171 | should diagnose if there were no declaration specifiers) or a | |
1172 | function definition (in which case the diagnostic for | |
1173 | implicit int suffices). */ | |
1174 | declarator = c_parser_declarator (parser, specs->type_seen_p, | |
1175 | C_DTR_NORMAL, &dummy); | |
1176 | if (declarator == NULL) | |
1177 | { | |
1178 | c_parser_skip_to_end_of_block_or_statement (parser); | |
1179 | return; | |
1180 | } | |
1181 | if (c_parser_next_token_is (parser, CPP_EQ) | |
1182 | || c_parser_next_token_is (parser, CPP_COMMA) | |
1183 | || c_parser_next_token_is (parser, CPP_SEMICOLON) | |
1184 | || c_parser_next_token_is_keyword (parser, RID_ASM) | |
1185 | || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE)) | |
1186 | { | |
1187 | tree asm_name = NULL_TREE; | |
1188 | tree postfix_attrs = NULL_TREE; | |
1189 | if (!diagnosed_no_specs && !specs->declspecs_seen_p) | |
1190 | { | |
1191 | diagnosed_no_specs = true; | |
1192 | pedwarn ("data definition has no type or storage class"); | |
1193 | } | |
1194 | /* Having seen a data definition, there cannot now be a | |
1195 | function definition. */ | |
1196 | fndef_ok = false; | |
1197 | if (c_parser_next_token_is_keyword (parser, RID_ASM)) | |
1198 | asm_name = c_parser_simple_asm_expr (parser); | |
1199 | if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE)) | |
1200 | postfix_attrs = c_parser_attributes (parser); | |
1201 | if (c_parser_next_token_is (parser, CPP_EQ)) | |
1202 | { | |
1203 | tree d; | |
1204 | struct c_expr init; | |
1205 | c_parser_consume_token (parser); | |
1206 | /* The declaration of the variable is in effect while | |
1207 | its initializer is parsed. */ | |
1208 | d = start_decl (declarator, specs, true, | |
1209 | chainon (postfix_attrs, all_prefix_attrs)); | |
1210 | if (!d) | |
1211 | d = error_mark_node; | |
1212 | start_init (d, asm_name, global_bindings_p ()); | |
1213 | init = c_parser_initializer (parser); | |
1214 | finish_init (); | |
1215 | if (d != error_mark_node) | |
1216 | { | |
1217 | maybe_warn_string_init (TREE_TYPE (d), init); | |
1218 | finish_decl (d, init.value, asm_name); | |
1219 | } | |
1220 | } | |
1221 | else | |
1222 | { | |
1223 | tree d = start_decl (declarator, specs, false, | |
1224 | chainon (postfix_attrs, | |
1225 | all_prefix_attrs)); | |
1226 | if (d) | |
1227 | finish_decl (d, NULL_TREE, asm_name); | |
1228 | } | |
1229 | if (c_parser_next_token_is (parser, CPP_COMMA)) | |
1230 | { | |
1231 | c_parser_consume_token (parser); | |
1232 | if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE)) | |
1233 | all_prefix_attrs = chainon (c_parser_attributes (parser), | |
1234 | prefix_attrs); | |
1235 | else | |
1236 | all_prefix_attrs = prefix_attrs; | |
1237 | continue; | |
1238 | } | |
1239 | else if (c_parser_next_token_is (parser, CPP_SEMICOLON)) | |
1240 | { | |
1241 | c_parser_consume_token (parser); | |
1242 | return; | |
1243 | } | |
1244 | else | |
1245 | { | |
1246 | c_parser_error (parser, "expected %<,%> or %<;%>"); | |
1247 | c_parser_skip_to_end_of_block_or_statement (parser); | |
1248 | return; | |
1249 | } | |
1250 | } | |
1251 | else if (!fndef_ok) | |
1252 | { | |
1253 | c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, " | |
1254 | "%<asm%> or %<__attribute__%>"); | |
1255 | c_parser_skip_to_end_of_block_or_statement (parser); | |
1256 | return; | |
1257 | } | |
1258 | /* Function definition (nested or otherwise). */ | |
1259 | if (nested) | |
1260 | { | |
1261 | if (pedantic) | |
1262 | pedwarn ("ISO C forbids nested functions"); | |
1263 | push_function_context (); | |
1264 | } | |
1265 | if (!start_function (specs, declarator, all_prefix_attrs)) | |
1266 | { | |
1267 | /* This can appear in many cases looking nothing like a | |
1268 | function definition, so we don't give a more specific | |
1269 | error suggesting there was one. */ | |
1270 | c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, %<asm%> " | |
1271 | "or %<__attribute__%>"); | |
1272 | if (nested) | |
1273 | pop_function_context (); | |
1274 | break; | |
1275 | } | |
1276 | /* Parse old-style parameter declarations. ??? Attributes are | |
1277 | not allowed to start declaration specifiers here because of a | |
1278 | syntax conflict between a function declaration with attribute | |
1279 | suffix and a function definition with an attribute prefix on | |
1280 | first old-style parameter declaration. Following the old | |
1281 | parser, they are not accepted on subsequent old-style | |
1282 | parameter declarations either. However, there is no | |
1283 | ambiguity after the first declaration, nor indeed on the | |
1284 | first as long as we don't allow postfix attributes after a | |
1285 | declarator with a nonempty identifier list in a definition; | |
1286 | and postfix attributes have never been accepted here in | |
1287 | function definitions either. */ | |
1288 | while (c_parser_next_token_is_not (parser, CPP_EOF) | |
1289 | && c_parser_next_token_is_not (parser, CPP_OPEN_BRACE)) | |
1290 | c_parser_declaration_or_fndef (parser, false, false, true, false); | |
1291 | DECL_SOURCE_LOCATION (current_function_decl) | |
1292 | = c_parser_peek_token (parser)->location; | |
1293 | store_parm_decls (); | |
1294 | fnbody = c_parser_compound_statement (parser); | |
1295 | if (nested) | |
1296 | { | |
1297 | tree decl = current_function_decl; | |
1298 | add_stmt (fnbody); | |
1299 | finish_function (); | |
1300 | pop_function_context (); | |
1301 | add_stmt (build_stmt (DECL_EXPR, decl)); | |
1302 | } | |
1303 | else | |
1304 | { | |
1305 | add_stmt (fnbody); | |
1306 | finish_function (); | |
1307 | } | |
1308 | break; | |
1309 | } | |
1310 | } | |
1311 | ||
1312 | /* Parse an asm-definition (asm() outside a function body). This is a | |
1313 | GNU extension. | |
1314 | ||
1315 | asm-definition: | |
1316 | simple-asm-expr ; | |
1317 | */ | |
1318 | ||
1319 | static void | |
1320 | c_parser_asm_definition (c_parser *parser) | |
1321 | { | |
1322 | tree asm_str = c_parser_simple_asm_expr (parser); | |
1323 | /* ??? This only works sensibly in the presence of | |
1324 | -fno-unit-at-a-time; file-scope asms really need to be passed to | |
1325 | cgraph which needs to preserve the order of functions and | |
1326 | file-scope asms. */ | |
1327 | if (asm_str) | |
1328 | assemble_asm (asm_str); | |
1329 | c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>"); | |
1330 | } | |
1331 | ||
1332 | /* Parse some declaration specifiers (possibly none) (C90 6.5, C99 | |
1333 | 6.7), adding them to SPECS (which may already include some). | |
1334 | Storage class specifiers are accepted iff SCSPEC_OK; type | |
1335 | specifiers are accepted iff TYPESPEC_OK; attributes are accepted at | |
1336 | the start iff START_ATTR_OK. | |
1337 | ||
1338 | declaration-specifiers: | |
1339 | storage-class-specifier declaration-specifiers[opt] | |
1340 | type-specifier declaration-specifiers[opt] | |
1341 | type-qualifier declaration-specifiers[opt] | |
1342 | function-specifier declaration-specifiers[opt] | |
1343 | ||
1344 | Function specifiers (inline) are from C99, and are currently | |
1345 | handled as storage class specifiers, as is __thread. | |
1346 | ||
1347 | C90 6.5.1, C99 6.7.1: | |
1348 | storage-class-specifier: | |
1349 | typedef | |
1350 | extern | |
1351 | static | |
1352 | auto | |
1353 | register | |
1354 | ||
1355 | C99 6.7.4: | |
1356 | function-specifier: | |
1357 | inline | |
1358 | ||
1359 | C90 6.5.2, C99 6.7.2: | |
1360 | type-specifier: | |
1361 | void | |
1362 | char | |
1363 | short | |
1364 | int | |
1365 | long | |
1366 | float | |
1367 | double | |
1368 | signed | |
1369 | unsigned | |
1370 | _Bool | |
1371 | _Complex | |
1372 | [_Imaginary removed in C99 TC2] | |
1373 | struct-or-union-specifier | |
1374 | enum-specifier | |
1375 | typedef-name | |
1376 | ||
1377 | (_Bool and _Complex are new in C99.) | |
1378 | ||
1379 | C90 6.5.3, C99 6.7.3: | |
1380 | ||
1381 | type-qualifier: | |
1382 | const | |
1383 | restrict | |
1384 | volatile | |
1385 | ||
1386 | (restrict is new in C99.) | |
1387 | ||
1388 | GNU extensions: | |
1389 | ||
1390 | declaration-specifiers: | |
1391 | attributes declaration-specifiers[opt] | |
1392 | ||
1393 | storage-class-specifier: | |
1394 | __thread | |
1395 | ||
1396 | type-specifier: | |
1397 | typeof-specifier | |
1398 | ||
1399 | Objective-C: | |
1400 | ||
1401 | type-specifier: | |
1402 | class-name objc-protocol-refs[opt] | |
1403 | typedef-name objc-protocol-refs | |
1404 | objc-protocol-refs | |
1405 | */ | |
1406 | ||
1407 | static void | |
1408 | c_parser_declspecs (c_parser *parser, struct c_declspecs *specs, | |
1409 | bool scspec_ok, bool typespec_ok, bool start_attr_ok) | |
1410 | { | |
1411 | bool attrs_ok = start_attr_ok; | |
1412 | bool seen_type = specs->type_seen_p; | |
1413 | while (c_parser_next_token_is (parser, CPP_NAME) | |
1414 | || c_parser_next_token_is (parser, CPP_KEYWORD) | |
1415 | || (c_dialect_objc () && c_parser_next_token_is (parser, CPP_LESS))) | |
1416 | { | |
1417 | struct c_typespec t; | |
1418 | tree attrs; | |
1419 | if (c_parser_next_token_is (parser, CPP_NAME)) | |
1420 | { | |
1421 | tree value = c_parser_peek_token (parser)->value; | |
1422 | c_id_kind kind = c_parser_peek_token (parser)->id_kind; | |
1423 | /* This finishes the specifiers unless a type name is OK, it | |
1424 | is declared as a type name and a type name hasn't yet | |
1425 | been seen. */ | |
1426 | if (!typespec_ok || seen_type | |
1427 | || (kind != C_ID_TYPENAME && kind != C_ID_CLASSNAME)) | |
1428 | break; | |
1429 | c_parser_consume_token (parser); | |
1430 | seen_type = true; | |
1431 | attrs_ok = true; | |
1432 | if (kind == C_ID_TYPENAME | |
1433 | && (!c_dialect_objc () | |
1434 | || c_parser_next_token_is_not (parser, CPP_LESS))) | |
1435 | { | |
1436 | t.kind = ctsk_typedef; | |
1437 | /* For a typedef name, record the meaning, not the name. | |
1438 | In case of 'foo foo, bar;'. */ | |
1439 | t.spec = lookup_name (value); | |
1440 | } | |
1441 | else | |
1442 | { | |
1443 | tree proto = NULL_TREE; | |
1444 | gcc_assert (c_dialect_objc ()); | |
1445 | t.kind = ctsk_objc; | |
1446 | if (c_parser_next_token_is (parser, CPP_LESS)) | |
1447 | proto = c_parser_objc_protocol_refs (parser); | |
1448 | t.spec = objc_get_protocol_qualified_type (value, proto); | |
1449 | } | |
1450 | declspecs_add_type (specs, t); | |
1451 | continue; | |
1452 | } | |
1453 | if (c_parser_next_token_is (parser, CPP_LESS)) | |
1454 | { | |
1455 | /* Make "<SomeProtocol>" equivalent to "id <SomeProtocol>" - | |
1456 | nisse@lysator.liu.se. */ | |
1457 | tree proto; | |
1458 | gcc_assert (c_dialect_objc ()); | |
1459 | if (!typespec_ok || seen_type) | |
1460 | break; | |
1461 | proto = c_parser_objc_protocol_refs (parser); | |
1462 | t.kind = ctsk_objc; | |
1463 | t.spec = objc_get_protocol_qualified_type (NULL_TREE, proto); | |
1464 | declspecs_add_type (specs, t); | |
1465 | continue; | |
1466 | } | |
1467 | gcc_assert (c_parser_next_token_is (parser, CPP_KEYWORD)); | |
1468 | switch (c_parser_peek_token (parser)->keyword) | |
1469 | { | |
1470 | case RID_STATIC: | |
1471 | case RID_EXTERN: | |
1472 | case RID_REGISTER: | |
1473 | case RID_TYPEDEF: | |
1474 | case RID_INLINE: | |
1475 | case RID_AUTO: | |
1476 | case RID_THREAD: | |
1477 | if (!scspec_ok) | |
1478 | goto out; | |
1479 | attrs_ok = true; | |
1480 | /* TODO: Distinguish between function specifiers (inline) | |
1481 | and storage class specifiers, either here or in | |
1482 | declspecs_add_scspec. */ | |
1483 | declspecs_add_scspec (specs, c_parser_peek_token (parser)->value); | |
1484 | c_parser_consume_token (parser); | |
1485 | break; | |
1486 | case RID_UNSIGNED: | |
1487 | case RID_LONG: | |
1488 | case RID_SHORT: | |
1489 | case RID_SIGNED: | |
1490 | case RID_COMPLEX: | |
1491 | case RID_INT: | |
1492 | case RID_CHAR: | |
1493 | case RID_FLOAT: | |
1494 | case RID_DOUBLE: | |
1495 | case RID_VOID: | |
1496 | case RID_BOOL: | |
1497 | if (!typespec_ok) | |
1498 | goto out; | |
1499 | attrs_ok = true; | |
1500 | seen_type = true; | |
1501 | OBJC_NEED_RAW_IDENTIFIER (1); | |
1502 | t.kind = ctsk_resword; | |
1503 | t.spec = c_parser_peek_token (parser)->value; | |
1504 | declspecs_add_type (specs, t); | |
1505 | c_parser_consume_token (parser); | |
1506 | break; | |
1507 | case RID_ENUM: | |
1508 | if (!typespec_ok) | |
1509 | goto out; | |
1510 | attrs_ok = true; | |
1511 | seen_type = true; | |
1512 | t = c_parser_enum_specifier (parser); | |
1513 | declspecs_add_type (specs, t); | |
1514 | break; | |
1515 | case RID_STRUCT: | |
1516 | case RID_UNION: | |
1517 | if (!typespec_ok) | |
1518 | goto out; | |
1519 | attrs_ok = true; | |
1520 | seen_type = true; | |
1521 | t = c_parser_struct_or_union_specifier (parser); | |
1522 | declspecs_add_type (specs, t); | |
1523 | break; | |
1524 | case RID_TYPEOF: | |
1525 | /* ??? The old parser rejected typeof after other type | |
1526 | specifiers, but is a syntax error the best way of | |
1527 | handling this? */ | |
1528 | if (!typespec_ok || seen_type) | |
1529 | goto out; | |
1530 | attrs_ok = true; | |
1531 | seen_type = true; | |
1532 | t = c_parser_typeof_specifier (parser); | |
1533 | declspecs_add_type (specs, t); | |
1534 | break; | |
1535 | case RID_CONST: | |
1536 | case RID_VOLATILE: | |
1537 | case RID_RESTRICT: | |
1538 | attrs_ok = true; | |
1539 | declspecs_add_qual (specs, c_parser_peek_token (parser)->value); | |
1540 | c_parser_consume_token (parser); | |
1541 | break; | |
1542 | case RID_ATTRIBUTE: | |
1543 | if (!attrs_ok) | |
1544 | goto out; | |
1545 | attrs = c_parser_attributes (parser); | |
1546 | declspecs_add_attrs (specs, attrs); | |
1547 | break; | |
1548 | default: | |
1549 | goto out; | |
1550 | } | |
1551 | } | |
1552 | out: ; | |
1553 | } | |
1554 | ||
1555 | /* Parse an enum specifier (C90 6.5.2.2, C99 6.7.2.2). | |
1556 | ||
1557 | enum-specifier: | |
1558 | enum attributes[opt] identifier[opt] { enumerator-list } attributes[opt] | |
1559 | enum attributes[opt] identifier[opt] { enumerator-list , } attributes[opt] | |
1560 | enum attributes[opt] identifier | |
1561 | ||
1562 | The form with trailing comma is new in C99. The forms with | |
1563 | attributes are GNU extensions. In GNU C, we accept any expression | |
1564 | without commas in the syntax (assignment expressions, not just | |
1565 | conditional expressions); assignment expressions will be diagnosed | |
1566 | as non-constant. | |
1567 | ||
1568 | enumerator-list: | |
1569 | enumerator | |
1570 | enumerator-list , enumerator | |
1571 | ||
1572 | enumerator: | |
1573 | enumeration-constant | |
1574 | enumeration-constant = constant-expression | |
1575 | */ | |
1576 | ||
1577 | static struct c_typespec | |
1578 | c_parser_enum_specifier (c_parser *parser) | |
1579 | { | |
1580 | struct c_typespec ret; | |
1581 | tree attrs; | |
1582 | tree ident = NULL_TREE; | |
1583 | gcc_assert (c_parser_next_token_is_keyword (parser, RID_ENUM)); | |
1584 | c_parser_consume_token (parser); | |
1585 | attrs = c_parser_attributes (parser); | |
1586 | if (c_parser_next_token_is (parser, CPP_NAME)) | |
1587 | { | |
1588 | ident = c_parser_peek_token (parser)->value; | |
1589 | c_parser_consume_token (parser); | |
1590 | } | |
1591 | if (c_parser_next_token_is (parser, CPP_OPEN_BRACE)) | |
1592 | { | |
1593 | /* Parse an enum definition. */ | |
1594 | tree type = start_enum (ident); | |
1595 | tree postfix_attrs; | |
1596 | /* We chain the enumerators in reverse order, then put them in | |
1597 | forward order at the end. */ | |
1598 | tree values = NULL_TREE; | |
1599 | c_parser_consume_token (parser); | |
1600 | while (true) | |
1601 | { | |
1602 | tree enum_id; | |
1603 | tree enum_value; | |
1604 | tree enum_decl; | |
1605 | bool seen_comma; | |
1606 | if (c_parser_next_token_is_not (parser, CPP_NAME)) | |
1607 | { | |
1608 | c_parser_error (parser, "expected identifier"); | |
1609 | c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL); | |
1610 | values = error_mark_node; | |
1611 | break; | |
1612 | } | |
1613 | enum_id = c_parser_peek_token (parser)->value; | |
1614 | c_parser_consume_token (parser); | |
1615 | if (c_parser_next_token_is (parser, CPP_EQ)) | |
1616 | { | |
1617 | c_parser_consume_token (parser); | |
1618 | enum_value = c_parser_expr_no_commas (parser, NULL).value; | |
1619 | } | |
1620 | else | |
1621 | enum_value = NULL_TREE; | |
1622 | enum_decl = build_enumerator (enum_id, enum_value); | |
1623 | TREE_CHAIN (enum_decl) = values; | |
1624 | values = enum_decl; | |
1625 | seen_comma = false; | |
1626 | if (c_parser_next_token_is (parser, CPP_COMMA)) | |
1627 | { | |
1628 | seen_comma = true; | |
1629 | c_parser_consume_token (parser); | |
1630 | } | |
1631 | if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE)) | |
1632 | { | |
1633 | if (seen_comma && pedantic && !flag_isoc99) | |
1634 | pedwarn ("comma at end of enumerator list"); | |
1635 | c_parser_consume_token (parser); | |
1636 | break; | |
1637 | } | |
1638 | if (!seen_comma) | |
1639 | { | |
1640 | c_parser_error (parser, "expected %<,%> or %<}%>"); | |
1641 | c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL); | |
1642 | values = error_mark_node; | |
1643 | break; | |
1644 | } | |
1645 | } | |
1646 | postfix_attrs = c_parser_attributes (parser); | |
1647 | ret.spec = finish_enum (type, nreverse (values), | |
1648 | chainon (attrs, postfix_attrs)); | |
1649 | ret.kind = ctsk_tagdef; | |
1650 | return ret; | |
1651 | } | |
1652 | else if (!ident) | |
1653 | { | |
1654 | c_parser_error (parser, "expected %<{%>"); | |
1655 | ret.spec = error_mark_node; | |
1656 | ret.kind = ctsk_tagref; | |
1657 | return ret; | |
1658 | } | |
1659 | ret = parser_xref_tag (ENUMERAL_TYPE, ident); | |
1660 | /* In ISO C, enumerated types can be referred to only if already | |
1661 | defined. */ | |
1662 | if (pedantic && !COMPLETE_TYPE_P (ret.spec)) | |
1663 | pedwarn ("ISO C forbids forward references to %<enum%> types"); | |
1664 | return ret; | |
1665 | } | |
1666 | ||
1667 | /* Parse a struct or union specifier (C90 6.5.2.1, C99 6.7.2.1). | |
1668 | ||
1669 | struct-or-union-specifier: | |
1670 | struct-or-union attributes[opt] identifier[opt] | |
1671 | { struct-contents } attributes[opt] | |
1672 | struct-or-union attributes[opt] identifier | |
1673 | ||
1674 | struct-contents: | |
1675 | struct-declaration-list | |
1676 | ||
1677 | struct-declaration-list: | |
1678 | struct-declaration ; | |
1679 | struct-declaration-list struct-declaration ; | |
1680 | ||
1681 | GNU extensions: | |
1682 | ||
1683 | struct-contents: | |
1684 | empty | |
1685 | struct-declaration | |
1686 | struct-declaration-list struct-declaration | |
1687 | ||
1688 | struct-declaration-list: | |
1689 | struct-declaration-list ; | |
1690 | ; | |
1691 | ||
1692 | (Note that in the syntax here, unlike that in ISO C, the semicolons | |
1693 | are included here rather than in struct-declaration, in order to | |
1694 | describe the syntax with extra semicolons and missing semicolon at | |
1695 | end.) | |
1696 | ||
1697 | Objective-C: | |
1698 | ||
1699 | struct-declaration-list: | |
1700 | @defs ( class-name ) | |
1701 | ||
1702 | (Note this does not include a trailing semicolon, but can be | |
1703 | followed by further declarations, and gets a pedwarn-if-pedantic | |
1704 | when followed by a semicolon.) */ | |
1705 | ||
1706 | static struct c_typespec | |
1707 | c_parser_struct_or_union_specifier (c_parser *parser) | |
1708 | { | |
1709 | struct c_typespec ret; | |
1710 | tree attrs; | |
1711 | tree ident = NULL_TREE; | |
1712 | enum tree_code code; | |
1713 | switch (c_parser_peek_token (parser)->keyword) | |
1714 | { | |
1715 | case RID_STRUCT: | |
1716 | code = RECORD_TYPE; | |
1717 | break; | |
1718 | case RID_UNION: | |
1719 | code = UNION_TYPE; | |
1720 | break; | |
1721 | default: | |
1722 | gcc_unreachable (); | |
1723 | } | |
1724 | c_parser_consume_token (parser); | |
1725 | attrs = c_parser_attributes (parser); | |
1726 | if (c_parser_next_token_is (parser, CPP_NAME)) | |
1727 | { | |
1728 | ident = c_parser_peek_token (parser)->value; | |
1729 | c_parser_consume_token (parser); | |
1730 | } | |
1731 | if (c_parser_next_token_is (parser, CPP_OPEN_BRACE)) | |
1732 | { | |
1733 | /* Parse a struct or union definition. Start the scope of the | |
1734 | tag before parsing components. */ | |
1735 | tree type = start_struct (code, ident); | |
1736 | tree postfix_attrs; | |
1737 | /* We chain the components in reverse order, then put them in | |
1738 | forward order at the end. Each struct-declaration may | |
1739 | declare multiple components (comma-separated), so we must use | |
1740 | chainon to join them, although when parsing each | |
1741 | struct-declaration we can use TREE_CHAIN directly. | |
1742 | ||
1743 | The theory behind all this is that there will be more | |
1744 | semicolon separated fields than comma separated fields, and | |
1745 | so we'll be minimizing the number of node traversals required | |
1746 | by chainon. */ | |
1747 | tree contents = NULL_TREE; | |
1748 | c_parser_consume_token (parser); | |
1749 | /* Handle the Objective-C @defs construct, | |
1750 | e.g. foo(sizeof(struct{ @defs(ClassName) }));. */ | |
1751 | if (c_parser_next_token_is_keyword (parser, RID_AT_DEFS)) | |
1752 | { | |
1753 | tree name; | |
1754 | gcc_assert (c_dialect_objc ()); | |
1755 | c_parser_consume_token (parser); | |
1756 | if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>")) | |
1757 | goto end_at_defs; | |
1758 | if (c_parser_next_token_is (parser, CPP_NAME) | |
1759 | && c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME) | |
1760 | { | |
1761 | name = c_parser_peek_token (parser)->value; | |
1762 | c_parser_consume_token (parser); | |
1763 | } | |
1764 | else | |
1765 | { | |
1766 | c_parser_error (parser, "expected class name"); | |
1767 | c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL); | |
1768 | goto end_at_defs; | |
1769 | } | |
1770 | c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, | |
1771 | "expected %<)%>"); | |
1772 | contents = nreverse (objc_get_class_ivars (name)); | |
1773 | } | |
1774 | end_at_defs: | |
1775 | /* Parse the struct-declarations and semicolons. Problems with | |
1776 | semicolons are diagnosed here; empty structures are diagnosed | |
1777 | elsewhere. */ | |
1778 | while (true) | |
1779 | { | |
1780 | tree decls; | |
1781 | /* Parse any stray semicolon. */ | |
1782 | if (c_parser_next_token_is (parser, CPP_SEMICOLON)) | |
1783 | { | |
1784 | if (pedantic) | |
1785 | pedwarn ("extra semicolon in struct or union specified"); | |
1786 | c_parser_consume_token (parser); | |
1787 | continue; | |
1788 | } | |
1789 | /* Stop if at the end of the struct or union contents. */ | |
1790 | if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE)) | |
1791 | { | |
1792 | c_parser_consume_token (parser); | |
1793 | break; | |
1794 | } | |
1795 | /* Parse some comma-separated declarations, but not the | |
1796 | trailing semicolon if any. */ | |
1797 | decls = c_parser_struct_declaration (parser); | |
1798 | contents = chainon (decls, contents); | |
1799 | /* If no semicolon follows, either we have a parse error or | |
1800 | are at the end of the struct or union and should | |
1801 | pedwarn. */ | |
1802 | if (c_parser_next_token_is (parser, CPP_SEMICOLON)) | |
1803 | c_parser_consume_token (parser); | |
1804 | else | |
1805 | { | |
1806 | if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE)) | |
1807 | pedwarn ("no semicolon at end of struct or union"); | |
1808 | else | |
1809 | { | |
1810 | c_parser_error (parser, "expected %<;%>"); | |
1811 | c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL); | |
1812 | break; | |
1813 | } | |
1814 | } | |
1815 | } | |
1816 | postfix_attrs = c_parser_attributes (parser); | |
1817 | ret.spec = finish_struct (type, nreverse (contents), | |
1818 | chainon (attrs, postfix_attrs)); | |
1819 | ret.kind = ctsk_tagdef; | |
1820 | return ret; | |
1821 | } | |
1822 | else if (!ident) | |
1823 | { | |
1824 | c_parser_error (parser, "expected %<{%>"); | |
1825 | ret.spec = error_mark_node; | |
1826 | ret.kind = ctsk_tagref; | |
67c2939d | 1827 | return ret; |
27bf414c JM |
1828 | } |
1829 | ret = parser_xref_tag (code, ident); | |
1830 | return ret; | |
1831 | } | |
1832 | ||
1833 | /* Parse a struct-declaration (C90 6.5.2.1, C99 6.7.2.1), *without* | |
1834 | the trailing semicolon. | |
1835 | ||
1836 | struct-declaration: | |
1837 | specifier-qualifier-list struct-declarator-list | |
1838 | ||
1839 | specifier-qualifier-list: | |
1840 | type-specifier specifier-qualifier-list[opt] | |
1841 | type-qualifier specifier-qualifier-list[opt] | |
1842 | attributes specifier-qualifier-list[opt] | |
1843 | ||
1844 | struct-declarator-list: | |
1845 | struct-declarator | |
1846 | struct-declarator-list , attributes[opt] struct-declarator | |
1847 | ||
1848 | struct-declarator: | |
1849 | declarator attributes[opt] | |
1850 | declarator[opt] : constant-expression attributes[opt] | |
1851 | ||
1852 | GNU extensions: | |
1853 | ||
1854 | struct-declaration: | |
1855 | __extension__ struct-declaration | |
1856 | specifier-qualifier-list | |
1857 | ||
1858 | Unlike the ISO C syntax, semicolons are handled elsewhere. The use | |
1859 | of attributes where shown is a GNU extension. In GNU C, we accept | |
1860 | any expression without commas in the syntax (assignment | |
1861 | expressions, not just conditional expressions); assignment | |
1862 | expressions will be diagnosed as non-constant. */ | |
1863 | ||
1864 | static tree | |
1865 | c_parser_struct_declaration (c_parser *parser) | |
1866 | { | |
1867 | struct c_declspecs *specs; | |
1868 | tree prefix_attrs; | |
1869 | tree all_prefix_attrs; | |
1870 | tree decls; | |
1871 | if (c_parser_next_token_is_keyword (parser, RID_EXTENSION)) | |
1872 | { | |
1873 | int ext; | |
1874 | tree decl; | |
1875 | ext = disable_extension_diagnostics (); | |
1876 | c_parser_consume_token (parser); | |
1877 | decl = c_parser_struct_declaration (parser); | |
1878 | restore_extension_diagnostics (ext); | |
1879 | return decl; | |
1880 | } | |
1881 | specs = build_null_declspecs (); | |
1882 | c_parser_declspecs (parser, specs, false, true, true); | |
1883 | if (parser->error) | |
67c2939d | 1884 | return NULL_TREE; |
27bf414c JM |
1885 | if (!specs->declspecs_seen_p) |
1886 | { | |
1887 | c_parser_error (parser, "expected specifier-qualifier-list"); | |
1888 | return NULL_TREE; | |
1889 | } | |
1890 | finish_declspecs (specs); | |
1891 | if (c_parser_next_token_is (parser, CPP_SEMICOLON)) | |
1892 | { | |
1893 | tree ret; | |
1894 | if (!specs->type_seen_p) | |
1895 | { | |
1896 | if (pedantic) | |
1897 | pedwarn ("ISO C forbids member declarations with no members"); | |
1898 | shadow_tag_warned (specs, pedantic); | |
1899 | ret = NULL_TREE; | |
1900 | } | |
1901 | else | |
1902 | { | |
1903 | /* Support for unnamed structs or unions as members of | |
1904 | structs or unions (which is [a] useful and [b] supports | |
1905 | MS P-SDK). */ | |
1906 | ret = grokfield (build_id_declarator (NULL_TREE), specs, NULL_TREE); | |
1907 | } | |
1908 | return ret; | |
1909 | } | |
1910 | pending_xref_error (); | |
1911 | prefix_attrs = specs->attrs; | |
1912 | all_prefix_attrs = prefix_attrs; | |
1913 | specs->attrs = NULL_TREE; | |
1914 | decls = NULL_TREE; | |
1915 | while (true) | |
1916 | { | |
1917 | /* Declaring one or more declarators or un-named bit-fields. */ | |
1918 | struct c_declarator *declarator; | |
1919 | bool dummy = false; | |
1920 | if (c_parser_next_token_is (parser, CPP_COLON)) | |
1921 | declarator = build_id_declarator (NULL_TREE); | |
1922 | else | |
1923 | declarator = c_parser_declarator (parser, specs->type_seen_p, | |
1924 | C_DTR_NORMAL, &dummy); | |
1925 | if (declarator == NULL) | |
1926 | { | |
1927 | c_parser_skip_to_end_of_block_or_statement (parser); | |
1928 | break; | |
1929 | } | |
1930 | if (c_parser_next_token_is (parser, CPP_COLON) | |
1931 | || c_parser_next_token_is (parser, CPP_COMMA) | |
1932 | || c_parser_next_token_is (parser, CPP_SEMICOLON) | |
1933 | || c_parser_next_token_is (parser, CPP_CLOSE_BRACE) | |
1934 | || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE)) | |
1935 | { | |
1936 | tree postfix_attrs = NULL_TREE; | |
1937 | tree width = NULL_TREE; | |
1938 | tree d; | |
1939 | if (c_parser_next_token_is (parser, CPP_COLON)) | |
1940 | { | |
1941 | c_parser_consume_token (parser); | |
1942 | width = c_parser_expr_no_commas (parser, NULL).value; | |
1943 | } | |
1944 | if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE)) | |
1945 | postfix_attrs = c_parser_attributes (parser); | |
1946 | d = grokfield (declarator, specs, width); | |
1947 | decl_attributes (&d, chainon (postfix_attrs, | |
1948 | all_prefix_attrs), 0); | |
1949 | TREE_CHAIN (d) = decls; | |
1950 | decls = d; | |
1951 | if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE)) | |
1952 | all_prefix_attrs = chainon (c_parser_attributes (parser), | |
1953 | prefix_attrs); | |
1954 | else | |
1955 | all_prefix_attrs = prefix_attrs; | |
1956 | if (c_parser_next_token_is (parser, CPP_COMMA)) | |
1957 | c_parser_consume_token (parser); | |
1958 | else if (c_parser_next_token_is (parser, CPP_SEMICOLON) | |
1959 | || c_parser_next_token_is (parser, CPP_CLOSE_BRACE)) | |
1960 | { | |
1961 | /* Semicolon consumed in caller. */ | |
1962 | break; | |
1963 | } | |
1964 | else | |
1965 | { | |
1966 | c_parser_error (parser, "expected %<,%>, %<;%> or %<}%>"); | |
1967 | break; | |
1968 | } | |
1969 | } | |
1970 | else | |
1971 | { | |
1972 | c_parser_error (parser, | |
1973 | "expected %<:%>, %<,%>, %<;%>, %<}%> or " | |
1974 | "%<__attribute__%>"); | |
1975 | break; | |
1976 | } | |
1977 | } | |
1978 | return decls; | |
1979 | } | |
1980 | ||
1981 | /* Parse a typeof specifier (a GNU extension). | |
1982 | ||
1983 | typeof-specifier: | |
1984 | typeof ( expression ) | |
1985 | typeof ( type-name ) | |
1986 | */ | |
1987 | ||
1988 | static struct c_typespec | |
1989 | c_parser_typeof_specifier (c_parser *parser) | |
1990 | { | |
1991 | struct c_typespec ret; | |
1992 | ret.kind = ctsk_typeof; | |
1993 | ret.spec = error_mark_node; | |
1994 | gcc_assert (c_parser_next_token_is_keyword (parser, RID_TYPEOF)); | |
1995 | c_parser_consume_token (parser); | |
1996 | skip_evaluation++; | |
1997 | in_typeof++; | |
1998 | if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>")) | |
1999 | { | |
2000 | skip_evaluation--; | |
2001 | in_typeof--; | |
2002 | return ret; | |
2003 | } | |
2004 | if (c_parser_next_token_starts_typename (parser)) | |
2005 | { | |
2006 | struct c_type_name *type = c_parser_type_name (parser); | |
2007 | skip_evaluation--; | |
2008 | in_typeof--; | |
2009 | if (type != NULL) | |
2010 | { | |
2011 | ret.spec = groktypename (type); | |
2012 | pop_maybe_used (variably_modified_type_p (ret.spec, NULL_TREE)); | |
2013 | } | |
2014 | } | |
2015 | else | |
2016 | { | |
2017 | struct c_expr expr = c_parser_expression (parser); | |
2018 | skip_evaluation--; | |
2019 | in_typeof--; | |
2020 | if (TREE_CODE (expr.value) == COMPONENT_REF | |
2021 | && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1))) | |
2022 | error ("%<typeof%> applied to a bit-field"); | |
2023 | ret.spec = TREE_TYPE (expr.value); | |
2024 | pop_maybe_used (variably_modified_type_p (ret.spec, NULL_TREE)); | |
2025 | } | |
2026 | c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>"); | |
2027 | return ret; | |
2028 | } | |
2029 | ||
2030 | /* Parse a declarator, possibly an abstract declarator (C90 6.5.4, | |
2031 | 6.5.5, C99 6.7.5, 6.7.6). If TYPE_SEEN_P then a typedef name may | |
2032 | be redeclared; otherwise it may not. KIND indicates which kind of | |
2033 | declarator is wanted. Returns a valid declarator except in the | |
2034 | case of a syntax error in which case NULL is returned. *SEEN_ID is | |
2035 | set to true if an identifier being declared is seen; this is used | |
2036 | to diagnose bad forms of abstract array declarators and to | |
2037 | determine whether an identifier list is syntactically permitted. | |
2038 | ||
2039 | declarator: | |
2040 | pointer[opt] direct-declarator | |
2041 | ||
2042 | direct-declarator: | |
2043 | identifier | |
2044 | ( attributes[opt] declarator ) | |
2045 | direct-declarator array-declarator | |
2046 | direct-declarator ( parameter-type-list ) | |
2047 | direct-declarator ( identifier-list[opt] ) | |
2048 | ||
2049 | pointer: | |
2050 | * type-qualifier-list[opt] | |
2051 | * type-qualifier-list[opt] pointer | |
2052 | ||
2053 | type-qualifier-list: | |
2054 | type-qualifier | |
2055 | attributes | |
2056 | type-qualifier-list type-qualifier | |
2057 | type-qualifier-list attributes | |
2058 | ||
2059 | parameter-type-list: | |
2060 | parameter-list | |
2061 | parameter-list , ... | |
2062 | ||
2063 | parameter-list: | |
2064 | parameter-declaration | |
2065 | parameter-list , parameter-declaration | |
2066 | ||
2067 | parameter-declaration: | |
2068 | declaration-specifiers declarator attributes[opt] | |
2069 | declaration-specifiers abstract-declarator[opt] attributes[opt] | |
2070 | ||
2071 | identifier-list: | |
2072 | identifier | |
2073 | identifier-list , identifier | |
2074 | ||
2075 | abstract-declarator: | |
2076 | pointer | |
2077 | pointer[opt] direct-abstract-declarator | |
2078 | ||
2079 | direct-abstract-declarator: | |
2080 | ( attributes[opt] abstract-declarator ) | |
2081 | direct-abstract-declarator[opt] array-declarator | |
2082 | direct-abstract-declarator[opt] ( parameter-type-list[opt] ) | |
2083 | ||
2084 | GNU extensions: | |
2085 | ||
2086 | direct-declarator: | |
2087 | direct-declarator ( parameter-forward-declarations | |
2088 | parameter-type-list[opt] ) | |
2089 | ||
2090 | direct-abstract-declarator: | |
2091 | direct-abstract-declarator[opt] ( parameter-forward-declarations | |
2092 | parameter-type-list[opt] ) | |
2093 | ||
2094 | parameter-forward-declarations: | |
2095 | parameter-list ; | |
2096 | parameter-forward-declarations parameter-list ; | |
2097 | ||
2098 | The uses of attributes shown above are GNU extensions. | |
2099 | ||
2100 | Some forms of array declarator are not included in C99 in the | |
2101 | syntax for abstract declarators; these are disallowed elsewhere. | |
2102 | This may be a defect (DR#289). | |
2103 | ||
2104 | This function also accepts an omitted abstract declarator as being | |
2105 | an abstract declarator, although not part of the formal syntax. */ | |
2106 | ||
2107 | static struct c_declarator * | |
2108 | c_parser_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind, | |
2109 | bool *seen_id) | |
2110 | { | |
2111 | /* Parse any initial pointer part. */ | |
2112 | if (c_parser_next_token_is (parser, CPP_MULT)) | |
2113 | { | |
2114 | struct c_declspecs *quals_attrs = build_null_declspecs (); | |
2115 | struct c_declarator *inner; | |
2116 | c_parser_consume_token (parser); | |
2117 | c_parser_declspecs (parser, quals_attrs, false, false, true); | |
2118 | inner = c_parser_declarator (parser, type_seen_p, kind, seen_id); | |
2119 | if (inner == NULL) | |
2120 | return NULL; | |
2121 | else | |
2122 | return make_pointer_declarator (quals_attrs, inner); | |
2123 | } | |
2124 | /* Now we have a direct declarator, direct abstract declarator or | |
2125 | nothing (which counts as a direct abstract declarator here). */ | |
2126 | return c_parser_direct_declarator (parser, type_seen_p, kind, seen_id); | |
2127 | } | |
2128 | ||
2129 | /* Parse a direct declarator or direct abstract declarator; arguments | |
2130 | as c_parser_declarator. */ | |
2131 | ||
2132 | static struct c_declarator * | |
2133 | c_parser_direct_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind, | |
2134 | bool *seen_id) | |
2135 | { | |
2136 | /* The direct declarator must start with an identifier (possibly | |
2137 | omitted) or a parenthesized declarator (possibly abstract). In | |
2138 | an ordinary declarator, initial parentheses must start a | |
2139 | parenthesized declarator. In an abstract declarator or parameter | |
2140 | declarator, they could start a parenthesized declarator or a | |
2141 | parameter list. To tell which, the open parenthesis and any | |
2142 | following attributes must be read. If a declaration specifier | |
2143 | follows, then it is a parameter list; if the specifier is a | |
2144 | typedef name, there might be an ambiguity about redeclaring it, | |
2145 | which is resolved in the direction of treating it as a typedef | |
2146 | name. If a close parenthesis follows, it is also an empty | |
2147 | parameter list, as the syntax does not permit empty abstract | |
2148 | declarators. Otherwise, it is a parenthesised declarator (in | |
2149 | which case the analysis may be repeated inside it, recursively). | |
2150 | ||
2151 | ??? There is an ambiguity in a parameter declaration "int | |
2152 | (__attribute__((foo)) x)", where x is not a typedef name: it | |
2153 | could be an abstract declarator for a function, or declare x with | |
2154 | parentheses. The proper resolution of this ambiguity needs | |
2155 | documenting. At present we follow an accident of the old | |
2156 | parser's implementation, whereby the first parameter must have | |
2157 | some declaration specifiers other than just attributes. Thus as | |
2158 | a parameter declaration it is treated as a parenthesised | |
2159 | parameter named x, and as an abstract declarator it is | |
2160 | rejected. | |
2161 | ||
2162 | ??? Also following the old parser, attributes inside an empty | |
2163 | parameter list are ignored, making it a list not yielding a | |
2164 | prototype, rather than giving an error or making it have one | |
2165 | parameter with implicit type int. | |
2166 | ||
2167 | ??? Also following the old parser, typedef names may be | |
2168 | redeclared in declarators, but not Objective-C class names. */ | |
2169 | ||
2170 | if (kind != C_DTR_ABSTRACT | |
2171 | && c_parser_next_token_is (parser, CPP_NAME) | |
2172 | && ((type_seen_p | |
2173 | && c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME) | |
2174 | || c_parser_peek_token (parser)->id_kind == C_ID_ID)) | |
2175 | { | |
2176 | struct c_declarator *inner | |
2177 | = build_id_declarator (c_parser_peek_token (parser)->value); | |
2178 | *seen_id = true; | |
6037d88d | 2179 | inner->id_loc = c_parser_peek_token (parser)->location; |
27bf414c JM |
2180 | c_parser_consume_token (parser); |
2181 | return c_parser_direct_declarator_inner (parser, *seen_id, inner); | |
2182 | } | |
2183 | ||
2184 | if (kind != C_DTR_NORMAL | |
2185 | && c_parser_next_token_is (parser, CPP_OPEN_SQUARE)) | |
2186 | { | |
2187 | struct c_declarator *inner = build_id_declarator (NULL_TREE); | |
2188 | return c_parser_direct_declarator_inner (parser, *seen_id, inner); | |
2189 | } | |
2190 | ||
2191 | /* Either we are at the end of an abstract declarator, or we have | |
2192 | parentheses. */ | |
2193 | ||
2194 | if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)) | |
2195 | { | |
2196 | tree attrs; | |
2197 | struct c_declarator *inner; | |
2198 | c_parser_consume_token (parser); | |
2199 | attrs = c_parser_attributes (parser); | |
2200 | if (kind != C_DTR_NORMAL | |
2201 | && (c_parser_next_token_starts_declspecs (parser) | |
2202 | || c_parser_next_token_is (parser, CPP_CLOSE_PAREN))) | |
2203 | { | |
2204 | struct c_arg_info *args | |
2205 | = c_parser_parms_declarator (parser, kind == C_DTR_NORMAL, | |
2206 | attrs); | |
2207 | if (args == NULL) | |
2208 | return NULL; | |
2209 | else | |
2210 | { | |
2211 | inner | |
2212 | = build_function_declarator (args, | |
2213 | build_id_declarator (NULL_TREE)); | |
2214 | return c_parser_direct_declarator_inner (parser, *seen_id, | |
2215 | inner); | |
2216 | } | |
2217 | } | |
2218 | /* A parenthesized declarator. */ | |
2219 | inner = c_parser_declarator (parser, type_seen_p, kind, seen_id); | |
2220 | if (inner != NULL && attrs != NULL) | |
2221 | inner = build_attrs_declarator (attrs, inner); | |
2222 | if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN)) | |
2223 | { | |
2224 | c_parser_consume_token (parser); | |
2225 | if (inner == NULL) | |
2226 | return NULL; | |
2227 | else | |
2228 | return c_parser_direct_declarator_inner (parser, *seen_id, inner); | |
2229 | } | |
2230 | else | |
2231 | { | |
2232 | c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, | |
2233 | "expected %<)%>"); | |
2234 | return NULL; | |
2235 | } | |
2236 | } | |
2237 | else | |
2238 | { | |
2239 | if (kind == C_DTR_NORMAL) | |
2240 | { | |
2241 | c_parser_error (parser, "expected identifier or %<(%>"); | |
2242 | return NULL; | |
2243 | } | |
2244 | else | |
2245 | return build_id_declarator (NULL_TREE); | |
2246 | } | |
2247 | } | |
2248 | ||
2249 | /* Parse part of a direct declarator or direct abstract declarator, | |
2250 | given that some (in INNER) has already been parsed; ID_PRESENT is | |
2251 | true if an identifier is present, false for an abstract | |
2252 | declarator. */ | |
2253 | ||
2254 | static struct c_declarator * | |
2255 | c_parser_direct_declarator_inner (c_parser *parser, bool id_present, | |
2256 | struct c_declarator *inner) | |
2257 | { | |
2258 | /* Parse a sequence of array declarators and parameter lists. */ | |
2259 | if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE)) | |
2260 | { | |
2261 | struct c_declarator *declarator; | |
2262 | struct c_declspecs *quals_attrs = build_null_declspecs (); | |
2263 | bool static_seen; | |
2264 | bool star_seen; | |
2265 | tree dimen; | |
2266 | c_parser_consume_token (parser); | |
2267 | c_parser_declspecs (parser, quals_attrs, false, false, true); | |
2268 | static_seen = c_parser_next_token_is_keyword (parser, RID_STATIC); | |
2269 | if (static_seen) | |
2270 | c_parser_consume_token (parser); | |
2271 | if (static_seen && !quals_attrs->declspecs_seen_p) | |
2272 | c_parser_declspecs (parser, quals_attrs, false, false, true); | |
2273 | if (!quals_attrs->declspecs_seen_p) | |
2274 | quals_attrs = NULL; | |
2275 | /* If "static" is present, there must be an array dimension. | |
2276 | Otherwise, there may be a dimension, "*", or no | |
2277 | dimension. */ | |
2278 | if (static_seen) | |
2279 | { | |
2280 | star_seen = false; | |
2281 | dimen = c_parser_expr_no_commas (parser, NULL).value; | |
2282 | } | |
2283 | else | |
2284 | { | |
2285 | if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE)) | |
2286 | { | |
2287 | dimen = NULL_TREE; | |
2288 | star_seen = false; | |
2289 | } | |
2290 | else if (c_parser_next_token_is (parser, CPP_MULT)) | |
2291 | { | |
2292 | if (c_parser_peek_2nd_token (parser)->type == CPP_CLOSE_SQUARE) | |
2293 | { | |
2294 | dimen = NULL_TREE; | |
2295 | star_seen = true; | |
2296 | c_parser_consume_token (parser); | |
2297 | } | |
2298 | else | |
2299 | { | |
2300 | star_seen = false; | |
2301 | dimen = c_parser_expr_no_commas (parser, NULL).value; | |
2302 | } | |
2303 | } | |
2304 | else | |
2305 | { | |
2306 | star_seen = false; | |
2307 | dimen = c_parser_expr_no_commas (parser, NULL).value; | |
2308 | } | |
2309 | } | |
2310 | if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE)) | |
2311 | c_parser_consume_token (parser); | |
2312 | else | |
2313 | { | |
2314 | c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, | |
2315 | "expected %<]%>"); | |
2316 | return NULL; | |
2317 | } | |
2318 | declarator = build_array_declarator (dimen, quals_attrs, static_seen, | |
2319 | star_seen); | |
2320 | inner = set_array_declarator_inner (declarator, inner, !id_present); | |
2321 | return c_parser_direct_declarator_inner (parser, id_present, inner); | |
2322 | } | |
2323 | else if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)) | |
2324 | { | |
2325 | tree attrs; | |
2326 | struct c_arg_info *args; | |
2327 | c_parser_consume_token (parser); | |
2328 | attrs = c_parser_attributes (parser); | |
2329 | args = c_parser_parms_declarator (parser, id_present, attrs); | |
2330 | if (args == NULL) | |
2331 | return NULL; | |
2332 | else | |
2333 | { | |
2334 | inner = build_function_declarator (args, inner); | |
2335 | return c_parser_direct_declarator_inner (parser, id_present, inner); | |
2336 | } | |
2337 | } | |
2338 | return inner; | |
2339 | } | |
2340 | ||
2341 | /* Parse a parameter list or identifier list, including the closing | |
2342 | parenthesis but not the opening one. ATTRS are the attributes at | |
2343 | the start of the list. ID_LIST_OK is true if an identifier list is | |
2344 | acceptable; such a list must not have attributes at the start. */ | |
2345 | ||
2346 | static struct c_arg_info * | |
2347 | c_parser_parms_declarator (c_parser *parser, bool id_list_ok, tree attrs) | |
2348 | { | |
2349 | push_scope (); | |
2350 | declare_parm_level (); | |
2351 | /* If the list starts with an identifier, it is an identifier list. | |
2352 | Otherwise, it is either a prototype list or an empty list. */ | |
2353 | if (id_list_ok | |
2354 | && !attrs | |
2355 | && c_parser_next_token_is (parser, CPP_NAME) | |
2356 | && c_parser_peek_token (parser)->id_kind == C_ID_ID) | |
2357 | { | |
2358 | tree list = NULL_TREE; | |
2359 | while (c_parser_next_token_is (parser, CPP_NAME) | |
2360 | && c_parser_peek_token (parser)->id_kind == C_ID_ID) | |
2361 | { | |
2362 | list = chainon (list, build_tree_list (NULL_TREE, | |
2363 | c_parser_peek_token (parser)->value)); | |
2364 | c_parser_consume_token (parser); | |
2365 | if (c_parser_next_token_is_not (parser, CPP_COMMA)) | |
2366 | break; | |
2367 | c_parser_consume_token (parser); | |
2368 | if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN)) | |
2369 | { | |
2370 | c_parser_error (parser, "expected identifier"); | |
2371 | break; | |
2372 | } | |
2373 | } | |
2374 | if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN)) | |
2375 | { | |
2376 | struct c_arg_info *ret = XOBNEW (&parser_obstack, struct c_arg_info); | |
2377 | ret->parms = 0; | |
2378 | ret->tags = 0; | |
2379 | ret->types = list; | |
2380 | ret->others = 0; | |
2381 | c_parser_consume_token (parser); | |
2382 | pop_scope (); | |
2383 | return ret; | |
2384 | } | |
2385 | else | |
2386 | { | |
2387 | c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, | |
2388 | "expected %<)%>"); | |
2389 | pop_scope (); | |
2390 | return NULL; | |
2391 | } | |
2392 | } | |
2393 | else | |
2394 | { | |
2395 | struct c_arg_info *ret = c_parser_parms_list_declarator (parser, attrs); | |
2396 | pop_scope (); | |
2397 | return ret; | |
2398 | } | |
2399 | } | |
2400 | ||
2401 | /* Parse a parameter list (possibly empty), including the closing | |
2402 | parenthesis but not the opening one. ATTRS are the attributes at | |
2403 | the start of the list. */ | |
2404 | ||
2405 | static struct c_arg_info * | |
2406 | c_parser_parms_list_declarator (c_parser *parser, tree attrs) | |
2407 | { | |
2408 | bool good_parm = false; | |
2409 | /* ??? Following the old parser, forward parameter declarations may | |
2410 | use abstract declarators, and if no real parameter declarations | |
2411 | follow the forward declarations then this is not diagnosed. Also | |
2412 | note as above that attributes are ignored as the only contents of | |
2413 | the parentheses, or as the only contents after forward | |
2414 | declarations. */ | |
2415 | if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN)) | |
2416 | { | |
2417 | struct c_arg_info *ret = XOBNEW (&parser_obstack, struct c_arg_info); | |
2418 | ret->parms = 0; | |
2419 | ret->tags = 0; | |
2420 | ret->types = 0; | |
2421 | ret->others = 0; | |
2422 | c_parser_consume_token (parser); | |
2423 | return ret; | |
2424 | } | |
2425 | if (c_parser_next_token_is (parser, CPP_ELLIPSIS)) | |
2426 | { | |
2427 | struct c_arg_info *ret = XOBNEW (&parser_obstack, struct c_arg_info); | |
2428 | ret->parms = 0; | |
2429 | ret->tags = 0; | |
2430 | ret->others = 0; | |
2431 | /* Suppress -Wold-style-definition for this case. */ | |
2432 | ret->types = error_mark_node; | |
2433 | error ("ISO C requires a named argument before %<...%>"); | |
2434 | c_parser_consume_token (parser); | |
2435 | if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN)) | |
2436 | { | |
2437 | c_parser_consume_token (parser); | |
2438 | return ret; | |
2439 | } | |
2440 | else | |
2441 | { | |
2442 | c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, | |
2443 | "expected %<)%>"); | |
2444 | return NULL; | |
2445 | } | |
2446 | } | |
2447 | /* Nonempty list of parameters, either terminated with semicolon | |
2448 | (forward declarations; recurse) or with close parenthesis (normal | |
2449 | function) or with ", ... )" (variadic function). */ | |
2450 | while (true) | |
2451 | { | |
2452 | /* Parse a parameter. */ | |
2453 | struct c_parm *parm = c_parser_parameter_declaration (parser, attrs); | |
2454 | attrs = NULL_TREE; | |
2455 | if (parm != NULL) | |
2456 | { | |
2457 | good_parm = true; | |
2458 | push_parm_decl (parm); | |
2459 | } | |
2460 | if (c_parser_next_token_is (parser, CPP_SEMICOLON)) | |
2461 | { | |
2462 | tree new_attrs; | |
2463 | c_parser_consume_token (parser); | |
2464 | new_attrs = c_parser_attributes (parser); | |
2465 | return c_parser_parms_list_declarator (parser, new_attrs); | |
2466 | } | |
2467 | if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN)) | |
2468 | { | |
2469 | c_parser_consume_token (parser); | |
2470 | if (good_parm) | |
2471 | return get_parm_info (false); | |
2472 | else | |
2473 | { | |
2474 | struct c_arg_info *ret | |
2475 | = XOBNEW (&parser_obstack, struct c_arg_info); | |
2476 | ret->parms = 0; | |
2477 | ret->tags = 0; | |
2478 | ret->types = 0; | |
2479 | ret->others = 0; | |
2480 | return ret; | |
2481 | } | |
2482 | } | |
2483 | if (!c_parser_require (parser, CPP_COMMA, | |
2484 | "expected %<;%>, %<,%> or %<)%>")) | |
2485 | { | |
2486 | c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL); | |
2487 | return NULL; | |
2488 | } | |
2489 | if (c_parser_next_token_is (parser, CPP_ELLIPSIS)) | |
2490 | { | |
2491 | c_parser_consume_token (parser); | |
2492 | if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN)) | |
2493 | { | |
2494 | c_parser_consume_token (parser); | |
2495 | if (good_parm) | |
2496 | return get_parm_info (true); | |
2497 | else | |
2498 | { | |
2499 | struct c_arg_info *ret | |
2500 | = XOBNEW (&parser_obstack, struct c_arg_info); | |
2501 | ret->parms = 0; | |
2502 | ret->tags = 0; | |
2503 | ret->types = 0; | |
2504 | ret->others = 0; | |
2505 | return ret; | |
2506 | } | |
2507 | } | |
2508 | else | |
2509 | { | |
2510 | c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, | |
2511 | "expected %<)%>"); | |
2512 | return NULL; | |
2513 | } | |
2514 | } | |
2515 | } | |
2516 | } | |
2517 | ||
2518 | /* Parse a parameter declaration. ATTRS are the attributes at the | |
2519 | start of the declaration if it is the first parameter. */ | |
2520 | ||
2521 | static struct c_parm * | |
2522 | c_parser_parameter_declaration (c_parser *parser, tree attrs) | |
2523 | { | |
2524 | struct c_declspecs *specs; | |
2525 | struct c_declarator *declarator; | |
2526 | tree prefix_attrs; | |
2527 | tree postfix_attrs = NULL_TREE; | |
2528 | bool dummy = false; | |
2529 | if (!c_parser_next_token_starts_declspecs (parser)) | |
2530 | { | |
2531 | /* ??? In some Objective-C cases '...' isn't applicable so there | |
2532 | should be a different message. */ | |
2533 | c_parser_error (parser, | |
2534 | "expected declaration specifiers or %<...%>"); | |
2535 | c_parser_skip_to_end_of_parameter (parser); | |
2536 | return NULL; | |
2537 | } | |
2538 | specs = build_null_declspecs (); | |
2539 | if (attrs) | |
2540 | { | |
2541 | declspecs_add_attrs (specs, attrs); | |
2542 | attrs = NULL_TREE; | |
2543 | } | |
2544 | c_parser_declspecs (parser, specs, true, true, true); | |
2545 | finish_declspecs (specs); | |
2546 | pending_xref_error (); | |
2547 | prefix_attrs = specs->attrs; | |
2548 | specs->attrs = NULL_TREE; | |
2549 | declarator = c_parser_declarator (parser, specs->type_seen_p, | |
2550 | C_DTR_PARM, &dummy); | |
2551 | if (declarator == NULL) | |
2552 | { | |
2553 | c_parser_skip_until_found (parser, CPP_COMMA, NULL); | |
2554 | return NULL; | |
2555 | } | |
2556 | if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE)) | |
2557 | postfix_attrs = c_parser_attributes (parser); | |
2558 | return build_c_parm (specs, chainon (postfix_attrs, prefix_attrs), | |
2559 | declarator); | |
2560 | } | |
2561 | ||
2562 | /* Parse a string literal in an asm expression. It should not be | |
2563 | translated, and wide string literals are an error although | |
2564 | permitted by the syntax. This is a GNU extension. | |
2565 | ||
2566 | asm-string-literal: | |
2567 | string-literal | |
2568 | ||
2569 | ??? At present, following the old parser, the caller needs to have | |
2570 | set c_lex_string_translate to 0. It would be better to follow the | |
2571 | C++ parser rather than using the c_lex_string_translate kludge. */ | |
2572 | ||
2573 | static tree | |
2574 | c_parser_asm_string_literal (c_parser *parser) | |
2575 | { | |
2576 | tree str; | |
2577 | if (c_parser_next_token_is (parser, CPP_STRING)) | |
2578 | { | |
2579 | str = c_parser_peek_token (parser)->value; | |
2580 | c_parser_consume_token (parser); | |
2581 | } | |
2582 | else if (c_parser_next_token_is (parser, CPP_WSTRING)) | |
2583 | { | |
2584 | error ("wide string literal in %<asm%>"); | |
2585 | str = build_string (1, ""); | |
2586 | c_parser_consume_token (parser); | |
2587 | } | |
2588 | else | |
2589 | { | |
2590 | c_parser_error (parser, "expected string literal"); | |
2591 | str = NULL_TREE; | |
2592 | } | |
2593 | return str; | |
2594 | } | |
2595 | ||
2596 | /* Parse a simple asm expression. This is used in restricted | |
2597 | contexts, where a full expression with inputs and outputs does not | |
2598 | make sense. This is a GNU extension. | |
2599 | ||
2600 | simple-asm-expr: | |
2601 | asm ( asm-string-literal ) | |
2602 | */ | |
2603 | ||
2604 | static tree | |
2605 | c_parser_simple_asm_expr (c_parser *parser) | |
2606 | { | |
2607 | tree str; | |
2608 | gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM)); | |
2609 | /* ??? Follow the C++ parser rather than using the | |
2610 | c_lex_string_translate kludge. */ | |
2611 | c_lex_string_translate = 0; | |
2612 | c_parser_consume_token (parser); | |
2613 | if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>")) | |
2614 | { | |
2615 | c_lex_string_translate = 1; | |
2616 | return NULL_TREE; | |
2617 | } | |
2618 | str = c_parser_asm_string_literal (parser); | |
2619 | c_lex_string_translate = 1; | |
2620 | if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>")) | |
2621 | { | |
2622 | c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL); | |
2623 | return NULL_TREE; | |
2624 | } | |
2625 | return str; | |
2626 | } | |
2627 | ||
2628 | /* Parse (possibly empty) attributes. This is a GNU extension. | |
2629 | ||
2630 | attributes: | |
2631 | empty | |
2632 | attributes attribute | |
2633 | ||
2634 | attribute: | |
2635 | __attribute__ ( ( attribute-list ) ) | |
2636 | ||
2637 | attribute-list: | |
2638 | attrib | |
2639 | attribute_list , attrib | |
2640 | ||
2641 | attrib: | |
2642 | empty | |
2643 | any-word | |
2644 | any-word ( identifier ) | |
2645 | any-word ( identifier , nonempty-expr-list ) | |
2646 | any-word ( expr-list ) | |
2647 | ||
2648 | where the "identifier" must not be declared as a type, and | |
2649 | "any-word" may be any identifier (including one declared as a | |
2650 | type), a reserved word storage class specifier, type specifier or | |
2651 | type qualifier. ??? This still leaves out most reserved keywords | |
2652 | (following the old parser), shouldn't we include them, and why not | |
2653 | allow identifiers declared as types to start the arguments? */ | |
2654 | ||
2655 | static tree | |
2656 | c_parser_attributes (c_parser *parser) | |
2657 | { | |
2658 | tree attrs = NULL_TREE; | |
2659 | while (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE)) | |
2660 | { | |
2661 | /* ??? Follow the C++ parser rather than using the | |
2662 | c_lex_string_translate kludge. */ | |
2663 | c_lex_string_translate = 0; | |
2664 | c_parser_consume_token (parser); | |
2665 | if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>")) | |
2666 | { | |
2667 | c_lex_string_translate = 1; | |
2668 | return attrs; | |
2669 | } | |
2670 | if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>")) | |
2671 | { | |
2672 | c_lex_string_translate = 1; | |
2673 | c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL); | |
2674 | return attrs; | |
2675 | } | |
2676 | /* Parse the attribute list. */ | |
2677 | while (c_parser_next_token_is (parser, CPP_COMMA) | |
2678 | || c_parser_next_token_is (parser, CPP_NAME) | |
2679 | || c_parser_next_token_is (parser, CPP_KEYWORD)) | |
2680 | { | |
2681 | tree attr, attr_name, attr_args; | |
2682 | if (c_parser_next_token_is (parser, CPP_COMMA)) | |
2683 | { | |
2684 | c_parser_consume_token (parser); | |
2685 | continue; | |
2686 | } | |
2687 | if (c_parser_next_token_is (parser, CPP_KEYWORD)) | |
2688 | { | |
2689 | /* ??? See comment above about what keywords are | |
2690 | accepted here. */ | |
2691 | bool ok; | |
2692 | switch (c_parser_peek_token (parser)->keyword) | |
2693 | { | |
2694 | case RID_STATIC: | |
2695 | case RID_UNSIGNED: | |
2696 | case RID_LONG: | |
2697 | case RID_CONST: | |
2698 | case RID_EXTERN: | |
2699 | case RID_REGISTER: | |
2700 | case RID_TYPEDEF: | |
2701 | case RID_SHORT: | |
2702 | case RID_INLINE: | |
2703 | case RID_VOLATILE: | |
2704 | case RID_SIGNED: | |
2705 | case RID_AUTO: | |
2706 | case RID_RESTRICT: | |
2707 | case RID_COMPLEX: | |
2708 | case RID_THREAD: | |
2709 | case RID_INT: | |
2710 | case RID_CHAR: | |
2711 | case RID_FLOAT: | |
2712 | case RID_DOUBLE: | |
2713 | case RID_VOID: | |
2714 | case RID_BOOL: | |
2715 | ok = true; | |
2716 | break; | |
2717 | default: | |
2718 | ok = false; | |
2719 | break; | |
2720 | } | |
2721 | if (!ok) | |
2722 | break; | |
2723 | } | |
2724 | attr_name = c_parser_peek_token (parser)->value; | |
2725 | c_parser_consume_token (parser); | |
2726 | if (c_parser_next_token_is_not (parser, CPP_OPEN_PAREN)) | |
2727 | { | |
2728 | attr = build_tree_list (attr_name, NULL_TREE); | |
2729 | attrs = chainon (attrs, attr); | |
2730 | continue; | |
2731 | } | |
2732 | c_parser_consume_token (parser); | |
2733 | /* Parse the attribute contents. If they start with an | |
2734 | identifier which is followed by a comma or close | |
2735 | parenthesis, then the arguments start with that | |
2736 | identifier; otherwise they are an expression list. */ | |
2737 | if (c_parser_next_token_is (parser, CPP_NAME) | |
2738 | && c_parser_peek_token (parser)->id_kind == C_ID_ID | |
2739 | && ((c_parser_peek_2nd_token (parser)->type == CPP_COMMA) | |
2740 | || (c_parser_peek_2nd_token (parser)->type | |
2741 | == CPP_CLOSE_PAREN))) | |
2742 | { | |
2743 | tree arg1 = c_parser_peek_token (parser)->value; | |
2744 | c_parser_consume_token (parser); | |
2745 | if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN)) | |
2746 | attr_args = build_tree_list (NULL_TREE, arg1); | |
2747 | else | |
2748 | { | |
2749 | c_parser_consume_token (parser); | |
2750 | attr_args = tree_cons (NULL_TREE, arg1, | |
2751 | c_parser_expr_list (parser)); | |
2752 | } | |
2753 | } | |
2754 | else | |
2755 | { | |
2756 | if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN)) | |
2757 | attr_args = NULL_TREE; | |
2758 | else | |
2759 | attr_args = c_parser_expr_list (parser); | |
2760 | } | |
2761 | attr = build_tree_list (attr_name, attr_args); | |
2762 | if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN)) | |
2763 | c_parser_consume_token (parser); | |
2764 | else | |
2765 | { | |
2766 | c_lex_string_translate = 1; | |
2767 | c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, | |
2768 | "expected %<)%>"); | |
2769 | return attrs; | |
2770 | } | |
2771 | attrs = chainon (attrs, attr); | |
2772 | } | |
2773 | if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN)) | |
2774 | c_parser_consume_token (parser); | |
2775 | else | |
2776 | { | |
2777 | c_lex_string_translate = 1; | |
2778 | c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, | |
2779 | "expected %<)%>"); | |
2780 | return attrs; | |
2781 | } | |
2782 | if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN)) | |
2783 | c_parser_consume_token (parser); | |
2784 | else | |
2785 | { | |
2786 | c_lex_string_translate = 1; | |
2787 | c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, | |
2788 | "expected %<)%>"); | |
2789 | return attrs; | |
2790 | } | |
2791 | c_lex_string_translate = 1; | |
2792 | } | |
2793 | return attrs; | |
2794 | } | |
2795 | ||
2796 | /* Parse a type name (C90 6.5.5, C99 6.7.6). | |
2797 | ||
2798 | type-name: | |
2799 | specifier-qualifier-list abstract-declarator[opt] | |
2800 | */ | |
2801 | ||
2802 | static struct c_type_name * | |
2803 | c_parser_type_name (c_parser *parser) | |
2804 | { | |
2805 | struct c_declspecs *specs = build_null_declspecs (); | |
2806 | struct c_declarator *declarator; | |
2807 | struct c_type_name *ret; | |
2808 | bool dummy = false; | |
2809 | c_parser_declspecs (parser, specs, false, true, true); | |
2810 | if (!specs->declspecs_seen_p) | |
2811 | { | |
2812 | c_parser_error (parser, "expected specifier-qualifier-list"); | |
2813 | return NULL; | |
2814 | } | |
2815 | pending_xref_error (); | |
2816 | finish_declspecs (specs); | |
2817 | declarator = c_parser_declarator (parser, specs->type_seen_p, | |
2818 | C_DTR_ABSTRACT, &dummy); | |
2819 | if (declarator == NULL) | |
2820 | return NULL; | |
2821 | ret = XOBNEW (&parser_obstack, struct c_type_name); | |
2822 | ret->specs = specs; | |
2823 | ret->declarator = declarator; | |
2824 | return ret; | |
2825 | } | |
2826 | ||
2827 | /* Parse an initializer (C90 6.5.7, C99 6.7.8). | |
2828 | ||
2829 | initializer: | |
2830 | assignment-expression | |
2831 | { initializer-list } | |
2832 | { initializer-list , } | |
2833 | ||
2834 | initializer-list: | |
2835 | designation[opt] initializer | |
2836 | initializer-list , designation[opt] initializer | |
2837 | ||
2838 | designation: | |
2839 | designator-list = | |
2840 | ||
2841 | designator-list: | |
2842 | designator | |
2843 | designator-list designator | |
2844 | ||
2845 | designator: | |
2846 | array-designator | |
2847 | . identifier | |
2848 | ||
2849 | array-designator: | |
2850 | [ constant-expression ] | |
2851 | ||
2852 | GNU extensions: | |
2853 | ||
2854 | initializer: | |
2855 | { } | |
2856 | ||
2857 | designation: | |
2858 | array-designator | |
2859 | identifier : | |
2860 | ||
2861 | array-designator: | |
2862 | [ constant-expression ... constant-expression ] | |
2863 | ||
2864 | Any expression without commas is accepted in the syntax for the | |
2865 | constant-expressions, with non-constant expressions rejected later. | |
2866 | ||
2867 | This function is only used for top-level initializers; for nested | |
2868 | ones, see c_parser_initval. */ | |
2869 | ||
2870 | static struct c_expr | |
2871 | c_parser_initializer (c_parser *parser) | |
2872 | { | |
2873 | if (c_parser_next_token_is (parser, CPP_OPEN_BRACE)) | |
2874 | return c_parser_braced_init (parser, NULL_TREE, false); | |
2875 | else | |
2876 | return c_parser_expr_no_commas (parser, NULL); | |
2877 | } | |
2878 | ||
2879 | /* Parse a braced initializer list. TYPE is the type specified for a | |
2880 | compound literal, and NULL_TREE for other initializers and for | |
2881 | nested braced lists. NESTED_P is true for nested braced lists, | |
2882 | false for the list of a compound literal or the list that is the | |
2883 | top-level initializer in a declaration. */ | |
2884 | ||
2885 | static struct c_expr | |
2886 | c_parser_braced_init (c_parser *parser, tree type, bool nested_p) | |
2887 | { | |
2888 | gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE)); | |
2889 | c_parser_consume_token (parser); | |
2890 | if (nested_p) | |
2891 | push_init_level (0); | |
2892 | else | |
2893 | really_start_incremental_init (type); | |
2894 | if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE)) | |
2895 | { | |
2896 | if (pedantic) | |
2897 | pedwarn ("ISO C forbids empty initializer braces"); | |
2898 | } | |
2899 | else | |
2900 | { | |
2901 | /* Parse a non-empty initializer list, possibly with a trailing | |
2902 | comma. */ | |
2903 | while (true) | |
2904 | { | |
2905 | c_parser_initelt (parser); | |
2906 | if (parser->error) | |
2907 | break; | |
2908 | if (c_parser_next_token_is (parser, CPP_COMMA)) | |
2909 | c_parser_consume_token (parser); | |
2910 | else | |
2911 | break; | |
2912 | if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE)) | |
2913 | break; | |
2914 | } | |
2915 | } | |
2916 | if (c_parser_next_token_is_not (parser, CPP_CLOSE_BRACE)) | |
2917 | { | |
2918 | struct c_expr ret; | |
2919 | ret.value = error_mark_node; | |
2920 | ret.original_code = ERROR_MARK; | |
2921 | c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, "expected %<}%>"); | |
2922 | return ret; | |
2923 | } | |
2924 | c_parser_consume_token (parser); | |
2925 | return pop_init_level (0); | |
2926 | } | |
2927 | ||
2928 | /* Parse a nested initializer, including designators. */ | |
2929 | ||
2930 | static void | |
2931 | c_parser_initelt (c_parser *parser) | |
2932 | { | |
2933 | /* Parse any designator or designator list. A single array | |
2934 | designator may have the subsequent "=" omitted in GNU C, but a | |
2935 | longer list or a structure member designator may not. */ | |
2936 | if (c_parser_next_token_is (parser, CPP_NAME) | |
2937 | && c_parser_peek_2nd_token (parser)->type == CPP_COLON) | |
2938 | { | |
2939 | /* Old-style structure member designator. */ | |
2940 | set_init_label (c_parser_peek_token (parser)->value); | |
2941 | if (pedantic) | |
2942 | pedwarn ("obsolete use of designated initializer with %<:%>"); | |
2943 | c_parser_consume_token (parser); | |
2944 | c_parser_consume_token (parser); | |
2945 | } | |
2946 | else | |
2947 | { | |
2948 | /* des_seen is 0 if there have been no designators, 1 if there | |
2949 | has been a single array designator and 2 otherwise. */ | |
2950 | int des_seen = 0; | |
2951 | while (c_parser_next_token_is (parser, CPP_OPEN_SQUARE) | |
2952 | || c_parser_next_token_is (parser, CPP_DOT)) | |
2953 | { | |
2954 | int des_prev = des_seen; | |
2955 | if (des_seen < 2) | |
2956 | des_seen++; | |
2957 | if (c_parser_next_token_is (parser, CPP_DOT)) | |
2958 | { | |
2959 | des_seen = 2; | |
2960 | c_parser_consume_token (parser); | |
2961 | if (c_parser_next_token_is (parser, CPP_NAME)) | |
2962 | { | |
2963 | set_init_label (c_parser_peek_token (parser)->value); | |
2964 | c_parser_consume_token (parser); | |
2965 | } | |
2966 | else | |
2967 | { | |
2968 | struct c_expr init; | |
2969 | init.value = error_mark_node; | |
2970 | init.original_code = ERROR_MARK; | |
2971 | c_parser_error (parser, "expected identifier"); | |
2972 | c_parser_skip_until_found (parser, CPP_COMMA, NULL); | |
2973 | process_init_element (init); | |
2974 | return; | |
2975 | } | |
2976 | } | |
2977 | else | |
2978 | { | |
2979 | tree first, second; | |
2980 | /* ??? Following the old parser, [ objc-receiver | |
2981 | objc-message-args ] is accepted as an initializer, | |
2982 | being distinguished from a designator by what follows | |
2983 | the first assignment expression inside the square | |
2984 | brackets, but after a first array designator a | |
2985 | subsequent square bracket is for Objective-C taken to | |
2986 | start an expression, using the obsolete form of | |
2987 | designated initializer without '=', rather than | |
2988 | possibly being a second level of designation: in LALR | |
2989 | terms, the '[' is shifted rather than reducing | |
2990 | designator to designator-list. */ | |
2991 | if (des_prev == 1 && c_dialect_objc ()) | |
2992 | { | |
2993 | des_seen = des_prev; | |
2994 | break; | |
2995 | } | |
2996 | if (des_prev == 0 && c_dialect_objc ()) | |
2997 | { | |
2998 | /* This might be an array designator or an | |
2999 | Objective-C message expression. If the former, | |
3000 | continue parsing here; if the latter, parse the | |
3001 | remainder of the initializer given the starting | |
3002 | primary-expression. ??? It might make sense to | |
3003 | distinguish when des_prev == 1 as well; see | |
3004 | previous comment. */ | |
3005 | tree rec, args; | |
3006 | struct c_expr mexpr; | |
3007 | c_parser_consume_token (parser); | |
3008 | if (c_parser_peek_token (parser)->type == CPP_NAME | |
3009 | && ((c_parser_peek_token (parser)->id_kind | |
3010 | == C_ID_TYPENAME) | |
3011 | || (c_parser_peek_token (parser)->id_kind | |
3012 | == C_ID_CLASSNAME))) | |
3013 | { | |
3014 | /* Type name receiver. */ | |
3015 | tree id = c_parser_peek_token (parser)->value; | |
3016 | c_parser_consume_token (parser); | |
3017 | rec = objc_get_class_reference (id); | |
3018 | goto parse_message_args; | |
3019 | } | |
3020 | first = c_parser_expr_no_commas (parser, NULL).value; | |
3021 | if (c_parser_next_token_is (parser, CPP_ELLIPSIS) | |
3022 | || c_parser_next_token_is (parser, CPP_CLOSE_SQUARE)) | |
3023 | goto array_desig_after_first; | |
3024 | /* Expression receiver. So far only one part | |
3025 | without commas has been parsed; there might be | |
3026 | more of the expression. */ | |
3027 | rec = first; | |
3028 | while (c_parser_next_token_is (parser, CPP_COMMA)) | |
3029 | { | |
3030 | tree next; | |
3031 | c_parser_consume_token (parser); | |
3032 | next = c_parser_expr_no_commas (parser, NULL).value; | |
3033 | rec = build_compound_expr (rec, next); | |
3034 | } | |
3035 | parse_message_args: | |
3036 | /* Now parse the objc-message-args. */ | |
3037 | args = c_parser_objc_message_args (parser); | |
3038 | c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, | |
3039 | "expected %<]%>"); | |
3040 | mexpr.value | |
3041 | = objc_build_message_expr (build_tree_list (rec, args)); | |
3042 | mexpr.original_code = ERROR_MARK; | |
3043 | /* Now parse and process the remainder of the | |
3044 | initializer, starting with this message | |
3045 | expression as a primary-expression. */ | |
3046 | c_parser_initval (parser, &mexpr); | |
3047 | return; | |
3048 | } | |
3049 | c_parser_consume_token (parser); | |
3050 | first = c_parser_expr_no_commas (parser, NULL).value; | |
3051 | array_desig_after_first: | |
3052 | if (c_parser_next_token_is (parser, CPP_ELLIPSIS)) | |
3053 | { | |
3054 | c_parser_consume_token (parser); | |
3055 | second = c_parser_expr_no_commas (parser, NULL).value; | |
3056 | } | |
3057 | else | |
3058 | second = NULL_TREE; | |
3059 | if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE)) | |
3060 | { | |
3061 | c_parser_consume_token (parser); | |
3062 | set_init_index (first, second); | |
3063 | if (pedantic && second) | |
3064 | pedwarn ("ISO C forbids specifying range of " | |
3065 | "elements to initialize"); | |
3066 | } | |
3067 | else | |
3068 | c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, | |
3069 | "expected %<]%>"); | |
3070 | } | |
3071 | } | |
3072 | if (des_seen >= 1) | |
3073 | { | |
3074 | if (c_parser_next_token_is (parser, CPP_EQ)) | |
3075 | { | |
3076 | if (pedantic && !flag_isoc99) | |
3077 | pedwarn ("ISO C90 forbids specifying subobject to initialize"); | |
3078 | c_parser_consume_token (parser); | |
3079 | } | |
3080 | else | |
3081 | { | |
3082 | if (des_seen == 1) | |
3083 | { | |
3084 | if (pedantic) | |
3085 | pedwarn ("obsolete use of designated initializer " | |
3086 | "without %<=%>"); | |
3087 | } | |
3088 | else | |
3089 | { | |
3090 | struct c_expr init; | |
3091 | init.value = error_mark_node; | |
3092 | init.original_code = ERROR_MARK; | |
3093 | c_parser_error (parser, "expected %<=%>"); | |
3094 | c_parser_skip_until_found (parser, CPP_COMMA, NULL); | |
3095 | process_init_element (init); | |
3096 | return; | |
3097 | } | |
3098 | } | |
3099 | } | |
3100 | } | |
3101 | c_parser_initval (parser, NULL); | |
3102 | } | |
3103 | ||
3104 | /* Parse a nested initializer; as c_parser_initializer but parses | |
3105 | initializers within braced lists, after any designators have been | |
3106 | applied. If AFTER is not NULL then it is an Objective-C message | |
3107 | expression which is the primary-expression starting the | |
3108 | initializer. */ | |
3109 | ||
3110 | static void | |
3111 | c_parser_initval (c_parser *parser, struct c_expr *after) | |
3112 | { | |
3113 | struct c_expr init; | |
3114 | gcc_assert (!after || c_dialect_objc ()); | |
3115 | if (c_parser_next_token_is (parser, CPP_OPEN_BRACE) && !after) | |
3116 | init = c_parser_braced_init (parser, NULL_TREE, true); | |
3117 | else | |
3118 | init = c_parser_expr_no_commas (parser, after); | |
3119 | process_init_element (init); | |
3120 | } | |
3121 | ||
3122 | /* Parse a compound statement (possibly a function body) (C90 6.6.2, | |
3123 | C99 6.8.2). | |
3124 | ||
3125 | compound-statement: | |
3126 | { block-item-list[opt] } | |
3127 | { label-declarations block-item-list } | |
3128 | ||
3129 | block-item-list: | |
3130 | block-item | |
3131 | block-item-list block-item | |
3132 | ||
3133 | block-item: | |
3134 | nested-declaration | |
3135 | statement | |
3136 | ||
3137 | nested-declaration: | |
3138 | declaration | |
3139 | ||
3140 | GNU extensions: | |
3141 | ||
3142 | compound-statement: | |
3143 | { label-declarations block-item-list } | |
3144 | ||
3145 | nested-declaration: | |
3146 | __extension__ nested-declaration | |
3147 | nested-function-definition | |
3148 | ||
3149 | label-declarations: | |
3150 | label-declaration | |
3151 | label-declarations label-declaration | |
3152 | ||
3153 | label-declaration: | |
3154 | __label__ identifier-list ; | |
3155 | ||
3156 | Allowing the mixing of declarations and code is new in C99. The | |
3157 | GNU syntax also permits (not shown above) labels at the end of | |
3158 | compound statements, which yield an error. We don't allow labels | |
3159 | on declarations; this might seem like a natural extension, but | |
3160 | there would be a conflict between attributes on the label and | |
3161 | prefix attributes on the declaration. ??? The syntax follows the | |
3162 | old parser in requiring something after label declarations. | |
3163 | Although they are erroneous if the labels declared aren't defined, | |
3164 | is it useful for the syntax to be this way? */ | |
3165 | ||
3166 | static tree | |
3167 | c_parser_compound_statement (c_parser *parser) | |
3168 | { | |
3169 | tree stmt; | |
3170 | if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>")) | |
4e3d9146 | 3171 | return error_mark_node; |
27bf414c JM |
3172 | stmt = c_begin_compound_stmt (true); |
3173 | c_parser_compound_statement_nostart (parser); | |
3174 | return c_end_compound_stmt (stmt, true); | |
3175 | } | |
3176 | ||
3177 | /* Parse a compound statement except for the opening brace. This is | |
3178 | used for parsing both compound statements and statement expressions | |
3179 | (which follow different paths to handling the opening). */ | |
3180 | ||
3181 | static void | |
3182 | c_parser_compound_statement_nostart (c_parser *parser) | |
3183 | { | |
3184 | bool last_stmt = false; | |
3185 | bool last_label = false; | |
3186 | if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE)) | |
3187 | { | |
3188 | c_parser_consume_token (parser); | |
3189 | return; | |
3190 | } | |
3191 | if (c_parser_next_token_is_keyword (parser, RID_LABEL)) | |
3192 | { | |
3193 | /* Read zero or more forward-declarations for labels that nested | |
3194 | functions can jump to. */ | |
3195 | while (c_parser_next_token_is_keyword (parser, RID_LABEL)) | |
3196 | { | |
3197 | c_parser_consume_token (parser); | |
3198 | /* Any identifiers, including those declared as type names, | |
3199 | are OK here. */ | |
3200 | while (true) | |
3201 | { | |
3202 | tree label; | |
3203 | if (c_parser_next_token_is_not (parser, CPP_NAME)) | |
3204 | { | |
3205 | c_parser_error (parser, "expected identifier"); | |
3206 | break; | |
3207 | } | |
3208 | label | |
3209 | = declare_label (c_parser_peek_token (parser)->value); | |
3210 | C_DECLARED_LABEL_FLAG (label) = 1; | |
3211 | add_stmt (build_stmt (DECL_EXPR, label)); | |
3212 | c_parser_consume_token (parser); | |
3213 | if (c_parser_next_token_is (parser, CPP_COMMA)) | |
3214 | c_parser_consume_token (parser); | |
3215 | else | |
3216 | break; | |
3217 | } | |
3218 | c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>"); | |
3219 | } | |
3220 | /* ??? Locating this diagnostic on the token after the | |
3221 | declarations end follows the old parser, but it might be | |
3222 | better to locate it where the declarations start instead. */ | |
3223 | if (pedantic) | |
3224 | pedwarn ("ISO C forbids label declarations"); | |
3225 | } | |
3226 | /* We must now have at least one statement, label or declaration. */ | |
3227 | if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE)) | |
3228 | { | |
3229 | c_parser_error (parser, "expected declaration or statement"); | |
3230 | c_parser_consume_token (parser); | |
3231 | return; | |
3232 | } | |
3233 | while (c_parser_next_token_is_not (parser, CPP_CLOSE_BRACE)) | |
3234 | { | |
3235 | location_t loc = c_parser_peek_token (parser)->location; | |
3236 | if (c_parser_next_token_is (parser, CPP_EOF)) | |
3237 | { | |
d1edc393 | 3238 | c_parser_error (parser, "expected declaration or statement"); |
27bf414c JM |
3239 | return; |
3240 | } | |
3241 | if (c_parser_next_token_is_keyword (parser, RID_CASE) | |
3242 | || c_parser_next_token_is_keyword (parser, RID_DEFAULT) | |
3243 | || (c_parser_next_token_is (parser, CPP_NAME) | |
3244 | && c_parser_peek_2nd_token (parser)->type == CPP_COLON)) | |
3245 | { | |
3246 | last_label = true; | |
3247 | last_stmt = false; | |
3248 | c_parser_label (parser); | |
3249 | } | |
3250 | else if (!last_label | |
3251 | && c_parser_next_token_starts_declspecs (parser)) | |
3252 | { | |
3253 | last_label = false; | |
3254 | c_parser_declaration_or_fndef (parser, true, true, true, true); | |
3255 | if (last_stmt | |
3256 | && ((pedantic && !flag_isoc99) | |
3257 | || warn_declaration_after_statement)) | |
3258 | pedwarn_c90 ("%HISO C90 forbids mixed declarations and code", | |
3259 | &loc); | |
3260 | last_stmt = false; | |
3261 | } | |
3262 | else if (!last_label | |
3263 | && c_parser_next_token_is_keyword (parser, RID_EXTENSION)) | |
3264 | { | |
3265 | /* __extension__ can start a declaration, but is also an | |
3266 | unary operator that can start an expression. Consume all | |
3267 | but the last of a possible series of __extension__ to | |
3268 | determine which. */ | |
3269 | while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD | |
3270 | && (c_parser_peek_2nd_token (parser)->keyword | |
3271 | == RID_EXTENSION)) | |
3272 | c_parser_consume_token (parser); | |
3273 | if (c_token_starts_declspecs (c_parser_peek_2nd_token (parser))) | |
3274 | { | |
3275 | int ext; | |
3276 | ext = disable_extension_diagnostics (); | |
3277 | c_parser_consume_token (parser); | |
3278 | last_label = false; | |
3279 | c_parser_declaration_or_fndef (parser, true, true, true, true); | |
3280 | /* Following the old parser, __extension__ does not | |
3281 | disable this diagnostic. */ | |
3282 | restore_extension_diagnostics (ext); | |
3283 | if (last_stmt | |
3284 | && ((pedantic && !flag_isoc99) | |
3285 | || warn_declaration_after_statement)) | |
3286 | pedwarn_c90 ("%HISO C90 forbids mixed declarations and code", | |
3287 | &loc); | |
3288 | last_stmt = false; | |
3289 | } | |
3290 | else | |
3291 | goto statement; | |
3292 | } | |
3293 | else | |
3294 | { | |
3295 | statement: | |
3296 | last_label = false; | |
3297 | last_stmt = true; | |
3298 | c_parser_statement_after_labels (parser); | |
3299 | } | |
3300 | } | |
3301 | if (last_label) | |
3302 | error ("label at end of compound statement"); | |
3303 | c_parser_consume_token (parser); | |
3304 | } | |
3305 | ||
3306 | /* Parse a label (C90 6.6.1, C99 6.8.1). | |
3307 | ||
3308 | label: | |
3309 | identifier : attributes[opt] | |
3310 | case constant-expression : | |
3311 | default : | |
3312 | ||
3313 | GNU extensions: | |
3314 | ||
3315 | label: | |
3316 | case constant-expression ... constant-expression : | |
3317 | ||
3318 | The use of attributes on labels is a GNU extension. The syntax in | |
3319 | GNU C accepts any expressions without commas, non-constant | |
3320 | expressions being rejected later. */ | |
3321 | ||
3322 | static void | |
3323 | c_parser_label (c_parser *parser) | |
3324 | { | |
3325 | location_t loc1 = c_parser_peek_token (parser)->location; | |
3326 | tree label = NULL_TREE; | |
3327 | if (c_parser_next_token_is_keyword (parser, RID_CASE)) | |
3328 | { | |
3329 | tree exp1, exp2; | |
3330 | c_parser_consume_token (parser); | |
3331 | exp1 = c_parser_expr_no_commas (parser, NULL).value; | |
3332 | if (c_parser_next_token_is (parser, CPP_COLON)) | |
3333 | { | |
3334 | c_parser_consume_token (parser); | |
3335 | label = do_case (exp1, NULL_TREE); | |
3336 | } | |
3337 | else if (c_parser_next_token_is (parser, CPP_ELLIPSIS)) | |
3338 | { | |
3339 | c_parser_consume_token (parser); | |
3340 | exp2 = c_parser_expr_no_commas (parser, NULL).value; | |
3341 | if (c_parser_require (parser, CPP_COLON, "expected %<:%>")) | |
3342 | label = do_case (exp1, exp2); | |
3343 | } | |
3344 | else | |
3345 | c_parser_error (parser, "expected %<:%> or %<...%>"); | |
3346 | } | |
3347 | else if (c_parser_next_token_is_keyword (parser, RID_DEFAULT)) | |
3348 | { | |
3349 | c_parser_consume_token (parser); | |
3350 | if (c_parser_require (parser, CPP_COLON, "expected %<:%>")) | |
3351 | label = do_case (NULL_TREE, NULL_TREE); | |
3352 | } | |
3353 | else | |
3354 | { | |
3355 | tree name = c_parser_peek_token (parser)->value; | |
3356 | tree tlab; | |
3357 | location_t loc2; | |
3358 | tree attrs; | |
3359 | gcc_assert (c_parser_next_token_is (parser, CPP_NAME)); | |
3360 | c_parser_consume_token (parser); | |
3361 | gcc_assert (c_parser_next_token_is (parser, CPP_COLON)); | |
3362 | loc2 = c_parser_peek_token (parser)->location; | |
3363 | c_parser_consume_token (parser); | |
3364 | attrs = c_parser_attributes (parser); | |
3365 | tlab = define_label (loc2, name); | |
3366 | if (tlab) | |
3367 | { | |
3368 | decl_attributes (&tlab, attrs, 0); | |
3369 | label = add_stmt (build_stmt (LABEL_EXPR, tlab)); | |
3370 | } | |
3371 | } | |
3372 | if (label) | |
3373 | SET_EXPR_LOCATION (label, loc1); | |
3374 | } | |
3375 | ||
3376 | /* Parse a statement (C90 6.6, C99 6.8). | |
3377 | ||
3378 | statement: | |
3379 | labeled-statement | |
3380 | compound-statement | |
3381 | expression-statement | |
3382 | selection-statement | |
3383 | iteration-statement | |
3384 | jump-statement | |
3385 | ||
3386 | labeled-statement: | |
3387 | label statement | |
3388 | ||
3389 | expression-statement: | |
3390 | expression[opt] ; | |
3391 | ||
3392 | selection-statement: | |
3393 | if-statement | |
3394 | switch-statement | |
3395 | ||
3396 | iteration-statement: | |
3397 | while-statement | |
3398 | do-statement | |
3399 | for-statement | |
3400 | ||
3401 | jump-statement: | |
3402 | goto identifier ; | |
3403 | continue ; | |
3404 | break ; | |
3405 | return expression[opt] ; | |
3406 | ||
3407 | GNU extensions: | |
3408 | ||
3409 | statement: | |
3410 | asm-statement | |
3411 | ||
3412 | jump-statement: | |
3413 | goto * expression ; | |
3414 | ||
3415 | Objective-C: | |
3416 | ||
3417 | statement: | |
3418 | objc-throw-statement | |
3419 | objc-try-catch-statement | |
3420 | objc-synchronized-statement | |
3421 | ||
3422 | objc-throw-statement: | |
3423 | @throw expression ; | |
3424 | @throw ; | |
3425 | */ | |
3426 | ||
3427 | static void | |
3428 | c_parser_statement (c_parser *parser) | |
3429 | { | |
3430 | while (c_parser_next_token_is_keyword (parser, RID_CASE) | |
3431 | || c_parser_next_token_is_keyword (parser, RID_DEFAULT) | |
3432 | || (c_parser_next_token_is (parser, CPP_NAME) | |
3433 | && c_parser_peek_2nd_token (parser)->type == CPP_COLON)) | |
3434 | c_parser_label (parser); | |
3435 | c_parser_statement_after_labels (parser); | |
3436 | } | |
3437 | ||
3438 | /* Parse a statement, other than a labeled statement. */ | |
3439 | ||
3440 | static void | |
3441 | c_parser_statement_after_labels (c_parser *parser) | |
3442 | { | |
3443 | location_t loc = c_parser_peek_token (parser)->location; | |
3444 | tree stmt = NULL_TREE; | |
3445 | switch (c_parser_peek_token (parser)->type) | |
3446 | { | |
3447 | case CPP_OPEN_BRACE: | |
3448 | add_stmt (c_parser_compound_statement (parser)); | |
3449 | break; | |
3450 | case CPP_KEYWORD: | |
3451 | switch (c_parser_peek_token (parser)->keyword) | |
3452 | { | |
3453 | case RID_IF: | |
3454 | c_parser_if_statement (parser); | |
3455 | break; | |
3456 | case RID_SWITCH: | |
3457 | c_parser_switch_statement (parser); | |
3458 | break; | |
3459 | case RID_WHILE: | |
3460 | c_parser_while_statement (parser); | |
3461 | break; | |
3462 | case RID_DO: | |
3463 | c_parser_do_statement (parser); | |
3464 | break; | |
3465 | case RID_FOR: | |
3466 | c_parser_for_statement (parser); | |
3467 | break; | |
3468 | case RID_GOTO: | |
3469 | c_parser_consume_token (parser); | |
3470 | if (c_parser_next_token_is (parser, CPP_NAME)) | |
3471 | { | |
3472 | stmt = c_finish_goto_label (c_parser_peek_token (parser)->value); | |
3473 | c_parser_consume_token (parser); | |
3474 | } | |
3475 | else if (c_parser_next_token_is (parser, CPP_MULT)) | |
3476 | { | |
3477 | c_parser_consume_token (parser); | |
3478 | stmt = c_finish_goto_ptr (c_parser_expression (parser).value); | |
3479 | } | |
3480 | else | |
3481 | c_parser_error (parser, "expected identifier or %<*%>"); | |
3482 | goto expect_semicolon; | |
3483 | case RID_CONTINUE: | |
3484 | c_parser_consume_token (parser); | |
3485 | stmt = c_finish_bc_stmt (&c_cont_label, false); | |
3486 | goto expect_semicolon; | |
3487 | case RID_BREAK: | |
3488 | c_parser_consume_token (parser); | |
3489 | stmt = c_finish_bc_stmt (&c_break_label, true); | |
3490 | goto expect_semicolon; | |
3491 | case RID_RETURN: | |
3492 | c_parser_consume_token (parser); | |
3493 | if (c_parser_next_token_is (parser, CPP_SEMICOLON)) | |
3494 | { | |
3495 | stmt = c_finish_return (NULL_TREE); | |
3496 | c_parser_consume_token (parser); | |
3497 | } | |
3498 | else | |
3499 | { | |
3500 | stmt = c_finish_return (c_parser_expression (parser).value); | |
3501 | goto expect_semicolon; | |
3502 | } | |
3503 | break; | |
3504 | case RID_ASM: | |
3505 | stmt = c_parser_asm_statement (parser); | |
3506 | break; | |
3507 | case RID_AT_THROW: | |
3508 | gcc_assert (c_dialect_objc ()); | |
3509 | c_parser_consume_token (parser); | |
3510 | if (c_parser_next_token_is (parser, CPP_SEMICOLON)) | |
3511 | { | |
3512 | stmt = objc_build_throw_stmt (NULL_TREE); | |
3513 | c_parser_consume_token (parser); | |
3514 | } | |
3515 | else | |
3516 | { | |
3517 | stmt | |
3518 | = objc_build_throw_stmt (c_parser_expression (parser).value); | |
3519 | goto expect_semicolon; | |
3520 | } | |
3521 | break; | |
3522 | case RID_AT_TRY: | |
3523 | gcc_assert (c_dialect_objc ()); | |
3524 | c_parser_objc_try_catch_statement (parser); | |
3525 | break; | |
3526 | case RID_AT_SYNCHRONIZED: | |
3527 | gcc_assert (c_dialect_objc ()); | |
3528 | c_parser_objc_synchronized_statement (parser); | |
3529 | break; | |
3530 | default: | |
3531 | goto expr_stmt; | |
3532 | } | |
3533 | break; | |
3534 | case CPP_SEMICOLON: | |
3535 | c_parser_consume_token (parser); | |
3536 | break; | |
3537 | case CPP_CLOSE_PAREN: | |
3538 | case CPP_CLOSE_SQUARE: | |
3539 | /* Avoid infinite loop in error recovery: | |
3540 | c_parser_skip_until_found stops at a closing nesting | |
3541 | delimiter without consuming it, but here we need to consume | |
3542 | it to proceed further. */ | |
3543 | c_parser_error (parser, "expected statement"); | |
3544 | c_parser_consume_token (parser); | |
3545 | break; | |
3546 | default: | |
3547 | expr_stmt: | |
3548 | stmt = c_finish_expr_stmt (c_parser_expression (parser).value); | |
3549 | expect_semicolon: | |
3550 | c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>"); | |
3551 | break; | |
3552 | } | |
3553 | /* Two cases cannot and do not have line numbers associated: If stmt | |
3554 | is degenerate, such as "2;", then stmt is an INTEGER_CST, which | |
3555 | cannot hold line numbers. But that's OK because the statement | |
3556 | will either be changed to a MODIFY_EXPR during gimplification of | |
3557 | the statement expr, or discarded. If stmt was compound, but | |
3558 | without new variables, we will have skipped the creation of a | |
3559 | BIND and will have a bare STATEMENT_LIST. But that's OK because | |
3560 | (recursively) all of the component statements should already have | |
3561 | line numbers assigned. ??? Can we discard no-op statements | |
3562 | earlier? */ | |
3563 | if (stmt && EXPR_P (stmt)) | |
3564 | SET_EXPR_LOCATION (stmt, loc); | |
3565 | } | |
3566 | ||
3567 | /* Parse a parenthesized condition from an if, do or while statement. | |
3568 | ||
3569 | condition: | |
3570 | ( expression ) | |
3571 | */ | |
3572 | static tree | |
3573 | c_parser_paren_condition (c_parser *parser) | |
3574 | { | |
3575 | location_t loc; | |
3576 | tree cond; | |
3577 | if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>")) | |
3578 | return error_mark_node; | |
3579 | loc = c_parser_peek_token (parser)->location; | |
85498824 JM |
3580 | cond = c_objc_common_truthvalue_conversion |
3581 | (c_parser_expression (parser).value); | |
27bf414c JM |
3582 | if (EXPR_P (cond)) |
3583 | SET_EXPR_LOCATION (cond, loc); | |
3584 | c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>"); | |
3585 | return cond; | |
3586 | } | |
3587 | ||
3588 | /* Parse a statement which is a block in C99. */ | |
3589 | ||
3590 | static tree | |
3591 | c_parser_c99_block_statement (c_parser *parser) | |
3592 | { | |
3593 | tree block = c_begin_compound_stmt (flag_isoc99); | |
3594 | c_parser_statement (parser); | |
3595 | return c_end_compound_stmt (block, flag_isoc99); | |
3596 | } | |
3597 | ||
3598 | /* Parse the body of an if statement or the else half thereof. This | |
3599 | is just parsing a statement but (a) it is a block in C99, (b) we | |
3600 | track whether the body is an if statement for the sake of | |
3601 | -Wparentheses warnings, (c) we handle an empty body specially for | |
3602 | the sake of -Wextra warnings. */ | |
3603 | ||
3604 | static tree | |
3605 | c_parser_if_body (c_parser *parser, bool *if_p) | |
3606 | { | |
3607 | tree block = c_begin_compound_stmt (flag_isoc99); | |
3608 | while (c_parser_next_token_is_keyword (parser, RID_CASE) | |
3609 | || c_parser_next_token_is_keyword (parser, RID_DEFAULT) | |
3610 | || (c_parser_next_token_is (parser, CPP_NAME) | |
3611 | && c_parser_peek_2nd_token (parser)->type == CPP_COLON)) | |
3612 | c_parser_label (parser); | |
3613 | *if_p = c_parser_next_token_is_keyword (parser, RID_IF); | |
3614 | if (extra_warnings && c_parser_next_token_is (parser, CPP_SEMICOLON)) | |
3615 | add_stmt (build (NOP_EXPR, NULL_TREE, NULL_TREE)); | |
3616 | c_parser_statement_after_labels (parser); | |
3617 | return c_end_compound_stmt (block, flag_isoc99); | |
3618 | } | |
3619 | ||
3620 | /* Parse an if statement (C90 6.6.4, C99 6.8.4). | |
3621 | ||
3622 | if-statement: | |
3623 | if ( expression ) statement | |
3624 | if ( expression ) statement else statement | |
3625 | */ | |
3626 | ||
3627 | static void | |
3628 | c_parser_if_statement (c_parser *parser) | |
3629 | { | |
3630 | tree block; | |
3631 | location_t loc; | |
3632 | tree cond; | |
3633 | bool first_if = false, second_if = false; | |
3634 | tree first_body, second_body; | |
3635 | gcc_assert (c_parser_next_token_is_keyword (parser, RID_IF)); | |
3636 | c_parser_consume_token (parser); | |
3637 | block = c_begin_compound_stmt (flag_isoc99); | |
3638 | loc = c_parser_peek_token (parser)->location; | |
3639 | cond = c_parser_paren_condition (parser); | |
3640 | first_body = c_parser_if_body (parser, &first_if); | |
3641 | if (c_parser_next_token_is_keyword (parser, RID_ELSE)) | |
3642 | { | |
3643 | c_parser_consume_token (parser); | |
3644 | second_body = c_parser_if_body (parser, &second_if); | |
3645 | } | |
3646 | else | |
3647 | second_body = NULL_TREE; | |
3648 | c_finish_if_stmt (loc, cond, first_body, second_body, first_if); | |
3649 | add_stmt (c_end_compound_stmt (block, flag_isoc99)); | |
3650 | } | |
3651 | ||
3652 | /* Parse a switch statement (C90 6.6.4, C99 6.8.4). | |
3653 | ||
3654 | switch-statement: | |
3655 | switch (expression) statement | |
3656 | */ | |
3657 | ||
3658 | static void | |
3659 | c_parser_switch_statement (c_parser *parser) | |
3660 | { | |
3661 | tree block, expr, body, save_break; | |
3662 | gcc_assert (c_parser_next_token_is_keyword (parser, RID_SWITCH)); | |
3663 | c_parser_consume_token (parser); | |
3664 | block = c_begin_compound_stmt (flag_isoc99); | |
3665 | if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>")) | |
3666 | { | |
3667 | expr = c_parser_expression (parser).value; | |
3668 | c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>"); | |
3669 | } | |
3670 | else | |
3671 | expr = error_mark_node; | |
3672 | c_start_case (expr); | |
3673 | save_break = c_break_label; | |
3674 | c_break_label = NULL_TREE; | |
3675 | body = c_parser_c99_block_statement (parser); | |
3676 | c_finish_case (body); | |
3677 | if (c_break_label) | |
3678 | add_stmt (build (LABEL_EXPR, void_type_node, c_break_label)); | |
3679 | c_break_label = save_break; | |
3680 | add_stmt (c_end_compound_stmt (block, flag_isoc99)); | |
3681 | } | |
3682 | ||
3683 | /* Parse a while statement (C90 6.6.5, C99 6.8.5). | |
3684 | ||
3685 | while-statement: | |
3686 | while (expression) statement | |
3687 | */ | |
3688 | ||
3689 | static void | |
3690 | c_parser_while_statement (c_parser *parser) | |
3691 | { | |
3692 | tree block, cond, body, save_break, save_cont; | |
3693 | location_t loc; | |
3694 | gcc_assert (c_parser_next_token_is_keyword (parser, RID_WHILE)); | |
3695 | c_parser_consume_token (parser); | |
3696 | block = c_begin_compound_stmt (flag_isoc99); | |
3697 | loc = c_parser_peek_token (parser)->location; | |
3698 | cond = c_parser_paren_condition (parser); | |
3699 | save_break = c_break_label; | |
3700 | c_break_label = NULL_TREE; | |
3701 | save_cont = c_cont_label; | |
3702 | c_cont_label = NULL_TREE; | |
3703 | body = c_parser_c99_block_statement (parser); | |
3704 | c_finish_loop (loc, cond, NULL, body, c_break_label, c_cont_label, true); | |
3705 | add_stmt (c_end_compound_stmt (block, flag_isoc99)); | |
3706 | c_break_label = save_break; | |
3707 | c_cont_label = save_cont; | |
3708 | } | |
3709 | ||
3710 | /* Parse a do statement (C90 6.6.5, C99 6.8.5). | |
3711 | ||
3712 | do-statement: | |
3713 | do statement while ( expression ) ; | |
3714 | */ | |
3715 | ||
3716 | static void | |
3717 | c_parser_do_statement (c_parser *parser) | |
3718 | { | |
3719 | tree block, cond, body, save_break, save_cont, new_break, new_cont; | |
3720 | location_t loc; | |
3721 | gcc_assert (c_parser_next_token_is_keyword (parser, RID_DO)); | |
3722 | c_parser_consume_token (parser); | |
3723 | block = c_begin_compound_stmt (flag_isoc99); | |
3724 | loc = c_parser_peek_token (parser)->location; | |
3725 | save_break = c_break_label; | |
3726 | c_break_label = NULL_TREE; | |
3727 | save_cont = c_cont_label; | |
3728 | c_cont_label = NULL_TREE; | |
3729 | body = c_parser_c99_block_statement (parser); | |
3730 | c_parser_require_keyword (parser, RID_WHILE, "expected %<while%>"); | |
3731 | new_break = c_break_label; | |
3732 | c_break_label = save_break; | |
3733 | new_cont = c_cont_label; | |
3734 | c_cont_label = save_cont; | |
3735 | cond = c_parser_paren_condition (parser); | |
3736 | if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>")) | |
3737 | c_parser_skip_to_end_of_block_or_statement (parser); | |
3738 | c_finish_loop (loc, cond, NULL, body, new_break, new_cont, false); | |
3739 | add_stmt (c_end_compound_stmt (block, flag_isoc99)); | |
3740 | } | |
3741 | ||
3742 | /* Parse a for statement (C90 6.6.5, C99 6.8.5). | |
3743 | ||
3744 | for-statement: | |
3745 | for ( expression[opt] ; expression[opt] ; expression[opt] ) statement | |
3746 | for ( nested-declaration expression[opt] ; expression[opt] ) statement | |
3747 | ||
3748 | The form with a declaration is new in C99. | |
3749 | ||
3750 | ??? In accordance with the old parser, the declaration may be a | |
3751 | nested function, which is then rejected in check_for_loop_decls, | |
3752 | but does it make any sense for this to be included in the grammar? | |
3753 | Note in particular that the nested function does not include a | |
3754 | trailing ';', whereas the "declaration" production includes one. | |
3755 | Also, can we reject bad declarations earlier and cheaper than | |
3756 | check_for_loop_decls? */ | |
3757 | ||
3758 | static void | |
3759 | c_parser_for_statement (c_parser *parser) | |
3760 | { | |
3761 | tree block, cond, incr, save_break, save_cont, body; | |
dbce1570 | 3762 | location_t loc = UNKNOWN_LOCATION; |
27bf414c JM |
3763 | gcc_assert (c_parser_next_token_is_keyword (parser, RID_FOR)); |
3764 | c_parser_consume_token (parser); | |
3765 | block = c_begin_compound_stmt (flag_isoc99); | |
3766 | if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>")) | |
3767 | { | |
3768 | /* Parse the initialization declaration or expression. */ | |
3769 | if (c_parser_next_token_is (parser, CPP_SEMICOLON)) | |
3770 | { | |
3771 | c_parser_consume_token (parser); | |
3772 | c_finish_expr_stmt (NULL_TREE); | |
3773 | } | |
3774 | else if (c_parser_next_token_starts_declspecs (parser)) | |
3775 | { | |
3776 | c_parser_declaration_or_fndef (parser, true, true, true, true); | |
3777 | check_for_loop_decls (); | |
3778 | } | |
3779 | else if (c_parser_next_token_is_keyword (parser, RID_EXTENSION)) | |
3780 | { | |
3781 | /* __extension__ can start a declaration, but is also an | |
3782 | unary operator that can start an expression. Consume all | |
3783 | but the last of a possible series of __extension__ to | |
3784 | determine which. */ | |
3785 | while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD | |
3786 | && (c_parser_peek_2nd_token (parser)->keyword | |
3787 | == RID_EXTENSION)) | |
3788 | c_parser_consume_token (parser); | |
3789 | if (c_token_starts_declspecs (c_parser_peek_2nd_token (parser))) | |
3790 | { | |
3791 | int ext; | |
3792 | ext = disable_extension_diagnostics (); | |
3793 | c_parser_consume_token (parser); | |
3794 | c_parser_declaration_or_fndef (parser, true, true, true, true); | |
3795 | restore_extension_diagnostics (ext); | |
3796 | check_for_loop_decls (); | |
3797 | } | |
3798 | else | |
3799 | goto init_expr; | |
3800 | } | |
3801 | else | |
3802 | { | |
3803 | init_expr: | |
3804 | c_finish_expr_stmt (c_parser_expression (parser).value); | |
3805 | c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>"); | |
3806 | } | |
3807 | /* Parse the loop condition. */ | |
3808 | loc = c_parser_peek_token (parser)->location; | |
3809 | if (c_parser_next_token_is (parser, CPP_SEMICOLON)) | |
3810 | { | |
3811 | c_parser_consume_token (parser); | |
3812 | cond = NULL_TREE; | |
3813 | } | |
3814 | else | |
3815 | { | |
3816 | tree ocond = c_parser_expression (parser).value; | |
85498824 | 3817 | cond = c_objc_common_truthvalue_conversion (ocond); |
27bf414c JM |
3818 | if (EXPR_P (cond)) |
3819 | SET_EXPR_LOCATION (cond, loc); | |
3820 | c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>"); | |
3821 | } | |
3822 | /* Parse the increment expression. */ | |
3823 | if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN)) | |
3824 | incr = c_process_expr_stmt (NULL_TREE); | |
3825 | else | |
3826 | incr = c_process_expr_stmt (c_parser_expression (parser).value); | |
3827 | c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>"); | |
3828 | } | |
3829 | else | |
3830 | { | |
3831 | cond = error_mark_node; | |
3832 | incr = error_mark_node; | |
3833 | } | |
3834 | save_break = c_break_label; | |
3835 | c_break_label = NULL_TREE; | |
3836 | save_cont = c_cont_label; | |
3837 | c_cont_label = NULL_TREE; | |
3838 | body = c_parser_c99_block_statement (parser); | |
3839 | c_finish_loop (loc, cond, incr, body, c_break_label, c_cont_label, true); | |
3840 | add_stmt (c_end_compound_stmt (block, flag_isoc99)); | |
3841 | c_break_label = save_break; | |
3842 | c_cont_label = save_cont; | |
3843 | } | |
3844 | ||
3845 | /* Parse an asm statement, a GNU extension. This is a full-blown asm | |
3846 | statement with inputs, outputs, clobbers, and volatile tag | |
3847 | allowed. | |
3848 | ||
3849 | asm-statement: | |
3850 | asm type-qualifier[opt] ( asm-argument ) ; | |
3851 | ||
3852 | asm-argument: | |
3853 | asm-string-literal | |
3854 | asm-string-literal : asm-operands[opt] | |
3855 | asm-string-literal : asm-operands[opt] : asm-operands[opt] | |
3856 | asm-string-literal : asm-operands[opt] : asm-operands[opt] : asm-clobbers | |
3857 | ||
3858 | Qualifiers other than volatile are accepted in the syntax but | |
3859 | warned for. */ | |
3860 | ||
3861 | static tree | |
3862 | c_parser_asm_statement (c_parser *parser) | |
3863 | { | |
3864 | tree quals, str, outputs, inputs, clobbers, ret; | |
3865 | bool simple; | |
3866 | gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM)); | |
3867 | c_parser_consume_token (parser); | |
3868 | if (c_parser_next_token_is_keyword (parser, RID_VOLATILE)) | |
3869 | { | |
3870 | quals = c_parser_peek_token (parser)->value; | |
3871 | c_parser_consume_token (parser); | |
3872 | } | |
3873 | else if (c_parser_next_token_is_keyword (parser, RID_CONST) | |
3874 | || c_parser_next_token_is_keyword (parser, RID_RESTRICT)) | |
3875 | { | |
3876 | warning ("%E qualifier ignored on asm", | |
3877 | c_parser_peek_token (parser)->value); | |
3878 | quals = NULL_TREE; | |
3879 | c_parser_consume_token (parser); | |
3880 | } | |
3881 | else | |
3882 | quals = NULL_TREE; | |
3883 | /* ??? Follow the C++ parser rather than using the | |
3884 | c_lex_string_translate kludge. */ | |
3885 | c_lex_string_translate = 0; | |
3886 | if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>")) | |
3887 | { | |
3888 | c_lex_string_translate = 1; | |
3889 | return NULL_TREE; | |
3890 | } | |
3891 | str = c_parser_asm_string_literal (parser); | |
3892 | if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN)) | |
3893 | { | |
3894 | simple = true; | |
3895 | outputs = NULL_TREE; | |
3896 | inputs = NULL_TREE; | |
3897 | clobbers = NULL_TREE; | |
3898 | goto done_asm; | |
3899 | } | |
3900 | if (!c_parser_require (parser, CPP_COLON, "expected %<:%> or %<)%>")) | |
3901 | { | |
3902 | c_lex_string_translate = 1; | |
3903 | c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL); | |
3904 | return NULL_TREE; | |
3905 | } | |
3906 | simple = false; | |
3907 | /* Parse outputs. */ | |
3908 | if (c_parser_next_token_is (parser, CPP_COLON) | |
3909 | || c_parser_next_token_is (parser, CPP_CLOSE_PAREN)) | |
3910 | outputs = NULL_TREE; | |
3911 | else | |
3912 | outputs = c_parser_asm_operands (parser); | |
3913 | if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN)) | |
3914 | { | |
3915 | inputs = NULL_TREE; | |
3916 | clobbers = NULL_TREE; | |
3917 | goto done_asm; | |
3918 | } | |
3919 | if (!c_parser_require (parser, CPP_COLON, "expected %<:%> or %<)%>")) | |
3920 | { | |
3921 | c_lex_string_translate = 1; | |
3922 | c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL); | |
3923 | return NULL_TREE; | |
3924 | } | |
3925 | /* Parse inputs. */ | |
3926 | if (c_parser_next_token_is (parser, CPP_COLON) | |
3927 | || c_parser_next_token_is (parser, CPP_CLOSE_PAREN)) | |
3928 | inputs = NULL_TREE; | |
3929 | else | |
3930 | inputs = c_parser_asm_operands (parser); | |
3931 | if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN)) | |
3932 | { | |
3933 | clobbers = NULL_TREE; | |
3934 | goto done_asm; | |
3935 | } | |
3936 | if (!c_parser_require (parser, CPP_COLON, "expected %<:%> or %<)%>")) | |
3937 | { | |
3938 | c_lex_string_translate = 1; | |
3939 | c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL); | |
3940 | return NULL_TREE; | |
3941 | } | |
3942 | /* Parse clobbers. */ | |
3943 | clobbers = c_parser_asm_clobbers (parser); | |
3944 | done_asm: | |
3945 | c_lex_string_translate = 1; | |
3946 | if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>")) | |
3947 | { | |
3948 | c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL); | |
3949 | return NULL_TREE; | |
3950 | } | |
3951 | if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>")) | |
3952 | c_parser_skip_to_end_of_block_or_statement (parser); | |
3953 | ret = build_asm_stmt (quals, build_asm_expr (str, outputs, inputs, | |
3954 | clobbers, simple)); | |
3955 | return ret; | |
3956 | } | |
3957 | ||
3958 | /* Parse asm operands, a GNU extension. | |
3959 | ||
3960 | asm-operands: | |
3961 | asm-operand | |
3962 | asm-operands , asm-operand | |
3963 | ||
3964 | asm-operand: | |
3965 | asm-string-literal ( expression ) | |
3966 | [ identifier ] asm-string-literal ( expression ) | |
3967 | */ | |
3968 | ||
3969 | static tree | |
3970 | c_parser_asm_operands (c_parser *parser) | |
3971 | { | |
3972 | tree list = NULL_TREE; | |
3973 | while (true) | |
3974 | { | |
3975 | tree name, str, expr; | |
3976 | if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE)) | |
3977 | { | |
3978 | c_parser_consume_token (parser); | |
3979 | if (c_parser_next_token_is (parser, CPP_NAME)) | |
3980 | { | |
3981 | tree id = c_parser_peek_token (parser)->value; | |
3982 | c_parser_consume_token (parser); | |
3983 | name = build_string (IDENTIFIER_LENGTH (id), | |
3984 | IDENTIFIER_POINTER (id)); | |
3985 | } | |
3986 | else | |
3987 | { | |
3988 | c_parser_error (parser, "expected identifier"); | |
3989 | c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL); | |
3990 | return NULL_TREE; | |
3991 | } | |
3992 | c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, | |
3993 | "expected %<]%>"); | |
3994 | } | |
3995 | else | |
3996 | name = NULL_TREE; | |
3997 | str = c_parser_asm_string_literal (parser); | |
3998 | if (str == NULL_TREE) | |
3999 | return NULL_TREE; | |
4000 | c_lex_string_translate = 1; | |
4001 | if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>")) | |
4002 | { | |
4003 | c_lex_string_translate = 0; | |
4004 | return NULL_TREE; | |
4005 | } | |
4006 | expr = c_parser_expression (parser).value; | |
4007 | c_lex_string_translate = 0; | |
4008 | if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>")) | |
4009 | { | |
4010 | c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL); | |
4011 | return NULL_TREE; | |
4012 | } | |
4013 | list = chainon (list, build_tree_list (build_tree_list (name, str), | |
4014 | expr)); | |
4015 | if (c_parser_next_token_is (parser, CPP_COMMA)) | |
4016 | c_parser_consume_token (parser); | |
4017 | else | |
4018 | break; | |
4019 | } | |
4020 | return list; | |
4021 | } | |
4022 | ||
4023 | /* Parse asm clobbers, a GNU extension. | |
4024 | ||
4025 | asm-clobbers: | |
4026 | asm-string-literal | |
4027 | asm-clobbers , asm-string-literal | |
4028 | */ | |
4029 | ||
4030 | static tree | |
4031 | c_parser_asm_clobbers (c_parser *parser) | |
4032 | { | |
4033 | tree list = NULL_TREE; | |
4034 | while (true) | |
4035 | { | |
4036 | tree str = c_parser_asm_string_literal (parser); | |
4037 | if (str) | |
4038 | list = tree_cons (NULL_TREE, str, list); | |
4039 | else | |
4040 | return NULL_TREE; | |
4041 | if (c_parser_next_token_is (parser, CPP_COMMA)) | |
4042 | c_parser_consume_token (parser); | |
4043 | else | |
4044 | break; | |
4045 | } | |
4046 | return list; | |
4047 | } | |
4048 | ||
4049 | /* Parse an expression other than a compound expression; that is, an | |
4050 | assignment expression (C90 6.3.16, C99 6.5.16). If AFTER is not | |
4051 | NULL then it is an Objective-C message expression which is the | |
4052 | primary-expression starting the expression as an initializer. | |
4053 | ||
4054 | assignment-expression: | |
4055 | conditional-expression | |
4056 | unary-expression assignment-operator assignment-expression | |
4057 | ||
4058 | assignment-operator: one of | |
4059 | = *= /= %= += -= <<= >>= &= ^= |= | |
4060 | ||
4061 | In GNU C we accept any conditional expression on the LHS and | |
4062 | diagnose the invalid lvalue rather than producing a syntax | |
4063 | error. */ | |
4064 | ||
4065 | static struct c_expr | |
4066 | c_parser_expr_no_commas (c_parser *parser, struct c_expr *after) | |
4067 | { | |
4068 | struct c_expr lhs, rhs, ret; | |
4069 | enum tree_code code; | |
4070 | gcc_assert (!after || c_dialect_objc ()); | |
4071 | lhs = c_parser_conditional_expression (parser, after); | |
4072 | switch (c_parser_peek_token (parser)->type) | |
4073 | { | |
4074 | case CPP_EQ: | |
4075 | code = NOP_EXPR; | |
4076 | break; | |
4077 | case CPP_MULT_EQ: | |
4078 | code = MULT_EXPR; | |
4079 | break; | |
4080 | case CPP_DIV_EQ: | |
4081 | code = TRUNC_DIV_EXPR; | |
4082 | break; | |
4083 | case CPP_MOD_EQ: | |
4084 | code = TRUNC_MOD_EXPR; | |
4085 | break; | |
4086 | case CPP_PLUS_EQ: | |
4087 | code = PLUS_EXPR; | |
4088 | break; | |
4089 | case CPP_MINUS_EQ: | |
4090 | code = MINUS_EXPR; | |
4091 | break; | |
4092 | case CPP_LSHIFT_EQ: | |
4093 | code = LSHIFT_EXPR; | |
4094 | break; | |
4095 | case CPP_RSHIFT_EQ: | |
4096 | code = RSHIFT_EXPR; | |
4097 | break; | |
4098 | case CPP_AND_EQ: | |
4099 | code = BIT_AND_EXPR; | |
4100 | break; | |
4101 | case CPP_XOR_EQ: | |
4102 | code = BIT_XOR_EXPR; | |
4103 | break; | |
4104 | case CPP_OR_EQ: | |
4105 | code = BIT_IOR_EXPR; | |
4106 | break; | |
4107 | default: | |
4108 | return lhs; | |
4109 | } | |
4110 | c_parser_consume_token (parser); | |
4111 | rhs = c_parser_expr_no_commas (parser, NULL); | |
4112 | ret.value = build_modify_expr (lhs.value, code, rhs.value); | |
4113 | if (code == NOP_EXPR) | |
4114 | ret.original_code = MODIFY_EXPR; | |
4115 | else | |
4116 | { | |
4117 | TREE_NO_WARNING (ret.value) = 1; | |
4118 | ret.original_code = ERROR_MARK; | |
4119 | } | |
4120 | return ret; | |
4121 | } | |
4122 | ||
4123 | /* Parse a conditional expression (C90 6.3.15, C99 6.5.15). If AFTER | |
4124 | is not NULL then it is an Objective-C message expression which is | |
4125 | the primary-expression starting the expression as an initializer. | |
4126 | ||
4127 | conditional-expression: | |
4128 | logical-OR-expression | |
4129 | logical-OR-expression ? expression : conditional-expression | |
4130 | ||
4131 | GNU extensions: | |
4132 | ||
4133 | conditional-expression: | |
4134 | logical-OR-expression ? : conditional-expression | |
4135 | */ | |
4136 | ||
4137 | static struct c_expr | |
4138 | c_parser_conditional_expression (c_parser *parser, struct c_expr *after) | |
4139 | { | |
4140 | struct c_expr cond, exp1, exp2, ret; | |
4141 | gcc_assert (!after || c_dialect_objc ()); | |
4142 | cond = c_parser_binary_expression (parser, after); | |
4143 | if (c_parser_next_token_is_not (parser, CPP_QUERY)) | |
4144 | return cond; | |
4145 | c_parser_consume_token (parser); | |
4146 | if (c_parser_next_token_is (parser, CPP_COLON)) | |
4147 | { | |
4148 | if (pedantic) | |
4149 | pedwarn ("ISO C forbids omitting the middle term of a ?: expression"); | |
4150 | /* Make sure first operand is calculated only once. */ | |
4151 | exp1.value = save_expr (default_conversion (cond.value)); | |
85498824 | 4152 | cond.value = c_objc_common_truthvalue_conversion (exp1.value); |
27bf414c JM |
4153 | skip_evaluation += cond.value == truthvalue_true_node; |
4154 | } | |
4155 | else | |
4156 | { | |
4157 | cond.value | |
85498824 JM |
4158 | = c_objc_common_truthvalue_conversion |
4159 | (default_conversion (cond.value)); | |
27bf414c JM |
4160 | skip_evaluation += cond.value == truthvalue_false_node; |
4161 | exp1 = c_parser_expression (parser); | |
4162 | skip_evaluation += ((cond.value == truthvalue_true_node) | |
4163 | - (cond.value == truthvalue_false_node)); | |
4164 | } | |
4165 | if (!c_parser_require (parser, CPP_COLON, "expected %<:%>")) | |
4166 | { | |
4167 | skip_evaluation -= cond.value == truthvalue_true_node; | |
4168 | ret.value = error_mark_node; | |
4169 | ret.original_code = ERROR_MARK; | |
4170 | return ret; | |
4171 | } | |
4172 | exp2 = c_parser_conditional_expression (parser, NULL); | |
4173 | skip_evaluation -= cond.value == truthvalue_true_node; | |
4174 | ret.value = build_conditional_expr (cond.value, exp1.value, exp2.value); | |
4175 | ret.original_code = ERROR_MARK; | |
4176 | return ret; | |
4177 | } | |
4178 | ||
4179 | /* Parse a binary expression; that is, a logical-OR-expression (C90 | |
4180 | 6.3.5-6.3.14, C99 6.5.5-6.5.14). If AFTER is not NULL then it is | |
4181 | an Objective-C message expression which is the primary-expression | |
4182 | starting the expression as an initializer. | |
4183 | ||
4184 | multiplicative-expression: | |
4185 | cast-expression | |
4186 | multiplicative-expression * cast-expression | |
4187 | multiplicative-expression / cast-expression | |
4188 | multiplicative-expression % cast-expression | |
4189 | ||
4190 | additive-expression: | |
4191 | multiplicative-expression | |
4192 | additive-expression + multiplicative-expression | |
4193 | additive-expression - multiplicative-expression | |
4194 | ||
4195 | shift-expression: | |
4196 | additive-expression | |
4197 | shift-expression << additive-expression | |
4198 | shift-expression >> additive-expression | |
4199 | ||
4200 | relational-expression: | |
4201 | shift-expression | |
4202 | relational-expression < shift-expression | |
4203 | relational-expression > shift-expression | |
4204 | relational-expression <= shift-expression | |
4205 | relational-expression >= shift-expression | |
4206 | ||
4207 | equality-expression: | |
4208 | relational-expression | |
4209 | equality-expression == relational-expression | |
4210 | equality-expression != relational-expression | |
4211 | ||
4212 | AND-expression: | |
4213 | equality-expression | |
4214 | AND-expression & equality-expression | |
4215 | ||
4216 | exclusive-OR-expression: | |
4217 | AND-expression | |
4218 | exclusive-OR-expression ^ AND-expression | |
4219 | ||
4220 | inclusive-OR-expression: | |
4221 | exclusive-OR-expression | |
4222 | inclusive-OR-expression | exclusive-OR-expression | |
4223 | ||
4224 | logical-AND-expression: | |
4225 | inclusive-OR-expression | |
4226 | logical-AND-expression && inclusive-OR-expression | |
4227 | ||
4228 | logical-OR-expression: | |
4229 | logical-AND-expression | |
4230 | logical-OR-expression || logical-AND-expression | |
4231 | */ | |
4232 | ||
4233 | static struct c_expr | |
4234 | c_parser_binary_expression (c_parser *parser, struct c_expr *after) | |
4235 | { | |
4236 | /* A binary expression is parsed using operator-precedence parsing, | |
4237 | with the operands being cast expressions. All the binary | |
4238 | operators are left-associative. Thus a binary expression is of | |
4239 | form: | |
4240 | ||
4241 | E0 op1 E1 op2 E2 ... | |
4242 | ||
4243 | which we represent on a stack. On the stack, the precedence | |
4244 | levels are strictly increasing. When a new operator is | |
4245 | encountered of higher precedence than that at the top of the | |
4246 | stack, it is pushed; its LHS is the top expression, and its RHS | |
4247 | is everything parsed until it is popped. When a new operator is | |
4248 | encountered with precedence less than or equal to that at the top | |
4249 | of the stack, triples E[i-1] op[i] E[i] are popped and replaced | |
4250 | by the result of the operation until the operator at the top of | |
4251 | the stack has lower precedence than the new operator or there is | |
4252 | only one element on the stack; then the top expression is the LHS | |
4253 | of the new operator. In the case of logical AND and OR | |
4254 | expressions, we also need to adjust skip_evaluation as | |
4255 | appropriate when the operators are pushed and popped. */ | |
4256 | ||
4257 | /* The precedence levels, where 0 is a dummy lowest level used for | |
4258 | the bottom of the stack. */ | |
4259 | enum prec { | |
4260 | PREC_NONE, | |
4261 | PREC_LOGOR, | |
4262 | PREC_LOGAND, | |
4263 | PREC_BITOR, | |
4264 | PREC_BITXOR, | |
4265 | PREC_BITAND, | |
4266 | PREC_EQ, | |
4267 | PREC_REL, | |
4268 | PREC_SHIFT, | |
4269 | PREC_ADD, | |
4270 | PREC_MULT, | |
4271 | NUM_PRECS | |
4272 | }; | |
4273 | struct { | |
4274 | /* The expression at this stack level. */ | |
4275 | struct c_expr expr; | |
4276 | /* The precedence of the operator on its left, PREC_NONE at the | |
4277 | bottom of the stack. */ | |
4278 | enum prec prec; | |
4279 | /* The operation on its left. */ | |
4280 | enum tree_code op; | |
4281 | } stack[NUM_PRECS]; | |
4282 | int sp; | |
4283 | #define POP \ | |
4284 | do { \ | |
4285 | switch (stack[sp].op) \ | |
4286 | { \ | |
4287 | case TRUTH_ANDIF_EXPR: \ | |
4288 | skip_evaluation -= stack[sp - 1].expr.value == truthvalue_false_node; \ | |
4289 | break; \ | |
4290 | case TRUTH_ORIF_EXPR: \ | |
4291 | skip_evaluation -= stack[sp - 1].expr.value == truthvalue_true_node; \ | |
4292 | break; \ | |
4293 | default: \ | |
4294 | break; \ | |
4295 | } \ | |
4296 | stack[sp - 1].expr = parser_build_binary_op (stack[sp].op, \ | |
4297 | stack[sp - 1].expr, \ | |
4298 | stack[sp].expr); \ | |
4299 | sp--; \ | |
4300 | } while (0) | |
4301 | gcc_assert (!after || c_dialect_objc ()); | |
4302 | stack[0].expr = c_parser_cast_expression (parser, after); | |
4303 | stack[0].prec = PREC_NONE; | |
4304 | sp = 0; | |
4305 | while (true) | |
4306 | { | |
4307 | enum prec oprec; | |
4308 | enum tree_code ocode; | |
4309 | if (parser->error) | |
4310 | goto out; | |
4311 | switch (c_parser_peek_token (parser)->type) | |
4312 | { | |
4313 | case CPP_MULT: | |
4314 | oprec = PREC_MULT; | |
4315 | ocode = MULT_EXPR; | |
4316 | break; | |
4317 | case CPP_DIV: | |
4318 | oprec = PREC_MULT; | |
4319 | ocode = TRUNC_DIV_EXPR; | |
4320 | break; | |
4321 | case CPP_MOD: | |
4322 | oprec = PREC_MULT; | |
4323 | ocode = TRUNC_MOD_EXPR; | |
4324 | break; | |
4325 | case CPP_PLUS: | |
4326 | oprec = PREC_ADD; | |
4327 | ocode = PLUS_EXPR; | |
4328 | break; | |
4329 | case CPP_MINUS: | |
4330 | oprec = PREC_ADD; | |
4331 | ocode = MINUS_EXPR; | |
4332 | break; | |
4333 | case CPP_LSHIFT: | |
4334 | oprec = PREC_SHIFT; | |
4335 | ocode = LSHIFT_EXPR; | |
4336 | break; | |
4337 | case CPP_RSHIFT: | |
4338 | oprec = PREC_SHIFT; | |
4339 | ocode = RSHIFT_EXPR; | |
4340 | break; | |
4341 | case CPP_LESS: | |
4342 | oprec = PREC_REL; | |
4343 | ocode = LT_EXPR; | |
4344 | break; | |
4345 | case CPP_GREATER: | |
4346 | oprec = PREC_REL; | |
4347 | ocode = GT_EXPR; | |
4348 | break; | |
4349 | case CPP_LESS_EQ: | |
4350 | oprec = PREC_REL; | |
4351 | ocode = LE_EXPR; | |
4352 | break; | |
4353 | case CPP_GREATER_EQ: | |
4354 | oprec = PREC_REL; | |
4355 | ocode = GE_EXPR; | |
4356 | break; | |
4357 | case CPP_EQ_EQ: | |
4358 | oprec = PREC_EQ; | |
4359 | ocode = EQ_EXPR; | |
4360 | break; | |
4361 | case CPP_NOT_EQ: | |
4362 | oprec = PREC_EQ; | |
4363 | ocode = NE_EXPR; | |
4364 | break; | |
4365 | case CPP_AND: | |
4366 | oprec = PREC_BITAND; | |
4367 | ocode = BIT_AND_EXPR; | |
4368 | break; | |
4369 | case CPP_XOR: | |
4370 | oprec = PREC_BITXOR; | |
4371 | ocode = BIT_XOR_EXPR; | |
4372 | break; | |
4373 | case CPP_OR: | |
4374 | oprec = PREC_BITOR; | |
4375 | ocode = BIT_IOR_EXPR; | |
4376 | break; | |
4377 | case CPP_AND_AND: | |
4378 | oprec = PREC_LOGAND; | |
4379 | ocode = TRUTH_ANDIF_EXPR; | |
4380 | break; | |
4381 | case CPP_OR_OR: | |
4382 | oprec = PREC_LOGOR; | |
4383 | ocode = TRUTH_ORIF_EXPR; | |
4384 | break; | |
4385 | default: | |
4386 | /* Not a binary operator, so end of the binary | |
4387 | expression. */ | |
4388 | goto out; | |
4389 | } | |
4390 | c_parser_consume_token (parser); | |
4391 | while (oprec <= stack[sp].prec) | |
4392 | POP; | |
4393 | switch (ocode) | |
4394 | { | |
4395 | case TRUTH_ANDIF_EXPR: | |
85498824 | 4396 | stack[sp].expr.value = c_objc_common_truthvalue_conversion |
27bf414c JM |
4397 | (default_conversion (stack[sp].expr.value)); |
4398 | skip_evaluation += stack[sp].expr.value == truthvalue_false_node; | |
4399 | break; | |
4400 | case TRUTH_ORIF_EXPR: | |
85498824 | 4401 | stack[sp].expr.value = c_objc_common_truthvalue_conversion |
27bf414c JM |
4402 | (default_conversion (stack[sp].expr.value)); |
4403 | skip_evaluation += stack[sp].expr.value == truthvalue_true_node; | |
4404 | break; | |
4405 | default: | |
4406 | break; | |
4407 | } | |
4408 | sp++; | |
4409 | stack[sp].expr = c_parser_cast_expression (parser, NULL); | |
4410 | stack[sp].prec = oprec; | |
4411 | stack[sp].op = ocode; | |
4412 | } | |
4413 | out: | |
4414 | while (sp > 0) | |
4415 | POP; | |
4416 | return stack[0].expr; | |
4417 | #undef POP | |
4418 | } | |
4419 | ||
4420 | /* Parse a cast expression (C90 6.3.4, C99 6.5.4). If AFTER is not | |
4421 | NULL then it is an Objective-C message expression which is the | |
4422 | primary-expression starting the expression as an initializer. | |
4423 | ||
4424 | cast-expression: | |
4425 | unary-expression | |
4426 | ( type-name ) unary-expression | |
4427 | */ | |
4428 | ||
4429 | static struct c_expr | |
4430 | c_parser_cast_expression (c_parser *parser, struct c_expr *after) | |
4431 | { | |
4432 | gcc_assert (!after || c_dialect_objc ()); | |
4433 | if (after) | |
4434 | return c_parser_postfix_expression_after_primary (parser, *after); | |
4435 | /* If the expression begins with a parenthesized type name, it may | |
4436 | be either a cast or a compound literal; we need to see whether | |
4437 | the next character is '{' to tell the difference. If not, it is | |
4438 | an unary expression. */ | |
4439 | if (c_parser_next_token_is (parser, CPP_OPEN_PAREN) | |
4440 | && c_token_starts_typename (c_parser_peek_2nd_token (parser))) | |
4441 | { | |
4442 | struct c_type_name *type_name; | |
4443 | struct c_expr ret; | |
4444 | tree expr; | |
4445 | c_parser_consume_token (parser); | |
4446 | type_name = c_parser_type_name (parser); | |
4447 | c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>"); | |
4448 | if (type_name == NULL) | |
4449 | { | |
4450 | ret.value = error_mark_node; | |
4451 | ret.original_code = ERROR_MARK; | |
4452 | return ret; | |
4453 | } | |
4454 | if (c_parser_next_token_is (parser, CPP_OPEN_BRACE)) | |
4455 | return c_parser_postfix_expression_after_paren_type (parser, | |
4456 | type_name); | |
4457 | expr = c_parser_cast_expression (parser, NULL).value; | |
4458 | ret.value = c_cast_expr (type_name, expr); | |
4459 | ret.original_code = ERROR_MARK; | |
4460 | return ret; | |
4461 | } | |
4462 | else | |
4463 | return c_parser_unary_expression (parser); | |
4464 | } | |
4465 | ||
4466 | /* Parse an unary expression (C90 6.3.3, C99 6.5.3). | |
4467 | ||
4468 | unary-expression: | |
4469 | postfix-expression | |
4470 | ++ unary-expression | |
4471 | -- unary-expression | |
4472 | unary-operator cast-expression | |
4473 | sizeof unary-expression | |
4474 | sizeof ( type-name ) | |
4475 | ||
4476 | unary-operator: one of | |
4477 | & * + - ~ ! | |
4478 | ||
4479 | GNU extensions: | |
4480 | ||
4481 | unary-expression: | |
4482 | __alignof__ unary-expression | |
4483 | __alignof__ ( type-name ) | |
4484 | && identifier | |
4485 | ||
4486 | unary-operator: one of | |
4487 | __extension__ __real__ __imag__ | |
4488 | ||
4489 | In addition, the GNU syntax treats ++ and -- as unary operators, so | |
4490 | they may be applied to cast expressions with errors for non-lvalues | |
4491 | given later. */ | |
4492 | ||
4493 | static struct c_expr | |
4494 | c_parser_unary_expression (c_parser *parser) | |
4495 | { | |
4496 | int ext; | |
4497 | struct c_expr ret; | |
4498 | ret.original_code = ERROR_MARK; | |
4499 | switch (c_parser_peek_token (parser)->type) | |
4500 | { | |
4501 | case CPP_PLUS_PLUS: | |
4502 | c_parser_consume_token (parser); | |
4503 | ret.value | |
4504 | = build_unary_op (PREINCREMENT_EXPR, | |
4505 | c_parser_cast_expression (parser, NULL).value, 0); | |
4506 | overflow_warning (ret.value); | |
4507 | return ret; | |
4508 | case CPP_MINUS_MINUS: | |
4509 | c_parser_consume_token (parser); | |
4510 | ret.value | |
4511 | = build_unary_op (PREDECREMENT_EXPR, | |
4512 | c_parser_cast_expression (parser, NULL).value, 0); | |
4513 | overflow_warning (ret.value); | |
4514 | return ret; | |
4515 | case CPP_AND: | |
4516 | c_parser_consume_token (parser); | |
4517 | ret.value | |
4518 | = build_unary_op (ADDR_EXPR, | |
4519 | c_parser_cast_expression (parser, NULL).value, 0); | |
4520 | overflow_warning (ret.value); | |
4521 | return ret; | |
4522 | case CPP_MULT: | |
4523 | c_parser_consume_token (parser); | |
4524 | ret.value | |
4525 | = build_indirect_ref (c_parser_cast_expression (parser, NULL).value, | |
4526 | "unary *"); | |
4527 | return ret; | |
4528 | case CPP_PLUS: | |
4529 | c_parser_consume_token (parser); | |
4530 | if (!c_dialect_objc () && warn_traditional && !in_system_header) | |
4531 | warning ("traditional C rejects the unary plus operator"); | |
4532 | ret.value | |
4533 | = build_unary_op (CONVERT_EXPR, | |
4534 | c_parser_cast_expression (parser, NULL).value, 0); | |
4535 | overflow_warning (ret.value); | |
4536 | return ret; | |
4537 | case CPP_MINUS: | |
4538 | c_parser_consume_token (parser); | |
4539 | ret.value | |
4540 | = build_unary_op (NEGATE_EXPR, | |
4541 | c_parser_cast_expression (parser, NULL).value, 0); | |
4542 | overflow_warning (ret.value); | |
4543 | return ret; | |
4544 | case CPP_COMPL: | |
4545 | c_parser_consume_token (parser); | |
4546 | ret.value | |
4547 | = build_unary_op (BIT_NOT_EXPR, | |
4548 | c_parser_cast_expression (parser, NULL).value, 0); | |
4549 | overflow_warning (ret.value); | |
4550 | return ret; | |
4551 | case CPP_NOT: | |
4552 | c_parser_consume_token (parser); | |
4553 | ret.value | |
4554 | = build_unary_op (TRUTH_NOT_EXPR, | |
4555 | c_parser_cast_expression (parser, NULL).value, 0); | |
4556 | overflow_warning (ret.value); | |
4557 | return ret; | |
4558 | case CPP_AND_AND: | |
4559 | /* Refer to the address of a label as a pointer. */ | |
4560 | c_parser_consume_token (parser); | |
4561 | if (c_parser_next_token_is (parser, CPP_NAME)) | |
4562 | { | |
4563 | ret.value = finish_label_address_expr | |
4564 | (c_parser_peek_token (parser)->value); | |
4565 | c_parser_consume_token (parser); | |
4566 | return ret; | |
4567 | } | |
4568 | else | |
4569 | { | |
4570 | c_parser_error (parser, "expected identifier"); | |
4571 | ret.value = error_mark_node; | |
4572 | return ret; | |
4573 | } | |
4574 | case CPP_KEYWORD: | |
4575 | switch (c_parser_peek_token (parser)->keyword) | |
4576 | { | |
4577 | case RID_SIZEOF: | |
4578 | return c_parser_sizeof_expression (parser); | |
4579 | case RID_ALIGNOF: | |
4580 | return c_parser_alignof_expression (parser); | |
4581 | case RID_EXTENSION: | |
4582 | c_parser_consume_token (parser); | |
4583 | ext = disable_extension_diagnostics (); | |
4584 | ret = c_parser_cast_expression (parser, NULL); | |
4585 | restore_extension_diagnostics (ext); | |
4586 | return ret; | |
4587 | case RID_REALPART: | |
4588 | c_parser_consume_token (parser); | |
4589 | ret.value | |
4590 | = build_unary_op (REALPART_EXPR, | |
4591 | c_parser_cast_expression (parser, NULL).value, | |
4592 | 0); | |
4593 | return ret; | |
4594 | case RID_IMAGPART: | |
4595 | c_parser_consume_token (parser); | |
4596 | ret.value | |
4597 | = build_unary_op (IMAGPART_EXPR, | |
4598 | c_parser_cast_expression (parser, NULL).value, | |
4599 | 0); | |
4600 | return ret; | |
4601 | default: | |
4602 | return c_parser_postfix_expression (parser); | |
4603 | } | |
4604 | default: | |
4605 | return c_parser_postfix_expression (parser); | |
4606 | } | |
4607 | } | |
4608 | ||
4609 | /* Parse a sizeof expression. */ | |
4610 | ||
4611 | static struct c_expr | |
4612 | c_parser_sizeof_expression (c_parser *parser) | |
4613 | { | |
4614 | struct c_expr expr; | |
4615 | gcc_assert (c_parser_next_token_is_keyword (parser, RID_SIZEOF)); | |
4616 | c_parser_consume_token (parser); | |
4617 | skip_evaluation++; | |
4618 | in_sizeof++; | |
4619 | if (c_parser_next_token_is (parser, CPP_OPEN_PAREN) | |
4620 | && c_token_starts_typename (c_parser_peek_2nd_token (parser))) | |
4621 | { | |
4622 | /* Either sizeof ( type-name ) or sizeof unary-expression | |
4623 | starting with a compound literal. */ | |
4624 | struct c_type_name *type_name; | |
4625 | c_parser_consume_token (parser); | |
4626 | type_name = c_parser_type_name (parser); | |
4627 | c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>"); | |
4628 | if (type_name == NULL) | |
4629 | { | |
4630 | struct c_expr ret; | |
4631 | skip_evaluation--; | |
4632 | in_sizeof--; | |
4633 | ret.value = error_mark_node; | |
4634 | ret.original_code = ERROR_MARK; | |
4635 | return ret; | |
4636 | } | |
4637 | if (c_parser_next_token_is (parser, CPP_OPEN_BRACE)) | |
4638 | { | |
4639 | expr = c_parser_postfix_expression_after_paren_type (parser, | |
4640 | type_name); | |
4641 | goto sizeof_expr; | |
4642 | } | |
4643 | /* sizeof ( type-name ). */ | |
4644 | skip_evaluation--; | |
4645 | in_sizeof--; | |
4646 | return c_expr_sizeof_type (type_name); | |
4647 | } | |
4648 | else | |
4649 | { | |
4650 | expr = c_parser_unary_expression (parser); | |
4651 | sizeof_expr: | |
4652 | skip_evaluation--; | |
4653 | in_sizeof--; | |
4654 | if (TREE_CODE (expr.value) == COMPONENT_REF | |
4655 | && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1))) | |
4656 | error ("%<sizeof%> applied to a bit-field"); | |
4657 | return c_expr_sizeof_expr (expr); | |
4658 | } | |
4659 | } | |
4660 | ||
4661 | /* Parse an alignof expression. */ | |
4662 | ||
4663 | static struct c_expr | |
4664 | c_parser_alignof_expression (c_parser *parser) | |
4665 | { | |
4666 | struct c_expr expr; | |
4667 | gcc_assert (c_parser_next_token_is_keyword (parser, RID_ALIGNOF)); | |
4668 | c_parser_consume_token (parser); | |
4669 | skip_evaluation++; | |
4670 | in_alignof++; | |
4671 | if (c_parser_next_token_is (parser, CPP_OPEN_PAREN) | |
4672 | && c_token_starts_typename (c_parser_peek_2nd_token (parser))) | |
4673 | { | |
4674 | /* Either __alignof__ ( type-name ) or __alignof__ | |
4675 | unary-expression starting with a compound literal. */ | |
4676 | struct c_type_name *type_name; | |
4677 | struct c_expr ret; | |
4678 | c_parser_consume_token (parser); | |
4679 | type_name = c_parser_type_name (parser); | |
4680 | c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>"); | |
4681 | if (type_name == NULL) | |
4682 | { | |
4683 | struct c_expr ret; | |
4684 | skip_evaluation--; | |
4685 | in_alignof--; | |
4686 | ret.value = error_mark_node; | |
4687 | ret.original_code = ERROR_MARK; | |
4688 | return ret; | |
4689 | } | |
4690 | if (c_parser_next_token_is (parser, CPP_OPEN_BRACE)) | |
4691 | { | |
4692 | expr = c_parser_postfix_expression_after_paren_type (parser, | |
4693 | type_name); | |
4694 | goto alignof_expr; | |
4695 | } | |
4696 | /* alignof ( type-name ). */ | |
4697 | skip_evaluation--; | |
4698 | in_alignof--; | |
4699 | ret.value = c_alignof (groktypename (type_name)); | |
4700 | ret.original_code = ERROR_MARK; | |
4701 | return ret; | |
4702 | } | |
4703 | else | |
4704 | { | |
4705 | struct c_expr ret; | |
4706 | expr = c_parser_unary_expression (parser); | |
4707 | alignof_expr: | |
4708 | skip_evaluation--; | |
4709 | in_alignof--; | |
4710 | ret.value = c_alignof_expr (expr.value); | |
4711 | ret.original_code = ERROR_MARK; | |
4712 | return ret; | |
4713 | } | |
4714 | } | |
4715 | ||
4716 | /* Parse a postfix expression (C90 6.3.1-6.3.2, C99 6.5.1-6.5.2). | |
4717 | ||
4718 | postfix-expression: | |
4719 | primary-expression | |
4720 | postfix-expression [ expression ] | |
4721 | postfix-expression ( argument-expression-list[opt] ) | |
4722 | postfix-expression . identifier | |
4723 | postfix-expression -> identifier | |
4724 | postfix-expression ++ | |
4725 | postfix-expression -- | |
4726 | ( type-name ) { initializer-list } | |
4727 | ( type-name ) { initializer-list , } | |
4728 | ||
4729 | argument-expression-list: | |
4730 | argument-expression | |
4731 | argument-expression-list , argument-expression | |
4732 | ||
4733 | primary-expression: | |
4734 | identifier | |
4735 | constant | |
4736 | string-literal | |
4737 | ( expression ) | |
4738 | ||
4739 | GNU extensions: | |
4740 | ||
4741 | primary-expression: | |
4742 | __func__ | |
4743 | (treated as a keyword in GNU C) | |
4744 | __FUNCTION__ | |
4745 | __PRETTY_FUNCTION__ | |
4746 | ( compound-statement ) | |
4747 | __builtin_va_arg ( assignment-expression , type-name ) | |
4748 | __builtin_offsetof ( type-name , offsetof-member-designator ) | |
4749 | __builtin_choose_expr ( assignment-expression , | |
4750 | assignment-expression , | |
4751 | assignment-expression ) | |
4752 | __builtin_types_compatible_p ( type-name , type-name ) | |
4753 | ||
4754 | offsetof-member-designator: | |
4755 | identifier | |
4756 | offsetof-member-designator . identifier | |
4757 | offsetof-member-designator [ expression ] | |
4758 | ||
4759 | Objective-C: | |
4760 | ||
4761 | primary-expression: | |
4762 | [ objc-receiver objc-message-args ] | |
4763 | @selector ( objc-selector-arg ) | |
4764 | @protocol ( identifier ) | |
4765 | @encode ( type-name ) | |
4766 | objc-string-literal | |
4767 | */ | |
4768 | ||
4769 | static struct c_expr | |
4770 | c_parser_postfix_expression (c_parser *parser) | |
4771 | { | |
4772 | struct c_expr expr, e1, e2, e3; | |
4773 | struct c_type_name *t1, *t2; | |
4774 | switch (c_parser_peek_token (parser)->type) | |
4775 | { | |
4776 | case CPP_NUMBER: | |
4777 | case CPP_CHAR: | |
4778 | case CPP_WCHAR: | |
4779 | expr.value = c_parser_peek_token (parser)->value; | |
4780 | expr.original_code = ERROR_MARK; | |
4781 | c_parser_consume_token (parser); | |
4782 | break; | |
4783 | case CPP_STRING: | |
4784 | case CPP_WSTRING: | |
4785 | expr.value = c_parser_peek_token (parser)->value; | |
4786 | expr.original_code = STRING_CST; | |
4787 | c_parser_consume_token (parser); | |
4788 | break; | |
4789 | case CPP_OBJC_STRING: | |
4790 | gcc_assert (c_dialect_objc ()); | |
4791 | expr.value | |
4792 | = objc_build_string_object (c_parser_peek_token (parser)->value); | |
4793 | expr.original_code = ERROR_MARK; | |
4794 | c_parser_consume_token (parser); | |
4795 | break; | |
4796 | case CPP_NAME: | |
4797 | if (c_parser_peek_token (parser)->id_kind != C_ID_ID) | |
4798 | { | |
4799 | c_parser_error (parser, "expected expression"); | |
4800 | expr.value = error_mark_node; | |
4801 | expr.original_code = ERROR_MARK; | |
4802 | break; | |
4803 | } | |
4804 | { | |
4805 | tree id = c_parser_peek_token (parser)->value; | |
766beb40 | 4806 | location_t loc = c_parser_peek_token (parser)->location; |
27bf414c JM |
4807 | c_parser_consume_token (parser); |
4808 | expr.value = build_external_ref (id, | |
4809 | (c_parser_peek_token (parser)->type | |
766beb40 | 4810 | == CPP_OPEN_PAREN), loc); |
27bf414c JM |
4811 | expr.original_code = ERROR_MARK; |
4812 | } | |
4813 | break; | |
4814 | case CPP_OPEN_PAREN: | |
4815 | /* A parenthesized expression, statement expression or compound | |
4816 | literal. */ | |
4817 | if (c_parser_peek_2nd_token (parser)->type == CPP_OPEN_BRACE) | |
4818 | { | |
4819 | /* A statement expression. */ | |
4820 | tree stmt; | |
4821 | c_parser_consume_token (parser); | |
4822 | c_parser_consume_token (parser); | |
4823 | if (cur_stmt_list == NULL) | |
4824 | { | |
4825 | error ("braced-group within expression allowed " | |
4826 | "only inside a function"); | |
4827 | parser->error = true; | |
4828 | c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL); | |
4829 | c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL); | |
4830 | expr.value = error_mark_node; | |
4831 | expr.original_code = ERROR_MARK; | |
4832 | break; | |
4833 | } | |
4834 | stmt = c_begin_stmt_expr (); | |
4835 | c_parser_compound_statement_nostart (parser); | |
4836 | c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, | |
4837 | "expected %<)%>"); | |
4838 | if (pedantic) | |
4839 | pedwarn ("ISO C forbids braced-groups within expressions"); | |
4840 | expr.value = c_finish_stmt_expr (stmt); | |
4841 | expr.original_code = ERROR_MARK; | |
4842 | } | |
4843 | else if (c_token_starts_typename (c_parser_peek_2nd_token (parser))) | |
4844 | { | |
4845 | /* A compound literal. ??? Can we actually get here rather | |
4846 | than going directly to | |
4847 | c_parser_postfix_expression_after_paren_type from | |
4848 | elsewhere? */ | |
4849 | struct c_type_name *type_name; | |
4850 | c_parser_consume_token (parser); | |
4851 | type_name = c_parser_type_name (parser); | |
4852 | c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, | |
4853 | "expected %<)%>"); | |
4854 | if (type_name == NULL) | |
4855 | { | |
4856 | expr.value = error_mark_node; | |
4857 | expr.original_code = ERROR_MARK; | |
4858 | } | |
4859 | else | |
4860 | expr = c_parser_postfix_expression_after_paren_type (parser, | |
4861 | type_name); | |
4862 | } | |
4863 | else | |
4864 | { | |
4865 | /* A parenthesized expression. */ | |
4866 | c_parser_consume_token (parser); | |
4867 | expr = c_parser_expression (parser); | |
4868 | if (TREE_CODE (expr.value) == MODIFY_EXPR) | |
4869 | TREE_NO_WARNING (expr.value) = 1; | |
4870 | expr.original_code = ERROR_MARK; | |
4871 | c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, | |
4872 | "expected %<)%>"); | |
4873 | } | |
4874 | break; | |
4875 | case CPP_KEYWORD: | |
4876 | switch (c_parser_peek_token (parser)->keyword) | |
4877 | { | |
4878 | case RID_FUNCTION_NAME: | |
4879 | case RID_PRETTY_FUNCTION_NAME: | |
4880 | case RID_C99_FUNCTION_NAME: | |
4881 | expr.value = fname_decl (c_parser_peek_token (parser)->keyword, | |
4882 | c_parser_peek_token (parser)->value); | |
4883 | expr.original_code = ERROR_MARK; | |
4884 | c_parser_consume_token (parser); | |
4885 | break; | |
4886 | case RID_VA_ARG: | |
4887 | c_parser_consume_token (parser); | |
4888 | if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>")) | |
4889 | { | |
4890 | expr.value = error_mark_node; | |
4891 | expr.original_code = ERROR_MARK; | |
4892 | break; | |
4893 | } | |
4894 | e1 = c_parser_expr_no_commas (parser, NULL); | |
4895 | if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>")) | |
4896 | { | |
4897 | c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL); | |
4898 | expr.value = error_mark_node; | |
4899 | expr.original_code = ERROR_MARK; | |
4900 | break; | |
4901 | } | |
4902 | t1 = c_parser_type_name (parser); | |
4903 | c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, | |
4904 | "expected %<)%>"); | |
4905 | if (t1 == NULL) | |
4906 | { | |
4907 | expr.value = error_mark_node; | |
4908 | expr.original_code = ERROR_MARK; | |
4909 | } | |
4910 | else | |
4911 | { | |
4912 | expr.value = build_va_arg (e1.value, groktypename (t1)); | |
4913 | expr.original_code = ERROR_MARK; | |
4914 | } | |
4915 | break; | |
4916 | case RID_OFFSETOF: | |
4917 | c_parser_consume_token (parser); | |
4918 | if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>")) | |
4919 | { | |
4920 | expr.value = error_mark_node; | |
4921 | expr.original_code = ERROR_MARK; | |
4922 | break; | |
4923 | } | |
4924 | t1 = c_parser_type_name (parser); | |
4925 | if (t1 == NULL) | |
4926 | { | |
4927 | expr.value = error_mark_node; | |
4928 | expr.original_code = ERROR_MARK; | |
4929 | break; | |
4930 | } | |
4931 | if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>")) | |
4932 | { | |
4933 | c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL); | |
4934 | expr.value = error_mark_node; | |
4935 | expr.original_code = ERROR_MARK; | |
4936 | break; | |
4937 | } | |
4938 | { | |
4939 | tree type = groktypename (t1); | |
4940 | tree offsetof_ref; | |
4941 | if (type == error_mark_node) | |
4942 | offsetof_ref = error_mark_node; | |
4943 | else | |
4944 | offsetof_ref = build1 (INDIRECT_REF, type, NULL); | |
4945 | /* Parse the second argument to __builtin_offsetof. We | |
4946 | must have one identifier, and beyond that we want to | |
4947 | accept sub structure and sub array references. */ | |
4948 | if (c_parser_next_token_is (parser, CPP_NAME)) | |
4949 | { | |
4950 | offsetof_ref = build_component_ref | |
4951 | (offsetof_ref, c_parser_peek_token (parser)->value); | |
4952 | c_parser_consume_token (parser); | |
4953 | while (c_parser_next_token_is (parser, CPP_DOT) | |
4954 | || c_parser_next_token_is (parser, | |
4955 | CPP_OPEN_SQUARE)) | |
4956 | { | |
4957 | if (c_parser_next_token_is (parser, CPP_DOT)) | |
4958 | { | |
4959 | c_parser_consume_token (parser); | |
4960 | if (c_parser_next_token_is_not (parser, | |
4961 | CPP_NAME)) | |
4962 | { | |
4963 | c_parser_error (parser, "expected identifier"); | |
4964 | break; | |
4965 | } | |
4966 | offsetof_ref = build_component_ref | |
4967 | (offsetof_ref, | |
4968 | c_parser_peek_token (parser)->value); | |
4969 | c_parser_consume_token (parser); | |
4970 | } | |
4971 | else | |
4972 | { | |
4973 | tree idx; | |
4974 | c_parser_consume_token (parser); | |
4975 | idx = c_parser_expression (parser).value; | |
4976 | c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, | |
4977 | "expected %<]%>"); | |
4978 | offsetof_ref = build_array_ref (offsetof_ref, idx); | |
4979 | } | |
4980 | } | |
4981 | } | |
4982 | else | |
4983 | c_parser_error (parser, "expected identifier"); | |
4984 | c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, | |
4985 | "expected %<)%>"); | |
4986 | expr.value = fold_offsetof (offsetof_ref); | |
4987 | expr.original_code = ERROR_MARK; | |
4988 | } | |
4989 | break; | |
4990 | case RID_CHOOSE_EXPR: | |
4991 | c_parser_consume_token (parser); | |
4992 | if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>")) | |
4993 | { | |
4994 | expr.value = error_mark_node; | |
4995 | expr.original_code = ERROR_MARK; | |
4996 | break; | |
4997 | } | |
4998 | e1 = c_parser_expr_no_commas (parser, NULL); | |
4999 | if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>")) | |
5000 | { | |
5001 | c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL); | |
5002 | expr.value = error_mark_node; | |
5003 | expr.original_code = ERROR_MARK; | |
5004 | break; | |
5005 | } | |
5006 | e2 = c_parser_expr_no_commas (parser, NULL); | |
5007 | if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>")) | |
5008 | { | |
5009 | c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL); | |
5010 | expr.value = error_mark_node; | |
5011 | expr.original_code = ERROR_MARK; | |
5012 | break; | |
5013 | } | |
5014 | e3 = c_parser_expr_no_commas (parser, NULL); | |
5015 | c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, | |
5016 | "expected %<)%>"); | |
5017 | { | |
5018 | tree c; | |
5019 | ||
5020 | c = fold (e1.value); | |
27bf414c JM |
5021 | if (TREE_CODE (c) != INTEGER_CST) |
5022 | error ("first argument to %<__builtin_choose_expr%> not" | |
5023 | " a constant"); | |
5024 | expr = integer_zerop (c) ? e3 : e2; | |
5025 | } | |
5026 | break; | |
5027 | case RID_TYPES_COMPATIBLE_P: | |
5028 | c_parser_consume_token (parser); | |
5029 | if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>")) | |
5030 | { | |
5031 | expr.value = error_mark_node; | |
5032 | expr.original_code = ERROR_MARK; | |
5033 | break; | |
5034 | } | |
5035 | t1 = c_parser_type_name (parser); | |
5036 | if (t1 == NULL) | |
5037 | { | |
5038 | expr.value = error_mark_node; | |
5039 | expr.original_code = ERROR_MARK; | |
5040 | break; | |
5041 | } | |
5042 | if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>")) | |
5043 | { | |
5044 | c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL); | |
5045 | expr.value = error_mark_node; | |
5046 | expr.original_code = ERROR_MARK; | |
5047 | break; | |
5048 | } | |
5049 | t2 = c_parser_type_name (parser); | |
5050 | if (t2 == NULL) | |
5051 | { | |
5052 | expr.value = error_mark_node; | |
5053 | expr.original_code = ERROR_MARK; | |
5054 | break; | |
5055 | } | |
5056 | c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, | |
5057 | "expected %<)%>"); | |
5058 | { | |
5059 | tree e1, e2; | |
5060 | ||
5061 | e1 = TYPE_MAIN_VARIANT (groktypename (t1)); | |
5062 | e2 = TYPE_MAIN_VARIANT (groktypename (t2)); | |
5063 | ||
5064 | expr.value = comptypes (e1, e2) | |
5065 | ? build_int_cst (NULL_TREE, 1) | |
5066 | : build_int_cst (NULL_TREE, 0); | |
5067 | expr.original_code = ERROR_MARK; | |
5068 | } | |
5069 | break; | |
5070 | case RID_AT_SELECTOR: | |
5071 | gcc_assert (c_dialect_objc ()); | |
5072 | c_parser_consume_token (parser); | |
5073 | if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>")) | |
5074 | { | |
5075 | expr.value = error_mark_node; | |
5076 | expr.original_code = ERROR_MARK; | |
5077 | break; | |
5078 | } | |
5079 | { | |
5080 | tree sel = c_parser_objc_selector_arg (parser); | |
5081 | c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, | |
5082 | "expected %<)%>"); | |
5083 | expr.value = objc_build_selector_expr (sel); | |
5084 | expr.original_code = ERROR_MARK; | |
5085 | } | |
5086 | break; | |
5087 | case RID_AT_PROTOCOL: | |
5088 | gcc_assert (c_dialect_objc ()); | |
5089 | c_parser_consume_token (parser); | |
5090 | if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>")) | |
5091 | { | |
5092 | expr.value = error_mark_node; | |
5093 | expr.original_code = ERROR_MARK; | |
5094 | break; | |
5095 | } | |
5096 | if (c_parser_next_token_is_not (parser, CPP_NAME)) | |
5097 | { | |
5098 | c_parser_error (parser, "expected identifier"); | |
5099 | c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL); | |
5100 | expr.value = error_mark_node; | |
5101 | expr.original_code = ERROR_MARK; | |
5102 | break; | |
5103 | } | |
5104 | { | |
5105 | tree id = c_parser_peek_token (parser)->value; | |
5106 | c_parser_consume_token (parser); | |
5107 | c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, | |
5108 | "expected %<)%>"); | |
5109 | expr.value = objc_build_protocol_expr (id); | |
5110 | expr.original_code = ERROR_MARK; | |
5111 | } | |
5112 | break; | |
5113 | case RID_AT_ENCODE: | |
5114 | /* Extension to support C-structures in the archiver. */ | |
5115 | gcc_assert (c_dialect_objc ()); | |
5116 | c_parser_consume_token (parser); | |
5117 | if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>")) | |
5118 | { | |
5119 | expr.value = error_mark_node; | |
5120 | expr.original_code = ERROR_MARK; | |
5121 | break; | |
5122 | } | |
5123 | t1 = c_parser_type_name (parser); | |
5124 | if (t1 == NULL) | |
5125 | { | |
5126 | expr.value = error_mark_node; | |
5127 | expr.original_code = ERROR_MARK; | |
5128 | c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL); | |
5129 | break; | |
5130 | } | |
5131 | c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, | |
5132 | "expected %<)%>"); | |
5133 | { | |
5134 | tree type = groktypename (t1); | |
5135 | expr.value = objc_build_encode_expr (type); | |
5136 | expr.original_code = ERROR_MARK; | |
5137 | } | |
5138 | break; | |
5139 | default: | |
5140 | c_parser_error (parser, "expected expression"); | |
5141 | expr.value = error_mark_node; | |
5142 | expr.original_code = ERROR_MARK; | |
5143 | break; | |
5144 | } | |
5145 | break; | |
5146 | case CPP_OPEN_SQUARE: | |
5147 | if (c_dialect_objc ()) | |
5148 | { | |
5149 | tree receiver, args; | |
5150 | c_parser_consume_token (parser); | |
5151 | receiver = c_parser_objc_receiver (parser); | |
5152 | args = c_parser_objc_message_args (parser); | |
5153 | c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, | |
5154 | "expected %<]%>"); | |
5155 | expr.value = objc_build_message_expr (build_tree_list (receiver, | |
5156 | args)); | |
5157 | expr.original_code = ERROR_MARK; | |
5158 | break; | |
5159 | } | |
5160 | /* Else fall through to report error. */ | |
5161 | default: | |
5162 | c_parser_error (parser, "expected expression"); | |
5163 | expr.value = error_mark_node; | |
5164 | expr.original_code = ERROR_MARK; | |
5165 | break; | |
5166 | } | |
5167 | return c_parser_postfix_expression_after_primary (parser, expr); | |
5168 | } | |
5169 | ||
5170 | /* Parse a postfix expression after a parenthesized type name: the | |
5171 | brace-enclosed initializer of a compound literal, possibly followed | |
5172 | by some postfix operators. This is separate because it is not | |
5173 | possible to tell until after the type name whether a cast | |
5174 | expression has a cast or a compound literal, or whether the operand | |
5175 | of sizeof is a parenthesized type name or starts with a compound | |
5176 | literal. */ | |
5177 | ||
5178 | static struct c_expr | |
5179 | c_parser_postfix_expression_after_paren_type (c_parser *parser, | |
5180 | struct c_type_name *type_name) | |
5181 | { | |
5182 | tree type; | |
5183 | struct c_expr init; | |
5184 | struct c_expr expr; | |
5185 | start_init (NULL_TREE, NULL, 0); | |
5186 | type = groktypename (type_name); | |
5187 | if (C_TYPE_VARIABLE_SIZE (type)) | |
5188 | { | |
5189 | error ("compound literal has variable size"); | |
5190 | type = error_mark_node; | |
5191 | } | |
5192 | init = c_parser_braced_init (parser, type, false); | |
5193 | finish_init (); | |
5194 | maybe_warn_string_init (type, init); | |
5195 | ||
5196 | if (pedantic && !flag_isoc99) | |
5197 | pedwarn ("ISO C90 forbids compound literals"); | |
5198 | expr.value = build_compound_literal (type, init.value); | |
5199 | expr.original_code = ERROR_MARK; | |
5200 | return c_parser_postfix_expression_after_primary (parser, expr); | |
5201 | } | |
5202 | ||
5203 | /* Parse a postfix expression after the initial primary or compound | |
5204 | literal; that is, parse a series of postfix operators. */ | |
5205 | ||
5206 | static struct c_expr | |
5207 | c_parser_postfix_expression_after_primary (c_parser *parser, | |
5208 | struct c_expr expr) | |
5209 | { | |
5210 | tree ident, idx, exprlist; | |
5211 | while (true) | |
5212 | { | |
5213 | switch (c_parser_peek_token (parser)->type) | |
5214 | { | |
5215 | case CPP_OPEN_SQUARE: | |
5216 | /* Array reference. */ | |
5217 | c_parser_consume_token (parser); | |
5218 | idx = c_parser_expression (parser).value; | |
5219 | c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, | |
5220 | "expected %<]%>"); | |
5221 | expr.value = build_array_ref (expr.value, idx); | |
5222 | expr.original_code = ERROR_MARK; | |
5223 | break; | |
5224 | case CPP_OPEN_PAREN: | |
5225 | /* Function call. */ | |
5226 | c_parser_consume_token (parser); | |
5227 | if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN)) | |
5228 | exprlist = NULL_TREE; | |
5229 | else | |
5230 | exprlist = c_parser_expr_list (parser); | |
5231 | c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, | |
5232 | "expected %<)%>"); | |
5233 | expr.value = build_function_call (expr.value, exprlist); | |
5234 | expr.original_code = ERROR_MARK; | |
5235 | break; | |
5236 | case CPP_DOT: | |
5237 | /* Structure element reference. */ | |
5238 | c_parser_consume_token (parser); | |
5239 | if (c_parser_next_token_is (parser, CPP_NAME)) | |
5240 | ident = c_parser_peek_token (parser)->value; | |
5241 | else | |
5242 | { | |
5243 | c_parser_error (parser, "expected identifier"); | |
5244 | expr.value = error_mark_node; | |
5245 | expr.original_code = ERROR_MARK; | |
5246 | return expr; | |
5247 | } | |
5248 | c_parser_consume_token (parser); | |
5249 | expr.value = build_component_ref (expr.value, ident); | |
5250 | expr.original_code = ERROR_MARK; | |
5251 | break; | |
5252 | case CPP_DEREF: | |
5253 | /* Structure element reference. */ | |
5254 | c_parser_consume_token (parser); | |
5255 | if (c_parser_next_token_is (parser, CPP_NAME)) | |
5256 | ident = c_parser_peek_token (parser)->value; | |
5257 | else | |
5258 | { | |
5259 | c_parser_error (parser, "expected identifier"); | |
5260 | expr.value = error_mark_node; | |
5261 | expr.original_code = ERROR_MARK; | |
5262 | return expr; | |
5263 | } | |
5264 | c_parser_consume_token (parser); | |
5265 | expr.value = build_component_ref (build_indirect_ref (expr.value, | |
5266 | "->"), ident); | |
5267 | expr.original_code = ERROR_MARK; | |
5268 | break; | |
5269 | case CPP_PLUS_PLUS: | |
5270 | /* Postincrement. */ | |
5271 | c_parser_consume_token (parser); | |
5272 | expr.value = build_unary_op (POSTINCREMENT_EXPR, expr.value, 0); | |
5273 | expr.original_code = ERROR_MARK; | |
5274 | break; | |
5275 | case CPP_MINUS_MINUS: | |
5276 | /* Postdecrement. */ | |
5277 | c_parser_consume_token (parser); | |
5278 | expr.value = build_unary_op (POSTDECREMENT_EXPR, expr.value, 0); | |
5279 | expr.original_code = ERROR_MARK; | |
5280 | break; | |
5281 | default: | |
5282 | return expr; | |
5283 | } | |
5284 | } | |
5285 | } | |
5286 | ||
5287 | /* Parse an expression (C90 6.3.17, C99 6.5.17). | |
5288 | ||
5289 | expression: | |
5290 | assignment-expression | |
5291 | expression , assignment-expression | |
5292 | */ | |
5293 | ||
5294 | static struct c_expr | |
5295 | c_parser_expression (c_parser *parser) | |
5296 | { | |
5297 | struct c_expr expr; | |
5298 | expr = c_parser_expr_no_commas (parser, NULL); | |
5299 | while (c_parser_next_token_is (parser, CPP_COMMA)) | |
5300 | { | |
5301 | struct c_expr next; | |
5302 | c_parser_consume_token (parser); | |
5303 | next = c_parser_expr_no_commas (parser, NULL); | |
5304 | expr.value = build_compound_expr (expr.value, next.value); | |
5305 | expr.original_code = COMPOUND_EXPR; | |
5306 | } | |
5307 | return expr; | |
5308 | } | |
5309 | ||
5310 | /* Parse a non-empty list of expressions. | |
5311 | ||
5312 | nonempty-expr-list: | |
5313 | assignment-expression | |
5314 | nonempty-expr-list , assignment-expression | |
5315 | */ | |
5316 | ||
5317 | static tree | |
5318 | c_parser_expr_list (c_parser *parser) | |
5319 | { | |
5320 | struct c_expr expr; | |
5321 | tree ret; | |
5322 | expr = c_parser_expr_no_commas (parser, NULL); | |
5323 | ret = build_tree_list (NULL_TREE, expr.value); | |
5324 | while (c_parser_next_token_is (parser, CPP_COMMA)) | |
5325 | { | |
5326 | c_parser_consume_token (parser); | |
5327 | expr = c_parser_expr_no_commas (parser, NULL); | |
5328 | ret = chainon (ret, build_tree_list (NULL_TREE, expr.value)); | |
5329 | } | |
5330 | return ret; | |
5331 | } | |
5332 | ||
5333 | \f | |
5334 | /* Parse Objective-C-specific constructs. */ | |
5335 | ||
5336 | /* Parse an objc-class-definition. | |
5337 | ||
5338 | objc-class-definition: | |
5339 | @interface identifier objc-superclass[opt] objc-protocol-refs[opt] | |
5340 | objc-class-instance-variables[opt] objc-methodprotolist @end | |
5341 | @implementation identifier objc-superclass[opt] | |
5342 | objc-class-instance-variables[opt] | |
5343 | @interface identifier ( identifier ) objc-protocol-refs[opt] | |
5344 | objc-methodprotolist @end | |
5345 | @implementation identifier ( identifier ) | |
5346 | ||
5347 | objc-superclass: | |
5348 | : identifier | |
5349 | ||
5350 | "@interface identifier (" must start "@interface identifier ( | |
5351 | identifier ) ...": objc-methodprotolist in the first production may | |
5352 | not start with a parenthesised identifier as a declarator of a data | |
5353 | definition with no declaration specifiers if the objc-superclass, | |
5354 | objc-protocol-refs and objc-class-instance-variables are omitted. */ | |
5355 | ||
5356 | static void | |
5357 | c_parser_objc_class_definition (c_parser *parser) | |
5358 | { | |
5359 | bool iface_p; | |
5360 | tree id1; | |
5361 | tree superclass; | |
5362 | if (c_parser_next_token_is_keyword (parser, RID_AT_INTERFACE)) | |
5363 | iface_p = true; | |
5364 | else if (c_parser_next_token_is_keyword (parser, RID_AT_IMPLEMENTATION)) | |
5365 | iface_p = false; | |
5366 | else | |
5367 | gcc_unreachable (); | |
5368 | c_parser_consume_token (parser); | |
5369 | if (c_parser_next_token_is_not (parser, CPP_NAME)) | |
5370 | { | |
5371 | c_parser_error (parser, "expected identifier"); | |
5372 | return; | |
5373 | } | |
5374 | id1 = c_parser_peek_token (parser)->value; | |
5375 | c_parser_consume_token (parser); | |
5376 | if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)) | |
5377 | { | |
5378 | tree id2; | |
5379 | tree proto = NULL_TREE; | |
5380 | c_parser_consume_token (parser); | |
5381 | if (c_parser_next_token_is_not (parser, CPP_NAME)) | |
5382 | { | |
5383 | c_parser_error (parser, "expected identifier"); | |
5384 | c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL); | |
5385 | return; | |
5386 | } | |
5387 | id2 = c_parser_peek_token (parser)->value; | |
5388 | c_parser_consume_token (parser); | |
5389 | c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>"); | |
5390 | if (!iface_p) | |
5391 | { | |
5392 | objc_start_category_implementation (id1, id2); | |
5393 | return; | |
5394 | } | |
5395 | if (c_parser_next_token_is (parser, CPP_LESS)) | |
5396 | proto = c_parser_objc_protocol_refs (parser); | |
5397 | objc_start_category_interface (id1, id2, proto); | |
5398 | c_parser_objc_methodprotolist (parser); | |
5399 | c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>"); | |
5400 | objc_finish_interface (); | |
5401 | return; | |
5402 | } | |
5403 | if (c_parser_next_token_is (parser, CPP_COLON)) | |
5404 | { | |
5405 | c_parser_consume_token (parser); | |
5406 | if (c_parser_next_token_is_not (parser, CPP_NAME)) | |
5407 | { | |
5408 | c_parser_error (parser, "expected identifier"); | |
5409 | return; | |
5410 | } | |
5411 | superclass = c_parser_peek_token (parser)->value; | |
5412 | c_parser_consume_token (parser); | |
5413 | } | |
5414 | else | |
5415 | superclass = NULL_TREE; | |
5416 | if (iface_p) | |
5417 | { | |
5418 | tree proto = NULL_TREE; | |
5419 | if (c_parser_next_token_is (parser, CPP_LESS)) | |
5420 | proto = c_parser_objc_protocol_refs (parser); | |
5421 | objc_start_class_interface (id1, superclass, proto); | |
5422 | } | |
5423 | else | |
5424 | objc_start_class_implementation (id1, superclass); | |
5425 | if (c_parser_next_token_is (parser, CPP_OPEN_BRACE)) | |
5426 | c_parser_objc_class_instance_variables (parser); | |
5427 | if (iface_p) | |
5428 | { | |
5429 | objc_continue_interface (); | |
5430 | c_parser_objc_methodprotolist (parser); | |
5431 | c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>"); | |
5432 | objc_finish_interface (); | |
5433 | } | |
5434 | else | |
5435 | { | |
5436 | objc_continue_implementation (); | |
5437 | return; | |
5438 | } | |
5439 | } | |
5440 | ||
5441 | /* Parse objc-class-instance-variables. | |
5442 | ||
5443 | objc-class-instance-variables: | |
5444 | { objc-instance-variable-decl-list[opt] } | |
5445 | ||
5446 | objc-instance-variable-decl-list: | |
5447 | objc-visibility-spec | |
5448 | objc-instance-variable-decl ; | |
5449 | ; | |
5450 | objc-instance-variable-decl-list objc-visibility-spec | |
5451 | objc-instance-variable-decl-list objc-instance-variable-decl ; | |
5452 | objc-instance-variable-decl-list ; | |
5453 | ||
5454 | objc-visibility-spec: | |
5455 | @private | |
5456 | @protected | |
5457 | @public | |
5458 | ||
5459 | objc-instance-variable-decl: | |
5460 | struct-declaration | |
5461 | */ | |
5462 | ||
5463 | static void | |
5464 | c_parser_objc_class_instance_variables (c_parser *parser) | |
5465 | { | |
5466 | gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE)); | |
5467 | c_parser_consume_token (parser); | |
5468 | while (c_parser_next_token_is_not (parser, CPP_EOF)) | |
5469 | { | |
5470 | tree decls; | |
5471 | /* Parse any stray semicolon. */ | |
5472 | if (c_parser_next_token_is (parser, CPP_SEMICOLON)) | |
5473 | { | |
5474 | if (pedantic) | |
5475 | pedwarn ("extra semicolon in struct or union specified"); | |
5476 | c_parser_consume_token (parser); | |
5477 | continue; | |
5478 | } | |
5479 | /* Stop if at the end of the instance variables. */ | |
5480 | if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE)) | |
5481 | { | |
5482 | c_parser_consume_token (parser); | |
5483 | break; | |
5484 | } | |
5485 | /* Parse any objc-visibility-spec. */ | |
5486 | if (c_parser_next_token_is_keyword (parser, RID_AT_PRIVATE)) | |
5487 | { | |
5488 | c_parser_consume_token (parser); | |
5489 | objc_set_visibility (2); | |
5490 | continue; | |
5491 | } | |
5492 | else if (c_parser_next_token_is_keyword (parser, RID_AT_PROTECTED)) | |
5493 | { | |
5494 | c_parser_consume_token (parser); | |
5495 | objc_set_visibility (0); | |
5496 | continue; | |
5497 | } | |
5498 | else if (c_parser_next_token_is_keyword (parser, RID_AT_PUBLIC)) | |
5499 | { | |
5500 | c_parser_consume_token (parser); | |
5501 | objc_set_visibility (1); | |
5502 | continue; | |
5503 | } | |
5504 | /* Parse some comma-separated declarations. */ | |
5505 | decls = c_parser_struct_declaration (parser); | |
5506 | { | |
5507 | /* Comma-separated instance variables are chained together in | |
5508 | reverse order; add them one by one. */ | |
5509 | tree ivar = nreverse (decls); | |
5510 | for (; ivar; ivar = TREE_CHAIN (ivar)) | |
5511 | objc_add_instance_variable (copy_node (ivar)); | |
5512 | } | |
5513 | c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>"); | |
5514 | } | |
5515 | } | |
5516 | ||
5517 | /* Parse an objc-class-declaration. | |
5518 | ||
5519 | objc-class-declaration: | |
5520 | @class identifier-list ; | |
5521 | */ | |
5522 | ||
5523 | static void | |
5524 | c_parser_objc_class_declaration (c_parser *parser) | |
5525 | { | |
5526 | tree list = NULL_TREE; | |
5527 | gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_CLASS)); | |
5528 | c_parser_consume_token (parser); | |
5529 | /* Any identifiers, including those declared as type names, are OK | |
5530 | here. */ | |
5531 | while (true) | |
5532 | { | |
5533 | tree id; | |
5534 | if (c_parser_next_token_is_not (parser, CPP_NAME)) | |
5535 | { | |
5536 | c_parser_error (parser, "expected identifier"); | |
5537 | break; | |
5538 | } | |
5539 | id = c_parser_peek_token (parser)->value; | |
5540 | list = chainon (list, build_tree_list (NULL_TREE, id)); | |
5541 | c_parser_consume_token (parser); | |
5542 | if (c_parser_next_token_is (parser, CPP_COMMA)) | |
5543 | c_parser_consume_token (parser); | |
5544 | else | |
5545 | break; | |
5546 | } | |
5547 | c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>"); | |
5548 | objc_declare_class (list); | |
5549 | } | |
5550 | ||
5551 | /* Parse an objc-alias-declaration. | |
5552 | ||
5553 | objc-alias-declaration: | |
5554 | @compatibility_alias identifier identifier ; | |
5555 | */ | |
5556 | ||
5557 | static void | |
5558 | c_parser_objc_alias_declaration (c_parser *parser) | |
5559 | { | |
5560 | tree id1, id2; | |
5561 | gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_ALIAS)); | |
5562 | c_parser_consume_token (parser); | |
5563 | if (c_parser_next_token_is_not (parser, CPP_NAME)) | |
5564 | { | |
5565 | c_parser_error (parser, "expected identifier"); | |
5566 | c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL); | |
5567 | return; | |
5568 | } | |
5569 | id1 = c_parser_peek_token (parser)->value; | |
5570 | c_parser_consume_token (parser); | |
5571 | if (c_parser_next_token_is_not (parser, CPP_NAME)) | |
5572 | { | |
5573 | c_parser_error (parser, "expected identifier"); | |
5574 | c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL); | |
5575 | return; | |
5576 | } | |
5577 | id2 = c_parser_peek_token (parser)->value; | |
5578 | c_parser_consume_token (parser); | |
5579 | c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>"); | |
5580 | objc_declare_alias (id1, id2); | |
5581 | } | |
5582 | ||
5583 | /* Parse an objc-protocol-definition. | |
5584 | ||
5585 | objc-protocol-definition: | |
5586 | @protocol identifier objc-protocol-refs[opt] objc-methodprotolist @end | |
5587 | @protocol identifier-list ; | |
5588 | ||
5589 | "@protocol identifier ;" should be resolved as "@protocol | |
5590 | identifier-list ;": objc-methodprotolist may not start with a | |
5591 | semicolon in the first alternative if objc-protocol-refs are | |
5592 | omitted. */ | |
5593 | ||
5594 | static void | |
5595 | c_parser_objc_protocol_definition (c_parser *parser) | |
5596 | { | |
5597 | gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_PROTOCOL)); | |
5598 | c_parser_consume_token (parser); | |
5599 | if (c_parser_next_token_is_not (parser, CPP_NAME)) | |
5600 | { | |
5601 | c_parser_error (parser, "expected identifier"); | |
5602 | return; | |
5603 | } | |
5604 | if (c_parser_peek_2nd_token (parser)->type == CPP_COMMA | |
5605 | || c_parser_peek_2nd_token (parser)->type == CPP_SEMICOLON) | |
5606 | { | |
5607 | tree list = NULL_TREE; | |
5608 | /* Any identifiers, including those declared as type names, are | |
5609 | OK here. */ | |
5610 | while (true) | |
5611 | { | |
5612 | tree id; | |
5613 | if (c_parser_next_token_is_not (parser, CPP_NAME)) | |
5614 | { | |
5615 | c_parser_error (parser, "expected identifier"); | |
5616 | break; | |
5617 | } | |
5618 | id = c_parser_peek_token (parser)->value; | |
5619 | list = chainon (list, build_tree_list (NULL_TREE, id)); | |
5620 | c_parser_consume_token (parser); | |
5621 | if (c_parser_next_token_is (parser, CPP_COMMA)) | |
5622 | c_parser_consume_token (parser); | |
5623 | else | |
5624 | break; | |
5625 | } | |
5626 | c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>"); | |
5627 | objc_declare_protocols (list); | |
5628 | } | |
5629 | else | |
5630 | { | |
5631 | tree id = c_parser_peek_token (parser)->value; | |
5632 | tree proto = NULL_TREE; | |
5633 | c_parser_consume_token (parser); | |
5634 | if (c_parser_next_token_is (parser, CPP_LESS)) | |
5635 | proto = c_parser_objc_protocol_refs (parser); | |
5636 | objc_pq_context = 1; | |
5637 | objc_start_protocol (id, proto); | |
5638 | c_parser_objc_methodprotolist (parser); | |
5639 | c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>"); | |
5640 | objc_pq_context = 0; | |
5641 | objc_finish_interface (); | |
5642 | } | |
5643 | } | |
5644 | ||
5645 | /* Parse an objc-method-type. | |
5646 | ||
5647 | objc-method-type: | |
5648 | + | |
5649 | - | |
5650 | */ | |
5651 | ||
5652 | static enum tree_code | |
5653 | c_parser_objc_method_type (c_parser *parser) | |
5654 | { | |
5655 | switch (c_parser_peek_token (parser)->type) | |
5656 | { | |
5657 | case CPP_PLUS: | |
5658 | c_parser_consume_token (parser); | |
5659 | return PLUS_EXPR; | |
5660 | case CPP_MINUS: | |
5661 | c_parser_consume_token (parser); | |
5662 | return MINUS_EXPR; | |
5663 | default: | |
5664 | gcc_unreachable (); | |
5665 | } | |
5666 | } | |
5667 | ||
5668 | /* Parse an objc-method-definition. | |
5669 | ||
5670 | objc-method-definition: | |
5671 | objc-method-type objc-method-decl ;[opt] compound-statement | |
5672 | */ | |
5673 | ||
5674 | static void | |
5675 | c_parser_objc_method_definition (c_parser *parser) | |
5676 | { | |
5677 | enum tree_code type = c_parser_objc_method_type (parser); | |
5678 | tree decl; | |
5679 | objc_set_method_type (type); | |
5680 | objc_pq_context = 1; | |
5681 | decl = c_parser_objc_method_decl (parser); | |
5682 | if (c_parser_next_token_is (parser, CPP_SEMICOLON)) | |
5683 | { | |
5684 | c_parser_consume_token (parser); | |
5685 | if (pedantic) | |
5686 | pedwarn ("extra semicolon in method definition specified"); | |
5687 | } | |
5688 | objc_pq_context = 0; | |
5689 | objc_start_method_definition (decl); | |
5690 | add_stmt (c_parser_compound_statement (parser)); | |
5691 | objc_finish_method_definition (current_function_decl); | |
5692 | } | |
5693 | ||
5694 | /* Parse an objc-methodprotolist. | |
5695 | ||
5696 | objc-methodprotolist: | |
5697 | empty | |
5698 | objc-methodprotolist objc-methodproto | |
5699 | objc-methodprotolist declaration | |
5700 | objc-methodprotolist ; | |
5701 | ||
5702 | The declaration is a data definition, which may be missing | |
5703 | declaration specifiers under the same rules and diagnostics as | |
5704 | other data definitions outside functions, and the stray semicolon | |
5705 | is diagnosed the same way as a stray semicolon outside a | |
5706 | function. */ | |
5707 | ||
5708 | static void | |
5709 | c_parser_objc_methodprotolist (c_parser *parser) | |
5710 | { | |
5711 | while (true) | |
5712 | { | |
5713 | /* The list is terminated by @end. */ | |
5714 | switch (c_parser_peek_token (parser)->type) | |
5715 | { | |
5716 | case CPP_SEMICOLON: | |
5717 | if (pedantic) | |
5718 | pedwarn ("ISO C does not allow extra %<;%> outside of a function"); | |
5719 | c_parser_consume_token (parser); | |
5720 | break; | |
5721 | case CPP_PLUS: | |
5722 | case CPP_MINUS: | |
5723 | c_parser_objc_methodproto (parser); | |
5724 | break; | |
5725 | case CPP_EOF: | |
5726 | return; | |
5727 | default: | |
5728 | if (c_parser_next_token_is_keyword (parser, RID_AT_END)) | |
5729 | return; | |
5730 | c_parser_declaration_or_fndef (parser, false, true, false, true); | |
5731 | break; | |
5732 | } | |
5733 | } | |
5734 | } | |
5735 | ||
5736 | /* Parse an objc-methodproto. | |
5737 | ||
5738 | objc-methodproto: | |
5739 | objc-method-type objc-method-decl ; | |
5740 | */ | |
5741 | ||
5742 | static void | |
5743 | c_parser_objc_methodproto (c_parser *parser) | |
5744 | { | |
5745 | enum tree_code type = c_parser_objc_method_type (parser); | |
5746 | tree decl; | |
5747 | objc_set_method_type (type); | |
5748 | /* Remember protocol qualifiers in prototypes. */ | |
5749 | objc_pq_context = 1; | |
5750 | decl = c_parser_objc_method_decl (parser); | |
5751 | /* Forget protocol qualifiers here. */ | |
5752 | objc_pq_context = 0; | |
5753 | objc_add_method_declaration (decl); | |
5754 | c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>"); | |
5755 | } | |
5756 | ||
5757 | /* Parse an objc-method-decl. | |
5758 | ||
5759 | objc-method-decl: | |
5760 | ( objc-type-name ) objc-selector | |
5761 | objc-selector | |
5762 | ( objc-type-name ) objc-keyword-selector objc-optparmlist | |
5763 | objc-keyword-selector objc-optparmlist | |
5764 | ||
5765 | objc-keyword-selector: | |
5766 | objc-keyword-decl | |
5767 | objc-keyword-selector objc-keyword-decl | |
5768 | ||
5769 | objc-keyword-decl: | |
5770 | objc-selector : ( objc-type-name ) identifier | |
5771 | objc-selector : identifier | |
5772 | : ( objc-type-name ) identifier | |
5773 | : identifier | |
5774 | ||
5775 | objc-optparmlist: | |
5776 | objc-optparms objc-optellipsis | |
5777 | ||
5778 | objc-optparms: | |
5779 | empty | |
5780 | objc-opt-parms , parameter-declaration | |
5781 | ||
5782 | objc-optellipsis: | |
5783 | empty | |
5784 | , ... | |
5785 | */ | |
5786 | ||
5787 | static tree | |
5788 | c_parser_objc_method_decl (c_parser *parser) | |
5789 | { | |
5790 | tree type = NULL_TREE; | |
5791 | tree sel; | |
5792 | tree parms = NULL_TREE; | |
5793 | if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)) | |
5794 | { | |
5795 | c_parser_consume_token (parser); | |
5796 | type = c_parser_objc_type_name (parser); | |
5797 | c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>"); | |
5798 | } | |
5799 | sel = c_parser_objc_selector (parser); | |
5800 | /* If there is no selector, or a colon follows, we have an | |
5801 | objc-keyword-selector. If there is a selector, and a colon does | |
5802 | not follow, that selector ends the objc-method-decl. */ | |
5803 | if (!sel || c_parser_next_token_is (parser, CPP_COLON)) | |
5804 | { | |
5805 | tree tsel = sel; | |
5806 | tree list = NULL_TREE; | |
5807 | bool ellipsis; | |
5808 | while (true) | |
5809 | { | |
5810 | tree atype = NULL_TREE, id, keyworddecl; | |
5811 | if (!c_parser_require (parser, CPP_COLON, "expected %<:%>")) | |
5812 | break; | |
5813 | if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)) | |
5814 | { | |
5815 | c_parser_consume_token (parser); | |
5816 | atype = c_parser_objc_type_name (parser); | |
5817 | c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, | |
5818 | "expected %<)%>"); | |
5819 | } | |
5820 | if (c_parser_next_token_is_not (parser, CPP_NAME)) | |
5821 | { | |
5822 | c_parser_error (parser, "expected identifier"); | |
5823 | return error_mark_node; | |
5824 | } | |
5825 | id = c_parser_peek_token (parser)->value; | |
5826 | c_parser_consume_token (parser); | |
5827 | keyworddecl = objc_build_keyword_decl (tsel, atype, id); | |
5828 | list = chainon (list, keyworddecl); | |
5829 | tsel = c_parser_objc_selector (parser); | |
5830 | if (!tsel && c_parser_next_token_is_not (parser, CPP_COLON)) | |
5831 | break; | |
5832 | } | |
5833 | /* Parse the optional parameter list. Optional Objective-C | |
5834 | method parameters follow the C syntax, and may include '...' | |
5835 | to denote a variable number of arguments. */ | |
5836 | parms = make_node (TREE_LIST); | |
5837 | ellipsis = false; | |
5838 | while (c_parser_next_token_is (parser, CPP_COMMA)) | |
5839 | { | |
5840 | struct c_parm *parm; | |
5841 | c_parser_consume_token (parser); | |
5842 | if (c_parser_next_token_is (parser, CPP_ELLIPSIS)) | |
5843 | { | |
5844 | ellipsis = true; | |
5845 | c_parser_consume_token (parser); | |
5846 | break; | |
5847 | } | |
5848 | parm = c_parser_parameter_declaration (parser, NULL_TREE); | |
5849 | if (parm == NULL) | |
5850 | break; | |
5851 | parms = chainon (parms, | |
5852 | build_tree_list (NULL_TREE, grokparm (parm))); | |
5853 | } | |
5854 | TREE_OVERFLOW (parms) = ellipsis; | |
5855 | sel = list; | |
5856 | } | |
5857 | return objc_build_method_signature (type, sel, parms); | |
5858 | } | |
5859 | ||
5860 | /* Parse an objc-type-name. | |
5861 | ||
5862 | objc-type-name: | |
5863 | objc-type-qualifiers[opt] type-name | |
5864 | objc-type-qualifiers[opt] | |
5865 | ||
5866 | objc-type-qualifiers: | |
5867 | objc-type-qualifier | |
5868 | objc-type-qualifiers objc-type-qualifier | |
5869 | ||
5870 | objc-type-qualifier: one of | |
5871 | in out inout bycopy byref oneway | |
5872 | */ | |
5873 | ||
5874 | static tree | |
5875 | c_parser_objc_type_name (c_parser *parser) | |
5876 | { | |
5877 | tree quals = NULL_TREE; | |
5878 | struct c_type_name *typename = NULL; | |
5879 | tree type = NULL_TREE; | |
5880 | while (true) | |
5881 | { | |
5882 | c_token *token = c_parser_peek_token (parser); | |
5883 | if (token->type == CPP_KEYWORD | |
5884 | && (token->keyword == RID_IN | |
5885 | || token->keyword == RID_OUT | |
5886 | || token->keyword == RID_INOUT | |
5887 | || token->keyword == RID_BYCOPY | |
5888 | || token->keyword == RID_BYREF | |
5889 | || token->keyword == RID_ONEWAY)) | |
5890 | { | |
5891 | quals = chainon (quals, build_tree_list (NULL_TREE, token->value)); | |
5892 | c_parser_consume_token (parser); | |
5893 | } | |
5894 | else | |
5895 | break; | |
5896 | } | |
5897 | if (c_parser_next_token_starts_typename (parser)) | |
5898 | typename = c_parser_type_name (parser); | |
5899 | if (typename) | |
5900 | type = groktypename (typename); | |
5901 | return build_tree_list (quals, type); | |
5902 | } | |
5903 | ||
5904 | /* Parse objc-protocol-refs. | |
5905 | ||
5906 | objc-protocol-refs: | |
5907 | < identifier-list > | |
5908 | */ | |
5909 | ||
5910 | static tree | |
5911 | c_parser_objc_protocol_refs (c_parser *parser) | |
5912 | { | |
5913 | tree list = NULL_TREE; | |
5914 | gcc_assert (c_parser_next_token_is (parser, CPP_LESS)); | |
5915 | c_parser_consume_token (parser); | |
5916 | /* Any identifiers, including those declared as type names, are OK | |
5917 | here. */ | |
5918 | while (true) | |
5919 | { | |
5920 | tree id; | |
5921 | if (c_parser_next_token_is_not (parser, CPP_NAME)) | |
5922 | { | |
5923 | c_parser_error (parser, "expected identifier"); | |
5924 | break; | |
5925 | } | |
5926 | id = c_parser_peek_token (parser)->value; | |
5927 | list = chainon (list, build_tree_list (NULL_TREE, id)); | |
5928 | c_parser_consume_token (parser); | |
5929 | if (c_parser_next_token_is (parser, CPP_COMMA)) | |
5930 | c_parser_consume_token (parser); | |
5931 | else | |
5932 | break; | |
5933 | } | |
5934 | c_parser_require (parser, CPP_GREATER, "expected %<>%>"); | |
5935 | return list; | |
5936 | } | |
5937 | ||
5938 | /* Parse an objc-try-catch-statement. | |
5939 | ||
5940 | objc-try-catch-statement: | |
5941 | @try compound-statement objc-catch-list[opt] | |
5942 | @try compound-statement objc-catch-list[opt] @finally compound-statement | |
5943 | ||
5944 | objc-catch-list: | |
5945 | @catch ( parameter-declaration ) compound-statement | |
5946 | objc-catch-list @catch ( parameter-declaration ) compound-statement | |
5947 | */ | |
5948 | ||
5949 | static void | |
5950 | c_parser_objc_try_catch_statement (c_parser *parser) | |
5951 | { | |
5952 | location_t loc; | |
5953 | tree stmt; | |
5954 | gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_TRY)); | |
5955 | c_parser_consume_token (parser); | |
5956 | loc = c_parser_peek_token (parser)->location; | |
5957 | stmt = c_parser_compound_statement (parser); | |
5958 | objc_begin_try_stmt (loc, stmt); | |
5959 | while (c_parser_next_token_is_keyword (parser, RID_AT_CATCH)) | |
5960 | { | |
5961 | struct c_parm *parm; | |
5962 | c_parser_consume_token (parser); | |
5963 | if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>")) | |
5964 | break; | |
5965 | parm = c_parser_parameter_declaration (parser, NULL_TREE); | |
5966 | if (parm == NULL) | |
5967 | { | |
5968 | c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL); | |
5969 | break; | |
5970 | } | |
5971 | c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>"); | |
5972 | objc_begin_catch_clause (grokparm (parm)); | |
5973 | if (c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>")) | |
5974 | c_parser_compound_statement_nostart (parser); | |
5975 | objc_finish_catch_clause (); | |
5976 | } | |
5977 | if (c_parser_next_token_is_keyword (parser, RID_AT_FINALLY)) | |
5978 | { | |
5979 | location_t finloc; | |
5980 | tree finstmt; | |
5981 | c_parser_consume_token (parser); | |
5982 | finloc = c_parser_peek_token (parser)->location; | |
5983 | finstmt = c_parser_compound_statement (parser); | |
5984 | objc_build_finally_clause (finloc, finstmt); | |
5985 | } | |
5986 | objc_finish_try_stmt (); | |
5987 | } | |
5988 | ||
5989 | /* Parse an objc-synchronized-statement. | |
5990 | ||
5991 | objc-synchronized-statement: | |
5992 | @synchronized ( expression ) compound-statement | |
5993 | */ | |
5994 | ||
5995 | static void | |
5996 | c_parser_objc_synchronized_statement (c_parser *parser) | |
5997 | { | |
5998 | location_t loc; | |
5999 | tree expr, stmt; | |
6000 | gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_SYNCHRONIZED)); | |
6001 | c_parser_consume_token (parser); | |
6002 | loc = c_parser_peek_token (parser)->location; | |
6003 | if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>")) | |
6004 | { | |
6005 | expr = c_parser_expression (parser).value; | |
6006 | c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>"); | |
6007 | } | |
6008 | else | |
6009 | expr = error_mark_node; | |
6010 | stmt = c_parser_compound_statement (parser); | |
6011 | objc_build_synchronized (loc, expr, stmt); | |
6012 | } | |
6013 | ||
6014 | /* Parse an objc-selector; return NULL_TREE without an error if the | |
6015 | next token is not an objc-selector. | |
6016 | ||
6017 | objc-selector: | |
6018 | identifier | |
6019 | one of | |
6020 | enum struct union if else while do for switch case default | |
6021 | break continue return goto asm sizeof typeof __alignof | |
6022 | unsigned long const short volatile signed restrict _Complex | |
6023 | in out inout bycopy byref oneway int char float double void _Bool | |
6024 | ||
6025 | ??? Why this selection of keywords but not, for example, storage | |
6026 | class specifiers? */ | |
6027 | ||
6028 | static tree | |
6029 | c_parser_objc_selector (c_parser *parser) | |
6030 | { | |
6031 | c_token *token = c_parser_peek_token (parser); | |
6032 | tree value = token->value; | |
6033 | if (token->type == CPP_NAME) | |
6034 | { | |
6035 | c_parser_consume_token (parser); | |
6036 | return value; | |
6037 | } | |
6038 | if (token->type != CPP_KEYWORD) | |
6039 | return NULL_TREE; | |
6040 | switch (token->keyword) | |
6041 | { | |
6042 | case RID_ENUM: | |
6043 | case RID_STRUCT: | |
6044 | case RID_UNION: | |
6045 | case RID_IF: | |
6046 | case RID_ELSE: | |
6047 | case RID_WHILE: | |
6048 | case RID_DO: | |
6049 | case RID_FOR: | |
6050 | case RID_SWITCH: | |
6051 | case RID_CASE: | |
6052 | case RID_DEFAULT: | |
6053 | case RID_BREAK: | |
6054 | case RID_CONTINUE: | |
6055 | case RID_RETURN: | |
6056 | case RID_GOTO: | |
6057 | case RID_ASM: | |
6058 | case RID_SIZEOF: | |
6059 | case RID_TYPEOF: | |
6060 | case RID_ALIGNOF: | |
6061 | case RID_UNSIGNED: | |
6062 | case RID_LONG: | |
6063 | case RID_CONST: | |
6064 | case RID_SHORT: | |
6065 | case RID_VOLATILE: | |
6066 | case RID_SIGNED: | |
6067 | case RID_RESTRICT: | |
6068 | case RID_COMPLEX: | |
6069 | case RID_IN: | |
6070 | case RID_OUT: | |
6071 | case RID_INOUT: | |
6072 | case RID_BYCOPY: | |
6073 | case RID_BYREF: | |
6074 | case RID_ONEWAY: | |
6075 | case RID_INT: | |
6076 | case RID_CHAR: | |
6077 | case RID_FLOAT: | |
6078 | case RID_DOUBLE: | |
6079 | case RID_VOID: | |
6080 | case RID_BOOL: | |
6081 | c_parser_consume_token (parser); | |
6082 | return value; | |
6083 | default: | |
6084 | return NULL_TREE; | |
6085 | } | |
6086 | } | |
6087 | ||
6088 | /* Parse an objc-selector-arg. | |
6089 | ||
6090 | objc-selector-arg: | |
6091 | objc-selector | |
6092 | objc-keywordname-list | |
6093 | ||
6094 | objc-keywordname-list: | |
6095 | objc-keywordname | |
6096 | objc-keywordname-list objc-keywordname | |
6097 | ||
6098 | objc-keywordname: | |
6099 | objc-selector : | |
6100 | : | |
6101 | */ | |
6102 | ||
6103 | static tree | |
6104 | c_parser_objc_selector_arg (c_parser *parser) | |
6105 | { | |
6106 | tree sel = c_parser_objc_selector (parser); | |
6107 | tree list = NULL_TREE; | |
6108 | if (sel && c_parser_next_token_is_not (parser, CPP_COLON)) | |
6109 | return sel; | |
6110 | while (true) | |
6111 | { | |
6112 | if (!c_parser_require (parser, CPP_COLON, "expected %<:%>")) | |
6113 | return list; | |
6114 | list = chainon (list, build_tree_list (sel, NULL_TREE)); | |
6115 | sel = c_parser_objc_selector (parser); | |
6116 | if (!sel && c_parser_next_token_is_not (parser, CPP_COLON)) | |
6117 | break; | |
6118 | } | |
6119 | return list; | |
6120 | } | |
6121 | ||
6122 | /* Parse an objc-receiver. | |
6123 | ||
6124 | objc-receiver: | |
6125 | expression | |
6126 | class-name | |
6127 | type-name | |
6128 | */ | |
6129 | ||
6130 | static tree | |
6131 | c_parser_objc_receiver (c_parser *parser) | |
6132 | { | |
6133 | if (c_parser_peek_token (parser)->type == CPP_NAME | |
6134 | && (c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME | |
6135 | || c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME)) | |
6136 | { | |
6137 | tree id = c_parser_peek_token (parser)->value; | |
6138 | c_parser_consume_token (parser); | |
6139 | return objc_get_class_reference (id); | |
6140 | } | |
6141 | return c_parser_expression (parser).value; | |
6142 | } | |
6143 | ||
6144 | /* Parse objc-message-args. | |
6145 | ||
6146 | objc-message-args: | |
6147 | objc-selector | |
6148 | objc-keywordarg-list | |
6149 | ||
6150 | objc-keywordarg-list: | |
6151 | objc-keywordarg | |
6152 | objc-keywordarg-list objc-keywordarg | |
6153 | ||
6154 | objc-keywordarg: | |
6155 | objc-selector : objc-keywordexpr | |
6156 | : objc-keywordexpr | |
6157 | */ | |
6158 | ||
6159 | static tree | |
6160 | c_parser_objc_message_args (c_parser *parser) | |
6161 | { | |
6162 | tree sel = c_parser_objc_selector (parser); | |
6163 | tree list = NULL_TREE; | |
6164 | if (sel && c_parser_next_token_is_not (parser, CPP_COLON)) | |
6165 | return sel; | |
6166 | while (true) | |
6167 | { | |
6168 | tree keywordexpr; | |
6169 | if (!c_parser_require (parser, CPP_COLON, "expected %<:%>")) | |
6170 | return list; | |
6171 | keywordexpr = c_parser_objc_keywordexpr (parser); | |
6172 | list = chainon (list, build_tree_list (sel, keywordexpr)); | |
6173 | sel = c_parser_objc_selector (parser); | |
6174 | if (!sel && c_parser_next_token_is_not (parser, CPP_COLON)) | |
6175 | break; | |
6176 | } | |
6177 | return list; | |
6178 | } | |
6179 | ||
6180 | /* Parse an objc-keywordexpr. | |
6181 | ||
6182 | objc-keywordexpr: | |
6183 | nonempty-expr-list | |
6184 | */ | |
6185 | ||
6186 | static tree | |
6187 | c_parser_objc_keywordexpr (c_parser *parser) | |
6188 | { | |
6189 | tree list = c_parser_expr_list (parser); | |
6190 | if (TREE_CHAIN (list) == NULL_TREE) | |
6191 | { | |
6192 | /* Just return the expression, remove a level of | |
6193 | indirection. */ | |
6194 | return TREE_VALUE (list); | |
6195 | } | |
6196 | else | |
6197 | { | |
6198 | /* We have a comma expression, we will collapse later. */ | |
6199 | return list; | |
6200 | } | |
6201 | } | |
6202 | ||
6203 | \f | |
6204 | /* The actual parser and external interface. ??? Does this need to be | |
6205 | garbage-collected? */ | |
6206 | ||
6207 | static GTY (()) c_parser *the_parser; | |
6208 | ||
6209 | /* Parse a single source file. */ | |
6210 | ||
6211 | void | |
6212 | c_parse_file (void) | |
6213 | { | |
6214 | the_parser = c_parser_new (); | |
6215 | c_parser_translation_unit (the_parser); | |
6216 | the_parser = NULL; | |
6217 | } | |
6218 | ||
6219 | #include "gt-c-parser.h" |