OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-old/] [gcc-4.2.2/] [gcc/] [c-parser.c] - Blame information for rev 867

Go to most recent revision | Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.