OSDN Git Service

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