OSDN Git Service

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