]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/c/c-parser.c
c-family:
[thirdparty/gcc.git] / gcc / c / c-parser.c
1 /* Parser for C and Objective-C.
2 Copyright (C) 1987-2013 Free Software Foundation, Inc.
3
4 Parser actions based on the old Bison parser; structure somewhat
5 influenced by and fragments based on the C++ parser.
6
7 This file is part of GCC.
8
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 3, or (at your option) any later
12 version.
13
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
22
23 /* TODO:
24
25 Make sure all relevant comments, and all relevant code from all
26 actions, brought over from old parser. Verify exact correspondence
27 of syntax accepted.
28
29 Add testcases covering every input symbol in every state in old and
30 new parsers.
31
32 Include full syntax for GNU C, including erroneous cases accepted
33 with error messages, in syntax productions in comments.
34
35 Make more diagnostics in the front end generally take an explicit
36 location rather than implicitly using input_location. */
37
38 #include "config.h"
39 #include "system.h"
40 #include "coretypes.h"
41 #include "tm.h" /* For rtl.h: needs enum reg_class. */
42 #include "tree.h"
43 #include "langhooks.h"
44 #include "input.h"
45 #include "cpplib.h"
46 #include "timevar.h"
47 #include "c-family/c-pragma.h"
48 #include "c-tree.h"
49 #include "c-lang.h"
50 #include "flags.h"
51 #include "ggc.h"
52 #include "c-family/c-common.h"
53 #include "c-family/c-objc.h"
54 #include "vec.h"
55 #include "target.h"
56 #include "cgraph.h"
57 #include "plugin.h"
58 #include "omp-low.h"
59
60 \f
61 /* Initialization routine for this file. */
62
63 void
64 c_parse_init (void)
65 {
66 /* The only initialization required is of the reserved word
67 identifiers. */
68 unsigned int i;
69 tree id;
70 int mask = 0;
71
72 /* Make sure RID_MAX hasn't grown past the 8 bits used to hold the keyword in
73 the c_token structure. */
74 gcc_assert (RID_MAX <= 255);
75
76 mask |= D_CXXONLY;
77 if (!flag_isoc99)
78 mask |= D_C99;
79 if (flag_no_asm)
80 {
81 mask |= D_ASM | D_EXT;
82 if (!flag_isoc99)
83 mask |= D_EXT89;
84 }
85 if (!c_dialect_objc ())
86 mask |= D_OBJC | D_CXX_OBJC;
87
88 ridpointers = ggc_alloc_cleared_vec_tree ((int) RID_MAX);
89 for (i = 0; i < num_c_common_reswords; i++)
90 {
91 /* If a keyword is disabled, do not enter it into the table
92 and so create a canonical spelling that isn't a keyword. */
93 if (c_common_reswords[i].disable & mask)
94 {
95 if (warn_cxx_compat
96 && (c_common_reswords[i].disable & D_CXXWARN))
97 {
98 id = get_identifier (c_common_reswords[i].word);
99 C_SET_RID_CODE (id, RID_CXX_COMPAT_WARN);
100 C_IS_RESERVED_WORD (id) = 1;
101 }
102 continue;
103 }
104
105 id = get_identifier (c_common_reswords[i].word);
106 C_SET_RID_CODE (id, c_common_reswords[i].rid);
107 C_IS_RESERVED_WORD (id) = 1;
108 ridpointers [(int) c_common_reswords[i].rid] = id;
109 }
110 }
111 \f
112 /* The C lexer intermediates between the lexer in cpplib and c-lex.c
113 and the C parser. Unlike the C++ lexer, the parser structure
114 stores the lexer information instead of using a separate structure.
115 Identifiers are separated into ordinary identifiers, type names,
116 keywords and some other Objective-C types of identifiers, and some
117 look-ahead is maintained.
118
119 ??? It might be a good idea to lex the whole file up front (as for
120 C++). It would then be possible to share more of the C and C++
121 lexer code, if desired. */
122
123 /* The following local token type is used. */
124
125 /* A keyword. */
126 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
127
128 /* More information about the type of a CPP_NAME token. */
129 typedef enum c_id_kind {
130 /* An ordinary identifier. */
131 C_ID_ID,
132 /* An identifier declared as a typedef name. */
133 C_ID_TYPENAME,
134 /* An identifier declared as an Objective-C class name. */
135 C_ID_CLASSNAME,
136 /* An address space identifier. */
137 C_ID_ADDRSPACE,
138 /* Not an identifier. */
139 C_ID_NONE
140 } c_id_kind;
141
142 /* A single C token after string literal concatenation and conversion
143 of preprocessing tokens to tokens. */
144 typedef struct GTY (()) c_token {
145 /* The kind of token. */
146 ENUM_BITFIELD (cpp_ttype) type : 8;
147 /* If this token is a CPP_NAME, this value indicates whether also
148 declared as some kind of type. Otherwise, it is C_ID_NONE. */
149 ENUM_BITFIELD (c_id_kind) id_kind : 8;
150 /* If this token is a keyword, this value indicates which keyword.
151 Otherwise, this value is RID_MAX. */
152 ENUM_BITFIELD (rid) keyword : 8;
153 /* If this token is a CPP_PRAGMA, this indicates the pragma that
154 was seen. Otherwise it is PRAGMA_NONE. */
155 ENUM_BITFIELD (pragma_kind) pragma_kind : 8;
156 /* The location at which this token was found. */
157 location_t location;
158 /* The value associated with this token, if any. */
159 tree value;
160 } c_token;
161
162 /* A parser structure recording information about the state and
163 context of parsing. Includes lexer information with up to two
164 tokens of look-ahead; more are not needed for C. */
165 typedef struct GTY(()) c_parser {
166 /* The look-ahead tokens. */
167 c_token * GTY((skip)) tokens;
168 /* Buffer for look-ahead tokens. */
169 c_token tokens_buf[2];
170 /* How many look-ahead tokens are available (0, 1 or 2, or
171 more if parsing from pre-lexed tokens). */
172 unsigned int tokens_avail;
173 /* True if a syntax error is being recovered from; false otherwise.
174 c_parser_error sets this flag. It should clear this flag when
175 enough tokens have been consumed to recover from the error. */
176 BOOL_BITFIELD error : 1;
177 /* True if we're processing a pragma, and shouldn't automatically
178 consume CPP_PRAGMA_EOL. */
179 BOOL_BITFIELD in_pragma : 1;
180 /* True if we're parsing the outermost block of an if statement. */
181 BOOL_BITFIELD in_if_block : 1;
182 /* True if we want to lex an untranslated string. */
183 BOOL_BITFIELD lex_untranslated_string : 1;
184
185 /* Objective-C specific parser/lexer information. */
186
187 /* True if we are in a context where the Objective-C "PQ" keywords
188 are considered keywords. */
189 BOOL_BITFIELD objc_pq_context : 1;
190 /* True if we are parsing a (potential) Objective-C foreach
191 statement. This is set to true after we parsed 'for (' and while
192 we wait for 'in' or ';' to decide if it's a standard C for loop or an
193 Objective-C foreach loop. */
194 BOOL_BITFIELD objc_could_be_foreach_context : 1;
195 /* The following flag is needed to contextualize Objective-C lexical
196 analysis. In some cases (e.g., 'int NSObject;'), it is
197 undesirable to bind an identifier to an Objective-C class, even
198 if a class with that name exists. */
199 BOOL_BITFIELD objc_need_raw_identifier : 1;
200 /* Nonzero if we're processing a __transaction statement. The value
201 is 1 | TM_STMT_ATTR_*. */
202 unsigned int in_transaction : 4;
203 /* True if we are in a context where the Objective-C "Property attribute"
204 keywords are valid. */
205 BOOL_BITFIELD objc_property_attr_context : 1;
206 } c_parser;
207
208
209 /* The actual parser and external interface. ??? Does this need to be
210 garbage-collected? */
211
212 static GTY (()) c_parser *the_parser;
213
214 /* Read in and lex a single token, storing it in *TOKEN. */
215
216 static void
217 c_lex_one_token (c_parser *parser, c_token *token)
218 {
219 timevar_push (TV_LEX);
220
221 token->type = c_lex_with_flags (&token->value, &token->location, NULL,
222 (parser->lex_untranslated_string
223 ? C_LEX_STRING_NO_TRANSLATE : 0));
224 token->id_kind = C_ID_NONE;
225 token->keyword = RID_MAX;
226 token->pragma_kind = PRAGMA_NONE;
227
228 switch (token->type)
229 {
230 case CPP_NAME:
231 {
232 tree decl;
233
234 bool objc_force_identifier = parser->objc_need_raw_identifier;
235 if (c_dialect_objc ())
236 parser->objc_need_raw_identifier = false;
237
238 if (C_IS_RESERVED_WORD (token->value))
239 {
240 enum rid rid_code = C_RID_CODE (token->value);
241
242 if (rid_code == RID_CXX_COMPAT_WARN)
243 {
244 warning_at (token->location,
245 OPT_Wc___compat,
246 "identifier %qE conflicts with C++ keyword",
247 token->value);
248 }
249 else if (rid_code >= RID_FIRST_ADDR_SPACE
250 && rid_code <= RID_LAST_ADDR_SPACE)
251 {
252 token->id_kind = C_ID_ADDRSPACE;
253 token->keyword = rid_code;
254 break;
255 }
256 else if (c_dialect_objc () && OBJC_IS_PQ_KEYWORD (rid_code))
257 {
258 /* We found an Objective-C "pq" keyword (in, out,
259 inout, bycopy, byref, oneway). They need special
260 care because the interpretation depends on the
261 context. */
262 if (parser->objc_pq_context)
263 {
264 token->type = CPP_KEYWORD;
265 token->keyword = rid_code;
266 break;
267 }
268 else if (parser->objc_could_be_foreach_context
269 && rid_code == RID_IN)
270 {
271 /* We are in Objective-C, inside a (potential)
272 foreach context (which means after having
273 parsed 'for (', but before having parsed ';'),
274 and we found 'in'. We consider it the keyword
275 which terminates the declaration at the
276 beginning of a foreach-statement. Note that
277 this means you can't use 'in' for anything else
278 in that context; in particular, in Objective-C
279 you can't use 'in' as the name of the running
280 variable in a C for loop. We could potentially
281 try to add code here to disambiguate, but it
282 seems a reasonable limitation. */
283 token->type = CPP_KEYWORD;
284 token->keyword = rid_code;
285 break;
286 }
287 /* Else, "pq" keywords outside of the "pq" context are
288 not keywords, and we fall through to the code for
289 normal tokens. */
290 }
291 else if (c_dialect_objc () && OBJC_IS_PATTR_KEYWORD (rid_code))
292 {
293 /* We found an Objective-C "property attribute"
294 keyword (getter, setter, readonly, etc). These are
295 only valid in the property context. */
296 if (parser->objc_property_attr_context)
297 {
298 token->type = CPP_KEYWORD;
299 token->keyword = rid_code;
300 break;
301 }
302 /* Else they are not special keywords.
303 */
304 }
305 else if (c_dialect_objc ()
306 && (OBJC_IS_AT_KEYWORD (rid_code)
307 || OBJC_IS_CXX_KEYWORD (rid_code)))
308 {
309 /* We found one of the Objective-C "@" keywords (defs,
310 selector, synchronized, etc) or one of the
311 Objective-C "cxx" keywords (class, private,
312 protected, public, try, catch, throw) without a
313 preceding '@' sign. Do nothing and fall through to
314 the code for normal tokens (in C++ we would still
315 consider the CXX ones keywords, but not in C). */
316 ;
317 }
318 else
319 {
320 token->type = CPP_KEYWORD;
321 token->keyword = rid_code;
322 break;
323 }
324 }
325
326 decl = lookup_name (token->value);
327 if (decl)
328 {
329 if (TREE_CODE (decl) == TYPE_DECL)
330 {
331 token->id_kind = C_ID_TYPENAME;
332 break;
333 }
334 }
335 else if (c_dialect_objc ())
336 {
337 tree objc_interface_decl = objc_is_class_name (token->value);
338 /* Objective-C class names are in the same namespace as
339 variables and typedefs, and hence are shadowed by local
340 declarations. */
341 if (objc_interface_decl
342 && (!objc_force_identifier || global_bindings_p ()))
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 a pointer to the next-but-one token from PARSER, reading it
437 in if necessary. The next token is already read in. */
438
439 static c_token *
440 c_parser_peek_2nd_token (c_parser *parser)
441 {
442 if (parser->tokens_avail >= 2)
443 return &parser->tokens[1];
444 gcc_assert (parser->tokens_avail == 1);
445 gcc_assert (parser->tokens[0].type != CPP_EOF);
446 gcc_assert (parser->tokens[0].type != CPP_PRAGMA_EOL);
447 c_lex_one_token (parser, &parser->tokens[1]);
448 parser->tokens_avail = 2;
449 return &parser->tokens[1];
450 }
451
452 /* Return true if TOKEN can start a type name,
453 false otherwise. */
454 static bool
455 c_token_starts_typename (c_token *token)
456 {
457 switch (token->type)
458 {
459 case CPP_NAME:
460 switch (token->id_kind)
461 {
462 case C_ID_ID:
463 return false;
464 case C_ID_ADDRSPACE:
465 return true;
466 case C_ID_TYPENAME:
467 return true;
468 case C_ID_CLASSNAME:
469 gcc_assert (c_dialect_objc ());
470 return true;
471 default:
472 gcc_unreachable ();
473 }
474 case CPP_KEYWORD:
475 switch (token->keyword)
476 {
477 case RID_UNSIGNED:
478 case RID_LONG:
479 case RID_INT128:
480 case RID_SHORT:
481 case RID_SIGNED:
482 case RID_COMPLEX:
483 case RID_INT:
484 case RID_CHAR:
485 case RID_FLOAT:
486 case RID_DOUBLE:
487 case RID_VOID:
488 case RID_DFLOAT32:
489 case RID_DFLOAT64:
490 case RID_DFLOAT128:
491 case RID_BOOL:
492 case RID_ENUM:
493 case RID_STRUCT:
494 case RID_UNION:
495 case RID_TYPEOF:
496 case RID_CONST:
497 case RID_ATOMIC:
498 case RID_VOLATILE:
499 case RID_RESTRICT:
500 case RID_ATTRIBUTE:
501 case RID_FRACT:
502 case RID_ACCUM:
503 case RID_SAT:
504 return true;
505 default:
506 return false;
507 }
508 case CPP_LESS:
509 if (c_dialect_objc ())
510 return true;
511 return false;
512 default:
513 return false;
514 }
515 }
516
517 enum c_lookahead_kind {
518 /* Always treat unknown identifiers as typenames. */
519 cla_prefer_type,
520
521 /* Could be parsing a nonabstract declarator. Only treat an identifier
522 as a typename if followed by another identifier or a star. */
523 cla_nonabstract_decl,
524
525 /* Never treat identifiers as typenames. */
526 cla_prefer_id
527 };
528
529 /* Return true if the next token from PARSER can start a type name,
530 false otherwise. LA specifies how to do lookahead in order to
531 detect unknown type names. If unsure, pick CLA_PREFER_ID. */
532
533 static inline bool
534 c_parser_next_tokens_start_typename (c_parser *parser, enum c_lookahead_kind la)
535 {
536 c_token *token = c_parser_peek_token (parser);
537 if (c_token_starts_typename (token))
538 return true;
539
540 /* Try a bit harder to detect an unknown typename. */
541 if (la != cla_prefer_id
542 && token->type == CPP_NAME
543 && token->id_kind == C_ID_ID
544
545 /* Do not try too hard when we could have "object in array". */
546 && !parser->objc_could_be_foreach_context
547
548 && (la == cla_prefer_type
549 || c_parser_peek_2nd_token (parser)->type == CPP_NAME
550 || c_parser_peek_2nd_token (parser)->type == CPP_MULT)
551
552 /* Only unknown identifiers. */
553 && !lookup_name (token->value))
554 return true;
555
556 return false;
557 }
558
559 /* Return true if TOKEN is a type qualifier, false otherwise. */
560 static bool
561 c_token_is_qualifier (c_token *token)
562 {
563 switch (token->type)
564 {
565 case CPP_NAME:
566 switch (token->id_kind)
567 {
568 case C_ID_ADDRSPACE:
569 return true;
570 default:
571 return false;
572 }
573 case CPP_KEYWORD:
574 switch (token->keyword)
575 {
576 case RID_CONST:
577 case RID_VOLATILE:
578 case RID_RESTRICT:
579 case RID_ATTRIBUTE:
580 case RID_ATOMIC:
581 return true;
582 default:
583 return false;
584 }
585 case CPP_LESS:
586 return false;
587 default:
588 gcc_unreachable ();
589 }
590 }
591
592 /* Return true if the next token from PARSER is a type qualifier,
593 false otherwise. */
594 static inline bool
595 c_parser_next_token_is_qualifier (c_parser *parser)
596 {
597 c_token *token = c_parser_peek_token (parser);
598 return c_token_is_qualifier (token);
599 }
600
601 /* Return true if TOKEN can start declaration specifiers, false
602 otherwise. */
603 static bool
604 c_token_starts_declspecs (c_token *token)
605 {
606 switch (token->type)
607 {
608 case CPP_NAME:
609 switch (token->id_kind)
610 {
611 case C_ID_ID:
612 return false;
613 case C_ID_ADDRSPACE:
614 return true;
615 case C_ID_TYPENAME:
616 return true;
617 case C_ID_CLASSNAME:
618 gcc_assert (c_dialect_objc ());
619 return true;
620 default:
621 gcc_unreachable ();
622 }
623 case CPP_KEYWORD:
624 switch (token->keyword)
625 {
626 case RID_STATIC:
627 case RID_EXTERN:
628 case RID_REGISTER:
629 case RID_TYPEDEF:
630 case RID_INLINE:
631 case RID_NORETURN:
632 case RID_AUTO:
633 case RID_THREAD:
634 case RID_UNSIGNED:
635 case RID_LONG:
636 case RID_INT128:
637 case RID_SHORT:
638 case RID_SIGNED:
639 case RID_COMPLEX:
640 case RID_INT:
641 case RID_CHAR:
642 case RID_FLOAT:
643 case RID_DOUBLE:
644 case RID_VOID:
645 case RID_DFLOAT32:
646 case RID_DFLOAT64:
647 case RID_DFLOAT128:
648 case RID_BOOL:
649 case RID_ENUM:
650 case RID_STRUCT:
651 case RID_UNION:
652 case RID_TYPEOF:
653 case RID_CONST:
654 case RID_VOLATILE:
655 case RID_RESTRICT:
656 case RID_ATTRIBUTE:
657 case RID_FRACT:
658 case RID_ACCUM:
659 case RID_SAT:
660 case RID_ALIGNAS:
661 case RID_ATOMIC:
662 return true;
663 default:
664 return false;
665 }
666 case CPP_LESS:
667 if (c_dialect_objc ())
668 return true;
669 return false;
670 default:
671 return false;
672 }
673 }
674
675
676 /* Return true if TOKEN can start declaration specifiers or a static
677 assertion, false otherwise. */
678 static bool
679 c_token_starts_declaration (c_token *token)
680 {
681 if (c_token_starts_declspecs (token)
682 || token->keyword == RID_STATIC_ASSERT)
683 return true;
684 else
685 return false;
686 }
687
688 /* Return true if the next token from PARSER can start declaration
689 specifiers, false otherwise. */
690 static inline bool
691 c_parser_next_token_starts_declspecs (c_parser *parser)
692 {
693 c_token *token = c_parser_peek_token (parser);
694
695 /* In Objective-C, a classname normally starts a declspecs unless it
696 is immediately followed by a dot. In that case, it is the
697 Objective-C 2.0 "dot-syntax" for class objects, ie, calls the
698 setter/getter on the class. c_token_starts_declspecs() can't
699 differentiate between the two cases because it only checks the
700 current token, so we have a special check here. */
701 if (c_dialect_objc ()
702 && token->type == CPP_NAME
703 && token->id_kind == C_ID_CLASSNAME
704 && c_parser_peek_2nd_token (parser)->type == CPP_DOT)
705 return false;
706
707 return c_token_starts_declspecs (token);
708 }
709
710 /* Return true if the next tokens from PARSER can start declaration
711 specifiers or a static assertion, false otherwise. */
712 static inline bool
713 c_parser_next_tokens_start_declaration (c_parser *parser)
714 {
715 c_token *token = c_parser_peek_token (parser);
716
717 /* Same as above. */
718 if (c_dialect_objc ()
719 && token->type == CPP_NAME
720 && token->id_kind == C_ID_CLASSNAME
721 && c_parser_peek_2nd_token (parser)->type == CPP_DOT)
722 return false;
723
724 /* Labels do not start declarations. */
725 if (token->type == CPP_NAME
726 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
727 return false;
728
729 if (c_token_starts_declaration (token))
730 return true;
731
732 if (c_parser_next_tokens_start_typename (parser, cla_nonabstract_decl))
733 return true;
734
735 return false;
736 }
737
738 /* Consume the next token from PARSER. */
739
740 static void
741 c_parser_consume_token (c_parser *parser)
742 {
743 gcc_assert (parser->tokens_avail >= 1);
744 gcc_assert (parser->tokens[0].type != CPP_EOF);
745 gcc_assert (!parser->in_pragma || parser->tokens[0].type != CPP_PRAGMA_EOL);
746 gcc_assert (parser->error || parser->tokens[0].type != CPP_PRAGMA);
747 if (parser->tokens != &parser->tokens_buf[0])
748 parser->tokens++;
749 else if (parser->tokens_avail == 2)
750 parser->tokens[0] = parser->tokens[1];
751 parser->tokens_avail--;
752 }
753
754 /* Expect the current token to be a #pragma. Consume it and remember
755 that we've begun parsing a pragma. */
756
757 static void
758 c_parser_consume_pragma (c_parser *parser)
759 {
760 gcc_assert (!parser->in_pragma);
761 gcc_assert (parser->tokens_avail >= 1);
762 gcc_assert (parser->tokens[0].type == CPP_PRAGMA);
763 if (parser->tokens != &parser->tokens_buf[0])
764 parser->tokens++;
765 else if (parser->tokens_avail == 2)
766 parser->tokens[0] = parser->tokens[1];
767 parser->tokens_avail--;
768 parser->in_pragma = true;
769 }
770
771 /* Update the globals input_location and in_system_header from
772 TOKEN. */
773 static inline void
774 c_parser_set_source_position_from_token (c_token *token)
775 {
776 if (token->type != CPP_EOF)
777 {
778 input_location = token->location;
779 }
780 }
781
782 /* Issue a diagnostic of the form
783 FILE:LINE: MESSAGE before TOKEN
784 where TOKEN is the next token in the input stream of PARSER.
785 MESSAGE (specified by the caller) is usually of the form "expected
786 OTHER-TOKEN".
787
788 Do not issue a diagnostic if still recovering from an error.
789
790 ??? This is taken from the C++ parser, but building up messages in
791 this way is not i18n-friendly and some other approach should be
792 used. */
793
794 static void
795 c_parser_error (c_parser *parser, const char *gmsgid)
796 {
797 c_token *token = c_parser_peek_token (parser);
798 if (parser->error)
799 return;
800 parser->error = true;
801 if (!gmsgid)
802 return;
803 /* This diagnostic makes more sense if it is tagged to the line of
804 the token we just peeked at. */
805 c_parser_set_source_position_from_token (token);
806 c_parse_error (gmsgid,
807 /* Because c_parse_error does not understand
808 CPP_KEYWORD, keywords are treated like
809 identifiers. */
810 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
811 /* ??? The C parser does not save the cpp flags of a
812 token, we need to pass 0 here and we will not get
813 the source spelling of some tokens but rather the
814 canonical spelling. */
815 token->value, /*flags=*/0);
816 }
817
818 /* If the next token is of the indicated TYPE, consume it. Otherwise,
819 issue the error MSGID. If MSGID is NULL then a message has already
820 been produced and no message will be produced this time. Returns
821 true if found, false otherwise. */
822
823 static bool
824 c_parser_require (c_parser *parser,
825 enum cpp_ttype type,
826 const char *msgid)
827 {
828 if (c_parser_next_token_is (parser, type))
829 {
830 c_parser_consume_token (parser);
831 return true;
832 }
833 else
834 {
835 c_parser_error (parser, msgid);
836 return false;
837 }
838 }
839
840 /* If the next token is the indicated keyword, consume it. Otherwise,
841 issue the error MSGID. Returns true if found, false otherwise. */
842
843 static bool
844 c_parser_require_keyword (c_parser *parser,
845 enum rid keyword,
846 const char *msgid)
847 {
848 if (c_parser_next_token_is_keyword (parser, keyword))
849 {
850 c_parser_consume_token (parser);
851 return true;
852 }
853 else
854 {
855 c_parser_error (parser, msgid);
856 return false;
857 }
858 }
859
860 /* Like c_parser_require, except that tokens will be skipped until the
861 desired token is found. An error message is still produced if the
862 next token is not as expected. If MSGID is NULL then a message has
863 already been produced and no message will be produced this
864 time. */
865
866 static void
867 c_parser_skip_until_found (c_parser *parser,
868 enum cpp_ttype type,
869 const char *msgid)
870 {
871 unsigned nesting_depth = 0;
872
873 if (c_parser_require (parser, type, msgid))
874 return;
875
876 /* Skip tokens until the desired token is found. */
877 while (true)
878 {
879 /* Peek at the next token. */
880 c_token *token = c_parser_peek_token (parser);
881 /* If we've reached the token we want, consume it and stop. */
882 if (token->type == type && !nesting_depth)
883 {
884 c_parser_consume_token (parser);
885 break;
886 }
887
888 /* If we've run out of tokens, stop. */
889 if (token->type == CPP_EOF)
890 return;
891 if (token->type == CPP_PRAGMA_EOL && parser->in_pragma)
892 return;
893 if (token->type == CPP_OPEN_BRACE
894 || token->type == CPP_OPEN_PAREN
895 || token->type == CPP_OPEN_SQUARE)
896 ++nesting_depth;
897 else if (token->type == CPP_CLOSE_BRACE
898 || token->type == CPP_CLOSE_PAREN
899 || token->type == CPP_CLOSE_SQUARE)
900 {
901 if (nesting_depth-- == 0)
902 break;
903 }
904 /* Consume this token. */
905 c_parser_consume_token (parser);
906 }
907 parser->error = false;
908 }
909
910 /* Skip tokens until the end of a parameter is found, but do not
911 consume the comma, semicolon or closing delimiter. */
912
913 static void
914 c_parser_skip_to_end_of_parameter (c_parser *parser)
915 {
916 unsigned nesting_depth = 0;
917
918 while (true)
919 {
920 c_token *token = c_parser_peek_token (parser);
921 if ((token->type == CPP_COMMA || token->type == CPP_SEMICOLON)
922 && !nesting_depth)
923 break;
924 /* If we've run out of tokens, stop. */
925 if (token->type == CPP_EOF)
926 return;
927 if (token->type == CPP_PRAGMA_EOL && parser->in_pragma)
928 return;
929 if (token->type == CPP_OPEN_BRACE
930 || token->type == CPP_OPEN_PAREN
931 || token->type == CPP_OPEN_SQUARE)
932 ++nesting_depth;
933 else if (token->type == CPP_CLOSE_BRACE
934 || token->type == CPP_CLOSE_PAREN
935 || token->type == CPP_CLOSE_SQUARE)
936 {
937 if (nesting_depth-- == 0)
938 break;
939 }
940 /* Consume this token. */
941 c_parser_consume_token (parser);
942 }
943 parser->error = false;
944 }
945
946 /* Expect to be at the end of the pragma directive and consume an
947 end of line marker. */
948
949 static void
950 c_parser_skip_to_pragma_eol (c_parser *parser)
951 {
952 gcc_assert (parser->in_pragma);
953 parser->in_pragma = false;
954
955 if (!c_parser_require (parser, CPP_PRAGMA_EOL, "expected end of line"))
956 while (true)
957 {
958 c_token *token = c_parser_peek_token (parser);
959 if (token->type == CPP_EOF)
960 break;
961 if (token->type == CPP_PRAGMA_EOL)
962 {
963 c_parser_consume_token (parser);
964 break;
965 }
966 c_parser_consume_token (parser);
967 }
968
969 parser->error = false;
970 }
971
972 /* Skip tokens until we have consumed an entire block, or until we
973 have consumed a non-nested ';'. */
974
975 static void
976 c_parser_skip_to_end_of_block_or_statement (c_parser *parser)
977 {
978 unsigned nesting_depth = 0;
979 bool save_error = parser->error;
980
981 while (true)
982 {
983 c_token *token;
984
985 /* Peek at the next token. */
986 token = c_parser_peek_token (parser);
987
988 switch (token->type)
989 {
990 case CPP_EOF:
991 return;
992
993 case CPP_PRAGMA_EOL:
994 if (parser->in_pragma)
995 return;
996 break;
997
998 case CPP_SEMICOLON:
999 /* If the next token is a ';', we have reached the
1000 end of the statement. */
1001 if (!nesting_depth)
1002 {
1003 /* Consume the ';'. */
1004 c_parser_consume_token (parser);
1005 goto finished;
1006 }
1007 break;
1008
1009 case CPP_CLOSE_BRACE:
1010 /* If the next token is a non-nested '}', then we have
1011 reached the end of the current block. */
1012 if (nesting_depth == 0 || --nesting_depth == 0)
1013 {
1014 c_parser_consume_token (parser);
1015 goto finished;
1016 }
1017 break;
1018
1019 case CPP_OPEN_BRACE:
1020 /* If it the next token is a '{', then we are entering a new
1021 block. Consume the entire block. */
1022 ++nesting_depth;
1023 break;
1024
1025 case CPP_PRAGMA:
1026 /* If we see a pragma, consume the whole thing at once. We
1027 have some safeguards against consuming pragmas willy-nilly.
1028 Normally, we'd expect to be here with parser->error set,
1029 which disables these safeguards. But it's possible to get
1030 here for secondary error recovery, after parser->error has
1031 been cleared. */
1032 c_parser_consume_pragma (parser);
1033 c_parser_skip_to_pragma_eol (parser);
1034 parser->error = save_error;
1035 continue;
1036
1037 default:
1038 break;
1039 }
1040
1041 c_parser_consume_token (parser);
1042 }
1043
1044 finished:
1045 parser->error = false;
1046 }
1047
1048 /* CPP's options (initialized by c-opts.c). */
1049 extern cpp_options *cpp_opts;
1050
1051 /* Save the warning flags which are controlled by __extension__. */
1052
1053 static inline int
1054 disable_extension_diagnostics (void)
1055 {
1056 int ret = (pedantic
1057 | (warn_pointer_arith << 1)
1058 | (warn_traditional << 2)
1059 | (flag_iso << 3)
1060 | (warn_long_long << 4)
1061 | (warn_cxx_compat << 5)
1062 | (warn_overlength_strings << 6));
1063 cpp_opts->cpp_pedantic = pedantic = 0;
1064 warn_pointer_arith = 0;
1065 cpp_opts->cpp_warn_traditional = warn_traditional = 0;
1066 flag_iso = 0;
1067 cpp_opts->cpp_warn_long_long = warn_long_long = 0;
1068 warn_cxx_compat = 0;
1069 warn_overlength_strings = 0;
1070 return ret;
1071 }
1072
1073 /* Restore the warning flags which are controlled by __extension__.
1074 FLAGS is the return value from disable_extension_diagnostics. */
1075
1076 static inline void
1077 restore_extension_diagnostics (int flags)
1078 {
1079 cpp_opts->cpp_pedantic = pedantic = flags & 1;
1080 warn_pointer_arith = (flags >> 1) & 1;
1081 cpp_opts->cpp_warn_traditional = warn_traditional = (flags >> 2) & 1;
1082 flag_iso = (flags >> 3) & 1;
1083 cpp_opts->cpp_warn_long_long = warn_long_long = (flags >> 4) & 1;
1084 warn_cxx_compat = (flags >> 5) & 1;
1085 warn_overlength_strings = (flags >> 6) & 1;
1086 }
1087
1088 /* Possibly kinds of declarator to parse. */
1089 typedef enum c_dtr_syn {
1090 /* A normal declarator with an identifier. */
1091 C_DTR_NORMAL,
1092 /* An abstract declarator (maybe empty). */
1093 C_DTR_ABSTRACT,
1094 /* A parameter declarator: may be either, but after a type name does
1095 not redeclare a typedef name as an identifier if it can
1096 alternatively be interpreted as a typedef name; see DR#009,
1097 applied in C90 TC1, omitted from C99 and reapplied in C99 TC2
1098 following DR#249. For example, given a typedef T, "int T" and
1099 "int *T" are valid parameter declarations redeclaring T, while
1100 "int (T)" and "int * (T)" and "int (T[])" and "int (T (int))" are
1101 abstract declarators rather than involving redundant parentheses;
1102 the same applies with attributes inside the parentheses before
1103 "T". */
1104 C_DTR_PARM
1105 } c_dtr_syn;
1106
1107 /* The binary operation precedence levels, where 0 is a dummy lowest level
1108 used for the bottom of the stack. */
1109 enum c_parser_prec {
1110 PREC_NONE,
1111 PREC_LOGOR,
1112 PREC_LOGAND,
1113 PREC_BITOR,
1114 PREC_BITXOR,
1115 PREC_BITAND,
1116 PREC_EQ,
1117 PREC_REL,
1118 PREC_SHIFT,
1119 PREC_ADD,
1120 PREC_MULT,
1121 NUM_PRECS
1122 };
1123
1124 static void c_parser_external_declaration (c_parser *);
1125 static void c_parser_asm_definition (c_parser *);
1126 static void c_parser_declaration_or_fndef (c_parser *, bool, bool, bool,
1127 bool, bool, tree *, vec<c_token>);
1128 static void c_parser_static_assert_declaration_no_semi (c_parser *);
1129 static void c_parser_static_assert_declaration (c_parser *);
1130 static void c_parser_declspecs (c_parser *, struct c_declspecs *, bool, bool,
1131 bool, bool, enum c_lookahead_kind);
1132 static struct c_typespec c_parser_enum_specifier (c_parser *);
1133 static struct c_typespec c_parser_struct_or_union_specifier (c_parser *);
1134 static tree c_parser_struct_declaration (c_parser *);
1135 static struct c_typespec c_parser_typeof_specifier (c_parser *);
1136 static tree c_parser_alignas_specifier (c_parser *);
1137 static struct c_declarator *c_parser_declarator (c_parser *, bool, c_dtr_syn,
1138 bool *);
1139 static struct c_declarator *c_parser_direct_declarator (c_parser *, bool,
1140 c_dtr_syn, bool *);
1141 static struct c_declarator *c_parser_direct_declarator_inner (c_parser *,
1142 bool,
1143 struct c_declarator *);
1144 static struct c_arg_info *c_parser_parms_declarator (c_parser *, bool, tree);
1145 static struct c_arg_info *c_parser_parms_list_declarator (c_parser *, tree,
1146 tree);
1147 static struct c_parm *c_parser_parameter_declaration (c_parser *, tree);
1148 static tree c_parser_simple_asm_expr (c_parser *);
1149 static tree c_parser_attributes (c_parser *);
1150 static struct c_type_name *c_parser_type_name (c_parser *);
1151 static struct c_expr c_parser_initializer (c_parser *);
1152 static struct c_expr c_parser_braced_init (c_parser *, tree, bool);
1153 static void c_parser_initelt (c_parser *, struct obstack *);
1154 static void c_parser_initval (c_parser *, struct c_expr *,
1155 struct obstack *);
1156 static tree c_parser_compound_statement (c_parser *);
1157 static void c_parser_compound_statement_nostart (c_parser *);
1158 static void c_parser_label (c_parser *);
1159 static void c_parser_statement (c_parser *);
1160 static void c_parser_statement_after_labels (c_parser *);
1161 static void c_parser_if_statement (c_parser *);
1162 static void c_parser_switch_statement (c_parser *);
1163 static void c_parser_while_statement (c_parser *, bool);
1164 static void c_parser_do_statement (c_parser *, bool);
1165 static void c_parser_for_statement (c_parser *, bool);
1166 static tree c_parser_asm_statement (c_parser *);
1167 static tree c_parser_asm_operands (c_parser *);
1168 static tree c_parser_asm_goto_operands (c_parser *);
1169 static tree c_parser_asm_clobbers (c_parser *);
1170 static struct c_expr c_parser_expr_no_commas (c_parser *, struct c_expr *,
1171 tree = NULL_TREE);
1172 static struct c_expr c_parser_conditional_expression (c_parser *,
1173 struct c_expr *, tree);
1174 static struct c_expr c_parser_binary_expression (c_parser *, struct c_expr *,
1175 tree);
1176 static struct c_expr c_parser_cast_expression (c_parser *, struct c_expr *);
1177 static struct c_expr c_parser_unary_expression (c_parser *);
1178 static struct c_expr c_parser_sizeof_expression (c_parser *);
1179 static struct c_expr c_parser_alignof_expression (c_parser *);
1180 static struct c_expr c_parser_postfix_expression (c_parser *);
1181 static struct c_expr c_parser_postfix_expression_after_paren_type (c_parser *,
1182 struct c_type_name *,
1183 location_t);
1184 static struct c_expr c_parser_postfix_expression_after_primary (c_parser *,
1185 location_t loc,
1186 struct c_expr);
1187 static tree c_parser_transaction (c_parser *, enum rid);
1188 static struct c_expr c_parser_transaction_expression (c_parser *, enum rid);
1189 static tree c_parser_transaction_cancel (c_parser *);
1190 static struct c_expr c_parser_expression (c_parser *);
1191 static struct c_expr c_parser_expression_conv (c_parser *);
1192 static vec<tree, va_gc> *c_parser_expr_list (c_parser *, bool, bool,
1193 vec<tree, va_gc> **, location_t *,
1194 tree *);
1195 static void c_parser_omp_construct (c_parser *);
1196 static void c_parser_omp_threadprivate (c_parser *);
1197 static void c_parser_omp_barrier (c_parser *);
1198 static void c_parser_omp_flush (c_parser *);
1199 static void c_parser_omp_taskwait (c_parser *);
1200 static void c_parser_omp_taskyield (c_parser *);
1201 static void c_parser_omp_cancel (c_parser *);
1202 static void c_parser_omp_cancellation_point (c_parser *);
1203
1204 enum pragma_context { pragma_external, pragma_struct, pragma_param,
1205 pragma_stmt, pragma_compound };
1206 static bool c_parser_pragma (c_parser *, enum pragma_context);
1207 static bool c_parser_omp_target (c_parser *, enum pragma_context);
1208 static void c_parser_omp_end_declare_target (c_parser *);
1209 static void c_parser_omp_declare (c_parser *, enum pragma_context);
1210
1211 /* These Objective-C parser functions are only ever called when
1212 compiling Objective-C. */
1213 static void c_parser_objc_class_definition (c_parser *, tree);
1214 static void c_parser_objc_class_instance_variables (c_parser *);
1215 static void c_parser_objc_class_declaration (c_parser *);
1216 static void c_parser_objc_alias_declaration (c_parser *);
1217 static void c_parser_objc_protocol_definition (c_parser *, tree);
1218 static bool c_parser_objc_method_type (c_parser *);
1219 static void c_parser_objc_method_definition (c_parser *);
1220 static void c_parser_objc_methodprotolist (c_parser *);
1221 static void c_parser_objc_methodproto (c_parser *);
1222 static tree c_parser_objc_method_decl (c_parser *, bool, tree *, tree *);
1223 static tree c_parser_objc_type_name (c_parser *);
1224 static tree c_parser_objc_protocol_refs (c_parser *);
1225 static void c_parser_objc_try_catch_finally_statement (c_parser *);
1226 static void c_parser_objc_synchronized_statement (c_parser *);
1227 static tree c_parser_objc_selector (c_parser *);
1228 static tree c_parser_objc_selector_arg (c_parser *);
1229 static tree c_parser_objc_receiver (c_parser *);
1230 static tree c_parser_objc_message_args (c_parser *);
1231 static tree c_parser_objc_keywordexpr (c_parser *);
1232 static void c_parser_objc_at_property_declaration (c_parser *);
1233 static void c_parser_objc_at_synthesize_declaration (c_parser *);
1234 static void c_parser_objc_at_dynamic_declaration (c_parser *);
1235 static bool c_parser_objc_diagnose_bad_element_prefix
1236 (c_parser *, struct c_declspecs *);
1237
1238 static tree c_parser_array_notation (location_t, c_parser *, tree, tree);
1239
1240 /* Parse a translation unit (C90 6.7, C99 6.9).
1241
1242 translation-unit:
1243 external-declarations
1244
1245 external-declarations:
1246 external-declaration
1247 external-declarations external-declaration
1248
1249 GNU extensions:
1250
1251 translation-unit:
1252 empty
1253 */
1254
1255 static void
1256 c_parser_translation_unit (c_parser *parser)
1257 {
1258 if (c_parser_next_token_is (parser, CPP_EOF))
1259 {
1260 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
1261 "ISO C forbids an empty translation unit");
1262 }
1263 else
1264 {
1265 void *obstack_position = obstack_alloc (&parser_obstack, 0);
1266 mark_valid_location_for_stdc_pragma (false);
1267 do
1268 {
1269 ggc_collect ();
1270 c_parser_external_declaration (parser);
1271 obstack_free (&parser_obstack, obstack_position);
1272 }
1273 while (c_parser_next_token_is_not (parser, CPP_EOF));
1274 }
1275 }
1276
1277 /* Parse an external declaration (C90 6.7, C99 6.9).
1278
1279 external-declaration:
1280 function-definition
1281 declaration
1282
1283 GNU extensions:
1284
1285 external-declaration:
1286 asm-definition
1287 ;
1288 __extension__ external-declaration
1289
1290 Objective-C:
1291
1292 external-declaration:
1293 objc-class-definition
1294 objc-class-declaration
1295 objc-alias-declaration
1296 objc-protocol-definition
1297 objc-method-definition
1298 @end
1299 */
1300
1301 static void
1302 c_parser_external_declaration (c_parser *parser)
1303 {
1304 int ext;
1305 switch (c_parser_peek_token (parser)->type)
1306 {
1307 case CPP_KEYWORD:
1308 switch (c_parser_peek_token (parser)->keyword)
1309 {
1310 case RID_EXTENSION:
1311 ext = disable_extension_diagnostics ();
1312 c_parser_consume_token (parser);
1313 c_parser_external_declaration (parser);
1314 restore_extension_diagnostics (ext);
1315 break;
1316 case RID_ASM:
1317 c_parser_asm_definition (parser);
1318 break;
1319 case RID_AT_INTERFACE:
1320 case RID_AT_IMPLEMENTATION:
1321 gcc_assert (c_dialect_objc ());
1322 c_parser_objc_class_definition (parser, NULL_TREE);
1323 break;
1324 case RID_AT_CLASS:
1325 gcc_assert (c_dialect_objc ());
1326 c_parser_objc_class_declaration (parser);
1327 break;
1328 case RID_AT_ALIAS:
1329 gcc_assert (c_dialect_objc ());
1330 c_parser_objc_alias_declaration (parser);
1331 break;
1332 case RID_AT_PROTOCOL:
1333 gcc_assert (c_dialect_objc ());
1334 c_parser_objc_protocol_definition (parser, NULL_TREE);
1335 break;
1336 case RID_AT_PROPERTY:
1337 gcc_assert (c_dialect_objc ());
1338 c_parser_objc_at_property_declaration (parser);
1339 break;
1340 case RID_AT_SYNTHESIZE:
1341 gcc_assert (c_dialect_objc ());
1342 c_parser_objc_at_synthesize_declaration (parser);
1343 break;
1344 case RID_AT_DYNAMIC:
1345 gcc_assert (c_dialect_objc ());
1346 c_parser_objc_at_dynamic_declaration (parser);
1347 break;
1348 case RID_AT_END:
1349 gcc_assert (c_dialect_objc ());
1350 c_parser_consume_token (parser);
1351 objc_finish_implementation ();
1352 break;
1353 default:
1354 goto decl_or_fndef;
1355 }
1356 break;
1357 case CPP_SEMICOLON:
1358 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
1359 "ISO C does not allow extra %<;%> outside of a function");
1360 c_parser_consume_token (parser);
1361 break;
1362 case CPP_PRAGMA:
1363 mark_valid_location_for_stdc_pragma (true);
1364 c_parser_pragma (parser, pragma_external);
1365 mark_valid_location_for_stdc_pragma (false);
1366 break;
1367 case CPP_PLUS:
1368 case CPP_MINUS:
1369 if (c_dialect_objc ())
1370 {
1371 c_parser_objc_method_definition (parser);
1372 break;
1373 }
1374 /* Else fall through, and yield a syntax error trying to parse
1375 as a declaration or function definition. */
1376 default:
1377 decl_or_fndef:
1378 /* A declaration or a function definition (or, in Objective-C,
1379 an @interface or @protocol with prefix attributes). We can
1380 only tell which after parsing the declaration specifiers, if
1381 any, and the first declarator. */
1382 c_parser_declaration_or_fndef (parser, true, true, true, false, true,
1383 NULL, vNULL);
1384 break;
1385 }
1386 }
1387
1388 static void c_finish_omp_declare_simd (c_parser *, tree, tree, vec<c_token>);
1389
1390 /* Parse a declaration or function definition (C90 6.5, 6.7.1, C99
1391 6.7, 6.9.1). If FNDEF_OK is true, a function definition is
1392 accepted; otherwise (old-style parameter declarations) only other
1393 declarations are accepted. If STATIC_ASSERT_OK is true, a static
1394 assertion is accepted; otherwise (old-style parameter declarations)
1395 it is not. If NESTED is true, we are inside a function or parsing
1396 old-style parameter declarations; any functions encountered are
1397 nested functions and declaration specifiers are required; otherwise
1398 we are at top level and functions are normal functions and
1399 declaration specifiers may be optional. If EMPTY_OK is true, empty
1400 declarations are OK (subject to all other constraints); otherwise
1401 (old-style parameter declarations) they are diagnosed. If
1402 START_ATTR_OK is true, the declaration specifiers may start with
1403 attributes; otherwise they may not.
1404 OBJC_FOREACH_OBJECT_DECLARATION can be used to get back the parsed
1405 declaration when parsing an Objective-C foreach statement.
1406
1407 declaration:
1408 declaration-specifiers init-declarator-list[opt] ;
1409 static_assert-declaration
1410
1411 function-definition:
1412 declaration-specifiers[opt] declarator declaration-list[opt]
1413 compound-statement
1414
1415 declaration-list:
1416 declaration
1417 declaration-list declaration
1418
1419 init-declarator-list:
1420 init-declarator
1421 init-declarator-list , init-declarator
1422
1423 init-declarator:
1424 declarator simple-asm-expr[opt] attributes[opt]
1425 declarator simple-asm-expr[opt] attributes[opt] = initializer
1426
1427 GNU extensions:
1428
1429 nested-function-definition:
1430 declaration-specifiers declarator declaration-list[opt]
1431 compound-statement
1432
1433 Objective-C:
1434 attributes objc-class-definition
1435 attributes objc-category-definition
1436 attributes objc-protocol-definition
1437
1438 The simple-asm-expr and attributes are GNU extensions.
1439
1440 This function does not handle __extension__; that is handled in its
1441 callers. ??? Following the old parser, __extension__ may start
1442 external declarations, declarations in functions and declarations
1443 at the start of "for" loops, but not old-style parameter
1444 declarations.
1445
1446 C99 requires declaration specifiers in a function definition; the
1447 absence is diagnosed through the diagnosis of implicit int. In GNU
1448 C we also allow but diagnose declarations without declaration
1449 specifiers, but only at top level (elsewhere they conflict with
1450 other syntax).
1451
1452 In Objective-C, declarations of the looping variable in a foreach
1453 statement are exceptionally terminated by 'in' (for example, 'for
1454 (NSObject *object in array) { ... }').
1455
1456 OpenMP:
1457
1458 declaration:
1459 threadprivate-directive */
1460
1461 static void
1462 c_parser_declaration_or_fndef (c_parser *parser, bool fndef_ok,
1463 bool static_assert_ok, bool empty_ok,
1464 bool nested, bool start_attr_ok,
1465 tree *objc_foreach_object_declaration,
1466 vec<c_token> omp_declare_simd_clauses)
1467 {
1468 struct c_declspecs *specs;
1469 tree prefix_attrs;
1470 tree all_prefix_attrs;
1471 bool diagnosed_no_specs = false;
1472 location_t here = c_parser_peek_token (parser)->location;
1473
1474 if (static_assert_ok
1475 && c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT))
1476 {
1477 c_parser_static_assert_declaration (parser);
1478 return;
1479 }
1480 specs = build_null_declspecs ();
1481
1482 /* Try to detect an unknown type name when we have "A B" or "A *B". */
1483 if (c_parser_peek_token (parser)->type == CPP_NAME
1484 && c_parser_peek_token (parser)->id_kind == C_ID_ID
1485 && (c_parser_peek_2nd_token (parser)->type == CPP_NAME
1486 || c_parser_peek_2nd_token (parser)->type == CPP_MULT)
1487 && (!nested || !lookup_name (c_parser_peek_token (parser)->value)))
1488 {
1489 error_at (here, "unknown type name %qE",
1490 c_parser_peek_token (parser)->value);
1491
1492 /* Parse declspecs normally to get a correct pointer type, but avoid
1493 a further "fails to be a type name" error. Refuse nested functions
1494 since it is not how the user likely wants us to recover. */
1495 c_parser_peek_token (parser)->type = CPP_KEYWORD;
1496 c_parser_peek_token (parser)->keyword = RID_VOID;
1497 c_parser_peek_token (parser)->value = error_mark_node;
1498 fndef_ok = !nested;
1499 }
1500
1501 c_parser_declspecs (parser, specs, true, true, start_attr_ok,
1502 true, cla_nonabstract_decl);
1503 if (parser->error)
1504 {
1505 c_parser_skip_to_end_of_block_or_statement (parser);
1506 return;
1507 }
1508 if (nested && !specs->declspecs_seen_p)
1509 {
1510 c_parser_error (parser, "expected declaration specifiers");
1511 c_parser_skip_to_end_of_block_or_statement (parser);
1512 return;
1513 }
1514 finish_declspecs (specs);
1515 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1516 {
1517 if (empty_ok)
1518 shadow_tag (specs);
1519 else
1520 {
1521 shadow_tag_warned (specs, 1);
1522 pedwarn (here, 0, "empty declaration");
1523 }
1524 c_parser_consume_token (parser);
1525 return;
1526 }
1527
1528 /* Provide better error recovery. Note that a type name here is usually
1529 better diagnosed as a redeclaration. */
1530 if (empty_ok
1531 && specs->typespec_kind == ctsk_tagdef
1532 && c_parser_next_token_starts_declspecs (parser)
1533 && !c_parser_next_token_is (parser, CPP_NAME))
1534 {
1535 c_parser_error (parser, "expected %<;%>, identifier or %<(%>");
1536 parser->error = false;
1537 shadow_tag_warned (specs, 1);
1538 return;
1539 }
1540 else if (c_dialect_objc ())
1541 {
1542 /* Prefix attributes are an error on method decls. */
1543 switch (c_parser_peek_token (parser)->type)
1544 {
1545 case CPP_PLUS:
1546 case CPP_MINUS:
1547 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1548 return;
1549 if (specs->attrs)
1550 {
1551 warning_at (c_parser_peek_token (parser)->location,
1552 OPT_Wattributes,
1553 "prefix attributes are ignored for methods");
1554 specs->attrs = NULL_TREE;
1555 }
1556 if (fndef_ok)
1557 c_parser_objc_method_definition (parser);
1558 else
1559 c_parser_objc_methodproto (parser);
1560 return;
1561 break;
1562 default:
1563 break;
1564 }
1565 /* This is where we parse 'attributes @interface ...',
1566 'attributes @implementation ...', 'attributes @protocol ...'
1567 (where attributes could be, for example, __attribute__
1568 ((deprecated)).
1569 */
1570 switch (c_parser_peek_token (parser)->keyword)
1571 {
1572 case RID_AT_INTERFACE:
1573 {
1574 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1575 return;
1576 c_parser_objc_class_definition (parser, specs->attrs);
1577 return;
1578 }
1579 break;
1580 case RID_AT_IMPLEMENTATION:
1581 {
1582 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1583 return;
1584 if (specs->attrs)
1585 {
1586 warning_at (c_parser_peek_token (parser)->location,
1587 OPT_Wattributes,
1588 "prefix attributes are ignored for implementations");
1589 specs->attrs = NULL_TREE;
1590 }
1591 c_parser_objc_class_definition (parser, NULL_TREE);
1592 return;
1593 }
1594 break;
1595 case RID_AT_PROTOCOL:
1596 {
1597 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1598 return;
1599 c_parser_objc_protocol_definition (parser, specs->attrs);
1600 return;
1601 }
1602 break;
1603 case RID_AT_ALIAS:
1604 case RID_AT_CLASS:
1605 case RID_AT_END:
1606 case RID_AT_PROPERTY:
1607 if (specs->attrs)
1608 {
1609 c_parser_error (parser, "unexpected attribute");
1610 specs->attrs = NULL;
1611 }
1612 break;
1613 default:
1614 break;
1615 }
1616 }
1617
1618 pending_xref_error ();
1619 prefix_attrs = specs->attrs;
1620 all_prefix_attrs = prefix_attrs;
1621 specs->attrs = NULL_TREE;
1622 while (true)
1623 {
1624 struct c_declarator *declarator;
1625 bool dummy = false;
1626 timevar_id_t tv;
1627 tree fnbody;
1628 /* Declaring either one or more declarators (in which case we
1629 should diagnose if there were no declaration specifiers) or a
1630 function definition (in which case the diagnostic for
1631 implicit int suffices). */
1632 declarator = c_parser_declarator (parser,
1633 specs->typespec_kind != ctsk_none,
1634 C_DTR_NORMAL, &dummy);
1635 if (declarator == NULL)
1636 {
1637 if (omp_declare_simd_clauses.exists ())
1638 c_finish_omp_declare_simd (parser, NULL_TREE, NULL_TREE,
1639 omp_declare_simd_clauses);
1640 c_parser_skip_to_end_of_block_or_statement (parser);
1641 return;
1642 }
1643 if (c_parser_next_token_is (parser, CPP_EQ)
1644 || c_parser_next_token_is (parser, CPP_COMMA)
1645 || c_parser_next_token_is (parser, CPP_SEMICOLON)
1646 || c_parser_next_token_is_keyword (parser, RID_ASM)
1647 || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE)
1648 || c_parser_next_token_is_keyword (parser, RID_IN))
1649 {
1650 tree asm_name = NULL_TREE;
1651 tree postfix_attrs = NULL_TREE;
1652 if (!diagnosed_no_specs && !specs->declspecs_seen_p)
1653 {
1654 diagnosed_no_specs = true;
1655 pedwarn (here, 0, "data definition has no type or storage class");
1656 }
1657 /* Having seen a data definition, there cannot now be a
1658 function definition. */
1659 fndef_ok = false;
1660 if (c_parser_next_token_is_keyword (parser, RID_ASM))
1661 asm_name = c_parser_simple_asm_expr (parser);
1662 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1663 postfix_attrs = c_parser_attributes (parser);
1664 if (c_parser_next_token_is (parser, CPP_EQ))
1665 {
1666 tree d;
1667 struct c_expr init;
1668 location_t init_loc;
1669 c_parser_consume_token (parser);
1670 /* The declaration of the variable is in effect while
1671 its initializer is parsed. */
1672 d = start_decl (declarator, specs, true,
1673 chainon (postfix_attrs, all_prefix_attrs));
1674 if (!d)
1675 d = error_mark_node;
1676 if (omp_declare_simd_clauses.exists ())
1677 c_finish_omp_declare_simd (parser, d, NULL_TREE,
1678 omp_declare_simd_clauses);
1679 start_init (d, asm_name, global_bindings_p ());
1680 init_loc = c_parser_peek_token (parser)->location;
1681 init = c_parser_initializer (parser);
1682 finish_init ();
1683 if (d != error_mark_node)
1684 {
1685 maybe_warn_string_init (TREE_TYPE (d), init);
1686 finish_decl (d, init_loc, init.value,
1687 init.original_type, asm_name);
1688 }
1689 }
1690 else
1691 {
1692 tree d = start_decl (declarator, specs, false,
1693 chainon (postfix_attrs,
1694 all_prefix_attrs));
1695 if (omp_declare_simd_clauses.exists ())
1696 {
1697 tree parms = NULL_TREE;
1698 if (d && TREE_CODE (d) == FUNCTION_DECL)
1699 {
1700 struct c_declarator *ce = declarator;
1701 while (ce != NULL)
1702 if (ce->kind == cdk_function)
1703 {
1704 parms = ce->u.arg_info->parms;
1705 break;
1706 }
1707 else
1708 ce = ce->declarator;
1709 }
1710 if (parms)
1711 temp_store_parm_decls (d, parms);
1712 c_finish_omp_declare_simd (parser, d, parms,
1713 omp_declare_simd_clauses);
1714 if (parms)
1715 temp_pop_parm_decls ();
1716 }
1717 if (d)
1718 finish_decl (d, UNKNOWN_LOCATION, NULL_TREE,
1719 NULL_TREE, asm_name);
1720
1721 if (c_parser_next_token_is_keyword (parser, RID_IN))
1722 {
1723 if (d)
1724 *objc_foreach_object_declaration = d;
1725 else
1726 *objc_foreach_object_declaration = error_mark_node;
1727 }
1728 }
1729 if (c_parser_next_token_is (parser, CPP_COMMA))
1730 {
1731 c_parser_consume_token (parser);
1732 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1733 all_prefix_attrs = chainon (c_parser_attributes (parser),
1734 prefix_attrs);
1735 else
1736 all_prefix_attrs = prefix_attrs;
1737 continue;
1738 }
1739 else if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1740 {
1741 c_parser_consume_token (parser);
1742 return;
1743 }
1744 else if (c_parser_next_token_is_keyword (parser, RID_IN))
1745 {
1746 /* This can only happen in Objective-C: we found the
1747 'in' that terminates the declaration inside an
1748 Objective-C foreach statement. Do not consume the
1749 token, so that the caller can use it to determine
1750 that this indeed is a foreach context. */
1751 return;
1752 }
1753 else
1754 {
1755 c_parser_error (parser, "expected %<,%> or %<;%>");
1756 c_parser_skip_to_end_of_block_or_statement (parser);
1757 return;
1758 }
1759 }
1760 else if (!fndef_ok)
1761 {
1762 c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, "
1763 "%<asm%> or %<__attribute__%>");
1764 c_parser_skip_to_end_of_block_or_statement (parser);
1765 return;
1766 }
1767 /* Function definition (nested or otherwise). */
1768 if (nested)
1769 {
1770 pedwarn (here, OPT_Wpedantic, "ISO C forbids nested functions");
1771 c_push_function_context ();
1772 }
1773 if (!start_function (specs, declarator, all_prefix_attrs))
1774 {
1775 /* This can appear in many cases looking nothing like a
1776 function definition, so we don't give a more specific
1777 error suggesting there was one. */
1778 c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, %<asm%> "
1779 "or %<__attribute__%>");
1780 if (nested)
1781 c_pop_function_context ();
1782 break;
1783 }
1784
1785 if (DECL_DECLARED_INLINE_P (current_function_decl))
1786 tv = TV_PARSE_INLINE;
1787 else
1788 tv = TV_PARSE_FUNC;
1789 timevar_push (tv);
1790
1791 /* Parse old-style parameter declarations. ??? Attributes are
1792 not allowed to start declaration specifiers here because of a
1793 syntax conflict between a function declaration with attribute
1794 suffix and a function definition with an attribute prefix on
1795 first old-style parameter declaration. Following the old
1796 parser, they are not accepted on subsequent old-style
1797 parameter declarations either. However, there is no
1798 ambiguity after the first declaration, nor indeed on the
1799 first as long as we don't allow postfix attributes after a
1800 declarator with a nonempty identifier list in a definition;
1801 and postfix attributes have never been accepted here in
1802 function definitions either. */
1803 while (c_parser_next_token_is_not (parser, CPP_EOF)
1804 && c_parser_next_token_is_not (parser, CPP_OPEN_BRACE))
1805 c_parser_declaration_or_fndef (parser, false, false, false,
1806 true, false, NULL, vNULL);
1807 store_parm_decls ();
1808 if (omp_declare_simd_clauses.exists ())
1809 c_finish_omp_declare_simd (parser, current_function_decl, NULL_TREE,
1810 omp_declare_simd_clauses);
1811 DECL_STRUCT_FUNCTION (current_function_decl)->function_start_locus
1812 = c_parser_peek_token (parser)->location;
1813 fnbody = c_parser_compound_statement (parser);
1814 if (flag_enable_cilkplus && contains_array_notation_expr (fnbody))
1815 fnbody = expand_array_notation_exprs (fnbody);
1816 if (nested)
1817 {
1818 tree decl = current_function_decl;
1819 /* Mark nested functions as needing static-chain initially.
1820 lower_nested_functions will recompute it but the
1821 DECL_STATIC_CHAIN flag is also used before that happens,
1822 by initializer_constant_valid_p. See gcc.dg/nested-fn-2.c. */
1823 DECL_STATIC_CHAIN (decl) = 1;
1824 add_stmt (fnbody);
1825 finish_function ();
1826 c_pop_function_context ();
1827 add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl));
1828 }
1829 else
1830 {
1831 add_stmt (fnbody);
1832 finish_function ();
1833 }
1834
1835 timevar_pop (tv);
1836 break;
1837 }
1838 }
1839
1840 /* Parse an asm-definition (asm() outside a function body). This is a
1841 GNU extension.
1842
1843 asm-definition:
1844 simple-asm-expr ;
1845 */
1846
1847 static void
1848 c_parser_asm_definition (c_parser *parser)
1849 {
1850 tree asm_str = c_parser_simple_asm_expr (parser);
1851 if (asm_str)
1852 add_asm_node (asm_str);
1853 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
1854 }
1855
1856 /* Parse a static assertion (C11 6.7.10).
1857
1858 static_assert-declaration:
1859 static_assert-declaration-no-semi ;
1860 */
1861
1862 static void
1863 c_parser_static_assert_declaration (c_parser *parser)
1864 {
1865 c_parser_static_assert_declaration_no_semi (parser);
1866 if (parser->error
1867 || !c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
1868 c_parser_skip_to_end_of_block_or_statement (parser);
1869 }
1870
1871 /* Parse a static assertion (C11 6.7.10), without the trailing
1872 semicolon.
1873
1874 static_assert-declaration-no-semi:
1875 _Static_assert ( constant-expression , string-literal )
1876 */
1877
1878 static void
1879 c_parser_static_assert_declaration_no_semi (c_parser *parser)
1880 {
1881 location_t assert_loc, value_loc;
1882 tree value;
1883 tree string;
1884
1885 gcc_assert (c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT));
1886 assert_loc = c_parser_peek_token (parser)->location;
1887 if (!flag_isoc11)
1888 {
1889 if (flag_isoc99)
1890 pedwarn (assert_loc, OPT_Wpedantic,
1891 "ISO C99 does not support %<_Static_assert%>");
1892 else
1893 pedwarn (assert_loc, OPT_Wpedantic,
1894 "ISO C90 does not support %<_Static_assert%>");
1895 }
1896 c_parser_consume_token (parser);
1897 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
1898 return;
1899 value_loc = c_parser_peek_token (parser)->location;
1900 value = c_parser_expr_no_commas (parser, NULL).value;
1901 parser->lex_untranslated_string = true;
1902 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
1903 {
1904 parser->lex_untranslated_string = false;
1905 return;
1906 }
1907 switch (c_parser_peek_token (parser)->type)
1908 {
1909 case CPP_STRING:
1910 case CPP_STRING16:
1911 case CPP_STRING32:
1912 case CPP_WSTRING:
1913 case CPP_UTF8STRING:
1914 string = c_parser_peek_token (parser)->value;
1915 c_parser_consume_token (parser);
1916 parser->lex_untranslated_string = false;
1917 break;
1918 default:
1919 c_parser_error (parser, "expected string literal");
1920 parser->lex_untranslated_string = false;
1921 return;
1922 }
1923 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
1924
1925 if (!INTEGRAL_TYPE_P (TREE_TYPE (value)))
1926 {
1927 error_at (value_loc, "expression in static assertion is not an integer");
1928 return;
1929 }
1930 if (TREE_CODE (value) != INTEGER_CST)
1931 {
1932 value = c_fully_fold (value, false, NULL);
1933 if (TREE_CODE (value) == INTEGER_CST)
1934 pedwarn (value_loc, OPT_Wpedantic, "expression in static assertion "
1935 "is not an integer constant expression");
1936 }
1937 if (TREE_CODE (value) != INTEGER_CST)
1938 {
1939 error_at (value_loc, "expression in static assertion is not constant");
1940 return;
1941 }
1942 constant_expression_warning (value);
1943 if (integer_zerop (value))
1944 error_at (assert_loc, "static assertion failed: %E", string);
1945 }
1946
1947 /* Parse some declaration specifiers (possibly none) (C90 6.5, C99
1948 6.7), adding them to SPECS (which may already include some).
1949 Storage class specifiers are accepted iff SCSPEC_OK; type
1950 specifiers are accepted iff TYPESPEC_OK; alignment specifiers are
1951 accepted iff ALIGNSPEC_OK; attributes are accepted at the start
1952 iff START_ATTR_OK.
1953
1954 declaration-specifiers:
1955 storage-class-specifier declaration-specifiers[opt]
1956 type-specifier declaration-specifiers[opt]
1957 type-qualifier declaration-specifiers[opt]
1958 function-specifier declaration-specifiers[opt]
1959 alignment-specifier declaration-specifiers[opt]
1960
1961 Function specifiers (inline) are from C99, and are currently
1962 handled as storage class specifiers, as is __thread. Alignment
1963 specifiers are from C11.
1964
1965 C90 6.5.1, C99 6.7.1:
1966 storage-class-specifier:
1967 typedef
1968 extern
1969 static
1970 auto
1971 register
1972 _Thread_local
1973
1974 (_Thread_local is new in C11.)
1975
1976 C99 6.7.4:
1977 function-specifier:
1978 inline
1979 _Noreturn
1980
1981 (_Noreturn is new in C11.)
1982
1983 C90 6.5.2, C99 6.7.2:
1984 type-specifier:
1985 void
1986 char
1987 short
1988 int
1989 long
1990 float
1991 double
1992 signed
1993 unsigned
1994 _Bool
1995 _Complex
1996 [_Imaginary removed in C99 TC2]
1997 struct-or-union-specifier
1998 enum-specifier
1999 typedef-name
2000 atomic-type-specifier
2001
2002 (_Bool and _Complex are new in C99.)
2003 (atomic-type-specifier is new in C11.)
2004
2005 C90 6.5.3, C99 6.7.3:
2006
2007 type-qualifier:
2008 const
2009 restrict
2010 volatile
2011 address-space-qualifier
2012 _Atomic
2013
2014 (restrict is new in C99.)
2015 (_Atomic is new in C11.)
2016
2017 GNU extensions:
2018
2019 declaration-specifiers:
2020 attributes declaration-specifiers[opt]
2021
2022 type-qualifier:
2023 address-space
2024
2025 address-space:
2026 identifier recognized by the target
2027
2028 storage-class-specifier:
2029 __thread
2030
2031 type-specifier:
2032 typeof-specifier
2033 __int128
2034 _Decimal32
2035 _Decimal64
2036 _Decimal128
2037 _Fract
2038 _Accum
2039 _Sat
2040
2041 (_Fract, _Accum, and _Sat are new from ISO/IEC DTR 18037:
2042 http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1169.pdf)
2043
2044 atomic-type-specifier
2045 _Atomic ( type-name )
2046
2047 Objective-C:
2048
2049 type-specifier:
2050 class-name objc-protocol-refs[opt]
2051 typedef-name objc-protocol-refs
2052 objc-protocol-refs
2053 */
2054
2055 static void
2056 c_parser_declspecs (c_parser *parser, struct c_declspecs *specs,
2057 bool scspec_ok, bool typespec_ok, bool start_attr_ok,
2058 bool alignspec_ok, enum c_lookahead_kind la)
2059 {
2060 bool attrs_ok = start_attr_ok;
2061 bool seen_type = specs->typespec_kind != ctsk_none;
2062
2063 if (!typespec_ok)
2064 gcc_assert (la == cla_prefer_id);
2065
2066 while (c_parser_next_token_is (parser, CPP_NAME)
2067 || c_parser_next_token_is (parser, CPP_KEYWORD)
2068 || (c_dialect_objc () && c_parser_next_token_is (parser, CPP_LESS)))
2069 {
2070 struct c_typespec t;
2071 tree attrs;
2072 tree align;
2073 location_t loc = c_parser_peek_token (parser)->location;
2074
2075 /* If we cannot accept a type, exit if the next token must start
2076 one. Also, if we already have seen a tagged definition,
2077 a typename would be an error anyway and likely the user
2078 has simply forgotten a semicolon, so we exit. */
2079 if ((!typespec_ok || specs->typespec_kind == ctsk_tagdef)
2080 && c_parser_next_tokens_start_typename (parser, la)
2081 && !c_parser_next_token_is_qualifier (parser))
2082 break;
2083
2084 if (c_parser_next_token_is (parser, CPP_NAME))
2085 {
2086 c_token *name_token = c_parser_peek_token (parser);
2087 tree value = name_token->value;
2088 c_id_kind kind = name_token->id_kind;
2089
2090 if (kind == C_ID_ADDRSPACE)
2091 {
2092 addr_space_t as
2093 = name_token->keyword - RID_FIRST_ADDR_SPACE;
2094 declspecs_add_addrspace (name_token->location, specs, as);
2095 c_parser_consume_token (parser);
2096 attrs_ok = true;
2097 continue;
2098 }
2099
2100 gcc_assert (!c_parser_next_token_is_qualifier (parser));
2101
2102 /* If we cannot accept a type, and the next token must start one,
2103 exit. Do the same if we already have seen a tagged definition,
2104 since it would be an error anyway and likely the user has simply
2105 forgotten a semicolon. */
2106 if (seen_type || !c_parser_next_tokens_start_typename (parser, la))
2107 break;
2108
2109 /* Now at an unknown typename (C_ID_ID), a C_ID_TYPENAME or
2110 a C_ID_CLASSNAME. */
2111 c_parser_consume_token (parser);
2112 seen_type = true;
2113 attrs_ok = true;
2114 if (kind == C_ID_ID)
2115 {
2116 error ("unknown type name %qE", value);
2117 t.kind = ctsk_typedef;
2118 t.spec = error_mark_node;
2119 }
2120 else if (kind == C_ID_TYPENAME
2121 && (!c_dialect_objc ()
2122 || c_parser_next_token_is_not (parser, CPP_LESS)))
2123 {
2124 t.kind = ctsk_typedef;
2125 /* For a typedef name, record the meaning, not the name.
2126 In case of 'foo foo, bar;'. */
2127 t.spec = lookup_name (value);
2128 }
2129 else
2130 {
2131 tree proto = NULL_TREE;
2132 gcc_assert (c_dialect_objc ());
2133 t.kind = ctsk_objc;
2134 if (c_parser_next_token_is (parser, CPP_LESS))
2135 proto = c_parser_objc_protocol_refs (parser);
2136 t.spec = objc_get_protocol_qualified_type (value, proto);
2137 }
2138 t.expr = NULL_TREE;
2139 t.expr_const_operands = true;
2140 declspecs_add_type (name_token->location, specs, t);
2141 continue;
2142 }
2143 if (c_parser_next_token_is (parser, CPP_LESS))
2144 {
2145 /* Make "<SomeProtocol>" equivalent to "id <SomeProtocol>" -
2146 nisse@lysator.liu.se. */
2147 tree proto;
2148 gcc_assert (c_dialect_objc ());
2149 if (!typespec_ok || seen_type)
2150 break;
2151 proto = c_parser_objc_protocol_refs (parser);
2152 t.kind = ctsk_objc;
2153 t.spec = objc_get_protocol_qualified_type (NULL_TREE, proto);
2154 t.expr = NULL_TREE;
2155 t.expr_const_operands = true;
2156 declspecs_add_type (loc, specs, t);
2157 continue;
2158 }
2159 gcc_assert (c_parser_next_token_is (parser, CPP_KEYWORD));
2160 switch (c_parser_peek_token (parser)->keyword)
2161 {
2162 case RID_STATIC:
2163 case RID_EXTERN:
2164 case RID_REGISTER:
2165 case RID_TYPEDEF:
2166 case RID_INLINE:
2167 case RID_NORETURN:
2168 case RID_AUTO:
2169 case RID_THREAD:
2170 if (!scspec_ok)
2171 goto out;
2172 attrs_ok = true;
2173 /* TODO: Distinguish between function specifiers (inline, noreturn)
2174 and storage class specifiers, either here or in
2175 declspecs_add_scspec. */
2176 declspecs_add_scspec (loc, specs,
2177 c_parser_peek_token (parser)->value);
2178 c_parser_consume_token (parser);
2179 break;
2180 case RID_UNSIGNED:
2181 case RID_LONG:
2182 case RID_INT128:
2183 case RID_SHORT:
2184 case RID_SIGNED:
2185 case RID_COMPLEX:
2186 case RID_INT:
2187 case RID_CHAR:
2188 case RID_FLOAT:
2189 case RID_DOUBLE:
2190 case RID_VOID:
2191 case RID_DFLOAT32:
2192 case RID_DFLOAT64:
2193 case RID_DFLOAT128:
2194 case RID_BOOL:
2195 case RID_FRACT:
2196 case RID_ACCUM:
2197 case RID_SAT:
2198 if (!typespec_ok)
2199 goto out;
2200 attrs_ok = true;
2201 seen_type = true;
2202 if (c_dialect_objc ())
2203 parser->objc_need_raw_identifier = true;
2204 t.kind = ctsk_resword;
2205 t.spec = c_parser_peek_token (parser)->value;
2206 t.expr = NULL_TREE;
2207 t.expr_const_operands = true;
2208 declspecs_add_type (loc, specs, t);
2209 c_parser_consume_token (parser);
2210 break;
2211 case RID_ENUM:
2212 if (!typespec_ok)
2213 goto out;
2214 attrs_ok = true;
2215 seen_type = true;
2216 t = c_parser_enum_specifier (parser);
2217 declspecs_add_type (loc, specs, t);
2218 break;
2219 case RID_STRUCT:
2220 case RID_UNION:
2221 if (!typespec_ok)
2222 goto out;
2223 attrs_ok = true;
2224 seen_type = true;
2225 t = c_parser_struct_or_union_specifier (parser);
2226 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, t.spec);
2227 declspecs_add_type (loc, specs, t);
2228 break;
2229 case RID_TYPEOF:
2230 /* ??? The old parser rejected typeof after other type
2231 specifiers, but is a syntax error the best way of
2232 handling this? */
2233 if (!typespec_ok || seen_type)
2234 goto out;
2235 attrs_ok = true;
2236 seen_type = true;
2237 t = c_parser_typeof_specifier (parser);
2238 declspecs_add_type (loc, specs, t);
2239 break;
2240 case RID_ATOMIC:
2241 /* C parser handling of Objective-C constructs needs
2242 checking for correct lvalue-to-rvalue conversions, and
2243 the code in build_modify_expr handling various
2244 Objective-C cases, and that in build_unary_op handling
2245 Objective-C cases for increment / decrement, also needs
2246 updating; uses of TYPE_MAIN_VARIANT in objc_compare_types
2247 and objc_types_are_equivalent may also need updates. */
2248 if (c_dialect_objc ())
2249 sorry ("%<_Atomic%> in Objective-C");
2250 /* C parser handling of OpenMP constructs needs checking for
2251 correct lvalue-to-rvalue conversions. */
2252 if (flag_openmp)
2253 sorry ("%<_Atomic%> with OpenMP");
2254 if (!flag_isoc11)
2255 {
2256 if (flag_isoc99)
2257 pedwarn (loc, OPT_Wpedantic,
2258 "ISO C99 does not support the %<_Atomic%> qualifier");
2259 else
2260 pedwarn (loc, OPT_Wpedantic,
2261 "ISO C90 does not support the %<_Atomic%> qualifier");
2262 }
2263 attrs_ok = true;
2264 tree value;
2265 value = c_parser_peek_token (parser)->value;
2266 c_parser_consume_token (parser);
2267 if (typespec_ok && c_parser_next_token_is (parser, CPP_OPEN_PAREN))
2268 {
2269 /* _Atomic ( type-name ). */
2270 seen_type = true;
2271 c_parser_consume_token (parser);
2272 struct c_type_name *type = c_parser_type_name (parser);
2273 t.kind = ctsk_typeof;
2274 t.spec = error_mark_node;
2275 t.expr = NULL_TREE;
2276 t.expr_const_operands = true;
2277 if (type != NULL)
2278 t.spec = groktypename (type, &t.expr,
2279 &t.expr_const_operands);
2280 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2281 "expected %<)%>");
2282 if (t.spec != error_mark_node)
2283 {
2284 if (TREE_CODE (t.spec) == ARRAY_TYPE)
2285 error_at (loc, "%<_Atomic%>-qualified array type");
2286 else if (TREE_CODE (t.spec) == FUNCTION_TYPE)
2287 error_at (loc, "%<_Atomic%>-qualified function type");
2288 else if (TYPE_QUALS (t.spec) != TYPE_UNQUALIFIED)
2289 error_at (loc, "%<_Atomic%> applied to a qualified type");
2290 else
2291 t.spec = c_build_qualified_type (t.spec, TYPE_QUAL_ATOMIC);
2292 }
2293 declspecs_add_type (loc, specs, t);
2294 }
2295 else
2296 declspecs_add_qual (loc, specs, value);
2297 break;
2298 case RID_CONST:
2299 case RID_VOLATILE:
2300 case RID_RESTRICT:
2301 attrs_ok = true;
2302 declspecs_add_qual (loc, specs, c_parser_peek_token (parser)->value);
2303 c_parser_consume_token (parser);
2304 break;
2305 case RID_ATTRIBUTE:
2306 if (!attrs_ok)
2307 goto out;
2308 attrs = c_parser_attributes (parser);
2309 declspecs_add_attrs (loc, specs, attrs);
2310 break;
2311 case RID_ALIGNAS:
2312 if (!alignspec_ok)
2313 goto out;
2314 align = c_parser_alignas_specifier (parser);
2315 declspecs_add_alignas (loc, specs, align);
2316 break;
2317 default:
2318 goto out;
2319 }
2320 }
2321 out: ;
2322 }
2323
2324 /* Parse an enum specifier (C90 6.5.2.2, C99 6.7.2.2).
2325
2326 enum-specifier:
2327 enum attributes[opt] identifier[opt] { enumerator-list } attributes[opt]
2328 enum attributes[opt] identifier[opt] { enumerator-list , } attributes[opt]
2329 enum attributes[opt] identifier
2330
2331 The form with trailing comma is new in C99. The forms with
2332 attributes are GNU extensions. In GNU C, we accept any expression
2333 without commas in the syntax (assignment expressions, not just
2334 conditional expressions); assignment expressions will be diagnosed
2335 as non-constant.
2336
2337 enumerator-list:
2338 enumerator
2339 enumerator-list , enumerator
2340
2341 enumerator:
2342 enumeration-constant
2343 enumeration-constant = constant-expression
2344 */
2345
2346 static struct c_typespec
2347 c_parser_enum_specifier (c_parser *parser)
2348 {
2349 struct c_typespec ret;
2350 tree attrs;
2351 tree ident = NULL_TREE;
2352 location_t enum_loc;
2353 location_t ident_loc = UNKNOWN_LOCATION; /* Quiet warning. */
2354 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ENUM));
2355 enum_loc = c_parser_peek_token (parser)->location;
2356 c_parser_consume_token (parser);
2357 attrs = c_parser_attributes (parser);
2358 enum_loc = c_parser_peek_token (parser)->location;
2359 /* Set the location in case we create a decl now. */
2360 c_parser_set_source_position_from_token (c_parser_peek_token (parser));
2361 if (c_parser_next_token_is (parser, CPP_NAME))
2362 {
2363 ident = c_parser_peek_token (parser)->value;
2364 ident_loc = c_parser_peek_token (parser)->location;
2365 enum_loc = ident_loc;
2366 c_parser_consume_token (parser);
2367 }
2368 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
2369 {
2370 /* Parse an enum definition. */
2371 struct c_enum_contents the_enum;
2372 tree type;
2373 tree postfix_attrs;
2374 /* We chain the enumerators in reverse order, then put them in
2375 forward order at the end. */
2376 tree values;
2377 timevar_push (TV_PARSE_ENUM);
2378 type = start_enum (enum_loc, &the_enum, ident);
2379 values = NULL_TREE;
2380 c_parser_consume_token (parser);
2381 while (true)
2382 {
2383 tree enum_id;
2384 tree enum_value;
2385 tree enum_decl;
2386 bool seen_comma;
2387 c_token *token;
2388 location_t comma_loc = UNKNOWN_LOCATION; /* Quiet warning. */
2389 location_t decl_loc, value_loc;
2390 if (c_parser_next_token_is_not (parser, CPP_NAME))
2391 {
2392 c_parser_error (parser, "expected identifier");
2393 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
2394 values = error_mark_node;
2395 break;
2396 }
2397 token = c_parser_peek_token (parser);
2398 enum_id = token->value;
2399 /* Set the location in case we create a decl now. */
2400 c_parser_set_source_position_from_token (token);
2401 decl_loc = value_loc = token->location;
2402 c_parser_consume_token (parser);
2403 if (c_parser_next_token_is (parser, CPP_EQ))
2404 {
2405 c_parser_consume_token (parser);
2406 value_loc = c_parser_peek_token (parser)->location;
2407 enum_value = c_parser_expr_no_commas (parser, NULL).value;
2408 }
2409 else
2410 enum_value = NULL_TREE;
2411 enum_decl = build_enumerator (decl_loc, value_loc,
2412 &the_enum, enum_id, enum_value);
2413 TREE_CHAIN (enum_decl) = values;
2414 values = enum_decl;
2415 seen_comma = false;
2416 if (c_parser_next_token_is (parser, CPP_COMMA))
2417 {
2418 comma_loc = c_parser_peek_token (parser)->location;
2419 seen_comma = true;
2420 c_parser_consume_token (parser);
2421 }
2422 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2423 {
2424 if (seen_comma && !flag_isoc99)
2425 pedwarn (comma_loc, OPT_Wpedantic, "comma at end of enumerator list");
2426 c_parser_consume_token (parser);
2427 break;
2428 }
2429 if (!seen_comma)
2430 {
2431 c_parser_error (parser, "expected %<,%> or %<}%>");
2432 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
2433 values = error_mark_node;
2434 break;
2435 }
2436 }
2437 postfix_attrs = c_parser_attributes (parser);
2438 ret.spec = finish_enum (type, nreverse (values),
2439 chainon (attrs, postfix_attrs));
2440 ret.kind = ctsk_tagdef;
2441 ret.expr = NULL_TREE;
2442 ret.expr_const_operands = true;
2443 timevar_pop (TV_PARSE_ENUM);
2444 return ret;
2445 }
2446 else if (!ident)
2447 {
2448 c_parser_error (parser, "expected %<{%>");
2449 ret.spec = error_mark_node;
2450 ret.kind = ctsk_tagref;
2451 ret.expr = NULL_TREE;
2452 ret.expr_const_operands = true;
2453 return ret;
2454 }
2455 ret = parser_xref_tag (ident_loc, ENUMERAL_TYPE, ident);
2456 /* In ISO C, enumerated types can be referred to only if already
2457 defined. */
2458 if (pedantic && !COMPLETE_TYPE_P (ret.spec))
2459 {
2460 gcc_assert (ident);
2461 pedwarn (enum_loc, OPT_Wpedantic,
2462 "ISO C forbids forward references to %<enum%> types");
2463 }
2464 return ret;
2465 }
2466
2467 /* Parse a struct or union specifier (C90 6.5.2.1, C99 6.7.2.1).
2468
2469 struct-or-union-specifier:
2470 struct-or-union attributes[opt] identifier[opt]
2471 { struct-contents } attributes[opt]
2472 struct-or-union attributes[opt] identifier
2473
2474 struct-contents:
2475 struct-declaration-list
2476
2477 struct-declaration-list:
2478 struct-declaration ;
2479 struct-declaration-list struct-declaration ;
2480
2481 GNU extensions:
2482
2483 struct-contents:
2484 empty
2485 struct-declaration
2486 struct-declaration-list struct-declaration
2487
2488 struct-declaration-list:
2489 struct-declaration-list ;
2490 ;
2491
2492 (Note that in the syntax here, unlike that in ISO C, the semicolons
2493 are included here rather than in struct-declaration, in order to
2494 describe the syntax with extra semicolons and missing semicolon at
2495 end.)
2496
2497 Objective-C:
2498
2499 struct-declaration-list:
2500 @defs ( class-name )
2501
2502 (Note this does not include a trailing semicolon, but can be
2503 followed by further declarations, and gets a pedwarn-if-pedantic
2504 when followed by a semicolon.) */
2505
2506 static struct c_typespec
2507 c_parser_struct_or_union_specifier (c_parser *parser)
2508 {
2509 struct c_typespec ret;
2510 tree attrs;
2511 tree ident = NULL_TREE;
2512 location_t struct_loc;
2513 location_t ident_loc = UNKNOWN_LOCATION;
2514 enum tree_code code;
2515 switch (c_parser_peek_token (parser)->keyword)
2516 {
2517 case RID_STRUCT:
2518 code = RECORD_TYPE;
2519 break;
2520 case RID_UNION:
2521 code = UNION_TYPE;
2522 break;
2523 default:
2524 gcc_unreachable ();
2525 }
2526 struct_loc = c_parser_peek_token (parser)->location;
2527 c_parser_consume_token (parser);
2528 attrs = c_parser_attributes (parser);
2529
2530 /* Set the location in case we create a decl now. */
2531 c_parser_set_source_position_from_token (c_parser_peek_token (parser));
2532
2533 if (c_parser_next_token_is (parser, CPP_NAME))
2534 {
2535 ident = c_parser_peek_token (parser)->value;
2536 ident_loc = c_parser_peek_token (parser)->location;
2537 struct_loc = ident_loc;
2538 c_parser_consume_token (parser);
2539 }
2540 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
2541 {
2542 /* Parse a struct or union definition. Start the scope of the
2543 tag before parsing components. */
2544 struct c_struct_parse_info *struct_info;
2545 tree type = start_struct (struct_loc, code, ident, &struct_info);
2546 tree postfix_attrs;
2547 /* We chain the components in reverse order, then put them in
2548 forward order at the end. Each struct-declaration may
2549 declare multiple components (comma-separated), so we must use
2550 chainon to join them, although when parsing each
2551 struct-declaration we can use TREE_CHAIN directly.
2552
2553 The theory behind all this is that there will be more
2554 semicolon separated fields than comma separated fields, and
2555 so we'll be minimizing the number of node traversals required
2556 by chainon. */
2557 tree contents;
2558 timevar_push (TV_PARSE_STRUCT);
2559 contents = NULL_TREE;
2560 c_parser_consume_token (parser);
2561 /* Handle the Objective-C @defs construct,
2562 e.g. foo(sizeof(struct{ @defs(ClassName) }));. */
2563 if (c_parser_next_token_is_keyword (parser, RID_AT_DEFS))
2564 {
2565 tree name;
2566 gcc_assert (c_dialect_objc ());
2567 c_parser_consume_token (parser);
2568 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2569 goto end_at_defs;
2570 if (c_parser_next_token_is (parser, CPP_NAME)
2571 && c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME)
2572 {
2573 name = c_parser_peek_token (parser)->value;
2574 c_parser_consume_token (parser);
2575 }
2576 else
2577 {
2578 c_parser_error (parser, "expected class name");
2579 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
2580 goto end_at_defs;
2581 }
2582 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2583 "expected %<)%>");
2584 contents = nreverse (objc_get_class_ivars (name));
2585 }
2586 end_at_defs:
2587 /* Parse the struct-declarations and semicolons. Problems with
2588 semicolons are diagnosed here; empty structures are diagnosed
2589 elsewhere. */
2590 while (true)
2591 {
2592 tree decls;
2593 /* Parse any stray semicolon. */
2594 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
2595 {
2596 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
2597 "extra semicolon in struct or union specified");
2598 c_parser_consume_token (parser);
2599 continue;
2600 }
2601 /* Stop if at the end of the struct or union contents. */
2602 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2603 {
2604 c_parser_consume_token (parser);
2605 break;
2606 }
2607 /* Accept #pragmas at struct scope. */
2608 if (c_parser_next_token_is (parser, CPP_PRAGMA))
2609 {
2610 c_parser_pragma (parser, pragma_struct);
2611 continue;
2612 }
2613 /* Parse some comma-separated declarations, but not the
2614 trailing semicolon if any. */
2615 decls = c_parser_struct_declaration (parser);
2616 contents = chainon (decls, contents);
2617 /* If no semicolon follows, either we have a parse error or
2618 are at the end of the struct or union and should
2619 pedwarn. */
2620 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
2621 c_parser_consume_token (parser);
2622 else
2623 {
2624 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2625 pedwarn (c_parser_peek_token (parser)->location, 0,
2626 "no semicolon at end of struct or union");
2627 else if (parser->error
2628 || !c_parser_next_token_starts_declspecs (parser))
2629 {
2630 c_parser_error (parser, "expected %<;%>");
2631 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
2632 break;
2633 }
2634
2635 /* If we come here, we have already emitted an error
2636 for an expected `;', identifier or `(', and we also
2637 recovered already. Go on with the next field. */
2638 }
2639 }
2640 postfix_attrs = c_parser_attributes (parser);
2641 ret.spec = finish_struct (struct_loc, type, nreverse (contents),
2642 chainon (attrs, postfix_attrs), struct_info);
2643 ret.kind = ctsk_tagdef;
2644 ret.expr = NULL_TREE;
2645 ret.expr_const_operands = true;
2646 timevar_pop (TV_PARSE_STRUCT);
2647 return ret;
2648 }
2649 else if (!ident)
2650 {
2651 c_parser_error (parser, "expected %<{%>");
2652 ret.spec = error_mark_node;
2653 ret.kind = ctsk_tagref;
2654 ret.expr = NULL_TREE;
2655 ret.expr_const_operands = true;
2656 return ret;
2657 }
2658 ret = parser_xref_tag (ident_loc, code, ident);
2659 return ret;
2660 }
2661
2662 /* Parse a struct-declaration (C90 6.5.2.1, C99 6.7.2.1), *without*
2663 the trailing semicolon.
2664
2665 struct-declaration:
2666 specifier-qualifier-list struct-declarator-list
2667 static_assert-declaration-no-semi
2668
2669 specifier-qualifier-list:
2670 type-specifier specifier-qualifier-list[opt]
2671 type-qualifier specifier-qualifier-list[opt]
2672 attributes specifier-qualifier-list[opt]
2673
2674 struct-declarator-list:
2675 struct-declarator
2676 struct-declarator-list , attributes[opt] struct-declarator
2677
2678 struct-declarator:
2679 declarator attributes[opt]
2680 declarator[opt] : constant-expression attributes[opt]
2681
2682 GNU extensions:
2683
2684 struct-declaration:
2685 __extension__ struct-declaration
2686 specifier-qualifier-list
2687
2688 Unlike the ISO C syntax, semicolons are handled elsewhere. The use
2689 of attributes where shown is a GNU extension. In GNU C, we accept
2690 any expression without commas in the syntax (assignment
2691 expressions, not just conditional expressions); assignment
2692 expressions will be diagnosed as non-constant. */
2693
2694 static tree
2695 c_parser_struct_declaration (c_parser *parser)
2696 {
2697 struct c_declspecs *specs;
2698 tree prefix_attrs;
2699 tree all_prefix_attrs;
2700 tree decls;
2701 location_t decl_loc;
2702 if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
2703 {
2704 int ext;
2705 tree decl;
2706 ext = disable_extension_diagnostics ();
2707 c_parser_consume_token (parser);
2708 decl = c_parser_struct_declaration (parser);
2709 restore_extension_diagnostics (ext);
2710 return decl;
2711 }
2712 if (c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT))
2713 {
2714 c_parser_static_assert_declaration_no_semi (parser);
2715 return NULL_TREE;
2716 }
2717 specs = build_null_declspecs ();
2718 decl_loc = c_parser_peek_token (parser)->location;
2719 /* Strictly by the standard, we shouldn't allow _Alignas here,
2720 but it appears to have been intended to allow it there, so
2721 we're keeping it as it is until WG14 reaches a conclusion
2722 of N1731.
2723 <http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1731.pdf> */
2724 c_parser_declspecs (parser, specs, false, true, true,
2725 true, cla_nonabstract_decl);
2726 if (parser->error)
2727 return NULL_TREE;
2728 if (!specs->declspecs_seen_p)
2729 {
2730 c_parser_error (parser, "expected specifier-qualifier-list");
2731 return NULL_TREE;
2732 }
2733 finish_declspecs (specs);
2734 if (c_parser_next_token_is (parser, CPP_SEMICOLON)
2735 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2736 {
2737 tree ret;
2738 if (specs->typespec_kind == ctsk_none)
2739 {
2740 pedwarn (decl_loc, OPT_Wpedantic,
2741 "ISO C forbids member declarations with no members");
2742 shadow_tag_warned (specs, pedantic);
2743 ret = NULL_TREE;
2744 }
2745 else
2746 {
2747 /* Support for unnamed structs or unions as members of
2748 structs or unions (which is [a] useful and [b] supports
2749 MS P-SDK). */
2750 tree attrs = NULL;
2751
2752 ret = grokfield (c_parser_peek_token (parser)->location,
2753 build_id_declarator (NULL_TREE), specs,
2754 NULL_TREE, &attrs);
2755 if (ret)
2756 decl_attributes (&ret, attrs, 0);
2757 }
2758 return ret;
2759 }
2760
2761 /* Provide better error recovery. Note that a type name here is valid,
2762 and will be treated as a field name. */
2763 if (specs->typespec_kind == ctsk_tagdef
2764 && TREE_CODE (specs->type) != ENUMERAL_TYPE
2765 && c_parser_next_token_starts_declspecs (parser)
2766 && !c_parser_next_token_is (parser, CPP_NAME))
2767 {
2768 c_parser_error (parser, "expected %<;%>, identifier or %<(%>");
2769 parser->error = false;
2770 return NULL_TREE;
2771 }
2772
2773 pending_xref_error ();
2774 prefix_attrs = specs->attrs;
2775 all_prefix_attrs = prefix_attrs;
2776 specs->attrs = NULL_TREE;
2777 decls = NULL_TREE;
2778 while (true)
2779 {
2780 /* Declaring one or more declarators or un-named bit-fields. */
2781 struct c_declarator *declarator;
2782 bool dummy = false;
2783 if (c_parser_next_token_is (parser, CPP_COLON))
2784 declarator = build_id_declarator (NULL_TREE);
2785 else
2786 declarator = c_parser_declarator (parser,
2787 specs->typespec_kind != ctsk_none,
2788 C_DTR_NORMAL, &dummy);
2789 if (declarator == NULL)
2790 {
2791 c_parser_skip_to_end_of_block_or_statement (parser);
2792 break;
2793 }
2794 if (c_parser_next_token_is (parser, CPP_COLON)
2795 || c_parser_next_token_is (parser, CPP_COMMA)
2796 || c_parser_next_token_is (parser, CPP_SEMICOLON)
2797 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE)
2798 || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2799 {
2800 tree postfix_attrs = NULL_TREE;
2801 tree width = NULL_TREE;
2802 tree d;
2803 if (c_parser_next_token_is (parser, CPP_COLON))
2804 {
2805 c_parser_consume_token (parser);
2806 width = c_parser_expr_no_commas (parser, NULL).value;
2807 }
2808 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2809 postfix_attrs = c_parser_attributes (parser);
2810 d = grokfield (c_parser_peek_token (parser)->location,
2811 declarator, specs, width, &all_prefix_attrs);
2812 decl_attributes (&d, chainon (postfix_attrs,
2813 all_prefix_attrs), 0);
2814 DECL_CHAIN (d) = decls;
2815 decls = d;
2816 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2817 all_prefix_attrs = chainon (c_parser_attributes (parser),
2818 prefix_attrs);
2819 else
2820 all_prefix_attrs = prefix_attrs;
2821 if (c_parser_next_token_is (parser, CPP_COMMA))
2822 c_parser_consume_token (parser);
2823 else if (c_parser_next_token_is (parser, CPP_SEMICOLON)
2824 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2825 {
2826 /* Semicolon consumed in caller. */
2827 break;
2828 }
2829 else
2830 {
2831 c_parser_error (parser, "expected %<,%>, %<;%> or %<}%>");
2832 break;
2833 }
2834 }
2835 else
2836 {
2837 c_parser_error (parser,
2838 "expected %<:%>, %<,%>, %<;%>, %<}%> or "
2839 "%<__attribute__%>");
2840 break;
2841 }
2842 }
2843 return decls;
2844 }
2845
2846 /* Parse a typeof specifier (a GNU extension).
2847
2848 typeof-specifier:
2849 typeof ( expression )
2850 typeof ( type-name )
2851 */
2852
2853 static struct c_typespec
2854 c_parser_typeof_specifier (c_parser *parser)
2855 {
2856 struct c_typespec ret;
2857 ret.kind = ctsk_typeof;
2858 ret.spec = error_mark_node;
2859 ret.expr = NULL_TREE;
2860 ret.expr_const_operands = true;
2861 gcc_assert (c_parser_next_token_is_keyword (parser, RID_TYPEOF));
2862 c_parser_consume_token (parser);
2863 c_inhibit_evaluation_warnings++;
2864 in_typeof++;
2865 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2866 {
2867 c_inhibit_evaluation_warnings--;
2868 in_typeof--;
2869 return ret;
2870 }
2871 if (c_parser_next_tokens_start_typename (parser, cla_prefer_id))
2872 {
2873 struct c_type_name *type = c_parser_type_name (parser);
2874 c_inhibit_evaluation_warnings--;
2875 in_typeof--;
2876 if (type != NULL)
2877 {
2878 ret.spec = groktypename (type, &ret.expr, &ret.expr_const_operands);
2879 pop_maybe_used (variably_modified_type_p (ret.spec, NULL_TREE));
2880 }
2881 }
2882 else
2883 {
2884 bool was_vm;
2885 location_t here = c_parser_peek_token (parser)->location;
2886 struct c_expr expr = c_parser_expression (parser);
2887 c_inhibit_evaluation_warnings--;
2888 in_typeof--;
2889 if (TREE_CODE (expr.value) == COMPONENT_REF
2890 && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1)))
2891 error_at (here, "%<typeof%> applied to a bit-field");
2892 mark_exp_read (expr.value);
2893 ret.spec = TREE_TYPE (expr.value);
2894 was_vm = variably_modified_type_p (ret.spec, NULL_TREE);
2895 /* This is returned with the type so that when the type is
2896 evaluated, this can be evaluated. */
2897 if (was_vm)
2898 ret.expr = c_fully_fold (expr.value, false, &ret.expr_const_operands);
2899 pop_maybe_used (was_vm);
2900 /* For use in macros such as those in <stdatomic.h>, remove
2901 _Atomic and const qualifiers from atomic types. (Possibly
2902 all qualifiers should be removed; const can be an issue for
2903 more macros using typeof than just the <stdatomic.h>
2904 ones.) */
2905 if (ret.spec != error_mark_node && TYPE_ATOMIC (ret.spec))
2906 ret.spec = c_build_qualified_type (ret.spec,
2907 (TYPE_QUALS (ret.spec)
2908 & ~(TYPE_QUAL_ATOMIC
2909 | TYPE_QUAL_CONST)));
2910 }
2911 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
2912 return ret;
2913 }
2914
2915 /* Parse an alignment-specifier.
2916
2917 C11 6.7.5:
2918
2919 alignment-specifier:
2920 _Alignas ( type-name )
2921 _Alignas ( constant-expression )
2922 */
2923
2924 static tree
2925 c_parser_alignas_specifier (c_parser * parser)
2926 {
2927 tree ret = error_mark_node;
2928 location_t loc = c_parser_peek_token (parser)->location;
2929 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ALIGNAS));
2930 c_parser_consume_token (parser);
2931 if (!flag_isoc11)
2932 {
2933 if (flag_isoc99)
2934 pedwarn (loc, OPT_Wpedantic,
2935 "ISO C99 does not support %<_Alignas%>");
2936 else
2937 pedwarn (loc, OPT_Wpedantic,
2938 "ISO C90 does not support %<_Alignas%>");
2939 }
2940 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2941 return ret;
2942 if (c_parser_next_tokens_start_typename (parser, cla_prefer_id))
2943 {
2944 struct c_type_name *type = c_parser_type_name (parser);
2945 if (type != NULL)
2946 ret = c_alignof (loc, groktypename (type, NULL, NULL));
2947 }
2948 else
2949 ret = c_parser_expr_no_commas (parser, NULL).value;
2950 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
2951 return ret;
2952 }
2953
2954 /* Parse a declarator, possibly an abstract declarator (C90 6.5.4,
2955 6.5.5, C99 6.7.5, 6.7.6). If TYPE_SEEN_P then a typedef name may
2956 be redeclared; otherwise it may not. KIND indicates which kind of
2957 declarator is wanted. Returns a valid declarator except in the
2958 case of a syntax error in which case NULL is returned. *SEEN_ID is
2959 set to true if an identifier being declared is seen; this is used
2960 to diagnose bad forms of abstract array declarators and to
2961 determine whether an identifier list is syntactically permitted.
2962
2963 declarator:
2964 pointer[opt] direct-declarator
2965
2966 direct-declarator:
2967 identifier
2968 ( attributes[opt] declarator )
2969 direct-declarator array-declarator
2970 direct-declarator ( parameter-type-list )
2971 direct-declarator ( identifier-list[opt] )
2972
2973 pointer:
2974 * type-qualifier-list[opt]
2975 * type-qualifier-list[opt] pointer
2976
2977 type-qualifier-list:
2978 type-qualifier
2979 attributes
2980 type-qualifier-list type-qualifier
2981 type-qualifier-list attributes
2982
2983 array-declarator:
2984 [ type-qualifier-list[opt] assignment-expression[opt] ]
2985 [ static type-qualifier-list[opt] assignment-expression ]
2986 [ type-qualifier-list static assignment-expression ]
2987 [ type-qualifier-list[opt] * ]
2988
2989 parameter-type-list:
2990 parameter-list
2991 parameter-list , ...
2992
2993 parameter-list:
2994 parameter-declaration
2995 parameter-list , parameter-declaration
2996
2997 parameter-declaration:
2998 declaration-specifiers declarator attributes[opt]
2999 declaration-specifiers abstract-declarator[opt] attributes[opt]
3000
3001 identifier-list:
3002 identifier
3003 identifier-list , identifier
3004
3005 abstract-declarator:
3006 pointer
3007 pointer[opt] direct-abstract-declarator
3008
3009 direct-abstract-declarator:
3010 ( attributes[opt] abstract-declarator )
3011 direct-abstract-declarator[opt] array-declarator
3012 direct-abstract-declarator[opt] ( parameter-type-list[opt] )
3013
3014 GNU extensions:
3015
3016 direct-declarator:
3017 direct-declarator ( parameter-forward-declarations
3018 parameter-type-list[opt] )
3019
3020 direct-abstract-declarator:
3021 direct-abstract-declarator[opt] ( parameter-forward-declarations
3022 parameter-type-list[opt] )
3023
3024 parameter-forward-declarations:
3025 parameter-list ;
3026 parameter-forward-declarations parameter-list ;
3027
3028 The uses of attributes shown above are GNU extensions.
3029
3030 Some forms of array declarator are not included in C99 in the
3031 syntax for abstract declarators; these are disallowed elsewhere.
3032 This may be a defect (DR#289).
3033
3034 This function also accepts an omitted abstract declarator as being
3035 an abstract declarator, although not part of the formal syntax. */
3036
3037 static struct c_declarator *
3038 c_parser_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
3039 bool *seen_id)
3040 {
3041 /* Parse any initial pointer part. */
3042 if (c_parser_next_token_is (parser, CPP_MULT))
3043 {
3044 struct c_declspecs *quals_attrs = build_null_declspecs ();
3045 struct c_declarator *inner;
3046 c_parser_consume_token (parser);
3047 c_parser_declspecs (parser, quals_attrs, false, false, true,
3048 false, cla_prefer_id);
3049 inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
3050 if (inner == NULL)
3051 return NULL;
3052 else
3053 return make_pointer_declarator (quals_attrs, inner);
3054 }
3055 /* Now we have a direct declarator, direct abstract declarator or
3056 nothing (which counts as a direct abstract declarator here). */
3057 return c_parser_direct_declarator (parser, type_seen_p, kind, seen_id);
3058 }
3059
3060 /* Parse a direct declarator or direct abstract declarator; arguments
3061 as c_parser_declarator. */
3062
3063 static struct c_declarator *
3064 c_parser_direct_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
3065 bool *seen_id)
3066 {
3067 /* The direct declarator must start with an identifier (possibly
3068 omitted) or a parenthesized declarator (possibly abstract). In
3069 an ordinary declarator, initial parentheses must start a
3070 parenthesized declarator. In an abstract declarator or parameter
3071 declarator, they could start a parenthesized declarator or a
3072 parameter list. To tell which, the open parenthesis and any
3073 following attributes must be read. If a declaration specifier
3074 follows, then it is a parameter list; if the specifier is a
3075 typedef name, there might be an ambiguity about redeclaring it,
3076 which is resolved in the direction of treating it as a typedef
3077 name. If a close parenthesis follows, it is also an empty
3078 parameter list, as the syntax does not permit empty abstract
3079 declarators. Otherwise, it is a parenthesized declarator (in
3080 which case the analysis may be repeated inside it, recursively).
3081
3082 ??? There is an ambiguity in a parameter declaration "int
3083 (__attribute__((foo)) x)", where x is not a typedef name: it
3084 could be an abstract declarator for a function, or declare x with
3085 parentheses. The proper resolution of this ambiguity needs
3086 documenting. At present we follow an accident of the old
3087 parser's implementation, whereby the first parameter must have
3088 some declaration specifiers other than just attributes. Thus as
3089 a parameter declaration it is treated as a parenthesized
3090 parameter named x, and as an abstract declarator it is
3091 rejected.
3092
3093 ??? Also following the old parser, attributes inside an empty
3094 parameter list are ignored, making it a list not yielding a
3095 prototype, rather than giving an error or making it have one
3096 parameter with implicit type int.
3097
3098 ??? Also following the old parser, typedef names may be
3099 redeclared in declarators, but not Objective-C class names. */
3100
3101 if (kind != C_DTR_ABSTRACT
3102 && c_parser_next_token_is (parser, CPP_NAME)
3103 && ((type_seen_p
3104 && (c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME
3105 || c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME))
3106 || c_parser_peek_token (parser)->id_kind == C_ID_ID))
3107 {
3108 struct c_declarator *inner
3109 = build_id_declarator (c_parser_peek_token (parser)->value);
3110 *seen_id = true;
3111 inner->id_loc = c_parser_peek_token (parser)->location;
3112 c_parser_consume_token (parser);
3113 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
3114 }
3115
3116 if (kind != C_DTR_NORMAL
3117 && c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
3118 {
3119 struct c_declarator *inner = build_id_declarator (NULL_TREE);
3120 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
3121 }
3122
3123 /* Either we are at the end of an abstract declarator, or we have
3124 parentheses. */
3125
3126 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
3127 {
3128 tree attrs;
3129 struct c_declarator *inner;
3130 c_parser_consume_token (parser);
3131 attrs = c_parser_attributes (parser);
3132 if (kind != C_DTR_NORMAL
3133 && (c_parser_next_token_starts_declspecs (parser)
3134 || c_parser_next_token_is (parser, CPP_CLOSE_PAREN)))
3135 {
3136 struct c_arg_info *args
3137 = c_parser_parms_declarator (parser, kind == C_DTR_NORMAL,
3138 attrs);
3139 if (args == NULL)
3140 return NULL;
3141 else
3142 {
3143 inner
3144 = build_function_declarator (args,
3145 build_id_declarator (NULL_TREE));
3146 return c_parser_direct_declarator_inner (parser, *seen_id,
3147 inner);
3148 }
3149 }
3150 /* A parenthesized declarator. */
3151 inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
3152 if (inner != NULL && attrs != NULL)
3153 inner = build_attrs_declarator (attrs, inner);
3154 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3155 {
3156 c_parser_consume_token (parser);
3157 if (inner == NULL)
3158 return NULL;
3159 else
3160 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
3161 }
3162 else
3163 {
3164 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3165 "expected %<)%>");
3166 return NULL;
3167 }
3168 }
3169 else
3170 {
3171 if (kind == C_DTR_NORMAL)
3172 {
3173 c_parser_error (parser, "expected identifier or %<(%>");
3174 return NULL;
3175 }
3176 else
3177 return build_id_declarator (NULL_TREE);
3178 }
3179 }
3180
3181 /* Parse part of a direct declarator or direct abstract declarator,
3182 given that some (in INNER) has already been parsed; ID_PRESENT is
3183 true if an identifier is present, false for an abstract
3184 declarator. */
3185
3186 static struct c_declarator *
3187 c_parser_direct_declarator_inner (c_parser *parser, bool id_present,
3188 struct c_declarator *inner)
3189 {
3190 /* Parse a sequence of array declarators and parameter lists. */
3191 if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
3192 {
3193 location_t brace_loc = c_parser_peek_token (parser)->location;
3194 struct c_declarator *declarator;
3195 struct c_declspecs *quals_attrs = build_null_declspecs ();
3196 bool static_seen;
3197 bool star_seen;
3198 struct c_expr dimen;
3199 dimen.value = NULL_TREE;
3200 dimen.original_code = ERROR_MARK;
3201 dimen.original_type = NULL_TREE;
3202 c_parser_consume_token (parser);
3203 c_parser_declspecs (parser, quals_attrs, false, false, true,
3204 false, cla_prefer_id);
3205 static_seen = c_parser_next_token_is_keyword (parser, RID_STATIC);
3206 if (static_seen)
3207 c_parser_consume_token (parser);
3208 if (static_seen && !quals_attrs->declspecs_seen_p)
3209 c_parser_declspecs (parser, quals_attrs, false, false, true,
3210 false, cla_prefer_id);
3211 if (!quals_attrs->declspecs_seen_p)
3212 quals_attrs = NULL;
3213 /* If "static" is present, there must be an array dimension.
3214 Otherwise, there may be a dimension, "*", or no
3215 dimension. */
3216 if (static_seen)
3217 {
3218 star_seen = false;
3219 dimen = c_parser_expr_no_commas (parser, NULL);
3220 }
3221 else
3222 {
3223 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3224 {
3225 dimen.value = NULL_TREE;
3226 star_seen = false;
3227 }
3228 else if (flag_enable_cilkplus
3229 && c_parser_next_token_is (parser, CPP_COLON))
3230 {
3231 dimen.value = error_mark_node;
3232 star_seen = false;
3233 error_at (c_parser_peek_token (parser)->location,
3234 "array notations cannot be used in declaration");
3235 c_parser_consume_token (parser);
3236 }
3237 else if (c_parser_next_token_is (parser, CPP_MULT))
3238 {
3239 if (c_parser_peek_2nd_token (parser)->type == CPP_CLOSE_SQUARE)
3240 {
3241 dimen.value = NULL_TREE;
3242 star_seen = true;
3243 c_parser_consume_token (parser);
3244 }
3245 else
3246 {
3247 star_seen = false;
3248 dimen = c_parser_expr_no_commas (parser, NULL);
3249 }
3250 }
3251 else
3252 {
3253 star_seen = false;
3254 dimen = c_parser_expr_no_commas (parser, NULL);
3255 }
3256 }
3257 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3258 c_parser_consume_token (parser);
3259 else if (flag_enable_cilkplus
3260 && c_parser_next_token_is (parser, CPP_COLON))
3261 {
3262 error_at (c_parser_peek_token (parser)->location,
3263 "array notations cannot be used in declaration");
3264 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
3265 return NULL;
3266 }
3267 else
3268 {
3269 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
3270 "expected %<]%>");
3271 return NULL;
3272 }
3273 if (dimen.value)
3274 dimen = convert_lvalue_to_rvalue (brace_loc, dimen, true, true);
3275 declarator = build_array_declarator (brace_loc, dimen.value, quals_attrs,
3276 static_seen, star_seen);
3277 if (declarator == NULL)
3278 return NULL;
3279 inner = set_array_declarator_inner (declarator, inner);
3280 return c_parser_direct_declarator_inner (parser, id_present, inner);
3281 }
3282 else if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
3283 {
3284 tree attrs;
3285 struct c_arg_info *args;
3286 c_parser_consume_token (parser);
3287 attrs = c_parser_attributes (parser);
3288 args = c_parser_parms_declarator (parser, id_present, attrs);
3289 if (args == NULL)
3290 return NULL;
3291 else
3292 {
3293 inner = build_function_declarator (args, inner);
3294 return c_parser_direct_declarator_inner (parser, id_present, inner);
3295 }
3296 }
3297 return inner;
3298 }
3299
3300 /* Parse a parameter list or identifier list, including the closing
3301 parenthesis but not the opening one. ATTRS are the attributes at
3302 the start of the list. ID_LIST_OK is true if an identifier list is
3303 acceptable; such a list must not have attributes at the start. */
3304
3305 static struct c_arg_info *
3306 c_parser_parms_declarator (c_parser *parser, bool id_list_ok, tree attrs)
3307 {
3308 push_scope ();
3309 declare_parm_level ();
3310 /* If the list starts with an identifier, it is an identifier list.
3311 Otherwise, it is either a prototype list or an empty list. */
3312 if (id_list_ok
3313 && !attrs
3314 && c_parser_next_token_is (parser, CPP_NAME)
3315 && c_parser_peek_token (parser)->id_kind == C_ID_ID
3316
3317 /* Look ahead to detect typos in type names. */
3318 && c_parser_peek_2nd_token (parser)->type != CPP_NAME
3319 && c_parser_peek_2nd_token (parser)->type != CPP_MULT
3320 && c_parser_peek_2nd_token (parser)->type != CPP_OPEN_PAREN
3321 && c_parser_peek_2nd_token (parser)->type != CPP_OPEN_SQUARE)
3322 {
3323 tree list = NULL_TREE, *nextp = &list;
3324 while (c_parser_next_token_is (parser, CPP_NAME)
3325 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
3326 {
3327 *nextp = build_tree_list (NULL_TREE,
3328 c_parser_peek_token (parser)->value);
3329 nextp = & TREE_CHAIN (*nextp);
3330 c_parser_consume_token (parser);
3331 if (c_parser_next_token_is_not (parser, CPP_COMMA))
3332 break;
3333 c_parser_consume_token (parser);
3334 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3335 {
3336 c_parser_error (parser, "expected identifier");
3337 break;
3338 }
3339 }
3340 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3341 {
3342 struct c_arg_info *ret = build_arg_info ();
3343 ret->types = list;
3344 c_parser_consume_token (parser);
3345 pop_scope ();
3346 return ret;
3347 }
3348 else
3349 {
3350 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3351 "expected %<)%>");
3352 pop_scope ();
3353 return NULL;
3354 }
3355 }
3356 else
3357 {
3358 struct c_arg_info *ret = c_parser_parms_list_declarator (parser, attrs,
3359 NULL);
3360 pop_scope ();
3361 return ret;
3362 }
3363 }
3364
3365 /* Parse a parameter list (possibly empty), including the closing
3366 parenthesis but not the opening one. ATTRS are the attributes at
3367 the start of the list. EXPR is NULL or an expression that needs to
3368 be evaluated for the side effects of array size expressions in the
3369 parameters. */
3370
3371 static struct c_arg_info *
3372 c_parser_parms_list_declarator (c_parser *parser, tree attrs, tree expr)
3373 {
3374 bool bad_parm = false;
3375
3376 /* ??? Following the old parser, forward parameter declarations may
3377 use abstract declarators, and if no real parameter declarations
3378 follow the forward declarations then this is not diagnosed. Also
3379 note as above that attributes are ignored as the only contents of
3380 the parentheses, or as the only contents after forward
3381 declarations. */
3382 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3383 {
3384 struct c_arg_info *ret = build_arg_info ();
3385 c_parser_consume_token (parser);
3386 return ret;
3387 }
3388 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
3389 {
3390 struct c_arg_info *ret = build_arg_info ();
3391
3392 if (flag_allow_parameterless_variadic_functions)
3393 {
3394 /* F (...) is allowed. */
3395 ret->types = NULL_TREE;
3396 }
3397 else
3398 {
3399 /* Suppress -Wold-style-definition for this case. */
3400 ret->types = error_mark_node;
3401 error_at (c_parser_peek_token (parser)->location,
3402 "ISO C requires a named argument before %<...%>");
3403 }
3404 c_parser_consume_token (parser);
3405 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3406 {
3407 c_parser_consume_token (parser);
3408 return ret;
3409 }
3410 else
3411 {
3412 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3413 "expected %<)%>");
3414 return NULL;
3415 }
3416 }
3417 /* Nonempty list of parameters, either terminated with semicolon
3418 (forward declarations; recurse) or with close parenthesis (normal
3419 function) or with ", ... )" (variadic function). */
3420 while (true)
3421 {
3422 /* Parse a parameter. */
3423 struct c_parm *parm = c_parser_parameter_declaration (parser, attrs);
3424 attrs = NULL_TREE;
3425 if (parm == NULL)
3426 bad_parm = true;
3427 else
3428 push_parm_decl (parm, &expr);
3429 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
3430 {
3431 tree new_attrs;
3432 c_parser_consume_token (parser);
3433 mark_forward_parm_decls ();
3434 new_attrs = c_parser_attributes (parser);
3435 return c_parser_parms_list_declarator (parser, new_attrs, expr);
3436 }
3437 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3438 {
3439 c_parser_consume_token (parser);
3440 if (bad_parm)
3441 return NULL;
3442 else
3443 return get_parm_info (false, expr);
3444 }
3445 if (!c_parser_require (parser, CPP_COMMA,
3446 "expected %<;%>, %<,%> or %<)%>"))
3447 {
3448 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
3449 return NULL;
3450 }
3451 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
3452 {
3453 c_parser_consume_token (parser);
3454 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3455 {
3456 c_parser_consume_token (parser);
3457 if (bad_parm)
3458 return NULL;
3459 else
3460 return get_parm_info (true, expr);
3461 }
3462 else
3463 {
3464 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3465 "expected %<)%>");
3466 return NULL;
3467 }
3468 }
3469 }
3470 }
3471
3472 /* Parse a parameter declaration. ATTRS are the attributes at the
3473 start of the declaration if it is the first parameter. */
3474
3475 static struct c_parm *
3476 c_parser_parameter_declaration (c_parser *parser, tree attrs)
3477 {
3478 struct c_declspecs *specs;
3479 struct c_declarator *declarator;
3480 tree prefix_attrs;
3481 tree postfix_attrs = NULL_TREE;
3482 bool dummy = false;
3483
3484 /* Accept #pragmas between parameter declarations. */
3485 while (c_parser_next_token_is (parser, CPP_PRAGMA))
3486 c_parser_pragma (parser, pragma_param);
3487
3488 if (!c_parser_next_token_starts_declspecs (parser))
3489 {
3490 c_token *token = c_parser_peek_token (parser);
3491 if (parser->error)
3492 return NULL;
3493 c_parser_set_source_position_from_token (token);
3494 if (c_parser_next_tokens_start_typename (parser, cla_prefer_type))
3495 {
3496 error ("unknown type name %qE", token->value);
3497 parser->error = true;
3498 }
3499 /* ??? In some Objective-C cases '...' isn't applicable so there
3500 should be a different message. */
3501 else
3502 c_parser_error (parser,
3503 "expected declaration specifiers or %<...%>");
3504 c_parser_skip_to_end_of_parameter (parser);
3505 return NULL;
3506 }
3507 specs = build_null_declspecs ();
3508 if (attrs)
3509 {
3510 declspecs_add_attrs (input_location, specs, attrs);
3511 attrs = NULL_TREE;
3512 }
3513 c_parser_declspecs (parser, specs, true, true, true, true,
3514 cla_nonabstract_decl);
3515 finish_declspecs (specs);
3516 pending_xref_error ();
3517 prefix_attrs = specs->attrs;
3518 specs->attrs = NULL_TREE;
3519 declarator = c_parser_declarator (parser,
3520 specs->typespec_kind != ctsk_none,
3521 C_DTR_PARM, &dummy);
3522 if (declarator == NULL)
3523 {
3524 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
3525 return NULL;
3526 }
3527 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
3528 postfix_attrs = c_parser_attributes (parser);
3529 return build_c_parm (specs, chainon (postfix_attrs, prefix_attrs),
3530 declarator);
3531 }
3532
3533 /* Parse a string literal in an asm expression. It should not be
3534 translated, and wide string literals are an error although
3535 permitted by the syntax. This is a GNU extension.
3536
3537 asm-string-literal:
3538 string-literal
3539
3540 ??? At present, following the old parser, the caller needs to have
3541 set lex_untranslated_string to 1. It would be better to follow the
3542 C++ parser rather than using this kludge. */
3543
3544 static tree
3545 c_parser_asm_string_literal (c_parser *parser)
3546 {
3547 tree str;
3548 int save_flag = warn_overlength_strings;
3549 warn_overlength_strings = 0;
3550 if (c_parser_next_token_is (parser, CPP_STRING))
3551 {
3552 str = c_parser_peek_token (parser)->value;
3553 c_parser_consume_token (parser);
3554 }
3555 else if (c_parser_next_token_is (parser, CPP_WSTRING))
3556 {
3557 error_at (c_parser_peek_token (parser)->location,
3558 "wide string literal in %<asm%>");
3559 str = build_string (1, "");
3560 c_parser_consume_token (parser);
3561 }
3562 else
3563 {
3564 c_parser_error (parser, "expected string literal");
3565 str = NULL_TREE;
3566 }
3567 warn_overlength_strings = save_flag;
3568 return str;
3569 }
3570
3571 /* Parse a simple asm expression. This is used in restricted
3572 contexts, where a full expression with inputs and outputs does not
3573 make sense. This is a GNU extension.
3574
3575 simple-asm-expr:
3576 asm ( asm-string-literal )
3577 */
3578
3579 static tree
3580 c_parser_simple_asm_expr (c_parser *parser)
3581 {
3582 tree str;
3583 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM));
3584 /* ??? Follow the C++ parser rather than using the
3585 lex_untranslated_string kludge. */
3586 parser->lex_untranslated_string = true;
3587 c_parser_consume_token (parser);
3588 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3589 {
3590 parser->lex_untranslated_string = false;
3591 return NULL_TREE;
3592 }
3593 str = c_parser_asm_string_literal (parser);
3594 parser->lex_untranslated_string = false;
3595 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
3596 {
3597 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
3598 return NULL_TREE;
3599 }
3600 return str;
3601 }
3602
3603 static tree
3604 c_parser_attribute_any_word (c_parser *parser)
3605 {
3606 tree attr_name = NULL_TREE;
3607
3608 if (c_parser_next_token_is (parser, CPP_KEYWORD))
3609 {
3610 /* ??? See comment above about what keywords are accepted here. */
3611 bool ok;
3612 switch (c_parser_peek_token (parser)->keyword)
3613 {
3614 case RID_STATIC:
3615 case RID_UNSIGNED:
3616 case RID_LONG:
3617 case RID_INT128:
3618 case RID_CONST:
3619 case RID_EXTERN:
3620 case RID_REGISTER:
3621 case RID_TYPEDEF:
3622 case RID_SHORT:
3623 case RID_INLINE:
3624 case RID_NORETURN:
3625 case RID_VOLATILE:
3626 case RID_SIGNED:
3627 case RID_AUTO:
3628 case RID_RESTRICT:
3629 case RID_COMPLEX:
3630 case RID_THREAD:
3631 case RID_INT:
3632 case RID_CHAR:
3633 case RID_FLOAT:
3634 case RID_DOUBLE:
3635 case RID_VOID:
3636 case RID_DFLOAT32:
3637 case RID_DFLOAT64:
3638 case RID_DFLOAT128:
3639 case RID_BOOL:
3640 case RID_FRACT:
3641 case RID_ACCUM:
3642 case RID_SAT:
3643 case RID_TRANSACTION_ATOMIC:
3644 case RID_TRANSACTION_CANCEL:
3645 case RID_ATOMIC:
3646 ok = true;
3647 break;
3648 default:
3649 ok = false;
3650 break;
3651 }
3652 if (!ok)
3653 return NULL_TREE;
3654
3655 /* Accept __attribute__((__const)) as __attribute__((const)) etc. */
3656 attr_name = ridpointers[(int) c_parser_peek_token (parser)->keyword];
3657 }
3658 else if (c_parser_next_token_is (parser, CPP_NAME))
3659 attr_name = c_parser_peek_token (parser)->value;
3660
3661 return attr_name;
3662 }
3663
3664 /* Parse (possibly empty) attributes. This is a GNU extension.
3665
3666 attributes:
3667 empty
3668 attributes attribute
3669
3670 attribute:
3671 __attribute__ ( ( attribute-list ) )
3672
3673 attribute-list:
3674 attrib
3675 attribute_list , attrib
3676
3677 attrib:
3678 empty
3679 any-word
3680 any-word ( identifier )
3681 any-word ( identifier , nonempty-expr-list )
3682 any-word ( expr-list )
3683
3684 where the "identifier" must not be declared as a type, and
3685 "any-word" may be any identifier (including one declared as a
3686 type), a reserved word storage class specifier, type specifier or
3687 type qualifier. ??? This still leaves out most reserved keywords
3688 (following the old parser), shouldn't we include them, and why not
3689 allow identifiers declared as types to start the arguments? */
3690
3691 static tree
3692 c_parser_attributes (c_parser *parser)
3693 {
3694 tree attrs = NULL_TREE;
3695 while (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
3696 {
3697 /* ??? Follow the C++ parser rather than using the
3698 lex_untranslated_string kludge. */
3699 parser->lex_untranslated_string = true;
3700 c_parser_consume_token (parser);
3701 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3702 {
3703 parser->lex_untranslated_string = false;
3704 return attrs;
3705 }
3706 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3707 {
3708 parser->lex_untranslated_string = false;
3709 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
3710 return attrs;
3711 }
3712 /* Parse the attribute list. */
3713 while (c_parser_next_token_is (parser, CPP_COMMA)
3714 || c_parser_next_token_is (parser, CPP_NAME)
3715 || c_parser_next_token_is (parser, CPP_KEYWORD))
3716 {
3717 tree attr, attr_name, attr_args;
3718 vec<tree, va_gc> *expr_list;
3719 if (c_parser_next_token_is (parser, CPP_COMMA))
3720 {
3721 c_parser_consume_token (parser);
3722 continue;
3723 }
3724
3725 attr_name = c_parser_attribute_any_word (parser);
3726 if (attr_name == NULL)
3727 break;
3728 c_parser_consume_token (parser);
3729 if (c_parser_next_token_is_not (parser, CPP_OPEN_PAREN))
3730 {
3731 attr = build_tree_list (attr_name, NULL_TREE);
3732 attrs = chainon (attrs, attr);
3733 continue;
3734 }
3735 c_parser_consume_token (parser);
3736 /* Parse the attribute contents. If they start with an
3737 identifier which is followed by a comma or close
3738 parenthesis, then the arguments start with that
3739 identifier; otherwise they are an expression list.
3740 In objective-c the identifier may be a classname. */
3741 if (c_parser_next_token_is (parser, CPP_NAME)
3742 && (c_parser_peek_token (parser)->id_kind == C_ID_ID
3743 || (c_dialect_objc ()
3744 && c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME))
3745 && ((c_parser_peek_2nd_token (parser)->type == CPP_COMMA)
3746 || (c_parser_peek_2nd_token (parser)->type
3747 == CPP_CLOSE_PAREN)))
3748 {
3749 tree arg1 = c_parser_peek_token (parser)->value;
3750 c_parser_consume_token (parser);
3751 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3752 attr_args = build_tree_list (NULL_TREE, arg1);
3753 else
3754 {
3755 tree tree_list;
3756 c_parser_consume_token (parser);
3757 expr_list = c_parser_expr_list (parser, false, true,
3758 NULL, NULL, NULL);
3759 tree_list = build_tree_list_vec (expr_list);
3760 attr_args = tree_cons (NULL_TREE, arg1, tree_list);
3761 release_tree_vector (expr_list);
3762 }
3763 }
3764 else
3765 {
3766 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3767 attr_args = NULL_TREE;
3768 else
3769 {
3770 expr_list = c_parser_expr_list (parser, false, true,
3771 NULL, NULL, NULL);
3772 attr_args = build_tree_list_vec (expr_list);
3773 release_tree_vector (expr_list);
3774 }
3775 }
3776 attr = build_tree_list (attr_name, attr_args);
3777 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3778 c_parser_consume_token (parser);
3779 else
3780 {
3781 parser->lex_untranslated_string = false;
3782 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3783 "expected %<)%>");
3784 return attrs;
3785 }
3786 attrs = chainon (attrs, attr);
3787 }
3788 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3789 c_parser_consume_token (parser);
3790 else
3791 {
3792 parser->lex_untranslated_string = false;
3793 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3794 "expected %<)%>");
3795 return attrs;
3796 }
3797 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3798 c_parser_consume_token (parser);
3799 else
3800 {
3801 parser->lex_untranslated_string = false;
3802 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3803 "expected %<)%>");
3804 return attrs;
3805 }
3806 parser->lex_untranslated_string = false;
3807 }
3808 return attrs;
3809 }
3810
3811 /* Parse a type name (C90 6.5.5, C99 6.7.6).
3812
3813 type-name:
3814 specifier-qualifier-list abstract-declarator[opt]
3815 */
3816
3817 static struct c_type_name *
3818 c_parser_type_name (c_parser *parser)
3819 {
3820 struct c_declspecs *specs = build_null_declspecs ();
3821 struct c_declarator *declarator;
3822 struct c_type_name *ret;
3823 bool dummy = false;
3824 c_parser_declspecs (parser, specs, false, true, true, false,
3825 cla_prefer_type);
3826 if (!specs->declspecs_seen_p)
3827 {
3828 c_parser_error (parser, "expected specifier-qualifier-list");
3829 return NULL;
3830 }
3831 if (specs->type != error_mark_node)
3832 {
3833 pending_xref_error ();
3834 finish_declspecs (specs);
3835 }
3836 declarator = c_parser_declarator (parser,
3837 specs->typespec_kind != ctsk_none,
3838 C_DTR_ABSTRACT, &dummy);
3839 if (declarator == NULL)
3840 return NULL;
3841 ret = XOBNEW (&parser_obstack, struct c_type_name);
3842 ret->specs = specs;
3843 ret->declarator = declarator;
3844 return ret;
3845 }
3846
3847 /* Parse an initializer (C90 6.5.7, C99 6.7.8).
3848
3849 initializer:
3850 assignment-expression
3851 { initializer-list }
3852 { initializer-list , }
3853
3854 initializer-list:
3855 designation[opt] initializer
3856 initializer-list , designation[opt] initializer
3857
3858 designation:
3859 designator-list =
3860
3861 designator-list:
3862 designator
3863 designator-list designator
3864
3865 designator:
3866 array-designator
3867 . identifier
3868
3869 array-designator:
3870 [ constant-expression ]
3871
3872 GNU extensions:
3873
3874 initializer:
3875 { }
3876
3877 designation:
3878 array-designator
3879 identifier :
3880
3881 array-designator:
3882 [ constant-expression ... constant-expression ]
3883
3884 Any expression without commas is accepted in the syntax for the
3885 constant-expressions, with non-constant expressions rejected later.
3886
3887 This function is only used for top-level initializers; for nested
3888 ones, see c_parser_initval. */
3889
3890 static struct c_expr
3891 c_parser_initializer (c_parser *parser)
3892 {
3893 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
3894 return c_parser_braced_init (parser, NULL_TREE, false);
3895 else
3896 {
3897 struct c_expr ret;
3898 location_t loc = c_parser_peek_token (parser)->location;
3899 ret = c_parser_expr_no_commas (parser, NULL);
3900 if (TREE_CODE (ret.value) != STRING_CST
3901 && TREE_CODE (ret.value) != COMPOUND_LITERAL_EXPR)
3902 ret = convert_lvalue_to_rvalue (loc, ret, true, true);
3903 return ret;
3904 }
3905 }
3906
3907 /* Parse a braced initializer list. TYPE is the type specified for a
3908 compound literal, and NULL_TREE for other initializers and for
3909 nested braced lists. NESTED_P is true for nested braced lists,
3910 false for the list of a compound literal or the list that is the
3911 top-level initializer in a declaration. */
3912
3913 static struct c_expr
3914 c_parser_braced_init (c_parser *parser, tree type, bool nested_p)
3915 {
3916 struct c_expr ret;
3917 struct obstack braced_init_obstack;
3918 location_t brace_loc = c_parser_peek_token (parser)->location;
3919 gcc_obstack_init (&braced_init_obstack);
3920 gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE));
3921 c_parser_consume_token (parser);
3922 if (nested_p)
3923 push_init_level (0, &braced_init_obstack);
3924 else
3925 really_start_incremental_init (type);
3926 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3927 {
3928 pedwarn (brace_loc, OPT_Wpedantic, "ISO C forbids empty initializer braces");
3929 }
3930 else
3931 {
3932 /* Parse a non-empty initializer list, possibly with a trailing
3933 comma. */
3934 while (true)
3935 {
3936 c_parser_initelt (parser, &braced_init_obstack);
3937 if (parser->error)
3938 break;
3939 if (c_parser_next_token_is (parser, CPP_COMMA))
3940 c_parser_consume_token (parser);
3941 else
3942 break;
3943 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3944 break;
3945 }
3946 }
3947 if (c_parser_next_token_is_not (parser, CPP_CLOSE_BRACE))
3948 {
3949 ret.value = error_mark_node;
3950 ret.original_code = ERROR_MARK;
3951 ret.original_type = NULL;
3952 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, "expected %<}%>");
3953 pop_init_level (0, &braced_init_obstack);
3954 obstack_free (&braced_init_obstack, NULL);
3955 return ret;
3956 }
3957 c_parser_consume_token (parser);
3958 ret = pop_init_level (0, &braced_init_obstack);
3959 obstack_free (&braced_init_obstack, NULL);
3960 return ret;
3961 }
3962
3963 /* Parse a nested initializer, including designators. */
3964
3965 static void
3966 c_parser_initelt (c_parser *parser, struct obstack * braced_init_obstack)
3967 {
3968 /* Parse any designator or designator list. A single array
3969 designator may have the subsequent "=" omitted in GNU C, but a
3970 longer list or a structure member designator may not. */
3971 if (c_parser_next_token_is (parser, CPP_NAME)
3972 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
3973 {
3974 /* Old-style structure member designator. */
3975 set_init_label (c_parser_peek_token (parser)->value,
3976 braced_init_obstack);
3977 /* Use the colon as the error location. */
3978 pedwarn (c_parser_peek_2nd_token (parser)->location, OPT_Wpedantic,
3979 "obsolete use of designated initializer with %<:%>");
3980 c_parser_consume_token (parser);
3981 c_parser_consume_token (parser);
3982 }
3983 else
3984 {
3985 /* des_seen is 0 if there have been no designators, 1 if there
3986 has been a single array designator and 2 otherwise. */
3987 int des_seen = 0;
3988 /* Location of a designator. */
3989 location_t des_loc = UNKNOWN_LOCATION; /* Quiet warning. */
3990 while (c_parser_next_token_is (parser, CPP_OPEN_SQUARE)
3991 || c_parser_next_token_is (parser, CPP_DOT))
3992 {
3993 int des_prev = des_seen;
3994 if (!des_seen)
3995 des_loc = c_parser_peek_token (parser)->location;
3996 if (des_seen < 2)
3997 des_seen++;
3998 if (c_parser_next_token_is (parser, CPP_DOT))
3999 {
4000 des_seen = 2;
4001 c_parser_consume_token (parser);
4002 if (c_parser_next_token_is (parser, CPP_NAME))
4003 {
4004 set_init_label (c_parser_peek_token (parser)->value,
4005 braced_init_obstack);
4006 c_parser_consume_token (parser);
4007 }
4008 else
4009 {
4010 struct c_expr init;
4011 init.value = error_mark_node;
4012 init.original_code = ERROR_MARK;
4013 init.original_type = NULL;
4014 c_parser_error (parser, "expected identifier");
4015 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
4016 process_init_element (init, false, braced_init_obstack);
4017 return;
4018 }
4019 }
4020 else
4021 {
4022 tree first, second;
4023 location_t ellipsis_loc = UNKNOWN_LOCATION; /* Quiet warning. */
4024 /* ??? Following the old parser, [ objc-receiver
4025 objc-message-args ] is accepted as an initializer,
4026 being distinguished from a designator by what follows
4027 the first assignment expression inside the square
4028 brackets, but after a first array designator a
4029 subsequent square bracket is for Objective-C taken to
4030 start an expression, using the obsolete form of
4031 designated initializer without '=', rather than
4032 possibly being a second level of designation: in LALR
4033 terms, the '[' is shifted rather than reducing
4034 designator to designator-list. */
4035 if (des_prev == 1 && c_dialect_objc ())
4036 {
4037 des_seen = des_prev;
4038 break;
4039 }
4040 if (des_prev == 0 && c_dialect_objc ())
4041 {
4042 /* This might be an array designator or an
4043 Objective-C message expression. If the former,
4044 continue parsing here; if the latter, parse the
4045 remainder of the initializer given the starting
4046 primary-expression. ??? It might make sense to
4047 distinguish when des_prev == 1 as well; see
4048 previous comment. */
4049 tree rec, args;
4050 struct c_expr mexpr;
4051 c_parser_consume_token (parser);
4052 if (c_parser_peek_token (parser)->type == CPP_NAME
4053 && ((c_parser_peek_token (parser)->id_kind
4054 == C_ID_TYPENAME)
4055 || (c_parser_peek_token (parser)->id_kind
4056 == C_ID_CLASSNAME)))
4057 {
4058 /* Type name receiver. */
4059 tree id = c_parser_peek_token (parser)->value;
4060 c_parser_consume_token (parser);
4061 rec = objc_get_class_reference (id);
4062 goto parse_message_args;
4063 }
4064 first = c_parser_expr_no_commas (parser, NULL).value;
4065 mark_exp_read (first);
4066 if (c_parser_next_token_is (parser, CPP_ELLIPSIS)
4067 || c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
4068 goto array_desig_after_first;
4069 /* Expression receiver. So far only one part
4070 without commas has been parsed; there might be
4071 more of the expression. */
4072 rec = first;
4073 while (c_parser_next_token_is (parser, CPP_COMMA))
4074 {
4075 struct c_expr next;
4076 location_t comma_loc, exp_loc;
4077 comma_loc = c_parser_peek_token (parser)->location;
4078 c_parser_consume_token (parser);
4079 exp_loc = c_parser_peek_token (parser)->location;
4080 next = c_parser_expr_no_commas (parser, NULL);
4081 next = convert_lvalue_to_rvalue (exp_loc, next,
4082 true, true);
4083 rec = build_compound_expr (comma_loc, rec, next.value);
4084 }
4085 parse_message_args:
4086 /* Now parse the objc-message-args. */
4087 args = c_parser_objc_message_args (parser);
4088 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
4089 "expected %<]%>");
4090 mexpr.value
4091 = objc_build_message_expr (rec, args);
4092 mexpr.original_code = ERROR_MARK;
4093 mexpr.original_type = NULL;
4094 /* Now parse and process the remainder of the
4095 initializer, starting with this message
4096 expression as a primary-expression. */
4097 c_parser_initval (parser, &mexpr, braced_init_obstack);
4098 return;
4099 }
4100 c_parser_consume_token (parser);
4101 first = c_parser_expr_no_commas (parser, NULL).value;
4102 mark_exp_read (first);
4103 array_desig_after_first:
4104 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
4105 {
4106 ellipsis_loc = c_parser_peek_token (parser)->location;
4107 c_parser_consume_token (parser);
4108 second = c_parser_expr_no_commas (parser, NULL).value;
4109 mark_exp_read (second);
4110 }
4111 else
4112 second = NULL_TREE;
4113 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
4114 {
4115 c_parser_consume_token (parser);
4116 set_init_index (first, second, braced_init_obstack);
4117 if (second)
4118 pedwarn (ellipsis_loc, OPT_Wpedantic,
4119 "ISO C forbids specifying range of elements to initialize");
4120 }
4121 else
4122 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
4123 "expected %<]%>");
4124 }
4125 }
4126 if (des_seen >= 1)
4127 {
4128 if (c_parser_next_token_is (parser, CPP_EQ))
4129 {
4130 if (!flag_isoc99)
4131 pedwarn (des_loc, OPT_Wpedantic,
4132 "ISO C90 forbids specifying subobject to initialize");
4133 c_parser_consume_token (parser);
4134 }
4135 else
4136 {
4137 if (des_seen == 1)
4138 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
4139 "obsolete use of designated initializer without %<=%>");
4140 else
4141 {
4142 struct c_expr init;
4143 init.value = error_mark_node;
4144 init.original_code = ERROR_MARK;
4145 init.original_type = NULL;
4146 c_parser_error (parser, "expected %<=%>");
4147 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
4148 process_init_element (init, false, braced_init_obstack);
4149 return;
4150 }
4151 }
4152 }
4153 }
4154 c_parser_initval (parser, NULL, braced_init_obstack);
4155 }
4156
4157 /* Parse a nested initializer; as c_parser_initializer but parses
4158 initializers within braced lists, after any designators have been
4159 applied. If AFTER is not NULL then it is an Objective-C message
4160 expression which is the primary-expression starting the
4161 initializer. */
4162
4163 static void
4164 c_parser_initval (c_parser *parser, struct c_expr *after,
4165 struct obstack * braced_init_obstack)
4166 {
4167 struct c_expr init;
4168 gcc_assert (!after || c_dialect_objc ());
4169 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE) && !after)
4170 init = c_parser_braced_init (parser, NULL_TREE, true);
4171 else
4172 {
4173 location_t loc = c_parser_peek_token (parser)->location;
4174 init = c_parser_expr_no_commas (parser, after);
4175 if (init.value != NULL_TREE
4176 && TREE_CODE (init.value) != STRING_CST
4177 && TREE_CODE (init.value) != COMPOUND_LITERAL_EXPR)
4178 init = convert_lvalue_to_rvalue (loc, init, true, true);
4179 }
4180 process_init_element (init, false, braced_init_obstack);
4181 }
4182
4183 /* Parse a compound statement (possibly a function body) (C90 6.6.2,
4184 C99 6.8.2).
4185
4186 compound-statement:
4187 { block-item-list[opt] }
4188 { label-declarations block-item-list }
4189
4190 block-item-list:
4191 block-item
4192 block-item-list block-item
4193
4194 block-item:
4195 nested-declaration
4196 statement
4197
4198 nested-declaration:
4199 declaration
4200
4201 GNU extensions:
4202
4203 compound-statement:
4204 { label-declarations block-item-list }
4205
4206 nested-declaration:
4207 __extension__ nested-declaration
4208 nested-function-definition
4209
4210 label-declarations:
4211 label-declaration
4212 label-declarations label-declaration
4213
4214 label-declaration:
4215 __label__ identifier-list ;
4216
4217 Allowing the mixing of declarations and code is new in C99. The
4218 GNU syntax also permits (not shown above) labels at the end of
4219 compound statements, which yield an error. We don't allow labels
4220 on declarations; this might seem like a natural extension, but
4221 there would be a conflict between attributes on the label and
4222 prefix attributes on the declaration. ??? The syntax follows the
4223 old parser in requiring something after label declarations.
4224 Although they are erroneous if the labels declared aren't defined,
4225 is it useful for the syntax to be this way?
4226
4227 OpenMP:
4228
4229 block-item:
4230 openmp-directive
4231
4232 openmp-directive:
4233 barrier-directive
4234 flush-directive
4235 taskwait-directive
4236 taskyield-directive
4237 cancel-directive
4238 cancellation-point-directive */
4239
4240 static tree
4241 c_parser_compound_statement (c_parser *parser)
4242 {
4243 tree stmt;
4244 location_t brace_loc;
4245 brace_loc = c_parser_peek_token (parser)->location;
4246 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
4247 {
4248 /* Ensure a scope is entered and left anyway to avoid confusion
4249 if we have just prepared to enter a function body. */
4250 stmt = c_begin_compound_stmt (true);
4251 c_end_compound_stmt (brace_loc, stmt, true);
4252 return error_mark_node;
4253 }
4254 stmt = c_begin_compound_stmt (true);
4255 c_parser_compound_statement_nostart (parser);
4256
4257 /* If the compound stmt contains array notations, then we expand them. */
4258 if (flag_enable_cilkplus && contains_array_notation_expr (stmt))
4259 stmt = expand_array_notation_exprs (stmt);
4260 return c_end_compound_stmt (brace_loc, stmt, true);
4261 }
4262
4263 /* Parse a compound statement except for the opening brace. This is
4264 used for parsing both compound statements and statement expressions
4265 (which follow different paths to handling the opening). */
4266
4267 static void
4268 c_parser_compound_statement_nostart (c_parser *parser)
4269 {
4270 bool last_stmt = false;
4271 bool last_label = false;
4272 bool save_valid_for_pragma = valid_location_for_stdc_pragma_p ();
4273 location_t label_loc = UNKNOWN_LOCATION; /* Quiet warning. */
4274 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
4275 {
4276 c_parser_consume_token (parser);
4277 return;
4278 }
4279 mark_valid_location_for_stdc_pragma (true);
4280 if (c_parser_next_token_is_keyword (parser, RID_LABEL))
4281 {
4282 /* Read zero or more forward-declarations for labels that nested
4283 functions can jump to. */
4284 mark_valid_location_for_stdc_pragma (false);
4285 while (c_parser_next_token_is_keyword (parser, RID_LABEL))
4286 {
4287 label_loc = c_parser_peek_token (parser)->location;
4288 c_parser_consume_token (parser);
4289 /* Any identifiers, including those declared as type names,
4290 are OK here. */
4291 while (true)
4292 {
4293 tree label;
4294 if (c_parser_next_token_is_not (parser, CPP_NAME))
4295 {
4296 c_parser_error (parser, "expected identifier");
4297 break;
4298 }
4299 label
4300 = declare_label (c_parser_peek_token (parser)->value);
4301 C_DECLARED_LABEL_FLAG (label) = 1;
4302 add_stmt (build_stmt (label_loc, DECL_EXPR, label));
4303 c_parser_consume_token (parser);
4304 if (c_parser_next_token_is (parser, CPP_COMMA))
4305 c_parser_consume_token (parser);
4306 else
4307 break;
4308 }
4309 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
4310 }
4311 pedwarn (label_loc, OPT_Wpedantic, "ISO C forbids label declarations");
4312 }
4313 /* We must now have at least one statement, label or declaration. */
4314 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
4315 {
4316 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
4317 c_parser_error (parser, "expected declaration or statement");
4318 c_parser_consume_token (parser);
4319 return;
4320 }
4321 while (c_parser_next_token_is_not (parser, CPP_CLOSE_BRACE))
4322 {
4323 location_t loc = c_parser_peek_token (parser)->location;
4324 if (c_parser_next_token_is_keyword (parser, RID_CASE)
4325 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
4326 || (c_parser_next_token_is (parser, CPP_NAME)
4327 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
4328 {
4329 if (c_parser_next_token_is_keyword (parser, RID_CASE))
4330 label_loc = c_parser_peek_2nd_token (parser)->location;
4331 else
4332 label_loc = c_parser_peek_token (parser)->location;
4333 last_label = true;
4334 last_stmt = false;
4335 mark_valid_location_for_stdc_pragma (false);
4336 c_parser_label (parser);
4337 }
4338 else if (!last_label
4339 && c_parser_next_tokens_start_declaration (parser))
4340 {
4341 last_label = false;
4342 mark_valid_location_for_stdc_pragma (false);
4343 c_parser_declaration_or_fndef (parser, true, true, true, true,
4344 true, NULL, vNULL);
4345 if (last_stmt)
4346 pedwarn_c90 (loc,
4347 (pedantic && !flag_isoc99)
4348 ? OPT_Wpedantic
4349 : OPT_Wdeclaration_after_statement,
4350 "ISO C90 forbids mixed declarations and code");
4351 last_stmt = false;
4352 }
4353 else if (!last_label
4354 && c_parser_next_token_is_keyword (parser, RID_EXTENSION))
4355 {
4356 /* __extension__ can start a declaration, but is also an
4357 unary operator that can start an expression. Consume all
4358 but the last of a possible series of __extension__ to
4359 determine which. */
4360 while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
4361 && (c_parser_peek_2nd_token (parser)->keyword
4362 == RID_EXTENSION))
4363 c_parser_consume_token (parser);
4364 if (c_token_starts_declaration (c_parser_peek_2nd_token (parser)))
4365 {
4366 int ext;
4367 ext = disable_extension_diagnostics ();
4368 c_parser_consume_token (parser);
4369 last_label = false;
4370 mark_valid_location_for_stdc_pragma (false);
4371 c_parser_declaration_or_fndef (parser, true, true, true, true,
4372 true, NULL, vNULL);
4373 /* Following the old parser, __extension__ does not
4374 disable this diagnostic. */
4375 restore_extension_diagnostics (ext);
4376 if (last_stmt)
4377 pedwarn_c90 (loc, (pedantic && !flag_isoc99)
4378 ? OPT_Wpedantic
4379 : OPT_Wdeclaration_after_statement,
4380 "ISO C90 forbids mixed declarations and code");
4381 last_stmt = false;
4382 }
4383 else
4384 goto statement;
4385 }
4386 else if (c_parser_next_token_is (parser, CPP_PRAGMA))
4387 {
4388 /* External pragmas, and some omp pragmas, are not associated
4389 with regular c code, and so are not to be considered statements
4390 syntactically. This ensures that the user doesn't put them
4391 places that would turn into syntax errors if the directive
4392 were ignored. */
4393 if (c_parser_pragma (parser, pragma_compound))
4394 last_label = false, last_stmt = true;
4395 }
4396 else if (c_parser_next_token_is (parser, CPP_EOF))
4397 {
4398 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
4399 c_parser_error (parser, "expected declaration or statement");
4400 return;
4401 }
4402 else if (c_parser_next_token_is_keyword (parser, RID_ELSE))
4403 {
4404 if (parser->in_if_block)
4405 {
4406 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
4407 error_at (loc, """expected %<}%> before %<else%>");
4408 return;
4409 }
4410 else
4411 {
4412 error_at (loc, "%<else%> without a previous %<if%>");
4413 c_parser_consume_token (parser);
4414 continue;
4415 }
4416 }
4417 else
4418 {
4419 statement:
4420 last_label = false;
4421 last_stmt = true;
4422 mark_valid_location_for_stdc_pragma (false);
4423 c_parser_statement_after_labels (parser);
4424 }
4425
4426 parser->error = false;
4427 }
4428 if (last_label)
4429 error_at (label_loc, "label at end of compound statement");
4430 c_parser_consume_token (parser);
4431 /* Restore the value we started with. */
4432 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
4433 }
4434
4435 /* Parse a label (C90 6.6.1, C99 6.8.1).
4436
4437 label:
4438 identifier : attributes[opt]
4439 case constant-expression :
4440 default :
4441
4442 GNU extensions:
4443
4444 label:
4445 case constant-expression ... constant-expression :
4446
4447 The use of attributes on labels is a GNU extension. The syntax in
4448 GNU C accepts any expressions without commas, non-constant
4449 expressions being rejected later. */
4450
4451 static void
4452 c_parser_label (c_parser *parser)
4453 {
4454 location_t loc1 = c_parser_peek_token (parser)->location;
4455 tree label = NULL_TREE;
4456 if (c_parser_next_token_is_keyword (parser, RID_CASE))
4457 {
4458 tree exp1, exp2;
4459 c_parser_consume_token (parser);
4460 exp1 = c_parser_expr_no_commas (parser, NULL).value;
4461 if (c_parser_next_token_is (parser, CPP_COLON))
4462 {
4463 c_parser_consume_token (parser);
4464 label = do_case (loc1, exp1, NULL_TREE);
4465 }
4466 else if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
4467 {
4468 c_parser_consume_token (parser);
4469 exp2 = c_parser_expr_no_commas (parser, NULL).value;
4470 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
4471 label = do_case (loc1, exp1, exp2);
4472 }
4473 else
4474 c_parser_error (parser, "expected %<:%> or %<...%>");
4475 }
4476 else if (c_parser_next_token_is_keyword (parser, RID_DEFAULT))
4477 {
4478 c_parser_consume_token (parser);
4479 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
4480 label = do_case (loc1, NULL_TREE, NULL_TREE);
4481 }
4482 else
4483 {
4484 tree name = c_parser_peek_token (parser)->value;
4485 tree tlab;
4486 tree attrs;
4487 location_t loc2 = c_parser_peek_token (parser)->location;
4488 gcc_assert (c_parser_next_token_is (parser, CPP_NAME));
4489 c_parser_consume_token (parser);
4490 gcc_assert (c_parser_next_token_is (parser, CPP_COLON));
4491 c_parser_consume_token (parser);
4492 attrs = c_parser_attributes (parser);
4493 tlab = define_label (loc2, name);
4494 if (tlab)
4495 {
4496 decl_attributes (&tlab, attrs, 0);
4497 label = add_stmt (build_stmt (loc1, LABEL_EXPR, tlab));
4498 }
4499 }
4500 if (label)
4501 {
4502 if (c_parser_next_tokens_start_declaration (parser))
4503 {
4504 error_at (c_parser_peek_token (parser)->location,
4505 "a label can only be part of a statement and "
4506 "a declaration is not a statement");
4507 c_parser_declaration_or_fndef (parser, /*fndef_ok*/ false,
4508 /*static_assert_ok*/ true,
4509 /*empty_ok*/ true, /*nested*/ true,
4510 /*start_attr_ok*/ true, NULL,
4511 vNULL);
4512 }
4513 }
4514 }
4515
4516 /* Parse a statement (C90 6.6, C99 6.8).
4517
4518 statement:
4519 labeled-statement
4520 compound-statement
4521 expression-statement
4522 selection-statement
4523 iteration-statement
4524 jump-statement
4525
4526 labeled-statement:
4527 label statement
4528
4529 expression-statement:
4530 expression[opt] ;
4531
4532 selection-statement:
4533 if-statement
4534 switch-statement
4535
4536 iteration-statement:
4537 while-statement
4538 do-statement
4539 for-statement
4540
4541 jump-statement:
4542 goto identifier ;
4543 continue ;
4544 break ;
4545 return expression[opt] ;
4546
4547 GNU extensions:
4548
4549 statement:
4550 asm-statement
4551
4552 jump-statement:
4553 goto * expression ;
4554
4555 Objective-C:
4556
4557 statement:
4558 objc-throw-statement
4559 objc-try-catch-statement
4560 objc-synchronized-statement
4561
4562 objc-throw-statement:
4563 @throw expression ;
4564 @throw ;
4565
4566 OpenMP:
4567
4568 statement:
4569 openmp-construct
4570
4571 openmp-construct:
4572 parallel-construct
4573 for-construct
4574 simd-construct
4575 for-simd-construct
4576 sections-construct
4577 single-construct
4578 parallel-for-construct
4579 parallel-for-simd-construct
4580 parallel-sections-construct
4581 master-construct
4582 critical-construct
4583 atomic-construct
4584 ordered-construct
4585
4586 parallel-construct:
4587 parallel-directive structured-block
4588
4589 for-construct:
4590 for-directive iteration-statement
4591
4592 simd-construct:
4593 simd-directive iteration-statements
4594
4595 for-simd-construct:
4596 for-simd-directive iteration-statements
4597
4598 sections-construct:
4599 sections-directive section-scope
4600
4601 single-construct:
4602 single-directive structured-block
4603
4604 parallel-for-construct:
4605 parallel-for-directive iteration-statement
4606
4607 parallel-for-simd-construct:
4608 parallel-for-simd-directive iteration-statement
4609
4610 parallel-sections-construct:
4611 parallel-sections-directive section-scope
4612
4613 master-construct:
4614 master-directive structured-block
4615
4616 critical-construct:
4617 critical-directive structured-block
4618
4619 atomic-construct:
4620 atomic-directive expression-statement
4621
4622 ordered-construct:
4623 ordered-directive structured-block
4624
4625 Transactional Memory:
4626
4627 statement:
4628 transaction-statement
4629 transaction-cancel-statement
4630 */
4631
4632 static void
4633 c_parser_statement (c_parser *parser)
4634 {
4635 while (c_parser_next_token_is_keyword (parser, RID_CASE)
4636 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
4637 || (c_parser_next_token_is (parser, CPP_NAME)
4638 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
4639 c_parser_label (parser);
4640 c_parser_statement_after_labels (parser);
4641 }
4642
4643 /* Parse a statement, other than a labeled statement. */
4644
4645 static void
4646 c_parser_statement_after_labels (c_parser *parser)
4647 {
4648 location_t loc = c_parser_peek_token (parser)->location;
4649 tree stmt = NULL_TREE;
4650 bool in_if_block = parser->in_if_block;
4651 parser->in_if_block = false;
4652 switch (c_parser_peek_token (parser)->type)
4653 {
4654 case CPP_OPEN_BRACE:
4655 add_stmt (c_parser_compound_statement (parser));
4656 break;
4657 case CPP_KEYWORD:
4658 switch (c_parser_peek_token (parser)->keyword)
4659 {
4660 case RID_IF:
4661 c_parser_if_statement (parser);
4662 break;
4663 case RID_SWITCH:
4664 c_parser_switch_statement (parser);
4665 break;
4666 case RID_WHILE:
4667 c_parser_while_statement (parser, false);
4668 break;
4669 case RID_DO:
4670 c_parser_do_statement (parser, false);
4671 break;
4672 case RID_FOR:
4673 c_parser_for_statement (parser, false);
4674 break;
4675 case RID_CILK_SYNC:
4676 c_parser_consume_token (parser);
4677 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
4678 if (!flag_enable_cilkplus)
4679 error_at (loc, "-fcilkplus must be enabled to use %<_Cilk_sync%>");
4680 else
4681 add_stmt (build_cilk_sync ());
4682 break;
4683 case RID_GOTO:
4684 c_parser_consume_token (parser);
4685 if (c_parser_next_token_is (parser, CPP_NAME))
4686 {
4687 stmt = c_finish_goto_label (loc,
4688 c_parser_peek_token (parser)->value);
4689 c_parser_consume_token (parser);
4690 }
4691 else if (c_parser_next_token_is (parser, CPP_MULT))
4692 {
4693 struct c_expr val;
4694
4695 c_parser_consume_token (parser);
4696 val = c_parser_expression (parser);
4697 val = convert_lvalue_to_rvalue (loc, val, false, true);
4698 stmt = c_finish_goto_ptr (loc, val.value);
4699 }
4700 else
4701 c_parser_error (parser, "expected identifier or %<*%>");
4702 goto expect_semicolon;
4703 case RID_CONTINUE:
4704 c_parser_consume_token (parser);
4705 stmt = c_finish_bc_stmt (loc, &c_cont_label, false);
4706 goto expect_semicolon;
4707 case RID_BREAK:
4708 c_parser_consume_token (parser);
4709 stmt = c_finish_bc_stmt (loc, &c_break_label, true);
4710 goto expect_semicolon;
4711 case RID_RETURN:
4712 c_parser_consume_token (parser);
4713 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4714 {
4715 stmt = c_finish_return (loc, NULL_TREE, NULL_TREE);
4716 c_parser_consume_token (parser);
4717 }
4718 else
4719 {
4720 struct c_expr expr = c_parser_expression_conv (parser);
4721 mark_exp_read (expr.value);
4722 stmt = c_finish_return (loc, expr.value, expr.original_type);
4723 goto expect_semicolon;
4724 }
4725 break;
4726 case RID_ASM:
4727 stmt = c_parser_asm_statement (parser);
4728 break;
4729 case RID_TRANSACTION_ATOMIC:
4730 case RID_TRANSACTION_RELAXED:
4731 stmt = c_parser_transaction (parser,
4732 c_parser_peek_token (parser)->keyword);
4733 break;
4734 case RID_TRANSACTION_CANCEL:
4735 stmt = c_parser_transaction_cancel (parser);
4736 goto expect_semicolon;
4737 case RID_AT_THROW:
4738 gcc_assert (c_dialect_objc ());
4739 c_parser_consume_token (parser);
4740 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4741 {
4742 stmt = objc_build_throw_stmt (loc, NULL_TREE);
4743 c_parser_consume_token (parser);
4744 }
4745 else
4746 {
4747 struct c_expr expr = c_parser_expression (parser);
4748 expr = convert_lvalue_to_rvalue (loc, expr, false, false);
4749 expr.value = c_fully_fold (expr.value, false, NULL);
4750 stmt = objc_build_throw_stmt (loc, expr.value);
4751 goto expect_semicolon;
4752 }
4753 break;
4754 case RID_AT_TRY:
4755 gcc_assert (c_dialect_objc ());
4756 c_parser_objc_try_catch_finally_statement (parser);
4757 break;
4758 case RID_AT_SYNCHRONIZED:
4759 gcc_assert (c_dialect_objc ());
4760 c_parser_objc_synchronized_statement (parser);
4761 break;
4762 default:
4763 goto expr_stmt;
4764 }
4765 break;
4766 case CPP_SEMICOLON:
4767 c_parser_consume_token (parser);
4768 break;
4769 case CPP_CLOSE_PAREN:
4770 case CPP_CLOSE_SQUARE:
4771 /* Avoid infinite loop in error recovery:
4772 c_parser_skip_until_found stops at a closing nesting
4773 delimiter without consuming it, but here we need to consume
4774 it to proceed further. */
4775 c_parser_error (parser, "expected statement");
4776 c_parser_consume_token (parser);
4777 break;
4778 case CPP_PRAGMA:
4779 c_parser_pragma (parser, pragma_stmt);
4780 break;
4781 default:
4782 expr_stmt:
4783 stmt = c_finish_expr_stmt (loc, c_parser_expression_conv (parser).value);
4784 expect_semicolon:
4785 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
4786 break;
4787 }
4788 /* Two cases cannot and do not have line numbers associated: If stmt
4789 is degenerate, such as "2;", then stmt is an INTEGER_CST, which
4790 cannot hold line numbers. But that's OK because the statement
4791 will either be changed to a MODIFY_EXPR during gimplification of
4792 the statement expr, or discarded. If stmt was compound, but
4793 without new variables, we will have skipped the creation of a
4794 BIND and will have a bare STATEMENT_LIST. But that's OK because
4795 (recursively) all of the component statements should already have
4796 line numbers assigned. ??? Can we discard no-op statements
4797 earlier? */
4798 if (CAN_HAVE_LOCATION_P (stmt)
4799 && EXPR_LOCATION (stmt) == UNKNOWN_LOCATION)
4800 SET_EXPR_LOCATION (stmt, loc);
4801
4802 parser->in_if_block = in_if_block;
4803 }
4804
4805 /* Parse the condition from an if, do, while or for statements. */
4806
4807 static tree
4808 c_parser_condition (c_parser *parser)
4809 {
4810 location_t loc = c_parser_peek_token (parser)->location;
4811 tree cond;
4812 cond = c_parser_expression_conv (parser).value;
4813 cond = c_objc_common_truthvalue_conversion (loc, cond);
4814 cond = c_fully_fold (cond, false, NULL);
4815 if (warn_sequence_point)
4816 verify_sequence_points (cond);
4817 return cond;
4818 }
4819
4820 /* Parse a parenthesized condition from an if, do or while statement.
4821
4822 condition:
4823 ( expression )
4824 */
4825 static tree
4826 c_parser_paren_condition (c_parser *parser)
4827 {
4828 tree cond;
4829 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4830 return error_mark_node;
4831 cond = c_parser_condition (parser);
4832 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
4833 return cond;
4834 }
4835
4836 /* Parse a statement which is a block in C99. */
4837
4838 static tree
4839 c_parser_c99_block_statement (c_parser *parser)
4840 {
4841 tree block = c_begin_compound_stmt (flag_isoc99);
4842 location_t loc = c_parser_peek_token (parser)->location;
4843 c_parser_statement (parser);
4844 return c_end_compound_stmt (loc, block, flag_isoc99);
4845 }
4846
4847 /* Parse the body of an if statement. This is just parsing a
4848 statement but (a) it is a block in C99, (b) we track whether the
4849 body is an if statement for the sake of -Wparentheses warnings, (c)
4850 we handle an empty body specially for the sake of -Wempty-body
4851 warnings, and (d) we call parser_compound_statement directly
4852 because c_parser_statement_after_labels resets
4853 parser->in_if_block. */
4854
4855 static tree
4856 c_parser_if_body (c_parser *parser, bool *if_p)
4857 {
4858 tree block = c_begin_compound_stmt (flag_isoc99);
4859 location_t body_loc = c_parser_peek_token (parser)->location;
4860 while (c_parser_next_token_is_keyword (parser, RID_CASE)
4861 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
4862 || (c_parser_next_token_is (parser, CPP_NAME)
4863 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
4864 c_parser_label (parser);
4865 *if_p = c_parser_next_token_is_keyword (parser, RID_IF);
4866 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4867 {
4868 location_t loc = c_parser_peek_token (parser)->location;
4869 add_stmt (build_empty_stmt (loc));
4870 c_parser_consume_token (parser);
4871 if (!c_parser_next_token_is_keyword (parser, RID_ELSE))
4872 warning_at (loc, OPT_Wempty_body,
4873 "suggest braces around empty body in an %<if%> statement");
4874 }
4875 else if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
4876 add_stmt (c_parser_compound_statement (parser));
4877 else
4878 c_parser_statement_after_labels (parser);
4879 return c_end_compound_stmt (body_loc, block, flag_isoc99);
4880 }
4881
4882 /* Parse the else body of an if statement. This is just parsing a
4883 statement but (a) it is a block in C99, (b) we handle an empty body
4884 specially for the sake of -Wempty-body warnings. */
4885
4886 static tree
4887 c_parser_else_body (c_parser *parser)
4888 {
4889 location_t else_loc = c_parser_peek_token (parser)->location;
4890 tree block = c_begin_compound_stmt (flag_isoc99);
4891 while (c_parser_next_token_is_keyword (parser, RID_CASE)
4892 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
4893 || (c_parser_next_token_is (parser, CPP_NAME)
4894 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
4895 c_parser_label (parser);
4896 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4897 {
4898 location_t loc = c_parser_peek_token (parser)->location;
4899 warning_at (loc,
4900 OPT_Wempty_body,
4901 "suggest braces around empty body in an %<else%> statement");
4902 add_stmt (build_empty_stmt (loc));
4903 c_parser_consume_token (parser);
4904 }
4905 else
4906 c_parser_statement_after_labels (parser);
4907 return c_end_compound_stmt (else_loc, block, flag_isoc99);
4908 }
4909
4910 /* Parse an if statement (C90 6.6.4, C99 6.8.4).
4911
4912 if-statement:
4913 if ( expression ) statement
4914 if ( expression ) statement else statement
4915 */
4916
4917 static void
4918 c_parser_if_statement (c_parser *parser)
4919 {
4920 tree block;
4921 location_t loc;
4922 tree cond;
4923 bool first_if = false;
4924 tree first_body, second_body;
4925 bool in_if_block;
4926 tree if_stmt;
4927
4928 gcc_assert (c_parser_next_token_is_keyword (parser, RID_IF));
4929 c_parser_consume_token (parser);
4930 block = c_begin_compound_stmt (flag_isoc99);
4931 loc = c_parser_peek_token (parser)->location;
4932 cond = c_parser_paren_condition (parser);
4933 in_if_block = parser->in_if_block;
4934 parser->in_if_block = true;
4935 first_body = c_parser_if_body (parser, &first_if);
4936 parser->in_if_block = in_if_block;
4937 if (c_parser_next_token_is_keyword (parser, RID_ELSE))
4938 {
4939 c_parser_consume_token (parser);
4940 second_body = c_parser_else_body (parser);
4941 }
4942 else
4943 second_body = NULL_TREE;
4944 c_finish_if_stmt (loc, cond, first_body, second_body, first_if);
4945 if_stmt = c_end_compound_stmt (loc, block, flag_isoc99);
4946
4947 /* If the if statement contains array notations, then we expand them. */
4948 if (flag_enable_cilkplus && contains_array_notation_expr (if_stmt))
4949 if_stmt = fix_conditional_array_notations (if_stmt);
4950 add_stmt (if_stmt);
4951 }
4952
4953 /* Parse a switch statement (C90 6.6.4, C99 6.8.4).
4954
4955 switch-statement:
4956 switch (expression) statement
4957 */
4958
4959 static void
4960 c_parser_switch_statement (c_parser *parser)
4961 {
4962 struct c_expr ce;
4963 tree block, expr, body, save_break;
4964 location_t switch_loc = c_parser_peek_token (parser)->location;
4965 location_t switch_cond_loc;
4966 gcc_assert (c_parser_next_token_is_keyword (parser, RID_SWITCH));
4967 c_parser_consume_token (parser);
4968 block = c_begin_compound_stmt (flag_isoc99);
4969 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4970 {
4971 switch_cond_loc = c_parser_peek_token (parser)->location;
4972 ce = c_parser_expression (parser);
4973 ce = convert_lvalue_to_rvalue (switch_cond_loc, ce, true, false);
4974 expr = ce.value;
4975 if (flag_enable_cilkplus && contains_array_notation_expr (expr))
4976 {
4977 error_at (switch_cond_loc,
4978 "array notations cannot be used as a condition for switch "
4979 "statement");
4980 expr = error_mark_node;
4981 }
4982 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
4983 }
4984 else
4985 {
4986 switch_cond_loc = UNKNOWN_LOCATION;
4987 expr = error_mark_node;
4988 }
4989 c_start_case (switch_loc, switch_cond_loc, expr);
4990 save_break = c_break_label;
4991 c_break_label = NULL_TREE;
4992 body = c_parser_c99_block_statement (parser);
4993 c_finish_case (body);
4994 if (c_break_label)
4995 {
4996 location_t here = c_parser_peek_token (parser)->location;
4997 tree t = build1 (LABEL_EXPR, void_type_node, c_break_label);
4998 SET_EXPR_LOCATION (t, here);
4999 add_stmt (t);
5000 }
5001 c_break_label = save_break;
5002 add_stmt (c_end_compound_stmt (switch_loc, block, flag_isoc99));
5003 }
5004
5005 /* Parse a while statement (C90 6.6.5, C99 6.8.5).
5006
5007 while-statement:
5008 while (expression) statement
5009 */
5010
5011 static void
5012 c_parser_while_statement (c_parser *parser, bool ivdep)
5013 {
5014 tree block, cond, body, save_break, save_cont;
5015 location_t loc;
5016 gcc_assert (c_parser_next_token_is_keyword (parser, RID_WHILE));
5017 c_parser_consume_token (parser);
5018 block = c_begin_compound_stmt (flag_isoc99);
5019 loc = c_parser_peek_token (parser)->location;
5020 cond = c_parser_paren_condition (parser);
5021 if (flag_enable_cilkplus && contains_array_notation_expr (cond))
5022 {
5023 error_at (loc, "array notations cannot be used as a condition for while "
5024 "statement");
5025 cond = error_mark_node;
5026 }
5027
5028 if (ivdep && cond != error_mark_node)
5029 cond = build2 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
5030 build_int_cst (integer_type_node,
5031 annot_expr_ivdep_kind));
5032 save_break = c_break_label;
5033 c_break_label = NULL_TREE;
5034 save_cont = c_cont_label;
5035 c_cont_label = NULL_TREE;
5036 body = c_parser_c99_block_statement (parser);
5037 c_finish_loop (loc, cond, NULL, body, c_break_label, c_cont_label, true);
5038 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
5039 c_break_label = save_break;
5040 c_cont_label = save_cont;
5041 }
5042
5043 /* Parse a do statement (C90 6.6.5, C99 6.8.5).
5044
5045 do-statement:
5046 do statement while ( expression ) ;
5047 */
5048
5049 static void
5050 c_parser_do_statement (c_parser *parser, bool ivdep)
5051 {
5052 tree block, cond, body, save_break, save_cont, new_break, new_cont;
5053 location_t loc;
5054 gcc_assert (c_parser_next_token_is_keyword (parser, RID_DO));
5055 c_parser_consume_token (parser);
5056 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5057 warning_at (c_parser_peek_token (parser)->location,
5058 OPT_Wempty_body,
5059 "suggest braces around empty body in %<do%> statement");
5060 block = c_begin_compound_stmt (flag_isoc99);
5061 loc = c_parser_peek_token (parser)->location;
5062 save_break = c_break_label;
5063 c_break_label = NULL_TREE;
5064 save_cont = c_cont_label;
5065 c_cont_label = NULL_TREE;
5066 body = c_parser_c99_block_statement (parser);
5067 c_parser_require_keyword (parser, RID_WHILE, "expected %<while%>");
5068 new_break = c_break_label;
5069 c_break_label = save_break;
5070 new_cont = c_cont_label;
5071 c_cont_label = save_cont;
5072 cond = c_parser_paren_condition (parser);
5073 if (flag_enable_cilkplus && contains_array_notation_expr (cond))
5074 {
5075 error_at (loc, "array notations cannot be used as a condition for a "
5076 "do-while statement");
5077 cond = error_mark_node;
5078 }
5079 if (ivdep && cond != error_mark_node)
5080 cond = build2 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
5081 build_int_cst (integer_type_node,
5082 annot_expr_ivdep_kind));
5083 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
5084 c_parser_skip_to_end_of_block_or_statement (parser);
5085 c_finish_loop (loc, cond, NULL, body, new_break, new_cont, false);
5086 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
5087 }
5088
5089 /* Parse a for statement (C90 6.6.5, C99 6.8.5).
5090
5091 for-statement:
5092 for ( expression[opt] ; expression[opt] ; expression[opt] ) statement
5093 for ( nested-declaration expression[opt] ; expression[opt] ) statement
5094
5095 The form with a declaration is new in C99.
5096
5097 ??? In accordance with the old parser, the declaration may be a
5098 nested function, which is then rejected in check_for_loop_decls,
5099 but does it make any sense for this to be included in the grammar?
5100 Note in particular that the nested function does not include a
5101 trailing ';', whereas the "declaration" production includes one.
5102 Also, can we reject bad declarations earlier and cheaper than
5103 check_for_loop_decls?
5104
5105 In Objective-C, there are two additional variants:
5106
5107 foreach-statement:
5108 for ( expression in expresssion ) statement
5109 for ( declaration in expression ) statement
5110
5111 This is inconsistent with C, because the second variant is allowed
5112 even if c99 is not enabled.
5113
5114 The rest of the comment documents these Objective-C foreach-statement.
5115
5116 Here is the canonical example of the first variant:
5117 for (object in array) { do something with object }
5118 we call the first expression ("object") the "object_expression" and
5119 the second expression ("array") the "collection_expression".
5120 object_expression must be an lvalue of type "id" (a generic Objective-C
5121 object) because the loop works by assigning to object_expression the
5122 various objects from the collection_expression. collection_expression
5123 must evaluate to something of type "id" which responds to the method
5124 countByEnumeratingWithState:objects:count:.
5125
5126 The canonical example of the second variant is:
5127 for (id object in array) { do something with object }
5128 which is completely equivalent to
5129 {
5130 id object;
5131 for (object in array) { do something with object }
5132 }
5133 Note that initizializing 'object' in some way (eg, "for ((object =
5134 xxx) in array) { do something with object }") is possibly
5135 technically valid, but completely pointless as 'object' will be
5136 assigned to something else as soon as the loop starts. We should
5137 most likely reject it (TODO).
5138
5139 The beginning of the Objective-C foreach-statement looks exactly
5140 like the beginning of the for-statement, and we can tell it is a
5141 foreach-statement only because the initial declaration or
5142 expression is terminated by 'in' instead of ';'.
5143 */
5144
5145 static void
5146 c_parser_for_statement (c_parser *parser, bool ivdep)
5147 {
5148 tree block, cond, incr, save_break, save_cont, body;
5149 /* The following are only used when parsing an ObjC foreach statement. */
5150 tree object_expression;
5151 /* Silence the bogus uninitialized warning. */
5152 tree collection_expression = NULL;
5153 location_t loc = c_parser_peek_token (parser)->location;
5154 location_t for_loc = c_parser_peek_token (parser)->location;
5155 bool is_foreach_statement = false;
5156 gcc_assert (c_parser_next_token_is_keyword (parser, RID_FOR));
5157 c_parser_consume_token (parser);
5158 /* Open a compound statement in Objective-C as well, just in case this is
5159 as foreach expression. */
5160 block = c_begin_compound_stmt (flag_isoc99 || c_dialect_objc ());
5161 cond = error_mark_node;
5162 incr = error_mark_node;
5163 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5164 {
5165 /* Parse the initialization declaration or expression. */
5166 object_expression = error_mark_node;
5167 parser->objc_could_be_foreach_context = c_dialect_objc ();
5168 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5169 {
5170 parser->objc_could_be_foreach_context = false;
5171 c_parser_consume_token (parser);
5172 c_finish_expr_stmt (loc, NULL_TREE);
5173 }
5174 else if (c_parser_next_tokens_start_declaration (parser))
5175 {
5176 c_parser_declaration_or_fndef (parser, true, true, true, true, true,
5177 &object_expression, vNULL);
5178 parser->objc_could_be_foreach_context = false;
5179
5180 if (c_parser_next_token_is_keyword (parser, RID_IN))
5181 {
5182 c_parser_consume_token (parser);
5183 is_foreach_statement = true;
5184 if (check_for_loop_decls (for_loc, true) == NULL_TREE)
5185 c_parser_error (parser, "multiple iterating variables in fast enumeration");
5186 }
5187 else
5188 check_for_loop_decls (for_loc, flag_isoc99);
5189 }
5190 else if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
5191 {
5192 /* __extension__ can start a declaration, but is also an
5193 unary operator that can start an expression. Consume all
5194 but the last of a possible series of __extension__ to
5195 determine which. */
5196 while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
5197 && (c_parser_peek_2nd_token (parser)->keyword
5198 == RID_EXTENSION))
5199 c_parser_consume_token (parser);
5200 if (c_token_starts_declaration (c_parser_peek_2nd_token (parser)))
5201 {
5202 int ext;
5203 ext = disable_extension_diagnostics ();
5204 c_parser_consume_token (parser);
5205 c_parser_declaration_or_fndef (parser, true, true, true, true,
5206 true, &object_expression, vNULL);
5207 parser->objc_could_be_foreach_context = false;
5208
5209 restore_extension_diagnostics (ext);
5210 if (c_parser_next_token_is_keyword (parser, RID_IN))
5211 {
5212 c_parser_consume_token (parser);
5213 is_foreach_statement = true;
5214 if (check_for_loop_decls (for_loc, true) == NULL_TREE)
5215 c_parser_error (parser, "multiple iterating variables in fast enumeration");
5216 }
5217 else
5218 check_for_loop_decls (for_loc, flag_isoc99);
5219 }
5220 else
5221 goto init_expr;
5222 }
5223 else
5224 {
5225 init_expr:
5226 {
5227 struct c_expr ce;
5228 tree init_expression;
5229 ce = c_parser_expression (parser);
5230 init_expression = ce.value;
5231 parser->objc_could_be_foreach_context = false;
5232 if (c_parser_next_token_is_keyword (parser, RID_IN))
5233 {
5234 c_parser_consume_token (parser);
5235 is_foreach_statement = true;
5236 if (! lvalue_p (init_expression))
5237 c_parser_error (parser, "invalid iterating variable in fast enumeration");
5238 object_expression = c_fully_fold (init_expression, false, NULL);
5239 }
5240 else
5241 {
5242 ce = convert_lvalue_to_rvalue (loc, ce, true, false);
5243 init_expression = ce.value;
5244 c_finish_expr_stmt (loc, init_expression);
5245 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
5246 }
5247 }
5248 }
5249 /* Parse the loop condition. In the case of a foreach
5250 statement, there is no loop condition. */
5251 gcc_assert (!parser->objc_could_be_foreach_context);
5252 if (!is_foreach_statement)
5253 {
5254 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5255 {
5256 if (ivdep)
5257 {
5258 c_parser_error (parser, "missing loop condition in loop with "
5259 "%<GCC ivdep%> pragma");
5260 cond = error_mark_node;
5261 }
5262 else
5263 {
5264 c_parser_consume_token (parser);
5265 cond = NULL_TREE;
5266 }
5267 }
5268 else
5269 {
5270 cond = c_parser_condition (parser);
5271 if (flag_enable_cilkplus && contains_array_notation_expr (cond))
5272 {
5273 error_at (loc, "array notations cannot be used in a "
5274 "condition for a for-loop");
5275 cond = error_mark_node;
5276 }
5277 c_parser_skip_until_found (parser, CPP_SEMICOLON,
5278 "expected %<;%>");
5279 }
5280 if (ivdep && cond != error_mark_node)
5281 cond = build2 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
5282 build_int_cst (integer_type_node,
5283 annot_expr_ivdep_kind));
5284 }
5285 /* Parse the increment expression (the third expression in a
5286 for-statement). In the case of a foreach-statement, this is
5287 the expression that follows the 'in'. */
5288 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
5289 {
5290 if (is_foreach_statement)
5291 {
5292 c_parser_error (parser, "missing collection in fast enumeration");
5293 collection_expression = error_mark_node;
5294 }
5295 else
5296 incr = c_process_expr_stmt (loc, NULL_TREE);
5297 }
5298 else
5299 {
5300 if (is_foreach_statement)
5301 collection_expression = c_fully_fold (c_parser_expression (parser).value,
5302 false, NULL);
5303 else
5304 {
5305 struct c_expr ce = c_parser_expression (parser);
5306 ce = convert_lvalue_to_rvalue (loc, ce, true, false);
5307 incr = c_process_expr_stmt (loc, ce.value);
5308 }
5309 }
5310 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
5311 }
5312 save_break = c_break_label;
5313 c_break_label = NULL_TREE;
5314 save_cont = c_cont_label;
5315 c_cont_label = NULL_TREE;
5316 body = c_parser_c99_block_statement (parser);
5317 if (is_foreach_statement)
5318 objc_finish_foreach_loop (loc, object_expression, collection_expression, body, c_break_label, c_cont_label);
5319 else
5320 c_finish_loop (loc, cond, incr, body, c_break_label, c_cont_label, true);
5321 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99 || c_dialect_objc ()));
5322 c_break_label = save_break;
5323 c_cont_label = save_cont;
5324 }
5325
5326 /* Parse an asm statement, a GNU extension. This is a full-blown asm
5327 statement with inputs, outputs, clobbers, and volatile tag
5328 allowed.
5329
5330 asm-statement:
5331 asm type-qualifier[opt] ( asm-argument ) ;
5332 asm type-qualifier[opt] goto ( asm-goto-argument ) ;
5333
5334 asm-argument:
5335 asm-string-literal
5336 asm-string-literal : asm-operands[opt]
5337 asm-string-literal : asm-operands[opt] : asm-operands[opt]
5338 asm-string-literal : asm-operands[opt] : asm-operands[opt] : asm-clobbers[opt]
5339
5340 asm-goto-argument:
5341 asm-string-literal : : asm-operands[opt] : asm-clobbers[opt] \
5342 : asm-goto-operands
5343
5344 Qualifiers other than volatile are accepted in the syntax but
5345 warned for. */
5346
5347 static tree
5348 c_parser_asm_statement (c_parser *parser)
5349 {
5350 tree quals, str, outputs, inputs, clobbers, labels, ret;
5351 bool simple, is_goto;
5352 location_t asm_loc = c_parser_peek_token (parser)->location;
5353 int section, nsections;
5354
5355 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM));
5356 c_parser_consume_token (parser);
5357 if (c_parser_next_token_is_keyword (parser, RID_VOLATILE))
5358 {
5359 quals = c_parser_peek_token (parser)->value;
5360 c_parser_consume_token (parser);
5361 }
5362 else if (c_parser_next_token_is_keyword (parser, RID_CONST)
5363 || c_parser_next_token_is_keyword (parser, RID_RESTRICT))
5364 {
5365 warning_at (c_parser_peek_token (parser)->location,
5366 0,
5367 "%E qualifier ignored on asm",
5368 c_parser_peek_token (parser)->value);
5369 quals = NULL_TREE;
5370 c_parser_consume_token (parser);
5371 }
5372 else
5373 quals = NULL_TREE;
5374
5375 is_goto = false;
5376 if (c_parser_next_token_is_keyword (parser, RID_GOTO))
5377 {
5378 c_parser_consume_token (parser);
5379 is_goto = true;
5380 }
5381
5382 /* ??? Follow the C++ parser rather than using the
5383 lex_untranslated_string kludge. */
5384 parser->lex_untranslated_string = true;
5385 ret = NULL;
5386
5387 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5388 goto error;
5389
5390 str = c_parser_asm_string_literal (parser);
5391 if (str == NULL_TREE)
5392 goto error_close_paren;
5393
5394 simple = true;
5395 outputs = NULL_TREE;
5396 inputs = NULL_TREE;
5397 clobbers = NULL_TREE;
5398 labels = NULL_TREE;
5399
5400 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN) && !is_goto)
5401 goto done_asm;
5402
5403 /* Parse each colon-delimited section of operands. */
5404 nsections = 3 + is_goto;
5405 for (section = 0; section < nsections; ++section)
5406 {
5407 if (!c_parser_require (parser, CPP_COLON,
5408 is_goto
5409 ? "expected %<:%>"
5410 : "expected %<:%> or %<)%>"))
5411 goto error_close_paren;
5412
5413 /* Once past any colon, we're no longer a simple asm. */
5414 simple = false;
5415
5416 if ((!c_parser_next_token_is (parser, CPP_COLON)
5417 && !c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
5418 || section == 3)
5419 switch (section)
5420 {
5421 case 0:
5422 /* For asm goto, we don't allow output operands, but reserve
5423 the slot for a future extension that does allow them. */
5424 if (!is_goto)
5425 outputs = c_parser_asm_operands (parser);
5426 break;
5427 case 1:
5428 inputs = c_parser_asm_operands (parser);
5429 break;
5430 case 2:
5431 clobbers = c_parser_asm_clobbers (parser);
5432 break;
5433 case 3:
5434 labels = c_parser_asm_goto_operands (parser);
5435 break;
5436 default:
5437 gcc_unreachable ();
5438 }
5439
5440 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN) && !is_goto)
5441 goto done_asm;
5442 }
5443
5444 done_asm:
5445 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
5446 {
5447 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5448 goto error;
5449 }
5450
5451 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
5452 c_parser_skip_to_end_of_block_or_statement (parser);
5453
5454 ret = build_asm_stmt (quals, build_asm_expr (asm_loc, str, outputs, inputs,
5455 clobbers, labels, simple));
5456
5457 error:
5458 parser->lex_untranslated_string = false;
5459 return ret;
5460
5461 error_close_paren:
5462 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5463 goto error;
5464 }
5465
5466 /* Parse asm operands, a GNU extension.
5467
5468 asm-operands:
5469 asm-operand
5470 asm-operands , asm-operand
5471
5472 asm-operand:
5473 asm-string-literal ( expression )
5474 [ identifier ] asm-string-literal ( expression )
5475 */
5476
5477 static tree
5478 c_parser_asm_operands (c_parser *parser)
5479 {
5480 tree list = NULL_TREE;
5481 while (true)
5482 {
5483 tree name, str;
5484 struct c_expr expr;
5485 if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
5486 {
5487 c_parser_consume_token (parser);
5488 if (c_parser_next_token_is (parser, CPP_NAME))
5489 {
5490 tree id = c_parser_peek_token (parser)->value;
5491 c_parser_consume_token (parser);
5492 name = build_string (IDENTIFIER_LENGTH (id),
5493 IDENTIFIER_POINTER (id));
5494 }
5495 else
5496 {
5497 c_parser_error (parser, "expected identifier");
5498 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
5499 return NULL_TREE;
5500 }
5501 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
5502 "expected %<]%>");
5503 }
5504 else
5505 name = NULL_TREE;
5506 str = c_parser_asm_string_literal (parser);
5507 if (str == NULL_TREE)
5508 return NULL_TREE;
5509 parser->lex_untranslated_string = false;
5510 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5511 {
5512 parser->lex_untranslated_string = true;
5513 return NULL_TREE;
5514 }
5515 expr = c_parser_expression (parser);
5516 mark_exp_read (expr.value);
5517 parser->lex_untranslated_string = true;
5518 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
5519 {
5520 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5521 return NULL_TREE;
5522 }
5523 list = chainon (list, build_tree_list (build_tree_list (name, str),
5524 expr.value));
5525 if (c_parser_next_token_is (parser, CPP_COMMA))
5526 c_parser_consume_token (parser);
5527 else
5528 break;
5529 }
5530 return list;
5531 }
5532
5533 /* Parse asm clobbers, a GNU extension.
5534
5535 asm-clobbers:
5536 asm-string-literal
5537 asm-clobbers , asm-string-literal
5538 */
5539
5540 static tree
5541 c_parser_asm_clobbers (c_parser *parser)
5542 {
5543 tree list = NULL_TREE;
5544 while (true)
5545 {
5546 tree str = c_parser_asm_string_literal (parser);
5547 if (str)
5548 list = tree_cons (NULL_TREE, str, list);
5549 else
5550 return NULL_TREE;
5551 if (c_parser_next_token_is (parser, CPP_COMMA))
5552 c_parser_consume_token (parser);
5553 else
5554 break;
5555 }
5556 return list;
5557 }
5558
5559 /* Parse asm goto labels, a GNU extension.
5560
5561 asm-goto-operands:
5562 identifier
5563 asm-goto-operands , identifier
5564 */
5565
5566 static tree
5567 c_parser_asm_goto_operands (c_parser *parser)
5568 {
5569 tree list = NULL_TREE;
5570 while (true)
5571 {
5572 tree name, label;
5573
5574 if (c_parser_next_token_is (parser, CPP_NAME))
5575 {
5576 c_token *tok = c_parser_peek_token (parser);
5577 name = tok->value;
5578 label = lookup_label_for_goto (tok->location, name);
5579 c_parser_consume_token (parser);
5580 TREE_USED (label) = 1;
5581 }
5582 else
5583 {
5584 c_parser_error (parser, "expected identifier");
5585 return NULL_TREE;
5586 }
5587
5588 name = build_string (IDENTIFIER_LENGTH (name),
5589 IDENTIFIER_POINTER (name));
5590 list = tree_cons (name, label, list);
5591 if (c_parser_next_token_is (parser, CPP_COMMA))
5592 c_parser_consume_token (parser);
5593 else
5594 return nreverse (list);
5595 }
5596 }
5597
5598 /* Parse an expression other than a compound expression; that is, an
5599 assignment expression (C90 6.3.16, C99 6.5.16). If AFTER is not
5600 NULL then it is an Objective-C message expression which is the
5601 primary-expression starting the expression as an initializer.
5602
5603 assignment-expression:
5604 conditional-expression
5605 unary-expression assignment-operator assignment-expression
5606
5607 assignment-operator: one of
5608 = *= /= %= += -= <<= >>= &= ^= |=
5609
5610 In GNU C we accept any conditional expression on the LHS and
5611 diagnose the invalid lvalue rather than producing a syntax
5612 error. */
5613
5614 static struct c_expr
5615 c_parser_expr_no_commas (c_parser *parser, struct c_expr *after,
5616 tree omp_atomic_lhs)
5617 {
5618 struct c_expr lhs, rhs, ret;
5619 enum tree_code code;
5620 location_t op_location, exp_location;
5621 gcc_assert (!after || c_dialect_objc ());
5622 lhs = c_parser_conditional_expression (parser, after, omp_atomic_lhs);
5623 op_location = c_parser_peek_token (parser)->location;
5624 switch (c_parser_peek_token (parser)->type)
5625 {
5626 case CPP_EQ:
5627 code = NOP_EXPR;
5628 break;
5629 case CPP_MULT_EQ:
5630 code = MULT_EXPR;
5631 break;
5632 case CPP_DIV_EQ:
5633 code = TRUNC_DIV_EXPR;
5634 break;
5635 case CPP_MOD_EQ:
5636 code = TRUNC_MOD_EXPR;
5637 break;
5638 case CPP_PLUS_EQ:
5639 code = PLUS_EXPR;
5640 break;
5641 case CPP_MINUS_EQ:
5642 code = MINUS_EXPR;
5643 break;
5644 case CPP_LSHIFT_EQ:
5645 code = LSHIFT_EXPR;
5646 break;
5647 case CPP_RSHIFT_EQ:
5648 code = RSHIFT_EXPR;
5649 break;
5650 case CPP_AND_EQ:
5651 code = BIT_AND_EXPR;
5652 break;
5653 case CPP_XOR_EQ:
5654 code = BIT_XOR_EXPR;
5655 break;
5656 case CPP_OR_EQ:
5657 code = BIT_IOR_EXPR;
5658 break;
5659 default:
5660 return lhs;
5661 }
5662 c_parser_consume_token (parser);
5663 exp_location = c_parser_peek_token (parser)->location;
5664 rhs = c_parser_expr_no_commas (parser, NULL);
5665 rhs = convert_lvalue_to_rvalue (exp_location, rhs, true, true);
5666
5667 ret.value = build_modify_expr (op_location, lhs.value, lhs.original_type,
5668 code, exp_location, rhs.value,
5669 rhs.original_type);
5670 if (code == NOP_EXPR)
5671 ret.original_code = MODIFY_EXPR;
5672 else
5673 {
5674 TREE_NO_WARNING (ret.value) = 1;
5675 ret.original_code = ERROR_MARK;
5676 }
5677 ret.original_type = NULL;
5678 return ret;
5679 }
5680
5681 /* Parse a conditional expression (C90 6.3.15, C99 6.5.15). If AFTER
5682 is not NULL then it is an Objective-C message expression which is
5683 the primary-expression starting the expression as an initializer.
5684
5685 conditional-expression:
5686 logical-OR-expression
5687 logical-OR-expression ? expression : conditional-expression
5688
5689 GNU extensions:
5690
5691 conditional-expression:
5692 logical-OR-expression ? : conditional-expression
5693 */
5694
5695 static struct c_expr
5696 c_parser_conditional_expression (c_parser *parser, struct c_expr *after,
5697 tree omp_atomic_lhs)
5698 {
5699 struct c_expr cond, exp1, exp2, ret;
5700 location_t cond_loc, colon_loc, middle_loc;
5701
5702 gcc_assert (!after || c_dialect_objc ());
5703
5704 cond = c_parser_binary_expression (parser, after, omp_atomic_lhs);
5705
5706 if (c_parser_next_token_is_not (parser, CPP_QUERY))
5707 return cond;
5708 cond_loc = c_parser_peek_token (parser)->location;
5709 cond = convert_lvalue_to_rvalue (cond_loc, cond, true, true);
5710 c_parser_consume_token (parser);
5711 if (c_parser_next_token_is (parser, CPP_COLON))
5712 {
5713 tree eptype = NULL_TREE;
5714
5715 middle_loc = c_parser_peek_token (parser)->location;
5716 pedwarn (middle_loc, OPT_Wpedantic,
5717 "ISO C forbids omitting the middle term of a ?: expression");
5718 warn_for_omitted_condop (middle_loc, cond.value);
5719 if (TREE_CODE (cond.value) == EXCESS_PRECISION_EXPR)
5720 {
5721 eptype = TREE_TYPE (cond.value);
5722 cond.value = TREE_OPERAND (cond.value, 0);
5723 }
5724 /* Make sure first operand is calculated only once. */
5725 exp1.value = c_save_expr (default_conversion (cond.value));
5726 if (eptype)
5727 exp1.value = build1 (EXCESS_PRECISION_EXPR, eptype, exp1.value);
5728 exp1.original_type = NULL;
5729 cond.value = c_objc_common_truthvalue_conversion (cond_loc, exp1.value);
5730 c_inhibit_evaluation_warnings += cond.value == truthvalue_true_node;
5731 }
5732 else
5733 {
5734 cond.value
5735 = c_objc_common_truthvalue_conversion
5736 (cond_loc, default_conversion (cond.value));
5737 c_inhibit_evaluation_warnings += cond.value == truthvalue_false_node;
5738 exp1 = c_parser_expression_conv (parser);
5739 mark_exp_read (exp1.value);
5740 c_inhibit_evaluation_warnings +=
5741 ((cond.value == truthvalue_true_node)
5742 - (cond.value == truthvalue_false_node));
5743 }
5744
5745 colon_loc = c_parser_peek_token (parser)->location;
5746 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
5747 {
5748 c_inhibit_evaluation_warnings -= cond.value == truthvalue_true_node;
5749 ret.value = error_mark_node;
5750 ret.original_code = ERROR_MARK;
5751 ret.original_type = NULL;
5752 return ret;
5753 }
5754 {
5755 location_t exp2_loc = c_parser_peek_token (parser)->location;
5756 exp2 = c_parser_conditional_expression (parser, NULL, NULL_TREE);
5757 exp2 = convert_lvalue_to_rvalue (exp2_loc, exp2, true, true);
5758 }
5759 c_inhibit_evaluation_warnings -= cond.value == truthvalue_true_node;
5760 ret.value = build_conditional_expr (colon_loc, cond.value,
5761 cond.original_code == C_MAYBE_CONST_EXPR,
5762 exp1.value, exp1.original_type,
5763 exp2.value, exp2.original_type);
5764 ret.original_code = ERROR_MARK;
5765 if (exp1.value == error_mark_node || exp2.value == error_mark_node)
5766 ret.original_type = NULL;
5767 else
5768 {
5769 tree t1, t2;
5770
5771 /* If both sides are enum type, the default conversion will have
5772 made the type of the result be an integer type. We want to
5773 remember the enum types we started with. */
5774 t1 = exp1.original_type ? exp1.original_type : TREE_TYPE (exp1.value);
5775 t2 = exp2.original_type ? exp2.original_type : TREE_TYPE (exp2.value);
5776 ret.original_type = ((t1 != error_mark_node
5777 && t2 != error_mark_node
5778 && (TYPE_MAIN_VARIANT (t1)
5779 == TYPE_MAIN_VARIANT (t2)))
5780 ? t1
5781 : NULL);
5782 }
5783 return ret;
5784 }
5785
5786 /* Parse a binary expression; that is, a logical-OR-expression (C90
5787 6.3.5-6.3.14, C99 6.5.5-6.5.14). If AFTER is not NULL then it is
5788 an Objective-C message expression which is the primary-expression
5789 starting the expression as an initializer.
5790
5791 OMP_ATOMIC_LHS is NULL, unless parsing OpenMP #pragma omp atomic,
5792 when it should be the unfolded lhs. In a valid OpenMP source,
5793 one of the operands of the toplevel binary expression must be equal
5794 to it. In that case, just return a build2 created binary operation
5795 rather than result of parser_build_binary_op.
5796
5797 multiplicative-expression:
5798 cast-expression
5799 multiplicative-expression * cast-expression
5800 multiplicative-expression / cast-expression
5801 multiplicative-expression % cast-expression
5802
5803 additive-expression:
5804 multiplicative-expression
5805 additive-expression + multiplicative-expression
5806 additive-expression - multiplicative-expression
5807
5808 shift-expression:
5809 additive-expression
5810 shift-expression << additive-expression
5811 shift-expression >> additive-expression
5812
5813 relational-expression:
5814 shift-expression
5815 relational-expression < shift-expression
5816 relational-expression > shift-expression
5817 relational-expression <= shift-expression
5818 relational-expression >= shift-expression
5819
5820 equality-expression:
5821 relational-expression
5822 equality-expression == relational-expression
5823 equality-expression != relational-expression
5824
5825 AND-expression:
5826 equality-expression
5827 AND-expression & equality-expression
5828
5829 exclusive-OR-expression:
5830 AND-expression
5831 exclusive-OR-expression ^ AND-expression
5832
5833 inclusive-OR-expression:
5834 exclusive-OR-expression
5835 inclusive-OR-expression | exclusive-OR-expression
5836
5837 logical-AND-expression:
5838 inclusive-OR-expression
5839 logical-AND-expression && inclusive-OR-expression
5840
5841 logical-OR-expression:
5842 logical-AND-expression
5843 logical-OR-expression || logical-AND-expression
5844 */
5845
5846 static struct c_expr
5847 c_parser_binary_expression (c_parser *parser, struct c_expr *after,
5848 tree omp_atomic_lhs)
5849 {
5850 /* A binary expression is parsed using operator-precedence parsing,
5851 with the operands being cast expressions. All the binary
5852 operators are left-associative. Thus a binary expression is of
5853 form:
5854
5855 E0 op1 E1 op2 E2 ...
5856
5857 which we represent on a stack. On the stack, the precedence
5858 levels are strictly increasing. When a new operator is
5859 encountered of higher precedence than that at the top of the
5860 stack, it is pushed; its LHS is the top expression, and its RHS
5861 is everything parsed until it is popped. When a new operator is
5862 encountered with precedence less than or equal to that at the top
5863 of the stack, triples E[i-1] op[i] E[i] are popped and replaced
5864 by the result of the operation until the operator at the top of
5865 the stack has lower precedence than the new operator or there is
5866 only one element on the stack; then the top expression is the LHS
5867 of the new operator. In the case of logical AND and OR
5868 expressions, we also need to adjust c_inhibit_evaluation_warnings
5869 as appropriate when the operators are pushed and popped. */
5870
5871 struct {
5872 /* The expression at this stack level. */
5873 struct c_expr expr;
5874 /* The precedence of the operator on its left, PREC_NONE at the
5875 bottom of the stack. */
5876 enum c_parser_prec prec;
5877 /* The operation on its left. */
5878 enum tree_code op;
5879 /* The source location of this operation. */
5880 location_t loc;
5881 } stack[NUM_PRECS];
5882 int sp;
5883 /* Location of the binary operator. */
5884 location_t binary_loc = UNKNOWN_LOCATION; /* Quiet warning. */
5885 #define POP \
5886 do { \
5887 switch (stack[sp].op) \
5888 { \
5889 case TRUTH_ANDIF_EXPR: \
5890 c_inhibit_evaluation_warnings -= (stack[sp - 1].expr.value \
5891 == truthvalue_false_node); \
5892 break; \
5893 case TRUTH_ORIF_EXPR: \
5894 c_inhibit_evaluation_warnings -= (stack[sp - 1].expr.value \
5895 == truthvalue_true_node); \
5896 break; \
5897 default: \
5898 break; \
5899 } \
5900 stack[sp - 1].expr \
5901 = convert_lvalue_to_rvalue (stack[sp - 1].loc, \
5902 stack[sp - 1].expr, true, true); \
5903 stack[sp].expr \
5904 = convert_lvalue_to_rvalue (stack[sp].loc, \
5905 stack[sp].expr, true, true); \
5906 if (__builtin_expect (omp_atomic_lhs != NULL_TREE, 0) && sp == 1 \
5907 && c_parser_peek_token (parser)->type == CPP_SEMICOLON \
5908 && ((1 << stack[sp].prec) \
5909 & (1 << (PREC_BITOR | PREC_BITXOR | PREC_BITAND | PREC_SHIFT \
5910 | PREC_ADD | PREC_MULT))) \
5911 && stack[sp].op != TRUNC_MOD_EXPR \
5912 && stack[0].expr.value != error_mark_node \
5913 && stack[1].expr.value != error_mark_node \
5914 && (c_tree_equal (stack[0].expr.value, omp_atomic_lhs) \
5915 || c_tree_equal (stack[1].expr.value, omp_atomic_lhs))) \
5916 stack[0].expr.value \
5917 = build2 (stack[1].op, TREE_TYPE (stack[0].expr.value), \
5918 stack[0].expr.value, stack[1].expr.value); \
5919 else \
5920 stack[sp - 1].expr = parser_build_binary_op (stack[sp].loc, \
5921 stack[sp].op, \
5922 stack[sp - 1].expr, \
5923 stack[sp].expr); \
5924 sp--; \
5925 } while (0)
5926 gcc_assert (!after || c_dialect_objc ());
5927 stack[0].loc = c_parser_peek_token (parser)->location;
5928 stack[0].expr = c_parser_cast_expression (parser, after);
5929 stack[0].prec = PREC_NONE;
5930 sp = 0;
5931 while (true)
5932 {
5933 enum c_parser_prec oprec;
5934 enum tree_code ocode;
5935 if (parser->error)
5936 goto out;
5937 switch (c_parser_peek_token (parser)->type)
5938 {
5939 case CPP_MULT:
5940 oprec = PREC_MULT;
5941 ocode = MULT_EXPR;
5942 break;
5943 case CPP_DIV:
5944 oprec = PREC_MULT;
5945 ocode = TRUNC_DIV_EXPR;
5946 break;
5947 case CPP_MOD:
5948 oprec = PREC_MULT;
5949 ocode = TRUNC_MOD_EXPR;
5950 break;
5951 case CPP_PLUS:
5952 oprec = PREC_ADD;
5953 ocode = PLUS_EXPR;
5954 break;
5955 case CPP_MINUS:
5956 oprec = PREC_ADD;
5957 ocode = MINUS_EXPR;
5958 break;
5959 case CPP_LSHIFT:
5960 oprec = PREC_SHIFT;
5961 ocode = LSHIFT_EXPR;
5962 break;
5963 case CPP_RSHIFT:
5964 oprec = PREC_SHIFT;
5965 ocode = RSHIFT_EXPR;
5966 break;
5967 case CPP_LESS:
5968 oprec = PREC_REL;
5969 ocode = LT_EXPR;
5970 break;
5971 case CPP_GREATER:
5972 oprec = PREC_REL;
5973 ocode = GT_EXPR;
5974 break;
5975 case CPP_LESS_EQ:
5976 oprec = PREC_REL;
5977 ocode = LE_EXPR;
5978 break;
5979 case CPP_GREATER_EQ:
5980 oprec = PREC_REL;
5981 ocode = GE_EXPR;
5982 break;
5983 case CPP_EQ_EQ:
5984 oprec = PREC_EQ;
5985 ocode = EQ_EXPR;
5986 break;
5987 case CPP_NOT_EQ:
5988 oprec = PREC_EQ;
5989 ocode = NE_EXPR;
5990 break;
5991 case CPP_AND:
5992 oprec = PREC_BITAND;
5993 ocode = BIT_AND_EXPR;
5994 break;
5995 case CPP_XOR:
5996 oprec = PREC_BITXOR;
5997 ocode = BIT_XOR_EXPR;
5998 break;
5999 case CPP_OR:
6000 oprec = PREC_BITOR;
6001 ocode = BIT_IOR_EXPR;
6002 break;
6003 case CPP_AND_AND:
6004 oprec = PREC_LOGAND;
6005 ocode = TRUTH_ANDIF_EXPR;
6006 break;
6007 case CPP_OR_OR:
6008 oprec = PREC_LOGOR;
6009 ocode = TRUTH_ORIF_EXPR;
6010 break;
6011 default:
6012 /* Not a binary operator, so end of the binary
6013 expression. */
6014 goto out;
6015 }
6016 binary_loc = c_parser_peek_token (parser)->location;
6017 while (oprec <= stack[sp].prec)
6018 POP;
6019 c_parser_consume_token (parser);
6020 switch (ocode)
6021 {
6022 case TRUTH_ANDIF_EXPR:
6023 stack[sp].expr
6024 = convert_lvalue_to_rvalue (stack[sp].loc,
6025 stack[sp].expr, true, true);
6026 stack[sp].expr.value = c_objc_common_truthvalue_conversion
6027 (stack[sp].loc, default_conversion (stack[sp].expr.value));
6028 c_inhibit_evaluation_warnings += (stack[sp].expr.value
6029 == truthvalue_false_node);
6030 break;
6031 case TRUTH_ORIF_EXPR:
6032 stack[sp].expr
6033 = convert_lvalue_to_rvalue (stack[sp].loc,
6034 stack[sp].expr, true, true);
6035 stack[sp].expr.value = c_objc_common_truthvalue_conversion
6036 (stack[sp].loc, default_conversion (stack[sp].expr.value));
6037 c_inhibit_evaluation_warnings += (stack[sp].expr.value
6038 == truthvalue_true_node);
6039 break;
6040 default:
6041 break;
6042 }
6043 sp++;
6044 stack[sp].loc = binary_loc;
6045 stack[sp].expr = c_parser_cast_expression (parser, NULL);
6046 stack[sp].prec = oprec;
6047 stack[sp].op = ocode;
6048 stack[sp].loc = binary_loc;
6049 }
6050 out:
6051 while (sp > 0)
6052 POP;
6053 return stack[0].expr;
6054 #undef POP
6055 }
6056
6057 /* Parse a cast expression (C90 6.3.4, C99 6.5.4). If AFTER is not
6058 NULL then it is an Objective-C message expression which is the
6059 primary-expression starting the expression as an initializer.
6060
6061 cast-expression:
6062 unary-expression
6063 ( type-name ) unary-expression
6064 */
6065
6066 static struct c_expr
6067 c_parser_cast_expression (c_parser *parser, struct c_expr *after)
6068 {
6069 location_t cast_loc = c_parser_peek_token (parser)->location;
6070 gcc_assert (!after || c_dialect_objc ());
6071 if (after)
6072 return c_parser_postfix_expression_after_primary (parser,
6073 cast_loc, *after);
6074 /* If the expression begins with a parenthesized type name, it may
6075 be either a cast or a compound literal; we need to see whether
6076 the next character is '{' to tell the difference. If not, it is
6077 an unary expression. Full detection of unknown typenames here
6078 would require a 3-token lookahead. */
6079 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
6080 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
6081 {
6082 struct c_type_name *type_name;
6083 struct c_expr ret;
6084 struct c_expr expr;
6085 c_parser_consume_token (parser);
6086 type_name = c_parser_type_name (parser);
6087 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
6088 if (type_name == NULL)
6089 {
6090 ret.value = error_mark_node;
6091 ret.original_code = ERROR_MARK;
6092 ret.original_type = NULL;
6093 return ret;
6094 }
6095
6096 /* Save casted types in the function's used types hash table. */
6097 used_types_insert (type_name->specs->type);
6098
6099 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
6100 return c_parser_postfix_expression_after_paren_type (parser, type_name,
6101 cast_loc);
6102 {
6103 location_t expr_loc = c_parser_peek_token (parser)->location;
6104 expr = c_parser_cast_expression (parser, NULL);
6105 expr = convert_lvalue_to_rvalue (expr_loc, expr, true, true);
6106 }
6107 ret.value = c_cast_expr (cast_loc, type_name, expr.value);
6108 ret.original_code = ERROR_MARK;
6109 ret.original_type = NULL;
6110 return ret;
6111 }
6112 else
6113 return c_parser_unary_expression (parser);
6114 }
6115
6116 /* Parse an unary expression (C90 6.3.3, C99 6.5.3).
6117
6118 unary-expression:
6119 postfix-expression
6120 ++ unary-expression
6121 -- unary-expression
6122 unary-operator cast-expression
6123 sizeof unary-expression
6124 sizeof ( type-name )
6125
6126 unary-operator: one of
6127 & * + - ~ !
6128
6129 GNU extensions:
6130
6131 unary-expression:
6132 __alignof__ unary-expression
6133 __alignof__ ( type-name )
6134 && identifier
6135
6136 (C11 permits _Alignof with type names only.)
6137
6138 unary-operator: one of
6139 __extension__ __real__ __imag__
6140
6141 Transactional Memory:
6142
6143 unary-expression:
6144 transaction-expression
6145
6146 In addition, the GNU syntax treats ++ and -- as unary operators, so
6147 they may be applied to cast expressions with errors for non-lvalues
6148 given later. */
6149
6150 static struct c_expr
6151 c_parser_unary_expression (c_parser *parser)
6152 {
6153 int ext;
6154 struct c_expr ret, op;
6155 location_t op_loc = c_parser_peek_token (parser)->location;
6156 location_t exp_loc;
6157 ret.original_code = ERROR_MARK;
6158 ret.original_type = NULL;
6159 switch (c_parser_peek_token (parser)->type)
6160 {
6161 case CPP_PLUS_PLUS:
6162 c_parser_consume_token (parser);
6163 exp_loc = c_parser_peek_token (parser)->location;
6164 op = c_parser_cast_expression (parser, NULL);
6165
6166 /* If there is array notations in op, we expand them. */
6167 if (flag_enable_cilkplus && TREE_CODE (op.value) == ARRAY_NOTATION_REF)
6168 return fix_array_notation_expr (exp_loc, PREINCREMENT_EXPR, op);
6169 else
6170 {
6171 op = default_function_array_read_conversion (exp_loc, op);
6172 return parser_build_unary_op (op_loc, PREINCREMENT_EXPR, op);
6173 }
6174 case CPP_MINUS_MINUS:
6175 c_parser_consume_token (parser);
6176 exp_loc = c_parser_peek_token (parser)->location;
6177 op = c_parser_cast_expression (parser, NULL);
6178
6179 /* If there is array notations in op, we expand them. */
6180 if (flag_enable_cilkplus && TREE_CODE (op.value) == ARRAY_NOTATION_REF)
6181 return fix_array_notation_expr (exp_loc, PREDECREMENT_EXPR, op);
6182 else
6183 {
6184 op = default_function_array_read_conversion (exp_loc, op);
6185 return parser_build_unary_op (op_loc, PREDECREMENT_EXPR, op);
6186 }
6187 case CPP_AND:
6188 c_parser_consume_token (parser);
6189 op = c_parser_cast_expression (parser, NULL);
6190 mark_exp_read (op.value);
6191 return parser_build_unary_op (op_loc, ADDR_EXPR, op);
6192 case CPP_MULT:
6193 c_parser_consume_token (parser);
6194 exp_loc = c_parser_peek_token (parser)->location;
6195 op = c_parser_cast_expression (parser, NULL);
6196 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
6197 ret.value = build_indirect_ref (op_loc, op.value, RO_UNARY_STAR);
6198 return ret;
6199 case CPP_PLUS:
6200 if (!c_dialect_objc () && !in_system_header)
6201 warning_at (op_loc,
6202 OPT_Wtraditional,
6203 "traditional C rejects the unary plus operator");
6204 c_parser_consume_token (parser);
6205 exp_loc = c_parser_peek_token (parser)->location;
6206 op = c_parser_cast_expression (parser, NULL);
6207 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
6208 return parser_build_unary_op (op_loc, CONVERT_EXPR, op);
6209 case CPP_MINUS:
6210 c_parser_consume_token (parser);
6211 exp_loc = c_parser_peek_token (parser)->location;
6212 op = c_parser_cast_expression (parser, NULL);
6213 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
6214 return parser_build_unary_op (op_loc, NEGATE_EXPR, op);
6215 case CPP_COMPL:
6216 c_parser_consume_token (parser);
6217 exp_loc = c_parser_peek_token (parser)->location;
6218 op = c_parser_cast_expression (parser, NULL);
6219 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
6220 return parser_build_unary_op (op_loc, BIT_NOT_EXPR, op);
6221 case CPP_NOT:
6222 c_parser_consume_token (parser);
6223 exp_loc = c_parser_peek_token (parser)->location;
6224 op = c_parser_cast_expression (parser, NULL);
6225 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
6226 return parser_build_unary_op (op_loc, TRUTH_NOT_EXPR, op);
6227 case CPP_AND_AND:
6228 /* Refer to the address of a label as a pointer. */
6229 c_parser_consume_token (parser);
6230 if (c_parser_next_token_is (parser, CPP_NAME))
6231 {
6232 ret.value = finish_label_address_expr
6233 (c_parser_peek_token (parser)->value, op_loc);
6234 c_parser_consume_token (parser);
6235 }
6236 else
6237 {
6238 c_parser_error (parser, "expected identifier");
6239 ret.value = error_mark_node;
6240 }
6241 return ret;
6242 case CPP_KEYWORD:
6243 switch (c_parser_peek_token (parser)->keyword)
6244 {
6245 case RID_SIZEOF:
6246 return c_parser_sizeof_expression (parser);
6247 case RID_ALIGNOF:
6248 return c_parser_alignof_expression (parser);
6249 case RID_EXTENSION:
6250 c_parser_consume_token (parser);
6251 ext = disable_extension_diagnostics ();
6252 ret = c_parser_cast_expression (parser, NULL);
6253 restore_extension_diagnostics (ext);
6254 return ret;
6255 case RID_REALPART:
6256 c_parser_consume_token (parser);
6257 exp_loc = c_parser_peek_token (parser)->location;
6258 op = c_parser_cast_expression (parser, NULL);
6259 op = default_function_array_conversion (exp_loc, op);
6260 return parser_build_unary_op (op_loc, REALPART_EXPR, op);
6261 case RID_IMAGPART:
6262 c_parser_consume_token (parser);
6263 exp_loc = c_parser_peek_token (parser)->location;
6264 op = c_parser_cast_expression (parser, NULL);
6265 op = default_function_array_conversion (exp_loc, op);
6266 return parser_build_unary_op (op_loc, IMAGPART_EXPR, op);
6267 case RID_TRANSACTION_ATOMIC:
6268 case RID_TRANSACTION_RELAXED:
6269 return c_parser_transaction_expression (parser,
6270 c_parser_peek_token (parser)->keyword);
6271 default:
6272 return c_parser_postfix_expression (parser);
6273 }
6274 default:
6275 return c_parser_postfix_expression (parser);
6276 }
6277 }
6278
6279 /* Parse a sizeof expression. */
6280
6281 static struct c_expr
6282 c_parser_sizeof_expression (c_parser *parser)
6283 {
6284 struct c_expr expr;
6285 location_t expr_loc;
6286 gcc_assert (c_parser_next_token_is_keyword (parser, RID_SIZEOF));
6287 c_parser_consume_token (parser);
6288 c_inhibit_evaluation_warnings++;
6289 in_sizeof++;
6290 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
6291 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
6292 {
6293 /* Either sizeof ( type-name ) or sizeof unary-expression
6294 starting with a compound literal. */
6295 struct c_type_name *type_name;
6296 c_parser_consume_token (parser);
6297 expr_loc = c_parser_peek_token (parser)->location;
6298 type_name = c_parser_type_name (parser);
6299 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
6300 if (type_name == NULL)
6301 {
6302 struct c_expr ret;
6303 c_inhibit_evaluation_warnings--;
6304 in_sizeof--;
6305 ret.value = error_mark_node;
6306 ret.original_code = ERROR_MARK;
6307 ret.original_type = NULL;
6308 return ret;
6309 }
6310 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
6311 {
6312 expr = c_parser_postfix_expression_after_paren_type (parser,
6313 type_name,
6314 expr_loc);
6315 goto sizeof_expr;
6316 }
6317 /* sizeof ( type-name ). */
6318 c_inhibit_evaluation_warnings--;
6319 in_sizeof--;
6320 return c_expr_sizeof_type (expr_loc, type_name);
6321 }
6322 else
6323 {
6324 expr_loc = c_parser_peek_token (parser)->location;
6325 expr = c_parser_unary_expression (parser);
6326 sizeof_expr:
6327 c_inhibit_evaluation_warnings--;
6328 in_sizeof--;
6329 mark_exp_read (expr.value);
6330 if (TREE_CODE (expr.value) == COMPONENT_REF
6331 && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1)))
6332 error_at (expr_loc, "%<sizeof%> applied to a bit-field");
6333 return c_expr_sizeof_expr (expr_loc, expr);
6334 }
6335 }
6336
6337 /* Parse an alignof expression. */
6338
6339 static struct c_expr
6340 c_parser_alignof_expression (c_parser *parser)
6341 {
6342 struct c_expr expr;
6343 location_t loc = c_parser_peek_token (parser)->location;
6344 tree alignof_spelling = c_parser_peek_token (parser)->value;
6345 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ALIGNOF));
6346 /* A diagnostic is not required for the use of this identifier in
6347 the implementation namespace; only diagnose it for the C11
6348 spelling because of existing code using the other spellings. */
6349 if (!flag_isoc11
6350 && strcmp (IDENTIFIER_POINTER (alignof_spelling), "_Alignof") == 0)
6351 {
6352 if (flag_isoc99)
6353 pedwarn (loc, OPT_Wpedantic, "ISO C99 does not support %qE",
6354 alignof_spelling);
6355 else
6356 pedwarn (loc, OPT_Wpedantic, "ISO C90 does not support %qE",
6357 alignof_spelling);
6358 }
6359 c_parser_consume_token (parser);
6360 c_inhibit_evaluation_warnings++;
6361 in_alignof++;
6362 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
6363 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
6364 {
6365 /* Either __alignof__ ( type-name ) or __alignof__
6366 unary-expression starting with a compound literal. */
6367 location_t loc;
6368 struct c_type_name *type_name;
6369 struct c_expr ret;
6370 c_parser_consume_token (parser);
6371 loc = c_parser_peek_token (parser)->location;
6372 type_name = c_parser_type_name (parser);
6373 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
6374 if (type_name == NULL)
6375 {
6376 struct c_expr ret;
6377 c_inhibit_evaluation_warnings--;
6378 in_alignof--;
6379 ret.value = error_mark_node;
6380 ret.original_code = ERROR_MARK;
6381 ret.original_type = NULL;
6382 return ret;
6383 }
6384 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
6385 {
6386 expr = c_parser_postfix_expression_after_paren_type (parser,
6387 type_name,
6388 loc);
6389 goto alignof_expr;
6390 }
6391 /* alignof ( type-name ). */
6392 c_inhibit_evaluation_warnings--;
6393 in_alignof--;
6394 ret.value = c_alignof (loc, groktypename (type_name, NULL, NULL));
6395 ret.original_code = ERROR_MARK;
6396 ret.original_type = NULL;
6397 return ret;
6398 }
6399 else
6400 {
6401 struct c_expr ret;
6402 expr = c_parser_unary_expression (parser);
6403 alignof_expr:
6404 mark_exp_read (expr.value);
6405 c_inhibit_evaluation_warnings--;
6406 in_alignof--;
6407 pedwarn (loc, OPT_Wpedantic, "ISO C does not allow %<%E (expression)%>",
6408 alignof_spelling);
6409 ret.value = c_alignof_expr (loc, expr.value);
6410 ret.original_code = ERROR_MARK;
6411 ret.original_type = NULL;
6412 return ret;
6413 }
6414 }
6415
6416 /* Helper function to read arguments of builtins which are interfaces
6417 for the middle-end nodes like COMPLEX_EXPR, VEC_PERM_EXPR and
6418 others. The name of the builtin is passed using BNAME parameter.
6419 Function returns true if there were no errors while parsing and
6420 stores the arguments in CEXPR_LIST. */
6421 static bool
6422 c_parser_get_builtin_args (c_parser *parser, const char *bname,
6423 vec<c_expr_t, va_gc> **ret_cexpr_list,
6424 bool choose_expr_p)
6425 {
6426 location_t loc = c_parser_peek_token (parser)->location;
6427 vec<c_expr_t, va_gc> *cexpr_list;
6428 c_expr_t expr;
6429 bool saved_force_folding_builtin_constant_p;
6430
6431 *ret_cexpr_list = NULL;
6432 if (c_parser_next_token_is_not (parser, CPP_OPEN_PAREN))
6433 {
6434 error_at (loc, "cannot take address of %qs", bname);
6435 return false;
6436 }
6437
6438 c_parser_consume_token (parser);
6439
6440 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
6441 {
6442 c_parser_consume_token (parser);
6443 return true;
6444 }
6445
6446 saved_force_folding_builtin_constant_p
6447 = force_folding_builtin_constant_p;
6448 force_folding_builtin_constant_p |= choose_expr_p;
6449 expr = c_parser_expr_no_commas (parser, NULL);
6450 force_folding_builtin_constant_p
6451 = saved_force_folding_builtin_constant_p;
6452 vec_alloc (cexpr_list, 1);
6453 C_EXPR_APPEND (cexpr_list, expr);
6454 while (c_parser_next_token_is (parser, CPP_COMMA))
6455 {
6456 c_parser_consume_token (parser);
6457 expr = c_parser_expr_no_commas (parser, NULL);
6458 C_EXPR_APPEND (cexpr_list, expr);
6459 }
6460
6461 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
6462 return false;
6463
6464 *ret_cexpr_list = cexpr_list;
6465 return true;
6466 }
6467
6468 /* This represents a single generic-association. */
6469
6470 struct c_generic_association
6471 {
6472 /* The location of the starting token of the type. */
6473 location_t type_location;
6474 /* The association's type, or NULL_TREE for 'default'. */
6475 tree type;
6476 /* The association's expression. */
6477 struct c_expr expression;
6478 };
6479
6480 /* Parse a generic-selection. (C11 6.5.1.1).
6481
6482 generic-selection:
6483 _Generic ( assignment-expression , generic-assoc-list )
6484
6485 generic-assoc-list:
6486 generic-association
6487 generic-assoc-list , generic-association
6488
6489 generic-association:
6490 type-name : assignment-expression
6491 default : assignment-expression
6492 */
6493
6494 static struct c_expr
6495 c_parser_generic_selection (c_parser *parser)
6496 {
6497 vec<c_generic_association> associations = vNULL;
6498 struct c_expr selector, error_expr;
6499 tree selector_type;
6500 struct c_generic_association matched_assoc;
6501 bool match_found = false;
6502 location_t generic_loc, selector_loc;
6503
6504 error_expr.original_code = ERROR_MARK;
6505 error_expr.original_type = NULL;
6506 error_expr.value = error_mark_node;
6507 matched_assoc.type_location = UNKNOWN_LOCATION;
6508 matched_assoc.type = NULL_TREE;
6509 matched_assoc.expression = error_expr;
6510
6511 gcc_assert (c_parser_next_token_is_keyword (parser, RID_GENERIC));
6512 generic_loc = c_parser_peek_token (parser)->location;
6513 c_parser_consume_token (parser);
6514 if (!flag_isoc11)
6515 {
6516 if (flag_isoc99)
6517 pedwarn (generic_loc, OPT_Wpedantic,
6518 "ISO C99 does not support %<_Generic%>");
6519 else
6520 pedwarn (generic_loc, OPT_Wpedantic,
6521 "ISO C90 does not support %<_Generic%>");
6522 }
6523
6524 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6525 return error_expr;
6526
6527 c_inhibit_evaluation_warnings++;
6528 selector_loc = c_parser_peek_token (parser)->location;
6529 selector = c_parser_expr_no_commas (parser, NULL);
6530 selector = default_function_array_conversion (selector_loc, selector);
6531 c_inhibit_evaluation_warnings--;
6532
6533 if (selector.value == error_mark_node)
6534 {
6535 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6536 return selector;
6537 }
6538 selector_type = TREE_TYPE (selector.value);
6539 /* In ISO C terms, rvalues (including the controlling expression of
6540 _Generic) do not have qualified types. */
6541 if (TREE_CODE (selector_type) != ARRAY_TYPE)
6542 selector_type = TYPE_MAIN_VARIANT (selector_type);
6543 /* In ISO C terms, _Noreturn is not part of the type of expressions
6544 such as &abort, but in GCC it is represented internally as a type
6545 qualifier. */
6546 if (FUNCTION_POINTER_TYPE_P (selector_type)
6547 && TYPE_QUALS (TREE_TYPE (selector_type)) != TYPE_UNQUALIFIED)
6548 selector_type
6549 = build_pointer_type (TYPE_MAIN_VARIANT (TREE_TYPE (selector_type)));
6550
6551 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
6552 {
6553 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6554 return error_expr;
6555 }
6556
6557 while (1)
6558 {
6559 struct c_generic_association assoc, *iter;
6560 unsigned int ix;
6561 c_token *token = c_parser_peek_token (parser);
6562
6563 assoc.type_location = token->location;
6564 if (token->type == CPP_KEYWORD && token->keyword == RID_DEFAULT)
6565 {
6566 c_parser_consume_token (parser);
6567 assoc.type = NULL_TREE;
6568 }
6569 else
6570 {
6571 struct c_type_name *type_name;
6572
6573 type_name = c_parser_type_name (parser);
6574 if (type_name == NULL)
6575 {
6576 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6577 goto error_exit;
6578 }
6579 assoc.type = groktypename (type_name, NULL, NULL);
6580 if (assoc.type == error_mark_node)
6581 {
6582 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6583 goto error_exit;
6584 }
6585
6586 if (TREE_CODE (assoc.type) == FUNCTION_TYPE)
6587 error_at (assoc.type_location,
6588 "%<_Generic%> association has function type");
6589 else if (!COMPLETE_TYPE_P (assoc.type))
6590 error_at (assoc.type_location,
6591 "%<_Generic%> association has incomplete type");
6592
6593 if (variably_modified_type_p (assoc.type, NULL_TREE))
6594 error_at (assoc.type_location,
6595 "%<_Generic%> association has "
6596 "variable length type");
6597 }
6598
6599 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
6600 {
6601 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6602 goto error_exit;
6603 }
6604
6605 assoc.expression = c_parser_expr_no_commas (parser, NULL);
6606 if (assoc.expression.value == error_mark_node)
6607 {
6608 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6609 goto error_exit;
6610 }
6611
6612 for (ix = 0; associations.iterate (ix, &iter); ++ix)
6613 {
6614 if (assoc.type == NULL_TREE)
6615 {
6616 if (iter->type == NULL_TREE)
6617 {
6618 error_at (assoc.type_location,
6619 "duplicate %<default%> case in %<_Generic%>");
6620 inform (iter->type_location, "original %<default%> is here");
6621 }
6622 }
6623 else if (iter->type != NULL_TREE)
6624 {
6625 if (comptypes (assoc.type, iter->type))
6626 {
6627 error_at (assoc.type_location,
6628 "%<_Generic%> specifies two compatible types");
6629 inform (iter->type_location, "compatible type is here");
6630 }
6631 }
6632 }
6633
6634 if (assoc.type == NULL_TREE)
6635 {
6636 if (!match_found)
6637 {
6638 matched_assoc = assoc;
6639 match_found = true;
6640 }
6641 }
6642 else if (comptypes (assoc.type, selector_type))
6643 {
6644 if (!match_found || matched_assoc.type == NULL_TREE)
6645 {
6646 matched_assoc = assoc;
6647 match_found = true;
6648 }
6649 else
6650 {
6651 error_at (assoc.type_location,
6652 "%<_Generic> selector matches multiple associations");
6653 inform (matched_assoc.type_location,
6654 "other match is here");
6655 }
6656 }
6657
6658 associations.safe_push (assoc);
6659
6660 if (c_parser_peek_token (parser)->type != CPP_COMMA)
6661 break;
6662 c_parser_consume_token (parser);
6663 }
6664
6665 associations.release ();
6666
6667 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
6668 {
6669 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6670 return error_expr;
6671 }
6672
6673 if (!match_found)
6674 {
6675 error_at (selector_loc, "%<_Generic%> selector of type %qT is not "
6676 "compatible with any association",
6677 selector_type);
6678 return error_expr;
6679 }
6680
6681 return matched_assoc.expression;
6682
6683 error_exit:
6684 associations.release ();
6685 return error_expr;
6686 }
6687
6688 /* Parse a postfix expression (C90 6.3.1-6.3.2, C99 6.5.1-6.5.2).
6689
6690 postfix-expression:
6691 primary-expression
6692 postfix-expression [ expression ]
6693 postfix-expression ( argument-expression-list[opt] )
6694 postfix-expression . identifier
6695 postfix-expression -> identifier
6696 postfix-expression ++
6697 postfix-expression --
6698 ( type-name ) { initializer-list }
6699 ( type-name ) { initializer-list , }
6700
6701 argument-expression-list:
6702 argument-expression
6703 argument-expression-list , argument-expression
6704
6705 primary-expression:
6706 identifier
6707 constant
6708 string-literal
6709 ( expression )
6710 generic-selection
6711
6712 GNU extensions:
6713
6714 primary-expression:
6715 __func__
6716 (treated as a keyword in GNU C)
6717 __FUNCTION__
6718 __PRETTY_FUNCTION__
6719 ( compound-statement )
6720 __builtin_va_arg ( assignment-expression , type-name )
6721 __builtin_offsetof ( type-name , offsetof-member-designator )
6722 __builtin_choose_expr ( assignment-expression ,
6723 assignment-expression ,
6724 assignment-expression )
6725 __builtin_types_compatible_p ( type-name , type-name )
6726 __builtin_complex ( assignment-expression , assignment-expression )
6727 __builtin_shuffle ( assignment-expression , assignment-expression )
6728 __builtin_shuffle ( assignment-expression ,
6729 assignment-expression ,
6730 assignment-expression, )
6731
6732 offsetof-member-designator:
6733 identifier
6734 offsetof-member-designator . identifier
6735 offsetof-member-designator [ expression ]
6736
6737 Objective-C:
6738
6739 primary-expression:
6740 [ objc-receiver objc-message-args ]
6741 @selector ( objc-selector-arg )
6742 @protocol ( identifier )
6743 @encode ( type-name )
6744 objc-string-literal
6745 Classname . identifier
6746 */
6747
6748 static struct c_expr
6749 c_parser_postfix_expression (c_parser *parser)
6750 {
6751 struct c_expr expr, e1;
6752 struct c_type_name *t1, *t2;
6753 location_t loc = c_parser_peek_token (parser)->location;;
6754 expr.original_code = ERROR_MARK;
6755 expr.original_type = NULL;
6756 switch (c_parser_peek_token (parser)->type)
6757 {
6758 case CPP_NUMBER:
6759 expr.value = c_parser_peek_token (parser)->value;
6760 loc = c_parser_peek_token (parser)->location;
6761 c_parser_consume_token (parser);
6762 if (TREE_CODE (expr.value) == FIXED_CST
6763 && !targetm.fixed_point_supported_p ())
6764 {
6765 error_at (loc, "fixed-point types not supported for this target");
6766 expr.value = error_mark_node;
6767 }
6768 break;
6769 case CPP_CHAR:
6770 case CPP_CHAR16:
6771 case CPP_CHAR32:
6772 case CPP_WCHAR:
6773 expr.value = c_parser_peek_token (parser)->value;
6774 c_parser_consume_token (parser);
6775 break;
6776 case CPP_STRING:
6777 case CPP_STRING16:
6778 case CPP_STRING32:
6779 case CPP_WSTRING:
6780 case CPP_UTF8STRING:
6781 expr.value = c_parser_peek_token (parser)->value;
6782 expr.original_code = STRING_CST;
6783 c_parser_consume_token (parser);
6784 break;
6785 case CPP_OBJC_STRING:
6786 gcc_assert (c_dialect_objc ());
6787 expr.value
6788 = objc_build_string_object (c_parser_peek_token (parser)->value);
6789 c_parser_consume_token (parser);
6790 break;
6791 case CPP_NAME:
6792 switch (c_parser_peek_token (parser)->id_kind)
6793 {
6794 case C_ID_ID:
6795 {
6796 tree id = c_parser_peek_token (parser)->value;
6797 c_parser_consume_token (parser);
6798 expr.value = build_external_ref (loc, id,
6799 (c_parser_peek_token (parser)->type
6800 == CPP_OPEN_PAREN),
6801 &expr.original_type);
6802 break;
6803 }
6804 case C_ID_CLASSNAME:
6805 {
6806 /* Here we parse the Objective-C 2.0 Class.name dot
6807 syntax. */
6808 tree class_name = c_parser_peek_token (parser)->value;
6809 tree component;
6810 c_parser_consume_token (parser);
6811 gcc_assert (c_dialect_objc ());
6812 if (!c_parser_require (parser, CPP_DOT, "expected %<.%>"))
6813 {
6814 expr.value = error_mark_node;
6815 break;
6816 }
6817 if (c_parser_next_token_is_not (parser, CPP_NAME))
6818 {
6819 c_parser_error (parser, "expected identifier");
6820 expr.value = error_mark_node;
6821 break;
6822 }
6823 component = c_parser_peek_token (parser)->value;
6824 c_parser_consume_token (parser);
6825 expr.value = objc_build_class_component_ref (class_name,
6826 component);
6827 break;
6828 }
6829 default:
6830 c_parser_error (parser, "expected expression");
6831 expr.value = error_mark_node;
6832 break;
6833 }
6834 break;
6835 case CPP_OPEN_PAREN:
6836 /* A parenthesized expression, statement expression or compound
6837 literal. */
6838 if (c_parser_peek_2nd_token (parser)->type == CPP_OPEN_BRACE)
6839 {
6840 /* A statement expression. */
6841 tree stmt;
6842 location_t brace_loc;
6843 c_parser_consume_token (parser);
6844 brace_loc = c_parser_peek_token (parser)->location;
6845 c_parser_consume_token (parser);
6846 if (!building_stmt_list_p ())
6847 {
6848 error_at (loc, "braced-group within expression allowed "
6849 "only inside a function");
6850 parser->error = true;
6851 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
6852 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6853 expr.value = error_mark_node;
6854 break;
6855 }
6856 stmt = c_begin_stmt_expr ();
6857 c_parser_compound_statement_nostart (parser);
6858 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6859 "expected %<)%>");
6860 pedwarn (loc, OPT_Wpedantic,
6861 "ISO C forbids braced-groups within expressions");
6862 expr.value = c_finish_stmt_expr (brace_loc, stmt);
6863 mark_exp_read (expr.value);
6864 }
6865 else if (c_token_starts_typename (c_parser_peek_2nd_token (parser)))
6866 {
6867 /* A compound literal. ??? Can we actually get here rather
6868 than going directly to
6869 c_parser_postfix_expression_after_paren_type from
6870 elsewhere? */
6871 location_t loc;
6872 struct c_type_name *type_name;
6873 c_parser_consume_token (parser);
6874 loc = c_parser_peek_token (parser)->location;
6875 type_name = c_parser_type_name (parser);
6876 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6877 "expected %<)%>");
6878 if (type_name == NULL)
6879 {
6880 expr.value = error_mark_node;
6881 }
6882 else
6883 expr = c_parser_postfix_expression_after_paren_type (parser,
6884 type_name,
6885 loc);
6886 }
6887 else
6888 {
6889 /* A parenthesized expression. */
6890 c_parser_consume_token (parser);
6891 expr = c_parser_expression (parser);
6892 if (TREE_CODE (expr.value) == MODIFY_EXPR)
6893 TREE_NO_WARNING (expr.value) = 1;
6894 if (expr.original_code != C_MAYBE_CONST_EXPR)
6895 expr.original_code = ERROR_MARK;
6896 /* Don't change EXPR.ORIGINAL_TYPE. */
6897 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6898 "expected %<)%>");
6899 }
6900 break;
6901 case CPP_KEYWORD:
6902 switch (c_parser_peek_token (parser)->keyword)
6903 {
6904 case RID_FUNCTION_NAME:
6905 case RID_PRETTY_FUNCTION_NAME:
6906 case RID_C99_FUNCTION_NAME:
6907 expr.value = fname_decl (loc,
6908 c_parser_peek_token (parser)->keyword,
6909 c_parser_peek_token (parser)->value);
6910 c_parser_consume_token (parser);
6911 break;
6912 case RID_VA_ARG:
6913 c_parser_consume_token (parser);
6914 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6915 {
6916 expr.value = error_mark_node;
6917 break;
6918 }
6919 e1 = c_parser_expr_no_commas (parser, NULL);
6920 mark_exp_read (e1.value);
6921 e1.value = c_fully_fold (e1.value, false, NULL);
6922 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
6923 {
6924 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6925 expr.value = error_mark_node;
6926 break;
6927 }
6928 loc = c_parser_peek_token (parser)->location;
6929 t1 = c_parser_type_name (parser);
6930 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6931 "expected %<)%>");
6932 if (t1 == NULL)
6933 {
6934 expr.value = error_mark_node;
6935 }
6936 else
6937 {
6938 tree type_expr = NULL_TREE;
6939 expr.value = c_build_va_arg (loc, e1.value,
6940 groktypename (t1, &type_expr, NULL));
6941 if (type_expr)
6942 {
6943 expr.value = build2 (C_MAYBE_CONST_EXPR,
6944 TREE_TYPE (expr.value), type_expr,
6945 expr.value);
6946 C_MAYBE_CONST_EXPR_NON_CONST (expr.value) = true;
6947 }
6948 }
6949 break;
6950 case RID_OFFSETOF:
6951 c_parser_consume_token (parser);
6952 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6953 {
6954 expr.value = error_mark_node;
6955 break;
6956 }
6957 t1 = c_parser_type_name (parser);
6958 if (t1 == NULL)
6959 parser->error = true;
6960 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
6961 gcc_assert (parser->error);
6962 if (parser->error)
6963 {
6964 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6965 expr.value = error_mark_node;
6966 break;
6967 }
6968
6969 {
6970 tree type = groktypename (t1, NULL, NULL);
6971 tree offsetof_ref;
6972 if (type == error_mark_node)
6973 offsetof_ref = error_mark_node;
6974 else
6975 {
6976 offsetof_ref = build1 (INDIRECT_REF, type, null_pointer_node);
6977 SET_EXPR_LOCATION (offsetof_ref, loc);
6978 }
6979 /* Parse the second argument to __builtin_offsetof. We
6980 must have one identifier, and beyond that we want to
6981 accept sub structure and sub array references. */
6982 if (c_parser_next_token_is (parser, CPP_NAME))
6983 {
6984 offsetof_ref = build_component_ref
6985 (loc, offsetof_ref, c_parser_peek_token (parser)->value);
6986 c_parser_consume_token (parser);
6987 while (c_parser_next_token_is (parser, CPP_DOT)
6988 || c_parser_next_token_is (parser,
6989 CPP_OPEN_SQUARE)
6990 || c_parser_next_token_is (parser,
6991 CPP_DEREF))
6992 {
6993 if (c_parser_next_token_is (parser, CPP_DEREF))
6994 {
6995 loc = c_parser_peek_token (parser)->location;
6996 offsetof_ref = build_array_ref (loc,
6997 offsetof_ref,
6998 integer_zero_node);
6999 goto do_dot;
7000 }
7001 else if (c_parser_next_token_is (parser, CPP_DOT))
7002 {
7003 do_dot:
7004 c_parser_consume_token (parser);
7005 if (c_parser_next_token_is_not (parser,
7006 CPP_NAME))
7007 {
7008 c_parser_error (parser, "expected identifier");
7009 break;
7010 }
7011 offsetof_ref = build_component_ref
7012 (loc, offsetof_ref,
7013 c_parser_peek_token (parser)->value);
7014 c_parser_consume_token (parser);
7015 }
7016 else
7017 {
7018 struct c_expr ce;
7019 tree idx;
7020 loc = c_parser_peek_token (parser)->location;
7021 c_parser_consume_token (parser);
7022 ce = c_parser_expression (parser);
7023 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
7024 idx = ce.value;
7025 idx = c_fully_fold (idx, false, NULL);
7026 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
7027 "expected %<]%>");
7028 offsetof_ref = build_array_ref (loc, offsetof_ref, idx);
7029 }
7030 }
7031 }
7032 else
7033 c_parser_error (parser, "expected identifier");
7034 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7035 "expected %<)%>");
7036 expr.value = fold_offsetof (offsetof_ref);
7037 }
7038 break;
7039 case RID_CHOOSE_EXPR:
7040 {
7041 vec<c_expr_t, va_gc> *cexpr_list;
7042 c_expr_t *e1_p, *e2_p, *e3_p;
7043 tree c;
7044
7045 c_parser_consume_token (parser);
7046 if (!c_parser_get_builtin_args (parser,
7047 "__builtin_choose_expr",
7048 &cexpr_list, true))
7049 {
7050 expr.value = error_mark_node;
7051 break;
7052 }
7053
7054 if (vec_safe_length (cexpr_list) != 3)
7055 {
7056 error_at (loc, "wrong number of arguments to "
7057 "%<__builtin_choose_expr%>");
7058 expr.value = error_mark_node;
7059 break;
7060 }
7061
7062 e1_p = &(*cexpr_list)[0];
7063 e2_p = &(*cexpr_list)[1];
7064 e3_p = &(*cexpr_list)[2];
7065
7066 c = e1_p->value;
7067 mark_exp_read (e2_p->value);
7068 mark_exp_read (e3_p->value);
7069 if (TREE_CODE (c) != INTEGER_CST
7070 || !INTEGRAL_TYPE_P (TREE_TYPE (c)))
7071 error_at (loc,
7072 "first argument to %<__builtin_choose_expr%> not"
7073 " a constant");
7074 constant_expression_warning (c);
7075 expr = integer_zerop (c) ? *e3_p : *e2_p;
7076 break;
7077 }
7078 case RID_TYPES_COMPATIBLE_P:
7079 c_parser_consume_token (parser);
7080 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7081 {
7082 expr.value = error_mark_node;
7083 break;
7084 }
7085 t1 = c_parser_type_name (parser);
7086 if (t1 == NULL)
7087 {
7088 expr.value = error_mark_node;
7089 break;
7090 }
7091 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
7092 {
7093 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7094 expr.value = error_mark_node;
7095 break;
7096 }
7097 t2 = c_parser_type_name (parser);
7098 if (t2 == NULL)
7099 {
7100 expr.value = error_mark_node;
7101 break;
7102 }
7103 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7104 "expected %<)%>");
7105 {
7106 tree e1, e2;
7107 e1 = groktypename (t1, NULL, NULL);
7108 e2 = groktypename (t2, NULL, NULL);
7109 if (e1 == error_mark_node || e2 == error_mark_node)
7110 {
7111 expr.value = error_mark_node;
7112 break;
7113 }
7114
7115 e1 = TYPE_MAIN_VARIANT (e1);
7116 e2 = TYPE_MAIN_VARIANT (e2);
7117
7118 expr.value
7119 = comptypes (e1, e2) ? integer_one_node : integer_zero_node;
7120 }
7121 break;
7122 case RID_BUILTIN_COMPLEX:
7123 {
7124 vec<c_expr_t, va_gc> *cexpr_list;
7125 c_expr_t *e1_p, *e2_p;
7126
7127 c_parser_consume_token (parser);
7128 if (!c_parser_get_builtin_args (parser,
7129 "__builtin_complex",
7130 &cexpr_list, false))
7131 {
7132 expr.value = error_mark_node;
7133 break;
7134 }
7135
7136 if (vec_safe_length (cexpr_list) != 2)
7137 {
7138 error_at (loc, "wrong number of arguments to "
7139 "%<__builtin_complex%>");
7140 expr.value = error_mark_node;
7141 break;
7142 }
7143
7144 e1_p = &(*cexpr_list)[0];
7145 e2_p = &(*cexpr_list)[1];
7146
7147 *e1_p = convert_lvalue_to_rvalue (loc, *e1_p, true, true);
7148 if (TREE_CODE (e1_p->value) == EXCESS_PRECISION_EXPR)
7149 e1_p->value = convert (TREE_TYPE (e1_p->value),
7150 TREE_OPERAND (e1_p->value, 0));
7151 *e2_p = convert_lvalue_to_rvalue (loc, *e2_p, true, true);
7152 if (TREE_CODE (e2_p->value) == EXCESS_PRECISION_EXPR)
7153 e2_p->value = convert (TREE_TYPE (e2_p->value),
7154 TREE_OPERAND (e2_p->value, 0));
7155 if (!SCALAR_FLOAT_TYPE_P (TREE_TYPE (e1_p->value))
7156 || DECIMAL_FLOAT_TYPE_P (TREE_TYPE (e1_p->value))
7157 || !SCALAR_FLOAT_TYPE_P (TREE_TYPE (e2_p->value))
7158 || DECIMAL_FLOAT_TYPE_P (TREE_TYPE (e2_p->value)))
7159 {
7160 error_at (loc, "%<__builtin_complex%> operand "
7161 "not of real binary floating-point type");
7162 expr.value = error_mark_node;
7163 break;
7164 }
7165 if (TYPE_MAIN_VARIANT (TREE_TYPE (e1_p->value))
7166 != TYPE_MAIN_VARIANT (TREE_TYPE (e2_p->value)))
7167 {
7168 error_at (loc,
7169 "%<__builtin_complex%> operands of different types");
7170 expr.value = error_mark_node;
7171 break;
7172 }
7173 if (!flag_isoc99)
7174 pedwarn (loc, OPT_Wpedantic,
7175 "ISO C90 does not support complex types");
7176 expr.value = build2 (COMPLEX_EXPR,
7177 build_complex_type
7178 (TYPE_MAIN_VARIANT
7179 (TREE_TYPE (e1_p->value))),
7180 e1_p->value, e2_p->value);
7181 break;
7182 }
7183 case RID_BUILTIN_SHUFFLE:
7184 {
7185 vec<c_expr_t, va_gc> *cexpr_list;
7186 unsigned int i;
7187 c_expr_t *p;
7188
7189 c_parser_consume_token (parser);
7190 if (!c_parser_get_builtin_args (parser,
7191 "__builtin_shuffle",
7192 &cexpr_list, false))
7193 {
7194 expr.value = error_mark_node;
7195 break;
7196 }
7197
7198 FOR_EACH_VEC_SAFE_ELT (cexpr_list, i, p)
7199 *p = convert_lvalue_to_rvalue (loc, *p, true, true);
7200
7201 if (vec_safe_length (cexpr_list) == 2)
7202 expr.value =
7203 c_build_vec_perm_expr
7204 (loc, (*cexpr_list)[0].value,
7205 NULL_TREE, (*cexpr_list)[1].value);
7206
7207 else if (vec_safe_length (cexpr_list) == 3)
7208 expr.value =
7209 c_build_vec_perm_expr
7210 (loc, (*cexpr_list)[0].value,
7211 (*cexpr_list)[1].value,
7212 (*cexpr_list)[2].value);
7213 else
7214 {
7215 error_at (loc, "wrong number of arguments to "
7216 "%<__builtin_shuffle%>");
7217 expr.value = error_mark_node;
7218 }
7219 break;
7220 }
7221 case RID_AT_SELECTOR:
7222 gcc_assert (c_dialect_objc ());
7223 c_parser_consume_token (parser);
7224 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7225 {
7226 expr.value = error_mark_node;
7227 break;
7228 }
7229 {
7230 tree sel = c_parser_objc_selector_arg (parser);
7231 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7232 "expected %<)%>");
7233 expr.value = objc_build_selector_expr (loc, sel);
7234 }
7235 break;
7236 case RID_AT_PROTOCOL:
7237 gcc_assert (c_dialect_objc ());
7238 c_parser_consume_token (parser);
7239 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7240 {
7241 expr.value = error_mark_node;
7242 break;
7243 }
7244 if (c_parser_next_token_is_not (parser, CPP_NAME))
7245 {
7246 c_parser_error (parser, "expected identifier");
7247 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7248 expr.value = error_mark_node;
7249 break;
7250 }
7251 {
7252 tree id = c_parser_peek_token (parser)->value;
7253 c_parser_consume_token (parser);
7254 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7255 "expected %<)%>");
7256 expr.value = objc_build_protocol_expr (id);
7257 }
7258 break;
7259 case RID_AT_ENCODE:
7260 /* Extension to support C-structures in the archiver. */
7261 gcc_assert (c_dialect_objc ());
7262 c_parser_consume_token (parser);
7263 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7264 {
7265 expr.value = error_mark_node;
7266 break;
7267 }
7268 t1 = c_parser_type_name (parser);
7269 if (t1 == NULL)
7270 {
7271 expr.value = error_mark_node;
7272 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7273 break;
7274 }
7275 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7276 "expected %<)%>");
7277 {
7278 tree type = groktypename (t1, NULL, NULL);
7279 expr.value = objc_build_encode_expr (type);
7280 }
7281 break;
7282 case RID_GENERIC:
7283 expr = c_parser_generic_selection (parser);
7284 break;
7285 case RID_CILK_SPAWN:
7286 c_parser_consume_token (parser);
7287 if (!flag_enable_cilkplus)
7288 {
7289 error_at (loc, "-fcilkplus must be enabled to use "
7290 "%<_Cilk_spawn%>");
7291 expr = c_parser_postfix_expression (parser);
7292 expr.value = error_mark_node;
7293 }
7294 if (c_parser_peek_token (parser)->keyword == RID_CILK_SPAWN)
7295 {
7296 error_at (loc, "consecutive %<_Cilk_spawn%> keywords "
7297 "are not permitted");
7298 /* Now flush out all the _Cilk_spawns. */
7299 while (c_parser_peek_token (parser)->keyword == RID_CILK_SPAWN)
7300 c_parser_consume_token (parser);
7301 expr = c_parser_postfix_expression (parser);
7302 }
7303 else
7304 {
7305 expr = c_parser_postfix_expression (parser);
7306 expr.value = build_cilk_spawn (loc, expr.value);
7307 }
7308 break;
7309 default:
7310 c_parser_error (parser, "expected expression");
7311 expr.value = error_mark_node;
7312 break;
7313 }
7314 break;
7315 case CPP_OPEN_SQUARE:
7316 if (c_dialect_objc ())
7317 {
7318 tree receiver, args;
7319 c_parser_consume_token (parser);
7320 receiver = c_parser_objc_receiver (parser);
7321 args = c_parser_objc_message_args (parser);
7322 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
7323 "expected %<]%>");
7324 expr.value = objc_build_message_expr (receiver, args);
7325 break;
7326 }
7327 /* Else fall through to report error. */
7328 default:
7329 c_parser_error (parser, "expected expression");
7330 expr.value = error_mark_node;
7331 break;
7332 }
7333 return c_parser_postfix_expression_after_primary (parser, loc, expr);
7334 }
7335
7336 /* Parse a postfix expression after a parenthesized type name: the
7337 brace-enclosed initializer of a compound literal, possibly followed
7338 by some postfix operators. This is separate because it is not
7339 possible to tell until after the type name whether a cast
7340 expression has a cast or a compound literal, or whether the operand
7341 of sizeof is a parenthesized type name or starts with a compound
7342 literal. TYPE_LOC is the location where TYPE_NAME starts--the
7343 location of the first token after the parentheses around the type
7344 name. */
7345
7346 static struct c_expr
7347 c_parser_postfix_expression_after_paren_type (c_parser *parser,
7348 struct c_type_name *type_name,
7349 location_t type_loc)
7350 {
7351 tree type;
7352 struct c_expr init;
7353 bool non_const;
7354 struct c_expr expr;
7355 location_t start_loc;
7356 tree type_expr = NULL_TREE;
7357 bool type_expr_const = true;
7358 check_compound_literal_type (type_loc, type_name);
7359 start_init (NULL_TREE, NULL, 0);
7360 type = groktypename (type_name, &type_expr, &type_expr_const);
7361 start_loc = c_parser_peek_token (parser)->location;
7362 if (type != error_mark_node && C_TYPE_VARIABLE_SIZE (type))
7363 {
7364 error_at (type_loc, "compound literal has variable size");
7365 type = error_mark_node;
7366 }
7367 init = c_parser_braced_init (parser, type, false);
7368 finish_init ();
7369 maybe_warn_string_init (type, init);
7370
7371 if (type != error_mark_node
7372 && !ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (type))
7373 && current_function_decl)
7374 {
7375 error ("compound literal qualified by address-space qualifier");
7376 type = error_mark_node;
7377 }
7378
7379 if (!flag_isoc99)
7380 pedwarn (start_loc, OPT_Wpedantic, "ISO C90 forbids compound literals");
7381 non_const = ((init.value && TREE_CODE (init.value) == CONSTRUCTOR)
7382 ? CONSTRUCTOR_NON_CONST (init.value)
7383 : init.original_code == C_MAYBE_CONST_EXPR);
7384 non_const |= !type_expr_const;
7385 expr.value = build_compound_literal (start_loc, type, init.value, non_const);
7386 expr.original_code = ERROR_MARK;
7387 expr.original_type = NULL;
7388 if (type_expr)
7389 {
7390 if (TREE_CODE (expr.value) == C_MAYBE_CONST_EXPR)
7391 {
7392 gcc_assert (C_MAYBE_CONST_EXPR_PRE (expr.value) == NULL_TREE);
7393 C_MAYBE_CONST_EXPR_PRE (expr.value) = type_expr;
7394 }
7395 else
7396 {
7397 gcc_assert (!non_const);
7398 expr.value = build2 (C_MAYBE_CONST_EXPR, type,
7399 type_expr, expr.value);
7400 }
7401 }
7402 return c_parser_postfix_expression_after_primary (parser, start_loc, expr);
7403 }
7404
7405 /* Callback function for sizeof_pointer_memaccess_warning to compare
7406 types. */
7407
7408 static bool
7409 sizeof_ptr_memacc_comptypes (tree type1, tree type2)
7410 {
7411 return comptypes (type1, type2) == 1;
7412 }
7413
7414 /* Parse a postfix expression after the initial primary or compound
7415 literal; that is, parse a series of postfix operators.
7416
7417 EXPR_LOC is the location of the primary expression. */
7418
7419 static struct c_expr
7420 c_parser_postfix_expression_after_primary (c_parser *parser,
7421 location_t expr_loc,
7422 struct c_expr expr)
7423 {
7424 struct c_expr orig_expr;
7425 tree ident, idx;
7426 location_t sizeof_arg_loc[3];
7427 tree sizeof_arg[3];
7428 unsigned int i;
7429 vec<tree, va_gc> *exprlist;
7430 vec<tree, va_gc> *origtypes = NULL;
7431 while (true)
7432 {
7433 location_t op_loc = c_parser_peek_token (parser)->location;
7434 switch (c_parser_peek_token (parser)->type)
7435 {
7436 case CPP_OPEN_SQUARE:
7437 /* Array reference. */
7438 c_parser_consume_token (parser);
7439 if (flag_enable_cilkplus
7440 && c_parser_peek_token (parser)->type == CPP_COLON)
7441 /* If we are here, then we have something like this:
7442 Array [ : ]
7443 */
7444 expr.value = c_parser_array_notation (expr_loc, parser, NULL_TREE,
7445 expr.value);
7446 else
7447 {
7448 idx = c_parser_expression (parser).value;
7449 /* Here we have 3 options:
7450 1. Array [EXPR] -- Normal Array call.
7451 2. Array [EXPR : EXPR] -- Array notation without stride.
7452 3. Array [EXPR : EXPR : EXPR] -- Array notation with stride.
7453
7454 For 1, we just handle it just like a normal array expression.
7455 For 2 and 3 we handle it like we handle array notations. The
7456 idx value we have above becomes the initial/start index.
7457 */
7458 if (flag_enable_cilkplus
7459 && c_parser_peek_token (parser)->type == CPP_COLON)
7460 expr.value = c_parser_array_notation (expr_loc, parser, idx,
7461 expr.value);
7462 else
7463 {
7464 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
7465 "expected %<]%>");
7466 expr.value = build_array_ref (op_loc, expr.value, idx);
7467 }
7468 }
7469 expr.original_code = ERROR_MARK;
7470 expr.original_type = NULL;
7471 break;
7472 case CPP_OPEN_PAREN:
7473 /* Function call. */
7474 c_parser_consume_token (parser);
7475 for (i = 0; i < 3; i++)
7476 {
7477 sizeof_arg[i] = NULL_TREE;
7478 sizeof_arg_loc[i] = UNKNOWN_LOCATION;
7479 }
7480 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
7481 exprlist = NULL;
7482 else
7483 exprlist = c_parser_expr_list (parser, true, false, &origtypes,
7484 sizeof_arg_loc, sizeof_arg);
7485 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7486 "expected %<)%>");
7487 orig_expr = expr;
7488 mark_exp_read (expr.value);
7489 if (warn_sizeof_pointer_memaccess)
7490 sizeof_pointer_memaccess_warning (sizeof_arg_loc,
7491 expr.value, exprlist,
7492 sizeof_arg,
7493 sizeof_ptr_memacc_comptypes);
7494 /* FIXME diagnostics: Ideally we want the FUNCNAME, not the
7495 "(" after the FUNCNAME, which is what we have now. */
7496 expr.value = build_function_call_vec (op_loc, expr.value, exprlist,
7497 origtypes);
7498 expr.original_code = ERROR_MARK;
7499 if (TREE_CODE (expr.value) == INTEGER_CST
7500 && TREE_CODE (orig_expr.value) == FUNCTION_DECL
7501 && DECL_BUILT_IN_CLASS (orig_expr.value) == BUILT_IN_NORMAL
7502 && DECL_FUNCTION_CODE (orig_expr.value) == BUILT_IN_CONSTANT_P)
7503 expr.original_code = C_MAYBE_CONST_EXPR;
7504 expr.original_type = NULL;
7505 if (exprlist)
7506 {
7507 release_tree_vector (exprlist);
7508 release_tree_vector (origtypes);
7509 }
7510 break;
7511 case CPP_DOT:
7512 /* Structure element reference. */
7513 c_parser_consume_token (parser);
7514 expr = default_function_array_conversion (expr_loc, expr);
7515 if (c_parser_next_token_is (parser, CPP_NAME))
7516 ident = c_parser_peek_token (parser)->value;
7517 else
7518 {
7519 c_parser_error (parser, "expected identifier");
7520 expr.value = error_mark_node;
7521 expr.original_code = ERROR_MARK;
7522 expr.original_type = NULL;
7523 return expr;
7524 }
7525 c_parser_consume_token (parser);
7526 expr.value = build_component_ref (op_loc, expr.value, ident);
7527 expr.original_code = ERROR_MARK;
7528 if (TREE_CODE (expr.value) != COMPONENT_REF)
7529 expr.original_type = NULL;
7530 else
7531 {
7532 /* Remember the original type of a bitfield. */
7533 tree field = TREE_OPERAND (expr.value, 1);
7534 if (TREE_CODE (field) != FIELD_DECL)
7535 expr.original_type = NULL;
7536 else
7537 expr.original_type = DECL_BIT_FIELD_TYPE (field);
7538 }
7539 break;
7540 case CPP_DEREF:
7541 /* Structure element reference. */
7542 c_parser_consume_token (parser);
7543 expr = convert_lvalue_to_rvalue (expr_loc, expr, true, false);
7544 if (c_parser_next_token_is (parser, CPP_NAME))
7545 ident = c_parser_peek_token (parser)->value;
7546 else
7547 {
7548 c_parser_error (parser, "expected identifier");
7549 expr.value = error_mark_node;
7550 expr.original_code = ERROR_MARK;
7551 expr.original_type = NULL;
7552 return expr;
7553 }
7554 c_parser_consume_token (parser);
7555 expr.value = build_component_ref (op_loc,
7556 build_indirect_ref (op_loc,
7557 expr.value,
7558 RO_ARROW),
7559 ident);
7560 expr.original_code = ERROR_MARK;
7561 if (TREE_CODE (expr.value) != COMPONENT_REF)
7562 expr.original_type = NULL;
7563 else
7564 {
7565 /* Remember the original type of a bitfield. */
7566 tree field = TREE_OPERAND (expr.value, 1);
7567 if (TREE_CODE (field) != FIELD_DECL)
7568 expr.original_type = NULL;
7569 else
7570 expr.original_type = DECL_BIT_FIELD_TYPE (field);
7571 }
7572 break;
7573 case CPP_PLUS_PLUS:
7574 /* Postincrement. */
7575 c_parser_consume_token (parser);
7576 /* If the expressions have array notations, we expand them. */
7577 if (flag_enable_cilkplus
7578 && TREE_CODE (expr.value) == ARRAY_NOTATION_REF)
7579 expr = fix_array_notation_expr (expr_loc, POSTINCREMENT_EXPR, expr);
7580 else
7581 {
7582 expr = default_function_array_read_conversion (expr_loc, expr);
7583 expr.value = build_unary_op (op_loc,
7584 POSTINCREMENT_EXPR, expr.value, 0);
7585 }
7586 expr.original_code = ERROR_MARK;
7587 expr.original_type = NULL;
7588 break;
7589 case CPP_MINUS_MINUS:
7590 /* Postdecrement. */
7591 c_parser_consume_token (parser);
7592 /* If the expressions have array notations, we expand them. */
7593 if (flag_enable_cilkplus
7594 && TREE_CODE (expr.value) == ARRAY_NOTATION_REF)
7595 expr = fix_array_notation_expr (expr_loc, POSTDECREMENT_EXPR, expr);
7596 else
7597 {
7598 expr = default_function_array_read_conversion (expr_loc, expr);
7599 expr.value = build_unary_op (op_loc,
7600 POSTDECREMENT_EXPR, expr.value, 0);
7601 }
7602 expr.original_code = ERROR_MARK;
7603 expr.original_type = NULL;
7604 break;
7605 default:
7606 return expr;
7607 }
7608 }
7609 }
7610
7611 /* Parse an expression (C90 6.3.17, C99 6.5.17).
7612
7613 expression:
7614 assignment-expression
7615 expression , assignment-expression
7616 */
7617
7618 static struct c_expr
7619 c_parser_expression (c_parser *parser)
7620 {
7621 location_t tloc = c_parser_peek_token (parser)->location;
7622 struct c_expr expr;
7623 expr = c_parser_expr_no_commas (parser, NULL);
7624 if (c_parser_next_token_is (parser, CPP_COMMA))
7625 expr = convert_lvalue_to_rvalue (tloc, expr, true, false);
7626 while (c_parser_next_token_is (parser, CPP_COMMA))
7627 {
7628 struct c_expr next;
7629 tree lhsval;
7630 location_t loc = c_parser_peek_token (parser)->location;
7631 location_t expr_loc;
7632 c_parser_consume_token (parser);
7633 expr_loc = c_parser_peek_token (parser)->location;
7634 lhsval = expr.value;
7635 while (TREE_CODE (lhsval) == COMPOUND_EXPR)
7636 lhsval = TREE_OPERAND (lhsval, 1);
7637 if (DECL_P (lhsval) || handled_component_p (lhsval))
7638 mark_exp_read (lhsval);
7639 next = c_parser_expr_no_commas (parser, NULL);
7640 next = convert_lvalue_to_rvalue (expr_loc, next, true, false);
7641 expr.value = build_compound_expr (loc, expr.value, next.value);
7642 expr.original_code = COMPOUND_EXPR;
7643 expr.original_type = next.original_type;
7644 }
7645 return expr;
7646 }
7647
7648 /* Parse an expression and convert functions or arrays to pointers and
7649 lvalues to rvalues. */
7650
7651 static struct c_expr
7652 c_parser_expression_conv (c_parser *parser)
7653 {
7654 struct c_expr expr;
7655 location_t loc = c_parser_peek_token (parser)->location;
7656 expr = c_parser_expression (parser);
7657 expr = convert_lvalue_to_rvalue (loc, expr, true, false);
7658 return expr;
7659 }
7660
7661 /* Parse a non-empty list of expressions. If CONVERT_P, convert
7662 functions and arrays to pointers and lvalues to rvalues. If
7663 FOLD_P, fold the expressions.
7664
7665 nonempty-expr-list:
7666 assignment-expression
7667 nonempty-expr-list , assignment-expression
7668 */
7669
7670 static vec<tree, va_gc> *
7671 c_parser_expr_list (c_parser *parser, bool convert_p, bool fold_p,
7672 vec<tree, va_gc> **p_orig_types,
7673 location_t *sizeof_arg_loc, tree *sizeof_arg)
7674 {
7675 vec<tree, va_gc> *ret;
7676 vec<tree, va_gc> *orig_types;
7677 struct c_expr expr;
7678 location_t loc = c_parser_peek_token (parser)->location;
7679 location_t cur_sizeof_arg_loc = UNKNOWN_LOCATION;
7680 unsigned int idx = 0;
7681
7682 ret = make_tree_vector ();
7683 if (p_orig_types == NULL)
7684 orig_types = NULL;
7685 else
7686 orig_types = make_tree_vector ();
7687
7688 if (sizeof_arg != NULL
7689 && c_parser_next_token_is_keyword (parser, RID_SIZEOF))
7690 cur_sizeof_arg_loc = c_parser_peek_2nd_token (parser)->location;
7691 expr = c_parser_expr_no_commas (parser, NULL);
7692 if (convert_p)
7693 expr = convert_lvalue_to_rvalue (loc, expr, true, true);
7694 if (fold_p)
7695 expr.value = c_fully_fold (expr.value, false, NULL);
7696 ret->quick_push (expr.value);
7697 if (orig_types)
7698 orig_types->quick_push (expr.original_type);
7699 if (sizeof_arg != NULL
7700 && cur_sizeof_arg_loc != UNKNOWN_LOCATION
7701 && expr.original_code == SIZEOF_EXPR)
7702 {
7703 sizeof_arg[0] = c_last_sizeof_arg;
7704 sizeof_arg_loc[0] = cur_sizeof_arg_loc;
7705 }
7706 while (c_parser_next_token_is (parser, CPP_COMMA))
7707 {
7708 c_parser_consume_token (parser);
7709 loc = c_parser_peek_token (parser)->location;
7710 if (sizeof_arg != NULL
7711 && c_parser_next_token_is_keyword (parser, RID_SIZEOF))
7712 cur_sizeof_arg_loc = c_parser_peek_2nd_token (parser)->location;
7713 else
7714 cur_sizeof_arg_loc = UNKNOWN_LOCATION;
7715 expr = c_parser_expr_no_commas (parser, NULL);
7716 if (convert_p)
7717 expr = convert_lvalue_to_rvalue (loc, expr, true, true);
7718 if (fold_p)
7719 expr.value = c_fully_fold (expr.value, false, NULL);
7720 vec_safe_push (ret, expr.value);
7721 if (orig_types)
7722 vec_safe_push (orig_types, expr.original_type);
7723 if (++idx < 3
7724 && sizeof_arg != NULL
7725 && cur_sizeof_arg_loc != UNKNOWN_LOCATION
7726 && expr.original_code == SIZEOF_EXPR)
7727 {
7728 sizeof_arg[idx] = c_last_sizeof_arg;
7729 sizeof_arg_loc[idx] = cur_sizeof_arg_loc;
7730 }
7731 }
7732 if (orig_types)
7733 *p_orig_types = orig_types;
7734 return ret;
7735 }
7736 \f
7737 /* Parse Objective-C-specific constructs. */
7738
7739 /* Parse an objc-class-definition.
7740
7741 objc-class-definition:
7742 @interface identifier objc-superclass[opt] objc-protocol-refs[opt]
7743 objc-class-instance-variables[opt] objc-methodprotolist @end
7744 @implementation identifier objc-superclass[opt]
7745 objc-class-instance-variables[opt]
7746 @interface identifier ( identifier ) objc-protocol-refs[opt]
7747 objc-methodprotolist @end
7748 @interface identifier ( ) objc-protocol-refs[opt]
7749 objc-methodprotolist @end
7750 @implementation identifier ( identifier )
7751
7752 objc-superclass:
7753 : identifier
7754
7755 "@interface identifier (" must start "@interface identifier (
7756 identifier ) ...": objc-methodprotolist in the first production may
7757 not start with a parenthesized identifier as a declarator of a data
7758 definition with no declaration specifiers if the objc-superclass,
7759 objc-protocol-refs and objc-class-instance-variables are omitted. */
7760
7761 static void
7762 c_parser_objc_class_definition (c_parser *parser, tree attributes)
7763 {
7764 bool iface_p;
7765 tree id1;
7766 tree superclass;
7767 if (c_parser_next_token_is_keyword (parser, RID_AT_INTERFACE))
7768 iface_p = true;
7769 else if (c_parser_next_token_is_keyword (parser, RID_AT_IMPLEMENTATION))
7770 iface_p = false;
7771 else
7772 gcc_unreachable ();
7773
7774 c_parser_consume_token (parser);
7775 if (c_parser_next_token_is_not (parser, CPP_NAME))
7776 {
7777 c_parser_error (parser, "expected identifier");
7778 return;
7779 }
7780 id1 = c_parser_peek_token (parser)->value;
7781 c_parser_consume_token (parser);
7782 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
7783 {
7784 /* We have a category or class extension. */
7785 tree id2;
7786 tree proto = NULL_TREE;
7787 c_parser_consume_token (parser);
7788 if (c_parser_next_token_is_not (parser, CPP_NAME))
7789 {
7790 if (iface_p && c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
7791 {
7792 /* We have a class extension. */
7793 id2 = NULL_TREE;
7794 }
7795 else
7796 {
7797 c_parser_error (parser, "expected identifier or %<)%>");
7798 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7799 return;
7800 }
7801 }
7802 else
7803 {
7804 id2 = c_parser_peek_token (parser)->value;
7805 c_parser_consume_token (parser);
7806 }
7807 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
7808 if (!iface_p)
7809 {
7810 objc_start_category_implementation (id1, id2);
7811 return;
7812 }
7813 if (c_parser_next_token_is (parser, CPP_LESS))
7814 proto = c_parser_objc_protocol_refs (parser);
7815 objc_start_category_interface (id1, id2, proto, attributes);
7816 c_parser_objc_methodprotolist (parser);
7817 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
7818 objc_finish_interface ();
7819 return;
7820 }
7821 if (c_parser_next_token_is (parser, CPP_COLON))
7822 {
7823 c_parser_consume_token (parser);
7824 if (c_parser_next_token_is_not (parser, CPP_NAME))
7825 {
7826 c_parser_error (parser, "expected identifier");
7827 return;
7828 }
7829 superclass = c_parser_peek_token (parser)->value;
7830 c_parser_consume_token (parser);
7831 }
7832 else
7833 superclass = NULL_TREE;
7834 if (iface_p)
7835 {
7836 tree proto = NULL_TREE;
7837 if (c_parser_next_token_is (parser, CPP_LESS))
7838 proto = c_parser_objc_protocol_refs (parser);
7839 objc_start_class_interface (id1, superclass, proto, attributes);
7840 }
7841 else
7842 objc_start_class_implementation (id1, superclass);
7843 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
7844 c_parser_objc_class_instance_variables (parser);
7845 if (iface_p)
7846 {
7847 objc_continue_interface ();
7848 c_parser_objc_methodprotolist (parser);
7849 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
7850 objc_finish_interface ();
7851 }
7852 else
7853 {
7854 objc_continue_implementation ();
7855 return;
7856 }
7857 }
7858
7859 /* Parse objc-class-instance-variables.
7860
7861 objc-class-instance-variables:
7862 { objc-instance-variable-decl-list[opt] }
7863
7864 objc-instance-variable-decl-list:
7865 objc-visibility-spec
7866 objc-instance-variable-decl ;
7867 ;
7868 objc-instance-variable-decl-list objc-visibility-spec
7869 objc-instance-variable-decl-list objc-instance-variable-decl ;
7870 objc-instance-variable-decl-list ;
7871
7872 objc-visibility-spec:
7873 @private
7874 @protected
7875 @public
7876
7877 objc-instance-variable-decl:
7878 struct-declaration
7879 */
7880
7881 static void
7882 c_parser_objc_class_instance_variables (c_parser *parser)
7883 {
7884 gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE));
7885 c_parser_consume_token (parser);
7886 while (c_parser_next_token_is_not (parser, CPP_EOF))
7887 {
7888 tree decls;
7889 /* Parse any stray semicolon. */
7890 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
7891 {
7892 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
7893 "extra semicolon");
7894 c_parser_consume_token (parser);
7895 continue;
7896 }
7897 /* Stop if at the end of the instance variables. */
7898 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
7899 {
7900 c_parser_consume_token (parser);
7901 break;
7902 }
7903 /* Parse any objc-visibility-spec. */
7904 if (c_parser_next_token_is_keyword (parser, RID_AT_PRIVATE))
7905 {
7906 c_parser_consume_token (parser);
7907 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
7908 continue;
7909 }
7910 else if (c_parser_next_token_is_keyword (parser, RID_AT_PROTECTED))
7911 {
7912 c_parser_consume_token (parser);
7913 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
7914 continue;
7915 }
7916 else if (c_parser_next_token_is_keyword (parser, RID_AT_PUBLIC))
7917 {
7918 c_parser_consume_token (parser);
7919 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
7920 continue;
7921 }
7922 else if (c_parser_next_token_is_keyword (parser, RID_AT_PACKAGE))
7923 {
7924 c_parser_consume_token (parser);
7925 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
7926 continue;
7927 }
7928 else if (c_parser_next_token_is (parser, CPP_PRAGMA))
7929 {
7930 c_parser_pragma (parser, pragma_external);
7931 continue;
7932 }
7933
7934 /* Parse some comma-separated declarations. */
7935 decls = c_parser_struct_declaration (parser);
7936 if (decls == NULL)
7937 {
7938 /* There is a syntax error. We want to skip the offending
7939 tokens up to the next ';' (included) or '}'
7940 (excluded). */
7941
7942 /* First, skip manually a ')' or ']'. This is because they
7943 reduce the nesting level, so c_parser_skip_until_found()
7944 wouldn't be able to skip past them. */
7945 c_token *token = c_parser_peek_token (parser);
7946 if (token->type == CPP_CLOSE_PAREN || token->type == CPP_CLOSE_SQUARE)
7947 c_parser_consume_token (parser);
7948
7949 /* Then, do the standard skipping. */
7950 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
7951
7952 /* We hopefully recovered. Start normal parsing again. */
7953 parser->error = false;
7954 continue;
7955 }
7956 else
7957 {
7958 /* Comma-separated instance variables are chained together
7959 in reverse order; add them one by one. */
7960 tree ivar = nreverse (decls);
7961 for (; ivar; ivar = DECL_CHAIN (ivar))
7962 objc_add_instance_variable (copy_node (ivar));
7963 }
7964 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
7965 }
7966 }
7967
7968 /* Parse an objc-class-declaration.
7969
7970 objc-class-declaration:
7971 @class identifier-list ;
7972 */
7973
7974 static void
7975 c_parser_objc_class_declaration (c_parser *parser)
7976 {
7977 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_CLASS));
7978 c_parser_consume_token (parser);
7979 /* Any identifiers, including those declared as type names, are OK
7980 here. */
7981 while (true)
7982 {
7983 tree id;
7984 if (c_parser_next_token_is_not (parser, CPP_NAME))
7985 {
7986 c_parser_error (parser, "expected identifier");
7987 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
7988 parser->error = false;
7989 return;
7990 }
7991 id = c_parser_peek_token (parser)->value;
7992 objc_declare_class (id);
7993 c_parser_consume_token (parser);
7994 if (c_parser_next_token_is (parser, CPP_COMMA))
7995 c_parser_consume_token (parser);
7996 else
7997 break;
7998 }
7999 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
8000 }
8001
8002 /* Parse an objc-alias-declaration.
8003
8004 objc-alias-declaration:
8005 @compatibility_alias identifier identifier ;
8006 */
8007
8008 static void
8009 c_parser_objc_alias_declaration (c_parser *parser)
8010 {
8011 tree id1, id2;
8012 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_ALIAS));
8013 c_parser_consume_token (parser);
8014 if (c_parser_next_token_is_not (parser, CPP_NAME))
8015 {
8016 c_parser_error (parser, "expected identifier");
8017 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
8018 return;
8019 }
8020 id1 = c_parser_peek_token (parser)->value;
8021 c_parser_consume_token (parser);
8022 if (c_parser_next_token_is_not (parser, CPP_NAME))
8023 {
8024 c_parser_error (parser, "expected identifier");
8025 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
8026 return;
8027 }
8028 id2 = c_parser_peek_token (parser)->value;
8029 c_parser_consume_token (parser);
8030 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
8031 objc_declare_alias (id1, id2);
8032 }
8033
8034 /* Parse an objc-protocol-definition.
8035
8036 objc-protocol-definition:
8037 @protocol identifier objc-protocol-refs[opt] objc-methodprotolist @end
8038 @protocol identifier-list ;
8039
8040 "@protocol identifier ;" should be resolved as "@protocol
8041 identifier-list ;": objc-methodprotolist may not start with a
8042 semicolon in the first alternative if objc-protocol-refs are
8043 omitted. */
8044
8045 static void
8046 c_parser_objc_protocol_definition (c_parser *parser, tree attributes)
8047 {
8048 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_PROTOCOL));
8049
8050 c_parser_consume_token (parser);
8051 if (c_parser_next_token_is_not (parser, CPP_NAME))
8052 {
8053 c_parser_error (parser, "expected identifier");
8054 return;
8055 }
8056 if (c_parser_peek_2nd_token (parser)->type == CPP_COMMA
8057 || c_parser_peek_2nd_token (parser)->type == CPP_SEMICOLON)
8058 {
8059 /* Any identifiers, including those declared as type names, are
8060 OK here. */
8061 while (true)
8062 {
8063 tree id;
8064 if (c_parser_next_token_is_not (parser, CPP_NAME))
8065 {
8066 c_parser_error (parser, "expected identifier");
8067 break;
8068 }
8069 id = c_parser_peek_token (parser)->value;
8070 objc_declare_protocol (id, attributes);
8071 c_parser_consume_token (parser);
8072 if (c_parser_next_token_is (parser, CPP_COMMA))
8073 c_parser_consume_token (parser);
8074 else
8075 break;
8076 }
8077 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
8078 }
8079 else
8080 {
8081 tree id = c_parser_peek_token (parser)->value;
8082 tree proto = NULL_TREE;
8083 c_parser_consume_token (parser);
8084 if (c_parser_next_token_is (parser, CPP_LESS))
8085 proto = c_parser_objc_protocol_refs (parser);
8086 parser->objc_pq_context = true;
8087 objc_start_protocol (id, proto, attributes);
8088 c_parser_objc_methodprotolist (parser);
8089 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
8090 parser->objc_pq_context = false;
8091 objc_finish_interface ();
8092 }
8093 }
8094
8095 /* Parse an objc-method-type.
8096
8097 objc-method-type:
8098 +
8099 -
8100
8101 Return true if it is a class method (+) and false if it is
8102 an instance method (-).
8103 */
8104 static inline bool
8105 c_parser_objc_method_type (c_parser *parser)
8106 {
8107 switch (c_parser_peek_token (parser)->type)
8108 {
8109 case CPP_PLUS:
8110 c_parser_consume_token (parser);
8111 return true;
8112 case CPP_MINUS:
8113 c_parser_consume_token (parser);
8114 return false;
8115 default:
8116 gcc_unreachable ();
8117 }
8118 }
8119
8120 /* Parse an objc-method-definition.
8121
8122 objc-method-definition:
8123 objc-method-type objc-method-decl ;[opt] compound-statement
8124 */
8125
8126 static void
8127 c_parser_objc_method_definition (c_parser *parser)
8128 {
8129 bool is_class_method = c_parser_objc_method_type (parser);
8130 tree decl, attributes = NULL_TREE, expr = NULL_TREE;
8131 parser->objc_pq_context = true;
8132 decl = c_parser_objc_method_decl (parser, is_class_method, &attributes,
8133 &expr);
8134 if (decl == error_mark_node)
8135 return; /* Bail here. */
8136
8137 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
8138 {
8139 c_parser_consume_token (parser);
8140 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
8141 "extra semicolon in method definition specified");
8142 }
8143
8144 if (!c_parser_next_token_is (parser, CPP_OPEN_BRACE))
8145 {
8146 c_parser_error (parser, "expected %<{%>");
8147 return;
8148 }
8149
8150 parser->objc_pq_context = false;
8151 if (objc_start_method_definition (is_class_method, decl, attributes, expr))
8152 {
8153 add_stmt (c_parser_compound_statement (parser));
8154 objc_finish_method_definition (current_function_decl);
8155 }
8156 else
8157 {
8158 /* This code is executed when we find a method definition
8159 outside of an @implementation context (or invalid for other
8160 reasons). Parse the method (to keep going) but do not emit
8161 any code.
8162 */
8163 c_parser_compound_statement (parser);
8164 }
8165 }
8166
8167 /* Parse an objc-methodprotolist.
8168
8169 objc-methodprotolist:
8170 empty
8171 objc-methodprotolist objc-methodproto
8172 objc-methodprotolist declaration
8173 objc-methodprotolist ;
8174 @optional
8175 @required
8176
8177 The declaration is a data definition, which may be missing
8178 declaration specifiers under the same rules and diagnostics as
8179 other data definitions outside functions, and the stray semicolon
8180 is diagnosed the same way as a stray semicolon outside a
8181 function. */
8182
8183 static void
8184 c_parser_objc_methodprotolist (c_parser *parser)
8185 {
8186 while (true)
8187 {
8188 /* The list is terminated by @end. */
8189 switch (c_parser_peek_token (parser)->type)
8190 {
8191 case CPP_SEMICOLON:
8192 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
8193 "ISO C does not allow extra %<;%> outside of a function");
8194 c_parser_consume_token (parser);
8195 break;
8196 case CPP_PLUS:
8197 case CPP_MINUS:
8198 c_parser_objc_methodproto (parser);
8199 break;
8200 case CPP_PRAGMA:
8201 c_parser_pragma (parser, pragma_external);
8202 break;
8203 case CPP_EOF:
8204 return;
8205 default:
8206 if (c_parser_next_token_is_keyword (parser, RID_AT_END))
8207 return;
8208 else if (c_parser_next_token_is_keyword (parser, RID_AT_PROPERTY))
8209 c_parser_objc_at_property_declaration (parser);
8210 else if (c_parser_next_token_is_keyword (parser, RID_AT_OPTIONAL))
8211 {
8212 objc_set_method_opt (true);
8213 c_parser_consume_token (parser);
8214 }
8215 else if (c_parser_next_token_is_keyword (parser, RID_AT_REQUIRED))
8216 {
8217 objc_set_method_opt (false);
8218 c_parser_consume_token (parser);
8219 }
8220 else
8221 c_parser_declaration_or_fndef (parser, false, false, true,
8222 false, true, NULL, vNULL);
8223 break;
8224 }
8225 }
8226 }
8227
8228 /* Parse an objc-methodproto.
8229
8230 objc-methodproto:
8231 objc-method-type objc-method-decl ;
8232 */
8233
8234 static void
8235 c_parser_objc_methodproto (c_parser *parser)
8236 {
8237 bool is_class_method = c_parser_objc_method_type (parser);
8238 tree decl, attributes = NULL_TREE;
8239
8240 /* Remember protocol qualifiers in prototypes. */
8241 parser->objc_pq_context = true;
8242 decl = c_parser_objc_method_decl (parser, is_class_method, &attributes,
8243 NULL);
8244 /* Forget protocol qualifiers now. */
8245 parser->objc_pq_context = false;
8246
8247 /* Do not allow the presence of attributes to hide an erroneous
8248 method implementation in the interface section. */
8249 if (!c_parser_next_token_is (parser, CPP_SEMICOLON))
8250 {
8251 c_parser_error (parser, "expected %<;%>");
8252 return;
8253 }
8254
8255 if (decl != error_mark_node)
8256 objc_add_method_declaration (is_class_method, decl, attributes);
8257
8258 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
8259 }
8260
8261 /* If we are at a position that method attributes may be present, check that
8262 there are not any parsed already (a syntax error) and then collect any
8263 specified at the current location. Finally, if new attributes were present,
8264 check that the next token is legal ( ';' for decls and '{' for defs). */
8265
8266 static bool
8267 c_parser_objc_maybe_method_attributes (c_parser* parser, tree* attributes)
8268 {
8269 bool bad = false;
8270 if (*attributes)
8271 {
8272 c_parser_error (parser,
8273 "method attributes must be specified at the end only");
8274 *attributes = NULL_TREE;
8275 bad = true;
8276 }
8277
8278 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
8279 *attributes = c_parser_attributes (parser);
8280
8281 /* If there were no attributes here, just report any earlier error. */
8282 if (*attributes == NULL_TREE || bad)
8283 return bad;
8284
8285 /* If the attributes are followed by a ; or {, then just report any earlier
8286 error. */
8287 if (c_parser_next_token_is (parser, CPP_SEMICOLON)
8288 || c_parser_next_token_is (parser, CPP_OPEN_BRACE))
8289 return bad;
8290
8291 /* We've got attributes, but not at the end. */
8292 c_parser_error (parser,
8293 "expected %<;%> or %<{%> after method attribute definition");
8294 return true;
8295 }
8296
8297 /* Parse an objc-method-decl.
8298
8299 objc-method-decl:
8300 ( objc-type-name ) objc-selector
8301 objc-selector
8302 ( objc-type-name ) objc-keyword-selector objc-optparmlist
8303 objc-keyword-selector objc-optparmlist
8304 attributes
8305
8306 objc-keyword-selector:
8307 objc-keyword-decl
8308 objc-keyword-selector objc-keyword-decl
8309
8310 objc-keyword-decl:
8311 objc-selector : ( objc-type-name ) identifier
8312 objc-selector : identifier
8313 : ( objc-type-name ) identifier
8314 : identifier
8315
8316 objc-optparmlist:
8317 objc-optparms objc-optellipsis
8318
8319 objc-optparms:
8320 empty
8321 objc-opt-parms , parameter-declaration
8322
8323 objc-optellipsis:
8324 empty
8325 , ...
8326 */
8327
8328 static tree
8329 c_parser_objc_method_decl (c_parser *parser, bool is_class_method,
8330 tree *attributes, tree *expr)
8331 {
8332 tree type = NULL_TREE;
8333 tree sel;
8334 tree parms = NULL_TREE;
8335 bool ellipsis = false;
8336 bool attr_err = false;
8337
8338 *attributes = NULL_TREE;
8339 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
8340 {
8341 c_parser_consume_token (parser);
8342 type = c_parser_objc_type_name (parser);
8343 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
8344 }
8345 sel = c_parser_objc_selector (parser);
8346 /* If there is no selector, or a colon follows, we have an
8347 objc-keyword-selector. If there is a selector, and a colon does
8348 not follow, that selector ends the objc-method-decl. */
8349 if (!sel || c_parser_next_token_is (parser, CPP_COLON))
8350 {
8351 tree tsel = sel;
8352 tree list = NULL_TREE;
8353 while (true)
8354 {
8355 tree atype = NULL_TREE, id, keyworddecl;
8356 tree param_attr = NULL_TREE;
8357 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
8358 break;
8359 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
8360 {
8361 c_parser_consume_token (parser);
8362 atype = c_parser_objc_type_name (parser);
8363 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
8364 "expected %<)%>");
8365 }
8366 /* New ObjC allows attributes on method parameters. */
8367 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
8368 param_attr = c_parser_attributes (parser);
8369 if (c_parser_next_token_is_not (parser, CPP_NAME))
8370 {
8371 c_parser_error (parser, "expected identifier");
8372 return error_mark_node;
8373 }
8374 id = c_parser_peek_token (parser)->value;
8375 c_parser_consume_token (parser);
8376 keyworddecl = objc_build_keyword_decl (tsel, atype, id, param_attr);
8377 list = chainon (list, keyworddecl);
8378 tsel = c_parser_objc_selector (parser);
8379 if (!tsel && c_parser_next_token_is_not (parser, CPP_COLON))
8380 break;
8381 }
8382
8383 attr_err |= c_parser_objc_maybe_method_attributes (parser, attributes) ;
8384
8385 /* Parse the optional parameter list. Optional Objective-C
8386 method parameters follow the C syntax, and may include '...'
8387 to denote a variable number of arguments. */
8388 parms = make_node (TREE_LIST);
8389 while (c_parser_next_token_is (parser, CPP_COMMA))
8390 {
8391 struct c_parm *parm;
8392 c_parser_consume_token (parser);
8393 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
8394 {
8395 ellipsis = true;
8396 c_parser_consume_token (parser);
8397 attr_err |= c_parser_objc_maybe_method_attributes
8398 (parser, attributes) ;
8399 break;
8400 }
8401 parm = c_parser_parameter_declaration (parser, NULL_TREE);
8402 if (parm == NULL)
8403 break;
8404 parms = chainon (parms,
8405 build_tree_list (NULL_TREE, grokparm (parm, expr)));
8406 }
8407 sel = list;
8408 }
8409 else
8410 attr_err |= c_parser_objc_maybe_method_attributes (parser, attributes) ;
8411
8412 if (sel == NULL)
8413 {
8414 c_parser_error (parser, "objective-c method declaration is expected");
8415 return error_mark_node;
8416 }
8417
8418 if (attr_err)
8419 return error_mark_node;
8420
8421 return objc_build_method_signature (is_class_method, type, sel, parms, ellipsis);
8422 }
8423
8424 /* Parse an objc-type-name.
8425
8426 objc-type-name:
8427 objc-type-qualifiers[opt] type-name
8428 objc-type-qualifiers[opt]
8429
8430 objc-type-qualifiers:
8431 objc-type-qualifier
8432 objc-type-qualifiers objc-type-qualifier
8433
8434 objc-type-qualifier: one of
8435 in out inout bycopy byref oneway
8436 */
8437
8438 static tree
8439 c_parser_objc_type_name (c_parser *parser)
8440 {
8441 tree quals = NULL_TREE;
8442 struct c_type_name *type_name = NULL;
8443 tree type = NULL_TREE;
8444 while (true)
8445 {
8446 c_token *token = c_parser_peek_token (parser);
8447 if (token->type == CPP_KEYWORD
8448 && (token->keyword == RID_IN
8449 || token->keyword == RID_OUT
8450 || token->keyword == RID_INOUT
8451 || token->keyword == RID_BYCOPY
8452 || token->keyword == RID_BYREF
8453 || token->keyword == RID_ONEWAY))
8454 {
8455 quals = chainon (build_tree_list (NULL_TREE, token->value), quals);
8456 c_parser_consume_token (parser);
8457 }
8458 else
8459 break;
8460 }
8461 if (c_parser_next_tokens_start_typename (parser, cla_prefer_type))
8462 type_name = c_parser_type_name (parser);
8463 if (type_name)
8464 type = groktypename (type_name, NULL, NULL);
8465
8466 /* If the type is unknown, and error has already been produced and
8467 we need to recover from the error. In that case, use NULL_TREE
8468 for the type, as if no type had been specified; this will use the
8469 default type ('id') which is good for error recovery. */
8470 if (type == error_mark_node)
8471 type = NULL_TREE;
8472
8473 return build_tree_list (quals, type);
8474 }
8475
8476 /* Parse objc-protocol-refs.
8477
8478 objc-protocol-refs:
8479 < identifier-list >
8480 */
8481
8482 static tree
8483 c_parser_objc_protocol_refs (c_parser *parser)
8484 {
8485 tree list = NULL_TREE;
8486 gcc_assert (c_parser_next_token_is (parser, CPP_LESS));
8487 c_parser_consume_token (parser);
8488 /* Any identifiers, including those declared as type names, are OK
8489 here. */
8490 while (true)
8491 {
8492 tree id;
8493 if (c_parser_next_token_is_not (parser, CPP_NAME))
8494 {
8495 c_parser_error (parser, "expected identifier");
8496 break;
8497 }
8498 id = c_parser_peek_token (parser)->value;
8499 list = chainon (list, build_tree_list (NULL_TREE, id));
8500 c_parser_consume_token (parser);
8501 if (c_parser_next_token_is (parser, CPP_COMMA))
8502 c_parser_consume_token (parser);
8503 else
8504 break;
8505 }
8506 c_parser_require (parser, CPP_GREATER, "expected %<>%>");
8507 return list;
8508 }
8509
8510 /* Parse an objc-try-catch-finally-statement.
8511
8512 objc-try-catch-finally-statement:
8513 @try compound-statement objc-catch-list[opt]
8514 @try compound-statement objc-catch-list[opt] @finally compound-statement
8515
8516 objc-catch-list:
8517 @catch ( objc-catch-parameter-declaration ) compound-statement
8518 objc-catch-list @catch ( objc-catch-parameter-declaration ) compound-statement
8519
8520 objc-catch-parameter-declaration:
8521 parameter-declaration
8522 '...'
8523
8524 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
8525
8526 PS: This function is identical to cp_parser_objc_try_catch_finally_statement
8527 for C++. Keep them in sync. */
8528
8529 static void
8530 c_parser_objc_try_catch_finally_statement (c_parser *parser)
8531 {
8532 location_t location;
8533 tree stmt;
8534
8535 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_TRY));
8536 c_parser_consume_token (parser);
8537 location = c_parser_peek_token (parser)->location;
8538 objc_maybe_warn_exceptions (location);
8539 stmt = c_parser_compound_statement (parser);
8540 objc_begin_try_stmt (location, stmt);
8541
8542 while (c_parser_next_token_is_keyword (parser, RID_AT_CATCH))
8543 {
8544 struct c_parm *parm;
8545 tree parameter_declaration = error_mark_node;
8546 bool seen_open_paren = false;
8547
8548 c_parser_consume_token (parser);
8549 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
8550 seen_open_paren = true;
8551 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
8552 {
8553 /* We have "@catch (...)" (where the '...' are literally
8554 what is in the code). Skip the '...'.
8555 parameter_declaration is set to NULL_TREE, and
8556 objc_being_catch_clauses() knows that that means
8557 '...'. */
8558 c_parser_consume_token (parser);
8559 parameter_declaration = NULL_TREE;
8560 }
8561 else
8562 {
8563 /* We have "@catch (NSException *exception)" or something
8564 like that. Parse the parameter declaration. */
8565 parm = c_parser_parameter_declaration (parser, NULL_TREE);
8566 if (parm == NULL)
8567 parameter_declaration = error_mark_node;
8568 else
8569 parameter_declaration = grokparm (parm, NULL);
8570 }
8571 if (seen_open_paren)
8572 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
8573 else
8574 {
8575 /* If there was no open parenthesis, we are recovering from
8576 an error, and we are trying to figure out what mistake
8577 the user has made. */
8578
8579 /* If there is an immediate closing parenthesis, the user
8580 probably forgot the opening one (ie, they typed "@catch
8581 NSException *e)". Parse the closing parenthesis and keep
8582 going. */
8583 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
8584 c_parser_consume_token (parser);
8585
8586 /* If these is no immediate closing parenthesis, the user
8587 probably doesn't know that parenthesis are required at
8588 all (ie, they typed "@catch NSException *e"). So, just
8589 forget about the closing parenthesis and keep going. */
8590 }
8591 objc_begin_catch_clause (parameter_declaration);
8592 if (c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
8593 c_parser_compound_statement_nostart (parser);
8594 objc_finish_catch_clause ();
8595 }
8596 if (c_parser_next_token_is_keyword (parser, RID_AT_FINALLY))
8597 {
8598 c_parser_consume_token (parser);
8599 location = c_parser_peek_token (parser)->location;
8600 stmt = c_parser_compound_statement (parser);
8601 objc_build_finally_clause (location, stmt);
8602 }
8603 objc_finish_try_stmt ();
8604 }
8605
8606 /* Parse an objc-synchronized-statement.
8607
8608 objc-synchronized-statement:
8609 @synchronized ( expression ) compound-statement
8610 */
8611
8612 static void
8613 c_parser_objc_synchronized_statement (c_parser *parser)
8614 {
8615 location_t loc;
8616 tree expr, stmt;
8617 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_SYNCHRONIZED));
8618 c_parser_consume_token (parser);
8619 loc = c_parser_peek_token (parser)->location;
8620 objc_maybe_warn_exceptions (loc);
8621 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
8622 {
8623 struct c_expr ce = c_parser_expression (parser);
8624 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
8625 expr = ce.value;
8626 expr = c_fully_fold (expr, false, NULL);
8627 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
8628 }
8629 else
8630 expr = error_mark_node;
8631 stmt = c_parser_compound_statement (parser);
8632 objc_build_synchronized (loc, expr, stmt);
8633 }
8634
8635 /* Parse an objc-selector; return NULL_TREE without an error if the
8636 next token is not an objc-selector.
8637
8638 objc-selector:
8639 identifier
8640 one of
8641 enum struct union if else while do for switch case default
8642 break continue return goto asm sizeof typeof __alignof
8643 unsigned long const short volatile signed restrict _Complex
8644 in out inout bycopy byref oneway int char float double void _Bool
8645 _Atomic
8646
8647 ??? Why this selection of keywords but not, for example, storage
8648 class specifiers? */
8649
8650 static tree
8651 c_parser_objc_selector (c_parser *parser)
8652 {
8653 c_token *token = c_parser_peek_token (parser);
8654 tree value = token->value;
8655 if (token->type == CPP_NAME)
8656 {
8657 c_parser_consume_token (parser);
8658 return value;
8659 }
8660 if (token->type != CPP_KEYWORD)
8661 return NULL_TREE;
8662 switch (token->keyword)
8663 {
8664 case RID_ENUM:
8665 case RID_STRUCT:
8666 case RID_UNION:
8667 case RID_IF:
8668 case RID_ELSE:
8669 case RID_WHILE:
8670 case RID_DO:
8671 case RID_FOR:
8672 case RID_SWITCH:
8673 case RID_CASE:
8674 case RID_DEFAULT:
8675 case RID_BREAK:
8676 case RID_CONTINUE:
8677 case RID_RETURN:
8678 case RID_GOTO:
8679 case RID_ASM:
8680 case RID_SIZEOF:
8681 case RID_TYPEOF:
8682 case RID_ALIGNOF:
8683 case RID_UNSIGNED:
8684 case RID_LONG:
8685 case RID_INT128:
8686 case RID_CONST:
8687 case RID_SHORT:
8688 case RID_VOLATILE:
8689 case RID_SIGNED:
8690 case RID_RESTRICT:
8691 case RID_COMPLEX:
8692 case RID_IN:
8693 case RID_OUT:
8694 case RID_INOUT:
8695 case RID_BYCOPY:
8696 case RID_BYREF:
8697 case RID_ONEWAY:
8698 case RID_INT:
8699 case RID_CHAR:
8700 case RID_FLOAT:
8701 case RID_DOUBLE:
8702 case RID_VOID:
8703 case RID_BOOL:
8704 case RID_ATOMIC:
8705 c_parser_consume_token (parser);
8706 return value;
8707 default:
8708 return NULL_TREE;
8709 }
8710 }
8711
8712 /* Parse an objc-selector-arg.
8713
8714 objc-selector-arg:
8715 objc-selector
8716 objc-keywordname-list
8717
8718 objc-keywordname-list:
8719 objc-keywordname
8720 objc-keywordname-list objc-keywordname
8721
8722 objc-keywordname:
8723 objc-selector :
8724 :
8725 */
8726
8727 static tree
8728 c_parser_objc_selector_arg (c_parser *parser)
8729 {
8730 tree sel = c_parser_objc_selector (parser);
8731 tree list = NULL_TREE;
8732 if (sel && c_parser_next_token_is_not (parser, CPP_COLON))
8733 return sel;
8734 while (true)
8735 {
8736 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
8737 return list;
8738 list = chainon (list, build_tree_list (sel, NULL_TREE));
8739 sel = c_parser_objc_selector (parser);
8740 if (!sel && c_parser_next_token_is_not (parser, CPP_COLON))
8741 break;
8742 }
8743 return list;
8744 }
8745
8746 /* Parse an objc-receiver.
8747
8748 objc-receiver:
8749 expression
8750 class-name
8751 type-name
8752 */
8753
8754 static tree
8755 c_parser_objc_receiver (c_parser *parser)
8756 {
8757 location_t loc = c_parser_peek_token (parser)->location;
8758
8759 if (c_parser_peek_token (parser)->type == CPP_NAME
8760 && (c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME
8761 || c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME))
8762 {
8763 tree id = c_parser_peek_token (parser)->value;
8764 c_parser_consume_token (parser);
8765 return objc_get_class_reference (id);
8766 }
8767 struct c_expr ce = c_parser_expression (parser);
8768 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
8769 return c_fully_fold (ce.value, false, NULL);
8770 }
8771
8772 /* Parse objc-message-args.
8773
8774 objc-message-args:
8775 objc-selector
8776 objc-keywordarg-list
8777
8778 objc-keywordarg-list:
8779 objc-keywordarg
8780 objc-keywordarg-list objc-keywordarg
8781
8782 objc-keywordarg:
8783 objc-selector : objc-keywordexpr
8784 : objc-keywordexpr
8785 */
8786
8787 static tree
8788 c_parser_objc_message_args (c_parser *parser)
8789 {
8790 tree sel = c_parser_objc_selector (parser);
8791 tree list = NULL_TREE;
8792 if (sel && c_parser_next_token_is_not (parser, CPP_COLON))
8793 return sel;
8794 while (true)
8795 {
8796 tree keywordexpr;
8797 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
8798 return error_mark_node;
8799 keywordexpr = c_parser_objc_keywordexpr (parser);
8800 list = chainon (list, build_tree_list (sel, keywordexpr));
8801 sel = c_parser_objc_selector (parser);
8802 if (!sel && c_parser_next_token_is_not (parser, CPP_COLON))
8803 break;
8804 }
8805 return list;
8806 }
8807
8808 /* Parse an objc-keywordexpr.
8809
8810 objc-keywordexpr:
8811 nonempty-expr-list
8812 */
8813
8814 static tree
8815 c_parser_objc_keywordexpr (c_parser *parser)
8816 {
8817 tree ret;
8818 vec<tree, va_gc> *expr_list = c_parser_expr_list (parser, true, true,
8819 NULL, NULL, NULL);
8820 if (vec_safe_length (expr_list) == 1)
8821 {
8822 /* Just return the expression, remove a level of
8823 indirection. */
8824 ret = (*expr_list)[0];
8825 }
8826 else
8827 {
8828 /* We have a comma expression, we will collapse later. */
8829 ret = build_tree_list_vec (expr_list);
8830 }
8831 release_tree_vector (expr_list);
8832 return ret;
8833 }
8834
8835 /* A check, needed in several places, that ObjC interface, implementation or
8836 method definitions are not prefixed by incorrect items. */
8837 static bool
8838 c_parser_objc_diagnose_bad_element_prefix (c_parser *parser,
8839 struct c_declspecs *specs)
8840 {
8841 if (!specs->declspecs_seen_p || specs->non_sc_seen_p
8842 || specs->typespec_kind != ctsk_none)
8843 {
8844 c_parser_error (parser,
8845 "no type or storage class may be specified here,");
8846 c_parser_skip_to_end_of_block_or_statement (parser);
8847 return true;
8848 }
8849 return false;
8850 }
8851
8852 /* Parse an Objective-C @property declaration. The syntax is:
8853
8854 objc-property-declaration:
8855 '@property' objc-property-attributes[opt] struct-declaration ;
8856
8857 objc-property-attributes:
8858 '(' objc-property-attribute-list ')'
8859
8860 objc-property-attribute-list:
8861 objc-property-attribute
8862 objc-property-attribute-list, objc-property-attribute
8863
8864 objc-property-attribute
8865 'getter' = identifier
8866 'setter' = identifier
8867 'readonly'
8868 'readwrite'
8869 'assign'
8870 'retain'
8871 'copy'
8872 'nonatomic'
8873
8874 For example:
8875 @property NSString *name;
8876 @property (readonly) id object;
8877 @property (retain, nonatomic, getter=getTheName) id name;
8878 @property int a, b, c;
8879
8880 PS: This function is identical to cp_parser_objc_at_propery_declaration
8881 for C++. Keep them in sync. */
8882 static void
8883 c_parser_objc_at_property_declaration (c_parser *parser)
8884 {
8885 /* The following variables hold the attributes of the properties as
8886 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
8887 seen. When we see an attribute, we set them to 'true' (if they
8888 are boolean properties) or to the identifier (if they have an
8889 argument, ie, for getter and setter). Note that here we only
8890 parse the list of attributes, check the syntax and accumulate the
8891 attributes that we find. objc_add_property_declaration() will
8892 then process the information. */
8893 bool property_assign = false;
8894 bool property_copy = false;
8895 tree property_getter_ident = NULL_TREE;
8896 bool property_nonatomic = false;
8897 bool property_readonly = false;
8898 bool property_readwrite = false;
8899 bool property_retain = false;
8900 tree property_setter_ident = NULL_TREE;
8901
8902 /* 'properties' is the list of properties that we read. Usually a
8903 single one, but maybe more (eg, in "@property int a, b, c;" there
8904 are three). */
8905 tree properties;
8906 location_t loc;
8907
8908 loc = c_parser_peek_token (parser)->location;
8909 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_PROPERTY));
8910
8911 c_parser_consume_token (parser); /* Eat '@property'. */
8912
8913 /* Parse the optional attribute list... */
8914 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
8915 {
8916 /* Eat the '(' */
8917 c_parser_consume_token (parser);
8918
8919 /* Property attribute keywords are valid now. */
8920 parser->objc_property_attr_context = true;
8921
8922 while (true)
8923 {
8924 bool syntax_error = false;
8925 c_token *token = c_parser_peek_token (parser);
8926 enum rid keyword;
8927
8928 if (token->type != CPP_KEYWORD)
8929 {
8930 if (token->type == CPP_CLOSE_PAREN)
8931 c_parser_error (parser, "expected identifier");
8932 else
8933 {
8934 c_parser_consume_token (parser);
8935 c_parser_error (parser, "unknown property attribute");
8936 }
8937 break;
8938 }
8939 keyword = token->keyword;
8940 c_parser_consume_token (parser);
8941 switch (keyword)
8942 {
8943 case RID_ASSIGN: property_assign = true; break;
8944 case RID_COPY: property_copy = true; break;
8945 case RID_NONATOMIC: property_nonatomic = true; break;
8946 case RID_READONLY: property_readonly = true; break;
8947 case RID_READWRITE: property_readwrite = true; break;
8948 case RID_RETAIN: property_retain = true; break;
8949
8950 case RID_GETTER:
8951 case RID_SETTER:
8952 if (c_parser_next_token_is_not (parser, CPP_EQ))
8953 {
8954 if (keyword == RID_GETTER)
8955 c_parser_error (parser,
8956 "missing %<=%> (after %<getter%> attribute)");
8957 else
8958 c_parser_error (parser,
8959 "missing %<=%> (after %<setter%> attribute)");
8960 syntax_error = true;
8961 break;
8962 }
8963 c_parser_consume_token (parser); /* eat the = */
8964 if (c_parser_next_token_is_not (parser, CPP_NAME))
8965 {
8966 c_parser_error (parser, "expected identifier");
8967 syntax_error = true;
8968 break;
8969 }
8970 if (keyword == RID_SETTER)
8971 {
8972 if (property_setter_ident != NULL_TREE)
8973 c_parser_error (parser, "the %<setter%> attribute may only be specified once");
8974 else
8975 property_setter_ident = c_parser_peek_token (parser)->value;
8976 c_parser_consume_token (parser);
8977 if (c_parser_next_token_is_not (parser, CPP_COLON))
8978 c_parser_error (parser, "setter name must terminate with %<:%>");
8979 else
8980 c_parser_consume_token (parser);
8981 }
8982 else
8983 {
8984 if (property_getter_ident != NULL_TREE)
8985 c_parser_error (parser, "the %<getter%> attribute may only be specified once");
8986 else
8987 property_getter_ident = c_parser_peek_token (parser)->value;
8988 c_parser_consume_token (parser);
8989 }
8990 break;
8991 default:
8992 c_parser_error (parser, "unknown property attribute");
8993 syntax_error = true;
8994 break;
8995 }
8996
8997 if (syntax_error)
8998 break;
8999
9000 if (c_parser_next_token_is (parser, CPP_COMMA))
9001 c_parser_consume_token (parser);
9002 else
9003 break;
9004 }
9005 parser->objc_property_attr_context = false;
9006 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
9007 }
9008 /* ... and the property declaration(s). */
9009 properties = c_parser_struct_declaration (parser);
9010
9011 if (properties == error_mark_node)
9012 {
9013 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
9014 parser->error = false;
9015 return;
9016 }
9017
9018 if (properties == NULL_TREE)
9019 c_parser_error (parser, "expected identifier");
9020 else
9021 {
9022 /* Comma-separated properties are chained together in
9023 reverse order; add them one by one. */
9024 properties = nreverse (properties);
9025
9026 for (; properties; properties = TREE_CHAIN (properties))
9027 objc_add_property_declaration (loc, copy_node (properties),
9028 property_readonly, property_readwrite,
9029 property_assign, property_retain,
9030 property_copy, property_nonatomic,
9031 property_getter_ident, property_setter_ident);
9032 }
9033
9034 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9035 parser->error = false;
9036 }
9037
9038 /* Parse an Objective-C @synthesize declaration. The syntax is:
9039
9040 objc-synthesize-declaration:
9041 @synthesize objc-synthesize-identifier-list ;
9042
9043 objc-synthesize-identifier-list:
9044 objc-synthesize-identifier
9045 objc-synthesize-identifier-list, objc-synthesize-identifier
9046
9047 objc-synthesize-identifier
9048 identifier
9049 identifier = identifier
9050
9051 For example:
9052 @synthesize MyProperty;
9053 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
9054
9055 PS: This function is identical to cp_parser_objc_at_synthesize_declaration
9056 for C++. Keep them in sync.
9057 */
9058 static void
9059 c_parser_objc_at_synthesize_declaration (c_parser *parser)
9060 {
9061 tree list = NULL_TREE;
9062 location_t loc;
9063 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_SYNTHESIZE));
9064 loc = c_parser_peek_token (parser)->location;
9065
9066 c_parser_consume_token (parser);
9067 while (true)
9068 {
9069 tree property, ivar;
9070 if (c_parser_next_token_is_not (parser, CPP_NAME))
9071 {
9072 c_parser_error (parser, "expected identifier");
9073 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
9074 /* Once we find the semicolon, we can resume normal parsing.
9075 We have to reset parser->error manually because
9076 c_parser_skip_until_found() won't reset it for us if the
9077 next token is precisely a semicolon. */
9078 parser->error = false;
9079 return;
9080 }
9081 property = c_parser_peek_token (parser)->value;
9082 c_parser_consume_token (parser);
9083 if (c_parser_next_token_is (parser, CPP_EQ))
9084 {
9085 c_parser_consume_token (parser);
9086 if (c_parser_next_token_is_not (parser, CPP_NAME))
9087 {
9088 c_parser_error (parser, "expected identifier");
9089 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
9090 parser->error = false;
9091 return;
9092 }
9093 ivar = c_parser_peek_token (parser)->value;
9094 c_parser_consume_token (parser);
9095 }
9096 else
9097 ivar = NULL_TREE;
9098 list = chainon (list, build_tree_list (ivar, property));
9099 if (c_parser_next_token_is (parser, CPP_COMMA))
9100 c_parser_consume_token (parser);
9101 else
9102 break;
9103 }
9104 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9105 objc_add_synthesize_declaration (loc, list);
9106 }
9107
9108 /* Parse an Objective-C @dynamic declaration. The syntax is:
9109
9110 objc-dynamic-declaration:
9111 @dynamic identifier-list ;
9112
9113 For example:
9114 @dynamic MyProperty;
9115 @dynamic MyProperty, AnotherProperty;
9116
9117 PS: This function is identical to cp_parser_objc_at_dynamic_declaration
9118 for C++. Keep them in sync.
9119 */
9120 static void
9121 c_parser_objc_at_dynamic_declaration (c_parser *parser)
9122 {
9123 tree list = NULL_TREE;
9124 location_t loc;
9125 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_DYNAMIC));
9126 loc = c_parser_peek_token (parser)->location;
9127
9128 c_parser_consume_token (parser);
9129 while (true)
9130 {
9131 tree property;
9132 if (c_parser_next_token_is_not (parser, CPP_NAME))
9133 {
9134 c_parser_error (parser, "expected identifier");
9135 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
9136 parser->error = false;
9137 return;
9138 }
9139 property = c_parser_peek_token (parser)->value;
9140 list = chainon (list, build_tree_list (NULL_TREE, property));
9141 c_parser_consume_token (parser);
9142 if (c_parser_next_token_is (parser, CPP_COMMA))
9143 c_parser_consume_token (parser);
9144 else
9145 break;
9146 }
9147 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9148 objc_add_dynamic_declaration (loc, list);
9149 }
9150
9151 \f
9152 /* Handle pragmas. Some OpenMP pragmas are associated with, and therefore
9153 should be considered, statements. ALLOW_STMT is true if we're within
9154 the context of a function and such pragmas are to be allowed. Returns
9155 true if we actually parsed such a pragma. */
9156
9157 static bool
9158 c_parser_pragma (c_parser *parser, enum pragma_context context)
9159 {
9160 unsigned int id;
9161
9162 id = c_parser_peek_token (parser)->pragma_kind;
9163 gcc_assert (id != PRAGMA_NONE);
9164
9165 switch (id)
9166 {
9167 case PRAGMA_OMP_BARRIER:
9168 if (context != pragma_compound)
9169 {
9170 if (context == pragma_stmt)
9171 c_parser_error (parser, "%<#pragma omp barrier%> may only be "
9172 "used in compound statements");
9173 goto bad_stmt;
9174 }
9175 c_parser_omp_barrier (parser);
9176 return false;
9177
9178 case PRAGMA_OMP_FLUSH:
9179 if (context != pragma_compound)
9180 {
9181 if (context == pragma_stmt)
9182 c_parser_error (parser, "%<#pragma omp flush%> may only be "
9183 "used in compound statements");
9184 goto bad_stmt;
9185 }
9186 c_parser_omp_flush (parser);
9187 return false;
9188
9189 case PRAGMA_OMP_TASKWAIT:
9190 if (context != pragma_compound)
9191 {
9192 if (context == pragma_stmt)
9193 c_parser_error (parser, "%<#pragma omp taskwait%> may only be "
9194 "used in compound statements");
9195 goto bad_stmt;
9196 }
9197 c_parser_omp_taskwait (parser);
9198 return false;
9199
9200 case PRAGMA_OMP_TASKYIELD:
9201 if (context != pragma_compound)
9202 {
9203 if (context == pragma_stmt)
9204 c_parser_error (parser, "%<#pragma omp taskyield%> may only be "
9205 "used in compound statements");
9206 goto bad_stmt;
9207 }
9208 c_parser_omp_taskyield (parser);
9209 return false;
9210
9211 case PRAGMA_OMP_CANCEL:
9212 if (context != pragma_compound)
9213 {
9214 if (context == pragma_stmt)
9215 c_parser_error (parser, "%<#pragma omp cancel%> may only be "
9216 "used in compound statements");
9217 goto bad_stmt;
9218 }
9219 c_parser_omp_cancel (parser);
9220 return false;
9221
9222 case PRAGMA_OMP_CANCELLATION_POINT:
9223 if (context != pragma_compound)
9224 {
9225 if (context == pragma_stmt)
9226 c_parser_error (parser, "%<#pragma omp cancellation point%> may "
9227 "only be used in compound statements");
9228 goto bad_stmt;
9229 }
9230 c_parser_omp_cancellation_point (parser);
9231 return false;
9232
9233 case PRAGMA_OMP_THREADPRIVATE:
9234 c_parser_omp_threadprivate (parser);
9235 return false;
9236
9237 case PRAGMA_OMP_TARGET:
9238 return c_parser_omp_target (parser, context);
9239
9240 case PRAGMA_OMP_END_DECLARE_TARGET:
9241 c_parser_omp_end_declare_target (parser);
9242 return false;
9243
9244 case PRAGMA_OMP_SECTION:
9245 error_at (c_parser_peek_token (parser)->location,
9246 "%<#pragma omp section%> may only be used in "
9247 "%<#pragma omp sections%> construct");
9248 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
9249 return false;
9250
9251 case PRAGMA_OMP_DECLARE_REDUCTION:
9252 c_parser_omp_declare (parser, context);
9253 return false;
9254 case PRAGMA_IVDEP:
9255 c_parser_consume_pragma (parser);
9256 c_parser_skip_to_pragma_eol (parser);
9257 if (!c_parser_next_token_is_keyword (parser, RID_FOR)
9258 && !c_parser_next_token_is_keyword (parser, RID_WHILE)
9259 && !c_parser_next_token_is_keyword (parser, RID_DO))
9260 {
9261 c_parser_error (parser, "for, while or do statement expected");
9262 return false;
9263 }
9264 if (c_parser_next_token_is_keyword (parser, RID_FOR))
9265 c_parser_for_statement (parser, true);
9266 else if (c_parser_next_token_is_keyword (parser, RID_WHILE))
9267 c_parser_while_statement (parser, true);
9268 else
9269 c_parser_do_statement (parser, true);
9270 return false;
9271
9272 case PRAGMA_GCC_PCH_PREPROCESS:
9273 c_parser_error (parser, "%<#pragma GCC pch_preprocess%> must be first");
9274 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
9275 return false;
9276
9277 default:
9278 if (id < PRAGMA_FIRST_EXTERNAL)
9279 {
9280 if (context != pragma_stmt && context != pragma_compound)
9281 {
9282 bad_stmt:
9283 c_parser_error (parser, "expected declaration specifiers");
9284 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
9285 return false;
9286 }
9287 c_parser_omp_construct (parser);
9288 return true;
9289 }
9290 break;
9291 }
9292
9293 c_parser_consume_pragma (parser);
9294 c_invoke_pragma_handler (id);
9295
9296 /* Skip to EOL, but suppress any error message. Those will have been
9297 generated by the handler routine through calling error, as opposed
9298 to calling c_parser_error. */
9299 parser->error = true;
9300 c_parser_skip_to_pragma_eol (parser);
9301
9302 return false;
9303 }
9304
9305 /* The interface the pragma parsers have to the lexer. */
9306
9307 enum cpp_ttype
9308 pragma_lex (tree *value)
9309 {
9310 c_token *tok = c_parser_peek_token (the_parser);
9311 enum cpp_ttype ret = tok->type;
9312
9313 *value = tok->value;
9314 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
9315 ret = CPP_EOF;
9316 else
9317 {
9318 if (ret == CPP_KEYWORD)
9319 ret = CPP_NAME;
9320 c_parser_consume_token (the_parser);
9321 }
9322
9323 return ret;
9324 }
9325
9326 static void
9327 c_parser_pragma_pch_preprocess (c_parser *parser)
9328 {
9329 tree name = NULL;
9330
9331 c_parser_consume_pragma (parser);
9332 if (c_parser_next_token_is (parser, CPP_STRING))
9333 {
9334 name = c_parser_peek_token (parser)->value;
9335 c_parser_consume_token (parser);
9336 }
9337 else
9338 c_parser_error (parser, "expected string literal");
9339 c_parser_skip_to_pragma_eol (parser);
9340
9341 if (name)
9342 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
9343 }
9344 \f
9345 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 parsing routines. */
9346
9347 /* Returns name of the next clause.
9348 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
9349 the token is not consumed. Otherwise appropriate pragma_omp_clause is
9350 returned and the token is consumed. */
9351
9352 static pragma_omp_clause
9353 c_parser_omp_clause_name (c_parser *parser)
9354 {
9355 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
9356
9357 if (c_parser_next_token_is_keyword (parser, RID_IF))
9358 result = PRAGMA_OMP_CLAUSE_IF;
9359 else if (c_parser_next_token_is_keyword (parser, RID_DEFAULT))
9360 result = PRAGMA_OMP_CLAUSE_DEFAULT;
9361 else if (c_parser_next_token_is_keyword (parser, RID_FOR))
9362 result = PRAGMA_OMP_CLAUSE_FOR;
9363 else if (c_parser_next_token_is (parser, CPP_NAME))
9364 {
9365 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
9366
9367 switch (p[0])
9368 {
9369 case 'a':
9370 if (!strcmp ("aligned", p))
9371 result = PRAGMA_OMP_CLAUSE_ALIGNED;
9372 break;
9373 case 'c':
9374 if (!strcmp ("collapse", p))
9375 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
9376 else if (!strcmp ("copyin", p))
9377 result = PRAGMA_OMP_CLAUSE_COPYIN;
9378 else if (!strcmp ("copyprivate", p))
9379 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
9380 break;
9381 case 'd':
9382 if (!strcmp ("depend", p))
9383 result = PRAGMA_OMP_CLAUSE_DEPEND;
9384 else if (!strcmp ("device", p))
9385 result = PRAGMA_OMP_CLAUSE_DEVICE;
9386 else if (!strcmp ("dist_schedule", p))
9387 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
9388 break;
9389 case 'f':
9390 if (!strcmp ("final", p))
9391 result = PRAGMA_OMP_CLAUSE_FINAL;
9392 else if (!strcmp ("firstprivate", p))
9393 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
9394 else if (!strcmp ("from", p))
9395 result = PRAGMA_OMP_CLAUSE_FROM;
9396 break;
9397 case 'i':
9398 if (!strcmp ("inbranch", p))
9399 result = PRAGMA_OMP_CLAUSE_INBRANCH;
9400 break;
9401 case 'l':
9402 if (!strcmp ("lastprivate", p))
9403 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
9404 else if (!strcmp ("linear", p))
9405 result = PRAGMA_OMP_CLAUSE_LINEAR;
9406 break;
9407 case 'm':
9408 if (!strcmp ("map", p))
9409 result = PRAGMA_OMP_CLAUSE_MAP;
9410 else if (!strcmp ("mergeable", p))
9411 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
9412 break;
9413 case 'n':
9414 if (!strcmp ("notinbranch", p))
9415 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
9416 else if (!strcmp ("nowait", p))
9417 result = PRAGMA_OMP_CLAUSE_NOWAIT;
9418 else if (!strcmp ("num_teams", p))
9419 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
9420 else if (!strcmp ("num_threads", p))
9421 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
9422 break;
9423 case 'o':
9424 if (!strcmp ("ordered", p))
9425 result = PRAGMA_OMP_CLAUSE_ORDERED;
9426 break;
9427 case 'p':
9428 if (!strcmp ("parallel", p))
9429 result = PRAGMA_OMP_CLAUSE_PARALLEL;
9430 else if (!strcmp ("private", p))
9431 result = PRAGMA_OMP_CLAUSE_PRIVATE;
9432 else if (!strcmp ("proc_bind", p))
9433 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
9434 break;
9435 case 'r':
9436 if (!strcmp ("reduction", p))
9437 result = PRAGMA_OMP_CLAUSE_REDUCTION;
9438 break;
9439 case 's':
9440 if (!strcmp ("safelen", p))
9441 result = PRAGMA_OMP_CLAUSE_SAFELEN;
9442 else if (!strcmp ("schedule", p))
9443 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
9444 else if (!strcmp ("sections", p))
9445 result = PRAGMA_OMP_CLAUSE_SECTIONS;
9446 else if (!strcmp ("shared", p))
9447 result = PRAGMA_OMP_CLAUSE_SHARED;
9448 else if (!strcmp ("simdlen", p))
9449 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
9450 break;
9451 case 't':
9452 if (!strcmp ("taskgroup", p))
9453 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
9454 else if (!strcmp ("thread_limit", p))
9455 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
9456 else if (!strcmp ("to", p))
9457 result = PRAGMA_OMP_CLAUSE_TO;
9458 break;
9459 case 'u':
9460 if (!strcmp ("uniform", p))
9461 result = PRAGMA_OMP_CLAUSE_UNIFORM;
9462 else if (!strcmp ("untied", p))
9463 result = PRAGMA_OMP_CLAUSE_UNTIED;
9464 break;
9465 }
9466 }
9467
9468 if (result != PRAGMA_OMP_CLAUSE_NONE)
9469 c_parser_consume_token (parser);
9470
9471 return result;
9472 }
9473
9474 /* Validate that a clause of the given type does not already exist. */
9475
9476 static void
9477 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
9478 const char *name)
9479 {
9480 tree c;
9481
9482 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
9483 if (OMP_CLAUSE_CODE (c) == code)
9484 {
9485 location_t loc = OMP_CLAUSE_LOCATION (c);
9486 error_at (loc, "too many %qs clauses", name);
9487 break;
9488 }
9489 }
9490
9491 /* OpenMP 2.5:
9492 variable-list:
9493 identifier
9494 variable-list , identifier
9495
9496 If KIND is nonzero, create the appropriate node and install the
9497 decl in OMP_CLAUSE_DECL and add the node to the head of the list.
9498 If KIND is nonzero, CLAUSE_LOC is the location of the clause.
9499
9500 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
9501 return the list created. */
9502
9503 static tree
9504 c_parser_omp_variable_list (c_parser *parser,
9505 location_t clause_loc,
9506 enum omp_clause_code kind, tree list)
9507 {
9508 if (c_parser_next_token_is_not (parser, CPP_NAME)
9509 || c_parser_peek_token (parser)->id_kind != C_ID_ID)
9510 c_parser_error (parser, "expected identifier");
9511
9512 while (c_parser_next_token_is (parser, CPP_NAME)
9513 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
9514 {
9515 tree t = lookup_name (c_parser_peek_token (parser)->value);
9516
9517 if (t == NULL_TREE)
9518 {
9519 undeclared_variable (c_parser_peek_token (parser)->location,
9520 c_parser_peek_token (parser)->value);
9521 t = error_mark_node;
9522 }
9523
9524 c_parser_consume_token (parser);
9525
9526 if (t == error_mark_node)
9527 ;
9528 else if (kind != 0)
9529 {
9530 switch (kind)
9531 {
9532 case OMP_CLAUSE_MAP:
9533 case OMP_CLAUSE_FROM:
9534 case OMP_CLAUSE_TO:
9535 case OMP_CLAUSE_DEPEND:
9536 while (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
9537 {
9538 tree low_bound = NULL_TREE, length = NULL_TREE;
9539
9540 c_parser_consume_token (parser);
9541 if (!c_parser_next_token_is (parser, CPP_COLON))
9542 low_bound = c_parser_expression (parser).value;
9543 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
9544 length = integer_one_node;
9545 else
9546 {
9547 /* Look for `:'. */
9548 if (!c_parser_require (parser, CPP_COLON,
9549 "expected %<:%>"))
9550 {
9551 t = error_mark_node;
9552 break;
9553 }
9554 if (!c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
9555 length = c_parser_expression (parser).value;
9556 }
9557 /* Look for the closing `]'. */
9558 if (!c_parser_require (parser, CPP_CLOSE_SQUARE,
9559 "expected %<]%>"))
9560 {
9561 t = error_mark_node;
9562 break;
9563 }
9564 t = tree_cons (low_bound, length, t);
9565 }
9566 break;
9567 default:
9568 break;
9569 }
9570
9571 if (t != error_mark_node)
9572 {
9573 tree u = build_omp_clause (clause_loc, kind);
9574 OMP_CLAUSE_DECL (u) = t;
9575 OMP_CLAUSE_CHAIN (u) = list;
9576 list = u;
9577 }
9578 }
9579 else
9580 list = tree_cons (t, NULL_TREE, list);
9581
9582 if (c_parser_next_token_is_not (parser, CPP_COMMA))
9583 break;
9584
9585 c_parser_consume_token (parser);
9586 }
9587
9588 return list;
9589 }
9590
9591 /* Similarly, but expect leading and trailing parenthesis. This is a very
9592 common case for omp clauses. */
9593
9594 static tree
9595 c_parser_omp_var_list_parens (c_parser *parser, enum omp_clause_code kind,
9596 tree list)
9597 {
9598 /* The clauses location. */
9599 location_t loc = c_parser_peek_token (parser)->location;
9600
9601 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
9602 {
9603 list = c_parser_omp_variable_list (parser, loc, kind, list);
9604 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
9605 }
9606 return list;
9607 }
9608
9609 /* OpenMP 3.0:
9610 collapse ( constant-expression ) */
9611
9612 static tree
9613 c_parser_omp_clause_collapse (c_parser *parser, tree list)
9614 {
9615 tree c, num = error_mark_node;
9616 HOST_WIDE_INT n;
9617 location_t loc;
9618
9619 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse");
9620
9621 loc = c_parser_peek_token (parser)->location;
9622 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
9623 {
9624 num = c_parser_expr_no_commas (parser, NULL).value;
9625 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
9626 }
9627 if (num == error_mark_node)
9628 return list;
9629 mark_exp_read (num);
9630 num = c_fully_fold (num, false, NULL);
9631 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
9632 || !host_integerp (num, 0)
9633 || (n = tree_low_cst (num, 0)) <= 0
9634 || (int) n != n)
9635 {
9636 error_at (loc,
9637 "collapse argument needs positive constant integer expression");
9638 return list;
9639 }
9640 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
9641 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
9642 OMP_CLAUSE_CHAIN (c) = list;
9643 return c;
9644 }
9645
9646 /* OpenMP 2.5:
9647 copyin ( variable-list ) */
9648
9649 static tree
9650 c_parser_omp_clause_copyin (c_parser *parser, tree list)
9651 {
9652 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_COPYIN, list);
9653 }
9654
9655 /* OpenMP 2.5:
9656 copyprivate ( variable-list ) */
9657
9658 static tree
9659 c_parser_omp_clause_copyprivate (c_parser *parser, tree list)
9660 {
9661 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_COPYPRIVATE, list);
9662 }
9663
9664 /* OpenMP 2.5:
9665 default ( shared | none ) */
9666
9667 static tree
9668 c_parser_omp_clause_default (c_parser *parser, tree list)
9669 {
9670 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
9671 location_t loc = c_parser_peek_token (parser)->location;
9672 tree c;
9673
9674 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
9675 return list;
9676 if (c_parser_next_token_is (parser, CPP_NAME))
9677 {
9678 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
9679
9680 switch (p[0])
9681 {
9682 case 'n':
9683 if (strcmp ("none", p) != 0)
9684 goto invalid_kind;
9685 kind = OMP_CLAUSE_DEFAULT_NONE;
9686 break;
9687
9688 case 's':
9689 if (strcmp ("shared", p) != 0)
9690 goto invalid_kind;
9691 kind = OMP_CLAUSE_DEFAULT_SHARED;
9692 break;
9693
9694 default:
9695 goto invalid_kind;
9696 }
9697
9698 c_parser_consume_token (parser);
9699 }
9700 else
9701 {
9702 invalid_kind:
9703 c_parser_error (parser, "expected %<none%> or %<shared%>");
9704 }
9705 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
9706
9707 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
9708 return list;
9709
9710 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default");
9711 c = build_omp_clause (loc, OMP_CLAUSE_DEFAULT);
9712 OMP_CLAUSE_CHAIN (c) = list;
9713 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
9714
9715 return c;
9716 }
9717
9718 /* OpenMP 2.5:
9719 firstprivate ( variable-list ) */
9720
9721 static tree
9722 c_parser_omp_clause_firstprivate (c_parser *parser, tree list)
9723 {
9724 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_FIRSTPRIVATE, list);
9725 }
9726
9727 /* OpenMP 3.1:
9728 final ( expression ) */
9729
9730 static tree
9731 c_parser_omp_clause_final (c_parser *parser, tree list)
9732 {
9733 location_t loc = c_parser_peek_token (parser)->location;
9734 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
9735 {
9736 tree t = c_parser_paren_condition (parser);
9737 tree c;
9738
9739 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final");
9740
9741 c = build_omp_clause (loc, OMP_CLAUSE_FINAL);
9742 OMP_CLAUSE_FINAL_EXPR (c) = t;
9743 OMP_CLAUSE_CHAIN (c) = list;
9744 list = c;
9745 }
9746 else
9747 c_parser_error (parser, "expected %<(%>");
9748
9749 return list;
9750 }
9751
9752 /* OpenMP 2.5:
9753 if ( expression ) */
9754
9755 static tree
9756 c_parser_omp_clause_if (c_parser *parser, tree list)
9757 {
9758 location_t loc = c_parser_peek_token (parser)->location;
9759 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
9760 {
9761 tree t = c_parser_paren_condition (parser);
9762 tree c;
9763
9764 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if");
9765
9766 c = build_omp_clause (loc, OMP_CLAUSE_IF);
9767 OMP_CLAUSE_IF_EXPR (c) = t;
9768 OMP_CLAUSE_CHAIN (c) = list;
9769 list = c;
9770 }
9771 else
9772 c_parser_error (parser, "expected %<(%>");
9773
9774 return list;
9775 }
9776
9777 /* OpenMP 2.5:
9778 lastprivate ( variable-list ) */
9779
9780 static tree
9781 c_parser_omp_clause_lastprivate (c_parser *parser, tree list)
9782 {
9783 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_LASTPRIVATE, list);
9784 }
9785
9786 /* OpenMP 3.1:
9787 mergeable */
9788
9789 static tree
9790 c_parser_omp_clause_mergeable (c_parser *parser ATTRIBUTE_UNUSED, tree list)
9791 {
9792 tree c;
9793
9794 /* FIXME: Should we allow duplicates? */
9795 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable");
9796
9797 c = build_omp_clause (c_parser_peek_token (parser)->location,
9798 OMP_CLAUSE_MERGEABLE);
9799 OMP_CLAUSE_CHAIN (c) = list;
9800
9801 return c;
9802 }
9803
9804 /* OpenMP 2.5:
9805 nowait */
9806
9807 static tree
9808 c_parser_omp_clause_nowait (c_parser *parser ATTRIBUTE_UNUSED, tree list)
9809 {
9810 tree c;
9811 location_t loc = c_parser_peek_token (parser)->location;
9812
9813 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait");
9814
9815 c = build_omp_clause (loc, OMP_CLAUSE_NOWAIT);
9816 OMP_CLAUSE_CHAIN (c) = list;
9817 return c;
9818 }
9819
9820 /* OpenMP 2.5:
9821 num_threads ( expression ) */
9822
9823 static tree
9824 c_parser_omp_clause_num_threads (c_parser *parser, tree list)
9825 {
9826 location_t num_threads_loc = c_parser_peek_token (parser)->location;
9827 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
9828 {
9829 location_t expr_loc = c_parser_peek_token (parser)->location;
9830 tree c, t = c_parser_expression (parser).value;
9831 mark_exp_read (t);
9832 t = c_fully_fold (t, false, NULL);
9833
9834 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
9835
9836 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
9837 {
9838 c_parser_error (parser, "expected integer expression");
9839 return list;
9840 }
9841
9842 /* Attempt to statically determine when the number isn't positive. */
9843 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
9844 build_int_cst (TREE_TYPE (t), 0));
9845 if (CAN_HAVE_LOCATION_P (c))
9846 SET_EXPR_LOCATION (c, expr_loc);
9847 if (c == boolean_true_node)
9848 {
9849 warning_at (expr_loc, 0,
9850 "%<num_threads%> value must be positive");
9851 t = integer_one_node;
9852 }
9853
9854 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS, "num_threads");
9855
9856 c = build_omp_clause (num_threads_loc, OMP_CLAUSE_NUM_THREADS);
9857 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
9858 OMP_CLAUSE_CHAIN (c) = list;
9859 list = c;
9860 }
9861
9862 return list;
9863 }
9864
9865 /* OpenMP 2.5:
9866 ordered */
9867
9868 static tree
9869 c_parser_omp_clause_ordered (c_parser *parser, tree list)
9870 {
9871 tree c;
9872
9873 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED, "ordered");
9874
9875 c = build_omp_clause (c_parser_peek_token (parser)->location,
9876 OMP_CLAUSE_ORDERED);
9877 OMP_CLAUSE_CHAIN (c) = list;
9878
9879 return c;
9880 }
9881
9882 /* OpenMP 2.5:
9883 private ( variable-list ) */
9884
9885 static tree
9886 c_parser_omp_clause_private (c_parser *parser, tree list)
9887 {
9888 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_PRIVATE, list);
9889 }
9890
9891 /* OpenMP 2.5:
9892 reduction ( reduction-operator : variable-list )
9893
9894 reduction-operator:
9895 One of: + * - & ^ | && ||
9896
9897 OpenMP 3.1:
9898
9899 reduction-operator:
9900 One of: + * - & ^ | && || max min
9901
9902 OpenMP 4.0:
9903
9904 reduction-operator:
9905 One of: + * - & ^ | && ||
9906 identifier */
9907
9908 static tree
9909 c_parser_omp_clause_reduction (c_parser *parser, tree list)
9910 {
9911 location_t clause_loc = c_parser_peek_token (parser)->location;
9912 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
9913 {
9914 enum tree_code code = ERROR_MARK;
9915 tree reduc_id = NULL_TREE;
9916
9917 switch (c_parser_peek_token (parser)->type)
9918 {
9919 case CPP_PLUS:
9920 code = PLUS_EXPR;
9921 break;
9922 case CPP_MULT:
9923 code = MULT_EXPR;
9924 break;
9925 case CPP_MINUS:
9926 code = MINUS_EXPR;
9927 break;
9928 case CPP_AND:
9929 code = BIT_AND_EXPR;
9930 break;
9931 case CPP_XOR:
9932 code = BIT_XOR_EXPR;
9933 break;
9934 case CPP_OR:
9935 code = BIT_IOR_EXPR;
9936 break;
9937 case CPP_AND_AND:
9938 code = TRUTH_ANDIF_EXPR;
9939 break;
9940 case CPP_OR_OR:
9941 code = TRUTH_ORIF_EXPR;
9942 break;
9943 case CPP_NAME:
9944 {
9945 const char *p
9946 = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
9947 if (strcmp (p, "min") == 0)
9948 {
9949 code = MIN_EXPR;
9950 break;
9951 }
9952 if (strcmp (p, "max") == 0)
9953 {
9954 code = MAX_EXPR;
9955 break;
9956 }
9957 reduc_id = c_parser_peek_token (parser)->value;
9958 break;
9959 }
9960 default:
9961 c_parser_error (parser,
9962 "expected %<+%>, %<*%>, %<-%>, %<&%>, "
9963 "%<^%>, %<|%>, %<&&%>, %<||%>, %<min%> or %<max%>");
9964 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
9965 return list;
9966 }
9967 c_parser_consume_token (parser);
9968 reduc_id = c_omp_reduction_id (code, reduc_id);
9969 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
9970 {
9971 tree nl, c;
9972
9973 nl = c_parser_omp_variable_list (parser, clause_loc,
9974 OMP_CLAUSE_REDUCTION, list);
9975 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
9976 {
9977 tree type = TREE_TYPE (OMP_CLAUSE_DECL (c));
9978 OMP_CLAUSE_REDUCTION_CODE (c) = code;
9979 if (code == ERROR_MARK
9980 || !(INTEGRAL_TYPE_P (type)
9981 || TREE_CODE (type) == REAL_TYPE
9982 || TREE_CODE (type) == COMPLEX_TYPE))
9983 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c)
9984 = c_omp_reduction_lookup (reduc_id,
9985 TYPE_MAIN_VARIANT (type));
9986 }
9987
9988 list = nl;
9989 }
9990 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
9991 }
9992 return list;
9993 }
9994
9995 /* OpenMP 2.5:
9996 schedule ( schedule-kind )
9997 schedule ( schedule-kind , expression )
9998
9999 schedule-kind:
10000 static | dynamic | guided | runtime | auto
10001 */
10002
10003 static tree
10004 c_parser_omp_clause_schedule (c_parser *parser, tree list)
10005 {
10006 tree c, t;
10007 location_t loc = c_parser_peek_token (parser)->location;
10008
10009 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10010 return list;
10011
10012 c = build_omp_clause (loc, OMP_CLAUSE_SCHEDULE);
10013
10014 if (c_parser_next_token_is (parser, CPP_NAME))
10015 {
10016 tree kind = c_parser_peek_token (parser)->value;
10017 const char *p = IDENTIFIER_POINTER (kind);
10018
10019 switch (p[0])
10020 {
10021 case 'd':
10022 if (strcmp ("dynamic", p) != 0)
10023 goto invalid_kind;
10024 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
10025 break;
10026
10027 case 'g':
10028 if (strcmp ("guided", p) != 0)
10029 goto invalid_kind;
10030 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
10031 break;
10032
10033 case 'r':
10034 if (strcmp ("runtime", p) != 0)
10035 goto invalid_kind;
10036 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
10037 break;
10038
10039 default:
10040 goto invalid_kind;
10041 }
10042 }
10043 else if (c_parser_next_token_is_keyword (parser, RID_STATIC))
10044 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
10045 else if (c_parser_next_token_is_keyword (parser, RID_AUTO))
10046 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
10047 else
10048 goto invalid_kind;
10049
10050 c_parser_consume_token (parser);
10051 if (c_parser_next_token_is (parser, CPP_COMMA))
10052 {
10053 location_t here;
10054 c_parser_consume_token (parser);
10055
10056 here = c_parser_peek_token (parser)->location;
10057 t = c_parser_expr_no_commas (parser, NULL).value;
10058 mark_exp_read (t);
10059 t = c_fully_fold (t, false, NULL);
10060
10061 if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
10062 error_at (here, "schedule %<runtime%> does not take "
10063 "a %<chunk_size%> parameter");
10064 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
10065 error_at (here,
10066 "schedule %<auto%> does not take "
10067 "a %<chunk_size%> parameter");
10068 else if (TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE)
10069 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
10070 else
10071 c_parser_error (parser, "expected integer expression");
10072
10073 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10074 }
10075 else
10076 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
10077 "expected %<,%> or %<)%>");
10078
10079 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
10080 OMP_CLAUSE_CHAIN (c) = list;
10081 return c;
10082
10083 invalid_kind:
10084 c_parser_error (parser, "invalid schedule kind");
10085 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
10086 return list;
10087 }
10088
10089 /* OpenMP 2.5:
10090 shared ( variable-list ) */
10091
10092 static tree
10093 c_parser_omp_clause_shared (c_parser *parser, tree list)
10094 {
10095 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_SHARED, list);
10096 }
10097
10098 /* OpenMP 3.0:
10099 untied */
10100
10101 static tree
10102 c_parser_omp_clause_untied (c_parser *parser ATTRIBUTE_UNUSED, tree list)
10103 {
10104 tree c;
10105
10106 /* FIXME: Should we allow duplicates? */
10107 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied");
10108
10109 c = build_omp_clause (c_parser_peek_token (parser)->location,
10110 OMP_CLAUSE_UNTIED);
10111 OMP_CLAUSE_CHAIN (c) = list;
10112
10113 return c;
10114 }
10115
10116 /* OpenMP 4.0:
10117 inbranch
10118 notinbranch */
10119
10120 static tree
10121 c_parser_omp_clause_branch (c_parser *parser ATTRIBUTE_UNUSED,
10122 enum omp_clause_code code, tree list)
10123 {
10124 check_no_duplicate_clause (list, code, omp_clause_code_name[code]);
10125
10126 tree c = build_omp_clause (c_parser_peek_token (parser)->location, code);
10127 OMP_CLAUSE_CHAIN (c) = list;
10128
10129 return c;
10130 }
10131
10132 /* OpenMP 4.0:
10133 parallel
10134 for
10135 sections
10136 taskgroup */
10137
10138 static tree
10139 c_parser_omp_clause_cancelkind (c_parser *parser ATTRIBUTE_UNUSED,
10140 enum omp_clause_code code, tree list)
10141 {
10142 tree c = build_omp_clause (c_parser_peek_token (parser)->location, code);
10143 OMP_CLAUSE_CHAIN (c) = list;
10144
10145 return c;
10146 }
10147
10148 /* OpenMP 4.0:
10149 num_teams ( expression ) */
10150
10151 static tree
10152 c_parser_omp_clause_num_teams (c_parser *parser, tree list)
10153 {
10154 location_t num_teams_loc = c_parser_peek_token (parser)->location;
10155 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10156 {
10157 location_t expr_loc = c_parser_peek_token (parser)->location;
10158 tree c, t = c_parser_expression (parser).value;
10159 mark_exp_read (t);
10160 t = c_fully_fold (t, false, NULL);
10161
10162 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10163
10164 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
10165 {
10166 c_parser_error (parser, "expected integer expression");
10167 return list;
10168 }
10169
10170 /* Attempt to statically determine when the number isn't positive. */
10171 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
10172 build_int_cst (TREE_TYPE (t), 0));
10173 if (CAN_HAVE_LOCATION_P (c))
10174 SET_EXPR_LOCATION (c, expr_loc);
10175 if (c == boolean_true_node)
10176 {
10177 warning_at (expr_loc, 0, "%<num_teams%> value must be positive");
10178 t = integer_one_node;
10179 }
10180
10181 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS, "num_teams");
10182
10183 c = build_omp_clause (num_teams_loc, OMP_CLAUSE_NUM_TEAMS);
10184 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
10185 OMP_CLAUSE_CHAIN (c) = list;
10186 list = c;
10187 }
10188
10189 return list;
10190 }
10191
10192 /* OpenMP 4.0:
10193 thread_limit ( expression ) */
10194
10195 static tree
10196 c_parser_omp_clause_thread_limit (c_parser *parser, tree list)
10197 {
10198 location_t num_teams_loc = c_parser_peek_token (parser)->location;
10199 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10200 {
10201 location_t expr_loc = c_parser_peek_token (parser)->location;
10202 tree c, t = c_parser_expression (parser).value;
10203 mark_exp_read (t);
10204 t = c_fully_fold (t, false, NULL);
10205
10206 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10207
10208 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
10209 {
10210 c_parser_error (parser, "expected integer expression");
10211 return list;
10212 }
10213
10214 /* Attempt to statically determine when the number isn't positive. */
10215 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
10216 build_int_cst (TREE_TYPE (t), 0));
10217 if (CAN_HAVE_LOCATION_P (c))
10218 SET_EXPR_LOCATION (c, expr_loc);
10219 if (c == boolean_true_node)
10220 {
10221 warning_at (expr_loc, 0, "%<thread_limit%> value must be positive");
10222 t = integer_one_node;
10223 }
10224
10225 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
10226 "thread_limit");
10227
10228 c = build_omp_clause (num_teams_loc, OMP_CLAUSE_THREAD_LIMIT);
10229 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
10230 OMP_CLAUSE_CHAIN (c) = list;
10231 list = c;
10232 }
10233
10234 return list;
10235 }
10236
10237 /* OpenMP 4.0:
10238 aligned ( variable-list )
10239 aligned ( variable-list : constant-expression ) */
10240
10241 static tree
10242 c_parser_omp_clause_aligned (c_parser *parser, tree list)
10243 {
10244 location_t clause_loc = c_parser_peek_token (parser)->location;
10245 tree nl, c;
10246
10247 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10248 return list;
10249
10250 nl = c_parser_omp_variable_list (parser, clause_loc,
10251 OMP_CLAUSE_ALIGNED, list);
10252
10253 if (c_parser_next_token_is (parser, CPP_COLON))
10254 {
10255 c_parser_consume_token (parser);
10256 tree alignment = c_parser_expr_no_commas (parser, NULL).value;
10257 mark_exp_read (alignment);
10258 alignment = c_fully_fold (alignment, false, NULL);
10259 if (!INTEGRAL_TYPE_P (TREE_TYPE (alignment))
10260 && TREE_CODE (alignment) != INTEGER_CST
10261 && tree_int_cst_sgn (alignment) != 1)
10262 {
10263 error_at (clause_loc, "%<aligned%> clause alignment expression must "
10264 "be positive constant integer expression");
10265 alignment = NULL_TREE;
10266 }
10267
10268 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
10269 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
10270 }
10271
10272 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10273 return nl;
10274 }
10275
10276 /* OpenMP 4.0:
10277 linear ( variable-list )
10278 linear ( variable-list : expression ) */
10279
10280 static tree
10281 c_parser_omp_clause_linear (c_parser *parser, tree list)
10282 {
10283 location_t clause_loc = c_parser_peek_token (parser)->location;
10284 tree nl, c, step;
10285
10286 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10287 return list;
10288
10289 nl = c_parser_omp_variable_list (parser, clause_loc,
10290 OMP_CLAUSE_LINEAR, list);
10291
10292 if (c_parser_next_token_is (parser, CPP_COLON))
10293 {
10294 c_parser_consume_token (parser);
10295 step = c_parser_expression (parser).value;
10296 mark_exp_read (step);
10297 step = c_fully_fold (step, false, NULL);
10298 if (!INTEGRAL_TYPE_P (TREE_TYPE (step)))
10299 {
10300 error_at (clause_loc, "%<linear%> clause step expression must "
10301 "be integral");
10302 step = integer_one_node;
10303 }
10304
10305 }
10306 else
10307 step = integer_one_node;
10308
10309 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
10310 {
10311 OMP_CLAUSE_LINEAR_STEP (c) = step;
10312 }
10313
10314 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10315 return nl;
10316 }
10317
10318 /* OpenMP 4.0:
10319 safelen ( constant-expression ) */
10320
10321 static tree
10322 c_parser_omp_clause_safelen (c_parser *parser, tree list)
10323 {
10324 location_t clause_loc = c_parser_peek_token (parser)->location;
10325 tree c, t;
10326
10327 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10328 return list;
10329
10330 t = c_parser_expr_no_commas (parser, NULL).value;
10331 mark_exp_read (t);
10332 t = c_fully_fold (t, false, NULL);
10333 if (!INTEGRAL_TYPE_P (TREE_TYPE (t))
10334 && TREE_CODE (t) != INTEGER_CST
10335 && tree_int_cst_sgn (t) != 1)
10336 {
10337 error_at (clause_loc, "%<safelen%> clause expression must "
10338 "be positive constant integer expression");
10339 t = NULL_TREE;
10340 }
10341
10342 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10343 if (t == NULL_TREE || t == error_mark_node)
10344 return list;
10345
10346 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen");
10347
10348 c = build_omp_clause (clause_loc, OMP_CLAUSE_SAFELEN);
10349 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
10350 OMP_CLAUSE_CHAIN (c) = list;
10351 return c;
10352 }
10353
10354 /* OpenMP 4.0:
10355 simdlen ( constant-expression ) */
10356
10357 static tree
10358 c_parser_omp_clause_simdlen (c_parser *parser, tree list)
10359 {
10360 location_t clause_loc = c_parser_peek_token (parser)->location;
10361 tree c, t;
10362
10363 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10364 return list;
10365
10366 t = c_parser_expr_no_commas (parser, NULL).value;
10367 mark_exp_read (t);
10368 t = c_fully_fold (t, false, NULL);
10369 if (!INTEGRAL_TYPE_P (TREE_TYPE (t))
10370 && TREE_CODE (t) != INTEGER_CST
10371 && tree_int_cst_sgn (t) != 1)
10372 {
10373 error_at (clause_loc, "%<simdlen%> clause expression must "
10374 "be positive constant integer expression");
10375 t = NULL_TREE;
10376 }
10377
10378 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10379 if (t == NULL_TREE || t == error_mark_node)
10380 return list;
10381
10382 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen");
10383
10384 c = build_omp_clause (clause_loc, OMP_CLAUSE_SIMDLEN);
10385 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
10386 OMP_CLAUSE_CHAIN (c) = list;
10387 return c;
10388 }
10389
10390 /* OpenMP 4.0:
10391 depend ( depend-kind: variable-list )
10392
10393 depend-kind:
10394 in | out | inout */
10395
10396 static tree
10397 c_parser_omp_clause_depend (c_parser *parser, tree list)
10398 {
10399 location_t clause_loc = c_parser_peek_token (parser)->location;
10400 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_INOUT;
10401 tree nl, c;
10402
10403 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10404 return list;
10405
10406 if (c_parser_next_token_is (parser, CPP_NAME))
10407 {
10408 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
10409 if (strcmp ("in", p) == 0)
10410 kind = OMP_CLAUSE_DEPEND_IN;
10411 else if (strcmp ("inout", p) == 0)
10412 kind = OMP_CLAUSE_DEPEND_INOUT;
10413 else if (strcmp ("out", p) == 0)
10414 kind = OMP_CLAUSE_DEPEND_OUT;
10415 else
10416 goto invalid_kind;
10417 }
10418 else
10419 goto invalid_kind;
10420
10421 c_parser_consume_token (parser);
10422 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
10423 goto resync_fail;
10424
10425 nl = c_parser_omp_variable_list (parser, clause_loc,
10426 OMP_CLAUSE_DEPEND, list);
10427
10428 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
10429 OMP_CLAUSE_DEPEND_KIND (c) = kind;
10430
10431 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10432 return nl;
10433
10434 invalid_kind:
10435 c_parser_error (parser, "invalid depend kind");
10436 resync_fail:
10437 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10438 return list;
10439 }
10440
10441 /* OpenMP 4.0:
10442 map ( map-kind: variable-list )
10443 map ( variable-list )
10444
10445 map-kind:
10446 alloc | to | from | tofrom */
10447
10448 static tree
10449 c_parser_omp_clause_map (c_parser *parser, tree list)
10450 {
10451 location_t clause_loc = c_parser_peek_token (parser)->location;
10452 enum omp_clause_map_kind kind = OMP_CLAUSE_MAP_TOFROM;
10453 tree nl, c;
10454
10455 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10456 return list;
10457
10458 if (c_parser_next_token_is (parser, CPP_NAME)
10459 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
10460 {
10461 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
10462 if (strcmp ("alloc", p) == 0)
10463 kind = OMP_CLAUSE_MAP_ALLOC;
10464 else if (strcmp ("to", p) == 0)
10465 kind = OMP_CLAUSE_MAP_TO;
10466 else if (strcmp ("from", p) == 0)
10467 kind = OMP_CLAUSE_MAP_FROM;
10468 else if (strcmp ("tofrom", p) == 0)
10469 kind = OMP_CLAUSE_MAP_TOFROM;
10470 else
10471 {
10472 c_parser_error (parser, "invalid map kind");
10473 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
10474 "expected %<)%>");
10475 return list;
10476 }
10477 c_parser_consume_token (parser);
10478 c_parser_consume_token (parser);
10479 }
10480
10481 nl = c_parser_omp_variable_list (parser, clause_loc, OMP_CLAUSE_MAP, list);
10482
10483 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
10484 OMP_CLAUSE_MAP_KIND (c) = kind;
10485
10486 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10487 return nl;
10488 }
10489
10490 /* OpenMP 4.0:
10491 device ( expression ) */
10492
10493 static tree
10494 c_parser_omp_clause_device (c_parser *parser, tree list)
10495 {
10496 location_t clause_loc = c_parser_peek_token (parser)->location;
10497 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10498 {
10499 tree c, t = c_parser_expr_no_commas (parser, NULL).value;
10500 mark_exp_read (t);
10501 t = c_fully_fold (t, false, NULL);
10502
10503 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10504
10505 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
10506 {
10507 c_parser_error (parser, "expected integer expression");
10508 return list;
10509 }
10510
10511 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE, "device");
10512
10513 c = build_omp_clause (clause_loc, OMP_CLAUSE_DEVICE);
10514 OMP_CLAUSE_DEVICE_ID (c) = t;
10515 OMP_CLAUSE_CHAIN (c) = list;
10516 list = c;
10517 }
10518
10519 return list;
10520 }
10521
10522 /* OpenMP 4.0:
10523 dist_schedule ( static )
10524 dist_schedule ( static , expression ) */
10525
10526 static tree
10527 c_parser_omp_clause_dist_schedule (c_parser *parser, tree list)
10528 {
10529 tree c, t = NULL_TREE;
10530 location_t loc = c_parser_peek_token (parser)->location;
10531
10532 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10533 return list;
10534
10535 if (!c_parser_next_token_is_keyword (parser, RID_STATIC))
10536 {
10537 c_parser_error (parser, "invalid dist_schedule kind");
10538 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
10539 "expected %<)%>");
10540 return list;
10541 }
10542
10543 c_parser_consume_token (parser);
10544 if (c_parser_next_token_is (parser, CPP_COMMA))
10545 {
10546 c_parser_consume_token (parser);
10547
10548 t = c_parser_expr_no_commas (parser, NULL).value;
10549 mark_exp_read (t);
10550 t = c_fully_fold (t, false, NULL);
10551 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10552 }
10553 else
10554 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
10555 "expected %<,%> or %<)%>");
10556
10557 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
10558 if (t == error_mark_node)
10559 return list;
10560
10561 c = build_omp_clause (loc, OMP_CLAUSE_DIST_SCHEDULE);
10562 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
10563 OMP_CLAUSE_CHAIN (c) = list;
10564 return c;
10565 }
10566
10567 /* OpenMP 4.0:
10568 proc_bind ( proc-bind-kind )
10569
10570 proc-bind-kind:
10571 master | close | spread */
10572
10573 static tree
10574 c_parser_omp_clause_proc_bind (c_parser *parser, tree list)
10575 {
10576 location_t clause_loc = c_parser_peek_token (parser)->location;
10577 enum omp_clause_proc_bind_kind kind;
10578 tree c;
10579
10580 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10581 return list;
10582
10583 if (c_parser_next_token_is (parser, CPP_NAME))
10584 {
10585 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
10586 if (strcmp ("master", p) == 0)
10587 kind = OMP_CLAUSE_PROC_BIND_MASTER;
10588 else if (strcmp ("close", p) == 0)
10589 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
10590 else if (strcmp ("spread", p) == 0)
10591 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
10592 else
10593 goto invalid_kind;
10594 }
10595 else
10596 goto invalid_kind;
10597
10598 c_parser_consume_token (parser);
10599 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10600 c = build_omp_clause (clause_loc, OMP_CLAUSE_PROC_BIND);
10601 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
10602 OMP_CLAUSE_CHAIN (c) = list;
10603 return c;
10604
10605 invalid_kind:
10606 c_parser_error (parser, "invalid proc_bind kind");
10607 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10608 return list;
10609 }
10610
10611 /* OpenMP 4.0:
10612 to ( variable-list ) */
10613
10614 static tree
10615 c_parser_omp_clause_to (c_parser *parser, tree list)
10616 {
10617 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_TO, list);
10618 }
10619
10620 /* OpenMP 4.0:
10621 from ( variable-list ) */
10622
10623 static tree
10624 c_parser_omp_clause_from (c_parser *parser, tree list)
10625 {
10626 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_FROM, list);
10627 }
10628
10629 /* OpenMP 4.0:
10630 uniform ( variable-list ) */
10631
10632 static tree
10633 c_parser_omp_clause_uniform (c_parser *parser, tree list)
10634 {
10635 /* The clauses location. */
10636 location_t loc = c_parser_peek_token (parser)->location;
10637
10638 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10639 {
10640 list = c_parser_omp_variable_list (parser, loc, OMP_CLAUSE_UNIFORM,
10641 list);
10642 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10643 }
10644 return list;
10645 }
10646
10647 /* Parse all OpenMP clauses. The set clauses allowed by the directive
10648 is a bitmask in MASK. Return the list of clauses found; the result
10649 of clause default goes in *pdefault. */
10650
10651 static tree
10652 c_parser_omp_all_clauses (c_parser *parser, omp_clause_mask mask,
10653 const char *where, bool finish_p = true)
10654 {
10655 tree clauses = NULL;
10656 bool first = true;
10657
10658 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
10659 {
10660 location_t here;
10661 pragma_omp_clause c_kind;
10662 const char *c_name;
10663 tree prev = clauses;
10664
10665 if (!first && c_parser_next_token_is (parser, CPP_COMMA))
10666 c_parser_consume_token (parser);
10667
10668 here = c_parser_peek_token (parser)->location;
10669 c_kind = c_parser_omp_clause_name (parser);
10670
10671 switch (c_kind)
10672 {
10673 case PRAGMA_OMP_CLAUSE_COLLAPSE:
10674 clauses = c_parser_omp_clause_collapse (parser, clauses);
10675 c_name = "collapse";
10676 break;
10677 case PRAGMA_OMP_CLAUSE_COPYIN:
10678 clauses = c_parser_omp_clause_copyin (parser, clauses);
10679 c_name = "copyin";
10680 break;
10681 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
10682 clauses = c_parser_omp_clause_copyprivate (parser, clauses);
10683 c_name = "copyprivate";
10684 break;
10685 case PRAGMA_OMP_CLAUSE_DEFAULT:
10686 clauses = c_parser_omp_clause_default (parser, clauses);
10687 c_name = "default";
10688 break;
10689 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
10690 clauses = c_parser_omp_clause_firstprivate (parser, clauses);
10691 c_name = "firstprivate";
10692 break;
10693 case PRAGMA_OMP_CLAUSE_FINAL:
10694 clauses = c_parser_omp_clause_final (parser, clauses);
10695 c_name = "final";
10696 break;
10697 case PRAGMA_OMP_CLAUSE_IF:
10698 clauses = c_parser_omp_clause_if (parser, clauses);
10699 c_name = "if";
10700 break;
10701 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
10702 clauses = c_parser_omp_clause_lastprivate (parser, clauses);
10703 c_name = "lastprivate";
10704 break;
10705 case PRAGMA_OMP_CLAUSE_MERGEABLE:
10706 clauses = c_parser_omp_clause_mergeable (parser, clauses);
10707 c_name = "mergeable";
10708 break;
10709 case PRAGMA_OMP_CLAUSE_NOWAIT:
10710 clauses = c_parser_omp_clause_nowait (parser, clauses);
10711 c_name = "nowait";
10712 break;
10713 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
10714 clauses = c_parser_omp_clause_num_threads (parser, clauses);
10715 c_name = "num_threads";
10716 break;
10717 case PRAGMA_OMP_CLAUSE_ORDERED:
10718 clauses = c_parser_omp_clause_ordered (parser, clauses);
10719 c_name = "ordered";
10720 break;
10721 case PRAGMA_OMP_CLAUSE_PRIVATE:
10722 clauses = c_parser_omp_clause_private (parser, clauses);
10723 c_name = "private";
10724 break;
10725 case PRAGMA_OMP_CLAUSE_REDUCTION:
10726 clauses = c_parser_omp_clause_reduction (parser, clauses);
10727 c_name = "reduction";
10728 break;
10729 case PRAGMA_OMP_CLAUSE_SCHEDULE:
10730 clauses = c_parser_omp_clause_schedule (parser, clauses);
10731 c_name = "schedule";
10732 break;
10733 case PRAGMA_OMP_CLAUSE_SHARED:
10734 clauses = c_parser_omp_clause_shared (parser, clauses);
10735 c_name = "shared";
10736 break;
10737 case PRAGMA_OMP_CLAUSE_UNTIED:
10738 clauses = c_parser_omp_clause_untied (parser, clauses);
10739 c_name = "untied";
10740 break;
10741 case PRAGMA_OMP_CLAUSE_INBRANCH:
10742 clauses = c_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
10743 clauses);
10744 c_name = "inbranch";
10745 break;
10746 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
10747 clauses = c_parser_omp_clause_branch (parser, OMP_CLAUSE_NOTINBRANCH,
10748 clauses);
10749 c_name = "notinbranch";
10750 break;
10751 case PRAGMA_OMP_CLAUSE_PARALLEL:
10752 clauses
10753 = c_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
10754 clauses);
10755 c_name = "parallel";
10756 if (!first)
10757 {
10758 clause_not_first:
10759 error_at (here, "%qs must be the first clause of %qs",
10760 c_name, where);
10761 clauses = prev;
10762 }
10763 break;
10764 case PRAGMA_OMP_CLAUSE_FOR:
10765 clauses
10766 = c_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
10767 clauses);
10768 c_name = "for";
10769 if (!first)
10770 goto clause_not_first;
10771 break;
10772 case PRAGMA_OMP_CLAUSE_SECTIONS:
10773 clauses
10774 = c_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
10775 clauses);
10776 c_name = "sections";
10777 if (!first)
10778 goto clause_not_first;
10779 break;
10780 case PRAGMA_OMP_CLAUSE_TASKGROUP:
10781 clauses
10782 = c_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
10783 clauses);
10784 c_name = "taskgroup";
10785 if (!first)
10786 goto clause_not_first;
10787 break;
10788 case PRAGMA_OMP_CLAUSE_TO:
10789 clauses = c_parser_omp_clause_to (parser, clauses);
10790 c_name = "to";
10791 break;
10792 case PRAGMA_OMP_CLAUSE_FROM:
10793 clauses = c_parser_omp_clause_from (parser, clauses);
10794 c_name = "from";
10795 break;
10796 case PRAGMA_OMP_CLAUSE_UNIFORM:
10797 clauses = c_parser_omp_clause_uniform (parser, clauses);
10798 c_name = "uniform";
10799 break;
10800 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
10801 clauses = c_parser_omp_clause_num_teams (parser, clauses);
10802 c_name = "num_teams";
10803 break;
10804 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
10805 clauses = c_parser_omp_clause_thread_limit (parser, clauses);
10806 c_name = "thread_limit";
10807 break;
10808 case PRAGMA_OMP_CLAUSE_ALIGNED:
10809 clauses = c_parser_omp_clause_aligned (parser, clauses);
10810 c_name = "aligned";
10811 break;
10812 case PRAGMA_OMP_CLAUSE_LINEAR:
10813 clauses = c_parser_omp_clause_linear (parser, clauses);
10814 c_name = "linear";
10815 break;
10816 case PRAGMA_OMP_CLAUSE_DEPEND:
10817 clauses = c_parser_omp_clause_depend (parser, clauses);
10818 c_name = "depend";
10819 break;
10820 case PRAGMA_OMP_CLAUSE_MAP:
10821 clauses = c_parser_omp_clause_map (parser, clauses);
10822 c_name = "map";
10823 break;
10824 case PRAGMA_OMP_CLAUSE_DEVICE:
10825 clauses = c_parser_omp_clause_device (parser, clauses);
10826 c_name = "device";
10827 break;
10828 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
10829 clauses = c_parser_omp_clause_dist_schedule (parser, clauses);
10830 c_name = "dist_schedule";
10831 break;
10832 case PRAGMA_OMP_CLAUSE_PROC_BIND:
10833 clauses = c_parser_omp_clause_proc_bind (parser, clauses);
10834 c_name = "proc_bind";
10835 break;
10836 case PRAGMA_OMP_CLAUSE_SAFELEN:
10837 clauses = c_parser_omp_clause_safelen (parser, clauses);
10838 c_name = "safelen";
10839 break;
10840 case PRAGMA_OMP_CLAUSE_SIMDLEN:
10841 clauses = c_parser_omp_clause_simdlen (parser, clauses);
10842 c_name = "simdlen";
10843 break;
10844 default:
10845 c_parser_error (parser, "expected %<#pragma omp%> clause");
10846 goto saw_error;
10847 }
10848
10849 first = false;
10850
10851 if (((mask >> c_kind) & 1) == 0 && !parser->error)
10852 {
10853 /* Remove the invalid clause(s) from the list to avoid
10854 confusing the rest of the compiler. */
10855 clauses = prev;
10856 error_at (here, "%qs is not valid for %qs", c_name, where);
10857 }
10858 }
10859
10860 saw_error:
10861 c_parser_skip_to_pragma_eol (parser);
10862
10863 if (finish_p)
10864 return c_finish_omp_clauses (clauses);
10865
10866 return clauses;
10867 }
10868
10869 /* OpenMP 2.5:
10870 structured-block:
10871 statement
10872
10873 In practice, we're also interested in adding the statement to an
10874 outer node. So it is convenient if we work around the fact that
10875 c_parser_statement calls add_stmt. */
10876
10877 static tree
10878 c_parser_omp_structured_block (c_parser *parser)
10879 {
10880 tree stmt = push_stmt_list ();
10881 c_parser_statement (parser);
10882 return pop_stmt_list (stmt);
10883 }
10884
10885 /* OpenMP 2.5:
10886 # pragma omp atomic new-line
10887 expression-stmt
10888
10889 expression-stmt:
10890 x binop= expr | x++ | ++x | x-- | --x
10891 binop:
10892 +, *, -, /, &, ^, |, <<, >>
10893
10894 where x is an lvalue expression with scalar type.
10895
10896 OpenMP 3.1:
10897 # pragma omp atomic new-line
10898 update-stmt
10899
10900 # pragma omp atomic read new-line
10901 read-stmt
10902
10903 # pragma omp atomic write new-line
10904 write-stmt
10905
10906 # pragma omp atomic update new-line
10907 update-stmt
10908
10909 # pragma omp atomic capture new-line
10910 capture-stmt
10911
10912 # pragma omp atomic capture new-line
10913 capture-block
10914
10915 read-stmt:
10916 v = x
10917 write-stmt:
10918 x = expr
10919 update-stmt:
10920 expression-stmt | x = x binop expr
10921 capture-stmt:
10922 v = expression-stmt
10923 capture-block:
10924 { v = x; update-stmt; } | { update-stmt; v = x; }
10925
10926 OpenMP 4.0:
10927 update-stmt:
10928 expression-stmt | x = x binop expr | x = expr binop x
10929 capture-stmt:
10930 v = update-stmt
10931 capture-block:
10932 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
10933
10934 where x and v are lvalue expressions with scalar type.
10935
10936 LOC is the location of the #pragma token. */
10937
10938 static void
10939 c_parser_omp_atomic (location_t loc, c_parser *parser)
10940 {
10941 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE;
10942 tree lhs1 = NULL_TREE, rhs1 = NULL_TREE;
10943 tree stmt, orig_lhs, unfolded_lhs = NULL_TREE, unfolded_lhs1 = NULL_TREE;
10944 enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
10945 struct c_expr expr;
10946 location_t eloc;
10947 bool structured_block = false;
10948 bool swapped = false;
10949 bool seq_cst = false;
10950
10951 if (c_parser_next_token_is (parser, CPP_NAME))
10952 {
10953 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
10954
10955 if (!strcmp (p, "read"))
10956 code = OMP_ATOMIC_READ;
10957 else if (!strcmp (p, "write"))
10958 code = NOP_EXPR;
10959 else if (!strcmp (p, "update"))
10960 code = OMP_ATOMIC;
10961 else if (!strcmp (p, "capture"))
10962 code = OMP_ATOMIC_CAPTURE_NEW;
10963 else
10964 p = NULL;
10965 if (p)
10966 c_parser_consume_token (parser);
10967 }
10968 if (c_parser_next_token_is (parser, CPP_NAME))
10969 {
10970 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
10971 if (!strcmp (p, "seq_cst"))
10972 {
10973 seq_cst = true;
10974 c_parser_consume_token (parser);
10975 }
10976 }
10977 c_parser_skip_to_pragma_eol (parser);
10978
10979 switch (code)
10980 {
10981 case OMP_ATOMIC_READ:
10982 case NOP_EXPR: /* atomic write */
10983 v = c_parser_unary_expression (parser).value;
10984 v = c_fully_fold (v, false, NULL);
10985 if (v == error_mark_node)
10986 goto saw_error;
10987 loc = c_parser_peek_token (parser)->location;
10988 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
10989 goto saw_error;
10990 if (code == NOP_EXPR)
10991 lhs = c_parser_expression (parser).value;
10992 else
10993 lhs = c_parser_unary_expression (parser).value;
10994 lhs = c_fully_fold (lhs, false, NULL);
10995 if (lhs == error_mark_node)
10996 goto saw_error;
10997 if (code == NOP_EXPR)
10998 {
10999 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
11000 opcode. */
11001 code = OMP_ATOMIC;
11002 rhs = lhs;
11003 lhs = v;
11004 v = NULL_TREE;
11005 }
11006 goto done;
11007 case OMP_ATOMIC_CAPTURE_NEW:
11008 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
11009 {
11010 c_parser_consume_token (parser);
11011 structured_block = true;
11012 }
11013 else
11014 {
11015 v = c_parser_unary_expression (parser).value;
11016 v = c_fully_fold (v, false, NULL);
11017 if (v == error_mark_node)
11018 goto saw_error;
11019 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
11020 goto saw_error;
11021 }
11022 break;
11023 default:
11024 break;
11025 }
11026
11027 /* For structured_block case we don't know yet whether
11028 old or new x should be captured. */
11029 restart:
11030 eloc = c_parser_peek_token (parser)->location;
11031 expr = c_parser_unary_expression (parser);
11032 lhs = expr.value;
11033 expr = default_function_array_conversion (eloc, expr);
11034 unfolded_lhs = expr.value;
11035 lhs = c_fully_fold (lhs, false, NULL);
11036 orig_lhs = lhs;
11037 switch (TREE_CODE (lhs))
11038 {
11039 case ERROR_MARK:
11040 saw_error:
11041 c_parser_skip_to_end_of_block_or_statement (parser);
11042 if (structured_block)
11043 {
11044 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
11045 c_parser_consume_token (parser);
11046 else if (code == OMP_ATOMIC_CAPTURE_NEW)
11047 {
11048 c_parser_skip_to_end_of_block_or_statement (parser);
11049 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
11050 c_parser_consume_token (parser);
11051 }
11052 }
11053 return;
11054
11055 case POSTINCREMENT_EXPR:
11056 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
11057 code = OMP_ATOMIC_CAPTURE_OLD;
11058 /* FALLTHROUGH */
11059 case PREINCREMENT_EXPR:
11060 lhs = TREE_OPERAND (lhs, 0);
11061 unfolded_lhs = NULL_TREE;
11062 opcode = PLUS_EXPR;
11063 rhs = integer_one_node;
11064 break;
11065
11066 case POSTDECREMENT_EXPR:
11067 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
11068 code = OMP_ATOMIC_CAPTURE_OLD;
11069 /* FALLTHROUGH */
11070 case PREDECREMENT_EXPR:
11071 lhs = TREE_OPERAND (lhs, 0);
11072 unfolded_lhs = NULL_TREE;
11073 opcode = MINUS_EXPR;
11074 rhs = integer_one_node;
11075 break;
11076
11077 case COMPOUND_EXPR:
11078 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
11079 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
11080 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
11081 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
11082 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
11083 (TREE_OPERAND (lhs, 1), 0), 0)))
11084 == BOOLEAN_TYPE)
11085 /* Undo effects of boolean_increment for post {in,de}crement. */
11086 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
11087 /* FALLTHRU */
11088 case MODIFY_EXPR:
11089 if (TREE_CODE (lhs) == MODIFY_EXPR
11090 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
11091 {
11092 /* Undo effects of boolean_increment. */
11093 if (integer_onep (TREE_OPERAND (lhs, 1)))
11094 {
11095 /* This is pre or post increment. */
11096 rhs = TREE_OPERAND (lhs, 1);
11097 lhs = TREE_OPERAND (lhs, 0);
11098 unfolded_lhs = NULL_TREE;
11099 opcode = NOP_EXPR;
11100 if (code == OMP_ATOMIC_CAPTURE_NEW
11101 && !structured_block
11102 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
11103 code = OMP_ATOMIC_CAPTURE_OLD;
11104 break;
11105 }
11106 if (TREE_CODE (TREE_OPERAND (lhs, 1)) == TRUTH_NOT_EXPR
11107 && TREE_OPERAND (lhs, 0)
11108 == TREE_OPERAND (TREE_OPERAND (lhs, 1), 0))
11109 {
11110 /* This is pre or post decrement. */
11111 rhs = TREE_OPERAND (lhs, 1);
11112 lhs = TREE_OPERAND (lhs, 0);
11113 unfolded_lhs = NULL_TREE;
11114 opcode = NOP_EXPR;
11115 if (code == OMP_ATOMIC_CAPTURE_NEW
11116 && !structured_block
11117 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
11118 code = OMP_ATOMIC_CAPTURE_OLD;
11119 break;
11120 }
11121 }
11122 /* FALLTHRU */
11123 default:
11124 switch (c_parser_peek_token (parser)->type)
11125 {
11126 case CPP_MULT_EQ:
11127 opcode = MULT_EXPR;
11128 break;
11129 case CPP_DIV_EQ:
11130 opcode = TRUNC_DIV_EXPR;
11131 break;
11132 case CPP_PLUS_EQ:
11133 opcode = PLUS_EXPR;
11134 break;
11135 case CPP_MINUS_EQ:
11136 opcode = MINUS_EXPR;
11137 break;
11138 case CPP_LSHIFT_EQ:
11139 opcode = LSHIFT_EXPR;
11140 break;
11141 case CPP_RSHIFT_EQ:
11142 opcode = RSHIFT_EXPR;
11143 break;
11144 case CPP_AND_EQ:
11145 opcode = BIT_AND_EXPR;
11146 break;
11147 case CPP_OR_EQ:
11148 opcode = BIT_IOR_EXPR;
11149 break;
11150 case CPP_XOR_EQ:
11151 opcode = BIT_XOR_EXPR;
11152 break;
11153 case CPP_EQ:
11154 c_parser_consume_token (parser);
11155 eloc = c_parser_peek_token (parser)->location;
11156 expr = c_parser_expr_no_commas (parser, NULL, unfolded_lhs);
11157 rhs1 = expr.value;
11158 switch (TREE_CODE (rhs1))
11159 {
11160 case MULT_EXPR:
11161 case TRUNC_DIV_EXPR:
11162 case PLUS_EXPR:
11163 case MINUS_EXPR:
11164 case LSHIFT_EXPR:
11165 case RSHIFT_EXPR:
11166 case BIT_AND_EXPR:
11167 case BIT_IOR_EXPR:
11168 case BIT_XOR_EXPR:
11169 if (c_tree_equal (TREE_OPERAND (rhs1, 0), unfolded_lhs))
11170 {
11171 opcode = TREE_CODE (rhs1);
11172 rhs = c_fully_fold (TREE_OPERAND (rhs1, 1), false, NULL);
11173 rhs1 = c_fully_fold (TREE_OPERAND (rhs1, 0), false, NULL);
11174 goto stmt_done;
11175 }
11176 if (c_tree_equal (TREE_OPERAND (rhs1, 1), unfolded_lhs))
11177 {
11178 opcode = TREE_CODE (rhs1);
11179 rhs = c_fully_fold (TREE_OPERAND (rhs1, 0), false, NULL);
11180 rhs1 = c_fully_fold (TREE_OPERAND (rhs1, 1), false, NULL);
11181 swapped = !commutative_tree_code (opcode);
11182 goto stmt_done;
11183 }
11184 break;
11185 case ERROR_MARK:
11186 goto saw_error;
11187 default:
11188 break;
11189 }
11190 if (c_parser_peek_token (parser)->type == CPP_SEMICOLON)
11191 {
11192 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
11193 {
11194 code = OMP_ATOMIC_CAPTURE_OLD;
11195 v = lhs;
11196 lhs = NULL_TREE;
11197 expr = default_function_array_read_conversion (eloc, expr);
11198 unfolded_lhs1 = expr.value;
11199 lhs1 = c_fully_fold (unfolded_lhs1, false, NULL);
11200 rhs1 = NULL_TREE;
11201 c_parser_consume_token (parser);
11202 goto restart;
11203 }
11204 if (structured_block)
11205 {
11206 opcode = NOP_EXPR;
11207 expr = default_function_array_read_conversion (eloc, expr);
11208 rhs = c_fully_fold (expr.value, false, NULL);
11209 rhs1 = NULL_TREE;
11210 goto stmt_done;
11211 }
11212 }
11213 c_parser_error (parser, "invalid form of %<#pragma omp atomic%>");
11214 goto saw_error;
11215 default:
11216 c_parser_error (parser,
11217 "invalid operator for %<#pragma omp atomic%>");
11218 goto saw_error;
11219 }
11220
11221 /* Arrange to pass the location of the assignment operator to
11222 c_finish_omp_atomic. */
11223 loc = c_parser_peek_token (parser)->location;
11224 c_parser_consume_token (parser);
11225 eloc = c_parser_peek_token (parser)->location;
11226 expr = c_parser_expression (parser);
11227 expr = default_function_array_read_conversion (eloc, expr);
11228 rhs = expr.value;
11229 rhs = c_fully_fold (rhs, false, NULL);
11230 break;
11231 }
11232 stmt_done:
11233 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
11234 {
11235 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
11236 goto saw_error;
11237 v = c_parser_unary_expression (parser).value;
11238 v = c_fully_fold (v, false, NULL);
11239 if (v == error_mark_node)
11240 goto saw_error;
11241 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
11242 goto saw_error;
11243 eloc = c_parser_peek_token (parser)->location;
11244 expr = c_parser_unary_expression (parser);
11245 lhs1 = expr.value;
11246 expr = default_function_array_read_conversion (eloc, expr);
11247 unfolded_lhs1 = expr.value;
11248 lhs1 = c_fully_fold (lhs1, false, NULL);
11249 if (lhs1 == error_mark_node)
11250 goto saw_error;
11251 }
11252 if (structured_block)
11253 {
11254 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
11255 c_parser_require (parser, CPP_CLOSE_BRACE, "expected %<}%>");
11256 }
11257 done:
11258 if (unfolded_lhs && unfolded_lhs1
11259 && !c_tree_equal (unfolded_lhs, unfolded_lhs1))
11260 {
11261 error ("%<#pragma omp atomic capture%> uses two different "
11262 "expressions for memory");
11263 stmt = error_mark_node;
11264 }
11265 else
11266 stmt = c_finish_omp_atomic (loc, code, opcode, lhs, rhs, v, lhs1, rhs1,
11267 swapped, seq_cst);
11268 if (stmt != error_mark_node)
11269 add_stmt (stmt);
11270
11271 if (!structured_block)
11272 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
11273 }
11274
11275
11276 /* OpenMP 2.5:
11277 # pragma omp barrier new-line
11278 */
11279
11280 static void
11281 c_parser_omp_barrier (c_parser *parser)
11282 {
11283 location_t loc = c_parser_peek_token (parser)->location;
11284 c_parser_consume_pragma (parser);
11285 c_parser_skip_to_pragma_eol (parser);
11286
11287 c_finish_omp_barrier (loc);
11288 }
11289
11290 /* OpenMP 2.5:
11291 # pragma omp critical [(name)] new-line
11292 structured-block
11293
11294 LOC is the location of the #pragma itself. */
11295
11296 static tree
11297 c_parser_omp_critical (location_t loc, c_parser *parser)
11298 {
11299 tree stmt, name = NULL;
11300
11301 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
11302 {
11303 c_parser_consume_token (parser);
11304 if (c_parser_next_token_is (parser, CPP_NAME))
11305 {
11306 name = c_parser_peek_token (parser)->value;
11307 c_parser_consume_token (parser);
11308 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11309 }
11310 else
11311 c_parser_error (parser, "expected identifier");
11312 }
11313 else if (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
11314 c_parser_error (parser, "expected %<(%> or end of line");
11315 c_parser_skip_to_pragma_eol (parser);
11316
11317 stmt = c_parser_omp_structured_block (parser);
11318 return c_finish_omp_critical (loc, stmt, name);
11319 }
11320
11321 /* OpenMP 2.5:
11322 # pragma omp flush flush-vars[opt] new-line
11323
11324 flush-vars:
11325 ( variable-list ) */
11326
11327 static void
11328 c_parser_omp_flush (c_parser *parser)
11329 {
11330 location_t loc = c_parser_peek_token (parser)->location;
11331 c_parser_consume_pragma (parser);
11332 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
11333 c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
11334 else if (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
11335 c_parser_error (parser, "expected %<(%> or end of line");
11336 c_parser_skip_to_pragma_eol (parser);
11337
11338 c_finish_omp_flush (loc);
11339 }
11340
11341 /* Parse the restricted form of the for statement allowed by OpenMP.
11342 The real trick here is to determine the loop control variable early
11343 so that we can push a new decl if necessary to make it private.
11344 LOC is the location of the OMP in "#pragma omp". */
11345
11346 static tree
11347 c_parser_omp_for_loop (location_t loc, c_parser *parser, enum tree_code code,
11348 tree clauses, tree *cclauses)
11349 {
11350 tree decl, cond, incr, save_break, save_cont, body, init, stmt, cl;
11351 tree declv, condv, incrv, initv, ret = NULL;
11352 bool fail = false, open_brace_parsed = false;
11353 int i, collapse = 1, nbraces = 0;
11354 location_t for_loc;
11355 vec<tree, va_gc> *for_block = make_tree_vector ();
11356
11357 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
11358 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
11359 collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
11360
11361 gcc_assert (collapse >= 1);
11362
11363 declv = make_tree_vec (collapse);
11364 initv = make_tree_vec (collapse);
11365 condv = make_tree_vec (collapse);
11366 incrv = make_tree_vec (collapse);
11367
11368 if (!c_parser_next_token_is_keyword (parser, RID_FOR))
11369 {
11370 c_parser_error (parser, "for statement expected");
11371 return NULL;
11372 }
11373 for_loc = c_parser_peek_token (parser)->location;
11374 c_parser_consume_token (parser);
11375
11376 for (i = 0; i < collapse; i++)
11377 {
11378 int bracecount = 0;
11379
11380 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11381 goto pop_scopes;
11382
11383 /* Parse the initialization declaration or expression. */
11384 if (c_parser_next_tokens_start_declaration (parser))
11385 {
11386 if (i > 0)
11387 vec_safe_push (for_block, c_begin_compound_stmt (true));
11388 c_parser_declaration_or_fndef (parser, true, true, true, true, true,
11389 NULL, vNULL);
11390 decl = check_for_loop_decls (for_loc, flag_isoc99);
11391 if (decl == NULL)
11392 goto error_init;
11393 if (DECL_INITIAL (decl) == error_mark_node)
11394 decl = error_mark_node;
11395 init = decl;
11396 }
11397 else if (c_parser_next_token_is (parser, CPP_NAME)
11398 && c_parser_peek_2nd_token (parser)->type == CPP_EQ)
11399 {
11400 struct c_expr decl_exp;
11401 struct c_expr init_exp;
11402 location_t init_loc;
11403
11404 decl_exp = c_parser_postfix_expression (parser);
11405 decl = decl_exp.value;
11406
11407 c_parser_require (parser, CPP_EQ, "expected %<=%>");
11408
11409 init_loc = c_parser_peek_token (parser)->location;
11410 init_exp = c_parser_expr_no_commas (parser, NULL);
11411 init_exp = default_function_array_read_conversion (init_loc,
11412 init_exp);
11413 init = build_modify_expr (init_loc, decl, decl_exp.original_type,
11414 NOP_EXPR, init_loc, init_exp.value,
11415 init_exp.original_type);
11416 init = c_process_expr_stmt (init_loc, init);
11417
11418 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
11419 }
11420 else
11421 {
11422 error_init:
11423 c_parser_error (parser,
11424 "expected iteration declaration or initialization");
11425 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
11426 "expected %<)%>");
11427 fail = true;
11428 goto parse_next;
11429 }
11430
11431 /* Parse the loop condition. */
11432 cond = NULL_TREE;
11433 if (c_parser_next_token_is_not (parser, CPP_SEMICOLON))
11434 {
11435 location_t cond_loc = c_parser_peek_token (parser)->location;
11436 struct c_expr cond_expr
11437 = c_parser_binary_expression (parser, NULL, NULL_TREE);
11438
11439 cond = cond_expr.value;
11440 cond = c_objc_common_truthvalue_conversion (cond_loc, cond);
11441 cond = c_fully_fold (cond, false, NULL);
11442 switch (cond_expr.original_code)
11443 {
11444 case GT_EXPR:
11445 case GE_EXPR:
11446 case LT_EXPR:
11447 case LE_EXPR:
11448 break;
11449 default:
11450 /* Can't be cond = error_mark_node, because we want to preserve
11451 the location until c_finish_omp_for. */
11452 cond = build1 (NOP_EXPR, boolean_type_node, error_mark_node);
11453 break;
11454 }
11455 protected_set_expr_location (cond, cond_loc);
11456 }
11457 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
11458
11459 /* Parse the increment expression. */
11460 incr = NULL_TREE;
11461 if (c_parser_next_token_is_not (parser, CPP_CLOSE_PAREN))
11462 {
11463 location_t incr_loc = c_parser_peek_token (parser)->location;
11464
11465 incr = c_process_expr_stmt (incr_loc,
11466 c_parser_expression (parser).value);
11467 }
11468 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11469
11470 if (decl == NULL || decl == error_mark_node || init == error_mark_node)
11471 fail = true;
11472 else
11473 {
11474 TREE_VEC_ELT (declv, i) = decl;
11475 TREE_VEC_ELT (initv, i) = init;
11476 TREE_VEC_ELT (condv, i) = cond;
11477 TREE_VEC_ELT (incrv, i) = incr;
11478 }
11479
11480 parse_next:
11481 if (i == collapse - 1)
11482 break;
11483
11484 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
11485 in between the collapsed for loops to be still considered perfectly
11486 nested. Hopefully the final version clarifies this.
11487 For now handle (multiple) {'s and empty statements. */
11488 do
11489 {
11490 if (c_parser_next_token_is_keyword (parser, RID_FOR))
11491 {
11492 c_parser_consume_token (parser);
11493 break;
11494 }
11495 else if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
11496 {
11497 c_parser_consume_token (parser);
11498 bracecount++;
11499 }
11500 else if (bracecount
11501 && c_parser_next_token_is (parser, CPP_SEMICOLON))
11502 c_parser_consume_token (parser);
11503 else
11504 {
11505 c_parser_error (parser, "not enough perfectly nested loops");
11506 if (bracecount)
11507 {
11508 open_brace_parsed = true;
11509 bracecount--;
11510 }
11511 fail = true;
11512 collapse = 0;
11513 break;
11514 }
11515 }
11516 while (1);
11517
11518 nbraces += bracecount;
11519 }
11520
11521 save_break = c_break_label;
11522 c_break_label = size_one_node;
11523 save_cont = c_cont_label;
11524 c_cont_label = NULL_TREE;
11525 body = push_stmt_list ();
11526
11527 if (open_brace_parsed)
11528 {
11529 location_t here = c_parser_peek_token (parser)->location;
11530 stmt = c_begin_compound_stmt (true);
11531 c_parser_compound_statement_nostart (parser);
11532 add_stmt (c_end_compound_stmt (here, stmt, true));
11533 }
11534 else
11535 add_stmt (c_parser_c99_block_statement (parser));
11536 if (c_cont_label)
11537 {
11538 tree t = build1 (LABEL_EXPR, void_type_node, c_cont_label);
11539 SET_EXPR_LOCATION (t, loc);
11540 add_stmt (t);
11541 }
11542
11543 body = pop_stmt_list (body);
11544 c_break_label = save_break;
11545 c_cont_label = save_cont;
11546
11547 while (nbraces)
11548 {
11549 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
11550 {
11551 c_parser_consume_token (parser);
11552 nbraces--;
11553 }
11554 else if (c_parser_next_token_is (parser, CPP_SEMICOLON))
11555 c_parser_consume_token (parser);
11556 else
11557 {
11558 c_parser_error (parser, "collapsed loops not perfectly nested");
11559 while (nbraces)
11560 {
11561 location_t here = c_parser_peek_token (parser)->location;
11562 stmt = c_begin_compound_stmt (true);
11563 add_stmt (body);
11564 c_parser_compound_statement_nostart (parser);
11565 body = c_end_compound_stmt (here, stmt, true);
11566 nbraces--;
11567 }
11568 goto pop_scopes;
11569 }
11570 }
11571
11572 /* Only bother calling c_finish_omp_for if we haven't already generated
11573 an error from the initialization parsing. */
11574 if (!fail)
11575 {
11576 stmt = c_finish_omp_for (loc, code, declv, initv, condv,
11577 incrv, body, NULL);
11578 if (stmt)
11579 {
11580 if (cclauses != NULL
11581 && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL)
11582 {
11583 tree *c;
11584 for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
11585 if (OMP_CLAUSE_CODE (*c) != OMP_CLAUSE_FIRSTPRIVATE
11586 && OMP_CLAUSE_CODE (*c) != OMP_CLAUSE_LASTPRIVATE)
11587 c = &OMP_CLAUSE_CHAIN (*c);
11588 else
11589 {
11590 for (i = 0; i < collapse; i++)
11591 if (TREE_VEC_ELT (declv, i) == OMP_CLAUSE_DECL (*c))
11592 break;
11593 if (i == collapse)
11594 c = &OMP_CLAUSE_CHAIN (*c);
11595 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE)
11596 {
11597 error_at (loc,
11598 "iteration variable %qD should not be firstprivate",
11599 OMP_CLAUSE_DECL (*c));
11600 *c = OMP_CLAUSE_CHAIN (*c);
11601 }
11602 else
11603 {
11604 /* Copy lastprivate (decl) clause to OMP_FOR_CLAUSES,
11605 change it to shared (decl) in
11606 OMP_PARALLEL_CLAUSES. */
11607 tree l = build_omp_clause (OMP_CLAUSE_LOCATION (*c),
11608 OMP_CLAUSE_LASTPRIVATE);
11609 OMP_CLAUSE_DECL (l) = OMP_CLAUSE_DECL (*c);
11610 OMP_CLAUSE_CHAIN (l) = clauses;
11611 clauses = l;
11612 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
11613 }
11614 }
11615 }
11616 OMP_FOR_CLAUSES (stmt) = clauses;
11617 }
11618 ret = stmt;
11619 }
11620 pop_scopes:
11621 while (!for_block->is_empty ())
11622 {
11623 /* FIXME diagnostics: LOC below should be the actual location of
11624 this particular for block. We need to build a list of
11625 locations to go along with FOR_BLOCK. */
11626 stmt = c_end_compound_stmt (loc, for_block->pop (), true);
11627 add_stmt (stmt);
11628 }
11629 release_tree_vector (for_block);
11630 return ret;
11631 }
11632
11633 /* Helper function for OpenMP parsing, split clauses and call
11634 finish_omp_clauses on each of the set of clauses afterwards. */
11635
11636 static void
11637 omp_split_clauses (location_t loc, enum tree_code code,
11638 omp_clause_mask mask, tree clauses, tree *cclauses)
11639 {
11640 int i;
11641 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
11642 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
11643 if (cclauses[i])
11644 cclauses[i] = c_finish_omp_clauses (cclauses[i]);
11645 }
11646
11647 /* OpenMP 4.0:
11648 #pragma omp simd simd-clause[optseq] new-line
11649 for-loop
11650
11651 LOC is the location of the #pragma token.
11652 */
11653
11654 #define OMP_SIMD_CLAUSE_MASK \
11655 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
11656 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
11657 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
11658 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
11659 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
11660 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
11661 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
11662
11663 static tree
11664 c_parser_omp_simd (location_t loc, c_parser *parser,
11665 char *p_name, omp_clause_mask mask, tree *cclauses)
11666 {
11667 tree block, clauses, ret;
11668
11669 strcat (p_name, " simd");
11670 mask |= OMP_SIMD_CLAUSE_MASK;
11671 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
11672
11673 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
11674 if (cclauses)
11675 {
11676 omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
11677 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
11678 }
11679
11680 block = c_begin_compound_stmt (true);
11681 ret = c_parser_omp_for_loop (loc, parser, OMP_SIMD, clauses, cclauses);
11682 block = c_end_compound_stmt (loc, block, true);
11683 add_stmt (block);
11684
11685 return ret;
11686 }
11687
11688 /* OpenMP 2.5:
11689 #pragma omp for for-clause[optseq] new-line
11690 for-loop
11691
11692 OpenMP 4.0:
11693 #pragma omp for simd for-simd-clause[optseq] new-line
11694 for-loop
11695
11696 LOC is the location of the #pragma token.
11697 */
11698
11699 #define OMP_FOR_CLAUSE_MASK \
11700 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
11701 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
11702 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
11703 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
11704 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
11705 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
11706 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
11707 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
11708
11709 static tree
11710 c_parser_omp_for (location_t loc, c_parser *parser,
11711 char *p_name, omp_clause_mask mask, tree *cclauses)
11712 {
11713 tree block, clauses, ret;
11714
11715 strcat (p_name, " for");
11716 mask |= OMP_FOR_CLAUSE_MASK;
11717 if (cclauses)
11718 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
11719
11720 if (c_parser_next_token_is (parser, CPP_NAME))
11721 {
11722 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
11723
11724 if (strcmp (p, "simd") == 0)
11725 {
11726 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
11727 if (cclauses == NULL)
11728 cclauses = cclauses_buf;
11729
11730 c_parser_consume_token (parser);
11731 if (!flag_openmp) /* flag_openmp_simd */
11732 return c_parser_omp_simd (loc, parser, p_name, mask, cclauses);
11733 block = c_begin_compound_stmt (true);
11734 ret = c_parser_omp_simd (loc, parser, p_name, mask, cclauses);
11735 block = c_end_compound_stmt (loc, block, true);
11736 if (ret == NULL_TREE)
11737 return ret;
11738 ret = make_node (OMP_FOR);
11739 TREE_TYPE (ret) = void_type_node;
11740 OMP_FOR_BODY (ret) = block;
11741 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
11742 SET_EXPR_LOCATION (ret, loc);
11743 add_stmt (ret);
11744 return ret;
11745 }
11746 }
11747 if (!flag_openmp) /* flag_openmp_simd */
11748 {
11749 c_parser_skip_to_pragma_eol (parser);
11750 return NULL_TREE;
11751 }
11752
11753 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
11754 if (cclauses)
11755 {
11756 omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
11757 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
11758 }
11759
11760 block = c_begin_compound_stmt (true);
11761 ret = c_parser_omp_for_loop (loc, parser, OMP_FOR, clauses, cclauses);
11762 block = c_end_compound_stmt (loc, block, true);
11763 add_stmt (block);
11764
11765 return ret;
11766 }
11767
11768 /* OpenMP 2.5:
11769 # pragma omp master new-line
11770 structured-block
11771
11772 LOC is the location of the #pragma token.
11773 */
11774
11775 static tree
11776 c_parser_omp_master (location_t loc, c_parser *parser)
11777 {
11778 c_parser_skip_to_pragma_eol (parser);
11779 return c_finish_omp_master (loc, c_parser_omp_structured_block (parser));
11780 }
11781
11782 /* OpenMP 2.5:
11783 # pragma omp ordered new-line
11784 structured-block
11785
11786 LOC is the location of the #pragma itself.
11787 */
11788
11789 static tree
11790 c_parser_omp_ordered (location_t loc, c_parser *parser)
11791 {
11792 c_parser_skip_to_pragma_eol (parser);
11793 return c_finish_omp_ordered (loc, c_parser_omp_structured_block (parser));
11794 }
11795
11796 /* OpenMP 2.5:
11797
11798 section-scope:
11799 { section-sequence }
11800
11801 section-sequence:
11802 section-directive[opt] structured-block
11803 section-sequence section-directive structured-block
11804
11805 SECTIONS_LOC is the location of the #pragma omp sections. */
11806
11807 static tree
11808 c_parser_omp_sections_scope (location_t sections_loc, c_parser *parser)
11809 {
11810 tree stmt, substmt;
11811 bool error_suppress = false;
11812 location_t loc;
11813
11814 loc = c_parser_peek_token (parser)->location;
11815 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
11816 {
11817 /* Avoid skipping until the end of the block. */
11818 parser->error = false;
11819 return NULL_TREE;
11820 }
11821
11822 stmt = push_stmt_list ();
11823
11824 if (c_parser_peek_token (parser)->pragma_kind != PRAGMA_OMP_SECTION)
11825 {
11826 substmt = c_parser_omp_structured_block (parser);
11827 substmt = build1 (OMP_SECTION, void_type_node, substmt);
11828 SET_EXPR_LOCATION (substmt, loc);
11829 add_stmt (substmt);
11830 }
11831
11832 while (1)
11833 {
11834 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
11835 break;
11836 if (c_parser_next_token_is (parser, CPP_EOF))
11837 break;
11838
11839 loc = c_parser_peek_token (parser)->location;
11840 if (c_parser_peek_token (parser)->pragma_kind == PRAGMA_OMP_SECTION)
11841 {
11842 c_parser_consume_pragma (parser);
11843 c_parser_skip_to_pragma_eol (parser);
11844 error_suppress = false;
11845 }
11846 else if (!error_suppress)
11847 {
11848 error_at (loc, "expected %<#pragma omp section%> or %<}%>");
11849 error_suppress = true;
11850 }
11851
11852 substmt = c_parser_omp_structured_block (parser);
11853 substmt = build1 (OMP_SECTION, void_type_node, substmt);
11854 SET_EXPR_LOCATION (substmt, loc);
11855 add_stmt (substmt);
11856 }
11857 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE,
11858 "expected %<#pragma omp section%> or %<}%>");
11859
11860 substmt = pop_stmt_list (stmt);
11861
11862 stmt = make_node (OMP_SECTIONS);
11863 SET_EXPR_LOCATION (stmt, sections_loc);
11864 TREE_TYPE (stmt) = void_type_node;
11865 OMP_SECTIONS_BODY (stmt) = substmt;
11866
11867 return add_stmt (stmt);
11868 }
11869
11870 /* OpenMP 2.5:
11871 # pragma omp sections sections-clause[optseq] newline
11872 sections-scope
11873
11874 LOC is the location of the #pragma token.
11875 */
11876
11877 #define OMP_SECTIONS_CLAUSE_MASK \
11878 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
11879 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
11880 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
11881 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
11882 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
11883
11884 static tree
11885 c_parser_omp_sections (location_t loc, c_parser *parser,
11886 char *p_name, omp_clause_mask mask, tree *cclauses)
11887 {
11888 tree block, clauses, ret;
11889
11890 strcat (p_name, " sections");
11891 mask |= OMP_SECTIONS_CLAUSE_MASK;
11892 if (cclauses)
11893 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
11894
11895 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
11896 if (cclauses)
11897 {
11898 omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
11899 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
11900 }
11901
11902 block = c_begin_compound_stmt (true);
11903 ret = c_parser_omp_sections_scope (loc, parser);
11904 if (ret)
11905 OMP_SECTIONS_CLAUSES (ret) = clauses;
11906 block = c_end_compound_stmt (loc, block, true);
11907 add_stmt (block);
11908
11909 return ret;
11910 }
11911
11912 /* OpenMP 2.5:
11913 # pragma parallel parallel-clause new-line
11914 # pragma parallel for parallel-for-clause new-line
11915 # pragma parallel sections parallel-sections-clause new-line
11916
11917 LOC is the location of the #pragma token.
11918 */
11919
11920 #define OMP_PARALLEL_CLAUSE_MASK \
11921 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
11922 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
11923 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
11924 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
11925 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
11926 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
11927 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
11928 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
11929 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
11930
11931 static tree
11932 c_parser_omp_parallel (location_t loc, c_parser *parser,
11933 char *p_name, omp_clause_mask mask, tree *cclauses)
11934 {
11935 tree stmt, clauses, block;
11936
11937 strcat (p_name, " parallel");
11938 mask |= OMP_PARALLEL_CLAUSE_MASK;
11939
11940 if (c_parser_next_token_is_keyword (parser, RID_FOR))
11941 {
11942 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
11943 if (cclauses == NULL)
11944 cclauses = cclauses_buf;
11945
11946 c_parser_consume_token (parser);
11947 if (!flag_openmp) /* flag_openmp_simd */
11948 return c_parser_omp_for (loc, parser, p_name, mask, cclauses);
11949 block = c_begin_omp_parallel ();
11950 c_parser_omp_for (loc, parser, p_name, mask, cclauses);
11951 stmt
11952 = c_finish_omp_parallel (loc, cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
11953 block);
11954 OMP_PARALLEL_COMBINED (stmt) = 1;
11955 return stmt;
11956 }
11957 else if (cclauses)
11958 {
11959 error_at (loc, "expected %<for%> after %qs", p_name);
11960 c_parser_skip_to_pragma_eol (parser);
11961 return NULL_TREE;
11962 }
11963 else if (!flag_openmp) /* flag_openmp_simd */
11964 {
11965 c_parser_skip_to_pragma_eol (parser);
11966 return NULL_TREE;
11967 }
11968 else if (c_parser_next_token_is (parser, CPP_NAME))
11969 {
11970 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
11971 if (strcmp (p, "sections") == 0)
11972 {
11973 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
11974 if (cclauses == NULL)
11975 cclauses = cclauses_buf;
11976
11977 c_parser_consume_token (parser);
11978 block = c_begin_omp_parallel ();
11979 c_parser_omp_sections (loc, parser, p_name, mask, cclauses);
11980 stmt = c_finish_omp_parallel (loc,
11981 cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
11982 block);
11983 OMP_PARALLEL_COMBINED (stmt) = 1;
11984 return stmt;
11985 }
11986 }
11987
11988 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
11989
11990 block = c_begin_omp_parallel ();
11991 c_parser_statement (parser);
11992 stmt = c_finish_omp_parallel (loc, clauses, block);
11993
11994 return stmt;
11995 }
11996
11997 /* OpenMP 2.5:
11998 # pragma omp single single-clause[optseq] new-line
11999 structured-block
12000
12001 LOC is the location of the #pragma.
12002 */
12003
12004 #define OMP_SINGLE_CLAUSE_MASK \
12005 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
12006 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
12007 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
12008 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
12009
12010 static tree
12011 c_parser_omp_single (location_t loc, c_parser *parser)
12012 {
12013 tree stmt = make_node (OMP_SINGLE);
12014 SET_EXPR_LOCATION (stmt, loc);
12015 TREE_TYPE (stmt) = void_type_node;
12016
12017 OMP_SINGLE_CLAUSES (stmt)
12018 = c_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
12019 "#pragma omp single");
12020 OMP_SINGLE_BODY (stmt) = c_parser_omp_structured_block (parser);
12021
12022 return add_stmt (stmt);
12023 }
12024
12025 /* OpenMP 3.0:
12026 # pragma omp task task-clause[optseq] new-line
12027
12028 LOC is the location of the #pragma.
12029 */
12030
12031 #define OMP_TASK_CLAUSE_MASK \
12032 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
12033 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
12034 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
12035 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
12036 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
12037 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
12038 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
12039 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
12040 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND))
12041
12042 static tree
12043 c_parser_omp_task (location_t loc, c_parser *parser)
12044 {
12045 tree clauses, block;
12046
12047 clauses = c_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
12048 "#pragma omp task");
12049
12050 block = c_begin_omp_task ();
12051 c_parser_statement (parser);
12052 return c_finish_omp_task (loc, clauses, block);
12053 }
12054
12055 /* OpenMP 3.0:
12056 # pragma omp taskwait new-line
12057 */
12058
12059 static void
12060 c_parser_omp_taskwait (c_parser *parser)
12061 {
12062 location_t loc = c_parser_peek_token (parser)->location;
12063 c_parser_consume_pragma (parser);
12064 c_parser_skip_to_pragma_eol (parser);
12065
12066 c_finish_omp_taskwait (loc);
12067 }
12068
12069 /* OpenMP 3.1:
12070 # pragma omp taskyield new-line
12071 */
12072
12073 static void
12074 c_parser_omp_taskyield (c_parser *parser)
12075 {
12076 location_t loc = c_parser_peek_token (parser)->location;
12077 c_parser_consume_pragma (parser);
12078 c_parser_skip_to_pragma_eol (parser);
12079
12080 c_finish_omp_taskyield (loc);
12081 }
12082
12083 /* OpenMP 4.0:
12084 # pragma omp taskgroup new-line
12085 */
12086
12087 static tree
12088 c_parser_omp_taskgroup (c_parser *parser)
12089 {
12090 location_t loc = c_parser_peek_token (parser)->location;
12091 c_parser_skip_to_pragma_eol (parser);
12092 return c_finish_omp_taskgroup (loc, c_parser_omp_structured_block (parser));
12093 }
12094
12095 /* OpenMP 4.0:
12096 # pragma omp cancel cancel-clause[optseq] new-line
12097
12098 LOC is the location of the #pragma.
12099 */
12100
12101 #define OMP_CANCEL_CLAUSE_MASK \
12102 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
12103 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
12104 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
12105 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
12106 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
12107
12108 static void
12109 c_parser_omp_cancel (c_parser *parser)
12110 {
12111 location_t loc = c_parser_peek_token (parser)->location;
12112
12113 c_parser_consume_pragma (parser);
12114 tree clauses = c_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
12115 "#pragma omp cancel");
12116
12117 c_finish_omp_cancel (loc, clauses);
12118 }
12119
12120 /* OpenMP 4.0:
12121 # pragma omp cancellation point cancelpt-clause[optseq] new-line
12122
12123 LOC is the location of the #pragma.
12124 */
12125
12126 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
12127 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
12128 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
12129 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
12130 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
12131
12132 static void
12133 c_parser_omp_cancellation_point (c_parser *parser)
12134 {
12135 location_t loc = c_parser_peek_token (parser)->location;
12136 tree clauses;
12137 bool point_seen = false;
12138
12139 c_parser_consume_pragma (parser);
12140 if (c_parser_next_token_is (parser, CPP_NAME))
12141 {
12142 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
12143 if (strcmp (p, "point") == 0)
12144 {
12145 c_parser_consume_token (parser);
12146 point_seen = true;
12147 }
12148 }
12149 if (!point_seen)
12150 {
12151 c_parser_error (parser, "expected %<point%>");
12152 c_parser_skip_to_pragma_eol (parser);
12153 return;
12154 }
12155
12156 clauses
12157 = c_parser_omp_all_clauses (parser, OMP_CANCELLATION_POINT_CLAUSE_MASK,
12158 "#pragma omp cancellation point");
12159
12160 c_finish_omp_cancellation_point (loc, clauses);
12161 }
12162
12163 /* OpenMP 4.0:
12164 #pragma omp distribute distribute-clause[optseq] new-line
12165 for-loop */
12166
12167 #define OMP_DISTRIBUTE_CLAUSE_MASK \
12168 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
12169 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
12170 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
12171 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
12172
12173 static tree
12174 c_parser_omp_distribute (location_t loc, c_parser *parser,
12175 char *p_name, omp_clause_mask mask, tree *cclauses)
12176 {
12177 tree clauses, block, ret;
12178
12179 strcat (p_name, " distribute");
12180 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
12181
12182 if (c_parser_next_token_is (parser, CPP_NAME))
12183 {
12184 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
12185 bool simd = false;
12186 bool parallel = false;
12187
12188 if (strcmp (p, "simd") == 0)
12189 simd = true;
12190 else
12191 parallel = strcmp (p, "parallel") == 0;
12192 if (parallel || simd)
12193 {
12194 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
12195 if (cclauses == NULL)
12196 cclauses = cclauses_buf;
12197 c_parser_consume_token (parser);
12198 if (!flag_openmp) /* flag_openmp_simd */
12199 {
12200 if (simd)
12201 return c_parser_omp_simd (loc, parser, p_name, mask, cclauses);
12202 else
12203 return c_parser_omp_parallel (loc, parser, p_name, mask,
12204 cclauses);
12205 }
12206 block = c_begin_compound_stmt (true);
12207 if (simd)
12208 ret = c_parser_omp_simd (loc, parser, p_name, mask, cclauses);
12209 else
12210 ret = c_parser_omp_parallel (loc, parser, p_name, mask, cclauses);
12211 block = c_end_compound_stmt (loc, block, true);
12212 if (ret == NULL)
12213 return ret;
12214 ret = make_node (OMP_DISTRIBUTE);
12215 TREE_TYPE (ret) = void_type_node;
12216 OMP_FOR_BODY (ret) = block;
12217 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
12218 SET_EXPR_LOCATION (ret, loc);
12219 add_stmt (ret);
12220 return ret;
12221 }
12222 }
12223 if (!flag_openmp) /* flag_openmp_simd */
12224 {
12225 c_parser_skip_to_pragma_eol (parser);
12226 return NULL_TREE;
12227 }
12228
12229 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
12230 if (cclauses)
12231 {
12232 omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
12233 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
12234 }
12235
12236 block = c_begin_compound_stmt (true);
12237 ret = c_parser_omp_for_loop (loc, parser, OMP_DISTRIBUTE, clauses, NULL);
12238 block = c_end_compound_stmt (loc, block, true);
12239 add_stmt (block);
12240
12241 return ret;
12242 }
12243
12244 /* OpenMP 4.0:
12245 # pragma omp teams teams-clause[optseq] new-line
12246 structured-block */
12247
12248 #define OMP_TEAMS_CLAUSE_MASK \
12249 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
12250 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
12251 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
12252 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
12253 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
12254 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
12255 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
12256
12257 static tree
12258 c_parser_omp_teams (location_t loc, c_parser *parser,
12259 char *p_name, omp_clause_mask mask, tree *cclauses)
12260 {
12261 tree clauses, block, ret;
12262
12263 strcat (p_name, " teams");
12264 mask |= OMP_TEAMS_CLAUSE_MASK;
12265
12266 if (c_parser_next_token_is (parser, CPP_NAME))
12267 {
12268 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
12269 if (strcmp (p, "distribute") == 0)
12270 {
12271 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
12272 if (cclauses == NULL)
12273 cclauses = cclauses_buf;
12274
12275 c_parser_consume_token (parser);
12276 if (!flag_openmp) /* flag_openmp_simd */
12277 return c_parser_omp_distribute (loc, parser, p_name, mask, cclauses);
12278 block = c_begin_compound_stmt (true);
12279 ret = c_parser_omp_distribute (loc, parser, p_name, mask, cclauses);
12280 block = c_end_compound_stmt (loc, block, true);
12281 if (ret == NULL)
12282 return ret;
12283 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
12284 ret = make_node (OMP_TEAMS);
12285 TREE_TYPE (ret) = void_type_node;
12286 OMP_TEAMS_CLAUSES (ret) = clauses;
12287 OMP_TEAMS_BODY (ret) = block;
12288 return add_stmt (ret);
12289 }
12290 }
12291 if (!flag_openmp) /* flag_openmp_simd */
12292 {
12293 c_parser_skip_to_pragma_eol (parser);
12294 return NULL_TREE;
12295 }
12296
12297 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
12298 if (cclauses)
12299 {
12300 omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
12301 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
12302 }
12303
12304 tree stmt = make_node (OMP_TEAMS);
12305 TREE_TYPE (stmt) = void_type_node;
12306 OMP_TEAMS_CLAUSES (stmt) = clauses;
12307 OMP_TEAMS_BODY (stmt) = c_parser_omp_structured_block (parser);
12308
12309 return add_stmt (stmt);
12310 }
12311
12312 /* OpenMP 4.0:
12313 # pragma omp target data target-data-clause[optseq] new-line
12314 structured-block */
12315
12316 #define OMP_TARGET_DATA_CLAUSE_MASK \
12317 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
12318 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
12319 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
12320
12321 static tree
12322 c_parser_omp_target_data (location_t loc, c_parser *parser)
12323 {
12324 tree stmt = make_node (OMP_TARGET_DATA);
12325 TREE_TYPE (stmt) = void_type_node;
12326
12327 OMP_TARGET_DATA_CLAUSES (stmt)
12328 = c_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
12329 "#pragma omp target data");
12330 keep_next_level ();
12331 tree block = c_begin_compound_stmt (true);
12332 add_stmt (c_parser_omp_structured_block (parser));
12333 OMP_TARGET_DATA_BODY (stmt) = c_end_compound_stmt (loc, block, true);
12334
12335 SET_EXPR_LOCATION (stmt, loc);
12336 return add_stmt (stmt);
12337 }
12338
12339 /* OpenMP 4.0:
12340 # pragma omp target update target-update-clause[optseq] new-line */
12341
12342 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
12343 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
12344 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
12345 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
12346 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
12347
12348 static bool
12349 c_parser_omp_target_update (location_t loc, c_parser *parser,
12350 enum pragma_context context)
12351 {
12352 if (context == pragma_stmt)
12353 {
12354 error_at (loc,
12355 "%<#pragma omp target update%> may only be "
12356 "used in compound statements");
12357 c_parser_skip_to_pragma_eol (parser);
12358 return false;
12359 }
12360
12361 tree clauses
12362 = c_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
12363 "#pragma omp target update");
12364 if (find_omp_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
12365 && find_omp_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
12366 {
12367 error_at (loc,
12368 "%<#pragma omp target update must contain at least one "
12369 "%<from%> or %<to%> clauses");
12370 return false;
12371 }
12372
12373 tree stmt = make_node (OMP_TARGET_UPDATE);
12374 TREE_TYPE (stmt) = void_type_node;
12375 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
12376 SET_EXPR_LOCATION (stmt, loc);
12377 add_stmt (stmt);
12378 return false;
12379 }
12380
12381 /* OpenMP 4.0:
12382 # pragma omp target target-clause[optseq] new-line
12383 structured-block */
12384
12385 #define OMP_TARGET_CLAUSE_MASK \
12386 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
12387 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
12388 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
12389
12390 static bool
12391 c_parser_omp_target (c_parser *parser, enum pragma_context context)
12392 {
12393 location_t loc = c_parser_peek_token (parser)->location;
12394 c_parser_consume_pragma (parser);
12395
12396 if (context != pragma_stmt && context != pragma_compound)
12397 {
12398 c_parser_error (parser, "expected declaration specifiers");
12399 c_parser_skip_to_pragma_eol (parser);
12400 return false;
12401 }
12402
12403 if (c_parser_next_token_is (parser, CPP_NAME))
12404 {
12405 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
12406
12407 if (strcmp (p, "teams") == 0)
12408 {
12409 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
12410 char p_name[sizeof ("#pragma omp target teams distribute "
12411 "parallel for simd")];
12412
12413 c_parser_consume_token (parser);
12414 if (!flag_openmp) /* flag_openmp_simd */
12415 return c_parser_omp_teams (loc, parser, p_name,
12416 OMP_TARGET_CLAUSE_MASK, cclauses);
12417 strcpy (p_name, "#pragma omp target");
12418 keep_next_level ();
12419 tree block = c_begin_compound_stmt (true);
12420 tree ret = c_parser_omp_teams (loc, parser, p_name,
12421 OMP_TARGET_CLAUSE_MASK, cclauses);
12422 block = c_end_compound_stmt (loc, block, true);
12423 if (ret == NULL)
12424 return ret;
12425 tree stmt = make_node (OMP_TARGET);
12426 TREE_TYPE (stmt) = void_type_node;
12427 OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
12428 OMP_TARGET_BODY (stmt) = block;
12429 add_stmt (stmt);
12430 return true;
12431 }
12432 else if (!flag_openmp) /* flag_openmp_simd */
12433 {
12434 c_parser_skip_to_pragma_eol (parser);
12435 return NULL_TREE;
12436 }
12437 else if (strcmp (p, "data") == 0)
12438 {
12439 c_parser_consume_token (parser);
12440 c_parser_omp_target_data (loc, parser);
12441 return true;
12442 }
12443 else if (strcmp (p, "update") == 0)
12444 {
12445 c_parser_consume_token (parser);
12446 return c_parser_omp_target_update (loc, parser, context);
12447 }
12448 }
12449
12450 tree stmt = make_node (OMP_TARGET);
12451 TREE_TYPE (stmt) = void_type_node;
12452
12453 OMP_TARGET_CLAUSES (stmt)
12454 = c_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
12455 "#pragma omp target");
12456 keep_next_level ();
12457 tree block = c_begin_compound_stmt (true);
12458 add_stmt (c_parser_omp_structured_block (parser));
12459 OMP_TARGET_BODY (stmt) = c_end_compound_stmt (loc, block, true);
12460
12461 SET_EXPR_LOCATION (stmt, loc);
12462 add_stmt (stmt);
12463 return true;
12464 }
12465
12466 /* OpenMP 4.0:
12467 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
12468
12469 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
12470 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
12471 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
12472 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
12473 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
12474 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
12475 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
12476
12477 static void
12478 c_parser_omp_declare_simd (c_parser *parser, enum pragma_context context)
12479 {
12480 vec<c_token> clauses = vNULL;
12481 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
12482 {
12483 c_token *token = c_parser_peek_token (parser);
12484 if (token->type == CPP_EOF)
12485 {
12486 c_parser_skip_to_pragma_eol (parser);
12487 clauses.release ();
12488 return;
12489 }
12490 clauses.safe_push (*token);
12491 c_parser_consume_token (parser);
12492 }
12493 clauses.safe_push (*c_parser_peek_token (parser));
12494 c_parser_skip_to_pragma_eol (parser);
12495
12496 while (c_parser_next_token_is (parser, CPP_PRAGMA))
12497 {
12498 if (c_parser_peek_token (parser)->pragma_kind
12499 != PRAGMA_OMP_DECLARE_REDUCTION
12500 || c_parser_peek_2nd_token (parser)->type != CPP_NAME
12501 || strcmp (IDENTIFIER_POINTER
12502 (c_parser_peek_2nd_token (parser)->value),
12503 "simd") != 0)
12504 {
12505 c_parser_error (parser,
12506 "%<#pragma omp declare simd%> must be followed by "
12507 "function declaration or definition or another "
12508 "%<#pragma omp declare simd%>");
12509 clauses.release ();
12510 return;
12511 }
12512 c_parser_consume_pragma (parser);
12513 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
12514 {
12515 c_token *token = c_parser_peek_token (parser);
12516 if (token->type == CPP_EOF)
12517 {
12518 c_parser_skip_to_pragma_eol (parser);
12519 clauses.release ();
12520 return;
12521 }
12522 clauses.safe_push (*token);
12523 c_parser_consume_token (parser);
12524 }
12525 clauses.safe_push (*c_parser_peek_token (parser));
12526 c_parser_skip_to_pragma_eol (parser);
12527 }
12528
12529 /* Make sure nothing tries to read past the end of the tokens. */
12530 c_token eof_token;
12531 memset (&eof_token, 0, sizeof (eof_token));
12532 eof_token.type = CPP_EOF;
12533 clauses.safe_push (eof_token);
12534 clauses.safe_push (eof_token);
12535
12536 switch (context)
12537 {
12538 case pragma_external:
12539 if (c_parser_next_token_is (parser, CPP_KEYWORD)
12540 && c_parser_peek_token (parser)->keyword == RID_EXTENSION)
12541 {
12542 int ext = disable_extension_diagnostics ();
12543 do
12544 c_parser_consume_token (parser);
12545 while (c_parser_next_token_is (parser, CPP_KEYWORD)
12546 && c_parser_peek_token (parser)->keyword == RID_EXTENSION);
12547 c_parser_declaration_or_fndef (parser, true, true, true, false, true,
12548 NULL, clauses);
12549 restore_extension_diagnostics (ext);
12550 }
12551 else
12552 c_parser_declaration_or_fndef (parser, true, true, true, false, true,
12553 NULL, clauses);
12554 break;
12555 case pragma_struct:
12556 case pragma_param:
12557 c_parser_error (parser, "%<#pragma omp declare simd%> must be followed by "
12558 "function declaration or definition");
12559 break;
12560 case pragma_compound:
12561 case pragma_stmt:
12562 if (c_parser_next_token_is (parser, CPP_KEYWORD)
12563 && c_parser_peek_token (parser)->keyword == RID_EXTENSION)
12564 {
12565 int ext = disable_extension_diagnostics ();
12566 do
12567 c_parser_consume_token (parser);
12568 while (c_parser_next_token_is (parser, CPP_KEYWORD)
12569 && c_parser_peek_token (parser)->keyword == RID_EXTENSION);
12570 if (c_parser_next_tokens_start_declaration (parser))
12571 {
12572 c_parser_declaration_or_fndef (parser, true, true, true, true,
12573 true, NULL, clauses);
12574 restore_extension_diagnostics (ext);
12575 break;
12576 }
12577 restore_extension_diagnostics (ext);
12578 }
12579 else if (c_parser_next_tokens_start_declaration (parser))
12580 {
12581 c_parser_declaration_or_fndef (parser, true, true, true, true, true,
12582 NULL, clauses);
12583 break;
12584 }
12585 c_parser_error (parser, "%<#pragma omp declare simd%> must be followed by "
12586 "function declaration or definition");
12587 break;
12588 default:
12589 gcc_unreachable ();
12590 }
12591 clauses.release ();
12592 }
12593
12594 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
12595 and put that into "omp declare simd" attribute. */
12596
12597 static void
12598 c_finish_omp_declare_simd (c_parser *parser, tree fndecl, tree parms,
12599 vec<c_token> clauses)
12600 {
12601 /* Normally first token is CPP_NAME "simd". CPP_EOF there indicates
12602 error has been reported and CPP_PRAGMA that c_finish_omp_declare_simd
12603 has already processed the tokens. */
12604 if (clauses[0].type == CPP_EOF)
12605 return;
12606 if (fndecl == NULL_TREE || TREE_CODE (fndecl) != FUNCTION_DECL)
12607 {
12608 error ("%<#pragma omp declare simd%> not immediately followed by "
12609 "a function declaration or definition");
12610 clauses[0].type = CPP_EOF;
12611 return;
12612 }
12613 if (clauses[0].type != CPP_NAME)
12614 {
12615 error_at (DECL_SOURCE_LOCATION (fndecl),
12616 "%<#pragma omp declare simd%> not immediately followed by "
12617 "a single function declaration or definition");
12618 clauses[0].type = CPP_EOF;
12619 return;
12620 }
12621
12622 if (parms == NULL_TREE)
12623 parms = DECL_ARGUMENTS (fndecl);
12624
12625 unsigned int tokens_avail = parser->tokens_avail;
12626 gcc_assert (parser->tokens == &parser->tokens_buf[0]);
12627 parser->tokens = clauses.address ();
12628 parser->tokens_avail = clauses.length ();
12629
12630 /* c_parser_omp_declare_simd pushed 2 extra CPP_EOF tokens at the end. */
12631 while (parser->tokens_avail > 3)
12632 {
12633 c_token *token = c_parser_peek_token (parser);
12634 gcc_assert (token->type == CPP_NAME
12635 && strcmp (IDENTIFIER_POINTER (token->value), "simd") == 0);
12636 c_parser_consume_token (parser);
12637 parser->in_pragma = true;
12638
12639 tree c = c_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
12640 "#pragma omp declare simd");
12641 c = c_omp_declare_simd_clauses_to_numbers (parms, c);
12642 if (c != NULL_TREE)
12643 c = tree_cons (NULL_TREE, c, NULL_TREE);
12644 c = build_tree_list (get_identifier ("omp declare simd"), c);
12645 TREE_CHAIN (c) = DECL_ATTRIBUTES (fndecl);
12646 DECL_ATTRIBUTES (fndecl) = c;
12647 }
12648
12649 parser->tokens = &parser->tokens_buf[0];
12650 parser->tokens_avail = tokens_avail;
12651 clauses[0].type = CPP_PRAGMA;
12652 }
12653
12654
12655 /* OpenMP 4.0:
12656 # pragma omp declare target new-line
12657 declarations and definitions
12658 # pragma omp end declare target new-line */
12659
12660 static void
12661 c_parser_omp_declare_target (c_parser *parser)
12662 {
12663 c_parser_skip_to_pragma_eol (parser);
12664 current_omp_declare_target_attribute++;
12665 }
12666
12667 static void
12668 c_parser_omp_end_declare_target (c_parser *parser)
12669 {
12670 location_t loc = c_parser_peek_token (parser)->location;
12671 c_parser_consume_pragma (parser);
12672 if (c_parser_next_token_is (parser, CPP_NAME)
12673 && strcmp (IDENTIFIER_POINTER (c_parser_peek_token (parser)->value),
12674 "declare") == 0)
12675 {
12676 c_parser_consume_token (parser);
12677 if (c_parser_next_token_is (parser, CPP_NAME)
12678 && strcmp (IDENTIFIER_POINTER (c_parser_peek_token (parser)->value),
12679 "target") == 0)
12680 c_parser_consume_token (parser);
12681 else
12682 {
12683 c_parser_error (parser, "expected %<target%>");
12684 c_parser_skip_to_pragma_eol (parser);
12685 return;
12686 }
12687 }
12688 else
12689 {
12690 c_parser_error (parser, "expected %<declare%>");
12691 c_parser_skip_to_pragma_eol (parser);
12692 return;
12693 }
12694 c_parser_skip_to_pragma_eol (parser);
12695 if (!current_omp_declare_target_attribute)
12696 error_at (loc, "%<#pragma omp end declare target%> without corresponding "
12697 "%<#pragma omp declare target%>");
12698 else
12699 current_omp_declare_target_attribute--;
12700 }
12701
12702
12703 /* OpenMP 4.0
12704 #pragma omp declare reduction (reduction-id : typename-list : expression) \
12705 initializer-clause[opt] new-line
12706
12707 initializer-clause:
12708 initializer (omp_priv = initializer)
12709 initializer (function-name (argument-list)) */
12710
12711 static void
12712 c_parser_omp_declare_reduction (c_parser *parser, enum pragma_context context)
12713 {
12714 unsigned int tokens_avail = 0, i;
12715 vec<tree> types = vNULL;
12716 vec<c_token> clauses = vNULL;
12717 enum tree_code reduc_code = ERROR_MARK;
12718 tree reduc_id = NULL_TREE;
12719 tree type;
12720 location_t rloc = c_parser_peek_token (parser)->location;
12721
12722 if (context == pragma_struct || context == pragma_param)
12723 {
12724 error ("%<#pragma omp declare reduction%> not at file or block scope");
12725 goto fail;
12726 }
12727
12728 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
12729 goto fail;
12730
12731 switch (c_parser_peek_token (parser)->type)
12732 {
12733 case CPP_PLUS:
12734 reduc_code = PLUS_EXPR;
12735 break;
12736 case CPP_MULT:
12737 reduc_code = MULT_EXPR;
12738 break;
12739 case CPP_MINUS:
12740 reduc_code = MINUS_EXPR;
12741 break;
12742 case CPP_AND:
12743 reduc_code = BIT_AND_EXPR;
12744 break;
12745 case CPP_XOR:
12746 reduc_code = BIT_XOR_EXPR;
12747 break;
12748 case CPP_OR:
12749 reduc_code = BIT_IOR_EXPR;
12750 break;
12751 case CPP_AND_AND:
12752 reduc_code = TRUTH_ANDIF_EXPR;
12753 break;
12754 case CPP_OR_OR:
12755 reduc_code = TRUTH_ORIF_EXPR;
12756 break;
12757 case CPP_NAME:
12758 const char *p;
12759 p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
12760 if (strcmp (p, "min") == 0)
12761 {
12762 reduc_code = MIN_EXPR;
12763 break;
12764 }
12765 if (strcmp (p, "max") == 0)
12766 {
12767 reduc_code = MAX_EXPR;
12768 break;
12769 }
12770 reduc_id = c_parser_peek_token (parser)->value;
12771 break;
12772 default:
12773 c_parser_error (parser,
12774 "expected %<+%>, %<*%>, %<-%>, %<&%>, "
12775 "%<^%>, %<|%>, %<&&%>, %<||%>, %<min%> or identifier");
12776 goto fail;
12777 }
12778
12779 tree orig_reduc_id, reduc_decl;
12780 orig_reduc_id = reduc_id;
12781 reduc_id = c_omp_reduction_id (reduc_code, reduc_id);
12782 reduc_decl = c_omp_reduction_decl (reduc_id);
12783 c_parser_consume_token (parser);
12784
12785 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
12786 goto fail;
12787
12788 while (true)
12789 {
12790 location_t loc = c_parser_peek_token (parser)->location;
12791 struct c_type_name *ctype = c_parser_type_name (parser);
12792 if (ctype != NULL)
12793 {
12794 type = groktypename (ctype, NULL, NULL);
12795 if (type == error_mark_node)
12796 ;
12797 else if ((INTEGRAL_TYPE_P (type)
12798 || TREE_CODE (type) == REAL_TYPE
12799 || TREE_CODE (type) == COMPLEX_TYPE)
12800 && orig_reduc_id == NULL_TREE)
12801 error_at (loc, "predeclared arithmetic type in "
12802 "%<#pragma omp declare reduction%>");
12803 else if (TREE_CODE (type) == FUNCTION_TYPE
12804 || TREE_CODE (type) == ARRAY_TYPE)
12805 error_at (loc, "function or array type in "
12806 "%<#pragma omp declare reduction%>");
12807 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
12808 error_at (loc, "const, volatile or restrict qualified type in "
12809 "%<#pragma omp declare reduction%>");
12810 else
12811 {
12812 tree t;
12813 for (t = DECL_INITIAL (reduc_decl); t; t = TREE_CHAIN (t))
12814 if (comptypes (TREE_PURPOSE (t), type))
12815 {
12816 error_at (loc, "redeclaration of %qs "
12817 "%<#pragma omp declare reduction%> for "
12818 "type %qT",
12819 IDENTIFIER_POINTER (reduc_id)
12820 + sizeof ("omp declare reduction ") - 1,
12821 type);
12822 location_t ploc
12823 = DECL_SOURCE_LOCATION (TREE_VEC_ELT (TREE_VALUE (t),
12824 0));
12825 error_at (ploc, "previous %<#pragma omp declare "
12826 "reduction%>");
12827 break;
12828 }
12829 if (t == NULL_TREE)
12830 types.safe_push (type);
12831 }
12832 if (c_parser_next_token_is (parser, CPP_COMMA))
12833 c_parser_consume_token (parser);
12834 else
12835 break;
12836 }
12837 else
12838 break;
12839 }
12840
12841 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>")
12842 || types.is_empty ())
12843 {
12844 fail:
12845 clauses.release ();
12846 types.release ();
12847 while (true)
12848 {
12849 c_token *token = c_parser_peek_token (parser);
12850 if (token->type == CPP_EOF || token->type == CPP_PRAGMA_EOL)
12851 break;
12852 c_parser_consume_token (parser);
12853 }
12854 c_parser_skip_to_pragma_eol (parser);
12855 return;
12856 }
12857
12858 if (types.length () > 1)
12859 {
12860 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
12861 {
12862 c_token *token = c_parser_peek_token (parser);
12863 if (token->type == CPP_EOF)
12864 goto fail;
12865 clauses.safe_push (*token);
12866 c_parser_consume_token (parser);
12867 }
12868 clauses.safe_push (*c_parser_peek_token (parser));
12869 c_parser_skip_to_pragma_eol (parser);
12870
12871 /* Make sure nothing tries to read past the end of the tokens. */
12872 c_token eof_token;
12873 memset (&eof_token, 0, sizeof (eof_token));
12874 eof_token.type = CPP_EOF;
12875 clauses.safe_push (eof_token);
12876 clauses.safe_push (eof_token);
12877 }
12878
12879 int errs = errorcount;
12880 FOR_EACH_VEC_ELT (types, i, type)
12881 {
12882 tokens_avail = parser->tokens_avail;
12883 gcc_assert (parser->tokens == &parser->tokens_buf[0]);
12884 if (!clauses.is_empty ())
12885 {
12886 parser->tokens = clauses.address ();
12887 parser->tokens_avail = clauses.length ();
12888 parser->in_pragma = true;
12889 }
12890
12891 bool nested = current_function_decl != NULL_TREE;
12892 if (nested)
12893 c_push_function_context ();
12894 tree fndecl = build_decl (BUILTINS_LOCATION, FUNCTION_DECL,
12895 reduc_id, default_function_type);
12896 current_function_decl = fndecl;
12897 allocate_struct_function (fndecl, true);
12898 push_scope ();
12899 tree stmt = push_stmt_list ();
12900 /* Intentionally BUILTINS_LOCATION, so that -Wshadow doesn't
12901 warn about these. */
12902 tree omp_out = build_decl (BUILTINS_LOCATION, VAR_DECL,
12903 get_identifier ("omp_out"), type);
12904 DECL_ARTIFICIAL (omp_out) = 1;
12905 DECL_CONTEXT (omp_out) = fndecl;
12906 pushdecl (omp_out);
12907 tree omp_in = build_decl (BUILTINS_LOCATION, VAR_DECL,
12908 get_identifier ("omp_in"), type);
12909 DECL_ARTIFICIAL (omp_in) = 1;
12910 DECL_CONTEXT (omp_in) = fndecl;
12911 pushdecl (omp_in);
12912 struct c_expr combiner = c_parser_expression (parser);
12913 struct c_expr initializer;
12914 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE;
12915 bool bad = false;
12916 initializer.value = error_mark_node;
12917 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
12918 bad = true;
12919 else if (c_parser_next_token_is (parser, CPP_NAME)
12920 && strcmp (IDENTIFIER_POINTER
12921 (c_parser_peek_token (parser)->value),
12922 "initializer") == 0)
12923 {
12924 c_parser_consume_token (parser);
12925 pop_scope ();
12926 push_scope ();
12927 omp_priv = build_decl (BUILTINS_LOCATION, VAR_DECL,
12928 get_identifier ("omp_priv"), type);
12929 DECL_ARTIFICIAL (omp_priv) = 1;
12930 DECL_INITIAL (omp_priv) = error_mark_node;
12931 DECL_CONTEXT (omp_priv) = fndecl;
12932 pushdecl (omp_priv);
12933 omp_orig = build_decl (BUILTINS_LOCATION, VAR_DECL,
12934 get_identifier ("omp_orig"), type);
12935 DECL_ARTIFICIAL (omp_orig) = 1;
12936 DECL_CONTEXT (omp_orig) = fndecl;
12937 pushdecl (omp_orig);
12938 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
12939 bad = true;
12940 else if (!c_parser_next_token_is (parser, CPP_NAME))
12941 {
12942 c_parser_error (parser, "expected %<omp_priv%> or "
12943 "function-name");
12944 bad = true;
12945 }
12946 else if (strcmp (IDENTIFIER_POINTER
12947 (c_parser_peek_token (parser)->value),
12948 "omp_priv") != 0)
12949 {
12950 if (c_parser_peek_2nd_token (parser)->type != CPP_OPEN_PAREN
12951 || c_parser_peek_token (parser)->id_kind != C_ID_ID)
12952 {
12953 c_parser_error (parser, "expected function-name %<(%>");
12954 bad = true;
12955 }
12956 else
12957 initializer = c_parser_postfix_expression (parser);
12958 if (initializer.value
12959 && TREE_CODE (initializer.value) == CALL_EXPR)
12960 {
12961 int j;
12962 tree c = initializer.value;
12963 for (j = 0; j < call_expr_nargs (c); j++)
12964 if (TREE_CODE (CALL_EXPR_ARG (c, j)) == ADDR_EXPR
12965 && TREE_OPERAND (CALL_EXPR_ARG (c, j), 0) == omp_priv)
12966 break;
12967 if (j == call_expr_nargs (c))
12968 error ("one of the initializer call arguments should be "
12969 "%<&omp_priv%>");
12970 }
12971 }
12972 else
12973 {
12974 c_parser_consume_token (parser);
12975 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
12976 bad = true;
12977 else
12978 {
12979 tree st = push_stmt_list ();
12980 start_init (omp_priv, NULL_TREE, 0);
12981 location_t loc = c_parser_peek_token (parser)->location;
12982 struct c_expr init = c_parser_initializer (parser);
12983 finish_init ();
12984 finish_decl (omp_priv, loc, init.value,
12985 init.original_type, NULL_TREE);
12986 pop_stmt_list (st);
12987 }
12988 }
12989 if (!bad
12990 && !c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
12991 bad = true;
12992 }
12993
12994 if (!bad)
12995 {
12996 c_parser_skip_to_pragma_eol (parser);
12997
12998 tree t = tree_cons (type, make_tree_vec (omp_priv ? 6 : 3),
12999 DECL_INITIAL (reduc_decl));
13000 DECL_INITIAL (reduc_decl) = t;
13001 DECL_SOURCE_LOCATION (omp_out) = rloc;
13002 TREE_VEC_ELT (TREE_VALUE (t), 0) = omp_out;
13003 TREE_VEC_ELT (TREE_VALUE (t), 1) = omp_in;
13004 TREE_VEC_ELT (TREE_VALUE (t), 2) = combiner.value;
13005 walk_tree (&combiner.value, c_check_omp_declare_reduction_r,
13006 &TREE_VEC_ELT (TREE_VALUE (t), 0), NULL);
13007 if (omp_priv)
13008 {
13009 DECL_SOURCE_LOCATION (omp_priv) = rloc;
13010 TREE_VEC_ELT (TREE_VALUE (t), 3) = omp_priv;
13011 TREE_VEC_ELT (TREE_VALUE (t), 4) = omp_orig;
13012 TREE_VEC_ELT (TREE_VALUE (t), 5) = initializer.value;
13013 walk_tree (&initializer.value, c_check_omp_declare_reduction_r,
13014 &TREE_VEC_ELT (TREE_VALUE (t), 3), NULL);
13015 walk_tree (&DECL_INITIAL (omp_priv),
13016 c_check_omp_declare_reduction_r,
13017 &TREE_VEC_ELT (TREE_VALUE (t), 3), NULL);
13018 }
13019 }
13020
13021 pop_stmt_list (stmt);
13022 pop_scope ();
13023 if (cfun->language != NULL)
13024 {
13025 ggc_free (cfun->language);
13026 cfun->language = NULL;
13027 }
13028 set_cfun (NULL);
13029 current_function_decl = NULL_TREE;
13030 if (nested)
13031 c_pop_function_context ();
13032
13033 if (!clauses.is_empty ())
13034 {
13035 parser->tokens = &parser->tokens_buf[0];
13036 parser->tokens_avail = tokens_avail;
13037 }
13038 if (bad)
13039 goto fail;
13040 if (errs != errorcount)
13041 break;
13042 }
13043
13044 clauses.release ();
13045 types.release ();
13046 }
13047
13048
13049 /* OpenMP 4.0
13050 #pragma omp declare simd declare-simd-clauses[optseq] new-line
13051 #pragma omp declare reduction (reduction-id : typename-list : expression) \
13052 initializer-clause[opt] new-line
13053 #pragma omp declare target new-line */
13054
13055 static void
13056 c_parser_omp_declare (c_parser *parser, enum pragma_context context)
13057 {
13058 c_parser_consume_pragma (parser);
13059 if (c_parser_next_token_is (parser, CPP_NAME))
13060 {
13061 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
13062 if (strcmp (p, "simd") == 0)
13063 {
13064 /* c_parser_consume_token (parser); done in
13065 c_parser_omp_declare_simd. */
13066 c_parser_omp_declare_simd (parser, context);
13067 return;
13068 }
13069 if (strcmp (p, "reduction") == 0)
13070 {
13071 c_parser_consume_token (parser);
13072 c_parser_omp_declare_reduction (parser, context);
13073 return;
13074 }
13075 if (!flag_openmp) /* flag_openmp_simd */
13076 {
13077 c_parser_skip_to_pragma_eol (parser);
13078 return;
13079 }
13080 if (strcmp (p, "target") == 0)
13081 {
13082 c_parser_consume_token (parser);
13083 c_parser_omp_declare_target (parser);
13084 return;
13085 }
13086 }
13087
13088 c_parser_error (parser, "expected %<simd%> or %<reduction%> "
13089 "or %<target%>");
13090 c_parser_skip_to_pragma_eol (parser);
13091 }
13092
13093 /* Main entry point to parsing most OpenMP pragmas. */
13094
13095 static void
13096 c_parser_omp_construct (c_parser *parser)
13097 {
13098 enum pragma_kind p_kind;
13099 location_t loc;
13100 tree stmt;
13101 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
13102 omp_clause_mask mask (0);
13103
13104 loc = c_parser_peek_token (parser)->location;
13105 p_kind = c_parser_peek_token (parser)->pragma_kind;
13106 c_parser_consume_pragma (parser);
13107
13108 switch (p_kind)
13109 {
13110 case PRAGMA_OMP_ATOMIC:
13111 c_parser_omp_atomic (loc, parser);
13112 return;
13113 case PRAGMA_OMP_CRITICAL:
13114 stmt = c_parser_omp_critical (loc, parser);
13115 break;
13116 case PRAGMA_OMP_DISTRIBUTE:
13117 strcpy (p_name, "#pragma omp");
13118 stmt = c_parser_omp_distribute (loc, parser, p_name, mask, NULL);
13119 break;
13120 case PRAGMA_OMP_FOR:
13121 strcpy (p_name, "#pragma omp");
13122 stmt = c_parser_omp_for (loc, parser, p_name, mask, NULL);
13123 break;
13124 case PRAGMA_OMP_MASTER:
13125 stmt = c_parser_omp_master (loc, parser);
13126 break;
13127 case PRAGMA_OMP_ORDERED:
13128 stmt = c_parser_omp_ordered (loc, parser);
13129 break;
13130 case PRAGMA_OMP_PARALLEL:
13131 strcpy (p_name, "#pragma omp");
13132 stmt = c_parser_omp_parallel (loc, parser, p_name, mask, NULL);
13133 break;
13134 case PRAGMA_OMP_SECTIONS:
13135 strcpy (p_name, "#pragma omp");
13136 stmt = c_parser_omp_sections (loc, parser, p_name, mask, NULL);
13137 break;
13138 case PRAGMA_OMP_SIMD:
13139 strcpy (p_name, "#pragma omp");
13140 stmt = c_parser_omp_simd (loc, parser, p_name, mask, NULL);
13141 break;
13142 case PRAGMA_OMP_SINGLE:
13143 stmt = c_parser_omp_single (loc, parser);
13144 break;
13145 case PRAGMA_OMP_TASK:
13146 stmt = c_parser_omp_task (loc, parser);
13147 break;
13148 case PRAGMA_OMP_TASKGROUP:
13149 stmt = c_parser_omp_taskgroup (parser);
13150 break;
13151 case PRAGMA_OMP_TEAMS:
13152 strcpy (p_name, "#pragma omp");
13153 stmt = c_parser_omp_teams (loc, parser, p_name, mask, NULL);
13154 break;
13155 default:
13156 gcc_unreachable ();
13157 }
13158
13159 if (stmt)
13160 gcc_assert (EXPR_LOCATION (stmt) != UNKNOWN_LOCATION);
13161 }
13162
13163
13164 /* OpenMP 2.5:
13165 # pragma omp threadprivate (variable-list) */
13166
13167 static void
13168 c_parser_omp_threadprivate (c_parser *parser)
13169 {
13170 tree vars, t;
13171 location_t loc;
13172
13173 c_parser_consume_pragma (parser);
13174 loc = c_parser_peek_token (parser)->location;
13175 vars = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
13176
13177 /* Mark every variable in VARS to be assigned thread local storage. */
13178 for (t = vars; t; t = TREE_CHAIN (t))
13179 {
13180 tree v = TREE_PURPOSE (t);
13181
13182 /* FIXME diagnostics: Ideally we should keep individual
13183 locations for all the variables in the var list to make the
13184 following errors more precise. Perhaps
13185 c_parser_omp_var_list_parens() should construct a list of
13186 locations to go along with the var list. */
13187
13188 /* If V had already been marked threadprivate, it doesn't matter
13189 whether it had been used prior to this point. */
13190 if (TREE_CODE (v) != VAR_DECL)
13191 error_at (loc, "%qD is not a variable", v);
13192 else if (TREE_USED (v) && !C_DECL_THREADPRIVATE_P (v))
13193 error_at (loc, "%qE declared %<threadprivate%> after first use", v);
13194 else if (! TREE_STATIC (v) && ! DECL_EXTERNAL (v))
13195 error_at (loc, "automatic variable %qE cannot be %<threadprivate%>", v);
13196 else if (TREE_TYPE (v) == error_mark_node)
13197 ;
13198 else if (! COMPLETE_TYPE_P (TREE_TYPE (v)))
13199 error_at (loc, "%<threadprivate%> %qE has incomplete type", v);
13200 else
13201 {
13202 if (! DECL_THREAD_LOCAL_P (v))
13203 {
13204 DECL_TLS_MODEL (v) = decl_default_tls_model (v);
13205 /* If rtl has been already set for this var, call
13206 make_decl_rtl once again, so that encode_section_info
13207 has a chance to look at the new decl flags. */
13208 if (DECL_RTL_SET_P (v))
13209 make_decl_rtl (v);
13210 }
13211 C_DECL_THREADPRIVATE_P (v) = 1;
13212 }
13213 }
13214
13215 c_parser_skip_to_pragma_eol (parser);
13216 }
13217
13218 /* Parse a transaction attribute (GCC Extension).
13219
13220 transaction-attribute:
13221 attributes
13222 [ [ any-word ] ]
13223
13224 The transactional memory language description is written for C++,
13225 and uses the C++0x attribute syntax. For compatibility, allow the
13226 bracket style for transactions in C as well. */
13227
13228 static tree
13229 c_parser_transaction_attributes (c_parser *parser)
13230 {
13231 tree attr_name, attr = NULL;
13232
13233 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
13234 return c_parser_attributes (parser);
13235
13236 if (!c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
13237 return NULL_TREE;
13238 c_parser_consume_token (parser);
13239 if (!c_parser_require (parser, CPP_OPEN_SQUARE, "expected %<[%>"))
13240 goto error1;
13241
13242 attr_name = c_parser_attribute_any_word (parser);
13243 if (attr_name)
13244 {
13245 c_parser_consume_token (parser);
13246 attr = build_tree_list (attr_name, NULL_TREE);
13247 }
13248 else
13249 c_parser_error (parser, "expected identifier");
13250
13251 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, "expected %<]%>");
13252 error1:
13253 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, "expected %<]%>");
13254 return attr;
13255 }
13256
13257 /* Parse a __transaction_atomic or __transaction_relaxed statement
13258 (GCC Extension).
13259
13260 transaction-statement:
13261 __transaction_atomic transaction-attribute[opt] compound-statement
13262 __transaction_relaxed compound-statement
13263
13264 Note that the only valid attribute is: "outer".
13265 */
13266
13267 static tree
13268 c_parser_transaction (c_parser *parser, enum rid keyword)
13269 {
13270 unsigned int old_in = parser->in_transaction;
13271 unsigned int this_in = 1, new_in;
13272 location_t loc = c_parser_peek_token (parser)->location;
13273 tree stmt, attrs;
13274
13275 gcc_assert ((keyword == RID_TRANSACTION_ATOMIC
13276 || keyword == RID_TRANSACTION_RELAXED)
13277 && c_parser_next_token_is_keyword (parser, keyword));
13278 c_parser_consume_token (parser);
13279
13280 if (keyword == RID_TRANSACTION_RELAXED)
13281 this_in |= TM_STMT_ATTR_RELAXED;
13282 else
13283 {
13284 attrs = c_parser_transaction_attributes (parser);
13285 if (attrs)
13286 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
13287 }
13288
13289 /* Keep track if we're in the lexical scope of an outer transaction. */
13290 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
13291
13292 parser->in_transaction = new_in;
13293 stmt = c_parser_compound_statement (parser);
13294 parser->in_transaction = old_in;
13295
13296 if (flag_tm)
13297 stmt = c_finish_transaction (loc, stmt, this_in);
13298 else
13299 error_at (loc, (keyword == RID_TRANSACTION_ATOMIC ?
13300 "%<__transaction_atomic%> without transactional memory support enabled"
13301 : "%<__transaction_relaxed %> "
13302 "without transactional memory support enabled"));
13303
13304 return stmt;
13305 }
13306
13307 /* Parse a __transaction_atomic or __transaction_relaxed expression
13308 (GCC Extension).
13309
13310 transaction-expression:
13311 __transaction_atomic ( expression )
13312 __transaction_relaxed ( expression )
13313 */
13314
13315 static struct c_expr
13316 c_parser_transaction_expression (c_parser *parser, enum rid keyword)
13317 {
13318 struct c_expr ret;
13319 unsigned int old_in = parser->in_transaction;
13320 unsigned int this_in = 1;
13321 location_t loc = c_parser_peek_token (parser)->location;
13322 tree attrs;
13323
13324 gcc_assert ((keyword == RID_TRANSACTION_ATOMIC
13325 || keyword == RID_TRANSACTION_RELAXED)
13326 && c_parser_next_token_is_keyword (parser, keyword));
13327 c_parser_consume_token (parser);
13328
13329 if (keyword == RID_TRANSACTION_RELAXED)
13330 this_in |= TM_STMT_ATTR_RELAXED;
13331 else
13332 {
13333 attrs = c_parser_transaction_attributes (parser);
13334 if (attrs)
13335 this_in |= parse_tm_stmt_attr (attrs, 0);
13336 }
13337
13338 parser->in_transaction = this_in;
13339 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
13340 {
13341 tree expr = c_parser_expression (parser).value;
13342 ret.original_type = TREE_TYPE (expr);
13343 ret.value = build1 (TRANSACTION_EXPR, ret.original_type, expr);
13344 if (this_in & TM_STMT_ATTR_RELAXED)
13345 TRANSACTION_EXPR_RELAXED (ret.value) = 1;
13346 SET_EXPR_LOCATION (ret.value, loc);
13347 ret.original_code = TRANSACTION_EXPR;
13348 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
13349 {
13350 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
13351 goto error;
13352 }
13353 }
13354 else
13355 {
13356 error:
13357 ret.value = error_mark_node;
13358 ret.original_code = ERROR_MARK;
13359 ret.original_type = NULL;
13360 }
13361 parser->in_transaction = old_in;
13362
13363 if (!flag_tm)
13364 error_at (loc, (keyword == RID_TRANSACTION_ATOMIC ?
13365 "%<__transaction_atomic%> without transactional memory support enabled"
13366 : "%<__transaction_relaxed %> "
13367 "without transactional memory support enabled"));
13368
13369 return ret;
13370 }
13371
13372 /* Parse a __transaction_cancel statement (GCC Extension).
13373
13374 transaction-cancel-statement:
13375 __transaction_cancel transaction-attribute[opt] ;
13376
13377 Note that the only valid attribute is "outer".
13378 */
13379
13380 static tree
13381 c_parser_transaction_cancel (c_parser *parser)
13382 {
13383 location_t loc = c_parser_peek_token (parser)->location;
13384 tree attrs;
13385 bool is_outer = false;
13386
13387 gcc_assert (c_parser_next_token_is_keyword (parser, RID_TRANSACTION_CANCEL));
13388 c_parser_consume_token (parser);
13389
13390 attrs = c_parser_transaction_attributes (parser);
13391 if (attrs)
13392 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
13393
13394 if (!flag_tm)
13395 {
13396 error_at (loc, "%<__transaction_cancel%> without "
13397 "transactional memory support enabled");
13398 goto ret_error;
13399 }
13400 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
13401 {
13402 error_at (loc, "%<__transaction_cancel%> within a "
13403 "%<__transaction_relaxed%>");
13404 goto ret_error;
13405 }
13406 else if (is_outer)
13407 {
13408 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
13409 && !is_tm_may_cancel_outer (current_function_decl))
13410 {
13411 error_at (loc, "outer %<__transaction_cancel%> not "
13412 "within outer %<__transaction_atomic%>");
13413 error_at (loc, " or a %<transaction_may_cancel_outer%> function");
13414 goto ret_error;
13415 }
13416 }
13417 else if (parser->in_transaction == 0)
13418 {
13419 error_at (loc, "%<__transaction_cancel%> not within "
13420 "%<__transaction_atomic%>");
13421 goto ret_error;
13422 }
13423
13424 return add_stmt (build_tm_abort_call (loc, is_outer));
13425
13426 ret_error:
13427 return build1 (NOP_EXPR, void_type_node, error_mark_node);
13428 }
13429 \f
13430 /* Parse a single source file. */
13431
13432 void
13433 c_parse_file (void)
13434 {
13435 /* Use local storage to begin. If the first token is a pragma, parse it.
13436 If it is #pragma GCC pch_preprocess, then this will load a PCH file
13437 which will cause garbage collection. */
13438 c_parser tparser;
13439
13440 memset (&tparser, 0, sizeof tparser);
13441 tparser.tokens = &tparser.tokens_buf[0];
13442 the_parser = &tparser;
13443
13444 if (c_parser_peek_token (&tparser)->pragma_kind == PRAGMA_GCC_PCH_PREPROCESS)
13445 c_parser_pragma_pch_preprocess (&tparser);
13446
13447 the_parser = ggc_alloc_c_parser ();
13448 *the_parser = tparser;
13449 if (tparser.tokens == &tparser.tokens_buf[0])
13450 the_parser->tokens = &the_parser->tokens_buf[0];
13451
13452 /* Initialize EH, if we've been told to do so. */
13453 if (flag_exceptions)
13454 using_eh_for_cleanups ();
13455
13456 c_parser_translation_unit (the_parser);
13457 the_parser = NULL;
13458 }
13459
13460 /* This function parses Cilk Plus array notation. The starting index is
13461 passed in INITIAL_INDEX and the array name is passes in ARRAY_VALUE. The
13462 return value of this function is a tree_node called VALUE_TREE of type
13463 ARRAY_NOTATION_REF. */
13464
13465 static tree
13466 c_parser_array_notation (location_t loc, c_parser *parser, tree initial_index,
13467 tree array_value)
13468 {
13469 c_token *token = NULL;
13470 tree start_index = NULL_TREE, end_index = NULL_TREE, stride = NULL_TREE;
13471 tree value_tree = NULL_TREE, type = NULL_TREE, array_type = NULL_TREE;
13472 tree array_type_domain = NULL_TREE;
13473
13474 if (array_value == error_mark_node)
13475 {
13476 /* No need to continue. If either of these 2 were true, then an error
13477 must be emitted already. Thus, no need to emit them twice. */
13478 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
13479 return error_mark_node;
13480 }
13481
13482 array_type = TREE_TYPE (array_value);
13483 gcc_assert (array_type);
13484 type = TREE_TYPE (array_type);
13485 token = c_parser_peek_token (parser);
13486
13487 if (token->type == CPP_EOF)
13488 {
13489 c_parser_error (parser, "expected %<:%> or numeral");
13490 return value_tree;
13491 }
13492 else if (token->type == CPP_COLON)
13493 {
13494 if (!initial_index)
13495 {
13496 /* If we are here, then we have a case like this A[:]. */
13497 c_parser_consume_token (parser);
13498 if (TREE_CODE (array_type) == POINTER_TYPE)
13499 {
13500 error_at (loc, "start-index and length fields necessary for "
13501 "using array notations in pointers");
13502 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
13503 return error_mark_node;
13504 }
13505 if (TREE_CODE (array_type) == FUNCTION_TYPE)
13506 {
13507 error_at (loc, "array notations cannot be used with function "
13508 "type");
13509 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
13510 return error_mark_node;
13511 }
13512 array_type_domain = TYPE_DOMAIN (array_type);
13513
13514 if (!array_type_domain)
13515 {
13516 error_at (loc, "start-index and length fields necessary for "
13517 "using array notations in dimensionless arrays");
13518 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
13519 return error_mark_node;
13520 }
13521
13522 start_index = TYPE_MINVAL (array_type_domain);
13523 start_index = fold_build1 (CONVERT_EXPR, ptrdiff_type_node,
13524 start_index);
13525 if (!TYPE_MAXVAL (array_type_domain)
13526 || !TREE_CONSTANT (TYPE_MAXVAL (array_type_domain)))
13527 {
13528 error_at (loc, "start-index and length fields necessary for "
13529 "using array notations in variable-length arrays");
13530 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
13531 return error_mark_node;
13532 }
13533 end_index = TYPE_MAXVAL (array_type_domain);
13534 end_index = fold_build2 (PLUS_EXPR, TREE_TYPE (end_index),
13535 end_index, integer_one_node);
13536 end_index = fold_build1 (CONVERT_EXPR, ptrdiff_type_node, end_index);
13537 stride = build_int_cst (integer_type_node, 1);
13538 stride = fold_build1 (CONVERT_EXPR, ptrdiff_type_node, stride);
13539 }
13540 else if (initial_index != error_mark_node)
13541 {
13542 /* If we are here, then there should be 2 possibilities:
13543 1. Array [EXPR : EXPR]
13544 2. Array [EXPR : EXPR : EXPR]
13545 */
13546 start_index = initial_index;
13547
13548 if (TREE_CODE (array_type) == FUNCTION_TYPE)
13549 {
13550 error_at (loc, "array notations cannot be used with function "
13551 "type");
13552 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
13553 return error_mark_node;
13554 }
13555 c_parser_consume_token (parser); /* consume the ':' */
13556 struct c_expr ce = c_parser_expression (parser);
13557 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
13558 end_index = ce.value;
13559 if (!end_index || end_index == error_mark_node)
13560 {
13561 c_parser_skip_to_end_of_block_or_statement (parser);
13562 return error_mark_node;
13563 }
13564 if (c_parser_peek_token (parser)->type == CPP_COLON)
13565 {
13566 c_parser_consume_token (parser);
13567 ce = c_parser_expression (parser);
13568 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
13569 stride = ce.value;
13570 if (!stride || stride == error_mark_node)
13571 {
13572 c_parser_skip_to_end_of_block_or_statement (parser);
13573 return error_mark_node;
13574 }
13575 }
13576 }
13577 else
13578 c_parser_error (parser, "expected array notation expression");
13579 }
13580 else
13581 c_parser_error (parser, "expected array notation expression");
13582
13583 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, "expected %<]%>");
13584
13585 value_tree = build_array_notation_ref (loc, array_value, start_index,
13586 end_index, stride, type);
13587 if (value_tree != error_mark_node)
13588 SET_EXPR_LOCATION (value_tree, loc);
13589 return value_tree;
13590 }
13591
13592 #include "gt-c-c-parser.h"