OSDN Git Service

* c-parser.c (c_parser_for_statement): Initialize incr.
[pf3gnuchains/gcc-fork.git] / gcc / c-parser.c
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, 2007, 2008, 2009, 2010
4    Free Software Foundation, Inc.
5
6    Parser actions based on the old Bison parser; structure somewhat
7    influenced by and fragments based on the C++ parser.
8
9 This file is part of GCC.
10
11 GCC is free software; you can redistribute it and/or modify it under
12 the terms of the GNU General Public License as published by the Free
13 Software Foundation; either version 3, or (at your option) any later
14 version.
15
16 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
17 WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
19 for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with GCC; see the file COPYING3.  If not see
23 <http://www.gnu.org/licenses/>.  */
24
25 /* TODO:
26
27    Make sure all relevant comments, and all relevant code from all
28    actions, brought over from old parser.  Verify exact correspondence
29    of syntax accepted.
30
31    Add testcases covering every input symbol in every state in old and
32    new parsers.
33
34    Include full syntax for GNU C, including erroneous cases accepted
35    with error messages, in syntax productions in comments.
36
37    Make more diagnostics in the front end generally take an explicit
38    location rather than implicitly using input_location.  */
39
40 #include "config.h"
41 #include "system.h"
42 #include "coretypes.h"
43 #include "tm.h"                 /* For rtl.h: needs enum reg_class.  */
44 #include "tree.h"
45 #include "langhooks.h"
46 #include "input.h"
47 #include "cpplib.h"
48 #include "timevar.h"
49 #include "c-family/c-pragma.h"
50 #include "c-tree.h"
51 #include "flags.h"
52 #include "output.h"
53 #include "toplev.h"
54 #include "ggc.h"
55 #include "c-family/c-common.h"
56 #include "vec.h"
57 #include "target.h"
58 #include "cgraph.h"
59 #include "plugin.h"
60
61 \f
62 /* Initialization routine for this file.  */
63
64 void
65 c_parse_init (void)
66 {
67   /* The only initialization required is of the reserved word
68      identifiers.  */
69   unsigned int i;
70   tree id;
71   int mask = 0;
72
73   /* Make sure RID_MAX hasn't grown past the 8 bits used to hold the keyword in
74      the c_token structure.  */
75   gcc_assert (RID_MAX <= 255);
76
77   mask |= D_CXXONLY;
78   if (!flag_isoc99)
79     mask |= D_C99;
80   if (flag_no_asm)
81     {
82       mask |= D_ASM | D_EXT;
83       if (!flag_isoc99)
84         mask |= D_EXT89;
85     }
86   if (!c_dialect_objc ())
87     mask |= D_OBJC | D_CXX_OBJC;
88
89   ridpointers = ggc_alloc_cleared_vec_tree ((int) RID_MAX);
90   for (i = 0; i < num_c_common_reswords; i++)
91     {
92       /* If a keyword is disabled, do not enter it into the table
93          and so create a canonical spelling that isn't a keyword.  */
94       if (c_common_reswords[i].disable & mask)
95         {
96           if (warn_cxx_compat
97               && (c_common_reswords[i].disable & D_CXXWARN))
98             {
99               id = get_identifier (c_common_reswords[i].word);
100               C_SET_RID_CODE (id, RID_CXX_COMPAT_WARN);
101               C_IS_RESERVED_WORD (id) = 1;
102             }
103           continue;
104         }
105
106       id = get_identifier (c_common_reswords[i].word);
107       C_SET_RID_CODE (id, c_common_reswords[i].rid);
108       C_IS_RESERVED_WORD (id) = 1;
109       ridpointers [(int) c_common_reswords[i].rid] = id;
110     }
111 }
112 \f
113 /* The C lexer intermediates between the lexer in cpplib and c-lex.c
114    and the C parser.  Unlike the C++ lexer, the parser structure
115    stores the lexer information instead of using a separate structure.
116    Identifiers are separated into ordinary identifiers, type names,
117    keywords and some other Objective-C types of identifiers, and some
118    look-ahead is maintained.
119
120    ??? It might be a good idea to lex the whole file up front (as for
121    C++).  It would then be possible to share more of the C and C++
122    lexer code, if desired.  */
123
124 /* The following local token type is used.  */
125
126 /* A keyword.  */
127 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
128
129 /* More information about the type of a CPP_NAME token.  */
130 typedef enum c_id_kind {
131   /* An ordinary identifier.  */
132   C_ID_ID,
133   /* An identifier declared as a typedef name.  */
134   C_ID_TYPENAME,
135   /* An identifier declared as an Objective-C class name.  */
136   C_ID_CLASSNAME,
137   /* An address space identifier.  */
138   C_ID_ADDRSPACE,
139   /* Not an identifier.  */
140   C_ID_NONE
141 } c_id_kind;
142
143 /* A single C token after string literal concatenation and conversion
144    of preprocessing tokens to tokens.  */
145 typedef struct GTY (()) c_token {
146   /* The kind of token.  */
147   ENUM_BITFIELD (cpp_ttype) type : 8;
148   /* If this token is a CPP_NAME, this value indicates whether also
149      declared as some kind of type.  Otherwise, it is C_ID_NONE.  */
150   ENUM_BITFIELD (c_id_kind) id_kind : 8;
151   /* If this token is a keyword, this value indicates which keyword.
152      Otherwise, this value is RID_MAX.  */
153   ENUM_BITFIELD (rid) keyword : 8;
154   /* If this token is a CPP_PRAGMA, this indicates the pragma that
155      was seen.  Otherwise it is PRAGMA_NONE.  */
156   ENUM_BITFIELD (pragma_kind) pragma_kind : 8;
157   /* The location at which this token was found.  */
158   location_t location;
159   /* The value associated with this token, if any.  */
160   tree value;
161 } c_token;
162
163 /* A parser structure recording information about the state and
164    context of parsing.  Includes lexer information with up to two
165    tokens of look-ahead; more are not needed for C.  */
166 typedef struct GTY(()) c_parser {
167   /* The look-ahead tokens.  */
168   c_token tokens[2];
169   /* How many look-ahead tokens are available (0, 1 or 2).  */
170   short tokens_avail;
171   /* True if a syntax error is being recovered from; false otherwise.
172      c_parser_error sets this flag.  It should clear this flag when
173      enough tokens have been consumed to recover from the error.  */
174   BOOL_BITFIELD error : 1;
175   /* True if we're processing a pragma, and shouldn't automatically
176      consume CPP_PRAGMA_EOL.  */
177   BOOL_BITFIELD in_pragma : 1;
178   /* True if we're parsing the outermost block of an if statement.  */
179   BOOL_BITFIELD in_if_block : 1;
180   /* True if we want to lex an untranslated string.  */
181   BOOL_BITFIELD lex_untranslated_string : 1;
182
183   /* Objective-C specific parser/lexer information.  */
184
185   /* True if we are in a context where the Objective-C "PQ" keywords
186      are considered keywords.  */
187   BOOL_BITFIELD objc_pq_context : 1;
188   /* True if we are parsing a (potential) Objective-C foreach
189      statement.  This is set to true after we parsed 'for (' and while
190      we wait for 'in' or ';' to decide if it's a standard C for loop or an
191      Objective-C foreach loop.  */
192   BOOL_BITFIELD objc_could_be_foreach_context : 1;
193   /* The following flag is needed to contextualize Objective-C lexical
194      analysis.  In some cases (e.g., 'int NSObject;'), it is
195      undesirable to bind an identifier to an Objective-C class, even
196      if a class with that name exists.  */
197   BOOL_BITFIELD objc_need_raw_identifier : 1;
198 } c_parser;
199
200
201 /* The actual parser and external interface.  ??? Does this need to be
202    garbage-collected?  */
203
204 static GTY (()) c_parser *the_parser;
205
206 /* Read in and lex a single token, storing it in *TOKEN.  */
207
208 static void
209 c_lex_one_token (c_parser *parser, c_token *token)
210 {
211   timevar_push (TV_LEX);
212
213   token->type = c_lex_with_flags (&token->value, &token->location, NULL,
214                                   (parser->lex_untranslated_string
215                                    ? C_LEX_STRING_NO_TRANSLATE : 0));
216   token->id_kind = C_ID_NONE;
217   token->keyword = RID_MAX;
218   token->pragma_kind = PRAGMA_NONE;
219
220   switch (token->type)
221     {
222     case CPP_NAME:
223       {
224         tree decl;
225
226         bool objc_force_identifier = parser->objc_need_raw_identifier;
227         if (c_dialect_objc ())
228           parser->objc_need_raw_identifier = false;
229
230         if (C_IS_RESERVED_WORD (token->value))
231           {
232             enum rid rid_code = C_RID_CODE (token->value);
233
234             if (rid_code == RID_CXX_COMPAT_WARN)
235               {
236                 warning_at (token->location,
237                             OPT_Wc___compat,
238                             "identifier %qE conflicts with C++ keyword",
239                             token->value);
240               }
241             else if (rid_code >= RID_FIRST_ADDR_SPACE
242                      && rid_code <= RID_LAST_ADDR_SPACE)
243               {
244                 token->id_kind = C_ID_ADDRSPACE;
245                 token->keyword = rid_code;
246                 break;
247               }
248             else if (c_dialect_objc () && OBJC_IS_PQ_KEYWORD (rid_code))
249               {
250                 /* We found an Objective-C "pq" keyword (in, out,
251                    inout, bycopy, byref, oneway).  They need special
252                    care because the interpretation depends on the
253                    context.
254                  */
255                 if (parser->objc_pq_context)
256                   {
257                     token->type = CPP_KEYWORD;
258                     token->keyword = rid_code;
259                     break;
260                   }
261                 else if (parser->objc_could_be_foreach_context
262                          && rid_code == RID_IN)
263                   {
264                     /* We are in Objective-C, inside a (potential)
265                        foreach context (which means after having
266                        parsed 'for (', but before having parsed ';'),
267                        and we found 'in'.  We consider it the keyword
268                        which terminates the declaration at the
269                        beginning of a foreach-statement.  Note that
270                        this means you can't use 'in' for anything else
271                        in that context; in particular, in Objective-C
272                        you can't use 'in' as the name of the running
273                        variable in a C for loop.  We could potentially
274                        try to add code here to disambiguate, but it
275                        seems a reasonable limitation.
276                     */
277                     token->type = CPP_KEYWORD;
278                     token->keyword = rid_code;
279                     break;
280                   }
281                 /* Else, "pq" keywords outside of the "pq" context are
282                    not keywords, and we fall through to the code for
283                    normal tokens.
284                 */
285               }
286             else if (c_dialect_objc () 
287                      && (OBJC_IS_AT_KEYWORD (rid_code)
288                          || OBJC_IS_CXX_KEYWORD (rid_code)))
289               {
290                 /* We found one of the Objective-C "@" keywords (defs,
291                    selector, synchronized, etc) or one of the
292                    Objective-C "cxx" keywords (class, private,
293                    protected, public, try, catch, throw) without a
294                    preceding '@' sign.  Do nothing and fall through to
295                    the code for normal tokens (in C++ we would still
296                    consider the CXX ones keywords, but not in C).
297                 */
298                 ;
299               }
300             else
301               {
302                 token->type = CPP_KEYWORD;
303                 token->keyword = rid_code;
304                 break;
305               }
306           }
307
308         decl = lookup_name (token->value);
309         if (decl)
310           {
311             if (TREE_CODE (decl) == TYPE_DECL)
312               {
313                 token->id_kind = C_ID_TYPENAME;
314                 break;
315               }
316           }
317         else if (c_dialect_objc ())
318           {
319             tree objc_interface_decl = objc_is_class_name (token->value);
320             /* Objective-C class names are in the same namespace as
321                variables and typedefs, and hence are shadowed by local
322                declarations.  */
323             if (objc_interface_decl
324                 && (global_bindings_p ()
325                     || (!objc_force_identifier && !decl)))
326               {
327                 token->value = objc_interface_decl;
328                 token->id_kind = C_ID_CLASSNAME;
329                 break;
330               }
331           }
332         token->id_kind = C_ID_ID;
333       }
334       break;
335     case CPP_AT_NAME:
336       /* This only happens in Objective-C; it must be a keyword.  */
337       token->type = CPP_KEYWORD;
338       switch (C_RID_CODE (token->value))
339         {
340           /* Replace 'class' with '@class', 'private' with '@private',
341              etc.  This prevents confusion with the C++ keyword
342              'class', and makes the tokens consistent with other
343              Objective-C 'AT' keywords.  For example '@class' is
344              reported as RID_AT_CLASS which is consistent with
345              '@synchronized', which is reported as
346              RID_AT_SYNCHRONIZED.
347           */
348         case RID_CLASS:     token->keyword = RID_AT_CLASS; break;
349         case RID_PRIVATE:   token->keyword = RID_AT_PRIVATE; break;
350         case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
351         case RID_PUBLIC:    token->keyword = RID_AT_PUBLIC; break;
352         case RID_THROW:     token->keyword = RID_AT_THROW; break;
353         case RID_TRY:       token->keyword = RID_AT_TRY; break;
354         case RID_CATCH:     token->keyword = RID_AT_CATCH; break;
355         default:            token->keyword = C_RID_CODE (token->value);
356         }
357       break;
358     case CPP_COLON:
359     case CPP_COMMA:
360     case CPP_CLOSE_PAREN:
361     case CPP_SEMICOLON:
362       /* These tokens may affect the interpretation of any identifiers
363          following, if doing Objective-C.  */
364       if (c_dialect_objc ())
365         parser->objc_need_raw_identifier = false;
366       break;
367     case CPP_PRAGMA:
368       /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST.  */
369       token->pragma_kind = (enum pragma_kind) TREE_INT_CST_LOW (token->value);
370       token->value = NULL;
371       break;
372     default:
373       break;
374     }
375   timevar_pop (TV_LEX);
376 }
377
378 /* Return a pointer to the next token from PARSER, reading it in if
379    necessary.  */
380
381 static inline c_token *
382 c_parser_peek_token (c_parser *parser)
383 {
384   if (parser->tokens_avail == 0)
385     {
386       c_lex_one_token (parser, &parser->tokens[0]);
387       parser->tokens_avail = 1;
388     }
389   return &parser->tokens[0];
390 }
391
392 /* Return true if the next token from PARSER has the indicated
393    TYPE.  */
394
395 static inline bool
396 c_parser_next_token_is (c_parser *parser, enum cpp_ttype type)
397 {
398   return c_parser_peek_token (parser)->type == type;
399 }
400
401 /* Return true if the next token from PARSER does not have the
402    indicated TYPE.  */
403
404 static inline bool
405 c_parser_next_token_is_not (c_parser *parser, enum cpp_ttype type)
406 {
407   return !c_parser_next_token_is (parser, type);
408 }
409
410 /* Return true if the next token from PARSER is the indicated
411    KEYWORD.  */
412
413 static inline bool
414 c_parser_next_token_is_keyword (c_parser *parser, enum rid keyword)
415 {
416   return c_parser_peek_token (parser)->keyword == keyword;
417 }
418
419 /* Return true if TOKEN can start a type name,
420    false otherwise.  */
421 static bool
422 c_token_starts_typename (c_token *token)
423 {
424   switch (token->type)
425     {
426     case CPP_NAME:
427       switch (token->id_kind)
428         {
429         case C_ID_ID:
430           return false;
431         case C_ID_ADDRSPACE:
432           return true;
433         case C_ID_TYPENAME:
434           return true;
435         case C_ID_CLASSNAME:
436           gcc_assert (c_dialect_objc ());
437           return true;
438         default:
439           gcc_unreachable ();
440         }
441     case CPP_KEYWORD:
442       switch (token->keyword)
443         {
444         case RID_UNSIGNED:
445         case RID_LONG:
446         case RID_INT128:
447         case RID_SHORT:
448         case RID_SIGNED:
449         case RID_COMPLEX:
450         case RID_INT:
451         case RID_CHAR:
452         case RID_FLOAT:
453         case RID_DOUBLE:
454         case RID_VOID:
455         case RID_DFLOAT32:
456         case RID_DFLOAT64:
457         case RID_DFLOAT128:
458         case RID_BOOL:
459         case RID_ENUM:
460         case RID_STRUCT:
461         case RID_UNION:
462         case RID_TYPEOF:
463         case RID_CONST:
464         case RID_VOLATILE:
465         case RID_RESTRICT:
466         case RID_ATTRIBUTE:
467         case RID_FRACT:
468         case RID_ACCUM:
469         case RID_SAT:
470           return true;
471         default:
472           return false;
473         }
474     case CPP_LESS:
475       if (c_dialect_objc ())
476         return true;
477       return false;
478     default:
479       return false;
480     }
481 }
482
483 /* Return true if the next token from PARSER can start a type name,
484    false otherwise.  */
485 static inline bool
486 c_parser_next_token_starts_typename (c_parser *parser)
487 {
488   c_token *token = c_parser_peek_token (parser);
489   return c_token_starts_typename (token);
490 }
491
492 /* Return true if TOKEN can start declaration specifiers, false
493    otherwise.  */
494 static bool
495 c_token_starts_declspecs (c_token *token)
496 {
497   switch (token->type)
498     {
499     case CPP_NAME:
500       switch (token->id_kind)
501         {
502         case C_ID_ID:
503           return false;
504         case C_ID_ADDRSPACE:
505           return true;
506         case C_ID_TYPENAME:
507           return true;
508         case C_ID_CLASSNAME:
509           gcc_assert (c_dialect_objc ());
510           return true;
511         default:
512           gcc_unreachable ();
513         }
514     case CPP_KEYWORD:
515       switch (token->keyword)
516         {
517         case RID_STATIC:
518         case RID_EXTERN:
519         case RID_REGISTER:
520         case RID_TYPEDEF:
521         case RID_INLINE:
522         case RID_AUTO:
523         case RID_THREAD:
524         case RID_UNSIGNED:
525         case RID_LONG:
526         case RID_INT128:
527         case RID_SHORT:
528         case RID_SIGNED:
529         case RID_COMPLEX:
530         case RID_INT:
531         case RID_CHAR:
532         case RID_FLOAT:
533         case RID_DOUBLE:
534         case RID_VOID:
535         case RID_DFLOAT32:
536         case RID_DFLOAT64:
537         case RID_DFLOAT128:
538         case RID_BOOL:
539         case RID_ENUM:
540         case RID_STRUCT:
541         case RID_UNION:
542         case RID_TYPEOF:
543         case RID_CONST:
544         case RID_VOLATILE:
545         case RID_RESTRICT:
546         case RID_ATTRIBUTE:
547         case RID_FRACT:
548         case RID_ACCUM:
549         case RID_SAT:
550           return true;
551         default:
552           return false;
553         }
554     case CPP_LESS:
555       if (c_dialect_objc ())
556         return true;
557       return false;
558     default:
559       return false;
560     }
561 }
562
563
564 /* Return true if TOKEN can start declaration specifiers or a static
565    assertion, false otherwise.  */
566 static bool
567 c_token_starts_declaration (c_token *token)
568 {
569   if (c_token_starts_declspecs (token)
570       || token->keyword == RID_STATIC_ASSERT)
571     return true;
572   else
573     return false;
574 }
575
576 /* Return true if the next token from PARSER can start declaration
577    specifiers, false otherwise.  */
578 static inline bool
579 c_parser_next_token_starts_declspecs (c_parser *parser)
580 {
581   c_token *token = c_parser_peek_token (parser);
582   return c_token_starts_declspecs (token);
583 }
584
585 /* Return true if the next token from PARSER can start declaration
586    specifiers or a static assertion, false otherwise.  */
587 static inline bool
588 c_parser_next_token_starts_declaration (c_parser *parser)
589 {
590   c_token *token = c_parser_peek_token (parser);
591   return c_token_starts_declaration (token);
592 }
593
594 /* Return a pointer to the next-but-one token from PARSER, reading it
595    in if necessary.  The next token is already read in.  */
596
597 static c_token *
598 c_parser_peek_2nd_token (c_parser *parser)
599 {
600   if (parser->tokens_avail >= 2)
601     return &parser->tokens[1];
602   gcc_assert (parser->tokens_avail == 1);
603   gcc_assert (parser->tokens[0].type != CPP_EOF);
604   gcc_assert (parser->tokens[0].type != CPP_PRAGMA_EOL);
605   c_lex_one_token (parser, &parser->tokens[1]);
606   parser->tokens_avail = 2;
607   return &parser->tokens[1];
608 }
609
610 /* Consume the next token from PARSER.  */
611
612 static void
613 c_parser_consume_token (c_parser *parser)
614 {
615   gcc_assert (parser->tokens_avail >= 1);
616   gcc_assert (parser->tokens[0].type != CPP_EOF);
617   gcc_assert (!parser->in_pragma || parser->tokens[0].type != CPP_PRAGMA_EOL);
618   gcc_assert (parser->error || parser->tokens[0].type != CPP_PRAGMA);
619   if (parser->tokens_avail == 2)
620     parser->tokens[0] = parser->tokens[1];
621   parser->tokens_avail--;
622 }
623
624 /* Expect the current token to be a #pragma.  Consume it and remember
625    that we've begun parsing a pragma.  */
626
627 static void
628 c_parser_consume_pragma (c_parser *parser)
629 {
630   gcc_assert (!parser->in_pragma);
631   gcc_assert (parser->tokens_avail >= 1);
632   gcc_assert (parser->tokens[0].type == CPP_PRAGMA);
633   if (parser->tokens_avail == 2)
634     parser->tokens[0] = parser->tokens[1];
635   parser->tokens_avail--;
636   parser->in_pragma = true;
637 }
638
639 /* Update the globals input_location and in_system_header from
640    TOKEN.  */
641 static inline void
642 c_parser_set_source_position_from_token (c_token *token)
643 {
644   if (token->type != CPP_EOF)
645     {
646       input_location = token->location;
647     }
648 }
649
650 /* Issue a diagnostic of the form
651       FILE:LINE: MESSAGE before TOKEN
652    where TOKEN is the next token in the input stream of PARSER.
653    MESSAGE (specified by the caller) is usually of the form "expected
654    OTHER-TOKEN".
655
656    Do not issue a diagnostic if still recovering from an error.
657
658    ??? This is taken from the C++ parser, but building up messages in
659    this way is not i18n-friendly and some other approach should be
660    used.  */
661
662 static void
663 c_parser_error (c_parser *parser, const char *gmsgid)
664 {
665   c_token *token = c_parser_peek_token (parser);
666   if (parser->error)
667     return;
668   parser->error = true;
669   if (!gmsgid)
670     return;
671   /* This diagnostic makes more sense if it is tagged to the line of
672      the token we just peeked at.  */
673   c_parser_set_source_position_from_token (token);
674   c_parse_error (gmsgid,
675                  /* Because c_parse_error does not understand
676                     CPP_KEYWORD, keywords are treated like
677                     identifiers.  */
678                  (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
679                  /* ??? The C parser does not save the cpp flags of a
680                     token, we need to pass 0 here and we will not get
681                     the source spelling of some tokens but rather the
682                     canonical spelling.  */
683                  token->value, /*flags=*/0);
684 }
685
686 /* If the next token is of the indicated TYPE, consume it.  Otherwise,
687    issue the error MSGID.  If MSGID is NULL then a message has already
688    been produced and no message will be produced this time.  Returns
689    true if found, false otherwise.  */
690
691 static bool
692 c_parser_require (c_parser *parser,
693                   enum cpp_ttype type,
694                   const char *msgid)
695 {
696   if (c_parser_next_token_is (parser, type))
697     {
698       c_parser_consume_token (parser);
699       return true;
700     }
701   else
702     {
703       c_parser_error (parser, msgid);
704       return false;
705     }
706 }
707
708 /* If the next token is the indicated keyword, consume it.  Otherwise,
709    issue the error MSGID.  Returns true if found, false otherwise.  */
710
711 static bool
712 c_parser_require_keyword (c_parser *parser,
713                           enum rid keyword,
714                           const char *msgid)
715 {
716   if (c_parser_next_token_is_keyword (parser, keyword))
717     {
718       c_parser_consume_token (parser);
719       return true;
720     }
721   else
722     {
723       c_parser_error (parser, msgid);
724       return false;
725     }
726 }
727
728 /* Like c_parser_require, except that tokens will be skipped until the
729    desired token is found.  An error message is still produced if the
730    next token is not as expected.  If MSGID is NULL then a message has
731    already been produced and no message will be produced this
732    time.  */
733
734 static void
735 c_parser_skip_until_found (c_parser *parser,
736                            enum cpp_ttype type,
737                            const char *msgid)
738 {
739   unsigned nesting_depth = 0;
740
741   if (c_parser_require (parser, type, msgid))
742     return;
743
744   /* Skip tokens until the desired token is found.  */
745   while (true)
746     {
747       /* Peek at the next token.  */
748       c_token *token = c_parser_peek_token (parser);
749       /* If we've reached the token we want, consume it and stop.  */
750       if (token->type == type && !nesting_depth)
751         {
752           c_parser_consume_token (parser);
753           break;
754         }
755
756       /* If we've run out of tokens, stop.  */
757       if (token->type == CPP_EOF)
758         return;
759       if (token->type == CPP_PRAGMA_EOL && parser->in_pragma)
760         return;
761       if (token->type == CPP_OPEN_BRACE
762           || token->type == CPP_OPEN_PAREN
763           || token->type == CPP_OPEN_SQUARE)
764         ++nesting_depth;
765       else if (token->type == CPP_CLOSE_BRACE
766                || token->type == CPP_CLOSE_PAREN
767                || token->type == CPP_CLOSE_SQUARE)
768         {
769           if (nesting_depth-- == 0)
770             break;
771         }
772       /* Consume this token.  */
773       c_parser_consume_token (parser);
774     }
775   parser->error = false;
776 }
777
778 /* Skip tokens until the end of a parameter is found, but do not
779    consume the comma, semicolon or closing delimiter.  */
780
781 static void
782 c_parser_skip_to_end_of_parameter (c_parser *parser)
783 {
784   unsigned nesting_depth = 0;
785
786   while (true)
787     {
788       c_token *token = c_parser_peek_token (parser);
789       if ((token->type == CPP_COMMA || token->type == CPP_SEMICOLON)
790           && !nesting_depth)
791         break;
792       /* If we've run out of tokens, stop.  */
793       if (token->type == CPP_EOF)
794         return;
795       if (token->type == CPP_PRAGMA_EOL && parser->in_pragma)
796         return;
797       if (token->type == CPP_OPEN_BRACE
798           || token->type == CPP_OPEN_PAREN
799           || token->type == CPP_OPEN_SQUARE)
800         ++nesting_depth;
801       else if (token->type == CPP_CLOSE_BRACE
802                || token->type == CPP_CLOSE_PAREN
803                || token->type == CPP_CLOSE_SQUARE)
804         {
805           if (nesting_depth-- == 0)
806             break;
807         }
808       /* Consume this token.  */
809       c_parser_consume_token (parser);
810     }
811   parser->error = false;
812 }
813
814 /* Expect to be at the end of the pragma directive and consume an
815    end of line marker.  */
816
817 static void
818 c_parser_skip_to_pragma_eol (c_parser *parser)
819 {
820   gcc_assert (parser->in_pragma);
821   parser->in_pragma = false;
822
823   if (!c_parser_require (parser, CPP_PRAGMA_EOL, "expected end of line"))
824     while (true)
825       {
826         c_token *token = c_parser_peek_token (parser);
827         if (token->type == CPP_EOF)
828           break;
829         if (token->type == CPP_PRAGMA_EOL)
830           {
831             c_parser_consume_token (parser);
832             break;
833           }
834         c_parser_consume_token (parser);
835       }
836
837   parser->error = false;
838 }
839
840 /* Skip tokens until we have consumed an entire block, or until we
841    have consumed a non-nested ';'.  */
842
843 static void
844 c_parser_skip_to_end_of_block_or_statement (c_parser *parser)
845 {
846   unsigned nesting_depth = 0;
847   bool save_error = parser->error;
848
849   while (true)
850     {
851       c_token *token;
852
853       /* Peek at the next token.  */
854       token = c_parser_peek_token (parser);
855
856       switch (token->type)
857         {
858         case CPP_EOF:
859           return;
860
861         case CPP_PRAGMA_EOL:
862           if (parser->in_pragma)
863             return;
864           break;
865
866         case CPP_SEMICOLON:
867           /* If the next token is a ';', we have reached the
868              end of the statement.  */
869           if (!nesting_depth)
870             {
871               /* Consume the ';'.  */
872               c_parser_consume_token (parser);
873               goto finished;
874             }
875           break;
876
877         case CPP_CLOSE_BRACE:
878           /* If the next token is a non-nested '}', then we have
879              reached the end of the current block.  */
880           if (nesting_depth == 0 || --nesting_depth == 0)
881             {
882               c_parser_consume_token (parser);
883               goto finished;
884             }
885           break;
886
887         case CPP_OPEN_BRACE:
888           /* If it the next token is a '{', then we are entering a new
889              block.  Consume the entire block.  */
890           ++nesting_depth;
891           break;
892
893         case CPP_PRAGMA:
894           /* If we see a pragma, consume the whole thing at once.  We
895              have some safeguards against consuming pragmas willy-nilly.
896              Normally, we'd expect to be here with parser->error set,
897              which disables these safeguards.  But it's possible to get
898              here for secondary error recovery, after parser->error has
899              been cleared.  */
900           c_parser_consume_pragma (parser);
901           c_parser_skip_to_pragma_eol (parser);
902           parser->error = save_error;
903           continue;
904
905         default:
906           break;
907         }
908
909       c_parser_consume_token (parser);
910     }
911
912  finished:
913   parser->error = false;
914 }
915
916 /* CPP's options (initialized by c-opts.c).  */
917 extern cpp_options *cpp_opts;
918
919 /* Save the warning flags which are controlled by __extension__.  */
920
921 static inline int
922 disable_extension_diagnostics (void)
923 {
924   int ret = (pedantic
925              | (warn_pointer_arith << 1)
926              | (warn_traditional << 2)
927              | (flag_iso << 3)
928              | (warn_long_long << 4)
929              | (warn_cxx_compat << 5));
930   cpp_opts->cpp_pedantic = pedantic = 0;
931   warn_pointer_arith = 0;
932   cpp_opts->cpp_warn_traditional = warn_traditional = 0;
933   flag_iso = 0;
934   cpp_opts->cpp_warn_long_long = warn_long_long = 0;
935   warn_cxx_compat = 0;
936   return ret;
937 }
938
939 /* Restore the warning flags which are controlled by __extension__.
940    FLAGS is the return value from disable_extension_diagnostics.  */
941
942 static inline void
943 restore_extension_diagnostics (int flags)
944 {
945   cpp_opts->cpp_pedantic = pedantic = flags & 1;
946   warn_pointer_arith = (flags >> 1) & 1;
947   cpp_opts->cpp_warn_traditional = warn_traditional = (flags >> 2) & 1;
948   flag_iso = (flags >> 3) & 1;
949   cpp_opts->cpp_warn_long_long = warn_long_long = (flags >> 4) & 1;
950   warn_cxx_compat = (flags >> 5) & 1;
951 }
952
953 /* Possibly kinds of declarator to parse.  */
954 typedef enum c_dtr_syn {
955   /* A normal declarator with an identifier.  */
956   C_DTR_NORMAL,
957   /* An abstract declarator (maybe empty).  */
958   C_DTR_ABSTRACT,
959   /* A parameter declarator: may be either, but after a type name does
960      not redeclare a typedef name as an identifier if it can
961      alternatively be interpreted as a typedef name; see DR#009,
962      applied in C90 TC1, omitted from C99 and reapplied in C99 TC2
963      following DR#249.  For example, given a typedef T, "int T" and
964      "int *T" are valid parameter declarations redeclaring T, while
965      "int (T)" and "int * (T)" and "int (T[])" and "int (T (int))" are
966      abstract declarators rather than involving redundant parentheses;
967      the same applies with attributes inside the parentheses before
968      "T".  */
969   C_DTR_PARM
970 } c_dtr_syn;
971
972 static void c_parser_external_declaration (c_parser *);
973 static void c_parser_asm_definition (c_parser *);
974 static void c_parser_declaration_or_fndef (c_parser *, bool, bool, bool,
975                                            bool, bool, tree *);
976 static void c_parser_static_assert_declaration_no_semi (c_parser *);
977 static void c_parser_static_assert_declaration (c_parser *);
978 static void c_parser_declspecs (c_parser *, struct c_declspecs *, bool, bool,
979                                 bool);
980 static struct c_typespec c_parser_enum_specifier (c_parser *);
981 static struct c_typespec c_parser_struct_or_union_specifier (c_parser *);
982 static tree c_parser_struct_declaration (c_parser *);
983 static struct c_typespec c_parser_typeof_specifier (c_parser *);
984 static struct c_declarator *c_parser_declarator (c_parser *, bool, c_dtr_syn,
985                                                  bool *);
986 static struct c_declarator *c_parser_direct_declarator (c_parser *, bool,
987                                                         c_dtr_syn, bool *);
988 static struct c_declarator *c_parser_direct_declarator_inner (c_parser *,
989                                                               bool,
990                                                               struct c_declarator *);
991 static struct c_arg_info *c_parser_parms_declarator (c_parser *, bool, tree);
992 static struct c_arg_info *c_parser_parms_list_declarator (c_parser *, tree);
993 static struct c_parm *c_parser_parameter_declaration (c_parser *, tree);
994 static tree c_parser_simple_asm_expr (c_parser *);
995 static tree c_parser_attributes (c_parser *);
996 static struct c_type_name *c_parser_type_name (c_parser *);
997 static struct c_expr c_parser_initializer (c_parser *);
998 static struct c_expr c_parser_braced_init (c_parser *, tree, bool);
999 static void c_parser_initelt (c_parser *, struct obstack *);
1000 static void c_parser_initval (c_parser *, struct c_expr *,
1001                               struct obstack *);
1002 static tree c_parser_compound_statement (c_parser *);
1003 static void c_parser_compound_statement_nostart (c_parser *);
1004 static void c_parser_label (c_parser *);
1005 static void c_parser_statement (c_parser *);
1006 static void c_parser_statement_after_labels (c_parser *);
1007 static void c_parser_if_statement (c_parser *);
1008 static void c_parser_switch_statement (c_parser *);
1009 static void c_parser_while_statement (c_parser *);
1010 static void c_parser_do_statement (c_parser *);
1011 static void c_parser_for_statement (c_parser *);
1012 static tree c_parser_asm_statement (c_parser *);
1013 static tree c_parser_asm_operands (c_parser *, bool);
1014 static tree c_parser_asm_goto_operands (c_parser *);
1015 static tree c_parser_asm_clobbers (c_parser *);
1016 static struct c_expr c_parser_expr_no_commas (c_parser *, struct c_expr *);
1017 static struct c_expr c_parser_conditional_expression (c_parser *,
1018                                                       struct c_expr *);
1019 static struct c_expr c_parser_binary_expression (c_parser *, struct c_expr *);
1020 static struct c_expr c_parser_cast_expression (c_parser *, struct c_expr *);
1021 static struct c_expr c_parser_unary_expression (c_parser *);
1022 static struct c_expr c_parser_sizeof_expression (c_parser *);
1023 static struct c_expr c_parser_alignof_expression (c_parser *);
1024 static struct c_expr c_parser_postfix_expression (c_parser *);
1025 static struct c_expr c_parser_postfix_expression_after_paren_type (c_parser *,
1026                                                                    struct c_type_name *,
1027                                                                    location_t);
1028 static struct c_expr c_parser_postfix_expression_after_primary (c_parser *,
1029                                                                 location_t loc,
1030                                                                 struct c_expr);
1031 static struct c_expr c_parser_expression (c_parser *);
1032 static struct c_expr c_parser_expression_conv (c_parser *);
1033 static VEC(tree,gc) *c_parser_expr_list (c_parser *, bool, bool,
1034                                          VEC(tree,gc) **);
1035 static void c_parser_omp_construct (c_parser *);
1036 static void c_parser_omp_threadprivate (c_parser *);
1037 static void c_parser_omp_barrier (c_parser *);
1038 static void c_parser_omp_flush (c_parser *);
1039 static void c_parser_omp_taskwait (c_parser *);
1040
1041 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
1042 static bool c_parser_pragma (c_parser *, enum pragma_context);
1043
1044 /* These Objective-C parser functions are only ever called when
1045    compiling Objective-C.  */
1046 static void c_parser_objc_class_definition (c_parser *, tree);
1047 static void c_parser_objc_class_instance_variables (c_parser *);
1048 static void c_parser_objc_class_declaration (c_parser *);
1049 static void c_parser_objc_alias_declaration (c_parser *);
1050 static void c_parser_objc_protocol_definition (c_parser *, tree);
1051 static enum tree_code c_parser_objc_method_type (c_parser *);
1052 static void c_parser_objc_method_definition (c_parser *);
1053 static void c_parser_objc_methodprotolist (c_parser *);
1054 static void c_parser_objc_methodproto (c_parser *);
1055 static tree c_parser_objc_method_decl (c_parser *, tree *);
1056 static tree c_parser_objc_type_name (c_parser *);
1057 static tree c_parser_objc_protocol_refs (c_parser *);
1058 static void c_parser_objc_try_catch_statement (c_parser *);
1059 static void c_parser_objc_synchronized_statement (c_parser *);
1060 static tree c_parser_objc_selector (c_parser *);
1061 static tree c_parser_objc_selector_arg (c_parser *);
1062 static tree c_parser_objc_receiver (c_parser *);
1063 static tree c_parser_objc_message_args (c_parser *);
1064 static tree c_parser_objc_keywordexpr (c_parser *);
1065 static bool c_parser_objc_diagnose_bad_element_prefix 
1066   (c_parser *, struct c_declspecs *);
1067
1068 /* Parse a translation unit (C90 6.7, C99 6.9).
1069
1070    translation-unit:
1071      external-declarations
1072
1073    external-declarations:
1074      external-declaration
1075      external-declarations external-declaration
1076
1077    GNU extensions:
1078
1079    translation-unit:
1080      empty
1081 */
1082
1083 static void
1084 c_parser_translation_unit (c_parser *parser)
1085 {
1086   if (c_parser_next_token_is (parser, CPP_EOF))
1087     {
1088       pedwarn (c_parser_peek_token (parser)->location, OPT_pedantic,
1089                "ISO C forbids an empty translation unit");
1090     }
1091   else
1092     {
1093       void *obstack_position = obstack_alloc (&parser_obstack, 0);
1094       mark_valid_location_for_stdc_pragma (false);
1095       do
1096         {
1097           ggc_collect ();
1098           c_parser_external_declaration (parser);
1099           obstack_free (&parser_obstack, obstack_position);
1100         }
1101       while (c_parser_next_token_is_not (parser, CPP_EOF));
1102     }
1103 }
1104
1105 /* Parse an external declaration (C90 6.7, C99 6.9).
1106
1107    external-declaration:
1108      function-definition
1109      declaration
1110
1111    GNU extensions:
1112
1113    external-declaration:
1114      asm-definition
1115      ;
1116      __extension__ external-declaration
1117
1118    Objective-C:
1119
1120    external-declaration:
1121      objc-class-definition
1122      objc-class-declaration
1123      objc-alias-declaration
1124      objc-protocol-definition
1125      objc-method-definition
1126      @end
1127 */
1128
1129 static void
1130 c_parser_external_declaration (c_parser *parser)
1131 {
1132   int ext;
1133   switch (c_parser_peek_token (parser)->type)
1134     {
1135     case CPP_KEYWORD:
1136       switch (c_parser_peek_token (parser)->keyword)
1137         {
1138         case RID_EXTENSION:
1139           ext = disable_extension_diagnostics ();
1140           c_parser_consume_token (parser);
1141           c_parser_external_declaration (parser);
1142           restore_extension_diagnostics (ext);
1143           break;
1144         case RID_ASM:
1145           c_parser_asm_definition (parser);
1146           break;
1147         case RID_AT_INTERFACE:
1148         case RID_AT_IMPLEMENTATION:
1149           gcc_assert (c_dialect_objc ());
1150           c_parser_objc_class_definition (parser, NULL_TREE);
1151           break;
1152         case RID_AT_CLASS:
1153           gcc_assert (c_dialect_objc ());
1154           c_parser_objc_class_declaration (parser);
1155           break;
1156         case RID_AT_ALIAS:
1157           gcc_assert (c_dialect_objc ());
1158           c_parser_objc_alias_declaration (parser);
1159           break;
1160         case RID_AT_PROTOCOL:
1161           gcc_assert (c_dialect_objc ());
1162           c_parser_objc_protocol_definition (parser, NULL_TREE);
1163           break;
1164         case RID_AT_END:
1165           gcc_assert (c_dialect_objc ());
1166           c_parser_consume_token (parser);
1167           objc_finish_implementation ();
1168           break;
1169         default:
1170           goto decl_or_fndef;
1171         }
1172       break;
1173     case CPP_SEMICOLON:
1174       pedwarn (c_parser_peek_token (parser)->location, OPT_pedantic,
1175                "ISO C does not allow extra %<;%> outside of a function");
1176       c_parser_consume_token (parser);
1177       break;
1178     case CPP_PRAGMA:
1179       mark_valid_location_for_stdc_pragma (true);
1180       c_parser_pragma (parser, pragma_external);
1181       mark_valid_location_for_stdc_pragma (false);
1182       break;
1183     case CPP_PLUS:
1184     case CPP_MINUS:
1185       if (c_dialect_objc ())
1186         {
1187           c_parser_objc_method_definition (parser);
1188           break;
1189         }
1190       /* Else fall through, and yield a syntax error trying to parse
1191          as a declaration or function definition.  */
1192     default:
1193     decl_or_fndef:
1194       /* A declaration or a function definition (or, in Objective-C,
1195          an @interface or @protocol with prefix attributes).  We can
1196          only tell which after parsing the declaration specifiers, if
1197          any, and the first declarator.  */
1198       c_parser_declaration_or_fndef (parser, true, true, true, false, true, NULL);
1199       break;
1200     }
1201 }
1202
1203 /* Parse a declaration or function definition (C90 6.5, 6.7.1, C99
1204    6.7, 6.9.1).  If FNDEF_OK is true, a function definition is
1205    accepted; otherwise (old-style parameter declarations) only other
1206    declarations are accepted.  If STATIC_ASSERT_OK is true, a static
1207    assertion is accepted; otherwise (old-style parameter declarations)
1208    it is not.  If NESTED is true, we are inside a function or parsing
1209    old-style parameter declarations; any functions encountered are
1210    nested functions and declaration specifiers are required; otherwise
1211    we are at top level and functions are normal functions and
1212    declaration specifiers may be optional.  If EMPTY_OK is true, empty
1213    declarations are OK (subject to all other constraints); otherwise
1214    (old-style parameter declarations) they are diagnosed.  If
1215    START_ATTR_OK is true, the declaration specifiers may start with
1216    attributes; otherwise they may not.
1217    OBJC_FOREACH_OBJECT_DECLARATION can be used to get back the parsed
1218    declaration when parsing an Objective-C foreach statement.
1219
1220    declaration:
1221      declaration-specifiers init-declarator-list[opt] ;
1222      static_assert-declaration
1223
1224    function-definition:
1225      declaration-specifiers[opt] declarator declaration-list[opt]
1226        compound-statement
1227
1228    declaration-list:
1229      declaration
1230      declaration-list declaration
1231
1232    init-declarator-list:
1233      init-declarator
1234      init-declarator-list , init-declarator
1235
1236    init-declarator:
1237      declarator simple-asm-expr[opt] attributes[opt]
1238      declarator simple-asm-expr[opt] attributes[opt] = initializer
1239
1240    GNU extensions:
1241
1242    nested-function-definition:
1243      declaration-specifiers declarator declaration-list[opt]
1244        compound-statement
1245
1246    Objective-C:
1247      attributes objc-class-definition
1248      attributes objc-category-definition
1249      attributes objc-protocol-definition
1250
1251    The simple-asm-expr and attributes are GNU extensions.
1252
1253    This function does not handle __extension__; that is handled in its
1254    callers.  ??? Following the old parser, __extension__ may start
1255    external declarations, declarations in functions and declarations
1256    at the start of "for" loops, but not old-style parameter
1257    declarations.
1258
1259    C99 requires declaration specifiers in a function definition; the
1260    absence is diagnosed through the diagnosis of implicit int.  In GNU
1261    C we also allow but diagnose declarations without declaration
1262    specifiers, but only at top level (elsewhere they conflict with
1263    other syntax).
1264
1265    In Objective-C, declarations of the looping variable in a foreach
1266    statement are exceptionally terminated by 'in' (for example, 'for
1267    (NSObject *object in array) { ... }').
1268
1269    OpenMP:
1270
1271    declaration:
1272      threadprivate-directive  */
1273
1274 static void
1275 c_parser_declaration_or_fndef (c_parser *parser, bool fndef_ok,
1276                                bool static_assert_ok, bool empty_ok,
1277                                bool nested, bool start_attr_ok,
1278                                tree *objc_foreach_object_declaration)
1279 {
1280   struct c_declspecs *specs;
1281   tree prefix_attrs;
1282   tree all_prefix_attrs;
1283   bool diagnosed_no_specs = false;
1284   location_t here = c_parser_peek_token (parser)->location;
1285
1286   if (static_assert_ok
1287       && c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT))
1288     {
1289       c_parser_static_assert_declaration (parser);
1290       return;
1291     }
1292   specs = build_null_declspecs ();
1293   c_parser_declspecs (parser, specs, true, true, start_attr_ok);
1294   if (parser->error)
1295     {
1296       c_parser_skip_to_end_of_block_or_statement (parser);
1297       return;
1298     }
1299   if (nested && !specs->declspecs_seen_p)
1300     {
1301       c_parser_error (parser, "expected declaration specifiers");
1302       c_parser_skip_to_end_of_block_or_statement (parser);
1303       return;
1304     }
1305   finish_declspecs (specs);
1306   if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1307     {
1308       if (empty_ok)
1309         shadow_tag (specs);
1310       else
1311         {
1312           shadow_tag_warned (specs, 1);
1313           pedwarn (here, 0, "empty declaration");
1314         }
1315       c_parser_consume_token (parser);
1316       return;
1317     }
1318   else if (c_dialect_objc ())
1319     {
1320       /* Prefix attributes are an error on method decls.  */
1321       switch (c_parser_peek_token (parser)->type)
1322         {
1323           case CPP_PLUS:
1324           case CPP_MINUS:
1325             if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1326               return;
1327             if (specs->attrs)
1328               {
1329                 warning_at (c_parser_peek_token (parser)->location, 
1330                             OPT_Wattributes,
1331                             "prefix attributes are ignored for methods");
1332                 specs->attrs = NULL_TREE;
1333               }
1334             if (fndef_ok)
1335               c_parser_objc_method_definition (parser);
1336             else
1337               c_parser_objc_methodproto (parser);
1338             return;
1339             break;
1340           default:
1341             break;
1342         }
1343       /* This is where we parse 'attributes @interface ...',
1344          'attributes @implementation ...', 'attributes @protocol ...'
1345          (where attributes could be, for example, __attribute__
1346          ((deprecated)).
1347       */
1348       switch (c_parser_peek_token (parser)->keyword)
1349         {
1350         case RID_AT_INTERFACE:
1351           {
1352             if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1353               return;
1354             c_parser_objc_class_definition (parser, specs->attrs);
1355             return;
1356           }
1357           break;
1358         case RID_AT_IMPLEMENTATION:
1359           {
1360             if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1361               return;
1362             if (specs->attrs)
1363               {
1364                 warning_at (c_parser_peek_token (parser)->location, 
1365                         OPT_Wattributes,
1366                         "prefix attributes are ignored for implementations");
1367                 specs->attrs = NULL_TREE;
1368               }
1369             c_parser_objc_class_definition (parser, NULL_TREE);     
1370             return;
1371           }
1372           break;
1373         case RID_AT_PROTOCOL:
1374           {
1375             if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1376               return;
1377             c_parser_objc_protocol_definition (parser, specs->attrs);
1378             return;
1379           }
1380           break;
1381         default:
1382           break;
1383         }
1384     }
1385   
1386   pending_xref_error ();
1387   prefix_attrs = specs->attrs;
1388   all_prefix_attrs = prefix_attrs;
1389   specs->attrs = NULL_TREE;
1390   while (true)
1391     {
1392       struct c_declarator *declarator;
1393       bool dummy = false;
1394       tree fnbody;
1395       /* Declaring either one or more declarators (in which case we
1396          should diagnose if there were no declaration specifiers) or a
1397          function definition (in which case the diagnostic for
1398          implicit int suffices).  */
1399       declarator = c_parser_declarator (parser, specs->type_seen_p,
1400                                         C_DTR_NORMAL, &dummy);
1401       if (declarator == NULL)
1402         {
1403           c_parser_skip_to_end_of_block_or_statement (parser);
1404           return;
1405         }
1406       if (c_parser_next_token_is (parser, CPP_EQ)
1407           || c_parser_next_token_is (parser, CPP_COMMA)
1408           || c_parser_next_token_is (parser, CPP_SEMICOLON)
1409           || c_parser_next_token_is_keyword (parser, RID_ASM)
1410           || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE)
1411           || c_parser_next_token_is_keyword (parser, RID_IN))
1412         {
1413           tree asm_name = NULL_TREE;
1414           tree postfix_attrs = NULL_TREE;
1415           if (!diagnosed_no_specs && !specs->declspecs_seen_p)
1416             {
1417               diagnosed_no_specs = true;
1418               pedwarn (here, 0, "data definition has no type or storage class");
1419             }
1420           /* Having seen a data definition, there cannot now be a
1421              function definition.  */
1422           fndef_ok = false;
1423           if (c_parser_next_token_is_keyword (parser, RID_ASM))
1424             asm_name = c_parser_simple_asm_expr (parser);
1425           if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1426             postfix_attrs = c_parser_attributes (parser);
1427           if (c_parser_next_token_is (parser, CPP_EQ))
1428             {
1429               tree d;
1430               struct c_expr init;
1431               location_t init_loc;
1432               c_parser_consume_token (parser);
1433               /* The declaration of the variable is in effect while
1434                  its initializer is parsed.  */
1435               d = start_decl (declarator, specs, true,
1436                               chainon (postfix_attrs, all_prefix_attrs));
1437               if (!d)
1438                 d = error_mark_node;
1439               start_init (d, asm_name, global_bindings_p ());
1440               init_loc = c_parser_peek_token (parser)->location;
1441               init = c_parser_initializer (parser);
1442               finish_init ();
1443               if (d != error_mark_node)
1444                 {
1445                   maybe_warn_string_init (TREE_TYPE (d), init);
1446                   finish_decl (d, init_loc, init.value,
1447                                init.original_type, asm_name);
1448                 }
1449             }
1450           else
1451             {
1452               tree d = start_decl (declarator, specs, false,
1453                                    chainon (postfix_attrs,
1454                                             all_prefix_attrs));
1455               if (d)
1456                 finish_decl (d, UNKNOWN_LOCATION, NULL_TREE,
1457                              NULL_TREE, asm_name);
1458               
1459               if (c_parser_next_token_is_keyword (parser, RID_IN))
1460                 {
1461                   if (d)
1462                     *objc_foreach_object_declaration = d;
1463                   else
1464                     *objc_foreach_object_declaration = error_mark_node;             
1465                 }
1466             }
1467           if (c_parser_next_token_is (parser, CPP_COMMA))
1468             {
1469               c_parser_consume_token (parser);
1470               if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1471                 all_prefix_attrs = chainon (c_parser_attributes (parser),
1472                                             prefix_attrs);
1473               else
1474                 all_prefix_attrs = prefix_attrs;
1475               continue;
1476             }
1477           else if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1478             {
1479               c_parser_consume_token (parser);
1480               return;
1481             }
1482           else if (c_parser_next_token_is_keyword (parser, RID_IN))
1483             {
1484               /* This can only happen in Objective-C: we found the
1485                  'in' that terminates the declaration inside an
1486                  Objective-C foreach statement.  Do not consume the
1487                  token, so that the caller can use it to determine
1488                  that this indeed is a foreach context.  */
1489               return;
1490             }
1491           else
1492             {
1493               c_parser_error (parser, "expected %<,%> or %<;%>");
1494               c_parser_skip_to_end_of_block_or_statement (parser);
1495               return;
1496             }
1497         }
1498       else if (!fndef_ok)
1499         {
1500           c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, "
1501                           "%<asm%> or %<__attribute__%>");
1502           c_parser_skip_to_end_of_block_or_statement (parser);
1503           return;
1504         }
1505       /* Function definition (nested or otherwise).  */
1506       if (nested)
1507         {
1508           pedwarn (here, OPT_pedantic, "ISO C forbids nested functions");
1509           c_push_function_context ();
1510         }
1511       if (!start_function (specs, declarator, all_prefix_attrs))
1512         {
1513           /* This can appear in many cases looking nothing like a
1514              function definition, so we don't give a more specific
1515              error suggesting there was one.  */
1516           c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, %<asm%> "
1517                           "or %<__attribute__%>");
1518           if (nested)
1519             c_pop_function_context ();
1520           break;
1521         }
1522       /* Parse old-style parameter declarations.  ??? Attributes are
1523          not allowed to start declaration specifiers here because of a
1524          syntax conflict between a function declaration with attribute
1525          suffix and a function definition with an attribute prefix on
1526          first old-style parameter declaration.  Following the old
1527          parser, they are not accepted on subsequent old-style
1528          parameter declarations either.  However, there is no
1529          ambiguity after the first declaration, nor indeed on the
1530          first as long as we don't allow postfix attributes after a
1531          declarator with a nonempty identifier list in a definition;
1532          and postfix attributes have never been accepted here in
1533          function definitions either.  */
1534       while (c_parser_next_token_is_not (parser, CPP_EOF)
1535              && c_parser_next_token_is_not (parser, CPP_OPEN_BRACE))
1536         c_parser_declaration_or_fndef (parser, false, false, false,
1537                                        true, false, NULL);
1538       store_parm_decls ();
1539       DECL_STRUCT_FUNCTION (current_function_decl)->function_start_locus
1540         = c_parser_peek_token (parser)->location;
1541       fnbody = c_parser_compound_statement (parser);
1542       if (nested)
1543         {
1544           tree decl = current_function_decl;
1545           /* Mark nested functions as needing static-chain initially.
1546              lower_nested_functions will recompute it but the
1547              DECL_STATIC_CHAIN flag is also used before that happens,
1548              by initializer_constant_valid_p.  See gcc.dg/nested-fn-2.c.  */
1549           DECL_STATIC_CHAIN (decl) = 1;
1550           add_stmt (fnbody);
1551           finish_function ();
1552           c_pop_function_context ();
1553           add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl));
1554         }
1555       else
1556         {
1557           add_stmt (fnbody);
1558           finish_function ();
1559         }
1560       break;
1561     }
1562 }
1563
1564 /* Parse an asm-definition (asm() outside a function body).  This is a
1565    GNU extension.
1566
1567    asm-definition:
1568      simple-asm-expr ;
1569 */
1570
1571 static void
1572 c_parser_asm_definition (c_parser *parser)
1573 {
1574   tree asm_str = c_parser_simple_asm_expr (parser);
1575   if (asm_str)
1576     cgraph_add_asm_node (asm_str);
1577   c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
1578 }
1579
1580 /* Parse a static assertion (C1X N1425 6.7.10).
1581
1582    static_assert-declaration:
1583      static_assert-declaration-no-semi ;
1584 */
1585
1586 static void
1587 c_parser_static_assert_declaration (c_parser *parser)
1588 {
1589   c_parser_static_assert_declaration_no_semi (parser);
1590   if (parser->error
1591       || !c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
1592     c_parser_skip_to_end_of_block_or_statement (parser);
1593 }
1594
1595 /* Parse a static assertion (C1X N1425 6.7.10), without the trailing
1596    semicolon.
1597
1598    static_assert-declaration-no-semi:
1599      _Static_assert ( constant-expression , string-literal )
1600 */
1601
1602 static void
1603 c_parser_static_assert_declaration_no_semi (c_parser *parser)
1604 {
1605   location_t assert_loc, value_loc;
1606   tree value;
1607   tree string;
1608
1609   gcc_assert (c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT));
1610   assert_loc = c_parser_peek_token (parser)->location;
1611   if (!flag_isoc1x)
1612     {
1613       if (flag_isoc99)
1614         pedwarn (assert_loc, OPT_pedantic,
1615                  "ISO C99 does not support %<_Static_assert%>");
1616       else
1617         pedwarn (assert_loc, OPT_pedantic,
1618                  "ISO C90 does not support %<_Static_assert%>");
1619     }
1620   c_parser_consume_token (parser);
1621   if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
1622     return;
1623   value_loc = c_parser_peek_token (parser)->location;
1624   value = c_parser_expr_no_commas (parser, NULL).value;
1625   parser->lex_untranslated_string = true;
1626   if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
1627     {
1628       parser->lex_untranslated_string = false;
1629       return;
1630     }
1631   switch (c_parser_peek_token (parser)->type)
1632     {
1633     case CPP_STRING:
1634     case CPP_STRING16:
1635     case CPP_STRING32:
1636     case CPP_WSTRING:
1637     case CPP_UTF8STRING:
1638       string = c_parser_peek_token (parser)->value;
1639       c_parser_consume_token (parser);
1640       parser->lex_untranslated_string = false;
1641       break;
1642     default:
1643       c_parser_error (parser, "expected string literal");
1644       parser->lex_untranslated_string = false;
1645       return;
1646     }
1647   c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
1648
1649   if (!INTEGRAL_TYPE_P (TREE_TYPE (value)))
1650     {
1651       error_at (value_loc, "expression in static assertion is not an integer");
1652       return;
1653     }
1654   if (TREE_CODE (value) != INTEGER_CST)
1655     {
1656       value = c_fully_fold (value, false, NULL);
1657       if (TREE_CODE (value) == INTEGER_CST)
1658         pedwarn (value_loc, OPT_pedantic, "expression in static assertion "
1659                  "is not an integer constant expression");
1660     }
1661   if (TREE_CODE (value) != INTEGER_CST)
1662     {
1663       error_at (value_loc, "expression in static assertion is not constant");
1664       return;
1665     }
1666   constant_expression_warning (value);
1667   if (integer_zerop (value))
1668     error_at (assert_loc, "static assertion failed: %E", string);
1669 }
1670
1671 /* Parse some declaration specifiers (possibly none) (C90 6.5, C99
1672    6.7), adding them to SPECS (which may already include some).
1673    Storage class specifiers are accepted iff SCSPEC_OK; type
1674    specifiers are accepted iff TYPESPEC_OK; attributes are accepted at
1675    the start iff START_ATTR_OK.
1676
1677    declaration-specifiers:
1678      storage-class-specifier declaration-specifiers[opt]
1679      type-specifier declaration-specifiers[opt]
1680      type-qualifier declaration-specifiers[opt]
1681      function-specifier declaration-specifiers[opt]
1682
1683    Function specifiers (inline) are from C99, and are currently
1684    handled as storage class specifiers, as is __thread.
1685
1686    C90 6.5.1, C99 6.7.1:
1687    storage-class-specifier:
1688      typedef
1689      extern
1690      static
1691      auto
1692      register
1693
1694    C99 6.7.4:
1695    function-specifier:
1696      inline
1697
1698    C90 6.5.2, C99 6.7.2:
1699    type-specifier:
1700      void
1701      char
1702      short
1703      int
1704      long
1705      float
1706      double
1707      signed
1708      unsigned
1709      _Bool
1710      _Complex
1711      [_Imaginary removed in C99 TC2]
1712      struct-or-union-specifier
1713      enum-specifier
1714      typedef-name
1715
1716    (_Bool and _Complex are new in C99.)
1717
1718    C90 6.5.3, C99 6.7.3:
1719
1720    type-qualifier:
1721      const
1722      restrict
1723      volatile
1724      address-space-qualifier
1725
1726    (restrict is new in C99.)
1727
1728    GNU extensions:
1729
1730    declaration-specifiers:
1731      attributes declaration-specifiers[opt]
1732
1733    type-qualifier:
1734      address-space
1735
1736    address-space:
1737      identifier recognized by the target
1738
1739    storage-class-specifier:
1740      __thread
1741
1742    type-specifier:
1743      typeof-specifier
1744      __int128
1745      _Decimal32
1746      _Decimal64
1747      _Decimal128
1748      _Fract
1749      _Accum
1750      _Sat
1751
1752   (_Fract, _Accum, and _Sat are new from ISO/IEC DTR 18037:
1753    http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1169.pdf)
1754
1755    Objective-C:
1756
1757    type-specifier:
1758      class-name objc-protocol-refs[opt]
1759      typedef-name objc-protocol-refs
1760      objc-protocol-refs
1761 */
1762
1763 static void
1764 c_parser_declspecs (c_parser *parser, struct c_declspecs *specs,
1765                     bool scspec_ok, bool typespec_ok, bool start_attr_ok)
1766 {
1767   bool attrs_ok = start_attr_ok;
1768   bool seen_type = specs->type_seen_p;
1769   while (c_parser_next_token_is (parser, CPP_NAME)
1770          || c_parser_next_token_is (parser, CPP_KEYWORD)
1771          || (c_dialect_objc () && c_parser_next_token_is (parser, CPP_LESS)))
1772     {
1773       struct c_typespec t;
1774       tree attrs;
1775       location_t loc = c_parser_peek_token (parser)->location;
1776       if (c_parser_next_token_is (parser, CPP_NAME))
1777         {
1778           tree value = c_parser_peek_token (parser)->value;
1779           c_id_kind kind = c_parser_peek_token (parser)->id_kind;
1780
1781           if (kind == C_ID_ADDRSPACE)
1782             {
1783               addr_space_t as
1784                 = c_parser_peek_token (parser)->keyword - RID_FIRST_ADDR_SPACE;
1785               declspecs_add_addrspace (specs, as);
1786               c_parser_consume_token (parser);
1787               attrs_ok = true;
1788               continue;
1789             }
1790
1791           /* This finishes the specifiers unless a type name is OK, it
1792              is declared as a type name and a type name hasn't yet
1793              been seen.  */
1794           if (!typespec_ok || seen_type
1795               || (kind != C_ID_TYPENAME && kind != C_ID_CLASSNAME))
1796             break;
1797           c_parser_consume_token (parser);
1798           seen_type = true;
1799           attrs_ok = true;
1800           if (kind == C_ID_TYPENAME
1801               && (!c_dialect_objc ()
1802                   || c_parser_next_token_is_not (parser, CPP_LESS)))
1803             {
1804               t.kind = ctsk_typedef;
1805               /* For a typedef name, record the meaning, not the name.
1806                  In case of 'foo foo, bar;'.  */
1807               t.spec = lookup_name (value);
1808               t.expr = NULL_TREE;
1809               t.expr_const_operands = true;
1810             }
1811           else
1812             {
1813               tree proto = NULL_TREE;
1814               gcc_assert (c_dialect_objc ());
1815               t.kind = ctsk_objc;
1816               if (c_parser_next_token_is (parser, CPP_LESS))
1817                 proto = c_parser_objc_protocol_refs (parser);
1818               t.spec = objc_get_protocol_qualified_type (value, proto);
1819               t.expr = NULL_TREE;
1820               t.expr_const_operands = true;
1821             }
1822           declspecs_add_type (loc, specs, t);
1823           continue;
1824         }
1825       if (c_parser_next_token_is (parser, CPP_LESS))
1826         {
1827           /* Make "<SomeProtocol>" equivalent to "id <SomeProtocol>" -
1828              nisse@lysator.liu.se.  */
1829           tree proto;
1830           gcc_assert (c_dialect_objc ());
1831           if (!typespec_ok || seen_type)
1832             break;
1833           proto = c_parser_objc_protocol_refs (parser);
1834           t.kind = ctsk_objc;
1835           t.spec = objc_get_protocol_qualified_type (NULL_TREE, proto);
1836           t.expr = NULL_TREE;
1837           t.expr_const_operands = true;
1838           declspecs_add_type (loc, specs, t);
1839           continue;
1840         }
1841       gcc_assert (c_parser_next_token_is (parser, CPP_KEYWORD));
1842       switch (c_parser_peek_token (parser)->keyword)
1843         {
1844         case RID_STATIC:
1845         case RID_EXTERN:
1846         case RID_REGISTER:
1847         case RID_TYPEDEF:
1848         case RID_INLINE:
1849         case RID_AUTO:
1850         case RID_THREAD:
1851           if (!scspec_ok)
1852             goto out;
1853           attrs_ok = true;
1854           /* TODO: Distinguish between function specifiers (inline)
1855              and storage class specifiers, either here or in
1856              declspecs_add_scspec.  */
1857           declspecs_add_scspec (specs, c_parser_peek_token (parser)->value);
1858           c_parser_consume_token (parser);
1859           break;
1860         case RID_UNSIGNED:
1861         case RID_LONG:
1862         case RID_INT128:
1863         case RID_SHORT:
1864         case RID_SIGNED:
1865         case RID_COMPLEX:
1866         case RID_INT:
1867         case RID_CHAR:
1868         case RID_FLOAT:
1869         case RID_DOUBLE:
1870         case RID_VOID:
1871         case RID_DFLOAT32:
1872         case RID_DFLOAT64:
1873         case RID_DFLOAT128:
1874         case RID_BOOL:
1875         case RID_FRACT:
1876         case RID_ACCUM:
1877         case RID_SAT:
1878           if (!typespec_ok)
1879             goto out;
1880           attrs_ok = true;
1881           seen_type = true;
1882           if (c_dialect_objc ())
1883             parser->objc_need_raw_identifier = true;
1884           t.kind = ctsk_resword;
1885           t.spec = c_parser_peek_token (parser)->value;
1886           t.expr = NULL_TREE;
1887           t.expr_const_operands = true;
1888           declspecs_add_type (loc, specs, t);
1889           c_parser_consume_token (parser);
1890           break;
1891         case RID_ENUM:
1892           if (!typespec_ok)
1893             goto out;
1894           attrs_ok = true;
1895           seen_type = true;
1896           t = c_parser_enum_specifier (parser);
1897           declspecs_add_type (loc, specs, t);
1898           break;
1899         case RID_STRUCT:
1900         case RID_UNION:
1901           if (!typespec_ok)
1902             goto out;
1903           attrs_ok = true;
1904           seen_type = true;
1905           t = c_parser_struct_or_union_specifier (parser);
1906           invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, t.spec);
1907           declspecs_add_type (loc, specs, t);
1908           break;
1909         case RID_TYPEOF:
1910           /* ??? The old parser rejected typeof after other type
1911              specifiers, but is a syntax error the best way of
1912              handling this?  */
1913           if (!typespec_ok || seen_type)
1914             goto out;
1915           attrs_ok = true;
1916           seen_type = true;
1917           t = c_parser_typeof_specifier (parser);
1918           declspecs_add_type (loc, specs, t);
1919           break;
1920         case RID_CONST:
1921         case RID_VOLATILE:
1922         case RID_RESTRICT:
1923           attrs_ok = true;
1924           declspecs_add_qual (specs, c_parser_peek_token (parser)->value);
1925           c_parser_consume_token (parser);
1926           break;
1927         case RID_ATTRIBUTE:
1928           if (!attrs_ok)
1929             goto out;
1930           attrs = c_parser_attributes (parser);
1931           declspecs_add_attrs (specs, attrs);
1932           break;
1933         default:
1934           goto out;
1935         }
1936     }
1937  out: ;
1938 }
1939
1940 /* Parse an enum specifier (C90 6.5.2.2, C99 6.7.2.2).
1941
1942    enum-specifier:
1943      enum attributes[opt] identifier[opt] { enumerator-list } attributes[opt]
1944      enum attributes[opt] identifier[opt] { enumerator-list , } attributes[opt]
1945      enum attributes[opt] identifier
1946
1947    The form with trailing comma is new in C99.  The forms with
1948    attributes are GNU extensions.  In GNU C, we accept any expression
1949    without commas in the syntax (assignment expressions, not just
1950    conditional expressions); assignment expressions will be diagnosed
1951    as non-constant.
1952
1953    enumerator-list:
1954      enumerator
1955      enumerator-list , enumerator
1956
1957    enumerator:
1958      enumeration-constant
1959      enumeration-constant = constant-expression
1960 */
1961
1962 static struct c_typespec
1963 c_parser_enum_specifier (c_parser *parser)
1964 {
1965   struct c_typespec ret;
1966   tree attrs;
1967   tree ident = NULL_TREE;
1968   location_t enum_loc;
1969   location_t ident_loc = UNKNOWN_LOCATION;  /* Quiet warning.  */
1970   gcc_assert (c_parser_next_token_is_keyword (parser, RID_ENUM));
1971   enum_loc = c_parser_peek_token (parser)->location;
1972   c_parser_consume_token (parser);
1973   attrs = c_parser_attributes (parser);
1974   enum_loc = c_parser_peek_token (parser)->location;
1975   /* Set the location in case we create a decl now.  */
1976   c_parser_set_source_position_from_token (c_parser_peek_token (parser));
1977   if (c_parser_next_token_is (parser, CPP_NAME))
1978     {
1979       ident = c_parser_peek_token (parser)->value;
1980       ident_loc = c_parser_peek_token (parser)->location;
1981       enum_loc = ident_loc;
1982       c_parser_consume_token (parser);
1983     }
1984   if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
1985     {
1986       /* Parse an enum definition.  */
1987       struct c_enum_contents the_enum;
1988       tree type = start_enum (enum_loc, &the_enum, ident);
1989       tree postfix_attrs;
1990       /* We chain the enumerators in reverse order, then put them in
1991          forward order at the end.  */
1992       tree values = NULL_TREE;
1993       c_parser_consume_token (parser);
1994       while (true)
1995         {
1996           tree enum_id;
1997           tree enum_value;
1998           tree enum_decl;
1999           bool seen_comma;
2000           c_token *token;
2001           location_t comma_loc = UNKNOWN_LOCATION;  /* Quiet warning.  */
2002           location_t decl_loc, value_loc;
2003           if (c_parser_next_token_is_not (parser, CPP_NAME))
2004             {
2005               c_parser_error (parser, "expected identifier");
2006               c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
2007               values = error_mark_node;
2008               break;
2009             }
2010           token = c_parser_peek_token (parser);
2011           enum_id = token->value;
2012           /* Set the location in case we create a decl now.  */
2013           c_parser_set_source_position_from_token (token);
2014           decl_loc = value_loc = token->location;
2015           c_parser_consume_token (parser);
2016           if (c_parser_next_token_is (parser, CPP_EQ))
2017             {
2018               c_parser_consume_token (parser);
2019               value_loc = c_parser_peek_token (parser)->location;
2020               enum_value = c_parser_expr_no_commas (parser, NULL).value;
2021             }
2022           else
2023             enum_value = NULL_TREE;
2024           enum_decl = build_enumerator (decl_loc, value_loc,
2025                                         &the_enum, enum_id, enum_value);
2026           TREE_CHAIN (enum_decl) = values;
2027           values = enum_decl;
2028           seen_comma = false;
2029           if (c_parser_next_token_is (parser, CPP_COMMA))
2030             {
2031               comma_loc = c_parser_peek_token (parser)->location;
2032               seen_comma = true;
2033               c_parser_consume_token (parser);
2034             }
2035           if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2036             {
2037               if (seen_comma && !flag_isoc99)
2038                 pedwarn (comma_loc, OPT_pedantic, "comma at end of enumerator list");
2039               c_parser_consume_token (parser);
2040               break;
2041             }
2042           if (!seen_comma)
2043             {
2044               c_parser_error (parser, "expected %<,%> or %<}%>");
2045               c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
2046               values = error_mark_node;
2047               break;
2048             }
2049         }
2050       postfix_attrs = c_parser_attributes (parser);
2051       ret.spec = finish_enum (type, nreverse (values),
2052                               chainon (attrs, postfix_attrs));
2053       ret.kind = ctsk_tagdef;
2054       ret.expr = NULL_TREE;
2055       ret.expr_const_operands = true;
2056       return ret;
2057     }
2058   else if (!ident)
2059     {
2060       c_parser_error (parser, "expected %<{%>");
2061       ret.spec = error_mark_node;
2062       ret.kind = ctsk_tagref;
2063       ret.expr = NULL_TREE;
2064       ret.expr_const_operands = true;
2065       return ret;
2066     }
2067   ret = parser_xref_tag (ident_loc, ENUMERAL_TYPE, ident);
2068   /* In ISO C, enumerated types can be referred to only if already
2069      defined.  */
2070   if (pedantic && !COMPLETE_TYPE_P (ret.spec))
2071     {
2072       gcc_assert (ident);
2073       pedwarn (enum_loc, OPT_pedantic,
2074                "ISO C forbids forward references to %<enum%> types");
2075     }
2076   return ret;
2077 }
2078
2079 /* Parse a struct or union specifier (C90 6.5.2.1, C99 6.7.2.1).
2080
2081    struct-or-union-specifier:
2082      struct-or-union attributes[opt] identifier[opt]
2083        { struct-contents } attributes[opt]
2084      struct-or-union attributes[opt] identifier
2085
2086    struct-contents:
2087      struct-declaration-list
2088
2089    struct-declaration-list:
2090      struct-declaration ;
2091      struct-declaration-list struct-declaration ;
2092
2093    GNU extensions:
2094
2095    struct-contents:
2096      empty
2097      struct-declaration
2098      struct-declaration-list struct-declaration
2099
2100    struct-declaration-list:
2101      struct-declaration-list ;
2102      ;
2103
2104    (Note that in the syntax here, unlike that in ISO C, the semicolons
2105    are included here rather than in struct-declaration, in order to
2106    describe the syntax with extra semicolons and missing semicolon at
2107    end.)
2108
2109    Objective-C:
2110
2111    struct-declaration-list:
2112      @defs ( class-name )
2113
2114    (Note this does not include a trailing semicolon, but can be
2115    followed by further declarations, and gets a pedwarn-if-pedantic
2116    when followed by a semicolon.)  */
2117
2118 static struct c_typespec
2119 c_parser_struct_or_union_specifier (c_parser *parser)
2120 {
2121   struct c_typespec ret;
2122   tree attrs;
2123   tree ident = NULL_TREE;
2124   location_t struct_loc;
2125   location_t ident_loc = UNKNOWN_LOCATION;
2126   enum tree_code code;
2127   switch (c_parser_peek_token (parser)->keyword)
2128     {
2129     case RID_STRUCT:
2130       code = RECORD_TYPE;
2131       break;
2132     case RID_UNION:
2133       code = UNION_TYPE;
2134       break;
2135     default:
2136       gcc_unreachable ();
2137     }
2138   struct_loc = c_parser_peek_token (parser)->location;
2139   c_parser_consume_token (parser);
2140   attrs = c_parser_attributes (parser);
2141
2142   /* Set the location in case we create a decl now.  */
2143   c_parser_set_source_position_from_token (c_parser_peek_token (parser));
2144
2145   if (c_parser_next_token_is (parser, CPP_NAME))
2146     {
2147       ident = c_parser_peek_token (parser)->value;
2148       ident_loc = c_parser_peek_token (parser)->location;
2149       struct_loc = ident_loc;
2150       c_parser_consume_token (parser);
2151     }
2152   if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
2153     {
2154       /* Parse a struct or union definition.  Start the scope of the
2155          tag before parsing components.  */
2156       struct c_struct_parse_info *struct_info;
2157       tree type = start_struct (struct_loc, code, ident, &struct_info);
2158       tree postfix_attrs;
2159       /* We chain the components in reverse order, then put them in
2160          forward order at the end.  Each struct-declaration may
2161          declare multiple components (comma-separated), so we must use
2162          chainon to join them, although when parsing each
2163          struct-declaration we can use TREE_CHAIN directly.
2164
2165          The theory behind all this is that there will be more
2166          semicolon separated fields than comma separated fields, and
2167          so we'll be minimizing the number of node traversals required
2168          by chainon.  */
2169       tree contents = NULL_TREE;
2170       c_parser_consume_token (parser);
2171       /* Handle the Objective-C @defs construct,
2172          e.g. foo(sizeof(struct{ @defs(ClassName) }));.  */
2173       if (c_parser_next_token_is_keyword (parser, RID_AT_DEFS))
2174         {
2175           tree name;
2176           gcc_assert (c_dialect_objc ());
2177           c_parser_consume_token (parser);
2178           if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2179             goto end_at_defs;
2180           if (c_parser_next_token_is (parser, CPP_NAME)
2181               && c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME)
2182             {
2183               name = c_parser_peek_token (parser)->value;
2184               c_parser_consume_token (parser);
2185             }
2186           else
2187             {
2188               c_parser_error (parser, "expected class name");
2189               c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
2190               goto end_at_defs;
2191             }
2192           c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2193                                      "expected %<)%>");
2194           contents = nreverse (objc_get_class_ivars (name));
2195         }
2196     end_at_defs:
2197       /* Parse the struct-declarations and semicolons.  Problems with
2198          semicolons are diagnosed here; empty structures are diagnosed
2199          elsewhere.  */
2200       while (true)
2201         {
2202           tree decls;
2203           /* Parse any stray semicolon.  */
2204           if (c_parser_next_token_is (parser, CPP_SEMICOLON))
2205             {
2206               pedwarn (c_parser_peek_token (parser)->location, OPT_pedantic,
2207                        "extra semicolon in struct or union specified");
2208               c_parser_consume_token (parser);
2209               continue;
2210             }
2211           /* Stop if at the end of the struct or union contents.  */
2212           if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2213             {
2214               c_parser_consume_token (parser);
2215               break;
2216             }
2217           /* Accept #pragmas at struct scope.  */
2218           if (c_parser_next_token_is (parser, CPP_PRAGMA))
2219             {
2220               c_parser_pragma (parser, pragma_external);
2221               continue;
2222             }
2223           /* Parse some comma-separated declarations, but not the
2224              trailing semicolon if any.  */
2225           decls = c_parser_struct_declaration (parser);
2226           contents = chainon (decls, contents);
2227           /* If no semicolon follows, either we have a parse error or
2228              are at the end of the struct or union and should
2229              pedwarn.  */
2230           if (c_parser_next_token_is (parser, CPP_SEMICOLON))
2231             c_parser_consume_token (parser);
2232           else
2233             {
2234               if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2235                 pedwarn (c_parser_peek_token (parser)->location, 0,
2236                          "no semicolon at end of struct or union");
2237               else
2238                 {
2239                   c_parser_error (parser, "expected %<;%>");
2240                   c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
2241                   break;
2242                 }
2243             }
2244         }
2245       postfix_attrs = c_parser_attributes (parser);
2246       ret.spec = finish_struct (struct_loc, type, nreverse (contents),
2247                                 chainon (attrs, postfix_attrs), struct_info);
2248       ret.kind = ctsk_tagdef;
2249       ret.expr = NULL_TREE;
2250       ret.expr_const_operands = true;
2251       return ret;
2252     }
2253   else if (!ident)
2254     {
2255       c_parser_error (parser, "expected %<{%>");
2256       ret.spec = error_mark_node;
2257       ret.kind = ctsk_tagref;
2258       ret.expr = NULL_TREE;
2259       ret.expr_const_operands = true;
2260       return ret;
2261     }
2262   ret = parser_xref_tag (ident_loc, code, ident);
2263   return ret;
2264 }
2265
2266 /* Parse a struct-declaration (C90 6.5.2.1, C99 6.7.2.1), *without*
2267    the trailing semicolon.
2268
2269    struct-declaration:
2270      specifier-qualifier-list struct-declarator-list
2271      static_assert-declaration-no-semi
2272
2273    specifier-qualifier-list:
2274      type-specifier specifier-qualifier-list[opt]
2275      type-qualifier specifier-qualifier-list[opt]
2276      attributes specifier-qualifier-list[opt]
2277
2278    struct-declarator-list:
2279      struct-declarator
2280      struct-declarator-list , attributes[opt] struct-declarator
2281
2282    struct-declarator:
2283      declarator attributes[opt]
2284      declarator[opt] : constant-expression attributes[opt]
2285
2286    GNU extensions:
2287
2288    struct-declaration:
2289      __extension__ struct-declaration
2290      specifier-qualifier-list
2291
2292    Unlike the ISO C syntax, semicolons are handled elsewhere.  The use
2293    of attributes where shown is a GNU extension.  In GNU C, we accept
2294    any expression without commas in the syntax (assignment
2295    expressions, not just conditional expressions); assignment
2296    expressions will be diagnosed as non-constant.  */
2297
2298 static tree
2299 c_parser_struct_declaration (c_parser *parser)
2300 {
2301   struct c_declspecs *specs;
2302   tree prefix_attrs;
2303   tree all_prefix_attrs;
2304   tree decls;
2305   location_t decl_loc;
2306   if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
2307     {
2308       int ext;
2309       tree decl;
2310       ext = disable_extension_diagnostics ();
2311       c_parser_consume_token (parser);
2312       decl = c_parser_struct_declaration (parser);
2313       restore_extension_diagnostics (ext);
2314       return decl;
2315     }
2316   if (c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT))
2317     {
2318       c_parser_static_assert_declaration_no_semi (parser);
2319       return NULL_TREE;
2320     }
2321   specs = build_null_declspecs ();
2322   decl_loc = c_parser_peek_token (parser)->location;
2323   c_parser_declspecs (parser, specs, false, true, true);
2324   if (parser->error)
2325     return NULL_TREE;
2326   if (!specs->declspecs_seen_p)
2327     {
2328       c_parser_error (parser, "expected specifier-qualifier-list");
2329       return NULL_TREE;
2330     }
2331   finish_declspecs (specs);
2332   if (c_parser_next_token_is (parser, CPP_SEMICOLON))
2333     {
2334       tree ret;
2335       if (!specs->type_seen_p)
2336         {
2337           pedwarn (decl_loc, OPT_pedantic,
2338                    "ISO C forbids member declarations with no members");
2339           shadow_tag_warned (specs, pedantic);
2340           ret = NULL_TREE;
2341         }
2342       else
2343         {
2344           /* Support for unnamed structs or unions as members of
2345              structs or unions (which is [a] useful and [b] supports
2346              MS P-SDK).  */
2347           tree attrs = NULL;
2348
2349           ret = grokfield (c_parser_peek_token (parser)->location,
2350                            build_id_declarator (NULL_TREE), specs,
2351                            NULL_TREE, &attrs);
2352           if (ret)
2353             decl_attributes (&ret, attrs, 0);
2354         }
2355       return ret;
2356     }
2357   pending_xref_error ();
2358   prefix_attrs = specs->attrs;
2359   all_prefix_attrs = prefix_attrs;
2360   specs->attrs = NULL_TREE;
2361   decls = NULL_TREE;
2362   while (true)
2363     {
2364       /* Declaring one or more declarators or un-named bit-fields.  */
2365       struct c_declarator *declarator;
2366       bool dummy = false;
2367       if (c_parser_next_token_is (parser, CPP_COLON))
2368         declarator = build_id_declarator (NULL_TREE);
2369       else
2370         declarator = c_parser_declarator (parser, specs->type_seen_p,
2371                                           C_DTR_NORMAL, &dummy);
2372       if (declarator == NULL)
2373         {
2374           c_parser_skip_to_end_of_block_or_statement (parser);
2375           break;
2376         }
2377       if (c_parser_next_token_is (parser, CPP_COLON)
2378           || c_parser_next_token_is (parser, CPP_COMMA)
2379           || c_parser_next_token_is (parser, CPP_SEMICOLON)
2380           || c_parser_next_token_is (parser, CPP_CLOSE_BRACE)
2381           || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2382         {
2383           tree postfix_attrs = NULL_TREE;
2384           tree width = NULL_TREE;
2385           tree d;
2386           if (c_parser_next_token_is (parser, CPP_COLON))
2387             {
2388               c_parser_consume_token (parser);
2389               width = c_parser_expr_no_commas (parser, NULL).value;
2390             }
2391           if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2392             postfix_attrs = c_parser_attributes (parser);
2393           d = grokfield (c_parser_peek_token (parser)->location,
2394                          declarator, specs, width, &all_prefix_attrs);
2395           decl_attributes (&d, chainon (postfix_attrs,
2396                                         all_prefix_attrs), 0);
2397           DECL_CHAIN (d) = decls;
2398           decls = d;
2399           if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2400             all_prefix_attrs = chainon (c_parser_attributes (parser),
2401                                         prefix_attrs);
2402           else
2403             all_prefix_attrs = prefix_attrs;
2404           if (c_parser_next_token_is (parser, CPP_COMMA))
2405             c_parser_consume_token (parser);
2406           else if (c_parser_next_token_is (parser, CPP_SEMICOLON)
2407                    || c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2408             {
2409               /* Semicolon consumed in caller.  */
2410               break;
2411             }
2412           else
2413             {
2414               c_parser_error (parser, "expected %<,%>, %<;%> or %<}%>");
2415               break;
2416             }
2417         }
2418       else
2419         {
2420           c_parser_error (parser,
2421                           "expected %<:%>, %<,%>, %<;%>, %<}%> or "
2422                           "%<__attribute__%>");
2423           break;
2424         }
2425     }
2426   return decls;
2427 }
2428
2429 /* Parse a typeof specifier (a GNU extension).
2430
2431    typeof-specifier:
2432      typeof ( expression )
2433      typeof ( type-name )
2434 */
2435
2436 static struct c_typespec
2437 c_parser_typeof_specifier (c_parser *parser)
2438 {
2439   struct c_typespec ret;
2440   ret.kind = ctsk_typeof;
2441   ret.spec = error_mark_node;
2442   ret.expr = NULL_TREE;
2443   ret.expr_const_operands = true;
2444   gcc_assert (c_parser_next_token_is_keyword (parser, RID_TYPEOF));
2445   c_parser_consume_token (parser);
2446   c_inhibit_evaluation_warnings++;
2447   in_typeof++;
2448   if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2449     {
2450       c_inhibit_evaluation_warnings--;
2451       in_typeof--;
2452       return ret;
2453     }
2454   if (c_parser_next_token_starts_typename (parser))
2455     {
2456       struct c_type_name *type = c_parser_type_name (parser);
2457       c_inhibit_evaluation_warnings--;
2458       in_typeof--;
2459       if (type != NULL)
2460         {
2461           ret.spec = groktypename (type, &ret.expr, &ret.expr_const_operands);
2462           pop_maybe_used (variably_modified_type_p (ret.spec, NULL_TREE));
2463         }
2464     }
2465   else
2466     {
2467       bool was_vm;
2468       location_t here = c_parser_peek_token (parser)->location;
2469       struct c_expr expr = c_parser_expression (parser);
2470       c_inhibit_evaluation_warnings--;
2471       in_typeof--;
2472       if (TREE_CODE (expr.value) == COMPONENT_REF
2473           && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1)))
2474         error_at (here, "%<typeof%> applied to a bit-field");
2475       mark_exp_read (expr.value);
2476       ret.spec = TREE_TYPE (expr.value);
2477       was_vm = variably_modified_type_p (ret.spec, NULL_TREE);
2478       /* This is returned with the type so that when the type is
2479          evaluated, this can be evaluated.  */
2480       if (was_vm)
2481         ret.expr = c_fully_fold (expr.value, false, &ret.expr_const_operands);
2482       pop_maybe_used (was_vm);
2483     }
2484   c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
2485   return ret;
2486 }
2487
2488 /* Parse a declarator, possibly an abstract declarator (C90 6.5.4,
2489    6.5.5, C99 6.7.5, 6.7.6).  If TYPE_SEEN_P then a typedef name may
2490    be redeclared; otherwise it may not.  KIND indicates which kind of
2491    declarator is wanted.  Returns a valid declarator except in the
2492    case of a syntax error in which case NULL is returned.  *SEEN_ID is
2493    set to true if an identifier being declared is seen; this is used
2494    to diagnose bad forms of abstract array declarators and to
2495    determine whether an identifier list is syntactically permitted.
2496
2497    declarator:
2498      pointer[opt] direct-declarator
2499
2500    direct-declarator:
2501      identifier
2502      ( attributes[opt] declarator )
2503      direct-declarator array-declarator
2504      direct-declarator ( parameter-type-list )
2505      direct-declarator ( identifier-list[opt] )
2506
2507    pointer:
2508      * type-qualifier-list[opt]
2509      * type-qualifier-list[opt] pointer
2510
2511    type-qualifier-list:
2512      type-qualifier
2513      attributes
2514      type-qualifier-list type-qualifier
2515      type-qualifier-list attributes
2516
2517    parameter-type-list:
2518      parameter-list
2519      parameter-list , ...
2520
2521    parameter-list:
2522      parameter-declaration
2523      parameter-list , parameter-declaration
2524
2525    parameter-declaration:
2526      declaration-specifiers declarator attributes[opt]
2527      declaration-specifiers abstract-declarator[opt] attributes[opt]
2528
2529    identifier-list:
2530      identifier
2531      identifier-list , identifier
2532
2533    abstract-declarator:
2534      pointer
2535      pointer[opt] direct-abstract-declarator
2536
2537    direct-abstract-declarator:
2538      ( attributes[opt] abstract-declarator )
2539      direct-abstract-declarator[opt] array-declarator
2540      direct-abstract-declarator[opt] ( parameter-type-list[opt] )
2541
2542    GNU extensions:
2543
2544    direct-declarator:
2545      direct-declarator ( parameter-forward-declarations
2546                          parameter-type-list[opt] )
2547
2548    direct-abstract-declarator:
2549      direct-abstract-declarator[opt] ( parameter-forward-declarations
2550                                        parameter-type-list[opt] )
2551
2552    parameter-forward-declarations:
2553      parameter-list ;
2554      parameter-forward-declarations parameter-list ;
2555
2556    The uses of attributes shown above are GNU extensions.
2557
2558    Some forms of array declarator are not included in C99 in the
2559    syntax for abstract declarators; these are disallowed elsewhere.
2560    This may be a defect (DR#289).
2561
2562    This function also accepts an omitted abstract declarator as being
2563    an abstract declarator, although not part of the formal syntax.  */
2564
2565 static struct c_declarator *
2566 c_parser_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
2567                      bool *seen_id)
2568 {
2569   /* Parse any initial pointer part.  */
2570   if (c_parser_next_token_is (parser, CPP_MULT))
2571     {
2572       struct c_declspecs *quals_attrs = build_null_declspecs ();
2573       struct c_declarator *inner;
2574       c_parser_consume_token (parser);
2575       c_parser_declspecs (parser, quals_attrs, false, false, true);
2576       inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
2577       if (inner == NULL)
2578         return NULL;
2579       else
2580         return make_pointer_declarator (quals_attrs, inner);
2581     }
2582   /* Now we have a direct declarator, direct abstract declarator or
2583      nothing (which counts as a direct abstract declarator here).  */
2584   return c_parser_direct_declarator (parser, type_seen_p, kind, seen_id);
2585 }
2586
2587 /* Parse a direct declarator or direct abstract declarator; arguments
2588    as c_parser_declarator.  */
2589
2590 static struct c_declarator *
2591 c_parser_direct_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
2592                             bool *seen_id)
2593 {
2594   /* The direct declarator must start with an identifier (possibly
2595      omitted) or a parenthesized declarator (possibly abstract).  In
2596      an ordinary declarator, initial parentheses must start a
2597      parenthesized declarator.  In an abstract declarator or parameter
2598      declarator, they could start a parenthesized declarator or a
2599      parameter list.  To tell which, the open parenthesis and any
2600      following attributes must be read.  If a declaration specifier
2601      follows, then it is a parameter list; if the specifier is a
2602      typedef name, there might be an ambiguity about redeclaring it,
2603      which is resolved in the direction of treating it as a typedef
2604      name.  If a close parenthesis follows, it is also an empty
2605      parameter list, as the syntax does not permit empty abstract
2606      declarators.  Otherwise, it is a parenthesized declarator (in
2607      which case the analysis may be repeated inside it, recursively).
2608
2609      ??? There is an ambiguity in a parameter declaration "int
2610      (__attribute__((foo)) x)", where x is not a typedef name: it
2611      could be an abstract declarator for a function, or declare x with
2612      parentheses.  The proper resolution of this ambiguity needs
2613      documenting.  At present we follow an accident of the old
2614      parser's implementation, whereby the first parameter must have
2615      some declaration specifiers other than just attributes.  Thus as
2616      a parameter declaration it is treated as a parenthesized
2617      parameter named x, and as an abstract declarator it is
2618      rejected.
2619
2620      ??? Also following the old parser, attributes inside an empty
2621      parameter list are ignored, making it a list not yielding a
2622      prototype, rather than giving an error or making it have one
2623      parameter with implicit type int.
2624
2625      ??? Also following the old parser, typedef names may be
2626      redeclared in declarators, but not Objective-C class names.  */
2627
2628   if (kind != C_DTR_ABSTRACT
2629       && c_parser_next_token_is (parser, CPP_NAME)
2630       && ((type_seen_p
2631            && (c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME
2632                || c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME))
2633           || c_parser_peek_token (parser)->id_kind == C_ID_ID))
2634     {
2635       struct c_declarator *inner
2636         = build_id_declarator (c_parser_peek_token (parser)->value);
2637       *seen_id = true;
2638       inner->id_loc = c_parser_peek_token (parser)->location;
2639       c_parser_consume_token (parser);
2640       return c_parser_direct_declarator_inner (parser, *seen_id, inner);
2641     }
2642
2643   if (kind != C_DTR_NORMAL
2644       && c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
2645     {
2646       struct c_declarator *inner = build_id_declarator (NULL_TREE);
2647       return c_parser_direct_declarator_inner (parser, *seen_id, inner);
2648     }
2649
2650   /* Either we are at the end of an abstract declarator, or we have
2651      parentheses.  */
2652
2653   if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
2654     {
2655       tree attrs;
2656       struct c_declarator *inner;
2657       c_parser_consume_token (parser);
2658       attrs = c_parser_attributes (parser);
2659       if (kind != C_DTR_NORMAL
2660           && (c_parser_next_token_starts_declspecs (parser)
2661               || c_parser_next_token_is (parser, CPP_CLOSE_PAREN)))
2662         {
2663           struct c_arg_info *args
2664             = c_parser_parms_declarator (parser, kind == C_DTR_NORMAL,
2665                                          attrs);
2666           if (args == NULL)
2667             return NULL;
2668           else
2669             {
2670               inner
2671                 = build_function_declarator (args,
2672                                              build_id_declarator (NULL_TREE));
2673               return c_parser_direct_declarator_inner (parser, *seen_id,
2674                                                        inner);
2675             }
2676         }
2677       /* A parenthesized declarator.  */
2678       inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
2679       if (inner != NULL && attrs != NULL)
2680         inner = build_attrs_declarator (attrs, inner);
2681       if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2682         {
2683           c_parser_consume_token (parser);
2684           if (inner == NULL)
2685             return NULL;
2686           else
2687             return c_parser_direct_declarator_inner (parser, *seen_id, inner);
2688         }
2689       else
2690         {
2691           c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2692                                      "expected %<)%>");
2693           return NULL;
2694         }
2695     }
2696   else
2697     {
2698       if (kind == C_DTR_NORMAL)
2699         {
2700           c_parser_error (parser, "expected identifier or %<(%>");
2701           return NULL;
2702         }
2703       else
2704         return build_id_declarator (NULL_TREE);
2705     }
2706 }
2707
2708 /* Parse part of a direct declarator or direct abstract declarator,
2709    given that some (in INNER) has already been parsed; ID_PRESENT is
2710    true if an identifier is present, false for an abstract
2711    declarator.  */
2712
2713 static struct c_declarator *
2714 c_parser_direct_declarator_inner (c_parser *parser, bool id_present,
2715                                   struct c_declarator *inner)
2716 {
2717   /* Parse a sequence of array declarators and parameter lists.  */
2718   if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
2719     {
2720       location_t brace_loc = c_parser_peek_token (parser)->location;
2721       struct c_declarator *declarator;
2722       struct c_declspecs *quals_attrs = build_null_declspecs ();
2723       bool static_seen;
2724       bool star_seen;
2725       tree dimen;
2726       c_parser_consume_token (parser);
2727       c_parser_declspecs (parser, quals_attrs, false, false, true);
2728       static_seen = c_parser_next_token_is_keyword (parser, RID_STATIC);
2729       if (static_seen)
2730         c_parser_consume_token (parser);
2731       if (static_seen && !quals_attrs->declspecs_seen_p)
2732         c_parser_declspecs (parser, quals_attrs, false, false, true);
2733       if (!quals_attrs->declspecs_seen_p)
2734         quals_attrs = NULL;
2735       /* If "static" is present, there must be an array dimension.
2736          Otherwise, there may be a dimension, "*", or no
2737          dimension.  */
2738       if (static_seen)
2739         {
2740           star_seen = false;
2741           dimen = c_parser_expr_no_commas (parser, NULL).value;
2742         }
2743       else
2744         {
2745           if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
2746             {
2747               dimen = NULL_TREE;
2748               star_seen = false;
2749             }
2750           else if (c_parser_next_token_is (parser, CPP_MULT))
2751             {
2752               if (c_parser_peek_2nd_token (parser)->type == CPP_CLOSE_SQUARE)
2753                 {
2754                   dimen = NULL_TREE;
2755                   star_seen = true;
2756                   c_parser_consume_token (parser);
2757                 }
2758               else
2759                 {
2760                   star_seen = false;
2761                   dimen = c_parser_expr_no_commas (parser, NULL).value;
2762                 }
2763             }
2764           else
2765             {
2766               star_seen = false;
2767               dimen = c_parser_expr_no_commas (parser, NULL).value;
2768             }
2769         }
2770       if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
2771         c_parser_consume_token (parser);
2772       else
2773         {
2774           c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
2775                                      "expected %<]%>");
2776           return NULL;
2777         }
2778       if (dimen)
2779         mark_exp_read (dimen);
2780       declarator = build_array_declarator (brace_loc, dimen, quals_attrs,
2781                                            static_seen, star_seen);
2782       if (declarator == NULL)
2783         return NULL;
2784       inner = set_array_declarator_inner (declarator, inner);
2785       return c_parser_direct_declarator_inner (parser, id_present, inner);
2786     }
2787   else if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
2788     {
2789       tree attrs;
2790       struct c_arg_info *args;
2791       c_parser_consume_token (parser);
2792       attrs = c_parser_attributes (parser);
2793       args = c_parser_parms_declarator (parser, id_present, attrs);
2794       if (args == NULL)
2795         return NULL;
2796       else
2797         {
2798           inner = build_function_declarator (args, inner);
2799           return c_parser_direct_declarator_inner (parser, id_present, inner);
2800         }
2801     }
2802   return inner;
2803 }
2804
2805 /* Parse a parameter list or identifier list, including the closing
2806    parenthesis but not the opening one.  ATTRS are the attributes at
2807    the start of the list.  ID_LIST_OK is true if an identifier list is
2808    acceptable; such a list must not have attributes at the start.  */
2809
2810 static struct c_arg_info *
2811 c_parser_parms_declarator (c_parser *parser, bool id_list_ok, tree attrs)
2812 {
2813   push_scope ();
2814   declare_parm_level ();
2815   /* If the list starts with an identifier, it is an identifier list.
2816      Otherwise, it is either a prototype list or an empty list.  */
2817   if (id_list_ok
2818       && !attrs
2819       && c_parser_next_token_is (parser, CPP_NAME)
2820       && c_parser_peek_token (parser)->id_kind == C_ID_ID)
2821     {
2822       tree list = NULL_TREE, *nextp = &list;
2823       while (c_parser_next_token_is (parser, CPP_NAME)
2824              && c_parser_peek_token (parser)->id_kind == C_ID_ID)
2825         {
2826           *nextp = build_tree_list (NULL_TREE,
2827                                     c_parser_peek_token (parser)->value);
2828           nextp = & TREE_CHAIN (*nextp);
2829           c_parser_consume_token (parser);
2830           if (c_parser_next_token_is_not (parser, CPP_COMMA))
2831             break;
2832           c_parser_consume_token (parser);
2833           if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2834             {
2835               c_parser_error (parser, "expected identifier");
2836               break;
2837             }
2838         }
2839       if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2840         {
2841           struct c_arg_info *ret = build_arg_info ();
2842           ret->types = list;
2843           c_parser_consume_token (parser);
2844           pop_scope ();
2845           return ret;
2846         }
2847       else
2848         {
2849           c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2850                                      "expected %<)%>");
2851           pop_scope ();
2852           return NULL;
2853         }
2854     }
2855   else
2856     {
2857       struct c_arg_info *ret = c_parser_parms_list_declarator (parser, attrs);
2858       pop_scope ();
2859       return ret;
2860     }
2861 }
2862
2863 /* Parse a parameter list (possibly empty), including the closing
2864    parenthesis but not the opening one.  ATTRS are the attributes at
2865    the start of the list.  */
2866
2867 static struct c_arg_info *
2868 c_parser_parms_list_declarator (c_parser *parser, tree attrs)
2869 {
2870   bool bad_parm = false;
2871   /* ??? Following the old parser, forward parameter declarations may
2872      use abstract declarators, and if no real parameter declarations
2873      follow the forward declarations then this is not diagnosed.  Also
2874      note as above that attributes are ignored as the only contents of
2875      the parentheses, or as the only contents after forward
2876      declarations.  */
2877   if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2878     {
2879       struct c_arg_info *ret = build_arg_info ();
2880       c_parser_consume_token (parser);
2881       return ret;
2882     }
2883   if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
2884     {
2885       struct c_arg_info *ret = build_arg_info ();
2886       /* Suppress -Wold-style-definition for this case.  */
2887       ret->types = error_mark_node;
2888       error_at (c_parser_peek_token (parser)->location,
2889                 "ISO C requires a named argument before %<...%>");
2890       c_parser_consume_token (parser);
2891       if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2892         {
2893           c_parser_consume_token (parser);
2894           return ret;
2895         }
2896       else
2897         {
2898           c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2899                                      "expected %<)%>");
2900           return NULL;
2901         }
2902     }
2903   /* Nonempty list of parameters, either terminated with semicolon
2904      (forward declarations; recurse) or with close parenthesis (normal
2905      function) or with ", ... )" (variadic function).  */
2906   while (true)
2907     {
2908       /* Parse a parameter.  */
2909       struct c_parm *parm = c_parser_parameter_declaration (parser, attrs);
2910       attrs = NULL_TREE;
2911       if (parm == NULL)
2912         bad_parm = true;
2913       else
2914         push_parm_decl (parm);
2915       if (c_parser_next_token_is (parser, CPP_SEMICOLON))
2916         {
2917           tree new_attrs;
2918           c_parser_consume_token (parser);
2919           mark_forward_parm_decls ();
2920           new_attrs = c_parser_attributes (parser);
2921           return c_parser_parms_list_declarator (parser, new_attrs);
2922         }
2923       if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2924         {
2925           c_parser_consume_token (parser);
2926           if (bad_parm)
2927             {
2928               get_pending_sizes ();
2929               return NULL;
2930             }
2931           else
2932             return get_parm_info (false);
2933         }
2934       if (!c_parser_require (parser, CPP_COMMA,
2935                              "expected %<;%>, %<,%> or %<)%>"))
2936         {
2937           c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
2938           get_pending_sizes ();
2939           return NULL;
2940         }
2941       if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
2942         {
2943           c_parser_consume_token (parser);
2944           if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2945             {
2946               c_parser_consume_token (parser);
2947               if (bad_parm)
2948                 {
2949                   get_pending_sizes ();
2950                   return NULL;
2951                 }
2952               else
2953                 return get_parm_info (true);
2954             }
2955           else
2956             {
2957               c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2958                                          "expected %<)%>");
2959               get_pending_sizes ();
2960               return NULL;
2961             }
2962         }
2963     }
2964 }
2965
2966 /* Parse a parameter declaration.  ATTRS are the attributes at the
2967    start of the declaration if it is the first parameter.  */
2968
2969 static struct c_parm *
2970 c_parser_parameter_declaration (c_parser *parser, tree attrs)
2971 {
2972   struct c_declspecs *specs;
2973   struct c_declarator *declarator;
2974   tree prefix_attrs;
2975   tree postfix_attrs = NULL_TREE;
2976   bool dummy = false;
2977   if (!c_parser_next_token_starts_declspecs (parser))
2978     {
2979       c_token *token = c_parser_peek_token (parser);
2980       if (parser->error)
2981         return NULL;
2982       c_parser_set_source_position_from_token (token);
2983       if (token->type == CPP_NAME
2984           && c_parser_peek_2nd_token (parser)->type != CPP_COMMA
2985           && c_parser_peek_2nd_token (parser)->type != CPP_CLOSE_PAREN)
2986         {
2987           error ("unknown type name %qE", token->value);
2988           parser->error = true;
2989         }
2990       /* ??? In some Objective-C cases '...' isn't applicable so there
2991          should be a different message.  */
2992       else
2993         c_parser_error (parser,
2994                         "expected declaration specifiers or %<...%>");
2995       c_parser_skip_to_end_of_parameter (parser);
2996       return NULL;
2997     }
2998   specs = build_null_declspecs ();
2999   if (attrs)
3000     {
3001       declspecs_add_attrs (specs, attrs);
3002       attrs = NULL_TREE;
3003     }
3004   c_parser_declspecs (parser, specs, true, true, true);
3005   finish_declspecs (specs);
3006   pending_xref_error ();
3007   prefix_attrs = specs->attrs;
3008   specs->attrs = NULL_TREE;
3009   declarator = c_parser_declarator (parser, specs->type_seen_p,
3010                                     C_DTR_PARM, &dummy);
3011   if (declarator == NULL)
3012     {
3013       c_parser_skip_until_found (parser, CPP_COMMA, NULL);
3014       return NULL;
3015     }
3016   if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
3017     postfix_attrs = c_parser_attributes (parser);
3018   return build_c_parm (specs, chainon (postfix_attrs, prefix_attrs),
3019                        declarator);
3020 }
3021
3022 /* Parse a string literal in an asm expression.  It should not be
3023    translated, and wide string literals are an error although
3024    permitted by the syntax.  This is a GNU extension.
3025
3026    asm-string-literal:
3027      string-literal
3028
3029    ??? At present, following the old parser, the caller needs to have
3030    set lex_untranslated_string to 1.  It would be better to follow the
3031    C++ parser rather than using this kludge.  */
3032
3033 static tree
3034 c_parser_asm_string_literal (c_parser *parser)
3035 {
3036   tree str;
3037   if (c_parser_next_token_is (parser, CPP_STRING))
3038     {
3039       str = c_parser_peek_token (parser)->value;
3040       c_parser_consume_token (parser);
3041     }
3042   else if (c_parser_next_token_is (parser, CPP_WSTRING))
3043     {
3044       error_at (c_parser_peek_token (parser)->location,
3045                 "wide string literal in %<asm%>");
3046       str = build_string (1, "");
3047       c_parser_consume_token (parser);
3048     }
3049   else
3050     {
3051       c_parser_error (parser, "expected string literal");
3052       str = NULL_TREE;
3053     }
3054   return str;
3055 }
3056
3057 /* Parse a simple asm expression.  This is used in restricted
3058    contexts, where a full expression with inputs and outputs does not
3059    make sense.  This is a GNU extension.
3060
3061    simple-asm-expr:
3062      asm ( asm-string-literal )
3063 */
3064
3065 static tree
3066 c_parser_simple_asm_expr (c_parser *parser)
3067 {
3068   tree str;
3069   gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM));
3070   /* ??? Follow the C++ parser rather than using the
3071      lex_untranslated_string kludge.  */
3072   parser->lex_untranslated_string = true;
3073   c_parser_consume_token (parser);
3074   if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3075     {
3076       parser->lex_untranslated_string = false;
3077       return NULL_TREE;
3078     }
3079   str = c_parser_asm_string_literal (parser);
3080   parser->lex_untranslated_string = false;
3081   if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
3082     {
3083       c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
3084       return NULL_TREE;
3085     }
3086   return str;
3087 }
3088
3089 /* Parse (possibly empty) attributes.  This is a GNU extension.
3090
3091    attributes:
3092      empty
3093      attributes attribute
3094
3095    attribute:
3096      __attribute__ ( ( attribute-list ) )
3097
3098    attribute-list:
3099      attrib
3100      attribute_list , attrib
3101
3102    attrib:
3103      empty
3104      any-word
3105      any-word ( identifier )
3106      any-word ( identifier , nonempty-expr-list )
3107      any-word ( expr-list )
3108
3109    where the "identifier" must not be declared as a type, and
3110    "any-word" may be any identifier (including one declared as a
3111    type), a reserved word storage class specifier, type specifier or
3112    type qualifier.  ??? This still leaves out most reserved keywords
3113    (following the old parser), shouldn't we include them, and why not
3114    allow identifiers declared as types to start the arguments?  */
3115
3116 static tree
3117 c_parser_attributes (c_parser *parser)
3118 {
3119   tree attrs = NULL_TREE;
3120   while (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
3121     {
3122       /* ??? Follow the C++ parser rather than using the
3123          lex_untranslated_string kludge.  */
3124       parser->lex_untranslated_string = true;
3125       c_parser_consume_token (parser);
3126       if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3127         {
3128           parser->lex_untranslated_string = false;
3129           return attrs;
3130         }
3131       if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3132         {
3133           parser->lex_untranslated_string = false;
3134           c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
3135           return attrs;
3136         }
3137       /* Parse the attribute list.  */
3138       while (c_parser_next_token_is (parser, CPP_COMMA)
3139              || c_parser_next_token_is (parser, CPP_NAME)
3140              || c_parser_next_token_is (parser, CPP_KEYWORD))
3141         {
3142           tree attr, attr_name, attr_args;
3143           VEC(tree,gc) *expr_list;
3144           if (c_parser_next_token_is (parser, CPP_COMMA))
3145             {
3146               c_parser_consume_token (parser);
3147               continue;
3148             }
3149           if (c_parser_next_token_is (parser, CPP_KEYWORD))
3150             {
3151               /* ??? See comment above about what keywords are
3152                  accepted here.  */
3153               bool ok;
3154               switch (c_parser_peek_token (parser)->keyword)
3155                 {
3156                 case RID_STATIC:
3157                 case RID_UNSIGNED:
3158                 case RID_LONG:
3159                 case RID_INT128:
3160                 case RID_CONST:
3161                 case RID_EXTERN:
3162                 case RID_REGISTER:
3163                 case RID_TYPEDEF:
3164                 case RID_SHORT:
3165                 case RID_INLINE:
3166                 case RID_VOLATILE:
3167                 case RID_SIGNED:
3168                 case RID_AUTO:
3169                 case RID_RESTRICT:
3170                 case RID_COMPLEX:
3171                 case RID_THREAD:
3172                 case RID_INT:
3173                 case RID_CHAR:
3174                 case RID_FLOAT:
3175                 case RID_DOUBLE:
3176                 case RID_VOID:
3177                 case RID_DFLOAT32:
3178                 case RID_DFLOAT64:
3179                 case RID_DFLOAT128:
3180                 case RID_BOOL:
3181                 case RID_FRACT:
3182                 case RID_ACCUM:
3183                 case RID_SAT:
3184                   ok = true;
3185                   break;
3186                 default:
3187                   ok = false;
3188                   break;
3189                 }
3190               if (!ok)
3191                 break;
3192               /* Accept __attribute__((__const)) as __attribute__((const))
3193                  etc.  */
3194               attr_name
3195                 = ridpointers[(int) c_parser_peek_token (parser)->keyword];
3196             }
3197           else
3198             attr_name = c_parser_peek_token (parser)->value;
3199           c_parser_consume_token (parser);
3200           if (c_parser_next_token_is_not (parser, CPP_OPEN_PAREN))
3201             {
3202               attr = build_tree_list (attr_name, NULL_TREE);
3203               attrs = chainon (attrs, attr);
3204               continue;
3205             }
3206           c_parser_consume_token (parser);
3207           /* Parse the attribute contents.  If they start with an
3208              identifier which is followed by a comma or close
3209              parenthesis, then the arguments start with that
3210              identifier; otherwise they are an expression list.  */
3211           if (c_parser_next_token_is (parser, CPP_NAME)
3212               && c_parser_peek_token (parser)->id_kind == C_ID_ID
3213               && ((c_parser_peek_2nd_token (parser)->type == CPP_COMMA)
3214                   || (c_parser_peek_2nd_token (parser)->type
3215                       == CPP_CLOSE_PAREN)))
3216             {
3217               tree arg1 = c_parser_peek_token (parser)->value;
3218               c_parser_consume_token (parser);
3219               if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3220                 attr_args = build_tree_list (NULL_TREE, arg1);
3221               else
3222                 {
3223                   tree tree_list;
3224                   c_parser_consume_token (parser);
3225                   expr_list = c_parser_expr_list (parser, false, true, NULL);
3226                   tree_list = build_tree_list_vec (expr_list);
3227                   attr_args = tree_cons (NULL_TREE, arg1, tree_list);
3228                   release_tree_vector (expr_list);
3229                 }
3230             }
3231           else
3232             {
3233               if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3234                 attr_args = NULL_TREE;
3235               else
3236                 {
3237                   expr_list = c_parser_expr_list (parser, false, true, NULL);
3238                   attr_args = build_tree_list_vec (expr_list);
3239                   release_tree_vector (expr_list);
3240                 }
3241             }
3242           attr = build_tree_list (attr_name, attr_args);
3243           if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3244             c_parser_consume_token (parser);
3245           else
3246             {
3247               parser->lex_untranslated_string = false;
3248               c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3249                                          "expected %<)%>");
3250               return attrs;
3251             }
3252           attrs = chainon (attrs, attr);
3253         }
3254       if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3255         c_parser_consume_token (parser);
3256       else
3257         {
3258           parser->lex_untranslated_string = false;
3259           c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3260                                      "expected %<)%>");
3261           return attrs;
3262         }
3263       if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3264         c_parser_consume_token (parser);
3265       else
3266         {
3267           parser->lex_untranslated_string = false;
3268           c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3269                                      "expected %<)%>");
3270           return attrs;
3271         }
3272       parser->lex_untranslated_string = false;
3273     }
3274   return attrs;
3275 }
3276
3277 /* Parse a type name (C90 6.5.5, C99 6.7.6).
3278
3279    type-name:
3280      specifier-qualifier-list abstract-declarator[opt]
3281 */
3282
3283 static struct c_type_name *
3284 c_parser_type_name (c_parser *parser)
3285 {
3286   struct c_declspecs *specs = build_null_declspecs ();
3287   struct c_declarator *declarator;
3288   struct c_type_name *ret;
3289   bool dummy = false;
3290   c_parser_declspecs (parser, specs, false, true, true);
3291   if (!specs->declspecs_seen_p)
3292     {
3293       c_parser_error (parser, "expected specifier-qualifier-list");
3294       return NULL;
3295     }
3296   pending_xref_error ();
3297   finish_declspecs (specs);
3298   declarator = c_parser_declarator (parser, specs->type_seen_p,
3299                                     C_DTR_ABSTRACT, &dummy);
3300   if (declarator == NULL)
3301     return NULL;
3302   ret = XOBNEW (&parser_obstack, struct c_type_name);
3303   ret->specs = specs;
3304   ret->declarator = declarator;
3305   return ret;
3306 }
3307
3308 /* Parse an initializer (C90 6.5.7, C99 6.7.8).
3309
3310    initializer:
3311      assignment-expression
3312      { initializer-list }
3313      { initializer-list , }
3314
3315    initializer-list:
3316      designation[opt] initializer
3317      initializer-list , designation[opt] initializer
3318
3319    designation:
3320      designator-list =
3321
3322    designator-list:
3323      designator
3324      designator-list designator
3325
3326    designator:
3327      array-designator
3328      . identifier
3329
3330    array-designator:
3331      [ constant-expression ]
3332
3333    GNU extensions:
3334
3335    initializer:
3336      { }
3337
3338    designation:
3339      array-designator
3340      identifier :
3341
3342    array-designator:
3343      [ constant-expression ... constant-expression ]
3344
3345    Any expression without commas is accepted in the syntax for the
3346    constant-expressions, with non-constant expressions rejected later.
3347
3348    This function is only used for top-level initializers; for nested
3349    ones, see c_parser_initval.  */
3350
3351 static struct c_expr
3352 c_parser_initializer (c_parser *parser)
3353 {
3354   if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
3355     return c_parser_braced_init (parser, NULL_TREE, false);
3356   else
3357     {
3358       struct c_expr ret;
3359       location_t loc = c_parser_peek_token (parser)->location;
3360       ret = c_parser_expr_no_commas (parser, NULL);
3361       if (TREE_CODE (ret.value) != STRING_CST
3362           && TREE_CODE (ret.value) != COMPOUND_LITERAL_EXPR)
3363         ret = default_function_array_read_conversion (loc, ret);
3364       return ret;
3365     }
3366 }
3367
3368 /* Parse a braced initializer list.  TYPE is the type specified for a
3369    compound literal, and NULL_TREE for other initializers and for
3370    nested braced lists.  NESTED_P is true for nested braced lists,
3371    false for the list of a compound literal or the list that is the
3372    top-level initializer in a declaration.  */
3373
3374 static struct c_expr
3375 c_parser_braced_init (c_parser *parser, tree type, bool nested_p)
3376 {
3377   struct c_expr ret;
3378   struct obstack braced_init_obstack;
3379   location_t brace_loc = c_parser_peek_token (parser)->location;
3380   gcc_obstack_init (&braced_init_obstack);
3381   gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE));
3382   c_parser_consume_token (parser);
3383   if (nested_p)
3384     push_init_level (0, &braced_init_obstack);
3385   else
3386     really_start_incremental_init (type);
3387   if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3388     {
3389       pedwarn (brace_loc, OPT_pedantic, "ISO C forbids empty initializer braces");
3390     }
3391   else
3392     {
3393       /* Parse a non-empty initializer list, possibly with a trailing
3394          comma.  */
3395       while (true)
3396         {
3397           c_parser_initelt (parser, &braced_init_obstack);
3398           if (parser->error)
3399             break;
3400           if (c_parser_next_token_is (parser, CPP_COMMA))
3401             c_parser_consume_token (parser);
3402           else
3403             break;
3404           if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3405             break;
3406         }
3407     }
3408   if (c_parser_next_token_is_not (parser, CPP_CLOSE_BRACE))
3409     {
3410       ret.value = error_mark_node;
3411       ret.original_code = ERROR_MARK;
3412       ret.original_type = NULL;
3413       c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, "expected %<}%>");
3414       pop_init_level (0, &braced_init_obstack);
3415       obstack_free (&braced_init_obstack, NULL);
3416       return ret;
3417     }
3418   c_parser_consume_token (parser);
3419   ret = pop_init_level (0, &braced_init_obstack);
3420   obstack_free (&braced_init_obstack, NULL);
3421   return ret;
3422 }
3423
3424 /* Parse a nested initializer, including designators.  */
3425
3426 static void
3427 c_parser_initelt (c_parser *parser, struct obstack * braced_init_obstack)
3428 {
3429   /* Parse any designator or designator list.  A single array
3430      designator may have the subsequent "=" omitted in GNU C, but a
3431      longer list or a structure member designator may not.  */
3432   if (c_parser_next_token_is (parser, CPP_NAME)
3433       && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
3434     {
3435       /* Old-style structure member designator.  */
3436       set_init_label (c_parser_peek_token (parser)->value,
3437                       braced_init_obstack);
3438       /* Use the colon as the error location.  */
3439       pedwarn (c_parser_peek_2nd_token (parser)->location, OPT_pedantic,
3440                "obsolete use of designated initializer with %<:%>");
3441       c_parser_consume_token (parser);
3442       c_parser_consume_token (parser);
3443     }
3444   else
3445     {
3446       /* des_seen is 0 if there have been no designators, 1 if there
3447          has been a single array designator and 2 otherwise.  */
3448       int des_seen = 0;
3449       /* Location of a designator.  */
3450       location_t des_loc = UNKNOWN_LOCATION;  /* Quiet warning.  */
3451       while (c_parser_next_token_is (parser, CPP_OPEN_SQUARE)
3452              || c_parser_next_token_is (parser, CPP_DOT))
3453         {
3454           int des_prev = des_seen;
3455           if (!des_seen)
3456             des_loc = c_parser_peek_token (parser)->location;
3457           if (des_seen < 2)
3458             des_seen++;
3459           if (c_parser_next_token_is (parser, CPP_DOT))
3460             {
3461               des_seen = 2;
3462               c_parser_consume_token (parser);
3463               if (c_parser_next_token_is (parser, CPP_NAME))
3464                 {
3465                   set_init_label (c_parser_peek_token (parser)->value,
3466                                   braced_init_obstack);
3467                   c_parser_consume_token (parser);
3468                 }
3469               else
3470                 {
3471                   struct c_expr init;
3472                   init.value = error_mark_node;
3473                   init.original_code = ERROR_MARK;
3474                   init.original_type = NULL;
3475                   c_parser_error (parser, "expected identifier");
3476                   c_parser_skip_until_found (parser, CPP_COMMA, NULL);
3477                   process_init_element (init, false, braced_init_obstack);
3478                   return;
3479                 }
3480             }
3481           else
3482             {
3483               tree first, second;
3484               location_t ellipsis_loc = UNKNOWN_LOCATION;  /* Quiet warning.  */
3485               /* ??? Following the old parser, [ objc-receiver
3486                  objc-message-args ] is accepted as an initializer,
3487                  being distinguished from a designator by what follows
3488                  the first assignment expression inside the square
3489                  brackets, but after a first array designator a
3490                  subsequent square bracket is for Objective-C taken to
3491                  start an expression, using the obsolete form of
3492                  designated initializer without '=', rather than
3493                  possibly being a second level of designation: in LALR
3494                  terms, the '[' is shifted rather than reducing
3495                  designator to designator-list.  */
3496               if (des_prev == 1 && c_dialect_objc ())
3497                 {
3498                   des_seen = des_prev;
3499                   break;
3500                 }
3501               if (des_prev == 0 && c_dialect_objc ())
3502                 {
3503                   /* This might be an array designator or an
3504                      Objective-C message expression.  If the former,
3505                      continue parsing here; if the latter, parse the
3506                      remainder of the initializer given the starting
3507                      primary-expression.  ??? It might make sense to
3508                      distinguish when des_prev == 1 as well; see
3509                      previous comment.  */
3510                   tree rec, args;
3511                   struct c_expr mexpr;
3512                   c_parser_consume_token (parser);
3513                   if (c_parser_peek_token (parser)->type == CPP_NAME
3514                       && ((c_parser_peek_token (parser)->id_kind
3515                            == C_ID_TYPENAME)
3516                           || (c_parser_peek_token (parser)->id_kind
3517                               == C_ID_CLASSNAME)))
3518                     {
3519                       /* Type name receiver.  */
3520                       tree id = c_parser_peek_token (parser)->value;
3521                       c_parser_consume_token (parser);
3522                       rec = objc_get_class_reference (id);
3523                       goto parse_message_args;
3524                     }
3525                   first = c_parser_expr_no_commas (parser, NULL).value;
3526                   mark_exp_read (first);
3527                   if (c_parser_next_token_is (parser, CPP_ELLIPSIS)
3528                       || c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3529                     goto array_desig_after_first;
3530                   /* Expression receiver.  So far only one part
3531                      without commas has been parsed; there might be
3532                      more of the expression.  */
3533                   rec = first;
3534                   while (c_parser_next_token_is (parser, CPP_COMMA))
3535                     {
3536                       struct c_expr next;
3537                       location_t comma_loc, exp_loc;
3538                       comma_loc = c_parser_peek_token (parser)->location;
3539                       c_parser_consume_token (parser);
3540                       exp_loc = c_parser_peek_token (parser)->location;
3541                       next = c_parser_expr_no_commas (parser, NULL);
3542                       next = default_function_array_read_conversion (exp_loc,
3543                                                                      next);
3544                       rec = build_compound_expr (comma_loc, rec, next.value);
3545                     }
3546                 parse_message_args:
3547                   /* Now parse the objc-message-args.  */
3548                   args = c_parser_objc_message_args (parser);
3549                   c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
3550                                              "expected %<]%>");
3551                   mexpr.value
3552                     = objc_build_message_expr (build_tree_list (rec, args));
3553                   mexpr.original_code = ERROR_MARK;
3554                   mexpr.original_type = NULL;
3555                   /* Now parse and process the remainder of the
3556                      initializer, starting with this message
3557                      expression as a primary-expression.  */
3558                   c_parser_initval (parser, &mexpr, braced_init_obstack);
3559                   return;
3560                 }
3561               c_parser_consume_token (parser);
3562               first = c_parser_expr_no_commas (parser, NULL).value;
3563               mark_exp_read (first);
3564             array_desig_after_first:
3565               if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
3566                 {
3567                   ellipsis_loc = c_parser_peek_token (parser)->location;
3568                   c_parser_consume_token (parser);
3569                   second = c_parser_expr_no_commas (parser, NULL).value;
3570                   mark_exp_read (second);
3571                 }
3572               else
3573                 second = NULL_TREE;
3574               if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3575                 {
3576                   c_parser_consume_token (parser);
3577                   set_init_index (first, second, braced_init_obstack);
3578                   if (second)
3579                     pedwarn (ellipsis_loc, OPT_pedantic,
3580                              "ISO C forbids specifying range of elements to initialize");
3581                 }
3582               else
3583                 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
3584                                            "expected %<]%>");
3585             }
3586         }
3587       if (des_seen >= 1)
3588         {
3589           if (c_parser_next_token_is (parser, CPP_EQ))
3590             {
3591               if (!flag_isoc99)
3592                 pedwarn (des_loc, OPT_pedantic,
3593                          "ISO C90 forbids specifying subobject to initialize");
3594               c_parser_consume_token (parser);
3595             }
3596           else
3597             {
3598               if (des_seen == 1)
3599                 pedwarn (c_parser_peek_token (parser)->location, OPT_pedantic,
3600                          "obsolete use of designated initializer without %<=%>");
3601               else
3602                 {
3603                   struct c_expr init;
3604                   init.value = error_mark_node;
3605                   init.original_code = ERROR_MARK;
3606                   init.original_type = NULL;
3607                   c_parser_error (parser, "expected %<=%>");
3608                   c_parser_skip_until_found (parser, CPP_COMMA, NULL);
3609                   process_init_element (init, false, braced_init_obstack);
3610                   return;
3611                 }
3612             }
3613         }
3614     }
3615   c_parser_initval (parser, NULL, braced_init_obstack);
3616 }
3617
3618 /* Parse a nested initializer; as c_parser_initializer but parses
3619    initializers within braced lists, after any designators have been
3620    applied.  If AFTER is not NULL then it is an Objective-C message
3621    expression which is the primary-expression starting the
3622    initializer.  */
3623
3624 static void
3625 c_parser_initval (c_parser *parser, struct c_expr *after,
3626                   struct obstack * braced_init_obstack)
3627 {
3628   struct c_expr init;
3629   gcc_assert (!after || c_dialect_objc ());
3630   if (c_parser_next_token_is (parser, CPP_OPEN_BRACE) && !after)
3631     init = c_parser_braced_init (parser, NULL_TREE, true);
3632   else
3633     {
3634       location_t loc = c_parser_peek_token (parser)->location;
3635       init = c_parser_expr_no_commas (parser, after);
3636       if (init.value != NULL_TREE
3637           && TREE_CODE (init.value) != STRING_CST
3638           && TREE_CODE (init.value) != COMPOUND_LITERAL_EXPR)
3639         init = default_function_array_read_conversion (loc, init);
3640     }
3641   process_init_element (init, false, braced_init_obstack);
3642 }
3643
3644 /* Parse a compound statement (possibly a function body) (C90 6.6.2,
3645    C99 6.8.2).
3646
3647    compound-statement:
3648      { block-item-list[opt] }
3649      { label-declarations block-item-list }
3650
3651    block-item-list:
3652      block-item
3653      block-item-list block-item
3654
3655    block-item:
3656      nested-declaration
3657      statement
3658
3659    nested-declaration:
3660      declaration
3661
3662    GNU extensions:
3663
3664    compound-statement:
3665      { label-declarations block-item-list }
3666
3667    nested-declaration:
3668      __extension__ nested-declaration
3669      nested-function-definition
3670
3671    label-declarations:
3672      label-declaration
3673      label-declarations label-declaration
3674
3675    label-declaration:
3676      __label__ identifier-list ;
3677
3678    Allowing the mixing of declarations and code is new in C99.  The
3679    GNU syntax also permits (not shown above) labels at the end of
3680    compound statements, which yield an error.  We don't allow labels
3681    on declarations; this might seem like a natural extension, but
3682    there would be a conflict between attributes on the label and
3683    prefix attributes on the declaration.  ??? The syntax follows the
3684    old parser in requiring something after label declarations.
3685    Although they are erroneous if the labels declared aren't defined,
3686    is it useful for the syntax to be this way?
3687
3688    OpenMP:
3689
3690    block-item:
3691      openmp-directive
3692
3693    openmp-directive:
3694      barrier-directive
3695      flush-directive  */
3696
3697 static tree
3698 c_parser_compound_statement (c_parser *parser)
3699 {
3700   tree stmt;
3701   location_t brace_loc;
3702   brace_loc = c_parser_peek_token (parser)->location;
3703   if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
3704     {
3705       /* Ensure a scope is entered and left anyway to avoid confusion
3706          if we have just prepared to enter a function body.  */
3707       stmt = c_begin_compound_stmt (true);
3708       c_end_compound_stmt (brace_loc, stmt, true);
3709       return error_mark_node;
3710     }
3711   stmt = c_begin_compound_stmt (true);
3712   c_parser_compound_statement_nostart (parser);
3713   return c_end_compound_stmt (brace_loc, stmt, true);
3714 }
3715
3716 /* Parse a compound statement except for the opening brace.  This is
3717    used for parsing both compound statements and statement expressions
3718    (which follow different paths to handling the opening).  */
3719
3720 static void
3721 c_parser_compound_statement_nostart (c_parser *parser)
3722 {
3723   bool last_stmt = false;
3724   bool last_label = false;
3725   bool save_valid_for_pragma = valid_location_for_stdc_pragma_p ();
3726   location_t label_loc = UNKNOWN_LOCATION;  /* Quiet warning.  */
3727   if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3728     {
3729       c_parser_consume_token (parser);
3730       return;
3731     }
3732   mark_valid_location_for_stdc_pragma (true);
3733   if (c_parser_next_token_is_keyword (parser, RID_LABEL))
3734     {
3735       /* Read zero or more forward-declarations for labels that nested
3736          functions can jump to.  */
3737       mark_valid_location_for_stdc_pragma (false);
3738       while (c_parser_next_token_is_keyword (parser, RID_LABEL))
3739         {
3740           label_loc = c_parser_peek_token (parser)->location;
3741           c_parser_consume_token (parser);
3742           /* Any identifiers, including those declared as type names,
3743              are OK here.  */
3744           while (true)
3745             {
3746               tree label;
3747               if (c_parser_next_token_is_not (parser, CPP_NAME))
3748                 {
3749                   c_parser_error (parser, "expected identifier");
3750                   break;
3751                 }
3752               label
3753                 = declare_label (c_parser_peek_token (parser)->value);
3754               C_DECLARED_LABEL_FLAG (label) = 1;
3755               add_stmt (build_stmt (label_loc, DECL_EXPR, label));
3756               c_parser_consume_token (parser);
3757               if (c_parser_next_token_is (parser, CPP_COMMA))
3758                 c_parser_consume_token (parser);
3759               else
3760                 break;
3761             }
3762           c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
3763         }
3764       pedwarn (label_loc, OPT_pedantic, "ISO C forbids label declarations");
3765     }
3766   /* We must now have at least one statement, label or declaration.  */
3767   if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3768     {
3769       mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
3770       c_parser_error (parser, "expected declaration or statement");
3771       c_parser_consume_token (parser);
3772       return;
3773     }
3774   while (c_parser_next_token_is_not (parser, CPP_CLOSE_BRACE))
3775     {
3776       location_t loc = c_parser_peek_token (parser)->location;
3777       if (c_parser_next_token_is_keyword (parser, RID_CASE)
3778           || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
3779           || (c_parser_next_token_is (parser, CPP_NAME)
3780               && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
3781         {
3782           if (c_parser_next_token_is_keyword (parser, RID_CASE))
3783             label_loc = c_parser_peek_2nd_token (parser)->location;
3784           else
3785             label_loc = c_parser_peek_token (parser)->location;
3786           last_label = true;
3787           last_stmt = false;
3788           mark_valid_location_for_stdc_pragma (false);
3789           c_parser_label (parser);
3790         }
3791       else if (!last_label
3792                && c_parser_next_token_starts_declaration (parser))
3793         {
3794           last_label = false;
3795           mark_valid_location_for_stdc_pragma (false);
3796           c_parser_declaration_or_fndef (parser, true, true, true, true, true, NULL);
3797           if (last_stmt)
3798             pedwarn_c90 (loc,
3799                          (pedantic && !flag_isoc99)
3800                          ? OPT_pedantic
3801                          : OPT_Wdeclaration_after_statement,
3802                          "ISO C90 forbids mixed declarations and code");
3803           last_stmt = false;
3804         }
3805       else if (!last_label
3806                && c_parser_next_token_is_keyword (parser, RID_EXTENSION))
3807         {
3808           /* __extension__ can start a declaration, but is also an
3809              unary operator that can start an expression.  Consume all
3810              but the last of a possible series of __extension__ to
3811              determine which.  */
3812           while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
3813                  && (c_parser_peek_2nd_token (parser)->keyword
3814                      == RID_EXTENSION))
3815             c_parser_consume_token (parser);
3816           if (c_token_starts_declaration (c_parser_peek_2nd_token (parser)))
3817             {
3818               int ext;
3819               ext = disable_extension_diagnostics ();
3820               c_parser_consume_token (parser);
3821               last_label = false;
3822               mark_valid_location_for_stdc_pragma (false);
3823               c_parser_declaration_or_fndef (parser, true, true, true, true,
3824                                              true, NULL);
3825               /* Following the old parser, __extension__ does not
3826                  disable this diagnostic.  */
3827               restore_extension_diagnostics (ext);
3828               if (last_stmt)
3829                 pedwarn_c90 (loc, (pedantic && !flag_isoc99)
3830                              ? OPT_pedantic
3831                              : OPT_Wdeclaration_after_statement,
3832                              "ISO C90 forbids mixed declarations and code");
3833               last_stmt = false;
3834             }
3835           else
3836             goto statement;
3837         }
3838       else if (c_parser_next_token_is (parser, CPP_PRAGMA))
3839         {
3840           /* External pragmas, and some omp pragmas, are not associated
3841              with regular c code, and so are not to be considered statements
3842              syntactically.  This ensures that the user doesn't put them
3843              places that would turn into syntax errors if the directive
3844              were ignored.  */
3845           if (c_parser_pragma (parser, pragma_compound))
3846             last_label = false, last_stmt = true;
3847         }
3848       else if (c_parser_next_token_is (parser, CPP_EOF))
3849         {
3850           mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
3851           c_parser_error (parser, "expected declaration or statement");
3852           return;
3853         }
3854       else if (c_parser_next_token_is_keyword (parser, RID_ELSE))
3855         {
3856           if (parser->in_if_block)
3857             {
3858               mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
3859               error_at (loc, """expected %<}%> before %<else%>");
3860               return;
3861             }
3862           else
3863             {
3864               error_at (loc, "%<else%> without a previous %<if%>");
3865               c_parser_consume_token (parser);
3866               continue;
3867             }
3868         }
3869       else
3870         {
3871         statement:
3872           last_label = false;
3873           last_stmt = true;
3874           mark_valid_location_for_stdc_pragma (false);
3875           c_parser_statement_after_labels (parser);
3876         }
3877
3878       parser->error = false;
3879     }
3880   if (last_label)
3881     error_at (label_loc, "label at end of compound statement");
3882   c_parser_consume_token (parser);
3883   /* Restore the value we started with.  */
3884   mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
3885 }
3886
3887 /* Parse a label (C90 6.6.1, C99 6.8.1).
3888
3889    label:
3890      identifier : attributes[opt]
3891      case constant-expression :
3892      default :
3893
3894    GNU extensions:
3895
3896    label:
3897      case constant-expression ... constant-expression :
3898
3899    The use of attributes on labels is a GNU extension.  The syntax in
3900    GNU C accepts any expressions without commas, non-constant
3901    expressions being rejected later.  */
3902
3903 static void
3904 c_parser_label (c_parser *parser)
3905 {
3906   location_t loc1 = c_parser_peek_token (parser)->location;
3907   tree label = NULL_TREE;
3908   if (c_parser_next_token_is_keyword (parser, RID_CASE))
3909     {
3910       tree exp1, exp2;
3911       c_parser_consume_token (parser);
3912       exp1 = c_parser_expr_no_commas (parser, NULL).value;
3913       if (c_parser_next_token_is (parser, CPP_COLON))
3914         {
3915           c_parser_consume_token (parser);
3916           label = do_case (loc1, exp1, NULL_TREE);
3917         }
3918       else if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
3919         {
3920           c_parser_consume_token (parser);
3921           exp2 = c_parser_expr_no_commas (parser, NULL).value;
3922           if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
3923             label = do_case (loc1, exp1, exp2);
3924         }
3925       else
3926         c_parser_error (parser, "expected %<:%> or %<...%>");
3927     }
3928   else if (c_parser_next_token_is_keyword (parser, RID_DEFAULT))
3929     {
3930       c_parser_consume_token (parser);
3931       if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
3932         label = do_case (loc1, NULL_TREE, NULL_TREE);
3933     }
3934   else
3935     {
3936       tree name = c_parser_peek_token (parser)->value;
3937       tree tlab;
3938       tree attrs;
3939       location_t loc2 = c_parser_peek_token (parser)->location;
3940       gcc_assert (c_parser_next_token_is (parser, CPP_NAME));
3941       c_parser_consume_token (parser);
3942       gcc_assert (c_parser_next_token_is (parser, CPP_COLON));
3943       c_parser_consume_token (parser);
3944       attrs = c_parser_attributes (parser);
3945       tlab = define_label (loc2, name);
3946       if (tlab)
3947         {
3948           decl_attributes (&tlab, attrs, 0);
3949           label = add_stmt (build_stmt (loc1, LABEL_EXPR, tlab));
3950         }
3951     }
3952   if (label)
3953     {
3954       if (c_parser_next_token_starts_declaration (parser)
3955           && !(c_parser_next_token_is (parser, CPP_NAME)
3956                && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
3957         {
3958           error_at (c_parser_peek_token (parser)->location,
3959                     "a label can only be part of a statement and "
3960                     "a declaration is not a statement");
3961           c_parser_declaration_or_fndef (parser, /*fndef_ok*/ false,
3962                                          /*static_assert_ok*/ true,
3963                                          /*nested*/ true, /*empty_ok*/ false,
3964                                          /*start_attr_ok*/ true, NULL);
3965         }
3966     }
3967 }
3968
3969 /* Parse a statement (C90 6.6, C99 6.8).
3970
3971    statement:
3972      labeled-statement
3973      compound-statement
3974      expression-statement
3975      selection-statement
3976      iteration-statement
3977      jump-statement
3978
3979    labeled-statement:
3980      label statement
3981
3982    expression-statement:
3983      expression[opt] ;
3984
3985    selection-statement:
3986      if-statement
3987      switch-statement
3988
3989    iteration-statement:
3990      while-statement
3991      do-statement
3992      for-statement
3993
3994    jump-statement:
3995      goto identifier ;
3996      continue ;
3997      break ;
3998      return expression[opt] ;
3999
4000    GNU extensions:
4001
4002    statement:
4003      asm-statement
4004
4005    jump-statement:
4006      goto * expression ;
4007
4008    Objective-C:
4009
4010    statement:
4011      objc-throw-statement
4012      objc-try-catch-statement
4013      objc-synchronized-statement
4014
4015    objc-throw-statement:
4016      @throw expression ;
4017      @throw ;
4018
4019    OpenMP:
4020
4021    statement:
4022      openmp-construct
4023
4024    openmp-construct:
4025      parallel-construct
4026      for-construct
4027      sections-construct
4028      single-construct
4029      parallel-for-construct
4030      parallel-sections-construct
4031      master-construct
4032      critical-construct
4033      atomic-construct
4034      ordered-construct
4035
4036    parallel-construct:
4037      parallel-directive structured-block
4038
4039    for-construct:
4040      for-directive iteration-statement
4041
4042    sections-construct:
4043      sections-directive section-scope
4044
4045    single-construct:
4046      single-directive structured-block
4047
4048    parallel-for-construct:
4049      parallel-for-directive iteration-statement
4050
4051    parallel-sections-construct:
4052      parallel-sections-directive section-scope
4053
4054    master-construct:
4055      master-directive structured-block
4056
4057    critical-construct:
4058      critical-directive structured-block
4059
4060    atomic-construct:
4061      atomic-directive expression-statement
4062
4063    ordered-construct:
4064      ordered-directive structured-block  */
4065
4066 static void
4067 c_parser_statement (c_parser *parser)
4068 {
4069   while (c_parser_next_token_is_keyword (parser, RID_CASE)
4070          || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
4071          || (c_parser_next_token_is (parser, CPP_NAME)
4072              && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
4073     c_parser_label (parser);
4074   c_parser_statement_after_labels (parser);
4075 }
4076
4077 /* Parse a statement, other than a labeled statement.  */
4078
4079 static void
4080 c_parser_statement_after_labels (c_parser *parser)
4081 {
4082   location_t loc = c_parser_peek_token (parser)->location;
4083   tree stmt = NULL_TREE;
4084   bool in_if_block = parser->in_if_block;
4085   parser->in_if_block = false;
4086   switch (c_parser_peek_token (parser)->type)
4087     {
4088     case CPP_OPEN_BRACE:
4089       add_stmt (c_parser_compound_statement (parser));
4090       break;
4091     case CPP_KEYWORD:
4092       switch (c_parser_peek_token (parser)->keyword)
4093         {
4094         case RID_IF:
4095           c_parser_if_statement (parser);
4096           break;
4097         case RID_SWITCH:
4098           c_parser_switch_statement (parser);
4099           break;
4100         case RID_WHILE:
4101           c_parser_while_statement (parser);
4102           break;
4103         case RID_DO:
4104           c_parser_do_statement (parser);
4105           break;
4106         case RID_FOR:
4107           c_parser_for_statement (parser);
4108           break;
4109         case RID_GOTO:
4110           c_parser_consume_token (parser);
4111           if (c_parser_next_token_is (parser, CPP_NAME))
4112             {
4113               stmt = c_finish_goto_label (loc,
4114                                           c_parser_peek_token (parser)->value);
4115               c_parser_consume_token (parser);
4116             }
4117           else if (c_parser_next_token_is (parser, CPP_MULT))
4118             {
4119               c_parser_consume_token (parser);
4120               stmt = c_finish_goto_ptr (loc,
4121                                         c_parser_expression (parser).value);
4122             }
4123           else
4124             c_parser_error (parser, "expected identifier or %<*%>");
4125           goto expect_semicolon;
4126         case RID_CONTINUE:
4127           c_parser_consume_token (parser);
4128           stmt = c_finish_bc_stmt (loc, &c_cont_label, false);
4129           goto expect_semicolon;
4130         case RID_BREAK:
4131           c_parser_consume_token (parser);
4132           stmt = c_finish_bc_stmt (loc, &c_break_label, true);
4133           goto expect_semicolon;
4134         case RID_RETURN:
4135           c_parser_consume_token (parser);
4136           if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4137             {
4138               stmt = c_finish_return (loc, NULL_TREE, NULL_TREE);
4139               c_parser_consume_token (parser);
4140             }
4141           else
4142             {
4143               struct c_expr expr = c_parser_expression_conv (parser);
4144               mark_exp_read (expr.value);
4145               stmt = c_finish_return (loc, expr.value, expr.original_type);
4146               goto expect_semicolon;
4147             }
4148           break;
4149         case RID_ASM:
4150           stmt = c_parser_asm_statement (parser);
4151           break;
4152         case RID_AT_THROW:
4153           gcc_assert (c_dialect_objc ());
4154           c_parser_consume_token (parser);
4155           if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4156             {
4157               stmt = objc_build_throw_stmt (loc, NULL_TREE);
4158               c_parser_consume_token (parser);
4159             }
4160           else
4161             {
4162               tree expr = c_parser_expression (parser).value;
4163               expr = c_fully_fold (expr, false, NULL);
4164               stmt = objc_build_throw_stmt (loc, expr);
4165               goto expect_semicolon;
4166             }
4167           break;
4168         case RID_AT_TRY:
4169           gcc_assert (c_dialect_objc ());
4170           c_parser_objc_try_catch_statement (parser);
4171           break;
4172         case RID_AT_SYNCHRONIZED:
4173           gcc_assert (c_dialect_objc ());
4174           c_parser_objc_synchronized_statement (parser);
4175           break;
4176         default:
4177           goto expr_stmt;
4178         }
4179       break;
4180     case CPP_SEMICOLON:
4181       c_parser_consume_token (parser);
4182       break;
4183     case CPP_CLOSE_PAREN:
4184     case CPP_CLOSE_SQUARE:
4185       /* Avoid infinite loop in error recovery:
4186          c_parser_skip_until_found stops at a closing nesting
4187          delimiter without consuming it, but here we need to consume
4188          it to proceed further.  */
4189       c_parser_error (parser, "expected statement");
4190       c_parser_consume_token (parser);
4191       break;
4192     case CPP_PRAGMA:
4193       c_parser_pragma (parser, pragma_stmt);
4194       break;
4195     default:
4196     expr_stmt:
4197       stmt = c_finish_expr_stmt (loc, c_parser_expression_conv (parser).value);
4198     expect_semicolon:
4199       c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
4200       break;
4201     }
4202   /* Two cases cannot and do not have line numbers associated: If stmt
4203      is degenerate, such as "2;", then stmt is an INTEGER_CST, which
4204      cannot hold line numbers.  But that's OK because the statement
4205      will either be changed to a MODIFY_EXPR during gimplification of
4206      the statement expr, or discarded.  If stmt was compound, but
4207      without new variables, we will have skipped the creation of a
4208      BIND and will have a bare STATEMENT_LIST.  But that's OK because
4209      (recursively) all of the component statements should already have
4210      line numbers assigned.  ??? Can we discard no-op statements
4211      earlier?  */
4212   if (CAN_HAVE_LOCATION_P (stmt)
4213       && EXPR_LOCATION (stmt) == UNKNOWN_LOCATION)
4214     SET_EXPR_LOCATION (stmt, loc);
4215
4216   parser->in_if_block = in_if_block;
4217 }
4218
4219 /* Parse the condition from an if, do, while or for statements.  */
4220
4221 static tree
4222 c_parser_condition (c_parser *parser)
4223 {
4224   location_t loc = c_parser_peek_token (parser)->location;
4225   tree cond;
4226   cond = c_parser_expression_conv (parser).value;
4227   cond = c_objc_common_truthvalue_conversion (loc, cond);
4228   cond = c_fully_fold (cond, false, NULL);
4229   if (warn_sequence_point)
4230     verify_sequence_points (cond);
4231   return cond;
4232 }
4233
4234 /* Parse a parenthesized condition from an if, do or while statement.
4235
4236    condition:
4237      ( expression )
4238 */
4239 static tree
4240 c_parser_paren_condition (c_parser *parser)
4241 {
4242   tree cond;
4243   if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4244     return error_mark_node;
4245   cond = c_parser_condition (parser);
4246   c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
4247   return cond;
4248 }
4249
4250 /* Parse a statement which is a block in C99.  */
4251
4252 static tree
4253 c_parser_c99_block_statement (c_parser *parser)
4254 {
4255   tree block = c_begin_compound_stmt (flag_isoc99);
4256   location_t loc = c_parser_peek_token (parser)->location;
4257   c_parser_statement (parser);
4258   return c_end_compound_stmt (loc, block, flag_isoc99);
4259 }
4260
4261 /* Parse the body of an if statement.  This is just parsing a
4262    statement but (a) it is a block in C99, (b) we track whether the
4263    body is an if statement for the sake of -Wparentheses warnings, (c)
4264    we handle an empty body specially for the sake of -Wempty-body
4265    warnings, and (d) we call parser_compound_statement directly
4266    because c_parser_statement_after_labels resets
4267    parser->in_if_block.  */
4268
4269 static tree
4270 c_parser_if_body (c_parser *parser, bool *if_p)
4271 {
4272   tree block = c_begin_compound_stmt (flag_isoc99);
4273   location_t body_loc = c_parser_peek_token (parser)->location;
4274   while (c_parser_next_token_is_keyword (parser, RID_CASE)
4275          || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
4276          || (c_parser_next_token_is (parser, CPP_NAME)
4277              && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
4278     c_parser_label (parser);
4279   *if_p = c_parser_next_token_is_keyword (parser, RID_IF);
4280   if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4281     {
4282       location_t loc = c_parser_peek_token (parser)->location;
4283       add_stmt (build_empty_stmt (loc));
4284       c_parser_consume_token (parser);
4285       if (!c_parser_next_token_is_keyword (parser, RID_ELSE))
4286         warning_at (loc, OPT_Wempty_body,
4287                     "suggest braces around empty body in an %<if%> statement");
4288     }
4289   else if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
4290     add_stmt (c_parser_compound_statement (parser));
4291   else
4292     c_parser_statement_after_labels (parser);
4293   return c_end_compound_stmt (body_loc, block, flag_isoc99);
4294 }
4295
4296 /* Parse the else body of an if statement.  This is just parsing a
4297    statement but (a) it is a block in C99, (b) we handle an empty body
4298    specially for the sake of -Wempty-body warnings.  */
4299
4300 static tree
4301 c_parser_else_body (c_parser *parser)
4302 {
4303   location_t else_loc = c_parser_peek_token (parser)->location;
4304   tree block = c_begin_compound_stmt (flag_isoc99);
4305   while (c_parser_next_token_is_keyword (parser, RID_CASE)
4306          || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
4307          || (c_parser_next_token_is (parser, CPP_NAME)
4308              && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
4309     c_parser_label (parser);
4310   if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4311     {
4312       location_t loc = c_parser_peek_token (parser)->location;
4313       warning_at (loc,
4314                   OPT_Wempty_body,
4315                  "suggest braces around empty body in an %<else%> statement");
4316       add_stmt (build_empty_stmt (loc));
4317       c_parser_consume_token (parser);
4318     }
4319   else
4320     c_parser_statement_after_labels (parser);
4321   return c_end_compound_stmt (else_loc, block, flag_isoc99);
4322 }
4323
4324 /* Parse an if statement (C90 6.6.4, C99 6.8.4).
4325
4326    if-statement:
4327      if ( expression ) statement
4328      if ( expression ) statement else statement
4329 */
4330
4331 static void
4332 c_parser_if_statement (c_parser *parser)
4333 {
4334   tree block;
4335   location_t loc;
4336   tree cond;
4337   bool first_if = false;
4338   tree first_body, second_body;
4339   bool in_if_block;
4340
4341   gcc_assert (c_parser_next_token_is_keyword (parser, RID_IF));
4342   c_parser_consume_token (parser);
4343   block = c_begin_compound_stmt (flag_isoc99);
4344   loc = c_parser_peek_token (parser)->location;
4345   cond = c_parser_paren_condition (parser);
4346   in_if_block = parser->in_if_block;
4347   parser->in_if_block = true;
4348   first_body = c_parser_if_body (parser, &first_if);
4349   parser->in_if_block = in_if_block;
4350   if (c_parser_next_token_is_keyword (parser, RID_ELSE))
4351     {
4352       c_parser_consume_token (parser);
4353       second_body = c_parser_else_body (parser);
4354     }
4355   else
4356     second_body = NULL_TREE;
4357   c_finish_if_stmt (loc, cond, first_body, second_body, first_if);
4358   add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
4359 }
4360
4361 /* Parse a switch statement (C90 6.6.4, C99 6.8.4).
4362
4363    switch-statement:
4364      switch (expression) statement
4365 */
4366
4367 static void
4368 c_parser_switch_statement (c_parser *parser)
4369 {
4370   tree block, expr, body, save_break;
4371   location_t switch_loc = c_parser_peek_token (parser)->location;
4372   location_t switch_cond_loc;
4373   gcc_assert (c_parser_next_token_is_keyword (parser, RID_SWITCH));
4374   c_parser_consume_token (parser);
4375   block = c_begin_compound_stmt (flag_isoc99);
4376   if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4377     {
4378       switch_cond_loc = c_parser_peek_token (parser)->location;
4379       expr = c_parser_expression (parser).value;
4380       c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
4381     }
4382   else
4383     {
4384       switch_cond_loc = UNKNOWN_LOCATION;
4385       expr = error_mark_node;
4386     }
4387   c_start_case (switch_loc, switch_cond_loc, expr);
4388   save_break = c_break_label;
4389   c_break_label = NULL_TREE;
4390   body = c_parser_c99_block_statement (parser);
4391   c_finish_case (body);
4392   if (c_break_label)
4393     {
4394       location_t here = c_parser_peek_token (parser)->location;
4395       tree t = build1 (LABEL_EXPR, void_type_node, c_break_label);
4396       SET_EXPR_LOCATION (t, here);
4397       add_stmt (t);
4398     }
4399   c_break_label = save_break;
4400   add_stmt (c_end_compound_stmt (switch_loc, block, flag_isoc99));
4401 }
4402
4403 /* Parse a while statement (C90 6.6.5, C99 6.8.5).
4404
4405    while-statement:
4406       while (expression) statement
4407 */
4408
4409 static void
4410 c_parser_while_statement (c_parser *parser)
4411 {
4412   tree block, cond, body, save_break, save_cont;
4413   location_t loc;
4414   gcc_assert (c_parser_next_token_is_keyword (parser, RID_WHILE));
4415   c_parser_consume_token (parser);
4416   block = c_begin_compound_stmt (flag_isoc99);
4417   loc = c_parser_peek_token (parser)->location;
4418   cond = c_parser_paren_condition (parser);
4419   save_break = c_break_label;
4420   c_break_label = NULL_TREE;
4421   save_cont = c_cont_label;
4422   c_cont_label = NULL_TREE;
4423   body = c_parser_c99_block_statement (parser);
4424   c_finish_loop (loc, cond, NULL, body, c_break_label, c_cont_label, true);
4425   add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
4426   c_break_label = save_break;
4427   c_cont_label = save_cont;
4428 }
4429
4430 /* Parse a do statement (C90 6.6.5, C99 6.8.5).
4431
4432    do-statement:
4433      do statement while ( expression ) ;
4434 */
4435
4436 static void
4437 c_parser_do_statement (c_parser *parser)
4438 {
4439   tree block, cond, body, save_break, save_cont, new_break, new_cont;
4440   location_t loc;
4441   gcc_assert (c_parser_next_token_is_keyword (parser, RID_DO));
4442   c_parser_consume_token (parser);
4443   if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4444     warning_at (c_parser_peek_token (parser)->location,
4445                 OPT_Wempty_body,
4446                 "suggest braces around empty body in %<do%> statement");
4447   block = c_begin_compound_stmt (flag_isoc99);
4448   loc = c_parser_peek_token (parser)->location;
4449   save_break = c_break_label;
4450   c_break_label = NULL_TREE;
4451   save_cont = c_cont_label;
4452   c_cont_label = NULL_TREE;
4453   body = c_parser_c99_block_statement (parser);
4454   c_parser_require_keyword (parser, RID_WHILE, "expected %<while%>");
4455   new_break = c_break_label;
4456   c_break_label = save_break;
4457   new_cont = c_cont_label;
4458   c_cont_label = save_cont;
4459   cond = c_parser_paren_condition (parser);
4460   if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
4461     c_parser_skip_to_end_of_block_or_statement (parser);
4462   c_finish_loop (loc, cond, NULL, body, new_break, new_cont, false);
4463   add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
4464 }
4465
4466 /* Parse a for statement (C90 6.6.5, C99 6.8.5).
4467
4468    for-statement:
4469      for ( expression[opt] ; expression[opt] ; expression[opt] ) statement
4470      for ( nested-declaration expression[opt] ; expression[opt] ) statement
4471
4472    The form with a declaration is new in C99.
4473
4474    ??? In accordance with the old parser, the declaration may be a
4475    nested function, which is then rejected in check_for_loop_decls,
4476    but does it make any sense for this to be included in the grammar?
4477    Note in particular that the nested function does not include a
4478    trailing ';', whereas the "declaration" production includes one.
4479    Also, can we reject bad declarations earlier and cheaper than
4480    check_for_loop_decls?
4481
4482    In Objective-C, there are two additional variants:
4483
4484    foreach-statement:
4485      for ( expression in expresssion ) statement
4486      for ( declaration in expression ) statement
4487
4488    This is inconsistent with C, because the second variant is allowed
4489    even if c99 is not enabled.
4490
4491    The rest of the comment documents these Objective-C foreach-statement.
4492
4493    Here is the canonical example of the first variant:
4494     for (object in array)    { do something with object }
4495    we call the first expression ("object") the "object_expression" and 
4496    the second expression ("array") the "collection_expression".
4497    object_expression must be an lvalue of type "id" (a generic Objective-C
4498    object) because the loop works by assigning to object_expression the
4499    various objects from the collection_expression.  collection_expression
4500    must evaluate to something of type "id" which responds to the method
4501    countByEnumeratingWithState:objects:count:.
4502
4503    The canonical example of the second variant is:
4504     for (id object in array)    { do something with object }
4505    which is completely equivalent to
4506     {
4507       id object;
4508       for (object in array) { do something with object }
4509     }
4510    Note that initizializing 'object' in some way (eg, "for ((object =
4511    xxx) in array) { do something with object }") is possibly
4512    technically valid, but completely pointless as 'object' will be
4513    assigned to something else as soon as the loop starts.  We should
4514    most likely reject it (TODO).
4515
4516    The beginning of the Objective-C foreach-statement looks exactly
4517    like the beginning of the for-statement, and we can tell it is a
4518    foreach-statement only because the initial declaration or
4519    expression is terminated by 'in' instead of ';'.
4520 */
4521
4522 static void
4523 c_parser_for_statement (c_parser *parser)
4524 {
4525   tree block, cond, incr, save_break, save_cont, body;
4526   /* The following are only used when parsing an ObjC foreach statement.  */
4527   tree object_expression, collection_expression;
4528   location_t loc = c_parser_peek_token (parser)->location;
4529   location_t for_loc = c_parser_peek_token (parser)->location;
4530   bool is_foreach_statement = false;
4531   gcc_assert (c_parser_next_token_is_keyword (parser, RID_FOR));
4532   c_parser_consume_token (parser);
4533   /* Open a compound statement in Objective-C as well, just in case this is
4534      as foreach expression.  */
4535   block = c_begin_compound_stmt (flag_isoc99 || c_dialect_objc ());
4536   if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4537     {
4538       /* Parse the initialization declaration or expression.  */
4539       cond = error_mark_node;
4540       object_expression = error_mark_node;
4541       /* Initializing incr should not be necessary, but it avoids
4542          bogus warnings of uninitialized uses.  */
4543       incr = error_mark_node;
4544
4545       if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4546         {
4547           c_parser_consume_token (parser);
4548           c_finish_expr_stmt (loc, NULL_TREE);
4549         }
4550       else if (c_parser_next_token_starts_declaration (parser))
4551         {
4552           parser->objc_could_be_foreach_context = true;
4553           c_parser_declaration_or_fndef (parser, true, true, true, true, true, 
4554                                          &object_expression);
4555           parser->objc_could_be_foreach_context = false;
4556           
4557           if (c_parser_next_token_is_keyword (parser, RID_IN))
4558             {
4559               c_parser_consume_token (parser);
4560               is_foreach_statement = true;
4561               if (check_for_loop_decls (for_loc, true) == NULL_TREE)
4562                 c_parser_error (parser, "multiple iterating variables in fast enumeration");
4563             }
4564           else
4565             check_for_loop_decls (for_loc, flag_isoc99);
4566         }
4567       else if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
4568         {
4569           /* __extension__ can start a declaration, but is also an
4570              unary operator that can start an expression.  Consume all
4571              but the last of a possible series of __extension__ to
4572              determine which.  */
4573           while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
4574                  && (c_parser_peek_2nd_token (parser)->keyword
4575                      == RID_EXTENSION))
4576             c_parser_consume_token (parser);
4577           if (c_token_starts_declaration (c_parser_peek_2nd_token (parser)))
4578             {
4579               int ext;
4580               ext = disable_extension_diagnostics ();
4581               c_parser_consume_token (parser);
4582               parser->objc_could_be_foreach_context = true;
4583               c_parser_declaration_or_fndef (parser, true, true, true, true,
4584                                              true, &object_expression);
4585               parser->objc_could_be_foreach_context = false;
4586               
4587               restore_extension_diagnostics (ext);
4588               if (c_parser_next_token_is_keyword (parser, RID_IN))
4589                 {
4590                   c_parser_consume_token (parser);
4591                   is_foreach_statement = true;
4592                   if (check_for_loop_decls (for_loc, true) == NULL_TREE)
4593                     c_parser_error (parser, "multiple iterating variables in fast enumeration");
4594                 }
4595               else
4596                 check_for_loop_decls (for_loc, flag_isoc99);
4597             }
4598           else
4599             goto init_expr;
4600         }
4601       else
4602         {
4603         init_expr:
4604           {
4605             tree init_expression;
4606             parser->objc_could_be_foreach_context = true;
4607             init_expression = c_parser_expression (parser).value;
4608             parser->objc_could_be_foreach_context = false;
4609             if (c_parser_next_token_is_keyword (parser, RID_IN))
4610               {
4611                 c_parser_consume_token (parser);
4612                 is_foreach_statement = true;
4613                 if (! lvalue_p (init_expression))
4614                   c_parser_error (parser, "invalid iterating variable in fast enumeration");
4615                 object_expression = c_process_expr_stmt (loc, init_expression);
4616
4617               }
4618             else
4619               {
4620                 c_finish_expr_stmt (loc, init_expression);
4621                 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
4622               }
4623           }
4624         }
4625       /* Parse the loop condition.  In the case of a foreach
4626          statement, there is no loop condition.  */
4627       if (!is_foreach_statement)
4628         {
4629           if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4630             {
4631               c_parser_consume_token (parser);
4632               cond = NULL_TREE;
4633             }
4634           else
4635             {
4636               cond = c_parser_condition (parser);
4637               c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
4638             }
4639         }
4640       /* Parse the increment expression (the third expression in a
4641          for-statement).  In the case of a foreach-statement, this is
4642          the expression that follows the 'in'.  */
4643       if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4644         {
4645           if (is_foreach_statement)
4646             {
4647               c_parser_error (parser, "missing collection in fast enumeration");
4648               collection_expression = error_mark_node;
4649             }
4650           else
4651             incr = c_process_expr_stmt (loc, NULL_TREE);
4652         }
4653       else
4654         {
4655           if (is_foreach_statement)
4656             collection_expression = c_process_expr_stmt (loc, c_parser_expression (parser).value);
4657           else
4658             incr = c_process_expr_stmt (loc, c_parser_expression (parser).value);
4659         }
4660       c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
4661     }
4662   else
4663     {
4664       cond = error_mark_node;
4665       incr = error_mark_node;
4666     }
4667   save_break = c_break_label;
4668   c_break_label = NULL_TREE;
4669   save_cont = c_cont_label;
4670   c_cont_label = NULL_TREE;
4671   body = c_parser_c99_block_statement (parser);
4672   if (is_foreach_statement)
4673     objc_finish_foreach_loop (loc, object_expression, collection_expression, body, c_break_label, c_cont_label);
4674   else
4675     c_finish_loop (loc, cond, incr, body, c_break_label, c_cont_label, true);
4676   add_stmt (c_end_compound_stmt (loc, block, flag_isoc99 || c_dialect_objc ()));
4677   c_break_label = save_break;
4678   c_cont_label = save_cont;
4679 }
4680
4681 /* Parse an asm statement, a GNU extension.  This is a full-blown asm
4682    statement with inputs, outputs, clobbers, and volatile tag
4683    allowed.
4684
4685    asm-statement:
4686      asm type-qualifier[opt] ( asm-argument ) ;
4687      asm type-qualifier[opt] goto ( asm-goto-argument ) ;
4688
4689    asm-argument:
4690      asm-string-literal
4691      asm-string-literal : asm-operands[opt]
4692      asm-string-literal : asm-operands[opt] : asm-operands[opt]
4693      asm-string-literal : asm-operands[opt] : asm-operands[opt] : asm-clobbers[opt]
4694
4695    asm-goto-argument:
4696      asm-string-literal : : asm-operands[opt] : asm-clobbers[opt] \
4697        : asm-goto-operands
4698
4699    Qualifiers other than volatile are accepted in the syntax but
4700    warned for.  */
4701
4702 static tree
4703 c_parser_asm_statement (c_parser *parser)
4704 {
4705   tree quals, str, outputs, inputs, clobbers, labels, ret;
4706   bool simple, is_goto;
4707   location_t asm_loc = c_parser_peek_token (parser)->location;
4708   int section, nsections;
4709
4710   gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM));
4711   c_parser_consume_token (parser);
4712   if (c_parser_next_token_is_keyword (parser, RID_VOLATILE))
4713     {
4714       quals = c_parser_peek_token (parser)->value;
4715       c_parser_consume_token (parser);
4716     }
4717   else if (c_parser_next_token_is_keyword (parser, RID_CONST)
4718            || c_parser_next_token_is_keyword (parser, RID_RESTRICT))
4719     {
4720       warning_at (c_parser_peek_token (parser)->location,
4721                   0,
4722                   "%E qualifier ignored on asm",
4723                   c_parser_peek_token (parser)->value);
4724       quals = NULL_TREE;
4725       c_parser_consume_token (parser);
4726     }
4727   else
4728     quals = NULL_TREE;
4729
4730   is_goto = false;
4731   if (c_parser_next_token_is_keyword (parser, RID_GOTO))
4732     {
4733       c_parser_consume_token (parser);
4734       is_goto = true;
4735     }
4736
4737   /* ??? Follow the C++ parser rather than using the
4738      lex_untranslated_string kludge.  */
4739   parser->lex_untranslated_string = true;
4740   ret = NULL;
4741
4742   if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4743     goto error;
4744
4745   str = c_parser_asm_string_literal (parser);
4746   if (str == NULL_TREE)
4747     goto error_close_paren;
4748
4749   simple = true;
4750   outputs = NULL_TREE;
4751   inputs = NULL_TREE;
4752   clobbers = NULL_TREE;
4753   labels = NULL_TREE;
4754
4755   if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN) && !is_goto)
4756     goto done_asm;
4757
4758   /* Parse each colon-delimited section of operands.  */
4759   nsections = 3 + is_goto;
4760   for (section = 0; section < nsections; ++section)
4761     {
4762       if (!c_parser_require (parser, CPP_COLON,
4763                              is_goto
4764                              ? "expected %<:%>"
4765                              : "expected %<:%> or %<)%>"))
4766         goto error_close_paren;
4767
4768       /* Once past any colon, we're no longer a simple asm.  */
4769       simple = false;
4770
4771       if ((!c_parser_next_token_is (parser, CPP_COLON)
4772            && !c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4773           || section == 3)
4774         switch (section)
4775           {
4776           case 0:
4777             /* For asm goto, we don't allow output operands, but reserve
4778                the slot for a future extension that does allow them.  */
4779             if (!is_goto)
4780               outputs = c_parser_asm_operands (parser, false);
4781             break;
4782           case 1:
4783             inputs = c_parser_asm_operands (parser, true);
4784             break;
4785           case 2:
4786             clobbers = c_parser_asm_clobbers (parser);
4787             break;
4788           case 3:
4789             labels = c_parser_asm_goto_operands (parser);
4790             break;
4791           default:
4792             gcc_unreachable ();
4793           }
4794
4795       if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN) && !is_goto)
4796         goto done_asm;
4797     }
4798
4799  done_asm:
4800   if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
4801     {
4802       c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4803       goto error;
4804     }
4805
4806   if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
4807     c_parser_skip_to_end_of_block_or_statement (parser);
4808
4809   ret = build_asm_stmt (quals, build_asm_expr (asm_loc, str, outputs, inputs,
4810                                                clobbers, labels, simple));
4811
4812  error:
4813   parser->lex_untranslated_string = false;
4814   return ret;
4815
4816  error_close_paren:
4817   c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4818   goto error;
4819 }
4820
4821 /* Parse asm operands, a GNU extension.  If CONVERT_P (for inputs but
4822    not outputs), apply the default conversion of functions and arrays
4823    to pointers.
4824
4825    asm-operands:
4826      asm-operand
4827      asm-operands , asm-operand
4828
4829    asm-operand:
4830      asm-string-literal ( expression )
4831      [ identifier ] asm-string-literal ( expression )
4832 */
4833
4834 static tree
4835 c_parser_asm_operands (c_parser *parser, bool convert_p)
4836 {
4837   tree list = NULL_TREE;
4838   location_t loc;
4839   while (true)
4840     {
4841       tree name, str;
4842       struct c_expr expr;
4843       if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
4844         {
4845           c_parser_consume_token (parser);
4846           if (c_parser_next_token_is (parser, CPP_NAME))
4847             {
4848               tree id = c_parser_peek_token (parser)->value;
4849               c_parser_consume_token (parser);
4850               name = build_string (IDENTIFIER_LENGTH (id),
4851                                    IDENTIFIER_POINTER (id));
4852             }
4853           else
4854             {
4855               c_parser_error (parser, "expected identifier");
4856               c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
4857               return NULL_TREE;
4858             }
4859           c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
4860                                      "expected %<]%>");
4861         }
4862       else
4863         name = NULL_TREE;
4864       str = c_parser_asm_string_literal (parser);
4865       if (str == NULL_TREE)
4866         return NULL_TREE;
4867       parser->lex_untranslated_string = false;
4868       if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4869         {
4870           parser->lex_untranslated_string = true;
4871           return NULL_TREE;
4872         }
4873       loc = c_parser_peek_token (parser)->location;
4874       expr = c_parser_expression (parser);
4875       mark_exp_read (expr.value);
4876       if (convert_p)
4877         expr = default_function_array_conversion (loc, expr);
4878       expr.value = c_fully_fold (expr.value, false, NULL);
4879       parser->lex_untranslated_string = true;
4880       if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
4881         {
4882           c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4883           return NULL_TREE;
4884         }
4885       list = chainon (list, build_tree_list (build_tree_list (name, str),
4886                                              expr.value));
4887       if (c_parser_next_token_is (parser, CPP_COMMA))
4888         c_parser_consume_token (parser);
4889       else
4890         break;
4891     }
4892   return list;
4893 }
4894
4895 /* Parse asm clobbers, a GNU extension.
4896
4897    asm-clobbers:
4898      asm-string-literal
4899      asm-clobbers , asm-string-literal
4900 */
4901
4902 static tree
4903 c_parser_asm_clobbers (c_parser *parser)
4904 {
4905   tree list = NULL_TREE;
4906   while (true)
4907     {
4908       tree str = c_parser_asm_string_literal (parser);
4909       if (str)
4910         list = tree_cons (NULL_TREE, str, list);
4911       else
4912         return NULL_TREE;
4913       if (c_parser_next_token_is (parser, CPP_COMMA))
4914         c_parser_consume_token (parser);
4915       else
4916         break;
4917     }
4918   return list;
4919 }
4920
4921 /* Parse asm goto labels, a GNU extension.
4922
4923    asm-goto-operands:
4924      identifier
4925      asm-goto-operands , identifier
4926 */
4927
4928 static tree
4929 c_parser_asm_goto_operands (c_parser *parser)
4930 {
4931   tree list = NULL_TREE;
4932   while (true)
4933     {
4934       tree name, label;
4935
4936       if (c_parser_next_token_is (parser, CPP_NAME))
4937         {
4938           c_token *tok = c_parser_peek_token (parser);
4939           name = tok->value;
4940           label = lookup_label_for_goto (tok->location, name);
4941           c_parser_consume_token (parser);
4942           TREE_USED (label) = 1;
4943         }
4944       else
4945         {
4946           c_parser_error (parser, "expected identifier");
4947           return NULL_TREE;
4948         }
4949
4950       name = build_string (IDENTIFIER_LENGTH (name),
4951                            IDENTIFIER_POINTER (name));
4952       list = tree_cons (name, label, list);
4953       if (c_parser_next_token_is (parser, CPP_COMMA))
4954         c_parser_consume_token (parser);
4955       else
4956         return nreverse (list);
4957     }
4958 }
4959
4960 /* Parse an expression other than a compound expression; that is, an
4961    assignment expression (C90 6.3.16, C99 6.5.16).  If AFTER is not
4962    NULL then it is an Objective-C message expression which is the
4963    primary-expression starting the expression as an initializer.
4964
4965    assignment-expression:
4966      conditional-expression
4967      unary-expression assignment-operator assignment-expression
4968
4969    assignment-operator: one of
4970      = *= /= %= += -= <<= >>= &= ^= |=
4971
4972    In GNU C we accept any conditional expression on the LHS and
4973    diagnose the invalid lvalue rather than producing a syntax
4974    error.  */
4975
4976 static struct c_expr
4977 c_parser_expr_no_commas (c_parser *parser, struct c_expr *after)
4978 {
4979   struct c_expr lhs, rhs, ret;
4980   enum tree_code code;
4981   location_t op_location, exp_location;
4982   gcc_assert (!after || c_dialect_objc ());
4983   lhs = c_parser_conditional_expression (parser, after);
4984   op_location = c_parser_peek_token (parser)->location;
4985   switch (c_parser_peek_token (parser)->type)
4986     {
4987     case CPP_EQ:
4988       code = NOP_EXPR;
4989       break;
4990     case CPP_MULT_EQ:
4991       code = MULT_EXPR;
4992       break;
4993     case CPP_DIV_EQ:
4994       code = TRUNC_DIV_EXPR;
4995       break;
4996     case CPP_MOD_EQ:
4997       code = TRUNC_MOD_EXPR;
4998       break;
4999     case CPP_PLUS_EQ:
5000       code = PLUS_EXPR;
5001       break;
5002     case CPP_MINUS_EQ:
5003       code = MINUS_EXPR;
5004       break;
5005     case CPP_LSHIFT_EQ:
5006       code = LSHIFT_EXPR;
5007       break;
5008     case CPP_RSHIFT_EQ:
5009       code = RSHIFT_EXPR;
5010       break;
5011     case CPP_AND_EQ:
5012       code = BIT_AND_EXPR;
5013       break;
5014     case CPP_XOR_EQ:
5015       code = BIT_XOR_EXPR;
5016       break;
5017     case CPP_OR_EQ:
5018       code = BIT_IOR_EXPR;
5019       break;
5020     default:
5021       return lhs;
5022     }
5023   c_parser_consume_token (parser);
5024   exp_location = c_parser_peek_token (parser)->location;
5025   rhs = c_parser_expr_no_commas (parser, NULL);
5026   rhs = default_function_array_read_conversion (exp_location, rhs);
5027   ret.value = build_modify_expr (op_location, lhs.value, lhs.original_type,
5028                                  code, exp_location, rhs.value,
5029                                  rhs.original_type);
5030   if (code == NOP_EXPR)
5031     ret.original_code = MODIFY_EXPR;
5032   else
5033     {
5034       TREE_NO_WARNING (ret.value) = 1;
5035       ret.original_code = ERROR_MARK;
5036     }
5037   ret.original_type = NULL;
5038   return ret;
5039 }
5040
5041 /* Parse a conditional expression (C90 6.3.15, C99 6.5.15).  If AFTER
5042    is not NULL then it is an Objective-C message expression which is
5043    the primary-expression starting the expression as an initializer.
5044
5045    conditional-expression:
5046      logical-OR-expression
5047      logical-OR-expression ? expression : conditional-expression
5048
5049    GNU extensions:
5050
5051    conditional-expression:
5052      logical-OR-expression ? : conditional-expression
5053 */
5054
5055 static struct c_expr
5056 c_parser_conditional_expression (c_parser *parser, struct c_expr *after)
5057 {
5058   struct c_expr cond, exp1, exp2, ret;
5059   location_t cond_loc, colon_loc, middle_loc;
5060
5061   gcc_assert (!after || c_dialect_objc ());
5062
5063   cond = c_parser_binary_expression (parser, after);
5064
5065   if (c_parser_next_token_is_not (parser, CPP_QUERY))
5066     return cond;
5067   cond_loc = c_parser_peek_token (parser)->location;
5068   cond = default_function_array_read_conversion (cond_loc, cond);
5069   c_parser_consume_token (parser);
5070   if (c_parser_next_token_is (parser, CPP_COLON))
5071     {
5072       tree eptype = NULL_TREE;
5073
5074       middle_loc = c_parser_peek_token (parser)->location;
5075       pedwarn (middle_loc, OPT_pedantic, 
5076                "ISO C forbids omitting the middle term of a ?: expression");
5077       warn_for_omitted_condop (middle_loc, cond.value);
5078       if (TREE_CODE (cond.value) == EXCESS_PRECISION_EXPR)
5079         {
5080           eptype = TREE_TYPE (cond.value);
5081           cond.value = TREE_OPERAND (cond.value, 0);
5082         }
5083       /* Make sure first operand is calculated only once.  */
5084       exp1.value = c_save_expr (default_conversion (cond.value));
5085       if (eptype)
5086         exp1.value = build1 (EXCESS_PRECISION_EXPR, eptype, exp1.value);
5087       exp1.original_type = NULL;
5088       cond.value = c_objc_common_truthvalue_conversion (cond_loc, exp1.value);
5089       c_inhibit_evaluation_warnings += cond.value == truthvalue_true_node;
5090     }
5091   else
5092     {
5093       cond.value
5094         = c_objc_common_truthvalue_conversion
5095         (cond_loc, default_conversion (cond.value));
5096       c_inhibit_evaluation_warnings += cond.value == truthvalue_false_node;
5097       exp1 = c_parser_expression_conv (parser);
5098       mark_exp_read (exp1.value);
5099       c_inhibit_evaluation_warnings +=
5100         ((cond.value == truthvalue_true_node)
5101          - (cond.value == truthvalue_false_node));
5102     }
5103
5104   colon_loc = c_parser_peek_token (parser)->location;
5105   if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
5106     {
5107       c_inhibit_evaluation_warnings -= cond.value == truthvalue_true_node;
5108       ret.value = error_mark_node;
5109       ret.original_code = ERROR_MARK;
5110       ret.original_type = NULL;
5111       return ret;
5112     }
5113   {
5114     location_t exp2_loc = c_parser_peek_token (parser)->location;
5115     exp2 = c_parser_conditional_expression (parser, NULL);
5116     exp2 = default_function_array_read_conversion (exp2_loc, exp2);
5117   }
5118   c_inhibit_evaluation_warnings -= cond.value == truthvalue_true_node;
5119   ret.value = build_conditional_expr (colon_loc, cond.value,
5120                                       cond.original_code == C_MAYBE_CONST_EXPR,
5121                                       exp1.value, exp1.original_type,
5122                                       exp2.value, exp2.original_type);
5123   ret.original_code = ERROR_MARK;
5124   if (exp1.value == error_mark_node || exp2.value == error_mark_node)
5125     ret.original_type = NULL;
5126   else
5127     {
5128       tree t1, t2;
5129
5130       /* If both sides are enum type, the default conversion will have
5131          made the type of the result be an integer type.  We want to
5132          remember the enum types we started with.  */
5133       t1 = exp1.original_type ? exp1.original_type : TREE_TYPE (exp1.value);
5134       t2 = exp2.original_type ? exp2.original_type : TREE_TYPE (exp2.value);
5135       ret.original_type = ((t1 != error_mark_node
5136                             && t2 != error_mark_node
5137                             && (TYPE_MAIN_VARIANT (t1)
5138                                 == TYPE_MAIN_VARIANT (t2)))
5139                            ? t1
5140                            : NULL);
5141     }
5142   return ret;
5143 }
5144
5145 /* Parse a binary expression; that is, a logical-OR-expression (C90
5146    6.3.5-6.3.14, C99 6.5.5-6.5.14).  If AFTER is not NULL then it is
5147    an Objective-C message expression which is the primary-expression
5148    starting the expression as an initializer.
5149
5150    multiplicative-expression:
5151      cast-expression
5152      multiplicative-expression * cast-expression
5153      multiplicative-expression / cast-expression
5154      multiplicative-expression % cast-expression
5155
5156    additive-expression:
5157      multiplicative-expression
5158      additive-expression + multiplicative-expression
5159      additive-expression - multiplicative-expression
5160
5161    shift-expression:
5162      additive-expression
5163      shift-expression << additive-expression
5164      shift-expression >> additive-expression
5165
5166    relational-expression:
5167      shift-expression
5168      relational-expression < shift-expression
5169      relational-expression > shift-expression
5170      relational-expression <= shift-expression
5171      relational-expression >= shift-expression
5172
5173    equality-expression:
5174      relational-expression
5175      equality-expression == relational-expression
5176      equality-expression != relational-expression
5177
5178    AND-expression:
5179      equality-expression
5180      AND-expression & equality-expression
5181
5182    exclusive-OR-expression:
5183      AND-expression
5184      exclusive-OR-expression ^ AND-expression
5185
5186    inclusive-OR-expression:
5187      exclusive-OR-expression
5188      inclusive-OR-expression | exclusive-OR-expression
5189
5190    logical-AND-expression:
5191      inclusive-OR-expression
5192      logical-AND-expression && inclusive-OR-expression
5193
5194    logical-OR-expression:
5195      logical-AND-expression
5196      logical-OR-expression || logical-AND-expression
5197 */
5198
5199 static struct c_expr
5200 c_parser_binary_expression (c_parser *parser, struct c_expr *after)
5201 {
5202   /* A binary expression is parsed using operator-precedence parsing,
5203      with the operands being cast expressions.  All the binary
5204      operators are left-associative.  Thus a binary expression is of
5205      form:
5206
5207      E0 op1 E1 op2 E2 ...
5208
5209      which we represent on a stack.  On the stack, the precedence
5210      levels are strictly increasing.  When a new operator is
5211      encountered of higher precedence than that at the top of the
5212      stack, it is pushed; its LHS is the top expression, and its RHS
5213      is everything parsed until it is popped.  When a new operator is
5214      encountered with precedence less than or equal to that at the top
5215      of the stack, triples E[i-1] op[i] E[i] are popped and replaced
5216      by the result of the operation until the operator at the top of
5217      the stack has lower precedence than the new operator or there is
5218      only one element on the stack; then the top expression is the LHS
5219      of the new operator.  In the case of logical AND and OR
5220      expressions, we also need to adjust c_inhibit_evaluation_warnings
5221      as appropriate when the operators are pushed and popped.  */
5222
5223   /* The precedence levels, where 0 is a dummy lowest level used for
5224      the bottom of the stack.  */
5225   enum prec {
5226     PREC_NONE,
5227     PREC_LOGOR,
5228     PREC_LOGAND,
5229     PREC_BITOR,
5230     PREC_BITXOR,
5231     PREC_BITAND,
5232     PREC_EQ,
5233     PREC_REL,
5234     PREC_SHIFT,
5235     PREC_ADD,
5236     PREC_MULT,
5237     NUM_PRECS
5238   };
5239   struct {
5240     /* The expression at this stack level.  */
5241     struct c_expr expr;
5242     /* The precedence of the operator on its left, PREC_NONE at the
5243        bottom of the stack.  */
5244     enum prec prec;
5245     /* The operation on its left.  */
5246     enum tree_code op;
5247     /* The source location of this operation.  */
5248     location_t loc;
5249   } stack[NUM_PRECS];
5250   int sp;
5251   /* Location of the binary operator.  */
5252   location_t binary_loc = UNKNOWN_LOCATION;  /* Quiet warning.  */
5253 #define POP                                                                   \
5254   do {                                                                        \
5255     switch (stack[sp].op)                                                     \
5256       {                                                                       \
5257       case TRUTH_ANDIF_EXPR:                                                  \
5258         c_inhibit_evaluation_warnings -= (stack[sp - 1].expr.value            \
5259                                           == truthvalue_false_node);          \
5260         break;                                                                \
5261       case TRUTH_ORIF_EXPR:                                                   \
5262         c_inhibit_evaluation_warnings -= (stack[sp - 1].expr.value            \
5263                                           == truthvalue_true_node);           \
5264         break;                                                                \
5265       default:                                                                \
5266         break;                                                                \
5267       }                                                                       \
5268     stack[sp - 1].expr                                                        \
5269       = default_function_array_read_conversion (stack[sp - 1].loc,            \
5270                                                 stack[sp - 1].expr);          \
5271     stack[sp].expr                                                            \
5272       = default_function_array_read_conversion (stack[sp].loc,                \
5273                                                 stack[sp].expr);              \
5274     stack[sp - 1].expr = parser_build_binary_op (stack[sp].loc,               \
5275                                                  stack[sp].op,                \
5276                                                  stack[sp - 1].expr,          \
5277                                                  stack[sp].expr);             \
5278     sp--;                                                                     \
5279   } while (0)
5280   gcc_assert (!after || c_dialect_objc ());
5281   stack[0].loc = c_parser_peek_token (parser)->location;
5282   stack[0].expr = c_parser_cast_expression (parser, after);
5283   stack[0].prec = PREC_NONE;
5284   sp = 0;
5285   while (true)
5286     {
5287       enum prec oprec;
5288       enum tree_code ocode;
5289       if (parser->error)
5290         goto out;
5291       switch (c_parser_peek_token (parser)->type)
5292         {
5293         case CPP_MULT:
5294           oprec = PREC_MULT;
5295           ocode = MULT_EXPR;
5296           break;
5297         case CPP_DIV:
5298           oprec = PREC_MULT;
5299           ocode = TRUNC_DIV_EXPR;
5300           break;
5301         case CPP_MOD:
5302           oprec = PREC_MULT;
5303           ocode = TRUNC_MOD_EXPR;
5304           break;
5305         case CPP_PLUS:
5306           oprec = PREC_ADD;
5307           ocode = PLUS_EXPR;
5308           break;
5309         case CPP_MINUS:
5310           oprec = PREC_ADD;
5311           ocode = MINUS_EXPR;
5312           break;
5313         case CPP_LSHIFT:
5314           oprec = PREC_SHIFT;
5315           ocode = LSHIFT_EXPR;
5316           break;
5317         case CPP_RSHIFT:
5318           oprec = PREC_SHIFT;
5319           ocode = RSHIFT_EXPR;
5320           break;
5321         case CPP_LESS:
5322           oprec = PREC_REL;
5323           ocode = LT_EXPR;
5324           break;
5325         case CPP_GREATER:
5326           oprec = PREC_REL;
5327           ocode = GT_EXPR;
5328           break;
5329         case CPP_LESS_EQ:
5330           oprec = PREC_REL;
5331           ocode = LE_EXPR;
5332           break;
5333         case CPP_GREATER_EQ:
5334           oprec = PREC_REL;
5335           ocode = GE_EXPR;
5336           break;
5337         case CPP_EQ_EQ:
5338           oprec = PREC_EQ;
5339           ocode = EQ_EXPR;
5340           break;
5341         case CPP_NOT_EQ:
5342           oprec = PREC_EQ;
5343           ocode = NE_EXPR;
5344           break;
5345         case CPP_AND:
5346           oprec = PREC_BITAND;
5347           ocode = BIT_AND_EXPR;
5348           break;
5349         case CPP_XOR:
5350           oprec = PREC_BITXOR;
5351           ocode = BIT_XOR_EXPR;
5352           break;
5353         case CPP_OR:
5354           oprec = PREC_BITOR;
5355           ocode = BIT_IOR_EXPR;
5356           break;
5357         case CPP_AND_AND:
5358           oprec = PREC_LOGAND;
5359           ocode = TRUTH_ANDIF_EXPR;
5360           break;
5361         case CPP_OR_OR:
5362           oprec = PREC_LOGOR;
5363           ocode = TRUTH_ORIF_EXPR;
5364           break;
5365         default:
5366           /* Not a binary operator, so end of the binary
5367              expression.  */
5368           goto out;
5369         }
5370       binary_loc = c_parser_peek_token (parser)->location;
5371       c_parser_consume_token (parser);
5372       while (oprec <= stack[sp].prec)
5373         POP;
5374       switch (ocode)
5375         {
5376         case TRUTH_ANDIF_EXPR:
5377           stack[sp].expr
5378             = default_function_array_read_conversion (stack[sp].loc,
5379                                                       stack[sp].expr);
5380           stack[sp].expr.value = c_objc_common_truthvalue_conversion
5381             (stack[sp].loc, default_conversion (stack[sp].expr.value));
5382           c_inhibit_evaluation_warnings += (stack[sp].expr.value
5383                                             == truthvalue_false_node);
5384           break;
5385         case TRUTH_ORIF_EXPR:
5386           stack[sp].expr
5387             = default_function_array_read_conversion (stack[sp].loc,
5388                                                       stack[sp].expr);
5389           stack[sp].expr.value = c_objc_common_truthvalue_conversion
5390             (stack[sp].loc, default_conversion (stack[sp].expr.value));
5391           c_inhibit_evaluation_warnings += (stack[sp].expr.value
5392                                             == truthvalue_true_node);
5393           break;
5394         default:
5395           break;
5396         }
5397       sp++;
5398       stack[sp].loc = binary_loc;
5399       stack[sp].expr = c_parser_cast_expression (parser, NULL);
5400       stack[sp].prec = oprec;
5401       stack[sp].op = ocode;
5402       stack[sp].loc = binary_loc;
5403     }
5404  out:
5405   while (sp > 0)
5406     POP;
5407   return stack[0].expr;
5408 #undef POP
5409 }
5410
5411 /* Parse a cast expression (C90 6.3.4, C99 6.5.4).  If AFTER is not
5412    NULL then it is an Objective-C message expression which is the
5413    primary-expression starting the expression as an initializer.
5414
5415    cast-expression:
5416      unary-expression
5417      ( type-name ) unary-expression
5418 */
5419
5420 static struct c_expr
5421 c_parser_cast_expression (c_parser *parser, struct c_expr *after)
5422 {
5423   location_t cast_loc = c_parser_peek_token (parser)->location;
5424   gcc_assert (!after || c_dialect_objc ());
5425   if (after)
5426     return c_parser_postfix_expression_after_primary (parser,
5427                                                       cast_loc, *after);
5428   /* If the expression begins with a parenthesized type name, it may
5429      be either a cast or a compound literal; we need to see whether
5430      the next character is '{' to tell the difference.  If not, it is
5431      an unary expression.  */
5432   if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
5433       && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
5434     {
5435       struct c_type_name *type_name;
5436       struct c_expr ret;
5437       struct c_expr expr;
5438       c_parser_consume_token (parser);
5439       type_name = c_parser_type_name (parser);
5440       c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
5441       if (type_name == NULL)
5442         {
5443           ret.value = error_mark_node;
5444           ret.original_code = ERROR_MARK;
5445           ret.original_type = NULL;
5446           return ret;
5447         }
5448
5449       /* Save casted types in the function's used types hash table.  */
5450       used_types_insert (type_name->specs->type);
5451
5452       if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
5453         return c_parser_postfix_expression_after_paren_type (parser, type_name,
5454                                                              cast_loc);
5455       {
5456         location_t expr_loc = c_parser_peek_token (parser)->location;
5457         expr = c_parser_cast_expression (parser, NULL);
5458         expr = default_function_array_read_conversion (expr_loc, expr);
5459       }
5460       ret.value = c_cast_expr (cast_loc, type_name, expr.value);
5461       ret.original_code = ERROR_MARK;
5462       ret.original_type = NULL;
5463       return ret;
5464     }
5465   else
5466     return c_parser_unary_expression (parser);
5467 }
5468
5469 /* Parse an unary expression (C90 6.3.3, C99 6.5.3).
5470
5471    unary-expression:
5472      postfix-expression
5473      ++ unary-expression
5474      -- unary-expression
5475      unary-operator cast-expression
5476      sizeof unary-expression
5477      sizeof ( type-name )
5478
5479    unary-operator: one of
5480      & * + - ~ !
5481
5482    GNU extensions:
5483
5484    unary-expression:
5485      __alignof__ unary-expression
5486      __alignof__ ( type-name )
5487      && identifier
5488
5489    unary-operator: one of
5490      __extension__ __real__ __imag__
5491
5492    In addition, the GNU syntax treats ++ and -- as unary operators, so
5493    they may be applied to cast expressions with errors for non-lvalues
5494    given later.  */
5495
5496 static struct c_expr
5497 c_parser_unary_expression (c_parser *parser)
5498 {
5499   int ext;
5500   struct c_expr ret, op;
5501   location_t op_loc = c_parser_peek_token (parser)->location;
5502   location_t exp_loc;
5503   ret.original_code = ERROR_MARK;
5504   ret.original_type = NULL;
5505   switch (c_parser_peek_token (parser)->type)
5506     {
5507     case CPP_PLUS_PLUS:
5508       c_parser_consume_token (parser);
5509       exp_loc = c_parser_peek_token (parser)->location;
5510       op = c_parser_cast_expression (parser, NULL);
5511       op = default_function_array_read_conversion (exp_loc, op);
5512       return parser_build_unary_op (op_loc, PREINCREMENT_EXPR, op);
5513     case CPP_MINUS_MINUS:
5514       c_parser_consume_token (parser);
5515       exp_loc = c_parser_peek_token (parser)->location;
5516       op = c_parser_cast_expression (parser, NULL);
5517       op = default_function_array_read_conversion (exp_loc, op);
5518       return parser_build_unary_op (op_loc, PREDECREMENT_EXPR, op);
5519     case CPP_AND:
5520       c_parser_consume_token (parser);
5521       op = c_parser_cast_expression (parser, NULL);
5522       mark_exp_read (op.value);
5523       return parser_build_unary_op (op_loc, ADDR_EXPR, op);
5524     case CPP_MULT:
5525       c_parser_consume_token (parser);
5526       exp_loc = c_parser_peek_token (parser)->location;
5527       op = c_parser_cast_expression (parser, NULL);
5528       op = default_function_array_read_conversion (exp_loc, op);
5529       ret.value = build_indirect_ref (op_loc, op.value, RO_UNARY_STAR);
5530       return ret;
5531     case CPP_PLUS:
5532       if (!c_dialect_objc () && !in_system_header)
5533         warning_at (op_loc,
5534                     OPT_Wtraditional,
5535                     "traditional C rejects the unary plus operator");
5536       c_parser_consume_token (parser);
5537       exp_loc = c_parser_peek_token (parser)->location;
5538       op = c_parser_cast_expression (parser, NULL);
5539       op = default_function_array_read_conversion (exp_loc, op);
5540       return parser_build_unary_op (op_loc, CONVERT_EXPR, op);
5541     case CPP_MINUS:
5542       c_parser_consume_token (parser);
5543       exp_loc = c_parser_peek_token (parser)->location;
5544       op = c_parser_cast_expression (parser, NULL);
5545       op = default_function_array_read_conversion (exp_loc, op);
5546       return parser_build_unary_op (op_loc, NEGATE_EXPR, op);
5547     case CPP_COMPL:
5548       c_parser_consume_token (parser);
5549       exp_loc = c_parser_peek_token (parser)->location;
5550       op = c_parser_cast_expression (parser, NULL);
5551       op = default_function_array_read_conversion (exp_loc, op);
5552       return parser_build_unary_op (op_loc, BIT_NOT_EXPR, op);
5553     case CPP_NOT:
5554       c_parser_consume_token (parser);
5555       exp_loc = c_parser_peek_token (parser)->location;
5556       op = c_parser_cast_expression (parser, NULL);
5557       op = default_function_array_read_conversion (exp_loc, op);
5558       return parser_build_unary_op (op_loc, TRUTH_NOT_EXPR, op);
5559     case CPP_AND_AND:
5560       /* Refer to the address of a label as a pointer.  */
5561       c_parser_consume_token (parser);
5562       if (c_parser_next_token_is (parser, CPP_NAME))
5563         {
5564           ret.value = finish_label_address_expr
5565             (c_parser_peek_token (parser)->value, op_loc);
5566           c_parser_consume_token (parser);
5567         }
5568       else
5569         {
5570           c_parser_error (parser, "expected identifier");
5571           ret.value = error_mark_node;
5572         }
5573         return ret;
5574     case CPP_KEYWORD:
5575       switch (c_parser_peek_token (parser)->keyword)
5576         {
5577         case RID_SIZEOF:
5578           return c_parser_sizeof_expression (parser);
5579         case RID_ALIGNOF:
5580           return c_parser_alignof_expression (parser);
5581         case RID_EXTENSION:
5582           c_parser_consume_token (parser);
5583           ext = disable_extension_diagnostics ();
5584           ret = c_parser_cast_expression (parser, NULL);
5585           restore_extension_diagnostics (ext);
5586           return ret;
5587         case RID_REALPART:
5588           c_parser_consume_token (parser);
5589           exp_loc = c_parser_peek_token (parser)->location;
5590           op = c_parser_cast_expression (parser, NULL);
5591           op = default_function_array_conversion (exp_loc, op);
5592           return parser_build_unary_op (op_loc, REALPART_EXPR, op);
5593         case RID_IMAGPART:
5594           c_parser_consume_token (parser);
5595           exp_loc = c_parser_peek_token (parser)->location;
5596           op = c_parser_cast_expression (parser, NULL);
5597           op = default_function_array_conversion (exp_loc, op);
5598           return parser_build_unary_op (op_loc, IMAGPART_EXPR, op);
5599         default:
5600           return c_parser_postfix_expression (parser);
5601         }
5602     default:
5603       return c_parser_postfix_expression (parser);
5604     }
5605 }
5606
5607 /* Parse a sizeof expression.  */
5608
5609 static struct c_expr
5610 c_parser_sizeof_expression (c_parser *parser)
5611 {
5612   struct c_expr expr;
5613   location_t expr_loc;
5614   gcc_assert (c_parser_next_token_is_keyword (parser, RID_SIZEOF));
5615   c_parser_consume_token (parser);
5616   c_inhibit_evaluation_warnings++;
5617   in_sizeof++;
5618   if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
5619       && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
5620     {
5621       /* Either sizeof ( type-name ) or sizeof unary-expression
5622          starting with a compound literal.  */
5623       struct c_type_name *type_name;
5624       c_parser_consume_token (parser);
5625       expr_loc = c_parser_peek_token (parser)->location;
5626       type_name = c_parser_type_name (parser);
5627       c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
5628       if (type_name == NULL)
5629         {
5630           struct c_expr ret;
5631           c_inhibit_evaluation_warnings--;
5632           in_sizeof--;
5633           ret.value = error_mark_node;
5634           ret.original_code = ERROR_MARK;
5635           ret.original_type = NULL;
5636           return ret;
5637         }
5638       if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
5639         {
5640           expr = c_parser_postfix_expression_after_paren_type (parser,
5641                                                                type_name,
5642                                                                expr_loc);
5643           goto sizeof_expr;
5644         }
5645       /* sizeof ( type-name ).  */
5646       c_inhibit_evaluation_warnings--;
5647       in_sizeof--;
5648       return c_expr_sizeof_type (expr_loc, type_name);
5649     }
5650   else
5651     {
5652       expr_loc = c_parser_peek_token (parser)->location;
5653       expr = c_parser_unary_expression (parser);
5654     sizeof_expr:
5655       c_inhibit_evaluation_warnings--;
5656       in_sizeof--;
5657       mark_exp_read (expr.value);
5658       if (TREE_CODE (expr.value) == COMPONENT_REF
5659           && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1)))
5660         error_at (expr_loc, "%<sizeof%> applied to a bit-field");
5661       return c_expr_sizeof_expr (expr_loc, expr);
5662     }
5663 }
5664
5665 /* Parse an alignof expression.  */
5666
5667 static struct c_expr
5668 c_parser_alignof_expression (c_parser *parser)
5669 {
5670   struct c_expr expr;
5671   location_t loc = c_parser_peek_token (parser)->location;
5672   gcc_assert (c_parser_next_token_is_keyword (parser, RID_ALIGNOF));
5673   c_parser_consume_token (parser);
5674   c_inhibit_evaluation_warnings++;
5675   in_alignof++;
5676   if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
5677       && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
5678     {
5679       /* Either __alignof__ ( type-name ) or __alignof__
5680          unary-expression starting with a compound literal.  */
5681       location_t loc;
5682       struct c_type_name *type_name;
5683       struct c_expr ret;
5684       c_parser_consume_token (parser);
5685       loc = c_parser_peek_token (parser)->location;
5686       type_name = c_parser_type_name (parser);
5687       c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
5688       if (type_name == NULL)
5689         {
5690           struct c_expr ret;
5691           c_inhibit_evaluation_warnings--;
5692           in_alignof--;
5693           ret.value = error_mark_node;
5694           ret.original_code = ERROR_MARK;
5695           ret.original_type = NULL;
5696           return ret;
5697         }
5698       if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
5699         {
5700           expr = c_parser_postfix_expression_after_paren_type (parser,
5701                                                                type_name,
5702                                                                loc);
5703           goto alignof_expr;
5704         }
5705       /* alignof ( type-name ).  */
5706       c_inhibit_evaluation_warnings--;
5707       in_alignof--;
5708       ret.value = c_alignof (loc, groktypename (type_name, NULL, NULL));
5709       ret.original_code = ERROR_MARK;
5710       ret.original_type = NULL;
5711       return ret;
5712     }
5713   else
5714     {
5715       struct c_expr ret;
5716       expr = c_parser_unary_expression (parser);
5717     alignof_expr:
5718       mark_exp_read (expr.value);
5719       c_inhibit_evaluation_warnings--;
5720       in_alignof--;
5721       ret.value = c_alignof_expr (loc, expr.value);
5722       ret.original_code = ERROR_MARK;
5723       ret.original_type = NULL;
5724       return ret;
5725     }
5726 }
5727
5728 /* Parse a postfix expression (C90 6.3.1-6.3.2, C99 6.5.1-6.5.2).
5729
5730    postfix-expression:
5731      primary-expression
5732      postfix-expression [ expression ]
5733      postfix-expression ( argument-expression-list[opt] )
5734      postfix-expression . identifier
5735      postfix-expression -> identifier
5736      postfix-expression ++
5737      postfix-expression --
5738      ( type-name ) { initializer-list }
5739      ( type-name ) { initializer-list , }
5740
5741    argument-expression-list:
5742      argument-expression
5743      argument-expression-list , argument-expression
5744
5745    primary-expression:
5746      identifier
5747      constant
5748      string-literal
5749      ( expression )
5750
5751    GNU extensions:
5752
5753    primary-expression:
5754      __func__
5755        (treated as a keyword in GNU C)
5756      __FUNCTION__
5757      __PRETTY_FUNCTION__
5758      ( compound-statement )
5759      __builtin_va_arg ( assignment-expression , type-name )
5760      __builtin_offsetof ( type-name , offsetof-member-designator )
5761      __builtin_choose_expr ( assignment-expression ,
5762                              assignment-expression ,
5763                              assignment-expression )
5764      __builtin_types_compatible_p ( type-name , type-name )
5765
5766    offsetof-member-designator:
5767      identifier
5768      offsetof-member-designator . identifier
5769      offsetof-member-designator [ expression ]
5770
5771    Objective-C:
5772
5773    primary-expression:
5774      [ objc-receiver objc-message-args ]
5775      @selector ( objc-selector-arg )
5776      @protocol ( identifier )
5777      @encode ( type-name )
5778      objc-string-literal
5779 */
5780
5781 static struct c_expr
5782 c_parser_postfix_expression (c_parser *parser)
5783 {
5784   struct c_expr expr, e1, e2, e3;
5785   struct c_type_name *t1, *t2;
5786   location_t loc = c_parser_peek_token (parser)->location;;
5787   expr.original_code = ERROR_MARK;
5788   expr.original_type = NULL;
5789   switch (c_parser_peek_token (parser)->type)
5790     {
5791     case CPP_NUMBER:
5792       expr.value = c_parser_peek_token (parser)->value;
5793       loc = c_parser_peek_token (parser)->location;
5794       c_parser_consume_token (parser);
5795       if (TREE_CODE (expr.value) == FIXED_CST
5796           && !targetm.fixed_point_supported_p ())
5797         {
5798           error_at (loc, "fixed-point types not supported for this target");
5799           expr.value = error_mark_node;
5800         }
5801       break;
5802     case CPP_CHAR:
5803     case CPP_CHAR16:
5804     case CPP_CHAR32:
5805     case CPP_WCHAR:
5806       expr.value = c_parser_peek_token (parser)->value;
5807       c_parser_consume_token (parser);
5808       break;
5809     case CPP_STRING:
5810     case CPP_STRING16:
5811     case CPP_STRING32:
5812     case CPP_WSTRING:
5813     case CPP_UTF8STRING:
5814       expr.value = c_parser_peek_token (parser)->value;
5815       expr.original_code = STRING_CST;
5816       c_parser_consume_token (parser);
5817       break;
5818     case CPP_OBJC_STRING:
5819       gcc_assert (c_dialect_objc ());
5820       expr.value
5821         = objc_build_string_object (c_parser_peek_token (parser)->value);
5822       c_parser_consume_token (parser);
5823       break;
5824     case CPP_NAME:
5825       if (c_parser_peek_token (parser)->id_kind != C_ID_ID)
5826         {
5827           c_parser_error (parser, "expected expression");
5828           expr.value = error_mark_node;
5829           break;
5830         }
5831       {
5832         tree id = c_parser_peek_token (parser)->value;
5833         c_parser_consume_token (parser);
5834         expr.value = build_external_ref (loc, id,
5835                                          (c_parser_peek_token (parser)->type
5836                                           == CPP_OPEN_PAREN),
5837                                          &expr.original_type);
5838       }
5839       break;
5840     case CPP_OPEN_PAREN:
5841       /* A parenthesized expression, statement expression or compound
5842          literal.  */
5843       if (c_parser_peek_2nd_token (parser)->type == CPP_OPEN_BRACE)
5844         {
5845           /* A statement expression.  */
5846           tree stmt;
5847           location_t brace_loc;
5848           c_parser_consume_token (parser);
5849           brace_loc = c_parser_peek_token (parser)->location;
5850           c_parser_consume_token (parser);
5851           if (cur_stmt_list == NULL)
5852             {
5853               error_at (loc, "braced-group within expression allowed "
5854                         "only inside a function");
5855               parser->error = true;
5856               c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
5857               c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5858               expr.value = error_mark_node;
5859               break;
5860             }
5861           stmt = c_begin_stmt_expr ();
5862           c_parser_compound_statement_nostart (parser);
5863           c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5864                                      "expected %<)%>");
5865           pedwarn (loc, OPT_pedantic,
5866                    "ISO C forbids braced-groups within expressions");
5867           expr.value = c_finish_stmt_expr (brace_loc, stmt);
5868           mark_exp_read (expr.value);
5869         }
5870       else if (c_token_starts_typename (c_parser_peek_2nd_token (parser)))
5871         {
5872           /* A compound literal.  ??? Can we actually get here rather
5873              than going directly to
5874              c_parser_postfix_expression_after_paren_type from
5875              elsewhere?  */
5876           location_t loc;
5877           struct c_type_name *type_name;
5878           c_parser_consume_token (parser);
5879           loc = c_parser_peek_token (parser)->location;
5880           type_name = c_parser_type_name (parser);
5881           c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5882                                      "expected %<)%>");
5883           if (type_name == NULL)
5884             {
5885               expr.value = error_mark_node;
5886             }
5887           else
5888             expr = c_parser_postfix_expression_after_paren_type (parser,
5889                                                                  type_name,
5890                                                                  loc);
5891         }
5892       else
5893         {
5894           /* A parenthesized expression.  */
5895           c_parser_consume_token (parser);
5896           expr = c_parser_expression (parser);
5897           if (TREE_CODE (expr.value) == MODIFY_EXPR)
5898             TREE_NO_WARNING (expr.value) = 1;
5899           if (expr.original_code != C_MAYBE_CONST_EXPR)
5900             expr.original_code = ERROR_MARK;
5901           /* Don't change EXPR.ORIGINAL_TYPE.  */
5902           c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5903                                      "expected %<)%>");
5904         }
5905       break;
5906     case CPP_KEYWORD:
5907       switch (c_parser_peek_token (parser)->keyword)
5908         {
5909         case RID_FUNCTION_NAME:
5910         case RID_PRETTY_FUNCTION_NAME:
5911         case RID_C99_FUNCTION_NAME:
5912           expr.value = fname_decl (loc,
5913                                    c_parser_peek_token (parser)->keyword,
5914                                    c_parser_peek_token (parser)->value);
5915           c_parser_consume_token (parser);
5916           break;
5917         case RID_VA_ARG:
5918           c_parser_consume_token (parser);
5919           if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5920             {
5921               expr.value = error_mark_node;
5922               break;
5923             }
5924           e1 = c_parser_expr_no_commas (parser, NULL);
5925           mark_exp_read (e1.value);
5926           e1.value = c_fully_fold (e1.value, false, NULL);
5927           if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
5928             {
5929               c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5930               expr.value = error_mark_node;
5931               break;
5932             }
5933           loc = c_parser_peek_token (parser)->location;
5934           t1 = c_parser_type_name (parser);
5935           c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5936                                      "expected %<)%>");
5937           if (t1 == NULL)
5938             {
5939               expr.value = error_mark_node;
5940             }
5941           else
5942             {
5943               tree type_expr = NULL_TREE;
5944               expr.value = c_build_va_arg (loc, e1.value,
5945                                            groktypename (t1, &type_expr, NULL));
5946               if (type_expr)
5947                 {
5948                   expr.value = build2 (C_MAYBE_CONST_EXPR,
5949                                        TREE_TYPE (expr.value), type_expr,
5950                                        expr.value);
5951                   C_MAYBE_CONST_EXPR_NON_CONST (expr.value) = true;
5952                 }
5953             }
5954           break;
5955         case RID_OFFSETOF:
5956           c_parser_consume_token (parser);
5957           if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5958             {
5959               expr.value = error_mark_node;
5960               break;
5961             }
5962           t1 = c_parser_type_name (parser);
5963           if (t1 == NULL)
5964             {
5965               expr.value = error_mark_node;
5966               break;
5967             }
5968           if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
5969             {
5970               c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5971               expr.value = error_mark_node;
5972               break;
5973             }
5974           {
5975             tree type = groktypename (t1, NULL, NULL);
5976             tree offsetof_ref;
5977             if (type == error_mark_node)
5978               offsetof_ref = error_mark_node;
5979             else
5980               {
5981                 offsetof_ref = build1 (INDIRECT_REF, type, null_pointer_node);
5982                 SET_EXPR_LOCATION (offsetof_ref, loc);
5983               }
5984             /* Parse the second argument to __builtin_offsetof.  We
5985                must have one identifier, and beyond that we want to
5986                accept sub structure and sub array references.  */
5987             if (c_parser_next_token_is (parser, CPP_NAME))
5988               {
5989                 offsetof_ref = build_component_ref
5990                   (loc, offsetof_ref, c_parser_peek_token (parser)->value);
5991                 c_parser_consume_token (parser);
5992                 while (c_parser_next_token_is (parser, CPP_DOT)
5993                        || c_parser_next_token_is (parser,
5994                                                   CPP_OPEN_SQUARE)
5995                        || c_parser_next_token_is (parser,
5996                                                   CPP_DEREF))
5997                   {
5998                     if (c_parser_next_token_is (parser, CPP_DEREF))
5999                       {
6000                         loc = c_parser_peek_token (parser)->location;
6001                         offsetof_ref = build_array_ref (loc,
6002                                                         offsetof_ref,
6003                                                         integer_zero_node);
6004                         goto do_dot;
6005                       }
6006                     else if (c_parser_next_token_is (parser, CPP_DOT))
6007                       {
6008                       do_dot:
6009                         c_parser_consume_token (parser);
6010                         if (c_parser_next_token_is_not (parser,
6011                                                         CPP_NAME))
6012                           {
6013                             c_parser_error (parser, "expected identifier");
6014                             break;
6015                           }
6016                         offsetof_ref = build_component_ref
6017                           (loc, offsetof_ref,
6018                            c_parser_peek_token (parser)->value);
6019                         c_parser_consume_token (parser);
6020                       }
6021                     else
6022                       {
6023                         tree idx;
6024                         loc = c_parser_peek_token (parser)->location;
6025                         c_parser_consume_token (parser);
6026                         idx = c_parser_expression (parser).value;
6027                         idx = c_fully_fold (idx, false, NULL);
6028                         c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
6029                                                    "expected %<]%>");
6030                         offsetof_ref = build_array_ref (loc, offsetof_ref, idx);
6031                       }
6032                   }
6033               }
6034             else
6035               c_parser_error (parser, "expected identifier");
6036             c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6037                                        "expected %<)%>");
6038             expr.value = fold_offsetof (offsetof_ref, NULL_TREE);
6039           }
6040           break;
6041         case RID_CHOOSE_EXPR:
6042           c_parser_consume_token (parser);
6043           if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6044             {
6045               expr.value = error_mark_node;
6046               break;
6047             }
6048           loc = c_parser_peek_token (parser)->location;
6049           e1 = c_parser_expr_no_commas (parser, NULL);
6050           if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
6051             {
6052               c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6053               expr.value = error_mark_node;
6054               break;
6055             }
6056           e2 = c_parser_expr_no_commas (parser, NULL);
6057           if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
6058             {
6059               c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6060               expr.value = error_mark_node;
6061               break;
6062             }
6063           e3 = c_parser_expr_no_commas (parser, NULL);
6064           c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6065                                      "expected %<)%>");
6066           {
6067             tree c;
6068
6069             c = e1.value;
6070             mark_exp_read (e2.value);
6071             mark_exp_read (e3.value);
6072             if (TREE_CODE (c) != INTEGER_CST
6073                 || !INTEGRAL_TYPE_P (TREE_TYPE (c)))
6074               error_at (loc,
6075                         "first argument to %<__builtin_choose_expr%> not"
6076                         " a constant");
6077             constant_expression_warning (c);
6078             expr = integer_zerop (c) ? e3 : e2;
6079           }
6080           break;
6081         case RID_TYPES_COMPATIBLE_P:
6082           c_parser_consume_token (parser);
6083           if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6084             {
6085               expr.value = error_mark_node;
6086               break;
6087             }
6088           t1 = c_parser_type_name (parser);
6089           if (t1 == NULL)
6090             {
6091               expr.value = error_mark_node;
6092               break;
6093             }
6094           if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
6095             {
6096               c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6097               expr.value = error_mark_node;
6098               break;
6099             }
6100           t2 = c_parser_type_name (parser);
6101           if (t2 == NULL)
6102             {
6103               expr.value = error_mark_node;
6104               break;
6105             }
6106           c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6107                                      "expected %<)%>");
6108           {
6109             tree e1, e2;
6110
6111             e1 = TYPE_MAIN_VARIANT (groktypename (t1, NULL, NULL));
6112             e2 = TYPE_MAIN_VARIANT (groktypename (t2, NULL, NULL));
6113
6114             expr.value
6115               = comptypes (e1, e2) ? integer_one_node : integer_zero_node;
6116           }
6117           break;
6118         case RID_AT_SELECTOR:
6119           gcc_assert (c_dialect_objc ());
6120           c_parser_consume_token (parser);
6121           if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6122             {
6123               expr.value = error_mark_node;
6124               break;
6125             }
6126           {
6127             tree sel = c_parser_objc_selector_arg (parser);
6128             c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6129                                        "expected %<)%>");
6130             expr.value = objc_build_selector_expr (loc, sel);
6131           }
6132           break;
6133         case RID_AT_PROTOCOL:
6134           gcc_assert (c_dialect_objc ());
6135           c_parser_consume_token (parser);
6136           if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6137             {
6138               expr.value = error_mark_node;
6139               break;
6140             }
6141           if (c_parser_next_token_is_not (parser, CPP_NAME))
6142             {
6143               c_parser_error (parser, "expected identifier");
6144               c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6145               expr.value = error_mark_node;
6146               break;
6147             }
6148           {
6149             tree id = c_parser_peek_token (parser)->value;
6150             c_parser_consume_token (parser);
6151             c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6152                                        "expected %<)%>");
6153             expr.value = objc_build_protocol_expr (id);
6154           }
6155           break;
6156         case RID_AT_ENCODE:
6157           /* Extension to support C-structures in the archiver.  */
6158           gcc_assert (c_dialect_objc ());
6159           c_parser_consume_token (parser);
6160           if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6161             {
6162               expr.value = error_mark_node;
6163               break;
6164             }
6165           t1 = c_parser_type_name (parser);
6166           if (t1 == NULL)
6167             {
6168               expr.value = error_mark_node;
6169               c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6170               break;
6171             }
6172           c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6173                                      "expected %<)%>");
6174           {
6175             tree type = groktypename (t1, NULL, NULL);
6176             expr.value = objc_build_encode_expr (type);
6177           }
6178           break;
6179         default:
6180           c_parser_error (parser, "expected expression");
6181           expr.value = error_mark_node;
6182           break;
6183         }
6184       break;
6185     case CPP_OPEN_SQUARE:
6186       if (c_dialect_objc ())
6187         {
6188           tree receiver, args;
6189           c_parser_consume_token (parser);
6190           receiver = c_parser_objc_receiver (parser);
6191           args = c_parser_objc_message_args (parser);
6192           c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
6193                                      "expected %<]%>");
6194           expr.value = objc_build_message_expr (build_tree_list (receiver,
6195                                                                  args));
6196           break;
6197         }
6198       /* Else fall through to report error.  */
6199     default:
6200       c_parser_error (parser, "expected expression");
6201       expr.value = error_mark_node;
6202       break;
6203     }
6204   return c_parser_postfix_expression_after_primary (parser, loc, expr);
6205 }
6206
6207 /* Parse a postfix expression after a parenthesized type name: the
6208    brace-enclosed initializer of a compound literal, possibly followed
6209    by some postfix operators.  This is separate because it is not
6210    possible to tell until after the type name whether a cast
6211    expression has a cast or a compound literal, or whether the operand
6212    of sizeof is a parenthesized type name or starts with a compound
6213    literal.  TYPE_LOC is the location where TYPE_NAME starts--the
6214    location of the first token after the parentheses around the type
6215    name.  */
6216
6217 static struct c_expr
6218 c_parser_postfix_expression_after_paren_type (c_parser *parser,
6219                                               struct c_type_name *type_name,
6220                                               location_t type_loc)
6221 {
6222   tree type;
6223   struct c_expr init;
6224   bool non_const;
6225   struct c_expr expr;
6226   location_t start_loc;
6227   tree type_expr = NULL_TREE;
6228   bool type_expr_const = true;
6229   check_compound_literal_type (type_loc, type_name);
6230   start_init (NULL_TREE, NULL, 0);
6231   type = groktypename (type_name, &type_expr, &type_expr_const);
6232   start_loc = c_parser_peek_token (parser)->location;
6233   if (type != error_mark_node && C_TYPE_VARIABLE_SIZE (type))
6234     {
6235       error_at (type_loc, "compound literal has variable size");
6236       type = error_mark_node;
6237     }
6238   init = c_parser_braced_init (parser, type, false);
6239   finish_init ();
6240   maybe_warn_string_init (type, init);
6241
6242   if (type != error_mark_node
6243       && !ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (type))
6244       && current_function_decl)
6245     {
6246       error ("compound literal qualified by address-space qualifier");
6247       type = error_mark_node;
6248     }
6249
6250   if (!flag_isoc99)
6251     pedwarn (start_loc, OPT_pedantic, "ISO C90 forbids compound literals");
6252   non_const = ((init.value && TREE_CODE (init.value) == CONSTRUCTOR)
6253                ? CONSTRUCTOR_NON_CONST (init.value)
6254                : init.original_code == C_MAYBE_CONST_EXPR);
6255   non_const |= !type_expr_const;
6256   expr.value = build_compound_literal (start_loc, type, init.value, non_const);
6257   expr.original_code = ERROR_MARK;
6258   expr.original_type = NULL;
6259   if (type_expr)
6260     {
6261       if (TREE_CODE (expr.value) == C_MAYBE_CONST_EXPR)
6262         {
6263           gcc_assert (C_MAYBE_CONST_EXPR_PRE (expr.value) == NULL_TREE);
6264           C_MAYBE_CONST_EXPR_PRE (expr.value) = type_expr;
6265         }
6266       else
6267         {
6268           gcc_assert (!non_const);
6269           expr.value = build2 (C_MAYBE_CONST_EXPR, type,
6270                                type_expr, expr.value);
6271         }
6272     }
6273   return c_parser_postfix_expression_after_primary (parser, start_loc, expr);
6274 }
6275
6276 /* Parse a postfix expression after the initial primary or compound
6277    literal; that is, parse a series of postfix operators.
6278
6279    EXPR_LOC is the location of the primary expression.  */
6280
6281 static struct c_expr
6282 c_parser_postfix_expression_after_primary (c_parser *parser,
6283                                            location_t expr_loc,
6284                                            struct c_expr expr)
6285 {
6286   struct c_expr orig_expr;
6287   tree ident, idx;
6288   VEC(tree,gc) *exprlist;
6289   VEC(tree,gc) *origtypes;
6290   while (true)
6291     {
6292       location_t op_loc = c_parser_peek_token (parser)->location;
6293       switch (c_parser_peek_token (parser)->type)
6294         {
6295         case CPP_OPEN_SQUARE:
6296           /* Array reference.  */
6297           c_parser_consume_token (parser);
6298           idx = c_parser_expression (parser).value;
6299           c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
6300                                      "expected %<]%>");
6301           expr.value = build_array_ref (op_loc, expr.value, idx);
6302           expr.original_code = ERROR_MARK;
6303           expr.original_type = NULL;
6304           break;
6305         case CPP_OPEN_PAREN:
6306           /* Function call.  */
6307           c_parser_consume_token (parser);
6308           if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
6309             exprlist = NULL;
6310           else
6311             exprlist = c_parser_expr_list (parser, true, false, &origtypes);
6312           c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6313                                      "expected %<)%>");
6314           orig_expr = expr;
6315           mark_exp_read (expr.value);
6316           /* FIXME diagnostics: Ideally we want the FUNCNAME, not the
6317              "(" after the FUNCNAME, which is what we have now.    */
6318           expr.value = build_function_call_vec (op_loc, expr.value, exprlist,
6319                                                 origtypes);
6320           expr.original_code = ERROR_MARK;
6321           if (TREE_CODE (expr.value) == INTEGER_CST
6322               && TREE_CODE (orig_expr.value) == FUNCTION_DECL
6323               && DECL_BUILT_IN_CLASS (orig_expr.value) == BUILT_IN_NORMAL
6324               && DECL_FUNCTION_CODE (orig_expr.value) == BUILT_IN_CONSTANT_P)
6325             expr.original_code = C_MAYBE_CONST_EXPR;
6326           expr.original_type = NULL;
6327           if (exprlist != NULL)
6328             {
6329               release_tree_vector (exprlist);
6330               release_tree_vector (origtypes);
6331             }
6332           break;
6333         case CPP_DOT:
6334           /* Structure element reference.  */
6335           c_parser_consume_token (parser);
6336           expr = default_function_array_conversion (expr_loc, expr);
6337           if (c_parser_next_token_is (parser, CPP_NAME))
6338             ident = c_parser_peek_token (parser)->value;
6339           else
6340             {
6341               c_parser_error (parser, "expected identifier");
6342               expr.value = error_mark_node;
6343               expr.original_code = ERROR_MARK;
6344               expr.original_type = NULL;
6345               return expr;
6346             }
6347           c_parser_consume_token (parser);
6348           expr.value = build_component_ref (op_loc, expr.value, ident);
6349           expr.original_code = ERROR_MARK;
6350           if (TREE_CODE (expr.value) != COMPONENT_REF)
6351             expr.original_type = NULL;
6352           else
6353             {
6354               /* Remember the original type of a bitfield.  */
6355               tree field = TREE_OPERAND (expr.value, 1);
6356               if (TREE_CODE (field) != FIELD_DECL)
6357                 expr.original_type = NULL;
6358               else
6359                 expr.original_type = DECL_BIT_FIELD_TYPE (field);
6360             }
6361           break;
6362         case CPP_DEREF:
6363           /* Structure element reference.  */
6364           c_parser_consume_token (parser);
6365           expr = default_function_array_conversion (expr_loc, expr);
6366           if (c_parser_next_token_is (parser, CPP_NAME))
6367             ident = c_parser_peek_token (parser)->value;
6368           else
6369             {
6370               c_parser_error (parser, "expected identifier");
6371               expr.value = error_mark_node;
6372               expr.original_code = ERROR_MARK;
6373               expr.original_type = NULL;
6374               return expr;
6375             }
6376           c_parser_consume_token (parser);
6377           expr.value = build_component_ref (op_loc,
6378                                             build_indirect_ref (op_loc,
6379                                                                 expr.value,
6380                                                                 RO_ARROW),
6381                                             ident);
6382           expr.original_code = ERROR_MARK;
6383           if (TREE_CODE (expr.value) != COMPONENT_REF)
6384             expr.original_type = NULL;
6385           else
6386             {
6387               /* Remember the original type of a bitfield.  */
6388               tree field = TREE_OPERAND (expr.value, 1);
6389               if (TREE_CODE (field) != FIELD_DECL)
6390                 expr.original_type = NULL;
6391               else
6392                 expr.original_type = DECL_BIT_FIELD_TYPE (field);
6393             }
6394           break;
6395         case CPP_PLUS_PLUS:
6396           /* Postincrement.  */
6397           c_parser_consume_token (parser);
6398           expr = default_function_array_read_conversion (expr_loc, expr);
6399           expr.value = build_unary_op (op_loc,
6400                                        POSTINCREMENT_EXPR, expr.value, 0);
6401           expr.original_code = ERROR_MARK;
6402           expr.original_type = NULL;
6403           break;
6404         case CPP_MINUS_MINUS:
6405           /* Postdecrement.  */
6406           c_parser_consume_token (parser);
6407           expr = default_function_array_read_conversion (expr_loc, expr);
6408           expr.value = build_unary_op (op_loc,
6409                                        POSTDECREMENT_EXPR, expr.value, 0);
6410           expr.original_code = ERROR_MARK;
6411           expr.original_type = NULL;
6412           break;
6413         default:
6414           return expr;
6415         }
6416     }
6417 }
6418
6419 /* Parse an expression (C90 6.3.17, C99 6.5.17).
6420
6421    expression:
6422      assignment-expression
6423      expression , assignment-expression
6424 */
6425
6426 static struct c_expr
6427 c_parser_expression (c_parser *parser)
6428 {
6429   struct c_expr expr;
6430   expr = c_parser_expr_no_commas (parser, NULL);
6431   while (c_parser_next_token_is (parser, CPP_COMMA))
6432     {
6433       struct c_expr next;
6434       tree lhsval;
6435       location_t loc = c_parser_peek_token (parser)->location;
6436       location_t expr_loc;
6437       c_parser_consume_token (parser);
6438       expr_loc = c_parser_peek_token (parser)->location;
6439       lhsval = expr.value;
6440       while (TREE_CODE (lhsval) == COMPOUND_EXPR)
6441         lhsval = TREE_OPERAND (lhsval, 1);
6442       if (DECL_P (lhsval) || handled_component_p (lhsval))
6443         mark_exp_read (lhsval);
6444       next = c_parser_expr_no_commas (parser, NULL);
6445       next = default_function_array_conversion (expr_loc, next);
6446       expr.value = build_compound_expr (loc, expr.value, next.value);
6447       expr.original_code = COMPOUND_EXPR;
6448       expr.original_type = next.original_type;
6449     }
6450   return expr;
6451 }
6452
6453 /* Parse an expression and convert functions or arrays to
6454    pointers.  */
6455
6456 static struct c_expr
6457 c_parser_expression_conv (c_parser *parser)
6458 {
6459   struct c_expr expr;
6460   location_t loc = c_parser_peek_token (parser)->location;
6461   expr = c_parser_expression (parser);
6462   expr = default_function_array_conversion (loc, expr);
6463   return expr;
6464 }
6465
6466 /* Parse a non-empty list of expressions.  If CONVERT_P, convert
6467    functions and arrays to pointers.  If FOLD_P, fold the expressions.
6468
6469    nonempty-expr-list:
6470      assignment-expression
6471      nonempty-expr-list , assignment-expression
6472 */
6473
6474 static VEC(tree,gc) *
6475 c_parser_expr_list (c_parser *parser, bool convert_p, bool fold_p,
6476                     VEC(tree,gc) **p_orig_types)
6477 {
6478   VEC(tree,gc) *ret;
6479   VEC(tree,gc) *orig_types;
6480   struct c_expr expr;
6481   location_t loc = c_parser_peek_token (parser)->location;
6482
6483   ret = make_tree_vector ();
6484   if (p_orig_types == NULL)
6485     orig_types = NULL;
6486   else
6487     orig_types = make_tree_vector ();
6488
6489   expr = c_parser_expr_no_commas (parser, NULL);
6490   if (convert_p)
6491     expr = default_function_array_read_conversion (loc, expr);
6492   if (fold_p)
6493     expr.value = c_fully_fold (expr.value, false, NULL);
6494   VEC_quick_push (tree, ret, expr.value);
6495   if (orig_types != NULL)
6496     VEC_quick_push (tree, orig_types, expr.original_type);
6497   while (c_parser_next_token_is (parser, CPP_COMMA))
6498     {
6499       c_parser_consume_token (parser);
6500       loc = c_parser_peek_token (parser)->location;
6501       expr = c_parser_expr_no_commas (parser, NULL);
6502       if (convert_p)
6503         expr = default_function_array_read_conversion (loc, expr);
6504       if (fold_p)
6505         expr.value = c_fully_fold (expr.value, false, NULL);
6506       VEC_safe_push (tree, gc, ret, expr.value);
6507       if (orig_types != NULL)
6508         VEC_safe_push (tree, gc, orig_types, expr.original_type);
6509     }
6510   if (orig_types != NULL)
6511     *p_orig_types = orig_types;
6512   return ret;
6513 }
6514 \f
6515 /* Parse Objective-C-specific constructs.  */
6516
6517 /* Parse an objc-class-definition.
6518
6519    objc-class-definition:
6520      @interface identifier objc-superclass[opt] objc-protocol-refs[opt]
6521        objc-class-instance-variables[opt] objc-methodprotolist @end
6522      @implementation identifier objc-superclass[opt]
6523        objc-class-instance-variables[opt]
6524      @interface identifier ( identifier ) objc-protocol-refs[opt]
6525        objc-methodprotolist @end
6526      @implementation identifier ( identifier )
6527
6528    objc-superclass:
6529      : identifier
6530
6531    "@interface identifier (" must start "@interface identifier (
6532    identifier ) ...": objc-methodprotolist in the first production may
6533    not start with a parenthesized identifier as a declarator of a data
6534    definition with no declaration specifiers if the objc-superclass,
6535    objc-protocol-refs and objc-class-instance-variables are omitted.  */
6536
6537 static void
6538 c_parser_objc_class_definition (c_parser *parser, tree attributes)
6539 {
6540   bool iface_p;
6541   tree id1;
6542   tree superclass;
6543   if (c_parser_next_token_is_keyword (parser, RID_AT_INTERFACE))
6544     iface_p = true;
6545   else if (c_parser_next_token_is_keyword (parser, RID_AT_IMPLEMENTATION))
6546     iface_p = false;
6547   else
6548     gcc_unreachable ();
6549
6550   c_parser_consume_token (parser);
6551   if (c_parser_next_token_is_not (parser, CPP_NAME))
6552     {
6553       c_parser_error (parser, "expected identifier");
6554       return;
6555     }
6556   id1 = c_parser_peek_token (parser)->value;
6557   c_parser_consume_token (parser);
6558   if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
6559     {
6560       tree id2;
6561       tree proto = NULL_TREE;
6562       c_parser_consume_token (parser);
6563       if (c_parser_next_token_is_not (parser, CPP_NAME))
6564         {
6565           c_parser_error (parser, "expected identifier");
6566           c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6567           return;
6568         }
6569       id2 = c_parser_peek_token (parser)->value;
6570       c_parser_consume_token (parser);
6571       c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
6572       if (!iface_p)
6573         {
6574           objc_start_category_implementation (id1, id2);
6575           return;
6576         }
6577       if (c_parser_next_token_is (parser, CPP_LESS))
6578         proto = c_parser_objc_protocol_refs (parser);
6579       objc_start_category_interface (id1, id2, proto, attributes);
6580       c_parser_objc_methodprotolist (parser);
6581       c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
6582       objc_finish_interface ();
6583       return;
6584     }
6585   if (c_parser_next_token_is (parser, CPP_COLON))
6586     {
6587       c_parser_consume_token (parser);
6588       if (c_parser_next_token_is_not (parser, CPP_NAME))
6589         {
6590           c_parser_error (parser, "expected identifier");
6591           return;
6592         }
6593       superclass = c_parser_peek_token (parser)->value;
6594       c_parser_consume_token (parser);
6595     }
6596   else
6597     superclass = NULL_TREE;
6598   if (iface_p)
6599     {
6600       tree proto = NULL_TREE;
6601       if (c_parser_next_token_is (parser, CPP_LESS))
6602         proto = c_parser_objc_protocol_refs (parser);
6603       objc_start_class_interface (id1, superclass, proto, attributes);
6604     }
6605   else
6606     objc_start_class_implementation (id1, superclass);
6607   if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
6608     c_parser_objc_class_instance_variables (parser);
6609   if (iface_p)
6610     {
6611       objc_continue_interface ();
6612       c_parser_objc_methodprotolist (parser);
6613       c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
6614       objc_finish_interface ();
6615     }
6616   else
6617     {
6618       objc_continue_implementation ();
6619       return;
6620     }
6621 }
6622
6623 /* Parse objc-class-instance-variables.
6624
6625    objc-class-instance-variables:
6626      { objc-instance-variable-decl-list[opt] }
6627
6628    objc-instance-variable-decl-list:
6629      objc-visibility-spec
6630      objc-instance-variable-decl ;
6631      ;
6632      objc-instance-variable-decl-list objc-visibility-spec
6633      objc-instance-variable-decl-list objc-instance-variable-decl ;
6634      objc-instance-variable-decl-list ;
6635
6636    objc-visibility-spec:
6637      @private
6638      @protected
6639      @public
6640
6641    objc-instance-variable-decl:
6642      struct-declaration
6643 */
6644
6645 static void
6646 c_parser_objc_class_instance_variables (c_parser *parser)
6647 {
6648   gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE));
6649   c_parser_consume_token (parser);
6650   while (c_parser_next_token_is_not (parser, CPP_EOF))
6651     {
6652       tree decls;
6653       /* Parse any stray semicolon.  */
6654       if (c_parser_next_token_is (parser, CPP_SEMICOLON))
6655         {
6656           pedwarn (c_parser_peek_token (parser)->location, OPT_pedantic,
6657                    "extra semicolon in struct or union specified");
6658           c_parser_consume_token (parser);
6659           continue;
6660         }
6661       /* Stop if at the end of the instance variables.  */
6662       if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
6663         {
6664           c_parser_consume_token (parser);
6665           break;
6666         }
6667       /* Parse any objc-visibility-spec.  */
6668       if (c_parser_next_token_is_keyword (parser, RID_AT_PRIVATE))
6669         {
6670           c_parser_consume_token (parser);
6671           objc_set_visibility (2);
6672           continue;
6673         }
6674       else if (c_parser_next_token_is_keyword (parser, RID_AT_PROTECTED))
6675         {
6676           c_parser_consume_token (parser);
6677           objc_set_visibility (0);
6678           continue;
6679         }
6680       else if (c_parser_next_token_is_keyword (parser, RID_AT_PUBLIC))
6681         {
6682           c_parser_consume_token (parser);
6683           objc_set_visibility (1);
6684           continue;
6685         }
6686       else if (c_parser_next_token_is (parser, CPP_PRAGMA))
6687         {
6688           c_parser_pragma (parser, pragma_external);
6689           continue;
6690         }
6691
6692       /* Parse some comma-separated declarations.  */
6693       decls = c_parser_struct_declaration (parser);
6694       {
6695         /* Comma-separated instance variables are chained together in
6696            reverse order; add them one by one.  */
6697         tree ivar = nreverse (decls);
6698         for (; ivar; ivar = DECL_CHAIN (ivar))
6699           objc_add_instance_variable (copy_node (ivar));
6700       }
6701       c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
6702     }
6703 }
6704
6705 /* Parse an objc-class-declaration.
6706
6707    objc-class-declaration:
6708      @class identifier-list ;
6709 */
6710
6711 static void
6712 c_parser_objc_class_declaration (c_parser *parser)
6713 {
6714   tree list = NULL_TREE;
6715   gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_CLASS));
6716   c_parser_consume_token (parser);
6717   /* Any identifiers, including those declared as type names, are OK
6718      here.  */
6719   while (true)
6720     {
6721       tree id;
6722       if (c_parser_next_token_is_not (parser, CPP_NAME))
6723         {
6724           c_parser_error (parser, "expected identifier");
6725           break;
6726         }
6727       id = c_parser_peek_token (parser)->value;
6728       list = chainon (list, build_tree_list (NULL_TREE, id));
6729       c_parser_consume_token (parser);
6730       if (c_parser_next_token_is (parser, CPP_COMMA))
6731         c_parser_consume_token (parser);
6732       else
6733         break;
6734     }
6735   c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
6736   objc_declare_class (list);
6737 }
6738
6739 /* Parse an objc-alias-declaration.
6740
6741    objc-alias-declaration:
6742      @compatibility_alias identifier identifier ;
6743 */
6744
6745 static void
6746 c_parser_objc_alias_declaration (c_parser *parser)
6747 {
6748   tree id1, id2;
6749   gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_ALIAS));
6750   c_parser_consume_token (parser);
6751   if (c_parser_next_token_is_not (parser, CPP_NAME))
6752     {
6753       c_parser_error (parser, "expected identifier");
6754       c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
6755       return;
6756     }
6757   id1 = c_parser_peek_token (parser)->value;
6758   c_parser_consume_token (parser);
6759   if (c_parser_next_token_is_not (parser, CPP_NAME))
6760     {
6761       c_parser_error (parser, "expected identifier");
6762       c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
6763       return;
6764     }
6765   id2 = c_parser_peek_token (parser)->value;
6766   c_parser_consume_token (parser);
6767   c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
6768   objc_declare_alias (id1, id2);
6769 }
6770
6771 /* Parse an objc-protocol-definition.
6772
6773    objc-protocol-definition:
6774      @protocol identifier objc-protocol-refs[opt] objc-methodprotolist @end
6775      @protocol identifier-list ;
6776
6777    "@protocol identifier ;" should be resolved as "@protocol
6778    identifier-list ;": objc-methodprotolist may not start with a
6779    semicolon in the first alternative if objc-protocol-refs are
6780    omitted.  */
6781
6782 static void
6783 c_parser_objc_protocol_definition (c_parser *parser, tree attributes)
6784 {
6785   gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_PROTOCOL));
6786
6787   c_parser_consume_token (parser);
6788   if (c_parser_next_token_is_not (parser, CPP_NAME))
6789     {
6790       c_parser_error (parser, "expected identifier");
6791       return;
6792     }
6793   if (c_parser_peek_2nd_token (parser)->type == CPP_COMMA
6794       || c_parser_peek_2nd_token (parser)->type == CPP_SEMICOLON)
6795     {
6796       tree list = NULL_TREE;
6797       /* Any identifiers, including those declared as type names, are
6798          OK here.  */
6799       while (true)
6800         {
6801           tree id;
6802           if (c_parser_next_token_is_not (parser, CPP_NAME))
6803             {
6804               c_parser_error (parser, "expected identifier");
6805               break;
6806             }
6807           id = c_parser_peek_token (parser)->value;
6808           list = chainon (list, build_tree_list (NULL_TREE, id));
6809           c_parser_consume_token (parser);
6810           if (c_parser_next_token_is (parser, CPP_COMMA))
6811             c_parser_consume_token (parser);
6812           else
6813             break;
6814         }
6815       c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
6816       objc_declare_protocols (list);
6817     }
6818   else
6819     {
6820       tree id = c_parser_peek_token (parser)->value;
6821       tree proto = NULL_TREE;
6822       c_parser_consume_token (parser);
6823       if (c_parser_next_token_is (parser, CPP_LESS))
6824         proto = c_parser_objc_protocol_refs (parser);
6825       parser->objc_pq_context = true;
6826       objc_start_protocol (id, proto, attributes);
6827       c_parser_objc_methodprotolist (parser);
6828       c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
6829       parser->objc_pq_context = false;
6830       objc_finish_interface ();
6831     }
6832 }
6833
6834 /* Parse an objc-method-type.
6835
6836    objc-method-type:
6837      +
6838      -
6839 */
6840
6841 static enum tree_code
6842 c_parser_objc_method_type (c_parser *parser)
6843 {
6844   switch (c_parser_peek_token (parser)->type)
6845     {
6846     case CPP_PLUS:
6847       c_parser_consume_token (parser);
6848       return PLUS_EXPR;
6849     case CPP_MINUS:
6850       c_parser_consume_token (parser);
6851       return MINUS_EXPR;
6852     default:
6853       gcc_unreachable ();
6854     }
6855 }
6856
6857 /* Parse an objc-method-definition.
6858
6859    objc-method-definition:
6860      objc-method-type objc-method-decl ;[opt] compound-statement
6861 */
6862
6863 static void
6864 c_parser_objc_method_definition (c_parser *parser)
6865 {
6866   enum tree_code type = c_parser_objc_method_type (parser);
6867   tree decl, attributes = NULL_TREE;
6868   objc_set_method_type (type);
6869   parser->objc_pq_context = true;
6870   decl = c_parser_objc_method_decl (parser, &attributes);
6871   if (decl == error_mark_node)
6872     return;  /* Bail here. */
6873
6874   if (c_parser_next_token_is (parser, CPP_SEMICOLON))
6875     {
6876       c_parser_consume_token (parser);
6877       pedwarn (c_parser_peek_token (parser)->location, OPT_pedantic,
6878                "extra semicolon in method definition specified");
6879     }
6880
6881   if (!c_parser_next_token_is (parser, CPP_OPEN_BRACE))
6882     {
6883       c_parser_error (parser, "expected %<{%>");
6884       return;
6885     }
6886
6887   parser->objc_pq_context = false;
6888   if (objc_start_method_definition (decl, attributes))
6889     {
6890       add_stmt (c_parser_compound_statement (parser));
6891       objc_finish_method_definition (current_function_decl);
6892     }
6893   else
6894     {
6895       /* This code is executed when we find a method definition
6896          outside of an @implementation context (or invalid for other
6897          reasons).  Parse the method (to keep going) but do not emit
6898          any code.
6899       */
6900       c_parser_compound_statement (parser);
6901     }
6902 }
6903
6904 /* Parse an objc-methodprotolist.
6905
6906    objc-methodprotolist:
6907      empty
6908      objc-methodprotolist objc-methodproto
6909      objc-methodprotolist declaration
6910      objc-methodprotolist ;
6911      @optional
6912      @required
6913
6914    The declaration is a data definition, which may be missing
6915    declaration specifiers under the same rules and diagnostics as
6916    other data definitions outside functions, and the stray semicolon
6917    is diagnosed the same way as a stray semicolon outside a
6918    function.  */
6919
6920 static void
6921 c_parser_objc_methodprotolist (c_parser *parser)
6922 {
6923   while (true)
6924     {
6925       /* The list is terminated by @end.  */
6926       switch (c_parser_peek_token (parser)->type)
6927         {
6928         case CPP_SEMICOLON:
6929           pedwarn (c_parser_peek_token (parser)->location, OPT_pedantic,
6930                    "ISO C does not allow extra %<;%> outside of a function");
6931           c_parser_consume_token (parser);
6932           break;
6933         case CPP_PLUS:
6934         case CPP_MINUS:
6935           c_parser_objc_methodproto (parser);
6936           break;
6937         case CPP_PRAGMA:
6938           c_parser_pragma (parser, pragma_external);
6939           break;
6940         case CPP_EOF:
6941           return;
6942         default:
6943           if (c_parser_next_token_is_keyword (parser, RID_AT_END))
6944             return;
6945           else if (c_parser_next_token_is_keyword (parser, RID_AT_OPTIONAL))
6946             {
6947               objc_set_method_opt (true);
6948               c_parser_consume_token (parser);
6949             }
6950           else if (c_parser_next_token_is_keyword (parser, RID_AT_REQUIRED))
6951             {
6952               objc_set_method_opt (false);
6953               c_parser_consume_token (parser);
6954             }
6955           else
6956             c_parser_declaration_or_fndef (parser, false, false, true,
6957                                            false, true, NULL);
6958           break;
6959         }
6960     }
6961 }
6962
6963 /* Parse an objc-methodproto.
6964
6965    objc-methodproto:
6966      objc-method-type objc-method-decl ;
6967 */
6968
6969 static void
6970 c_parser_objc_methodproto (c_parser *parser)
6971 {
6972   enum tree_code type = c_parser_objc_method_type (parser);
6973   tree decl, attributes = NULL_TREE;
6974   objc_set_method_type (type);
6975   /* Remember protocol qualifiers in prototypes.  */
6976   parser->objc_pq_context = true;
6977   decl = c_parser_objc_method_decl (parser, &attributes);
6978   /* Forget protocol qualifiers now.  */
6979   parser->objc_pq_context = false;
6980
6981   /* Do not allow the presence of attributes to hide an erroneous 
6982      method implementation in the interface section.  */
6983   if (!c_parser_next_token_is (parser, CPP_SEMICOLON))
6984     {
6985       c_parser_error (parser, "expected %<;%>");
6986       return;
6987     }
6988   
6989   if (decl != error_mark_node)
6990     objc_add_method_declaration (decl, attributes);
6991
6992   c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
6993 }
6994
6995 /* If we are at a position that method attributes may be present, check that 
6996    there are not any parsed already (a syntax error) and then collect any 
6997    specified at the current location.  Finally, if new attributes were present,
6998    check that the next token is legal ( ';' for decls and '{' for defs).  */
6999    
7000 static bool 
7001 c_parser_objc_maybe_method_attributes (c_parser* parser, tree* attributes)
7002 {
7003   bool bad = false;
7004   if (*attributes)
7005     {
7006       c_parser_error (parser, 
7007                     "method attributes must be specified at the end only");
7008       *attributes = NULL_TREE;
7009       bad = true;
7010     }
7011
7012   if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
7013     *attributes = c_parser_attributes (parser);
7014
7015   /* If there were no attributes here, just report any earlier error.  */
7016   if (*attributes == NULL_TREE || bad)
7017     return bad;
7018
7019   /* If the attributes are followed by a ; or {, then just report any earlier
7020      error.  */
7021   if (c_parser_next_token_is (parser, CPP_SEMICOLON)
7022       || c_parser_next_token_is (parser, CPP_OPEN_BRACE))
7023     return bad;
7024
7025   /* We've got attributes, but not at the end.  */
7026   c_parser_error (parser, 
7027                   "expected %<;%> or %<{%> after method attribute definition");
7028   return true;
7029 }
7030
7031 /* Parse an objc-method-decl.
7032
7033    objc-method-decl:
7034      ( objc-type-name ) objc-selector
7035      objc-selector
7036      ( objc-type-name ) objc-keyword-selector objc-optparmlist
7037      objc-keyword-selector objc-optparmlist
7038      attributes
7039
7040    objc-keyword-selector:
7041      objc-keyword-decl
7042      objc-keyword-selector objc-keyword-decl
7043
7044    objc-keyword-decl:
7045      objc-selector : ( objc-type-name ) identifier
7046      objc-selector : identifier
7047      : ( objc-type-name ) identifier
7048      : identifier
7049
7050    objc-optparmlist:
7051      objc-optparms objc-optellipsis
7052
7053    objc-optparms:
7054      empty
7055      objc-opt-parms , parameter-declaration
7056
7057    objc-optellipsis:
7058      empty
7059      , ...
7060 */
7061
7062 static tree
7063 c_parser_objc_method_decl (c_parser *parser, tree *attributes)
7064 {
7065   tree type = NULL_TREE;
7066   tree sel;
7067   tree parms = NULL_TREE;
7068   bool ellipsis = false;
7069   bool attr_err = false;
7070
7071   *attributes = NULL_TREE;
7072   if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
7073     {
7074       c_parser_consume_token (parser);
7075       type = c_parser_objc_type_name (parser);
7076       c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
7077     }
7078   sel = c_parser_objc_selector (parser);
7079   /* If there is no selector, or a colon follows, we have an
7080      objc-keyword-selector.  If there is a selector, and a colon does
7081      not follow, that selector ends the objc-method-decl.  */
7082   if (!sel || c_parser_next_token_is (parser, CPP_COLON))
7083     {
7084       tree tsel = sel;
7085       tree list = NULL_TREE;
7086       while (true)
7087         {
7088           tree atype = NULL_TREE, id, keyworddecl;
7089           tree param_attr = NULL_TREE;
7090           if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
7091             break;
7092           if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
7093             {
7094               c_parser_consume_token (parser);
7095               atype = c_parser_objc_type_name (parser);
7096               c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7097                                          "expected %<)%>");
7098             }
7099           /* New ObjC allows attributes on method parameters.  */
7100           if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
7101             param_attr = c_parser_attributes (parser);
7102           if (c_parser_next_token_is_not (parser, CPP_NAME))
7103             {
7104               c_parser_error (parser, "expected identifier");
7105               return error_mark_node;
7106             }
7107           id = c_parser_peek_token (parser)->value;
7108           c_parser_consume_token (parser);
7109           keyworddecl = objc_build_keyword_decl (tsel, atype, id, param_attr);
7110           list = chainon (list, keyworddecl);
7111           tsel = c_parser_objc_selector (parser);
7112           if (!tsel && c_parser_next_token_is_not (parser, CPP_COLON))
7113             break;
7114         }
7115
7116       attr_err |= c_parser_objc_maybe_method_attributes (parser, attributes) ;
7117
7118       /* Parse the optional parameter list.  Optional Objective-C
7119          method parameters follow the C syntax, and may include '...'
7120          to denote a variable number of arguments.  */
7121       parms = make_node (TREE_LIST);
7122       while (c_parser_next_token_is (parser, CPP_COMMA))
7123         {
7124           struct c_parm *parm;
7125           c_parser_consume_token (parser);
7126           if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
7127             {
7128               ellipsis = true;
7129               c_parser_consume_token (parser);
7130               attr_err |= c_parser_objc_maybe_method_attributes 
7131                                                 (parser, attributes) ;
7132               break;
7133             }
7134           parm = c_parser_parameter_declaration (parser, NULL_TREE);
7135           if (parm == NULL)
7136             break;
7137           parms = chainon (parms,
7138                            build_tree_list (NULL_TREE, grokparm (parm)));
7139         }
7140       sel = list;
7141     }
7142   else
7143     attr_err |= c_parser_objc_maybe_method_attributes (parser, attributes) ;
7144
7145   if (sel == NULL)
7146     {
7147       c_parser_error (parser, "objective-c method declaration is expected");
7148       return error_mark_node;
7149     }
7150
7151   if (attr_err)
7152     return error_mark_node;
7153
7154   return objc_build_method_signature (type, sel, parms, ellipsis);
7155 }
7156
7157 /* Parse an objc-type-name.
7158
7159    objc-type-name:
7160      objc-type-qualifiers[opt] type-name
7161      objc-type-qualifiers[opt]
7162
7163    objc-type-qualifiers:
7164      objc-type-qualifier
7165      objc-type-qualifiers objc-type-qualifier
7166
7167    objc-type-qualifier: one of
7168      in out inout bycopy byref oneway
7169 */
7170
7171 static tree
7172 c_parser_objc_type_name (c_parser *parser)
7173 {
7174   tree quals = NULL_TREE;
7175   struct c_type_name *type_name = NULL;
7176   tree type = NULL_TREE;
7177   while (true)
7178     {
7179       c_token *token = c_parser_peek_token (parser);
7180       if (token->type == CPP_KEYWORD
7181           && (token->keyword == RID_IN
7182               || token->keyword == RID_OUT
7183               || token->keyword == RID_INOUT
7184               || token->keyword == RID_BYCOPY
7185               || token->keyword == RID_BYREF
7186               || token->keyword == RID_ONEWAY))
7187         {
7188           quals = chainon (quals, build_tree_list (NULL_TREE, token->value));
7189           c_parser_consume_token (parser);
7190         }
7191       else
7192         break;
7193     }
7194   if (c_parser_next_token_starts_typename (parser))
7195     type_name = c_parser_type_name (parser);
7196   if (type_name)
7197     type = groktypename (type_name, NULL, NULL);
7198   return build_tree_list (quals, type);
7199 }
7200
7201 /* Parse objc-protocol-refs.
7202
7203    objc-protocol-refs:
7204      < identifier-list >
7205 */
7206
7207 static tree
7208 c_parser_objc_protocol_refs (c_parser *parser)
7209 {
7210   tree list = NULL_TREE;
7211   gcc_assert (c_parser_next_token_is (parser, CPP_LESS));
7212   c_parser_consume_token (parser);
7213   /* Any identifiers, including those declared as type names, are OK
7214      here.  */
7215   while (true)
7216     {
7217       tree id;
7218       if (c_parser_next_token_is_not (parser, CPP_NAME))
7219         {
7220           c_parser_error (parser, "expected identifier");
7221           break;
7222         }
7223       id = c_parser_peek_token (parser)->value;
7224       list = chainon (list, build_tree_list (NULL_TREE, id));
7225       c_parser_consume_token (parser);
7226       if (c_parser_next_token_is (parser, CPP_COMMA))
7227         c_parser_consume_token (parser);
7228       else
7229         break;
7230     }
7231   c_parser_require (parser, CPP_GREATER, "expected %<>%>");
7232   return list;
7233 }
7234
7235 /* Parse an objc-try-catch-statement.
7236
7237    objc-try-catch-statement:
7238      @try compound-statement objc-catch-list[opt]
7239      @try compound-statement objc-catch-list[opt] @finally compound-statement
7240
7241    objc-catch-list:
7242      @catch ( parameter-declaration ) compound-statement
7243      objc-catch-list @catch ( parameter-declaration ) compound-statement
7244 */
7245
7246 static void
7247 c_parser_objc_try_catch_statement (c_parser *parser)
7248 {
7249   location_t loc;
7250   tree stmt;
7251   gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_TRY));
7252   c_parser_consume_token (parser);
7253   loc = c_parser_peek_token (parser)->location;
7254   stmt = c_parser_compound_statement (parser);
7255   objc_begin_try_stmt (loc, stmt);
7256   while (c_parser_next_token_is_keyword (parser, RID_AT_CATCH))
7257     {
7258       struct c_parm *parm;
7259       c_parser_consume_token (parser);
7260       if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7261         break;
7262       parm = c_parser_parameter_declaration (parser, NULL_TREE);
7263       if (parm == NULL)
7264         {
7265           c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7266           break;
7267         }
7268       c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
7269       objc_begin_catch_clause (grokparm (parm));
7270       if (c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
7271         c_parser_compound_statement_nostart (parser);
7272       objc_finish_catch_clause ();
7273     }
7274   if (c_parser_next_token_is_keyword (parser, RID_AT_FINALLY))
7275     {
7276       location_t finloc;
7277       tree finstmt;
7278       c_parser_consume_token (parser);
7279       finloc = c_parser_peek_token (parser)->location;
7280       finstmt = c_parser_compound_statement (parser);
7281       objc_build_finally_clause (finloc, finstmt);
7282     }
7283   objc_finish_try_stmt ();
7284 }
7285
7286 /* Parse an objc-synchronized-statement.
7287
7288    objc-synchronized-statement:
7289      @synchronized ( expression ) compound-statement
7290 */
7291
7292 static void
7293 c_parser_objc_synchronized_statement (c_parser *parser)
7294 {
7295   location_t loc;
7296   tree expr, stmt;
7297   gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_SYNCHRONIZED));
7298   c_parser_consume_token (parser);
7299   loc = c_parser_peek_token (parser)->location;
7300   if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7301     {
7302       expr = c_parser_expression (parser).value;
7303       expr = c_fully_fold (expr, false, NULL);
7304       c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
7305     }
7306   else
7307     expr = error_mark_node;
7308   stmt = c_parser_compound_statement (parser);
7309   objc_build_synchronized (loc, expr, stmt);
7310 }
7311
7312 /* Parse an objc-selector; return NULL_TREE without an error if the
7313    next token is not an objc-selector.
7314
7315    objc-selector:
7316      identifier
7317      one of
7318        enum struct union if else while do for switch case default
7319        break continue return goto asm sizeof typeof __alignof
7320        unsigned long const short volatile signed restrict _Complex
7321        in out inout bycopy byref oneway int char float double void _Bool
7322
7323    ??? Why this selection of keywords but not, for example, storage
7324    class specifiers?  */
7325
7326 static tree
7327 c_parser_objc_selector (c_parser *parser)
7328 {
7329   c_token *token = c_parser_peek_token (parser);
7330   tree value = token->value;
7331   if (token->type == CPP_NAME)
7332     {
7333       c_parser_consume_token (parser);
7334       return value;
7335     }
7336   if (token->type != CPP_KEYWORD)
7337     return NULL_TREE;
7338   switch (token->keyword)
7339     {
7340     case RID_ENUM:
7341     case RID_STRUCT:
7342     case RID_UNION:
7343     case RID_IF:
7344     case RID_ELSE:
7345     case RID_WHILE:
7346     case RID_DO:
7347     case RID_FOR:
7348     case RID_SWITCH:
7349     case RID_CASE:
7350     case RID_DEFAULT:
7351     case RID_BREAK:
7352     case RID_CONTINUE:
7353     case RID_RETURN:
7354     case RID_GOTO:
7355     case RID_ASM:
7356     case RID_SIZEOF:
7357     case RID_TYPEOF:
7358     case RID_ALIGNOF:
7359     case RID_UNSIGNED:
7360     case RID_LONG:
7361     case RID_INT128:
7362     case RID_CONST:
7363     case RID_SHORT:
7364     case RID_VOLATILE:
7365     case RID_SIGNED:
7366     case RID_RESTRICT:
7367     case RID_COMPLEX:
7368     case RID_IN:
7369     case RID_OUT:
7370     case RID_INOUT:
7371     case RID_BYCOPY:
7372     case RID_BYREF:
7373     case RID_ONEWAY:
7374     case RID_INT:
7375     case RID_CHAR:
7376     case RID_FLOAT:
7377     case RID_DOUBLE:
7378     case RID_VOID:
7379     case RID_BOOL:
7380       c_parser_consume_token (parser);
7381       return value;
7382     default:
7383       return NULL_TREE;
7384     }
7385 }
7386
7387 /* Parse an objc-selector-arg.
7388
7389    objc-selector-arg:
7390      objc-selector
7391      objc-keywordname-list
7392
7393    objc-keywordname-list:
7394      objc-keywordname
7395      objc-keywordname-list objc-keywordname
7396
7397    objc-keywordname:
7398      objc-selector :
7399      :
7400 */
7401
7402 static tree
7403 c_parser_objc_selector_arg (c_parser *parser)
7404 {
7405   tree sel = c_parser_objc_selector (parser);
7406   tree list = NULL_TREE;
7407   if (sel && c_parser_next_token_is_not (parser, CPP_COLON))
7408     return sel;
7409   while (true)
7410     {
7411       if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
7412         return list;
7413       list = chainon (list, build_tree_list (sel, NULL_TREE));
7414       sel = c_parser_objc_selector (parser);
7415       if (!sel && c_parser_next_token_is_not (parser, CPP_COLON))
7416         break;
7417     }
7418   return list;
7419 }
7420
7421 /* Parse an objc-receiver.
7422
7423    objc-receiver:
7424      expression
7425      class-name
7426      type-name
7427 */
7428
7429 static tree
7430 c_parser_objc_receiver (c_parser *parser)
7431 {
7432   if (c_parser_peek_token (parser)->type == CPP_NAME
7433       && (c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME
7434           || c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME))
7435     {
7436       tree id = c_parser_peek_token (parser)->value;
7437       c_parser_consume_token (parser);
7438       return objc_get_class_reference (id);
7439     }
7440   return c_fully_fold (c_parser_expression (parser).value, false, NULL);
7441 }
7442
7443 /* Parse objc-message-args.
7444
7445    objc-message-args:
7446      objc-selector
7447      objc-keywordarg-list
7448
7449    objc-keywordarg-list:
7450      objc-keywordarg
7451      objc-keywordarg-list objc-keywordarg
7452
7453    objc-keywordarg:
7454      objc-selector : objc-keywordexpr
7455      : objc-keywordexpr
7456 */
7457
7458 static tree
7459 c_parser_objc_message_args (c_parser *parser)
7460 {
7461   tree sel = c_parser_objc_selector (parser);
7462   tree list = NULL_TREE;
7463   if (sel && c_parser_next_token_is_not (parser, CPP_COLON))
7464     return sel;
7465   while (true)
7466     {
7467       tree keywordexpr;
7468       if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
7469         return error_mark_node;
7470       keywordexpr = c_parser_objc_keywordexpr (parser);
7471       list = chainon (list, build_tree_list (sel, keywordexpr));
7472       sel = c_parser_objc_selector (parser);
7473       if (!sel && c_parser_next_token_is_not (parser, CPP_COLON))
7474         break;
7475     }
7476   return list;
7477 }
7478
7479 /* Parse an objc-keywordexpr.
7480
7481    objc-keywordexpr:
7482      nonempty-expr-list
7483 */
7484
7485 static tree
7486 c_parser_objc_keywordexpr (c_parser *parser)
7487 {
7488   tree ret;
7489   VEC(tree,gc) *expr_list = c_parser_expr_list (parser, true, true, NULL);
7490   if (VEC_length (tree, expr_list) == 1)
7491     {
7492       /* Just return the expression, remove a level of
7493          indirection.  */
7494       ret = VEC_index (tree, expr_list, 0);
7495     }
7496   else
7497     {
7498       /* We have a comma expression, we will collapse later.  */
7499       ret = build_tree_list_vec (expr_list);
7500     }
7501   release_tree_vector (expr_list);
7502   return ret;
7503 }
7504
7505 /* A check, needed in several places, that ObjC interface, implementation or
7506    method definitions are not prefixed by incorrect items.  */
7507 static bool
7508 c_parser_objc_diagnose_bad_element_prefix (c_parser *parser, 
7509                                            struct c_declspecs *specs)
7510 {
7511   if (!specs->declspecs_seen_p || specs->type_seen_p || specs->non_sc_seen_p)
7512     {
7513       c_parser_error (parser, 
7514                       "no type or storage class may be specified here,");
7515       c_parser_skip_to_end_of_block_or_statement (parser);
7516       return true;
7517     }
7518   return false;
7519 }
7520 \f
7521 /* Handle pragmas.  Some OpenMP pragmas are associated with, and therefore
7522    should be considered, statements.  ALLOW_STMT is true if we're within
7523    the context of a function and such pragmas are to be allowed.  Returns
7524    true if we actually parsed such a pragma.  */
7525
7526 static bool
7527 c_parser_pragma (c_parser *parser, enum pragma_context context)
7528 {
7529   unsigned int id;
7530
7531   id = c_parser_peek_token (parser)->pragma_kind;
7532   gcc_assert (id != PRAGMA_NONE);
7533
7534   switch (id)
7535     {
7536     case PRAGMA_OMP_BARRIER:
7537       if (context != pragma_compound)
7538         {
7539           if (context == pragma_stmt)
7540             c_parser_error (parser, "%<#pragma omp barrier%> may only be "
7541                             "used in compound statements");
7542           goto bad_stmt;
7543         }
7544       c_parser_omp_barrier (parser);
7545       return false;
7546
7547     case PRAGMA_OMP_FLUSH:
7548       if (context != pragma_compound)
7549         {
7550           if (context == pragma_stmt)
7551             c_parser_error (parser, "%<#pragma omp flush%> may only be "
7552                             "used in compound statements");
7553           goto bad_stmt;
7554         }
7555       c_parser_omp_flush (parser);
7556       return false;
7557
7558     case PRAGMA_OMP_TASKWAIT:
7559       if (context != pragma_compound)
7560         {
7561           if (context == pragma_stmt)
7562             c_parser_error (parser, "%<#pragma omp taskwait%> may only be "
7563                             "used in compound statements");
7564           goto bad_stmt;
7565         }
7566       c_parser_omp_taskwait (parser);
7567       return false;
7568
7569     case PRAGMA_OMP_THREADPRIVATE:
7570       c_parser_omp_threadprivate (parser);
7571       return false;
7572
7573     case PRAGMA_OMP_SECTION:
7574       error_at (c_parser_peek_token (parser)->location,
7575                 "%<#pragma omp section%> may only be used in "
7576                 "%<#pragma omp sections%> construct");
7577       c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
7578       return false;
7579
7580     case PRAGMA_GCC_PCH_PREPROCESS:
7581       c_parser_error (parser, "%<#pragma GCC pch_preprocess%> must be first");
7582       c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
7583       return false;
7584
7585     default:
7586       if (id < PRAGMA_FIRST_EXTERNAL)
7587         {
7588           if (context == pragma_external)
7589             {
7590             bad_stmt:
7591               c_parser_error (parser, "expected declaration specifiers");
7592               c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
7593               return false;
7594             }
7595           c_parser_omp_construct (parser);
7596           return true;
7597         }
7598       break;
7599     }
7600
7601   c_parser_consume_pragma (parser);
7602   c_invoke_pragma_handler (id);
7603
7604   /* Skip to EOL, but suppress any error message.  Those will have been
7605      generated by the handler routine through calling error, as opposed
7606      to calling c_parser_error.  */
7607   parser->error = true;
7608   c_parser_skip_to_pragma_eol (parser);
7609
7610   return false;
7611 }
7612
7613 /* The interface the pragma parsers have to the lexer.  */
7614
7615 enum cpp_ttype
7616 pragma_lex (tree *value)
7617 {
7618   c_token *tok = c_parser_peek_token (the_parser);
7619   enum cpp_ttype ret = tok->type;
7620
7621   *value = tok->value;
7622   if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
7623     ret = CPP_EOF;
7624   else
7625     {
7626       if (ret == CPP_KEYWORD)
7627         ret = CPP_NAME;
7628       c_parser_consume_token (the_parser);
7629     }
7630
7631   return ret;
7632 }
7633
7634 static void
7635 c_parser_pragma_pch_preprocess (c_parser *parser)
7636 {
7637   tree name = NULL;
7638
7639   c_parser_consume_pragma (parser);
7640   if (c_parser_next_token_is (parser, CPP_STRING))
7641     {
7642       name = c_parser_peek_token (parser)->value;
7643       c_parser_consume_token (parser);
7644     }
7645   else
7646     c_parser_error (parser, "expected string literal");
7647   c_parser_skip_to_pragma_eol (parser);
7648
7649   if (name)
7650     c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
7651 }
7652 \f
7653 /* OpenMP 2.5 parsing routines.  */
7654
7655 /* Returns name of the next clause.
7656    If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
7657    the token is not consumed.  Otherwise appropriate pragma_omp_clause is
7658    returned and the token is consumed.  */
7659
7660 static pragma_omp_clause
7661 c_parser_omp_clause_name (c_parser *parser)
7662 {
7663   pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
7664
7665   if (c_parser_next_token_is_keyword (parser, RID_IF))
7666     result = PRAGMA_OMP_CLAUSE_IF;
7667   else if (c_parser_next_token_is_keyword (parser, RID_DEFAULT))
7668     result = PRAGMA_OMP_CLAUSE_DEFAULT;
7669   else if (c_parser_next_token_is (parser, CPP_NAME))
7670     {
7671       const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
7672
7673       switch (p[0])
7674         {
7675         case 'c':
7676           if (!strcmp ("collapse", p))
7677             result = PRAGMA_OMP_CLAUSE_COLLAPSE;
7678           else if (!strcmp ("copyin", p))
7679             result = PRAGMA_OMP_CLAUSE_COPYIN;
7680           else if (!strcmp ("copyprivate", p))
7681             result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
7682           break;
7683         case 'f':
7684           if (!strcmp ("firstprivate", p))
7685             result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
7686           break;
7687         case 'l':
7688           if (!strcmp ("lastprivate", p))
7689             result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
7690           break;
7691         case 'n':
7692           if (!strcmp ("nowait", p))
7693             result = PRAGMA_OMP_CLAUSE_NOWAIT;
7694           else if (!strcmp ("num_threads", p))
7695             result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
7696           break;
7697         case 'o':
7698           if (!strcmp ("ordered", p))
7699             result = PRAGMA_OMP_CLAUSE_ORDERED;
7700           break;
7701         case 'p':
7702           if (!strcmp ("private", p))
7703             result = PRAGMA_OMP_CLAUSE_PRIVATE;
7704           break;
7705         case 'r':
7706           if (!strcmp ("reduction", p))
7707             result = PRAGMA_OMP_CLAUSE_REDUCTION;
7708           break;
7709         case 's':
7710           if (!strcmp ("schedule", p))
7711             result = PRAGMA_OMP_CLAUSE_SCHEDULE;
7712           else if (!strcmp ("shared", p))
7713             result = PRAGMA_OMP_CLAUSE_SHARED;
7714           break;
7715         case 'u':
7716           if (!strcmp ("untied", p))
7717             result = PRAGMA_OMP_CLAUSE_UNTIED;
7718           break;
7719         }
7720     }
7721
7722   if (result != PRAGMA_OMP_CLAUSE_NONE)
7723     c_parser_consume_token (parser);
7724
7725   return result;
7726 }
7727
7728 /* Validate that a clause of the given type does not already exist.  */
7729
7730 static void
7731 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
7732                            const char *name)
7733 {
7734   tree c;
7735
7736   for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
7737     if (OMP_CLAUSE_CODE (c) == code)
7738       {
7739         location_t loc = OMP_CLAUSE_LOCATION (c);
7740         error_at (loc, "too many %qs clauses", name);
7741         break;
7742       }
7743 }
7744
7745 /* OpenMP 2.5:
7746    variable-list:
7747      identifier
7748      variable-list , identifier
7749
7750    If KIND is nonzero, create the appropriate node and install the
7751    decl in OMP_CLAUSE_DECL and add the node to the head of the list.
7752    If KIND is nonzero, CLAUSE_LOC is the location of the clause.
7753
7754    If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
7755    return the list created.  */
7756
7757 static tree
7758 c_parser_omp_variable_list (c_parser *parser,
7759                             location_t clause_loc,
7760                             enum omp_clause_code kind,
7761                             tree list)
7762 {
7763   if (c_parser_next_token_is_not (parser, CPP_NAME)
7764       || c_parser_peek_token (parser)->id_kind != C_ID_ID)
7765     c_parser_error (parser, "expected identifier");
7766
7767   while (c_parser_next_token_is (parser, CPP_NAME)
7768          && c_parser_peek_token (parser)->id_kind == C_ID_ID)
7769     {
7770       tree t = lookup_name (c_parser_peek_token (parser)->value);
7771
7772       if (t == NULL_TREE)
7773         undeclared_variable (c_parser_peek_token (parser)->location,
7774                              c_parser_peek_token (parser)->value);
7775       else if (t == error_mark_node)
7776         ;
7777       else if (kind != 0)
7778         {
7779           tree u = build_omp_clause (clause_loc, kind);
7780           OMP_CLAUSE_DECL (u) = t;
7781           OMP_CLAUSE_CHAIN (u) = list;
7782           list = u;
7783         }
7784       else
7785         list = tree_cons (t, NULL_TREE, list);
7786
7787       c_parser_consume_token (parser);
7788
7789       if (c_parser_next_token_is_not (parser, CPP_COMMA))
7790         break;
7791
7792       c_parser_consume_token (parser);
7793     }
7794
7795   return list;
7796 }
7797
7798 /* Similarly, but expect leading and trailing parenthesis.  This is a very
7799    common case for omp clauses.  */
7800
7801 static tree
7802 c_parser_omp_var_list_parens (c_parser *parser, enum omp_clause_code kind,
7803                               tree list)
7804 {
7805   /* The clauses location.  */
7806   location_t loc = c_parser_peek_token (parser)->location;
7807
7808   if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7809     {
7810       list = c_parser_omp_variable_list (parser, loc, kind, list);
7811       c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
7812     }
7813   return list;
7814 }
7815
7816 /* OpenMP 3.0:
7817    collapse ( constant-expression ) */
7818
7819 static tree
7820 c_parser_omp_clause_collapse (c_parser *parser, tree list)
7821 {
7822   tree c, num = error_mark_node;
7823   HOST_WIDE_INT n;
7824   location_t loc;
7825
7826   check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse");
7827
7828   loc = c_parser_peek_token (parser)->location;
7829   if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7830     {
7831       num = c_parser_expr_no_commas (parser, NULL).value;
7832       c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
7833     }
7834   if (num == error_mark_node)
7835     return list;
7836   if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
7837       || !host_integerp (num, 0)
7838       || (n = tree_low_cst (num, 0)) <= 0
7839       || (int) n != n)
7840     {
7841       error_at (loc,
7842                 "collapse argument needs positive constant integer expression");
7843       return list;
7844     }
7845   c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
7846   OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
7847   OMP_CLAUSE_CHAIN (c) = list;
7848   return c;
7849 }
7850
7851 /* OpenMP 2.5:
7852    copyin ( variable-list ) */
7853
7854 static tree
7855 c_parser_omp_clause_copyin (c_parser *parser, tree list)
7856 {
7857   return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_COPYIN, list);
7858 }
7859
7860 /* OpenMP 2.5:
7861    copyprivate ( variable-list ) */
7862
7863 static tree
7864 c_parser_omp_clause_copyprivate (c_parser *parser, tree list)
7865 {
7866   return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_COPYPRIVATE, list);
7867 }
7868
7869 /* OpenMP 2.5:
7870    default ( shared | none ) */
7871
7872 static tree
7873 c_parser_omp_clause_default (c_parser *parser, tree list)
7874 {
7875   enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
7876   location_t loc = c_parser_peek_token (parser)->location;
7877   tree c;
7878
7879   if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7880     return list;
7881   if (c_parser_next_token_is (parser, CPP_NAME))
7882     {
7883       const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
7884
7885       switch (p[0])
7886         {
7887         case 'n':
7888           if (strcmp ("none", p) != 0)
7889             goto invalid_kind;
7890           kind = OMP_CLAUSE_DEFAULT_NONE;
7891           break;
7892
7893         case 's':
7894           if (strcmp ("shared", p) != 0)
7895             goto invalid_kind;
7896           kind = OMP_CLAUSE_DEFAULT_SHARED;
7897           break;
7898
7899         default:
7900           goto invalid_kind;
7901         }
7902
7903       c_parser_consume_token (parser);
7904     }
7905   else
7906     {
7907     invalid_kind:
7908       c_parser_error (parser, "expected %<none%> or %<shared%>");
7909     }
7910   c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
7911
7912   if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
7913     return list;
7914
7915   check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default");
7916   c = build_omp_clause (loc, OMP_CLAUSE_DEFAULT);
7917   OMP_CLAUSE_CHAIN (c) = list;
7918   OMP_CLAUSE_DEFAULT_KIND (c) = kind;
7919
7920   return c;
7921 }
7922
7923 /* OpenMP 2.5:
7924    firstprivate ( variable-list ) */
7925
7926 static tree
7927 c_parser_omp_clause_firstprivate (c_parser *parser, tree list)
7928 {
7929   return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_FIRSTPRIVATE, list);
7930 }
7931
7932 /* OpenMP 2.5:
7933    if ( expression ) */
7934
7935 static tree
7936 c_parser_omp_clause_if (c_parser *parser, tree list)
7937 {
7938   location_t loc = c_parser_peek_token (parser)->location;
7939   if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
7940     {
7941       tree t = c_parser_paren_condition (parser);
7942       tree c;
7943
7944       check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if");
7945
7946       c = build_omp_clause (loc, OMP_CLAUSE_IF);
7947       OMP_CLAUSE_IF_EXPR (c) = t;
7948       OMP_CLAUSE_CHAIN (c) = list;
7949       list = c;
7950     }
7951   else
7952     c_parser_error (parser, "expected %<(%>");
7953
7954   return list;
7955 }
7956
7957 /* OpenMP 2.5:
7958    lastprivate ( variable-list ) */
7959
7960 static tree
7961 c_parser_omp_clause_lastprivate (c_parser *parser, tree list)
7962 {
7963   return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_LASTPRIVATE, list);
7964 }
7965
7966 /* OpenMP 2.5:
7967    nowait */
7968
7969 static tree
7970 c_parser_omp_clause_nowait (c_parser *parser ATTRIBUTE_UNUSED, tree list)
7971 {
7972   tree c;
7973   location_t loc = c_parser_peek_token (parser)->location;
7974
7975   check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait");
7976
7977   c = build_omp_clause (loc, OMP_CLAUSE_NOWAIT);
7978   OMP_CLAUSE_CHAIN (c) = list;
7979   return c;
7980 }
7981
7982 /* OpenMP 2.5:
7983    num_threads ( expression ) */
7984
7985 static tree
7986 c_parser_omp_clause_num_threads (c_parser *parser, tree list)
7987 {
7988   location_t num_threads_loc = c_parser_peek_token (parser)->location;
7989   if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7990     {
7991       location_t expr_loc = c_parser_peek_token (parser)->location;
7992       tree c, t = c_parser_expression (parser).value;
7993       t = c_fully_fold (t, false, NULL);
7994
7995       c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
7996
7997       if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
7998         {
7999           c_parser_error (parser, "expected integer expression");
8000           return list;
8001         }
8002
8003       /* Attempt to statically determine when the number isn't positive.  */
8004       c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
8005                        build_int_cst (TREE_TYPE (t), 0));
8006       if (CAN_HAVE_LOCATION_P (c))
8007         SET_EXPR_LOCATION (c, expr_loc);
8008       if (c == boolean_true_node)
8009         {
8010           warning_at (expr_loc, 0,
8011                       "%<num_threads%> value must be positive");
8012           t = integer_one_node;
8013         }
8014
8015       check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS, "num_threads");
8016
8017       c = build_omp_clause (num_threads_loc, OMP_CLAUSE_NUM_THREADS);
8018       OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
8019       OMP_CLAUSE_CHAIN (c) = list;
8020       list = c;
8021     }
8022
8023   return list;
8024 }
8025
8026 /* OpenMP 2.5:
8027    ordered */
8028
8029 static tree
8030 c_parser_omp_clause_ordered (c_parser *parser, tree list)
8031 {
8032   tree c;
8033
8034   check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED, "ordered");
8035
8036   c = build_omp_clause (c_parser_peek_token (parser)->location,
8037                         OMP_CLAUSE_ORDERED);
8038   OMP_CLAUSE_CHAIN (c) = list;
8039
8040   return c;
8041 }
8042
8043 /* OpenMP 2.5:
8044    private ( variable-list ) */
8045
8046 static tree
8047 c_parser_omp_clause_private (c_parser *parser, tree list)
8048 {
8049   return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_PRIVATE, list);
8050 }
8051
8052 /* OpenMP 2.5:
8053    reduction ( reduction-operator : variable-list )
8054
8055    reduction-operator:
8056      One of: + * - & ^ | && || */
8057
8058 static tree
8059 c_parser_omp_clause_reduction (c_parser *parser, tree list)
8060 {
8061   location_t clause_loc = c_parser_peek_token (parser)->location;
8062   if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
8063     {
8064       enum tree_code code;
8065
8066       switch (c_parser_peek_token (parser)->type)
8067         {
8068         case CPP_PLUS:
8069           code = PLUS_EXPR;
8070           break;
8071         case CPP_MULT:
8072           code = MULT_EXPR;
8073           break;
8074         case CPP_MINUS:
8075           code = MINUS_EXPR;
8076           break;
8077         case CPP_AND:
8078           code = BIT_AND_EXPR;
8079           break;
8080         case CPP_XOR:
8081           code = BIT_XOR_EXPR;
8082           break;
8083         case CPP_OR:
8084           code = BIT_IOR_EXPR;
8085           break;
8086         case CPP_AND_AND:
8087           code = TRUTH_ANDIF_EXPR;
8088           break;
8089         case CPP_OR_OR:
8090           code = TRUTH_ORIF_EXPR;
8091           break;
8092         default:
8093           c_parser_error (parser,
8094                           "expected %<+%>, %<*%>, %<-%>, %<&%>, "
8095                           "%<^%>, %<|%>, %<&&%>, or %<||%>");
8096           c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
8097           return list;
8098         }
8099       c_parser_consume_token (parser);
8100       if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
8101         {
8102           tree nl, c;
8103
8104           nl = c_parser_omp_variable_list (parser, clause_loc,
8105                                            OMP_CLAUSE_REDUCTION, list);
8106           for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
8107             OMP_CLAUSE_REDUCTION_CODE (c) = code;
8108
8109           list = nl;
8110         }
8111       c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
8112     }
8113   return list;
8114 }
8115
8116 /* OpenMP 2.5:
8117    schedule ( schedule-kind )
8118    schedule ( schedule-kind , expression )
8119
8120    schedule-kind:
8121      static | dynamic | guided | runtime | auto
8122 */
8123
8124 static tree
8125 c_parser_omp_clause_schedule (c_parser *parser, tree list)
8126 {
8127   tree c, t;
8128   location_t loc = c_parser_peek_token (parser)->location;
8129
8130   if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
8131     return list;
8132
8133   c = build_omp_clause (loc, OMP_CLAUSE_SCHEDULE);
8134
8135   if (c_parser_next_token_is (parser, CPP_NAME))
8136     {
8137       tree kind = c_parser_peek_token (parser)->value;
8138       const char *p = IDENTIFIER_POINTER (kind);
8139
8140       switch (p[0])
8141         {
8142         case 'd':
8143           if (strcmp ("dynamic", p) != 0)
8144             goto invalid_kind;
8145           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
8146           break;
8147
8148         case 'g':
8149           if (strcmp ("guided", p) != 0)
8150             goto invalid_kind;
8151           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
8152           break;
8153
8154         case 'r':
8155           if (strcmp ("runtime", p) != 0)
8156             goto invalid_kind;
8157           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
8158           break;
8159
8160         default:
8161           goto invalid_kind;
8162         }
8163     }
8164   else if (c_parser_next_token_is_keyword (parser, RID_STATIC))
8165     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
8166   else if (c_parser_next_token_is_keyword (parser, RID_AUTO))
8167     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
8168   else
8169     goto invalid_kind;
8170
8171   c_parser_consume_token (parser);
8172   if (c_parser_next_token_is (parser, CPP_COMMA))
8173     {
8174       location_t here;
8175       c_parser_consume_token (parser);
8176
8177       here = c_parser_peek_token (parser)->location;
8178       t = c_parser_expr_no_commas (parser, NULL).value;
8179       t = c_fully_fold (t, false, NULL);
8180
8181       if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
8182         error_at (here, "schedule %<runtime%> does not take "
8183                   "a %<chunk_size%> parameter");
8184       else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
8185         error_at (here,
8186                   "schedule %<auto%> does not take "
8187                   "a %<chunk_size%> parameter");
8188       else if (TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE)
8189         OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
8190       else
8191         c_parser_error (parser, "expected integer expression");
8192
8193       c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
8194     }
8195   else
8196     c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
8197                                "expected %<,%> or %<)%>");
8198
8199   check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
8200   OMP_CLAUSE_CHAIN (c) = list;
8201   return c;
8202
8203  invalid_kind:
8204   c_parser_error (parser, "invalid schedule kind");
8205   c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
8206   return list;
8207 }
8208
8209 /* OpenMP 2.5:
8210    shared ( variable-list ) */
8211
8212 static tree
8213 c_parser_omp_clause_shared (c_parser *parser, tree list)
8214 {
8215   return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_SHARED, list);
8216 }
8217
8218 /* OpenMP 3.0:
8219    untied */
8220
8221 static tree
8222 c_parser_omp_clause_untied (c_parser *parser ATTRIBUTE_UNUSED, tree list)
8223 {
8224   tree c;
8225
8226   /* FIXME: Should we allow duplicates?  */
8227   check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied");
8228
8229   c = build_omp_clause (c_parser_peek_token (parser)->location,
8230                         OMP_CLAUSE_UNTIED);
8231   OMP_CLAUSE_CHAIN (c) = list;
8232
8233   return c;
8234 }
8235
8236 /* Parse all OpenMP clauses.  The set clauses allowed by the directive
8237    is a bitmask in MASK.  Return the list of clauses found; the result
8238    of clause default goes in *pdefault.  */
8239
8240 static tree
8241 c_parser_omp_all_clauses (c_parser *parser, unsigned int mask,
8242                           const char *where)
8243 {
8244   tree clauses = NULL;
8245   bool first = true;
8246
8247   while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
8248     {
8249       location_t here;
8250       pragma_omp_clause c_kind;
8251       const char *c_name;
8252       tree prev = clauses;
8253
8254       if (!first && c_parser_next_token_is (parser, CPP_COMMA))
8255         c_parser_consume_token (parser);
8256
8257       first = false;
8258       here = c_parser_peek_token (parser)->location;
8259       c_kind = c_parser_omp_clause_name (parser);
8260
8261       switch (c_kind)
8262         {
8263         case PRAGMA_OMP_CLAUSE_COLLAPSE:
8264           clauses = c_parser_omp_clause_collapse (parser, clauses);
8265           c_name = "collapse";
8266           break;
8267         case PRAGMA_OMP_CLAUSE_COPYIN:
8268           clauses = c_parser_omp_clause_copyin (parser, clauses);
8269           c_name = "copyin";
8270           break;
8271         case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
8272           clauses = c_parser_omp_clause_copyprivate (parser, clauses);
8273           c_name = "copyprivate";
8274           break;
8275         case PRAGMA_OMP_CLAUSE_DEFAULT:
8276           clauses = c_parser_omp_clause_default (parser, clauses);
8277           c_name = "default";
8278           break;
8279         case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
8280           clauses = c_parser_omp_clause_firstprivate (parser, clauses);
8281           c_name = "firstprivate";
8282           break;
8283         case PRAGMA_OMP_CLAUSE_IF:
8284           clauses = c_parser_omp_clause_if (parser, clauses);
8285           c_name = "if";
8286           break;
8287         case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
8288           clauses = c_parser_omp_clause_lastprivate (parser, clauses);
8289           c_name = "lastprivate";
8290           break;
8291         case PRAGMA_OMP_CLAUSE_NOWAIT:
8292           clauses = c_parser_omp_clause_nowait (parser, clauses);
8293           c_name = "nowait";
8294           break;
8295         case PRAGMA_OMP_CLAUSE_NUM_THREADS:
8296           clauses = c_parser_omp_clause_num_threads (parser, clauses);
8297           c_name = "num_threads";
8298           break;
8299         case PRAGMA_OMP_CLAUSE_ORDERED:
8300           clauses = c_parser_omp_clause_ordered (parser, clauses);
8301           c_name = "ordered";
8302           break;
8303         case PRAGMA_OMP_CLAUSE_PRIVATE:
8304           clauses = c_parser_omp_clause_private (parser, clauses);
8305           c_name = "private";
8306           break;
8307         case PRAGMA_OMP_CLAUSE_REDUCTION:
8308           clauses = c_parser_omp_clause_reduction (parser, clauses);
8309           c_name = "reduction";
8310           break;
8311         case PRAGMA_OMP_CLAUSE_SCHEDULE:
8312           clauses = c_parser_omp_clause_schedule (parser, clauses);
8313           c_name = "schedule";
8314           break;
8315         case PRAGMA_OMP_CLAUSE_SHARED:
8316           clauses = c_parser_omp_clause_shared (parser, clauses);
8317           c_name = "shared";
8318           break;
8319         case PRAGMA_OMP_CLAUSE_UNTIED:
8320           clauses = c_parser_omp_clause_untied (parser, clauses);
8321           c_name = "untied";
8322           break;
8323         default:
8324           c_parser_error (parser, "expected %<#pragma omp%> clause");
8325           goto saw_error;
8326         }
8327
8328       if (((mask >> c_kind) & 1) == 0 && !parser->error)
8329         {
8330           /* Remove the invalid clause(s) from the list to avoid
8331              confusing the rest of the compiler.  */
8332           clauses = prev;
8333           error_at (here, "%qs is not valid for %qs", c_name, where);
8334         }
8335     }
8336
8337  saw_error:
8338   c_parser_skip_to_pragma_eol (parser);
8339
8340   return c_finish_omp_clauses (clauses);
8341 }
8342
8343 /* OpenMP 2.5:
8344    structured-block:
8345      statement
8346
8347    In practice, we're also interested in adding the statement to an
8348    outer node.  So it is convenient if we work around the fact that
8349    c_parser_statement calls add_stmt.  */
8350
8351 static tree
8352 c_parser_omp_structured_block (c_parser *parser)
8353 {
8354   tree stmt = push_stmt_list ();
8355   c_parser_statement (parser);
8356   return pop_stmt_list (stmt);
8357 }
8358
8359 /* OpenMP 2.5:
8360    # pragma omp atomic new-line
8361      expression-stmt
8362
8363    expression-stmt:
8364      x binop= expr | x++ | ++x | x-- | --x
8365    binop:
8366      +, *, -, /, &, ^, |, <<, >>
8367
8368   where x is an lvalue expression with scalar type.
8369
8370   LOC is the location of the #pragma token.  */
8371
8372 static void
8373 c_parser_omp_atomic (location_t loc, c_parser *parser)
8374 {
8375   tree lhs, rhs;
8376   tree stmt;
8377   enum tree_code code;
8378   struct c_expr rhs_expr;
8379
8380   c_parser_skip_to_pragma_eol (parser);
8381
8382   lhs = c_parser_unary_expression (parser).value;
8383   lhs = c_fully_fold (lhs, false, NULL);
8384   switch (TREE_CODE (lhs))
8385     {
8386     case ERROR_MARK:
8387     saw_error:
8388       c_parser_skip_to_end_of_block_or_statement (parser);
8389       return;
8390
8391     case PREINCREMENT_EXPR:
8392     case POSTINCREMENT_EXPR:
8393       lhs = TREE_OPERAND (lhs, 0);
8394       code = PLUS_EXPR;
8395       rhs = integer_one_node;
8396       break;
8397
8398     case PREDECREMENT_EXPR:
8399     case POSTDECREMENT_EXPR:
8400       lhs = TREE_OPERAND (lhs, 0);
8401       code = MINUS_EXPR;
8402       rhs = integer_one_node;
8403       break;
8404
8405     case COMPOUND_EXPR:
8406       if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
8407           && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
8408           && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
8409           && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
8410           && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
8411                                               (TREE_OPERAND (lhs, 1), 0), 0)))
8412              == BOOLEAN_TYPE)
8413         /* Undo effects of boolean_increment for post {in,de}crement.  */
8414         lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
8415       /* FALLTHRU */
8416     case MODIFY_EXPR:
8417       if (TREE_CODE (lhs) == MODIFY_EXPR
8418           && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
8419         {
8420           /* Undo effects of boolean_increment.  */
8421           if (integer_onep (TREE_OPERAND (lhs, 1)))
8422             {
8423               /* This is pre or post increment.  */
8424               rhs = TREE_OPERAND (lhs, 1);
8425               lhs = TREE_OPERAND (lhs, 0);
8426               code = NOP_EXPR;
8427               break;
8428             }
8429           if (TREE_CODE (TREE_OPERAND (lhs, 1)) == TRUTH_NOT_EXPR
8430               && TREE_OPERAND (lhs, 0)
8431                  == TREE_OPERAND (TREE_OPERAND (lhs, 1), 0))
8432             {
8433               /* This is pre or post decrement.  */
8434               rhs = TREE_OPERAND (lhs, 1);
8435               lhs = TREE_OPERAND (lhs, 0);
8436               code = NOP_EXPR;
8437               break;
8438             }
8439         }
8440       /* FALLTHRU */
8441     default:
8442       switch (c_parser_peek_token (parser)->type)
8443         {
8444         case CPP_MULT_EQ:
8445           code = MULT_EXPR;
8446           break;
8447         case CPP_DIV_EQ:
8448           code = TRUNC_DIV_EXPR;
8449           break;
8450         case CPP_PLUS_EQ:
8451           code = PLUS_EXPR;
8452           break;
8453         case CPP_MINUS_EQ:
8454           code = MINUS_EXPR;
8455           break;
8456         case CPP_LSHIFT_EQ:
8457           code = LSHIFT_EXPR;
8458           break;
8459         case CPP_RSHIFT_EQ:
8460           code = RSHIFT_EXPR;
8461           break;
8462         case CPP_AND_EQ:
8463           code = BIT_AND_EXPR;
8464           break;
8465         case CPP_OR_EQ:
8466           code = BIT_IOR_EXPR;
8467           break;
8468         case CPP_XOR_EQ:
8469           code = BIT_XOR_EXPR;
8470           break;
8471         default:
8472           c_parser_error (parser,
8473                           "invalid operator for %<#pragma omp atomic%>");
8474           goto saw_error;
8475         }
8476
8477       c_parser_consume_token (parser);
8478       {
8479         location_t rhs_loc = c_parser_peek_token (parser)->location;
8480         rhs_expr = c_parser_expression (parser);
8481         rhs_expr = default_function_array_read_conversion (rhs_loc, rhs_expr);
8482       }
8483       rhs = rhs_expr.value;
8484       rhs = c_fully_fold (rhs, false, NULL);
8485       break;
8486     }
8487   stmt = c_finish_omp_atomic (loc, code, lhs, rhs);
8488   if (stmt != error_mark_node)
8489     add_stmt (stmt);
8490   c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
8491 }
8492
8493
8494 /* OpenMP 2.5:
8495    # pragma omp barrier new-line
8496 */
8497
8498 static void
8499 c_parser_omp_barrier (c_parser *parser)
8500 {
8501   location_t loc = c_parser_peek_token (parser)->location;
8502   c_parser_consume_pragma (parser);
8503   c_parser_skip_to_pragma_eol (parser);
8504
8505   c_finish_omp_barrier (loc);
8506 }
8507
8508 /* OpenMP 2.5:
8509    # pragma omp critical [(name)] new-line
8510      structured-block
8511
8512   LOC is the location of the #pragma itself.  */
8513
8514 static tree
8515 c_parser_omp_critical (location_t loc, c_parser *parser)
8516 {
8517   tree stmt, name = NULL;
8518
8519   if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
8520     {
8521       c_parser_consume_token (parser);
8522       if (c_parser_next_token_is (parser, CPP_NAME))
8523         {
8524           name = c_parser_peek_token (parser)->value;
8525           c_parser_consume_token (parser);
8526           c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
8527         }
8528       else
8529         c_parser_error (parser, "expected identifier");
8530     }
8531   else if (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
8532     c_parser_error (parser, "expected %<(%> or end of line");
8533   c_parser_skip_to_pragma_eol (parser);
8534
8535   stmt = c_parser_omp_structured_block (parser);
8536   return c_finish_omp_critical (loc, stmt, name);
8537 }
8538
8539 /* OpenMP 2.5:
8540    # pragma omp flush flush-vars[opt] new-line
8541
8542    flush-vars:
8543      ( variable-list ) */
8544
8545 static void
8546 c_parser_omp_flush (c_parser *parser)
8547 {
8548   location_t loc = c_parser_peek_token (parser)->location;
8549   c_parser_consume_pragma (parser);
8550   if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
8551     c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
8552   else if (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
8553     c_parser_error (parser, "expected %<(%> or end of line");
8554   c_parser_skip_to_pragma_eol (parser);
8555
8556   c_finish_omp_flush (loc);
8557 }
8558
8559 /* Parse the restricted form of the for statement allowed by OpenMP.
8560    The real trick here is to determine the loop control variable early
8561    so that we can push a new decl if necessary to make it private.
8562    LOC is the location of the OMP in "#pragma omp".  */
8563
8564 static tree
8565 c_parser_omp_for_loop (location_t loc,
8566                        c_parser *parser, tree clauses, tree *par_clauses)
8567 {
8568   tree decl, cond, incr, save_break, save_cont, body, init, stmt, cl;
8569   tree declv, condv, incrv, initv, ret = NULL;
8570   bool fail = false, open_brace_parsed = false;
8571   int i, collapse = 1, nbraces = 0;
8572   location_t for_loc;
8573   VEC(tree,gc) *for_block = make_tree_vector ();
8574
8575   for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
8576     if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
8577       collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
8578
8579   gcc_assert (collapse >= 1);
8580
8581   declv = make_tree_vec (collapse);
8582   initv = make_tree_vec (collapse);
8583   condv = make_tree_vec (collapse);
8584   incrv = make_tree_vec (collapse);
8585
8586   if (!c_parser_next_token_is_keyword (parser, RID_FOR))
8587     {
8588       c_parser_error (parser, "for statement expected");
8589       return NULL;
8590     }
8591   for_loc = c_parser_peek_token (parser)->location;
8592   c_parser_consume_token (parser);
8593
8594   for (i = 0; i < collapse; i++)
8595     {
8596       int bracecount = 0;
8597
8598       if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
8599         goto pop_scopes;
8600
8601       /* Parse the initialization declaration or expression.  */
8602       if (c_parser_next_token_starts_declaration (parser))
8603         {
8604           if (i > 0)
8605             VEC_safe_push (tree, gc, for_block, c_begin_compound_stmt (true));
8606           c_parser_declaration_or_fndef (parser, true, true, true, true, true, NULL);
8607           decl = check_for_loop_decls (for_loc, flag_isoc99);
8608           if (decl == NULL)
8609             goto error_init;
8610           if (DECL_INITIAL (decl) == error_mark_node)
8611             decl = error_mark_node;
8612           init = decl;
8613         }
8614       else if (c_parser_next_token_is (parser, CPP_NAME)
8615                && c_parser_peek_2nd_token (parser)->type == CPP_EQ)
8616         {
8617           struct c_expr decl_exp;
8618           struct c_expr init_exp;
8619           location_t init_loc;
8620
8621           decl_exp = c_parser_postfix_expression (parser);
8622           decl = decl_exp.value;
8623
8624           c_parser_require (parser, CPP_EQ, "expected %<=%>");
8625
8626           init_loc = c_parser_peek_token (parser)->location;
8627           init_exp = c_parser_expr_no_commas (parser, NULL);
8628           init_exp = default_function_array_read_conversion (init_loc,
8629                                                              init_exp);
8630           init = build_modify_expr (init_loc, decl, decl_exp.original_type,
8631                                     NOP_EXPR, init_loc, init_exp.value,
8632                                     init_exp.original_type);
8633           init = c_process_expr_stmt (init_loc, init);
8634
8635           c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
8636         }
8637       else
8638         {
8639         error_init:
8640           c_parser_error (parser,
8641                           "expected iteration declaration or initialization");
8642           c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
8643                                      "expected %<)%>");
8644           fail = true;
8645           goto parse_next;
8646         }
8647
8648       /* Parse the loop condition.  */
8649       cond = NULL_TREE;
8650       if (c_parser_next_token_is_not (parser, CPP_SEMICOLON))
8651         {
8652           location_t cond_loc = c_parser_peek_token (parser)->location;
8653           struct c_expr cond_expr = c_parser_binary_expression (parser, NULL);
8654
8655           cond = cond_expr.value;
8656           cond = c_objc_common_truthvalue_conversion (cond_loc, cond);
8657           cond = c_fully_fold (cond, false, NULL);
8658           switch (cond_expr.original_code)
8659             {
8660             case GT_EXPR:
8661             case GE_EXPR:
8662             case LT_EXPR:
8663             case LE_EXPR:
8664               break;
8665             default:
8666               /* Can't be cond = error_mark_node, because we want to preserve
8667                  the location until c_finish_omp_for.  */
8668               cond = build1 (NOP_EXPR, boolean_type_node, error_mark_node);
8669               break;
8670             }
8671           protected_set_expr_location (cond, cond_loc);
8672         }
8673       c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
8674
8675       /* Parse the increment expression.  */
8676       incr = NULL_TREE;
8677       if (c_parser_next_token_is_not (parser, CPP_CLOSE_PAREN))
8678         {
8679           location_t incr_loc = c_parser_peek_token (parser)->location;
8680
8681           incr = c_process_expr_stmt (incr_loc,
8682                                       c_parser_expression (parser).value);
8683         }
8684       c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
8685
8686       if (decl == NULL || decl == error_mark_node || init == error_mark_node)
8687         fail = true;
8688       else
8689         {
8690           TREE_VEC_ELT (declv, i) = decl;
8691           TREE_VEC_ELT (initv, i) = init;
8692           TREE_VEC_ELT (condv, i) = cond;
8693           TREE_VEC_ELT (incrv, i) = incr;
8694         }
8695
8696     parse_next:
8697       if (i == collapse - 1)
8698         break;
8699
8700       /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
8701          in between the collapsed for loops to be still considered perfectly
8702          nested.  Hopefully the final version clarifies this.
8703          For now handle (multiple) {'s and empty statements.  */
8704       do
8705         {
8706           if (c_parser_next_token_is_keyword (parser, RID_FOR))
8707             {
8708               c_parser_consume_token (parser);
8709               break;
8710             }
8711           else if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
8712             {
8713               c_parser_consume_token (parser);
8714               bracecount++;
8715             }
8716           else if (bracecount
8717                    && c_parser_next_token_is (parser, CPP_SEMICOLON))
8718             c_parser_consume_token (parser);
8719           else
8720             {
8721               c_parser_error (parser, "not enough perfectly nested loops");
8722               if (bracecount)
8723                 {
8724                   open_brace_parsed = true;
8725                   bracecount--;
8726                 }
8727               fail = true;
8728               collapse = 0;
8729               break;
8730             }
8731         }
8732       while (1);
8733
8734       nbraces += bracecount;
8735     }
8736
8737   save_break = c_break_label;
8738   c_break_label = size_one_node;
8739   save_cont = c_cont_label;
8740   c_cont_label = NULL_TREE;
8741   body = push_stmt_list ();
8742
8743   if (open_brace_parsed)
8744     {
8745       location_t here = c_parser_peek_token (parser)->location;
8746       stmt = c_begin_compound_stmt (true);
8747       c_parser_compound_statement_nostart (parser);
8748       add_stmt (c_end_compound_stmt (here, stmt, true));
8749     }
8750   else
8751     add_stmt (c_parser_c99_block_statement (parser));
8752   if (c_cont_label)
8753     {
8754       tree t = build1 (LABEL_EXPR, void_type_node, c_cont_label);
8755       SET_EXPR_LOCATION (t, loc);
8756       add_stmt (t);
8757     }
8758
8759   body = pop_stmt_list (body);
8760   c_break_label = save_break;
8761   c_cont_label = save_cont;
8762
8763   while (nbraces)
8764     {
8765       if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
8766         {
8767           c_parser_consume_token (parser);
8768           nbraces--;
8769         }
8770       else if (c_parser_next_token_is (parser, CPP_SEMICOLON))
8771         c_parser_consume_token (parser);
8772       else
8773         {
8774           c_parser_error (parser, "collapsed loops not perfectly nested");
8775           while (nbraces)
8776             {
8777               location_t here = c_parser_peek_token (parser)->location;
8778               stmt = c_begin_compound_stmt (true);
8779               add_stmt (body);
8780               c_parser_compound_statement_nostart (parser);
8781               body = c_end_compound_stmt (here, stmt, true);
8782               nbraces--;
8783             }
8784           goto pop_scopes;
8785         }
8786     }
8787
8788   /* Only bother calling c_finish_omp_for if we haven't already generated
8789      an error from the initialization parsing.  */
8790   if (!fail)
8791     {
8792       stmt = c_finish_omp_for (loc, declv, initv, condv, incrv, body, NULL);
8793       if (stmt)
8794         {
8795           if (par_clauses != NULL)
8796             {
8797               tree *c;
8798               for (c = par_clauses; *c ; )
8799                 if (OMP_CLAUSE_CODE (*c) != OMP_CLAUSE_FIRSTPRIVATE
8800                     && OMP_CLAUSE_CODE (*c) != OMP_CLAUSE_LASTPRIVATE)
8801                   c = &OMP_CLAUSE_CHAIN (*c);
8802                 else
8803                   {
8804                     for (i = 0; i < collapse; i++)
8805                       if (TREE_VEC_ELT (declv, i) == OMP_CLAUSE_DECL (*c))
8806                         break;
8807                     if (i == collapse)
8808                       c = &OMP_CLAUSE_CHAIN (*c);
8809                     else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE)
8810                       {
8811                         error_at (loc,
8812                                   "iteration variable %qD should not be firstprivate",
8813                                   OMP_CLAUSE_DECL (*c));
8814                         *c = OMP_CLAUSE_CHAIN (*c);
8815                       }
8816                     else
8817                       {
8818                         /* Copy lastprivate (decl) clause to OMP_FOR_CLAUSES,
8819                            change it to shared (decl) in
8820                            OMP_PARALLEL_CLAUSES.  */
8821                         tree l = build_omp_clause (OMP_CLAUSE_LOCATION (*c),
8822                                                    OMP_CLAUSE_LASTPRIVATE);
8823                         OMP_CLAUSE_DECL (l) = OMP_CLAUSE_DECL (*c);
8824                         OMP_CLAUSE_CHAIN (l) = clauses;
8825                         clauses = l;
8826                         OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
8827                       }
8828                   }
8829             }
8830           OMP_FOR_CLAUSES (stmt) = clauses;
8831         }
8832       ret = stmt;
8833     }
8834 pop_scopes:
8835   while (!VEC_empty (tree, for_block))
8836     {
8837       /* FIXME diagnostics: LOC below should be the actual location of
8838          this particular for block.  We need to build a list of
8839          locations to go along with FOR_BLOCK.  */
8840       stmt = c_end_compound_stmt (loc, VEC_pop (tree, for_block), true);
8841       add_stmt (stmt);
8842     }
8843   release_tree_vector (for_block);
8844   return ret;
8845 }
8846
8847 /* OpenMP 2.5:
8848    #pragma omp for for-clause[optseq] new-line
8849      for-loop
8850
8851    LOC is the location of the #pragma token.
8852 */
8853
8854 #define OMP_FOR_CLAUSE_MASK                             \
8855         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
8856         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
8857         | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)         \
8858         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
8859         | (1u << PRAGMA_OMP_CLAUSE_ORDERED)             \
8860         | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE)            \
8861         | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE)            \
8862         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
8863
8864 static tree
8865 c_parser_omp_for (location_t loc, c_parser *parser)
8866 {
8867   tree block, clauses, ret;
8868
8869   clauses = c_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
8870                                       "#pragma omp for");
8871
8872   block = c_begin_compound_stmt (true);
8873   ret = c_parser_omp_for_loop (loc, parser, clauses, NULL);
8874   block = c_end_compound_stmt (loc, block, true);
8875   add_stmt (block);
8876
8877   return ret;
8878 }
8879
8880 /* OpenMP 2.5:
8881    # pragma omp master new-line
8882      structured-block
8883
8884    LOC is the location of the #pragma token.
8885 */
8886
8887 static tree
8888 c_parser_omp_master (location_t loc, c_parser *parser)
8889 {
8890   c_parser_skip_to_pragma_eol (parser);
8891   return c_finish_omp_master (loc, c_parser_omp_structured_block (parser));
8892 }
8893
8894 /* OpenMP 2.5:
8895    # pragma omp ordered new-line
8896      structured-block
8897
8898    LOC is the location of the #pragma itself.
8899 */
8900
8901 static tree
8902 c_parser_omp_ordered (location_t loc, c_parser *parser)
8903 {
8904   c_parser_skip_to_pragma_eol (parser);
8905   return c_finish_omp_ordered (loc, c_parser_omp_structured_block (parser));
8906 }
8907
8908 /* OpenMP 2.5:
8909
8910    section-scope:
8911      { section-sequence }
8912
8913    section-sequence:
8914      section-directive[opt] structured-block
8915      section-sequence section-directive structured-block
8916
8917     SECTIONS_LOC is the location of the #pragma omp sections.  */
8918
8919 static tree
8920 c_parser_omp_sections_scope (location_t sections_loc, c_parser *parser)
8921 {
8922   tree stmt, substmt;
8923   bool error_suppress = false;
8924   location_t loc;
8925
8926   loc = c_parser_peek_token (parser)->location;
8927   if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
8928     {
8929       /* Avoid skipping until the end of the block.  */
8930       parser->error = false;
8931       return NULL_TREE;
8932     }
8933
8934   stmt = push_stmt_list ();
8935
8936   if (c_parser_peek_token (parser)->pragma_kind != PRAGMA_OMP_SECTION)
8937     {
8938       substmt = push_stmt_list ();
8939
8940       while (1)
8941         {
8942           c_parser_statement (parser);
8943
8944           if (c_parser_peek_token (parser)->pragma_kind == PRAGMA_OMP_SECTION)
8945             break;
8946           if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
8947             break;
8948           if (c_parser_next_token_is (parser, CPP_EOF))
8949             break;
8950         }
8951
8952       substmt = pop_stmt_list (substmt);
8953       substmt = build1 (OMP_SECTION, void_type_node, substmt);
8954       SET_EXPR_LOCATION (substmt, loc);
8955       add_stmt (substmt);
8956     }
8957
8958   while (1)
8959     {
8960       if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
8961         break;
8962       if (c_parser_next_token_is (parser, CPP_EOF))
8963         break;
8964
8965       loc = c_parser_peek_token (parser)->location;
8966       if (c_parser_peek_token (parser)->pragma_kind == PRAGMA_OMP_SECTION)
8967         {
8968           c_parser_consume_pragma (parser);
8969           c_parser_skip_to_pragma_eol (parser);
8970           error_suppress = false;
8971         }
8972       else if (!error_suppress)
8973         {
8974           error_at (loc, "expected %<#pragma omp section%> or %<}%>");
8975           error_suppress = true;
8976         }
8977
8978       substmt = c_parser_omp_structured_block (parser);
8979       substmt = build1 (OMP_SECTION, void_type_node, substmt);
8980       SET_EXPR_LOCATION (substmt, loc);
8981       add_stmt (substmt);
8982     }
8983   c_parser_skip_until_found (parser, CPP_CLOSE_BRACE,
8984                              "expected %<#pragma omp section%> or %<}%>");
8985
8986   substmt = pop_stmt_list (stmt);
8987
8988   stmt = make_node (OMP_SECTIONS);
8989   SET_EXPR_LOCATION (stmt, sections_loc);
8990   TREE_TYPE (stmt) = void_type_node;
8991   OMP_SECTIONS_BODY (stmt) = substmt;
8992
8993   return add_stmt (stmt);
8994 }
8995
8996 /* OpenMP 2.5:
8997    # pragma omp sections sections-clause[optseq] newline
8998      sections-scope
8999
9000    LOC is the location of the #pragma token.
9001 */
9002
9003 #define OMP_SECTIONS_CLAUSE_MASK                        \
9004         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
9005         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
9006         | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)         \
9007         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
9008         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
9009
9010 static tree
9011 c_parser_omp_sections (location_t loc, c_parser *parser)
9012 {
9013   tree block, clauses, ret;
9014
9015   clauses = c_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
9016                                       "#pragma omp sections");
9017
9018   block = c_begin_compound_stmt (true);
9019   ret = c_parser_omp_sections_scope (loc, parser);
9020   if (ret)
9021     OMP_SECTIONS_CLAUSES (ret) = clauses;
9022   block = c_end_compound_stmt (loc, block, true);
9023   add_stmt (block);
9024
9025   return ret;
9026 }
9027
9028 /* OpenMP 2.5:
9029    # pragma parallel parallel-clause new-line
9030    # pragma parallel for parallel-for-clause new-line
9031    # pragma parallel sections parallel-sections-clause new-line
9032
9033    LOC is the location of the #pragma token.
9034 */
9035
9036 #define OMP_PARALLEL_CLAUSE_MASK                        \
9037         ( (1u << PRAGMA_OMP_CLAUSE_IF)                  \
9038         | (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
9039         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
9040         | (1u << PRAGMA_OMP_CLAUSE_DEFAULT)             \
9041         | (1u << PRAGMA_OMP_CLAUSE_SHARED)              \
9042         | (1u << PRAGMA_OMP_CLAUSE_COPYIN)              \
9043         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
9044         | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
9045
9046 static tree
9047 c_parser_omp_parallel (location_t loc, c_parser *parser)
9048 {
9049   enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
9050   const char *p_name = "#pragma omp parallel";
9051   tree stmt, clauses, par_clause, ws_clause, block;
9052   unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
9053
9054   if (c_parser_next_token_is_keyword (parser, RID_FOR))
9055     {
9056       c_parser_consume_token (parser);
9057       p_kind = PRAGMA_OMP_PARALLEL_FOR;
9058       p_name = "#pragma omp parallel for";
9059       mask |= OMP_FOR_CLAUSE_MASK;
9060       mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
9061     }
9062   else if (c_parser_next_token_is (parser, CPP_NAME))
9063     {
9064       const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
9065       if (strcmp (p, "sections") == 0)
9066         {
9067           c_parser_consume_token (parser);
9068           p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
9069           p_name = "#pragma omp parallel sections";
9070           mask |= OMP_SECTIONS_CLAUSE_MASK;
9071           mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
9072         }
9073     }
9074
9075   clauses = c_parser_omp_all_clauses (parser, mask, p_name);
9076
9077   switch (p_kind)
9078     {
9079     case PRAGMA_OMP_PARALLEL:
9080       block = c_begin_omp_parallel ();
9081       c_parser_statement (parser);
9082       stmt = c_finish_omp_parallel (loc, clauses, block);
9083       break;
9084
9085     case PRAGMA_OMP_PARALLEL_FOR:
9086       block = c_begin_omp_parallel ();
9087       c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
9088       c_parser_omp_for_loop (loc, parser, ws_clause, &par_clause);
9089       stmt = c_finish_omp_parallel (loc, par_clause, block);
9090       OMP_PARALLEL_COMBINED (stmt) = 1;
9091       break;
9092
9093     case PRAGMA_OMP_PARALLEL_SECTIONS:
9094       block = c_begin_omp_parallel ();
9095       c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
9096       stmt = c_parser_omp_sections_scope (loc, parser);
9097       if (stmt)
9098         OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
9099       stmt = c_finish_omp_parallel (loc, par_clause, block);
9100       OMP_PARALLEL_COMBINED (stmt) = 1;
9101       break;
9102
9103     default:
9104       gcc_unreachable ();
9105     }
9106
9107   return stmt;
9108 }
9109
9110 /* OpenMP 2.5:
9111    # pragma omp single single-clause[optseq] new-line
9112      structured-block
9113
9114    LOC is the location of the #pragma.
9115 */
9116
9117 #define OMP_SINGLE_CLAUSE_MASK                          \
9118         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
9119         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
9120         | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE)         \
9121         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
9122
9123 static tree
9124 c_parser_omp_single (location_t loc, c_parser *parser)
9125 {
9126   tree stmt = make_node (OMP_SINGLE);
9127   SET_EXPR_LOCATION (stmt, loc);
9128   TREE_TYPE (stmt) = void_type_node;
9129
9130   OMP_SINGLE_CLAUSES (stmt)
9131     = c_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
9132                                 "#pragma omp single");
9133   OMP_SINGLE_BODY (stmt) = c_parser_omp_structured_block (parser);
9134
9135   return add_stmt (stmt);
9136 }
9137
9138 /* OpenMP 3.0:
9139    # pragma omp task task-clause[optseq] new-line
9140
9141    LOC is the location of the #pragma.
9142 */
9143
9144 #define OMP_TASK_CLAUSE_MASK                            \
9145         ( (1u << PRAGMA_OMP_CLAUSE_IF)                  \
9146         | (1u << PRAGMA_OMP_CLAUSE_UNTIED)              \
9147         | (1u << PRAGMA_OMP_CLAUSE_DEFAULT)             \
9148         | (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
9149         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
9150         | (1u << PRAGMA_OMP_CLAUSE_SHARED))
9151
9152 static tree
9153 c_parser_omp_task (location_t loc, c_parser *parser)
9154 {
9155   tree clauses, block;
9156
9157   clauses = c_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
9158                                       "#pragma omp task");
9159
9160   block = c_begin_omp_task ();
9161   c_parser_statement (parser);
9162   return c_finish_omp_task (loc, clauses, block);
9163 }
9164
9165 /* OpenMP 3.0:
9166    # pragma omp taskwait new-line
9167 */
9168
9169 static void
9170 c_parser_omp_taskwait (c_parser *parser)
9171 {
9172   location_t loc = c_parser_peek_token (parser)->location;
9173   c_parser_consume_pragma (parser);
9174   c_parser_skip_to_pragma_eol (parser);
9175
9176   c_finish_omp_taskwait (loc);
9177 }
9178
9179 /* Main entry point to parsing most OpenMP pragmas.  */
9180
9181 static void
9182 c_parser_omp_construct (c_parser *parser)
9183 {
9184   enum pragma_kind p_kind;
9185   location_t loc;
9186   tree stmt;
9187
9188   loc = c_parser_peek_token (parser)->location;
9189   p_kind = c_parser_peek_token (parser)->pragma_kind;
9190   c_parser_consume_pragma (parser);
9191
9192   switch (p_kind)
9193     {
9194     case PRAGMA_OMP_ATOMIC:
9195       c_parser_omp_atomic (loc, parser);
9196       return;
9197     case PRAGMA_OMP_CRITICAL:
9198       stmt = c_parser_omp_critical (loc, parser);
9199       break;
9200     case PRAGMA_OMP_FOR:
9201       stmt = c_parser_omp_for (loc, parser);
9202       break;
9203     case PRAGMA_OMP_MASTER:
9204       stmt = c_parser_omp_master (loc, parser);
9205       break;
9206     case PRAGMA_OMP_ORDERED:
9207       stmt = c_parser_omp_ordered (loc, parser);
9208       break;
9209     case PRAGMA_OMP_PARALLEL:
9210       stmt = c_parser_omp_parallel (loc, parser);
9211       break;
9212     case PRAGMA_OMP_SECTIONS:
9213       stmt = c_parser_omp_sections (loc, parser);
9214       break;
9215     case PRAGMA_OMP_SINGLE:
9216       stmt = c_parser_omp_single (loc, parser);
9217       break;
9218     case PRAGMA_OMP_TASK:
9219       stmt = c_parser_omp_task (loc, parser);
9220       break;
9221     default:
9222       gcc_unreachable ();
9223     }
9224
9225   if (stmt)
9226     gcc_assert (EXPR_LOCATION (stmt) != UNKNOWN_LOCATION);
9227 }
9228
9229
9230 /* OpenMP 2.5:
9231    # pragma omp threadprivate (variable-list) */
9232
9233 static void
9234 c_parser_omp_threadprivate (c_parser *parser)
9235 {
9236   tree vars, t;
9237   location_t loc;
9238
9239   c_parser_consume_pragma (parser);
9240   loc = c_parser_peek_token (parser)->location;
9241   vars = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
9242
9243   /* Mark every variable in VARS to be assigned thread local storage.  */
9244   for (t = vars; t; t = TREE_CHAIN (t))
9245     {
9246       tree v = TREE_PURPOSE (t);
9247
9248       /* FIXME diagnostics: Ideally we should keep individual
9249          locations for all the variables in the var list to make the
9250          following errors more precise.  Perhaps
9251          c_parser_omp_var_list_parens() should construct a list of
9252          locations to go along with the var list.  */
9253
9254       /* If V had already been marked threadprivate, it doesn't matter
9255          whether it had been used prior to this point.  */
9256       if (TREE_CODE (v) != VAR_DECL)
9257         error_at (loc, "%qD is not a variable", v);
9258       else if (TREE_USED (v) && !C_DECL_THREADPRIVATE_P (v))
9259         error_at (loc, "%qE declared %<threadprivate%> after first use", v);
9260       else if (! TREE_STATIC (v) && ! DECL_EXTERNAL (v))
9261         error_at (loc, "automatic variable %qE cannot be %<threadprivate%>", v);
9262       else if (TREE_TYPE (v) == error_mark_node)
9263         ;
9264       else if (! COMPLETE_TYPE_P (TREE_TYPE (v)))
9265         error_at (loc, "%<threadprivate%> %qE has incomplete type", v);
9266       else
9267         {
9268           if (! DECL_THREAD_LOCAL_P (v))
9269             {
9270               DECL_TLS_MODEL (v) = decl_default_tls_model (v);
9271               /* If rtl has been already set for this var, call
9272                  make_decl_rtl once again, so that encode_section_info
9273                  has a chance to look at the new decl flags.  */
9274               if (DECL_RTL_SET_P (v))
9275                 make_decl_rtl (v);
9276             }
9277           C_DECL_THREADPRIVATE_P (v) = 1;
9278         }
9279     }
9280
9281   c_parser_skip_to_pragma_eol (parser);
9282 }
9283
9284 \f
9285 /* Parse a single source file.  */
9286
9287 void
9288 c_parse_file (void)
9289 {
9290   /* Use local storage to begin.  If the first token is a pragma, parse it.
9291      If it is #pragma GCC pch_preprocess, then this will load a PCH file
9292      which will cause garbage collection.  */
9293   c_parser tparser;
9294
9295   memset (&tparser, 0, sizeof tparser);
9296   the_parser = &tparser;
9297
9298   if (c_parser_peek_token (&tparser)->pragma_kind == PRAGMA_GCC_PCH_PREPROCESS)
9299     c_parser_pragma_pch_preprocess (&tparser);
9300
9301   the_parser = ggc_alloc_c_parser ();
9302   *the_parser = tparser;
9303
9304   /* Initialize EH, if we've been told to do so.  */
9305   if (flag_exceptions)
9306     using_eh_for_cleanups ();
9307
9308   c_parser_translation_unit (the_parser);
9309   the_parser = NULL;
9310 }
9311
9312 #include "gt-c-parser.h"