OSDN Git Service

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