OSDN Git Service

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