OSDN Git Service

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