OSDN Git Service

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