OSDN Git Service

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