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