OSDN Git Service

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