* config/i386/i386.md (paritydi2, paritysi2): New expanders.
[official-gcc.git] / gcc / c-parser.c
blob665b494e231cb2f91139013eda6959ff2522f2b3
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.
5 Parser actions based on the old Bison parser; structure somewhat
6 influenced by and fragments based on the C++ parser.
8 This file is part of GCC.
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.
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.
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, 51 Franklin Street, Fifth Floor, Boston, MA
23 02110-1301, USA. */
25 /* TODO:
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.
31 Add testcases covering every input symbol in every state in old and
32 new parsers.
34 Include full syntax for GNU C, including erroneous cases accepted
35 with error messages, in syntax productions in comments.
37 Make more diagnostics in the front end generally take an explicit
38 location rather than implicitly using input_location. */
40 #include "config.h"
41 #include "system.h"
42 #include "coretypes.h"
43 #include "tm.h"
44 #include "tree.h"
45 #include "rtl.h"
46 #include "langhooks.h"
47 #include "input.h"
48 #include "cpplib.h"
49 #include "timevar.h"
50 #include "c-pragma.h"
51 #include "c-tree.h"
52 #include "flags.h"
53 #include "output.h"
54 #include "toplev.h"
55 #include "ggc.h"
56 #include "c-common.h"
57 #include "vec.h"
58 #include "target.h"
59 #include "cgraph.h"
62 /* Objective-C specific parser/lexer information. */
64 static int objc_pq_context = 0;
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)
77 /* The reserved keyword table. */
78 struct resword
80 const char *word;
81 ENUM_BITFIELD(rid) rid : 16;
82 unsigned int disable : 16;
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 */
92 static const struct resword reswords[] =
94 { "_Bool", RID_BOOL, 0 },
95 { "_Complex", RID_COMPLEX, 0 },
96 { "_Decimal32", RID_DFLOAT32, D_EXT },
97 { "_Decimal64", RID_DFLOAT64, D_EXT },
98 { "_Decimal128", RID_DFLOAT128, D_EXT },
99 { "__FUNCTION__", RID_FUNCTION_NAME, 0 },
100 { "__PRETTY_FUNCTION__", RID_PRETTY_FUNCTION_NAME, 0 },
101 { "__alignof", RID_ALIGNOF, 0 },
102 { "__alignof__", RID_ALIGNOF, 0 },
103 { "__asm", RID_ASM, 0 },
104 { "__asm__", RID_ASM, 0 },
105 { "__attribute", RID_ATTRIBUTE, 0 },
106 { "__attribute__", RID_ATTRIBUTE, 0 },
107 { "__builtin_choose_expr", RID_CHOOSE_EXPR, 0 },
108 { "__builtin_offsetof", RID_OFFSETOF, 0 },
109 { "__builtin_types_compatible_p", RID_TYPES_COMPATIBLE_P, 0 },
110 { "__builtin_va_arg", RID_VA_ARG, 0 },
111 { "__complex", RID_COMPLEX, 0 },
112 { "__complex__", RID_COMPLEX, 0 },
113 { "__const", RID_CONST, 0 },
114 { "__const__", RID_CONST, 0 },
115 { "__extension__", RID_EXTENSION, 0 },
116 { "__func__", RID_C99_FUNCTION_NAME, 0 },
117 { "__imag", RID_IMAGPART, 0 },
118 { "__imag__", RID_IMAGPART, 0 },
119 { "__inline", RID_INLINE, 0 },
120 { "__inline__", RID_INLINE, 0 },
121 { "__label__", RID_LABEL, 0 },
122 { "__real", RID_REALPART, 0 },
123 { "__real__", RID_REALPART, 0 },
124 { "__restrict", RID_RESTRICT, 0 },
125 { "__restrict__", RID_RESTRICT, 0 },
126 { "__signed", RID_SIGNED, 0 },
127 { "__signed__", RID_SIGNED, 0 },
128 { "__thread", RID_THREAD, 0 },
129 { "__typeof", RID_TYPEOF, 0 },
130 { "__typeof__", RID_TYPEOF, 0 },
131 { "__volatile", RID_VOLATILE, 0 },
132 { "__volatile__", RID_VOLATILE, 0 },
133 { "asm", RID_ASM, D_EXT },
134 { "auto", RID_AUTO, 0 },
135 { "break", RID_BREAK, 0 },
136 { "case", RID_CASE, 0 },
137 { "char", RID_CHAR, 0 },
138 { "const", RID_CONST, 0 },
139 { "continue", RID_CONTINUE, 0 },
140 { "default", RID_DEFAULT, 0 },
141 { "do", RID_DO, 0 },
142 { "double", RID_DOUBLE, 0 },
143 { "else", RID_ELSE, 0 },
144 { "enum", RID_ENUM, 0 },
145 { "extern", RID_EXTERN, 0 },
146 { "float", RID_FLOAT, 0 },
147 { "for", RID_FOR, 0 },
148 { "goto", RID_GOTO, 0 },
149 { "if", RID_IF, 0 },
150 { "inline", RID_INLINE, D_EXT89 },
151 { "int", RID_INT, 0 },
152 { "long", RID_LONG, 0 },
153 { "register", RID_REGISTER, 0 },
154 { "restrict", RID_RESTRICT, D_C89 },
155 { "return", RID_RETURN, 0 },
156 { "short", RID_SHORT, 0 },
157 { "signed", RID_SIGNED, 0 },
158 { "sizeof", RID_SIZEOF, 0 },
159 { "static", RID_STATIC, 0 },
160 { "struct", RID_STRUCT, 0 },
161 { "switch", RID_SWITCH, 0 },
162 { "typedef", RID_TYPEDEF, 0 },
163 { "typeof", RID_TYPEOF, D_EXT },
164 { "union", RID_UNION, 0 },
165 { "unsigned", RID_UNSIGNED, 0 },
166 { "void", RID_VOID, 0 },
167 { "volatile", RID_VOLATILE, 0 },
168 { "while", RID_WHILE, 0 },
169 /* These Objective-C keywords are recognized only immediately after
170 an '@'. */
171 { "class", RID_AT_CLASS, D_OBJC },
172 { "compatibility_alias", RID_AT_ALIAS, D_OBJC },
173 { "defs", RID_AT_DEFS, D_OBJC },
174 { "encode", RID_AT_ENCODE, D_OBJC },
175 { "end", RID_AT_END, D_OBJC },
176 { "implementation", RID_AT_IMPLEMENTATION, D_OBJC },
177 { "interface", RID_AT_INTERFACE, D_OBJC },
178 { "private", RID_AT_PRIVATE, D_OBJC },
179 { "protected", RID_AT_PROTECTED, D_OBJC },
180 { "protocol", RID_AT_PROTOCOL, D_OBJC },
181 { "public", RID_AT_PUBLIC, D_OBJC },
182 { "selector", RID_AT_SELECTOR, D_OBJC },
183 { "throw", RID_AT_THROW, D_OBJC },
184 { "try", RID_AT_TRY, D_OBJC },
185 { "catch", RID_AT_CATCH, D_OBJC },
186 { "finally", RID_AT_FINALLY, D_OBJC },
187 { "synchronized", RID_AT_SYNCHRONIZED, D_OBJC },
188 /* These are recognized only in protocol-qualifier context
189 (see above) */
190 { "bycopy", RID_BYCOPY, D_OBJC },
191 { "byref", RID_BYREF, D_OBJC },
192 { "in", RID_IN, D_OBJC },
193 { "inout", RID_INOUT, D_OBJC },
194 { "oneway", RID_ONEWAY, D_OBJC },
195 { "out", RID_OUT, D_OBJC },
197 #define N_reswords (sizeof reswords / sizeof (struct resword))
199 /* Initialization routine for this file. */
201 void
202 c_parse_init (void)
204 /* The only initialization required is of the reserved word
205 identifiers. */
206 unsigned int i;
207 tree id;
208 int mask = (flag_isoc99 ? 0 : D_C89)
209 | (flag_no_asm ? (flag_isoc99 ? D_EXT : D_EXT|D_EXT89) : 0);
211 if (!c_dialect_objc ())
212 mask |= D_OBJC;
214 ridpointers = GGC_CNEWVEC (tree, (int) RID_MAX);
215 for (i = 0; i < N_reswords; i++)
217 /* If a keyword is disabled, do not enter it into the table
218 and so create a canonical spelling that isn't a keyword. */
219 if (reswords[i].disable & mask)
220 continue;
222 id = get_identifier (reswords[i].word);
223 C_RID_CODE (id) = reswords[i].rid;
224 C_IS_RESERVED_WORD (id) = 1;
225 ridpointers [(int) reswords[i].rid] = id;
229 /* The C lexer intermediates between the lexer in cpplib and c-lex.c
230 and the C parser. Unlike the C++ lexer, the parser structure
231 stores the lexer information instead of using a separate structure.
232 Identifiers are separated into ordinary identifiers, type names,
233 keywords and some other Objective-C types of identifiers, and some
234 look-ahead is maintained.
236 ??? It might be a good idea to lex the whole file up front (as for
237 C++). It would then be possible to share more of the C and C++
238 lexer code, if desired. */
240 /* The following local token type is used. */
242 /* A keyword. */
243 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
245 /* More information about the type of a CPP_NAME token. */
246 typedef enum c_id_kind {
247 /* An ordinary identifier. */
248 C_ID_ID,
249 /* An identifier declared as a typedef name. */
250 C_ID_TYPENAME,
251 /* An identifier declared as an Objective-C class name. */
252 C_ID_CLASSNAME,
253 /* Not an identifier. */
254 C_ID_NONE
255 } c_id_kind;
257 /* A single C token after string literal concatenation and conversion
258 of preprocessing tokens to tokens. */
259 typedef struct c_token GTY (())
261 /* The kind of token. */
262 ENUM_BITFIELD (cpp_ttype) type : 8;
263 /* If this token is a CPP_NAME, this value indicates whether also
264 declared as some kind of type. Otherwise, it is C_ID_NONE. */
265 ENUM_BITFIELD (c_id_kind) id_kind : 8;
266 /* If this token is a keyword, this value indicates which keyword.
267 Otherwise, this value is RID_MAX. */
268 ENUM_BITFIELD (rid) keyword : 8;
269 /* If this token is a CPP_PRAGMA, this indicates the pragma that
270 was seen. Otherwise it is PRAGMA_NONE. */
271 ENUM_BITFIELD (pragma_kind) pragma_kind : 7;
272 /* True if this token is from a system header. */
273 BOOL_BITFIELD in_system_header : 1;
274 /* The value associated with this token, if any. */
275 tree value;
276 /* The location at which this token was found. */
277 location_t location;
278 } c_token;
280 /* A parser structure recording information about the state and
281 context of parsing. Includes lexer information with up to two
282 tokens of look-ahead; more are not needed for C. */
283 typedef struct c_parser GTY(())
285 /* The look-ahead tokens. */
286 c_token tokens[2];
287 /* How many look-ahead tokens are available (0, 1 or 2). */
288 short tokens_avail;
289 /* True if a syntax error is being recovered from; false otherwise.
290 c_parser_error sets this flag. It should clear this flag when
291 enough tokens have been consumed to recover from the error. */
292 BOOL_BITFIELD error : 1;
293 /* True if we're processing a pragma, and shouldn't automatically
294 consume CPP_PRAGMA_EOL. */
295 BOOL_BITFIELD in_pragma : 1;
296 } c_parser;
299 /* The actual parser and external interface. ??? Does this need to be
300 garbage-collected? */
302 static GTY (()) c_parser *the_parser;
305 /* Read in and lex a single token, storing it in *TOKEN. */
307 static void
308 c_lex_one_token (c_token *token)
310 timevar_push (TV_LEX);
312 token->type = c_lex_with_flags (&token->value, &token->location, NULL);
313 token->id_kind = C_ID_NONE;
314 token->keyword = RID_MAX;
315 token->pragma_kind = PRAGMA_NONE;
316 token->in_system_header = in_system_header;
318 switch (token->type)
320 case CPP_NAME:
322 tree decl;
324 int objc_force_identifier = objc_need_raw_identifier;
325 OBJC_NEED_RAW_IDENTIFIER (0);
327 if (C_IS_RESERVED_WORD (token->value))
329 enum rid rid_code = C_RID_CODE (token->value);
331 if (c_dialect_objc ())
333 if (!OBJC_IS_AT_KEYWORD (rid_code)
334 && (!OBJC_IS_PQ_KEYWORD (rid_code) || objc_pq_context))
336 /* Return the canonical spelling for this keyword. */
337 token->value = ridpointers[(int) rid_code];
338 token->type = CPP_KEYWORD;
339 token->keyword = rid_code;
340 break;
343 else
345 /* Return the canonical spelling for this keyword. */
346 token->value = ridpointers[(int) rid_code];
347 token->type = CPP_KEYWORD;
348 token->keyword = rid_code;
349 break;
353 decl = lookup_name (token->value);
354 if (decl)
356 if (TREE_CODE (decl) == TYPE_DECL)
358 token->id_kind = C_ID_TYPENAME;
359 break;
362 else if (c_dialect_objc ())
364 tree objc_interface_decl = objc_is_class_name (token->value);
365 /* Objective-C class names are in the same namespace as
366 variables and typedefs, and hence are shadowed by local
367 declarations. */
368 if (objc_interface_decl
369 && (global_bindings_p ()
370 || (!objc_force_identifier && !decl)))
372 token->value = objc_interface_decl;
373 token->id_kind = C_ID_CLASSNAME;
374 break;
377 token->id_kind = C_ID_ID;
379 break;
380 case CPP_AT_NAME:
381 /* This only happens in Objective-C; it must be a keyword. */
382 token->type = CPP_KEYWORD;
383 token->keyword = C_RID_CODE (token->value);
384 break;
385 case CPP_COLON:
386 case CPP_COMMA:
387 case CPP_CLOSE_PAREN:
388 case CPP_SEMICOLON:
389 /* These tokens may affect the interpretation of any identifiers
390 following, if doing Objective-C. */
391 OBJC_NEED_RAW_IDENTIFIER (0);
392 break;
393 case CPP_PRAGMA:
394 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
395 token->pragma_kind = TREE_INT_CST_LOW (token->value);
396 token->value = NULL;
397 break;
398 default:
399 break;
401 timevar_pop (TV_LEX);
404 /* Return a pointer to the next token from PARSER, reading it in if
405 necessary. */
407 static inline c_token *
408 c_parser_peek_token (c_parser *parser)
410 if (parser->tokens_avail == 0)
412 c_lex_one_token (&parser->tokens[0]);
413 parser->tokens_avail = 1;
415 return &parser->tokens[0];
418 /* Return true if the next token from PARSER has the indicated
419 TYPE. */
421 static inline bool
422 c_parser_next_token_is (c_parser *parser, enum cpp_ttype type)
424 return c_parser_peek_token (parser)->type == type;
427 /* Return true if the next token from PARSER does not have the
428 indicated TYPE. */
430 static inline bool
431 c_parser_next_token_is_not (c_parser *parser, enum cpp_ttype type)
433 return !c_parser_next_token_is (parser, type);
436 /* Return true if the next token from PARSER is the indicated
437 KEYWORD. */
439 static inline bool
440 c_parser_next_token_is_keyword (c_parser *parser, enum rid keyword)
442 c_token *token;
444 /* Peek at the next token. */
445 token = c_parser_peek_token (parser);
446 /* Check to see if it is the indicated keyword. */
447 return token->keyword == keyword;
450 /* Return true if TOKEN can start a type name,
451 false otherwise. */
452 static bool
453 c_token_starts_typename (c_token *token)
455 switch (token->type)
457 case CPP_NAME:
458 switch (token->id_kind)
460 case C_ID_ID:
461 return false;
462 case C_ID_TYPENAME:
463 return true;
464 case C_ID_CLASSNAME:
465 gcc_assert (c_dialect_objc ());
466 return true;
467 default:
468 gcc_unreachable ();
470 case CPP_KEYWORD:
471 switch (token->keyword)
473 case RID_UNSIGNED:
474 case RID_LONG:
475 case RID_SHORT:
476 case RID_SIGNED:
477 case RID_COMPLEX:
478 case RID_INT:
479 case RID_CHAR:
480 case RID_FLOAT:
481 case RID_DOUBLE:
482 case RID_VOID:
483 case RID_DFLOAT32:
484 case RID_DFLOAT64:
485 case RID_DFLOAT128:
486 case RID_BOOL:
487 case RID_ENUM:
488 case RID_STRUCT:
489 case RID_UNION:
490 case RID_TYPEOF:
491 case RID_CONST:
492 case RID_VOLATILE:
493 case RID_RESTRICT:
494 case RID_ATTRIBUTE:
495 return true;
496 default:
497 return false;
499 case CPP_LESS:
500 if (c_dialect_objc ())
501 return true;
502 return false;
503 default:
504 return false;
508 /* Return true if the next token from PARSER can start a type name,
509 false otherwise. */
510 static inline bool
511 c_parser_next_token_starts_typename (c_parser *parser)
513 c_token *token = c_parser_peek_token (parser);
514 return c_token_starts_typename (token);
517 /* Return true if TOKEN can start declaration specifiers, false
518 otherwise. */
519 static bool
520 c_token_starts_declspecs (c_token *token)
522 switch (token->type)
524 case CPP_NAME:
525 switch (token->id_kind)
527 case C_ID_ID:
528 return false;
529 case C_ID_TYPENAME:
530 return true;
531 case C_ID_CLASSNAME:
532 gcc_assert (c_dialect_objc ());
533 return true;
534 default:
535 gcc_unreachable ();
537 case CPP_KEYWORD:
538 switch (token->keyword)
540 case RID_STATIC:
541 case RID_EXTERN:
542 case RID_REGISTER:
543 case RID_TYPEDEF:
544 case RID_INLINE:
545 case RID_AUTO:
546 case RID_THREAD:
547 case RID_UNSIGNED:
548 case RID_LONG:
549 case RID_SHORT:
550 case RID_SIGNED:
551 case RID_COMPLEX:
552 case RID_INT:
553 case RID_CHAR:
554 case RID_FLOAT:
555 case RID_DOUBLE:
556 case RID_VOID:
557 case RID_DFLOAT32:
558 case RID_DFLOAT64:
559 case RID_DFLOAT128:
560 case RID_BOOL:
561 case RID_ENUM:
562 case RID_STRUCT:
563 case RID_UNION:
564 case RID_TYPEOF:
565 case RID_CONST:
566 case RID_VOLATILE:
567 case RID_RESTRICT:
568 case RID_ATTRIBUTE:
569 return true;
570 default:
571 return false;
573 case CPP_LESS:
574 if (c_dialect_objc ())
575 return true;
576 return false;
577 default:
578 return false;
582 /* Return true if the next token from PARSER can start declaration
583 specifiers, false otherwise. */
584 static inline bool
585 c_parser_next_token_starts_declspecs (c_parser *parser)
587 c_token *token = c_parser_peek_token (parser);
588 return c_token_starts_declspecs (token);
591 /* Return a pointer to the next-but-one token from PARSER, reading it
592 in if necessary. The next token is already read in. */
594 static c_token *
595 c_parser_peek_2nd_token (c_parser *parser)
597 if (parser->tokens_avail >= 2)
598 return &parser->tokens[1];
599 gcc_assert (parser->tokens_avail == 1);
600 gcc_assert (parser->tokens[0].type != CPP_EOF);
601 gcc_assert (parser->tokens[0].type != CPP_PRAGMA_EOL);
602 c_lex_one_token (&parser->tokens[1]);
603 parser->tokens_avail = 2;
604 return &parser->tokens[1];
607 /* Consume the next token from PARSER. */
609 static void
610 c_parser_consume_token (c_parser *parser)
612 gcc_assert (parser->tokens_avail >= 1);
613 gcc_assert (parser->tokens[0].type != CPP_EOF);
614 gcc_assert (!parser->in_pragma || parser->tokens[0].type != CPP_PRAGMA_EOL);
615 gcc_assert (parser->error || parser->tokens[0].type != CPP_PRAGMA);
616 if (parser->tokens_avail == 2)
617 parser->tokens[0] = parser->tokens[1];
618 parser->tokens_avail--;
621 /* Expect the current token to be a #pragma. Consume it and remember
622 that we've begun parsing a pragma. */
624 static void
625 c_parser_consume_pragma (c_parser *parser)
627 gcc_assert (!parser->in_pragma);
628 gcc_assert (parser->tokens_avail >= 1);
629 gcc_assert (parser->tokens[0].type == CPP_PRAGMA);
630 if (parser->tokens_avail == 2)
631 parser->tokens[0] = parser->tokens[1];
632 parser->tokens_avail--;
633 parser->in_pragma = true;
636 /* Update the globals input_location and in_system_header from
637 TOKEN. */
638 static inline void
639 c_parser_set_source_position_from_token (c_token *token)
641 if (token->type != CPP_EOF)
643 input_location = token->location;
644 in_system_header = token->in_system_header;
648 /* Issue a diagnostic of the form
649 FILE:LINE: MESSAGE before TOKEN
650 where TOKEN is the next token in the input stream of PARSER.
651 MESSAGE (specified by the caller) is usually of the form "expected
652 OTHER-TOKEN".
654 Do not issue a diagnostic if still recovering from an error.
656 ??? This is taken from the C++ parser, but building up messages in
657 this way is not i18n-friendly and some other approach should be
658 used. */
660 static void
661 c_parser_error (c_parser *parser, const char *gmsgid)
663 c_token *token = c_parser_peek_token (parser);
664 if (parser->error)
665 return;
666 parser->error = true;
667 if (!gmsgid)
668 return;
669 /* This diagnostic makes more sense if it is tagged to the line of
670 the token we just peeked at. */
671 c_parser_set_source_position_from_token (token);
672 c_parse_error (gmsgid,
673 /* Because c_parse_error does not understand
674 CPP_KEYWORD, keywords are treated like
675 identifiers. */
676 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
677 token->value);
680 /* If the next token is of the indicated TYPE, consume it. Otherwise,
681 issue the error MSGID. If MSGID is NULL then a message has already
682 been produced and no message will be produced this time. Returns
683 true if found, false otherwise. */
685 static bool
686 c_parser_require (c_parser *parser,
687 enum cpp_ttype type,
688 const char *msgid)
690 if (c_parser_next_token_is (parser, type))
692 c_parser_consume_token (parser);
693 return true;
695 else
697 c_parser_error (parser, msgid);
698 return false;
702 /* If the next token is the indicated keyword, consume it. Otherwise,
703 issue the error MSGID. Returns true if found, false otherwise. */
705 static bool
706 c_parser_require_keyword (c_parser *parser,
707 enum rid keyword,
708 const char *msgid)
710 if (c_parser_next_token_is_keyword (parser, keyword))
712 c_parser_consume_token (parser);
713 return true;
715 else
717 c_parser_error (parser, msgid);
718 return false;
722 /* Like c_parser_require, except that tokens will be skipped until the
723 desired token is found. An error message is still produced if the
724 next token is not as expected. If MSGID is NULL then a message has
725 already been produced and no message will be produced this
726 time. */
728 static void
729 c_parser_skip_until_found (c_parser *parser,
730 enum cpp_ttype type,
731 const char *msgid)
733 unsigned nesting_depth = 0;
735 if (c_parser_require (parser, type, msgid))
736 return;
738 /* Skip tokens until the desired token is found. */
739 while (true)
741 /* Peek at the next token. */
742 c_token *token = c_parser_peek_token (parser);
743 /* If we've reached the token we want, consume it and stop. */
744 if (token->type == type && !nesting_depth)
746 c_parser_consume_token (parser);
747 break;
750 /* If we've run out of tokens, stop. */
751 if (token->type == CPP_EOF)
752 return;
753 if (token->type == CPP_PRAGMA_EOL && parser->in_pragma)
754 return;
755 if (token->type == CPP_OPEN_BRACE
756 || token->type == CPP_OPEN_PAREN
757 || token->type == CPP_OPEN_SQUARE)
758 ++nesting_depth;
759 else if (token->type == CPP_CLOSE_BRACE
760 || token->type == CPP_CLOSE_PAREN
761 || token->type == CPP_CLOSE_SQUARE)
763 if (nesting_depth-- == 0)
764 break;
766 /* Consume this token. */
767 c_parser_consume_token (parser);
769 parser->error = false;
772 /* Skip tokens until the end of a parameter is found, but do not
773 consume the comma, semicolon or closing delimiter. */
775 static void
776 c_parser_skip_to_end_of_parameter (c_parser *parser)
778 unsigned nesting_depth = 0;
780 while (true)
782 c_token *token = c_parser_peek_token (parser);
783 if ((token->type == CPP_COMMA || token->type == CPP_SEMICOLON)
784 && !nesting_depth)
785 break;
786 /* If we've run out of tokens, stop. */
787 if (token->type == CPP_EOF)
788 return;
789 if (token->type == CPP_PRAGMA_EOL && parser->in_pragma)
790 return;
791 if (token->type == CPP_OPEN_BRACE
792 || token->type == CPP_OPEN_PAREN
793 || token->type == CPP_OPEN_SQUARE)
794 ++nesting_depth;
795 else if (token->type == CPP_CLOSE_BRACE
796 || token->type == CPP_CLOSE_PAREN
797 || token->type == CPP_CLOSE_SQUARE)
799 if (nesting_depth-- == 0)
800 break;
802 /* Consume this token. */
803 c_parser_consume_token (parser);
805 parser->error = false;
808 /* Expect to be at the end of the pragma directive and consume an
809 end of line marker. */
811 static void
812 c_parser_skip_to_pragma_eol (c_parser *parser)
814 gcc_assert (parser->in_pragma);
815 parser->in_pragma = false;
817 if (!c_parser_require (parser, CPP_PRAGMA_EOL, "expected end of line"))
818 while (true)
820 c_token *token = c_parser_peek_token (parser);
821 if (token->type == CPP_EOF)
822 break;
823 if (token->type == CPP_PRAGMA_EOL)
825 c_parser_consume_token (parser);
826 break;
828 c_parser_consume_token (parser);
831 parser->error = false;
834 /* Skip tokens until we have consumed an entire block, or until we
835 have consumed a non-nested ';'. */
837 static void
838 c_parser_skip_to_end_of_block_or_statement (c_parser *parser)
840 unsigned nesting_depth = 0;
841 bool save_error = parser->error;
843 while (true)
845 c_token *token;
847 /* Peek at the next token. */
848 token = c_parser_peek_token (parser);
850 switch (token->type)
852 case CPP_EOF:
853 return;
855 case CPP_PRAGMA_EOL:
856 if (parser->in_pragma)
857 return;
858 break;
860 case CPP_SEMICOLON:
861 /* If the next token is a ';', we have reached the
862 end of the statement. */
863 if (!nesting_depth)
865 /* Consume the ';'. */
866 c_parser_consume_token (parser);
867 goto finished;
869 break;
871 case CPP_CLOSE_BRACE:
872 /* If the next token is a non-nested '}', then we have
873 reached the end of the current block. */
874 if (nesting_depth == 0 || --nesting_depth == 0)
876 c_parser_consume_token (parser);
877 goto finished;
879 break;
881 case CPP_OPEN_BRACE:
882 /* If it the next token is a '{', then we are entering a new
883 block. Consume the entire block. */
884 ++nesting_depth;
885 break;
887 case CPP_PRAGMA:
888 /* If we see a pragma, consume the whole thing at once. We
889 have some safeguards against consuming pragmas willy-nilly.
890 Normally, we'd expect to be here with parser->error set,
891 which disables these safeguards. But it's possible to get
892 here for secondary error recovery, after parser->error has
893 been cleared. */
894 c_parser_consume_pragma (parser);
895 c_parser_skip_to_pragma_eol (parser);
896 parser->error = save_error;
897 continue;
899 default:
900 break;
903 c_parser_consume_token (parser);
906 finished:
907 parser->error = false;
910 /* Save the warning flags which are controlled by __extension__. */
912 static inline int
913 disable_extension_diagnostics (void)
915 int ret = (pedantic
916 | (warn_pointer_arith << 1)
917 | (warn_traditional << 2)
918 | (flag_iso << 3));
919 pedantic = 0;
920 warn_pointer_arith = 0;
921 warn_traditional = 0;
922 flag_iso = 0;
923 return ret;
926 /* Restore the warning flags which are controlled by __extension__.
927 FLAGS is the return value from disable_extension_diagnostics. */
929 static inline void
930 restore_extension_diagnostics (int flags)
932 pedantic = flags & 1;
933 warn_pointer_arith = (flags >> 1) & 1;
934 warn_traditional = (flags >> 2) & 1;
935 flag_iso = (flags >> 3) & 1;
938 /* Possibly kinds of declarator to parse. */
939 typedef enum c_dtr_syn {
940 /* A normal declarator with an identifier. */
941 C_DTR_NORMAL,
942 /* An abstract declarator (maybe empty). */
943 C_DTR_ABSTRACT,
944 /* A parameter declarator: may be either, but after a type name does
945 not redeclare a typedef name as an identifier if it can
946 alternatively be interpreted as a typedef name; see DR#009,
947 applied in C90 TC1, omitted from C99 and reapplied in C99 TC2
948 following DR#249. For example, given a typedef T, "int T" and
949 "int *T" are valid parameter declarations redeclaring T, while
950 "int (T)" and "int * (T)" and "int (T[])" and "int (T (int))" are
951 abstract declarators rather than involving redundant parentheses;
952 the same applies with attributes inside the parentheses before
953 "T". */
954 C_DTR_PARM
955 } c_dtr_syn;
957 static void c_parser_external_declaration (c_parser *);
958 static void c_parser_asm_definition (c_parser *);
959 static void c_parser_declaration_or_fndef (c_parser *, bool, bool, bool, bool);
960 static void c_parser_declspecs (c_parser *, struct c_declspecs *, bool, bool,
961 bool);
962 static struct c_typespec c_parser_enum_specifier (c_parser *);
963 static struct c_typespec c_parser_struct_or_union_specifier (c_parser *);
964 static tree c_parser_struct_declaration (c_parser *);
965 static struct c_typespec c_parser_typeof_specifier (c_parser *);
966 static struct c_declarator *c_parser_declarator (c_parser *, bool, c_dtr_syn,
967 bool *);
968 static struct c_declarator *c_parser_direct_declarator (c_parser *, bool,
969 c_dtr_syn, bool *);
970 static struct c_declarator *c_parser_direct_declarator_inner (c_parser *,
971 bool,
972 struct c_declarator *);
973 static struct c_arg_info *c_parser_parms_declarator (c_parser *, bool, tree);
974 static struct c_arg_info *c_parser_parms_list_declarator (c_parser *, tree);
975 static struct c_parm *c_parser_parameter_declaration (c_parser *, tree);
976 static tree c_parser_simple_asm_expr (c_parser *);
977 static tree c_parser_attributes (c_parser *);
978 static struct c_type_name *c_parser_type_name (c_parser *);
979 static struct c_expr c_parser_initializer (c_parser *);
980 static struct c_expr c_parser_braced_init (c_parser *, tree, bool);
981 static void c_parser_initelt (c_parser *);
982 static void c_parser_initval (c_parser *, struct c_expr *);
983 static tree c_parser_compound_statement (c_parser *);
984 static void c_parser_compound_statement_nostart (c_parser *);
985 static void c_parser_label (c_parser *);
986 static void c_parser_statement (c_parser *);
987 static void c_parser_statement_after_labels (c_parser *);
988 static void c_parser_if_statement (c_parser *);
989 static void c_parser_switch_statement (c_parser *);
990 static void c_parser_while_statement (c_parser *);
991 static void c_parser_do_statement (c_parser *);
992 static void c_parser_for_statement (c_parser *);
993 static tree c_parser_asm_statement (c_parser *);
994 static tree c_parser_asm_operands (c_parser *, bool);
995 static tree c_parser_asm_clobbers (c_parser *);
996 static struct c_expr c_parser_expr_no_commas (c_parser *, struct c_expr *);
997 static struct c_expr c_parser_conditional_expression (c_parser *,
998 struct c_expr *);
999 static struct c_expr c_parser_binary_expression (c_parser *, struct c_expr *);
1000 static struct c_expr c_parser_cast_expression (c_parser *, struct c_expr *);
1001 static struct c_expr c_parser_unary_expression (c_parser *);
1002 static struct c_expr c_parser_sizeof_expression (c_parser *);
1003 static struct c_expr c_parser_alignof_expression (c_parser *);
1004 static struct c_expr c_parser_postfix_expression (c_parser *);
1005 static struct c_expr c_parser_postfix_expression_after_paren_type (c_parser *,
1006 struct c_type_name *);
1007 static struct c_expr c_parser_postfix_expression_after_primary (c_parser *,
1008 struct c_expr);
1009 static struct c_expr c_parser_expression (c_parser *);
1010 static struct c_expr c_parser_expression_conv (c_parser *);
1011 static tree c_parser_expr_list (c_parser *, bool);
1012 static void c_parser_omp_construct (c_parser *);
1013 static void c_parser_omp_threadprivate (c_parser *);
1014 static void c_parser_omp_barrier (c_parser *);
1015 static void c_parser_omp_flush (c_parser *);
1017 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
1018 static bool c_parser_pragma (c_parser *, enum pragma_context);
1020 /* These Objective-C parser functions are only ever called when
1021 compiling Objective-C. */
1022 static void c_parser_objc_class_definition (c_parser *);
1023 static void c_parser_objc_class_instance_variables (c_parser *);
1024 static void c_parser_objc_class_declaration (c_parser *);
1025 static void c_parser_objc_alias_declaration (c_parser *);
1026 static void c_parser_objc_protocol_definition (c_parser *);
1027 static enum tree_code c_parser_objc_method_type (c_parser *);
1028 static void c_parser_objc_method_definition (c_parser *);
1029 static void c_parser_objc_methodprotolist (c_parser *);
1030 static void c_parser_objc_methodproto (c_parser *);
1031 static tree c_parser_objc_method_decl (c_parser *);
1032 static tree c_parser_objc_type_name (c_parser *);
1033 static tree c_parser_objc_protocol_refs (c_parser *);
1034 static void c_parser_objc_try_catch_statement (c_parser *);
1035 static void c_parser_objc_synchronized_statement (c_parser *);
1036 static tree c_parser_objc_selector (c_parser *);
1037 static tree c_parser_objc_selector_arg (c_parser *);
1038 static tree c_parser_objc_receiver (c_parser *);
1039 static tree c_parser_objc_message_args (c_parser *);
1040 static tree c_parser_objc_keywordexpr (c_parser *);
1042 /* Parse a translation unit (C90 6.7, C99 6.9).
1044 translation-unit:
1045 external-declarations
1047 external-declarations:
1048 external-declaration
1049 external-declarations external-declaration
1051 GNU extensions:
1053 translation-unit:
1054 empty
1057 static void
1058 c_parser_translation_unit (c_parser *parser)
1060 if (c_parser_next_token_is (parser, CPP_EOF))
1062 if (pedantic)
1063 pedwarn ("ISO C forbids an empty source file");
1065 else
1067 void *obstack_position = obstack_alloc (&parser_obstack, 0);
1070 ggc_collect ();
1071 c_parser_external_declaration (parser);
1072 obstack_free (&parser_obstack, obstack_position);
1074 while (c_parser_next_token_is_not (parser, CPP_EOF));
1078 /* Parse an external declaration (C90 6.7, C99 6.9).
1080 external-declaration:
1081 function-definition
1082 declaration
1084 GNU extensions:
1086 external-declaration:
1087 asm-definition
1089 __extension__ external-declaration
1091 Objective-C:
1093 external-declaration:
1094 objc-class-definition
1095 objc-class-declaration
1096 objc-alias-declaration
1097 objc-protocol-definition
1098 objc-method-definition
1099 @end
1102 static void
1103 c_parser_external_declaration (c_parser *parser)
1105 int ext;
1106 switch (c_parser_peek_token (parser)->type)
1108 case CPP_KEYWORD:
1109 switch (c_parser_peek_token (parser)->keyword)
1111 case RID_EXTENSION:
1112 ext = disable_extension_diagnostics ();
1113 c_parser_consume_token (parser);
1114 c_parser_external_declaration (parser);
1115 restore_extension_diagnostics (ext);
1116 break;
1117 case RID_ASM:
1118 c_parser_asm_definition (parser);
1119 break;
1120 case RID_AT_INTERFACE:
1121 case RID_AT_IMPLEMENTATION:
1122 gcc_assert (c_dialect_objc ());
1123 c_parser_objc_class_definition (parser);
1124 break;
1125 case RID_AT_CLASS:
1126 gcc_assert (c_dialect_objc ());
1127 c_parser_objc_class_declaration (parser);
1128 break;
1129 case RID_AT_ALIAS:
1130 gcc_assert (c_dialect_objc ());
1131 c_parser_objc_alias_declaration (parser);
1132 break;
1133 case RID_AT_PROTOCOL:
1134 gcc_assert (c_dialect_objc ());
1135 c_parser_objc_protocol_definition (parser);
1136 break;
1137 case RID_AT_END:
1138 gcc_assert (c_dialect_objc ());
1139 c_parser_consume_token (parser);
1140 objc_finish_implementation ();
1141 break;
1142 default:
1143 goto decl_or_fndef;
1145 break;
1146 case CPP_SEMICOLON:
1147 if (pedantic)
1148 pedwarn ("ISO C does not allow extra %<;%> outside of a function");
1149 c_parser_consume_token (parser);
1150 break;
1151 case CPP_PRAGMA:
1152 c_parser_pragma (parser, pragma_external);
1153 break;
1154 case CPP_PLUS:
1155 case CPP_MINUS:
1156 if (c_dialect_objc ())
1158 c_parser_objc_method_definition (parser);
1159 break;
1161 /* Else fall through, and yield a syntax error trying to parse
1162 as a declaration or function definition. */
1163 default:
1164 decl_or_fndef:
1165 /* A declaration or a function definition. We can only tell
1166 which after parsing the declaration specifiers, if any, and
1167 the first declarator. */
1168 c_parser_declaration_or_fndef (parser, true, true, false, true);
1169 break;
1174 /* Parse a declaration or function definition (C90 6.5, 6.7.1, C99
1175 6.7, 6.9.1). If FNDEF_OK is true, a function definition is
1176 accepted; otherwise (old-style parameter declarations) only other
1177 declarations are accepted. If NESTED is true, we are inside a
1178 function or parsing old-style parameter declarations; any functions
1179 encountered are nested functions and declaration specifiers are
1180 required; otherwise we are at top level and functions are normal
1181 functions and declaration specifiers may be optional. If EMPTY_OK
1182 is true, empty declarations are OK (subject to all other
1183 constraints); otherwise (old-style parameter declarations) they are
1184 diagnosed. If START_ATTR_OK is true, the declaration specifiers
1185 may start with attributes; otherwise they may not.
1187 declaration:
1188 declaration-specifiers init-declarator-list[opt] ;
1190 function-definition:
1191 declaration-specifiers[opt] declarator declaration-list[opt]
1192 compound-statement
1194 declaration-list:
1195 declaration
1196 declaration-list declaration
1198 init-declarator-list:
1199 init-declarator
1200 init-declarator-list , init-declarator
1202 init-declarator:
1203 declarator simple-asm-expr[opt] attributes[opt]
1204 declarator simple-asm-expr[opt] attributes[opt] = initializer
1206 GNU extensions:
1208 nested-function-definition:
1209 declaration-specifiers declarator declaration-list[opt]
1210 compound-statement
1212 The simple-asm-expr and attributes are GNU extensions.
1214 This function does not handle __extension__; that is handled in its
1215 callers. ??? Following the old parser, __extension__ may start
1216 external declarations, declarations in functions and declarations
1217 at the start of "for" loops, but not old-style parameter
1218 declarations.
1220 C99 requires declaration specifiers in a function definition; the
1221 absence is diagnosed through the diagnosis of implicit int. In GNU
1222 C we also allow but diagnose declarations without declaration
1223 specifiers, but only at top level (elsewhere they conflict with
1224 other syntax).
1226 OpenMP:
1228 declaration:
1229 threadprivate-directive */
1231 static void
1232 c_parser_declaration_or_fndef (c_parser *parser, bool fndef_ok, bool empty_ok,
1233 bool nested, bool start_attr_ok)
1235 struct c_declspecs *specs;
1236 tree prefix_attrs;
1237 tree all_prefix_attrs;
1238 bool diagnosed_no_specs = false;
1240 specs = build_null_declspecs ();
1241 c_parser_declspecs (parser, specs, true, true, start_attr_ok);
1242 if (parser->error)
1244 c_parser_skip_to_end_of_block_or_statement (parser);
1245 return;
1247 if (nested && !specs->declspecs_seen_p)
1249 c_parser_error (parser, "expected declaration specifiers");
1250 c_parser_skip_to_end_of_block_or_statement (parser);
1251 return;
1253 finish_declspecs (specs);
1254 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1256 if (empty_ok)
1257 shadow_tag (specs);
1258 else
1260 shadow_tag_warned (specs, 1);
1261 pedwarn ("empty declaration");
1263 c_parser_consume_token (parser);
1264 return;
1266 pending_xref_error ();
1267 prefix_attrs = specs->attrs;
1268 all_prefix_attrs = prefix_attrs;
1269 specs->attrs = NULL_TREE;
1270 while (true)
1272 struct c_declarator *declarator;
1273 bool dummy = false;
1274 tree fnbody;
1275 /* Declaring either one or more declarators (in which case we
1276 should diagnose if there were no declaration specifiers) or a
1277 function definition (in which case the diagnostic for
1278 implicit int suffices). */
1279 declarator = c_parser_declarator (parser, specs->type_seen_p,
1280 C_DTR_NORMAL, &dummy);
1281 if (declarator == NULL)
1283 c_parser_skip_to_end_of_block_or_statement (parser);
1284 return;
1286 if (c_parser_next_token_is (parser, CPP_EQ)
1287 || c_parser_next_token_is (parser, CPP_COMMA)
1288 || c_parser_next_token_is (parser, CPP_SEMICOLON)
1289 || c_parser_next_token_is_keyword (parser, RID_ASM)
1290 || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1292 tree asm_name = NULL_TREE;
1293 tree postfix_attrs = NULL_TREE;
1294 if (!diagnosed_no_specs && !specs->declspecs_seen_p)
1296 diagnosed_no_specs = true;
1297 pedwarn ("data definition has no type or storage class");
1299 /* Having seen a data definition, there cannot now be a
1300 function definition. */
1301 fndef_ok = false;
1302 if (c_parser_next_token_is_keyword (parser, RID_ASM))
1303 asm_name = c_parser_simple_asm_expr (parser);
1304 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1305 postfix_attrs = c_parser_attributes (parser);
1306 if (c_parser_next_token_is (parser, CPP_EQ))
1308 tree d;
1309 struct c_expr init;
1310 c_parser_consume_token (parser);
1311 /* The declaration of the variable is in effect while
1312 its initializer is parsed. */
1313 d = start_decl (declarator, specs, true,
1314 chainon (postfix_attrs, all_prefix_attrs));
1315 if (!d)
1316 d = error_mark_node;
1317 start_init (d, asm_name, global_bindings_p ());
1318 init = c_parser_initializer (parser);
1319 finish_init ();
1320 if (d != error_mark_node)
1322 maybe_warn_string_init (TREE_TYPE (d), init);
1323 finish_decl (d, init.value, asm_name);
1326 else
1328 tree d = start_decl (declarator, specs, false,
1329 chainon (postfix_attrs,
1330 all_prefix_attrs));
1331 if (d)
1332 finish_decl (d, NULL_TREE, asm_name);
1334 if (c_parser_next_token_is (parser, CPP_COMMA))
1336 c_parser_consume_token (parser);
1337 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1338 all_prefix_attrs = chainon (c_parser_attributes (parser),
1339 prefix_attrs);
1340 else
1341 all_prefix_attrs = prefix_attrs;
1342 continue;
1344 else if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1346 c_parser_consume_token (parser);
1347 return;
1349 else
1351 c_parser_error (parser, "expected %<,%> or %<;%>");
1352 c_parser_skip_to_end_of_block_or_statement (parser);
1353 return;
1356 else if (!fndef_ok)
1358 c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, "
1359 "%<asm%> or %<__attribute__%>");
1360 c_parser_skip_to_end_of_block_or_statement (parser);
1361 return;
1363 /* Function definition (nested or otherwise). */
1364 if (nested)
1366 if (pedantic)
1367 pedwarn ("ISO C forbids nested functions");
1368 push_function_context ();
1370 if (!start_function (specs, declarator, all_prefix_attrs))
1372 /* This can appear in many cases looking nothing like a
1373 function definition, so we don't give a more specific
1374 error suggesting there was one. */
1375 c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, %<asm%> "
1376 "or %<__attribute__%>");
1377 if (nested)
1378 pop_function_context ();
1379 break;
1381 /* Parse old-style parameter declarations. ??? Attributes are
1382 not allowed to start declaration specifiers here because of a
1383 syntax conflict between a function declaration with attribute
1384 suffix and a function definition with an attribute prefix on
1385 first old-style parameter declaration. Following the old
1386 parser, they are not accepted on subsequent old-style
1387 parameter declarations either. However, there is no
1388 ambiguity after the first declaration, nor indeed on the
1389 first as long as we don't allow postfix attributes after a
1390 declarator with a nonempty identifier list in a definition;
1391 and postfix attributes have never been accepted here in
1392 function definitions either. */
1393 while (c_parser_next_token_is_not (parser, CPP_EOF)
1394 && c_parser_next_token_is_not (parser, CPP_OPEN_BRACE))
1395 c_parser_declaration_or_fndef (parser, false, false, true, false);
1396 DECL_SOURCE_LOCATION (current_function_decl)
1397 = c_parser_peek_token (parser)->location;
1398 store_parm_decls ();
1399 fnbody = c_parser_compound_statement (parser);
1400 if (nested)
1402 tree decl = current_function_decl;
1403 add_stmt (fnbody);
1404 finish_function ();
1405 pop_function_context ();
1406 add_stmt (build_stmt (DECL_EXPR, decl));
1408 else
1410 add_stmt (fnbody);
1411 finish_function ();
1413 break;
1417 /* Parse an asm-definition (asm() outside a function body). This is a
1418 GNU extension.
1420 asm-definition:
1421 simple-asm-expr ;
1424 static void
1425 c_parser_asm_definition (c_parser *parser)
1427 tree asm_str = c_parser_simple_asm_expr (parser);
1428 if (asm_str)
1429 cgraph_add_asm_node (asm_str);
1430 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
1433 /* Parse some declaration specifiers (possibly none) (C90 6.5, C99
1434 6.7), adding them to SPECS (which may already include some).
1435 Storage class specifiers are accepted iff SCSPEC_OK; type
1436 specifiers are accepted iff TYPESPEC_OK; attributes are accepted at
1437 the start iff START_ATTR_OK.
1439 declaration-specifiers:
1440 storage-class-specifier declaration-specifiers[opt]
1441 type-specifier declaration-specifiers[opt]
1442 type-qualifier declaration-specifiers[opt]
1443 function-specifier declaration-specifiers[opt]
1445 Function specifiers (inline) are from C99, and are currently
1446 handled as storage class specifiers, as is __thread.
1448 C90 6.5.1, C99 6.7.1:
1449 storage-class-specifier:
1450 typedef
1451 extern
1452 static
1453 auto
1454 register
1456 C99 6.7.4:
1457 function-specifier:
1458 inline
1460 C90 6.5.2, C99 6.7.2:
1461 type-specifier:
1462 void
1463 char
1464 short
1466 long
1467 float
1468 double
1469 signed
1470 unsigned
1471 _Bool
1472 _Complex
1473 [_Imaginary removed in C99 TC2]
1474 struct-or-union-specifier
1475 enum-specifier
1476 typedef-name
1478 (_Bool and _Complex are new in C99.)
1480 C90 6.5.3, C99 6.7.3:
1482 type-qualifier:
1483 const
1484 restrict
1485 volatile
1487 (restrict is new in C99.)
1489 GNU extensions:
1491 declaration-specifiers:
1492 attributes declaration-specifiers[opt]
1494 storage-class-specifier:
1495 __thread
1497 type-specifier:
1498 typeof-specifier
1499 _Decimal32
1500 _Decimal64
1501 _Decimal128
1503 Objective-C:
1505 type-specifier:
1506 class-name objc-protocol-refs[opt]
1507 typedef-name objc-protocol-refs
1508 objc-protocol-refs
1511 static void
1512 c_parser_declspecs (c_parser *parser, struct c_declspecs *specs,
1513 bool scspec_ok, bool typespec_ok, bool start_attr_ok)
1515 bool attrs_ok = start_attr_ok;
1516 bool seen_type = specs->type_seen_p;
1517 while (c_parser_next_token_is (parser, CPP_NAME)
1518 || c_parser_next_token_is (parser, CPP_KEYWORD)
1519 || (c_dialect_objc () && c_parser_next_token_is (parser, CPP_LESS)))
1521 struct c_typespec t;
1522 tree attrs;
1523 if (c_parser_next_token_is (parser, CPP_NAME))
1525 tree value = c_parser_peek_token (parser)->value;
1526 c_id_kind kind = c_parser_peek_token (parser)->id_kind;
1527 /* This finishes the specifiers unless a type name is OK, it
1528 is declared as a type name and a type name hasn't yet
1529 been seen. */
1530 if (!typespec_ok || seen_type
1531 || (kind != C_ID_TYPENAME && kind != C_ID_CLASSNAME))
1532 break;
1533 c_parser_consume_token (parser);
1534 seen_type = true;
1535 attrs_ok = true;
1536 if (kind == C_ID_TYPENAME
1537 && (!c_dialect_objc ()
1538 || c_parser_next_token_is_not (parser, CPP_LESS)))
1540 t.kind = ctsk_typedef;
1541 /* For a typedef name, record the meaning, not the name.
1542 In case of 'foo foo, bar;'. */
1543 t.spec = lookup_name (value);
1545 else
1547 tree proto = NULL_TREE;
1548 gcc_assert (c_dialect_objc ());
1549 t.kind = ctsk_objc;
1550 if (c_parser_next_token_is (parser, CPP_LESS))
1551 proto = c_parser_objc_protocol_refs (parser);
1552 t.spec = objc_get_protocol_qualified_type (value, proto);
1554 declspecs_add_type (specs, t);
1555 continue;
1557 if (c_parser_next_token_is (parser, CPP_LESS))
1559 /* Make "<SomeProtocol>" equivalent to "id <SomeProtocol>" -
1560 nisse@lysator.liu.se. */
1561 tree proto;
1562 gcc_assert (c_dialect_objc ());
1563 if (!typespec_ok || seen_type)
1564 break;
1565 proto = c_parser_objc_protocol_refs (parser);
1566 t.kind = ctsk_objc;
1567 t.spec = objc_get_protocol_qualified_type (NULL_TREE, proto);
1568 declspecs_add_type (specs, t);
1569 continue;
1571 gcc_assert (c_parser_next_token_is (parser, CPP_KEYWORD));
1572 switch (c_parser_peek_token (parser)->keyword)
1574 case RID_STATIC:
1575 case RID_EXTERN:
1576 case RID_REGISTER:
1577 case RID_TYPEDEF:
1578 case RID_INLINE:
1579 case RID_AUTO:
1580 case RID_THREAD:
1581 if (!scspec_ok)
1582 goto out;
1583 attrs_ok = true;
1584 /* TODO: Distinguish between function specifiers (inline)
1585 and storage class specifiers, either here or in
1586 declspecs_add_scspec. */
1587 declspecs_add_scspec (specs, c_parser_peek_token (parser)->value);
1588 c_parser_consume_token (parser);
1589 break;
1590 case RID_UNSIGNED:
1591 case RID_LONG:
1592 case RID_SHORT:
1593 case RID_SIGNED:
1594 case RID_COMPLEX:
1595 case RID_INT:
1596 case RID_CHAR:
1597 case RID_FLOAT:
1598 case RID_DOUBLE:
1599 case RID_VOID:
1600 case RID_DFLOAT32:
1601 case RID_DFLOAT64:
1602 case RID_DFLOAT128:
1603 case RID_BOOL:
1604 if (!typespec_ok)
1605 goto out;
1606 attrs_ok = true;
1607 seen_type = true;
1608 OBJC_NEED_RAW_IDENTIFIER (1);
1609 t.kind = ctsk_resword;
1610 t.spec = c_parser_peek_token (parser)->value;
1611 declspecs_add_type (specs, t);
1612 c_parser_consume_token (parser);
1613 break;
1614 case RID_ENUM:
1615 if (!typespec_ok)
1616 goto out;
1617 attrs_ok = true;
1618 seen_type = true;
1619 t = c_parser_enum_specifier (parser);
1620 declspecs_add_type (specs, t);
1621 break;
1622 case RID_STRUCT:
1623 case RID_UNION:
1624 if (!typespec_ok)
1625 goto out;
1626 attrs_ok = true;
1627 seen_type = true;
1628 t = c_parser_struct_or_union_specifier (parser);
1629 declspecs_add_type (specs, t);
1630 break;
1631 case RID_TYPEOF:
1632 /* ??? The old parser rejected typeof after other type
1633 specifiers, but is a syntax error the best way of
1634 handling this? */
1635 if (!typespec_ok || seen_type)
1636 goto out;
1637 attrs_ok = true;
1638 seen_type = true;
1639 t = c_parser_typeof_specifier (parser);
1640 declspecs_add_type (specs, t);
1641 break;
1642 case RID_CONST:
1643 case RID_VOLATILE:
1644 case RID_RESTRICT:
1645 attrs_ok = true;
1646 declspecs_add_qual (specs, c_parser_peek_token (parser)->value);
1647 c_parser_consume_token (parser);
1648 break;
1649 case RID_ATTRIBUTE:
1650 if (!attrs_ok)
1651 goto out;
1652 attrs = c_parser_attributes (parser);
1653 declspecs_add_attrs (specs, attrs);
1654 break;
1655 default:
1656 goto out;
1659 out: ;
1662 /* Parse an enum specifier (C90 6.5.2.2, C99 6.7.2.2).
1664 enum-specifier:
1665 enum attributes[opt] identifier[opt] { enumerator-list } attributes[opt]
1666 enum attributes[opt] identifier[opt] { enumerator-list , } attributes[opt]
1667 enum attributes[opt] identifier
1669 The form with trailing comma is new in C99. The forms with
1670 attributes are GNU extensions. In GNU C, we accept any expression
1671 without commas in the syntax (assignment expressions, not just
1672 conditional expressions); assignment expressions will be diagnosed
1673 as non-constant.
1675 enumerator-list:
1676 enumerator
1677 enumerator-list , enumerator
1679 enumerator:
1680 enumeration-constant
1681 enumeration-constant = constant-expression
1684 static struct c_typespec
1685 c_parser_enum_specifier (c_parser *parser)
1687 struct c_typespec ret;
1688 tree attrs;
1689 tree ident = NULL_TREE;
1690 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ENUM));
1691 c_parser_consume_token (parser);
1692 attrs = c_parser_attributes (parser);
1693 if (c_parser_next_token_is (parser, CPP_NAME))
1695 ident = c_parser_peek_token (parser)->value;
1696 c_parser_consume_token (parser);
1698 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
1700 /* Parse an enum definition. */
1701 tree type = start_enum (ident);
1702 tree postfix_attrs;
1703 /* We chain the enumerators in reverse order, then put them in
1704 forward order at the end. */
1705 tree values = NULL_TREE;
1706 c_parser_consume_token (parser);
1707 while (true)
1709 tree enum_id;
1710 tree enum_value;
1711 tree enum_decl;
1712 bool seen_comma;
1713 if (c_parser_next_token_is_not (parser, CPP_NAME))
1715 c_parser_error (parser, "expected identifier");
1716 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
1717 values = error_mark_node;
1718 break;
1720 enum_id = c_parser_peek_token (parser)->value;
1721 c_parser_consume_token (parser);
1722 if (c_parser_next_token_is (parser, CPP_EQ))
1724 c_parser_consume_token (parser);
1725 enum_value = c_parser_expr_no_commas (parser, NULL).value;
1727 else
1728 enum_value = NULL_TREE;
1729 enum_decl = build_enumerator (enum_id, enum_value);
1730 TREE_CHAIN (enum_decl) = values;
1731 values = enum_decl;
1732 seen_comma = false;
1733 if (c_parser_next_token_is (parser, CPP_COMMA))
1735 seen_comma = true;
1736 c_parser_consume_token (parser);
1738 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
1740 if (seen_comma && pedantic && !flag_isoc99)
1741 pedwarn ("comma at end of enumerator list");
1742 c_parser_consume_token (parser);
1743 break;
1745 if (!seen_comma)
1747 c_parser_error (parser, "expected %<,%> or %<}%>");
1748 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
1749 values = error_mark_node;
1750 break;
1753 postfix_attrs = c_parser_attributes (parser);
1754 ret.spec = finish_enum (type, nreverse (values),
1755 chainon (attrs, postfix_attrs));
1756 ret.kind = ctsk_tagdef;
1757 return ret;
1759 else if (!ident)
1761 c_parser_error (parser, "expected %<{%>");
1762 ret.spec = error_mark_node;
1763 ret.kind = ctsk_tagref;
1764 return ret;
1766 ret = parser_xref_tag (ENUMERAL_TYPE, ident);
1767 /* In ISO C, enumerated types can be referred to only if already
1768 defined. */
1769 if (pedantic && !COMPLETE_TYPE_P (ret.spec))
1770 pedwarn ("ISO C forbids forward references to %<enum%> types");
1771 return ret;
1774 /* Parse a struct or union specifier (C90 6.5.2.1, C99 6.7.2.1).
1776 struct-or-union-specifier:
1777 struct-or-union attributes[opt] identifier[opt]
1778 { struct-contents } attributes[opt]
1779 struct-or-union attributes[opt] identifier
1781 struct-contents:
1782 struct-declaration-list
1784 struct-declaration-list:
1785 struct-declaration ;
1786 struct-declaration-list struct-declaration ;
1788 GNU extensions:
1790 struct-contents:
1791 empty
1792 struct-declaration
1793 struct-declaration-list struct-declaration
1795 struct-declaration-list:
1796 struct-declaration-list ;
1799 (Note that in the syntax here, unlike that in ISO C, the semicolons
1800 are included here rather than in struct-declaration, in order to
1801 describe the syntax with extra semicolons and missing semicolon at
1802 end.)
1804 Objective-C:
1806 struct-declaration-list:
1807 @defs ( class-name )
1809 (Note this does not include a trailing semicolon, but can be
1810 followed by further declarations, and gets a pedwarn-if-pedantic
1811 when followed by a semicolon.) */
1813 static struct c_typespec
1814 c_parser_struct_or_union_specifier (c_parser *parser)
1816 struct c_typespec ret;
1817 tree attrs;
1818 tree ident = NULL_TREE;
1819 enum tree_code code;
1820 switch (c_parser_peek_token (parser)->keyword)
1822 case RID_STRUCT:
1823 code = RECORD_TYPE;
1824 break;
1825 case RID_UNION:
1826 code = UNION_TYPE;
1827 break;
1828 default:
1829 gcc_unreachable ();
1831 c_parser_consume_token (parser);
1832 attrs = c_parser_attributes (parser);
1833 if (c_parser_next_token_is (parser, CPP_NAME))
1835 ident = c_parser_peek_token (parser)->value;
1836 c_parser_consume_token (parser);
1838 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
1840 /* Parse a struct or union definition. Start the scope of the
1841 tag before parsing components. */
1842 tree type = start_struct (code, ident);
1843 tree postfix_attrs;
1844 /* We chain the components in reverse order, then put them in
1845 forward order at the end. Each struct-declaration may
1846 declare multiple components (comma-separated), so we must use
1847 chainon to join them, although when parsing each
1848 struct-declaration we can use TREE_CHAIN directly.
1850 The theory behind all this is that there will be more
1851 semicolon separated fields than comma separated fields, and
1852 so we'll be minimizing the number of node traversals required
1853 by chainon. */
1854 tree contents = NULL_TREE;
1855 c_parser_consume_token (parser);
1856 /* Handle the Objective-C @defs construct,
1857 e.g. foo(sizeof(struct{ @defs(ClassName) }));. */
1858 if (c_parser_next_token_is_keyword (parser, RID_AT_DEFS))
1860 tree name;
1861 gcc_assert (c_dialect_objc ());
1862 c_parser_consume_token (parser);
1863 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
1864 goto end_at_defs;
1865 if (c_parser_next_token_is (parser, CPP_NAME)
1866 && c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME)
1868 name = c_parser_peek_token (parser)->value;
1869 c_parser_consume_token (parser);
1871 else
1873 c_parser_error (parser, "expected class name");
1874 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
1875 goto end_at_defs;
1877 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
1878 "expected %<)%>");
1879 contents = nreverse (objc_get_class_ivars (name));
1881 end_at_defs:
1882 /* Parse the struct-declarations and semicolons. Problems with
1883 semicolons are diagnosed here; empty structures are diagnosed
1884 elsewhere. */
1885 while (true)
1887 tree decls;
1888 /* Parse any stray semicolon. */
1889 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1891 if (pedantic)
1892 pedwarn ("extra semicolon in struct or union specified");
1893 c_parser_consume_token (parser);
1894 continue;
1896 /* Stop if at the end of the struct or union contents. */
1897 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
1899 c_parser_consume_token (parser);
1900 break;
1902 /* Accept #pragmas at struct scope. */
1903 if (c_parser_next_token_is (parser, CPP_PRAGMA))
1905 c_parser_pragma (parser, pragma_external);
1906 continue;
1908 /* Parse some comma-separated declarations, but not the
1909 trailing semicolon if any. */
1910 decls = c_parser_struct_declaration (parser);
1911 contents = chainon (decls, contents);
1912 /* If no semicolon follows, either we have a parse error or
1913 are at the end of the struct or union and should
1914 pedwarn. */
1915 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1916 c_parser_consume_token (parser);
1917 else
1919 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
1920 pedwarn ("no semicolon at end of struct or union");
1921 else
1923 c_parser_error (parser, "expected %<;%>");
1924 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
1925 break;
1929 postfix_attrs = c_parser_attributes (parser);
1930 ret.spec = finish_struct (type, nreverse (contents),
1931 chainon (attrs, postfix_attrs));
1932 ret.kind = ctsk_tagdef;
1933 return ret;
1935 else if (!ident)
1937 c_parser_error (parser, "expected %<{%>");
1938 ret.spec = error_mark_node;
1939 ret.kind = ctsk_tagref;
1940 return ret;
1942 ret = parser_xref_tag (code, ident);
1943 return ret;
1946 /* Parse a struct-declaration (C90 6.5.2.1, C99 6.7.2.1), *without*
1947 the trailing semicolon.
1949 struct-declaration:
1950 specifier-qualifier-list struct-declarator-list
1952 specifier-qualifier-list:
1953 type-specifier specifier-qualifier-list[opt]
1954 type-qualifier specifier-qualifier-list[opt]
1955 attributes specifier-qualifier-list[opt]
1957 struct-declarator-list:
1958 struct-declarator
1959 struct-declarator-list , attributes[opt] struct-declarator
1961 struct-declarator:
1962 declarator attributes[opt]
1963 declarator[opt] : constant-expression attributes[opt]
1965 GNU extensions:
1967 struct-declaration:
1968 __extension__ struct-declaration
1969 specifier-qualifier-list
1971 Unlike the ISO C syntax, semicolons are handled elsewhere. The use
1972 of attributes where shown is a GNU extension. In GNU C, we accept
1973 any expression without commas in the syntax (assignment
1974 expressions, not just conditional expressions); assignment
1975 expressions will be diagnosed as non-constant. */
1977 static tree
1978 c_parser_struct_declaration (c_parser *parser)
1980 struct c_declspecs *specs;
1981 tree prefix_attrs;
1982 tree all_prefix_attrs;
1983 tree decls;
1984 if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
1986 int ext;
1987 tree decl;
1988 ext = disable_extension_diagnostics ();
1989 c_parser_consume_token (parser);
1990 decl = c_parser_struct_declaration (parser);
1991 restore_extension_diagnostics (ext);
1992 return decl;
1994 specs = build_null_declspecs ();
1995 c_parser_declspecs (parser, specs, false, true, true);
1996 if (parser->error)
1997 return NULL_TREE;
1998 if (!specs->declspecs_seen_p)
2000 c_parser_error (parser, "expected specifier-qualifier-list");
2001 return NULL_TREE;
2003 finish_declspecs (specs);
2004 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
2006 tree ret;
2007 if (!specs->type_seen_p)
2009 if (pedantic)
2010 pedwarn ("ISO C forbids member declarations with no members");
2011 shadow_tag_warned (specs, pedantic);
2012 ret = NULL_TREE;
2014 else
2016 /* Support for unnamed structs or unions as members of
2017 structs or unions (which is [a] useful and [b] supports
2018 MS P-SDK). */
2019 ret = grokfield (build_id_declarator (NULL_TREE), specs, NULL_TREE);
2021 return ret;
2023 pending_xref_error ();
2024 prefix_attrs = specs->attrs;
2025 all_prefix_attrs = prefix_attrs;
2026 specs->attrs = NULL_TREE;
2027 decls = NULL_TREE;
2028 while (true)
2030 /* Declaring one or more declarators or un-named bit-fields. */
2031 struct c_declarator *declarator;
2032 bool dummy = false;
2033 if (c_parser_next_token_is (parser, CPP_COLON))
2034 declarator = build_id_declarator (NULL_TREE);
2035 else
2036 declarator = c_parser_declarator (parser, specs->type_seen_p,
2037 C_DTR_NORMAL, &dummy);
2038 if (declarator == NULL)
2040 c_parser_skip_to_end_of_block_or_statement (parser);
2041 break;
2043 if (c_parser_next_token_is (parser, CPP_COLON)
2044 || c_parser_next_token_is (parser, CPP_COMMA)
2045 || c_parser_next_token_is (parser, CPP_SEMICOLON)
2046 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE)
2047 || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2049 tree postfix_attrs = NULL_TREE;
2050 tree width = NULL_TREE;
2051 tree d;
2052 if (c_parser_next_token_is (parser, CPP_COLON))
2054 c_parser_consume_token (parser);
2055 width = c_parser_expr_no_commas (parser, NULL).value;
2057 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2058 postfix_attrs = c_parser_attributes (parser);
2059 d = grokfield (declarator, specs, width);
2060 decl_attributes (&d, chainon (postfix_attrs,
2061 all_prefix_attrs), 0);
2062 TREE_CHAIN (d) = decls;
2063 decls = d;
2064 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2065 all_prefix_attrs = chainon (c_parser_attributes (parser),
2066 prefix_attrs);
2067 else
2068 all_prefix_attrs = prefix_attrs;
2069 if (c_parser_next_token_is (parser, CPP_COMMA))
2070 c_parser_consume_token (parser);
2071 else if (c_parser_next_token_is (parser, CPP_SEMICOLON)
2072 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2074 /* Semicolon consumed in caller. */
2075 break;
2077 else
2079 c_parser_error (parser, "expected %<,%>, %<;%> or %<}%>");
2080 break;
2083 else
2085 c_parser_error (parser,
2086 "expected %<:%>, %<,%>, %<;%>, %<}%> or "
2087 "%<__attribute__%>");
2088 break;
2091 return decls;
2094 /* Parse a typeof specifier (a GNU extension).
2096 typeof-specifier:
2097 typeof ( expression )
2098 typeof ( type-name )
2101 static struct c_typespec
2102 c_parser_typeof_specifier (c_parser *parser)
2104 struct c_typespec ret;
2105 ret.kind = ctsk_typeof;
2106 ret.spec = error_mark_node;
2107 gcc_assert (c_parser_next_token_is_keyword (parser, RID_TYPEOF));
2108 c_parser_consume_token (parser);
2109 skip_evaluation++;
2110 in_typeof++;
2111 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2113 skip_evaluation--;
2114 in_typeof--;
2115 return ret;
2117 if (c_parser_next_token_starts_typename (parser))
2119 struct c_type_name *type = c_parser_type_name (parser);
2120 skip_evaluation--;
2121 in_typeof--;
2122 if (type != NULL)
2124 ret.spec = groktypename (type);
2125 pop_maybe_used (variably_modified_type_p (ret.spec, NULL_TREE));
2128 else
2130 bool was_vm;
2131 struct c_expr expr = c_parser_expression (parser);
2132 skip_evaluation--;
2133 in_typeof--;
2134 if (TREE_CODE (expr.value) == COMPONENT_REF
2135 && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1)))
2136 error ("%<typeof%> applied to a bit-field");
2137 ret.spec = TREE_TYPE (expr.value);
2138 was_vm = variably_modified_type_p (ret.spec, NULL_TREE);
2139 /* This should be returned with the type so that when the type
2140 is evaluated, this can be evaluated. For now, we avoid
2141 evaluation when the context might. */
2142 if (!skip_evaluation && was_vm)
2144 tree e = expr.value;
2146 /* If the expression is not of a type to which we cannot assign a line
2147 number, wrap the thing in a no-op NOP_EXPR. */
2148 if (DECL_P (e) || CONSTANT_CLASS_P (e))
2149 e = build1 (NOP_EXPR, void_type_node, e);
2151 if (CAN_HAVE_LOCATION_P (e))
2152 SET_EXPR_LOCATION (e, input_location);
2154 add_stmt (e);
2156 pop_maybe_used (was_vm);
2158 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
2159 return ret;
2162 /* Parse a declarator, possibly an abstract declarator (C90 6.5.4,
2163 6.5.5, C99 6.7.5, 6.7.6). If TYPE_SEEN_P then a typedef name may
2164 be redeclared; otherwise it may not. KIND indicates which kind of
2165 declarator is wanted. Returns a valid declarator except in the
2166 case of a syntax error in which case NULL is returned. *SEEN_ID is
2167 set to true if an identifier being declared is seen; this is used
2168 to diagnose bad forms of abstract array declarators and to
2169 determine whether an identifier list is syntactically permitted.
2171 declarator:
2172 pointer[opt] direct-declarator
2174 direct-declarator:
2175 identifier
2176 ( attributes[opt] declarator )
2177 direct-declarator array-declarator
2178 direct-declarator ( parameter-type-list )
2179 direct-declarator ( identifier-list[opt] )
2181 pointer:
2182 * type-qualifier-list[opt]
2183 * type-qualifier-list[opt] pointer
2185 type-qualifier-list:
2186 type-qualifier
2187 attributes
2188 type-qualifier-list type-qualifier
2189 type-qualifier-list attributes
2191 parameter-type-list:
2192 parameter-list
2193 parameter-list , ...
2195 parameter-list:
2196 parameter-declaration
2197 parameter-list , parameter-declaration
2199 parameter-declaration:
2200 declaration-specifiers declarator attributes[opt]
2201 declaration-specifiers abstract-declarator[opt] attributes[opt]
2203 identifier-list:
2204 identifier
2205 identifier-list , identifier
2207 abstract-declarator:
2208 pointer
2209 pointer[opt] direct-abstract-declarator
2211 direct-abstract-declarator:
2212 ( attributes[opt] abstract-declarator )
2213 direct-abstract-declarator[opt] array-declarator
2214 direct-abstract-declarator[opt] ( parameter-type-list[opt] )
2216 GNU extensions:
2218 direct-declarator:
2219 direct-declarator ( parameter-forward-declarations
2220 parameter-type-list[opt] )
2222 direct-abstract-declarator:
2223 direct-abstract-declarator[opt] ( parameter-forward-declarations
2224 parameter-type-list[opt] )
2226 parameter-forward-declarations:
2227 parameter-list ;
2228 parameter-forward-declarations parameter-list ;
2230 The uses of attributes shown above are GNU extensions.
2232 Some forms of array declarator are not included in C99 in the
2233 syntax for abstract declarators; these are disallowed elsewhere.
2234 This may be a defect (DR#289).
2236 This function also accepts an omitted abstract declarator as being
2237 an abstract declarator, although not part of the formal syntax. */
2239 static struct c_declarator *
2240 c_parser_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
2241 bool *seen_id)
2243 /* Parse any initial pointer part. */
2244 if (c_parser_next_token_is (parser, CPP_MULT))
2246 struct c_declspecs *quals_attrs = build_null_declspecs ();
2247 struct c_declarator *inner;
2248 c_parser_consume_token (parser);
2249 c_parser_declspecs (parser, quals_attrs, false, false, true);
2250 inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
2251 if (inner == NULL)
2252 return NULL;
2253 else
2254 return make_pointer_declarator (quals_attrs, inner);
2256 /* Now we have a direct declarator, direct abstract declarator or
2257 nothing (which counts as a direct abstract declarator here). */
2258 return c_parser_direct_declarator (parser, type_seen_p, kind, seen_id);
2261 /* Parse a direct declarator or direct abstract declarator; arguments
2262 as c_parser_declarator. */
2264 static struct c_declarator *
2265 c_parser_direct_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
2266 bool *seen_id)
2268 /* The direct declarator must start with an identifier (possibly
2269 omitted) or a parenthesized declarator (possibly abstract). In
2270 an ordinary declarator, initial parentheses must start a
2271 parenthesized declarator. In an abstract declarator or parameter
2272 declarator, they could start a parenthesized declarator or a
2273 parameter list. To tell which, the open parenthesis and any
2274 following attributes must be read. If a declaration specifier
2275 follows, then it is a parameter list; if the specifier is a
2276 typedef name, there might be an ambiguity about redeclaring it,
2277 which is resolved in the direction of treating it as a typedef
2278 name. If a close parenthesis follows, it is also an empty
2279 parameter list, as the syntax does not permit empty abstract
2280 declarators. Otherwise, it is a parenthesized declarator (in
2281 which case the analysis may be repeated inside it, recursively).
2283 ??? There is an ambiguity in a parameter declaration "int
2284 (__attribute__((foo)) x)", where x is not a typedef name: it
2285 could be an abstract declarator for a function, or declare x with
2286 parentheses. The proper resolution of this ambiguity needs
2287 documenting. At present we follow an accident of the old
2288 parser's implementation, whereby the first parameter must have
2289 some declaration specifiers other than just attributes. Thus as
2290 a parameter declaration it is treated as a parenthesized
2291 parameter named x, and as an abstract declarator it is
2292 rejected.
2294 ??? Also following the old parser, attributes inside an empty
2295 parameter list are ignored, making it a list not yielding a
2296 prototype, rather than giving an error or making it have one
2297 parameter with implicit type int.
2299 ??? Also following the old parser, typedef names may be
2300 redeclared in declarators, but not Objective-C class names. */
2302 if (kind != C_DTR_ABSTRACT
2303 && c_parser_next_token_is (parser, CPP_NAME)
2304 && ((type_seen_p
2305 && c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME)
2306 || c_parser_peek_token (parser)->id_kind == C_ID_ID))
2308 struct c_declarator *inner
2309 = build_id_declarator (c_parser_peek_token (parser)->value);
2310 *seen_id = true;
2311 inner->id_loc = c_parser_peek_token (parser)->location;
2312 c_parser_consume_token (parser);
2313 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
2316 if (kind != C_DTR_NORMAL
2317 && c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
2319 struct c_declarator *inner = build_id_declarator (NULL_TREE);
2320 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
2323 /* Either we are at the end of an abstract declarator, or we have
2324 parentheses. */
2326 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
2328 tree attrs;
2329 struct c_declarator *inner;
2330 c_parser_consume_token (parser);
2331 attrs = c_parser_attributes (parser);
2332 if (kind != C_DTR_NORMAL
2333 && (c_parser_next_token_starts_declspecs (parser)
2334 || c_parser_next_token_is (parser, CPP_CLOSE_PAREN)))
2336 struct c_arg_info *args
2337 = c_parser_parms_declarator (parser, kind == C_DTR_NORMAL,
2338 attrs);
2339 if (args == NULL)
2340 return NULL;
2341 else
2343 inner
2344 = build_function_declarator (args,
2345 build_id_declarator (NULL_TREE));
2346 return c_parser_direct_declarator_inner (parser, *seen_id,
2347 inner);
2350 /* A parenthesized declarator. */
2351 inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
2352 if (inner != NULL && attrs != NULL)
2353 inner = build_attrs_declarator (attrs, inner);
2354 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2356 c_parser_consume_token (parser);
2357 if (inner == NULL)
2358 return NULL;
2359 else
2360 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
2362 else
2364 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2365 "expected %<)%>");
2366 return NULL;
2369 else
2371 if (kind == C_DTR_NORMAL)
2373 c_parser_error (parser, "expected identifier or %<(%>");
2374 return NULL;
2376 else
2377 return build_id_declarator (NULL_TREE);
2381 /* Parse part of a direct declarator or direct abstract declarator,
2382 given that some (in INNER) has already been parsed; ID_PRESENT is
2383 true if an identifier is present, false for an abstract
2384 declarator. */
2386 static struct c_declarator *
2387 c_parser_direct_declarator_inner (c_parser *parser, bool id_present,
2388 struct c_declarator *inner)
2390 /* Parse a sequence of array declarators and parameter lists. */
2391 if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
2393 struct c_declarator *declarator;
2394 struct c_declspecs *quals_attrs = build_null_declspecs ();
2395 bool static_seen;
2396 bool star_seen;
2397 tree dimen;
2398 c_parser_consume_token (parser);
2399 c_parser_declspecs (parser, quals_attrs, false, false, true);
2400 static_seen = c_parser_next_token_is_keyword (parser, RID_STATIC);
2401 if (static_seen)
2402 c_parser_consume_token (parser);
2403 if (static_seen && !quals_attrs->declspecs_seen_p)
2404 c_parser_declspecs (parser, quals_attrs, false, false, true);
2405 if (!quals_attrs->declspecs_seen_p)
2406 quals_attrs = NULL;
2407 /* If "static" is present, there must be an array dimension.
2408 Otherwise, there may be a dimension, "*", or no
2409 dimension. */
2410 if (static_seen)
2412 star_seen = false;
2413 dimen = c_parser_expr_no_commas (parser, NULL).value;
2415 else
2417 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
2419 dimen = NULL_TREE;
2420 star_seen = false;
2422 else if (c_parser_next_token_is (parser, CPP_MULT))
2424 if (c_parser_peek_2nd_token (parser)->type == CPP_CLOSE_SQUARE)
2426 dimen = NULL_TREE;
2427 star_seen = true;
2428 c_parser_consume_token (parser);
2430 else
2432 star_seen = false;
2433 dimen = c_parser_expr_no_commas (parser, NULL).value;
2436 else
2438 star_seen = false;
2439 dimen = c_parser_expr_no_commas (parser, NULL).value;
2442 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
2443 c_parser_consume_token (parser);
2444 else
2446 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
2447 "expected %<]%>");
2448 return NULL;
2450 declarator = build_array_declarator (dimen, quals_attrs, static_seen,
2451 star_seen);
2452 if (declarator == NULL)
2453 return NULL;
2454 inner = set_array_declarator_inner (declarator, inner, !id_present);
2455 return c_parser_direct_declarator_inner (parser, id_present, inner);
2457 else if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
2459 tree attrs;
2460 struct c_arg_info *args;
2461 c_parser_consume_token (parser);
2462 attrs = c_parser_attributes (parser);
2463 args = c_parser_parms_declarator (parser, id_present, attrs);
2464 if (args == NULL)
2465 return NULL;
2466 else
2468 inner = build_function_declarator (args, inner);
2469 return c_parser_direct_declarator_inner (parser, id_present, inner);
2472 return inner;
2475 /* Parse a parameter list or identifier list, including the closing
2476 parenthesis but not the opening one. ATTRS are the attributes at
2477 the start of the list. ID_LIST_OK is true if an identifier list is
2478 acceptable; such a list must not have attributes at the start. */
2480 static struct c_arg_info *
2481 c_parser_parms_declarator (c_parser *parser, bool id_list_ok, tree attrs)
2483 push_scope ();
2484 declare_parm_level ();
2485 /* If the list starts with an identifier, it is an identifier list.
2486 Otherwise, it is either a prototype list or an empty list. */
2487 if (id_list_ok
2488 && !attrs
2489 && c_parser_next_token_is (parser, CPP_NAME)
2490 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
2492 tree list = NULL_TREE, *nextp = &list;
2493 while (c_parser_next_token_is (parser, CPP_NAME)
2494 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
2496 *nextp = build_tree_list (NULL_TREE,
2497 c_parser_peek_token (parser)->value);
2498 nextp = & TREE_CHAIN (*nextp);
2499 c_parser_consume_token (parser);
2500 if (c_parser_next_token_is_not (parser, CPP_COMMA))
2501 break;
2502 c_parser_consume_token (parser);
2503 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2505 c_parser_error (parser, "expected identifier");
2506 break;
2509 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2511 struct c_arg_info *ret = XOBNEW (&parser_obstack, struct c_arg_info);
2512 ret->parms = 0;
2513 ret->tags = 0;
2514 ret->types = list;
2515 ret->others = 0;
2516 ret->pending_sizes = 0;
2517 ret->had_vla_unspec = 0;
2518 c_parser_consume_token (parser);
2519 pop_scope ();
2520 return ret;
2522 else
2524 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2525 "expected %<)%>");
2526 pop_scope ();
2527 return NULL;
2530 else
2532 struct c_arg_info *ret = c_parser_parms_list_declarator (parser, attrs);
2533 pop_scope ();
2534 return ret;
2538 /* Parse a parameter list (possibly empty), including the closing
2539 parenthesis but not the opening one. ATTRS are the attributes at
2540 the start of the list. */
2542 static struct c_arg_info *
2543 c_parser_parms_list_declarator (c_parser *parser, tree attrs)
2545 bool good_parm = false;
2546 /* ??? Following the old parser, forward parameter declarations may
2547 use abstract declarators, and if no real parameter declarations
2548 follow the forward declarations then this is not diagnosed. Also
2549 note as above that attributes are ignored as the only contents of
2550 the parentheses, or as the only contents after forward
2551 declarations. */
2552 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2554 struct c_arg_info *ret = XOBNEW (&parser_obstack, struct c_arg_info);
2555 ret->parms = 0;
2556 ret->tags = 0;
2557 ret->types = 0;
2558 ret->others = 0;
2559 ret->pending_sizes = 0;
2560 ret->had_vla_unspec = 0;
2561 c_parser_consume_token (parser);
2562 return ret;
2564 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
2566 struct c_arg_info *ret = XOBNEW (&parser_obstack, struct c_arg_info);
2567 ret->parms = 0;
2568 ret->tags = 0;
2569 ret->others = 0;
2570 ret->pending_sizes = 0;
2571 ret->had_vla_unspec = 0;
2572 /* Suppress -Wold-style-definition for this case. */
2573 ret->types = error_mark_node;
2574 error ("ISO C requires a named argument before %<...%>");
2575 c_parser_consume_token (parser);
2576 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2578 c_parser_consume_token (parser);
2579 return ret;
2581 else
2583 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2584 "expected %<)%>");
2585 return NULL;
2588 /* Nonempty list of parameters, either terminated with semicolon
2589 (forward declarations; recurse) or with close parenthesis (normal
2590 function) or with ", ... )" (variadic function). */
2591 while (true)
2593 /* Parse a parameter. */
2594 struct c_parm *parm = c_parser_parameter_declaration (parser, attrs);
2595 attrs = NULL_TREE;
2596 if (parm != NULL)
2598 good_parm = true;
2599 push_parm_decl (parm);
2601 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
2603 tree new_attrs;
2604 c_parser_consume_token (parser);
2605 mark_forward_parm_decls ();
2606 new_attrs = c_parser_attributes (parser);
2607 return c_parser_parms_list_declarator (parser, new_attrs);
2609 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2611 c_parser_consume_token (parser);
2612 if (good_parm)
2613 return get_parm_info (false);
2614 else
2616 struct c_arg_info *ret
2617 = XOBNEW (&parser_obstack, struct c_arg_info);
2618 ret->parms = 0;
2619 ret->tags = 0;
2620 ret->types = 0;
2621 ret->others = 0;
2622 ret->pending_sizes = 0;
2623 ret->had_vla_unspec = 0;
2624 return ret;
2627 if (!c_parser_require (parser, CPP_COMMA,
2628 "expected %<;%>, %<,%> or %<)%>"))
2630 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
2631 return NULL;
2633 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
2635 c_parser_consume_token (parser);
2636 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2638 c_parser_consume_token (parser);
2639 if (good_parm)
2640 return get_parm_info (true);
2641 else
2643 struct c_arg_info *ret
2644 = XOBNEW (&parser_obstack, struct c_arg_info);
2645 ret->parms = 0;
2646 ret->tags = 0;
2647 ret->types = 0;
2648 ret->others = 0;
2649 ret->pending_sizes = 0;
2650 ret->had_vla_unspec = 0;
2651 return ret;
2654 else
2656 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2657 "expected %<)%>");
2658 return NULL;
2664 /* Parse a parameter declaration. ATTRS are the attributes at the
2665 start of the declaration if it is the first parameter. */
2667 static struct c_parm *
2668 c_parser_parameter_declaration (c_parser *parser, tree attrs)
2670 struct c_declspecs *specs;
2671 struct c_declarator *declarator;
2672 tree prefix_attrs;
2673 tree postfix_attrs = NULL_TREE;
2674 bool dummy = false;
2675 if (!c_parser_next_token_starts_declspecs (parser))
2677 /* ??? In some Objective-C cases '...' isn't applicable so there
2678 should be a different message. */
2679 c_parser_error (parser,
2680 "expected declaration specifiers or %<...%>");
2681 c_parser_skip_to_end_of_parameter (parser);
2682 return NULL;
2684 specs = build_null_declspecs ();
2685 if (attrs)
2687 declspecs_add_attrs (specs, attrs);
2688 attrs = NULL_TREE;
2690 c_parser_declspecs (parser, specs, true, true, true);
2691 finish_declspecs (specs);
2692 pending_xref_error ();
2693 prefix_attrs = specs->attrs;
2694 specs->attrs = NULL_TREE;
2695 declarator = c_parser_declarator (parser, specs->type_seen_p,
2696 C_DTR_PARM, &dummy);
2697 if (declarator == NULL)
2699 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
2700 return NULL;
2702 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2703 postfix_attrs = c_parser_attributes (parser);
2704 return build_c_parm (specs, chainon (postfix_attrs, prefix_attrs),
2705 declarator);
2708 /* Parse a string literal in an asm expression. It should not be
2709 translated, and wide string literals are an error although
2710 permitted by the syntax. This is a GNU extension.
2712 asm-string-literal:
2713 string-literal
2715 ??? At present, following the old parser, the caller needs to have
2716 set c_lex_string_translate to 0. It would be better to follow the
2717 C++ parser rather than using the c_lex_string_translate kludge. */
2719 static tree
2720 c_parser_asm_string_literal (c_parser *parser)
2722 tree str;
2723 if (c_parser_next_token_is (parser, CPP_STRING))
2725 str = c_parser_peek_token (parser)->value;
2726 c_parser_consume_token (parser);
2728 else if (c_parser_next_token_is (parser, CPP_WSTRING))
2730 error ("wide string literal in %<asm%>");
2731 str = build_string (1, "");
2732 c_parser_consume_token (parser);
2734 else
2736 c_parser_error (parser, "expected string literal");
2737 str = NULL_TREE;
2739 return str;
2742 /* Parse a simple asm expression. This is used in restricted
2743 contexts, where a full expression with inputs and outputs does not
2744 make sense. This is a GNU extension.
2746 simple-asm-expr:
2747 asm ( asm-string-literal )
2750 static tree
2751 c_parser_simple_asm_expr (c_parser *parser)
2753 tree str;
2754 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM));
2755 /* ??? Follow the C++ parser rather than using the
2756 c_lex_string_translate kludge. */
2757 c_lex_string_translate = 0;
2758 c_parser_consume_token (parser);
2759 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2761 c_lex_string_translate = 1;
2762 return NULL_TREE;
2764 str = c_parser_asm_string_literal (parser);
2765 c_lex_string_translate = 1;
2766 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
2768 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
2769 return NULL_TREE;
2771 return str;
2774 /* Parse (possibly empty) attributes. This is a GNU extension.
2776 attributes:
2777 empty
2778 attributes attribute
2780 attribute:
2781 __attribute__ ( ( attribute-list ) )
2783 attribute-list:
2784 attrib
2785 attribute_list , attrib
2787 attrib:
2788 empty
2789 any-word
2790 any-word ( identifier )
2791 any-word ( identifier , nonempty-expr-list )
2792 any-word ( expr-list )
2794 where the "identifier" must not be declared as a type, and
2795 "any-word" may be any identifier (including one declared as a
2796 type), a reserved word storage class specifier, type specifier or
2797 type qualifier. ??? This still leaves out most reserved keywords
2798 (following the old parser), shouldn't we include them, and why not
2799 allow identifiers declared as types to start the arguments? */
2801 static tree
2802 c_parser_attributes (c_parser *parser)
2804 tree attrs = NULL_TREE;
2805 while (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2807 /* ??? Follow the C++ parser rather than using the
2808 c_lex_string_translate kludge. */
2809 c_lex_string_translate = 0;
2810 c_parser_consume_token (parser);
2811 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2813 c_lex_string_translate = 1;
2814 return attrs;
2816 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2818 c_lex_string_translate = 1;
2819 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
2820 return attrs;
2822 /* Parse the attribute list. */
2823 while (c_parser_next_token_is (parser, CPP_COMMA)
2824 || c_parser_next_token_is (parser, CPP_NAME)
2825 || c_parser_next_token_is (parser, CPP_KEYWORD))
2827 tree attr, attr_name, attr_args;
2828 if (c_parser_next_token_is (parser, CPP_COMMA))
2830 c_parser_consume_token (parser);
2831 continue;
2833 if (c_parser_next_token_is (parser, CPP_KEYWORD))
2835 /* ??? See comment above about what keywords are
2836 accepted here. */
2837 bool ok;
2838 switch (c_parser_peek_token (parser)->keyword)
2840 case RID_STATIC:
2841 case RID_UNSIGNED:
2842 case RID_LONG:
2843 case RID_CONST:
2844 case RID_EXTERN:
2845 case RID_REGISTER:
2846 case RID_TYPEDEF:
2847 case RID_SHORT:
2848 case RID_INLINE:
2849 case RID_VOLATILE:
2850 case RID_SIGNED:
2851 case RID_AUTO:
2852 case RID_RESTRICT:
2853 case RID_COMPLEX:
2854 case RID_THREAD:
2855 case RID_INT:
2856 case RID_CHAR:
2857 case RID_FLOAT:
2858 case RID_DOUBLE:
2859 case RID_VOID:
2860 case RID_DFLOAT32:
2861 case RID_DFLOAT64:
2862 case RID_DFLOAT128:
2863 case RID_BOOL:
2864 ok = true;
2865 break;
2866 default:
2867 ok = false;
2868 break;
2870 if (!ok)
2871 break;
2873 attr_name = c_parser_peek_token (parser)->value;
2874 c_parser_consume_token (parser);
2875 if (c_parser_next_token_is_not (parser, CPP_OPEN_PAREN))
2877 attr = build_tree_list (attr_name, NULL_TREE);
2878 attrs = chainon (attrs, attr);
2879 continue;
2881 c_parser_consume_token (parser);
2882 /* Parse the attribute contents. If they start with an
2883 identifier which is followed by a comma or close
2884 parenthesis, then the arguments start with that
2885 identifier; otherwise they are an expression list. */
2886 if (c_parser_next_token_is (parser, CPP_NAME)
2887 && c_parser_peek_token (parser)->id_kind == C_ID_ID
2888 && ((c_parser_peek_2nd_token (parser)->type == CPP_COMMA)
2889 || (c_parser_peek_2nd_token (parser)->type
2890 == CPP_CLOSE_PAREN)))
2892 tree arg1 = c_parser_peek_token (parser)->value;
2893 c_parser_consume_token (parser);
2894 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2895 attr_args = build_tree_list (NULL_TREE, arg1);
2896 else
2898 c_parser_consume_token (parser);
2899 attr_args = tree_cons (NULL_TREE, arg1,
2900 c_parser_expr_list (parser, false));
2903 else
2905 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2906 attr_args = NULL_TREE;
2907 else
2908 attr_args = c_parser_expr_list (parser, false);
2910 attr = build_tree_list (attr_name, attr_args);
2911 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2912 c_parser_consume_token (parser);
2913 else
2915 c_lex_string_translate = 1;
2916 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2917 "expected %<)%>");
2918 return attrs;
2920 attrs = chainon (attrs, attr);
2922 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2923 c_parser_consume_token (parser);
2924 else
2926 c_lex_string_translate = 1;
2927 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2928 "expected %<)%>");
2929 return attrs;
2931 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2932 c_parser_consume_token (parser);
2933 else
2935 c_lex_string_translate = 1;
2936 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2937 "expected %<)%>");
2938 return attrs;
2940 c_lex_string_translate = 1;
2942 return attrs;
2945 /* Parse a type name (C90 6.5.5, C99 6.7.6).
2947 type-name:
2948 specifier-qualifier-list abstract-declarator[opt]
2951 static struct c_type_name *
2952 c_parser_type_name (c_parser *parser)
2954 struct c_declspecs *specs = build_null_declspecs ();
2955 struct c_declarator *declarator;
2956 struct c_type_name *ret;
2957 bool dummy = false;
2958 c_parser_declspecs (parser, specs, false, true, true);
2959 if (!specs->declspecs_seen_p)
2961 c_parser_error (parser, "expected specifier-qualifier-list");
2962 return NULL;
2964 pending_xref_error ();
2965 finish_declspecs (specs);
2966 declarator = c_parser_declarator (parser, specs->type_seen_p,
2967 C_DTR_ABSTRACT, &dummy);
2968 if (declarator == NULL)
2969 return NULL;
2970 ret = XOBNEW (&parser_obstack, struct c_type_name);
2971 ret->specs = specs;
2972 ret->declarator = declarator;
2973 return ret;
2976 /* Parse an initializer (C90 6.5.7, C99 6.7.8).
2978 initializer:
2979 assignment-expression
2980 { initializer-list }
2981 { initializer-list , }
2983 initializer-list:
2984 designation[opt] initializer
2985 initializer-list , designation[opt] initializer
2987 designation:
2988 designator-list =
2990 designator-list:
2991 designator
2992 designator-list designator
2994 designator:
2995 array-designator
2996 . identifier
2998 array-designator:
2999 [ constant-expression ]
3001 GNU extensions:
3003 initializer:
3006 designation:
3007 array-designator
3008 identifier :
3010 array-designator:
3011 [ constant-expression ... constant-expression ]
3013 Any expression without commas is accepted in the syntax for the
3014 constant-expressions, with non-constant expressions rejected later.
3016 This function is only used for top-level initializers; for nested
3017 ones, see c_parser_initval. */
3019 static struct c_expr
3020 c_parser_initializer (c_parser *parser)
3022 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
3023 return c_parser_braced_init (parser, NULL_TREE, false);
3024 else
3026 struct c_expr ret;
3027 ret = c_parser_expr_no_commas (parser, NULL);
3028 if (TREE_CODE (ret.value) != STRING_CST
3029 && TREE_CODE (ret.value) != COMPOUND_LITERAL_EXPR)
3030 ret = default_function_array_conversion (ret);
3031 return ret;
3035 /* Parse a braced initializer list. TYPE is the type specified for a
3036 compound literal, and NULL_TREE for other initializers and for
3037 nested braced lists. NESTED_P is true for nested braced lists,
3038 false for the list of a compound literal or the list that is the
3039 top-level initializer in a declaration. */
3041 static struct c_expr
3042 c_parser_braced_init (c_parser *parser, tree type, bool nested_p)
3044 gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE));
3045 c_parser_consume_token (parser);
3046 if (nested_p)
3047 push_init_level (0);
3048 else
3049 really_start_incremental_init (type);
3050 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3052 if (pedantic)
3053 pedwarn ("ISO C forbids empty initializer braces");
3055 else
3057 /* Parse a non-empty initializer list, possibly with a trailing
3058 comma. */
3059 while (true)
3061 c_parser_initelt (parser);
3062 if (parser->error)
3063 break;
3064 if (c_parser_next_token_is (parser, CPP_COMMA))
3065 c_parser_consume_token (parser);
3066 else
3067 break;
3068 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3069 break;
3072 if (c_parser_next_token_is_not (parser, CPP_CLOSE_BRACE))
3074 struct c_expr ret;
3075 ret.value = error_mark_node;
3076 ret.original_code = ERROR_MARK;
3077 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, "expected %<}%>");
3078 return ret;
3080 c_parser_consume_token (parser);
3081 return pop_init_level (0);
3084 /* Parse a nested initializer, including designators. */
3086 static void
3087 c_parser_initelt (c_parser *parser)
3089 /* Parse any designator or designator list. A single array
3090 designator may have the subsequent "=" omitted in GNU C, but a
3091 longer list or a structure member designator may not. */
3092 if (c_parser_next_token_is (parser, CPP_NAME)
3093 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
3095 /* Old-style structure member designator. */
3096 set_init_label (c_parser_peek_token (parser)->value);
3097 if (pedantic)
3098 pedwarn ("obsolete use of designated initializer with %<:%>");
3099 c_parser_consume_token (parser);
3100 c_parser_consume_token (parser);
3102 else
3104 /* des_seen is 0 if there have been no designators, 1 if there
3105 has been a single array designator and 2 otherwise. */
3106 int des_seen = 0;
3107 while (c_parser_next_token_is (parser, CPP_OPEN_SQUARE)
3108 || c_parser_next_token_is (parser, CPP_DOT))
3110 int des_prev = des_seen;
3111 if (des_seen < 2)
3112 des_seen++;
3113 if (c_parser_next_token_is (parser, CPP_DOT))
3115 des_seen = 2;
3116 c_parser_consume_token (parser);
3117 if (c_parser_next_token_is (parser, CPP_NAME))
3119 set_init_label (c_parser_peek_token (parser)->value);
3120 c_parser_consume_token (parser);
3122 else
3124 struct c_expr init;
3125 init.value = error_mark_node;
3126 init.original_code = ERROR_MARK;
3127 c_parser_error (parser, "expected identifier");
3128 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
3129 process_init_element (init);
3130 return;
3133 else
3135 tree first, second;
3136 /* ??? Following the old parser, [ objc-receiver
3137 objc-message-args ] is accepted as an initializer,
3138 being distinguished from a designator by what follows
3139 the first assignment expression inside the square
3140 brackets, but after a first array designator a
3141 subsequent square bracket is for Objective-C taken to
3142 start an expression, using the obsolete form of
3143 designated initializer without '=', rather than
3144 possibly being a second level of designation: in LALR
3145 terms, the '[' is shifted rather than reducing
3146 designator to designator-list. */
3147 if (des_prev == 1 && c_dialect_objc ())
3149 des_seen = des_prev;
3150 break;
3152 if (des_prev == 0 && c_dialect_objc ())
3154 /* This might be an array designator or an
3155 Objective-C message expression. If the former,
3156 continue parsing here; if the latter, parse the
3157 remainder of the initializer given the starting
3158 primary-expression. ??? It might make sense to
3159 distinguish when des_prev == 1 as well; see
3160 previous comment. */
3161 tree rec, args;
3162 struct c_expr mexpr;
3163 c_parser_consume_token (parser);
3164 if (c_parser_peek_token (parser)->type == CPP_NAME
3165 && ((c_parser_peek_token (parser)->id_kind
3166 == C_ID_TYPENAME)
3167 || (c_parser_peek_token (parser)->id_kind
3168 == C_ID_CLASSNAME)))
3170 /* Type name receiver. */
3171 tree id = c_parser_peek_token (parser)->value;
3172 c_parser_consume_token (parser);
3173 rec = objc_get_class_reference (id);
3174 goto parse_message_args;
3176 first = c_parser_expr_no_commas (parser, NULL).value;
3177 if (c_parser_next_token_is (parser, CPP_ELLIPSIS)
3178 || c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3179 goto array_desig_after_first;
3180 /* Expression receiver. So far only one part
3181 without commas has been parsed; there might be
3182 more of the expression. */
3183 rec = first;
3184 while (c_parser_next_token_is (parser, CPP_COMMA))
3186 struct c_expr next;
3187 c_parser_consume_token (parser);
3188 next = c_parser_expr_no_commas (parser, NULL);
3189 next = default_function_array_conversion (next);
3190 rec = build_compound_expr (rec, next.value);
3192 parse_message_args:
3193 /* Now parse the objc-message-args. */
3194 args = c_parser_objc_message_args (parser);
3195 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
3196 "expected %<]%>");
3197 mexpr.value
3198 = objc_build_message_expr (build_tree_list (rec, args));
3199 mexpr.original_code = ERROR_MARK;
3200 /* Now parse and process the remainder of the
3201 initializer, starting with this message
3202 expression as a primary-expression. */
3203 c_parser_initval (parser, &mexpr);
3204 return;
3206 c_parser_consume_token (parser);
3207 first = c_parser_expr_no_commas (parser, NULL).value;
3208 array_desig_after_first:
3209 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
3211 c_parser_consume_token (parser);
3212 second = c_parser_expr_no_commas (parser, NULL).value;
3214 else
3215 second = NULL_TREE;
3216 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3218 c_parser_consume_token (parser);
3219 set_init_index (first, second);
3220 if (pedantic && second)
3221 pedwarn ("ISO C forbids specifying range of "
3222 "elements to initialize");
3224 else
3225 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
3226 "expected %<]%>");
3229 if (des_seen >= 1)
3231 if (c_parser_next_token_is (parser, CPP_EQ))
3233 if (pedantic && !flag_isoc99)
3234 pedwarn ("ISO C90 forbids specifying subobject to initialize");
3235 c_parser_consume_token (parser);
3237 else
3239 if (des_seen == 1)
3241 if (pedantic)
3242 pedwarn ("obsolete use of designated initializer "
3243 "without %<=%>");
3245 else
3247 struct c_expr init;
3248 init.value = error_mark_node;
3249 init.original_code = ERROR_MARK;
3250 c_parser_error (parser, "expected %<=%>");
3251 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
3252 process_init_element (init);
3253 return;
3258 c_parser_initval (parser, NULL);
3261 /* Parse a nested initializer; as c_parser_initializer but parses
3262 initializers within braced lists, after any designators have been
3263 applied. If AFTER is not NULL then it is an Objective-C message
3264 expression which is the primary-expression starting the
3265 initializer. */
3267 static void
3268 c_parser_initval (c_parser *parser, struct c_expr *after)
3270 struct c_expr init;
3271 gcc_assert (!after || c_dialect_objc ());
3272 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE) && !after)
3273 init = c_parser_braced_init (parser, NULL_TREE, true);
3274 else
3276 init = c_parser_expr_no_commas (parser, after);
3277 if (init.value != NULL_TREE
3278 && TREE_CODE (init.value) != STRING_CST
3279 && TREE_CODE (init.value) != COMPOUND_LITERAL_EXPR)
3280 init = default_function_array_conversion (init);
3282 process_init_element (init);
3285 /* Parse a compound statement (possibly a function body) (C90 6.6.2,
3286 C99 6.8.2).
3288 compound-statement:
3289 { block-item-list[opt] }
3290 { label-declarations block-item-list }
3292 block-item-list:
3293 block-item
3294 block-item-list block-item
3296 block-item:
3297 nested-declaration
3298 statement
3300 nested-declaration:
3301 declaration
3303 GNU extensions:
3305 compound-statement:
3306 { label-declarations block-item-list }
3308 nested-declaration:
3309 __extension__ nested-declaration
3310 nested-function-definition
3312 label-declarations:
3313 label-declaration
3314 label-declarations label-declaration
3316 label-declaration:
3317 __label__ identifier-list ;
3319 Allowing the mixing of declarations and code is new in C99. The
3320 GNU syntax also permits (not shown above) labels at the end of
3321 compound statements, which yield an error. We don't allow labels
3322 on declarations; this might seem like a natural extension, but
3323 there would be a conflict between attributes on the label and
3324 prefix attributes on the declaration. ??? The syntax follows the
3325 old parser in requiring something after label declarations.
3326 Although they are erroneous if the labels declared aren't defined,
3327 is it useful for the syntax to be this way?
3329 OpenMP:
3331 block-item:
3332 openmp-directive
3334 openmp-directive:
3335 barrier-directive
3336 flush-directive */
3338 static tree
3339 c_parser_compound_statement (c_parser *parser)
3341 tree stmt;
3342 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
3343 return error_mark_node;
3344 stmt = c_begin_compound_stmt (true);
3345 c_parser_compound_statement_nostart (parser);
3346 return c_end_compound_stmt (stmt, true);
3349 /* Parse a compound statement except for the opening brace. This is
3350 used for parsing both compound statements and statement expressions
3351 (which follow different paths to handling the opening). */
3353 static void
3354 c_parser_compound_statement_nostart (c_parser *parser)
3356 bool last_stmt = false;
3357 bool last_label = false;
3358 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3360 c_parser_consume_token (parser);
3361 return;
3363 if (c_parser_next_token_is_keyword (parser, RID_LABEL))
3365 /* Read zero or more forward-declarations for labels that nested
3366 functions can jump to. */
3367 while (c_parser_next_token_is_keyword (parser, RID_LABEL))
3369 c_parser_consume_token (parser);
3370 /* Any identifiers, including those declared as type names,
3371 are OK here. */
3372 while (true)
3374 tree label;
3375 if (c_parser_next_token_is_not (parser, CPP_NAME))
3377 c_parser_error (parser, "expected identifier");
3378 break;
3380 label
3381 = declare_label (c_parser_peek_token (parser)->value);
3382 C_DECLARED_LABEL_FLAG (label) = 1;
3383 add_stmt (build_stmt (DECL_EXPR, label));
3384 c_parser_consume_token (parser);
3385 if (c_parser_next_token_is (parser, CPP_COMMA))
3386 c_parser_consume_token (parser);
3387 else
3388 break;
3390 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
3392 /* ??? Locating this diagnostic on the token after the
3393 declarations end follows the old parser, but it might be
3394 better to locate it where the declarations start instead. */
3395 if (pedantic)
3396 pedwarn ("ISO C forbids label declarations");
3398 /* We must now have at least one statement, label or declaration. */
3399 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3401 c_parser_error (parser, "expected declaration or statement");
3402 c_parser_consume_token (parser);
3403 return;
3405 while (c_parser_next_token_is_not (parser, CPP_CLOSE_BRACE))
3407 location_t loc = c_parser_peek_token (parser)->location;
3408 if (c_parser_next_token_is_keyword (parser, RID_CASE)
3409 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
3410 || (c_parser_next_token_is (parser, CPP_NAME)
3411 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
3413 last_label = true;
3414 last_stmt = false;
3415 c_parser_label (parser);
3417 else if (!last_label
3418 && c_parser_next_token_starts_declspecs (parser))
3420 last_label = false;
3421 c_parser_declaration_or_fndef (parser, true, true, true, true);
3422 if (last_stmt
3423 && ((pedantic && !flag_isoc99)
3424 || warn_declaration_after_statement))
3425 pedwarn_c90 ("%HISO C90 forbids mixed declarations and code",
3426 &loc);
3427 last_stmt = false;
3429 else if (!last_label
3430 && c_parser_next_token_is_keyword (parser, RID_EXTENSION))
3432 /* __extension__ can start a declaration, but is also an
3433 unary operator that can start an expression. Consume all
3434 but the last of a possible series of __extension__ to
3435 determine which. */
3436 while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
3437 && (c_parser_peek_2nd_token (parser)->keyword
3438 == RID_EXTENSION))
3439 c_parser_consume_token (parser);
3440 if (c_token_starts_declspecs (c_parser_peek_2nd_token (parser)))
3442 int ext;
3443 ext = disable_extension_diagnostics ();
3444 c_parser_consume_token (parser);
3445 last_label = false;
3446 c_parser_declaration_or_fndef (parser, true, true, true, true);
3447 /* Following the old parser, __extension__ does not
3448 disable this diagnostic. */
3449 restore_extension_diagnostics (ext);
3450 if (last_stmt
3451 && ((pedantic && !flag_isoc99)
3452 || warn_declaration_after_statement))
3453 pedwarn_c90 ("%HISO C90 forbids mixed declarations and code",
3454 &loc);
3455 last_stmt = false;
3457 else
3458 goto statement;
3460 else if (c_parser_next_token_is (parser, CPP_PRAGMA))
3462 /* External pragmas, and some omp pragmas, are not associated
3463 with regular c code, and so are not to be considered statements
3464 syntactically. This ensures that the user doesn't put them
3465 places that would turn into syntax errors if the directive
3466 were ignored. */
3467 if (c_parser_pragma (parser, pragma_compound))
3468 last_label = false, last_stmt = true;
3470 else if (c_parser_next_token_is (parser, CPP_EOF))
3472 c_parser_error (parser, "expected declaration or statement");
3473 return;
3475 else
3477 statement:
3478 last_label = false;
3479 last_stmt = true;
3480 c_parser_statement_after_labels (parser);
3483 parser->error = false;
3485 if (last_label)
3486 error ("label at end of compound statement");
3487 c_parser_consume_token (parser);
3490 /* Parse a label (C90 6.6.1, C99 6.8.1).
3492 label:
3493 identifier : attributes[opt]
3494 case constant-expression :
3495 default :
3497 GNU extensions:
3499 label:
3500 case constant-expression ... constant-expression :
3502 The use of attributes on labels is a GNU extension. The syntax in
3503 GNU C accepts any expressions without commas, non-constant
3504 expressions being rejected later. */
3506 static void
3507 c_parser_label (c_parser *parser)
3509 location_t loc1 = c_parser_peek_token (parser)->location;
3510 tree label = NULL_TREE;
3511 if (c_parser_next_token_is_keyword (parser, RID_CASE))
3513 tree exp1, exp2;
3514 c_parser_consume_token (parser);
3515 exp1 = c_parser_expr_no_commas (parser, NULL).value;
3516 if (c_parser_next_token_is (parser, CPP_COLON))
3518 c_parser_consume_token (parser);
3519 label = do_case (exp1, NULL_TREE);
3521 else if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
3523 c_parser_consume_token (parser);
3524 exp2 = c_parser_expr_no_commas (parser, NULL).value;
3525 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
3526 label = do_case (exp1, exp2);
3528 else
3529 c_parser_error (parser, "expected %<:%> or %<...%>");
3531 else if (c_parser_next_token_is_keyword (parser, RID_DEFAULT))
3533 c_parser_consume_token (parser);
3534 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
3535 label = do_case (NULL_TREE, NULL_TREE);
3537 else
3539 tree name = c_parser_peek_token (parser)->value;
3540 tree tlab;
3541 location_t loc2;
3542 tree attrs;
3543 gcc_assert (c_parser_next_token_is (parser, CPP_NAME));
3544 c_parser_consume_token (parser);
3545 gcc_assert (c_parser_next_token_is (parser, CPP_COLON));
3546 loc2 = c_parser_peek_token (parser)->location;
3547 c_parser_consume_token (parser);
3548 attrs = c_parser_attributes (parser);
3549 tlab = define_label (loc2, name);
3550 if (tlab)
3552 decl_attributes (&tlab, attrs, 0);
3553 label = add_stmt (build_stmt (LABEL_EXPR, tlab));
3556 if (label)
3557 SET_EXPR_LOCATION (label, loc1);
3560 /* Parse a statement (C90 6.6, C99 6.8).
3562 statement:
3563 labeled-statement
3564 compound-statement
3565 expression-statement
3566 selection-statement
3567 iteration-statement
3568 jump-statement
3570 labeled-statement:
3571 label statement
3573 expression-statement:
3574 expression[opt] ;
3576 selection-statement:
3577 if-statement
3578 switch-statement
3580 iteration-statement:
3581 while-statement
3582 do-statement
3583 for-statement
3585 jump-statement:
3586 goto identifier ;
3587 continue ;
3588 break ;
3589 return expression[opt] ;
3591 GNU extensions:
3593 statement:
3594 asm-statement
3596 jump-statement:
3597 goto * expression ;
3599 Objective-C:
3601 statement:
3602 objc-throw-statement
3603 objc-try-catch-statement
3604 objc-synchronized-statement
3606 objc-throw-statement:
3607 @throw expression ;
3608 @throw ;
3610 OpenMP:
3612 statement:
3613 openmp-construct
3615 openmp-construct:
3616 parallel-construct
3617 for-construct
3618 sections-construct
3619 single-construct
3620 parallel-for-construct
3621 parallel-sections-construct
3622 master-construct
3623 critical-construct
3624 atomic-construct
3625 ordered-construct
3627 parallel-construct:
3628 parallel-directive structured-block
3630 for-construct:
3631 for-directive iteration-statement
3633 sections-construct:
3634 sections-directive section-scope
3636 single-construct:
3637 single-directive structured-block
3639 parallel-for-construct:
3640 parallel-for-directive iteration-statement
3642 parallel-sections-construct:
3643 parallel-sections-directive section-scope
3645 master-construct:
3646 master-directive structured-block
3648 critical-construct:
3649 critical-directive structured-block
3651 atomic-construct:
3652 atomic-directive expression-statement
3654 ordered-construct:
3655 ordered-directive structured-block */
3657 static void
3658 c_parser_statement (c_parser *parser)
3660 while (c_parser_next_token_is_keyword (parser, RID_CASE)
3661 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
3662 || (c_parser_next_token_is (parser, CPP_NAME)
3663 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
3664 c_parser_label (parser);
3665 c_parser_statement_after_labels (parser);
3668 /* Parse a statement, other than a labeled statement. */
3670 static void
3671 c_parser_statement_after_labels (c_parser *parser)
3673 location_t loc = c_parser_peek_token (parser)->location;
3674 tree stmt = NULL_TREE;
3675 switch (c_parser_peek_token (parser)->type)
3677 case CPP_OPEN_BRACE:
3678 add_stmt (c_parser_compound_statement (parser));
3679 break;
3680 case CPP_KEYWORD:
3681 switch (c_parser_peek_token (parser)->keyword)
3683 case RID_IF:
3684 c_parser_if_statement (parser);
3685 break;
3686 case RID_SWITCH:
3687 c_parser_switch_statement (parser);
3688 break;
3689 case RID_WHILE:
3690 c_parser_while_statement (parser);
3691 break;
3692 case RID_DO:
3693 c_parser_do_statement (parser);
3694 break;
3695 case RID_FOR:
3696 c_parser_for_statement (parser);
3697 break;
3698 case RID_GOTO:
3699 c_parser_consume_token (parser);
3700 if (c_parser_next_token_is (parser, CPP_NAME))
3702 stmt = c_finish_goto_label (c_parser_peek_token (parser)->value);
3703 c_parser_consume_token (parser);
3705 else if (c_parser_next_token_is (parser, CPP_MULT))
3707 c_parser_consume_token (parser);
3708 stmt = c_finish_goto_ptr (c_parser_expression (parser).value);
3710 else
3711 c_parser_error (parser, "expected identifier or %<*%>");
3712 goto expect_semicolon;
3713 case RID_CONTINUE:
3714 c_parser_consume_token (parser);
3715 stmt = c_finish_bc_stmt (&c_cont_label, false);
3716 goto expect_semicolon;
3717 case RID_BREAK:
3718 c_parser_consume_token (parser);
3719 stmt = c_finish_bc_stmt (&c_break_label, true);
3720 goto expect_semicolon;
3721 case RID_RETURN:
3722 c_parser_consume_token (parser);
3723 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
3725 stmt = c_finish_return (NULL_TREE);
3726 c_parser_consume_token (parser);
3728 else
3730 stmt = c_finish_return (c_parser_expression_conv (parser).value);
3731 goto expect_semicolon;
3733 break;
3734 case RID_ASM:
3735 stmt = c_parser_asm_statement (parser);
3736 break;
3737 case RID_AT_THROW:
3738 gcc_assert (c_dialect_objc ());
3739 c_parser_consume_token (parser);
3740 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
3742 stmt = objc_build_throw_stmt (NULL_TREE);
3743 c_parser_consume_token (parser);
3745 else
3747 stmt
3748 = objc_build_throw_stmt (c_parser_expression (parser).value);
3749 goto expect_semicolon;
3751 break;
3752 case RID_AT_TRY:
3753 gcc_assert (c_dialect_objc ());
3754 c_parser_objc_try_catch_statement (parser);
3755 break;
3756 case RID_AT_SYNCHRONIZED:
3757 gcc_assert (c_dialect_objc ());
3758 c_parser_objc_synchronized_statement (parser);
3759 break;
3760 default:
3761 goto expr_stmt;
3763 break;
3764 case CPP_SEMICOLON:
3765 c_parser_consume_token (parser);
3766 break;
3767 case CPP_CLOSE_PAREN:
3768 case CPP_CLOSE_SQUARE:
3769 /* Avoid infinite loop in error recovery:
3770 c_parser_skip_until_found stops at a closing nesting
3771 delimiter without consuming it, but here we need to consume
3772 it to proceed further. */
3773 c_parser_error (parser, "expected statement");
3774 c_parser_consume_token (parser);
3775 break;
3776 case CPP_PRAGMA:
3777 c_parser_pragma (parser, pragma_stmt);
3778 break;
3779 default:
3780 expr_stmt:
3781 stmt = c_finish_expr_stmt (c_parser_expression_conv (parser).value);
3782 expect_semicolon:
3783 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
3784 break;
3786 /* Two cases cannot and do not have line numbers associated: If stmt
3787 is degenerate, such as "2;", then stmt is an INTEGER_CST, which
3788 cannot hold line numbers. But that's OK because the statement
3789 will either be changed to a MODIFY_EXPR during gimplification of
3790 the statement expr, or discarded. If stmt was compound, but
3791 without new variables, we will have skipped the creation of a
3792 BIND and will have a bare STATEMENT_LIST. But that's OK because
3793 (recursively) all of the component statements should already have
3794 line numbers assigned. ??? Can we discard no-op statements
3795 earlier? */
3796 if (stmt && CAN_HAVE_LOCATION_P (stmt))
3797 SET_EXPR_LOCATION (stmt, loc);
3800 /* Parse a parenthesized condition from an if, do or while statement.
3802 condition:
3803 ( expression )
3805 static tree
3806 c_parser_paren_condition (c_parser *parser)
3808 location_t loc;
3809 tree cond;
3810 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3811 return error_mark_node;
3812 loc = c_parser_peek_token (parser)->location;
3813 cond = c_objc_common_truthvalue_conversion
3814 (c_parser_expression_conv (parser).value);
3815 if (CAN_HAVE_LOCATION_P (cond))
3816 SET_EXPR_LOCATION (cond, loc);
3817 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
3818 return cond;
3821 /* Parse a statement which is a block in C99. */
3823 static tree
3824 c_parser_c99_block_statement (c_parser *parser)
3826 tree block = c_begin_compound_stmt (flag_isoc99);
3827 c_parser_statement (parser);
3828 return c_end_compound_stmt (block, flag_isoc99);
3831 /* Parse the body of an if statement or the else half thereof. This
3832 is just parsing a statement but (a) it is a block in C99, (b) we
3833 track whether the body is an if statement for the sake of
3834 -Wparentheses warnings, (c) we handle an empty body specially for
3835 the sake of -Wempty-body warnings. */
3837 static tree
3838 c_parser_if_body (c_parser *parser, bool *if_p)
3840 tree block = c_begin_compound_stmt (flag_isoc99);
3841 while (c_parser_next_token_is_keyword (parser, RID_CASE)
3842 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
3843 || (c_parser_next_token_is (parser, CPP_NAME)
3844 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
3845 c_parser_label (parser);
3846 *if_p = c_parser_next_token_is_keyword (parser, RID_IF);
3847 if (warn_empty_body && c_parser_next_token_is (parser, CPP_SEMICOLON))
3848 add_stmt (build_empty_stmt ());
3849 c_parser_statement_after_labels (parser);
3850 return c_end_compound_stmt (block, flag_isoc99);
3853 /* Parse an if statement (C90 6.6.4, C99 6.8.4).
3855 if-statement:
3856 if ( expression ) statement
3857 if ( expression ) statement else statement
3860 static void
3861 c_parser_if_statement (c_parser *parser)
3863 tree block;
3864 location_t loc;
3865 tree cond;
3866 bool first_if = false, second_if = false;
3867 tree first_body, second_body;
3868 gcc_assert (c_parser_next_token_is_keyword (parser, RID_IF));
3869 c_parser_consume_token (parser);
3870 block = c_begin_compound_stmt (flag_isoc99);
3871 loc = c_parser_peek_token (parser)->location;
3872 cond = c_parser_paren_condition (parser);
3873 first_body = c_parser_if_body (parser, &first_if);
3874 if (c_parser_next_token_is_keyword (parser, RID_ELSE))
3876 c_parser_consume_token (parser);
3877 second_body = c_parser_if_body (parser, &second_if);
3879 else
3880 second_body = NULL_TREE;
3881 c_finish_if_stmt (loc, cond, first_body, second_body, first_if);
3882 add_stmt (c_end_compound_stmt (block, flag_isoc99));
3885 /* Parse a switch statement (C90 6.6.4, C99 6.8.4).
3887 switch-statement:
3888 switch (expression) statement
3891 static void
3892 c_parser_switch_statement (c_parser *parser)
3894 tree block, expr, body, save_break;
3895 gcc_assert (c_parser_next_token_is_keyword (parser, RID_SWITCH));
3896 c_parser_consume_token (parser);
3897 block = c_begin_compound_stmt (flag_isoc99);
3898 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3900 expr = c_parser_expression (parser).value;
3901 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
3903 else
3904 expr = error_mark_node;
3905 c_start_case (expr);
3906 save_break = c_break_label;
3907 c_break_label = NULL_TREE;
3908 body = c_parser_c99_block_statement (parser);
3909 c_finish_case (body);
3910 if (c_break_label)
3911 add_stmt (build1 (LABEL_EXPR, void_type_node, c_break_label));
3912 c_break_label = save_break;
3913 add_stmt (c_end_compound_stmt (block, flag_isoc99));
3916 /* Parse a while statement (C90 6.6.5, C99 6.8.5).
3918 while-statement:
3919 while (expression) statement
3922 static void
3923 c_parser_while_statement (c_parser *parser)
3925 tree block, cond, body, save_break, save_cont;
3926 location_t loc;
3927 gcc_assert (c_parser_next_token_is_keyword (parser, RID_WHILE));
3928 c_parser_consume_token (parser);
3929 block = c_begin_compound_stmt (flag_isoc99);
3930 loc = c_parser_peek_token (parser)->location;
3931 cond = c_parser_paren_condition (parser);
3932 save_break = c_break_label;
3933 c_break_label = NULL_TREE;
3934 save_cont = c_cont_label;
3935 c_cont_label = NULL_TREE;
3936 body = c_parser_c99_block_statement (parser);
3937 c_finish_loop (loc, cond, NULL, body, c_break_label, c_cont_label, true);
3938 add_stmt (c_end_compound_stmt (block, flag_isoc99));
3939 c_break_label = save_break;
3940 c_cont_label = save_cont;
3943 /* Parse a do statement (C90 6.6.5, C99 6.8.5).
3945 do-statement:
3946 do statement while ( expression ) ;
3949 static void
3950 c_parser_do_statement (c_parser *parser)
3952 tree block, cond, body, save_break, save_cont, new_break, new_cont;
3953 location_t loc;
3954 gcc_assert (c_parser_next_token_is_keyword (parser, RID_DO));
3955 c_parser_consume_token (parser);
3956 block = c_begin_compound_stmt (flag_isoc99);
3957 loc = c_parser_peek_token (parser)->location;
3958 save_break = c_break_label;
3959 c_break_label = NULL_TREE;
3960 save_cont = c_cont_label;
3961 c_cont_label = NULL_TREE;
3962 body = c_parser_c99_block_statement (parser);
3963 c_parser_require_keyword (parser, RID_WHILE, "expected %<while%>");
3964 new_break = c_break_label;
3965 c_break_label = save_break;
3966 new_cont = c_cont_label;
3967 c_cont_label = save_cont;
3968 cond = c_parser_paren_condition (parser);
3969 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
3970 c_parser_skip_to_end_of_block_or_statement (parser);
3971 c_finish_loop (loc, cond, NULL, body, new_break, new_cont, false);
3972 add_stmt (c_end_compound_stmt (block, flag_isoc99));
3975 /* Parse a for statement (C90 6.6.5, C99 6.8.5).
3977 for-statement:
3978 for ( expression[opt] ; expression[opt] ; expression[opt] ) statement
3979 for ( nested-declaration expression[opt] ; expression[opt] ) statement
3981 The form with a declaration is new in C99.
3983 ??? In accordance with the old parser, the declaration may be a
3984 nested function, which is then rejected in check_for_loop_decls,
3985 but does it make any sense for this to be included in the grammar?
3986 Note in particular that the nested function does not include a
3987 trailing ';', whereas the "declaration" production includes one.
3988 Also, can we reject bad declarations earlier and cheaper than
3989 check_for_loop_decls? */
3991 static void
3992 c_parser_for_statement (c_parser *parser)
3994 tree block, cond, incr, save_break, save_cont, body;
3995 location_t loc;
3996 gcc_assert (c_parser_next_token_is_keyword (parser, RID_FOR));
3997 loc = c_parser_peek_token (parser)->location;
3998 c_parser_consume_token (parser);
3999 block = c_begin_compound_stmt (flag_isoc99);
4000 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4002 /* Parse the initialization declaration or expression. */
4003 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4005 c_parser_consume_token (parser);
4006 c_finish_expr_stmt (NULL_TREE);
4008 else if (c_parser_next_token_starts_declspecs (parser))
4010 c_parser_declaration_or_fndef (parser, true, true, true, true);
4011 check_for_loop_decls ();
4013 else if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
4015 /* __extension__ can start a declaration, but is also an
4016 unary operator that can start an expression. Consume all
4017 but the last of a possible series of __extension__ to
4018 determine which. */
4019 while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
4020 && (c_parser_peek_2nd_token (parser)->keyword
4021 == RID_EXTENSION))
4022 c_parser_consume_token (parser);
4023 if (c_token_starts_declspecs (c_parser_peek_2nd_token (parser)))
4025 int ext;
4026 ext = disable_extension_diagnostics ();
4027 c_parser_consume_token (parser);
4028 c_parser_declaration_or_fndef (parser, true, true, true, true);
4029 restore_extension_diagnostics (ext);
4030 check_for_loop_decls ();
4032 else
4033 goto init_expr;
4035 else
4037 init_expr:
4038 c_finish_expr_stmt (c_parser_expression (parser).value);
4039 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
4041 /* Parse the loop condition. */
4042 loc = c_parser_peek_token (parser)->location;
4043 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4045 c_parser_consume_token (parser);
4046 cond = NULL_TREE;
4048 else
4050 tree ocond = c_parser_expression_conv (parser).value;
4051 cond = c_objc_common_truthvalue_conversion (ocond);
4052 if (CAN_HAVE_LOCATION_P (cond))
4053 SET_EXPR_LOCATION (cond, loc);
4054 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
4056 /* Parse the increment expression. */
4057 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4058 incr = c_process_expr_stmt (NULL_TREE);
4059 else
4060 incr = c_process_expr_stmt (c_parser_expression (parser).value);
4061 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
4063 else
4065 cond = error_mark_node;
4066 incr = error_mark_node;
4068 save_break = c_break_label;
4069 c_break_label = NULL_TREE;
4070 save_cont = c_cont_label;
4071 c_cont_label = NULL_TREE;
4072 body = c_parser_c99_block_statement (parser);
4073 c_finish_loop (loc, cond, incr, body, c_break_label, c_cont_label, true);
4074 add_stmt (c_end_compound_stmt (block, flag_isoc99));
4075 c_break_label = save_break;
4076 c_cont_label = save_cont;
4079 /* Parse an asm statement, a GNU extension. This is a full-blown asm
4080 statement with inputs, outputs, clobbers, and volatile tag
4081 allowed.
4083 asm-statement:
4084 asm type-qualifier[opt] ( asm-argument ) ;
4086 asm-argument:
4087 asm-string-literal
4088 asm-string-literal : asm-operands[opt]
4089 asm-string-literal : asm-operands[opt] : asm-operands[opt]
4090 asm-string-literal : asm-operands[opt] : asm-operands[opt] : asm-clobbers
4092 Qualifiers other than volatile are accepted in the syntax but
4093 warned for. */
4095 static tree
4096 c_parser_asm_statement (c_parser *parser)
4098 tree quals, str, outputs, inputs, clobbers, ret;
4099 bool simple;
4100 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM));
4101 c_parser_consume_token (parser);
4102 if (c_parser_next_token_is_keyword (parser, RID_VOLATILE))
4104 quals = c_parser_peek_token (parser)->value;
4105 c_parser_consume_token (parser);
4107 else if (c_parser_next_token_is_keyword (parser, RID_CONST)
4108 || c_parser_next_token_is_keyword (parser, RID_RESTRICT))
4110 warning (0, "%E qualifier ignored on asm",
4111 c_parser_peek_token (parser)->value);
4112 quals = NULL_TREE;
4113 c_parser_consume_token (parser);
4115 else
4116 quals = NULL_TREE;
4117 /* ??? Follow the C++ parser rather than using the
4118 c_lex_string_translate kludge. */
4119 c_lex_string_translate = 0;
4120 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4122 c_lex_string_translate = 1;
4123 return NULL_TREE;
4125 str = c_parser_asm_string_literal (parser);
4126 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4128 simple = true;
4129 outputs = NULL_TREE;
4130 inputs = NULL_TREE;
4131 clobbers = NULL_TREE;
4132 goto done_asm;
4134 if (!c_parser_require (parser, CPP_COLON, "expected %<:%> or %<)%>"))
4136 c_lex_string_translate = 1;
4137 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4138 return NULL_TREE;
4140 simple = false;
4141 /* Parse outputs. */
4142 if (c_parser_next_token_is (parser, CPP_COLON)
4143 || c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4144 outputs = NULL_TREE;
4145 else
4146 outputs = c_parser_asm_operands (parser, false);
4147 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4149 inputs = NULL_TREE;
4150 clobbers = NULL_TREE;
4151 goto done_asm;
4153 if (!c_parser_require (parser, CPP_COLON, "expected %<:%> or %<)%>"))
4155 c_lex_string_translate = 1;
4156 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4157 return NULL_TREE;
4159 /* Parse inputs. */
4160 if (c_parser_next_token_is (parser, CPP_COLON)
4161 || c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4162 inputs = NULL_TREE;
4163 else
4164 inputs = c_parser_asm_operands (parser, true);
4165 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4167 clobbers = NULL_TREE;
4168 goto done_asm;
4170 if (!c_parser_require (parser, CPP_COLON, "expected %<:%> or %<)%>"))
4172 c_lex_string_translate = 1;
4173 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4174 return NULL_TREE;
4176 /* Parse clobbers. */
4177 clobbers = c_parser_asm_clobbers (parser);
4178 done_asm:
4179 c_lex_string_translate = 1;
4180 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
4182 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4183 return NULL_TREE;
4185 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
4186 c_parser_skip_to_end_of_block_or_statement (parser);
4187 ret = build_asm_stmt (quals, build_asm_expr (str, outputs, inputs,
4188 clobbers, simple));
4189 return ret;
4192 /* Parse asm operands, a GNU extension. If CONVERT_P (for inputs but
4193 not outputs), apply the default conversion of functions and arrays
4194 to pointers.
4196 asm-operands:
4197 asm-operand
4198 asm-operands , asm-operand
4200 asm-operand:
4201 asm-string-literal ( expression )
4202 [ identifier ] asm-string-literal ( expression )
4205 static tree
4206 c_parser_asm_operands (c_parser *parser, bool convert_p)
4208 tree list = NULL_TREE;
4209 while (true)
4211 tree name, str;
4212 struct c_expr expr;
4213 if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
4215 c_parser_consume_token (parser);
4216 if (c_parser_next_token_is (parser, CPP_NAME))
4218 tree id = c_parser_peek_token (parser)->value;
4219 c_parser_consume_token (parser);
4220 name = build_string (IDENTIFIER_LENGTH (id),
4221 IDENTIFIER_POINTER (id));
4223 else
4225 c_parser_error (parser, "expected identifier");
4226 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
4227 return NULL_TREE;
4229 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
4230 "expected %<]%>");
4232 else
4233 name = NULL_TREE;
4234 str = c_parser_asm_string_literal (parser);
4235 if (str == NULL_TREE)
4236 return NULL_TREE;
4237 c_lex_string_translate = 1;
4238 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4240 c_lex_string_translate = 0;
4241 return NULL_TREE;
4243 expr = c_parser_expression (parser);
4244 if (convert_p)
4245 expr = default_function_array_conversion (expr);
4246 c_lex_string_translate = 0;
4247 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
4249 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4250 return NULL_TREE;
4252 list = chainon (list, build_tree_list (build_tree_list (name, str),
4253 expr.value));
4254 if (c_parser_next_token_is (parser, CPP_COMMA))
4255 c_parser_consume_token (parser);
4256 else
4257 break;
4259 return list;
4262 /* Parse asm clobbers, a GNU extension.
4264 asm-clobbers:
4265 asm-string-literal
4266 asm-clobbers , asm-string-literal
4269 static tree
4270 c_parser_asm_clobbers (c_parser *parser)
4272 tree list = NULL_TREE;
4273 while (true)
4275 tree str = c_parser_asm_string_literal (parser);
4276 if (str)
4277 list = tree_cons (NULL_TREE, str, list);
4278 else
4279 return NULL_TREE;
4280 if (c_parser_next_token_is (parser, CPP_COMMA))
4281 c_parser_consume_token (parser);
4282 else
4283 break;
4285 return list;
4288 /* Parse an expression other than a compound expression; that is, an
4289 assignment expression (C90 6.3.16, C99 6.5.16). If AFTER is not
4290 NULL then it is an Objective-C message expression which is the
4291 primary-expression starting the expression as an initializer.
4293 assignment-expression:
4294 conditional-expression
4295 unary-expression assignment-operator assignment-expression
4297 assignment-operator: one of
4298 = *= /= %= += -= <<= >>= &= ^= |=
4300 In GNU C we accept any conditional expression on the LHS and
4301 diagnose the invalid lvalue rather than producing a syntax
4302 error. */
4304 static struct c_expr
4305 c_parser_expr_no_commas (c_parser *parser, struct c_expr *after)
4307 struct c_expr lhs, rhs, ret;
4308 enum tree_code code;
4309 gcc_assert (!after || c_dialect_objc ());
4310 lhs = c_parser_conditional_expression (parser, after);
4311 switch (c_parser_peek_token (parser)->type)
4313 case CPP_EQ:
4314 code = NOP_EXPR;
4315 break;
4316 case CPP_MULT_EQ:
4317 code = MULT_EXPR;
4318 break;
4319 case CPP_DIV_EQ:
4320 code = TRUNC_DIV_EXPR;
4321 break;
4322 case CPP_MOD_EQ:
4323 code = TRUNC_MOD_EXPR;
4324 break;
4325 case CPP_PLUS_EQ:
4326 code = PLUS_EXPR;
4327 break;
4328 case CPP_MINUS_EQ:
4329 code = MINUS_EXPR;
4330 break;
4331 case CPP_LSHIFT_EQ:
4332 code = LSHIFT_EXPR;
4333 break;
4334 case CPP_RSHIFT_EQ:
4335 code = RSHIFT_EXPR;
4336 break;
4337 case CPP_AND_EQ:
4338 code = BIT_AND_EXPR;
4339 break;
4340 case CPP_XOR_EQ:
4341 code = BIT_XOR_EXPR;
4342 break;
4343 case CPP_OR_EQ:
4344 code = BIT_IOR_EXPR;
4345 break;
4346 default:
4347 return lhs;
4349 c_parser_consume_token (parser);
4350 rhs = c_parser_expr_no_commas (parser, NULL);
4351 rhs = default_function_array_conversion (rhs);
4352 ret.value = build_modify_expr (lhs.value, code, rhs.value);
4353 if (code == NOP_EXPR)
4354 ret.original_code = MODIFY_EXPR;
4355 else
4357 TREE_NO_WARNING (ret.value) = 1;
4358 ret.original_code = ERROR_MARK;
4360 return ret;
4363 /* Parse a conditional expression (C90 6.3.15, C99 6.5.15). If AFTER
4364 is not NULL then it is an Objective-C message expression which is
4365 the primary-expression starting the expression as an initializer.
4367 conditional-expression:
4368 logical-OR-expression
4369 logical-OR-expression ? expression : conditional-expression
4371 GNU extensions:
4373 conditional-expression:
4374 logical-OR-expression ? : conditional-expression
4377 static struct c_expr
4378 c_parser_conditional_expression (c_parser *parser, struct c_expr *after)
4380 struct c_expr cond, exp1, exp2, ret;
4381 gcc_assert (!after || c_dialect_objc ());
4382 cond = c_parser_binary_expression (parser, after);
4383 if (c_parser_next_token_is_not (parser, CPP_QUERY))
4384 return cond;
4385 cond = default_function_array_conversion (cond);
4386 c_parser_consume_token (parser);
4387 if (c_parser_next_token_is (parser, CPP_COLON))
4389 if (pedantic)
4390 pedwarn ("ISO C forbids omitting the middle term of a ?: expression");
4391 /* Make sure first operand is calculated only once. */
4392 exp1.value = save_expr (default_conversion (cond.value));
4393 cond.value = c_objc_common_truthvalue_conversion (exp1.value);
4394 skip_evaluation += cond.value == truthvalue_true_node;
4396 else
4398 cond.value
4399 = c_objc_common_truthvalue_conversion
4400 (default_conversion (cond.value));
4401 skip_evaluation += cond.value == truthvalue_false_node;
4402 exp1 = c_parser_expression_conv (parser);
4403 skip_evaluation += ((cond.value == truthvalue_true_node)
4404 - (cond.value == truthvalue_false_node));
4406 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
4408 skip_evaluation -= cond.value == truthvalue_true_node;
4409 ret.value = error_mark_node;
4410 ret.original_code = ERROR_MARK;
4411 return ret;
4413 exp2 = c_parser_conditional_expression (parser, NULL);
4414 exp2 = default_function_array_conversion (exp2);
4415 skip_evaluation -= cond.value == truthvalue_true_node;
4416 ret.value = build_conditional_expr (cond.value, exp1.value, exp2.value);
4417 ret.original_code = ERROR_MARK;
4418 return ret;
4421 /* Parse a binary expression; that is, a logical-OR-expression (C90
4422 6.3.5-6.3.14, C99 6.5.5-6.5.14). If AFTER is not NULL then it is
4423 an Objective-C message expression which is the primary-expression
4424 starting the expression as an initializer.
4426 multiplicative-expression:
4427 cast-expression
4428 multiplicative-expression * cast-expression
4429 multiplicative-expression / cast-expression
4430 multiplicative-expression % cast-expression
4432 additive-expression:
4433 multiplicative-expression
4434 additive-expression + multiplicative-expression
4435 additive-expression - multiplicative-expression
4437 shift-expression:
4438 additive-expression
4439 shift-expression << additive-expression
4440 shift-expression >> additive-expression
4442 relational-expression:
4443 shift-expression
4444 relational-expression < shift-expression
4445 relational-expression > shift-expression
4446 relational-expression <= shift-expression
4447 relational-expression >= shift-expression
4449 equality-expression:
4450 relational-expression
4451 equality-expression == relational-expression
4452 equality-expression != relational-expression
4454 AND-expression:
4455 equality-expression
4456 AND-expression & equality-expression
4458 exclusive-OR-expression:
4459 AND-expression
4460 exclusive-OR-expression ^ AND-expression
4462 inclusive-OR-expression:
4463 exclusive-OR-expression
4464 inclusive-OR-expression | exclusive-OR-expression
4466 logical-AND-expression:
4467 inclusive-OR-expression
4468 logical-AND-expression && inclusive-OR-expression
4470 logical-OR-expression:
4471 logical-AND-expression
4472 logical-OR-expression || logical-AND-expression
4475 static struct c_expr
4476 c_parser_binary_expression (c_parser *parser, struct c_expr *after)
4478 /* A binary expression is parsed using operator-precedence parsing,
4479 with the operands being cast expressions. All the binary
4480 operators are left-associative. Thus a binary expression is of
4481 form:
4483 E0 op1 E1 op2 E2 ...
4485 which we represent on a stack. On the stack, the precedence
4486 levels are strictly increasing. When a new operator is
4487 encountered of higher precedence than that at the top of the
4488 stack, it is pushed; its LHS is the top expression, and its RHS
4489 is everything parsed until it is popped. When a new operator is
4490 encountered with precedence less than or equal to that at the top
4491 of the stack, triples E[i-1] op[i] E[i] are popped and replaced
4492 by the result of the operation until the operator at the top of
4493 the stack has lower precedence than the new operator or there is
4494 only one element on the stack; then the top expression is the LHS
4495 of the new operator. In the case of logical AND and OR
4496 expressions, we also need to adjust skip_evaluation as
4497 appropriate when the operators are pushed and popped. */
4499 /* The precedence levels, where 0 is a dummy lowest level used for
4500 the bottom of the stack. */
4501 enum prec {
4502 PREC_NONE,
4503 PREC_LOGOR,
4504 PREC_LOGAND,
4505 PREC_BITOR,
4506 PREC_BITXOR,
4507 PREC_BITAND,
4508 PREC_EQ,
4509 PREC_REL,
4510 PREC_SHIFT,
4511 PREC_ADD,
4512 PREC_MULT,
4513 NUM_PRECS
4515 struct {
4516 /* The expression at this stack level. */
4517 struct c_expr expr;
4518 /* The precedence of the operator on its left, PREC_NONE at the
4519 bottom of the stack. */
4520 enum prec prec;
4521 /* The operation on its left. */
4522 enum tree_code op;
4523 } stack[NUM_PRECS];
4524 int sp;
4525 #define POP \
4526 do { \
4527 switch (stack[sp].op) \
4529 case TRUTH_ANDIF_EXPR: \
4530 skip_evaluation -= stack[sp - 1].expr.value == truthvalue_false_node; \
4531 break; \
4532 case TRUTH_ORIF_EXPR: \
4533 skip_evaluation -= stack[sp - 1].expr.value == truthvalue_true_node; \
4534 break; \
4535 default: \
4536 break; \
4538 stack[sp - 1].expr \
4539 = default_function_array_conversion (stack[sp - 1].expr); \
4540 stack[sp].expr \
4541 = default_function_array_conversion (stack[sp].expr); \
4542 stack[sp - 1].expr = parser_build_binary_op (stack[sp].op, \
4543 stack[sp - 1].expr, \
4544 stack[sp].expr); \
4545 sp--; \
4546 } while (0)
4547 gcc_assert (!after || c_dialect_objc ());
4548 stack[0].expr = c_parser_cast_expression (parser, after);
4549 stack[0].prec = PREC_NONE;
4550 sp = 0;
4551 while (true)
4553 enum prec oprec;
4554 enum tree_code ocode;
4555 if (parser->error)
4556 goto out;
4557 switch (c_parser_peek_token (parser)->type)
4559 case CPP_MULT:
4560 oprec = PREC_MULT;
4561 ocode = MULT_EXPR;
4562 break;
4563 case CPP_DIV:
4564 oprec = PREC_MULT;
4565 ocode = TRUNC_DIV_EXPR;
4566 break;
4567 case CPP_MOD:
4568 oprec = PREC_MULT;
4569 ocode = TRUNC_MOD_EXPR;
4570 break;
4571 case CPP_PLUS:
4572 oprec = PREC_ADD;
4573 ocode = PLUS_EXPR;
4574 break;
4575 case CPP_MINUS:
4576 oprec = PREC_ADD;
4577 ocode = MINUS_EXPR;
4578 break;
4579 case CPP_LSHIFT:
4580 oprec = PREC_SHIFT;
4581 ocode = LSHIFT_EXPR;
4582 break;
4583 case CPP_RSHIFT:
4584 oprec = PREC_SHIFT;
4585 ocode = RSHIFT_EXPR;
4586 break;
4587 case CPP_LESS:
4588 oprec = PREC_REL;
4589 ocode = LT_EXPR;
4590 break;
4591 case CPP_GREATER:
4592 oprec = PREC_REL;
4593 ocode = GT_EXPR;
4594 break;
4595 case CPP_LESS_EQ:
4596 oprec = PREC_REL;
4597 ocode = LE_EXPR;
4598 break;
4599 case CPP_GREATER_EQ:
4600 oprec = PREC_REL;
4601 ocode = GE_EXPR;
4602 break;
4603 case CPP_EQ_EQ:
4604 oprec = PREC_EQ;
4605 ocode = EQ_EXPR;
4606 break;
4607 case CPP_NOT_EQ:
4608 oprec = PREC_EQ;
4609 ocode = NE_EXPR;
4610 break;
4611 case CPP_AND:
4612 oprec = PREC_BITAND;
4613 ocode = BIT_AND_EXPR;
4614 break;
4615 case CPP_XOR:
4616 oprec = PREC_BITXOR;
4617 ocode = BIT_XOR_EXPR;
4618 break;
4619 case CPP_OR:
4620 oprec = PREC_BITOR;
4621 ocode = BIT_IOR_EXPR;
4622 break;
4623 case CPP_AND_AND:
4624 oprec = PREC_LOGAND;
4625 ocode = TRUTH_ANDIF_EXPR;
4626 break;
4627 case CPP_OR_OR:
4628 oprec = PREC_LOGOR;
4629 ocode = TRUTH_ORIF_EXPR;
4630 break;
4631 default:
4632 /* Not a binary operator, so end of the binary
4633 expression. */
4634 goto out;
4636 c_parser_consume_token (parser);
4637 while (oprec <= stack[sp].prec)
4638 POP;
4639 switch (ocode)
4641 case TRUTH_ANDIF_EXPR:
4642 stack[sp].expr
4643 = default_function_array_conversion (stack[sp].expr);
4644 stack[sp].expr.value = c_objc_common_truthvalue_conversion
4645 (default_conversion (stack[sp].expr.value));
4646 skip_evaluation += stack[sp].expr.value == truthvalue_false_node;
4647 break;
4648 case TRUTH_ORIF_EXPR:
4649 stack[sp].expr
4650 = default_function_array_conversion (stack[sp].expr);
4651 stack[sp].expr.value = c_objc_common_truthvalue_conversion
4652 (default_conversion (stack[sp].expr.value));
4653 skip_evaluation += stack[sp].expr.value == truthvalue_true_node;
4654 break;
4655 default:
4656 break;
4658 sp++;
4659 stack[sp].expr = c_parser_cast_expression (parser, NULL);
4660 stack[sp].prec = oprec;
4661 stack[sp].op = ocode;
4663 out:
4664 while (sp > 0)
4665 POP;
4666 return stack[0].expr;
4667 #undef POP
4670 /* Parse a cast expression (C90 6.3.4, C99 6.5.4). If AFTER is not
4671 NULL then it is an Objective-C message expression which is the
4672 primary-expression starting the expression as an initializer.
4674 cast-expression:
4675 unary-expression
4676 ( type-name ) unary-expression
4679 static struct c_expr
4680 c_parser_cast_expression (c_parser *parser, struct c_expr *after)
4682 gcc_assert (!after || c_dialect_objc ());
4683 if (after)
4684 return c_parser_postfix_expression_after_primary (parser, *after);
4685 /* If the expression begins with a parenthesized type name, it may
4686 be either a cast or a compound literal; we need to see whether
4687 the next character is '{' to tell the difference. If not, it is
4688 an unary expression. */
4689 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
4690 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
4692 struct c_type_name *type_name;
4693 struct c_expr ret;
4694 struct c_expr expr;
4695 c_parser_consume_token (parser);
4696 type_name = c_parser_type_name (parser);
4697 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
4698 if (type_name == NULL)
4700 ret.value = error_mark_node;
4701 ret.original_code = ERROR_MARK;
4702 return ret;
4705 /* Save casted types in the function's used types hash table. */
4706 used_types_insert (type_name->specs->type);
4708 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
4709 return c_parser_postfix_expression_after_paren_type (parser,
4710 type_name);
4711 expr = c_parser_cast_expression (parser, NULL);
4712 expr = default_function_array_conversion (expr);
4713 ret.value = c_cast_expr (type_name, expr.value);
4714 ret.original_code = ERROR_MARK;
4715 return ret;
4717 else
4718 return c_parser_unary_expression (parser);
4721 /* Parse an unary expression (C90 6.3.3, C99 6.5.3).
4723 unary-expression:
4724 postfix-expression
4725 ++ unary-expression
4726 -- unary-expression
4727 unary-operator cast-expression
4728 sizeof unary-expression
4729 sizeof ( type-name )
4731 unary-operator: one of
4732 & * + - ~ !
4734 GNU extensions:
4736 unary-expression:
4737 __alignof__ unary-expression
4738 __alignof__ ( type-name )
4739 && identifier
4741 unary-operator: one of
4742 __extension__ __real__ __imag__
4744 In addition, the GNU syntax treats ++ and -- as unary operators, so
4745 they may be applied to cast expressions with errors for non-lvalues
4746 given later. */
4748 static struct c_expr
4749 c_parser_unary_expression (c_parser *parser)
4751 int ext;
4752 struct c_expr ret, op;
4753 switch (c_parser_peek_token (parser)->type)
4755 case CPP_PLUS_PLUS:
4756 c_parser_consume_token (parser);
4757 op = c_parser_cast_expression (parser, NULL);
4758 op = default_function_array_conversion (op);
4759 return parser_build_unary_op (PREINCREMENT_EXPR, op);
4760 case CPP_MINUS_MINUS:
4761 c_parser_consume_token (parser);
4762 op = c_parser_cast_expression (parser, NULL);
4763 op = default_function_array_conversion (op);
4764 return parser_build_unary_op (PREDECREMENT_EXPR, op);
4765 case CPP_AND:
4766 c_parser_consume_token (parser);
4767 return parser_build_unary_op (ADDR_EXPR,
4768 c_parser_cast_expression (parser, NULL));
4769 case CPP_MULT:
4770 c_parser_consume_token (parser);
4771 op = c_parser_cast_expression (parser, NULL);
4772 op = default_function_array_conversion (op);
4773 ret.value = build_indirect_ref (op.value, "unary *");
4774 ret.original_code = ERROR_MARK;
4775 return ret;
4776 case CPP_PLUS:
4777 c_parser_consume_token (parser);
4778 if (!c_dialect_objc () && !in_system_header)
4779 warning (OPT_Wtraditional,
4780 "traditional C rejects the unary plus operator");
4781 op = c_parser_cast_expression (parser, NULL);
4782 op = default_function_array_conversion (op);
4783 return parser_build_unary_op (CONVERT_EXPR, op);
4784 case CPP_MINUS:
4785 c_parser_consume_token (parser);
4786 op = c_parser_cast_expression (parser, NULL);
4787 op = default_function_array_conversion (op);
4788 return parser_build_unary_op (NEGATE_EXPR, op);
4789 case CPP_COMPL:
4790 c_parser_consume_token (parser);
4791 op = c_parser_cast_expression (parser, NULL);
4792 op = default_function_array_conversion (op);
4793 return parser_build_unary_op (BIT_NOT_EXPR, op);
4794 case CPP_NOT:
4795 c_parser_consume_token (parser);
4796 op = c_parser_cast_expression (parser, NULL);
4797 op = default_function_array_conversion (op);
4798 return parser_build_unary_op (TRUTH_NOT_EXPR, op);
4799 case CPP_AND_AND:
4800 /* Refer to the address of a label as a pointer. */
4801 c_parser_consume_token (parser);
4802 if (c_parser_next_token_is (parser, CPP_NAME))
4804 ret.value = finish_label_address_expr
4805 (c_parser_peek_token (parser)->value);
4806 c_parser_consume_token (parser);
4808 else
4810 c_parser_error (parser, "expected identifier");
4811 ret.value = error_mark_node;
4813 ret.original_code = ERROR_MARK;
4814 return ret;
4815 case CPP_KEYWORD:
4816 switch (c_parser_peek_token (parser)->keyword)
4818 case RID_SIZEOF:
4819 return c_parser_sizeof_expression (parser);
4820 case RID_ALIGNOF:
4821 return c_parser_alignof_expression (parser);
4822 case RID_EXTENSION:
4823 c_parser_consume_token (parser);
4824 ext = disable_extension_diagnostics ();
4825 ret = c_parser_cast_expression (parser, NULL);
4826 restore_extension_diagnostics (ext);
4827 return ret;
4828 case RID_REALPART:
4829 c_parser_consume_token (parser);
4830 op = c_parser_cast_expression (parser, NULL);
4831 op = default_function_array_conversion (op);
4832 return parser_build_unary_op (REALPART_EXPR, op);
4833 case RID_IMAGPART:
4834 c_parser_consume_token (parser);
4835 op = c_parser_cast_expression (parser, NULL);
4836 op = default_function_array_conversion (op);
4837 return parser_build_unary_op (IMAGPART_EXPR, op);
4838 default:
4839 return c_parser_postfix_expression (parser);
4841 default:
4842 return c_parser_postfix_expression (parser);
4846 /* Parse a sizeof expression. */
4848 static struct c_expr
4849 c_parser_sizeof_expression (c_parser *parser)
4851 struct c_expr expr;
4852 gcc_assert (c_parser_next_token_is_keyword (parser, RID_SIZEOF));
4853 c_parser_consume_token (parser);
4854 skip_evaluation++;
4855 in_sizeof++;
4856 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
4857 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
4859 /* Either sizeof ( type-name ) or sizeof unary-expression
4860 starting with a compound literal. */
4861 struct c_type_name *type_name;
4862 c_parser_consume_token (parser);
4863 type_name = c_parser_type_name (parser);
4864 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
4865 if (type_name == NULL)
4867 struct c_expr ret;
4868 skip_evaluation--;
4869 in_sizeof--;
4870 ret.value = error_mark_node;
4871 ret.original_code = ERROR_MARK;
4872 return ret;
4874 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
4876 expr = c_parser_postfix_expression_after_paren_type (parser,
4877 type_name);
4878 goto sizeof_expr;
4880 /* sizeof ( type-name ). */
4881 skip_evaluation--;
4882 in_sizeof--;
4883 if (type_name->declarator->kind == cdk_array
4884 && type_name->declarator->u.array.vla_unspec_p)
4886 /* C99 6.7.5.2p4 */
4887 error ("%<[*]%> not allowed in other than a declaration");
4889 return c_expr_sizeof_type (type_name);
4891 else
4893 expr = c_parser_unary_expression (parser);
4894 sizeof_expr:
4895 skip_evaluation--;
4896 in_sizeof--;
4897 if (TREE_CODE (expr.value) == COMPONENT_REF
4898 && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1)))
4899 error ("%<sizeof%> applied to a bit-field");
4900 return c_expr_sizeof_expr (expr);
4904 /* Parse an alignof expression. */
4906 static struct c_expr
4907 c_parser_alignof_expression (c_parser *parser)
4909 struct c_expr expr;
4910 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ALIGNOF));
4911 c_parser_consume_token (parser);
4912 skip_evaluation++;
4913 in_alignof++;
4914 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
4915 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
4917 /* Either __alignof__ ( type-name ) or __alignof__
4918 unary-expression starting with a compound literal. */
4919 struct c_type_name *type_name;
4920 struct c_expr ret;
4921 c_parser_consume_token (parser);
4922 type_name = c_parser_type_name (parser);
4923 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
4924 if (type_name == NULL)
4926 struct c_expr ret;
4927 skip_evaluation--;
4928 in_alignof--;
4929 ret.value = error_mark_node;
4930 ret.original_code = ERROR_MARK;
4931 return ret;
4933 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
4935 expr = c_parser_postfix_expression_after_paren_type (parser,
4936 type_name);
4937 goto alignof_expr;
4939 /* alignof ( type-name ). */
4940 skip_evaluation--;
4941 in_alignof--;
4942 ret.value = c_alignof (groktypename (type_name));
4943 ret.original_code = ERROR_MARK;
4944 return ret;
4946 else
4948 struct c_expr ret;
4949 expr = c_parser_unary_expression (parser);
4950 alignof_expr:
4951 skip_evaluation--;
4952 in_alignof--;
4953 ret.value = c_alignof_expr (expr.value);
4954 ret.original_code = ERROR_MARK;
4955 return ret;
4959 /* Parse a postfix expression (C90 6.3.1-6.3.2, C99 6.5.1-6.5.2).
4961 postfix-expression:
4962 primary-expression
4963 postfix-expression [ expression ]
4964 postfix-expression ( argument-expression-list[opt] )
4965 postfix-expression . identifier
4966 postfix-expression -> identifier
4967 postfix-expression ++
4968 postfix-expression --
4969 ( type-name ) { initializer-list }
4970 ( type-name ) { initializer-list , }
4972 argument-expression-list:
4973 argument-expression
4974 argument-expression-list , argument-expression
4976 primary-expression:
4977 identifier
4978 constant
4979 string-literal
4980 ( expression )
4982 GNU extensions:
4984 primary-expression:
4985 __func__
4986 (treated as a keyword in GNU C)
4987 __FUNCTION__
4988 __PRETTY_FUNCTION__
4989 ( compound-statement )
4990 __builtin_va_arg ( assignment-expression , type-name )
4991 __builtin_offsetof ( type-name , offsetof-member-designator )
4992 __builtin_choose_expr ( assignment-expression ,
4993 assignment-expression ,
4994 assignment-expression )
4995 __builtin_types_compatible_p ( type-name , type-name )
4997 offsetof-member-designator:
4998 identifier
4999 offsetof-member-designator . identifier
5000 offsetof-member-designator [ expression ]
5002 Objective-C:
5004 primary-expression:
5005 [ objc-receiver objc-message-args ]
5006 @selector ( objc-selector-arg )
5007 @protocol ( identifier )
5008 @encode ( type-name )
5009 objc-string-literal
5012 static struct c_expr
5013 c_parser_postfix_expression (c_parser *parser)
5015 struct c_expr expr, e1, e2, e3;
5016 struct c_type_name *t1, *t2;
5017 switch (c_parser_peek_token (parser)->type)
5019 case CPP_NUMBER:
5020 case CPP_CHAR:
5021 case CPP_WCHAR:
5022 expr.value = c_parser_peek_token (parser)->value;
5023 expr.original_code = ERROR_MARK;
5024 c_parser_consume_token (parser);
5025 break;
5026 case CPP_STRING:
5027 case CPP_WSTRING:
5028 expr.value = c_parser_peek_token (parser)->value;
5029 expr.original_code = STRING_CST;
5030 c_parser_consume_token (parser);
5031 break;
5032 case CPP_OBJC_STRING:
5033 gcc_assert (c_dialect_objc ());
5034 expr.value
5035 = objc_build_string_object (c_parser_peek_token (parser)->value);
5036 expr.original_code = ERROR_MARK;
5037 c_parser_consume_token (parser);
5038 break;
5039 case CPP_NAME:
5040 if (c_parser_peek_token (parser)->id_kind != C_ID_ID)
5042 c_parser_error (parser, "expected expression");
5043 expr.value = error_mark_node;
5044 expr.original_code = ERROR_MARK;
5045 break;
5048 tree id = c_parser_peek_token (parser)->value;
5049 location_t loc = c_parser_peek_token (parser)->location;
5050 c_parser_consume_token (parser);
5051 expr.value = build_external_ref (id,
5052 (c_parser_peek_token (parser)->type
5053 == CPP_OPEN_PAREN), loc);
5054 expr.original_code = ERROR_MARK;
5056 break;
5057 case CPP_OPEN_PAREN:
5058 /* A parenthesized expression, statement expression or compound
5059 literal. */
5060 if (c_parser_peek_2nd_token (parser)->type == CPP_OPEN_BRACE)
5062 /* A statement expression. */
5063 tree stmt;
5064 c_parser_consume_token (parser);
5065 c_parser_consume_token (parser);
5066 if (cur_stmt_list == NULL)
5068 error ("braced-group within expression allowed "
5069 "only inside a function");
5070 parser->error = true;
5071 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
5072 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5073 expr.value = error_mark_node;
5074 expr.original_code = ERROR_MARK;
5075 break;
5077 stmt = c_begin_stmt_expr ();
5078 c_parser_compound_statement_nostart (parser);
5079 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5080 "expected %<)%>");
5081 if (pedantic)
5082 pedwarn ("ISO C forbids braced-groups within expressions");
5083 expr.value = c_finish_stmt_expr (stmt);
5084 expr.original_code = ERROR_MARK;
5086 else if (c_token_starts_typename (c_parser_peek_2nd_token (parser)))
5088 /* A compound literal. ??? Can we actually get here rather
5089 than going directly to
5090 c_parser_postfix_expression_after_paren_type from
5091 elsewhere? */
5092 struct c_type_name *type_name;
5093 c_parser_consume_token (parser);
5094 type_name = c_parser_type_name (parser);
5095 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5096 "expected %<)%>");
5097 if (type_name == NULL)
5099 expr.value = error_mark_node;
5100 expr.original_code = ERROR_MARK;
5102 else
5103 expr = c_parser_postfix_expression_after_paren_type (parser,
5104 type_name);
5106 else
5108 /* A parenthesized expression. */
5109 c_parser_consume_token (parser);
5110 expr = c_parser_expression (parser);
5111 if (TREE_CODE (expr.value) == MODIFY_EXPR)
5112 TREE_NO_WARNING (expr.value) = 1;
5113 expr.original_code = ERROR_MARK;
5114 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5115 "expected %<)%>");
5117 break;
5118 case CPP_KEYWORD:
5119 switch (c_parser_peek_token (parser)->keyword)
5121 case RID_FUNCTION_NAME:
5122 case RID_PRETTY_FUNCTION_NAME:
5123 case RID_C99_FUNCTION_NAME:
5124 expr.value = fname_decl (c_parser_peek_token (parser)->keyword,
5125 c_parser_peek_token (parser)->value);
5126 expr.original_code = ERROR_MARK;
5127 c_parser_consume_token (parser);
5128 break;
5129 case RID_VA_ARG:
5130 c_parser_consume_token (parser);
5131 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5133 expr.value = error_mark_node;
5134 expr.original_code = ERROR_MARK;
5135 break;
5137 e1 = c_parser_expr_no_commas (parser, NULL);
5138 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
5140 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5141 expr.value = error_mark_node;
5142 expr.original_code = ERROR_MARK;
5143 break;
5145 t1 = c_parser_type_name (parser);
5146 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5147 "expected %<)%>");
5148 if (t1 == NULL)
5150 expr.value = error_mark_node;
5151 expr.original_code = ERROR_MARK;
5153 else
5155 expr.value = build_va_arg (e1.value, groktypename (t1));
5156 expr.original_code = ERROR_MARK;
5158 break;
5159 case RID_OFFSETOF:
5160 c_parser_consume_token (parser);
5161 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5163 expr.value = error_mark_node;
5164 expr.original_code = ERROR_MARK;
5165 break;
5167 t1 = c_parser_type_name (parser);
5168 if (t1 == NULL)
5170 expr.value = error_mark_node;
5171 expr.original_code = ERROR_MARK;
5172 break;
5174 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
5176 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5177 expr.value = error_mark_node;
5178 expr.original_code = ERROR_MARK;
5179 break;
5182 tree type = groktypename (t1);
5183 tree offsetof_ref;
5184 if (type == error_mark_node)
5185 offsetof_ref = error_mark_node;
5186 else
5187 offsetof_ref = build1 (INDIRECT_REF, type, null_pointer_node);
5188 /* Parse the second argument to __builtin_offsetof. We
5189 must have one identifier, and beyond that we want to
5190 accept sub structure and sub array references. */
5191 if (c_parser_next_token_is (parser, CPP_NAME))
5193 offsetof_ref = build_component_ref
5194 (offsetof_ref, c_parser_peek_token (parser)->value);
5195 c_parser_consume_token (parser);
5196 while (c_parser_next_token_is (parser, CPP_DOT)
5197 || c_parser_next_token_is (parser,
5198 CPP_OPEN_SQUARE))
5200 if (c_parser_next_token_is (parser, CPP_DOT))
5202 c_parser_consume_token (parser);
5203 if (c_parser_next_token_is_not (parser,
5204 CPP_NAME))
5206 c_parser_error (parser, "expected identifier");
5207 break;
5209 offsetof_ref = build_component_ref
5210 (offsetof_ref,
5211 c_parser_peek_token (parser)->value);
5212 c_parser_consume_token (parser);
5214 else
5216 tree idx;
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 offsetof_ref = build_array_ref (offsetof_ref, idx);
5225 else
5226 c_parser_error (parser, "expected identifier");
5227 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5228 "expected %<)%>");
5229 expr.value = fold_offsetof (offsetof_ref, NULL_TREE);
5230 expr.original_code = ERROR_MARK;
5232 break;
5233 case RID_CHOOSE_EXPR:
5234 c_parser_consume_token (parser);
5235 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5237 expr.value = error_mark_node;
5238 expr.original_code = ERROR_MARK;
5239 break;
5241 e1 = c_parser_expr_no_commas (parser, NULL);
5242 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
5244 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5245 expr.value = error_mark_node;
5246 expr.original_code = ERROR_MARK;
5247 break;
5249 e2 = c_parser_expr_no_commas (parser, NULL);
5250 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
5252 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5253 expr.value = error_mark_node;
5254 expr.original_code = ERROR_MARK;
5255 break;
5257 e3 = c_parser_expr_no_commas (parser, NULL);
5258 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5259 "expected %<)%>");
5261 tree c;
5263 c = fold (e1.value);
5264 if (TREE_CODE (c) != INTEGER_CST)
5265 error ("first argument to %<__builtin_choose_expr%> not"
5266 " a constant");
5267 expr = integer_zerop (c) ? e3 : e2;
5269 break;
5270 case RID_TYPES_COMPATIBLE_P:
5271 c_parser_consume_token (parser);
5272 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5274 expr.value = error_mark_node;
5275 expr.original_code = ERROR_MARK;
5276 break;
5278 t1 = c_parser_type_name (parser);
5279 if (t1 == NULL)
5281 expr.value = error_mark_node;
5282 expr.original_code = ERROR_MARK;
5283 break;
5285 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
5287 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5288 expr.value = error_mark_node;
5289 expr.original_code = ERROR_MARK;
5290 break;
5292 t2 = c_parser_type_name (parser);
5293 if (t2 == NULL)
5295 expr.value = error_mark_node;
5296 expr.original_code = ERROR_MARK;
5297 break;
5299 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5300 "expected %<)%>");
5302 tree e1, e2;
5304 e1 = TYPE_MAIN_VARIANT (groktypename (t1));
5305 e2 = TYPE_MAIN_VARIANT (groktypename (t2));
5307 expr.value = comptypes (e1, e2)
5308 ? build_int_cst (NULL_TREE, 1)
5309 : build_int_cst (NULL_TREE, 0);
5310 expr.original_code = ERROR_MARK;
5312 break;
5313 case RID_AT_SELECTOR:
5314 gcc_assert (c_dialect_objc ());
5315 c_parser_consume_token (parser);
5316 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5318 expr.value = error_mark_node;
5319 expr.original_code = ERROR_MARK;
5320 break;
5323 tree sel = c_parser_objc_selector_arg (parser);
5324 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5325 "expected %<)%>");
5326 expr.value = objc_build_selector_expr (sel);
5327 expr.original_code = ERROR_MARK;
5329 break;
5330 case RID_AT_PROTOCOL:
5331 gcc_assert (c_dialect_objc ());
5332 c_parser_consume_token (parser);
5333 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5335 expr.value = error_mark_node;
5336 expr.original_code = ERROR_MARK;
5337 break;
5339 if (c_parser_next_token_is_not (parser, CPP_NAME))
5341 c_parser_error (parser, "expected identifier");
5342 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5343 expr.value = error_mark_node;
5344 expr.original_code = ERROR_MARK;
5345 break;
5348 tree id = c_parser_peek_token (parser)->value;
5349 c_parser_consume_token (parser);
5350 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5351 "expected %<)%>");
5352 expr.value = objc_build_protocol_expr (id);
5353 expr.original_code = ERROR_MARK;
5355 break;
5356 case RID_AT_ENCODE:
5357 /* Extension to support C-structures in the archiver. */
5358 gcc_assert (c_dialect_objc ());
5359 c_parser_consume_token (parser);
5360 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5362 expr.value = error_mark_node;
5363 expr.original_code = ERROR_MARK;
5364 break;
5366 t1 = c_parser_type_name (parser);
5367 if (t1 == NULL)
5369 expr.value = error_mark_node;
5370 expr.original_code = ERROR_MARK;
5371 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5372 break;
5374 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5375 "expected %<)%>");
5377 tree type = groktypename (t1);
5378 expr.value = objc_build_encode_expr (type);
5379 expr.original_code = ERROR_MARK;
5381 break;
5382 default:
5383 c_parser_error (parser, "expected expression");
5384 expr.value = error_mark_node;
5385 expr.original_code = ERROR_MARK;
5386 break;
5388 break;
5389 case CPP_OPEN_SQUARE:
5390 if (c_dialect_objc ())
5392 tree receiver, args;
5393 c_parser_consume_token (parser);
5394 receiver = c_parser_objc_receiver (parser);
5395 args = c_parser_objc_message_args (parser);
5396 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
5397 "expected %<]%>");
5398 expr.value = objc_build_message_expr (build_tree_list (receiver,
5399 args));
5400 expr.original_code = ERROR_MARK;
5401 break;
5403 /* Else fall through to report error. */
5404 default:
5405 c_parser_error (parser, "expected expression");
5406 expr.value = error_mark_node;
5407 expr.original_code = ERROR_MARK;
5408 break;
5410 return c_parser_postfix_expression_after_primary (parser, expr);
5413 /* Parse a postfix expression after a parenthesized type name: the
5414 brace-enclosed initializer of a compound literal, possibly followed
5415 by some postfix operators. This is separate because it is not
5416 possible to tell until after the type name whether a cast
5417 expression has a cast or a compound literal, or whether the operand
5418 of sizeof is a parenthesized type name or starts with a compound
5419 literal. */
5421 static struct c_expr
5422 c_parser_postfix_expression_after_paren_type (c_parser *parser,
5423 struct c_type_name *type_name)
5425 tree type;
5426 struct c_expr init;
5427 struct c_expr expr;
5428 start_init (NULL_TREE, NULL, 0);
5429 type = groktypename (type_name);
5430 if (type != error_mark_node && C_TYPE_VARIABLE_SIZE (type))
5432 error ("compound literal has variable size");
5433 type = error_mark_node;
5435 init = c_parser_braced_init (parser, type, false);
5436 finish_init ();
5437 maybe_warn_string_init (type, init);
5439 if (pedantic && !flag_isoc99)
5440 pedwarn ("ISO C90 forbids compound literals");
5441 expr.value = build_compound_literal (type, init.value);
5442 expr.original_code = ERROR_MARK;
5443 return c_parser_postfix_expression_after_primary (parser, expr);
5446 /* Parse a postfix expression after the initial primary or compound
5447 literal; that is, parse a series of postfix operators. */
5449 static struct c_expr
5450 c_parser_postfix_expression_after_primary (c_parser *parser,
5451 struct c_expr expr)
5453 tree ident, idx, exprlist;
5454 while (true)
5456 switch (c_parser_peek_token (parser)->type)
5458 case CPP_OPEN_SQUARE:
5459 /* Array reference. */
5460 c_parser_consume_token (parser);
5461 idx = c_parser_expression (parser).value;
5462 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
5463 "expected %<]%>");
5464 expr.value = build_array_ref (expr.value, idx);
5465 expr.original_code = ERROR_MARK;
5466 break;
5467 case CPP_OPEN_PAREN:
5468 /* Function call. */
5469 c_parser_consume_token (parser);
5470 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
5471 exprlist = NULL_TREE;
5472 else
5473 exprlist = c_parser_expr_list (parser, true);
5474 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5475 "expected %<)%>");
5476 expr.value = build_function_call (expr.value, exprlist);
5477 expr.original_code = ERROR_MARK;
5478 break;
5479 case CPP_DOT:
5480 /* Structure element reference. */
5481 c_parser_consume_token (parser);
5482 expr = default_function_array_conversion (expr);
5483 if (c_parser_next_token_is (parser, CPP_NAME))
5484 ident = c_parser_peek_token (parser)->value;
5485 else
5487 c_parser_error (parser, "expected identifier");
5488 expr.value = error_mark_node;
5489 expr.original_code = ERROR_MARK;
5490 return expr;
5492 c_parser_consume_token (parser);
5493 expr.value = build_component_ref (expr.value, ident);
5494 expr.original_code = ERROR_MARK;
5495 break;
5496 case CPP_DEREF:
5497 /* Structure element reference. */
5498 c_parser_consume_token (parser);
5499 expr = default_function_array_conversion (expr);
5500 if (c_parser_next_token_is (parser, CPP_NAME))
5501 ident = c_parser_peek_token (parser)->value;
5502 else
5504 c_parser_error (parser, "expected identifier");
5505 expr.value = error_mark_node;
5506 expr.original_code = ERROR_MARK;
5507 return expr;
5509 c_parser_consume_token (parser);
5510 expr.value = build_component_ref (build_indirect_ref (expr.value,
5511 "->"), ident);
5512 expr.original_code = ERROR_MARK;
5513 break;
5514 case CPP_PLUS_PLUS:
5515 /* Postincrement. */
5516 c_parser_consume_token (parser);
5517 expr = default_function_array_conversion (expr);
5518 expr.value = build_unary_op (POSTINCREMENT_EXPR, expr.value, 0);
5519 expr.original_code = ERROR_MARK;
5520 break;
5521 case CPP_MINUS_MINUS:
5522 /* Postdecrement. */
5523 c_parser_consume_token (parser);
5524 expr = default_function_array_conversion (expr);
5525 expr.value = build_unary_op (POSTDECREMENT_EXPR, expr.value, 0);
5526 expr.original_code = ERROR_MARK;
5527 break;
5528 default:
5529 return expr;
5534 /* Parse an expression (C90 6.3.17, C99 6.5.17).
5536 expression:
5537 assignment-expression
5538 expression , assignment-expression
5541 static struct c_expr
5542 c_parser_expression (c_parser *parser)
5544 struct c_expr expr;
5545 expr = c_parser_expr_no_commas (parser, NULL);
5546 while (c_parser_next_token_is (parser, CPP_COMMA))
5548 struct c_expr next;
5549 c_parser_consume_token (parser);
5550 next = c_parser_expr_no_commas (parser, NULL);
5551 next = default_function_array_conversion (next);
5552 expr.value = build_compound_expr (expr.value, next.value);
5553 expr.original_code = COMPOUND_EXPR;
5555 return expr;
5558 /* Parse an expression and convert functions or arrays to
5559 pointers. */
5561 static struct c_expr
5562 c_parser_expression_conv (c_parser *parser)
5564 struct c_expr expr;
5565 expr = c_parser_expression (parser);
5566 expr = default_function_array_conversion (expr);
5567 return expr;
5570 /* Parse a non-empty list of expressions. If CONVERT_P, convert
5571 functions and arrays to pointers.
5573 nonempty-expr-list:
5574 assignment-expression
5575 nonempty-expr-list , assignment-expression
5578 static tree
5579 c_parser_expr_list (c_parser *parser, bool convert_p)
5581 struct c_expr expr;
5582 tree ret, cur;
5583 expr = c_parser_expr_no_commas (parser, NULL);
5584 if (convert_p)
5585 expr = default_function_array_conversion (expr);
5586 ret = cur = build_tree_list (NULL_TREE, expr.value);
5587 while (c_parser_next_token_is (parser, CPP_COMMA))
5589 c_parser_consume_token (parser);
5590 expr = c_parser_expr_no_commas (parser, NULL);
5591 if (convert_p)
5592 expr = default_function_array_conversion (expr);
5593 cur = TREE_CHAIN (cur) = build_tree_list (NULL_TREE, expr.value);
5595 return ret;
5599 /* Parse Objective-C-specific constructs. */
5601 /* Parse an objc-class-definition.
5603 objc-class-definition:
5604 @interface identifier objc-superclass[opt] objc-protocol-refs[opt]
5605 objc-class-instance-variables[opt] objc-methodprotolist @end
5606 @implementation identifier objc-superclass[opt]
5607 objc-class-instance-variables[opt]
5608 @interface identifier ( identifier ) objc-protocol-refs[opt]
5609 objc-methodprotolist @end
5610 @implementation identifier ( identifier )
5612 objc-superclass:
5613 : identifier
5615 "@interface identifier (" must start "@interface identifier (
5616 identifier ) ...": objc-methodprotolist in the first production may
5617 not start with a parenthesized identifier as a declarator of a data
5618 definition with no declaration specifiers if the objc-superclass,
5619 objc-protocol-refs and objc-class-instance-variables are omitted. */
5621 static void
5622 c_parser_objc_class_definition (c_parser *parser)
5624 bool iface_p;
5625 tree id1;
5626 tree superclass;
5627 if (c_parser_next_token_is_keyword (parser, RID_AT_INTERFACE))
5628 iface_p = true;
5629 else if (c_parser_next_token_is_keyword (parser, RID_AT_IMPLEMENTATION))
5630 iface_p = false;
5631 else
5632 gcc_unreachable ();
5633 c_parser_consume_token (parser);
5634 if (c_parser_next_token_is_not (parser, CPP_NAME))
5636 c_parser_error (parser, "expected identifier");
5637 return;
5639 id1 = c_parser_peek_token (parser)->value;
5640 c_parser_consume_token (parser);
5641 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
5643 tree id2;
5644 tree proto = NULL_TREE;
5645 c_parser_consume_token (parser);
5646 if (c_parser_next_token_is_not (parser, CPP_NAME))
5648 c_parser_error (parser, "expected identifier");
5649 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5650 return;
5652 id2 = c_parser_peek_token (parser)->value;
5653 c_parser_consume_token (parser);
5654 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
5655 if (!iface_p)
5657 objc_start_category_implementation (id1, id2);
5658 return;
5660 if (c_parser_next_token_is (parser, CPP_LESS))
5661 proto = c_parser_objc_protocol_refs (parser);
5662 objc_start_category_interface (id1, id2, proto);
5663 c_parser_objc_methodprotolist (parser);
5664 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
5665 objc_finish_interface ();
5666 return;
5668 if (c_parser_next_token_is (parser, CPP_COLON))
5670 c_parser_consume_token (parser);
5671 if (c_parser_next_token_is_not (parser, CPP_NAME))
5673 c_parser_error (parser, "expected identifier");
5674 return;
5676 superclass = c_parser_peek_token (parser)->value;
5677 c_parser_consume_token (parser);
5679 else
5680 superclass = NULL_TREE;
5681 if (iface_p)
5683 tree proto = NULL_TREE;
5684 if (c_parser_next_token_is (parser, CPP_LESS))
5685 proto = c_parser_objc_protocol_refs (parser);
5686 objc_start_class_interface (id1, superclass, proto);
5688 else
5689 objc_start_class_implementation (id1, superclass);
5690 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
5691 c_parser_objc_class_instance_variables (parser);
5692 if (iface_p)
5694 objc_continue_interface ();
5695 c_parser_objc_methodprotolist (parser);
5696 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
5697 objc_finish_interface ();
5699 else
5701 objc_continue_implementation ();
5702 return;
5706 /* Parse objc-class-instance-variables.
5708 objc-class-instance-variables:
5709 { objc-instance-variable-decl-list[opt] }
5711 objc-instance-variable-decl-list:
5712 objc-visibility-spec
5713 objc-instance-variable-decl ;
5715 objc-instance-variable-decl-list objc-visibility-spec
5716 objc-instance-variable-decl-list objc-instance-variable-decl ;
5717 objc-instance-variable-decl-list ;
5719 objc-visibility-spec:
5720 @private
5721 @protected
5722 @public
5724 objc-instance-variable-decl:
5725 struct-declaration
5728 static void
5729 c_parser_objc_class_instance_variables (c_parser *parser)
5731 gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE));
5732 c_parser_consume_token (parser);
5733 while (c_parser_next_token_is_not (parser, CPP_EOF))
5735 tree decls;
5736 /* Parse any stray semicolon. */
5737 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5739 if (pedantic)
5740 pedwarn ("extra semicolon in struct or union specified");
5741 c_parser_consume_token (parser);
5742 continue;
5744 /* Stop if at the end of the instance variables. */
5745 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
5747 c_parser_consume_token (parser);
5748 break;
5750 /* Parse any objc-visibility-spec. */
5751 if (c_parser_next_token_is_keyword (parser, RID_AT_PRIVATE))
5753 c_parser_consume_token (parser);
5754 objc_set_visibility (2);
5755 continue;
5757 else if (c_parser_next_token_is_keyword (parser, RID_AT_PROTECTED))
5759 c_parser_consume_token (parser);
5760 objc_set_visibility (0);
5761 continue;
5763 else if (c_parser_next_token_is_keyword (parser, RID_AT_PUBLIC))
5765 c_parser_consume_token (parser);
5766 objc_set_visibility (1);
5767 continue;
5769 else if (c_parser_next_token_is (parser, CPP_PRAGMA))
5771 c_parser_pragma (parser, pragma_external);
5772 continue;
5775 /* Parse some comma-separated declarations. */
5776 decls = c_parser_struct_declaration (parser);
5778 /* Comma-separated instance variables are chained together in
5779 reverse order; add them one by one. */
5780 tree ivar = nreverse (decls);
5781 for (; ivar; ivar = TREE_CHAIN (ivar))
5782 objc_add_instance_variable (copy_node (ivar));
5784 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
5788 /* Parse an objc-class-declaration.
5790 objc-class-declaration:
5791 @class identifier-list ;
5794 static void
5795 c_parser_objc_class_declaration (c_parser *parser)
5797 tree list = NULL_TREE;
5798 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_CLASS));
5799 c_parser_consume_token (parser);
5800 /* Any identifiers, including those declared as type names, are OK
5801 here. */
5802 while (true)
5804 tree id;
5805 if (c_parser_next_token_is_not (parser, CPP_NAME))
5807 c_parser_error (parser, "expected identifier");
5808 break;
5810 id = c_parser_peek_token (parser)->value;
5811 list = chainon (list, build_tree_list (NULL_TREE, id));
5812 c_parser_consume_token (parser);
5813 if (c_parser_next_token_is (parser, CPP_COMMA))
5814 c_parser_consume_token (parser);
5815 else
5816 break;
5818 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
5819 objc_declare_class (list);
5822 /* Parse an objc-alias-declaration.
5824 objc-alias-declaration:
5825 @compatibility_alias identifier identifier ;
5828 static void
5829 c_parser_objc_alias_declaration (c_parser *parser)
5831 tree id1, id2;
5832 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_ALIAS));
5833 c_parser_consume_token (parser);
5834 if (c_parser_next_token_is_not (parser, CPP_NAME))
5836 c_parser_error (parser, "expected identifier");
5837 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
5838 return;
5840 id1 = c_parser_peek_token (parser)->value;
5841 c_parser_consume_token (parser);
5842 if (c_parser_next_token_is_not (parser, CPP_NAME))
5844 c_parser_error (parser, "expected identifier");
5845 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
5846 return;
5848 id2 = c_parser_peek_token (parser)->value;
5849 c_parser_consume_token (parser);
5850 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
5851 objc_declare_alias (id1, id2);
5854 /* Parse an objc-protocol-definition.
5856 objc-protocol-definition:
5857 @protocol identifier objc-protocol-refs[opt] objc-methodprotolist @end
5858 @protocol identifier-list ;
5860 "@protocol identifier ;" should be resolved as "@protocol
5861 identifier-list ;": objc-methodprotolist may not start with a
5862 semicolon in the first alternative if objc-protocol-refs are
5863 omitted. */
5865 static void
5866 c_parser_objc_protocol_definition (c_parser *parser)
5868 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_PROTOCOL));
5869 c_parser_consume_token (parser);
5870 if (c_parser_next_token_is_not (parser, CPP_NAME))
5872 c_parser_error (parser, "expected identifier");
5873 return;
5875 if (c_parser_peek_2nd_token (parser)->type == CPP_COMMA
5876 || c_parser_peek_2nd_token (parser)->type == CPP_SEMICOLON)
5878 tree list = NULL_TREE;
5879 /* Any identifiers, including those declared as type names, are
5880 OK here. */
5881 while (true)
5883 tree id;
5884 if (c_parser_next_token_is_not (parser, CPP_NAME))
5886 c_parser_error (parser, "expected identifier");
5887 break;
5889 id = c_parser_peek_token (parser)->value;
5890 list = chainon (list, build_tree_list (NULL_TREE, id));
5891 c_parser_consume_token (parser);
5892 if (c_parser_next_token_is (parser, CPP_COMMA))
5893 c_parser_consume_token (parser);
5894 else
5895 break;
5897 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
5898 objc_declare_protocols (list);
5900 else
5902 tree id = c_parser_peek_token (parser)->value;
5903 tree proto = NULL_TREE;
5904 c_parser_consume_token (parser);
5905 if (c_parser_next_token_is (parser, CPP_LESS))
5906 proto = c_parser_objc_protocol_refs (parser);
5907 objc_pq_context = 1;
5908 objc_start_protocol (id, proto);
5909 c_parser_objc_methodprotolist (parser);
5910 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
5911 objc_pq_context = 0;
5912 objc_finish_interface ();
5916 /* Parse an objc-method-type.
5918 objc-method-type:
5923 static enum tree_code
5924 c_parser_objc_method_type (c_parser *parser)
5926 switch (c_parser_peek_token (parser)->type)
5928 case CPP_PLUS:
5929 c_parser_consume_token (parser);
5930 return PLUS_EXPR;
5931 case CPP_MINUS:
5932 c_parser_consume_token (parser);
5933 return MINUS_EXPR;
5934 default:
5935 gcc_unreachable ();
5939 /* Parse an objc-method-definition.
5941 objc-method-definition:
5942 objc-method-type objc-method-decl ;[opt] compound-statement
5945 static void
5946 c_parser_objc_method_definition (c_parser *parser)
5948 enum tree_code type = c_parser_objc_method_type (parser);
5949 tree decl;
5950 objc_set_method_type (type);
5951 objc_pq_context = 1;
5952 decl = c_parser_objc_method_decl (parser);
5953 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5955 c_parser_consume_token (parser);
5956 if (pedantic)
5957 pedwarn ("extra semicolon in method definition specified");
5959 if (!c_parser_next_token_is (parser, CPP_OPEN_BRACE))
5961 c_parser_error (parser, "expected %<{%>");
5962 return;
5964 objc_pq_context = 0;
5965 objc_start_method_definition (decl);
5966 add_stmt (c_parser_compound_statement (parser));
5967 objc_finish_method_definition (current_function_decl);
5970 /* Parse an objc-methodprotolist.
5972 objc-methodprotolist:
5973 empty
5974 objc-methodprotolist objc-methodproto
5975 objc-methodprotolist declaration
5976 objc-methodprotolist ;
5978 The declaration is a data definition, which may be missing
5979 declaration specifiers under the same rules and diagnostics as
5980 other data definitions outside functions, and the stray semicolon
5981 is diagnosed the same way as a stray semicolon outside a
5982 function. */
5984 static void
5985 c_parser_objc_methodprotolist (c_parser *parser)
5987 while (true)
5989 /* The list is terminated by @end. */
5990 switch (c_parser_peek_token (parser)->type)
5992 case CPP_SEMICOLON:
5993 if (pedantic)
5994 pedwarn ("ISO C does not allow extra %<;%> outside of a function");
5995 c_parser_consume_token (parser);
5996 break;
5997 case CPP_PLUS:
5998 case CPP_MINUS:
5999 c_parser_objc_methodproto (parser);
6000 break;
6001 case CPP_PRAGMA:
6002 c_parser_pragma (parser, pragma_external);
6003 break;
6004 case CPP_EOF:
6005 return;
6006 default:
6007 if (c_parser_next_token_is_keyword (parser, RID_AT_END))
6008 return;
6009 c_parser_declaration_or_fndef (parser, false, true, false, true);
6010 break;
6015 /* Parse an objc-methodproto.
6017 objc-methodproto:
6018 objc-method-type objc-method-decl ;
6021 static void
6022 c_parser_objc_methodproto (c_parser *parser)
6024 enum tree_code type = c_parser_objc_method_type (parser);
6025 tree decl;
6026 objc_set_method_type (type);
6027 /* Remember protocol qualifiers in prototypes. */
6028 objc_pq_context = 1;
6029 decl = c_parser_objc_method_decl (parser);
6030 /* Forget protocol qualifiers here. */
6031 objc_pq_context = 0;
6032 objc_add_method_declaration (decl);
6033 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
6036 /* Parse an objc-method-decl.
6038 objc-method-decl:
6039 ( objc-type-name ) objc-selector
6040 objc-selector
6041 ( objc-type-name ) objc-keyword-selector objc-optparmlist
6042 objc-keyword-selector objc-optparmlist
6044 objc-keyword-selector:
6045 objc-keyword-decl
6046 objc-keyword-selector objc-keyword-decl
6048 objc-keyword-decl:
6049 objc-selector : ( objc-type-name ) identifier
6050 objc-selector : identifier
6051 : ( objc-type-name ) identifier
6052 : identifier
6054 objc-optparmlist:
6055 objc-optparms objc-optellipsis
6057 objc-optparms:
6058 empty
6059 objc-opt-parms , parameter-declaration
6061 objc-optellipsis:
6062 empty
6063 , ...
6066 static tree
6067 c_parser_objc_method_decl (c_parser *parser)
6069 tree type = NULL_TREE;
6070 tree sel;
6071 tree parms = NULL_TREE;
6072 bool ellipsis = false;
6074 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
6076 c_parser_consume_token (parser);
6077 type = c_parser_objc_type_name (parser);
6078 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
6080 sel = c_parser_objc_selector (parser);
6081 /* If there is no selector, or a colon follows, we have an
6082 objc-keyword-selector. If there is a selector, and a colon does
6083 not follow, that selector ends the objc-method-decl. */
6084 if (!sel || c_parser_next_token_is (parser, CPP_COLON))
6086 tree tsel = sel;
6087 tree list = NULL_TREE;
6088 while (true)
6090 tree atype = NULL_TREE, id, keyworddecl;
6091 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
6092 break;
6093 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
6095 c_parser_consume_token (parser);
6096 atype = c_parser_objc_type_name (parser);
6097 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6098 "expected %<)%>");
6100 if (c_parser_next_token_is_not (parser, CPP_NAME))
6102 c_parser_error (parser, "expected identifier");
6103 return error_mark_node;
6105 id = c_parser_peek_token (parser)->value;
6106 c_parser_consume_token (parser);
6107 keyworddecl = objc_build_keyword_decl (tsel, atype, id);
6108 list = chainon (list, keyworddecl);
6109 tsel = c_parser_objc_selector (parser);
6110 if (!tsel && c_parser_next_token_is_not (parser, CPP_COLON))
6111 break;
6113 /* Parse the optional parameter list. Optional Objective-C
6114 method parameters follow the C syntax, and may include '...'
6115 to denote a variable number of arguments. */
6116 parms = make_node (TREE_LIST);
6117 while (c_parser_next_token_is (parser, CPP_COMMA))
6119 struct c_parm *parm;
6120 c_parser_consume_token (parser);
6121 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
6123 ellipsis = true;
6124 c_parser_consume_token (parser);
6125 break;
6127 parm = c_parser_parameter_declaration (parser, NULL_TREE);
6128 if (parm == NULL)
6129 break;
6130 parms = chainon (parms,
6131 build_tree_list (NULL_TREE, grokparm (parm)));
6133 sel = list;
6135 return objc_build_method_signature (type, sel, parms, ellipsis);
6138 /* Parse an objc-type-name.
6140 objc-type-name:
6141 objc-type-qualifiers[opt] type-name
6142 objc-type-qualifiers[opt]
6144 objc-type-qualifiers:
6145 objc-type-qualifier
6146 objc-type-qualifiers objc-type-qualifier
6148 objc-type-qualifier: one of
6149 in out inout bycopy byref oneway
6152 static tree
6153 c_parser_objc_type_name (c_parser *parser)
6155 tree quals = NULL_TREE;
6156 struct c_type_name *typename = NULL;
6157 tree type = NULL_TREE;
6158 while (true)
6160 c_token *token = c_parser_peek_token (parser);
6161 if (token->type == CPP_KEYWORD
6162 && (token->keyword == RID_IN
6163 || token->keyword == RID_OUT
6164 || token->keyword == RID_INOUT
6165 || token->keyword == RID_BYCOPY
6166 || token->keyword == RID_BYREF
6167 || token->keyword == RID_ONEWAY))
6169 quals = chainon (quals, build_tree_list (NULL_TREE, token->value));
6170 c_parser_consume_token (parser);
6172 else
6173 break;
6175 if (c_parser_next_token_starts_typename (parser))
6176 typename = c_parser_type_name (parser);
6177 if (typename)
6178 type = groktypename (typename);
6179 return build_tree_list (quals, type);
6182 /* Parse objc-protocol-refs.
6184 objc-protocol-refs:
6185 < identifier-list >
6188 static tree
6189 c_parser_objc_protocol_refs (c_parser *parser)
6191 tree list = NULL_TREE;
6192 gcc_assert (c_parser_next_token_is (parser, CPP_LESS));
6193 c_parser_consume_token (parser);
6194 /* Any identifiers, including those declared as type names, are OK
6195 here. */
6196 while (true)
6198 tree id;
6199 if (c_parser_next_token_is_not (parser, CPP_NAME))
6201 c_parser_error (parser, "expected identifier");
6202 break;
6204 id = c_parser_peek_token (parser)->value;
6205 list = chainon (list, build_tree_list (NULL_TREE, id));
6206 c_parser_consume_token (parser);
6207 if (c_parser_next_token_is (parser, CPP_COMMA))
6208 c_parser_consume_token (parser);
6209 else
6210 break;
6212 c_parser_require (parser, CPP_GREATER, "expected %<>%>");
6213 return list;
6216 /* Parse an objc-try-catch-statement.
6218 objc-try-catch-statement:
6219 @try compound-statement objc-catch-list[opt]
6220 @try compound-statement objc-catch-list[opt] @finally compound-statement
6222 objc-catch-list:
6223 @catch ( parameter-declaration ) compound-statement
6224 objc-catch-list @catch ( parameter-declaration ) compound-statement
6227 static void
6228 c_parser_objc_try_catch_statement (c_parser *parser)
6230 location_t loc;
6231 tree stmt;
6232 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_TRY));
6233 c_parser_consume_token (parser);
6234 loc = c_parser_peek_token (parser)->location;
6235 stmt = c_parser_compound_statement (parser);
6236 objc_begin_try_stmt (loc, stmt);
6237 while (c_parser_next_token_is_keyword (parser, RID_AT_CATCH))
6239 struct c_parm *parm;
6240 c_parser_consume_token (parser);
6241 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6242 break;
6243 parm = c_parser_parameter_declaration (parser, NULL_TREE);
6244 if (parm == NULL)
6246 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6247 break;
6249 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
6250 objc_begin_catch_clause (grokparm (parm));
6251 if (c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
6252 c_parser_compound_statement_nostart (parser);
6253 objc_finish_catch_clause ();
6255 if (c_parser_next_token_is_keyword (parser, RID_AT_FINALLY))
6257 location_t finloc;
6258 tree finstmt;
6259 c_parser_consume_token (parser);
6260 finloc = c_parser_peek_token (parser)->location;
6261 finstmt = c_parser_compound_statement (parser);
6262 objc_build_finally_clause (finloc, finstmt);
6264 objc_finish_try_stmt ();
6267 /* Parse an objc-synchronized-statement.
6269 objc-synchronized-statement:
6270 @synchronized ( expression ) compound-statement
6273 static void
6274 c_parser_objc_synchronized_statement (c_parser *parser)
6276 location_t loc;
6277 tree expr, stmt;
6278 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_SYNCHRONIZED));
6279 c_parser_consume_token (parser);
6280 loc = c_parser_peek_token (parser)->location;
6281 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6283 expr = c_parser_expression (parser).value;
6284 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
6286 else
6287 expr = error_mark_node;
6288 stmt = c_parser_compound_statement (parser);
6289 objc_build_synchronized (loc, expr, stmt);
6292 /* Parse an objc-selector; return NULL_TREE without an error if the
6293 next token is not an objc-selector.
6295 objc-selector:
6296 identifier
6297 one of
6298 enum struct union if else while do for switch case default
6299 break continue return goto asm sizeof typeof __alignof
6300 unsigned long const short volatile signed restrict _Complex
6301 in out inout bycopy byref oneway int char float double void _Bool
6303 ??? Why this selection of keywords but not, for example, storage
6304 class specifiers? */
6306 static tree
6307 c_parser_objc_selector (c_parser *parser)
6309 c_token *token = c_parser_peek_token (parser);
6310 tree value = token->value;
6311 if (token->type == CPP_NAME)
6313 c_parser_consume_token (parser);
6314 return value;
6316 if (token->type != CPP_KEYWORD)
6317 return NULL_TREE;
6318 switch (token->keyword)
6320 case RID_ENUM:
6321 case RID_STRUCT:
6322 case RID_UNION:
6323 case RID_IF:
6324 case RID_ELSE:
6325 case RID_WHILE:
6326 case RID_DO:
6327 case RID_FOR:
6328 case RID_SWITCH:
6329 case RID_CASE:
6330 case RID_DEFAULT:
6331 case RID_BREAK:
6332 case RID_CONTINUE:
6333 case RID_RETURN:
6334 case RID_GOTO:
6335 case RID_ASM:
6336 case RID_SIZEOF:
6337 case RID_TYPEOF:
6338 case RID_ALIGNOF:
6339 case RID_UNSIGNED:
6340 case RID_LONG:
6341 case RID_CONST:
6342 case RID_SHORT:
6343 case RID_VOLATILE:
6344 case RID_SIGNED:
6345 case RID_RESTRICT:
6346 case RID_COMPLEX:
6347 case RID_IN:
6348 case RID_OUT:
6349 case RID_INOUT:
6350 case RID_BYCOPY:
6351 case RID_BYREF:
6352 case RID_ONEWAY:
6353 case RID_INT:
6354 case RID_CHAR:
6355 case RID_FLOAT:
6356 case RID_DOUBLE:
6357 case RID_VOID:
6358 case RID_BOOL:
6359 c_parser_consume_token (parser);
6360 return value;
6361 default:
6362 return NULL_TREE;
6366 /* Parse an objc-selector-arg.
6368 objc-selector-arg:
6369 objc-selector
6370 objc-keywordname-list
6372 objc-keywordname-list:
6373 objc-keywordname
6374 objc-keywordname-list objc-keywordname
6376 objc-keywordname:
6377 objc-selector :
6381 static tree
6382 c_parser_objc_selector_arg (c_parser *parser)
6384 tree sel = c_parser_objc_selector (parser);
6385 tree list = NULL_TREE;
6386 if (sel && c_parser_next_token_is_not (parser, CPP_COLON))
6387 return sel;
6388 while (true)
6390 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
6391 return list;
6392 list = chainon (list, build_tree_list (sel, NULL_TREE));
6393 sel = c_parser_objc_selector (parser);
6394 if (!sel && c_parser_next_token_is_not (parser, CPP_COLON))
6395 break;
6397 return list;
6400 /* Parse an objc-receiver.
6402 objc-receiver:
6403 expression
6404 class-name
6405 type-name
6408 static tree
6409 c_parser_objc_receiver (c_parser *parser)
6411 if (c_parser_peek_token (parser)->type == CPP_NAME
6412 && (c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME
6413 || c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME))
6415 tree id = c_parser_peek_token (parser)->value;
6416 c_parser_consume_token (parser);
6417 return objc_get_class_reference (id);
6419 return c_parser_expression (parser).value;
6422 /* Parse objc-message-args.
6424 objc-message-args:
6425 objc-selector
6426 objc-keywordarg-list
6428 objc-keywordarg-list:
6429 objc-keywordarg
6430 objc-keywordarg-list objc-keywordarg
6432 objc-keywordarg:
6433 objc-selector : objc-keywordexpr
6434 : objc-keywordexpr
6437 static tree
6438 c_parser_objc_message_args (c_parser *parser)
6440 tree sel = c_parser_objc_selector (parser);
6441 tree list = NULL_TREE;
6442 if (sel && c_parser_next_token_is_not (parser, CPP_COLON))
6443 return sel;
6444 while (true)
6446 tree keywordexpr;
6447 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
6448 return list;
6449 keywordexpr = c_parser_objc_keywordexpr (parser);
6450 list = chainon (list, build_tree_list (sel, keywordexpr));
6451 sel = c_parser_objc_selector (parser);
6452 if (!sel && c_parser_next_token_is_not (parser, CPP_COLON))
6453 break;
6455 return list;
6458 /* Parse an objc-keywordexpr.
6460 objc-keywordexpr:
6461 nonempty-expr-list
6464 static tree
6465 c_parser_objc_keywordexpr (c_parser *parser)
6467 tree list = c_parser_expr_list (parser, true);
6468 if (TREE_CHAIN (list) == NULL_TREE)
6470 /* Just return the expression, remove a level of
6471 indirection. */
6472 return TREE_VALUE (list);
6474 else
6476 /* We have a comma expression, we will collapse later. */
6477 return list;
6482 /* Handle pragmas. Some OpenMP pragmas are associated with, and therefore
6483 should be considered, statements. ALLOW_STMT is true if we're within
6484 the context of a function and such pragmas are to be allowed. Returns
6485 true if we actually parsed such a pragma. */
6487 static bool
6488 c_parser_pragma (c_parser *parser, enum pragma_context context)
6490 unsigned int id;
6492 id = c_parser_peek_token (parser)->pragma_kind;
6493 gcc_assert (id != PRAGMA_NONE);
6495 switch (id)
6497 case PRAGMA_OMP_BARRIER:
6498 if (context != pragma_compound)
6500 if (context == pragma_stmt)
6501 c_parser_error (parser, "%<#pragma omp barrier%> may only be "
6502 "used in compound statements");
6503 goto bad_stmt;
6505 c_parser_omp_barrier (parser);
6506 return false;
6508 case PRAGMA_OMP_FLUSH:
6509 if (context != pragma_compound)
6511 if (context == pragma_stmt)
6512 c_parser_error (parser, "%<#pragma omp flush%> may only be "
6513 "used in compound statements");
6514 goto bad_stmt;
6516 c_parser_omp_flush (parser);
6517 return false;
6519 case PRAGMA_OMP_THREADPRIVATE:
6520 c_parser_omp_threadprivate (parser);
6521 return false;
6523 case PRAGMA_OMP_SECTION:
6524 error ("%<#pragma omp section%> may only be used in "
6525 "%<#pragma omp sections%> construct");
6526 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
6527 return false;
6529 case PRAGMA_GCC_PCH_PREPROCESS:
6530 c_parser_error (parser, "%<#pragma GCC pch_preprocess%> must be first");
6531 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
6532 return false;
6534 default:
6535 if (id < PRAGMA_FIRST_EXTERNAL)
6537 if (context == pragma_external)
6539 bad_stmt:
6540 c_parser_error (parser, "expected declaration specifiers");
6541 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
6542 return false;
6544 c_parser_omp_construct (parser);
6545 return true;
6547 break;
6550 c_parser_consume_pragma (parser);
6551 c_invoke_pragma_handler (id);
6553 /* Skip to EOL, but suppress any error message. Those will have been
6554 generated by the handler routine through calling error, as opposed
6555 to calling c_parser_error. */
6556 parser->error = true;
6557 c_parser_skip_to_pragma_eol (parser);
6559 return false;
6562 /* The interface the pragma parsers have to the lexer. */
6564 enum cpp_ttype
6565 pragma_lex (tree *value)
6567 c_token *tok = c_parser_peek_token (the_parser);
6568 enum cpp_ttype ret = tok->type;
6570 *value = tok->value;
6571 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
6572 ret = CPP_EOF;
6573 else
6575 if (ret == CPP_KEYWORD)
6576 ret = CPP_NAME;
6577 c_parser_consume_token (the_parser);
6580 return ret;
6583 static void
6584 c_parser_pragma_pch_preprocess (c_parser *parser)
6586 tree name = NULL;
6588 c_parser_consume_pragma (parser);
6589 if (c_parser_next_token_is (parser, CPP_STRING))
6591 name = c_parser_peek_token (parser)->value;
6592 c_parser_consume_token (parser);
6594 else
6595 c_parser_error (parser, "expected string literal");
6596 c_parser_skip_to_pragma_eol (parser);
6598 if (name)
6599 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
6602 /* OpenMP 2.5 parsing routines. */
6604 /* Returns name of the next clause.
6605 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
6606 the token is not consumed. Otherwise appropriate pragma_omp_clause is
6607 returned and the token is consumed. */
6609 static pragma_omp_clause
6610 c_parser_omp_clause_name (c_parser *parser)
6612 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
6614 if (c_parser_next_token_is_keyword (parser, RID_IF))
6615 result = PRAGMA_OMP_CLAUSE_IF;
6616 else if (c_parser_next_token_is_keyword (parser, RID_DEFAULT))
6617 result = PRAGMA_OMP_CLAUSE_DEFAULT;
6618 else if (c_parser_next_token_is (parser, CPP_NAME))
6620 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
6622 switch (p[0])
6624 case 'c':
6625 if (!strcmp ("copyin", p))
6626 result = PRAGMA_OMP_CLAUSE_COPYIN;
6627 else if (!strcmp ("copyprivate", p))
6628 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
6629 break;
6630 case 'f':
6631 if (!strcmp ("firstprivate", p))
6632 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
6633 break;
6634 case 'l':
6635 if (!strcmp ("lastprivate", p))
6636 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
6637 break;
6638 case 'n':
6639 if (!strcmp ("nowait", p))
6640 result = PRAGMA_OMP_CLAUSE_NOWAIT;
6641 else if (!strcmp ("num_threads", p))
6642 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
6643 break;
6644 case 'o':
6645 if (!strcmp ("ordered", p))
6646 result = PRAGMA_OMP_CLAUSE_ORDERED;
6647 break;
6648 case 'p':
6649 if (!strcmp ("private", p))
6650 result = PRAGMA_OMP_CLAUSE_PRIVATE;
6651 break;
6652 case 'r':
6653 if (!strcmp ("reduction", p))
6654 result = PRAGMA_OMP_CLAUSE_REDUCTION;
6655 break;
6656 case 's':
6657 if (!strcmp ("schedule", p))
6658 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
6659 else if (!strcmp ("shared", p))
6660 result = PRAGMA_OMP_CLAUSE_SHARED;
6661 break;
6665 if (result != PRAGMA_OMP_CLAUSE_NONE)
6666 c_parser_consume_token (parser);
6668 return result;
6671 /* Validate that a clause of the given type does not already exist. */
6673 static void
6674 check_no_duplicate_clause (tree clauses, enum tree_code code, const char *name)
6676 tree c;
6678 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
6679 if (OMP_CLAUSE_CODE (c) == code)
6681 error ("too many %qs clauses", name);
6682 break;
6686 /* OpenMP 2.5:
6687 variable-list:
6688 identifier
6689 variable-list , identifier
6691 If KIND is nonzero, create the appropriate node and install the decl
6692 in OMP_CLAUSE_DECL and add the node to the head of the list.
6694 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
6695 return the list created. */
6697 static tree
6698 c_parser_omp_variable_list (c_parser *parser, enum omp_clause_code kind,
6699 tree list)
6701 if (c_parser_next_token_is_not (parser, CPP_NAME)
6702 || c_parser_peek_token (parser)->id_kind != C_ID_ID)
6703 c_parser_error (parser, "expected identifier");
6705 while (c_parser_next_token_is (parser, CPP_NAME)
6706 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
6708 tree t = lookup_name (c_parser_peek_token (parser)->value);
6710 if (t == NULL_TREE)
6711 undeclared_variable (c_parser_peek_token (parser)->value,
6712 c_parser_peek_token (parser)->location);
6713 else if (t == error_mark_node)
6715 else if (kind != 0)
6717 tree u = build_omp_clause (kind);
6718 OMP_CLAUSE_DECL (u) = t;
6719 OMP_CLAUSE_CHAIN (u) = list;
6720 list = u;
6722 else
6723 list = tree_cons (t, NULL_TREE, list);
6725 c_parser_consume_token (parser);
6727 if (c_parser_next_token_is_not (parser, CPP_COMMA))
6728 break;
6730 c_parser_consume_token (parser);
6733 return list;
6736 /* Similarly, but expect leading and trailing parenthesis. This is a very
6737 common case for omp clauses. */
6739 static tree
6740 c_parser_omp_var_list_parens (c_parser *parser, enum tree_code kind, tree list)
6742 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6744 list = c_parser_omp_variable_list (parser, kind, list);
6745 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
6747 return list;
6750 /* OpenMP 2.5:
6751 copyin ( variable-list ) */
6753 static tree
6754 c_parser_omp_clause_copyin (c_parser *parser, tree list)
6756 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_COPYIN, list);
6759 /* OpenMP 2.5:
6760 copyprivate ( variable-list ) */
6762 static tree
6763 c_parser_omp_clause_copyprivate (c_parser *parser, tree list)
6765 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_COPYPRIVATE, list);
6768 /* OpenMP 2.5:
6769 default ( shared | none ) */
6771 static tree
6772 c_parser_omp_clause_default (c_parser *parser, tree list)
6774 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
6775 tree c;
6777 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6778 return list;
6779 if (c_parser_next_token_is (parser, CPP_NAME))
6781 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
6783 switch (p[0])
6785 case 'n':
6786 if (strcmp ("none", p) != 0)
6787 goto invalid_kind;
6788 kind = OMP_CLAUSE_DEFAULT_NONE;
6789 break;
6791 case 's':
6792 if (strcmp ("shared", p) != 0)
6793 goto invalid_kind;
6794 kind = OMP_CLAUSE_DEFAULT_SHARED;
6795 break;
6797 default:
6798 goto invalid_kind;
6801 c_parser_consume_token (parser);
6803 else
6805 invalid_kind:
6806 c_parser_error (parser, "expected %<none%> or %<shared%>");
6808 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
6810 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
6811 return list;
6813 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default");
6814 c = build_omp_clause (OMP_CLAUSE_DEFAULT);
6815 OMP_CLAUSE_CHAIN (c) = list;
6816 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
6818 return c;
6821 /* OpenMP 2.5:
6822 firstprivate ( variable-list ) */
6824 static tree
6825 c_parser_omp_clause_firstprivate (c_parser *parser, tree list)
6827 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_FIRSTPRIVATE, list);
6830 /* OpenMP 2.5:
6831 if ( expression ) */
6833 static tree
6834 c_parser_omp_clause_if (c_parser *parser, tree list)
6836 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
6838 tree t = c_parser_paren_condition (parser);
6839 tree c;
6841 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if");
6843 c = build_omp_clause (OMP_CLAUSE_IF);
6844 OMP_CLAUSE_IF_EXPR (c) = t;
6845 OMP_CLAUSE_CHAIN (c) = list;
6846 list = c;
6848 else
6849 c_parser_error (parser, "expected %<(%>");
6851 return list;
6854 /* OpenMP 2.5:
6855 lastprivate ( variable-list ) */
6857 static tree
6858 c_parser_omp_clause_lastprivate (c_parser *parser, tree list)
6860 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_LASTPRIVATE, list);
6863 /* OpenMP 2.5:
6864 nowait */
6866 static tree
6867 c_parser_omp_clause_nowait (c_parser *parser ATTRIBUTE_UNUSED, tree list)
6869 tree c;
6871 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait");
6873 c = build_omp_clause (OMP_CLAUSE_NOWAIT);
6874 OMP_CLAUSE_CHAIN (c) = list;
6875 return c;
6878 /* OpenMP 2.5:
6879 num_threads ( expression ) */
6881 static tree
6882 c_parser_omp_clause_num_threads (c_parser *parser, tree list)
6884 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6886 tree c, t = c_parser_expression (parser).value;
6888 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
6890 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
6892 c_parser_error (parser, "expected integer expression");
6893 return list;
6896 /* Attempt to statically determine when the number isn't positive. */
6897 c = fold_build2 (LE_EXPR, boolean_type_node, t,
6898 build_int_cst (TREE_TYPE (t), 0));
6899 if (c == boolean_true_node)
6901 warning (0, "%<num_threads%> value must be positive");
6902 t = integer_one_node;
6905 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS, "num_threads");
6907 c = build_omp_clause (OMP_CLAUSE_NUM_THREADS);
6908 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
6909 OMP_CLAUSE_CHAIN (c) = list;
6910 list = c;
6913 return list;
6916 /* OpenMP 2.5:
6917 ordered */
6919 static tree
6920 c_parser_omp_clause_ordered (c_parser *parser ATTRIBUTE_UNUSED, tree list)
6922 tree c;
6924 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED, "ordered");
6926 c = build_omp_clause (OMP_CLAUSE_ORDERED);
6927 OMP_CLAUSE_CHAIN (c) = list;
6928 return c;
6931 /* OpenMP 2.5:
6932 private ( variable-list ) */
6934 static tree
6935 c_parser_omp_clause_private (c_parser *parser, tree list)
6937 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_PRIVATE, list);
6940 /* OpenMP 2.5:
6941 reduction ( reduction-operator : variable-list )
6943 reduction-operator:
6944 One of: + * - & ^ | && || */
6946 static tree
6947 c_parser_omp_clause_reduction (c_parser *parser, tree list)
6949 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6951 enum tree_code code;
6953 switch (c_parser_peek_token (parser)->type)
6955 case CPP_PLUS:
6956 code = PLUS_EXPR;
6957 break;
6958 case CPP_MULT:
6959 code = MULT_EXPR;
6960 break;
6961 case CPP_MINUS:
6962 code = MINUS_EXPR;
6963 break;
6964 case CPP_AND:
6965 code = BIT_AND_EXPR;
6966 break;
6967 case CPP_XOR:
6968 code = BIT_XOR_EXPR;
6969 break;
6970 case CPP_OR:
6971 code = BIT_IOR_EXPR;
6972 break;
6973 case CPP_AND_AND:
6974 code = TRUTH_ANDIF_EXPR;
6975 break;
6976 case CPP_OR_OR:
6977 code = TRUTH_ORIF_EXPR;
6978 break;
6979 default:
6980 c_parser_error (parser,
6981 "expected %<+%>, %<*%>, %<-%>, %<&%>, "
6982 "%<^%>, %<|%>, %<&&%>, or %<||%>");
6983 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
6984 return list;
6986 c_parser_consume_token (parser);
6987 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
6989 tree nl, c;
6991 nl = c_parser_omp_variable_list (parser, OMP_CLAUSE_REDUCTION, list);
6992 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
6993 OMP_CLAUSE_REDUCTION_CODE (c) = code;
6995 list = nl;
6997 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
6999 return list;
7002 /* OpenMP 2.5:
7003 schedule ( schedule-kind )
7004 schedule ( schedule-kind , expression )
7006 schedule-kind:
7007 static | dynamic | guided | runtime
7010 static tree
7011 c_parser_omp_clause_schedule (c_parser *parser, tree list)
7013 tree c, t;
7015 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7016 return list;
7018 c = build_omp_clause (OMP_CLAUSE_SCHEDULE);
7020 if (c_parser_next_token_is (parser, CPP_NAME))
7022 tree kind = c_parser_peek_token (parser)->value;
7023 const char *p = IDENTIFIER_POINTER (kind);
7025 switch (p[0])
7027 case 'd':
7028 if (strcmp ("dynamic", p) != 0)
7029 goto invalid_kind;
7030 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
7031 break;
7033 case 'g':
7034 if (strcmp ("guided", p) != 0)
7035 goto invalid_kind;
7036 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
7037 break;
7039 case 'r':
7040 if (strcmp ("runtime", p) != 0)
7041 goto invalid_kind;
7042 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
7043 break;
7045 default:
7046 goto invalid_kind;
7049 else if (c_parser_next_token_is_keyword (parser, RID_STATIC))
7050 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
7051 else
7052 goto invalid_kind;
7054 c_parser_consume_token (parser);
7055 if (c_parser_next_token_is (parser, CPP_COMMA))
7057 c_parser_consume_token (parser);
7059 t = c_parser_expr_no_commas (parser, NULL).value;
7061 if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
7062 error ("schedule %<runtime%> does not take "
7063 "a %<chunk_size%> parameter");
7064 else if (TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE)
7065 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
7066 else
7067 c_parser_error (parser, "expected integer expression");
7069 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
7071 else
7072 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7073 "expected %<,%> or %<)%>");
7075 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
7076 OMP_CLAUSE_CHAIN (c) = list;
7077 return c;
7079 invalid_kind:
7080 c_parser_error (parser, "invalid schedule kind");
7081 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
7082 return list;
7085 /* OpenMP 2.5:
7086 shared ( variable-list ) */
7088 static tree
7089 c_parser_omp_clause_shared (c_parser *parser, tree list)
7091 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_SHARED, list);
7094 /* Parse all OpenMP clauses. The set clauses allowed by the directive
7095 is a bitmask in MASK. Return the list of clauses found; the result
7096 of clause default goes in *pdefault. */
7098 static tree
7099 c_parser_omp_all_clauses (c_parser *parser, unsigned int mask,
7100 const char *where)
7102 tree clauses = NULL;
7104 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
7106 const pragma_omp_clause c_kind = c_parser_omp_clause_name (parser);
7107 const char *c_name;
7108 tree prev = clauses;
7110 switch (c_kind)
7112 case PRAGMA_OMP_CLAUSE_COPYIN:
7113 clauses = c_parser_omp_clause_copyin (parser, clauses);
7114 c_name = "copyin";
7115 break;
7116 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
7117 clauses = c_parser_omp_clause_copyprivate (parser, clauses);
7118 c_name = "copyprivate";
7119 break;
7120 case PRAGMA_OMP_CLAUSE_DEFAULT:
7121 clauses = c_parser_omp_clause_default (parser, clauses);
7122 c_name = "default";
7123 break;
7124 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
7125 clauses = c_parser_omp_clause_firstprivate (parser, clauses);
7126 c_name = "firstprivate";
7127 break;
7128 case PRAGMA_OMP_CLAUSE_IF:
7129 clauses = c_parser_omp_clause_if (parser, clauses);
7130 c_name = "if";
7131 break;
7132 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
7133 clauses = c_parser_omp_clause_lastprivate (parser, clauses);
7134 c_name = "lastprivate";
7135 break;
7136 case PRAGMA_OMP_CLAUSE_NOWAIT:
7137 clauses = c_parser_omp_clause_nowait (parser, clauses);
7138 c_name = "nowait";
7139 break;
7140 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
7141 clauses = c_parser_omp_clause_num_threads (parser, clauses);
7142 c_name = "num_threads";
7143 break;
7144 case PRAGMA_OMP_CLAUSE_ORDERED:
7145 clauses = c_parser_omp_clause_ordered (parser, clauses);
7146 c_name = "ordered";
7147 break;
7148 case PRAGMA_OMP_CLAUSE_PRIVATE:
7149 clauses = c_parser_omp_clause_private (parser, clauses);
7150 c_name = "private";
7151 break;
7152 case PRAGMA_OMP_CLAUSE_REDUCTION:
7153 clauses = c_parser_omp_clause_reduction (parser, clauses);
7154 c_name = "reduction";
7155 break;
7156 case PRAGMA_OMP_CLAUSE_SCHEDULE:
7157 clauses = c_parser_omp_clause_schedule (parser, clauses);
7158 c_name = "schedule";
7159 break;
7160 case PRAGMA_OMP_CLAUSE_SHARED:
7161 clauses = c_parser_omp_clause_shared (parser, clauses);
7162 c_name = "shared";
7163 break;
7164 default:
7165 c_parser_error (parser, "expected %<#pragma omp%> clause");
7166 goto saw_error;
7169 if (((mask >> c_kind) & 1) == 0 && !parser->error)
7171 /* Remove the invalid clause(s) from the list to avoid
7172 confusing the rest of the compiler. */
7173 clauses = prev;
7174 error ("%qs is not valid for %qs", c_name, where);
7178 saw_error:
7179 c_parser_skip_to_pragma_eol (parser);
7181 return c_finish_omp_clauses (clauses);
7184 /* OpenMP 2.5:
7185 structured-block:
7186 statement
7188 In practice, we're also interested in adding the statement to an
7189 outer node. So it is convenient if we work around the fact that
7190 c_parser_statement calls add_stmt. */
7192 static tree
7193 c_parser_omp_structured_block (c_parser *parser)
7195 tree stmt = push_stmt_list ();
7196 c_parser_statement (parser);
7197 return pop_stmt_list (stmt);
7200 /* OpenMP 2.5:
7201 # pragma omp atomic new-line
7202 expression-stmt
7204 expression-stmt:
7205 x binop= expr | x++ | ++x | x-- | --x
7206 binop:
7207 +, *, -, /, &, ^, |, <<, >>
7209 where x is an lvalue expression with scalar type. */
7211 static void
7212 c_parser_omp_atomic (c_parser *parser)
7214 tree lhs, rhs;
7215 tree stmt;
7216 enum tree_code code;
7218 c_parser_skip_to_pragma_eol (parser);
7220 lhs = c_parser_unary_expression (parser).value;
7221 switch (TREE_CODE (lhs))
7223 case ERROR_MARK:
7224 saw_error:
7225 c_parser_skip_to_end_of_block_or_statement (parser);
7226 return;
7228 case PREINCREMENT_EXPR:
7229 case POSTINCREMENT_EXPR:
7230 lhs = TREE_OPERAND (lhs, 0);
7231 code = PLUS_EXPR;
7232 rhs = integer_one_node;
7233 break;
7235 case PREDECREMENT_EXPR:
7236 case POSTDECREMENT_EXPR:
7237 lhs = TREE_OPERAND (lhs, 0);
7238 code = MINUS_EXPR;
7239 rhs = integer_one_node;
7240 break;
7242 default:
7243 switch (c_parser_peek_token (parser)->type)
7245 case CPP_MULT_EQ:
7246 code = MULT_EXPR;
7247 break;
7248 case CPP_DIV_EQ:
7249 code = TRUNC_DIV_EXPR;
7250 break;
7251 case CPP_PLUS_EQ:
7252 code = PLUS_EXPR;
7253 break;
7254 case CPP_MINUS_EQ:
7255 code = MINUS_EXPR;
7256 break;
7257 case CPP_LSHIFT_EQ:
7258 code = LSHIFT_EXPR;
7259 break;
7260 case CPP_RSHIFT_EQ:
7261 code = RSHIFT_EXPR;
7262 break;
7263 case CPP_AND_EQ:
7264 code = BIT_AND_EXPR;
7265 break;
7266 case CPP_OR_EQ:
7267 code = BIT_IOR_EXPR;
7268 break;
7269 case CPP_XOR_EQ:
7270 code = BIT_XOR_EXPR;
7271 break;
7272 default:
7273 c_parser_error (parser,
7274 "invalid operator for %<#pragma omp atomic%>");
7275 goto saw_error;
7278 c_parser_consume_token (parser);
7279 rhs = c_parser_expression (parser).value;
7280 break;
7282 stmt = c_finish_omp_atomic (code, lhs, rhs);
7283 if (stmt != error_mark_node)
7284 add_stmt (stmt);
7285 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
7289 /* OpenMP 2.5:
7290 # pragma omp barrier new-line
7293 static void
7294 c_parser_omp_barrier (c_parser *parser)
7296 c_parser_consume_pragma (parser);
7297 c_parser_skip_to_pragma_eol (parser);
7299 c_finish_omp_barrier ();
7302 /* OpenMP 2.5:
7303 # pragma omp critical [(name)] new-line
7304 structured-block
7307 static tree
7308 c_parser_omp_critical (c_parser *parser)
7310 tree stmt, name = NULL;
7312 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
7314 c_parser_consume_token (parser);
7315 if (c_parser_next_token_is (parser, CPP_NAME))
7317 name = c_parser_peek_token (parser)->value;
7318 c_parser_consume_token (parser);
7319 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
7321 else
7322 c_parser_error (parser, "expected identifier");
7324 else if (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
7325 c_parser_error (parser, "expected %<(%> or end of line");
7326 c_parser_skip_to_pragma_eol (parser);
7328 stmt = c_parser_omp_structured_block (parser);
7329 return c_finish_omp_critical (stmt, name);
7332 /* OpenMP 2.5:
7333 # pragma omp flush flush-vars[opt] new-line
7335 flush-vars:
7336 ( variable-list ) */
7338 static void
7339 c_parser_omp_flush (c_parser *parser)
7341 c_parser_consume_pragma (parser);
7342 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
7343 c_parser_omp_var_list_parens (parser, 0, NULL);
7344 else if (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
7345 c_parser_error (parser, "expected %<(%> or end of line");
7346 c_parser_skip_to_pragma_eol (parser);
7348 c_finish_omp_flush ();
7351 /* Parse the restricted form of the for statment allowed by OpenMP.
7352 The real trick here is to determine the loop control variable early
7353 so that we can push a new decl if necessary to make it private. */
7355 static tree
7356 c_parser_omp_for_loop (c_parser *parser)
7358 tree decl, cond, incr, save_break, save_cont, body, init;
7359 location_t loc;
7361 if (!c_parser_next_token_is_keyword (parser, RID_FOR))
7363 c_parser_error (parser, "for statement expected");
7364 return NULL;
7366 loc = c_parser_peek_token (parser)->location;
7367 c_parser_consume_token (parser);
7369 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7370 return NULL;
7372 /* Parse the initialization declaration or expression. */
7373 if (c_parser_next_token_starts_declspecs (parser))
7375 c_parser_declaration_or_fndef (parser, true, true, true, true);
7376 decl = check_for_loop_decls ();
7377 if (decl == NULL)
7378 goto error_init;
7379 init = decl;
7381 else if (c_parser_next_token_is (parser, CPP_NAME)
7382 && c_parser_peek_2nd_token (parser)->type == CPP_EQ)
7384 decl = c_parser_postfix_expression (parser).value;
7386 c_parser_require (parser, CPP_EQ, "expected %<=%>");
7388 init = c_parser_expr_no_commas (parser, NULL).value;
7389 init = build_modify_expr (decl, NOP_EXPR, init);
7390 init = c_process_expr_stmt (init);
7392 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
7394 else
7395 goto error_init;
7397 /* Parse the loop condition. */
7398 cond = NULL_TREE;
7399 if (c_parser_next_token_is_not (parser, CPP_SEMICOLON))
7401 cond = c_parser_expression_conv (parser).value;
7402 cond = c_objc_common_truthvalue_conversion (cond);
7403 if (CAN_HAVE_LOCATION_P (cond))
7404 SET_EXPR_LOCATION (cond, input_location);
7406 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
7408 /* Parse the increment expression. */
7409 incr = NULL_TREE;
7410 if (c_parser_next_token_is_not (parser, CPP_CLOSE_PAREN))
7411 incr = c_process_expr_stmt (c_parser_expression (parser).value);
7412 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
7414 parse_body:
7415 save_break = c_break_label;
7416 c_break_label = size_one_node;
7417 save_cont = c_cont_label;
7418 c_cont_label = NULL_TREE;
7419 body = push_stmt_list ();
7421 add_stmt (c_parser_c99_block_statement (parser));
7422 if (c_cont_label)
7423 add_stmt (build1 (LABEL_EXPR, void_type_node, c_cont_label));
7425 body = pop_stmt_list (body);
7426 c_break_label = save_break;
7427 c_cont_label = save_cont;
7429 /* Only bother calling c_finish_omp_for if we havn't already generated
7430 an error from the initialization parsing. */
7431 if (decl != NULL && decl != error_mark_node && init != error_mark_node)
7432 return c_finish_omp_for (loc, decl, init, cond, incr, body, NULL);
7433 return NULL;
7435 error_init:
7436 c_parser_error (parser, "expected iteration declaration or initialization");
7437 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
7438 decl = init = cond = incr = NULL_TREE;
7439 goto parse_body;
7442 /* OpenMP 2.5:
7443 #pragma omp for for-clause[optseq] new-line
7444 for-loop
7447 #define OMP_FOR_CLAUSE_MASK \
7448 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
7449 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
7450 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
7451 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
7452 | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \
7453 | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \
7454 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
7456 static tree
7457 c_parser_omp_for (c_parser *parser)
7459 tree block, clauses, ret;
7461 clauses = c_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
7462 "#pragma omp for");
7464 block = c_begin_compound_stmt (true);
7465 ret = c_parser_omp_for_loop (parser);
7466 if (ret)
7467 OMP_FOR_CLAUSES (ret) = clauses;
7468 block = c_end_compound_stmt (block, true);
7469 add_stmt (block);
7471 return ret;
7474 /* OpenMP 2.5:
7475 # pragma omp master new-line
7476 structured-block
7479 static tree
7480 c_parser_omp_master (c_parser *parser)
7482 c_parser_skip_to_pragma_eol (parser);
7483 return c_finish_omp_master (c_parser_omp_structured_block (parser));
7486 /* OpenMP 2.5:
7487 # pragma omp ordered new-line
7488 structured-block
7491 static tree
7492 c_parser_omp_ordered (c_parser *parser)
7494 c_parser_skip_to_pragma_eol (parser);
7495 return c_finish_omp_ordered (c_parser_omp_structured_block (parser));
7498 /* OpenMP 2.5:
7500 section-scope:
7501 { section-sequence }
7503 section-sequence:
7504 section-directive[opt] structured-block
7505 section-sequence section-directive structured-block */
7507 static tree
7508 c_parser_omp_sections_scope (c_parser *parser)
7510 tree stmt, substmt;
7511 bool error_suppress = false;
7512 location_t loc;
7514 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
7516 /* Avoid skipping until the end of the block. */
7517 parser->error = false;
7518 return NULL_TREE;
7521 stmt = push_stmt_list ();
7523 loc = c_parser_peek_token (parser)->location;
7524 if (c_parser_peek_token (parser)->pragma_kind != PRAGMA_OMP_SECTION)
7526 substmt = push_stmt_list ();
7528 while (1)
7530 c_parser_statement (parser);
7532 if (c_parser_peek_token (parser)->pragma_kind == PRAGMA_OMP_SECTION)
7533 break;
7534 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
7535 break;
7536 if (c_parser_next_token_is (parser, CPP_EOF))
7537 break;
7540 substmt = pop_stmt_list (substmt);
7541 substmt = build1 (OMP_SECTION, void_type_node, substmt);
7542 SET_EXPR_LOCATION (substmt, loc);
7543 add_stmt (substmt);
7546 while (1)
7548 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
7549 break;
7550 if (c_parser_next_token_is (parser, CPP_EOF))
7551 break;
7553 loc = c_parser_peek_token (parser)->location;
7554 if (c_parser_peek_token (parser)->pragma_kind == PRAGMA_OMP_SECTION)
7556 c_parser_consume_pragma (parser);
7557 c_parser_skip_to_pragma_eol (parser);
7558 error_suppress = false;
7560 else if (!error_suppress)
7562 error ("expected %<#pragma omp section%> or %<}%>");
7563 error_suppress = true;
7566 substmt = c_parser_omp_structured_block (parser);
7567 substmt = build1 (OMP_SECTION, void_type_node, substmt);
7568 SET_EXPR_LOCATION (substmt, loc);
7569 add_stmt (substmt);
7571 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE,
7572 "expected %<#pragma omp section%> or %<}%>");
7574 substmt = pop_stmt_list (stmt);
7576 stmt = make_node (OMP_SECTIONS);
7577 TREE_TYPE (stmt) = void_type_node;
7578 OMP_SECTIONS_BODY (stmt) = substmt;
7580 return add_stmt (stmt);
7583 /* OpenMP 2.5:
7584 # pragma omp sections sections-clause[optseq] newline
7585 sections-scope
7588 #define OMP_SECTIONS_CLAUSE_MASK \
7589 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
7590 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
7591 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
7592 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
7593 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
7595 static tree
7596 c_parser_omp_sections (c_parser *parser)
7598 tree block, clauses, ret;
7600 clauses = c_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
7601 "#pragma omp sections");
7603 block = c_begin_compound_stmt (true);
7604 ret = c_parser_omp_sections_scope (parser);
7605 if (ret)
7606 OMP_SECTIONS_CLAUSES (ret) = clauses;
7607 block = c_end_compound_stmt (block, true);
7608 add_stmt (block);
7610 return ret;
7613 /* OpenMP 2.5:
7614 # pragma parallel parallel-clause new-line
7615 # pragma parallel for parallel-for-clause new-line
7616 # pragma parallel sections parallel-sections-clause new-line
7619 #define OMP_PARALLEL_CLAUSE_MASK \
7620 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
7621 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
7622 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
7623 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
7624 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
7625 | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \
7626 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
7627 | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
7629 static tree
7630 c_parser_omp_parallel (c_parser *parser)
7632 enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
7633 const char *p_name = "#pragma omp parallel";
7634 tree stmt, clauses, par_clause, ws_clause, block;
7635 unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
7637 if (c_parser_next_token_is_keyword (parser, RID_FOR))
7639 c_parser_consume_token (parser);
7640 p_kind = PRAGMA_OMP_PARALLEL_FOR;
7641 p_name = "#pragma omp parallel for";
7642 mask |= OMP_FOR_CLAUSE_MASK;
7643 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
7645 else if (c_parser_next_token_is (parser, CPP_NAME))
7647 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
7648 if (strcmp (p, "sections") == 0)
7650 c_parser_consume_token (parser);
7651 p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
7652 p_name = "#pragma omp parallel sections";
7653 mask |= OMP_SECTIONS_CLAUSE_MASK;
7654 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
7658 clauses = c_parser_omp_all_clauses (parser, mask, p_name);
7660 switch (p_kind)
7662 case PRAGMA_OMP_PARALLEL:
7663 block = c_begin_omp_parallel ();
7664 c_parser_statement (parser);
7665 stmt = c_finish_omp_parallel (clauses, block);
7666 break;
7668 case PRAGMA_OMP_PARALLEL_FOR:
7669 block = c_begin_omp_parallel ();
7670 c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
7671 stmt = c_parser_omp_for_loop (parser);
7672 if (stmt)
7673 OMP_FOR_CLAUSES (stmt) = ws_clause;
7674 stmt = c_finish_omp_parallel (par_clause, block);
7675 OMP_PARALLEL_COMBINED (stmt) = 1;
7676 break;
7678 case PRAGMA_OMP_PARALLEL_SECTIONS:
7679 block = c_begin_omp_parallel ();
7680 c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
7681 stmt = c_parser_omp_sections_scope (parser);
7682 if (stmt)
7683 OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
7684 stmt = c_finish_omp_parallel (par_clause, block);
7685 OMP_PARALLEL_COMBINED (stmt) = 1;
7686 break;
7688 default:
7689 gcc_unreachable ();
7692 return stmt;
7695 /* OpenMP 2.5:
7696 # pragma omp single single-clause[optseq] new-line
7697 structured-block
7700 #define OMP_SINGLE_CLAUSE_MASK \
7701 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
7702 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
7703 | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
7704 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
7706 static tree
7707 c_parser_omp_single (c_parser *parser)
7709 tree stmt = make_node (OMP_SINGLE);
7710 TREE_TYPE (stmt) = void_type_node;
7712 OMP_SINGLE_CLAUSES (stmt)
7713 = c_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
7714 "#pragma omp single");
7715 OMP_SINGLE_BODY (stmt) = c_parser_omp_structured_block (parser);
7717 return add_stmt (stmt);
7721 /* Main entry point to parsing most OpenMP pragmas. */
7723 static void
7724 c_parser_omp_construct (c_parser *parser)
7726 enum pragma_kind p_kind;
7727 location_t loc;
7728 tree stmt;
7730 loc = c_parser_peek_token (parser)->location;
7731 p_kind = c_parser_peek_token (parser)->pragma_kind;
7732 c_parser_consume_pragma (parser);
7734 /* For all constructs below except #pragma omp atomic
7735 MUST_NOT_THROW catch handlers are needed when exceptions
7736 are enabled. */
7737 if (p_kind != PRAGMA_OMP_ATOMIC)
7738 c_maybe_initialize_eh ();
7740 switch (p_kind)
7742 case PRAGMA_OMP_ATOMIC:
7743 c_parser_omp_atomic (parser);
7744 return;
7745 case PRAGMA_OMP_CRITICAL:
7746 stmt = c_parser_omp_critical (parser);
7747 break;
7748 case PRAGMA_OMP_FOR:
7749 stmt = c_parser_omp_for (parser);
7750 break;
7751 case PRAGMA_OMP_MASTER:
7752 stmt = c_parser_omp_master (parser);
7753 break;
7754 case PRAGMA_OMP_ORDERED:
7755 stmt = c_parser_omp_ordered (parser);
7756 break;
7757 case PRAGMA_OMP_PARALLEL:
7758 stmt = c_parser_omp_parallel (parser);
7759 break;
7760 case PRAGMA_OMP_SECTIONS:
7761 stmt = c_parser_omp_sections (parser);
7762 break;
7763 case PRAGMA_OMP_SINGLE:
7764 stmt = c_parser_omp_single (parser);
7765 break;
7766 default:
7767 gcc_unreachable ();
7770 if (stmt)
7771 SET_EXPR_LOCATION (stmt, loc);
7775 /* OpenMP 2.5:
7776 # pragma omp threadprivate (variable-list) */
7778 static void
7779 c_parser_omp_threadprivate (c_parser *parser)
7781 tree vars, t;
7783 c_parser_consume_pragma (parser);
7784 vars = c_parser_omp_var_list_parens (parser, 0, NULL);
7786 /* Mark every variable in VARS to be assigned thread local storage. */
7787 for (t = vars; t; t = TREE_CHAIN (t))
7789 tree v = TREE_PURPOSE (t);
7791 /* If V had already been marked threadprivate, it doesn't matter
7792 whether it had been used prior to this point. */
7793 if (TREE_USED (v) && !C_DECL_THREADPRIVATE_P (v))
7794 error ("%qE declared %<threadprivate%> after first use", v);
7795 else if (! TREE_STATIC (v) && ! DECL_EXTERNAL (v))
7796 error ("automatic variable %qE cannot be %<threadprivate%>", v);
7797 else if (! COMPLETE_TYPE_P (TREE_TYPE (v)))
7798 error ("%<threadprivate%> %qE has incomplete type", v);
7799 else
7801 if (! DECL_THREAD_LOCAL_P (v))
7803 DECL_TLS_MODEL (v) = decl_default_tls_model (v);
7804 /* If rtl has been already set for this var, call
7805 make_decl_rtl once again, so that encode_section_info
7806 has a chance to look at the new decl flags. */
7807 if (DECL_RTL_SET_P (v))
7808 make_decl_rtl (v);
7810 C_DECL_THREADPRIVATE_P (v) = 1;
7814 c_parser_skip_to_pragma_eol (parser);
7818 /* Parse a single source file. */
7820 void
7821 c_parse_file (void)
7823 /* Use local storage to begin. If the first token is a pragma, parse it.
7824 If it is #pragma GCC pch_preprocess, then this will load a PCH file
7825 which will cause garbage collection. */
7826 c_parser tparser;
7828 memset (&tparser, 0, sizeof tparser);
7829 the_parser = &tparser;
7831 if (c_parser_peek_token (&tparser)->pragma_kind == PRAGMA_GCC_PCH_PREPROCESS)
7832 c_parser_pragma_pch_preprocess (&tparser);
7834 the_parser = GGC_NEW (c_parser);
7835 *the_parser = tparser;
7837 c_parser_translation_unit (the_parser);
7838 the_parser = NULL;
7841 #include "gt-c-parser.h"