OSDN Git Service

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