OSDN Git Service

PR middle-end/46388
[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       || c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2400     {
2401       tree ret;
2402       if (!specs->type_seen_p)
2403         {
2404           pedwarn (decl_loc, OPT_pedantic,
2405                    "ISO C forbids member declarations with no members");
2406           shadow_tag_warned (specs, pedantic);
2407           ret = NULL_TREE;
2408         }
2409       else
2410         {
2411           /* Support for unnamed structs or unions as members of
2412              structs or unions (which is [a] useful and [b] supports
2413              MS P-SDK).  */
2414           tree attrs = NULL;
2415
2416           ret = grokfield (c_parser_peek_token (parser)->location,
2417                            build_id_declarator (NULL_TREE), specs,
2418                            NULL_TREE, &attrs);
2419           if (ret)
2420             decl_attributes (&ret, attrs, 0);
2421         }
2422       return ret;
2423     }
2424   pending_xref_error ();
2425   prefix_attrs = specs->attrs;
2426   all_prefix_attrs = prefix_attrs;
2427   specs->attrs = NULL_TREE;
2428   decls = NULL_TREE;
2429   while (true)
2430     {
2431       /* Declaring one or more declarators or un-named bit-fields.  */
2432       struct c_declarator *declarator;
2433       bool dummy = false;
2434       if (c_parser_next_token_is (parser, CPP_COLON))
2435         declarator = build_id_declarator (NULL_TREE);
2436       else
2437         declarator = c_parser_declarator (parser, specs->type_seen_p,
2438                                           C_DTR_NORMAL, &dummy);
2439       if (declarator == NULL)
2440         {
2441           c_parser_skip_to_end_of_block_or_statement (parser);
2442           break;
2443         }
2444       if (c_parser_next_token_is (parser, CPP_COLON)
2445           || c_parser_next_token_is (parser, CPP_COMMA)
2446           || c_parser_next_token_is (parser, CPP_SEMICOLON)
2447           || c_parser_next_token_is (parser, CPP_CLOSE_BRACE)
2448           || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2449         {
2450           tree postfix_attrs = NULL_TREE;
2451           tree width = NULL_TREE;
2452           tree d;
2453           if (c_parser_next_token_is (parser, CPP_COLON))
2454             {
2455               c_parser_consume_token (parser);
2456               width = c_parser_expr_no_commas (parser, NULL).value;
2457             }
2458           if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2459             postfix_attrs = c_parser_attributes (parser);
2460           d = grokfield (c_parser_peek_token (parser)->location,
2461                          declarator, specs, width, &all_prefix_attrs);
2462           decl_attributes (&d, chainon (postfix_attrs,
2463                                         all_prefix_attrs), 0);
2464           DECL_CHAIN (d) = decls;
2465           decls = d;
2466           if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2467             all_prefix_attrs = chainon (c_parser_attributes (parser),
2468                                         prefix_attrs);
2469           else
2470             all_prefix_attrs = prefix_attrs;
2471           if (c_parser_next_token_is (parser, CPP_COMMA))
2472             c_parser_consume_token (parser);
2473           else if (c_parser_next_token_is (parser, CPP_SEMICOLON)
2474                    || c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2475             {
2476               /* Semicolon consumed in caller.  */
2477               break;
2478             }
2479           else
2480             {
2481               c_parser_error (parser, "expected %<,%>, %<;%> or %<}%>");
2482               break;
2483             }
2484         }
2485       else
2486         {
2487           c_parser_error (parser,
2488                           "expected %<:%>, %<,%>, %<;%>, %<}%> or "
2489                           "%<__attribute__%>");
2490           break;
2491         }
2492     }
2493   return decls;
2494 }
2495
2496 /* Parse a typeof specifier (a GNU extension).
2497
2498    typeof-specifier:
2499      typeof ( expression )
2500      typeof ( type-name )
2501 */
2502
2503 static struct c_typespec
2504 c_parser_typeof_specifier (c_parser *parser)
2505 {
2506   struct c_typespec ret;
2507   ret.kind = ctsk_typeof;
2508   ret.spec = error_mark_node;
2509   ret.expr = NULL_TREE;
2510   ret.expr_const_operands = true;
2511   gcc_assert (c_parser_next_token_is_keyword (parser, RID_TYPEOF));
2512   c_parser_consume_token (parser);
2513   c_inhibit_evaluation_warnings++;
2514   in_typeof++;
2515   if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2516     {
2517       c_inhibit_evaluation_warnings--;
2518       in_typeof--;
2519       return ret;
2520     }
2521   if (c_parser_next_token_starts_typename (parser))
2522     {
2523       struct c_type_name *type = c_parser_type_name (parser);
2524       c_inhibit_evaluation_warnings--;
2525       in_typeof--;
2526       if (type != NULL)
2527         {
2528           ret.spec = groktypename (type, &ret.expr, &ret.expr_const_operands);
2529           pop_maybe_used (variably_modified_type_p (ret.spec, NULL_TREE));
2530         }
2531     }
2532   else
2533     {
2534       bool was_vm;
2535       location_t here = c_parser_peek_token (parser)->location;
2536       struct c_expr expr = c_parser_expression (parser);
2537       c_inhibit_evaluation_warnings--;
2538       in_typeof--;
2539       if (TREE_CODE (expr.value) == COMPONENT_REF
2540           && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1)))
2541         error_at (here, "%<typeof%> applied to a bit-field");
2542       mark_exp_read (expr.value);
2543       ret.spec = TREE_TYPE (expr.value);
2544       if (c_dialect_objc() 
2545           && ret.spec != error_mark_node
2546           && lookup_attribute ("objc_volatilized", TYPE_ATTRIBUTES (ret.spec)))
2547         ret.spec = build_qualified_type
2548           (ret.spec, (TYPE_QUALS (ret.spec) & ~TYPE_QUAL_VOLATILE));
2549       was_vm = variably_modified_type_p (ret.spec, NULL_TREE);
2550       /* This is returned with the type so that when the type is
2551          evaluated, this can be evaluated.  */
2552       if (was_vm)
2553         ret.expr = c_fully_fold (expr.value, false, &ret.expr_const_operands);
2554       pop_maybe_used (was_vm);
2555     }
2556   c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
2557   return ret;
2558 }
2559
2560 /* Parse a declarator, possibly an abstract declarator (C90 6.5.4,
2561    6.5.5, C99 6.7.5, 6.7.6).  If TYPE_SEEN_P then a typedef name may
2562    be redeclared; otherwise it may not.  KIND indicates which kind of
2563    declarator is wanted.  Returns a valid declarator except in the
2564    case of a syntax error in which case NULL is returned.  *SEEN_ID is
2565    set to true if an identifier being declared is seen; this is used
2566    to diagnose bad forms of abstract array declarators and to
2567    determine whether an identifier list is syntactically permitted.
2568
2569    declarator:
2570      pointer[opt] direct-declarator
2571
2572    direct-declarator:
2573      identifier
2574      ( attributes[opt] declarator )
2575      direct-declarator array-declarator
2576      direct-declarator ( parameter-type-list )
2577      direct-declarator ( identifier-list[opt] )
2578
2579    pointer:
2580      * type-qualifier-list[opt]
2581      * type-qualifier-list[opt] pointer
2582
2583    type-qualifier-list:
2584      type-qualifier
2585      attributes
2586      type-qualifier-list type-qualifier
2587      type-qualifier-list attributes
2588
2589    parameter-type-list:
2590      parameter-list
2591      parameter-list , ...
2592
2593    parameter-list:
2594      parameter-declaration
2595      parameter-list , parameter-declaration
2596
2597    parameter-declaration:
2598      declaration-specifiers declarator attributes[opt]
2599      declaration-specifiers abstract-declarator[opt] attributes[opt]
2600
2601    identifier-list:
2602      identifier
2603      identifier-list , identifier
2604
2605    abstract-declarator:
2606      pointer
2607      pointer[opt] direct-abstract-declarator
2608
2609    direct-abstract-declarator:
2610      ( attributes[opt] abstract-declarator )
2611      direct-abstract-declarator[opt] array-declarator
2612      direct-abstract-declarator[opt] ( parameter-type-list[opt] )
2613
2614    GNU extensions:
2615
2616    direct-declarator:
2617      direct-declarator ( parameter-forward-declarations
2618                          parameter-type-list[opt] )
2619
2620    direct-abstract-declarator:
2621      direct-abstract-declarator[opt] ( parameter-forward-declarations
2622                                        parameter-type-list[opt] )
2623
2624    parameter-forward-declarations:
2625      parameter-list ;
2626      parameter-forward-declarations parameter-list ;
2627
2628    The uses of attributes shown above are GNU extensions.
2629
2630    Some forms of array declarator are not included in C99 in the
2631    syntax for abstract declarators; these are disallowed elsewhere.
2632    This may be a defect (DR#289).
2633
2634    This function also accepts an omitted abstract declarator as being
2635    an abstract declarator, although not part of the formal syntax.  */
2636
2637 static struct c_declarator *
2638 c_parser_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
2639                      bool *seen_id)
2640 {
2641   /* Parse any initial pointer part.  */
2642   if (c_parser_next_token_is (parser, CPP_MULT))
2643     {
2644       struct c_declspecs *quals_attrs = build_null_declspecs ();
2645       struct c_declarator *inner;
2646       c_parser_consume_token (parser);
2647       c_parser_declspecs (parser, quals_attrs, false, false, true);
2648       inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
2649       if (inner == NULL)
2650         return NULL;
2651       else
2652         return make_pointer_declarator (quals_attrs, inner);
2653     }
2654   /* Now we have a direct declarator, direct abstract declarator or
2655      nothing (which counts as a direct abstract declarator here).  */
2656   return c_parser_direct_declarator (parser, type_seen_p, kind, seen_id);
2657 }
2658
2659 /* Parse a direct declarator or direct abstract declarator; arguments
2660    as c_parser_declarator.  */
2661
2662 static struct c_declarator *
2663 c_parser_direct_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
2664                             bool *seen_id)
2665 {
2666   /* The direct declarator must start with an identifier (possibly
2667      omitted) or a parenthesized declarator (possibly abstract).  In
2668      an ordinary declarator, initial parentheses must start a
2669      parenthesized declarator.  In an abstract declarator or parameter
2670      declarator, they could start a parenthesized declarator or a
2671      parameter list.  To tell which, the open parenthesis and any
2672      following attributes must be read.  If a declaration specifier
2673      follows, then it is a parameter list; if the specifier is a
2674      typedef name, there might be an ambiguity about redeclaring it,
2675      which is resolved in the direction of treating it as a typedef
2676      name.  If a close parenthesis follows, it is also an empty
2677      parameter list, as the syntax does not permit empty abstract
2678      declarators.  Otherwise, it is a parenthesized declarator (in
2679      which case the analysis may be repeated inside it, recursively).
2680
2681      ??? There is an ambiguity in a parameter declaration "int
2682      (__attribute__((foo)) x)", where x is not a typedef name: it
2683      could be an abstract declarator for a function, or declare x with
2684      parentheses.  The proper resolution of this ambiguity needs
2685      documenting.  At present we follow an accident of the old
2686      parser's implementation, whereby the first parameter must have
2687      some declaration specifiers other than just attributes.  Thus as
2688      a parameter declaration it is treated as a parenthesized
2689      parameter named x, and as an abstract declarator it is
2690      rejected.
2691
2692      ??? Also following the old parser, attributes inside an empty
2693      parameter list are ignored, making it a list not yielding a
2694      prototype, rather than giving an error or making it have one
2695      parameter with implicit type int.
2696
2697      ??? Also following the old parser, typedef names may be
2698      redeclared in declarators, but not Objective-C class names.  */
2699
2700   if (kind != C_DTR_ABSTRACT
2701       && c_parser_next_token_is (parser, CPP_NAME)
2702       && ((type_seen_p
2703            && (c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME
2704                || c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME))
2705           || c_parser_peek_token (parser)->id_kind == C_ID_ID))
2706     {
2707       struct c_declarator *inner
2708         = build_id_declarator (c_parser_peek_token (parser)->value);
2709       *seen_id = true;
2710       inner->id_loc = c_parser_peek_token (parser)->location;
2711       c_parser_consume_token (parser);
2712       return c_parser_direct_declarator_inner (parser, *seen_id, inner);
2713     }
2714
2715   if (kind != C_DTR_NORMAL
2716       && c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
2717     {
2718       struct c_declarator *inner = build_id_declarator (NULL_TREE);
2719       return c_parser_direct_declarator_inner (parser, *seen_id, inner);
2720     }
2721
2722   /* Either we are at the end of an abstract declarator, or we have
2723      parentheses.  */
2724
2725   if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
2726     {
2727       tree attrs;
2728       struct c_declarator *inner;
2729       c_parser_consume_token (parser);
2730       attrs = c_parser_attributes (parser);
2731       if (kind != C_DTR_NORMAL
2732           && (c_parser_next_token_starts_declspecs (parser)
2733               || c_parser_next_token_is (parser, CPP_CLOSE_PAREN)))
2734         {
2735           struct c_arg_info *args
2736             = c_parser_parms_declarator (parser, kind == C_DTR_NORMAL,
2737                                          attrs);
2738           if (args == NULL)
2739             return NULL;
2740           else
2741             {
2742               inner
2743                 = build_function_declarator (args,
2744                                              build_id_declarator (NULL_TREE));
2745               return c_parser_direct_declarator_inner (parser, *seen_id,
2746                                                        inner);
2747             }
2748         }
2749       /* A parenthesized declarator.  */
2750       inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
2751       if (inner != NULL && attrs != NULL)
2752         inner = build_attrs_declarator (attrs, inner);
2753       if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2754         {
2755           c_parser_consume_token (parser);
2756           if (inner == NULL)
2757             return NULL;
2758           else
2759             return c_parser_direct_declarator_inner (parser, *seen_id, inner);
2760         }
2761       else
2762         {
2763           c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2764                                      "expected %<)%>");
2765           return NULL;
2766         }
2767     }
2768   else
2769     {
2770       if (kind == C_DTR_NORMAL)
2771         {
2772           c_parser_error (parser, "expected identifier or %<(%>");
2773           return NULL;
2774         }
2775       else
2776         return build_id_declarator (NULL_TREE);
2777     }
2778 }
2779
2780 /* Parse part of a direct declarator or direct abstract declarator,
2781    given that some (in INNER) has already been parsed; ID_PRESENT is
2782    true if an identifier is present, false for an abstract
2783    declarator.  */
2784
2785 static struct c_declarator *
2786 c_parser_direct_declarator_inner (c_parser *parser, bool id_present,
2787                                   struct c_declarator *inner)
2788 {
2789   /* Parse a sequence of array declarators and parameter lists.  */
2790   if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
2791     {
2792       location_t brace_loc = c_parser_peek_token (parser)->location;
2793       struct c_declarator *declarator;
2794       struct c_declspecs *quals_attrs = build_null_declspecs ();
2795       bool static_seen;
2796       bool star_seen;
2797       tree dimen;
2798       c_parser_consume_token (parser);
2799       c_parser_declspecs (parser, quals_attrs, false, false, true);
2800       static_seen = c_parser_next_token_is_keyword (parser, RID_STATIC);
2801       if (static_seen)
2802         c_parser_consume_token (parser);
2803       if (static_seen && !quals_attrs->declspecs_seen_p)
2804         c_parser_declspecs (parser, quals_attrs, false, false, true);
2805       if (!quals_attrs->declspecs_seen_p)
2806         quals_attrs = NULL;
2807       /* If "static" is present, there must be an array dimension.
2808          Otherwise, there may be a dimension, "*", or no
2809          dimension.  */
2810       if (static_seen)
2811         {
2812           star_seen = false;
2813           dimen = c_parser_expr_no_commas (parser, NULL).value;
2814         }
2815       else
2816         {
2817           if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
2818             {
2819               dimen = NULL_TREE;
2820               star_seen = false;
2821             }
2822           else if (c_parser_next_token_is (parser, CPP_MULT))
2823             {
2824               if (c_parser_peek_2nd_token (parser)->type == CPP_CLOSE_SQUARE)
2825                 {
2826                   dimen = NULL_TREE;
2827                   star_seen = true;
2828                   c_parser_consume_token (parser);
2829                 }
2830               else
2831                 {
2832                   star_seen = false;
2833                   dimen = c_parser_expr_no_commas (parser, NULL).value;
2834                 }
2835             }
2836           else
2837             {
2838               star_seen = false;
2839               dimen = c_parser_expr_no_commas (parser, NULL).value;
2840             }
2841         }
2842       if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
2843         c_parser_consume_token (parser);
2844       else
2845         {
2846           c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
2847                                      "expected %<]%>");
2848           return NULL;
2849         }
2850       if (dimen)
2851         mark_exp_read (dimen);
2852       declarator = build_array_declarator (brace_loc, dimen, quals_attrs,
2853                                            static_seen, star_seen);
2854       if (declarator == NULL)
2855         return NULL;
2856       inner = set_array_declarator_inner (declarator, inner);
2857       return c_parser_direct_declarator_inner (parser, id_present, inner);
2858     }
2859   else if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
2860     {
2861       tree attrs;
2862       struct c_arg_info *args;
2863       c_parser_consume_token (parser);
2864       attrs = c_parser_attributes (parser);
2865       args = c_parser_parms_declarator (parser, id_present, attrs);
2866       if (args == NULL)
2867         return NULL;
2868       else
2869         {
2870           inner = build_function_declarator (args, inner);
2871           return c_parser_direct_declarator_inner (parser, id_present, inner);
2872         }
2873     }
2874   return inner;
2875 }
2876
2877 /* Parse a parameter list or identifier list, including the closing
2878    parenthesis but not the opening one.  ATTRS are the attributes at
2879    the start of the list.  ID_LIST_OK is true if an identifier list is
2880    acceptable; such a list must not have attributes at the start.  */
2881
2882 static struct c_arg_info *
2883 c_parser_parms_declarator (c_parser *parser, bool id_list_ok, tree attrs)
2884 {
2885   push_scope ();
2886   declare_parm_level ();
2887   /* If the list starts with an identifier, it is an identifier list.
2888      Otherwise, it is either a prototype list or an empty list.  */
2889   if (id_list_ok
2890       && !attrs
2891       && c_parser_next_token_is (parser, CPP_NAME)
2892       && c_parser_peek_token (parser)->id_kind == C_ID_ID)
2893     {
2894       tree list = NULL_TREE, *nextp = &list;
2895       while (c_parser_next_token_is (parser, CPP_NAME)
2896              && c_parser_peek_token (parser)->id_kind == C_ID_ID)
2897         {
2898           *nextp = build_tree_list (NULL_TREE,
2899                                     c_parser_peek_token (parser)->value);
2900           nextp = & TREE_CHAIN (*nextp);
2901           c_parser_consume_token (parser);
2902           if (c_parser_next_token_is_not (parser, CPP_COMMA))
2903             break;
2904           c_parser_consume_token (parser);
2905           if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2906             {
2907               c_parser_error (parser, "expected identifier");
2908               break;
2909             }
2910         }
2911       if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2912         {
2913           struct c_arg_info *ret = build_arg_info ();
2914           ret->types = list;
2915           c_parser_consume_token (parser);
2916           pop_scope ();
2917           return ret;
2918         }
2919       else
2920         {
2921           c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2922                                      "expected %<)%>");
2923           pop_scope ();
2924           return NULL;
2925         }
2926     }
2927   else
2928     {
2929       struct c_arg_info *ret = c_parser_parms_list_declarator (parser, attrs);
2930       pop_scope ();
2931       return ret;
2932     }
2933 }
2934
2935 /* Parse a parameter list (possibly empty), including the closing
2936    parenthesis but not the opening one.  ATTRS are the attributes at
2937    the start of the list.  */
2938
2939 static struct c_arg_info *
2940 c_parser_parms_list_declarator (c_parser *parser, tree attrs)
2941 {
2942   bool bad_parm = false;
2943   /* ??? Following the old parser, forward parameter declarations may
2944      use abstract declarators, and if no real parameter declarations
2945      follow the forward declarations then this is not diagnosed.  Also
2946      note as above that attributes are ignored as the only contents of
2947      the parentheses, or as the only contents after forward
2948      declarations.  */
2949   if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2950     {
2951       struct c_arg_info *ret = build_arg_info ();
2952       c_parser_consume_token (parser);
2953       return ret;
2954     }
2955   if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
2956     {
2957       struct c_arg_info *ret = build_arg_info ();
2958       /* Suppress -Wold-style-definition for this case.  */
2959       ret->types = error_mark_node;
2960       error_at (c_parser_peek_token (parser)->location,
2961                 "ISO C requires a named argument before %<...%>");
2962       c_parser_consume_token (parser);
2963       if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2964         {
2965           c_parser_consume_token (parser);
2966           return ret;
2967         }
2968       else
2969         {
2970           c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2971                                      "expected %<)%>");
2972           return NULL;
2973         }
2974     }
2975   /* Nonempty list of parameters, either terminated with semicolon
2976      (forward declarations; recurse) or with close parenthesis (normal
2977      function) or with ", ... )" (variadic function).  */
2978   while (true)
2979     {
2980       /* Parse a parameter.  */
2981       struct c_parm *parm = c_parser_parameter_declaration (parser, attrs);
2982       attrs = NULL_TREE;
2983       if (parm == NULL)
2984         bad_parm = true;
2985       else
2986         push_parm_decl (parm);
2987       if (c_parser_next_token_is (parser, CPP_SEMICOLON))
2988         {
2989           tree new_attrs;
2990           c_parser_consume_token (parser);
2991           mark_forward_parm_decls ();
2992           new_attrs = c_parser_attributes (parser);
2993           return c_parser_parms_list_declarator (parser, new_attrs);
2994         }
2995       if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2996         {
2997           c_parser_consume_token (parser);
2998           if (bad_parm)
2999             {
3000               get_pending_sizes ();
3001               return NULL;
3002             }
3003           else
3004             return get_parm_info (false);
3005         }
3006       if (!c_parser_require (parser, CPP_COMMA,
3007                              "expected %<;%>, %<,%> or %<)%>"))
3008         {
3009           c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
3010           get_pending_sizes ();
3011           return NULL;
3012         }
3013       if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
3014         {
3015           c_parser_consume_token (parser);
3016           if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3017             {
3018               c_parser_consume_token (parser);
3019               if (bad_parm)
3020                 {
3021                   get_pending_sizes ();
3022                   return NULL;
3023                 }
3024               else
3025                 return get_parm_info (true);
3026             }
3027           else
3028             {
3029               c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3030                                          "expected %<)%>");
3031               get_pending_sizes ();
3032               return NULL;
3033             }
3034         }
3035     }
3036 }
3037
3038 /* Parse a parameter declaration.  ATTRS are the attributes at the
3039    start of the declaration if it is the first parameter.  */
3040
3041 static struct c_parm *
3042 c_parser_parameter_declaration (c_parser *parser, tree attrs)
3043 {
3044   struct c_declspecs *specs;
3045   struct c_declarator *declarator;
3046   tree prefix_attrs;
3047   tree postfix_attrs = NULL_TREE;
3048   bool dummy = false;
3049   if (!c_parser_next_token_starts_declspecs (parser))
3050     {
3051       c_token *token = c_parser_peek_token (parser);
3052       if (parser->error)
3053         return NULL;
3054       c_parser_set_source_position_from_token (token);
3055       if (token->type == CPP_NAME
3056           && c_parser_peek_2nd_token (parser)->type != CPP_COMMA
3057           && c_parser_peek_2nd_token (parser)->type != CPP_CLOSE_PAREN)
3058         {
3059           error ("unknown type name %qE", token->value);
3060           parser->error = true;
3061         }
3062       /* ??? In some Objective-C cases '...' isn't applicable so there
3063          should be a different message.  */
3064       else
3065         c_parser_error (parser,
3066                         "expected declaration specifiers or %<...%>");
3067       c_parser_skip_to_end_of_parameter (parser);
3068       return NULL;
3069     }
3070   specs = build_null_declspecs ();
3071   if (attrs)
3072     {
3073       declspecs_add_attrs (specs, attrs);
3074       attrs = NULL_TREE;
3075     }
3076   c_parser_declspecs (parser, specs, true, true, true);
3077   finish_declspecs (specs);
3078   pending_xref_error ();
3079   prefix_attrs = specs->attrs;
3080   specs->attrs = NULL_TREE;
3081   declarator = c_parser_declarator (parser, specs->type_seen_p,
3082                                     C_DTR_PARM, &dummy);
3083   if (declarator == NULL)
3084     {
3085       c_parser_skip_until_found (parser, CPP_COMMA, NULL);
3086       return NULL;
3087     }
3088   if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
3089     postfix_attrs = c_parser_attributes (parser);
3090   return build_c_parm (specs, chainon (postfix_attrs, prefix_attrs),
3091                        declarator);
3092 }
3093
3094 /* Parse a string literal in an asm expression.  It should not be
3095    translated, and wide string literals are an error although
3096    permitted by the syntax.  This is a GNU extension.
3097
3098    asm-string-literal:
3099      string-literal
3100
3101    ??? At present, following the old parser, the caller needs to have
3102    set lex_untranslated_string to 1.  It would be better to follow the
3103    C++ parser rather than using this kludge.  */
3104
3105 static tree
3106 c_parser_asm_string_literal (c_parser *parser)
3107 {
3108   tree str;
3109   if (c_parser_next_token_is (parser, CPP_STRING))
3110     {
3111       str = c_parser_peek_token (parser)->value;
3112       c_parser_consume_token (parser);
3113     }
3114   else if (c_parser_next_token_is (parser, CPP_WSTRING))
3115     {
3116       error_at (c_parser_peek_token (parser)->location,
3117                 "wide string literal in %<asm%>");
3118       str = build_string (1, "");
3119       c_parser_consume_token (parser);
3120     }
3121   else
3122     {
3123       c_parser_error (parser, "expected string literal");
3124       str = NULL_TREE;
3125     }
3126   return str;
3127 }
3128
3129 /* Parse a simple asm expression.  This is used in restricted
3130    contexts, where a full expression with inputs and outputs does not
3131    make sense.  This is a GNU extension.
3132
3133    simple-asm-expr:
3134      asm ( asm-string-literal )
3135 */
3136
3137 static tree
3138 c_parser_simple_asm_expr (c_parser *parser)
3139 {
3140   tree str;
3141   gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM));
3142   /* ??? Follow the C++ parser rather than using the
3143      lex_untranslated_string kludge.  */
3144   parser->lex_untranslated_string = true;
3145   c_parser_consume_token (parser);
3146   if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3147     {
3148       parser->lex_untranslated_string = false;
3149       return NULL_TREE;
3150     }
3151   str = c_parser_asm_string_literal (parser);
3152   parser->lex_untranslated_string = false;
3153   if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
3154     {
3155       c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
3156       return NULL_TREE;
3157     }
3158   return str;
3159 }
3160
3161 /* Parse (possibly empty) attributes.  This is a GNU extension.
3162
3163    attributes:
3164      empty
3165      attributes attribute
3166
3167    attribute:
3168      __attribute__ ( ( attribute-list ) )
3169
3170    attribute-list:
3171      attrib
3172      attribute_list , attrib
3173
3174    attrib:
3175      empty
3176      any-word
3177      any-word ( identifier )
3178      any-word ( identifier , nonempty-expr-list )
3179      any-word ( expr-list )
3180
3181    where the "identifier" must not be declared as a type, and
3182    "any-word" may be any identifier (including one declared as a
3183    type), a reserved word storage class specifier, type specifier or
3184    type qualifier.  ??? This still leaves out most reserved keywords
3185    (following the old parser), shouldn't we include them, and why not
3186    allow identifiers declared as types to start the arguments?  */
3187
3188 static tree
3189 c_parser_attributes (c_parser *parser)
3190 {
3191   tree attrs = NULL_TREE;
3192   while (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
3193     {
3194       /* ??? Follow the C++ parser rather than using the
3195          lex_untranslated_string kludge.  */
3196       parser->lex_untranslated_string = true;
3197       c_parser_consume_token (parser);
3198       if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3199         {
3200           parser->lex_untranslated_string = false;
3201           return attrs;
3202         }
3203       if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3204         {
3205           parser->lex_untranslated_string = false;
3206           c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
3207           return attrs;
3208         }
3209       /* Parse the attribute list.  */
3210       while (c_parser_next_token_is (parser, CPP_COMMA)
3211              || c_parser_next_token_is (parser, CPP_NAME)
3212              || c_parser_next_token_is (parser, CPP_KEYWORD))
3213         {
3214           tree attr, attr_name, attr_args;
3215           VEC(tree,gc) *expr_list;
3216           if (c_parser_next_token_is (parser, CPP_COMMA))
3217             {
3218               c_parser_consume_token (parser);
3219               continue;
3220             }
3221           if (c_parser_next_token_is (parser, CPP_KEYWORD))
3222             {
3223               /* ??? See comment above about what keywords are
3224                  accepted here.  */
3225               bool ok;
3226               switch (c_parser_peek_token (parser)->keyword)
3227                 {
3228                 case RID_STATIC:
3229                 case RID_UNSIGNED:
3230                 case RID_LONG:
3231                 case RID_INT128:
3232                 case RID_CONST:
3233                 case RID_EXTERN:
3234                 case RID_REGISTER:
3235                 case RID_TYPEDEF:
3236                 case RID_SHORT:
3237                 case RID_INLINE:
3238                 case RID_VOLATILE:
3239                 case RID_SIGNED:
3240                 case RID_AUTO:
3241                 case RID_RESTRICT:
3242                 case RID_COMPLEX:
3243                 case RID_THREAD:
3244                 case RID_INT:
3245                 case RID_CHAR:
3246                 case RID_FLOAT:
3247                 case RID_DOUBLE:
3248                 case RID_VOID:
3249                 case RID_DFLOAT32:
3250                 case RID_DFLOAT64:
3251                 case RID_DFLOAT128:
3252                 case RID_BOOL:
3253                 case RID_FRACT:
3254                 case RID_ACCUM:
3255                 case RID_SAT:
3256                   ok = true;
3257                   break;
3258                 default:
3259                   ok = false;
3260                   break;
3261                 }
3262               if (!ok)
3263                 break;
3264               /* Accept __attribute__((__const)) as __attribute__((const))
3265                  etc.  */
3266               attr_name
3267                 = ridpointers[(int) c_parser_peek_token (parser)->keyword];
3268             }
3269           else
3270             attr_name = c_parser_peek_token (parser)->value;
3271           c_parser_consume_token (parser);
3272           if (c_parser_next_token_is_not (parser, CPP_OPEN_PAREN))
3273             {
3274               attr = build_tree_list (attr_name, NULL_TREE);
3275               attrs = chainon (attrs, attr);
3276               continue;
3277             }
3278           c_parser_consume_token (parser);
3279           /* Parse the attribute contents.  If they start with an
3280              identifier which is followed by a comma or close
3281              parenthesis, then the arguments start with that
3282              identifier; otherwise they are an expression list.  
3283              In objective-c the identifier may be a classname.  */
3284           if (c_parser_next_token_is (parser, CPP_NAME)
3285               && (c_parser_peek_token (parser)->id_kind == C_ID_ID
3286                   || (c_dialect_objc () 
3287                       && c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME))
3288               && ((c_parser_peek_2nd_token (parser)->type == CPP_COMMA)
3289                   || (c_parser_peek_2nd_token (parser)->type
3290                       == CPP_CLOSE_PAREN)))
3291             {
3292               tree arg1 = c_parser_peek_token (parser)->value;
3293               c_parser_consume_token (parser);
3294               if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3295                 attr_args = build_tree_list (NULL_TREE, arg1);
3296               else
3297                 {
3298                   tree tree_list;
3299                   c_parser_consume_token (parser);
3300                   expr_list = c_parser_expr_list (parser, false, true, NULL);
3301                   tree_list = build_tree_list_vec (expr_list);
3302                   attr_args = tree_cons (NULL_TREE, arg1, tree_list);
3303                   release_tree_vector (expr_list);
3304                 }
3305             }
3306           else
3307             {
3308               if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3309                 attr_args = NULL_TREE;
3310               else
3311                 {
3312                   expr_list = c_parser_expr_list (parser, false, true, NULL);
3313                   attr_args = build_tree_list_vec (expr_list);
3314                   release_tree_vector (expr_list);
3315                 }
3316             }
3317           attr = build_tree_list (attr_name, attr_args);
3318           if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3319             c_parser_consume_token (parser);
3320           else
3321             {
3322               parser->lex_untranslated_string = false;
3323               c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3324                                          "expected %<)%>");
3325               return attrs;
3326             }
3327           attrs = chainon (attrs, attr);
3328         }
3329       if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3330         c_parser_consume_token (parser);
3331       else
3332         {
3333           parser->lex_untranslated_string = false;
3334           c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3335                                      "expected %<)%>");
3336           return attrs;
3337         }
3338       if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3339         c_parser_consume_token (parser);
3340       else
3341         {
3342           parser->lex_untranslated_string = false;
3343           c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3344                                      "expected %<)%>");
3345           return attrs;
3346         }
3347       parser->lex_untranslated_string = false;
3348     }
3349   return attrs;
3350 }
3351
3352 /* Parse a type name (C90 6.5.5, C99 6.7.6).
3353
3354    type-name:
3355      specifier-qualifier-list abstract-declarator[opt]
3356 */
3357
3358 static struct c_type_name *
3359 c_parser_type_name (c_parser *parser)
3360 {
3361   struct c_declspecs *specs = build_null_declspecs ();
3362   struct c_declarator *declarator;
3363   struct c_type_name *ret;
3364   bool dummy = false;
3365   c_parser_declspecs (parser, specs, false, true, true);
3366   if (!specs->declspecs_seen_p)
3367     {
3368       c_parser_error (parser, "expected specifier-qualifier-list");
3369       return NULL;
3370     }
3371   pending_xref_error ();
3372   finish_declspecs (specs);
3373   declarator = c_parser_declarator (parser, specs->type_seen_p,
3374                                     C_DTR_ABSTRACT, &dummy);
3375   if (declarator == NULL)
3376     return NULL;
3377   ret = XOBNEW (&parser_obstack, struct c_type_name);
3378   ret->specs = specs;
3379   ret->declarator = declarator;
3380   return ret;
3381 }
3382
3383 /* Parse an initializer (C90 6.5.7, C99 6.7.8).
3384
3385    initializer:
3386      assignment-expression
3387      { initializer-list }
3388      { initializer-list , }
3389
3390    initializer-list:
3391      designation[opt] initializer
3392      initializer-list , designation[opt] initializer
3393
3394    designation:
3395      designator-list =
3396
3397    designator-list:
3398      designator
3399      designator-list designator
3400
3401    designator:
3402      array-designator
3403      . identifier
3404
3405    array-designator:
3406      [ constant-expression ]
3407
3408    GNU extensions:
3409
3410    initializer:
3411      { }
3412
3413    designation:
3414      array-designator
3415      identifier :
3416
3417    array-designator:
3418      [ constant-expression ... constant-expression ]
3419
3420    Any expression without commas is accepted in the syntax for the
3421    constant-expressions, with non-constant expressions rejected later.
3422
3423    This function is only used for top-level initializers; for nested
3424    ones, see c_parser_initval.  */
3425
3426 static struct c_expr
3427 c_parser_initializer (c_parser *parser)
3428 {
3429   if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
3430     return c_parser_braced_init (parser, NULL_TREE, false);
3431   else
3432     {
3433       struct c_expr ret;
3434       location_t loc = c_parser_peek_token (parser)->location;
3435       ret = c_parser_expr_no_commas (parser, NULL);
3436       if (TREE_CODE (ret.value) != STRING_CST
3437           && TREE_CODE (ret.value) != COMPOUND_LITERAL_EXPR)
3438         ret = default_function_array_read_conversion (loc, ret);
3439       return ret;
3440     }
3441 }
3442
3443 /* Parse a braced initializer list.  TYPE is the type specified for a
3444    compound literal, and NULL_TREE for other initializers and for
3445    nested braced lists.  NESTED_P is true for nested braced lists,
3446    false for the list of a compound literal or the list that is the
3447    top-level initializer in a declaration.  */
3448
3449 static struct c_expr
3450 c_parser_braced_init (c_parser *parser, tree type, bool nested_p)
3451 {
3452   struct c_expr ret;
3453   struct obstack braced_init_obstack;
3454   location_t brace_loc = c_parser_peek_token (parser)->location;
3455   gcc_obstack_init (&braced_init_obstack);
3456   gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE));
3457   c_parser_consume_token (parser);
3458   if (nested_p)
3459     push_init_level (0, &braced_init_obstack);
3460   else
3461     really_start_incremental_init (type);
3462   if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3463     {
3464       pedwarn (brace_loc, OPT_pedantic, "ISO C forbids empty initializer braces");
3465     }
3466   else
3467     {
3468       /* Parse a non-empty initializer list, possibly with a trailing
3469          comma.  */
3470       while (true)
3471         {
3472           c_parser_initelt (parser, &braced_init_obstack);
3473           if (parser->error)
3474             break;
3475           if (c_parser_next_token_is (parser, CPP_COMMA))
3476             c_parser_consume_token (parser);
3477           else
3478             break;
3479           if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3480             break;
3481         }
3482     }
3483   if (c_parser_next_token_is_not (parser, CPP_CLOSE_BRACE))
3484     {
3485       ret.value = error_mark_node;
3486       ret.original_code = ERROR_MARK;
3487       ret.original_type = NULL;
3488       c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, "expected %<}%>");
3489       pop_init_level (0, &braced_init_obstack);
3490       obstack_free (&braced_init_obstack, NULL);
3491       return ret;
3492     }
3493   c_parser_consume_token (parser);
3494   ret = pop_init_level (0, &braced_init_obstack);
3495   obstack_free (&braced_init_obstack, NULL);
3496   return ret;
3497 }
3498
3499 /* Parse a nested initializer, including designators.  */
3500
3501 static void
3502 c_parser_initelt (c_parser *parser, struct obstack * braced_init_obstack)
3503 {
3504   /* Parse any designator or designator list.  A single array
3505      designator may have the subsequent "=" omitted in GNU C, but a
3506      longer list or a structure member designator may not.  */
3507   if (c_parser_next_token_is (parser, CPP_NAME)
3508       && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
3509     {
3510       /* Old-style structure member designator.  */
3511       set_init_label (c_parser_peek_token (parser)->value,
3512                       braced_init_obstack);
3513       /* Use the colon as the error location.  */
3514       pedwarn (c_parser_peek_2nd_token (parser)->location, OPT_pedantic,
3515                "obsolete use of designated initializer with %<:%>");
3516       c_parser_consume_token (parser);
3517       c_parser_consume_token (parser);
3518     }
3519   else
3520     {
3521       /* des_seen is 0 if there have been no designators, 1 if there
3522          has been a single array designator and 2 otherwise.  */
3523       int des_seen = 0;
3524       /* Location of a designator.  */
3525       location_t des_loc = UNKNOWN_LOCATION;  /* Quiet warning.  */
3526       while (c_parser_next_token_is (parser, CPP_OPEN_SQUARE)
3527              || c_parser_next_token_is (parser, CPP_DOT))
3528         {
3529           int des_prev = des_seen;
3530           if (!des_seen)
3531             des_loc = c_parser_peek_token (parser)->location;
3532           if (des_seen < 2)
3533             des_seen++;
3534           if (c_parser_next_token_is (parser, CPP_DOT))
3535             {
3536               des_seen = 2;
3537               c_parser_consume_token (parser);
3538               if (c_parser_next_token_is (parser, CPP_NAME))
3539                 {
3540                   set_init_label (c_parser_peek_token (parser)->value,
3541                                   braced_init_obstack);
3542                   c_parser_consume_token (parser);
3543                 }
3544               else
3545                 {
3546                   struct c_expr init;
3547                   init.value = error_mark_node;
3548                   init.original_code = ERROR_MARK;
3549                   init.original_type = NULL;
3550                   c_parser_error (parser, "expected identifier");
3551                   c_parser_skip_until_found (parser, CPP_COMMA, NULL);
3552                   process_init_element (init, false, braced_init_obstack);
3553                   return;
3554                 }
3555             }
3556           else
3557             {
3558               tree first, second;
3559               location_t ellipsis_loc = UNKNOWN_LOCATION;  /* Quiet warning.  */
3560               /* ??? Following the old parser, [ objc-receiver
3561                  objc-message-args ] is accepted as an initializer,
3562                  being distinguished from a designator by what follows
3563                  the first assignment expression inside the square
3564                  brackets, but after a first array designator a
3565                  subsequent square bracket is for Objective-C taken to
3566                  start an expression, using the obsolete form of
3567                  designated initializer without '=', rather than
3568                  possibly being a second level of designation: in LALR
3569                  terms, the '[' is shifted rather than reducing
3570                  designator to designator-list.  */
3571               if (des_prev == 1 && c_dialect_objc ())
3572                 {
3573                   des_seen = des_prev;
3574                   break;
3575                 }
3576               if (des_prev == 0 && c_dialect_objc ())
3577                 {
3578                   /* This might be an array designator or an
3579                      Objective-C message expression.  If the former,
3580                      continue parsing here; if the latter, parse the
3581                      remainder of the initializer given the starting
3582                      primary-expression.  ??? It might make sense to
3583                      distinguish when des_prev == 1 as well; see
3584                      previous comment.  */
3585                   tree rec, args;
3586                   struct c_expr mexpr;
3587                   c_parser_consume_token (parser);
3588                   if (c_parser_peek_token (parser)->type == CPP_NAME
3589                       && ((c_parser_peek_token (parser)->id_kind
3590                            == C_ID_TYPENAME)
3591                           || (c_parser_peek_token (parser)->id_kind
3592                               == C_ID_CLASSNAME)))
3593                     {
3594                       /* Type name receiver.  */
3595                       tree id = c_parser_peek_token (parser)->value;
3596                       c_parser_consume_token (parser);
3597                       rec = objc_get_class_reference (id);
3598                       goto parse_message_args;
3599                     }
3600                   first = c_parser_expr_no_commas (parser, NULL).value;
3601                   mark_exp_read (first);
3602                   if (c_parser_next_token_is (parser, CPP_ELLIPSIS)
3603                       || c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3604                     goto array_desig_after_first;
3605                   /* Expression receiver.  So far only one part
3606                      without commas has been parsed; there might be
3607                      more of the expression.  */
3608                   rec = first;
3609                   while (c_parser_next_token_is (parser, CPP_COMMA))
3610                     {
3611                       struct c_expr next;
3612                       location_t comma_loc, exp_loc;
3613                       comma_loc = c_parser_peek_token (parser)->location;
3614                       c_parser_consume_token (parser);
3615                       exp_loc = c_parser_peek_token (parser)->location;
3616                       next = c_parser_expr_no_commas (parser, NULL);
3617                       next = default_function_array_read_conversion (exp_loc,
3618                                                                      next);
3619                       rec = build_compound_expr (comma_loc, rec, next.value);
3620                     }
3621                 parse_message_args:
3622                   /* Now parse the objc-message-args.  */
3623                   args = c_parser_objc_message_args (parser);
3624                   c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
3625                                              "expected %<]%>");
3626                   mexpr.value
3627                     = objc_build_message_expr (build_tree_list (rec, args));
3628                   mexpr.original_code = ERROR_MARK;
3629                   mexpr.original_type = NULL;
3630                   /* Now parse and process the remainder of the
3631                      initializer, starting with this message
3632                      expression as a primary-expression.  */
3633                   c_parser_initval (parser, &mexpr, braced_init_obstack);
3634                   return;
3635                 }
3636               c_parser_consume_token (parser);
3637               first = c_parser_expr_no_commas (parser, NULL).value;
3638               mark_exp_read (first);
3639             array_desig_after_first:
3640               if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
3641                 {
3642                   ellipsis_loc = c_parser_peek_token (parser)->location;
3643                   c_parser_consume_token (parser);
3644                   second = c_parser_expr_no_commas (parser, NULL).value;
3645                   mark_exp_read (second);
3646                 }
3647               else
3648                 second = NULL_TREE;
3649               if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3650                 {
3651                   c_parser_consume_token (parser);
3652                   set_init_index (first, second, braced_init_obstack);
3653                   if (second)
3654                     pedwarn (ellipsis_loc, OPT_pedantic,
3655                              "ISO C forbids specifying range of elements to initialize");
3656                 }
3657               else
3658                 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
3659                                            "expected %<]%>");
3660             }
3661         }
3662       if (des_seen >= 1)
3663         {
3664           if (c_parser_next_token_is (parser, CPP_EQ))
3665             {
3666               if (!flag_isoc99)
3667                 pedwarn (des_loc, OPT_pedantic,
3668                          "ISO C90 forbids specifying subobject to initialize");
3669               c_parser_consume_token (parser);
3670             }
3671           else
3672             {
3673               if (des_seen == 1)
3674                 pedwarn (c_parser_peek_token (parser)->location, OPT_pedantic,
3675                          "obsolete use of designated initializer without %<=%>");
3676               else
3677                 {
3678                   struct c_expr init;
3679                   init.value = error_mark_node;
3680                   init.original_code = ERROR_MARK;
3681                   init.original_type = NULL;
3682                   c_parser_error (parser, "expected %<=%>");
3683                   c_parser_skip_until_found (parser, CPP_COMMA, NULL);
3684                   process_init_element (init, false, braced_init_obstack);
3685                   return;
3686                 }
3687             }
3688         }
3689     }
3690   c_parser_initval (parser, NULL, braced_init_obstack);
3691 }
3692
3693 /* Parse a nested initializer; as c_parser_initializer but parses
3694    initializers within braced lists, after any designators have been
3695    applied.  If AFTER is not NULL then it is an Objective-C message
3696    expression which is the primary-expression starting the
3697    initializer.  */
3698
3699 static void
3700 c_parser_initval (c_parser *parser, struct c_expr *after,
3701                   struct obstack * braced_init_obstack)
3702 {
3703   struct c_expr init;
3704   gcc_assert (!after || c_dialect_objc ());
3705   if (c_parser_next_token_is (parser, CPP_OPEN_BRACE) && !after)
3706     init = c_parser_braced_init (parser, NULL_TREE, true);
3707   else
3708     {
3709       location_t loc = c_parser_peek_token (parser)->location;
3710       init = c_parser_expr_no_commas (parser, after);
3711       if (init.value != NULL_TREE
3712           && TREE_CODE (init.value) != STRING_CST
3713           && TREE_CODE (init.value) != COMPOUND_LITERAL_EXPR)
3714         init = default_function_array_read_conversion (loc, init);
3715     }
3716   process_init_element (init, false, braced_init_obstack);
3717 }
3718
3719 /* Parse a compound statement (possibly a function body) (C90 6.6.2,
3720    C99 6.8.2).
3721
3722    compound-statement:
3723      { block-item-list[opt] }
3724      { label-declarations block-item-list }
3725
3726    block-item-list:
3727      block-item
3728      block-item-list block-item
3729
3730    block-item:
3731      nested-declaration
3732      statement
3733
3734    nested-declaration:
3735      declaration
3736
3737    GNU extensions:
3738
3739    compound-statement:
3740      { label-declarations block-item-list }
3741
3742    nested-declaration:
3743      __extension__ nested-declaration
3744      nested-function-definition
3745
3746    label-declarations:
3747      label-declaration
3748      label-declarations label-declaration
3749
3750    label-declaration:
3751      __label__ identifier-list ;
3752
3753    Allowing the mixing of declarations and code is new in C99.  The
3754    GNU syntax also permits (not shown above) labels at the end of
3755    compound statements, which yield an error.  We don't allow labels
3756    on declarations; this might seem like a natural extension, but
3757    there would be a conflict between attributes on the label and
3758    prefix attributes on the declaration.  ??? The syntax follows the
3759    old parser in requiring something after label declarations.
3760    Although they are erroneous if the labels declared aren't defined,
3761    is it useful for the syntax to be this way?
3762
3763    OpenMP:
3764
3765    block-item:
3766      openmp-directive
3767
3768    openmp-directive:
3769      barrier-directive
3770      flush-directive  */
3771
3772 static tree
3773 c_parser_compound_statement (c_parser *parser)
3774 {
3775   tree stmt;
3776   location_t brace_loc;
3777   brace_loc = c_parser_peek_token (parser)->location;
3778   if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
3779     {
3780       /* Ensure a scope is entered and left anyway to avoid confusion
3781          if we have just prepared to enter a function body.  */
3782       stmt = c_begin_compound_stmt (true);
3783       c_end_compound_stmt (brace_loc, stmt, true);
3784       return error_mark_node;
3785     }
3786   stmt = c_begin_compound_stmt (true);
3787   c_parser_compound_statement_nostart (parser);
3788   return c_end_compound_stmt (brace_loc, stmt, true);
3789 }
3790
3791 /* Parse a compound statement except for the opening brace.  This is
3792    used for parsing both compound statements and statement expressions
3793    (which follow different paths to handling the opening).  */
3794
3795 static void
3796 c_parser_compound_statement_nostart (c_parser *parser)
3797 {
3798   bool last_stmt = false;
3799   bool last_label = false;
3800   bool save_valid_for_pragma = valid_location_for_stdc_pragma_p ();
3801   location_t label_loc = UNKNOWN_LOCATION;  /* Quiet warning.  */
3802   if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3803     {
3804       c_parser_consume_token (parser);
3805       return;
3806     }
3807   mark_valid_location_for_stdc_pragma (true);
3808   if (c_parser_next_token_is_keyword (parser, RID_LABEL))
3809     {
3810       /* Read zero or more forward-declarations for labels that nested
3811          functions can jump to.  */
3812       mark_valid_location_for_stdc_pragma (false);
3813       while (c_parser_next_token_is_keyword (parser, RID_LABEL))
3814         {
3815           label_loc = c_parser_peek_token (parser)->location;
3816           c_parser_consume_token (parser);
3817           /* Any identifiers, including those declared as type names,
3818              are OK here.  */
3819           while (true)
3820             {
3821               tree label;
3822               if (c_parser_next_token_is_not (parser, CPP_NAME))
3823                 {
3824                   c_parser_error (parser, "expected identifier");
3825                   break;
3826                 }
3827               label
3828                 = declare_label (c_parser_peek_token (parser)->value);
3829               C_DECLARED_LABEL_FLAG (label) = 1;
3830               add_stmt (build_stmt (label_loc, DECL_EXPR, label));
3831               c_parser_consume_token (parser);
3832               if (c_parser_next_token_is (parser, CPP_COMMA))
3833                 c_parser_consume_token (parser);
3834               else
3835                 break;
3836             }
3837           c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
3838         }
3839       pedwarn (label_loc, OPT_pedantic, "ISO C forbids label declarations");
3840     }
3841   /* We must now have at least one statement, label or declaration.  */
3842   if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3843     {
3844       mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
3845       c_parser_error (parser, "expected declaration or statement");
3846       c_parser_consume_token (parser);
3847       return;
3848     }
3849   while (c_parser_next_token_is_not (parser, CPP_CLOSE_BRACE))
3850     {
3851       location_t loc = c_parser_peek_token (parser)->location;
3852       if (c_parser_next_token_is_keyword (parser, RID_CASE)
3853           || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
3854           || (c_parser_next_token_is (parser, CPP_NAME)
3855               && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
3856         {
3857           if (c_parser_next_token_is_keyword (parser, RID_CASE))
3858             label_loc = c_parser_peek_2nd_token (parser)->location;
3859           else
3860             label_loc = c_parser_peek_token (parser)->location;
3861           last_label = true;
3862           last_stmt = false;
3863           mark_valid_location_for_stdc_pragma (false);
3864           c_parser_label (parser);
3865         }
3866       else if (!last_label
3867                && c_parser_next_token_starts_declaration (parser))
3868         {
3869           last_label = false;
3870           mark_valid_location_for_stdc_pragma (false);
3871           c_parser_declaration_or_fndef (parser, true, true, true, true, true, NULL);
3872           if (last_stmt)
3873             pedwarn_c90 (loc,
3874                          (pedantic && !flag_isoc99)
3875                          ? OPT_pedantic
3876                          : OPT_Wdeclaration_after_statement,
3877                          "ISO C90 forbids mixed declarations and code");
3878           last_stmt = false;
3879         }
3880       else if (!last_label
3881                && c_parser_next_token_is_keyword (parser, RID_EXTENSION))
3882         {
3883           /* __extension__ can start a declaration, but is also an
3884              unary operator that can start an expression.  Consume all
3885              but the last of a possible series of __extension__ to
3886              determine which.  */
3887           while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
3888                  && (c_parser_peek_2nd_token (parser)->keyword
3889                      == RID_EXTENSION))
3890             c_parser_consume_token (parser);
3891           if (c_token_starts_declaration (c_parser_peek_2nd_token (parser)))
3892             {
3893               int ext;
3894               ext = disable_extension_diagnostics ();
3895               c_parser_consume_token (parser);
3896               last_label = false;
3897               mark_valid_location_for_stdc_pragma (false);
3898               c_parser_declaration_or_fndef (parser, true, true, true, true,
3899                                              true, NULL);
3900               /* Following the old parser, __extension__ does not
3901                  disable this diagnostic.  */
3902               restore_extension_diagnostics (ext);
3903               if (last_stmt)
3904                 pedwarn_c90 (loc, (pedantic && !flag_isoc99)
3905                              ? OPT_pedantic
3906                              : OPT_Wdeclaration_after_statement,
3907                              "ISO C90 forbids mixed declarations and code");
3908               last_stmt = false;
3909             }
3910           else
3911             goto statement;
3912         }
3913       else if (c_parser_next_token_is (parser, CPP_PRAGMA))
3914         {
3915           /* External pragmas, and some omp pragmas, are not associated
3916              with regular c code, and so are not to be considered statements
3917              syntactically.  This ensures that the user doesn't put them
3918              places that would turn into syntax errors if the directive
3919              were ignored.  */
3920           if (c_parser_pragma (parser, pragma_compound))
3921             last_label = false, last_stmt = true;
3922         }
3923       else if (c_parser_next_token_is (parser, CPP_EOF))
3924         {
3925           mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
3926           c_parser_error (parser, "expected declaration or statement");
3927           return;
3928         }
3929       else if (c_parser_next_token_is_keyword (parser, RID_ELSE))
3930         {
3931           if (parser->in_if_block)
3932             {
3933               mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
3934               error_at (loc, """expected %<}%> before %<else%>");
3935               return;
3936             }
3937           else
3938             {
3939               error_at (loc, "%<else%> without a previous %<if%>");
3940               c_parser_consume_token (parser);
3941               continue;
3942             }
3943         }
3944       else
3945         {
3946         statement:
3947           last_label = false;
3948           last_stmt = true;
3949           mark_valid_location_for_stdc_pragma (false);
3950           c_parser_statement_after_labels (parser);
3951         }
3952
3953       parser->error = false;
3954     }
3955   if (last_label)
3956     error_at (label_loc, "label at end of compound statement");
3957   c_parser_consume_token (parser);
3958   /* Restore the value we started with.  */
3959   mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
3960 }
3961
3962 /* Parse a label (C90 6.6.1, C99 6.8.1).
3963
3964    label:
3965      identifier : attributes[opt]
3966      case constant-expression :
3967      default :
3968
3969    GNU extensions:
3970
3971    label:
3972      case constant-expression ... constant-expression :
3973
3974    The use of attributes on labels is a GNU extension.  The syntax in
3975    GNU C accepts any expressions without commas, non-constant
3976    expressions being rejected later.  */
3977
3978 static void
3979 c_parser_label (c_parser *parser)
3980 {
3981   location_t loc1 = c_parser_peek_token (parser)->location;
3982   tree label = NULL_TREE;
3983   if (c_parser_next_token_is_keyword (parser, RID_CASE))
3984     {
3985       tree exp1, exp2;
3986       c_parser_consume_token (parser);
3987       exp1 = c_parser_expr_no_commas (parser, NULL).value;
3988       if (c_parser_next_token_is (parser, CPP_COLON))
3989         {
3990           c_parser_consume_token (parser);
3991           label = do_case (loc1, exp1, NULL_TREE);
3992         }
3993       else if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
3994         {
3995           c_parser_consume_token (parser);
3996           exp2 = c_parser_expr_no_commas (parser, NULL).value;
3997           if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
3998             label = do_case (loc1, exp1, exp2);
3999         }
4000       else
4001         c_parser_error (parser, "expected %<:%> or %<...%>");
4002     }
4003   else if (c_parser_next_token_is_keyword (parser, RID_DEFAULT))
4004     {
4005       c_parser_consume_token (parser);
4006       if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
4007         label = do_case (loc1, NULL_TREE, NULL_TREE);
4008     }
4009   else
4010     {
4011       tree name = c_parser_peek_token (parser)->value;
4012       tree tlab;
4013       tree attrs;
4014       location_t loc2 = c_parser_peek_token (parser)->location;
4015       gcc_assert (c_parser_next_token_is (parser, CPP_NAME));
4016       c_parser_consume_token (parser);
4017       gcc_assert (c_parser_next_token_is (parser, CPP_COLON));
4018       c_parser_consume_token (parser);
4019       attrs = c_parser_attributes (parser);
4020       tlab = define_label (loc2, name);
4021       if (tlab)
4022         {
4023           decl_attributes (&tlab, attrs, 0);
4024           label = add_stmt (build_stmt (loc1, LABEL_EXPR, tlab));
4025         }
4026     }
4027   if (label)
4028     {
4029       if (c_parser_next_token_starts_declaration (parser)
4030           && !(c_parser_next_token_is (parser, CPP_NAME)
4031                && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
4032         {
4033           error_at (c_parser_peek_token (parser)->location,
4034                     "a label can only be part of a statement and "
4035                     "a declaration is not a statement");
4036           c_parser_declaration_or_fndef (parser, /*fndef_ok*/ false,
4037                                          /*static_assert_ok*/ true,
4038                                          /*nested*/ true, /*empty_ok*/ false,
4039                                          /*start_attr_ok*/ true, NULL);
4040         }
4041     }
4042 }
4043
4044 /* Parse a statement (C90 6.6, C99 6.8).
4045
4046    statement:
4047      labeled-statement
4048      compound-statement
4049      expression-statement
4050      selection-statement
4051      iteration-statement
4052      jump-statement
4053
4054    labeled-statement:
4055      label statement
4056
4057    expression-statement:
4058      expression[opt] ;
4059
4060    selection-statement:
4061      if-statement
4062      switch-statement
4063
4064    iteration-statement:
4065      while-statement
4066      do-statement
4067      for-statement
4068
4069    jump-statement:
4070      goto identifier ;
4071      continue ;
4072      break ;
4073      return expression[opt] ;
4074
4075    GNU extensions:
4076
4077    statement:
4078      asm-statement
4079
4080    jump-statement:
4081      goto * expression ;
4082
4083    Objective-C:
4084
4085    statement:
4086      objc-throw-statement
4087      objc-try-catch-statement
4088      objc-synchronized-statement
4089
4090    objc-throw-statement:
4091      @throw expression ;
4092      @throw ;
4093
4094    OpenMP:
4095
4096    statement:
4097      openmp-construct
4098
4099    openmp-construct:
4100      parallel-construct
4101      for-construct
4102      sections-construct
4103      single-construct
4104      parallel-for-construct
4105      parallel-sections-construct
4106      master-construct
4107      critical-construct
4108      atomic-construct
4109      ordered-construct
4110
4111    parallel-construct:
4112      parallel-directive structured-block
4113
4114    for-construct:
4115      for-directive iteration-statement
4116
4117    sections-construct:
4118      sections-directive section-scope
4119
4120    single-construct:
4121      single-directive structured-block
4122
4123    parallel-for-construct:
4124      parallel-for-directive iteration-statement
4125
4126    parallel-sections-construct:
4127      parallel-sections-directive section-scope
4128
4129    master-construct:
4130      master-directive structured-block
4131
4132    critical-construct:
4133      critical-directive structured-block
4134
4135    atomic-construct:
4136      atomic-directive expression-statement
4137
4138    ordered-construct:
4139      ordered-directive structured-block  */
4140
4141 static void
4142 c_parser_statement (c_parser *parser)
4143 {
4144   while (c_parser_next_token_is_keyword (parser, RID_CASE)
4145          || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
4146          || (c_parser_next_token_is (parser, CPP_NAME)
4147              && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
4148     c_parser_label (parser);
4149   c_parser_statement_after_labels (parser);
4150 }
4151
4152 /* Parse a statement, other than a labeled statement.  */
4153
4154 static void
4155 c_parser_statement_after_labels (c_parser *parser)
4156 {
4157   location_t loc = c_parser_peek_token (parser)->location;
4158   tree stmt = NULL_TREE;
4159   bool in_if_block = parser->in_if_block;
4160   parser->in_if_block = false;
4161   switch (c_parser_peek_token (parser)->type)
4162     {
4163     case CPP_OPEN_BRACE:
4164       add_stmt (c_parser_compound_statement (parser));
4165       break;
4166     case CPP_KEYWORD:
4167       switch (c_parser_peek_token (parser)->keyword)
4168         {
4169         case RID_IF:
4170           c_parser_if_statement (parser);
4171           break;
4172         case RID_SWITCH:
4173           c_parser_switch_statement (parser);
4174           break;
4175         case RID_WHILE:
4176           c_parser_while_statement (parser);
4177           break;
4178         case RID_DO:
4179           c_parser_do_statement (parser);
4180           break;
4181         case RID_FOR:
4182           c_parser_for_statement (parser);
4183           break;
4184         case RID_GOTO:
4185           c_parser_consume_token (parser);
4186           if (c_parser_next_token_is (parser, CPP_NAME))
4187             {
4188               stmt = c_finish_goto_label (loc,
4189                                           c_parser_peek_token (parser)->value);
4190               c_parser_consume_token (parser);
4191             }
4192           else if (c_parser_next_token_is (parser, CPP_MULT))
4193             {
4194               tree val;
4195
4196               c_parser_consume_token (parser);
4197               val = c_parser_expression (parser).value;
4198               mark_exp_read (val);
4199               stmt = c_finish_goto_ptr (loc, val);
4200             }
4201           else
4202             c_parser_error (parser, "expected identifier or %<*%>");
4203           goto expect_semicolon;
4204         case RID_CONTINUE:
4205           c_parser_consume_token (parser);
4206           stmt = c_finish_bc_stmt (loc, &c_cont_label, false);
4207           goto expect_semicolon;
4208         case RID_BREAK:
4209           c_parser_consume_token (parser);
4210           stmt = c_finish_bc_stmt (loc, &c_break_label, true);
4211           goto expect_semicolon;
4212         case RID_RETURN:
4213           c_parser_consume_token (parser);
4214           if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4215             {
4216               stmt = c_finish_return (loc, NULL_TREE, NULL_TREE);
4217               c_parser_consume_token (parser);
4218             }
4219           else
4220             {
4221               struct c_expr expr = c_parser_expression_conv (parser);
4222               mark_exp_read (expr.value);
4223               stmt = c_finish_return (loc, expr.value, expr.original_type);
4224               goto expect_semicolon;
4225             }
4226           break;
4227         case RID_ASM:
4228           stmt = c_parser_asm_statement (parser);
4229           break;
4230         case RID_AT_THROW:
4231           gcc_assert (c_dialect_objc ());
4232           c_parser_consume_token (parser);
4233           if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4234             {
4235               stmt = objc_build_throw_stmt (loc, NULL_TREE);
4236               c_parser_consume_token (parser);
4237             }
4238           else
4239             {
4240               tree expr = c_parser_expression (parser).value;
4241               expr = c_fully_fold (expr, false, NULL);
4242               stmt = objc_build_throw_stmt (loc, expr);
4243               goto expect_semicolon;
4244             }
4245           break;
4246         case RID_AT_TRY:
4247           gcc_assert (c_dialect_objc ());
4248           c_parser_objc_try_catch_statement (parser);
4249           break;
4250         case RID_AT_SYNCHRONIZED:
4251           gcc_assert (c_dialect_objc ());
4252           c_parser_objc_synchronized_statement (parser);
4253           break;
4254         default:
4255           goto expr_stmt;
4256         }
4257       break;
4258     case CPP_SEMICOLON:
4259       c_parser_consume_token (parser);
4260       break;
4261     case CPP_CLOSE_PAREN:
4262     case CPP_CLOSE_SQUARE:
4263       /* Avoid infinite loop in error recovery:
4264          c_parser_skip_until_found stops at a closing nesting
4265          delimiter without consuming it, but here we need to consume
4266          it to proceed further.  */
4267       c_parser_error (parser, "expected statement");
4268       c_parser_consume_token (parser);
4269       break;
4270     case CPP_PRAGMA:
4271       c_parser_pragma (parser, pragma_stmt);
4272       break;
4273     default:
4274     expr_stmt:
4275       stmt = c_finish_expr_stmt (loc, c_parser_expression_conv (parser).value);
4276     expect_semicolon:
4277       c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
4278       break;
4279     }
4280   /* Two cases cannot and do not have line numbers associated: If stmt
4281      is degenerate, such as "2;", then stmt is an INTEGER_CST, which
4282      cannot hold line numbers.  But that's OK because the statement
4283      will either be changed to a MODIFY_EXPR during gimplification of
4284      the statement expr, or discarded.  If stmt was compound, but
4285      without new variables, we will have skipped the creation of a
4286      BIND and will have a bare STATEMENT_LIST.  But that's OK because
4287      (recursively) all of the component statements should already have
4288      line numbers assigned.  ??? Can we discard no-op statements
4289      earlier?  */
4290   if (CAN_HAVE_LOCATION_P (stmt)
4291       && EXPR_LOCATION (stmt) == UNKNOWN_LOCATION)
4292     SET_EXPR_LOCATION (stmt, loc);
4293
4294   parser->in_if_block = in_if_block;
4295 }
4296
4297 /* Parse the condition from an if, do, while or for statements.  */
4298
4299 static tree
4300 c_parser_condition (c_parser *parser)
4301 {
4302   location_t loc = c_parser_peek_token (parser)->location;
4303   tree cond;
4304   cond = c_parser_expression_conv (parser).value;
4305   cond = c_objc_common_truthvalue_conversion (loc, cond);
4306   cond = c_fully_fold (cond, false, NULL);
4307   if (warn_sequence_point)
4308     verify_sequence_points (cond);
4309   return cond;
4310 }
4311
4312 /* Parse a parenthesized condition from an if, do or while statement.
4313
4314    condition:
4315      ( expression )
4316 */
4317 static tree
4318 c_parser_paren_condition (c_parser *parser)
4319 {
4320   tree cond;
4321   if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4322     return error_mark_node;
4323   cond = c_parser_condition (parser);
4324   c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
4325   return cond;
4326 }
4327
4328 /* Parse a statement which is a block in C99.  */
4329
4330 static tree
4331 c_parser_c99_block_statement (c_parser *parser)
4332 {
4333   tree block = c_begin_compound_stmt (flag_isoc99);
4334   location_t loc = c_parser_peek_token (parser)->location;
4335   c_parser_statement (parser);
4336   return c_end_compound_stmt (loc, block, flag_isoc99);
4337 }
4338
4339 /* Parse the body of an if statement.  This is just parsing a
4340    statement but (a) it is a block in C99, (b) we track whether the
4341    body is an if statement for the sake of -Wparentheses warnings, (c)
4342    we handle an empty body specially for the sake of -Wempty-body
4343    warnings, and (d) we call parser_compound_statement directly
4344    because c_parser_statement_after_labels resets
4345    parser->in_if_block.  */
4346
4347 static tree
4348 c_parser_if_body (c_parser *parser, bool *if_p)
4349 {
4350   tree block = c_begin_compound_stmt (flag_isoc99);
4351   location_t body_loc = c_parser_peek_token (parser)->location;
4352   while (c_parser_next_token_is_keyword (parser, RID_CASE)
4353          || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
4354          || (c_parser_next_token_is (parser, CPP_NAME)
4355              && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
4356     c_parser_label (parser);
4357   *if_p = c_parser_next_token_is_keyword (parser, RID_IF);
4358   if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4359     {
4360       location_t loc = c_parser_peek_token (parser)->location;
4361       add_stmt (build_empty_stmt (loc));
4362       c_parser_consume_token (parser);
4363       if (!c_parser_next_token_is_keyword (parser, RID_ELSE))
4364         warning_at (loc, OPT_Wempty_body,
4365                     "suggest braces around empty body in an %<if%> statement");
4366     }
4367   else if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
4368     add_stmt (c_parser_compound_statement (parser));
4369   else
4370     c_parser_statement_after_labels (parser);
4371   return c_end_compound_stmt (body_loc, block, flag_isoc99);
4372 }
4373
4374 /* Parse the else body of an if statement.  This is just parsing a
4375    statement but (a) it is a block in C99, (b) we handle an empty body
4376    specially for the sake of -Wempty-body warnings.  */
4377
4378 static tree
4379 c_parser_else_body (c_parser *parser)
4380 {
4381   location_t else_loc = c_parser_peek_token (parser)->location;
4382   tree block = c_begin_compound_stmt (flag_isoc99);
4383   while (c_parser_next_token_is_keyword (parser, RID_CASE)
4384          || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
4385          || (c_parser_next_token_is (parser, CPP_NAME)
4386              && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
4387     c_parser_label (parser);
4388   if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4389     {
4390       location_t loc = c_parser_peek_token (parser)->location;
4391       warning_at (loc,
4392                   OPT_Wempty_body,
4393                  "suggest braces around empty body in an %<else%> statement");
4394       add_stmt (build_empty_stmt (loc));
4395       c_parser_consume_token (parser);
4396     }
4397   else
4398     c_parser_statement_after_labels (parser);
4399   return c_end_compound_stmt (else_loc, block, flag_isoc99);
4400 }
4401
4402 /* Parse an if statement (C90 6.6.4, C99 6.8.4).
4403
4404    if-statement:
4405      if ( expression ) statement
4406      if ( expression ) statement else statement
4407 */
4408
4409 static void
4410 c_parser_if_statement (c_parser *parser)
4411 {
4412   tree block;
4413   location_t loc;
4414   tree cond;
4415   bool first_if = false;
4416   tree first_body, second_body;
4417   bool in_if_block;
4418
4419   gcc_assert (c_parser_next_token_is_keyword (parser, RID_IF));
4420   c_parser_consume_token (parser);
4421   block = c_begin_compound_stmt (flag_isoc99);
4422   loc = c_parser_peek_token (parser)->location;
4423   cond = c_parser_paren_condition (parser);
4424   in_if_block = parser->in_if_block;
4425   parser->in_if_block = true;
4426   first_body = c_parser_if_body (parser, &first_if);
4427   parser->in_if_block = in_if_block;
4428   if (c_parser_next_token_is_keyword (parser, RID_ELSE))
4429     {
4430       c_parser_consume_token (parser);
4431       second_body = c_parser_else_body (parser);
4432     }
4433   else
4434     second_body = NULL_TREE;
4435   c_finish_if_stmt (loc, cond, first_body, second_body, first_if);
4436   add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
4437 }
4438
4439 /* Parse a switch statement (C90 6.6.4, C99 6.8.4).
4440
4441    switch-statement:
4442      switch (expression) statement
4443 */
4444
4445 static void
4446 c_parser_switch_statement (c_parser *parser)
4447 {
4448   tree block, expr, body, save_break;
4449   location_t switch_loc = c_parser_peek_token (parser)->location;
4450   location_t switch_cond_loc;
4451   gcc_assert (c_parser_next_token_is_keyword (parser, RID_SWITCH));
4452   c_parser_consume_token (parser);
4453   block = c_begin_compound_stmt (flag_isoc99);
4454   if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4455     {
4456       switch_cond_loc = c_parser_peek_token (parser)->location;
4457       expr = c_parser_expression (parser).value;
4458       c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
4459     }
4460   else
4461     {
4462       switch_cond_loc = UNKNOWN_LOCATION;
4463       expr = error_mark_node;
4464     }
4465   c_start_case (switch_loc, switch_cond_loc, expr);
4466   save_break = c_break_label;
4467   c_break_label = NULL_TREE;
4468   body = c_parser_c99_block_statement (parser);
4469   c_finish_case (body);
4470   if (c_break_label)
4471     {
4472       location_t here = c_parser_peek_token (parser)->location;
4473       tree t = build1 (LABEL_EXPR, void_type_node, c_break_label);
4474       SET_EXPR_LOCATION (t, here);
4475       add_stmt (t);
4476     }
4477   c_break_label = save_break;
4478   add_stmt (c_end_compound_stmt (switch_loc, block, flag_isoc99));
4479 }
4480
4481 /* Parse a while statement (C90 6.6.5, C99 6.8.5).
4482
4483    while-statement:
4484       while (expression) statement
4485 */
4486
4487 static void
4488 c_parser_while_statement (c_parser *parser)
4489 {
4490   tree block, cond, body, save_break, save_cont;
4491   location_t loc;
4492   gcc_assert (c_parser_next_token_is_keyword (parser, RID_WHILE));
4493   c_parser_consume_token (parser);
4494   block = c_begin_compound_stmt (flag_isoc99);
4495   loc = c_parser_peek_token (parser)->location;
4496   cond = c_parser_paren_condition (parser);
4497   save_break = c_break_label;
4498   c_break_label = NULL_TREE;
4499   save_cont = c_cont_label;
4500   c_cont_label = NULL_TREE;
4501   body = c_parser_c99_block_statement (parser);
4502   c_finish_loop (loc, cond, NULL, body, c_break_label, c_cont_label, true);
4503   add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
4504   c_break_label = save_break;
4505   c_cont_label = save_cont;
4506 }
4507
4508 /* Parse a do statement (C90 6.6.5, C99 6.8.5).
4509
4510    do-statement:
4511      do statement while ( expression ) ;
4512 */
4513
4514 static void
4515 c_parser_do_statement (c_parser *parser)
4516 {
4517   tree block, cond, body, save_break, save_cont, new_break, new_cont;
4518   location_t loc;
4519   gcc_assert (c_parser_next_token_is_keyword (parser, RID_DO));
4520   c_parser_consume_token (parser);
4521   if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4522     warning_at (c_parser_peek_token (parser)->location,
4523                 OPT_Wempty_body,
4524                 "suggest braces around empty body in %<do%> statement");
4525   block = c_begin_compound_stmt (flag_isoc99);
4526   loc = c_parser_peek_token (parser)->location;
4527   save_break = c_break_label;
4528   c_break_label = NULL_TREE;
4529   save_cont = c_cont_label;
4530   c_cont_label = NULL_TREE;
4531   body = c_parser_c99_block_statement (parser);
4532   c_parser_require_keyword (parser, RID_WHILE, "expected %<while%>");
4533   new_break = c_break_label;
4534   c_break_label = save_break;
4535   new_cont = c_cont_label;
4536   c_cont_label = save_cont;
4537   cond = c_parser_paren_condition (parser);
4538   if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
4539     c_parser_skip_to_end_of_block_or_statement (parser);
4540   c_finish_loop (loc, cond, NULL, body, new_break, new_cont, false);
4541   add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
4542 }
4543
4544 /* Parse a for statement (C90 6.6.5, C99 6.8.5).
4545
4546    for-statement:
4547      for ( expression[opt] ; expression[opt] ; expression[opt] ) statement
4548      for ( nested-declaration expression[opt] ; expression[opt] ) statement
4549
4550    The form with a declaration is new in C99.
4551
4552    ??? In accordance with the old parser, the declaration may be a
4553    nested function, which is then rejected in check_for_loop_decls,
4554    but does it make any sense for this to be included in the grammar?
4555    Note in particular that the nested function does not include a
4556    trailing ';', whereas the "declaration" production includes one.
4557    Also, can we reject bad declarations earlier and cheaper than
4558    check_for_loop_decls?
4559
4560    In Objective-C, there are two additional variants:
4561
4562    foreach-statement:
4563      for ( expression in expresssion ) statement
4564      for ( declaration in expression ) statement
4565
4566    This is inconsistent with C, because the second variant is allowed
4567    even if c99 is not enabled.
4568
4569    The rest of the comment documents these Objective-C foreach-statement.
4570
4571    Here is the canonical example of the first variant:
4572     for (object in array)    { do something with object }
4573    we call the first expression ("object") the "object_expression" and 
4574    the second expression ("array") the "collection_expression".
4575    object_expression must be an lvalue of type "id" (a generic Objective-C
4576    object) because the loop works by assigning to object_expression the
4577    various objects from the collection_expression.  collection_expression
4578    must evaluate to something of type "id" which responds to the method
4579    countByEnumeratingWithState:objects:count:.
4580
4581    The canonical example of the second variant is:
4582     for (id object in array)    { do something with object }
4583    which is completely equivalent to
4584     {
4585       id object;
4586       for (object in array) { do something with object }
4587     }
4588    Note that initizializing 'object' in some way (eg, "for ((object =
4589    xxx) in array) { do something with object }") is possibly
4590    technically valid, but completely pointless as 'object' will be
4591    assigned to something else as soon as the loop starts.  We should
4592    most likely reject it (TODO).
4593
4594    The beginning of the Objective-C foreach-statement looks exactly
4595    like the beginning of the for-statement, and we can tell it is a
4596    foreach-statement only because the initial declaration or
4597    expression is terminated by 'in' instead of ';'.
4598 */
4599
4600 static void
4601 c_parser_for_statement (c_parser *parser)
4602 {
4603   tree block, cond, incr, save_break, save_cont, body;
4604   /* The following are only used when parsing an ObjC foreach statement.  */
4605   tree object_expression, collection_expression;
4606   location_t loc = c_parser_peek_token (parser)->location;
4607   location_t for_loc = c_parser_peek_token (parser)->location;
4608   bool is_foreach_statement = false;
4609   gcc_assert (c_parser_next_token_is_keyword (parser, RID_FOR));
4610   c_parser_consume_token (parser);
4611   /* Open a compound statement in Objective-C as well, just in case this is
4612      as foreach expression.  */
4613   block = c_begin_compound_stmt (flag_isoc99 || c_dialect_objc ());
4614   cond = error_mark_node;
4615   incr = error_mark_node;
4616   if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4617     {
4618       /* Parse the initialization declaration or expression.  */
4619       object_expression = error_mark_node;
4620       if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4621         {
4622           c_parser_consume_token (parser);
4623           c_finish_expr_stmt (loc, NULL_TREE);
4624         }
4625       else if (c_parser_next_token_starts_declaration (parser))
4626         {
4627           parser->objc_could_be_foreach_context = true;
4628           c_parser_declaration_or_fndef (parser, true, true, true, true, true, 
4629                                          &object_expression);
4630           parser->objc_could_be_foreach_context = false;
4631           
4632           if (c_parser_next_token_is_keyword (parser, RID_IN))
4633             {
4634               c_parser_consume_token (parser);
4635               is_foreach_statement = true;
4636               if (check_for_loop_decls (for_loc, true) == NULL_TREE)
4637                 c_parser_error (parser, "multiple iterating variables in fast enumeration");
4638             }
4639           else
4640             check_for_loop_decls (for_loc, flag_isoc99);
4641         }
4642       else if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
4643         {
4644           /* __extension__ can start a declaration, but is also an
4645              unary operator that can start an expression.  Consume all
4646              but the last of a possible series of __extension__ to
4647              determine which.  */
4648           while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
4649                  && (c_parser_peek_2nd_token (parser)->keyword
4650                      == RID_EXTENSION))
4651             c_parser_consume_token (parser);
4652           if (c_token_starts_declaration (c_parser_peek_2nd_token (parser)))
4653             {
4654               int ext;
4655               ext = disable_extension_diagnostics ();
4656               c_parser_consume_token (parser);
4657               parser->objc_could_be_foreach_context = true;
4658               c_parser_declaration_or_fndef (parser, true, true, true, true,
4659                                              true, &object_expression);
4660               parser->objc_could_be_foreach_context = false;
4661               
4662               restore_extension_diagnostics (ext);
4663               if (c_parser_next_token_is_keyword (parser, RID_IN))
4664                 {
4665                   c_parser_consume_token (parser);
4666                   is_foreach_statement = true;
4667                   if (check_for_loop_decls (for_loc, true) == NULL_TREE)
4668                     c_parser_error (parser, "multiple iterating variables in fast enumeration");
4669                 }
4670               else
4671                 check_for_loop_decls (for_loc, flag_isoc99);
4672             }
4673           else
4674             goto init_expr;
4675         }
4676       else
4677         {
4678         init_expr:
4679           {
4680             tree init_expression;
4681             parser->objc_could_be_foreach_context = true;
4682             init_expression = c_parser_expression (parser).value;
4683             parser->objc_could_be_foreach_context = false;
4684             if (c_parser_next_token_is_keyword (parser, RID_IN))
4685               {
4686                 c_parser_consume_token (parser);
4687                 is_foreach_statement = true;
4688                 if (! lvalue_p (init_expression))
4689                   c_parser_error (parser, "invalid iterating variable in fast enumeration");
4690                 object_expression = c_process_expr_stmt (loc, init_expression);
4691
4692               }
4693             else
4694               {
4695                 c_finish_expr_stmt (loc, init_expression);
4696                 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
4697               }
4698           }
4699         }
4700       /* Parse the loop condition.  In the case of a foreach
4701          statement, there is no loop condition.  */
4702       if (!is_foreach_statement)
4703         {
4704           if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4705             {
4706               c_parser_consume_token (parser);
4707               cond = NULL_TREE;
4708             }
4709           else
4710             {
4711               cond = c_parser_condition (parser);
4712               c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
4713             }
4714         }
4715       /* Parse the increment expression (the third expression in a
4716          for-statement).  In the case of a foreach-statement, this is
4717          the expression that follows the 'in'.  */
4718       if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4719         {
4720           if (is_foreach_statement)
4721             {
4722               c_parser_error (parser, "missing collection in fast enumeration");
4723               collection_expression = error_mark_node;
4724             }
4725           else
4726             incr = c_process_expr_stmt (loc, NULL_TREE);
4727         }
4728       else
4729         {
4730           if (is_foreach_statement)
4731             collection_expression = c_process_expr_stmt (loc, c_parser_expression (parser).value);
4732           else
4733             incr = c_process_expr_stmt (loc, c_parser_expression (parser).value);
4734         }
4735       c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
4736     }
4737   save_break = c_break_label;
4738   c_break_label = NULL_TREE;
4739   save_cont = c_cont_label;
4740   c_cont_label = NULL_TREE;
4741   body = c_parser_c99_block_statement (parser);
4742   if (is_foreach_statement)
4743     objc_finish_foreach_loop (loc, object_expression, collection_expression, body, c_break_label, c_cont_label);
4744   else
4745     c_finish_loop (loc, cond, incr, body, c_break_label, c_cont_label, true);
4746   add_stmt (c_end_compound_stmt (loc, block, flag_isoc99 || c_dialect_objc ()));
4747   c_break_label = save_break;
4748   c_cont_label = save_cont;
4749 }
4750
4751 /* Parse an asm statement, a GNU extension.  This is a full-blown asm
4752    statement with inputs, outputs, clobbers, and volatile tag
4753    allowed.
4754
4755    asm-statement:
4756      asm type-qualifier[opt] ( asm-argument ) ;
4757      asm type-qualifier[opt] goto ( asm-goto-argument ) ;
4758
4759    asm-argument:
4760      asm-string-literal
4761      asm-string-literal : asm-operands[opt]
4762      asm-string-literal : asm-operands[opt] : asm-operands[opt]
4763      asm-string-literal : asm-operands[opt] : asm-operands[opt] : asm-clobbers[opt]
4764
4765    asm-goto-argument:
4766      asm-string-literal : : asm-operands[opt] : asm-clobbers[opt] \
4767        : asm-goto-operands
4768
4769    Qualifiers other than volatile are accepted in the syntax but
4770    warned for.  */
4771
4772 static tree
4773 c_parser_asm_statement (c_parser *parser)
4774 {
4775   tree quals, str, outputs, inputs, clobbers, labels, ret;
4776   bool simple, is_goto;
4777   location_t asm_loc = c_parser_peek_token (parser)->location;
4778   int section, nsections;
4779
4780   gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM));
4781   c_parser_consume_token (parser);
4782   if (c_parser_next_token_is_keyword (parser, RID_VOLATILE))
4783     {
4784       quals = c_parser_peek_token (parser)->value;
4785       c_parser_consume_token (parser);
4786     }
4787   else if (c_parser_next_token_is_keyword (parser, RID_CONST)
4788            || c_parser_next_token_is_keyword (parser, RID_RESTRICT))
4789     {
4790       warning_at (c_parser_peek_token (parser)->location,
4791                   0,
4792                   "%E qualifier ignored on asm",
4793                   c_parser_peek_token (parser)->value);
4794       quals = NULL_TREE;
4795       c_parser_consume_token (parser);
4796     }
4797   else
4798     quals = NULL_TREE;
4799
4800   is_goto = false;
4801   if (c_parser_next_token_is_keyword (parser, RID_GOTO))
4802     {
4803       c_parser_consume_token (parser);
4804       is_goto = true;
4805     }
4806
4807   /* ??? Follow the C++ parser rather than using the
4808      lex_untranslated_string kludge.  */
4809   parser->lex_untranslated_string = true;
4810   ret = NULL;
4811
4812   if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4813     goto error;
4814
4815   str = c_parser_asm_string_literal (parser);
4816   if (str == NULL_TREE)
4817     goto error_close_paren;
4818
4819   simple = true;
4820   outputs = NULL_TREE;
4821   inputs = NULL_TREE;
4822   clobbers = NULL_TREE;
4823   labels = NULL_TREE;
4824
4825   if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN) && !is_goto)
4826     goto done_asm;
4827
4828   /* Parse each colon-delimited section of operands.  */
4829   nsections = 3 + is_goto;
4830   for (section = 0; section < nsections; ++section)
4831     {
4832       if (!c_parser_require (parser, CPP_COLON,
4833                              is_goto
4834                              ? "expected %<:%>"
4835                              : "expected %<:%> or %<)%>"))
4836         goto error_close_paren;
4837
4838       /* Once past any colon, we're no longer a simple asm.  */
4839       simple = false;
4840
4841       if ((!c_parser_next_token_is (parser, CPP_COLON)
4842            && !c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4843           || section == 3)
4844         switch (section)
4845           {
4846           case 0:
4847             /* For asm goto, we don't allow output operands, but reserve
4848                the slot for a future extension that does allow them.  */
4849             if (!is_goto)
4850               outputs = c_parser_asm_operands (parser, false);
4851             break;
4852           case 1:
4853             inputs = c_parser_asm_operands (parser, true);
4854             break;
4855           case 2:
4856             clobbers = c_parser_asm_clobbers (parser);
4857             break;
4858           case 3:
4859             labels = c_parser_asm_goto_operands (parser);
4860             break;
4861           default:
4862             gcc_unreachable ();
4863           }
4864
4865       if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN) && !is_goto)
4866         goto done_asm;
4867     }
4868
4869  done_asm:
4870   if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
4871     {
4872       c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4873       goto error;
4874     }
4875
4876   if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
4877     c_parser_skip_to_end_of_block_or_statement (parser);
4878
4879   ret = build_asm_stmt (quals, build_asm_expr (asm_loc, str, outputs, inputs,
4880                                                clobbers, labels, simple));
4881
4882  error:
4883   parser->lex_untranslated_string = false;
4884   return ret;
4885
4886  error_close_paren:
4887   c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4888   goto error;
4889 }
4890
4891 /* Parse asm operands, a GNU extension.  If CONVERT_P (for inputs but
4892    not outputs), apply the default conversion of functions and arrays
4893    to pointers.
4894
4895    asm-operands:
4896      asm-operand
4897      asm-operands , asm-operand
4898
4899    asm-operand:
4900      asm-string-literal ( expression )
4901      [ identifier ] asm-string-literal ( expression )
4902 */
4903
4904 static tree
4905 c_parser_asm_operands (c_parser *parser, bool convert_p)
4906 {
4907   tree list = NULL_TREE;
4908   location_t loc;
4909   while (true)
4910     {
4911       tree name, str;
4912       struct c_expr expr;
4913       if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
4914         {
4915           c_parser_consume_token (parser);
4916           if (c_parser_next_token_is (parser, CPP_NAME))
4917             {
4918               tree id = c_parser_peek_token (parser)->value;
4919               c_parser_consume_token (parser);
4920               name = build_string (IDENTIFIER_LENGTH (id),
4921                                    IDENTIFIER_POINTER (id));
4922             }
4923           else
4924             {
4925               c_parser_error (parser, "expected identifier");
4926               c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
4927               return NULL_TREE;
4928             }
4929           c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
4930                                      "expected %<]%>");
4931         }
4932       else
4933         name = NULL_TREE;
4934       str = c_parser_asm_string_literal (parser);
4935       if (str == NULL_TREE)
4936         return NULL_TREE;
4937       parser->lex_untranslated_string = false;
4938       if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4939         {
4940           parser->lex_untranslated_string = true;
4941           return NULL_TREE;
4942         }
4943       loc = c_parser_peek_token (parser)->location;
4944       expr = c_parser_expression (parser);
4945       mark_exp_read (expr.value);
4946       if (convert_p)
4947         expr = default_function_array_conversion (loc, expr);
4948       expr.value = c_fully_fold (expr.value, false, NULL);
4949       parser->lex_untranslated_string = true;
4950       if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
4951         {
4952           c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4953           return NULL_TREE;
4954         }
4955       list = chainon (list, build_tree_list (build_tree_list (name, str),
4956                                              expr.value));
4957       if (c_parser_next_token_is (parser, CPP_COMMA))
4958         c_parser_consume_token (parser);
4959       else
4960         break;
4961     }
4962   return list;
4963 }
4964
4965 /* Parse asm clobbers, a GNU extension.
4966
4967    asm-clobbers:
4968      asm-string-literal
4969      asm-clobbers , asm-string-literal
4970 */
4971
4972 static tree
4973 c_parser_asm_clobbers (c_parser *parser)
4974 {
4975   tree list = NULL_TREE;
4976   while (true)
4977     {
4978       tree str = c_parser_asm_string_literal (parser);
4979       if (str)
4980         list = tree_cons (NULL_TREE, str, list);
4981       else
4982         return NULL_TREE;
4983       if (c_parser_next_token_is (parser, CPP_COMMA))
4984         c_parser_consume_token (parser);
4985       else
4986         break;
4987     }
4988   return list;
4989 }
4990
4991 /* Parse asm goto labels, a GNU extension.
4992
4993    asm-goto-operands:
4994      identifier
4995      asm-goto-operands , identifier
4996 */
4997
4998 static tree
4999 c_parser_asm_goto_operands (c_parser *parser)
5000 {
5001   tree list = NULL_TREE;
5002   while (true)
5003     {
5004       tree name, label;
5005
5006       if (c_parser_next_token_is (parser, CPP_NAME))
5007         {
5008           c_token *tok = c_parser_peek_token (parser);
5009           name = tok->value;
5010           label = lookup_label_for_goto (tok->location, name);
5011           c_parser_consume_token (parser);
5012           TREE_USED (label) = 1;
5013         }
5014       else
5015         {
5016           c_parser_error (parser, "expected identifier");
5017           return NULL_TREE;
5018         }
5019
5020       name = build_string (IDENTIFIER_LENGTH (name),
5021                            IDENTIFIER_POINTER (name));
5022       list = tree_cons (name, label, list);
5023       if (c_parser_next_token_is (parser, CPP_COMMA))
5024         c_parser_consume_token (parser);
5025       else
5026         return nreverse (list);
5027     }
5028 }
5029
5030 /* Parse an expression other than a compound expression; that is, an
5031    assignment expression (C90 6.3.16, C99 6.5.16).  If AFTER is not
5032    NULL then it is an Objective-C message expression which is the
5033    primary-expression starting the expression as an initializer.
5034
5035    assignment-expression:
5036      conditional-expression
5037      unary-expression assignment-operator assignment-expression
5038
5039    assignment-operator: one of
5040      = *= /= %= += -= <<= >>= &= ^= |=
5041
5042    In GNU C we accept any conditional expression on the LHS and
5043    diagnose the invalid lvalue rather than producing a syntax
5044    error.  */
5045
5046 static struct c_expr
5047 c_parser_expr_no_commas (c_parser *parser, struct c_expr *after)
5048 {
5049   struct c_expr lhs, rhs, ret;
5050   enum tree_code code;
5051   location_t op_location, exp_location;
5052   gcc_assert (!after || c_dialect_objc ());
5053   lhs = c_parser_conditional_expression (parser, after);
5054   op_location = c_parser_peek_token (parser)->location;
5055   switch (c_parser_peek_token (parser)->type)
5056     {
5057     case CPP_EQ:
5058       code = NOP_EXPR;
5059       break;
5060     case CPP_MULT_EQ:
5061       code = MULT_EXPR;
5062       break;
5063     case CPP_DIV_EQ:
5064       code = TRUNC_DIV_EXPR;
5065       break;
5066     case CPP_MOD_EQ:
5067       code = TRUNC_MOD_EXPR;
5068       break;
5069     case CPP_PLUS_EQ:
5070       code = PLUS_EXPR;
5071       break;
5072     case CPP_MINUS_EQ:
5073       code = MINUS_EXPR;
5074       break;
5075     case CPP_LSHIFT_EQ:
5076       code = LSHIFT_EXPR;
5077       break;
5078     case CPP_RSHIFT_EQ:
5079       code = RSHIFT_EXPR;
5080       break;
5081     case CPP_AND_EQ:
5082       code = BIT_AND_EXPR;
5083       break;
5084     case CPP_XOR_EQ:
5085       code = BIT_XOR_EXPR;
5086       break;
5087     case CPP_OR_EQ:
5088       code = BIT_IOR_EXPR;
5089       break;
5090     default:
5091       return lhs;
5092     }
5093   c_parser_consume_token (parser);
5094   exp_location = c_parser_peek_token (parser)->location;
5095   rhs = c_parser_expr_no_commas (parser, NULL);
5096   rhs = default_function_array_read_conversion (exp_location, rhs);
5097   ret.value = build_modify_expr (op_location, lhs.value, lhs.original_type,
5098                                  code, exp_location, rhs.value,
5099                                  rhs.original_type);
5100   if (code == NOP_EXPR)
5101     ret.original_code = MODIFY_EXPR;
5102   else
5103     {
5104       TREE_NO_WARNING (ret.value) = 1;
5105       ret.original_code = ERROR_MARK;
5106     }
5107   ret.original_type = NULL;
5108   return ret;
5109 }
5110
5111 /* Parse a conditional expression (C90 6.3.15, C99 6.5.15).  If AFTER
5112    is not NULL then it is an Objective-C message expression which is
5113    the primary-expression starting the expression as an initializer.
5114
5115    conditional-expression:
5116      logical-OR-expression
5117      logical-OR-expression ? expression : conditional-expression
5118
5119    GNU extensions:
5120
5121    conditional-expression:
5122      logical-OR-expression ? : conditional-expression
5123 */
5124
5125 static struct c_expr
5126 c_parser_conditional_expression (c_parser *parser, struct c_expr *after)
5127 {
5128   struct c_expr cond, exp1, exp2, ret;
5129   location_t cond_loc, colon_loc, middle_loc;
5130
5131   gcc_assert (!after || c_dialect_objc ());
5132
5133   cond = c_parser_binary_expression (parser, after);
5134
5135   if (c_parser_next_token_is_not (parser, CPP_QUERY))
5136     return cond;
5137   cond_loc = c_parser_peek_token (parser)->location;
5138   cond = default_function_array_read_conversion (cond_loc, cond);
5139   c_parser_consume_token (parser);
5140   if (c_parser_next_token_is (parser, CPP_COLON))
5141     {
5142       tree eptype = NULL_TREE;
5143
5144       middle_loc = c_parser_peek_token (parser)->location;
5145       pedwarn (middle_loc, OPT_pedantic, 
5146                "ISO C forbids omitting the middle term of a ?: expression");
5147       warn_for_omitted_condop (middle_loc, cond.value);
5148       if (TREE_CODE (cond.value) == EXCESS_PRECISION_EXPR)
5149         {
5150           eptype = TREE_TYPE (cond.value);
5151           cond.value = TREE_OPERAND (cond.value, 0);
5152         }
5153       /* Make sure first operand is calculated only once.  */
5154       exp1.value = c_save_expr (default_conversion (cond.value));
5155       if (eptype)
5156         exp1.value = build1 (EXCESS_PRECISION_EXPR, eptype, exp1.value);
5157       exp1.original_type = NULL;
5158       cond.value = c_objc_common_truthvalue_conversion (cond_loc, exp1.value);
5159       c_inhibit_evaluation_warnings += cond.value == truthvalue_true_node;
5160     }
5161   else
5162     {
5163       cond.value
5164         = c_objc_common_truthvalue_conversion
5165         (cond_loc, default_conversion (cond.value));
5166       c_inhibit_evaluation_warnings += cond.value == truthvalue_false_node;
5167       exp1 = c_parser_expression_conv (parser);
5168       mark_exp_read (exp1.value);
5169       c_inhibit_evaluation_warnings +=
5170         ((cond.value == truthvalue_true_node)
5171          - (cond.value == truthvalue_false_node));
5172     }
5173
5174   colon_loc = c_parser_peek_token (parser)->location;
5175   if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
5176     {
5177       c_inhibit_evaluation_warnings -= cond.value == truthvalue_true_node;
5178       ret.value = error_mark_node;
5179       ret.original_code = ERROR_MARK;
5180       ret.original_type = NULL;
5181       return ret;
5182     }
5183   {
5184     location_t exp2_loc = c_parser_peek_token (parser)->location;
5185     exp2 = c_parser_conditional_expression (parser, NULL);
5186     exp2 = default_function_array_read_conversion (exp2_loc, exp2);
5187   }
5188   c_inhibit_evaluation_warnings -= cond.value == truthvalue_true_node;
5189   ret.value = build_conditional_expr (colon_loc, cond.value,
5190                                       cond.original_code == C_MAYBE_CONST_EXPR,
5191                                       exp1.value, exp1.original_type,
5192                                       exp2.value, exp2.original_type);
5193   ret.original_code = ERROR_MARK;
5194   if (exp1.value == error_mark_node || exp2.value == error_mark_node)
5195     ret.original_type = NULL;
5196   else
5197     {
5198       tree t1, t2;
5199
5200       /* If both sides are enum type, the default conversion will have
5201          made the type of the result be an integer type.  We want to
5202          remember the enum types we started with.  */
5203       t1 = exp1.original_type ? exp1.original_type : TREE_TYPE (exp1.value);
5204       t2 = exp2.original_type ? exp2.original_type : TREE_TYPE (exp2.value);
5205       ret.original_type = ((t1 != error_mark_node
5206                             && t2 != error_mark_node
5207                             && (TYPE_MAIN_VARIANT (t1)
5208                                 == TYPE_MAIN_VARIANT (t2)))
5209                            ? t1
5210                            : NULL);
5211     }
5212   return ret;
5213 }
5214
5215 /* Parse a binary expression; that is, a logical-OR-expression (C90
5216    6.3.5-6.3.14, C99 6.5.5-6.5.14).  If AFTER is not NULL then it is
5217    an Objective-C message expression which is the primary-expression
5218    starting the expression as an initializer.
5219
5220    multiplicative-expression:
5221      cast-expression
5222      multiplicative-expression * cast-expression
5223      multiplicative-expression / cast-expression
5224      multiplicative-expression % cast-expression
5225
5226    additive-expression:
5227      multiplicative-expression
5228      additive-expression + multiplicative-expression
5229      additive-expression - multiplicative-expression
5230
5231    shift-expression:
5232      additive-expression
5233      shift-expression << additive-expression
5234      shift-expression >> additive-expression
5235
5236    relational-expression:
5237      shift-expression
5238      relational-expression < shift-expression
5239      relational-expression > shift-expression
5240      relational-expression <= shift-expression
5241      relational-expression >= shift-expression
5242
5243    equality-expression:
5244      relational-expression
5245      equality-expression == relational-expression
5246      equality-expression != relational-expression
5247
5248    AND-expression:
5249      equality-expression
5250      AND-expression & equality-expression
5251
5252    exclusive-OR-expression:
5253      AND-expression
5254      exclusive-OR-expression ^ AND-expression
5255
5256    inclusive-OR-expression:
5257      exclusive-OR-expression
5258      inclusive-OR-expression | exclusive-OR-expression
5259
5260    logical-AND-expression:
5261      inclusive-OR-expression
5262      logical-AND-expression && inclusive-OR-expression
5263
5264    logical-OR-expression:
5265      logical-AND-expression
5266      logical-OR-expression || logical-AND-expression
5267 */
5268
5269 static struct c_expr
5270 c_parser_binary_expression (c_parser *parser, struct c_expr *after)
5271 {
5272   /* A binary expression is parsed using operator-precedence parsing,
5273      with the operands being cast expressions.  All the binary
5274      operators are left-associative.  Thus a binary expression is of
5275      form:
5276
5277      E0 op1 E1 op2 E2 ...
5278
5279      which we represent on a stack.  On the stack, the precedence
5280      levels are strictly increasing.  When a new operator is
5281      encountered of higher precedence than that at the top of the
5282      stack, it is pushed; its LHS is the top expression, and its RHS
5283      is everything parsed until it is popped.  When a new operator is
5284      encountered with precedence less than or equal to that at the top
5285      of the stack, triples E[i-1] op[i] E[i] are popped and replaced
5286      by the result of the operation until the operator at the top of
5287      the stack has lower precedence than the new operator or there is
5288      only one element on the stack; then the top expression is the LHS
5289      of the new operator.  In the case of logical AND and OR
5290      expressions, we also need to adjust c_inhibit_evaluation_warnings
5291      as appropriate when the operators are pushed and popped.  */
5292
5293   /* The precedence levels, where 0 is a dummy lowest level used for
5294      the bottom of the stack.  */
5295   enum prec {
5296     PREC_NONE,
5297     PREC_LOGOR,
5298     PREC_LOGAND,
5299     PREC_BITOR,
5300     PREC_BITXOR,
5301     PREC_BITAND,
5302     PREC_EQ,
5303     PREC_REL,
5304     PREC_SHIFT,
5305     PREC_ADD,
5306     PREC_MULT,
5307     NUM_PRECS
5308   };
5309   struct {
5310     /* The expression at this stack level.  */
5311     struct c_expr expr;
5312     /* The precedence of the operator on its left, PREC_NONE at the
5313        bottom of the stack.  */
5314     enum prec prec;
5315     /* The operation on its left.  */
5316     enum tree_code op;
5317     /* The source location of this operation.  */
5318     location_t loc;
5319   } stack[NUM_PRECS];
5320   int sp;
5321   /* Location of the binary operator.  */
5322   location_t binary_loc = UNKNOWN_LOCATION;  /* Quiet warning.  */
5323 #define POP                                                                   \
5324   do {                                                                        \
5325     switch (stack[sp].op)                                                     \
5326       {                                                                       \
5327       case TRUTH_ANDIF_EXPR:                                                  \
5328         c_inhibit_evaluation_warnings -= (stack[sp - 1].expr.value            \
5329                                           == truthvalue_false_node);          \
5330         break;                                                                \
5331       case TRUTH_ORIF_EXPR:                                                   \
5332         c_inhibit_evaluation_warnings -= (stack[sp - 1].expr.value            \
5333                                           == truthvalue_true_node);           \
5334         break;                                                                \
5335       default:                                                                \
5336         break;                                                                \
5337       }                                                                       \
5338     stack[sp - 1].expr                                                        \
5339       = default_function_array_read_conversion (stack[sp - 1].loc,            \
5340                                                 stack[sp - 1].expr);          \
5341     stack[sp].expr                                                            \
5342       = default_function_array_read_conversion (stack[sp].loc,                \
5343                                                 stack[sp].expr);              \
5344     stack[sp - 1].expr = parser_build_binary_op (stack[sp].loc,               \
5345                                                  stack[sp].op,                \
5346                                                  stack[sp - 1].expr,          \
5347                                                  stack[sp].expr);             \
5348     sp--;                                                                     \
5349   } while (0)
5350   gcc_assert (!after || c_dialect_objc ());
5351   stack[0].loc = c_parser_peek_token (parser)->location;
5352   stack[0].expr = c_parser_cast_expression (parser, after);
5353   stack[0].prec = PREC_NONE;
5354   sp = 0;
5355   while (true)
5356     {
5357       enum prec oprec;
5358       enum tree_code ocode;
5359       if (parser->error)
5360         goto out;
5361       switch (c_parser_peek_token (parser)->type)
5362         {
5363         case CPP_MULT:
5364           oprec = PREC_MULT;
5365           ocode = MULT_EXPR;
5366           break;
5367         case CPP_DIV:
5368           oprec = PREC_MULT;
5369           ocode = TRUNC_DIV_EXPR;
5370           break;
5371         case CPP_MOD:
5372           oprec = PREC_MULT;
5373           ocode = TRUNC_MOD_EXPR;
5374           break;
5375         case CPP_PLUS:
5376           oprec = PREC_ADD;
5377           ocode = PLUS_EXPR;
5378           break;
5379         case CPP_MINUS:
5380           oprec = PREC_ADD;
5381           ocode = MINUS_EXPR;
5382           break;
5383         case CPP_LSHIFT:
5384           oprec = PREC_SHIFT;
5385           ocode = LSHIFT_EXPR;
5386           break;
5387         case CPP_RSHIFT:
5388           oprec = PREC_SHIFT;
5389           ocode = RSHIFT_EXPR;
5390           break;
5391         case CPP_LESS:
5392           oprec = PREC_REL;
5393           ocode = LT_EXPR;
5394           break;
5395         case CPP_GREATER:
5396           oprec = PREC_REL;
5397           ocode = GT_EXPR;
5398           break;
5399         case CPP_LESS_EQ:
5400           oprec = PREC_REL;
5401           ocode = LE_EXPR;
5402           break;
5403         case CPP_GREATER_EQ:
5404           oprec = PREC_REL;
5405           ocode = GE_EXPR;
5406           break;
5407         case CPP_EQ_EQ:
5408           oprec = PREC_EQ;
5409           ocode = EQ_EXPR;
5410           break;
5411         case CPP_NOT_EQ:
5412           oprec = PREC_EQ;
5413           ocode = NE_EXPR;
5414           break;
5415         case CPP_AND:
5416           oprec = PREC_BITAND;
5417           ocode = BIT_AND_EXPR;
5418           break;
5419         case CPP_XOR:
5420           oprec = PREC_BITXOR;
5421           ocode = BIT_XOR_EXPR;
5422           break;
5423         case CPP_OR:
5424           oprec = PREC_BITOR;
5425           ocode = BIT_IOR_EXPR;
5426           break;
5427         case CPP_AND_AND:
5428           oprec = PREC_LOGAND;
5429           ocode = TRUTH_ANDIF_EXPR;
5430           break;
5431         case CPP_OR_OR:
5432           oprec = PREC_LOGOR;
5433           ocode = TRUTH_ORIF_EXPR;
5434           break;
5435         default:
5436           /* Not a binary operator, so end of the binary
5437              expression.  */
5438           goto out;
5439         }
5440       binary_loc = c_parser_peek_token (parser)->location;
5441       c_parser_consume_token (parser);
5442       while (oprec <= stack[sp].prec)
5443         POP;
5444       switch (ocode)
5445         {
5446         case TRUTH_ANDIF_EXPR:
5447           stack[sp].expr
5448             = default_function_array_read_conversion (stack[sp].loc,
5449                                                       stack[sp].expr);
5450           stack[sp].expr.value = c_objc_common_truthvalue_conversion
5451             (stack[sp].loc, default_conversion (stack[sp].expr.value));
5452           c_inhibit_evaluation_warnings += (stack[sp].expr.value
5453                                             == truthvalue_false_node);
5454           break;
5455         case TRUTH_ORIF_EXPR:
5456           stack[sp].expr
5457             = default_function_array_read_conversion (stack[sp].loc,
5458                                                       stack[sp].expr);
5459           stack[sp].expr.value = c_objc_common_truthvalue_conversion
5460             (stack[sp].loc, default_conversion (stack[sp].expr.value));
5461           c_inhibit_evaluation_warnings += (stack[sp].expr.value
5462                                             == truthvalue_true_node);
5463           break;
5464         default:
5465           break;
5466         }
5467       sp++;
5468       stack[sp].loc = binary_loc;
5469       stack[sp].expr = c_parser_cast_expression (parser, NULL);
5470       stack[sp].prec = oprec;
5471       stack[sp].op = ocode;
5472       stack[sp].loc = binary_loc;
5473     }
5474  out:
5475   while (sp > 0)
5476     POP;
5477   return stack[0].expr;
5478 #undef POP
5479 }
5480
5481 /* Parse a cast expression (C90 6.3.4, C99 6.5.4).  If AFTER is not
5482    NULL then it is an Objective-C message expression which is the
5483    primary-expression starting the expression as an initializer.
5484
5485    cast-expression:
5486      unary-expression
5487      ( type-name ) unary-expression
5488 */
5489
5490 static struct c_expr
5491 c_parser_cast_expression (c_parser *parser, struct c_expr *after)
5492 {
5493   location_t cast_loc = c_parser_peek_token (parser)->location;
5494   gcc_assert (!after || c_dialect_objc ());
5495   if (after)
5496     return c_parser_postfix_expression_after_primary (parser,
5497                                                       cast_loc, *after);
5498   /* If the expression begins with a parenthesized type name, it may
5499      be either a cast or a compound literal; we need to see whether
5500      the next character is '{' to tell the difference.  If not, it is
5501      an unary expression.  */
5502   if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
5503       && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
5504     {
5505       struct c_type_name *type_name;
5506       struct c_expr ret;
5507       struct c_expr expr;
5508       c_parser_consume_token (parser);
5509       type_name = c_parser_type_name (parser);
5510       c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
5511       if (type_name == NULL)
5512         {
5513           ret.value = error_mark_node;
5514           ret.original_code = ERROR_MARK;
5515           ret.original_type = NULL;
5516           return ret;
5517         }
5518
5519       /* Save casted types in the function's used types hash table.  */
5520       used_types_insert (type_name->specs->type);
5521
5522       if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
5523         return c_parser_postfix_expression_after_paren_type (parser, type_name,
5524                                                              cast_loc);
5525       {
5526         location_t expr_loc = c_parser_peek_token (parser)->location;
5527         expr = c_parser_cast_expression (parser, NULL);
5528         expr = default_function_array_read_conversion (expr_loc, expr);
5529       }
5530       ret.value = c_cast_expr (cast_loc, type_name, expr.value);
5531       ret.original_code = ERROR_MARK;
5532       ret.original_type = NULL;
5533       return ret;
5534     }
5535   else
5536     return c_parser_unary_expression (parser);
5537 }
5538
5539 /* Parse an unary expression (C90 6.3.3, C99 6.5.3).
5540
5541    unary-expression:
5542      postfix-expression
5543      ++ unary-expression
5544      -- unary-expression
5545      unary-operator cast-expression
5546      sizeof unary-expression
5547      sizeof ( type-name )
5548
5549    unary-operator: one of
5550      & * + - ~ !
5551
5552    GNU extensions:
5553
5554    unary-expression:
5555      __alignof__ unary-expression
5556      __alignof__ ( type-name )
5557      && identifier
5558
5559    unary-operator: one of
5560      __extension__ __real__ __imag__
5561
5562    In addition, the GNU syntax treats ++ and -- as unary operators, so
5563    they may be applied to cast expressions with errors for non-lvalues
5564    given later.  */
5565
5566 static struct c_expr
5567 c_parser_unary_expression (c_parser *parser)
5568 {
5569   int ext;
5570   struct c_expr ret, op;
5571   location_t op_loc = c_parser_peek_token (parser)->location;
5572   location_t exp_loc;
5573   ret.original_code = ERROR_MARK;
5574   ret.original_type = NULL;
5575   switch (c_parser_peek_token (parser)->type)
5576     {
5577     case CPP_PLUS_PLUS:
5578       c_parser_consume_token (parser);
5579       exp_loc = c_parser_peek_token (parser)->location;
5580       op = c_parser_cast_expression (parser, NULL);
5581       op = default_function_array_read_conversion (exp_loc, op);
5582       return parser_build_unary_op (op_loc, PREINCREMENT_EXPR, op);
5583     case CPP_MINUS_MINUS:
5584       c_parser_consume_token (parser);
5585       exp_loc = c_parser_peek_token (parser)->location;
5586       op = c_parser_cast_expression (parser, NULL);
5587       op = default_function_array_read_conversion (exp_loc, op);
5588       return parser_build_unary_op (op_loc, PREDECREMENT_EXPR, op);
5589     case CPP_AND:
5590       c_parser_consume_token (parser);
5591       op = c_parser_cast_expression (parser, NULL);
5592       mark_exp_read (op.value);
5593       return parser_build_unary_op (op_loc, ADDR_EXPR, op);
5594     case CPP_MULT:
5595       c_parser_consume_token (parser);
5596       exp_loc = c_parser_peek_token (parser)->location;
5597       op = c_parser_cast_expression (parser, NULL);
5598       op = default_function_array_read_conversion (exp_loc, op);
5599       ret.value = build_indirect_ref (op_loc, op.value, RO_UNARY_STAR);
5600       return ret;
5601     case CPP_PLUS:
5602       if (!c_dialect_objc () && !in_system_header)
5603         warning_at (op_loc,
5604                     OPT_Wtraditional,
5605                     "traditional C rejects the unary plus operator");
5606       c_parser_consume_token (parser);
5607       exp_loc = c_parser_peek_token (parser)->location;
5608       op = c_parser_cast_expression (parser, NULL);
5609       op = default_function_array_read_conversion (exp_loc, op);
5610       return parser_build_unary_op (op_loc, CONVERT_EXPR, op);
5611     case CPP_MINUS:
5612       c_parser_consume_token (parser);
5613       exp_loc = c_parser_peek_token (parser)->location;
5614       op = c_parser_cast_expression (parser, NULL);
5615       op = default_function_array_read_conversion (exp_loc, op);
5616       return parser_build_unary_op (op_loc, NEGATE_EXPR, op);
5617     case CPP_COMPL:
5618       c_parser_consume_token (parser);
5619       exp_loc = c_parser_peek_token (parser)->location;
5620       op = c_parser_cast_expression (parser, NULL);
5621       op = default_function_array_read_conversion (exp_loc, op);
5622       return parser_build_unary_op (op_loc, BIT_NOT_EXPR, op);
5623     case CPP_NOT:
5624       c_parser_consume_token (parser);
5625       exp_loc = c_parser_peek_token (parser)->location;
5626       op = c_parser_cast_expression (parser, NULL);
5627       op = default_function_array_read_conversion (exp_loc, op);
5628       return parser_build_unary_op (op_loc, TRUTH_NOT_EXPR, op);
5629     case CPP_AND_AND:
5630       /* Refer to the address of a label as a pointer.  */
5631       c_parser_consume_token (parser);
5632       if (c_parser_next_token_is (parser, CPP_NAME))
5633         {
5634           ret.value = finish_label_address_expr
5635             (c_parser_peek_token (parser)->value, op_loc);
5636           c_parser_consume_token (parser);
5637         }
5638       else
5639         {
5640           c_parser_error (parser, "expected identifier");
5641           ret.value = error_mark_node;
5642         }
5643         return ret;
5644     case CPP_KEYWORD:
5645       switch (c_parser_peek_token (parser)->keyword)
5646         {
5647         case RID_SIZEOF:
5648           return c_parser_sizeof_expression (parser);
5649         case RID_ALIGNOF:
5650           return c_parser_alignof_expression (parser);
5651         case RID_EXTENSION:
5652           c_parser_consume_token (parser);
5653           ext = disable_extension_diagnostics ();
5654           ret = c_parser_cast_expression (parser, NULL);
5655           restore_extension_diagnostics (ext);
5656           return ret;
5657         case RID_REALPART:
5658           c_parser_consume_token (parser);
5659           exp_loc = c_parser_peek_token (parser)->location;
5660           op = c_parser_cast_expression (parser, NULL);
5661           op = default_function_array_conversion (exp_loc, op);
5662           return parser_build_unary_op (op_loc, REALPART_EXPR, op);
5663         case RID_IMAGPART:
5664           c_parser_consume_token (parser);
5665           exp_loc = c_parser_peek_token (parser)->location;
5666           op = c_parser_cast_expression (parser, NULL);
5667           op = default_function_array_conversion (exp_loc, op);
5668           return parser_build_unary_op (op_loc, IMAGPART_EXPR, op);
5669         default:
5670           return c_parser_postfix_expression (parser);
5671         }
5672     default:
5673       return c_parser_postfix_expression (parser);
5674     }
5675 }
5676
5677 /* Parse a sizeof expression.  */
5678
5679 static struct c_expr
5680 c_parser_sizeof_expression (c_parser *parser)
5681 {
5682   struct c_expr expr;
5683   location_t expr_loc;
5684   gcc_assert (c_parser_next_token_is_keyword (parser, RID_SIZEOF));
5685   c_parser_consume_token (parser);
5686   c_inhibit_evaluation_warnings++;
5687   in_sizeof++;
5688   if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
5689       && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
5690     {
5691       /* Either sizeof ( type-name ) or sizeof unary-expression
5692          starting with a compound literal.  */
5693       struct c_type_name *type_name;
5694       c_parser_consume_token (parser);
5695       expr_loc = c_parser_peek_token (parser)->location;
5696       type_name = c_parser_type_name (parser);
5697       c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
5698       if (type_name == NULL)
5699         {
5700           struct c_expr ret;
5701           c_inhibit_evaluation_warnings--;
5702           in_sizeof--;
5703           ret.value = error_mark_node;
5704           ret.original_code = ERROR_MARK;
5705           ret.original_type = NULL;
5706           return ret;
5707         }
5708       if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
5709         {
5710           expr = c_parser_postfix_expression_after_paren_type (parser,
5711                                                                type_name,
5712                                                                expr_loc);
5713           goto sizeof_expr;
5714         }
5715       /* sizeof ( type-name ).  */
5716       c_inhibit_evaluation_warnings--;
5717       in_sizeof--;
5718       return c_expr_sizeof_type (expr_loc, type_name);
5719     }
5720   else
5721     {
5722       expr_loc = c_parser_peek_token (parser)->location;
5723       expr = c_parser_unary_expression (parser);
5724     sizeof_expr:
5725       c_inhibit_evaluation_warnings--;
5726       in_sizeof--;
5727       mark_exp_read (expr.value);
5728       if (TREE_CODE (expr.value) == COMPONENT_REF
5729           && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1)))
5730         error_at (expr_loc, "%<sizeof%> applied to a bit-field");
5731       return c_expr_sizeof_expr (expr_loc, expr);
5732     }
5733 }
5734
5735 /* Parse an alignof expression.  */
5736
5737 static struct c_expr
5738 c_parser_alignof_expression (c_parser *parser)
5739 {
5740   struct c_expr expr;
5741   location_t loc = c_parser_peek_token (parser)->location;
5742   gcc_assert (c_parser_next_token_is_keyword (parser, RID_ALIGNOF));
5743   c_parser_consume_token (parser);
5744   c_inhibit_evaluation_warnings++;
5745   in_alignof++;
5746   if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
5747       && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
5748     {
5749       /* Either __alignof__ ( type-name ) or __alignof__
5750          unary-expression starting with a compound literal.  */
5751       location_t loc;
5752       struct c_type_name *type_name;
5753       struct c_expr ret;
5754       c_parser_consume_token (parser);
5755       loc = c_parser_peek_token (parser)->location;
5756       type_name = c_parser_type_name (parser);
5757       c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
5758       if (type_name == NULL)
5759         {
5760           struct c_expr ret;
5761           c_inhibit_evaluation_warnings--;
5762           in_alignof--;
5763           ret.value = error_mark_node;
5764           ret.original_code = ERROR_MARK;
5765           ret.original_type = NULL;
5766           return ret;
5767         }
5768       if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
5769         {
5770           expr = c_parser_postfix_expression_after_paren_type (parser,
5771                                                                type_name,
5772                                                                loc);
5773           goto alignof_expr;
5774         }
5775       /* alignof ( type-name ).  */
5776       c_inhibit_evaluation_warnings--;
5777       in_alignof--;
5778       ret.value = c_alignof (loc, groktypename (type_name, NULL, NULL));
5779       ret.original_code = ERROR_MARK;
5780       ret.original_type = NULL;
5781       return ret;
5782     }
5783   else
5784     {
5785       struct c_expr ret;
5786       expr = c_parser_unary_expression (parser);
5787     alignof_expr:
5788       mark_exp_read (expr.value);
5789       c_inhibit_evaluation_warnings--;
5790       in_alignof--;
5791       ret.value = c_alignof_expr (loc, expr.value);
5792       ret.original_code = ERROR_MARK;
5793       ret.original_type = NULL;
5794       return ret;
5795     }
5796 }
5797
5798 /* Parse a postfix expression (C90 6.3.1-6.3.2, C99 6.5.1-6.5.2).
5799
5800    postfix-expression:
5801      primary-expression
5802      postfix-expression [ expression ]
5803      postfix-expression ( argument-expression-list[opt] )
5804      postfix-expression . identifier
5805      postfix-expression -> identifier
5806      postfix-expression ++
5807      postfix-expression --
5808      ( type-name ) { initializer-list }
5809      ( type-name ) { initializer-list , }
5810
5811    argument-expression-list:
5812      argument-expression
5813      argument-expression-list , argument-expression
5814
5815    primary-expression:
5816      identifier
5817      constant
5818      string-literal
5819      ( expression )
5820
5821    GNU extensions:
5822
5823    primary-expression:
5824      __func__
5825        (treated as a keyword in GNU C)
5826      __FUNCTION__
5827      __PRETTY_FUNCTION__
5828      ( compound-statement )
5829      __builtin_va_arg ( assignment-expression , type-name )
5830      __builtin_offsetof ( type-name , offsetof-member-designator )
5831      __builtin_choose_expr ( assignment-expression ,
5832                              assignment-expression ,
5833                              assignment-expression )
5834      __builtin_types_compatible_p ( type-name , type-name )
5835
5836    offsetof-member-designator:
5837      identifier
5838      offsetof-member-designator . identifier
5839      offsetof-member-designator [ expression ]
5840
5841    Objective-C:
5842
5843    primary-expression:
5844      [ objc-receiver objc-message-args ]
5845      @selector ( objc-selector-arg )
5846      @protocol ( identifier )
5847      @encode ( type-name )
5848      objc-string-literal
5849      Classname . identifier
5850 */
5851
5852 static struct c_expr
5853 c_parser_postfix_expression (c_parser *parser)
5854 {
5855   struct c_expr expr, e1, e2, e3;
5856   struct c_type_name *t1, *t2;
5857   location_t loc = c_parser_peek_token (parser)->location;;
5858   expr.original_code = ERROR_MARK;
5859   expr.original_type = NULL;
5860   switch (c_parser_peek_token (parser)->type)
5861     {
5862     case CPP_NUMBER:
5863       expr.value = c_parser_peek_token (parser)->value;
5864       loc = c_parser_peek_token (parser)->location;
5865       c_parser_consume_token (parser);
5866       if (TREE_CODE (expr.value) == FIXED_CST
5867           && !targetm.fixed_point_supported_p ())
5868         {
5869           error_at (loc, "fixed-point types not supported for this target");
5870           expr.value = error_mark_node;
5871         }
5872       break;
5873     case CPP_CHAR:
5874     case CPP_CHAR16:
5875     case CPP_CHAR32:
5876     case CPP_WCHAR:
5877       expr.value = c_parser_peek_token (parser)->value;
5878       c_parser_consume_token (parser);
5879       break;
5880     case CPP_STRING:
5881     case CPP_STRING16:
5882     case CPP_STRING32:
5883     case CPP_WSTRING:
5884     case CPP_UTF8STRING:
5885       expr.value = c_parser_peek_token (parser)->value;
5886       expr.original_code = STRING_CST;
5887       c_parser_consume_token (parser);
5888       break;
5889     case CPP_OBJC_STRING:
5890       gcc_assert (c_dialect_objc ());
5891       expr.value
5892         = objc_build_string_object (c_parser_peek_token (parser)->value);
5893       c_parser_consume_token (parser);
5894       break;
5895     case CPP_NAME:
5896       switch (c_parser_peek_token (parser)->id_kind)
5897         {
5898         case C_ID_ID:
5899           {
5900             tree id = c_parser_peek_token (parser)->value;
5901             c_parser_consume_token (parser);
5902             expr.value = build_external_ref (loc, id,
5903                                              (c_parser_peek_token (parser)->type
5904                                               == CPP_OPEN_PAREN),
5905                                              &expr.original_type);
5906             break;
5907           }
5908         case C_ID_CLASSNAME:
5909           {
5910             /* Here we parse the Objective-C 2.0 Class.name dot
5911                syntax.  */
5912             tree class_name = c_parser_peek_token (parser)->value;
5913             tree component;
5914             c_parser_consume_token (parser);
5915             gcc_assert (c_dialect_objc ());
5916             if (!c_parser_require (parser, CPP_DOT, "expected %<.%>"))
5917               {
5918                 expr.value = error_mark_node;
5919                 break;
5920               }
5921             if (c_parser_next_token_is_not (parser, CPP_NAME))
5922               {
5923                 c_parser_error (parser, "expected identifier");
5924                 expr.value = error_mark_node;
5925                 break;
5926               }
5927             component = c_parser_peek_token (parser)->value;
5928             c_parser_consume_token (parser);
5929             expr.value = objc_build_class_component_ref (class_name, 
5930                                                          component);
5931             break;
5932           }
5933         default:
5934           c_parser_error (parser, "expected expression");
5935           expr.value = error_mark_node;
5936           break;
5937         }
5938       break;
5939     case CPP_OPEN_PAREN:
5940       /* A parenthesized expression, statement expression or compound
5941          literal.  */
5942       if (c_parser_peek_2nd_token (parser)->type == CPP_OPEN_BRACE)
5943         {
5944           /* A statement expression.  */
5945           tree stmt;
5946           location_t brace_loc;
5947           c_parser_consume_token (parser);
5948           brace_loc = c_parser_peek_token (parser)->location;
5949           c_parser_consume_token (parser);
5950           if (cur_stmt_list == NULL)
5951             {
5952               error_at (loc, "braced-group within expression allowed "
5953                         "only inside a function");
5954               parser->error = true;
5955               c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
5956               c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5957               expr.value = error_mark_node;
5958               break;
5959             }
5960           stmt = c_begin_stmt_expr ();
5961           c_parser_compound_statement_nostart (parser);
5962           c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5963                                      "expected %<)%>");
5964           pedwarn (loc, OPT_pedantic,
5965                    "ISO C forbids braced-groups within expressions");
5966           expr.value = c_finish_stmt_expr (brace_loc, stmt);
5967           mark_exp_read (expr.value);
5968         }
5969       else if (c_token_starts_typename (c_parser_peek_2nd_token (parser)))
5970         {
5971           /* A compound literal.  ??? Can we actually get here rather
5972              than going directly to
5973              c_parser_postfix_expression_after_paren_type from
5974              elsewhere?  */
5975           location_t loc;
5976           struct c_type_name *type_name;
5977           c_parser_consume_token (parser);
5978           loc = c_parser_peek_token (parser)->location;
5979           type_name = c_parser_type_name (parser);
5980           c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5981                                      "expected %<)%>");
5982           if (type_name == NULL)
5983             {
5984               expr.value = error_mark_node;
5985             }
5986           else
5987             expr = c_parser_postfix_expression_after_paren_type (parser,
5988                                                                  type_name,
5989                                                                  loc);
5990         }
5991       else
5992         {
5993           /* A parenthesized expression.  */
5994           c_parser_consume_token (parser);
5995           expr = c_parser_expression (parser);
5996           if (TREE_CODE (expr.value) == MODIFY_EXPR)
5997             TREE_NO_WARNING (expr.value) = 1;
5998           if (expr.original_code != C_MAYBE_CONST_EXPR)
5999             expr.original_code = ERROR_MARK;
6000           /* Don't change EXPR.ORIGINAL_TYPE.  */
6001           c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6002                                      "expected %<)%>");
6003         }
6004       break;
6005     case CPP_KEYWORD:
6006       switch (c_parser_peek_token (parser)->keyword)
6007         {
6008         case RID_FUNCTION_NAME:
6009         case RID_PRETTY_FUNCTION_NAME:
6010         case RID_C99_FUNCTION_NAME:
6011           expr.value = fname_decl (loc,
6012                                    c_parser_peek_token (parser)->keyword,
6013                                    c_parser_peek_token (parser)->value);
6014           c_parser_consume_token (parser);
6015           break;
6016         case RID_VA_ARG:
6017           c_parser_consume_token (parser);
6018           if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6019             {
6020               expr.value = error_mark_node;
6021               break;
6022             }
6023           e1 = c_parser_expr_no_commas (parser, NULL);
6024           mark_exp_read (e1.value);
6025           e1.value = c_fully_fold (e1.value, false, NULL);
6026           if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
6027             {
6028               c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6029               expr.value = error_mark_node;
6030               break;
6031             }
6032           loc = c_parser_peek_token (parser)->location;
6033           t1 = c_parser_type_name (parser);
6034           c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6035                                      "expected %<)%>");
6036           if (t1 == NULL)
6037             {
6038               expr.value = error_mark_node;
6039             }
6040           else
6041             {
6042               tree type_expr = NULL_TREE;
6043               expr.value = c_build_va_arg (loc, e1.value,
6044                                            groktypename (t1, &type_expr, NULL));
6045               if (type_expr)
6046                 {
6047                   expr.value = build2 (C_MAYBE_CONST_EXPR,
6048                                        TREE_TYPE (expr.value), type_expr,
6049                                        expr.value);
6050                   C_MAYBE_CONST_EXPR_NON_CONST (expr.value) = true;
6051                 }
6052             }
6053           break;
6054         case RID_OFFSETOF:
6055           c_parser_consume_token (parser);
6056           if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6057             {
6058               expr.value = error_mark_node;
6059               break;
6060             }
6061           t1 = c_parser_type_name (parser);
6062           if (t1 == NULL)
6063             {
6064               expr.value = error_mark_node;
6065               break;
6066             }
6067           if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
6068             {
6069               c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6070               expr.value = error_mark_node;
6071               break;
6072             }
6073           {
6074             tree type = groktypename (t1, NULL, NULL);
6075             tree offsetof_ref;
6076             if (type == error_mark_node)
6077               offsetof_ref = error_mark_node;
6078             else
6079               {
6080                 offsetof_ref = build1 (INDIRECT_REF, type, null_pointer_node);
6081                 SET_EXPR_LOCATION (offsetof_ref, loc);
6082               }
6083             /* Parse the second argument to __builtin_offsetof.  We
6084                must have one identifier, and beyond that we want to
6085                accept sub structure and sub array references.  */
6086             if (c_parser_next_token_is (parser, CPP_NAME))
6087               {
6088                 offsetof_ref = build_component_ref
6089                   (loc, offsetof_ref, c_parser_peek_token (parser)->value);
6090                 c_parser_consume_token (parser);
6091                 while (c_parser_next_token_is (parser, CPP_DOT)
6092                        || c_parser_next_token_is (parser,
6093                                                   CPP_OPEN_SQUARE)
6094                        || c_parser_next_token_is (parser,
6095                                                   CPP_DEREF))
6096                   {
6097                     if (c_parser_next_token_is (parser, CPP_DEREF))
6098                       {
6099                         loc = c_parser_peek_token (parser)->location;
6100                         offsetof_ref = build_array_ref (loc,
6101                                                         offsetof_ref,
6102                                                         integer_zero_node);
6103                         goto do_dot;
6104                       }
6105                     else if (c_parser_next_token_is (parser, CPP_DOT))
6106                       {
6107                       do_dot:
6108                         c_parser_consume_token (parser);
6109                         if (c_parser_next_token_is_not (parser,
6110                                                         CPP_NAME))
6111                           {
6112                             c_parser_error (parser, "expected identifier");
6113                             break;
6114                           }
6115                         offsetof_ref = build_component_ref
6116                           (loc, offsetof_ref,
6117                            c_parser_peek_token (parser)->value);
6118                         c_parser_consume_token (parser);
6119                       }
6120                     else
6121                       {
6122                         tree idx;
6123                         loc = c_parser_peek_token (parser)->location;
6124                         c_parser_consume_token (parser);
6125                         idx = c_parser_expression (parser).value;
6126                         idx = c_fully_fold (idx, false, NULL);
6127                         c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
6128                                                    "expected %<]%>");
6129                         offsetof_ref = build_array_ref (loc, offsetof_ref, idx);
6130                       }
6131                   }
6132               }
6133             else
6134               c_parser_error (parser, "expected identifier");
6135             c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6136                                        "expected %<)%>");
6137             expr.value = fold_offsetof (offsetof_ref, NULL_TREE);
6138           }
6139           break;
6140         case RID_CHOOSE_EXPR:
6141           c_parser_consume_token (parser);
6142           if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6143             {
6144               expr.value = error_mark_node;
6145               break;
6146             }
6147           loc = c_parser_peek_token (parser)->location;
6148           e1 = c_parser_expr_no_commas (parser, NULL);
6149           if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
6150             {
6151               c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6152               expr.value = error_mark_node;
6153               break;
6154             }
6155           e2 = c_parser_expr_no_commas (parser, NULL);
6156           if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
6157             {
6158               c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6159               expr.value = error_mark_node;
6160               break;
6161             }
6162           e3 = c_parser_expr_no_commas (parser, NULL);
6163           c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6164                                      "expected %<)%>");
6165           {
6166             tree c;
6167
6168             c = e1.value;
6169             mark_exp_read (e2.value);
6170             mark_exp_read (e3.value);
6171             if (TREE_CODE (c) != INTEGER_CST
6172                 || !INTEGRAL_TYPE_P (TREE_TYPE (c)))
6173               error_at (loc,
6174                         "first argument to %<__builtin_choose_expr%> not"
6175                         " a constant");
6176             constant_expression_warning (c);
6177             expr = integer_zerop (c) ? e3 : e2;
6178           }
6179           break;
6180         case RID_TYPES_COMPATIBLE_P:
6181           c_parser_consume_token (parser);
6182           if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6183             {
6184               expr.value = error_mark_node;
6185               break;
6186             }
6187           t1 = c_parser_type_name (parser);
6188           if (t1 == NULL)
6189             {
6190               expr.value = error_mark_node;
6191               break;
6192             }
6193           if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
6194             {
6195               c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6196               expr.value = error_mark_node;
6197               break;
6198             }
6199           t2 = c_parser_type_name (parser);
6200           if (t2 == NULL)
6201             {
6202               expr.value = error_mark_node;
6203               break;
6204             }
6205           c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6206                                      "expected %<)%>");
6207           {
6208             tree e1, e2;
6209
6210             e1 = TYPE_MAIN_VARIANT (groktypename (t1, NULL, NULL));
6211             e2 = TYPE_MAIN_VARIANT (groktypename (t2, NULL, NULL));
6212
6213             expr.value
6214               = comptypes (e1, e2) ? integer_one_node : integer_zero_node;
6215           }
6216           break;
6217         case RID_AT_SELECTOR:
6218           gcc_assert (c_dialect_objc ());
6219           c_parser_consume_token (parser);
6220           if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6221             {
6222               expr.value = error_mark_node;
6223               break;
6224             }
6225           {
6226             tree sel = c_parser_objc_selector_arg (parser);
6227             c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6228                                        "expected %<)%>");
6229             expr.value = objc_build_selector_expr (loc, sel);
6230           }
6231           break;
6232         case RID_AT_PROTOCOL:
6233           gcc_assert (c_dialect_objc ());
6234           c_parser_consume_token (parser);
6235           if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6236             {
6237               expr.value = error_mark_node;
6238               break;
6239             }
6240           if (c_parser_next_token_is_not (parser, CPP_NAME))
6241             {
6242               c_parser_error (parser, "expected identifier");
6243               c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6244               expr.value = error_mark_node;
6245               break;
6246             }
6247           {
6248             tree id = c_parser_peek_token (parser)->value;
6249             c_parser_consume_token (parser);
6250             c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6251                                        "expected %<)%>");
6252             expr.value = objc_build_protocol_expr (id);
6253           }
6254           break;
6255         case RID_AT_ENCODE:
6256           /* Extension to support C-structures in the archiver.  */
6257           gcc_assert (c_dialect_objc ());
6258           c_parser_consume_token (parser);
6259           if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6260             {
6261               expr.value = error_mark_node;
6262               break;
6263             }
6264           t1 = c_parser_type_name (parser);
6265           if (t1 == NULL)
6266             {
6267               expr.value = error_mark_node;
6268               c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6269               break;
6270             }
6271           c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6272                                      "expected %<)%>");
6273           {
6274             tree type = groktypename (t1, NULL, NULL);
6275             expr.value = objc_build_encode_expr (type);
6276           }
6277           break;
6278         default:
6279           c_parser_error (parser, "expected expression");
6280           expr.value = error_mark_node;
6281           break;
6282         }
6283       break;
6284     case CPP_OPEN_SQUARE:
6285       if (c_dialect_objc ())
6286         {
6287           tree receiver, args;
6288           c_parser_consume_token (parser);
6289           receiver = c_parser_objc_receiver (parser);
6290           args = c_parser_objc_message_args (parser);
6291           c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
6292                                      "expected %<]%>");
6293           expr.value = objc_build_message_expr (build_tree_list (receiver,
6294                                                                  args));
6295           break;
6296         }
6297       /* Else fall through to report error.  */
6298     default:
6299       c_parser_error (parser, "expected expression");
6300       expr.value = error_mark_node;
6301       break;
6302     }
6303   return c_parser_postfix_expression_after_primary (parser, loc, expr);
6304 }
6305
6306 /* Parse a postfix expression after a parenthesized type name: the
6307    brace-enclosed initializer of a compound literal, possibly followed
6308    by some postfix operators.  This is separate because it is not
6309    possible to tell until after the type name whether a cast
6310    expression has a cast or a compound literal, or whether the operand
6311    of sizeof is a parenthesized type name or starts with a compound
6312    literal.  TYPE_LOC is the location where TYPE_NAME starts--the
6313    location of the first token after the parentheses around the type
6314    name.  */
6315
6316 static struct c_expr
6317 c_parser_postfix_expression_after_paren_type (c_parser *parser,
6318                                               struct c_type_name *type_name,
6319                                               location_t type_loc)
6320 {
6321   tree type;
6322   struct c_expr init;
6323   bool non_const;
6324   struct c_expr expr;
6325   location_t start_loc;
6326   tree type_expr = NULL_TREE;
6327   bool type_expr_const = true;
6328   check_compound_literal_type (type_loc, type_name);
6329   start_init (NULL_TREE, NULL, 0);
6330   type = groktypename (type_name, &type_expr, &type_expr_const);
6331   start_loc = c_parser_peek_token (parser)->location;
6332   if (type != error_mark_node && C_TYPE_VARIABLE_SIZE (type))
6333     {
6334       error_at (type_loc, "compound literal has variable size");
6335       type = error_mark_node;
6336     }
6337   init = c_parser_braced_init (parser, type, false);
6338   finish_init ();
6339   maybe_warn_string_init (type, init);
6340
6341   if (type != error_mark_node
6342       && !ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (type))
6343       && current_function_decl)
6344     {
6345       error ("compound literal qualified by address-space qualifier");
6346       type = error_mark_node;
6347     }
6348
6349   if (!flag_isoc99)
6350     pedwarn (start_loc, OPT_pedantic, "ISO C90 forbids compound literals");
6351   non_const = ((init.value && TREE_CODE (init.value) == CONSTRUCTOR)
6352                ? CONSTRUCTOR_NON_CONST (init.value)
6353                : init.original_code == C_MAYBE_CONST_EXPR);
6354   non_const |= !type_expr_const;
6355   expr.value = build_compound_literal (start_loc, type, init.value, non_const);
6356   expr.original_code = ERROR_MARK;
6357   expr.original_type = NULL;
6358   if (type_expr)
6359     {
6360       if (TREE_CODE (expr.value) == C_MAYBE_CONST_EXPR)
6361         {
6362           gcc_assert (C_MAYBE_CONST_EXPR_PRE (expr.value) == NULL_TREE);
6363           C_MAYBE_CONST_EXPR_PRE (expr.value) = type_expr;
6364         }
6365       else
6366         {
6367           gcc_assert (!non_const);
6368           expr.value = build2 (C_MAYBE_CONST_EXPR, type,
6369                                type_expr, expr.value);
6370         }
6371     }
6372   return c_parser_postfix_expression_after_primary (parser, start_loc, expr);
6373 }
6374
6375 /* Parse a postfix expression after the initial primary or compound
6376    literal; that is, parse a series of postfix operators.
6377
6378    EXPR_LOC is the location of the primary expression.  */
6379
6380 static struct c_expr
6381 c_parser_postfix_expression_after_primary (c_parser *parser,
6382                                            location_t expr_loc,
6383                                            struct c_expr expr)
6384 {
6385   struct c_expr orig_expr;
6386   tree ident, idx;
6387   VEC(tree,gc) *exprlist;
6388   VEC(tree,gc) *origtypes;
6389   while (true)
6390     {
6391       location_t op_loc = c_parser_peek_token (parser)->location;
6392       switch (c_parser_peek_token (parser)->type)
6393         {
6394         case CPP_OPEN_SQUARE:
6395           /* Array reference.  */
6396           c_parser_consume_token (parser);
6397           idx = c_parser_expression (parser).value;
6398           c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
6399                                      "expected %<]%>");
6400           expr.value = build_array_ref (op_loc, expr.value, idx);
6401           expr.original_code = ERROR_MARK;
6402           expr.original_type = NULL;
6403           break;
6404         case CPP_OPEN_PAREN:
6405           /* Function call.  */
6406           c_parser_consume_token (parser);
6407           if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
6408             exprlist = NULL;
6409           else
6410             exprlist = c_parser_expr_list (parser, true, false, &origtypes);
6411           c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6412                                      "expected %<)%>");
6413           orig_expr = expr;
6414           mark_exp_read (expr.value);
6415           /* FIXME diagnostics: Ideally we want the FUNCNAME, not the
6416              "(" after the FUNCNAME, which is what we have now.    */
6417           expr.value = build_function_call_vec (op_loc, expr.value, exprlist,
6418                                                 origtypes);
6419           expr.original_code = ERROR_MARK;
6420           if (TREE_CODE (expr.value) == INTEGER_CST
6421               && TREE_CODE (orig_expr.value) == FUNCTION_DECL
6422               && DECL_BUILT_IN_CLASS (orig_expr.value) == BUILT_IN_NORMAL
6423               && DECL_FUNCTION_CODE (orig_expr.value) == BUILT_IN_CONSTANT_P)
6424             expr.original_code = C_MAYBE_CONST_EXPR;
6425           expr.original_type = NULL;
6426           if (exprlist != NULL)
6427             {
6428               release_tree_vector (exprlist);
6429               release_tree_vector (origtypes);
6430             }
6431           break;
6432         case CPP_DOT:
6433           /* Structure element reference.  */
6434           c_parser_consume_token (parser);
6435           expr = default_function_array_conversion (expr_loc, expr);
6436           if (c_parser_next_token_is (parser, CPP_NAME))
6437             ident = c_parser_peek_token (parser)->value;
6438           else
6439             {
6440               c_parser_error (parser, "expected identifier");
6441               expr.value = error_mark_node;
6442               expr.original_code = ERROR_MARK;
6443               expr.original_type = NULL;
6444               return expr;
6445             }
6446           c_parser_consume_token (parser);
6447           expr.value = build_component_ref (op_loc, expr.value, ident);
6448           expr.original_code = ERROR_MARK;
6449           if (TREE_CODE (expr.value) != COMPONENT_REF)
6450             expr.original_type = NULL;
6451           else
6452             {
6453               /* Remember the original type of a bitfield.  */
6454               tree field = TREE_OPERAND (expr.value, 1);
6455               if (TREE_CODE (field) != FIELD_DECL)
6456                 expr.original_type = NULL;
6457               else
6458                 expr.original_type = DECL_BIT_FIELD_TYPE (field);
6459             }
6460           break;
6461         case CPP_DEREF:
6462           /* Structure element reference.  */
6463           c_parser_consume_token (parser);
6464           expr = default_function_array_conversion (expr_loc, expr);
6465           if (c_parser_next_token_is (parser, CPP_NAME))
6466             ident = c_parser_peek_token (parser)->value;
6467           else
6468             {
6469               c_parser_error (parser, "expected identifier");
6470               expr.value = error_mark_node;
6471               expr.original_code = ERROR_MARK;
6472               expr.original_type = NULL;
6473               return expr;
6474             }
6475           c_parser_consume_token (parser);
6476           expr.value = build_component_ref (op_loc,
6477                                             build_indirect_ref (op_loc,
6478                                                                 expr.value,
6479                                                                 RO_ARROW),
6480                                             ident);
6481           expr.original_code = ERROR_MARK;
6482           if (TREE_CODE (expr.value) != COMPONENT_REF)
6483             expr.original_type = NULL;
6484           else
6485             {
6486               /* Remember the original type of a bitfield.  */
6487               tree field = TREE_OPERAND (expr.value, 1);
6488               if (TREE_CODE (field) != FIELD_DECL)
6489                 expr.original_type = NULL;
6490               else
6491                 expr.original_type = DECL_BIT_FIELD_TYPE (field);
6492             }
6493           break;
6494         case CPP_PLUS_PLUS:
6495           /* Postincrement.  */
6496           c_parser_consume_token (parser);
6497           expr = default_function_array_read_conversion (expr_loc, expr);
6498           expr.value = build_unary_op (op_loc,
6499                                        POSTINCREMENT_EXPR, expr.value, 0);
6500           expr.original_code = ERROR_MARK;
6501           expr.original_type = NULL;
6502           break;
6503         case CPP_MINUS_MINUS:
6504           /* Postdecrement.  */
6505           c_parser_consume_token (parser);
6506           expr = default_function_array_read_conversion (expr_loc, expr);
6507           expr.value = build_unary_op (op_loc,
6508                                        POSTDECREMENT_EXPR, expr.value, 0);
6509           expr.original_code = ERROR_MARK;
6510           expr.original_type = NULL;
6511           break;
6512         default:
6513           return expr;
6514         }
6515     }
6516 }
6517
6518 /* Parse an expression (C90 6.3.17, C99 6.5.17).
6519
6520    expression:
6521      assignment-expression
6522      expression , assignment-expression
6523 */
6524
6525 static struct c_expr
6526 c_parser_expression (c_parser *parser)
6527 {
6528   struct c_expr expr;
6529   expr = c_parser_expr_no_commas (parser, NULL);
6530   while (c_parser_next_token_is (parser, CPP_COMMA))
6531     {
6532       struct c_expr next;
6533       tree lhsval;
6534       location_t loc = c_parser_peek_token (parser)->location;
6535       location_t expr_loc;
6536       c_parser_consume_token (parser);
6537       expr_loc = c_parser_peek_token (parser)->location;
6538       lhsval = expr.value;
6539       while (TREE_CODE (lhsval) == COMPOUND_EXPR)
6540         lhsval = TREE_OPERAND (lhsval, 1);
6541       if (DECL_P (lhsval) || handled_component_p (lhsval))
6542         mark_exp_read (lhsval);
6543       next = c_parser_expr_no_commas (parser, NULL);
6544       next = default_function_array_conversion (expr_loc, next);
6545       expr.value = build_compound_expr (loc, expr.value, next.value);
6546       expr.original_code = COMPOUND_EXPR;
6547       expr.original_type = next.original_type;
6548     }
6549   return expr;
6550 }
6551
6552 /* Parse an expression and convert functions or arrays to
6553    pointers.  */
6554
6555 static struct c_expr
6556 c_parser_expression_conv (c_parser *parser)
6557 {
6558   struct c_expr expr;
6559   location_t loc = c_parser_peek_token (parser)->location;
6560   expr = c_parser_expression (parser);
6561   expr = default_function_array_conversion (loc, expr);
6562   return expr;
6563 }
6564
6565 /* Parse a non-empty list of expressions.  If CONVERT_P, convert
6566    functions and arrays to pointers.  If FOLD_P, fold the expressions.
6567
6568    nonempty-expr-list:
6569      assignment-expression
6570      nonempty-expr-list , assignment-expression
6571 */
6572
6573 static VEC(tree,gc) *
6574 c_parser_expr_list (c_parser *parser, bool convert_p, bool fold_p,
6575                     VEC(tree,gc) **p_orig_types)
6576 {
6577   VEC(tree,gc) *ret;
6578   VEC(tree,gc) *orig_types;
6579   struct c_expr expr;
6580   location_t loc = c_parser_peek_token (parser)->location;
6581
6582   ret = make_tree_vector ();
6583   if (p_orig_types == NULL)
6584     orig_types = NULL;
6585   else
6586     orig_types = make_tree_vector ();
6587
6588   expr = c_parser_expr_no_commas (parser, NULL);
6589   if (convert_p)
6590     expr = default_function_array_read_conversion (loc, expr);
6591   if (fold_p)
6592     expr.value = c_fully_fold (expr.value, false, NULL);
6593   VEC_quick_push (tree, ret, expr.value);
6594   if (orig_types != NULL)
6595     VEC_quick_push (tree, orig_types, expr.original_type);
6596   while (c_parser_next_token_is (parser, CPP_COMMA))
6597     {
6598       c_parser_consume_token (parser);
6599       loc = c_parser_peek_token (parser)->location;
6600       expr = c_parser_expr_no_commas (parser, NULL);
6601       if (convert_p)
6602         expr = default_function_array_read_conversion (loc, expr);
6603       if (fold_p)
6604         expr.value = c_fully_fold (expr.value, false, NULL);
6605       VEC_safe_push (tree, gc, ret, expr.value);
6606       if (orig_types != NULL)
6607         VEC_safe_push (tree, gc, orig_types, expr.original_type);
6608     }
6609   if (orig_types != NULL)
6610     *p_orig_types = orig_types;
6611   return ret;
6612 }
6613 \f
6614 /* Parse Objective-C-specific constructs.  */
6615
6616 /* Parse an objc-class-definition.
6617
6618    objc-class-definition:
6619      @interface identifier objc-superclass[opt] objc-protocol-refs[opt]
6620        objc-class-instance-variables[opt] objc-methodprotolist @end
6621      @implementation identifier objc-superclass[opt]
6622        objc-class-instance-variables[opt]
6623      @interface identifier ( identifier ) objc-protocol-refs[opt]
6624        objc-methodprotolist @end
6625      @implementation identifier ( identifier )
6626
6627    objc-superclass:
6628      : identifier
6629
6630    "@interface identifier (" must start "@interface identifier (
6631    identifier ) ...": objc-methodprotolist in the first production may
6632    not start with a parenthesized identifier as a declarator of a data
6633    definition with no declaration specifiers if the objc-superclass,
6634    objc-protocol-refs and objc-class-instance-variables are omitted.  */
6635
6636 static void
6637 c_parser_objc_class_definition (c_parser *parser, tree attributes)
6638 {
6639   bool iface_p;
6640   tree id1;
6641   tree superclass;
6642   if (c_parser_next_token_is_keyword (parser, RID_AT_INTERFACE))
6643     iface_p = true;
6644   else if (c_parser_next_token_is_keyword (parser, RID_AT_IMPLEMENTATION))
6645     iface_p = false;
6646   else
6647     gcc_unreachable ();
6648
6649   c_parser_consume_token (parser);
6650   if (c_parser_next_token_is_not (parser, CPP_NAME))
6651     {
6652       c_parser_error (parser, "expected identifier");
6653       return;
6654     }
6655   id1 = c_parser_peek_token (parser)->value;
6656   c_parser_consume_token (parser);
6657   if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
6658     {
6659       tree id2;
6660       tree proto = NULL_TREE;
6661       c_parser_consume_token (parser);
6662       if (c_parser_next_token_is_not (parser, CPP_NAME))
6663         {
6664           c_parser_error (parser, "expected identifier");
6665           c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6666           return;
6667         }
6668       id2 = c_parser_peek_token (parser)->value;
6669       c_parser_consume_token (parser);
6670       c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
6671       if (!iface_p)
6672         {
6673           objc_start_category_implementation (id1, id2);
6674           return;
6675         }
6676       if (c_parser_next_token_is (parser, CPP_LESS))
6677         proto = c_parser_objc_protocol_refs (parser);
6678       objc_start_category_interface (id1, id2, proto, attributes);
6679       c_parser_objc_methodprotolist (parser);
6680       c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
6681       objc_finish_interface ();
6682       return;
6683     }
6684   if (c_parser_next_token_is (parser, CPP_COLON))
6685     {
6686       c_parser_consume_token (parser);
6687       if (c_parser_next_token_is_not (parser, CPP_NAME))
6688         {
6689           c_parser_error (parser, "expected identifier");
6690           return;
6691         }
6692       superclass = c_parser_peek_token (parser)->value;
6693       c_parser_consume_token (parser);
6694     }
6695   else
6696     superclass = NULL_TREE;
6697   if (iface_p)
6698     {
6699       tree proto = NULL_TREE;
6700       if (c_parser_next_token_is (parser, CPP_LESS))
6701         proto = c_parser_objc_protocol_refs (parser);
6702       objc_start_class_interface (id1, superclass, proto, attributes);
6703     }
6704   else
6705     objc_start_class_implementation (id1, superclass);
6706   if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
6707     c_parser_objc_class_instance_variables (parser);
6708   if (iface_p)
6709     {
6710       objc_continue_interface ();
6711       c_parser_objc_methodprotolist (parser);
6712       c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
6713       objc_finish_interface ();
6714     }
6715   else
6716     {
6717       objc_continue_implementation ();
6718       return;
6719     }
6720 }
6721
6722 /* Parse objc-class-instance-variables.
6723
6724    objc-class-instance-variables:
6725      { objc-instance-variable-decl-list[opt] }
6726
6727    objc-instance-variable-decl-list:
6728      objc-visibility-spec
6729      objc-instance-variable-decl ;
6730      ;
6731      objc-instance-variable-decl-list objc-visibility-spec
6732      objc-instance-variable-decl-list objc-instance-variable-decl ;
6733      objc-instance-variable-decl-list ;
6734
6735    objc-visibility-spec:
6736      @private
6737      @protected
6738      @public
6739
6740    objc-instance-variable-decl:
6741      struct-declaration
6742 */
6743
6744 static void
6745 c_parser_objc_class_instance_variables (c_parser *parser)
6746 {
6747   gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE));
6748   c_parser_consume_token (parser);
6749   while (c_parser_next_token_is_not (parser, CPP_EOF))
6750     {
6751       tree decls;
6752       /* Parse any stray semicolon.  */
6753       if (c_parser_next_token_is (parser, CPP_SEMICOLON))
6754         {
6755           pedwarn (c_parser_peek_token (parser)->location, OPT_pedantic,
6756                    "extra semicolon in struct or union specified");
6757           c_parser_consume_token (parser);
6758           continue;
6759         }
6760       /* Stop if at the end of the instance variables.  */
6761       if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
6762         {
6763           c_parser_consume_token (parser);
6764           break;
6765         }
6766       /* Parse any objc-visibility-spec.  */
6767       if (c_parser_next_token_is_keyword (parser, RID_AT_PRIVATE))
6768         {
6769           c_parser_consume_token (parser);
6770           objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
6771           continue;
6772         }
6773       else if (c_parser_next_token_is_keyword (parser, RID_AT_PROTECTED))
6774         {
6775           c_parser_consume_token (parser);
6776           objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
6777           continue;
6778         }
6779       else if (c_parser_next_token_is_keyword (parser, RID_AT_PUBLIC))
6780         {
6781           c_parser_consume_token (parser);
6782           objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
6783           continue;
6784         }
6785       else if (c_parser_next_token_is_keyword (parser, RID_AT_PACKAGE))
6786         {
6787           c_parser_consume_token (parser);
6788           objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
6789           continue;
6790         }
6791       else if (c_parser_next_token_is (parser, CPP_PRAGMA))
6792         {
6793           c_parser_pragma (parser, pragma_external);
6794           continue;
6795         }
6796
6797       /* Parse some comma-separated declarations.  */
6798       decls = c_parser_struct_declaration (parser);
6799       {
6800         /* Comma-separated instance variables are chained together in
6801            reverse order; add them one by one.  */
6802         tree ivar = nreverse (decls);
6803         for (; ivar; ivar = DECL_CHAIN (ivar))
6804           objc_add_instance_variable (copy_node (ivar));
6805       }
6806       c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
6807     }
6808 }
6809
6810 /* Parse an objc-class-declaration.
6811
6812    objc-class-declaration:
6813      @class identifier-list ;
6814 */
6815
6816 static void
6817 c_parser_objc_class_declaration (c_parser *parser)
6818 {
6819   tree list = NULL_TREE;
6820   gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_CLASS));
6821   c_parser_consume_token (parser);
6822   /* Any identifiers, including those declared as type names, are OK
6823      here.  */
6824   while (true)
6825     {
6826       tree id;
6827       if (c_parser_next_token_is_not (parser, CPP_NAME))
6828         {
6829           c_parser_error (parser, "expected identifier");
6830           c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
6831           parser->error = false;
6832           return;
6833         }
6834       id = c_parser_peek_token (parser)->value;
6835       list = chainon (list, build_tree_list (NULL_TREE, id));
6836       c_parser_consume_token (parser);
6837       if (c_parser_next_token_is (parser, CPP_COMMA))
6838         c_parser_consume_token (parser);
6839       else
6840         break;
6841     }
6842   c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
6843   objc_declare_class (list);
6844 }
6845
6846 /* Parse an objc-alias-declaration.
6847
6848    objc-alias-declaration:
6849      @compatibility_alias identifier identifier ;
6850 */
6851
6852 static void
6853 c_parser_objc_alias_declaration (c_parser *parser)
6854 {
6855   tree id1, id2;
6856   gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_ALIAS));
6857   c_parser_consume_token (parser);
6858   if (c_parser_next_token_is_not (parser, CPP_NAME))
6859     {
6860       c_parser_error (parser, "expected identifier");
6861       c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
6862       return;
6863     }
6864   id1 = c_parser_peek_token (parser)->value;
6865   c_parser_consume_token (parser);
6866   if (c_parser_next_token_is_not (parser, CPP_NAME))
6867     {
6868       c_parser_error (parser, "expected identifier");
6869       c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
6870       return;
6871     }
6872   id2 = c_parser_peek_token (parser)->value;
6873   c_parser_consume_token (parser);
6874   c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
6875   objc_declare_alias (id1, id2);
6876 }
6877
6878 /* Parse an objc-protocol-definition.
6879
6880    objc-protocol-definition:
6881      @protocol identifier objc-protocol-refs[opt] objc-methodprotolist @end
6882      @protocol identifier-list ;
6883
6884    "@protocol identifier ;" should be resolved as "@protocol
6885    identifier-list ;": objc-methodprotolist may not start with a
6886    semicolon in the first alternative if objc-protocol-refs are
6887    omitted.  */
6888
6889 static void
6890 c_parser_objc_protocol_definition (c_parser *parser, tree attributes)
6891 {
6892   gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_PROTOCOL));
6893
6894   c_parser_consume_token (parser);
6895   if (c_parser_next_token_is_not (parser, CPP_NAME))
6896     {
6897       c_parser_error (parser, "expected identifier");
6898       return;
6899     }
6900   if (c_parser_peek_2nd_token (parser)->type == CPP_COMMA
6901       || c_parser_peek_2nd_token (parser)->type == CPP_SEMICOLON)
6902     {
6903       tree list = NULL_TREE;
6904       /* Any identifiers, including those declared as type names, are
6905          OK here.  */
6906       while (true)
6907         {
6908           tree id;
6909           if (c_parser_next_token_is_not (parser, CPP_NAME))
6910             {
6911               c_parser_error (parser, "expected identifier");
6912               break;
6913             }
6914           id = c_parser_peek_token (parser)->value;
6915           list = chainon (list, build_tree_list (NULL_TREE, id));
6916           c_parser_consume_token (parser);
6917           if (c_parser_next_token_is (parser, CPP_COMMA))
6918             c_parser_consume_token (parser);
6919           else
6920             break;
6921         }
6922       c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
6923       objc_declare_protocols (list);
6924     }
6925   else
6926     {
6927       tree id = c_parser_peek_token (parser)->value;
6928       tree proto = NULL_TREE;
6929       c_parser_consume_token (parser);
6930       if (c_parser_next_token_is (parser, CPP_LESS))
6931         proto = c_parser_objc_protocol_refs (parser);
6932       parser->objc_pq_context = true;
6933       objc_start_protocol (id, proto, attributes);
6934       c_parser_objc_methodprotolist (parser);
6935       c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
6936       parser->objc_pq_context = false;
6937       objc_finish_interface ();
6938     }
6939 }
6940
6941 /* Parse an objc-method-type.
6942
6943    objc-method-type:
6944      +
6945      -
6946
6947    Return true if it is a class method (+) and false if it is
6948    an instance method (-).
6949 */
6950 static inline bool
6951 c_parser_objc_method_type (c_parser *parser)
6952 {
6953   switch (c_parser_peek_token (parser)->type)
6954     {
6955     case CPP_PLUS:
6956       c_parser_consume_token (parser);
6957       return true;
6958     case CPP_MINUS:
6959       c_parser_consume_token (parser);
6960       return false;
6961     default:
6962       gcc_unreachable ();
6963     }
6964 }
6965
6966 /* Parse an objc-method-definition.
6967
6968    objc-method-definition:
6969      objc-method-type objc-method-decl ;[opt] compound-statement
6970 */
6971
6972 static void
6973 c_parser_objc_method_definition (c_parser *parser)
6974 {
6975   bool is_class_method = c_parser_objc_method_type (parser);
6976   tree decl, attributes = NULL_TREE;
6977   parser->objc_pq_context = true;
6978   decl = c_parser_objc_method_decl (parser, is_class_method, &attributes);
6979   if (decl == error_mark_node)
6980     return;  /* Bail here. */
6981
6982   if (c_parser_next_token_is (parser, CPP_SEMICOLON))
6983     {
6984       c_parser_consume_token (parser);
6985       pedwarn (c_parser_peek_token (parser)->location, OPT_pedantic,
6986                "extra semicolon in method definition specified");
6987     }
6988
6989   if (!c_parser_next_token_is (parser, CPP_OPEN_BRACE))
6990     {
6991       c_parser_error (parser, "expected %<{%>");
6992       return;
6993     }
6994
6995   parser->objc_pq_context = false;
6996   if (objc_start_method_definition (is_class_method, decl, attributes))
6997     {
6998       add_stmt (c_parser_compound_statement (parser));
6999       objc_finish_method_definition (current_function_decl);
7000     }
7001   else
7002     {
7003       /* This code is executed when we find a method definition
7004          outside of an @implementation context (or invalid for other
7005          reasons).  Parse the method (to keep going) but do not emit
7006          any code.
7007       */
7008       c_parser_compound_statement (parser);
7009     }
7010 }
7011
7012 /* Parse an objc-methodprotolist.
7013
7014    objc-methodprotolist:
7015      empty
7016      objc-methodprotolist objc-methodproto
7017      objc-methodprotolist declaration
7018      objc-methodprotolist ;
7019      @optional
7020      @required
7021
7022    The declaration is a data definition, which may be missing
7023    declaration specifiers under the same rules and diagnostics as
7024    other data definitions outside functions, and the stray semicolon
7025    is diagnosed the same way as a stray semicolon outside a
7026    function.  */
7027
7028 static void
7029 c_parser_objc_methodprotolist (c_parser *parser)
7030 {
7031   while (true)
7032     {
7033       /* The list is terminated by @end.  */
7034       switch (c_parser_peek_token (parser)->type)
7035         {
7036         case CPP_SEMICOLON:
7037           pedwarn (c_parser_peek_token (parser)->location, OPT_pedantic,
7038                    "ISO C does not allow extra %<;%> outside of a function");
7039           c_parser_consume_token (parser);
7040           break;
7041         case CPP_PLUS:
7042         case CPP_MINUS:
7043           c_parser_objc_methodproto (parser);
7044           break;
7045         case CPP_PRAGMA:
7046           c_parser_pragma (parser, pragma_external);
7047           break;
7048         case CPP_EOF:
7049           return;
7050         default:
7051           if (c_parser_next_token_is_keyword (parser, RID_AT_END))
7052             return;
7053           else if (c_parser_next_token_is_keyword (parser, RID_AT_PROPERTY))
7054             c_parser_objc_at_property_declaration (parser);
7055           else if (c_parser_next_token_is_keyword (parser, RID_AT_OPTIONAL))
7056             {
7057               objc_set_method_opt (true);
7058               c_parser_consume_token (parser);
7059             }
7060           else if (c_parser_next_token_is_keyword (parser, RID_AT_REQUIRED))
7061             {
7062               objc_set_method_opt (false);
7063               c_parser_consume_token (parser);
7064             }
7065           else
7066             c_parser_declaration_or_fndef (parser, false, false, true,
7067                                            false, true, NULL);
7068           break;
7069         }
7070     }
7071 }
7072
7073 /* Parse an objc-methodproto.
7074
7075    objc-methodproto:
7076      objc-method-type objc-method-decl ;
7077 */
7078
7079 static void
7080 c_parser_objc_methodproto (c_parser *parser)
7081 {
7082   bool is_class_method = c_parser_objc_method_type (parser);
7083   tree decl, attributes = NULL_TREE;
7084
7085   /* Remember protocol qualifiers in prototypes.  */
7086   parser->objc_pq_context = true;
7087   decl = c_parser_objc_method_decl (parser, is_class_method, &attributes);
7088   /* Forget protocol qualifiers now.  */
7089   parser->objc_pq_context = false;
7090
7091   /* Do not allow the presence of attributes to hide an erroneous 
7092      method implementation in the interface section.  */
7093   if (!c_parser_next_token_is (parser, CPP_SEMICOLON))
7094     {
7095       c_parser_error (parser, "expected %<;%>");
7096       return;
7097     }
7098   
7099   if (decl != error_mark_node)
7100     objc_add_method_declaration (is_class_method, decl, attributes);
7101
7102   c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
7103 }
7104
7105 /* If we are at a position that method attributes may be present, check that 
7106    there are not any parsed already (a syntax error) and then collect any 
7107    specified at the current location.  Finally, if new attributes were present,
7108    check that the next token is legal ( ';' for decls and '{' for defs).  */
7109    
7110 static bool 
7111 c_parser_objc_maybe_method_attributes (c_parser* parser, tree* attributes)
7112 {
7113   bool bad = false;
7114   if (*attributes)
7115     {
7116       c_parser_error (parser, 
7117                     "method attributes must be specified at the end only");
7118       *attributes = NULL_TREE;
7119       bad = true;
7120     }
7121
7122   if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
7123     *attributes = c_parser_attributes (parser);
7124
7125   /* If there were no attributes here, just report any earlier error.  */
7126   if (*attributes == NULL_TREE || bad)
7127     return bad;
7128
7129   /* If the attributes are followed by a ; or {, then just report any earlier
7130      error.  */
7131   if (c_parser_next_token_is (parser, CPP_SEMICOLON)
7132       || c_parser_next_token_is (parser, CPP_OPEN_BRACE))
7133     return bad;
7134
7135   /* We've got attributes, but not at the end.  */
7136   c_parser_error (parser, 
7137                   "expected %<;%> or %<{%> after method attribute definition");
7138   return true;
7139 }
7140
7141 /* Parse an objc-method-decl.
7142
7143    objc-method-decl:
7144      ( objc-type-name ) objc-selector
7145      objc-selector
7146      ( objc-type-name ) objc-keyword-selector objc-optparmlist
7147      objc-keyword-selector objc-optparmlist
7148      attributes
7149
7150    objc-keyword-selector:
7151      objc-keyword-decl
7152      objc-keyword-selector objc-keyword-decl
7153
7154    objc-keyword-decl:
7155      objc-selector : ( objc-type-name ) identifier
7156      objc-selector : identifier
7157      : ( objc-type-name ) identifier
7158      : identifier
7159
7160    objc-optparmlist:
7161      objc-optparms objc-optellipsis
7162
7163    objc-optparms:
7164      empty
7165      objc-opt-parms , parameter-declaration
7166
7167    objc-optellipsis:
7168      empty
7169      , ...
7170 */
7171
7172 static tree
7173 c_parser_objc_method_decl (c_parser *parser, bool is_class_method, tree *attributes)
7174 {
7175   tree type = NULL_TREE;
7176   tree sel;
7177   tree parms = NULL_TREE;
7178   bool ellipsis = false;
7179   bool attr_err = false;
7180
7181   *attributes = NULL_TREE;
7182   if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
7183     {
7184       c_parser_consume_token (parser);
7185       type = c_parser_objc_type_name (parser);
7186       c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
7187     }
7188   sel = c_parser_objc_selector (parser);
7189   /* If there is no selector, or a colon follows, we have an
7190      objc-keyword-selector.  If there is a selector, and a colon does
7191      not follow, that selector ends the objc-method-decl.  */
7192   if (!sel || c_parser_next_token_is (parser, CPP_COLON))
7193     {
7194       tree tsel = sel;
7195       tree list = NULL_TREE;
7196       while (true)
7197         {
7198           tree atype = NULL_TREE, id, keyworddecl;
7199           tree param_attr = NULL_TREE;
7200           if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
7201             break;
7202           if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
7203             {
7204               c_parser_consume_token (parser);
7205               atype = c_parser_objc_type_name (parser);
7206               c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7207                                          "expected %<)%>");
7208             }
7209           /* New ObjC allows attributes on method parameters.  */
7210           if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
7211             param_attr = c_parser_attributes (parser);
7212           if (c_parser_next_token_is_not (parser, CPP_NAME))
7213             {
7214               c_parser_error (parser, "expected identifier");
7215               return error_mark_node;
7216             }
7217           id = c_parser_peek_token (parser)->value;
7218           c_parser_consume_token (parser);
7219           keyworddecl = objc_build_keyword_decl (tsel, atype, id, param_attr);
7220           list = chainon (list, keyworddecl);
7221           tsel = c_parser_objc_selector (parser);
7222           if (!tsel && c_parser_next_token_is_not (parser, CPP_COLON))
7223             break;
7224         }
7225
7226       attr_err |= c_parser_objc_maybe_method_attributes (parser, attributes) ;
7227
7228       /* Parse the optional parameter list.  Optional Objective-C
7229          method parameters follow the C syntax, and may include '...'
7230          to denote a variable number of arguments.  */
7231       parms = make_node (TREE_LIST);
7232       while (c_parser_next_token_is (parser, CPP_COMMA))
7233         {
7234           struct c_parm *parm;
7235           c_parser_consume_token (parser);
7236           if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
7237             {
7238               ellipsis = true;
7239               c_parser_consume_token (parser);
7240               attr_err |= c_parser_objc_maybe_method_attributes 
7241                                                 (parser, attributes) ;
7242               break;
7243             }
7244           parm = c_parser_parameter_declaration (parser, NULL_TREE);
7245           if (parm == NULL)
7246             break;
7247           parms = chainon (parms,
7248                            build_tree_list (NULL_TREE, grokparm (parm)));
7249         }
7250       sel = list;
7251     }
7252   else
7253     attr_err |= c_parser_objc_maybe_method_attributes (parser, attributes) ;
7254
7255   if (sel == NULL)
7256     {
7257       c_parser_error (parser, "objective-c method declaration is expected");
7258       return error_mark_node;
7259     }
7260
7261   if (attr_err)
7262     return error_mark_node;
7263
7264   return objc_build_method_signature (is_class_method, type, sel, parms, ellipsis);
7265 }
7266
7267 /* Parse an objc-type-name.
7268
7269    objc-type-name:
7270      objc-type-qualifiers[opt] type-name
7271      objc-type-qualifiers[opt]
7272
7273    objc-type-qualifiers:
7274      objc-type-qualifier
7275      objc-type-qualifiers objc-type-qualifier
7276
7277    objc-type-qualifier: one of
7278      in out inout bycopy byref oneway
7279 */
7280
7281 static tree
7282 c_parser_objc_type_name (c_parser *parser)
7283 {
7284   tree quals = NULL_TREE;
7285   struct c_type_name *type_name = NULL;
7286   tree type = NULL_TREE;
7287   while (true)
7288     {
7289       c_token *token = c_parser_peek_token (parser);
7290       if (token->type == CPP_KEYWORD
7291           && (token->keyword == RID_IN
7292               || token->keyword == RID_OUT
7293               || token->keyword == RID_INOUT
7294               || token->keyword == RID_BYCOPY
7295               || token->keyword == RID_BYREF
7296               || token->keyword == RID_ONEWAY))
7297         {
7298           quals = chainon (build_tree_list (NULL_TREE, token->value), quals);
7299           c_parser_consume_token (parser);
7300         }
7301       else
7302         break;
7303     }
7304   if (c_parser_next_token_starts_typename (parser))
7305     type_name = c_parser_type_name (parser);
7306   if (type_name)
7307     type = groktypename (type_name, NULL, NULL);
7308   return build_tree_list (quals, type);
7309 }
7310
7311 /* Parse objc-protocol-refs.
7312
7313    objc-protocol-refs:
7314      < identifier-list >
7315 */
7316
7317 static tree
7318 c_parser_objc_protocol_refs (c_parser *parser)
7319 {
7320   tree list = NULL_TREE;
7321   gcc_assert (c_parser_next_token_is (parser, CPP_LESS));
7322   c_parser_consume_token (parser);
7323   /* Any identifiers, including those declared as type names, are OK
7324      here.  */
7325   while (true)
7326     {
7327       tree id;
7328       if (c_parser_next_token_is_not (parser, CPP_NAME))
7329         {
7330           c_parser_error (parser, "expected identifier");
7331           break;
7332         }
7333       id = c_parser_peek_token (parser)->value;
7334       list = chainon (list, build_tree_list (NULL_TREE, id));
7335       c_parser_consume_token (parser);
7336       if (c_parser_next_token_is (parser, CPP_COMMA))
7337         c_parser_consume_token (parser);
7338       else
7339         break;
7340     }
7341   c_parser_require (parser, CPP_GREATER, "expected %<>%>");
7342   return list;
7343 }
7344
7345 /* Parse an objc-try-catch-statement.
7346
7347    objc-try-catch-statement:
7348      @try compound-statement objc-catch-list[opt]
7349      @try compound-statement objc-catch-list[opt] @finally compound-statement
7350
7351    objc-catch-list:
7352      @catch ( parameter-declaration ) compound-statement
7353      objc-catch-list @catch ( parameter-declaration ) compound-statement
7354 */
7355
7356 static void
7357 c_parser_objc_try_catch_statement (c_parser *parser)
7358 {
7359   location_t loc;
7360   tree stmt;
7361   gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_TRY));
7362   c_parser_consume_token (parser);
7363   loc = c_parser_peek_token (parser)->location;
7364   stmt = c_parser_compound_statement (parser);
7365   objc_begin_try_stmt (loc, stmt);
7366   while (c_parser_next_token_is_keyword (parser, RID_AT_CATCH))
7367     {
7368       struct c_parm *parm;
7369       c_parser_consume_token (parser);
7370       if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7371         break;
7372       parm = c_parser_parameter_declaration (parser, NULL_TREE);
7373       if (parm == NULL)
7374         {
7375           c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7376           break;
7377         }
7378       c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
7379       objc_begin_catch_clause (grokparm (parm));
7380       if (c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
7381         c_parser_compound_statement_nostart (parser);
7382       objc_finish_catch_clause ();
7383     }
7384   if (c_parser_next_token_is_keyword (parser, RID_AT_FINALLY))
7385     {
7386       location_t finloc;
7387       tree finstmt;
7388       c_parser_consume_token (parser);
7389       finloc = c_parser_peek_token (parser)->location;
7390       finstmt = c_parser_compound_statement (parser);
7391       objc_build_finally_clause (finloc, finstmt);
7392     }
7393   objc_finish_try_stmt ();
7394 }
7395
7396 /* Parse an objc-synchronized-statement.
7397
7398    objc-synchronized-statement:
7399      @synchronized ( expression ) compound-statement
7400 */
7401
7402 static void
7403 c_parser_objc_synchronized_statement (c_parser *parser)
7404 {
7405   location_t loc;
7406   tree expr, stmt;
7407   gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_SYNCHRONIZED));
7408   c_parser_consume_token (parser);
7409   loc = c_parser_peek_token (parser)->location;
7410   if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7411     {
7412       expr = c_parser_expression (parser).value;
7413       expr = c_fully_fold (expr, false, NULL);
7414       c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
7415     }
7416   else
7417     expr = error_mark_node;
7418   stmt = c_parser_compound_statement (parser);
7419   objc_build_synchronized (loc, expr, stmt);
7420 }
7421
7422 /* Parse an objc-selector; return NULL_TREE without an error if the
7423    next token is not an objc-selector.
7424
7425    objc-selector:
7426      identifier
7427      one of
7428        enum struct union if else while do for switch case default
7429        break continue return goto asm sizeof typeof __alignof
7430        unsigned long const short volatile signed restrict _Complex
7431        in out inout bycopy byref oneway int char float double void _Bool
7432
7433    ??? Why this selection of keywords but not, for example, storage
7434    class specifiers?  */
7435
7436 static tree
7437 c_parser_objc_selector (c_parser *parser)
7438 {
7439   c_token *token = c_parser_peek_token (parser);
7440   tree value = token->value;
7441   if (token->type == CPP_NAME)
7442     {
7443       c_parser_consume_token (parser);
7444       return value;
7445     }
7446   if (token->type != CPP_KEYWORD)
7447     return NULL_TREE;
7448   switch (token->keyword)
7449     {
7450     case RID_ENUM:
7451     case RID_STRUCT:
7452     case RID_UNION:
7453     case RID_IF:
7454     case RID_ELSE:
7455     case RID_WHILE:
7456     case RID_DO:
7457     case RID_FOR:
7458     case RID_SWITCH:
7459     case RID_CASE:
7460     case RID_DEFAULT:
7461     case RID_BREAK:
7462     case RID_CONTINUE:
7463     case RID_RETURN:
7464     case RID_GOTO:
7465     case RID_ASM:
7466     case RID_SIZEOF:
7467     case RID_TYPEOF:
7468     case RID_ALIGNOF:
7469     case RID_UNSIGNED:
7470     case RID_LONG:
7471     case RID_INT128:
7472     case RID_CONST:
7473     case RID_SHORT:
7474     case RID_VOLATILE:
7475     case RID_SIGNED:
7476     case RID_RESTRICT:
7477     case RID_COMPLEX:
7478     case RID_IN:
7479     case RID_OUT:
7480     case RID_INOUT:
7481     case RID_BYCOPY:
7482     case RID_BYREF:
7483     case RID_ONEWAY:
7484     case RID_INT:
7485     case RID_CHAR:
7486     case RID_FLOAT:
7487     case RID_DOUBLE:
7488     case RID_VOID:
7489     case RID_BOOL:
7490       c_parser_consume_token (parser);
7491       return value;
7492     default:
7493       return NULL_TREE;
7494     }
7495 }
7496
7497 /* Parse an objc-selector-arg.
7498
7499    objc-selector-arg:
7500      objc-selector
7501      objc-keywordname-list
7502
7503    objc-keywordname-list:
7504      objc-keywordname
7505      objc-keywordname-list objc-keywordname
7506
7507    objc-keywordname:
7508      objc-selector :
7509      :
7510 */
7511
7512 static tree
7513 c_parser_objc_selector_arg (c_parser *parser)
7514 {
7515   tree sel = c_parser_objc_selector (parser);
7516   tree list = NULL_TREE;
7517   if (sel && c_parser_next_token_is_not (parser, CPP_COLON))
7518     return sel;
7519   while (true)
7520     {
7521       if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
7522         return list;
7523       list = chainon (list, build_tree_list (sel, NULL_TREE));
7524       sel = c_parser_objc_selector (parser);
7525       if (!sel && c_parser_next_token_is_not (parser, CPP_COLON))
7526         break;
7527     }
7528   return list;
7529 }
7530
7531 /* Parse an objc-receiver.
7532
7533    objc-receiver:
7534      expression
7535      class-name
7536      type-name
7537 */
7538
7539 static tree
7540 c_parser_objc_receiver (c_parser *parser)
7541 {
7542   if (c_parser_peek_token (parser)->type == CPP_NAME
7543       && (c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME
7544           || c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME))
7545     {
7546       tree id = c_parser_peek_token (parser)->value;
7547       c_parser_consume_token (parser);
7548       return objc_get_class_reference (id);
7549     }
7550   return c_fully_fold (c_parser_expression (parser).value, false, NULL);
7551 }
7552
7553 /* Parse objc-message-args.
7554
7555    objc-message-args:
7556      objc-selector
7557      objc-keywordarg-list
7558
7559    objc-keywordarg-list:
7560      objc-keywordarg
7561      objc-keywordarg-list objc-keywordarg
7562
7563    objc-keywordarg:
7564      objc-selector : objc-keywordexpr
7565      : objc-keywordexpr
7566 */
7567
7568 static tree
7569 c_parser_objc_message_args (c_parser *parser)
7570 {
7571   tree sel = c_parser_objc_selector (parser);
7572   tree list = NULL_TREE;
7573   if (sel && c_parser_next_token_is_not (parser, CPP_COLON))
7574     return sel;
7575   while (true)
7576     {
7577       tree keywordexpr;
7578       if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
7579         return error_mark_node;
7580       keywordexpr = c_parser_objc_keywordexpr (parser);
7581       list = chainon (list, build_tree_list (sel, keywordexpr));
7582       sel = c_parser_objc_selector (parser);
7583       if (!sel && c_parser_next_token_is_not (parser, CPP_COLON))
7584         break;
7585     }
7586   return list;
7587 }
7588
7589 /* Parse an objc-keywordexpr.
7590
7591    objc-keywordexpr:
7592      nonempty-expr-list
7593 */
7594
7595 static tree
7596 c_parser_objc_keywordexpr (c_parser *parser)
7597 {
7598   tree ret;
7599   VEC(tree,gc) *expr_list = c_parser_expr_list (parser, true, true, NULL);
7600   if (VEC_length (tree, expr_list) == 1)
7601     {
7602       /* Just return the expression, remove a level of
7603          indirection.  */
7604       ret = VEC_index (tree, expr_list, 0);
7605     }
7606   else
7607     {
7608       /* We have a comma expression, we will collapse later.  */
7609       ret = build_tree_list_vec (expr_list);
7610     }
7611   release_tree_vector (expr_list);
7612   return ret;
7613 }
7614
7615 /* A check, needed in several places, that ObjC interface, implementation or
7616    method definitions are not prefixed by incorrect items.  */
7617 static bool
7618 c_parser_objc_diagnose_bad_element_prefix (c_parser *parser, 
7619                                            struct c_declspecs *specs)
7620 {
7621   if (!specs->declspecs_seen_p || specs->type_seen_p || specs->non_sc_seen_p)
7622     {
7623       c_parser_error (parser, 
7624                       "no type or storage class may be specified here,");
7625       c_parser_skip_to_end_of_block_or_statement (parser);
7626       return true;
7627     }
7628   return false;
7629 }
7630
7631 /* Parse an Objective-C @property declaration.  The syntax is:
7632
7633    objc-property-declaration:
7634      '@property' objc-property-attributes[opt] struct-declaration ;
7635
7636    objc-property-attributes:
7637     '(' objc-property-attribute-list ')'
7638
7639    objc-property-attribute-list:
7640      objc-property-attribute
7641      objc-property-attribute-list, objc-property-attribute
7642
7643    objc-property-attribute
7644      'getter' = identifier
7645      'setter' = identifier
7646      'readonly'
7647      'readwrite'
7648      'assign'
7649      'retain'
7650      'copy'
7651      'nonatomic'
7652
7653   For example:
7654     @property NSString *name;
7655     @property (readonly) id object;
7656     @property (retain, nonatomic, getter=getTheName) id name;
7657     @property int a, b, c;
7658
7659   PS: This function is identical to cp_parser_objc_at_propery_declaration
7660   for C++.  Keep them in sync.  */
7661 static void
7662 c_parser_objc_at_property_declaration (c_parser *parser)
7663 {
7664   /* The following variables hold the attributes of the properties as
7665      parsed.  They are 'false' or 'NULL_TREE' if the attribute was not
7666      seen.  When we see an attribute, we set them to 'true' (if they
7667      are boolean properties) or to the identifier (if they have an
7668      argument, ie, for getter and setter).  Note that here we only
7669      parse the list of attributes, check the syntax and accumulate the
7670      attributes that we find.  objc_add_property_declaration() will
7671      then process the information.  */
7672   bool property_assign = false;
7673   bool property_copy = false;
7674   tree property_getter_ident = NULL_TREE;
7675   bool property_nonatomic = false;
7676   bool property_readonly = false;
7677   bool property_readwrite = false;
7678   bool property_retain = false;
7679   tree property_setter_ident = NULL_TREE;
7680
7681   /* 'properties' is the list of properties that we read.  Usually a
7682      single one, but maybe more (eg, in "@property int a, b, c;" there
7683      are three).  */
7684   tree properties;
7685   location_t loc;
7686
7687   loc = c_parser_peek_token (parser)->location;
7688   gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_PROPERTY));
7689
7690   c_parser_consume_token (parser);  /* Eat '@property'.  */
7691
7692   /* Parse the optional attribute list...  */
7693   if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
7694     {
7695       /* Eat the '(' */
7696       c_parser_consume_token (parser);
7697       
7698       /* Property attribute keywords are valid now.  */
7699       parser->objc_property_attr_context = true;
7700
7701       while (true)
7702         {
7703           bool syntax_error = false;
7704           c_token *token = c_parser_peek_token (parser);
7705           enum rid keyword;
7706
7707           if (token->type != CPP_KEYWORD)
7708             {
7709               if (token->type == CPP_CLOSE_PAREN)
7710                 c_parser_error (parser, "expected identifier");
7711               else
7712                 {
7713                   c_parser_consume_token (parser);
7714                   c_parser_error (parser, "unknown property attribute");
7715                 }
7716               break;
7717             }
7718           keyword = token->keyword;
7719           c_parser_consume_token (parser);
7720           switch (keyword)
7721             {
7722             case RID_ASSIGN:    property_assign = true;    break;
7723             case RID_COPY:      property_copy = true;      break;
7724             case RID_NONATOMIC: property_nonatomic = true; break;
7725             case RID_READONLY:  property_readonly = true;  break;
7726             case RID_READWRITE: property_readwrite = true; break;
7727             case RID_RETAIN:    property_retain = true;    break;
7728
7729             case RID_GETTER:
7730             case RID_SETTER:
7731               if (c_parser_next_token_is_not (parser, CPP_EQ))
7732                 {
7733                   c_parser_error (parser,
7734                                   "getter/setter attribute must be followed by %<=%>");
7735                   syntax_error = true;
7736                   break;
7737                 }
7738               c_parser_consume_token (parser); /* eat the = */
7739               if (c_parser_next_token_is_not (parser, CPP_NAME))
7740                 {
7741                   c_parser_error (parser, "expected identifier");
7742                   syntax_error = true;
7743                   break;
7744                 }
7745               if (keyword == RID_SETTER)
7746                 {
7747                   if (property_setter_ident != NULL_TREE)
7748                     c_parser_error (parser, "the %<setter%> attribute may only be specified once");
7749                   else
7750                     property_setter_ident = c_parser_peek_token (parser)->value;
7751                   c_parser_consume_token (parser);
7752                   if (c_parser_next_token_is_not (parser, CPP_COLON))
7753                     c_parser_error (parser, "setter name must terminate with %<:%>");
7754                   else
7755                     c_parser_consume_token (parser);
7756                 }
7757               else
7758                 {
7759                   if (property_getter_ident != NULL_TREE)
7760                     c_parser_error (parser, "the %<getter%> attribute may only be specified once");
7761                   else
7762                     property_getter_ident = c_parser_peek_token (parser)->value;
7763                   c_parser_consume_token (parser);
7764                 }
7765               break;
7766             default:
7767               c_parser_error (parser, "unknown property attribute");
7768               syntax_error = true;
7769               break;
7770             }
7771
7772           if (syntax_error)
7773             break;
7774           
7775           if (c_parser_next_token_is (parser, CPP_COMMA))
7776             c_parser_consume_token (parser);
7777           else
7778             break;
7779         }
7780       parser->objc_property_attr_context = false;
7781       c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
7782     }
7783   /* ... and the property declaration(s).  */
7784   properties = c_parser_struct_declaration (parser);
7785
7786   if (properties == error_mark_node)
7787     {
7788       c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
7789       parser->error = false;
7790       return;
7791     }
7792
7793   if (properties == NULL_TREE)
7794     c_parser_error (parser, "expected identifier");
7795   else
7796     {
7797       /* Comma-separated properties are chained together in
7798          reverse order; add them one by one.  */
7799       properties = nreverse (properties);
7800       
7801       for (; properties; properties = TREE_CHAIN (properties))
7802         objc_add_property_declaration (loc, copy_node (properties),
7803                                        property_readonly, property_readwrite,
7804                                        property_assign, property_retain,
7805                                        property_copy, property_nonatomic,
7806                                        property_getter_ident, property_setter_ident);
7807     }
7808
7809   c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
7810   parser->error = false;
7811 }
7812
7813 /* Parse an Objective-C @synthesize declaration.  The syntax is:
7814
7815    objc-synthesize-declaration:
7816      @synthesize objc-synthesize-identifier-list ;
7817
7818    objc-synthesize-identifier-list:
7819      objc-synthesize-identifier
7820      objc-synthesize-identifier-list, objc-synthesize-identifier
7821
7822    objc-synthesize-identifier
7823      identifier
7824      identifier = identifier
7825
7826   For example:
7827     @synthesize MyProperty;
7828     @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
7829
7830   PS: This function is identical to cp_parser_objc_at_synthesize_declaration
7831   for C++.  Keep them in sync.
7832 */
7833 static void
7834 c_parser_objc_at_synthesize_declaration (c_parser *parser)
7835 {
7836   tree list = NULL_TREE;
7837   location_t loc;
7838   gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_SYNTHESIZE));
7839   loc = c_parser_peek_token (parser)->location;
7840
7841   c_parser_consume_token (parser);
7842   while (true)
7843     {
7844       tree property, ivar;
7845       if (c_parser_next_token_is_not (parser, CPP_NAME))
7846         {
7847           c_parser_error (parser, "expected identifier");
7848           c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
7849           /* Once we find the semicolon, we can resume normal parsing.
7850              We have to reset parser->error manually because
7851              c_parser_skip_until_found() won't reset it for us if the
7852              next token is precisely a semicolon.  */
7853           parser->error = false;
7854           return;
7855         }
7856       property = c_parser_peek_token (parser)->value;
7857       c_parser_consume_token (parser);
7858       if (c_parser_next_token_is (parser, CPP_EQ))
7859         {
7860           c_parser_consume_token (parser);
7861           if (c_parser_next_token_is_not (parser, CPP_NAME))
7862             {
7863               c_parser_error (parser, "expected identifier");
7864               c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
7865               parser->error = false;
7866               return;
7867             }
7868           ivar = c_parser_peek_token (parser)->value;
7869           c_parser_consume_token (parser);
7870         }
7871       else
7872         ivar = NULL_TREE;
7873       list = chainon (list, build_tree_list (ivar, property));
7874       if (c_parser_next_token_is (parser, CPP_COMMA))
7875         c_parser_consume_token (parser);
7876       else
7877         break;
7878     }
7879   c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
7880   objc_add_synthesize_declaration (loc, list);
7881 }
7882
7883 /* Parse an Objective-C @dynamic declaration.  The syntax is:
7884
7885    objc-dynamic-declaration:
7886      @dynamic identifier-list ;
7887
7888    For example:
7889      @dynamic MyProperty;
7890      @dynamic MyProperty, AnotherProperty;
7891
7892   PS: This function is identical to cp_parser_objc_at_dynamic_declaration
7893   for C++.  Keep them in sync.
7894 */
7895 static void
7896 c_parser_objc_at_dynamic_declaration (c_parser *parser)
7897 {
7898   tree list = NULL_TREE;
7899   location_t loc;
7900   gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_DYNAMIC));
7901   loc = c_parser_peek_token (parser)->location;
7902
7903   c_parser_consume_token (parser);
7904   while (true)
7905     {
7906       tree property;
7907       if (c_parser_next_token_is_not (parser, CPP_NAME))
7908         {
7909           c_parser_error (parser, "expected identifier");
7910           c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
7911           parser->error = false;
7912           return;
7913         }
7914       property = c_parser_peek_token (parser)->value;
7915       list = chainon (list, build_tree_list (NULL_TREE, property));
7916       c_parser_consume_token (parser);
7917       if (c_parser_next_token_is (parser, CPP_COMMA))
7918         c_parser_consume_token (parser);
7919       else
7920         break;
7921     }
7922   c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
7923   objc_add_dynamic_declaration (loc, list);
7924 }
7925
7926 \f
7927 /* Handle pragmas.  Some OpenMP pragmas are associated with, and therefore
7928    should be considered, statements.  ALLOW_STMT is true if we're within
7929    the context of a function and such pragmas are to be allowed.  Returns
7930    true if we actually parsed such a pragma.  */
7931
7932 static bool
7933 c_parser_pragma (c_parser *parser, enum pragma_context context)
7934 {
7935   unsigned int id;
7936
7937   id = c_parser_peek_token (parser)->pragma_kind;
7938   gcc_assert (id != PRAGMA_NONE);
7939
7940   switch (id)
7941     {
7942     case PRAGMA_OMP_BARRIER:
7943       if (context != pragma_compound)
7944         {
7945           if (context == pragma_stmt)
7946             c_parser_error (parser, "%<#pragma omp barrier%> may only be "
7947                             "used in compound statements");
7948           goto bad_stmt;
7949         }
7950       c_parser_omp_barrier (parser);
7951       return false;
7952
7953     case PRAGMA_OMP_FLUSH:
7954       if (context != pragma_compound)
7955         {
7956           if (context == pragma_stmt)
7957             c_parser_error (parser, "%<#pragma omp flush%> may only be "
7958                             "used in compound statements");
7959           goto bad_stmt;
7960         }
7961       c_parser_omp_flush (parser);
7962       return false;
7963
7964     case PRAGMA_OMP_TASKWAIT:
7965       if (context != pragma_compound)
7966         {
7967           if (context == pragma_stmt)
7968             c_parser_error (parser, "%<#pragma omp taskwait%> may only be "
7969                             "used in compound statements");
7970           goto bad_stmt;
7971         }
7972       c_parser_omp_taskwait (parser);
7973       return false;
7974
7975     case PRAGMA_OMP_THREADPRIVATE:
7976       c_parser_omp_threadprivate (parser);
7977       return false;
7978
7979     case PRAGMA_OMP_SECTION:
7980       error_at (c_parser_peek_token (parser)->location,
7981                 "%<#pragma omp section%> may only be used in "
7982                 "%<#pragma omp sections%> construct");
7983       c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
7984       return false;
7985
7986     case PRAGMA_GCC_PCH_PREPROCESS:
7987       c_parser_error (parser, "%<#pragma GCC pch_preprocess%> must be first");
7988       c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
7989       return false;
7990
7991     default:
7992       if (id < PRAGMA_FIRST_EXTERNAL)
7993         {
7994           if (context == pragma_external)
7995             {
7996             bad_stmt:
7997               c_parser_error (parser, "expected declaration specifiers");
7998               c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
7999               return false;
8000             }
8001           c_parser_omp_construct (parser);
8002           return true;
8003         }
8004       break;
8005     }
8006
8007   c_parser_consume_pragma (parser);
8008   c_invoke_pragma_handler (id);
8009
8010   /* Skip to EOL, but suppress any error message.  Those will have been
8011      generated by the handler routine through calling error, as opposed
8012      to calling c_parser_error.  */
8013   parser->error = true;
8014   c_parser_skip_to_pragma_eol (parser);
8015
8016   return false;
8017 }
8018
8019 /* The interface the pragma parsers have to the lexer.  */
8020
8021 enum cpp_ttype
8022 pragma_lex (tree *value)
8023 {
8024   c_token *tok = c_parser_peek_token (the_parser);
8025   enum cpp_ttype ret = tok->type;
8026
8027   *value = tok->value;
8028   if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
8029     ret = CPP_EOF;
8030   else
8031     {
8032       if (ret == CPP_KEYWORD)
8033         ret = CPP_NAME;
8034       c_parser_consume_token (the_parser);
8035     }
8036
8037   return ret;
8038 }
8039
8040 static void
8041 c_parser_pragma_pch_preprocess (c_parser *parser)
8042 {
8043   tree name = NULL;
8044
8045   c_parser_consume_pragma (parser);
8046   if (c_parser_next_token_is (parser, CPP_STRING))
8047     {
8048       name = c_parser_peek_token (parser)->value;
8049       c_parser_consume_token (parser);
8050     }
8051   else
8052     c_parser_error (parser, "expected string literal");
8053   c_parser_skip_to_pragma_eol (parser);
8054
8055   if (name)
8056     c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
8057 }
8058 \f
8059 /* OpenMP 2.5 parsing routines.  */
8060
8061 /* Returns name of the next clause.
8062    If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
8063    the token is not consumed.  Otherwise appropriate pragma_omp_clause is
8064    returned and the token is consumed.  */
8065
8066 static pragma_omp_clause
8067 c_parser_omp_clause_name (c_parser *parser)
8068 {
8069   pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
8070
8071   if (c_parser_next_token_is_keyword (parser, RID_IF))
8072     result = PRAGMA_OMP_CLAUSE_IF;
8073   else if (c_parser_next_token_is_keyword (parser, RID_DEFAULT))
8074     result = PRAGMA_OMP_CLAUSE_DEFAULT;
8075   else if (c_parser_next_token_is (parser, CPP_NAME))
8076     {
8077       const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
8078
8079       switch (p[0])
8080         {
8081         case 'c':
8082           if (!strcmp ("collapse", p))
8083             result = PRAGMA_OMP_CLAUSE_COLLAPSE;
8084           else if (!strcmp ("copyin", p))
8085             result = PRAGMA_OMP_CLAUSE_COPYIN;
8086           else if (!strcmp ("copyprivate", p))
8087             result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
8088           break;
8089         case 'f':
8090           if (!strcmp ("firstprivate", p))
8091             result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
8092           break;
8093         case 'l':
8094           if (!strcmp ("lastprivate", p))
8095             result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
8096           break;
8097         case 'n':
8098           if (!strcmp ("nowait", p))
8099             result = PRAGMA_OMP_CLAUSE_NOWAIT;
8100           else if (!strcmp ("num_threads", p))
8101             result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
8102           break;
8103         case 'o':
8104           if (!strcmp ("ordered", p))
8105             result = PRAGMA_OMP_CLAUSE_ORDERED;
8106           break;
8107         case 'p':
8108           if (!strcmp ("private", p))
8109             result = PRAGMA_OMP_CLAUSE_PRIVATE;
8110           break;
8111         case 'r':
8112           if (!strcmp ("reduction", p))
8113             result = PRAGMA_OMP_CLAUSE_REDUCTION;
8114           break;
8115         case 's':
8116           if (!strcmp ("schedule", p))
8117             result = PRAGMA_OMP_CLAUSE_SCHEDULE;
8118           else if (!strcmp ("shared", p))
8119             result = PRAGMA_OMP_CLAUSE_SHARED;
8120           break;
8121         case 'u':
8122           if (!strcmp ("untied", p))
8123             result = PRAGMA_OMP_CLAUSE_UNTIED;
8124           break;
8125         }
8126     }
8127
8128   if (result != PRAGMA_OMP_CLAUSE_NONE)
8129     c_parser_consume_token (parser);
8130
8131   return result;
8132 }
8133
8134 /* Validate that a clause of the given type does not already exist.  */
8135
8136 static void
8137 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
8138                            const char *name)
8139 {
8140   tree c;
8141
8142   for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
8143     if (OMP_CLAUSE_CODE (c) == code)
8144       {
8145         location_t loc = OMP_CLAUSE_LOCATION (c);
8146         error_at (loc, "too many %qs clauses", name);
8147         break;
8148       }
8149 }
8150
8151 /* OpenMP 2.5:
8152    variable-list:
8153      identifier
8154      variable-list , identifier
8155
8156    If KIND is nonzero, create the appropriate node and install the
8157    decl in OMP_CLAUSE_DECL and add the node to the head of the list.
8158    If KIND is nonzero, CLAUSE_LOC is the location of the clause.
8159
8160    If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
8161    return the list created.  */
8162
8163 static tree
8164 c_parser_omp_variable_list (c_parser *parser,
8165                             location_t clause_loc,
8166                             enum omp_clause_code kind,
8167                             tree list)
8168 {
8169   if (c_parser_next_token_is_not (parser, CPP_NAME)
8170       || c_parser_peek_token (parser)->id_kind != C_ID_ID)
8171     c_parser_error (parser, "expected identifier");
8172
8173   while (c_parser_next_token_is (parser, CPP_NAME)
8174          && c_parser_peek_token (parser)->id_kind == C_ID_ID)
8175     {
8176       tree t = lookup_name (c_parser_peek_token (parser)->value);
8177
8178       if (t == NULL_TREE)
8179         undeclared_variable (c_parser_peek_token (parser)->location,
8180                              c_parser_peek_token (parser)->value);
8181       else if (t == error_mark_node)
8182         ;
8183       else if (kind != 0)
8184         {
8185           tree u = build_omp_clause (clause_loc, kind);
8186           OMP_CLAUSE_DECL (u) = t;
8187           OMP_CLAUSE_CHAIN (u) = list;
8188           list = u;
8189         }
8190       else
8191         list = tree_cons (t, NULL_TREE, list);
8192
8193       c_parser_consume_token (parser);
8194
8195       if (c_parser_next_token_is_not (parser, CPP_COMMA))
8196         break;
8197
8198       c_parser_consume_token (parser);
8199     }
8200
8201   return list;
8202 }
8203
8204 /* Similarly, but expect leading and trailing parenthesis.  This is a very
8205    common case for omp clauses.  */
8206
8207 static tree
8208 c_parser_omp_var_list_parens (c_parser *parser, enum omp_clause_code kind,
8209                               tree list)
8210 {
8211   /* The clauses location.  */
8212   location_t loc = c_parser_peek_token (parser)->location;
8213
8214   if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
8215     {
8216       list = c_parser_omp_variable_list (parser, loc, kind, list);
8217       c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
8218     }
8219   return list;
8220 }
8221
8222 /* OpenMP 3.0:
8223    collapse ( constant-expression ) */
8224
8225 static tree
8226 c_parser_omp_clause_collapse (c_parser *parser, tree list)
8227 {
8228   tree c, num = error_mark_node;
8229   HOST_WIDE_INT n;
8230   location_t loc;
8231
8232   check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse");
8233
8234   loc = c_parser_peek_token (parser)->location;
8235   if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
8236     {
8237       num = c_parser_expr_no_commas (parser, NULL).value;
8238       c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
8239     }
8240   if (num == error_mark_node)
8241     return list;
8242   if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
8243       || !host_integerp (num, 0)
8244       || (n = tree_low_cst (num, 0)) <= 0
8245       || (int) n != n)
8246     {
8247       error_at (loc,
8248                 "collapse argument needs positive constant integer expression");
8249       return list;
8250     }
8251   c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
8252   OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
8253   OMP_CLAUSE_CHAIN (c) = list;
8254   return c;
8255 }
8256
8257 /* OpenMP 2.5:
8258    copyin ( variable-list ) */
8259
8260 static tree
8261 c_parser_omp_clause_copyin (c_parser *parser, tree list)
8262 {
8263   return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_COPYIN, list);
8264 }
8265
8266 /* OpenMP 2.5:
8267    copyprivate ( variable-list ) */
8268
8269 static tree
8270 c_parser_omp_clause_copyprivate (c_parser *parser, tree list)
8271 {
8272   return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_COPYPRIVATE, list);
8273 }
8274
8275 /* OpenMP 2.5:
8276    default ( shared | none ) */
8277
8278 static tree
8279 c_parser_omp_clause_default (c_parser *parser, tree list)
8280 {
8281   enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
8282   location_t loc = c_parser_peek_token (parser)->location;
8283   tree c;
8284
8285   if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
8286     return list;
8287   if (c_parser_next_token_is (parser, CPP_NAME))
8288     {
8289       const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
8290
8291       switch (p[0])
8292         {
8293         case 'n':
8294           if (strcmp ("none", p) != 0)
8295             goto invalid_kind;
8296           kind = OMP_CLAUSE_DEFAULT_NONE;
8297           break;
8298
8299         case 's':
8300           if (strcmp ("shared", p) != 0)
8301             goto invalid_kind;
8302           kind = OMP_CLAUSE_DEFAULT_SHARED;
8303           break;
8304
8305         default:
8306           goto invalid_kind;
8307         }
8308
8309       c_parser_consume_token (parser);
8310     }
8311   else
8312     {
8313     invalid_kind:
8314       c_parser_error (parser, "expected %<none%> or %<shared%>");
8315     }
8316   c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
8317
8318   if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
8319     return list;
8320
8321   check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default");
8322   c = build_omp_clause (loc, OMP_CLAUSE_DEFAULT);
8323   OMP_CLAUSE_CHAIN (c) = list;
8324   OMP_CLAUSE_DEFAULT_KIND (c) = kind;
8325
8326   return c;
8327 }
8328
8329 /* OpenMP 2.5:
8330    firstprivate ( variable-list ) */
8331
8332 static tree
8333 c_parser_omp_clause_firstprivate (c_parser *parser, tree list)
8334 {
8335   return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_FIRSTPRIVATE, list);
8336 }
8337
8338 /* OpenMP 2.5:
8339    if ( expression ) */
8340
8341 static tree
8342 c_parser_omp_clause_if (c_parser *parser, tree list)
8343 {
8344   location_t loc = c_parser_peek_token (parser)->location;
8345   if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
8346     {
8347       tree t = c_parser_paren_condition (parser);
8348       tree c;
8349
8350       check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if");
8351
8352       c = build_omp_clause (loc, OMP_CLAUSE_IF);
8353       OMP_CLAUSE_IF_EXPR (c) = t;
8354       OMP_CLAUSE_CHAIN (c) = list;
8355       list = c;
8356     }
8357   else
8358     c_parser_error (parser, "expected %<(%>");
8359
8360   return list;
8361 }
8362
8363 /* OpenMP 2.5:
8364    lastprivate ( variable-list ) */
8365
8366 static tree
8367 c_parser_omp_clause_lastprivate (c_parser *parser, tree list)
8368 {
8369   return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_LASTPRIVATE, list);
8370 }
8371
8372 /* OpenMP 2.5:
8373    nowait */
8374
8375 static tree
8376 c_parser_omp_clause_nowait (c_parser *parser ATTRIBUTE_UNUSED, tree list)
8377 {
8378   tree c;
8379   location_t loc = c_parser_peek_token (parser)->location;
8380
8381   check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait");
8382
8383   c = build_omp_clause (loc, OMP_CLAUSE_NOWAIT);
8384   OMP_CLAUSE_CHAIN (c) = list;
8385   return c;
8386 }
8387
8388 /* OpenMP 2.5:
8389    num_threads ( expression ) */
8390
8391 static tree
8392 c_parser_omp_clause_num_threads (c_parser *parser, tree list)
8393 {
8394   location_t num_threads_loc = c_parser_peek_token (parser)->location;
8395   if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
8396     {
8397       location_t expr_loc = c_parser_peek_token (parser)->location;
8398       tree c, t = c_parser_expression (parser).value;
8399       t = c_fully_fold (t, false, NULL);
8400
8401       c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
8402
8403       if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
8404         {
8405           c_parser_error (parser, "expected integer expression");
8406           return list;
8407         }
8408
8409       /* Attempt to statically determine when the number isn't positive.  */
8410       c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
8411                        build_int_cst (TREE_TYPE (t), 0));
8412       if (CAN_HAVE_LOCATION_P (c))
8413         SET_EXPR_LOCATION (c, expr_loc);
8414       if (c == boolean_true_node)
8415         {
8416           warning_at (expr_loc, 0,
8417                       "%<num_threads%> value must be positive");
8418           t = integer_one_node;
8419         }
8420
8421       check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS, "num_threads");
8422
8423       c = build_omp_clause (num_threads_loc, OMP_CLAUSE_NUM_THREADS);
8424       OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
8425       OMP_CLAUSE_CHAIN (c) = list;
8426       list = c;
8427     }
8428
8429   return list;
8430 }
8431
8432 /* OpenMP 2.5:
8433    ordered */
8434
8435 static tree
8436 c_parser_omp_clause_ordered (c_parser *parser, tree list)
8437 {
8438   tree c;
8439
8440   check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED, "ordered");
8441
8442   c = build_omp_clause (c_parser_peek_token (parser)->location,
8443                         OMP_CLAUSE_ORDERED);
8444   OMP_CLAUSE_CHAIN (c) = list;
8445
8446   return c;
8447 }
8448
8449 /* OpenMP 2.5:
8450    private ( variable-list ) */
8451
8452 static tree
8453 c_parser_omp_clause_private (c_parser *parser, tree list)
8454 {
8455   return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_PRIVATE, list);
8456 }
8457
8458 /* OpenMP 2.5:
8459    reduction ( reduction-operator : variable-list )
8460
8461    reduction-operator:
8462      One of: + * - & ^ | && || */
8463
8464 static tree
8465 c_parser_omp_clause_reduction (c_parser *parser, tree list)
8466 {
8467   location_t clause_loc = c_parser_peek_token (parser)->location;
8468   if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
8469     {
8470       enum tree_code code;
8471
8472       switch (c_parser_peek_token (parser)->type)
8473         {
8474         case CPP_PLUS:
8475           code = PLUS_EXPR;
8476           break;
8477         case CPP_MULT:
8478           code = MULT_EXPR;
8479           break;
8480         case CPP_MINUS:
8481           code = MINUS_EXPR;
8482           break;
8483         case CPP_AND:
8484           code = BIT_AND_EXPR;
8485           break;
8486         case CPP_XOR:
8487           code = BIT_XOR_EXPR;
8488           break;
8489         case CPP_OR:
8490           code = BIT_IOR_EXPR;
8491           break;
8492         case CPP_AND_AND:
8493           code = TRUTH_ANDIF_EXPR;
8494           break;
8495         case CPP_OR_OR:
8496           code = TRUTH_ORIF_EXPR;
8497           break;
8498         default:
8499           c_parser_error (parser,
8500                           "expected %<+%>, %<*%>, %<-%>, %<&%>, "
8501                           "%<^%>, %<|%>, %<&&%>, or %<||%>");
8502           c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
8503           return list;
8504         }
8505       c_parser_consume_token (parser);
8506       if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
8507         {
8508           tree nl, c;
8509
8510           nl = c_parser_omp_variable_list (parser, clause_loc,
8511                                            OMP_CLAUSE_REDUCTION, list);
8512           for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
8513             OMP_CLAUSE_REDUCTION_CODE (c) = code;
8514
8515           list = nl;
8516         }
8517       c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
8518     }
8519   return list;
8520 }
8521
8522 /* OpenMP 2.5:
8523    schedule ( schedule-kind )
8524    schedule ( schedule-kind , expression )
8525
8526    schedule-kind:
8527      static | dynamic | guided | runtime | auto
8528 */
8529
8530 static tree
8531 c_parser_omp_clause_schedule (c_parser *parser, tree list)
8532 {
8533   tree c, t;
8534   location_t loc = c_parser_peek_token (parser)->location;
8535
8536   if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
8537     return list;
8538
8539   c = build_omp_clause (loc, OMP_CLAUSE_SCHEDULE);
8540
8541   if (c_parser_next_token_is (parser, CPP_NAME))
8542     {
8543       tree kind = c_parser_peek_token (parser)->value;
8544       const char *p = IDENTIFIER_POINTER (kind);
8545
8546       switch (p[0])
8547         {
8548         case 'd':
8549           if (strcmp ("dynamic", p) != 0)
8550             goto invalid_kind;
8551           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
8552           break;
8553
8554         case 'g':
8555           if (strcmp ("guided", p) != 0)
8556             goto invalid_kind;
8557           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
8558           break;
8559
8560         case 'r':
8561           if (strcmp ("runtime", p) != 0)
8562             goto invalid_kind;
8563           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
8564           break;
8565
8566         default:
8567           goto invalid_kind;
8568         }
8569     }
8570   else if (c_parser_next_token_is_keyword (parser, RID_STATIC))
8571     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
8572   else if (c_parser_next_token_is_keyword (parser, RID_AUTO))
8573     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
8574   else
8575     goto invalid_kind;
8576
8577   c_parser_consume_token (parser);
8578   if (c_parser_next_token_is (parser, CPP_COMMA))
8579     {
8580       location_t here;
8581       c_parser_consume_token (parser);
8582
8583       here = c_parser_peek_token (parser)->location;
8584       t = c_parser_expr_no_commas (parser, NULL).value;
8585       t = c_fully_fold (t, false, NULL);
8586
8587       if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
8588         error_at (here, "schedule %<runtime%> does not take "
8589                   "a %<chunk_size%> parameter");
8590       else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
8591         error_at (here,
8592                   "schedule %<auto%> does not take "
8593                   "a %<chunk_size%> parameter");
8594       else if (TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE)
8595         OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
8596       else
8597         c_parser_error (parser, "expected integer expression");
8598
8599       c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
8600     }
8601   else
8602     c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
8603                                "expected %<,%> or %<)%>");
8604
8605   check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
8606   OMP_CLAUSE_CHAIN (c) = list;
8607   return c;
8608
8609  invalid_kind:
8610   c_parser_error (parser, "invalid schedule kind");
8611   c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
8612   return list;
8613 }
8614
8615 /* OpenMP 2.5:
8616    shared ( variable-list ) */
8617
8618 static tree
8619 c_parser_omp_clause_shared (c_parser *parser, tree list)
8620 {
8621   return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_SHARED, list);
8622 }
8623
8624 /* OpenMP 3.0:
8625    untied */
8626
8627 static tree
8628 c_parser_omp_clause_untied (c_parser *parser ATTRIBUTE_UNUSED, tree list)
8629 {
8630   tree c;
8631
8632   /* FIXME: Should we allow duplicates?  */
8633   check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied");
8634
8635   c = build_omp_clause (c_parser_peek_token (parser)->location,
8636                         OMP_CLAUSE_UNTIED);
8637   OMP_CLAUSE_CHAIN (c) = list;
8638
8639   return c;
8640 }
8641
8642 /* Parse all OpenMP clauses.  The set clauses allowed by the directive
8643    is a bitmask in MASK.  Return the list of clauses found; the result
8644    of clause default goes in *pdefault.  */
8645
8646 static tree
8647 c_parser_omp_all_clauses (c_parser *parser, unsigned int mask,
8648                           const char *where)
8649 {
8650   tree clauses = NULL;
8651   bool first = true;
8652
8653   while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
8654     {
8655       location_t here;
8656       pragma_omp_clause c_kind;
8657       const char *c_name;
8658       tree prev = clauses;
8659
8660       if (!first && c_parser_next_token_is (parser, CPP_COMMA))
8661         c_parser_consume_token (parser);
8662
8663       first = false;
8664       here = c_parser_peek_token (parser)->location;
8665       c_kind = c_parser_omp_clause_name (parser);
8666
8667       switch (c_kind)
8668         {
8669         case PRAGMA_OMP_CLAUSE_COLLAPSE:
8670           clauses = c_parser_omp_clause_collapse (parser, clauses);
8671           c_name = "collapse";
8672           break;
8673         case PRAGMA_OMP_CLAUSE_COPYIN:
8674           clauses = c_parser_omp_clause_copyin (parser, clauses);
8675           c_name = "copyin";
8676           break;
8677         case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
8678           clauses = c_parser_omp_clause_copyprivate (parser, clauses);
8679           c_name = "copyprivate";
8680           break;
8681         case PRAGMA_OMP_CLAUSE_DEFAULT:
8682           clauses = c_parser_omp_clause_default (parser, clauses);
8683           c_name = "default";
8684           break;
8685         case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
8686           clauses = c_parser_omp_clause_firstprivate (parser, clauses);
8687           c_name = "firstprivate";
8688           break;
8689         case PRAGMA_OMP_CLAUSE_IF:
8690           clauses = c_parser_omp_clause_if (parser, clauses);
8691           c_name = "if";
8692           break;
8693         case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
8694           clauses = c_parser_omp_clause_lastprivate (parser, clauses);
8695           c_name = "lastprivate";
8696           break;
8697         case PRAGMA_OMP_CLAUSE_NOWAIT:
8698           clauses = c_parser_omp_clause_nowait (parser, clauses);
8699           c_name = "nowait";
8700           break;
8701         case PRAGMA_OMP_CLAUSE_NUM_THREADS:
8702           clauses = c_parser_omp_clause_num_threads (parser, clauses);
8703           c_name = "num_threads";
8704           break;
8705         case PRAGMA_OMP_CLAUSE_ORDERED:
8706           clauses = c_parser_omp_clause_ordered (parser, clauses);
8707           c_name = "ordered";
8708           break;
8709         case PRAGMA_OMP_CLAUSE_PRIVATE:
8710           clauses = c_parser_omp_clause_private (parser, clauses);
8711           c_name = "private";
8712           break;
8713         case PRAGMA_OMP_CLAUSE_REDUCTION:
8714           clauses = c_parser_omp_clause_reduction (parser, clauses);
8715           c_name = "reduction";
8716           break;
8717         case PRAGMA_OMP_CLAUSE_SCHEDULE:
8718           clauses = c_parser_omp_clause_schedule (parser, clauses);
8719           c_name = "schedule";
8720           break;
8721         case PRAGMA_OMP_CLAUSE_SHARED:
8722           clauses = c_parser_omp_clause_shared (parser, clauses);
8723           c_name = "shared";
8724           break;
8725         case PRAGMA_OMP_CLAUSE_UNTIED:
8726           clauses = c_parser_omp_clause_untied (parser, clauses);
8727           c_name = "untied";
8728           break;
8729         default:
8730           c_parser_error (parser, "expected %<#pragma omp%> clause");
8731           goto saw_error;
8732         }
8733
8734       if (((mask >> c_kind) & 1) == 0 && !parser->error)
8735         {
8736           /* Remove the invalid clause(s) from the list to avoid
8737              confusing the rest of the compiler.  */
8738           clauses = prev;
8739           error_at (here, "%qs is not valid for %qs", c_name, where);
8740         }
8741     }
8742
8743  saw_error:
8744   c_parser_skip_to_pragma_eol (parser);
8745
8746   return c_finish_omp_clauses (clauses);
8747 }
8748
8749 /* OpenMP 2.5:
8750    structured-block:
8751      statement
8752
8753    In practice, we're also interested in adding the statement to an
8754    outer node.  So it is convenient if we work around the fact that
8755    c_parser_statement calls add_stmt.  */
8756
8757 static tree
8758 c_parser_omp_structured_block (c_parser *parser)
8759 {
8760   tree stmt = push_stmt_list ();
8761   c_parser_statement (parser);
8762   return pop_stmt_list (stmt);
8763 }
8764
8765 /* OpenMP 2.5:
8766    # pragma omp atomic new-line
8767      expression-stmt
8768
8769    expression-stmt:
8770      x binop= expr | x++ | ++x | x-- | --x
8771    binop:
8772      +, *, -, /, &, ^, |, <<, >>
8773
8774   where x is an lvalue expression with scalar type.
8775
8776   LOC is the location of the #pragma token.  */
8777
8778 static void
8779 c_parser_omp_atomic (location_t loc, c_parser *parser)
8780 {
8781   tree lhs, rhs;
8782   tree stmt;
8783   enum tree_code code;
8784   struct c_expr rhs_expr;
8785
8786   c_parser_skip_to_pragma_eol (parser);
8787
8788   lhs = c_parser_unary_expression (parser).value;
8789   lhs = c_fully_fold (lhs, false, NULL);
8790   switch (TREE_CODE (lhs))
8791     {
8792     case ERROR_MARK:
8793     saw_error:
8794       c_parser_skip_to_end_of_block_or_statement (parser);
8795       return;
8796
8797     case PREINCREMENT_EXPR:
8798     case POSTINCREMENT_EXPR:
8799       lhs = TREE_OPERAND (lhs, 0);
8800       code = PLUS_EXPR;
8801       rhs = integer_one_node;
8802       break;
8803
8804     case PREDECREMENT_EXPR:
8805     case POSTDECREMENT_EXPR:
8806       lhs = TREE_OPERAND (lhs, 0);
8807       code = MINUS_EXPR;
8808       rhs = integer_one_node;
8809       break;
8810
8811     case COMPOUND_EXPR:
8812       if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
8813           && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
8814           && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
8815           && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
8816           && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
8817                                               (TREE_OPERAND (lhs, 1), 0), 0)))
8818              == BOOLEAN_TYPE)
8819         /* Undo effects of boolean_increment for post {in,de}crement.  */
8820         lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
8821       /* FALLTHRU */
8822     case MODIFY_EXPR:
8823       if (TREE_CODE (lhs) == MODIFY_EXPR
8824           && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
8825         {
8826           /* Undo effects of boolean_increment.  */
8827           if (integer_onep (TREE_OPERAND (lhs, 1)))
8828             {
8829               /* This is pre or post increment.  */
8830               rhs = TREE_OPERAND (lhs, 1);
8831               lhs = TREE_OPERAND (lhs, 0);
8832               code = NOP_EXPR;
8833               break;
8834             }
8835           if (TREE_CODE (TREE_OPERAND (lhs, 1)) == TRUTH_NOT_EXPR
8836               && TREE_OPERAND (lhs, 0)
8837                  == TREE_OPERAND (TREE_OPERAND (lhs, 1), 0))
8838             {
8839               /* This is pre or post decrement.  */
8840               rhs = TREE_OPERAND (lhs, 1);
8841               lhs = TREE_OPERAND (lhs, 0);
8842               code = NOP_EXPR;
8843               break;
8844             }
8845         }
8846       /* FALLTHRU */
8847     default:
8848       switch (c_parser_peek_token (parser)->type)
8849         {
8850         case CPP_MULT_EQ:
8851           code = MULT_EXPR;
8852           break;
8853         case CPP_DIV_EQ:
8854           code = TRUNC_DIV_EXPR;
8855           break;
8856         case CPP_PLUS_EQ:
8857           code = PLUS_EXPR;
8858           break;
8859         case CPP_MINUS_EQ:
8860           code = MINUS_EXPR;
8861           break;
8862         case CPP_LSHIFT_EQ:
8863           code = LSHIFT_EXPR;
8864           break;
8865         case CPP_RSHIFT_EQ:
8866           code = RSHIFT_EXPR;
8867           break;
8868         case CPP_AND_EQ:
8869           code = BIT_AND_EXPR;
8870           break;
8871         case CPP_OR_EQ:
8872           code = BIT_IOR_EXPR;
8873           break;
8874         case CPP_XOR_EQ:
8875           code = BIT_XOR_EXPR;
8876           break;
8877         default:
8878           c_parser_error (parser,
8879                           "invalid operator for %<#pragma omp atomic%>");
8880           goto saw_error;
8881         }
8882
8883       c_parser_consume_token (parser);
8884       {
8885         location_t rhs_loc = c_parser_peek_token (parser)->location;
8886         rhs_expr = c_parser_expression (parser);
8887         rhs_expr = default_function_array_read_conversion (rhs_loc, rhs_expr);
8888       }
8889       rhs = rhs_expr.value;
8890       rhs = c_fully_fold (rhs, false, NULL);
8891       break;
8892     }
8893   stmt = c_finish_omp_atomic (loc, code, lhs, rhs);
8894   if (stmt != error_mark_node)
8895     add_stmt (stmt);
8896   c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
8897 }
8898
8899
8900 /* OpenMP 2.5:
8901    # pragma omp barrier new-line
8902 */
8903
8904 static void
8905 c_parser_omp_barrier (c_parser *parser)
8906 {
8907   location_t loc = c_parser_peek_token (parser)->location;
8908   c_parser_consume_pragma (parser);
8909   c_parser_skip_to_pragma_eol (parser);
8910
8911   c_finish_omp_barrier (loc);
8912 }
8913
8914 /* OpenMP 2.5:
8915    # pragma omp critical [(name)] new-line
8916      structured-block
8917
8918   LOC is the location of the #pragma itself.  */
8919
8920 static tree
8921 c_parser_omp_critical (location_t loc, c_parser *parser)
8922 {
8923   tree stmt, name = NULL;
8924
8925   if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
8926     {
8927       c_parser_consume_token (parser);
8928       if (c_parser_next_token_is (parser, CPP_NAME))
8929         {
8930           name = c_parser_peek_token (parser)->value;
8931           c_parser_consume_token (parser);
8932           c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
8933         }
8934       else
8935         c_parser_error (parser, "expected identifier");
8936     }
8937   else if (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
8938     c_parser_error (parser, "expected %<(%> or end of line");
8939   c_parser_skip_to_pragma_eol (parser);
8940
8941   stmt = c_parser_omp_structured_block (parser);
8942   return c_finish_omp_critical (loc, stmt, name);
8943 }
8944
8945 /* OpenMP 2.5:
8946    # pragma omp flush flush-vars[opt] new-line
8947
8948    flush-vars:
8949      ( variable-list ) */
8950
8951 static void
8952 c_parser_omp_flush (c_parser *parser)
8953 {
8954   location_t loc = c_parser_peek_token (parser)->location;
8955   c_parser_consume_pragma (parser);
8956   if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
8957     c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
8958   else if (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
8959     c_parser_error (parser, "expected %<(%> or end of line");
8960   c_parser_skip_to_pragma_eol (parser);
8961
8962   c_finish_omp_flush (loc);
8963 }
8964
8965 /* Parse the restricted form of the for statement allowed by OpenMP.
8966    The real trick here is to determine the loop control variable early
8967    so that we can push a new decl if necessary to make it private.
8968    LOC is the location of the OMP in "#pragma omp".  */
8969
8970 static tree
8971 c_parser_omp_for_loop (location_t loc,
8972                        c_parser *parser, tree clauses, tree *par_clauses)
8973 {
8974   tree decl, cond, incr, save_break, save_cont, body, init, stmt, cl;
8975   tree declv, condv, incrv, initv, ret = NULL;
8976   bool fail = false, open_brace_parsed = false;
8977   int i, collapse = 1, nbraces = 0;
8978   location_t for_loc;
8979   VEC(tree,gc) *for_block = make_tree_vector ();
8980
8981   for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
8982     if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
8983       collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
8984
8985   gcc_assert (collapse >= 1);
8986
8987   declv = make_tree_vec (collapse);
8988   initv = make_tree_vec (collapse);
8989   condv = make_tree_vec (collapse);
8990   incrv = make_tree_vec (collapse);
8991
8992   if (!c_parser_next_token_is_keyword (parser, RID_FOR))
8993     {
8994       c_parser_error (parser, "for statement expected");
8995       return NULL;
8996     }
8997   for_loc = c_parser_peek_token (parser)->location;
8998   c_parser_consume_token (parser);
8999
9000   for (i = 0; i < collapse; i++)
9001     {
9002       int bracecount = 0;
9003
9004       if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
9005         goto pop_scopes;
9006
9007       /* Parse the initialization declaration or expression.  */
9008       if (c_parser_next_token_starts_declaration (parser))
9009         {
9010           if (i > 0)
9011             VEC_safe_push (tree, gc, for_block, c_begin_compound_stmt (true));
9012           c_parser_declaration_or_fndef (parser, true, true, true, true, true, NULL);
9013           decl = check_for_loop_decls (for_loc, flag_isoc99);
9014           if (decl == NULL)
9015             goto error_init;
9016           if (DECL_INITIAL (decl) == error_mark_node)
9017             decl = error_mark_node;
9018           init = decl;
9019         }
9020       else if (c_parser_next_token_is (parser, CPP_NAME)
9021                && c_parser_peek_2nd_token (parser)->type == CPP_EQ)
9022         {
9023           struct c_expr decl_exp;
9024           struct c_expr init_exp;
9025           location_t init_loc;
9026
9027           decl_exp = c_parser_postfix_expression (parser);
9028           decl = decl_exp.value;
9029
9030           c_parser_require (parser, CPP_EQ, "expected %<=%>");
9031
9032           init_loc = c_parser_peek_token (parser)->location;
9033           init_exp = c_parser_expr_no_commas (parser, NULL);
9034           init_exp = default_function_array_read_conversion (init_loc,
9035                                                              init_exp);
9036           init = build_modify_expr (init_loc, decl, decl_exp.original_type,
9037                                     NOP_EXPR, init_loc, init_exp.value,
9038                                     init_exp.original_type);
9039           init = c_process_expr_stmt (init_loc, init);
9040
9041           c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9042         }
9043       else
9044         {
9045         error_init:
9046           c_parser_error (parser,
9047                           "expected iteration declaration or initialization");
9048           c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
9049                                      "expected %<)%>");
9050           fail = true;
9051           goto parse_next;
9052         }
9053
9054       /* Parse the loop condition.  */
9055       cond = NULL_TREE;
9056       if (c_parser_next_token_is_not (parser, CPP_SEMICOLON))
9057         {
9058           location_t cond_loc = c_parser_peek_token (parser)->location;
9059           struct c_expr cond_expr = c_parser_binary_expression (parser, NULL);
9060
9061           cond = cond_expr.value;
9062           cond = c_objc_common_truthvalue_conversion (cond_loc, cond);
9063           cond = c_fully_fold (cond, false, NULL);
9064           switch (cond_expr.original_code)
9065             {
9066             case GT_EXPR:
9067             case GE_EXPR:
9068             case LT_EXPR:
9069             case LE_EXPR:
9070               break;
9071             default:
9072               /* Can't be cond = error_mark_node, because we want to preserve
9073                  the location until c_finish_omp_for.  */
9074               cond = build1 (NOP_EXPR, boolean_type_node, error_mark_node);
9075               break;
9076             }
9077           protected_set_expr_location (cond, cond_loc);
9078         }
9079       c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9080
9081       /* Parse the increment expression.  */
9082       incr = NULL_TREE;
9083       if (c_parser_next_token_is_not (parser, CPP_CLOSE_PAREN))
9084         {
9085           location_t incr_loc = c_parser_peek_token (parser)->location;
9086
9087           incr = c_process_expr_stmt (incr_loc,
9088                                       c_parser_expression (parser).value);
9089         }
9090       c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
9091
9092       if (decl == NULL || decl == error_mark_node || init == error_mark_node)
9093         fail = true;
9094       else
9095         {
9096           TREE_VEC_ELT (declv, i) = decl;
9097           TREE_VEC_ELT (initv, i) = init;
9098           TREE_VEC_ELT (condv, i) = cond;
9099           TREE_VEC_ELT (incrv, i) = incr;
9100         }
9101
9102     parse_next:
9103       if (i == collapse - 1)
9104         break;
9105
9106       /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
9107          in between the collapsed for loops to be still considered perfectly
9108          nested.  Hopefully the final version clarifies this.
9109          For now handle (multiple) {'s and empty statements.  */
9110       do
9111         {
9112           if (c_parser_next_token_is_keyword (parser, RID_FOR))
9113             {
9114               c_parser_consume_token (parser);
9115               break;
9116             }
9117           else if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
9118             {
9119               c_parser_consume_token (parser);
9120               bracecount++;
9121             }
9122           else if (bracecount
9123                    && c_parser_next_token_is (parser, CPP_SEMICOLON))
9124             c_parser_consume_token (parser);
9125           else
9126             {
9127               c_parser_error (parser, "not enough perfectly nested loops");
9128               if (bracecount)
9129                 {
9130                   open_brace_parsed = true;
9131                   bracecount--;
9132                 }
9133               fail = true;
9134               collapse = 0;
9135               break;
9136             }
9137         }
9138       while (1);
9139
9140       nbraces += bracecount;
9141     }
9142
9143   save_break = c_break_label;
9144   c_break_label = size_one_node;
9145   save_cont = c_cont_label;
9146   c_cont_label = NULL_TREE;
9147   body = push_stmt_list ();
9148
9149   if (open_brace_parsed)
9150     {
9151       location_t here = c_parser_peek_token (parser)->location;
9152       stmt = c_begin_compound_stmt (true);
9153       c_parser_compound_statement_nostart (parser);
9154       add_stmt (c_end_compound_stmt (here, stmt, true));
9155     }
9156   else
9157     add_stmt (c_parser_c99_block_statement (parser));
9158   if (c_cont_label)
9159     {
9160       tree t = build1 (LABEL_EXPR, void_type_node, c_cont_label);
9161       SET_EXPR_LOCATION (t, loc);
9162       add_stmt (t);
9163     }
9164
9165   body = pop_stmt_list (body);
9166   c_break_label = save_break;
9167   c_cont_label = save_cont;
9168
9169   while (nbraces)
9170     {
9171       if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
9172         {
9173           c_parser_consume_token (parser);
9174           nbraces--;
9175         }
9176       else if (c_parser_next_token_is (parser, CPP_SEMICOLON))
9177         c_parser_consume_token (parser);
9178       else
9179         {
9180           c_parser_error (parser, "collapsed loops not perfectly nested");
9181           while (nbraces)
9182             {
9183               location_t here = c_parser_peek_token (parser)->location;
9184               stmt = c_begin_compound_stmt (true);
9185               add_stmt (body);
9186               c_parser_compound_statement_nostart (parser);
9187               body = c_end_compound_stmt (here, stmt, true);
9188               nbraces--;
9189             }
9190           goto pop_scopes;
9191         }
9192     }
9193
9194   /* Only bother calling c_finish_omp_for if we haven't already generated
9195      an error from the initialization parsing.  */
9196   if (!fail)
9197     {
9198       stmt = c_finish_omp_for (loc, declv, initv, condv, incrv, body, NULL);
9199       if (stmt)
9200         {
9201           if (par_clauses != NULL)
9202             {
9203               tree *c;
9204               for (c = par_clauses; *c ; )
9205                 if (OMP_CLAUSE_CODE (*c) != OMP_CLAUSE_FIRSTPRIVATE
9206                     && OMP_CLAUSE_CODE (*c) != OMP_CLAUSE_LASTPRIVATE)
9207                   c = &OMP_CLAUSE_CHAIN (*c);
9208                 else
9209                   {
9210                     for (i = 0; i < collapse; i++)
9211                       if (TREE_VEC_ELT (declv, i) == OMP_CLAUSE_DECL (*c))
9212                         break;
9213                     if (i == collapse)
9214                       c = &OMP_CLAUSE_CHAIN (*c);
9215                     else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE)
9216                       {
9217                         error_at (loc,
9218                                   "iteration variable %qD should not be firstprivate",
9219                                   OMP_CLAUSE_DECL (*c));
9220                         *c = OMP_CLAUSE_CHAIN (*c);
9221                       }
9222                     else
9223                       {
9224                         /* Copy lastprivate (decl) clause to OMP_FOR_CLAUSES,
9225                            change it to shared (decl) in
9226                            OMP_PARALLEL_CLAUSES.  */
9227                         tree l = build_omp_clause (OMP_CLAUSE_LOCATION (*c),
9228                                                    OMP_CLAUSE_LASTPRIVATE);
9229                         OMP_CLAUSE_DECL (l) = OMP_CLAUSE_DECL (*c);
9230                         OMP_CLAUSE_CHAIN (l) = clauses;
9231                         clauses = l;
9232                         OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
9233                       }
9234                   }
9235             }
9236           OMP_FOR_CLAUSES (stmt) = clauses;
9237         }
9238       ret = stmt;
9239     }
9240 pop_scopes:
9241   while (!VEC_empty (tree, for_block))
9242     {
9243       /* FIXME diagnostics: LOC below should be the actual location of
9244          this particular for block.  We need to build a list of
9245          locations to go along with FOR_BLOCK.  */
9246       stmt = c_end_compound_stmt (loc, VEC_pop (tree, for_block), true);
9247       add_stmt (stmt);
9248     }
9249   release_tree_vector (for_block);
9250   return ret;
9251 }
9252
9253 /* OpenMP 2.5:
9254    #pragma omp for for-clause[optseq] new-line
9255      for-loop
9256
9257    LOC is the location of the #pragma token.
9258 */
9259
9260 #define OMP_FOR_CLAUSE_MASK                             \
9261         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
9262         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
9263         | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)         \
9264         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
9265         | (1u << PRAGMA_OMP_CLAUSE_ORDERED)             \
9266         | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE)            \
9267         | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE)            \
9268         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
9269
9270 static tree
9271 c_parser_omp_for (location_t loc, c_parser *parser)
9272 {
9273   tree block, clauses, ret;
9274
9275   clauses = c_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
9276                                       "#pragma omp for");
9277
9278   block = c_begin_compound_stmt (true);
9279   ret = c_parser_omp_for_loop (loc, parser, clauses, NULL);
9280   block = c_end_compound_stmt (loc, block, true);
9281   add_stmt (block);
9282
9283   return ret;
9284 }
9285
9286 /* OpenMP 2.5:
9287    # pragma omp master new-line
9288      structured-block
9289
9290    LOC is the location of the #pragma token.
9291 */
9292
9293 static tree
9294 c_parser_omp_master (location_t loc, c_parser *parser)
9295 {
9296   c_parser_skip_to_pragma_eol (parser);
9297   return c_finish_omp_master (loc, c_parser_omp_structured_block (parser));
9298 }
9299
9300 /* OpenMP 2.5:
9301    # pragma omp ordered new-line
9302      structured-block
9303
9304    LOC is the location of the #pragma itself.
9305 */
9306
9307 static tree
9308 c_parser_omp_ordered (location_t loc, c_parser *parser)
9309 {
9310   c_parser_skip_to_pragma_eol (parser);
9311   return c_finish_omp_ordered (loc, c_parser_omp_structured_block (parser));
9312 }
9313
9314 /* OpenMP 2.5:
9315
9316    section-scope:
9317      { section-sequence }
9318
9319    section-sequence:
9320      section-directive[opt] structured-block
9321      section-sequence section-directive structured-block
9322
9323     SECTIONS_LOC is the location of the #pragma omp sections.  */
9324
9325 static tree
9326 c_parser_omp_sections_scope (location_t sections_loc, c_parser *parser)
9327 {
9328   tree stmt, substmt;
9329   bool error_suppress = false;
9330   location_t loc;
9331
9332   loc = c_parser_peek_token (parser)->location;
9333   if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
9334     {
9335       /* Avoid skipping until the end of the block.  */
9336       parser->error = false;
9337       return NULL_TREE;
9338     }
9339
9340   stmt = push_stmt_list ();
9341
9342   if (c_parser_peek_token (parser)->pragma_kind != PRAGMA_OMP_SECTION)
9343     {
9344       substmt = push_stmt_list ();
9345
9346       while (1)
9347         {
9348           c_parser_statement (parser);
9349
9350           if (c_parser_peek_token (parser)->pragma_kind == PRAGMA_OMP_SECTION)
9351             break;
9352           if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
9353             break;
9354           if (c_parser_next_token_is (parser, CPP_EOF))
9355             break;
9356         }
9357
9358       substmt = pop_stmt_list (substmt);
9359       substmt = build1 (OMP_SECTION, void_type_node, substmt);
9360       SET_EXPR_LOCATION (substmt, loc);
9361       add_stmt (substmt);
9362     }
9363
9364   while (1)
9365     {
9366       if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
9367         break;
9368       if (c_parser_next_token_is (parser, CPP_EOF))
9369         break;
9370
9371       loc = c_parser_peek_token (parser)->location;
9372       if (c_parser_peek_token (parser)->pragma_kind == PRAGMA_OMP_SECTION)
9373         {
9374           c_parser_consume_pragma (parser);
9375           c_parser_skip_to_pragma_eol (parser);
9376           error_suppress = false;
9377         }
9378       else if (!error_suppress)
9379         {
9380           error_at (loc, "expected %<#pragma omp section%> or %<}%>");
9381           error_suppress = true;
9382         }
9383
9384       substmt = c_parser_omp_structured_block (parser);
9385       substmt = build1 (OMP_SECTION, void_type_node, substmt);
9386       SET_EXPR_LOCATION (substmt, loc);
9387       add_stmt (substmt);
9388     }
9389   c_parser_skip_until_found (parser, CPP_CLOSE_BRACE,
9390                              "expected %<#pragma omp section%> or %<}%>");
9391
9392   substmt = pop_stmt_list (stmt);
9393
9394   stmt = make_node (OMP_SECTIONS);
9395   SET_EXPR_LOCATION (stmt, sections_loc);
9396   TREE_TYPE (stmt) = void_type_node;
9397   OMP_SECTIONS_BODY (stmt) = substmt;
9398
9399   return add_stmt (stmt);
9400 }
9401
9402 /* OpenMP 2.5:
9403    # pragma omp sections sections-clause[optseq] newline
9404      sections-scope
9405
9406    LOC is the location of the #pragma token.
9407 */
9408
9409 #define OMP_SECTIONS_CLAUSE_MASK                        \
9410         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
9411         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
9412         | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)         \
9413         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
9414         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
9415
9416 static tree
9417 c_parser_omp_sections (location_t loc, c_parser *parser)
9418 {
9419   tree block, clauses, ret;
9420
9421   clauses = c_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
9422                                       "#pragma omp sections");
9423
9424   block = c_begin_compound_stmt (true);
9425   ret = c_parser_omp_sections_scope (loc, parser);
9426   if (ret)
9427     OMP_SECTIONS_CLAUSES (ret) = clauses;
9428   block = c_end_compound_stmt (loc, block, true);
9429   add_stmt (block);
9430
9431   return ret;
9432 }
9433
9434 /* OpenMP 2.5:
9435    # pragma parallel parallel-clause new-line
9436    # pragma parallel for parallel-for-clause new-line
9437    # pragma parallel sections parallel-sections-clause new-line
9438
9439    LOC is the location of the #pragma token.
9440 */
9441
9442 #define OMP_PARALLEL_CLAUSE_MASK                        \
9443         ( (1u << PRAGMA_OMP_CLAUSE_IF)                  \
9444         | (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
9445         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
9446         | (1u << PRAGMA_OMP_CLAUSE_DEFAULT)             \
9447         | (1u << PRAGMA_OMP_CLAUSE_SHARED)              \
9448         | (1u << PRAGMA_OMP_CLAUSE_COPYIN)              \
9449         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
9450         | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
9451
9452 static tree
9453 c_parser_omp_parallel (location_t loc, c_parser *parser)
9454 {
9455   enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
9456   const char *p_name = "#pragma omp parallel";
9457   tree stmt, clauses, par_clause, ws_clause, block;
9458   unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
9459
9460   if (c_parser_next_token_is_keyword (parser, RID_FOR))
9461     {
9462       c_parser_consume_token (parser);
9463       p_kind = PRAGMA_OMP_PARALLEL_FOR;
9464       p_name = "#pragma omp parallel for";
9465       mask |= OMP_FOR_CLAUSE_MASK;
9466       mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
9467     }
9468   else if (c_parser_next_token_is (parser, CPP_NAME))
9469     {
9470       const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
9471       if (strcmp (p, "sections") == 0)
9472         {
9473           c_parser_consume_token (parser);
9474           p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
9475           p_name = "#pragma omp parallel sections";
9476           mask |= OMP_SECTIONS_CLAUSE_MASK;
9477           mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
9478         }
9479     }
9480
9481   clauses = c_parser_omp_all_clauses (parser, mask, p_name);
9482
9483   switch (p_kind)
9484     {
9485     case PRAGMA_OMP_PARALLEL:
9486       block = c_begin_omp_parallel ();
9487       c_parser_statement (parser);
9488       stmt = c_finish_omp_parallel (loc, clauses, block);
9489       break;
9490
9491     case PRAGMA_OMP_PARALLEL_FOR:
9492       block = c_begin_omp_parallel ();
9493       c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
9494       c_parser_omp_for_loop (loc, parser, ws_clause, &par_clause);
9495       stmt = c_finish_omp_parallel (loc, par_clause, block);
9496       OMP_PARALLEL_COMBINED (stmt) = 1;
9497       break;
9498
9499     case PRAGMA_OMP_PARALLEL_SECTIONS:
9500       block = c_begin_omp_parallel ();
9501       c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
9502       stmt = c_parser_omp_sections_scope (loc, parser);
9503       if (stmt)
9504         OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
9505       stmt = c_finish_omp_parallel (loc, par_clause, block);
9506       OMP_PARALLEL_COMBINED (stmt) = 1;
9507       break;
9508
9509     default:
9510       gcc_unreachable ();
9511     }
9512
9513   return stmt;
9514 }
9515
9516 /* OpenMP 2.5:
9517    # pragma omp single single-clause[optseq] new-line
9518      structured-block
9519
9520    LOC is the location of the #pragma.
9521 */
9522
9523 #define OMP_SINGLE_CLAUSE_MASK                          \
9524         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
9525         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
9526         | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE)         \
9527         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
9528
9529 static tree
9530 c_parser_omp_single (location_t loc, c_parser *parser)
9531 {
9532   tree stmt = make_node (OMP_SINGLE);
9533   SET_EXPR_LOCATION (stmt, loc);
9534   TREE_TYPE (stmt) = void_type_node;
9535
9536   OMP_SINGLE_CLAUSES (stmt)
9537     = c_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
9538                                 "#pragma omp single");
9539   OMP_SINGLE_BODY (stmt) = c_parser_omp_structured_block (parser);
9540
9541   return add_stmt (stmt);
9542 }
9543
9544 /* OpenMP 3.0:
9545    # pragma omp task task-clause[optseq] new-line
9546
9547    LOC is the location of the #pragma.
9548 */
9549
9550 #define OMP_TASK_CLAUSE_MASK                            \
9551         ( (1u << PRAGMA_OMP_CLAUSE_IF)                  \
9552         | (1u << PRAGMA_OMP_CLAUSE_UNTIED)              \
9553         | (1u << PRAGMA_OMP_CLAUSE_DEFAULT)             \
9554         | (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
9555         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
9556         | (1u << PRAGMA_OMP_CLAUSE_SHARED))
9557
9558 static tree
9559 c_parser_omp_task (location_t loc, c_parser *parser)
9560 {
9561   tree clauses, block;
9562
9563   clauses = c_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
9564                                       "#pragma omp task");
9565
9566   block = c_begin_omp_task ();
9567   c_parser_statement (parser);
9568   return c_finish_omp_task (loc, clauses, block);
9569 }
9570
9571 /* OpenMP 3.0:
9572    # pragma omp taskwait new-line
9573 */
9574
9575 static void
9576 c_parser_omp_taskwait (c_parser *parser)
9577 {
9578   location_t loc = c_parser_peek_token (parser)->location;
9579   c_parser_consume_pragma (parser);
9580   c_parser_skip_to_pragma_eol (parser);
9581
9582   c_finish_omp_taskwait (loc);
9583 }
9584
9585 /* Main entry point to parsing most OpenMP pragmas.  */
9586
9587 static void
9588 c_parser_omp_construct (c_parser *parser)
9589 {
9590   enum pragma_kind p_kind;
9591   location_t loc;
9592   tree stmt;
9593
9594   loc = c_parser_peek_token (parser)->location;
9595   p_kind = c_parser_peek_token (parser)->pragma_kind;
9596   c_parser_consume_pragma (parser);
9597
9598   switch (p_kind)
9599     {
9600     case PRAGMA_OMP_ATOMIC:
9601       c_parser_omp_atomic (loc, parser);
9602       return;
9603     case PRAGMA_OMP_CRITICAL:
9604       stmt = c_parser_omp_critical (loc, parser);
9605       break;
9606     case PRAGMA_OMP_FOR:
9607       stmt = c_parser_omp_for (loc, parser);
9608       break;
9609     case PRAGMA_OMP_MASTER:
9610       stmt = c_parser_omp_master (loc, parser);
9611       break;
9612     case PRAGMA_OMP_ORDERED:
9613       stmt = c_parser_omp_ordered (loc, parser);
9614       break;
9615     case PRAGMA_OMP_PARALLEL:
9616       stmt = c_parser_omp_parallel (loc, parser);
9617       break;
9618     case PRAGMA_OMP_SECTIONS:
9619       stmt = c_parser_omp_sections (loc, parser);
9620       break;
9621     case PRAGMA_OMP_SINGLE:
9622       stmt = c_parser_omp_single (loc, parser);
9623       break;
9624     case PRAGMA_OMP_TASK:
9625       stmt = c_parser_omp_task (loc, parser);
9626       break;
9627     default:
9628       gcc_unreachable ();
9629     }
9630
9631   if (stmt)
9632     gcc_assert (EXPR_LOCATION (stmt) != UNKNOWN_LOCATION);
9633 }
9634
9635
9636 /* OpenMP 2.5:
9637    # pragma omp threadprivate (variable-list) */
9638
9639 static void
9640 c_parser_omp_threadprivate (c_parser *parser)
9641 {
9642   tree vars, t;
9643   location_t loc;
9644
9645   c_parser_consume_pragma (parser);
9646   loc = c_parser_peek_token (parser)->location;
9647   vars = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
9648
9649   /* Mark every variable in VARS to be assigned thread local storage.  */
9650   for (t = vars; t; t = TREE_CHAIN (t))
9651     {
9652       tree v = TREE_PURPOSE (t);
9653
9654       /* FIXME diagnostics: Ideally we should keep individual
9655          locations for all the variables in the var list to make the
9656          following errors more precise.  Perhaps
9657          c_parser_omp_var_list_parens() should construct a list of
9658          locations to go along with the var list.  */
9659
9660       /* If V had already been marked threadprivate, it doesn't matter
9661          whether it had been used prior to this point.  */
9662       if (TREE_CODE (v) != VAR_DECL)
9663         error_at (loc, "%qD is not a variable", v);
9664       else if (TREE_USED (v) && !C_DECL_THREADPRIVATE_P (v))
9665         error_at (loc, "%qE declared %<threadprivate%> after first use", v);
9666       else if (! TREE_STATIC (v) && ! DECL_EXTERNAL (v))
9667         error_at (loc, "automatic variable %qE cannot be %<threadprivate%>", v);
9668       else if (TREE_TYPE (v) == error_mark_node)
9669         ;
9670       else if (! COMPLETE_TYPE_P (TREE_TYPE (v)))
9671         error_at (loc, "%<threadprivate%> %qE has incomplete type", v);
9672       else
9673         {
9674           if (! DECL_THREAD_LOCAL_P (v))
9675             {
9676               DECL_TLS_MODEL (v) = decl_default_tls_model (v);
9677               /* If rtl has been already set for this var, call
9678                  make_decl_rtl once again, so that encode_section_info
9679                  has a chance to look at the new decl flags.  */
9680               if (DECL_RTL_SET_P (v))
9681                 make_decl_rtl (v);
9682             }
9683           C_DECL_THREADPRIVATE_P (v) = 1;
9684         }
9685     }
9686
9687   c_parser_skip_to_pragma_eol (parser);
9688 }
9689
9690 \f
9691 /* Parse a single source file.  */
9692
9693 void
9694 c_parse_file (void)
9695 {
9696   /* Use local storage to begin.  If the first token is a pragma, parse it.
9697      If it is #pragma GCC pch_preprocess, then this will load a PCH file
9698      which will cause garbage collection.  */
9699   c_parser tparser;
9700
9701   memset (&tparser, 0, sizeof tparser);
9702   the_parser = &tparser;
9703
9704   if (c_parser_peek_token (&tparser)->pragma_kind == PRAGMA_GCC_PCH_PREPROCESS)
9705     c_parser_pragma_pch_preprocess (&tparser);
9706
9707   the_parser = ggc_alloc_c_parser ();
9708   *the_parser = tparser;
9709
9710   /* Initialize EH, if we've been told to do so.  */
9711   if (flag_exceptions)
9712     using_eh_for_cleanups ();
9713
9714   c_parser_translation_unit (the_parser);
9715   the_parser = NULL;
9716 }
9717
9718 #include "gt-c-parser.h"