OSDN Git Service

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