OSDN Git Service

./:
[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 Free Software Foundation, Inc.
4
5    Parser actions based on the old Bison parser; structure somewhat
6    influenced by and fragments based on the C++ parser.
7
8 This file is part of GCC.
9
10 GCC is free software; you can redistribute it and/or modify it under
11 the terms of the GNU General Public License as published by the Free
12 Software Foundation; either version 2, or (at your option) any later
13 version.
14
15 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
16 WARRANTY; without even the implied warranty of MERCHANTABILITY or
17 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
18 for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with GCC; see the file COPYING.  If not, write to the Free
22 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
23 02110-1301, USA.  */
24
25 /* TODO:
26
27    Make sure all relevant comments, and all relevant code from all
28    actions, brought over from old parser.  Verify exact correspondence
29    of syntax accepted.
30
31    Add testcases covering every input symbol in every state in old and
32    new parsers.
33
34    Include full syntax for GNU C, including erroneous cases accepted
35    with error messages, in syntax productions in comments.
36
37    Make more diagnostics in the front end generally take an explicit
38    location rather than implicitly using input_location.  */
39
40 #include "config.h"
41 #include "system.h"
42 #include "coretypes.h"
43 #include "tm.h"
44 #include "tree.h"
45 #include "rtl.h"
46 #include "langhooks.h"
47 #include "input.h"
48 #include "cpplib.h"
49 #include "timevar.h"
50 #include "c-pragma.h"
51 #include "c-tree.h"
52 #include "flags.h"
53 #include "output.h"
54 #include "toplev.h"
55 #include "ggc.h"
56 #include "c-common.h"
57 #include "vec.h"
58 #include "target.h"
59 #include "cgraph.h"
60
61 \f
62 /* Miscellaneous data and functions needed for the parser.  */
63
64 int yydebug;
65
66 /* Objective-C specific parser/lexer information.  */
67
68 static int objc_pq_context = 0;
69
70 /* The following flag is needed to contextualize Objective-C lexical
71    analysis.  In some cases (e.g., 'int NSObject;'), it is undesirable
72    to bind an identifier to an Objective-C class, even if a class with
73    that name exists.  */
74 static int objc_need_raw_identifier = 0;
75 #define OBJC_NEED_RAW_IDENTIFIER(VAL)           \
76   do {                                          \
77     if (c_dialect_objc ())                      \
78       objc_need_raw_identifier = VAL;           \
79   } while (0)
80
81 /* The reserved keyword table.  */
82 struct resword
83 {
84   const char *word;
85   ENUM_BITFIELD(rid) rid : 16;
86   unsigned int disable   : 16;
87 };
88
89 /* Disable mask.  Keywords are disabled if (reswords[i].disable &
90    mask) is _true_.  */
91 #define D_C89   0x01    /* not in C89 */
92 #define D_EXT   0x02    /* GCC extension */
93 #define D_EXT89 0x04    /* GCC extension incorporated in C99 */
94 #define D_OBJC  0x08    /* Objective C only */
95
96 static const struct resword reswords[] =
97 {
98   { "_Bool",            RID_BOOL,       0 },
99   { "_Complex",         RID_COMPLEX,    0 },
100   { "_Decimal32",       RID_DFLOAT32,  D_EXT },
101   { "_Decimal64",       RID_DFLOAT64,  D_EXT },
102   { "_Decimal128",      RID_DFLOAT128, D_EXT },
103   { "__FUNCTION__",     RID_FUNCTION_NAME, 0 },
104   { "__PRETTY_FUNCTION__", RID_PRETTY_FUNCTION_NAME, 0 },
105   { "__alignof",        RID_ALIGNOF,    0 },
106   { "__alignof__",      RID_ALIGNOF,    0 },
107   { "__asm",            RID_ASM,        0 },
108   { "__asm__",          RID_ASM,        0 },
109   { "__attribute",      RID_ATTRIBUTE,  0 },
110   { "__attribute__",    RID_ATTRIBUTE,  0 },
111   { "__builtin_choose_expr", RID_CHOOSE_EXPR, 0 },
112   { "__builtin_offsetof", RID_OFFSETOF, 0 },
113   { "__builtin_types_compatible_p", RID_TYPES_COMPATIBLE_P, 0 },
114   { "__builtin_va_arg", RID_VA_ARG,     0 },
115   { "__complex",        RID_COMPLEX,    0 },
116   { "__complex__",      RID_COMPLEX,    0 },
117   { "__const",          RID_CONST,      0 },
118   { "__const__",        RID_CONST,      0 },
119   { "__extension__",    RID_EXTENSION,  0 },
120   { "__func__",         RID_C99_FUNCTION_NAME, 0 },
121   { "__imag",           RID_IMAGPART,   0 },
122   { "__imag__",         RID_IMAGPART,   0 },
123   { "__inline",         RID_INLINE,     0 },
124   { "__inline__",       RID_INLINE,     0 },
125   { "__label__",        RID_LABEL,      0 },
126   { "__real",           RID_REALPART,   0 },
127   { "__real__",         RID_REALPART,   0 },
128   { "__restrict",       RID_RESTRICT,   0 },
129   { "__restrict__",     RID_RESTRICT,   0 },
130   { "__signed",         RID_SIGNED,     0 },
131   { "__signed__",       RID_SIGNED,     0 },
132   { "__thread",         RID_THREAD,     0 },
133   { "__typeof",         RID_TYPEOF,     0 },
134   { "__typeof__",       RID_TYPEOF,     0 },
135   { "__volatile",       RID_VOLATILE,   0 },
136   { "__volatile__",     RID_VOLATILE,   0 },
137   { "asm",              RID_ASM,        D_EXT },
138   { "auto",             RID_AUTO,       0 },
139   { "break",            RID_BREAK,      0 },
140   { "case",             RID_CASE,       0 },
141   { "char",             RID_CHAR,       0 },
142   { "const",            RID_CONST,      0 },
143   { "continue",         RID_CONTINUE,   0 },
144   { "default",          RID_DEFAULT,    0 },
145   { "do",               RID_DO,         0 },
146   { "double",           RID_DOUBLE,     0 },
147   { "else",             RID_ELSE,       0 },
148   { "enum",             RID_ENUM,       0 },
149   { "extern",           RID_EXTERN,     0 },
150   { "float",            RID_FLOAT,      0 },
151   { "for",              RID_FOR,        0 },
152   { "goto",             RID_GOTO,       0 },
153   { "if",               RID_IF,         0 },
154   { "inline",           RID_INLINE,     D_EXT89 },
155   { "int",              RID_INT,        0 },
156   { "long",             RID_LONG,       0 },
157   { "register",         RID_REGISTER,   0 },
158   { "restrict",         RID_RESTRICT,   D_C89 },
159   { "return",           RID_RETURN,     0 },
160   { "short",            RID_SHORT,      0 },
161   { "signed",           RID_SIGNED,     0 },
162   { "sizeof",           RID_SIZEOF,     0 },
163   { "static",           RID_STATIC,     0 },
164   { "struct",           RID_STRUCT,     0 },
165   { "switch",           RID_SWITCH,     0 },
166   { "typedef",          RID_TYPEDEF,    0 },
167   { "typeof",           RID_TYPEOF,     D_EXT },
168   { "union",            RID_UNION,      0 },
169   { "unsigned",         RID_UNSIGNED,   0 },
170   { "void",             RID_VOID,       0 },
171   { "volatile",         RID_VOLATILE,   0 },
172   { "while",            RID_WHILE,      0 },
173   /* These Objective-C keywords are recognized only immediately after
174      an '@'.  */
175   { "class",            RID_AT_CLASS,           D_OBJC },
176   { "compatibility_alias", RID_AT_ALIAS,        D_OBJC },
177   { "defs",             RID_AT_DEFS,            D_OBJC },
178   { "encode",           RID_AT_ENCODE,          D_OBJC },
179   { "end",              RID_AT_END,             D_OBJC },
180   { "implementation",   RID_AT_IMPLEMENTATION,  D_OBJC },
181   { "interface",        RID_AT_INTERFACE,       D_OBJC },
182   { "private",          RID_AT_PRIVATE,         D_OBJC },
183   { "protected",        RID_AT_PROTECTED,       D_OBJC },
184   { "protocol",         RID_AT_PROTOCOL,        D_OBJC },
185   { "public",           RID_AT_PUBLIC,          D_OBJC },
186   { "selector",         RID_AT_SELECTOR,        D_OBJC },
187   { "throw",            RID_AT_THROW,           D_OBJC },
188   { "try",              RID_AT_TRY,             D_OBJC },
189   { "catch",            RID_AT_CATCH,           D_OBJC },
190   { "finally",          RID_AT_FINALLY,         D_OBJC },
191   { "synchronized",     RID_AT_SYNCHRONIZED,    D_OBJC },
192   /* These are recognized only in protocol-qualifier context
193      (see above) */
194   { "bycopy",           RID_BYCOPY,             D_OBJC },
195   { "byref",            RID_BYREF,              D_OBJC },
196   { "in",               RID_IN,                 D_OBJC },
197   { "inout",            RID_INOUT,              D_OBJC },
198   { "oneway",           RID_ONEWAY,             D_OBJC },
199   { "out",              RID_OUT,                D_OBJC },
200 };
201 #define N_reswords (sizeof reswords / sizeof (struct resword))
202
203 /* Initialization routine for this file.  */
204
205 void
206 c_parse_init (void)
207 {
208   /* The only initialization required is of the reserved word
209      identifiers.  */
210   unsigned int i;
211   tree id;
212   int mask = (flag_isoc99 ? 0 : D_C89)
213               | (flag_no_asm ? (flag_isoc99 ? D_EXT : D_EXT|D_EXT89) : 0);
214
215   if (!c_dialect_objc ())
216      mask |= D_OBJC;
217
218   ridpointers = GGC_CNEWVEC (tree, (int) RID_MAX);
219   for (i = 0; i < N_reswords; i++)
220     {
221       /* If a keyword is disabled, do not enter it into the table
222          and so create a canonical spelling that isn't a keyword.  */
223       if (reswords[i].disable & mask)
224         continue;
225
226       id = get_identifier (reswords[i].word);
227       C_RID_CODE (id) = reswords[i].rid;
228       C_IS_RESERVED_WORD (id) = 1;
229       ridpointers [(int) reswords[i].rid] = id;
230     }
231 }
232 \f
233 /* The C lexer intermediates between the lexer in cpplib and c-lex.c
234    and the C parser.  Unlike the C++ lexer, the parser structure
235    stores the lexer information instead of using a separate structure.
236    Identifiers are separated into ordinary identifiers, type names,
237    keywords and some other Objective-C types of identifiers, and some
238    look-ahead is maintained.
239
240    ??? It might be a good idea to lex the whole file up front (as for
241    C++).  It would then be possible to share more of the C and C++
242    lexer code, if desired.  */
243
244 /* The following local token type is used.  */
245
246 /* A keyword.  */
247 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
248
249 /* More information about the type of a CPP_NAME token.  */
250 typedef enum c_id_kind {
251   /* An ordinary identifier.  */
252   C_ID_ID,
253   /* An identifier declared as a typedef name.  */
254   C_ID_TYPENAME,
255   /* An identifier declared as an Objective-C class name.  */
256   C_ID_CLASSNAME,
257   /* Not an identifier.  */
258   C_ID_NONE
259 } c_id_kind;
260
261 /* A single C token after string literal concatenation and conversion
262    of preprocessing tokens to tokens.  */
263 typedef struct c_token GTY (())
264 {
265   /* The kind of token.  */
266   ENUM_BITFIELD (cpp_ttype) type : 8;
267   /* If this token is a CPP_NAME, this value indicates whether also
268      declared as some kind of type.  Otherwise, it is C_ID_NONE.  */
269   ENUM_BITFIELD (c_id_kind) id_kind : 8;
270   /* If this token is a keyword, this value indicates which keyword.
271      Otherwise, this value is RID_MAX.  */
272   ENUM_BITFIELD (rid) keyword : 8;
273   /* If this token is a CPP_PRAGMA, this indicates the pragma that
274      was seen.  Otherwise it is PRAGMA_NONE.  */
275   ENUM_BITFIELD (pragma_kind) pragma_kind : 7;
276   /* True if this token is from a system header.  */
277   BOOL_BITFIELD in_system_header : 1;
278   /* The value associated with this token, if any.  */
279   tree value;
280   /* The location at which this token was found.  */
281   location_t location;
282 } c_token;
283
284 /* A parser structure recording information about the state and
285    context of parsing.  Includes lexer information with up to two
286    tokens of look-ahead; more are not needed for C.  */
287 typedef struct c_parser GTY(())
288 {
289   /* The look-ahead tokens.  */
290   c_token tokens[2];
291   /* How many look-ahead tokens are available (0, 1 or 2).  */
292   short tokens_avail;
293   /* True if a syntax error is being recovered from; false otherwise.
294      c_parser_error sets this flag.  It should clear this flag when
295      enough tokens have been consumed to recover from the error.  */
296   BOOL_BITFIELD error : 1;
297   /* True if we're processing a pragma, and shouldn't automatically
298      consume CPP_PRAGMA_EOL.  */
299   BOOL_BITFIELD in_pragma : 1;
300 } c_parser;
301
302
303 /* The actual parser and external interface.  ??? Does this need to be
304    garbage-collected?  */
305
306 static GTY (()) c_parser *the_parser;
307
308
309 /* Read in and lex a single token, storing it in *TOKEN.  */
310
311 static void
312 c_lex_one_token (c_token *token)
313 {
314   timevar_push (TV_LEX);
315
316   token->type = c_lex_with_flags (&token->value, &token->location, NULL);
317   token->id_kind = C_ID_NONE;
318   token->keyword = RID_MAX;
319   token->pragma_kind = PRAGMA_NONE;
320   token->in_system_header = in_system_header;
321
322   switch (token->type)
323     {
324     case CPP_NAME:
325       {
326         tree decl;
327
328         int objc_force_identifier = objc_need_raw_identifier;
329         OBJC_NEED_RAW_IDENTIFIER (0);
330
331         if (C_IS_RESERVED_WORD (token->value))
332           {
333             enum rid rid_code = C_RID_CODE (token->value);
334
335             if (c_dialect_objc ())
336               {
337                 if (!OBJC_IS_AT_KEYWORD (rid_code)
338                     && (!OBJC_IS_PQ_KEYWORD (rid_code) || objc_pq_context))
339                   {
340                     /* Return the canonical spelling for this keyword.  */
341                     token->value = ridpointers[(int) rid_code];
342                     token->type = CPP_KEYWORD;
343                     token->keyword = rid_code;
344                     break;
345                   }
346               }
347             else
348               {
349                 /* Return the canonical spelling for this keyword.  */
350                 token->value = ridpointers[(int) rid_code];
351                 token->type = CPP_KEYWORD;
352                 token->keyword = rid_code;
353                 break;
354               }
355           }
356
357         decl = lookup_name (token->value);
358         if (decl)
359           {
360             if (TREE_CODE (decl) == TYPE_DECL)
361               {
362                 token->id_kind = C_ID_TYPENAME;
363                 break;
364               }
365           }
366         else if (c_dialect_objc ())
367           {
368             tree objc_interface_decl = objc_is_class_name (token->value);
369             /* Objective-C class names are in the same namespace as
370                variables and typedefs, and hence are shadowed by local
371                declarations.  */
372             if (objc_interface_decl
373                 && (global_bindings_p ()
374                     || (!objc_force_identifier && !decl)))
375               {
376                 token->value = objc_interface_decl;
377                 token->id_kind = C_ID_CLASSNAME;
378                 break;
379               }
380           }
381         token->id_kind = C_ID_ID;
382       }
383       break;
384     case CPP_AT_NAME:
385       /* This only happens in Objective-C; it must be a keyword.  */
386       token->type = CPP_KEYWORD;
387       token->keyword = C_RID_CODE (token->value);
388       break;
389     case CPP_COLON:
390     case CPP_COMMA:
391     case CPP_CLOSE_PAREN:
392     case CPP_SEMICOLON:
393       /* These tokens may affect the interpretation of any identifiers
394          following, if doing Objective-C.  */
395       OBJC_NEED_RAW_IDENTIFIER (0);
396       break;
397     case CPP_PRAGMA:
398       /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST.  */
399       token->pragma_kind = TREE_INT_CST_LOW (token->value);
400       token->value = NULL;
401       break;
402     default:
403       break;
404     }
405   timevar_pop (TV_LEX);
406 }
407
408 /* Return a pointer to the next token from PARSER, reading it in if
409    necessary.  */
410
411 static inline c_token *
412 c_parser_peek_token (c_parser *parser)
413 {
414   if (parser->tokens_avail == 0)
415     {
416       c_lex_one_token (&parser->tokens[0]);
417       parser->tokens_avail = 1;
418     }
419   return &parser->tokens[0];
420 }
421
422 /* Return true if the next token from PARSER has the indicated
423    TYPE.  */
424
425 static inline bool
426 c_parser_next_token_is (c_parser *parser, enum cpp_ttype type)
427 {
428   return c_parser_peek_token (parser)->type == type;
429 }
430
431 /* Return true if the next token from PARSER does not have the
432    indicated TYPE.  */
433
434 static inline bool
435 c_parser_next_token_is_not (c_parser *parser, enum cpp_ttype type)
436 {
437   return !c_parser_next_token_is (parser, type);
438 }
439
440 /* Return true if the next token from PARSER is the indicated
441    KEYWORD.  */
442
443 static inline bool
444 c_parser_next_token_is_keyword (c_parser *parser, enum rid keyword)
445 {
446   c_token *token;
447
448   /* Peek at the next token.  */
449   token = c_parser_peek_token (parser);
450   /* Check to see if it is the indicated keyword.  */
451   return token->keyword == keyword;
452 }
453
454 /* Return true if TOKEN can start a type name,
455    false otherwise.  */
456 static bool
457 c_token_starts_typename (c_token *token)
458 {
459   switch (token->type)
460     {
461     case CPP_NAME:
462       switch (token->id_kind)
463         {
464         case C_ID_ID:
465           return false;
466         case C_ID_TYPENAME:
467           return true;
468         case C_ID_CLASSNAME:
469           gcc_assert (c_dialect_objc ());
470           return true;
471         default:
472           gcc_unreachable ();
473         }
474     case CPP_KEYWORD:
475       switch (token->keyword)
476         {
477         case RID_UNSIGNED:
478         case RID_LONG:
479         case RID_SHORT:
480         case RID_SIGNED:
481         case RID_COMPLEX:
482         case RID_INT:
483         case RID_CHAR:
484         case RID_FLOAT:
485         case RID_DOUBLE:
486         case RID_VOID:
487         case RID_DFLOAT32:
488         case RID_DFLOAT64:
489         case RID_DFLOAT128:
490         case RID_BOOL:
491         case RID_ENUM:
492         case RID_STRUCT:
493         case RID_UNION:
494         case RID_TYPEOF:
495         case RID_CONST:
496         case RID_VOLATILE:
497         case RID_RESTRICT:
498         case RID_ATTRIBUTE:
499           return true;
500         default:
501           return false;
502         }
503     case CPP_LESS:
504       if (c_dialect_objc ())
505         return true;
506       return false;
507     default:
508       return false;
509     }
510 }
511
512 /* Return true if the next token from PARSER can start a type name,
513    false otherwise.  */
514 static inline bool
515 c_parser_next_token_starts_typename (c_parser *parser)
516 {
517   c_token *token = c_parser_peek_token (parser);
518   return c_token_starts_typename (token);
519 }
520
521 /* Return true if TOKEN can start declaration specifiers, false
522    otherwise.  */
523 static bool
524 c_token_starts_declspecs (c_token *token)
525 {
526   switch (token->type)
527     {
528     case CPP_NAME:
529       switch (token->id_kind)
530         {
531         case C_ID_ID:
532           return false;
533         case C_ID_TYPENAME:
534           return true;
535         case C_ID_CLASSNAME:
536           gcc_assert (c_dialect_objc ());
537           return true;
538         default:
539           gcc_unreachable ();
540         }
541     case CPP_KEYWORD:
542       switch (token->keyword)
543         {
544         case RID_STATIC:
545         case RID_EXTERN:
546         case RID_REGISTER:
547         case RID_TYPEDEF:
548         case RID_INLINE:
549         case RID_AUTO:
550         case RID_THREAD:
551         case RID_UNSIGNED:
552         case RID_LONG:
553         case RID_SHORT:
554         case RID_SIGNED:
555         case RID_COMPLEX:
556         case RID_INT:
557         case RID_CHAR:
558         case RID_FLOAT:
559         case RID_DOUBLE:
560         case RID_VOID:
561         case RID_DFLOAT32:
562         case RID_DFLOAT64:
563         case RID_DFLOAT128:
564         case RID_BOOL:
565         case RID_ENUM:
566         case RID_STRUCT:
567         case RID_UNION:
568         case RID_TYPEOF:
569         case RID_CONST:
570         case RID_VOLATILE:
571         case RID_RESTRICT:
572         case RID_ATTRIBUTE:
573           return true;
574         default:
575           return false;
576         }
577     case CPP_LESS:
578       if (c_dialect_objc ())
579         return true;
580       return false;
581     default:
582       return false;
583     }
584 }
585
586 /* Return true if the next token from PARSER can start declaration
587    specifiers, false otherwise.  */
588 static inline bool
589 c_parser_next_token_starts_declspecs (c_parser *parser)
590 {
591   c_token *token = c_parser_peek_token (parser);
592   return c_token_starts_declspecs (token);
593 }
594
595 /* Return a pointer to the next-but-one token from PARSER, reading it
596    in if necessary.  The next token is already read in.  */
597
598 static c_token *
599 c_parser_peek_2nd_token (c_parser *parser)
600 {
601   if (parser->tokens_avail >= 2)
602     return &parser->tokens[1];
603   gcc_assert (parser->tokens_avail == 1);
604   gcc_assert (parser->tokens[0].type != CPP_EOF);
605   gcc_assert (parser->tokens[0].type != CPP_PRAGMA_EOL);
606   c_lex_one_token (&parser->tokens[1]);
607   parser->tokens_avail = 2;
608   return &parser->tokens[1];
609 }
610
611 /* Consume the next token from PARSER.  */
612
613 static void
614 c_parser_consume_token (c_parser *parser)
615 {
616   gcc_assert (parser->tokens_avail >= 1);
617   gcc_assert (parser->tokens[0].type != CPP_EOF);
618   gcc_assert (!parser->in_pragma || parser->tokens[0].type != CPP_PRAGMA_EOL);
619   gcc_assert (parser->error || parser->tokens[0].type != CPP_PRAGMA);
620   if (parser->tokens_avail == 2)
621     parser->tokens[0] = parser->tokens[1];
622   parser->tokens_avail--;
623 }
624
625 /* Expect the current token to be a #pragma.  Consume it and remember
626    that we've begun parsing a pragma.  */
627
628 static void
629 c_parser_consume_pragma (c_parser *parser)
630 {
631   gcc_assert (!parser->in_pragma);
632   gcc_assert (parser->tokens_avail >= 1);
633   gcc_assert (parser->tokens[0].type == CPP_PRAGMA);
634   if (parser->tokens_avail == 2)
635     parser->tokens[0] = parser->tokens[1];
636   parser->tokens_avail--;
637   parser->in_pragma = true;
638 }
639
640 /* Update the globals input_location and in_system_header from
641    TOKEN.  */
642 static inline void
643 c_parser_set_source_position_from_token (c_token *token)
644 {
645   if (token->type != CPP_EOF)
646     {
647       input_location = token->location;
648       in_system_header = token->in_system_header;
649     }
650 }
651
652 /* Issue a diagnostic of the form
653       FILE:LINE: MESSAGE before TOKEN
654    where TOKEN is the next token in the input stream of PARSER.
655    MESSAGE (specified by the caller) is usually of the form "expected
656    OTHER-TOKEN".
657
658    Do not issue a diagnostic if still recovering from an error.
659
660    ??? This is taken from the C++ parser, but building up messages in
661    this way is not i18n-friendly and some other approach should be
662    used.  */
663
664 static void
665 c_parser_error (c_parser *parser, const char *gmsgid)
666 {
667   c_token *token = c_parser_peek_token (parser);
668   if (parser->error)
669     return;
670   parser->error = true;
671   if (!gmsgid)
672     return;
673   /* This diagnostic makes more sense if it is tagged to the line of
674      the token we just peeked at.  */
675   c_parser_set_source_position_from_token (token);
676   c_parse_error (gmsgid,
677                  /* Because c_parse_error does not understand
678                     CPP_KEYWORD, keywords are treated like
679                     identifiers.  */
680                  (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
681                  token->value);
682 }
683
684 /* If the next token is of the indicated TYPE, consume it.  Otherwise,
685    issue the error MSGID.  If MSGID is NULL then a message has already
686    been produced and no message will be produced this time.  Returns
687    true if found, false otherwise.  */
688
689 static bool
690 c_parser_require (c_parser *parser,
691                   enum cpp_ttype type,
692                   const char *msgid)
693 {
694   if (c_parser_next_token_is (parser, type))
695     {
696       c_parser_consume_token (parser);
697       return true;
698     }
699   else
700     {
701       c_parser_error (parser, msgid);
702       return false;
703     }
704 }
705
706 /* If the next token is the indicated keyword, consume it.  Otherwise,
707    issue the error MSGID.  Returns true if found, false otherwise.  */
708
709 static bool
710 c_parser_require_keyword (c_parser *parser,
711                           enum rid keyword,
712                           const char *msgid)
713 {
714   if (c_parser_next_token_is_keyword (parser, keyword))
715     {
716       c_parser_consume_token (parser);
717       return true;
718     }
719   else
720     {
721       c_parser_error (parser, msgid);
722       return false;
723     }
724 }
725
726 /* Like c_parser_require, except that tokens will be skipped until the
727    desired token is found.  An error message is still produced if the
728    next token is not as expected.  If MSGID is NULL then a message has
729    already been produced and no message will be produced this
730    time.  */
731
732 static void
733 c_parser_skip_until_found (c_parser *parser,
734                            enum cpp_ttype type,
735                            const char *msgid)
736 {
737   unsigned nesting_depth = 0;
738
739   if (c_parser_require (parser, type, msgid))
740     return;
741
742   /* Skip tokens until the desired token is found.  */
743   while (true)
744     {
745       /* Peek at the next token.  */
746       c_token *token = c_parser_peek_token (parser);
747       /* If we've reached the token we want, consume it and stop.  */
748       if (token->type == type && !nesting_depth)
749         {
750           c_parser_consume_token (parser);
751           break;
752         }
753
754       /* If we've run out of tokens, stop.  */
755       if (token->type == CPP_EOF)
756         return;
757       if (token->type == CPP_PRAGMA_EOL && parser->in_pragma)
758         return;
759       if (token->type == CPP_OPEN_BRACE
760           || token->type == CPP_OPEN_PAREN
761           || token->type == CPP_OPEN_SQUARE)
762         ++nesting_depth;
763       else if (token->type == CPP_CLOSE_BRACE
764                || token->type == CPP_CLOSE_PAREN
765                || token->type == CPP_CLOSE_SQUARE)
766         {
767           if (nesting_depth-- == 0)
768             break;
769         }
770       /* Consume this token.  */
771       c_parser_consume_token (parser);
772     }
773   parser->error = false;
774 }
775
776 /* Skip tokens until the end of a parameter is found, but do not
777    consume the comma, semicolon or closing delimiter.  */
778
779 static void
780 c_parser_skip_to_end_of_parameter (c_parser *parser)
781 {
782   unsigned nesting_depth = 0;
783
784   while (true)
785     {
786       c_token *token = c_parser_peek_token (parser);
787       if ((token->type == CPP_COMMA || token->type == CPP_SEMICOLON)
788           && !nesting_depth)
789         break;
790       /* If we've run out of tokens, stop.  */
791       if (token->type == CPP_EOF)
792         return;
793       if (token->type == CPP_PRAGMA_EOL && parser->in_pragma)
794         return;
795       if (token->type == CPP_OPEN_BRACE
796           || token->type == CPP_OPEN_PAREN
797           || token->type == CPP_OPEN_SQUARE)
798         ++nesting_depth;
799       else if (token->type == CPP_CLOSE_BRACE
800                || token->type == CPP_CLOSE_PAREN
801                || token->type == CPP_CLOSE_SQUARE)
802         {
803           if (nesting_depth-- == 0)
804             break;
805         }
806       /* Consume this token.  */
807       c_parser_consume_token (parser);
808     }
809   parser->error = false;
810 }
811
812 /* Skip tokens until we have consumed an entire block, or until we
813    have consumed a non-nested ';'.  */
814
815 static void
816 c_parser_skip_to_end_of_block_or_statement (c_parser *parser)
817 {
818   unsigned nesting_depth = 0;
819
820   while (true)
821     {
822       c_token *token;
823
824       /* Peek at the next token.  */
825       token = c_parser_peek_token (parser);
826       /* If we've run out of tokens, stop.  */
827       if (token->type == CPP_EOF)
828         return;
829       if (token->type == CPP_PRAGMA_EOL && parser->in_pragma)
830         return;
831       /* If the next token is a ';', we have reached the end of the
832          statement.  */
833       if (token->type == CPP_SEMICOLON && !nesting_depth)
834         {
835           /* Consume the ';'.  */
836           c_parser_consume_token (parser);
837           break;
838         }
839       /* If the next token is a non-nested '}', then we have reached
840          the end of the current block.  */
841       if (token->type == CPP_CLOSE_BRACE
842           && (nesting_depth == 0 || --nesting_depth == 0))
843         {
844           c_parser_consume_token (parser);
845           break;
846         }
847       /* If it the next token is a '{', then we are entering a new
848          block.  Consume the entire block.  */
849       if (token->type == CPP_OPEN_BRACE)
850         ++nesting_depth;
851       c_parser_consume_token (parser);
852     }
853   parser->error = false;
854 }
855
856 /* Expect to be at the end of the pragma directive and consume an
857    end of line marker.  */
858
859 static void
860 c_parser_skip_to_pragma_eol (c_parser *parser)
861 {
862   gcc_assert (parser->in_pragma);
863   parser->in_pragma = false;
864
865   if (!c_parser_require (parser, CPP_PRAGMA_EOL, "expected end of line"))
866     while (true)
867       {
868         c_token *token = c_parser_peek_token (parser);
869         if (token->type == CPP_EOF)
870           break;
871         if (token->type == CPP_PRAGMA_EOL)
872           {
873             c_parser_consume_token (parser);
874             break;
875           }
876         c_parser_consume_token (parser);
877       }
878
879   parser->error = false;
880 }
881
882 /* Save the warning flags which are controlled by __extension__.  */
883
884 static inline int
885 disable_extension_diagnostics (void)
886 {
887   int ret = (pedantic
888              | (warn_pointer_arith << 1)
889              | (warn_traditional << 2)
890              | (flag_iso << 3));
891   pedantic = 0;
892   warn_pointer_arith = 0;
893   warn_traditional = 0;
894   flag_iso = 0;
895   return ret;
896 }
897
898 /* Restore the warning flags which are controlled by __extension__.
899    FLAGS is the return value from disable_extension_diagnostics.  */
900
901 static inline void
902 restore_extension_diagnostics (int flags)
903 {
904   pedantic = flags & 1;
905   warn_pointer_arith = (flags >> 1) & 1;
906   warn_traditional = (flags >> 2) & 1;
907   flag_iso = (flags >> 3) & 1;
908 }
909
910 /* Possibly kinds of declarator to parse.  */
911 typedef enum c_dtr_syn {
912   /* A normal declarator with an identifier.  */
913   C_DTR_NORMAL,
914   /* An abstract declarator (maybe empty).  */
915   C_DTR_ABSTRACT,
916   /* A parameter declarator: may be either, but after a type name does
917      not redeclare a typedef name as an identifier if it can
918      alternatively be interpreted as a typedef name; see DR#009,
919      applied in C90 TC1, omitted from C99 and reapplied in C99 TC2
920      following DR#249.  For example, given a typedef T, "int T" and
921      "int *T" are valid parameter declarations redeclaring T, while
922      "int (T)" and "int * (T)" and "int (T[])" and "int (T (int))" are
923      abstract declarators rather than involving redundant parentheses;
924      the same applies with attributes inside the parentheses before
925      "T".  */
926   C_DTR_PARM
927 } c_dtr_syn;
928
929 static void c_parser_external_declaration (c_parser *);
930 static void c_parser_asm_definition (c_parser *);
931 static void c_parser_declaration_or_fndef (c_parser *, bool, bool, bool, bool);
932 static void c_parser_declspecs (c_parser *, struct c_declspecs *, bool, bool,
933                                 bool);
934 static struct c_typespec c_parser_enum_specifier (c_parser *);
935 static struct c_typespec c_parser_struct_or_union_specifier (c_parser *);
936 static tree c_parser_struct_declaration (c_parser *);
937 static struct c_typespec c_parser_typeof_specifier (c_parser *);
938 static struct c_declarator *c_parser_declarator (c_parser *, bool, c_dtr_syn,
939                                                  bool *);
940 static struct c_declarator *c_parser_direct_declarator (c_parser *, bool,
941                                                         c_dtr_syn, bool *);
942 static struct c_declarator *c_parser_direct_declarator_inner (c_parser *,
943                                                               bool,
944                                                               struct c_declarator *);
945 static struct c_arg_info *c_parser_parms_declarator (c_parser *, bool, tree);
946 static struct c_arg_info *c_parser_parms_list_declarator (c_parser *, tree);
947 static struct c_parm *c_parser_parameter_declaration (c_parser *, tree);
948 static tree c_parser_simple_asm_expr (c_parser *);
949 static tree c_parser_attributes (c_parser *);
950 static struct c_type_name *c_parser_type_name (c_parser *);
951 static struct c_expr c_parser_initializer (c_parser *);
952 static struct c_expr c_parser_braced_init (c_parser *, tree, bool);
953 static void c_parser_initelt (c_parser *);
954 static void c_parser_initval (c_parser *, struct c_expr *);
955 static tree c_parser_compound_statement (c_parser *);
956 static void c_parser_compound_statement_nostart (c_parser *);
957 static void c_parser_label (c_parser *);
958 static void c_parser_statement (c_parser *);
959 static void c_parser_statement_after_labels (c_parser *);
960 static void c_parser_if_statement (c_parser *);
961 static void c_parser_switch_statement (c_parser *);
962 static void c_parser_while_statement (c_parser *);
963 static void c_parser_do_statement (c_parser *);
964 static void c_parser_for_statement (c_parser *);
965 static tree c_parser_asm_statement (c_parser *);
966 static tree c_parser_asm_operands (c_parser *, bool);
967 static tree c_parser_asm_clobbers (c_parser *);
968 static struct c_expr c_parser_expr_no_commas (c_parser *, struct c_expr *);
969 static struct c_expr c_parser_conditional_expression (c_parser *,
970                                                       struct c_expr *);
971 static struct c_expr c_parser_binary_expression (c_parser *, struct c_expr *);
972 static struct c_expr c_parser_cast_expression (c_parser *, struct c_expr *);
973 static struct c_expr c_parser_unary_expression (c_parser *);
974 static struct c_expr c_parser_sizeof_expression (c_parser *);
975 static struct c_expr c_parser_alignof_expression (c_parser *);
976 static struct c_expr c_parser_postfix_expression (c_parser *);
977 static struct c_expr c_parser_postfix_expression_after_paren_type (c_parser *,
978                                                                    struct c_type_name *);
979 static struct c_expr c_parser_postfix_expression_after_primary (c_parser *,
980                                                                 struct c_expr);
981 static struct c_expr c_parser_expression (c_parser *);
982 static struct c_expr c_parser_expression_conv (c_parser *);
983 static tree c_parser_expr_list (c_parser *, bool);
984
985 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
986 static bool c_parser_pragma (c_parser *, enum pragma_context);
987
988 /* These Objective-C parser functions are only ever called when
989    compiling Objective-C.  */
990 static void c_parser_objc_class_definition (c_parser *);
991 static void c_parser_objc_class_instance_variables (c_parser *);
992 static void c_parser_objc_class_declaration (c_parser *);
993 static void c_parser_objc_alias_declaration (c_parser *);
994 static void c_parser_objc_protocol_definition (c_parser *);
995 static enum tree_code c_parser_objc_method_type (c_parser *);
996 static void c_parser_objc_method_definition (c_parser *);
997 static void c_parser_objc_methodprotolist (c_parser *);
998 static void c_parser_objc_methodproto (c_parser *);
999 static tree c_parser_objc_method_decl (c_parser *);
1000 static tree c_parser_objc_type_name (c_parser *);
1001 static tree c_parser_objc_protocol_refs (c_parser *);
1002 static void c_parser_objc_try_catch_statement (c_parser *);
1003 static void c_parser_objc_synchronized_statement (c_parser *);
1004 static tree c_parser_objc_selector (c_parser *);
1005 static tree c_parser_objc_selector_arg (c_parser *);
1006 static tree c_parser_objc_receiver (c_parser *);
1007 static tree c_parser_objc_message_args (c_parser *);
1008 static tree c_parser_objc_keywordexpr (c_parser *);
1009
1010 /* Parse a translation unit (C90 6.7, C99 6.9).
1011
1012    translation-unit:
1013      external-declarations
1014
1015    external-declarations:
1016      external-declaration
1017      external-declarations external-declaration
1018
1019    GNU extensions:
1020
1021    translation-unit:
1022      empty
1023 */
1024
1025 static void
1026 c_parser_translation_unit (c_parser *parser)
1027 {
1028   if (c_parser_next_token_is (parser, CPP_EOF))
1029     {
1030       if (pedantic)
1031         pedwarn ("ISO C forbids an empty source file");
1032     }
1033   else
1034     {
1035       void *obstack_position = obstack_alloc (&parser_obstack, 0);
1036       do
1037         {
1038           ggc_collect ();
1039           c_parser_external_declaration (parser);
1040           obstack_free (&parser_obstack, obstack_position);
1041         }
1042       while (c_parser_next_token_is_not (parser, CPP_EOF));
1043     }
1044 }
1045
1046 /* Parse an external declaration (C90 6.7, C99 6.9).
1047
1048    external-declaration:
1049      function-definition
1050      declaration
1051
1052    GNU extensions:
1053
1054    external-declaration:
1055      asm-definition
1056      ;
1057      __extension__ external-declaration
1058
1059    Objective-C:
1060
1061    external-declaration:
1062      objc-class-definition
1063      objc-class-declaration
1064      objc-alias-declaration
1065      objc-protocol-definition
1066      objc-method-definition
1067      @end
1068 */
1069
1070 static void
1071 c_parser_external_declaration (c_parser *parser)
1072 {
1073   int ext;
1074   switch (c_parser_peek_token (parser)->type)
1075     {
1076     case CPP_KEYWORD:
1077       switch (c_parser_peek_token (parser)->keyword)
1078         {
1079         case RID_EXTENSION:
1080           ext = disable_extension_diagnostics ();
1081           c_parser_consume_token (parser);
1082           c_parser_external_declaration (parser);
1083           restore_extension_diagnostics (ext);
1084           break;
1085         case RID_ASM:
1086           c_parser_asm_definition (parser);
1087           break;
1088         case RID_AT_INTERFACE:
1089         case RID_AT_IMPLEMENTATION:
1090           gcc_assert (c_dialect_objc ());
1091           c_parser_objc_class_definition (parser);
1092           break;
1093         case RID_AT_CLASS:
1094           gcc_assert (c_dialect_objc ());
1095           c_parser_objc_class_declaration (parser);
1096           break;
1097         case RID_AT_ALIAS:
1098           gcc_assert (c_dialect_objc ());
1099           c_parser_objc_alias_declaration (parser);
1100           break;
1101         case RID_AT_PROTOCOL:
1102           gcc_assert (c_dialect_objc ());
1103           c_parser_objc_protocol_definition (parser);
1104           break;
1105         case RID_AT_END:
1106           gcc_assert (c_dialect_objc ());
1107           c_parser_consume_token (parser);
1108           objc_finish_implementation ();
1109           break;
1110         default:
1111           goto decl_or_fndef;
1112         }
1113       break;
1114     case CPP_SEMICOLON:
1115       if (pedantic)
1116         pedwarn ("ISO C does not allow extra %<;%> outside of a function");
1117       c_parser_consume_token (parser);
1118       break;
1119     case CPP_PRAGMA:
1120       c_parser_pragma (parser, pragma_external);
1121       break;
1122     case CPP_PLUS:
1123     case CPP_MINUS:
1124       if (c_dialect_objc ())
1125         {
1126           c_parser_objc_method_definition (parser);
1127           break;
1128         }
1129       /* Else fall through, and yield a syntax error trying to parse
1130          as a declaration or function definition.  */
1131     default:
1132     decl_or_fndef:
1133       /* A declaration or a function definition.  We can only tell
1134          which after parsing the declaration specifiers, if any, and
1135          the first declarator.  */
1136       c_parser_declaration_or_fndef (parser, true, true, false, true);
1137       break;
1138     }
1139 }
1140
1141
1142 /* Parse a declaration or function definition (C90 6.5, 6.7.1, C99
1143    6.7, 6.9.1).  If FNDEF_OK is true, a function definition is
1144    accepted; otherwise (old-style parameter declarations) only other
1145    declarations are accepted.  If NESTED is true, we are inside a
1146    function or parsing old-style parameter declarations; any functions
1147    encountered are nested functions and declaration specifiers are
1148    required; otherwise we are at top level and functions are normal
1149    functions and declaration specifiers may be optional.  If EMPTY_OK
1150    is true, empty declarations are OK (subject to all other
1151    constraints); otherwise (old-style parameter declarations) they are
1152    diagnosed.  If START_ATTR_OK is true, the declaration specifiers
1153    may start with attributes; otherwise they may not.
1154
1155    declaration:
1156      declaration-specifiers init-declarator-list[opt] ;
1157
1158    function-definition:
1159      declaration-specifiers[opt] declarator declaration-list[opt]
1160        compound-statement
1161
1162    declaration-list:
1163      declaration
1164      declaration-list declaration
1165
1166    init-declarator-list:
1167      init-declarator
1168      init-declarator-list , init-declarator
1169
1170    init-declarator:
1171      declarator simple-asm-expr[opt] attributes[opt]
1172      declarator simple-asm-expr[opt] attributes[opt] = initializer
1173
1174    GNU extensions:
1175
1176    nested-function-definition:
1177      declaration-specifiers declarator declaration-list[opt]
1178        compound-statement
1179
1180    The simple-asm-expr and attributes are GNU extensions.
1181
1182    This function does not handle __extension__; that is handled in its
1183    callers.  ??? Following the old parser, __extension__ may start
1184    external declarations, declarations in functions and declarations
1185    at the start of "for" loops, but not old-style parameter
1186    declarations.
1187
1188    C99 requires declaration specifiers in a function definition; the
1189    absence is diagnosed through the diagnosis of implicit int.  In GNU
1190    C we also allow but diagnose declarations without declaration
1191    specifiers, but only at top level (elsewhere they conflict with
1192    other syntax).  */
1193
1194 static void
1195 c_parser_declaration_or_fndef (c_parser *parser, bool fndef_ok, bool empty_ok,
1196                                bool nested, bool start_attr_ok)
1197 {
1198   struct c_declspecs *specs;
1199   tree prefix_attrs;
1200   tree all_prefix_attrs;
1201   bool diagnosed_no_specs = false;
1202
1203   specs = build_null_declspecs ();
1204   c_parser_declspecs (parser, specs, true, true, start_attr_ok);
1205   if (parser->error)
1206     {
1207       c_parser_skip_to_end_of_block_or_statement (parser);
1208       return;
1209     }
1210   if (nested && !specs->declspecs_seen_p)
1211     {
1212       c_parser_error (parser, "expected declaration specifiers");
1213       c_parser_skip_to_end_of_block_or_statement (parser);
1214       return;
1215     }
1216   finish_declspecs (specs);
1217   if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1218     {
1219       if (empty_ok)
1220         shadow_tag (specs);
1221       else
1222         {
1223           shadow_tag_warned (specs, 1);
1224           pedwarn ("empty declaration");
1225         }
1226       c_parser_consume_token (parser);
1227       return;
1228     }
1229   pending_xref_error ();
1230   prefix_attrs = specs->attrs;
1231   all_prefix_attrs = prefix_attrs;
1232   specs->attrs = NULL_TREE;
1233   while (true)
1234     {
1235       struct c_declarator *declarator;
1236       bool dummy = false;
1237       tree fnbody;
1238       /* Declaring either one or more declarators (in which case we
1239          should diagnose if there were no declaration specifiers) or a
1240          function definition (in which case the diagnostic for
1241          implicit int suffices).  */
1242       declarator = c_parser_declarator (parser, specs->type_seen_p,
1243                                         C_DTR_NORMAL, &dummy);
1244       if (declarator == NULL)
1245         {
1246           c_parser_skip_to_end_of_block_or_statement (parser);
1247           return;
1248         }
1249       if (c_parser_next_token_is (parser, CPP_EQ)
1250           || c_parser_next_token_is (parser, CPP_COMMA)
1251           || c_parser_next_token_is (parser, CPP_SEMICOLON)
1252           || c_parser_next_token_is_keyword (parser, RID_ASM)
1253           || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1254         {
1255           tree asm_name = NULL_TREE;
1256           tree postfix_attrs = NULL_TREE;
1257           if (!diagnosed_no_specs && !specs->declspecs_seen_p)
1258             {
1259               diagnosed_no_specs = true;
1260               pedwarn ("data definition has no type or storage class");
1261             }
1262           /* Having seen a data definition, there cannot now be a
1263              function definition.  */
1264           fndef_ok = false;
1265           if (c_parser_next_token_is_keyword (parser, RID_ASM))
1266             asm_name = c_parser_simple_asm_expr (parser);
1267           if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1268             postfix_attrs = c_parser_attributes (parser);
1269           if (c_parser_next_token_is (parser, CPP_EQ))
1270             {
1271               tree d;
1272               struct c_expr init;
1273               c_parser_consume_token (parser);
1274               /* The declaration of the variable is in effect while
1275                  its initializer is parsed.  */
1276               d = start_decl (declarator, specs, true,
1277                               chainon (postfix_attrs, all_prefix_attrs));
1278               if (!d)
1279                 d = error_mark_node;
1280               start_init (d, asm_name, global_bindings_p ());
1281               init = c_parser_initializer (parser);
1282               finish_init ();
1283               if (d != error_mark_node)
1284                 {
1285                   maybe_warn_string_init (TREE_TYPE (d), init);
1286                   finish_decl (d, init.value, asm_name);
1287                 }
1288             }
1289           else
1290             {
1291               tree d = start_decl (declarator, specs, false,
1292                                    chainon (postfix_attrs,
1293                                             all_prefix_attrs));
1294               if (d)
1295                 finish_decl (d, NULL_TREE, asm_name);
1296             }
1297           if (c_parser_next_token_is (parser, CPP_COMMA))
1298             {
1299               c_parser_consume_token (parser);
1300               if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1301                 all_prefix_attrs = chainon (c_parser_attributes (parser),
1302                                             prefix_attrs);
1303               else
1304                 all_prefix_attrs = prefix_attrs;
1305               continue;
1306             }
1307           else if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1308             {
1309               c_parser_consume_token (parser);
1310               return;
1311             }
1312           else
1313             {
1314               c_parser_error (parser, "expected %<,%> or %<;%>");
1315               c_parser_skip_to_end_of_block_or_statement (parser);
1316               return;
1317             }
1318         }
1319       else if (!fndef_ok)
1320         {
1321           c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, "
1322                           "%<asm%> or %<__attribute__%>");
1323           c_parser_skip_to_end_of_block_or_statement (parser);
1324           return;
1325         }
1326       /* Function definition (nested or otherwise).  */
1327       if (nested)
1328         {
1329           if (pedantic)
1330             pedwarn ("ISO C forbids nested functions");
1331           push_function_context ();
1332         }
1333       if (!start_function (specs, declarator, all_prefix_attrs))
1334         {
1335           /* This can appear in many cases looking nothing like a
1336              function definition, so we don't give a more specific
1337              error suggesting there was one.  */
1338           c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, %<asm%> "
1339                           "or %<__attribute__%>");
1340           if (nested)
1341             pop_function_context ();
1342           break;
1343         }
1344       /* Parse old-style parameter declarations.  ??? Attributes are
1345          not allowed to start declaration specifiers here because of a
1346          syntax conflict between a function declaration with attribute
1347          suffix and a function definition with an attribute prefix on
1348          first old-style parameter declaration.  Following the old
1349          parser, they are not accepted on subsequent old-style
1350          parameter declarations either.  However, there is no
1351          ambiguity after the first declaration, nor indeed on the
1352          first as long as we don't allow postfix attributes after a
1353          declarator with a nonempty identifier list in a definition;
1354          and postfix attributes have never been accepted here in
1355          function definitions either.  */
1356       while (c_parser_next_token_is_not (parser, CPP_EOF)
1357              && c_parser_next_token_is_not (parser, CPP_OPEN_BRACE))
1358         c_parser_declaration_or_fndef (parser, false, false, true, false);
1359       DECL_SOURCE_LOCATION (current_function_decl)
1360         = c_parser_peek_token (parser)->location;
1361       store_parm_decls ();
1362       fnbody = c_parser_compound_statement (parser);
1363       if (nested)
1364         {
1365           tree decl = current_function_decl;
1366           add_stmt (fnbody);
1367           finish_function ();
1368           pop_function_context ();
1369           add_stmt (build_stmt (DECL_EXPR, decl));
1370         }
1371       else
1372         {
1373           add_stmt (fnbody);
1374           finish_function ();
1375         }
1376       break;
1377     }
1378 }
1379
1380 /* Parse an asm-definition (asm() outside a function body).  This is a
1381    GNU extension.
1382
1383    asm-definition:
1384      simple-asm-expr ;
1385 */
1386
1387 static void
1388 c_parser_asm_definition (c_parser *parser)
1389 {
1390   tree asm_str = c_parser_simple_asm_expr (parser);
1391   if (asm_str)
1392     cgraph_add_asm_node (asm_str);
1393   c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
1394 }
1395
1396 /* Parse some declaration specifiers (possibly none) (C90 6.5, C99
1397    6.7), adding them to SPECS (which may already include some).
1398    Storage class specifiers are accepted iff SCSPEC_OK; type
1399    specifiers are accepted iff TYPESPEC_OK; attributes are accepted at
1400    the start iff START_ATTR_OK.
1401
1402    declaration-specifiers:
1403      storage-class-specifier declaration-specifiers[opt]
1404      type-specifier declaration-specifiers[opt]
1405      type-qualifier declaration-specifiers[opt]
1406      function-specifier declaration-specifiers[opt]
1407
1408    Function specifiers (inline) are from C99, and are currently
1409    handled as storage class specifiers, as is __thread.
1410
1411    C90 6.5.1, C99 6.7.1:
1412    storage-class-specifier:
1413      typedef
1414      extern
1415      static
1416      auto
1417      register
1418
1419    C99 6.7.4:
1420    function-specifier:
1421      inline
1422
1423    C90 6.5.2, C99 6.7.2:
1424    type-specifier:
1425      void
1426      char
1427      short
1428      int
1429      long
1430      float
1431      double
1432      signed
1433      unsigned
1434      _Bool
1435      _Complex
1436      [_Imaginary removed in C99 TC2]
1437      struct-or-union-specifier
1438      enum-specifier
1439      typedef-name
1440
1441    (_Bool and _Complex are new in C99.)
1442
1443    C90 6.5.3, C99 6.7.3:
1444
1445    type-qualifier:
1446      const
1447      restrict
1448      volatile
1449
1450    (restrict is new in C99.)
1451
1452    GNU extensions:
1453
1454    declaration-specifiers:
1455      attributes declaration-specifiers[opt]
1456
1457    storage-class-specifier:
1458      __thread
1459
1460    type-specifier:
1461      typeof-specifier
1462      _Decimal32
1463      _Decimal64
1464      _Decimal128
1465
1466    Objective-C:
1467
1468    type-specifier:
1469      class-name objc-protocol-refs[opt]
1470      typedef-name objc-protocol-refs
1471      objc-protocol-refs
1472 */
1473
1474 static void
1475 c_parser_declspecs (c_parser *parser, struct c_declspecs *specs,
1476                     bool scspec_ok, bool typespec_ok, bool start_attr_ok)
1477 {
1478   bool attrs_ok = start_attr_ok;
1479   bool seen_type = specs->type_seen_p;
1480   while (c_parser_next_token_is (parser, CPP_NAME)
1481          || c_parser_next_token_is (parser, CPP_KEYWORD)
1482          || (c_dialect_objc () && c_parser_next_token_is (parser, CPP_LESS)))
1483     {
1484       struct c_typespec t;
1485       tree attrs;
1486       if (c_parser_next_token_is (parser, CPP_NAME))
1487         {
1488           tree value = c_parser_peek_token (parser)->value;
1489           c_id_kind kind = c_parser_peek_token (parser)->id_kind;
1490           /* This finishes the specifiers unless a type name is OK, it
1491              is declared as a type name and a type name hasn't yet
1492              been seen.  */
1493           if (!typespec_ok || seen_type
1494               || (kind != C_ID_TYPENAME && kind != C_ID_CLASSNAME))
1495             break;
1496           c_parser_consume_token (parser);
1497           seen_type = true;
1498           attrs_ok = true;
1499           if (kind == C_ID_TYPENAME
1500               && (!c_dialect_objc ()
1501                   || c_parser_next_token_is_not (parser, CPP_LESS)))
1502             {
1503               t.kind = ctsk_typedef;
1504               /* For a typedef name, record the meaning, not the name.
1505                  In case of 'foo foo, bar;'.  */
1506               t.spec = lookup_name (value);
1507             }
1508           else
1509             {
1510               tree proto = NULL_TREE;
1511               gcc_assert (c_dialect_objc ());
1512               t.kind = ctsk_objc;
1513               if (c_parser_next_token_is (parser, CPP_LESS))
1514                 proto = c_parser_objc_protocol_refs (parser);
1515               t.spec = objc_get_protocol_qualified_type (value, proto);
1516             }
1517           declspecs_add_type (specs, t);
1518           continue;
1519         }
1520       if (c_parser_next_token_is (parser, CPP_LESS))
1521         {
1522           /* Make "<SomeProtocol>" equivalent to "id <SomeProtocol>" -
1523              nisse@lysator.liu.se.  */
1524           tree proto;
1525           gcc_assert (c_dialect_objc ());
1526           if (!typespec_ok || seen_type)
1527             break;
1528           proto = c_parser_objc_protocol_refs (parser);
1529           t.kind = ctsk_objc;
1530           t.spec = objc_get_protocol_qualified_type (NULL_TREE, proto);
1531           declspecs_add_type (specs, t);
1532           continue;
1533         }
1534       gcc_assert (c_parser_next_token_is (parser, CPP_KEYWORD));
1535       switch (c_parser_peek_token (parser)->keyword)
1536         {
1537         case RID_STATIC:
1538         case RID_EXTERN:
1539         case RID_REGISTER:
1540         case RID_TYPEDEF:
1541         case RID_INLINE:
1542         case RID_AUTO:
1543         case RID_THREAD:
1544           if (!scspec_ok)
1545             goto out;
1546           attrs_ok = true;
1547           /* TODO: Distinguish between function specifiers (inline)
1548              and storage class specifiers, either here or in
1549              declspecs_add_scspec.  */
1550           declspecs_add_scspec (specs, c_parser_peek_token (parser)->value);
1551           c_parser_consume_token (parser);
1552           break;
1553         case RID_UNSIGNED:
1554         case RID_LONG:
1555         case RID_SHORT:
1556         case RID_SIGNED:
1557         case RID_COMPLEX:
1558         case RID_INT:
1559         case RID_CHAR:
1560         case RID_FLOAT:
1561         case RID_DOUBLE:
1562         case RID_VOID:
1563         case RID_DFLOAT32:
1564         case RID_DFLOAT64:
1565         case RID_DFLOAT128:
1566         case RID_BOOL:
1567           if (!typespec_ok)
1568             goto out;
1569           attrs_ok = true;
1570           seen_type = true;
1571           OBJC_NEED_RAW_IDENTIFIER (1);
1572           t.kind = ctsk_resword;
1573           t.spec = c_parser_peek_token (parser)->value;
1574           declspecs_add_type (specs, t);
1575           c_parser_consume_token (parser);
1576           break;
1577         case RID_ENUM:
1578           if (!typespec_ok)
1579             goto out;
1580           attrs_ok = true;
1581           seen_type = true;
1582           t = c_parser_enum_specifier (parser);
1583           declspecs_add_type (specs, t);
1584           break;
1585         case RID_STRUCT:
1586         case RID_UNION:
1587           if (!typespec_ok)
1588             goto out;
1589           attrs_ok = true;
1590           seen_type = true;
1591           t = c_parser_struct_or_union_specifier (parser);
1592           declspecs_add_type (specs, t);
1593           break;
1594         case RID_TYPEOF:
1595           /* ??? The old parser rejected typeof after other type
1596              specifiers, but is a syntax error the best way of
1597              handling this?  */
1598           if (!typespec_ok || seen_type)
1599             goto out;
1600           attrs_ok = true;
1601           seen_type = true;
1602           t = c_parser_typeof_specifier (parser);
1603           declspecs_add_type (specs, t);
1604           break;
1605         case RID_CONST:
1606         case RID_VOLATILE:
1607         case RID_RESTRICT:
1608           attrs_ok = true;
1609           declspecs_add_qual (specs, c_parser_peek_token (parser)->value);
1610           c_parser_consume_token (parser);
1611           break;
1612         case RID_ATTRIBUTE:
1613           if (!attrs_ok)
1614             goto out;
1615           attrs = c_parser_attributes (parser);
1616           declspecs_add_attrs (specs, attrs);
1617           break;
1618         default:
1619           goto out;
1620         }
1621     }
1622  out: ;
1623 }
1624
1625 /* Parse an enum specifier (C90 6.5.2.2, C99 6.7.2.2).
1626
1627    enum-specifier:
1628      enum attributes[opt] identifier[opt] { enumerator-list } attributes[opt]
1629      enum attributes[opt] identifier[opt] { enumerator-list , } attributes[opt]
1630      enum attributes[opt] identifier
1631
1632    The form with trailing comma is new in C99.  The forms with
1633    attributes are GNU extensions.  In GNU C, we accept any expression
1634    without commas in the syntax (assignment expressions, not just
1635    conditional expressions); assignment expressions will be diagnosed
1636    as non-constant.
1637
1638    enumerator-list:
1639      enumerator
1640      enumerator-list , enumerator
1641
1642    enumerator:
1643      enumeration-constant
1644      enumeration-constant = constant-expression
1645 */
1646
1647 static struct c_typespec
1648 c_parser_enum_specifier (c_parser *parser)
1649 {
1650   struct c_typespec ret;
1651   tree attrs;
1652   tree ident = NULL_TREE;
1653   gcc_assert (c_parser_next_token_is_keyword (parser, RID_ENUM));
1654   c_parser_consume_token (parser);
1655   attrs = c_parser_attributes (parser);
1656   if (c_parser_next_token_is (parser, CPP_NAME))
1657     {
1658       ident = c_parser_peek_token (parser)->value;
1659       c_parser_consume_token (parser);
1660     }
1661   if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
1662     {
1663       /* Parse an enum definition.  */
1664       tree type = start_enum (ident);
1665       tree postfix_attrs;
1666       /* We chain the enumerators in reverse order, then put them in
1667          forward order at the end.  */
1668       tree values = NULL_TREE;
1669       c_parser_consume_token (parser);
1670       while (true)
1671         {
1672           tree enum_id;
1673           tree enum_value;
1674           tree enum_decl;
1675           bool seen_comma;
1676           if (c_parser_next_token_is_not (parser, CPP_NAME))
1677             {
1678               c_parser_error (parser, "expected identifier");
1679               c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
1680               values = error_mark_node;
1681               break;
1682             }
1683           enum_id = c_parser_peek_token (parser)->value;
1684           c_parser_consume_token (parser);
1685           if (c_parser_next_token_is (parser, CPP_EQ))
1686             {
1687               c_parser_consume_token (parser);
1688               enum_value = c_parser_expr_no_commas (parser, NULL).value;
1689             }
1690           else
1691             enum_value = NULL_TREE;
1692           enum_decl = build_enumerator (enum_id, enum_value);
1693           TREE_CHAIN (enum_decl) = values;
1694           values = enum_decl;
1695           seen_comma = false;
1696           if (c_parser_next_token_is (parser, CPP_COMMA))
1697             {
1698               seen_comma = true;
1699               c_parser_consume_token (parser);
1700             }
1701           if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
1702             {
1703               if (seen_comma && pedantic && !flag_isoc99)
1704                 pedwarn ("comma at end of enumerator list");
1705               c_parser_consume_token (parser);
1706               break;
1707             }
1708           if (!seen_comma)
1709             {
1710               c_parser_error (parser, "expected %<,%> or %<}%>");
1711               c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
1712               values = error_mark_node;
1713               break;
1714             }
1715         }
1716       postfix_attrs = c_parser_attributes (parser);
1717       ret.spec = finish_enum (type, nreverse (values),
1718                               chainon (attrs, postfix_attrs));
1719       ret.kind = ctsk_tagdef;
1720       return ret;
1721     }
1722   else if (!ident)
1723     {
1724       c_parser_error (parser, "expected %<{%>");
1725       ret.spec = error_mark_node;
1726       ret.kind = ctsk_tagref;
1727       return ret;
1728     }
1729   ret = parser_xref_tag (ENUMERAL_TYPE, ident);
1730   /* In ISO C, enumerated types can be referred to only if already
1731      defined.  */
1732   if (pedantic && !COMPLETE_TYPE_P (ret.spec))
1733     pedwarn ("ISO C forbids forward references to %<enum%> types");
1734   return ret;
1735 }
1736
1737 /* Parse a struct or union specifier (C90 6.5.2.1, C99 6.7.2.1).
1738
1739    struct-or-union-specifier:
1740      struct-or-union attributes[opt] identifier[opt]
1741        { struct-contents } attributes[opt]
1742      struct-or-union attributes[opt] identifier
1743
1744    struct-contents:
1745      struct-declaration-list
1746
1747    struct-declaration-list:
1748      struct-declaration ;
1749      struct-declaration-list struct-declaration ;
1750
1751    GNU extensions:
1752
1753    struct-contents:
1754      empty
1755      struct-declaration
1756      struct-declaration-list struct-declaration
1757
1758    struct-declaration-list:
1759      struct-declaration-list ;
1760      ;
1761
1762    (Note that in the syntax here, unlike that in ISO C, the semicolons
1763    are included here rather than in struct-declaration, in order to
1764    describe the syntax with extra semicolons and missing semicolon at
1765    end.)
1766
1767    Objective-C:
1768
1769    struct-declaration-list:
1770      @defs ( class-name )
1771
1772    (Note this does not include a trailing semicolon, but can be
1773    followed by further declarations, and gets a pedwarn-if-pedantic
1774    when followed by a semicolon.)  */
1775
1776 static struct c_typespec
1777 c_parser_struct_or_union_specifier (c_parser *parser)
1778 {
1779   struct c_typespec ret;
1780   tree attrs;
1781   tree ident = NULL_TREE;
1782   enum tree_code code;
1783   switch (c_parser_peek_token (parser)->keyword)
1784     {
1785     case RID_STRUCT:
1786       code = RECORD_TYPE;
1787       break;
1788     case RID_UNION:
1789       code = UNION_TYPE;
1790       break;
1791     default:
1792       gcc_unreachable ();
1793     }
1794   c_parser_consume_token (parser);
1795   attrs = c_parser_attributes (parser);
1796   if (c_parser_next_token_is (parser, CPP_NAME))
1797     {
1798       ident = c_parser_peek_token (parser)->value;
1799       c_parser_consume_token (parser);
1800     }
1801   if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
1802     {
1803       /* Parse a struct or union definition.  Start the scope of the
1804          tag before parsing components.  */
1805       tree type = start_struct (code, ident);
1806       tree postfix_attrs;
1807       /* We chain the components in reverse order, then put them in
1808          forward order at the end.  Each struct-declaration may
1809          declare multiple components (comma-separated), so we must use
1810          chainon to join them, although when parsing each
1811          struct-declaration we can use TREE_CHAIN directly.
1812
1813          The theory behind all this is that there will be more
1814          semicolon separated fields than comma separated fields, and
1815          so we'll be minimizing the number of node traversals required
1816          by chainon.  */
1817       tree contents = NULL_TREE;
1818       c_parser_consume_token (parser);
1819       /* Handle the Objective-C @defs construct,
1820          e.g. foo(sizeof(struct{ @defs(ClassName) }));.  */
1821       if (c_parser_next_token_is_keyword (parser, RID_AT_DEFS))
1822         {
1823           tree name;
1824           gcc_assert (c_dialect_objc ());
1825           c_parser_consume_token (parser);
1826           if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
1827             goto end_at_defs;
1828           if (c_parser_next_token_is (parser, CPP_NAME)
1829               && c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME)
1830             {
1831               name = c_parser_peek_token (parser)->value;
1832               c_parser_consume_token (parser);
1833             }
1834           else
1835             {
1836               c_parser_error (parser, "expected class name");
1837               c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
1838               goto end_at_defs;
1839             }
1840           c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
1841                                      "expected %<)%>");
1842           contents = nreverse (objc_get_class_ivars (name));
1843         }
1844     end_at_defs:
1845       /* Parse the struct-declarations and semicolons.  Problems with
1846          semicolons are diagnosed here; empty structures are diagnosed
1847          elsewhere.  */
1848       while (true)
1849         {
1850           tree decls;
1851           /* Parse any stray semicolon.  */
1852           if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1853             {
1854               if (pedantic)
1855                 pedwarn ("extra semicolon in struct or union specified");
1856               c_parser_consume_token (parser);
1857               continue;
1858             }
1859           /* Stop if at the end of the struct or union contents.  */
1860           if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
1861             {
1862               c_parser_consume_token (parser);
1863               break;
1864             }
1865           /* Accept #pragmas at struct scope.  */
1866           if (c_parser_next_token_is (parser, CPP_PRAGMA))
1867             {
1868               c_parser_pragma (parser, pragma_external);
1869               continue;
1870             }
1871           /* Parse some comma-separated declarations, but not the
1872              trailing semicolon if any.  */
1873           decls = c_parser_struct_declaration (parser);
1874           contents = chainon (decls, contents);
1875           /* If no semicolon follows, either we have a parse error or
1876              are at the end of the struct or union and should
1877              pedwarn.  */
1878           if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1879             c_parser_consume_token (parser);
1880           else
1881             {
1882               if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
1883                 pedwarn ("no semicolon at end of struct or union");
1884               else
1885                 {
1886                   c_parser_error (parser, "expected %<;%>");
1887                   c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
1888                   break;
1889                 }
1890             }
1891         }
1892       postfix_attrs = c_parser_attributes (parser);
1893       ret.spec = finish_struct (type, nreverse (contents),
1894                                 chainon (attrs, postfix_attrs));
1895       ret.kind = ctsk_tagdef;
1896       return ret;
1897     }
1898   else if (!ident)
1899     {
1900       c_parser_error (parser, "expected %<{%>");
1901       ret.spec = error_mark_node;
1902       ret.kind = ctsk_tagref;
1903       return ret;
1904     }
1905   ret = parser_xref_tag (code, ident);
1906   return ret;
1907 }
1908
1909 /* Parse a struct-declaration (C90 6.5.2.1, C99 6.7.2.1), *without*
1910    the trailing semicolon.
1911
1912    struct-declaration:
1913      specifier-qualifier-list struct-declarator-list
1914
1915    specifier-qualifier-list:
1916      type-specifier specifier-qualifier-list[opt]
1917      type-qualifier specifier-qualifier-list[opt]
1918      attributes specifier-qualifier-list[opt]
1919
1920    struct-declarator-list:
1921      struct-declarator
1922      struct-declarator-list , attributes[opt] struct-declarator
1923
1924    struct-declarator:
1925      declarator attributes[opt]
1926      declarator[opt] : constant-expression attributes[opt]
1927
1928    GNU extensions:
1929
1930    struct-declaration:
1931      __extension__ struct-declaration
1932      specifier-qualifier-list
1933
1934    Unlike the ISO C syntax, semicolons are handled elsewhere.  The use
1935    of attributes where shown is a GNU extension.  In GNU C, we accept
1936    any expression without commas in the syntax (assignment
1937    expressions, not just conditional expressions); assignment
1938    expressions will be diagnosed as non-constant.  */
1939
1940 static tree
1941 c_parser_struct_declaration (c_parser *parser)
1942 {
1943   struct c_declspecs *specs;
1944   tree prefix_attrs;
1945   tree all_prefix_attrs;
1946   tree decls;
1947   if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
1948     {
1949       int ext;
1950       tree decl;
1951       ext = disable_extension_diagnostics ();
1952       c_parser_consume_token (parser);
1953       decl = c_parser_struct_declaration (parser);
1954       restore_extension_diagnostics (ext);
1955       return decl;
1956     }
1957   specs = build_null_declspecs ();
1958   c_parser_declspecs (parser, specs, false, true, true);
1959   if (parser->error)
1960     return NULL_TREE;
1961   if (!specs->declspecs_seen_p)
1962     {
1963       c_parser_error (parser, "expected specifier-qualifier-list");
1964       return NULL_TREE;
1965     }
1966   finish_declspecs (specs);
1967   if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1968     {
1969       tree ret;
1970       if (!specs->type_seen_p)
1971         {
1972           if (pedantic)
1973             pedwarn ("ISO C forbids member declarations with no members");
1974           shadow_tag_warned (specs, pedantic);
1975           ret = NULL_TREE;
1976         }
1977       else
1978         {
1979           /* Support for unnamed structs or unions as members of
1980              structs or unions (which is [a] useful and [b] supports
1981              MS P-SDK).  */
1982           ret = grokfield (build_id_declarator (NULL_TREE), specs, NULL_TREE);
1983         }
1984       return ret;
1985     }
1986   pending_xref_error ();
1987   prefix_attrs = specs->attrs;
1988   all_prefix_attrs = prefix_attrs;
1989   specs->attrs = NULL_TREE;
1990   decls = NULL_TREE;
1991   while (true)
1992     {
1993       /* Declaring one or more declarators or un-named bit-fields.  */
1994       struct c_declarator *declarator;
1995       bool dummy = false;
1996       if (c_parser_next_token_is (parser, CPP_COLON))
1997         declarator = build_id_declarator (NULL_TREE);
1998       else
1999         declarator = c_parser_declarator (parser, specs->type_seen_p,
2000                                           C_DTR_NORMAL, &dummy);
2001       if (declarator == NULL)
2002         {
2003           c_parser_skip_to_end_of_block_or_statement (parser);
2004           break;
2005         }
2006       if (c_parser_next_token_is (parser, CPP_COLON)
2007           || c_parser_next_token_is (parser, CPP_COMMA)
2008           || c_parser_next_token_is (parser, CPP_SEMICOLON)
2009           || c_parser_next_token_is (parser, CPP_CLOSE_BRACE)
2010           || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2011         {
2012           tree postfix_attrs = NULL_TREE;
2013           tree width = NULL_TREE;
2014           tree d;
2015           if (c_parser_next_token_is (parser, CPP_COLON))
2016             {
2017               c_parser_consume_token (parser);
2018               width = c_parser_expr_no_commas (parser, NULL).value;
2019             }
2020           if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2021             postfix_attrs = c_parser_attributes (parser);
2022           d = grokfield (declarator, specs, width);
2023           decl_attributes (&d, chainon (postfix_attrs,
2024                                         all_prefix_attrs), 0);
2025           TREE_CHAIN (d) = decls;
2026           decls = d;
2027           if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2028             all_prefix_attrs = chainon (c_parser_attributes (parser),
2029                                         prefix_attrs);
2030           else
2031             all_prefix_attrs = prefix_attrs;
2032           if (c_parser_next_token_is (parser, CPP_COMMA))
2033             c_parser_consume_token (parser);
2034           else if (c_parser_next_token_is (parser, CPP_SEMICOLON)
2035                    || c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2036             {
2037               /* Semicolon consumed in caller.  */
2038               break;
2039             }
2040           else
2041             {
2042               c_parser_error (parser, "expected %<,%>, %<;%> or %<}%>");
2043               break;
2044             }
2045         }
2046       else
2047         {
2048           c_parser_error (parser,
2049                           "expected %<:%>, %<,%>, %<;%>, %<}%> or "
2050                           "%<__attribute__%>");
2051           break;
2052         }
2053     }
2054   return decls;
2055 }
2056
2057 /* Parse a typeof specifier (a GNU extension).
2058
2059    typeof-specifier:
2060      typeof ( expression )
2061      typeof ( type-name )
2062 */
2063
2064 static struct c_typespec
2065 c_parser_typeof_specifier (c_parser *parser)
2066 {
2067   struct c_typespec ret;
2068   ret.kind = ctsk_typeof;
2069   ret.spec = error_mark_node;
2070   gcc_assert (c_parser_next_token_is_keyword (parser, RID_TYPEOF));
2071   c_parser_consume_token (parser);
2072   skip_evaluation++;
2073   in_typeof++;
2074   if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2075     {
2076       skip_evaluation--;
2077       in_typeof--;
2078       return ret;
2079     }
2080   if (c_parser_next_token_starts_typename (parser))
2081     {
2082       struct c_type_name *type = c_parser_type_name (parser);
2083       skip_evaluation--;
2084       in_typeof--;
2085       if (type != NULL)
2086         {
2087           ret.spec = groktypename (type);
2088           pop_maybe_used (variably_modified_type_p (ret.spec, NULL_TREE));
2089         }
2090     }
2091   else
2092     {
2093       struct c_expr expr = c_parser_expression (parser);
2094       skip_evaluation--;
2095       in_typeof--;
2096       if (TREE_CODE (expr.value) == COMPONENT_REF
2097           && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1)))
2098         error ("%<typeof%> applied to a bit-field");
2099       ret.spec = TREE_TYPE (expr.value);
2100       pop_maybe_used (variably_modified_type_p (ret.spec, NULL_TREE));
2101     }
2102   c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
2103   return ret;
2104 }
2105
2106 /* Parse a declarator, possibly an abstract declarator (C90 6.5.4,
2107    6.5.5, C99 6.7.5, 6.7.6).  If TYPE_SEEN_P then a typedef name may
2108    be redeclared; otherwise it may not.  KIND indicates which kind of
2109    declarator is wanted.  Returns a valid declarator except in the
2110    case of a syntax error in which case NULL is returned.  *SEEN_ID is
2111    set to true if an identifier being declared is seen; this is used
2112    to diagnose bad forms of abstract array declarators and to
2113    determine whether an identifier list is syntactically permitted.
2114
2115    declarator:
2116      pointer[opt] direct-declarator
2117
2118    direct-declarator:
2119      identifier
2120      ( attributes[opt] declarator )
2121      direct-declarator array-declarator
2122      direct-declarator ( parameter-type-list )
2123      direct-declarator ( identifier-list[opt] )
2124
2125    pointer:
2126      * type-qualifier-list[opt]
2127      * type-qualifier-list[opt] pointer
2128
2129    type-qualifier-list:
2130      type-qualifier
2131      attributes
2132      type-qualifier-list type-qualifier
2133      type-qualifier-list attributes
2134
2135    parameter-type-list:
2136      parameter-list
2137      parameter-list , ...
2138
2139    parameter-list:
2140      parameter-declaration
2141      parameter-list , parameter-declaration
2142
2143    parameter-declaration:
2144      declaration-specifiers declarator attributes[opt]
2145      declaration-specifiers abstract-declarator[opt] attributes[opt]
2146
2147    identifier-list:
2148      identifier
2149      identifier-list , identifier
2150
2151    abstract-declarator:
2152      pointer
2153      pointer[opt] direct-abstract-declarator
2154
2155    direct-abstract-declarator:
2156      ( attributes[opt] abstract-declarator )
2157      direct-abstract-declarator[opt] array-declarator
2158      direct-abstract-declarator[opt] ( parameter-type-list[opt] )
2159
2160    GNU extensions:
2161
2162    direct-declarator:
2163      direct-declarator ( parameter-forward-declarations
2164                          parameter-type-list[opt] )
2165
2166    direct-abstract-declarator:
2167      direct-abstract-declarator[opt] ( parameter-forward-declarations 
2168                                        parameter-type-list[opt] )
2169
2170    parameter-forward-declarations:
2171      parameter-list ;
2172      parameter-forward-declarations parameter-list ;
2173
2174    The uses of attributes shown above are GNU extensions.
2175
2176    Some forms of array declarator are not included in C99 in the
2177    syntax for abstract declarators; these are disallowed elsewhere.
2178    This may be a defect (DR#289).
2179
2180    This function also accepts an omitted abstract declarator as being
2181    an abstract declarator, although not part of the formal syntax.  */
2182
2183 static struct c_declarator *
2184 c_parser_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
2185                      bool *seen_id)
2186 {
2187   /* Parse any initial pointer part.  */
2188   if (c_parser_next_token_is (parser, CPP_MULT))
2189     {
2190       struct c_declspecs *quals_attrs = build_null_declspecs ();
2191       struct c_declarator *inner;
2192       c_parser_consume_token (parser);
2193       c_parser_declspecs (parser, quals_attrs, false, false, true);
2194       inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
2195       if (inner == NULL)
2196         return NULL;
2197       else
2198         return make_pointer_declarator (quals_attrs, inner);
2199     }
2200   /* Now we have a direct declarator, direct abstract declarator or
2201      nothing (which counts as a direct abstract declarator here).  */
2202   return c_parser_direct_declarator (parser, type_seen_p, kind, seen_id);
2203 }
2204
2205 /* Parse a direct declarator or direct abstract declarator; arguments
2206    as c_parser_declarator.  */
2207
2208 static struct c_declarator *
2209 c_parser_direct_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
2210                             bool *seen_id)
2211 {
2212   /* The direct declarator must start with an identifier (possibly
2213      omitted) or a parenthesized declarator (possibly abstract).  In
2214      an ordinary declarator, initial parentheses must start a
2215      parenthesized declarator.  In an abstract declarator or parameter
2216      declarator, they could start a parenthesized declarator or a
2217      parameter list.  To tell which, the open parenthesis and any
2218      following attributes must be read.  If a declaration specifier
2219      follows, then it is a parameter list; if the specifier is a
2220      typedef name, there might be an ambiguity about redeclaring it,
2221      which is resolved in the direction of treating it as a typedef
2222      name.  If a close parenthesis follows, it is also an empty
2223      parameter list, as the syntax does not permit empty abstract
2224      declarators.  Otherwise, it is a parenthesized declarator (in
2225      which case the analysis may be repeated inside it, recursively).
2226
2227      ??? There is an ambiguity in a parameter declaration "int
2228      (__attribute__((foo)) x)", where x is not a typedef name: it
2229      could be an abstract declarator for a function, or declare x with
2230      parentheses.  The proper resolution of this ambiguity needs
2231      documenting.  At present we follow an accident of the old
2232      parser's implementation, whereby the first parameter must have
2233      some declaration specifiers other than just attributes.  Thus as
2234      a parameter declaration it is treated as a parenthesized
2235      parameter named x, and as an abstract declarator it is
2236      rejected.
2237
2238      ??? Also following the old parser, attributes inside an empty
2239      parameter list are ignored, making it a list not yielding a
2240      prototype, rather than giving an error or making it have one
2241      parameter with implicit type int.
2242
2243      ??? Also following the old parser, typedef names may be
2244      redeclared in declarators, but not Objective-C class names.  */
2245
2246   if (kind != C_DTR_ABSTRACT
2247       && c_parser_next_token_is (parser, CPP_NAME)
2248       && ((type_seen_p
2249            && c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME)
2250           || c_parser_peek_token (parser)->id_kind == C_ID_ID))
2251     {
2252       struct c_declarator *inner
2253         = build_id_declarator (c_parser_peek_token (parser)->value);
2254       *seen_id = true;
2255       inner->id_loc = c_parser_peek_token (parser)->location;
2256       c_parser_consume_token (parser);
2257       return c_parser_direct_declarator_inner (parser, *seen_id, inner);
2258     }
2259
2260   if (kind != C_DTR_NORMAL
2261       && c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
2262     {
2263       struct c_declarator *inner = build_id_declarator (NULL_TREE);
2264       return c_parser_direct_declarator_inner (parser, *seen_id, inner);
2265     }
2266
2267   /* Either we are at the end of an abstract declarator, or we have
2268      parentheses.  */
2269
2270   if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
2271     {
2272       tree attrs;
2273       struct c_declarator *inner;
2274       c_parser_consume_token (parser);
2275       attrs = c_parser_attributes (parser);
2276       if (kind != C_DTR_NORMAL
2277           && (c_parser_next_token_starts_declspecs (parser)
2278               || c_parser_next_token_is (parser, CPP_CLOSE_PAREN)))
2279         {
2280           struct c_arg_info *args
2281             = c_parser_parms_declarator (parser, kind == C_DTR_NORMAL,
2282                                          attrs);
2283           if (args == NULL)
2284             return NULL;
2285           else
2286             {
2287               inner
2288                 = build_function_declarator (args,
2289                                              build_id_declarator (NULL_TREE));
2290               return c_parser_direct_declarator_inner (parser, *seen_id,
2291                                                        inner);
2292             }
2293         }
2294       /* A parenthesized declarator.  */
2295       inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
2296       if (inner != NULL && attrs != NULL)
2297         inner = build_attrs_declarator (attrs, inner);
2298       if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2299         {
2300           c_parser_consume_token (parser);
2301           if (inner == NULL)
2302             return NULL;
2303           else
2304             return c_parser_direct_declarator_inner (parser, *seen_id, inner);
2305         }
2306       else
2307         {
2308           c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2309                                      "expected %<)%>");
2310           return NULL;
2311         }
2312     }
2313   else
2314     {
2315       if (kind == C_DTR_NORMAL)
2316         {
2317           c_parser_error (parser, "expected identifier or %<(%>");
2318           return NULL;
2319         }
2320       else
2321         return build_id_declarator (NULL_TREE);
2322     }
2323 }
2324
2325 /* Parse part of a direct declarator or direct abstract declarator,
2326    given that some (in INNER) has already been parsed; ID_PRESENT is
2327    true if an identifier is present, false for an abstract
2328    declarator.  */
2329
2330 static struct c_declarator *
2331 c_parser_direct_declarator_inner (c_parser *parser, bool id_present,
2332                                   struct c_declarator *inner)
2333 {
2334   /* Parse a sequence of array declarators and parameter lists.  */
2335   if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
2336     {
2337       struct c_declarator *declarator;
2338       struct c_declspecs *quals_attrs = build_null_declspecs ();
2339       bool static_seen;
2340       bool star_seen;
2341       tree dimen;
2342       c_parser_consume_token (parser);
2343       c_parser_declspecs (parser, quals_attrs, false, false, true);
2344       static_seen = c_parser_next_token_is_keyword (parser, RID_STATIC);
2345       if (static_seen)
2346         c_parser_consume_token (parser);
2347       if (static_seen && !quals_attrs->declspecs_seen_p)
2348         c_parser_declspecs (parser, quals_attrs, false, false, true);
2349       if (!quals_attrs->declspecs_seen_p)
2350         quals_attrs = NULL;
2351       /* If "static" is present, there must be an array dimension.
2352          Otherwise, there may be a dimension, "*", or no
2353          dimension.  */
2354       if (static_seen)
2355         {
2356           star_seen = false;
2357           dimen = c_parser_expr_no_commas (parser, NULL).value;
2358         }
2359       else
2360         {
2361           if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
2362             {
2363               dimen = NULL_TREE;
2364               star_seen = false;
2365             }
2366           else if (c_parser_next_token_is (parser, CPP_MULT))
2367             {
2368               if (c_parser_peek_2nd_token (parser)->type == CPP_CLOSE_SQUARE)
2369                 {
2370                   dimen = NULL_TREE;
2371                   star_seen = true;
2372                   c_parser_consume_token (parser);
2373                 }
2374               else
2375                 {
2376                   star_seen = false;
2377                   dimen = c_parser_expr_no_commas (parser, NULL).value;
2378                 }
2379             }
2380           else
2381             {
2382               star_seen = false;
2383               dimen = c_parser_expr_no_commas (parser, NULL).value;
2384             }
2385         }
2386       if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
2387         c_parser_consume_token (parser);
2388       else
2389         {
2390           c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
2391                                      "expected %<]%>");
2392           return NULL;
2393         }
2394       declarator = build_array_declarator (dimen, quals_attrs, static_seen,
2395                                            star_seen);
2396       inner = set_array_declarator_inner (declarator, inner, !id_present);
2397       return c_parser_direct_declarator_inner (parser, id_present, inner);
2398     }
2399   else if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
2400     {
2401       tree attrs;
2402       struct c_arg_info *args;
2403       c_parser_consume_token (parser);
2404       attrs = c_parser_attributes (parser);
2405       args = c_parser_parms_declarator (parser, id_present, attrs);
2406       if (args == NULL)
2407         return NULL;
2408       else
2409         {
2410           inner = build_function_declarator (args, inner);
2411           return c_parser_direct_declarator_inner (parser, id_present, inner);
2412         }
2413     }
2414   return inner;
2415 }
2416
2417 /* Parse a parameter list or identifier list, including the closing
2418    parenthesis but not the opening one.  ATTRS are the attributes at
2419    the start of the list.  ID_LIST_OK is true if an identifier list is
2420    acceptable; such a list must not have attributes at the start.  */
2421
2422 static struct c_arg_info *
2423 c_parser_parms_declarator (c_parser *parser, bool id_list_ok, tree attrs)
2424 {
2425   push_scope ();
2426   declare_parm_level ();
2427   /* If the list starts with an identifier, it is an identifier list.
2428      Otherwise, it is either a prototype list or an empty list.  */
2429   if (id_list_ok
2430       && !attrs
2431       && c_parser_next_token_is (parser, CPP_NAME)
2432       && c_parser_peek_token (parser)->id_kind == C_ID_ID)
2433     {
2434       tree list = NULL_TREE, *nextp = &list;
2435       while (c_parser_next_token_is (parser, CPP_NAME)
2436              && c_parser_peek_token (parser)->id_kind == C_ID_ID)
2437         {
2438           *nextp = build_tree_list (NULL_TREE,
2439                                     c_parser_peek_token (parser)->value);
2440           nextp = & TREE_CHAIN (*nextp);
2441           c_parser_consume_token (parser);
2442           if (c_parser_next_token_is_not (parser, CPP_COMMA))
2443             break;
2444           c_parser_consume_token (parser);
2445           if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2446             {
2447               c_parser_error (parser, "expected identifier");
2448               break;
2449             }
2450         }
2451       if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2452         {
2453           struct c_arg_info *ret = XOBNEW (&parser_obstack, struct c_arg_info);
2454           ret->parms = 0;
2455           ret->tags = 0;
2456           ret->types = list;
2457           ret->others = 0;
2458           c_parser_consume_token (parser);
2459           pop_scope ();
2460           return ret;
2461         }
2462       else
2463         {
2464           c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2465                                      "expected %<)%>");
2466           pop_scope ();
2467           return NULL;
2468         }
2469     }
2470   else
2471     {
2472       struct c_arg_info *ret = c_parser_parms_list_declarator (parser, attrs);
2473       pop_scope ();
2474       return ret;
2475     }
2476 }
2477
2478 /* Parse a parameter list (possibly empty), including the closing
2479    parenthesis but not the opening one.  ATTRS are the attributes at
2480    the start of the list.  */
2481
2482 static struct c_arg_info *
2483 c_parser_parms_list_declarator (c_parser *parser, tree attrs)
2484 {
2485   bool good_parm = false;
2486   /* ??? Following the old parser, forward parameter declarations may
2487      use abstract declarators, and if no real parameter declarations
2488      follow the forward declarations then this is not diagnosed.  Also
2489      note as above that attributes are ignored as the only contents of
2490      the parentheses, or as the only contents after forward
2491      declarations.  */
2492   if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2493     {
2494       struct c_arg_info *ret = XOBNEW (&parser_obstack, struct c_arg_info);
2495       ret->parms = 0;
2496       ret->tags = 0;
2497       ret->types = 0;
2498       ret->others = 0;
2499       c_parser_consume_token (parser);
2500       return ret;
2501     }
2502   if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
2503     {
2504       struct c_arg_info *ret = XOBNEW (&parser_obstack, struct c_arg_info);
2505       ret->parms = 0;
2506       ret->tags = 0;
2507       ret->others = 0;
2508       /* Suppress -Wold-style-definition for this case.  */
2509       ret->types = error_mark_node;
2510       error ("ISO C requires a named argument before %<...%>");
2511       c_parser_consume_token (parser);
2512       if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2513         {
2514           c_parser_consume_token (parser);
2515           return ret;
2516         }
2517       else
2518         {
2519           c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2520                                      "expected %<)%>");
2521           return NULL;
2522         }
2523     }
2524   /* Nonempty list of parameters, either terminated with semicolon
2525      (forward declarations; recurse) or with close parenthesis (normal
2526      function) or with ", ... )" (variadic function).  */
2527   while (true)
2528     {
2529       /* Parse a parameter.  */
2530       struct c_parm *parm = c_parser_parameter_declaration (parser, attrs);
2531       attrs = NULL_TREE;
2532       if (parm != NULL)
2533         {
2534           good_parm = true;
2535           push_parm_decl (parm);
2536         }
2537       if (c_parser_next_token_is (parser, CPP_SEMICOLON))
2538         {
2539           tree new_attrs;
2540           c_parser_consume_token (parser);
2541           mark_forward_parm_decls ();
2542           new_attrs = c_parser_attributes (parser);
2543           return c_parser_parms_list_declarator (parser, new_attrs);
2544         }
2545       if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2546         {
2547           c_parser_consume_token (parser);
2548           if (good_parm)
2549             return get_parm_info (false);
2550           else
2551             {
2552               struct c_arg_info *ret
2553                 = XOBNEW (&parser_obstack, struct c_arg_info);
2554               ret->parms = 0;
2555               ret->tags = 0;
2556               ret->types = 0;
2557               ret->others = 0;
2558               return ret;
2559             }
2560         }
2561       if (!c_parser_require (parser, CPP_COMMA,
2562                              "expected %<;%>, %<,%> or %<)%>"))
2563         {
2564           c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
2565           return NULL;
2566         }
2567       if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
2568         {
2569           c_parser_consume_token (parser);
2570           if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2571             {
2572               c_parser_consume_token (parser);
2573               if (good_parm)
2574                 return get_parm_info (true);
2575               else
2576                 {
2577                   struct c_arg_info *ret
2578                     = XOBNEW (&parser_obstack, struct c_arg_info);
2579                   ret->parms = 0;
2580                   ret->tags = 0;
2581                   ret->types = 0;
2582                   ret->others = 0;
2583                   return ret;
2584                 }
2585             }
2586           else
2587             {
2588               c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2589                                          "expected %<)%>");
2590               return NULL;
2591             }
2592         }
2593     }
2594 }
2595
2596 /* Parse a parameter declaration.  ATTRS are the attributes at the
2597    start of the declaration if it is the first parameter.  */
2598
2599 static struct c_parm *
2600 c_parser_parameter_declaration (c_parser *parser, tree attrs)
2601 {
2602   struct c_declspecs *specs;
2603   struct c_declarator *declarator;
2604   tree prefix_attrs;
2605   tree postfix_attrs = NULL_TREE;
2606   bool dummy = false;
2607   if (!c_parser_next_token_starts_declspecs (parser))
2608     {
2609       /* ??? In some Objective-C cases '...' isn't applicable so there
2610          should be a different message.  */
2611       c_parser_error (parser,
2612                       "expected declaration specifiers or %<...%>");
2613       c_parser_skip_to_end_of_parameter (parser);
2614       return NULL;
2615     }
2616   specs = build_null_declspecs ();
2617   if (attrs)
2618     {
2619       declspecs_add_attrs (specs, attrs);
2620       attrs = NULL_TREE;
2621     }
2622   c_parser_declspecs (parser, specs, true, true, true);
2623   finish_declspecs (specs);
2624   pending_xref_error ();
2625   prefix_attrs = specs->attrs;
2626   specs->attrs = NULL_TREE;
2627   declarator = c_parser_declarator (parser, specs->type_seen_p,
2628                                     C_DTR_PARM, &dummy);
2629   if (declarator == NULL)
2630     {
2631       c_parser_skip_until_found (parser, CPP_COMMA, NULL);
2632       return NULL;
2633     }
2634   if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2635     postfix_attrs = c_parser_attributes (parser);
2636   return build_c_parm (specs, chainon (postfix_attrs, prefix_attrs),
2637                        declarator);
2638 }
2639
2640 /* Parse a string literal in an asm expression.  It should not be
2641    translated, and wide string literals are an error although
2642    permitted by the syntax.  This is a GNU extension.
2643
2644    asm-string-literal:
2645      string-literal
2646
2647    ??? At present, following the old parser, the caller needs to have
2648    set c_lex_string_translate to 0.  It would be better to follow the
2649    C++ parser rather than using the c_lex_string_translate kludge.  */
2650
2651 static tree
2652 c_parser_asm_string_literal (c_parser *parser)
2653 {
2654   tree str;
2655   if (c_parser_next_token_is (parser, CPP_STRING))
2656     {
2657       str = c_parser_peek_token (parser)->value;
2658       c_parser_consume_token (parser);
2659     }
2660   else if (c_parser_next_token_is (parser, CPP_WSTRING))
2661     {
2662       error ("wide string literal in %<asm%>");
2663       str = build_string (1, "");
2664       c_parser_consume_token (parser);
2665     }
2666   else
2667     {
2668       c_parser_error (parser, "expected string literal");
2669       str = NULL_TREE;
2670     }
2671   return str;
2672 }
2673
2674 /* Parse a simple asm expression.  This is used in restricted
2675    contexts, where a full expression with inputs and outputs does not
2676    make sense.  This is a GNU extension.
2677
2678    simple-asm-expr:
2679      asm ( asm-string-literal )
2680 */
2681
2682 static tree
2683 c_parser_simple_asm_expr (c_parser *parser)
2684 {
2685   tree str;
2686   gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM));
2687   /* ??? Follow the C++ parser rather than using the
2688      c_lex_string_translate kludge.  */
2689   c_lex_string_translate = 0;
2690   c_parser_consume_token (parser);
2691   if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2692     {
2693       c_lex_string_translate = 1;
2694       return NULL_TREE;
2695     }
2696   str = c_parser_asm_string_literal (parser);
2697   c_lex_string_translate = 1;
2698   if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
2699     {
2700       c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
2701       return NULL_TREE;
2702     }
2703   return str;
2704 }
2705
2706 /* Parse (possibly empty) attributes.  This is a GNU extension.
2707
2708    attributes:
2709      empty
2710      attributes attribute
2711
2712    attribute:
2713      __attribute__ ( ( attribute-list ) )
2714
2715    attribute-list:
2716      attrib
2717      attribute_list , attrib
2718
2719    attrib:
2720      empty
2721      any-word
2722      any-word ( identifier )
2723      any-word ( identifier , nonempty-expr-list )
2724      any-word ( expr-list )
2725
2726    where the "identifier" must not be declared as a type, and
2727    "any-word" may be any identifier (including one declared as a
2728    type), a reserved word storage class specifier, type specifier or
2729    type qualifier.  ??? This still leaves out most reserved keywords
2730    (following the old parser), shouldn't we include them, and why not
2731    allow identifiers declared as types to start the arguments?  */
2732
2733 static tree
2734 c_parser_attributes (c_parser *parser)
2735 {
2736   tree attrs = NULL_TREE;
2737   while (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2738     {
2739       /* ??? Follow the C++ parser rather than using the
2740          c_lex_string_translate kludge.  */
2741       c_lex_string_translate = 0;
2742       c_parser_consume_token (parser);
2743       if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2744         {
2745           c_lex_string_translate = 1;
2746           return attrs;
2747         }
2748       if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2749         {
2750           c_lex_string_translate = 1;
2751           c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
2752           return attrs;
2753         }
2754       /* Parse the attribute list.  */
2755       while (c_parser_next_token_is (parser, CPP_COMMA)
2756              || c_parser_next_token_is (parser, CPP_NAME)
2757              || c_parser_next_token_is (parser, CPP_KEYWORD))
2758         {
2759           tree attr, attr_name, attr_args;
2760           if (c_parser_next_token_is (parser, CPP_COMMA))
2761             {
2762               c_parser_consume_token (parser);
2763               continue;
2764             }
2765           if (c_parser_next_token_is (parser, CPP_KEYWORD))
2766             {
2767               /* ??? See comment above about what keywords are
2768                  accepted here.  */
2769               bool ok;
2770               switch (c_parser_peek_token (parser)->keyword)
2771                 {
2772                 case RID_STATIC:
2773                 case RID_UNSIGNED:
2774                 case RID_LONG:
2775                 case RID_CONST:
2776                 case RID_EXTERN:
2777                 case RID_REGISTER:
2778                 case RID_TYPEDEF:
2779                 case RID_SHORT:
2780                 case RID_INLINE:
2781                 case RID_VOLATILE:
2782                 case RID_SIGNED:
2783                 case RID_AUTO:
2784                 case RID_RESTRICT:
2785                 case RID_COMPLEX:
2786                 case RID_THREAD:
2787                 case RID_INT:
2788                 case RID_CHAR:
2789                 case RID_FLOAT:
2790                 case RID_DOUBLE:
2791                 case RID_VOID:
2792                 case RID_DFLOAT32:
2793                 case RID_DFLOAT64:
2794                 case RID_DFLOAT128:
2795                 case RID_BOOL:
2796                   ok = true;
2797                   break;
2798                 default:
2799                   ok = false;
2800                   break;
2801                 }
2802               if (!ok)
2803                 break;
2804             }
2805           attr_name = c_parser_peek_token (parser)->value;
2806           c_parser_consume_token (parser);
2807           if (c_parser_next_token_is_not (parser, CPP_OPEN_PAREN))
2808             {
2809               attr = build_tree_list (attr_name, NULL_TREE);
2810               attrs = chainon (attrs, attr);
2811               continue;
2812             }
2813           c_parser_consume_token (parser);
2814           /* Parse the attribute contents.  If they start with an
2815              identifier which is followed by a comma or close
2816              parenthesis, then the arguments start with that
2817              identifier; otherwise they are an expression list.  */
2818           if (c_parser_next_token_is (parser, CPP_NAME)
2819               && c_parser_peek_token (parser)->id_kind == C_ID_ID
2820               && ((c_parser_peek_2nd_token (parser)->type == CPP_COMMA)
2821                   || (c_parser_peek_2nd_token (parser)->type
2822                       == CPP_CLOSE_PAREN)))
2823             {
2824               tree arg1 = c_parser_peek_token (parser)->value;
2825               c_parser_consume_token (parser);
2826               if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2827                 attr_args = build_tree_list (NULL_TREE, arg1);
2828               else
2829                 {
2830                   c_parser_consume_token (parser);
2831                   attr_args = tree_cons (NULL_TREE, arg1,
2832                                          c_parser_expr_list (parser, false));
2833                 }
2834             }
2835           else
2836             {
2837               if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2838                 attr_args = NULL_TREE;
2839               else
2840                 attr_args = c_parser_expr_list (parser, false);
2841             }
2842           attr = build_tree_list (attr_name, attr_args);
2843           if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2844             c_parser_consume_token (parser);
2845           else
2846             {
2847               c_lex_string_translate = 1;
2848               c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2849                                          "expected %<)%>");
2850               return attrs;
2851             }
2852           attrs = chainon (attrs, attr);
2853         }
2854       if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2855         c_parser_consume_token (parser);
2856       else
2857         {
2858           c_lex_string_translate = 1;
2859           c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2860                                      "expected %<)%>");
2861           return attrs;
2862         }
2863       if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2864         c_parser_consume_token (parser);
2865       else
2866         {
2867           c_lex_string_translate = 1;
2868           c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2869                                      "expected %<)%>");
2870           return attrs;
2871         }
2872       c_lex_string_translate = 1;
2873     }
2874   return attrs;
2875 }
2876
2877 /* Parse a type name (C90 6.5.5, C99 6.7.6).
2878
2879    type-name:
2880      specifier-qualifier-list abstract-declarator[opt]
2881 */
2882
2883 static struct c_type_name *
2884 c_parser_type_name (c_parser *parser)
2885 {
2886   struct c_declspecs *specs = build_null_declspecs ();
2887   struct c_declarator *declarator;
2888   struct c_type_name *ret;
2889   bool dummy = false;
2890   c_parser_declspecs (parser, specs, false, true, true);
2891   if (!specs->declspecs_seen_p)
2892     {
2893       c_parser_error (parser, "expected specifier-qualifier-list");
2894       return NULL;
2895     }
2896   pending_xref_error ();
2897   finish_declspecs (specs);
2898   declarator = c_parser_declarator (parser, specs->type_seen_p,
2899                                     C_DTR_ABSTRACT, &dummy);
2900   if (declarator == NULL)
2901     return NULL;
2902   ret = XOBNEW (&parser_obstack, struct c_type_name);
2903   ret->specs = specs;
2904   ret->declarator = declarator;
2905   return ret;
2906 }
2907
2908 /* Parse an initializer (C90 6.5.7, C99 6.7.8).
2909
2910    initializer:
2911      assignment-expression
2912      { initializer-list }
2913      { initializer-list , }
2914
2915    initializer-list:
2916      designation[opt] initializer
2917      initializer-list , designation[opt] initializer
2918
2919    designation:
2920      designator-list =
2921
2922    designator-list:
2923      designator
2924      designator-list designator
2925
2926    designator:
2927      array-designator
2928      . identifier
2929
2930    array-designator:
2931      [ constant-expression ]
2932
2933    GNU extensions:
2934
2935    initializer:
2936      { }
2937
2938    designation:
2939      array-designator
2940      identifier :
2941
2942    array-designator:
2943      [ constant-expression ... constant-expression ]
2944
2945    Any expression without commas is accepted in the syntax for the
2946    constant-expressions, with non-constant expressions rejected later.
2947
2948    This function is only used for top-level initializers; for nested
2949    ones, see c_parser_initval.  */
2950
2951 static struct c_expr
2952 c_parser_initializer (c_parser *parser)
2953 {
2954   if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
2955     return c_parser_braced_init (parser, NULL_TREE, false);
2956   else
2957     {
2958       struct c_expr ret;
2959       ret = c_parser_expr_no_commas (parser, NULL);
2960       if (TREE_CODE (ret.value) != STRING_CST
2961           && TREE_CODE (ret.value) != COMPOUND_LITERAL_EXPR)
2962         ret = default_function_array_conversion (ret);
2963       return ret;
2964     }
2965 }
2966
2967 /* Parse a braced initializer list.  TYPE is the type specified for a
2968    compound literal, and NULL_TREE for other initializers and for
2969    nested braced lists.  NESTED_P is true for nested braced lists,
2970    false for the list of a compound literal or the list that is the
2971    top-level initializer in a declaration.  */
2972
2973 static struct c_expr
2974 c_parser_braced_init (c_parser *parser, tree type, bool nested_p)
2975 {
2976   gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE));
2977   c_parser_consume_token (parser);
2978   if (nested_p)
2979     push_init_level (0);
2980   else
2981     really_start_incremental_init (type);
2982   if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2983     {
2984       if (pedantic)
2985         pedwarn ("ISO C forbids empty initializer braces");
2986     }
2987   else
2988     {
2989       /* Parse a non-empty initializer list, possibly with a trailing
2990          comma.  */
2991       while (true)
2992         {
2993           c_parser_initelt (parser);
2994           if (parser->error)
2995             break;
2996           if (c_parser_next_token_is (parser, CPP_COMMA))
2997             c_parser_consume_token (parser);
2998           else
2999             break;
3000           if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3001             break;
3002         }
3003     }
3004   if (c_parser_next_token_is_not (parser, CPP_CLOSE_BRACE))
3005     {
3006       struct c_expr ret;
3007       ret.value = error_mark_node;
3008       ret.original_code = ERROR_MARK;
3009       c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, "expected %<}%>");
3010       return ret;
3011     }
3012   c_parser_consume_token (parser);
3013   return pop_init_level (0);
3014 }
3015
3016 /* Parse a nested initializer, including designators.  */
3017
3018 static void
3019 c_parser_initelt (c_parser *parser)
3020 {
3021   /* Parse any designator or designator list.  A single array
3022      designator may have the subsequent "=" omitted in GNU C, but a
3023      longer list or a structure member designator may not.  */
3024   if (c_parser_next_token_is (parser, CPP_NAME)
3025       && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
3026     {
3027       /* Old-style structure member designator.  */
3028       set_init_label (c_parser_peek_token (parser)->value);
3029       if (pedantic)
3030         pedwarn ("obsolete use of designated initializer with %<:%>");
3031       c_parser_consume_token (parser);
3032       c_parser_consume_token (parser);
3033     }
3034   else
3035     {
3036       /* des_seen is 0 if there have been no designators, 1 if there
3037          has been a single array designator and 2 otherwise.  */
3038       int des_seen = 0;
3039       while (c_parser_next_token_is (parser, CPP_OPEN_SQUARE)
3040              || c_parser_next_token_is (parser, CPP_DOT))
3041         {
3042           int des_prev = des_seen;
3043           if (des_seen < 2)
3044             des_seen++;
3045           if (c_parser_next_token_is (parser, CPP_DOT))
3046             {
3047               des_seen = 2;
3048               c_parser_consume_token (parser);
3049               if (c_parser_next_token_is (parser, CPP_NAME))
3050                 {
3051                   set_init_label (c_parser_peek_token (parser)->value);
3052                   c_parser_consume_token (parser);
3053                 }
3054               else
3055                 {
3056                   struct c_expr init;
3057                   init.value = error_mark_node;
3058                   init.original_code = ERROR_MARK;
3059                   c_parser_error (parser, "expected identifier");
3060                   c_parser_skip_until_found (parser, CPP_COMMA, NULL);
3061                   process_init_element (init);
3062                   return;
3063                 }
3064             }
3065           else
3066             {
3067               tree first, second;
3068               /* ??? Following the old parser, [ objc-receiver
3069                  objc-message-args ] is accepted as an initializer,
3070                  being distinguished from a designator by what follows
3071                  the first assignment expression inside the square
3072                  brackets, but after a first array designator a
3073                  subsequent square bracket is for Objective-C taken to
3074                  start an expression, using the obsolete form of
3075                  designated initializer without '=', rather than
3076                  possibly being a second level of designation: in LALR
3077                  terms, the '[' is shifted rather than reducing
3078                  designator to designator-list.  */
3079               if (des_prev == 1 && c_dialect_objc ())
3080                 {
3081                   des_seen = des_prev;
3082                   break;
3083                 }
3084               if (des_prev == 0 && c_dialect_objc ())
3085                 {
3086                   /* This might be an array designator or an
3087                      Objective-C message expression.  If the former,
3088                      continue parsing here; if the latter, parse the
3089                      remainder of the initializer given the starting
3090                      primary-expression.  ??? It might make sense to
3091                      distinguish when des_prev == 1 as well; see
3092                      previous comment.  */
3093                   tree rec, args;
3094                   struct c_expr mexpr;
3095                   c_parser_consume_token (parser);
3096                   if (c_parser_peek_token (parser)->type == CPP_NAME
3097                       && ((c_parser_peek_token (parser)->id_kind
3098                            == C_ID_TYPENAME)
3099                           || (c_parser_peek_token (parser)->id_kind
3100                               == C_ID_CLASSNAME)))
3101                     {
3102                       /* Type name receiver.  */
3103                       tree id = c_parser_peek_token (parser)->value;
3104                       c_parser_consume_token (parser);
3105                       rec = objc_get_class_reference (id);
3106                       goto parse_message_args;
3107                     }
3108                   first = c_parser_expr_no_commas (parser, NULL).value;
3109                   if (c_parser_next_token_is (parser, CPP_ELLIPSIS)
3110                       || c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3111                     goto array_desig_after_first;
3112                   /* Expression receiver.  So far only one part
3113                      without commas has been parsed; there might be
3114                      more of the expression.  */
3115                   rec = first;
3116                   while (c_parser_next_token_is (parser, CPP_COMMA))
3117                     {
3118                       struct c_expr next;
3119                       c_parser_consume_token (parser);
3120                       next = c_parser_expr_no_commas (parser, NULL);
3121                       next = default_function_array_conversion (next);
3122                       rec = build_compound_expr (rec, next.value);
3123                     }
3124                 parse_message_args:
3125                   /* Now parse the objc-message-args.  */
3126                   args = c_parser_objc_message_args (parser);
3127                   c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
3128                                              "expected %<]%>");
3129                   mexpr.value
3130                     = objc_build_message_expr (build_tree_list (rec, args));
3131                   mexpr.original_code = ERROR_MARK;
3132                   /* Now parse and process the remainder of the
3133                      initializer, starting with this message
3134                      expression as a primary-expression.  */
3135                   c_parser_initval (parser, &mexpr);
3136                   return;
3137                 }
3138               c_parser_consume_token (parser);
3139               first = c_parser_expr_no_commas (parser, NULL).value;
3140             array_desig_after_first:
3141               if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
3142                 {
3143                   c_parser_consume_token (parser);
3144                   second = c_parser_expr_no_commas (parser, NULL).value;
3145                 }
3146               else
3147                 second = NULL_TREE;
3148               if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3149                 {
3150                   c_parser_consume_token (parser);
3151                   set_init_index (first, second);
3152                   if (pedantic && second)
3153                     pedwarn ("ISO C forbids specifying range of "
3154                              "elements to initialize");
3155                 }
3156               else
3157                 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
3158                                            "expected %<]%>");
3159             }
3160         }
3161       if (des_seen >= 1)
3162         {
3163           if (c_parser_next_token_is (parser, CPP_EQ))
3164             {
3165               if (pedantic && !flag_isoc99)
3166                 pedwarn ("ISO C90 forbids specifying subobject to initialize");
3167               c_parser_consume_token (parser);
3168             }
3169           else
3170             {
3171               if (des_seen == 1)
3172                 {
3173                   if (pedantic)
3174                     pedwarn ("obsolete use of designated initializer "
3175                              "without %<=%>");
3176                 }
3177               else
3178                 {
3179                   struct c_expr init;
3180                   init.value = error_mark_node;
3181                   init.original_code = ERROR_MARK;
3182                   c_parser_error (parser, "expected %<=%>");
3183                   c_parser_skip_until_found (parser, CPP_COMMA, NULL);
3184                   process_init_element (init);
3185                   return;
3186                 }
3187             }
3188         }
3189     }
3190   c_parser_initval (parser, NULL);
3191 }
3192
3193 /* Parse a nested initializer; as c_parser_initializer but parses
3194    initializers within braced lists, after any designators have been
3195    applied.  If AFTER is not NULL then it is an Objective-C message
3196    expression which is the primary-expression starting the
3197    initializer.  */
3198
3199 static void
3200 c_parser_initval (c_parser *parser, struct c_expr *after)
3201 {
3202   struct c_expr init;
3203   gcc_assert (!after || c_dialect_objc ());
3204   if (c_parser_next_token_is (parser, CPP_OPEN_BRACE) && !after)
3205     init = c_parser_braced_init (parser, NULL_TREE, true);
3206   else
3207     {
3208       init = c_parser_expr_no_commas (parser, after);
3209       if (init.value != NULL_TREE
3210           && TREE_CODE (init.value) != STRING_CST
3211           && TREE_CODE (init.value) != COMPOUND_LITERAL_EXPR)
3212         init = default_function_array_conversion (init);
3213     }
3214   process_init_element (init);
3215 }
3216
3217 /* Parse a compound statement (possibly a function body) (C90 6.6.2,
3218    C99 6.8.2).
3219
3220    compound-statement:
3221      { block-item-list[opt] }
3222      { label-declarations block-item-list }
3223
3224    block-item-list:
3225      block-item
3226      block-item-list block-item
3227
3228    block-item:
3229      nested-declaration
3230      statement
3231
3232    nested-declaration:
3233      declaration
3234
3235    GNU extensions:
3236
3237    compound-statement:
3238      { label-declarations block-item-list }
3239
3240    nested-declaration:
3241      __extension__ nested-declaration
3242      nested-function-definition
3243
3244    label-declarations:
3245      label-declaration
3246      label-declarations label-declaration
3247
3248    label-declaration:
3249      __label__ identifier-list ;
3250
3251    Allowing the mixing of declarations and code is new in C99.  The
3252    GNU syntax also permits (not shown above) labels at the end of
3253    compound statements, which yield an error.  We don't allow labels
3254    on declarations; this might seem like a natural extension, but
3255    there would be a conflict between attributes on the label and
3256    prefix attributes on the declaration.  ??? The syntax follows the
3257    old parser in requiring something after label declarations.
3258    Although they are erroneous if the labels declared aren't defined,
3259    is it useful for the syntax to be this way?  */
3260
3261 static tree
3262 c_parser_compound_statement (c_parser *parser)
3263 {
3264   tree stmt;
3265   if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
3266     return error_mark_node;
3267   stmt = c_begin_compound_stmt (true);
3268   c_parser_compound_statement_nostart (parser);
3269   return c_end_compound_stmt (stmt, true);
3270 }
3271
3272 /* Parse a compound statement except for the opening brace.  This is
3273    used for parsing both compound statements and statement expressions
3274    (which follow different paths to handling the opening).  */
3275
3276 static void
3277 c_parser_compound_statement_nostart (c_parser *parser)
3278 {
3279   bool last_stmt = false;
3280   bool last_label = false;
3281   if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3282     {
3283       c_parser_consume_token (parser);
3284       return;
3285     }
3286   if (c_parser_next_token_is_keyword (parser, RID_LABEL))
3287     {
3288       /* Read zero or more forward-declarations for labels that nested
3289          functions can jump to.  */
3290       while (c_parser_next_token_is_keyword (parser, RID_LABEL))
3291         {
3292           c_parser_consume_token (parser);
3293           /* Any identifiers, including those declared as type names,
3294              are OK here.  */
3295           while (true)
3296             {
3297               tree label;
3298               if (c_parser_next_token_is_not (parser, CPP_NAME))
3299                 {
3300                   c_parser_error (parser, "expected identifier");
3301                   break;
3302                 }
3303               label
3304                 = declare_label (c_parser_peek_token (parser)->value);
3305               C_DECLARED_LABEL_FLAG (label) = 1;
3306               add_stmt (build_stmt (DECL_EXPR, label));
3307               c_parser_consume_token (parser);
3308               if (c_parser_next_token_is (parser, CPP_COMMA))
3309                 c_parser_consume_token (parser);
3310               else
3311                 break;
3312             }
3313           c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
3314         }
3315       /* ??? Locating this diagnostic on the token after the
3316          declarations end follows the old parser, but it might be
3317          better to locate it where the declarations start instead.  */
3318       if (pedantic)
3319         pedwarn ("ISO C forbids label declarations");
3320     }
3321   /* We must now have at least one statement, label or declaration.  */
3322   if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3323     {
3324       c_parser_error (parser, "expected declaration or statement");
3325       c_parser_consume_token (parser);
3326       return;
3327     }
3328   while (c_parser_next_token_is_not (parser, CPP_CLOSE_BRACE))
3329     {
3330       location_t loc = c_parser_peek_token (parser)->location;
3331       if (c_parser_next_token_is_keyword (parser, RID_CASE)
3332           || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
3333           || (c_parser_next_token_is (parser, CPP_NAME)
3334               && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
3335         {
3336           last_label = true;
3337           last_stmt = false;
3338           c_parser_label (parser);
3339         }
3340       else if (!last_label
3341                && c_parser_next_token_starts_declspecs (parser))
3342         {
3343           last_label = false;
3344           c_parser_declaration_or_fndef (parser, true, true, true, true);
3345           if (last_stmt
3346               && ((pedantic && !flag_isoc99)
3347                   || warn_declaration_after_statement))
3348             pedwarn_c90 ("%HISO C90 forbids mixed declarations and code",
3349                          &loc);
3350           last_stmt = false;
3351         }
3352       else if (!last_label
3353                && c_parser_next_token_is_keyword (parser, RID_EXTENSION))
3354         {
3355           /* __extension__ can start a declaration, but is also an
3356              unary operator that can start an expression.  Consume all
3357              but the last of a possible series of __extension__ to
3358              determine which.  */
3359           while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
3360                  && (c_parser_peek_2nd_token (parser)->keyword
3361                      == RID_EXTENSION))
3362             c_parser_consume_token (parser);
3363           if (c_token_starts_declspecs (c_parser_peek_2nd_token (parser)))
3364             {
3365               int ext;
3366               ext = disable_extension_diagnostics ();
3367               c_parser_consume_token (parser);
3368               last_label = false;
3369               c_parser_declaration_or_fndef (parser, true, true, true, true);
3370               /* Following the old parser, __extension__ does not
3371                  disable this diagnostic.  */
3372               restore_extension_diagnostics (ext);
3373               if (last_stmt
3374                   && ((pedantic && !flag_isoc99)
3375                       || warn_declaration_after_statement))
3376                 pedwarn_c90 ("%HISO C90 forbids mixed declarations and code",
3377                              &loc);
3378               last_stmt = false;
3379             }
3380           else
3381             goto statement;
3382         }
3383       else if (c_parser_next_token_is (parser, CPP_PRAGMA))
3384         {
3385           /* External pragmas, and some omp pragmas, are not associated
3386              with regular c code, and so are not to be considered statements
3387              syntactically.  This ensures that the user doesn't put them
3388              places that would turn into syntax errors if the directive
3389              were ignored.  */
3390           if (c_parser_pragma (parser, pragma_compound))
3391             last_label = false, last_stmt = true;
3392         }
3393       else if (c_parser_next_token_is (parser, CPP_EOF))
3394         {
3395           c_parser_error (parser, "expected declaration or statement");
3396           return;
3397         }
3398       else
3399         {
3400         statement:
3401           last_label = false;
3402           last_stmt = true;
3403           c_parser_statement_after_labels (parser);
3404         }
3405     }
3406   if (last_label)
3407     error ("label at end of compound statement");
3408   c_parser_consume_token (parser);
3409 }
3410
3411 /* Parse a label (C90 6.6.1, C99 6.8.1).
3412
3413    label:
3414      identifier : attributes[opt]
3415      case constant-expression :
3416      default :
3417
3418    GNU extensions:
3419
3420    label:
3421      case constant-expression ... constant-expression :
3422
3423    The use of attributes on labels is a GNU extension.  The syntax in
3424    GNU C accepts any expressions without commas, non-constant
3425    expressions being rejected later.  */
3426
3427 static void
3428 c_parser_label (c_parser *parser)
3429 {
3430   location_t loc1 = c_parser_peek_token (parser)->location;
3431   tree label = NULL_TREE;
3432   if (c_parser_next_token_is_keyword (parser, RID_CASE))
3433     {
3434       tree exp1, exp2;
3435       c_parser_consume_token (parser);
3436       exp1 = c_parser_expr_no_commas (parser, NULL).value;
3437       if (c_parser_next_token_is (parser, CPP_COLON))
3438         {
3439           c_parser_consume_token (parser);
3440           label = do_case (exp1, NULL_TREE);
3441         }
3442       else if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
3443         {
3444           c_parser_consume_token (parser);
3445           exp2 = c_parser_expr_no_commas (parser, NULL).value;
3446           if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
3447             label = do_case (exp1, exp2);
3448         }
3449       else
3450         c_parser_error (parser, "expected %<:%> or %<...%>");
3451     }
3452   else if (c_parser_next_token_is_keyword (parser, RID_DEFAULT))
3453     {
3454       c_parser_consume_token (parser);
3455       if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
3456         label = do_case (NULL_TREE, NULL_TREE);
3457     }
3458   else
3459     {
3460       tree name = c_parser_peek_token (parser)->value;
3461       tree tlab;
3462       location_t loc2;
3463       tree attrs;
3464       gcc_assert (c_parser_next_token_is (parser, CPP_NAME));
3465       c_parser_consume_token (parser);
3466       gcc_assert (c_parser_next_token_is (parser, CPP_COLON));
3467       loc2 = c_parser_peek_token (parser)->location;
3468       c_parser_consume_token (parser);
3469       attrs = c_parser_attributes (parser);
3470       tlab = define_label (loc2, name);
3471       if (tlab)
3472         {
3473           decl_attributes (&tlab, attrs, 0);
3474           label = add_stmt (build_stmt (LABEL_EXPR, tlab));
3475         }
3476     }
3477   if (label)
3478     SET_EXPR_LOCATION (label, loc1);
3479 }
3480
3481 /* Parse a statement (C90 6.6, C99 6.8).
3482
3483    statement:
3484      labeled-statement
3485      compound-statement
3486      expression-statement
3487      selection-statement
3488      iteration-statement
3489      jump-statement
3490
3491    labeled-statement:
3492      label statement
3493
3494    expression-statement:
3495      expression[opt] ;
3496
3497    selection-statement:
3498      if-statement
3499      switch-statement
3500
3501    iteration-statement:
3502      while-statement
3503      do-statement
3504      for-statement
3505
3506    jump-statement:
3507      goto identifier ;
3508      continue ;
3509      break ;
3510      return expression[opt] ;
3511
3512    GNU extensions:
3513
3514    statement:
3515      asm-statement
3516
3517    jump-statement:
3518      goto * expression ;
3519
3520    Objective-C:
3521
3522    statement:
3523      objc-throw-statement
3524      objc-try-catch-statement
3525      objc-synchronized-statement
3526
3527    objc-throw-statement:
3528      @throw expression ;
3529      @throw ;
3530 */
3531
3532 static void
3533 c_parser_statement (c_parser *parser)
3534 {
3535   while (c_parser_next_token_is_keyword (parser, RID_CASE)
3536          || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
3537          || (c_parser_next_token_is (parser, CPP_NAME)
3538              && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
3539     c_parser_label (parser);
3540   c_parser_statement_after_labels (parser);
3541 }
3542
3543 /* Parse a statement, other than a labeled statement.  */
3544
3545 static void
3546 c_parser_statement_after_labels (c_parser *parser)
3547 {
3548   location_t loc = c_parser_peek_token (parser)->location;
3549   tree stmt = NULL_TREE;
3550   switch (c_parser_peek_token (parser)->type)
3551     {
3552     case CPP_OPEN_BRACE:
3553       add_stmt (c_parser_compound_statement (parser));
3554       break;
3555     case CPP_KEYWORD:
3556       switch (c_parser_peek_token (parser)->keyword)
3557         {
3558         case RID_IF:
3559           c_parser_if_statement (parser);
3560           break;
3561         case RID_SWITCH:
3562           c_parser_switch_statement (parser);
3563           break;
3564         case RID_WHILE:
3565           c_parser_while_statement (parser);
3566           break;
3567         case RID_DO:
3568           c_parser_do_statement (parser);
3569           break;
3570         case RID_FOR:
3571           c_parser_for_statement (parser);
3572           break;
3573         case RID_GOTO:
3574           c_parser_consume_token (parser);
3575           if (c_parser_next_token_is (parser, CPP_NAME))
3576             {
3577               stmt = c_finish_goto_label (c_parser_peek_token (parser)->value);
3578               c_parser_consume_token (parser);
3579             }
3580           else if (c_parser_next_token_is (parser, CPP_MULT))
3581             {
3582               c_parser_consume_token (parser);
3583               stmt = c_finish_goto_ptr (c_parser_expression (parser).value);
3584             }
3585           else
3586             c_parser_error (parser, "expected identifier or %<*%>");
3587           goto expect_semicolon;
3588         case RID_CONTINUE:
3589           c_parser_consume_token (parser);
3590           stmt = c_finish_bc_stmt (&c_cont_label, false);
3591           goto expect_semicolon;
3592         case RID_BREAK:
3593           c_parser_consume_token (parser);
3594           stmt = c_finish_bc_stmt (&c_break_label, true);
3595           goto expect_semicolon;
3596         case RID_RETURN:
3597           c_parser_consume_token (parser);
3598           if (c_parser_next_token_is (parser, CPP_SEMICOLON))
3599             {
3600               stmt = c_finish_return (NULL_TREE);
3601               c_parser_consume_token (parser);
3602             }
3603           else
3604             {
3605               stmt = c_finish_return (c_parser_expression_conv (parser).value);
3606               goto expect_semicolon;
3607             }
3608           break;
3609         case RID_ASM:
3610           stmt = c_parser_asm_statement (parser);
3611           break;
3612         case RID_AT_THROW:
3613           gcc_assert (c_dialect_objc ());
3614           c_parser_consume_token (parser);
3615           if (c_parser_next_token_is (parser, CPP_SEMICOLON))
3616             {
3617               stmt = objc_build_throw_stmt (NULL_TREE);
3618               c_parser_consume_token (parser);
3619             }
3620           else
3621             {
3622               stmt
3623                 = objc_build_throw_stmt (c_parser_expression (parser).value);
3624               goto expect_semicolon;
3625             }
3626           break;
3627         case RID_AT_TRY:
3628           gcc_assert (c_dialect_objc ());
3629           c_parser_objc_try_catch_statement (parser);
3630           break;
3631         case RID_AT_SYNCHRONIZED:
3632           gcc_assert (c_dialect_objc ());
3633           c_parser_objc_synchronized_statement (parser);
3634           break;
3635         default:
3636           goto expr_stmt;
3637         }
3638       break;
3639     case CPP_SEMICOLON:
3640       c_parser_consume_token (parser);
3641       break;
3642     case CPP_CLOSE_PAREN:
3643     case CPP_CLOSE_SQUARE:
3644       /* Avoid infinite loop in error recovery:
3645          c_parser_skip_until_found stops at a closing nesting
3646          delimiter without consuming it, but here we need to consume
3647          it to proceed further.  */
3648       c_parser_error (parser, "expected statement");
3649       c_parser_consume_token (parser);
3650       break;
3651     case CPP_PRAGMA:
3652       c_parser_pragma (parser, pragma_stmt);
3653       break;
3654     default:
3655     expr_stmt:
3656       stmt = c_finish_expr_stmt (c_parser_expression_conv (parser).value);
3657     expect_semicolon:
3658       c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
3659       break;
3660     }
3661   /* Two cases cannot and do not have line numbers associated: If stmt
3662      is degenerate, such as "2;", then stmt is an INTEGER_CST, which
3663      cannot hold line numbers.  But that's OK because the statement
3664      will either be changed to a MODIFY_EXPR during gimplification of
3665      the statement expr, or discarded.  If stmt was compound, but
3666      without new variables, we will have skipped the creation of a
3667      BIND and will have a bare STATEMENT_LIST.  But that's OK because
3668      (recursively) all of the component statements should already have
3669      line numbers assigned.  ??? Can we discard no-op statements
3670      earlier?  */
3671   if (stmt && EXPR_P (stmt))
3672     SET_EXPR_LOCATION (stmt, loc);
3673 }
3674
3675 /* Parse a parenthesized condition from an if, do or while statement.
3676
3677    condition:
3678      ( expression )
3679 */
3680 static tree
3681 c_parser_paren_condition (c_parser *parser)
3682 {
3683   location_t loc;
3684   tree cond;
3685   if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3686     return error_mark_node;
3687   loc = c_parser_peek_token (parser)->location;
3688   cond = c_objc_common_truthvalue_conversion
3689     (c_parser_expression_conv (parser).value);
3690   if (EXPR_P (cond))
3691     SET_EXPR_LOCATION (cond, loc);
3692   c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
3693   return cond;
3694 }
3695
3696 /* Parse a statement which is a block in C99.  */
3697
3698 static tree
3699 c_parser_c99_block_statement (c_parser *parser)
3700 {
3701   tree block = c_begin_compound_stmt (flag_isoc99);
3702   c_parser_statement (parser);
3703   return c_end_compound_stmt (block, flag_isoc99);
3704 }
3705
3706 /* Parse the body of an if statement or the else half thereof.  This
3707    is just parsing a statement but (a) it is a block in C99, (b) we
3708    track whether the body is an if statement for the sake of
3709    -Wparentheses warnings, (c) we handle an empty body specially for
3710    the sake of -Wextra warnings.  */
3711
3712 static tree
3713 c_parser_if_body (c_parser *parser, bool *if_p)
3714 {
3715   tree block = c_begin_compound_stmt (flag_isoc99);
3716   while (c_parser_next_token_is_keyword (parser, RID_CASE)
3717          || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
3718          || (c_parser_next_token_is (parser, CPP_NAME)
3719              && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
3720     c_parser_label (parser);
3721   *if_p = c_parser_next_token_is_keyword (parser, RID_IF);
3722   if (extra_warnings && c_parser_next_token_is (parser, CPP_SEMICOLON))
3723     add_stmt (build1 (NOP_EXPR, NULL_TREE, NULL_TREE));
3724   c_parser_statement_after_labels (parser);
3725   return c_end_compound_stmt (block, flag_isoc99);
3726 }
3727
3728 /* Parse an if statement (C90 6.6.4, C99 6.8.4).
3729
3730    if-statement:
3731      if ( expression ) statement
3732      if ( expression ) statement else statement
3733 */
3734
3735 static void
3736 c_parser_if_statement (c_parser *parser)
3737 {
3738   tree block;
3739   location_t loc;
3740   tree cond;
3741   bool first_if = false, second_if = false;
3742   tree first_body, second_body;
3743   gcc_assert (c_parser_next_token_is_keyword (parser, RID_IF));
3744   c_parser_consume_token (parser);
3745   block = c_begin_compound_stmt (flag_isoc99);
3746   loc = c_parser_peek_token (parser)->location;
3747   cond = c_parser_paren_condition (parser);
3748   first_body = c_parser_if_body (parser, &first_if);
3749   if (c_parser_next_token_is_keyword (parser, RID_ELSE))
3750     {
3751       c_parser_consume_token (parser);
3752       second_body = c_parser_if_body (parser, &second_if);
3753     }
3754   else
3755     second_body = NULL_TREE;
3756   c_finish_if_stmt (loc, cond, first_body, second_body, first_if);
3757   add_stmt (c_end_compound_stmt (block, flag_isoc99));
3758 }
3759
3760 /* Parse a switch statement (C90 6.6.4, C99 6.8.4).
3761
3762    switch-statement:
3763      switch (expression) statement
3764 */
3765
3766 static void
3767 c_parser_switch_statement (c_parser *parser)
3768 {
3769   tree block, expr, body, save_break;
3770   gcc_assert (c_parser_next_token_is_keyword (parser, RID_SWITCH));
3771   c_parser_consume_token (parser);
3772   block = c_begin_compound_stmt (flag_isoc99);
3773   if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3774     {
3775       expr = c_parser_expression (parser).value;
3776       c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
3777     }
3778   else
3779     expr = error_mark_node;
3780   c_start_case (expr);
3781   save_break = c_break_label;
3782   c_break_label = NULL_TREE;
3783   body = c_parser_c99_block_statement (parser);
3784   c_finish_case (body);
3785   if (c_break_label)
3786     add_stmt (build1 (LABEL_EXPR, void_type_node, c_break_label));
3787   c_break_label = save_break;
3788   add_stmt (c_end_compound_stmt (block, flag_isoc99));
3789 }
3790
3791 /* Parse a while statement (C90 6.6.5, C99 6.8.5).
3792
3793    while-statement:
3794       while (expression) statement
3795 */
3796
3797 static void
3798 c_parser_while_statement (c_parser *parser)
3799 {
3800   tree block, cond, body, save_break, save_cont;
3801   location_t loc;
3802   gcc_assert (c_parser_next_token_is_keyword (parser, RID_WHILE));
3803   c_parser_consume_token (parser);
3804   block = c_begin_compound_stmt (flag_isoc99);
3805   loc = c_parser_peek_token (parser)->location;
3806   cond = c_parser_paren_condition (parser);
3807   save_break = c_break_label;
3808   c_break_label = NULL_TREE;
3809   save_cont = c_cont_label;
3810   c_cont_label = NULL_TREE;
3811   body = c_parser_c99_block_statement (parser);
3812   c_finish_loop (loc, cond, NULL, body, c_break_label, c_cont_label, true);
3813   add_stmt (c_end_compound_stmt (block, flag_isoc99));
3814   c_break_label = save_break;
3815   c_cont_label = save_cont;
3816 }
3817
3818 /* Parse a do statement (C90 6.6.5, C99 6.8.5).
3819
3820    do-statement:
3821      do statement while ( expression ) ;
3822 */
3823
3824 static void
3825 c_parser_do_statement (c_parser *parser)
3826 {
3827   tree block, cond, body, save_break, save_cont, new_break, new_cont;
3828   location_t loc;
3829   gcc_assert (c_parser_next_token_is_keyword (parser, RID_DO));
3830   c_parser_consume_token (parser);
3831   block = c_begin_compound_stmt (flag_isoc99);
3832   loc = c_parser_peek_token (parser)->location;
3833   save_break = c_break_label;
3834   c_break_label = NULL_TREE;
3835   save_cont = c_cont_label;
3836   c_cont_label = NULL_TREE;
3837   body = c_parser_c99_block_statement (parser);
3838   c_parser_require_keyword (parser, RID_WHILE, "expected %<while%>");
3839   new_break = c_break_label;
3840   c_break_label = save_break;
3841   new_cont = c_cont_label;
3842   c_cont_label = save_cont;
3843   cond = c_parser_paren_condition (parser);
3844   if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
3845     c_parser_skip_to_end_of_block_or_statement (parser);
3846   c_finish_loop (loc, cond, NULL, body, new_break, new_cont, false);
3847   add_stmt (c_end_compound_stmt (block, flag_isoc99));
3848 }
3849
3850 /* Parse a for statement (C90 6.6.5, C99 6.8.5).
3851
3852    for-statement:
3853      for ( expression[opt] ; expression[opt] ; expression[opt] ) statement
3854      for ( nested-declaration expression[opt] ; expression[opt] ) statement
3855
3856    The form with a declaration is new in C99.
3857
3858    ??? In accordance with the old parser, the declaration may be a
3859    nested function, which is then rejected in check_for_loop_decls,
3860    but does it make any sense for this to be included in the grammar?
3861    Note in particular that the nested function does not include a
3862    trailing ';', whereas the "declaration" production includes one.
3863    Also, can we reject bad declarations earlier and cheaper than
3864    check_for_loop_decls?  */
3865
3866 static void
3867 c_parser_for_statement (c_parser *parser)
3868 {
3869   tree block, cond, incr, save_break, save_cont, body;
3870   location_t loc;
3871   gcc_assert (c_parser_next_token_is_keyword (parser, RID_FOR));
3872   loc = c_parser_peek_token (parser)->location;
3873   c_parser_consume_token (parser);
3874   block = c_begin_compound_stmt (flag_isoc99);
3875   if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3876     {
3877       /* Parse the initialization declaration or expression.  */
3878       if (c_parser_next_token_is (parser, CPP_SEMICOLON))
3879         {
3880           c_parser_consume_token (parser);
3881           c_finish_expr_stmt (NULL_TREE);
3882         }
3883       else if (c_parser_next_token_starts_declspecs (parser))
3884         {
3885           c_parser_declaration_or_fndef (parser, true, true, true, true);
3886           check_for_loop_decls ();
3887         }
3888       else if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
3889         {
3890           /* __extension__ can start a declaration, but is also an
3891              unary operator that can start an expression.  Consume all
3892              but the last of a possible series of __extension__ to
3893              determine which.  */
3894           while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
3895                  && (c_parser_peek_2nd_token (parser)->keyword
3896                      == RID_EXTENSION))
3897             c_parser_consume_token (parser);
3898           if (c_token_starts_declspecs (c_parser_peek_2nd_token (parser)))
3899             {
3900               int ext;
3901               ext = disable_extension_diagnostics ();
3902               c_parser_consume_token (parser);
3903               c_parser_declaration_or_fndef (parser, true, true, true, true);
3904               restore_extension_diagnostics (ext);
3905               check_for_loop_decls ();
3906             }
3907           else
3908             goto init_expr;
3909         }
3910       else
3911         {
3912         init_expr:
3913           c_finish_expr_stmt (c_parser_expression (parser).value);
3914           c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
3915         }
3916       /* Parse the loop condition.  */
3917       loc = c_parser_peek_token (parser)->location;
3918       if (c_parser_next_token_is (parser, CPP_SEMICOLON))
3919         {
3920           c_parser_consume_token (parser);
3921           cond = NULL_TREE;
3922         }
3923       else
3924         {
3925           tree ocond = c_parser_expression_conv (parser).value;
3926           cond = c_objc_common_truthvalue_conversion (ocond);
3927           if (EXPR_P (cond))
3928             SET_EXPR_LOCATION (cond, loc);
3929           c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
3930         }
3931       /* Parse the increment expression.  */
3932       if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3933         incr = c_process_expr_stmt (NULL_TREE);
3934       else
3935         incr = c_process_expr_stmt (c_parser_expression (parser).value);
3936       c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
3937     }
3938   else
3939     {
3940       cond = error_mark_node;
3941       incr = error_mark_node;
3942     }
3943   save_break = c_break_label;
3944   c_break_label = NULL_TREE;
3945   save_cont = c_cont_label;
3946   c_cont_label = NULL_TREE;
3947   body = c_parser_c99_block_statement (parser);
3948   c_finish_loop (loc, cond, incr, body, c_break_label, c_cont_label, true);
3949   add_stmt (c_end_compound_stmt (block, flag_isoc99));
3950   c_break_label = save_break;
3951   c_cont_label = save_cont;
3952 }
3953
3954 /* Parse an asm statement, a GNU extension.  This is a full-blown asm
3955    statement with inputs, outputs, clobbers, and volatile tag
3956    allowed.
3957
3958    asm-statement:
3959      asm type-qualifier[opt] ( asm-argument ) ;
3960
3961    asm-argument:
3962      asm-string-literal
3963      asm-string-literal : asm-operands[opt]
3964      asm-string-literal : asm-operands[opt] : asm-operands[opt]
3965      asm-string-literal : asm-operands[opt] : asm-operands[opt] : asm-clobbers
3966
3967    Qualifiers other than volatile are accepted in the syntax but
3968    warned for.  */
3969
3970 static tree
3971 c_parser_asm_statement (c_parser *parser)
3972 {
3973   tree quals, str, outputs, inputs, clobbers, ret;
3974   bool simple;
3975   gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM));
3976   c_parser_consume_token (parser);
3977   if (c_parser_next_token_is_keyword (parser, RID_VOLATILE))
3978     {
3979       quals = c_parser_peek_token (parser)->value;
3980       c_parser_consume_token (parser);
3981     }
3982   else if (c_parser_next_token_is_keyword (parser, RID_CONST)
3983            || c_parser_next_token_is_keyword (parser, RID_RESTRICT))
3984     {
3985       warning (0, "%E qualifier ignored on asm",
3986                c_parser_peek_token (parser)->value);
3987       quals = NULL_TREE;
3988       c_parser_consume_token (parser);
3989     }
3990   else
3991     quals = NULL_TREE;
3992   /* ??? Follow the C++ parser rather than using the
3993      c_lex_string_translate kludge.  */
3994   c_lex_string_translate = 0;
3995   if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3996     {
3997       c_lex_string_translate = 1;
3998       return NULL_TREE;
3999     }
4000   str = c_parser_asm_string_literal (parser);
4001   if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4002     {
4003       simple = true;
4004       outputs = NULL_TREE;
4005       inputs = NULL_TREE;
4006       clobbers = NULL_TREE;
4007       goto done_asm;
4008     }
4009   if (!c_parser_require (parser, CPP_COLON, "expected %<:%> or %<)%>"))
4010     {
4011       c_lex_string_translate = 1;
4012       c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4013       return NULL_TREE;
4014     }
4015   simple = false;
4016   /* Parse outputs.  */
4017   if (c_parser_next_token_is (parser, CPP_COLON)
4018       || c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4019     outputs = NULL_TREE;
4020   else
4021     outputs = c_parser_asm_operands (parser, false);
4022   if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4023     {
4024       inputs = NULL_TREE;
4025       clobbers = NULL_TREE;
4026       goto done_asm;
4027     }
4028   if (!c_parser_require (parser, CPP_COLON, "expected %<:%> or %<)%>"))
4029     {
4030       c_lex_string_translate = 1;
4031       c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4032       return NULL_TREE;
4033     }
4034   /* Parse inputs.  */
4035   if (c_parser_next_token_is (parser, CPP_COLON)
4036       || c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4037     inputs = NULL_TREE;
4038   else
4039     inputs = c_parser_asm_operands (parser, true);
4040   if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4041     {
4042       clobbers = NULL_TREE;
4043       goto done_asm;
4044     }
4045   if (!c_parser_require (parser, CPP_COLON, "expected %<:%> or %<)%>"))
4046     {
4047       c_lex_string_translate = 1;
4048       c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4049       return NULL_TREE;
4050     }
4051   /* Parse clobbers.  */
4052   clobbers = c_parser_asm_clobbers (parser);
4053  done_asm:
4054   c_lex_string_translate = 1;
4055   if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
4056     {
4057       c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4058       return NULL_TREE;
4059     }
4060   if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
4061     c_parser_skip_to_end_of_block_or_statement (parser);
4062   ret = build_asm_stmt (quals, build_asm_expr (str, outputs, inputs,
4063                                                clobbers, simple));
4064   return ret;
4065 }
4066
4067 /* Parse asm operands, a GNU extension.  If CONVERT_P (for inputs but
4068    not outputs), apply the default conversion of functions and arrays
4069    to pointers.
4070
4071    asm-operands:
4072      asm-operand
4073      asm-operands , asm-operand
4074
4075    asm-operand:
4076      asm-string-literal ( expression )
4077      [ identifier ] asm-string-literal ( expression )
4078 */
4079
4080 static tree
4081 c_parser_asm_operands (c_parser *parser, bool convert_p)
4082 {
4083   tree list = NULL_TREE;
4084   while (true)
4085     {
4086       tree name, str;
4087       struct c_expr expr;
4088       if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
4089         {
4090           c_parser_consume_token (parser);
4091           if (c_parser_next_token_is (parser, CPP_NAME))
4092             {
4093               tree id = c_parser_peek_token (parser)->value;
4094               c_parser_consume_token (parser);
4095               name = build_string (IDENTIFIER_LENGTH (id),
4096                                    IDENTIFIER_POINTER (id));
4097             }
4098           else
4099             {
4100               c_parser_error (parser, "expected identifier");
4101               c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
4102               return NULL_TREE;
4103             }
4104           c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
4105                                      "expected %<]%>");
4106         }
4107       else
4108         name = NULL_TREE;
4109       str = c_parser_asm_string_literal (parser);
4110       if (str == NULL_TREE)
4111         return NULL_TREE;
4112       c_lex_string_translate = 1;
4113       if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4114         {
4115           c_lex_string_translate = 0;
4116           return NULL_TREE;
4117         }
4118       expr = c_parser_expression (parser);
4119       if (convert_p)
4120         expr = default_function_array_conversion (expr);
4121       c_lex_string_translate = 0;
4122       if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
4123         {
4124           c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4125           return NULL_TREE;
4126         }
4127       list = chainon (list, build_tree_list (build_tree_list (name, str),
4128                                              expr.value));
4129       if (c_parser_next_token_is (parser, CPP_COMMA))
4130         c_parser_consume_token (parser);
4131       else
4132         break;
4133     }
4134   return list;
4135 }
4136
4137 /* Parse asm clobbers, a GNU extension.
4138
4139    asm-clobbers:
4140      asm-string-literal
4141      asm-clobbers , asm-string-literal
4142 */
4143
4144 static tree
4145 c_parser_asm_clobbers (c_parser *parser)
4146 {
4147   tree list = NULL_TREE;
4148   while (true)
4149     {
4150       tree str = c_parser_asm_string_literal (parser);
4151       if (str)
4152         list = tree_cons (NULL_TREE, str, list);
4153       else
4154         return NULL_TREE;
4155       if (c_parser_next_token_is (parser, CPP_COMMA))
4156         c_parser_consume_token (parser);
4157       else
4158         break;
4159     }
4160   return list;
4161 }
4162
4163 /* Parse an expression other than a compound expression; that is, an
4164    assignment expression (C90 6.3.16, C99 6.5.16).  If AFTER is not
4165    NULL then it is an Objective-C message expression which is the
4166    primary-expression starting the expression as an initializer.
4167
4168    assignment-expression:
4169      conditional-expression
4170      unary-expression assignment-operator assignment-expression
4171
4172    assignment-operator: one of
4173      = *= /= %= += -= <<= >>= &= ^= |=
4174
4175    In GNU C we accept any conditional expression on the LHS and
4176    diagnose the invalid lvalue rather than producing a syntax
4177    error.  */
4178
4179 static struct c_expr
4180 c_parser_expr_no_commas (c_parser *parser, struct c_expr *after)
4181 {
4182   struct c_expr lhs, rhs, ret;
4183   enum tree_code code;
4184   gcc_assert (!after || c_dialect_objc ());
4185   lhs = c_parser_conditional_expression (parser, after);
4186   switch (c_parser_peek_token (parser)->type)
4187     {
4188     case CPP_EQ:
4189       code = NOP_EXPR;
4190       break;
4191     case CPP_MULT_EQ:
4192       code = MULT_EXPR;
4193       break;
4194     case CPP_DIV_EQ:
4195       code = TRUNC_DIV_EXPR;
4196       break;
4197     case CPP_MOD_EQ:
4198       code = TRUNC_MOD_EXPR;
4199       break;
4200     case CPP_PLUS_EQ:
4201       code = PLUS_EXPR;
4202       break;
4203     case CPP_MINUS_EQ:
4204       code = MINUS_EXPR;
4205       break;
4206     case CPP_LSHIFT_EQ:
4207       code = LSHIFT_EXPR;
4208       break;
4209     case CPP_RSHIFT_EQ:
4210       code = RSHIFT_EXPR;
4211       break;
4212     case CPP_AND_EQ:
4213       code = BIT_AND_EXPR;
4214       break;
4215     case CPP_XOR_EQ:
4216       code = BIT_XOR_EXPR;
4217       break;
4218     case CPP_OR_EQ:
4219       code = BIT_IOR_EXPR;
4220       break;
4221     default:
4222       return lhs;
4223     }
4224   c_parser_consume_token (parser);
4225   rhs = c_parser_expr_no_commas (parser, NULL);
4226   rhs = default_function_array_conversion (rhs);
4227   ret.value = build_modify_expr (lhs.value, code, rhs.value);
4228   if (code == NOP_EXPR)
4229     ret.original_code = MODIFY_EXPR;
4230   else
4231     {
4232       TREE_NO_WARNING (ret.value) = 1;
4233       ret.original_code = ERROR_MARK;
4234     }
4235   return ret;
4236 }
4237
4238 /* Parse a conditional expression (C90 6.3.15, C99 6.5.15).  If AFTER
4239    is not NULL then it is an Objective-C message expression which is
4240    the primary-expression starting the expression as an initializer.
4241
4242    conditional-expression:
4243      logical-OR-expression
4244      logical-OR-expression ? expression : conditional-expression
4245
4246    GNU extensions:
4247
4248    conditional-expression:
4249      logical-OR-expression ? : conditional-expression
4250 */
4251
4252 static struct c_expr
4253 c_parser_conditional_expression (c_parser *parser, struct c_expr *after)
4254 {
4255   struct c_expr cond, exp1, exp2, ret;
4256   gcc_assert (!after || c_dialect_objc ());
4257   cond = c_parser_binary_expression (parser, after);
4258   if (c_parser_next_token_is_not (parser, CPP_QUERY))
4259     return cond;
4260   cond = default_function_array_conversion (cond);
4261   c_parser_consume_token (parser);
4262   if (c_parser_next_token_is (parser, CPP_COLON))
4263     {
4264       if (pedantic)
4265         pedwarn ("ISO C forbids omitting the middle term of a ?: expression");
4266       /* Make sure first operand is calculated only once.  */
4267       exp1.value = save_expr (default_conversion (cond.value));
4268       cond.value = c_objc_common_truthvalue_conversion (exp1.value);
4269       skip_evaluation += cond.value == truthvalue_true_node;
4270     }
4271   else
4272     {
4273       cond.value
4274         = c_objc_common_truthvalue_conversion
4275         (default_conversion (cond.value));
4276       skip_evaluation += cond.value == truthvalue_false_node;
4277       exp1 = c_parser_expression_conv (parser);
4278       skip_evaluation += ((cond.value == truthvalue_true_node)
4279                           - (cond.value == truthvalue_false_node));
4280     }
4281   if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
4282     {
4283       skip_evaluation -= cond.value == truthvalue_true_node;
4284       ret.value = error_mark_node;
4285       ret.original_code = ERROR_MARK;
4286       return ret;
4287     }
4288   exp2 = c_parser_conditional_expression (parser, NULL);
4289   exp2 = default_function_array_conversion (exp2);
4290   skip_evaluation -= cond.value == truthvalue_true_node;
4291   ret.value = build_conditional_expr (cond.value, exp1.value, exp2.value);
4292   ret.original_code = ERROR_MARK;
4293   return ret;
4294 }
4295
4296 /* Parse a binary expression; that is, a logical-OR-expression (C90
4297    6.3.5-6.3.14, C99 6.5.5-6.5.14).  If AFTER is not NULL then it is
4298    an Objective-C message expression which is the primary-expression
4299    starting the expression as an initializer.
4300
4301    multiplicative-expression:
4302      cast-expression
4303      multiplicative-expression * cast-expression
4304      multiplicative-expression / cast-expression
4305      multiplicative-expression % cast-expression
4306
4307    additive-expression:
4308      multiplicative-expression
4309      additive-expression + multiplicative-expression
4310      additive-expression - multiplicative-expression
4311
4312    shift-expression:
4313      additive-expression
4314      shift-expression << additive-expression
4315      shift-expression >> additive-expression
4316
4317    relational-expression:
4318      shift-expression
4319      relational-expression < shift-expression
4320      relational-expression > shift-expression
4321      relational-expression <= shift-expression
4322      relational-expression >= shift-expression
4323
4324    equality-expression:
4325      relational-expression
4326      equality-expression == relational-expression
4327      equality-expression != relational-expression
4328
4329    AND-expression:
4330      equality-expression
4331      AND-expression & equality-expression
4332
4333    exclusive-OR-expression:
4334      AND-expression
4335      exclusive-OR-expression ^ AND-expression
4336
4337    inclusive-OR-expression:
4338      exclusive-OR-expression
4339      inclusive-OR-expression | exclusive-OR-expression
4340
4341    logical-AND-expression:
4342      inclusive-OR-expression
4343      logical-AND-expression && inclusive-OR-expression
4344
4345    logical-OR-expression:
4346      logical-AND-expression
4347      logical-OR-expression || logical-AND-expression
4348 */
4349
4350 static struct c_expr
4351 c_parser_binary_expression (c_parser *parser, struct c_expr *after)
4352 {
4353   /* A binary expression is parsed using operator-precedence parsing,
4354      with the operands being cast expressions.  All the binary
4355      operators are left-associative.  Thus a binary expression is of
4356      form:
4357
4358      E0 op1 E1 op2 E2 ...
4359
4360      which we represent on a stack.  On the stack, the precedence
4361      levels are strictly increasing.  When a new operator is
4362      encountered of higher precedence than that at the top of the
4363      stack, it is pushed; its LHS is the top expression, and its RHS
4364      is everything parsed until it is popped.  When a new operator is
4365      encountered with precedence less than or equal to that at the top
4366      of the stack, triples E[i-1] op[i] E[i] are popped and replaced
4367      by the result of the operation until the operator at the top of
4368      the stack has lower precedence than the new operator or there is
4369      only one element on the stack; then the top expression is the LHS
4370      of the new operator.  In the case of logical AND and OR
4371      expressions, we also need to adjust skip_evaluation as
4372      appropriate when the operators are pushed and popped.  */
4373
4374   /* The precedence levels, where 0 is a dummy lowest level used for
4375      the bottom of the stack.  */
4376   enum prec {
4377     PREC_NONE,
4378     PREC_LOGOR,
4379     PREC_LOGAND,
4380     PREC_BITOR,
4381     PREC_BITXOR,
4382     PREC_BITAND,
4383     PREC_EQ,
4384     PREC_REL,
4385     PREC_SHIFT,
4386     PREC_ADD,
4387     PREC_MULT,
4388     NUM_PRECS
4389   };
4390   struct {
4391     /* The expression at this stack level.  */
4392     struct c_expr expr;
4393     /* The precedence of the operator on its left, PREC_NONE at the
4394        bottom of the stack.  */
4395     enum prec prec;
4396     /* The operation on its left.  */
4397     enum tree_code op;
4398   } stack[NUM_PRECS];
4399   int sp;
4400 #define POP                                                                   \
4401   do {                                                                        \
4402     switch (stack[sp].op)                                                     \
4403       {                                                                       \
4404       case TRUTH_ANDIF_EXPR:                                                  \
4405         skip_evaluation -= stack[sp - 1].expr.value == truthvalue_false_node; \
4406         break;                                                                \
4407       case TRUTH_ORIF_EXPR:                                                   \
4408         skip_evaluation -= stack[sp - 1].expr.value == truthvalue_true_node;  \
4409         break;                                                                \
4410       default:                                                                \
4411         break;                                                                \
4412       }                                                                       \
4413     stack[sp - 1].expr                                                        \
4414       = default_function_array_conversion (stack[sp - 1].expr);               \
4415     stack[sp].expr                                                            \
4416       = default_function_array_conversion (stack[sp].expr);                   \
4417     stack[sp - 1].expr = parser_build_binary_op (stack[sp].op,                \
4418                                                  stack[sp - 1].expr,          \
4419                                                  stack[sp].expr);             \
4420     sp--;                                                                     \
4421   } while (0)
4422   gcc_assert (!after || c_dialect_objc ());
4423   stack[0].expr = c_parser_cast_expression (parser, after);
4424   stack[0].prec = PREC_NONE;
4425   sp = 0;
4426   while (true)
4427     {
4428       enum prec oprec;
4429       enum tree_code ocode;
4430       if (parser->error)
4431         goto out;
4432       switch (c_parser_peek_token (parser)->type)
4433         {
4434         case CPP_MULT:
4435           oprec = PREC_MULT;
4436           ocode = MULT_EXPR;
4437           break;
4438         case CPP_DIV:
4439           oprec = PREC_MULT;
4440           ocode = TRUNC_DIV_EXPR;
4441           break;
4442         case CPP_MOD:
4443           oprec = PREC_MULT;
4444           ocode = TRUNC_MOD_EXPR;
4445           break;
4446         case CPP_PLUS:
4447           oprec = PREC_ADD;
4448           ocode = PLUS_EXPR;
4449           break;
4450         case CPP_MINUS:
4451           oprec = PREC_ADD;
4452           ocode = MINUS_EXPR;
4453           break;
4454         case CPP_LSHIFT:
4455           oprec = PREC_SHIFT;
4456           ocode = LSHIFT_EXPR;
4457           break;
4458         case CPP_RSHIFT:
4459           oprec = PREC_SHIFT;
4460           ocode = RSHIFT_EXPR;
4461           break;
4462         case CPP_LESS:
4463           oprec = PREC_REL;
4464           ocode = LT_EXPR;
4465           break;
4466         case CPP_GREATER:
4467           oprec = PREC_REL;
4468           ocode = GT_EXPR;
4469           break;
4470         case CPP_LESS_EQ:
4471           oprec = PREC_REL;
4472           ocode = LE_EXPR;
4473           break;
4474         case CPP_GREATER_EQ:
4475           oprec = PREC_REL;
4476           ocode = GE_EXPR;
4477           break;
4478         case CPP_EQ_EQ:
4479           oprec = PREC_EQ;
4480           ocode = EQ_EXPR;
4481           break;
4482         case CPP_NOT_EQ:
4483           oprec = PREC_EQ;
4484           ocode = NE_EXPR;
4485           break;
4486         case CPP_AND:
4487           oprec = PREC_BITAND;
4488           ocode = BIT_AND_EXPR;
4489           break;
4490         case CPP_XOR:
4491           oprec = PREC_BITXOR;
4492           ocode = BIT_XOR_EXPR;
4493           break;
4494         case CPP_OR:
4495           oprec = PREC_BITOR;
4496           ocode = BIT_IOR_EXPR;
4497           break;
4498         case CPP_AND_AND:
4499           oprec = PREC_LOGAND;
4500           ocode = TRUTH_ANDIF_EXPR;
4501           break;
4502         case CPP_OR_OR:
4503           oprec = PREC_LOGOR;
4504           ocode = TRUTH_ORIF_EXPR;
4505           break;
4506         default:
4507           /* Not a binary operator, so end of the binary
4508              expression.  */
4509           goto out;
4510         }
4511       c_parser_consume_token (parser);
4512       while (oprec <= stack[sp].prec)
4513         POP;
4514       switch (ocode)
4515         {
4516         case TRUTH_ANDIF_EXPR:
4517           stack[sp].expr
4518             = default_function_array_conversion (stack[sp].expr);
4519           stack[sp].expr.value = c_objc_common_truthvalue_conversion
4520             (default_conversion (stack[sp].expr.value));
4521           skip_evaluation += stack[sp].expr.value == truthvalue_false_node;
4522           break;
4523         case TRUTH_ORIF_EXPR:
4524           stack[sp].expr
4525             = default_function_array_conversion (stack[sp].expr);
4526           stack[sp].expr.value = c_objc_common_truthvalue_conversion
4527             (default_conversion (stack[sp].expr.value));
4528           skip_evaluation += stack[sp].expr.value == truthvalue_true_node;
4529           break;
4530         default:
4531           break;
4532         }
4533       sp++;
4534       stack[sp].expr = c_parser_cast_expression (parser, NULL);
4535       stack[sp].prec = oprec;
4536       stack[sp].op = ocode;
4537     }
4538  out:
4539   while (sp > 0)
4540     POP;
4541   return stack[0].expr;
4542 #undef POP
4543 }
4544
4545 /* Parse a cast expression (C90 6.3.4, C99 6.5.4).  If AFTER is not
4546    NULL then it is an Objective-C message expression which is the
4547    primary-expression starting the expression as an initializer.
4548
4549    cast-expression:
4550      unary-expression
4551      ( type-name ) unary-expression
4552 */
4553
4554 static struct c_expr
4555 c_parser_cast_expression (c_parser *parser, struct c_expr *after)
4556 {
4557   gcc_assert (!after || c_dialect_objc ());
4558   if (after)
4559     return c_parser_postfix_expression_after_primary (parser, *after);
4560   /* If the expression begins with a parenthesized type name, it may
4561      be either a cast or a compound literal; we need to see whether
4562      the next character is '{' to tell the difference.  If not, it is
4563      an unary expression.  */
4564   if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
4565       && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
4566     {
4567       struct c_type_name *type_name;
4568       struct c_expr ret;
4569       struct c_expr expr;
4570       c_parser_consume_token (parser);
4571       type_name = c_parser_type_name (parser);
4572       c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
4573       if (type_name == NULL)
4574         {
4575           ret.value = error_mark_node;
4576           ret.original_code = ERROR_MARK;
4577           return ret;
4578         }
4579       if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
4580         return c_parser_postfix_expression_after_paren_type (parser,
4581                                                              type_name);
4582       expr = c_parser_cast_expression (parser, NULL);
4583       expr = default_function_array_conversion (expr);
4584       ret.value = c_cast_expr (type_name, expr.value);
4585       ret.original_code = ERROR_MARK;
4586       return ret;
4587     }
4588   else
4589     return c_parser_unary_expression (parser);
4590 }
4591
4592 /* Parse an unary expression (C90 6.3.3, C99 6.5.3).
4593
4594    unary-expression:
4595      postfix-expression
4596      ++ unary-expression
4597      -- unary-expression
4598      unary-operator cast-expression
4599      sizeof unary-expression
4600      sizeof ( type-name )
4601
4602    unary-operator: one of
4603      & * + - ~ !
4604
4605    GNU extensions:
4606
4607    unary-expression:
4608      __alignof__ unary-expression
4609      __alignof__ ( type-name )
4610      && identifier
4611
4612    unary-operator: one of
4613      __extension__ __real__ __imag__
4614
4615    In addition, the GNU syntax treats ++ and -- as unary operators, so
4616    they may be applied to cast expressions with errors for non-lvalues
4617    given later.  */
4618
4619 static struct c_expr
4620 c_parser_unary_expression (c_parser *parser)
4621 {
4622   int ext;
4623   struct c_expr ret, op;
4624   switch (c_parser_peek_token (parser)->type)
4625     {
4626     case CPP_PLUS_PLUS:
4627       c_parser_consume_token (parser);
4628       op = c_parser_cast_expression (parser, NULL);
4629       op = default_function_array_conversion (op);
4630       return parser_build_unary_op (PREINCREMENT_EXPR, op);
4631     case CPP_MINUS_MINUS:
4632       c_parser_consume_token (parser);
4633       op = c_parser_cast_expression (parser, NULL);
4634       op = default_function_array_conversion (op);
4635       return parser_build_unary_op (PREDECREMENT_EXPR, op);
4636     case CPP_AND:
4637       c_parser_consume_token (parser);
4638       return parser_build_unary_op (ADDR_EXPR,
4639                                     c_parser_cast_expression (parser, NULL));
4640     case CPP_MULT:
4641       c_parser_consume_token (parser);
4642       op = c_parser_cast_expression (parser, NULL);
4643       op = default_function_array_conversion (op);
4644       ret.value = build_indirect_ref (op.value, "unary *");
4645       ret.original_code = ERROR_MARK;
4646       return ret;
4647     case CPP_PLUS:
4648       c_parser_consume_token (parser);
4649       if (!c_dialect_objc () && !in_system_header)
4650         warning (OPT_Wtraditional,
4651                  "traditional C rejects the unary plus operator");
4652       op = c_parser_cast_expression (parser, NULL);
4653       op = default_function_array_conversion (op);
4654       return parser_build_unary_op (CONVERT_EXPR, op);
4655     case CPP_MINUS:
4656       c_parser_consume_token (parser);
4657       op = c_parser_cast_expression (parser, NULL);
4658       op = default_function_array_conversion (op);
4659       return parser_build_unary_op (NEGATE_EXPR, op);
4660     case CPP_COMPL:
4661       c_parser_consume_token (parser);
4662       op = c_parser_cast_expression (parser, NULL);
4663       op = default_function_array_conversion (op);
4664       return parser_build_unary_op (BIT_NOT_EXPR, op);
4665     case CPP_NOT:
4666       c_parser_consume_token (parser);
4667       op = c_parser_cast_expression (parser, NULL);
4668       op = default_function_array_conversion (op);
4669       return parser_build_unary_op (TRUTH_NOT_EXPR, op);
4670     case CPP_AND_AND:
4671       /* Refer to the address of a label as a pointer.  */
4672       c_parser_consume_token (parser);
4673       if (c_parser_next_token_is (parser, CPP_NAME))
4674         {
4675           ret.value = finish_label_address_expr
4676             (c_parser_peek_token (parser)->value);
4677           c_parser_consume_token (parser);
4678         }
4679       else
4680         {
4681           c_parser_error (parser, "expected identifier");
4682           ret.value = error_mark_node;
4683         }
4684         ret.original_code = ERROR_MARK;
4685         return ret;
4686     case CPP_KEYWORD:
4687       switch (c_parser_peek_token (parser)->keyword)
4688         {
4689         case RID_SIZEOF:
4690           return c_parser_sizeof_expression (parser);
4691         case RID_ALIGNOF:
4692           return c_parser_alignof_expression (parser);
4693         case RID_EXTENSION:
4694           c_parser_consume_token (parser);
4695           ext = disable_extension_diagnostics ();
4696           ret = c_parser_cast_expression (parser, NULL);
4697           restore_extension_diagnostics (ext);
4698           return ret;
4699         case RID_REALPART:
4700           c_parser_consume_token (parser);
4701           op = c_parser_cast_expression (parser, NULL);
4702           op = default_function_array_conversion (op);
4703           return parser_build_unary_op (REALPART_EXPR, op);
4704         case RID_IMAGPART:
4705           c_parser_consume_token (parser);
4706           op = c_parser_cast_expression (parser, NULL);
4707           op = default_function_array_conversion (op);
4708           return parser_build_unary_op (IMAGPART_EXPR, op);
4709         default:
4710           return c_parser_postfix_expression (parser);
4711         }
4712     default:
4713       return c_parser_postfix_expression (parser);
4714     }
4715 }
4716
4717 /* Parse a sizeof expression.  */
4718
4719 static struct c_expr
4720 c_parser_sizeof_expression (c_parser *parser)
4721 {
4722   struct c_expr expr;
4723   gcc_assert (c_parser_next_token_is_keyword (parser, RID_SIZEOF));
4724   c_parser_consume_token (parser);
4725   skip_evaluation++;
4726   in_sizeof++;
4727   if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
4728       && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
4729     {
4730       /* Either sizeof ( type-name ) or sizeof unary-expression
4731          starting with a compound literal.  */
4732       struct c_type_name *type_name;
4733       c_parser_consume_token (parser);
4734       type_name = c_parser_type_name (parser);
4735       c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
4736       if (type_name == NULL)
4737         {
4738           struct c_expr ret;
4739           skip_evaluation--;
4740           in_sizeof--;
4741           ret.value = error_mark_node;
4742           ret.original_code = ERROR_MARK;
4743           return ret;
4744         }
4745       if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
4746         {
4747           expr = c_parser_postfix_expression_after_paren_type (parser,
4748                                                                type_name);
4749           goto sizeof_expr;
4750         }
4751       /* sizeof ( type-name ).  */
4752       skip_evaluation--;
4753       in_sizeof--;
4754       return c_expr_sizeof_type (type_name);
4755     }
4756   else
4757     {
4758       expr = c_parser_unary_expression (parser);
4759     sizeof_expr:
4760       skip_evaluation--;
4761       in_sizeof--;
4762       if (TREE_CODE (expr.value) == COMPONENT_REF
4763           && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1)))
4764         error ("%<sizeof%> applied to a bit-field");
4765       return c_expr_sizeof_expr (expr);
4766     }
4767 }
4768
4769 /* Parse an alignof expression.  */
4770
4771 static struct c_expr
4772 c_parser_alignof_expression (c_parser *parser)
4773 {
4774   struct c_expr expr;
4775   gcc_assert (c_parser_next_token_is_keyword (parser, RID_ALIGNOF));
4776   c_parser_consume_token (parser);
4777   skip_evaluation++;
4778   in_alignof++;
4779   if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
4780       && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
4781     {
4782       /* Either __alignof__ ( type-name ) or __alignof__
4783          unary-expression starting with a compound literal.  */
4784       struct c_type_name *type_name;
4785       struct c_expr ret;
4786       c_parser_consume_token (parser);
4787       type_name = c_parser_type_name (parser);
4788       c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
4789       if (type_name == NULL)
4790         {
4791           struct c_expr ret;
4792           skip_evaluation--;
4793           in_alignof--;
4794           ret.value = error_mark_node;
4795           ret.original_code = ERROR_MARK;
4796           return ret;
4797         }
4798       if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
4799         {
4800           expr = c_parser_postfix_expression_after_paren_type (parser,
4801                                                                type_name);
4802           goto alignof_expr;
4803         }
4804       /* alignof ( type-name ).  */
4805       skip_evaluation--;
4806       in_alignof--;
4807       ret.value = c_alignof (groktypename (type_name));
4808       ret.original_code = ERROR_MARK;
4809       return ret;
4810     }
4811   else
4812     {
4813       struct c_expr ret;
4814       expr = c_parser_unary_expression (parser);
4815     alignof_expr:
4816       skip_evaluation--;
4817       in_alignof--;
4818       ret.value = c_alignof_expr (expr.value);
4819       ret.original_code = ERROR_MARK;
4820       return ret;
4821     }
4822 }
4823
4824 /* Parse a postfix expression (C90 6.3.1-6.3.2, C99 6.5.1-6.5.2).
4825
4826    postfix-expression:
4827      primary-expression
4828      postfix-expression [ expression ]
4829      postfix-expression ( argument-expression-list[opt] )
4830      postfix-expression . identifier
4831      postfix-expression -> identifier
4832      postfix-expression ++
4833      postfix-expression --
4834      ( type-name ) { initializer-list }
4835      ( type-name ) { initializer-list , }
4836
4837    argument-expression-list:
4838      argument-expression
4839      argument-expression-list , argument-expression
4840
4841    primary-expression:
4842      identifier
4843      constant
4844      string-literal
4845      ( expression )
4846
4847    GNU extensions:
4848
4849    primary-expression:
4850      __func__
4851        (treated as a keyword in GNU C)
4852      __FUNCTION__
4853      __PRETTY_FUNCTION__
4854      ( compound-statement )
4855      __builtin_va_arg ( assignment-expression , type-name )
4856      __builtin_offsetof ( type-name , offsetof-member-designator )
4857      __builtin_choose_expr ( assignment-expression ,
4858                              assignment-expression ,
4859                              assignment-expression )
4860      __builtin_types_compatible_p ( type-name , type-name )
4861
4862    offsetof-member-designator:
4863      identifier
4864      offsetof-member-designator . identifier
4865      offsetof-member-designator [ expression ]
4866
4867    Objective-C:
4868
4869    primary-expression:
4870      [ objc-receiver objc-message-args ]
4871      @selector ( objc-selector-arg )
4872      @protocol ( identifier )
4873      @encode ( type-name )
4874      objc-string-literal
4875 */
4876
4877 static struct c_expr
4878 c_parser_postfix_expression (c_parser *parser)
4879 {
4880   struct c_expr expr, e1, e2, e3;
4881   struct c_type_name *t1, *t2;
4882   switch (c_parser_peek_token (parser)->type)
4883     {
4884     case CPP_NUMBER:
4885     case CPP_CHAR:
4886     case CPP_WCHAR:
4887       expr.value = c_parser_peek_token (parser)->value;
4888       expr.original_code = ERROR_MARK;
4889       c_parser_consume_token (parser);
4890       break;
4891     case CPP_STRING:
4892     case CPP_WSTRING:
4893       expr.value = c_parser_peek_token (parser)->value;
4894       expr.original_code = STRING_CST;
4895       c_parser_consume_token (parser);
4896       break;
4897     case CPP_OBJC_STRING:
4898       gcc_assert (c_dialect_objc ());
4899       expr.value
4900         = objc_build_string_object (c_parser_peek_token (parser)->value);
4901       expr.original_code = ERROR_MARK;
4902       c_parser_consume_token (parser);
4903       break;
4904     case CPP_NAME:
4905       if (c_parser_peek_token (parser)->id_kind != C_ID_ID)
4906         {
4907           c_parser_error (parser, "expected expression");
4908           expr.value = error_mark_node;
4909           expr.original_code = ERROR_MARK;
4910           break;
4911         }
4912       {
4913         tree id = c_parser_peek_token (parser)->value;
4914         location_t loc = c_parser_peek_token (parser)->location;
4915         c_parser_consume_token (parser);
4916         expr.value = build_external_ref (id,
4917                                          (c_parser_peek_token (parser)->type
4918                                           == CPP_OPEN_PAREN), loc);
4919         expr.original_code = ERROR_MARK;
4920       }
4921       break;
4922     case CPP_OPEN_PAREN:
4923       /* A parenthesized expression, statement expression or compound
4924          literal.  */
4925       if (c_parser_peek_2nd_token (parser)->type == CPP_OPEN_BRACE)
4926         {
4927           /* A statement expression.  */
4928           tree stmt;
4929           c_parser_consume_token (parser);
4930           c_parser_consume_token (parser);
4931           if (cur_stmt_list == NULL)
4932             {
4933               error ("braced-group within expression allowed "
4934                      "only inside a function");
4935               parser->error = true;
4936               c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
4937               c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4938               expr.value = error_mark_node;
4939               expr.original_code = ERROR_MARK;
4940               break;
4941             }
4942           stmt = c_begin_stmt_expr ();
4943           c_parser_compound_statement_nostart (parser);
4944           c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4945                                      "expected %<)%>");
4946           if (pedantic)
4947             pedwarn ("ISO C forbids braced-groups within expressions");
4948           expr.value = c_finish_stmt_expr (stmt);
4949           expr.original_code = ERROR_MARK;
4950         }
4951       else if (c_token_starts_typename (c_parser_peek_2nd_token (parser)))
4952         {
4953           /* A compound literal.  ??? Can we actually get here rather
4954              than going directly to
4955              c_parser_postfix_expression_after_paren_type from
4956              elsewhere?  */
4957           struct c_type_name *type_name;
4958           c_parser_consume_token (parser);
4959           type_name = c_parser_type_name (parser);
4960           c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4961                                      "expected %<)%>");
4962           if (type_name == NULL)
4963             {
4964               expr.value = error_mark_node;
4965               expr.original_code = ERROR_MARK;
4966             }
4967           else
4968             expr = c_parser_postfix_expression_after_paren_type (parser,
4969                                                                  type_name);
4970         }
4971       else
4972         {
4973           /* A parenthesized expression.  */
4974           c_parser_consume_token (parser);
4975           expr = c_parser_expression (parser);
4976           if (TREE_CODE (expr.value) == MODIFY_EXPR)
4977             TREE_NO_WARNING (expr.value) = 1;
4978           expr.original_code = ERROR_MARK;
4979           c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4980                                      "expected %<)%>");
4981         }
4982       break;
4983     case CPP_KEYWORD:
4984       switch (c_parser_peek_token (parser)->keyword)
4985         {
4986         case RID_FUNCTION_NAME:
4987         case RID_PRETTY_FUNCTION_NAME:
4988         case RID_C99_FUNCTION_NAME:
4989           expr.value = fname_decl (c_parser_peek_token (parser)->keyword,
4990                                    c_parser_peek_token (parser)->value);
4991           expr.original_code = ERROR_MARK;
4992           c_parser_consume_token (parser);
4993           break;
4994         case RID_VA_ARG:
4995           c_parser_consume_token (parser);
4996           if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4997             {
4998               expr.value = error_mark_node;
4999               expr.original_code = ERROR_MARK;
5000               break;
5001             }
5002           e1 = c_parser_expr_no_commas (parser, NULL);
5003           if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
5004             {
5005               c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5006               expr.value = error_mark_node;
5007               expr.original_code = ERROR_MARK;
5008               break;
5009             }
5010           t1 = c_parser_type_name (parser);
5011           c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5012                                      "expected %<)%>");
5013           if (t1 == NULL)
5014             {
5015               expr.value = error_mark_node;
5016               expr.original_code = ERROR_MARK;
5017             }
5018           else
5019             {
5020               expr.value = build_va_arg (e1.value, groktypename (t1));
5021               expr.original_code = ERROR_MARK;
5022             }
5023           break;
5024         case RID_OFFSETOF:
5025           c_parser_consume_token (parser);
5026           if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5027             {
5028               expr.value = error_mark_node;
5029               expr.original_code = ERROR_MARK;
5030               break;
5031             }
5032           t1 = c_parser_type_name (parser);
5033           if (t1 == NULL)
5034             {
5035               expr.value = error_mark_node;
5036               expr.original_code = ERROR_MARK;
5037               break;
5038             }
5039           if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
5040             {
5041               c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5042               expr.value = error_mark_node;
5043               expr.original_code = ERROR_MARK;
5044               break;
5045             }
5046           {
5047             tree type = groktypename (t1);
5048             tree offsetof_ref;
5049             if (type == error_mark_node)
5050               offsetof_ref = error_mark_node;
5051             else
5052               offsetof_ref = build1 (INDIRECT_REF, type, NULL);
5053             /* Parse the second argument to __builtin_offsetof.  We
5054                must have one identifier, and beyond that we want to
5055                accept sub structure and sub array references.  */
5056             if (c_parser_next_token_is (parser, CPP_NAME))
5057               {
5058                 offsetof_ref = build_component_ref
5059                   (offsetof_ref, c_parser_peek_token (parser)->value);
5060                 c_parser_consume_token (parser);
5061                 while (c_parser_next_token_is (parser, CPP_DOT)
5062                        || c_parser_next_token_is (parser,
5063                                                   CPP_OPEN_SQUARE))
5064                   {
5065                     if (c_parser_next_token_is (parser, CPP_DOT))
5066                       {
5067                         c_parser_consume_token (parser);
5068                         if (c_parser_next_token_is_not (parser,
5069                                                         CPP_NAME))
5070                           {
5071                             c_parser_error (parser, "expected identifier");
5072                             break;
5073                           }
5074                         offsetof_ref = build_component_ref
5075                           (offsetof_ref,
5076                            c_parser_peek_token (parser)->value);
5077                         c_parser_consume_token (parser);
5078                       }
5079                     else
5080                       {
5081                         tree idx;
5082                         c_parser_consume_token (parser);
5083                         idx = c_parser_expression (parser).value;
5084                         c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
5085                                                    "expected %<]%>");
5086                         offsetof_ref = build_array_ref (offsetof_ref, idx);
5087                       }
5088                   }
5089               }
5090             else
5091               c_parser_error (parser, "expected identifier");
5092             c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5093                                        "expected %<)%>");
5094             expr.value = fold_offsetof (offsetof_ref);
5095             expr.original_code = ERROR_MARK;
5096           }
5097           break;
5098         case RID_CHOOSE_EXPR:
5099           c_parser_consume_token (parser);
5100           if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5101             {
5102               expr.value = error_mark_node;
5103               expr.original_code = ERROR_MARK;
5104               break;
5105             }
5106           e1 = c_parser_expr_no_commas (parser, NULL);
5107           if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
5108             {
5109               c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5110               expr.value = error_mark_node;
5111               expr.original_code = ERROR_MARK;
5112               break;
5113             }
5114           e2 = c_parser_expr_no_commas (parser, NULL);
5115           if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
5116             {
5117               c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5118               expr.value = error_mark_node;
5119               expr.original_code = ERROR_MARK;
5120               break;
5121             }
5122           e3 = c_parser_expr_no_commas (parser, NULL);
5123           c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5124                                      "expected %<)%>");
5125           {
5126             tree c;
5127
5128             c = fold (e1.value);
5129             if (TREE_CODE (c) != INTEGER_CST)
5130               error ("first argument to %<__builtin_choose_expr%> not"
5131                      " a constant");
5132             expr = integer_zerop (c) ? e3 : e2;
5133           }
5134           break;
5135         case RID_TYPES_COMPATIBLE_P:
5136           c_parser_consume_token (parser);
5137           if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5138             {
5139               expr.value = error_mark_node;
5140               expr.original_code = ERROR_MARK;
5141               break;
5142             }
5143           t1 = c_parser_type_name (parser);
5144           if (t1 == NULL)
5145             {
5146               expr.value = error_mark_node;
5147               expr.original_code = ERROR_MARK;
5148               break;
5149             }
5150           if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
5151             {
5152               c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5153               expr.value = error_mark_node;
5154               expr.original_code = ERROR_MARK;
5155               break;
5156             }
5157           t2 = c_parser_type_name (parser);
5158           if (t2 == NULL)
5159             {
5160               expr.value = error_mark_node;
5161               expr.original_code = ERROR_MARK;
5162               break;
5163             }
5164           c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5165                                      "expected %<)%>");
5166           {
5167             tree e1, e2;
5168
5169             e1 = TYPE_MAIN_VARIANT (groktypename (t1));
5170             e2 = TYPE_MAIN_VARIANT (groktypename (t2));
5171
5172             expr.value = comptypes (e1, e2)
5173               ? build_int_cst (NULL_TREE, 1)
5174               : build_int_cst (NULL_TREE, 0);
5175             expr.original_code = ERROR_MARK;
5176           }
5177           break;
5178         case RID_AT_SELECTOR:
5179           gcc_assert (c_dialect_objc ());
5180           c_parser_consume_token (parser);
5181           if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5182             {
5183               expr.value = error_mark_node;
5184               expr.original_code = ERROR_MARK;
5185               break;
5186             }
5187           {
5188             tree sel = c_parser_objc_selector_arg (parser);
5189             c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5190                                        "expected %<)%>");
5191             expr.value = objc_build_selector_expr (sel);
5192             expr.original_code = ERROR_MARK;
5193           }
5194           break;
5195         case RID_AT_PROTOCOL:
5196           gcc_assert (c_dialect_objc ());
5197           c_parser_consume_token (parser);
5198           if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5199             {
5200               expr.value = error_mark_node;
5201               expr.original_code = ERROR_MARK;
5202               break;
5203             }
5204           if (c_parser_next_token_is_not (parser, CPP_NAME))
5205             {
5206               c_parser_error (parser, "expected identifier");
5207               c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5208               expr.value = error_mark_node;
5209               expr.original_code = ERROR_MARK;
5210               break;
5211             }
5212           {
5213             tree id = c_parser_peek_token (parser)->value;
5214             c_parser_consume_token (parser);
5215             c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5216                                        "expected %<)%>");
5217             expr.value = objc_build_protocol_expr (id);
5218             expr.original_code = ERROR_MARK;
5219           }
5220           break;
5221         case RID_AT_ENCODE:
5222           /* Extension to support C-structures in the archiver.  */
5223           gcc_assert (c_dialect_objc ());
5224           c_parser_consume_token (parser);
5225           if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5226             {
5227               expr.value = error_mark_node;
5228               expr.original_code = ERROR_MARK;
5229               break;
5230             }
5231           t1 = c_parser_type_name (parser);
5232           if (t1 == NULL)
5233             {
5234               expr.value = error_mark_node;
5235               expr.original_code = ERROR_MARK;
5236               c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5237               break;
5238             }
5239           c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5240                                      "expected %<)%>");
5241           {
5242             tree type = groktypename (t1);
5243             expr.value = objc_build_encode_expr (type);
5244             expr.original_code = ERROR_MARK;
5245           }
5246           break;
5247         default:
5248           c_parser_error (parser, "expected expression");
5249           expr.value = error_mark_node;
5250           expr.original_code = ERROR_MARK;
5251           break;
5252         }
5253       break;
5254     case CPP_OPEN_SQUARE:
5255       if (c_dialect_objc ())
5256         {
5257           tree receiver, args;
5258           c_parser_consume_token (parser);
5259           receiver = c_parser_objc_receiver (parser);
5260           args = c_parser_objc_message_args (parser);
5261           c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
5262                                      "expected %<]%>");
5263           expr.value = objc_build_message_expr (build_tree_list (receiver,
5264                                                                  args));
5265           expr.original_code = ERROR_MARK;
5266           break;
5267         }
5268       /* Else fall through to report error.  */
5269     default:
5270       c_parser_error (parser, "expected expression");
5271       expr.value = error_mark_node;
5272       expr.original_code = ERROR_MARK;
5273       break;
5274     }
5275   return c_parser_postfix_expression_after_primary (parser, expr);
5276 }
5277
5278 /* Parse a postfix expression after a parenthesized type name: the
5279    brace-enclosed initializer of a compound literal, possibly followed
5280    by some postfix operators.  This is separate because it is not
5281    possible to tell until after the type name whether a cast
5282    expression has a cast or a compound literal, or whether the operand
5283    of sizeof is a parenthesized type name or starts with a compound
5284    literal.  */
5285
5286 static struct c_expr
5287 c_parser_postfix_expression_after_paren_type (c_parser *parser,
5288                                               struct c_type_name *type_name)
5289 {
5290   tree type;
5291   struct c_expr init;
5292   struct c_expr expr;
5293   start_init (NULL_TREE, NULL, 0);
5294   type = groktypename (type_name);
5295   if (C_TYPE_VARIABLE_SIZE (type))
5296     {
5297       error ("compound literal has variable size");
5298       type = error_mark_node;
5299     }
5300   init = c_parser_braced_init (parser, type, false);
5301   finish_init ();
5302   maybe_warn_string_init (type, init);
5303
5304   if (pedantic && !flag_isoc99)
5305     pedwarn ("ISO C90 forbids compound literals");
5306   expr.value = build_compound_literal (type, init.value);
5307   expr.original_code = ERROR_MARK;
5308   return c_parser_postfix_expression_after_primary (parser, expr);
5309 }
5310
5311 /* Parse a postfix expression after the initial primary or compound
5312    literal; that is, parse a series of postfix operators.  */
5313
5314 static struct c_expr
5315 c_parser_postfix_expression_after_primary (c_parser *parser,
5316                                            struct c_expr expr)
5317 {
5318   tree ident, idx, exprlist;
5319   while (true)
5320     {
5321       switch (c_parser_peek_token (parser)->type)
5322         {
5323         case CPP_OPEN_SQUARE:
5324           /* Array reference.  */
5325           c_parser_consume_token (parser);
5326           idx = c_parser_expression (parser).value;
5327           c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
5328                                      "expected %<]%>");
5329           expr.value = build_array_ref (expr.value, idx);
5330           expr.original_code = ERROR_MARK;
5331           break;
5332         case CPP_OPEN_PAREN:
5333           /* Function call.  */
5334           c_parser_consume_token (parser);
5335           if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
5336             exprlist = NULL_TREE;
5337           else
5338             exprlist = c_parser_expr_list (parser, true);
5339           c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5340                                      "expected %<)%>");
5341           expr.value = build_function_call (expr.value, exprlist);
5342           expr.original_code = ERROR_MARK;
5343           break;
5344         case CPP_DOT:
5345           /* Structure element reference.  */
5346           c_parser_consume_token (parser);
5347           expr = default_function_array_conversion (expr);
5348           if (c_parser_next_token_is (parser, CPP_NAME))
5349             ident = c_parser_peek_token (parser)->value;
5350           else
5351             {
5352               c_parser_error (parser, "expected identifier");
5353               expr.value = error_mark_node;
5354               expr.original_code = ERROR_MARK;
5355               return expr;
5356             }
5357           c_parser_consume_token (parser);
5358           expr.value = build_component_ref (expr.value, ident);
5359           expr.original_code = ERROR_MARK;
5360           break;
5361         case CPP_DEREF:
5362           /* Structure element reference.  */
5363           c_parser_consume_token (parser);
5364           expr = default_function_array_conversion (expr);
5365           if (c_parser_next_token_is (parser, CPP_NAME))
5366             ident = c_parser_peek_token (parser)->value;
5367           else
5368             {
5369               c_parser_error (parser, "expected identifier");
5370               expr.value = error_mark_node;
5371               expr.original_code = ERROR_MARK;
5372               return expr;
5373             }
5374           c_parser_consume_token (parser);
5375           expr.value = build_component_ref (build_indirect_ref (expr.value,
5376                                                                 "->"), ident);
5377           expr.original_code = ERROR_MARK;
5378           break;
5379         case CPP_PLUS_PLUS:
5380           /* Postincrement.  */
5381           c_parser_consume_token (parser);
5382           expr = default_function_array_conversion (expr);
5383           expr.value = build_unary_op (POSTINCREMENT_EXPR, expr.value, 0);
5384           expr.original_code = ERROR_MARK;
5385           break;
5386         case CPP_MINUS_MINUS:
5387           /* Postdecrement.  */
5388           c_parser_consume_token (parser);
5389           expr = default_function_array_conversion (expr);
5390           expr.value = build_unary_op (POSTDECREMENT_EXPR, expr.value, 0);
5391           expr.original_code = ERROR_MARK;
5392           break;
5393         default:
5394           return expr;
5395         }
5396     }
5397 }
5398
5399 /* Parse an expression (C90 6.3.17, C99 6.5.17).
5400
5401    expression:
5402      assignment-expression
5403      expression , assignment-expression
5404 */
5405
5406 static struct c_expr
5407 c_parser_expression (c_parser *parser)
5408 {
5409   struct c_expr expr;
5410   expr = c_parser_expr_no_commas (parser, NULL);
5411   while (c_parser_next_token_is (parser, CPP_COMMA))
5412     {
5413       struct c_expr next;
5414       c_parser_consume_token (parser);
5415       next = c_parser_expr_no_commas (parser, NULL);
5416       next = default_function_array_conversion (next);
5417       expr.value = build_compound_expr (expr.value, next.value);
5418       expr.original_code = COMPOUND_EXPR;
5419     }
5420   return expr;
5421 }
5422
5423 /* Parse an expression and convert functions or arrays to
5424    pointers.  */
5425
5426 static struct c_expr
5427 c_parser_expression_conv (c_parser *parser)
5428 {
5429   struct c_expr expr;
5430   expr = c_parser_expression (parser);
5431   expr = default_function_array_conversion (expr);
5432   return expr;
5433 }
5434
5435 /* Parse a non-empty list of expressions.  If CONVERT_P, convert
5436    functions and arrays to pointers.
5437
5438    nonempty-expr-list:
5439      assignment-expression
5440      nonempty-expr-list , assignment-expression
5441 */
5442
5443 static tree
5444 c_parser_expr_list (c_parser *parser, bool convert_p)
5445 {
5446   struct c_expr expr;
5447   tree ret, cur;
5448   expr = c_parser_expr_no_commas (parser, NULL);
5449   if (convert_p)
5450     expr = default_function_array_conversion (expr);
5451   ret = cur = build_tree_list (NULL_TREE, expr.value);
5452   while (c_parser_next_token_is (parser, CPP_COMMA))
5453     {
5454       c_parser_consume_token (parser);
5455       expr = c_parser_expr_no_commas (parser, NULL);
5456       if (convert_p)
5457         expr = default_function_array_conversion (expr);
5458       cur = TREE_CHAIN (cur) = build_tree_list (NULL_TREE, expr.value);
5459     }
5460   return ret;
5461 }
5462
5463 \f
5464 /* Parse Objective-C-specific constructs.  */
5465
5466 /* Parse an objc-class-definition.
5467
5468    objc-class-definition:
5469      @interface identifier objc-superclass[opt] objc-protocol-refs[opt]
5470        objc-class-instance-variables[opt] objc-methodprotolist @end
5471      @implementation identifier objc-superclass[opt]
5472        objc-class-instance-variables[opt]
5473      @interface identifier ( identifier ) objc-protocol-refs[opt]
5474        objc-methodprotolist @end
5475      @implementation identifier ( identifier )
5476
5477    objc-superclass:
5478      : identifier
5479
5480    "@interface identifier (" must start "@interface identifier (
5481    identifier ) ...": objc-methodprotolist in the first production may
5482    not start with a parenthesized identifier as a declarator of a data
5483    definition with no declaration specifiers if the objc-superclass,
5484    objc-protocol-refs and objc-class-instance-variables are omitted.  */
5485
5486 static void
5487 c_parser_objc_class_definition (c_parser *parser)
5488 {
5489   bool iface_p;
5490   tree id1;
5491   tree superclass;
5492   if (c_parser_next_token_is_keyword (parser, RID_AT_INTERFACE))
5493     iface_p = true;
5494   else if (c_parser_next_token_is_keyword (parser, RID_AT_IMPLEMENTATION))
5495     iface_p = false;
5496   else
5497     gcc_unreachable ();
5498   c_parser_consume_token (parser);
5499   if (c_parser_next_token_is_not (parser, CPP_NAME))
5500     {
5501       c_parser_error (parser, "expected identifier");
5502       return;
5503     }
5504   id1 = c_parser_peek_token (parser)->value;
5505   c_parser_consume_token (parser);
5506   if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
5507     {
5508       tree id2;
5509       tree proto = NULL_TREE;
5510       c_parser_consume_token (parser);
5511       if (c_parser_next_token_is_not (parser, CPP_NAME))
5512         {
5513           c_parser_error (parser, "expected identifier");
5514           c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5515           return;
5516         }
5517       id2 = c_parser_peek_token (parser)->value;
5518       c_parser_consume_token (parser);
5519       c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
5520       if (!iface_p)
5521         {
5522           objc_start_category_implementation (id1, id2);
5523           return;
5524         }
5525       if (c_parser_next_token_is (parser, CPP_LESS))
5526         proto = c_parser_objc_protocol_refs (parser);
5527       objc_start_category_interface (id1, id2, proto);
5528       c_parser_objc_methodprotolist (parser);
5529       c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
5530       objc_finish_interface ();
5531       return;
5532     }
5533   if (c_parser_next_token_is (parser, CPP_COLON))
5534     {
5535       c_parser_consume_token (parser);
5536       if (c_parser_next_token_is_not (parser, CPP_NAME))
5537         {
5538           c_parser_error (parser, "expected identifier");
5539           return;
5540         }
5541       superclass = c_parser_peek_token (parser)->value;
5542       c_parser_consume_token (parser);
5543     }
5544   else
5545     superclass = NULL_TREE;
5546   if (iface_p)
5547     {
5548       tree proto = NULL_TREE;
5549       if (c_parser_next_token_is (parser, CPP_LESS))
5550         proto = c_parser_objc_protocol_refs (parser);
5551       objc_start_class_interface (id1, superclass, proto);
5552     }
5553   else
5554     objc_start_class_implementation (id1, superclass);
5555   if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
5556     c_parser_objc_class_instance_variables (parser);
5557   if (iface_p)
5558     {
5559       objc_continue_interface ();
5560       c_parser_objc_methodprotolist (parser);
5561       c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
5562       objc_finish_interface ();
5563     }
5564   else
5565     {
5566       objc_continue_implementation ();
5567       return;
5568     }
5569 }
5570
5571 /* Parse objc-class-instance-variables.
5572
5573    objc-class-instance-variables:
5574      { objc-instance-variable-decl-list[opt] }
5575
5576    objc-instance-variable-decl-list:
5577      objc-visibility-spec
5578      objc-instance-variable-decl ;
5579      ;
5580      objc-instance-variable-decl-list objc-visibility-spec
5581      objc-instance-variable-decl-list objc-instance-variable-decl ;
5582      objc-instance-variable-decl-list ;
5583
5584    objc-visibility-spec:
5585      @private
5586      @protected
5587      @public
5588
5589    objc-instance-variable-decl:
5590      struct-declaration
5591 */
5592
5593 static void
5594 c_parser_objc_class_instance_variables (c_parser *parser)
5595 {
5596   gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE));
5597   c_parser_consume_token (parser);
5598   while (c_parser_next_token_is_not (parser, CPP_EOF))
5599     {
5600       tree decls;
5601       /* Parse any stray semicolon.  */
5602       if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5603         {
5604           if (pedantic)
5605             pedwarn ("extra semicolon in struct or union specified");
5606           c_parser_consume_token (parser);
5607           continue;
5608         }
5609       /* Stop if at the end of the instance variables.  */
5610       if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
5611         {
5612           c_parser_consume_token (parser);
5613           break;
5614         }
5615       /* Parse any objc-visibility-spec.  */
5616       if (c_parser_next_token_is_keyword (parser, RID_AT_PRIVATE))
5617         {
5618           c_parser_consume_token (parser);
5619           objc_set_visibility (2);
5620           continue;
5621         }
5622       else if (c_parser_next_token_is_keyword (parser, RID_AT_PROTECTED))
5623         {
5624           c_parser_consume_token (parser);
5625           objc_set_visibility (0);
5626           continue;
5627         }
5628       else if (c_parser_next_token_is_keyword (parser, RID_AT_PUBLIC))
5629         {
5630           c_parser_consume_token (parser);
5631           objc_set_visibility (1);
5632           continue;
5633         }
5634       else if (c_parser_next_token_is (parser, CPP_PRAGMA))
5635         {
5636           c_parser_pragma (parser, pragma_external);
5637           continue;
5638         }
5639
5640       /* Parse some comma-separated declarations.  */
5641       decls = c_parser_struct_declaration (parser);
5642       {
5643         /* Comma-separated instance variables are chained together in
5644            reverse order; add them one by one.  */
5645         tree ivar = nreverse (decls);
5646         for (; ivar; ivar = TREE_CHAIN (ivar))
5647           objc_add_instance_variable (copy_node (ivar));
5648       }
5649       c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
5650     }
5651 }
5652
5653 /* Parse an objc-class-declaration.
5654
5655    objc-class-declaration:
5656      @class identifier-list ;
5657 */
5658
5659 static void
5660 c_parser_objc_class_declaration (c_parser *parser)
5661 {
5662   tree list = NULL_TREE;
5663   gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_CLASS));
5664   c_parser_consume_token (parser);
5665   /* Any identifiers, including those declared as type names, are OK
5666      here.  */
5667   while (true)
5668     {
5669       tree id;
5670       if (c_parser_next_token_is_not (parser, CPP_NAME))
5671         {
5672           c_parser_error (parser, "expected identifier");
5673           break;
5674         }
5675       id = c_parser_peek_token (parser)->value;
5676       list = chainon (list, build_tree_list (NULL_TREE, id));
5677       c_parser_consume_token (parser);
5678       if (c_parser_next_token_is (parser, CPP_COMMA))
5679         c_parser_consume_token (parser);
5680       else
5681         break;
5682     }
5683   c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
5684   objc_declare_class (list);
5685 }
5686
5687 /* Parse an objc-alias-declaration.
5688
5689    objc-alias-declaration:
5690      @compatibility_alias identifier identifier ;
5691 */
5692
5693 static void
5694 c_parser_objc_alias_declaration (c_parser *parser)
5695 {
5696   tree id1, id2;
5697   gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_ALIAS));
5698   c_parser_consume_token (parser);
5699   if (c_parser_next_token_is_not (parser, CPP_NAME))
5700     {
5701       c_parser_error (parser, "expected identifier");
5702       c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
5703       return;
5704     }
5705   id1 = c_parser_peek_token (parser)->value;
5706   c_parser_consume_token (parser);
5707   if (c_parser_next_token_is_not (parser, CPP_NAME))
5708     {
5709       c_parser_error (parser, "expected identifier");
5710       c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
5711       return;
5712     }
5713   id2 = c_parser_peek_token (parser)->value;
5714   c_parser_consume_token (parser);
5715   c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
5716   objc_declare_alias (id1, id2);
5717 }
5718
5719 /* Parse an objc-protocol-definition.
5720
5721    objc-protocol-definition:
5722      @protocol identifier objc-protocol-refs[opt] objc-methodprotolist @end
5723      @protocol identifier-list ;
5724
5725    "@protocol identifier ;" should be resolved as "@protocol
5726    identifier-list ;": objc-methodprotolist may not start with a
5727    semicolon in the first alternative if objc-protocol-refs are
5728    omitted.  */
5729
5730 static void
5731 c_parser_objc_protocol_definition (c_parser *parser)
5732 {
5733   gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_PROTOCOL));
5734   c_parser_consume_token (parser);
5735   if (c_parser_next_token_is_not (parser, CPP_NAME))
5736     {
5737       c_parser_error (parser, "expected identifier");
5738       return;
5739     }
5740   if (c_parser_peek_2nd_token (parser)->type == CPP_COMMA
5741       || c_parser_peek_2nd_token (parser)->type == CPP_SEMICOLON)
5742     {
5743       tree list = NULL_TREE;
5744       /* Any identifiers, including those declared as type names, are
5745          OK here.  */
5746       while (true)
5747         {
5748           tree id;
5749           if (c_parser_next_token_is_not (parser, CPP_NAME))
5750             {
5751               c_parser_error (parser, "expected identifier");
5752               break;
5753             }
5754           id = c_parser_peek_token (parser)->value;
5755           list = chainon (list, build_tree_list (NULL_TREE, id));
5756           c_parser_consume_token (parser);
5757           if (c_parser_next_token_is (parser, CPP_COMMA))
5758             c_parser_consume_token (parser);
5759           else
5760             break;
5761         }
5762       c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
5763       objc_declare_protocols (list);
5764     }
5765   else
5766     {
5767       tree id = c_parser_peek_token (parser)->value;
5768       tree proto = NULL_TREE;
5769       c_parser_consume_token (parser);
5770       if (c_parser_next_token_is (parser, CPP_LESS))
5771         proto = c_parser_objc_protocol_refs (parser);
5772       objc_pq_context = 1;
5773       objc_start_protocol (id, proto);
5774       c_parser_objc_methodprotolist (parser);
5775       c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
5776       objc_pq_context = 0;
5777       objc_finish_interface ();
5778     }
5779 }
5780
5781 /* Parse an objc-method-type.
5782
5783    objc-method-type:
5784      +
5785      -
5786 */
5787
5788 static enum tree_code
5789 c_parser_objc_method_type (c_parser *parser)
5790 {
5791   switch (c_parser_peek_token (parser)->type)
5792     {
5793     case CPP_PLUS:
5794       c_parser_consume_token (parser);
5795       return PLUS_EXPR;
5796     case CPP_MINUS:
5797       c_parser_consume_token (parser);
5798       return MINUS_EXPR;
5799     default:
5800       gcc_unreachable ();
5801     }
5802 }
5803
5804 /* Parse an objc-method-definition.
5805
5806    objc-method-definition:
5807      objc-method-type objc-method-decl ;[opt] compound-statement
5808 */
5809
5810 static void
5811 c_parser_objc_method_definition (c_parser *parser)
5812 {
5813   enum tree_code type = c_parser_objc_method_type (parser);
5814   tree decl;
5815   objc_set_method_type (type);
5816   objc_pq_context = 1;
5817   decl = c_parser_objc_method_decl (parser);
5818   if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5819     {
5820       c_parser_consume_token (parser);
5821       if (pedantic)
5822         pedwarn ("extra semicolon in method definition specified");
5823     }
5824   if (!c_parser_next_token_is (parser, CPP_OPEN_BRACE))
5825     {
5826       c_parser_error (parser, "expected %<{%>");
5827       return;
5828     }
5829   objc_pq_context = 0;
5830   objc_start_method_definition (decl);
5831   add_stmt (c_parser_compound_statement (parser));
5832   objc_finish_method_definition (current_function_decl);
5833 }
5834
5835 /* Parse an objc-methodprotolist.
5836
5837    objc-methodprotolist:
5838      empty
5839      objc-methodprotolist objc-methodproto
5840      objc-methodprotolist declaration
5841      objc-methodprotolist ;
5842
5843    The declaration is a data definition, which may be missing
5844    declaration specifiers under the same rules and diagnostics as
5845    other data definitions outside functions, and the stray semicolon
5846    is diagnosed the same way as a stray semicolon outside a
5847    function.  */
5848
5849 static void
5850 c_parser_objc_methodprotolist (c_parser *parser)
5851 {
5852   while (true)
5853     {
5854       /* The list is terminated by @end.  */
5855       switch (c_parser_peek_token (parser)->type)
5856         {
5857         case CPP_SEMICOLON:
5858           if (pedantic)
5859             pedwarn ("ISO C does not allow extra %<;%> outside of a function");
5860           c_parser_consume_token (parser);
5861           break;
5862         case CPP_PLUS:
5863         case CPP_MINUS:
5864           c_parser_objc_methodproto (parser);
5865           break;
5866         case CPP_PRAGMA:
5867           c_parser_pragma (parser, pragma_external);
5868           break;
5869         case CPP_EOF:
5870           return;
5871         default:
5872           if (c_parser_next_token_is_keyword (parser, RID_AT_END))
5873             return;
5874           c_parser_declaration_or_fndef (parser, false, true, false, true);
5875           break;
5876         }
5877     }
5878 }
5879
5880 /* Parse an objc-methodproto.
5881
5882    objc-methodproto:
5883      objc-method-type objc-method-decl ;
5884 */
5885
5886 static void
5887 c_parser_objc_methodproto (c_parser *parser)
5888 {
5889   enum tree_code type = c_parser_objc_method_type (parser);
5890   tree decl;
5891   objc_set_method_type (type);
5892   /* Remember protocol qualifiers in prototypes.  */
5893   objc_pq_context = 1;
5894   decl = c_parser_objc_method_decl (parser);
5895   /* Forget protocol qualifiers here.  */
5896   objc_pq_context = 0;
5897   objc_add_method_declaration (decl);
5898   c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
5899 }
5900
5901 /* Parse an objc-method-decl.
5902
5903    objc-method-decl:
5904      ( objc-type-name ) objc-selector
5905      objc-selector
5906      ( objc-type-name ) objc-keyword-selector objc-optparmlist
5907      objc-keyword-selector objc-optparmlist
5908
5909    objc-keyword-selector:
5910      objc-keyword-decl
5911      objc-keyword-selector objc-keyword-decl
5912
5913    objc-keyword-decl:
5914      objc-selector : ( objc-type-name ) identifier
5915      objc-selector : identifier
5916      : ( objc-type-name ) identifier
5917      : identifier
5918
5919    objc-optparmlist:
5920      objc-optparms objc-optellipsis
5921
5922    objc-optparms:
5923      empty
5924      objc-opt-parms , parameter-declaration
5925
5926    objc-optellipsis:
5927      empty
5928      , ...
5929 */
5930
5931 static tree
5932 c_parser_objc_method_decl (c_parser *parser)
5933 {
5934   tree type = NULL_TREE;
5935   tree sel;
5936   tree parms = NULL_TREE;
5937   bool ellipsis = false;
5938
5939   if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
5940     {
5941       c_parser_consume_token (parser);
5942       type = c_parser_objc_type_name (parser);
5943       c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
5944     }
5945   sel = c_parser_objc_selector (parser);
5946   /* If there is no selector, or a colon follows, we have an
5947      objc-keyword-selector.  If there is a selector, and a colon does
5948      not follow, that selector ends the objc-method-decl.  */
5949   if (!sel || c_parser_next_token_is (parser, CPP_COLON))
5950     {
5951       tree tsel = sel;
5952       tree list = NULL_TREE;
5953       while (true)
5954         {
5955           tree atype = NULL_TREE, id, keyworddecl;
5956           if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
5957             break;
5958           if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
5959             {
5960               c_parser_consume_token (parser);
5961               atype = c_parser_objc_type_name (parser);
5962               c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5963                                          "expected %<)%>");
5964             }
5965           if (c_parser_next_token_is_not (parser, CPP_NAME))
5966             {
5967               c_parser_error (parser, "expected identifier");
5968               return error_mark_node;
5969             }
5970           id = c_parser_peek_token (parser)->value;
5971           c_parser_consume_token (parser);
5972           keyworddecl = objc_build_keyword_decl (tsel, atype, id);
5973           list = chainon (list, keyworddecl);
5974           tsel = c_parser_objc_selector (parser);
5975           if (!tsel && c_parser_next_token_is_not (parser, CPP_COLON))
5976             break;
5977         }
5978       /* Parse the optional parameter list.  Optional Objective-C
5979          method parameters follow the C syntax, and may include '...'
5980          to denote a variable number of arguments.  */
5981       parms = make_node (TREE_LIST);
5982       while (c_parser_next_token_is (parser, CPP_COMMA))
5983         {
5984           struct c_parm *parm;
5985           c_parser_consume_token (parser);
5986           if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
5987             {
5988               ellipsis = true;
5989               c_parser_consume_token (parser);
5990               break;
5991             }
5992           parm = c_parser_parameter_declaration (parser, NULL_TREE);
5993           if (parm == NULL)
5994             break;
5995           parms = chainon (parms,
5996                            build_tree_list (NULL_TREE, grokparm (parm)));
5997         }
5998       sel = list;
5999     }
6000   return objc_build_method_signature (type, sel, parms, ellipsis);
6001 }
6002
6003 /* Parse an objc-type-name.
6004
6005    objc-type-name:
6006      objc-type-qualifiers[opt] type-name
6007      objc-type-qualifiers[opt]
6008
6009    objc-type-qualifiers:
6010      objc-type-qualifier
6011      objc-type-qualifiers objc-type-qualifier
6012
6013    objc-type-qualifier: one of
6014      in out inout bycopy byref oneway
6015 */
6016
6017 static tree
6018 c_parser_objc_type_name (c_parser *parser)
6019 {
6020   tree quals = NULL_TREE;
6021   struct c_type_name *typename = NULL;
6022   tree type = NULL_TREE;
6023   while (true)
6024     {
6025       c_token *token = c_parser_peek_token (parser);
6026       if (token->type == CPP_KEYWORD
6027           && (token->keyword == RID_IN
6028               || token->keyword == RID_OUT
6029               || token->keyword == RID_INOUT
6030               || token->keyword == RID_BYCOPY
6031               || token->keyword == RID_BYREF
6032               || token->keyword == RID_ONEWAY))
6033         {
6034           quals = chainon (quals, build_tree_list (NULL_TREE, token->value));
6035           c_parser_consume_token (parser);
6036         }
6037       else
6038         break;
6039     }
6040   if (c_parser_next_token_starts_typename (parser))
6041     typename = c_parser_type_name (parser);
6042   if (typename)
6043     type = groktypename (typename);
6044   return build_tree_list (quals, type);
6045 }
6046
6047 /* Parse objc-protocol-refs.
6048
6049    objc-protocol-refs:
6050      < identifier-list >
6051 */
6052
6053 static tree
6054 c_parser_objc_protocol_refs (c_parser *parser)
6055 {
6056   tree list = NULL_TREE;
6057   gcc_assert (c_parser_next_token_is (parser, CPP_LESS));
6058   c_parser_consume_token (parser);
6059   /* Any identifiers, including those declared as type names, are OK
6060      here.  */
6061   while (true)
6062     {
6063       tree id;
6064       if (c_parser_next_token_is_not (parser, CPP_NAME))
6065         {
6066           c_parser_error (parser, "expected identifier");
6067           break;
6068         }
6069       id = c_parser_peek_token (parser)->value;
6070       list = chainon (list, build_tree_list (NULL_TREE, id));
6071       c_parser_consume_token (parser);
6072       if (c_parser_next_token_is (parser, CPP_COMMA))
6073         c_parser_consume_token (parser);
6074       else
6075         break;
6076     }
6077   c_parser_require (parser, CPP_GREATER, "expected %<>%>");
6078   return list;
6079 }
6080
6081 /* Parse an objc-try-catch-statement.
6082
6083    objc-try-catch-statement:
6084      @try compound-statement objc-catch-list[opt]
6085      @try compound-statement objc-catch-list[opt] @finally compound-statement
6086
6087    objc-catch-list:
6088      @catch ( parameter-declaration ) compound-statement
6089      objc-catch-list @catch ( parameter-declaration ) compound-statement
6090 */
6091
6092 static void
6093 c_parser_objc_try_catch_statement (c_parser *parser)
6094 {
6095   location_t loc;
6096   tree stmt;
6097   gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_TRY));
6098   c_parser_consume_token (parser);
6099   loc = c_parser_peek_token (parser)->location;
6100   stmt = c_parser_compound_statement (parser);
6101   objc_begin_try_stmt (loc, stmt);
6102   while (c_parser_next_token_is_keyword (parser, RID_AT_CATCH))
6103     {
6104       struct c_parm *parm;
6105       c_parser_consume_token (parser);
6106       if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6107         break;
6108       parm = c_parser_parameter_declaration (parser, NULL_TREE);
6109       if (parm == NULL)
6110         {
6111           c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6112           break;
6113         }
6114       c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
6115       objc_begin_catch_clause (grokparm (parm));
6116       if (c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
6117         c_parser_compound_statement_nostart (parser);
6118       objc_finish_catch_clause ();
6119     }
6120   if (c_parser_next_token_is_keyword (parser, RID_AT_FINALLY))
6121     {
6122       location_t finloc;
6123       tree finstmt;
6124       c_parser_consume_token (parser);
6125       finloc = c_parser_peek_token (parser)->location;
6126       finstmt = c_parser_compound_statement (parser);
6127       objc_build_finally_clause (finloc, finstmt);
6128     }
6129   objc_finish_try_stmt ();
6130 }
6131
6132 /* Parse an objc-synchronized-statement.
6133
6134    objc-synchronized-statement:
6135      @synchronized ( expression ) compound-statement
6136 */
6137
6138 static void
6139 c_parser_objc_synchronized_statement (c_parser *parser)
6140 {
6141   location_t loc;
6142   tree expr, stmt;
6143   gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_SYNCHRONIZED));
6144   c_parser_consume_token (parser);
6145   loc = c_parser_peek_token (parser)->location;
6146   if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6147     {
6148       expr = c_parser_expression (parser).value;
6149       c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
6150     }
6151   else
6152     expr = error_mark_node;
6153   stmt = c_parser_compound_statement (parser);
6154   objc_build_synchronized (loc, expr, stmt);
6155 }
6156
6157 /* Parse an objc-selector; return NULL_TREE without an error if the
6158    next token is not an objc-selector.
6159
6160    objc-selector:
6161      identifier
6162      one of
6163        enum struct union if else while do for switch case default
6164        break continue return goto asm sizeof typeof __alignof
6165        unsigned long const short volatile signed restrict _Complex
6166        in out inout bycopy byref oneway int char float double void _Bool
6167
6168    ??? Why this selection of keywords but not, for example, storage
6169    class specifiers?  */
6170
6171 static tree
6172 c_parser_objc_selector (c_parser *parser)
6173 {
6174   c_token *token = c_parser_peek_token (parser);
6175   tree value = token->value;
6176   if (token->type == CPP_NAME)
6177     {
6178       c_parser_consume_token (parser);
6179       return value;
6180     }
6181   if (token->type != CPP_KEYWORD)
6182     return NULL_TREE;
6183   switch (token->keyword)
6184     {
6185     case RID_ENUM:
6186     case RID_STRUCT:
6187     case RID_UNION:
6188     case RID_IF:
6189     case RID_ELSE:
6190     case RID_WHILE:
6191     case RID_DO:
6192     case RID_FOR:
6193     case RID_SWITCH:
6194     case RID_CASE:
6195     case RID_DEFAULT:
6196     case RID_BREAK:
6197     case RID_CONTINUE:
6198     case RID_RETURN:
6199     case RID_GOTO:
6200     case RID_ASM:
6201     case RID_SIZEOF:
6202     case RID_TYPEOF:
6203     case RID_ALIGNOF:
6204     case RID_UNSIGNED:
6205     case RID_LONG:
6206     case RID_CONST:
6207     case RID_SHORT:
6208     case RID_VOLATILE:
6209     case RID_SIGNED:
6210     case RID_RESTRICT:
6211     case RID_COMPLEX:
6212     case RID_IN:
6213     case RID_OUT:
6214     case RID_INOUT:
6215     case RID_BYCOPY:
6216     case RID_BYREF:
6217     case RID_ONEWAY:
6218     case RID_INT:
6219     case RID_CHAR:
6220     case RID_FLOAT:
6221     case RID_DOUBLE:
6222     case RID_VOID:
6223     case RID_BOOL:
6224       c_parser_consume_token (parser);
6225       return value;
6226     default:
6227       return NULL_TREE;
6228     }
6229 }
6230
6231 /* Parse an objc-selector-arg.
6232
6233    objc-selector-arg:
6234      objc-selector
6235      objc-keywordname-list
6236
6237    objc-keywordname-list:
6238      objc-keywordname
6239      objc-keywordname-list objc-keywordname
6240
6241    objc-keywordname:
6242      objc-selector :
6243      :
6244 */
6245
6246 static tree
6247 c_parser_objc_selector_arg (c_parser *parser)
6248 {
6249   tree sel = c_parser_objc_selector (parser);
6250   tree list = NULL_TREE;
6251   if (sel && c_parser_next_token_is_not (parser, CPP_COLON))
6252     return sel;
6253   while (true)
6254     {
6255       if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
6256         return list;
6257       list = chainon (list, build_tree_list (sel, NULL_TREE));
6258       sel = c_parser_objc_selector (parser);
6259       if (!sel && c_parser_next_token_is_not (parser, CPP_COLON))
6260         break;
6261     }
6262   return list;
6263 }
6264
6265 /* Parse an objc-receiver.
6266
6267    objc-receiver:
6268      expression
6269      class-name
6270      type-name
6271 */
6272
6273 static tree
6274 c_parser_objc_receiver (c_parser *parser)
6275 {
6276   if (c_parser_peek_token (parser)->type == CPP_NAME
6277       && (c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME
6278           || c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME))
6279     {
6280       tree id = c_parser_peek_token (parser)->value;
6281       c_parser_consume_token (parser);
6282       return objc_get_class_reference (id);
6283     }
6284   return c_parser_expression (parser).value;
6285 }
6286
6287 /* Parse objc-message-args.
6288
6289    objc-message-args:
6290      objc-selector
6291      objc-keywordarg-list
6292
6293    objc-keywordarg-list:
6294      objc-keywordarg
6295      objc-keywordarg-list objc-keywordarg
6296
6297    objc-keywordarg:
6298      objc-selector : objc-keywordexpr
6299      : objc-keywordexpr
6300 */
6301
6302 static tree
6303 c_parser_objc_message_args (c_parser *parser)
6304 {
6305   tree sel = c_parser_objc_selector (parser);
6306   tree list = NULL_TREE;
6307   if (sel && c_parser_next_token_is_not (parser, CPP_COLON))
6308     return sel;
6309   while (true)
6310     {
6311       tree keywordexpr;
6312       if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
6313         return list;
6314       keywordexpr = c_parser_objc_keywordexpr (parser);
6315       list = chainon (list, build_tree_list (sel, keywordexpr));
6316       sel = c_parser_objc_selector (parser);
6317       if (!sel && c_parser_next_token_is_not (parser, CPP_COLON))
6318         break;
6319     }
6320   return list;
6321 }
6322
6323 /* Parse an objc-keywordexpr.
6324
6325    objc-keywordexpr:
6326      nonempty-expr-list
6327 */
6328
6329 static tree
6330 c_parser_objc_keywordexpr (c_parser *parser)
6331 {
6332   tree list = c_parser_expr_list (parser, true);
6333   if (TREE_CHAIN (list) == NULL_TREE)
6334     {
6335       /* Just return the expression, remove a level of
6336          indirection.  */
6337       return TREE_VALUE (list);
6338     }
6339   else
6340     {
6341       /* We have a comma expression, we will collapse later.  */
6342       return list;
6343     }
6344 }
6345
6346 \f
6347 /* Handle pragmas.  ALLOW_STMT is true if we're within the context of
6348    a function and such pragmas are to be allowed.  Returns true if we
6349    actually parsed such a pragma.  */
6350
6351 static bool
6352 c_parser_pragma (c_parser *parser, enum pragma_context context ATTRIBUTE_UNUSED)
6353 {
6354   unsigned int id;
6355
6356   id = c_parser_peek_token (parser)->pragma_kind;
6357   gcc_assert (id != PRAGMA_NONE);
6358
6359   switch (id)
6360     {
6361     case PRAGMA_GCC_PCH_PREPROCESS:
6362       c_parser_error (parser, "%<#pragma GCC pch_preprocess%> must be first");
6363       c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
6364       return false;
6365
6366     default:
6367       gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
6368       break;
6369     }
6370
6371   c_parser_consume_pragma (parser);
6372   c_invoke_pragma_handler (id);
6373
6374   /* Skip to EOL, but suppress any error message.  Those will have been 
6375      generated by the handler routine through calling error, as opposed
6376      to calling c_parser_error.  */
6377   parser->error = true;
6378   c_parser_skip_to_pragma_eol (parser);
6379
6380   return false;
6381 }
6382
6383 /* The interface the pragma parsers have to the lexer.  */
6384
6385 enum cpp_ttype
6386 pragma_lex (tree *value)
6387 {
6388   c_token *tok = c_parser_peek_token (the_parser);
6389   enum cpp_ttype ret = tok->type;
6390
6391   *value = tok->value;
6392   if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
6393     ret = CPP_EOF;
6394   else
6395     {
6396       if (ret == CPP_KEYWORD)
6397         ret = CPP_NAME;
6398       c_parser_consume_token (the_parser);
6399     }
6400
6401   return ret;
6402 }
6403
6404 static void
6405 c_parser_pragma_pch_preprocess (c_parser *parser)
6406 {
6407   tree name = NULL;
6408
6409   c_parser_consume_pragma (parser);
6410   if (c_parser_next_token_is (parser, CPP_STRING))
6411     {
6412       name = c_parser_peek_token (parser)->value;
6413       c_parser_consume_token (parser);
6414     }
6415   else
6416     c_parser_error (parser, "expected string literal");
6417   c_parser_skip_to_pragma_eol (parser);
6418
6419   if (name)
6420     c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
6421 }
6422 \f
6423 /* Parse a single source file.  */
6424
6425 void
6426 c_parse_file (void)
6427 {
6428   /* Use local storage to begin.  If the first token is a pragma, parse it.
6429      If it is #pragma GCC pch_preprocess, then this will load a PCH file
6430      which will cause garbage collection.  */
6431   c_parser tparser;
6432
6433   memset (&tparser, 0, sizeof tparser);
6434   the_parser = &tparser;
6435
6436   if (c_parser_peek_token (&tparser)->pragma_kind == PRAGMA_GCC_PCH_PREPROCESS)
6437     c_parser_pragma_pch_preprocess (&tparser);
6438
6439   the_parser = GGC_NEW (c_parser);
6440   *the_parser = tparser;
6441
6442   c_parser_translation_unit (the_parser);
6443   the_parser = NULL;
6444 }
6445
6446 #include "gt-c-parser.h"