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