OSDN Git Service

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