OSDN Git Service

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