OSDN Git Service

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