OSDN Git Service

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