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