]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/c/c-parser.c
Update copyright years.
[thirdparty/gcc.git] / gcc / c / c-parser.c
1 /* Parser for C and Objective-C.
2 Copyright (C) 1987-2016 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 "target.h"
42 #include "function.h"
43 #include "c-tree.h"
44 #include "timevar.h"
45 #include "stringpool.h"
46 #include "cgraph.h"
47 #include "attribs.h"
48 #include "stor-layout.h"
49 #include "varasm.h"
50 #include "trans-mem.h"
51 #include "c-family/c-pragma.h"
52 #include "c-lang.h"
53 #include "c-family/c-objc.h"
54 #include "plugin.h"
55 #include "omp-low.h"
56 #include "builtins.h"
57 #include "gomp-constants.h"
58 #include "c-family/c-indentation.h"
59 #include "gimple-expr.h"
60 #include "context.h"
61
62 void
63 set_c_expr_source_range (c_expr *expr,
64 location_t start, location_t finish)
65 {
66 expr->src_range.m_start = start;
67 expr->src_range.m_finish = finish;
68 if (expr->value)
69 set_source_range (expr->value, start, finish);
70 }
71
72 void
73 set_c_expr_source_range (c_expr *expr,
74 source_range src_range)
75 {
76 expr->src_range = src_range;
77 if (expr->value)
78 set_source_range (expr->value, src_range);
79 }
80
81 \f
82 /* Initialization routine for this file. */
83
84 void
85 c_parse_init (void)
86 {
87 /* The only initialization required is of the reserved word
88 identifiers. */
89 unsigned int i;
90 tree id;
91 int mask = 0;
92
93 /* Make sure RID_MAX hasn't grown past the 8 bits used to hold the keyword in
94 the c_token structure. */
95 gcc_assert (RID_MAX <= 255);
96
97 mask |= D_CXXONLY;
98 if (!flag_isoc99)
99 mask |= D_C99;
100 if (flag_no_asm)
101 {
102 mask |= D_ASM | D_EXT;
103 if (!flag_isoc99)
104 mask |= D_EXT89;
105 }
106 if (!c_dialect_objc ())
107 mask |= D_OBJC | D_CXX_OBJC;
108
109 ridpointers = ggc_cleared_vec_alloc<tree> ((int) RID_MAX);
110 for (i = 0; i < num_c_common_reswords; i++)
111 {
112 /* If a keyword is disabled, do not enter it into the table
113 and so create a canonical spelling that isn't a keyword. */
114 if (c_common_reswords[i].disable & mask)
115 {
116 if (warn_cxx_compat
117 && (c_common_reswords[i].disable & D_CXXWARN))
118 {
119 id = get_identifier (c_common_reswords[i].word);
120 C_SET_RID_CODE (id, RID_CXX_COMPAT_WARN);
121 C_IS_RESERVED_WORD (id) = 1;
122 }
123 continue;
124 }
125
126 id = get_identifier (c_common_reswords[i].word);
127 C_SET_RID_CODE (id, c_common_reswords[i].rid);
128 C_IS_RESERVED_WORD (id) = 1;
129 ridpointers [(int) c_common_reswords[i].rid] = id;
130 }
131
132 for (i = 0; i < NUM_INT_N_ENTS; i++)
133 {
134 /* We always create the symbols but they aren't always supported. */
135 char name[50];
136 sprintf (name, "__int%d", int_n_data[i].bitsize);
137 id = get_identifier (name);
138 C_SET_RID_CODE (id, RID_FIRST_INT_N + i);
139 C_IS_RESERVED_WORD (id) = 1;
140 }
141 }
142 \f
143 /* The C lexer intermediates between the lexer in cpplib and c-lex.c
144 and the C parser. Unlike the C++ lexer, the parser structure
145 stores the lexer information instead of using a separate structure.
146 Identifiers are separated into ordinary identifiers, type names,
147 keywords and some other Objective-C types of identifiers, and some
148 look-ahead is maintained.
149
150 ??? It might be a good idea to lex the whole file up front (as for
151 C++). It would then be possible to share more of the C and C++
152 lexer code, if desired. */
153
154 /* More information about the type of a CPP_NAME token. */
155 enum c_id_kind {
156 /* An ordinary identifier. */
157 C_ID_ID,
158 /* An identifier declared as a typedef name. */
159 C_ID_TYPENAME,
160 /* An identifier declared as an Objective-C class name. */
161 C_ID_CLASSNAME,
162 /* An address space identifier. */
163 C_ID_ADDRSPACE,
164 /* Not an identifier. */
165 C_ID_NONE
166 };
167
168 /* A single C token after string literal concatenation and conversion
169 of preprocessing tokens to tokens. */
170 struct GTY (()) c_token {
171 /* The kind of token. */
172 ENUM_BITFIELD (cpp_ttype) type : 8;
173 /* If this token is a CPP_NAME, this value indicates whether also
174 declared as some kind of type. Otherwise, it is C_ID_NONE. */
175 ENUM_BITFIELD (c_id_kind) id_kind : 8;
176 /* If this token is a keyword, this value indicates which keyword.
177 Otherwise, this value is RID_MAX. */
178 ENUM_BITFIELD (rid) keyword : 8;
179 /* If this token is a CPP_PRAGMA, this indicates the pragma that
180 was seen. Otherwise it is PRAGMA_NONE. */
181 ENUM_BITFIELD (pragma_kind) pragma_kind : 8;
182 /* The location at which this token was found. */
183 location_t location;
184 /* The value associated with this token, if any. */
185 tree value;
186
187 source_range get_range () const
188 {
189 return get_range_from_loc (line_table, location);
190 }
191
192 location_t get_finish () const
193 {
194 return get_range ().m_finish;
195 }
196 };
197
198 /* A parser structure recording information about the state and
199 context of parsing. Includes lexer information with up to two
200 tokens of look-ahead; more are not needed for C. */
201 struct GTY(()) c_parser {
202 /* The look-ahead tokens. */
203 c_token * GTY((skip)) tokens;
204 /* Buffer for look-ahead tokens. */
205 c_token tokens_buf[4];
206 /* How many look-ahead tokens are available (0 - 4, or
207 more if parsing from pre-lexed tokens). */
208 unsigned int tokens_avail;
209 /* True if a syntax error is being recovered from; false otherwise.
210 c_parser_error sets this flag. It should clear this flag when
211 enough tokens have been consumed to recover from the error. */
212 BOOL_BITFIELD error : 1;
213 /* True if we're processing a pragma, and shouldn't automatically
214 consume CPP_PRAGMA_EOL. */
215 BOOL_BITFIELD in_pragma : 1;
216 /* True if we're parsing the outermost block of an if statement. */
217 BOOL_BITFIELD in_if_block : 1;
218 /* True if we want to lex an untranslated string. */
219 BOOL_BITFIELD lex_untranslated_string : 1;
220
221 /* Objective-C specific parser/lexer information. */
222
223 /* True if we are in a context where the Objective-C "PQ" keywords
224 are considered keywords. */
225 BOOL_BITFIELD objc_pq_context : 1;
226 /* True if we are parsing a (potential) Objective-C foreach
227 statement. This is set to true after we parsed 'for (' and while
228 we wait for 'in' or ';' to decide if it's a standard C for loop or an
229 Objective-C foreach loop. */
230 BOOL_BITFIELD objc_could_be_foreach_context : 1;
231 /* The following flag is needed to contextualize Objective-C lexical
232 analysis. In some cases (e.g., 'int NSObject;'), it is
233 undesirable to bind an identifier to an Objective-C class, even
234 if a class with that name exists. */
235 BOOL_BITFIELD objc_need_raw_identifier : 1;
236 /* Nonzero if we're processing a __transaction statement. The value
237 is 1 | TM_STMT_ATTR_*. */
238 unsigned int in_transaction : 4;
239 /* True if we are in a context where the Objective-C "Property attribute"
240 keywords are valid. */
241 BOOL_BITFIELD objc_property_attr_context : 1;
242
243 /* Cilk Plus specific parser/lexer information. */
244
245 /* Buffer to hold all the tokens from parsing the vector attribute for the
246 SIMD-enabled functions (formerly known as elemental functions). */
247 vec <c_token, va_gc> *cilk_simd_fn_tokens;
248 };
249
250
251 /* The actual parser and external interface. ??? Does this need to be
252 garbage-collected? */
253
254 static GTY (()) c_parser *the_parser;
255
256 /* Read in and lex a single token, storing it in *TOKEN. */
257
258 static void
259 c_lex_one_token (c_parser *parser, c_token *token)
260 {
261 timevar_push (TV_LEX);
262
263 token->type = c_lex_with_flags (&token->value, &token->location, NULL,
264 (parser->lex_untranslated_string
265 ? C_LEX_STRING_NO_TRANSLATE : 0));
266 token->id_kind = C_ID_NONE;
267 token->keyword = RID_MAX;
268 token->pragma_kind = PRAGMA_NONE;
269
270 switch (token->type)
271 {
272 case CPP_NAME:
273 {
274 tree decl;
275
276 bool objc_force_identifier = parser->objc_need_raw_identifier;
277 if (c_dialect_objc ())
278 parser->objc_need_raw_identifier = false;
279
280 if (C_IS_RESERVED_WORD (token->value))
281 {
282 enum rid rid_code = C_RID_CODE (token->value);
283
284 if (rid_code == RID_CXX_COMPAT_WARN)
285 {
286 warning_at (token->location,
287 OPT_Wc___compat,
288 "identifier %qE conflicts with C++ keyword",
289 token->value);
290 }
291 else if (rid_code >= RID_FIRST_ADDR_SPACE
292 && rid_code <= RID_LAST_ADDR_SPACE)
293 {
294 token->id_kind = C_ID_ADDRSPACE;
295 token->keyword = rid_code;
296 break;
297 }
298 else if (c_dialect_objc () && OBJC_IS_PQ_KEYWORD (rid_code))
299 {
300 /* We found an Objective-C "pq" keyword (in, out,
301 inout, bycopy, byref, oneway). They need special
302 care because the interpretation depends on the
303 context. */
304 if (parser->objc_pq_context)
305 {
306 token->type = CPP_KEYWORD;
307 token->keyword = rid_code;
308 break;
309 }
310 else if (parser->objc_could_be_foreach_context
311 && rid_code == RID_IN)
312 {
313 /* We are in Objective-C, inside a (potential)
314 foreach context (which means after having
315 parsed 'for (', but before having parsed ';'),
316 and we found 'in'. We consider it the keyword
317 which terminates the declaration at the
318 beginning of a foreach-statement. Note that
319 this means you can't use 'in' for anything else
320 in that context; in particular, in Objective-C
321 you can't use 'in' as the name of the running
322 variable in a C for loop. We could potentially
323 try to add code here to disambiguate, but it
324 seems a reasonable limitation. */
325 token->type = CPP_KEYWORD;
326 token->keyword = rid_code;
327 break;
328 }
329 /* Else, "pq" keywords outside of the "pq" context are
330 not keywords, and we fall through to the code for
331 normal tokens. */
332 }
333 else if (c_dialect_objc () && OBJC_IS_PATTR_KEYWORD (rid_code))
334 {
335 /* We found an Objective-C "property attribute"
336 keyword (getter, setter, readonly, etc). These are
337 only valid in the property context. */
338 if (parser->objc_property_attr_context)
339 {
340 token->type = CPP_KEYWORD;
341 token->keyword = rid_code;
342 break;
343 }
344 /* Else they are not special keywords.
345 */
346 }
347 else if (c_dialect_objc ()
348 && (OBJC_IS_AT_KEYWORD (rid_code)
349 || OBJC_IS_CXX_KEYWORD (rid_code)))
350 {
351 /* We found one of the Objective-C "@" keywords (defs,
352 selector, synchronized, etc) or one of the
353 Objective-C "cxx" keywords (class, private,
354 protected, public, try, catch, throw) without a
355 preceding '@' sign. Do nothing and fall through to
356 the code for normal tokens (in C++ we would still
357 consider the CXX ones keywords, but not in C). */
358 ;
359 }
360 else
361 {
362 token->type = CPP_KEYWORD;
363 token->keyword = rid_code;
364 break;
365 }
366 }
367
368 decl = lookup_name (token->value);
369 if (decl)
370 {
371 if (TREE_CODE (decl) == TYPE_DECL)
372 {
373 token->id_kind = C_ID_TYPENAME;
374 break;
375 }
376 }
377 else if (c_dialect_objc ())
378 {
379 tree objc_interface_decl = objc_is_class_name (token->value);
380 /* Objective-C class names are in the same namespace as
381 variables and typedefs, and hence are shadowed by local
382 declarations. */
383 if (objc_interface_decl
384 && (!objc_force_identifier || global_bindings_p ()))
385 {
386 token->value = objc_interface_decl;
387 token->id_kind = C_ID_CLASSNAME;
388 break;
389 }
390 }
391 token->id_kind = C_ID_ID;
392 }
393 break;
394 case CPP_AT_NAME:
395 /* This only happens in Objective-C; it must be a keyword. */
396 token->type = CPP_KEYWORD;
397 switch (C_RID_CODE (token->value))
398 {
399 /* Replace 'class' with '@class', 'private' with '@private',
400 etc. This prevents confusion with the C++ keyword
401 'class', and makes the tokens consistent with other
402 Objective-C 'AT' keywords. For example '@class' is
403 reported as RID_AT_CLASS which is consistent with
404 '@synchronized', which is reported as
405 RID_AT_SYNCHRONIZED.
406 */
407 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
408 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
409 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
410 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
411 case RID_THROW: token->keyword = RID_AT_THROW; break;
412 case RID_TRY: token->keyword = RID_AT_TRY; break;
413 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
414 case RID_SYNCHRONIZED: token->keyword = RID_AT_SYNCHRONIZED; break;
415 default: token->keyword = C_RID_CODE (token->value);
416 }
417 break;
418 case CPP_COLON:
419 case CPP_COMMA:
420 case CPP_CLOSE_PAREN:
421 case CPP_SEMICOLON:
422 /* These tokens may affect the interpretation of any identifiers
423 following, if doing Objective-C. */
424 if (c_dialect_objc ())
425 parser->objc_need_raw_identifier = false;
426 break;
427 case CPP_PRAGMA:
428 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
429 token->pragma_kind = (enum pragma_kind) TREE_INT_CST_LOW (token->value);
430 token->value = NULL;
431 break;
432 default:
433 break;
434 }
435 timevar_pop (TV_LEX);
436 }
437
438 /* Return a pointer to the next token from PARSER, reading it in if
439 necessary. */
440
441 static inline c_token *
442 c_parser_peek_token (c_parser *parser)
443 {
444 if (parser->tokens_avail == 0)
445 {
446 c_lex_one_token (parser, &parser->tokens[0]);
447 parser->tokens_avail = 1;
448 }
449 return &parser->tokens[0];
450 }
451
452 /* Return true if the next token from PARSER has the indicated
453 TYPE. */
454
455 static inline bool
456 c_parser_next_token_is (c_parser *parser, enum cpp_ttype type)
457 {
458 return c_parser_peek_token (parser)->type == type;
459 }
460
461 /* Return true if the next token from PARSER does not have the
462 indicated TYPE. */
463
464 static inline bool
465 c_parser_next_token_is_not (c_parser *parser, enum cpp_ttype type)
466 {
467 return !c_parser_next_token_is (parser, type);
468 }
469
470 /* Return true if the next token from PARSER is the indicated
471 KEYWORD. */
472
473 static inline bool
474 c_parser_next_token_is_keyword (c_parser *parser, enum rid keyword)
475 {
476 return c_parser_peek_token (parser)->keyword == keyword;
477 }
478
479 /* Return a pointer to the next-but-one token from PARSER, reading it
480 in if necessary. The next token is already read in. */
481
482 static c_token *
483 c_parser_peek_2nd_token (c_parser *parser)
484 {
485 if (parser->tokens_avail >= 2)
486 return &parser->tokens[1];
487 gcc_assert (parser->tokens_avail == 1);
488 gcc_assert (parser->tokens[0].type != CPP_EOF);
489 gcc_assert (parser->tokens[0].type != CPP_PRAGMA_EOL);
490 c_lex_one_token (parser, &parser->tokens[1]);
491 parser->tokens_avail = 2;
492 return &parser->tokens[1];
493 }
494
495 /* Return a pointer to the Nth token from PARSER, reading it
496 in if necessary. The N-1th token is already read in. */
497
498 static c_token *
499 c_parser_peek_nth_token (c_parser *parser, unsigned int n)
500 {
501 /* N is 1-based, not zero-based. */
502 gcc_assert (n > 0);
503
504 if (parser->tokens_avail >= n)
505 return &parser->tokens[n - 1];
506 gcc_assert (parser->tokens_avail == n - 1);
507 c_lex_one_token (parser, &parser->tokens[n - 1]);
508 parser->tokens_avail = n;
509 return &parser->tokens[n - 1];
510 }
511
512 /* Return true if TOKEN can start a type name,
513 false otherwise. */
514 static bool
515 c_token_starts_typename (c_token *token)
516 {
517 switch (token->type)
518 {
519 case CPP_NAME:
520 switch (token->id_kind)
521 {
522 case C_ID_ID:
523 return false;
524 case C_ID_ADDRSPACE:
525 return true;
526 case C_ID_TYPENAME:
527 return true;
528 case C_ID_CLASSNAME:
529 gcc_assert (c_dialect_objc ());
530 return true;
531 default:
532 gcc_unreachable ();
533 }
534 case CPP_KEYWORD:
535 switch (token->keyword)
536 {
537 case RID_UNSIGNED:
538 case RID_LONG:
539 case RID_SHORT:
540 case RID_SIGNED:
541 case RID_COMPLEX:
542 case RID_INT:
543 case RID_CHAR:
544 case RID_FLOAT:
545 case RID_DOUBLE:
546 case RID_VOID:
547 case RID_DFLOAT32:
548 case RID_DFLOAT64:
549 case RID_DFLOAT128:
550 case RID_BOOL:
551 case RID_ENUM:
552 case RID_STRUCT:
553 case RID_UNION:
554 case RID_TYPEOF:
555 case RID_CONST:
556 case RID_ATOMIC:
557 case RID_VOLATILE:
558 case RID_RESTRICT:
559 case RID_ATTRIBUTE:
560 case RID_FRACT:
561 case RID_ACCUM:
562 case RID_SAT:
563 case RID_AUTO_TYPE:
564 return true;
565 default:
566 if (token->keyword >= RID_FIRST_INT_N
567 && token->keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
568 && int_n_enabled_p[token->keyword - RID_FIRST_INT_N])
569 return true;
570 return false;
571 }
572 case CPP_LESS:
573 if (c_dialect_objc ())
574 return true;
575 return false;
576 default:
577 return false;
578 }
579 }
580
581 enum c_lookahead_kind {
582 /* Always treat unknown identifiers as typenames. */
583 cla_prefer_type,
584
585 /* Could be parsing a nonabstract declarator. Only treat an identifier
586 as a typename if followed by another identifier or a star. */
587 cla_nonabstract_decl,
588
589 /* Never treat identifiers as typenames. */
590 cla_prefer_id
591 };
592
593 /* Return true if the next token from PARSER can start a type name,
594 false otherwise. LA specifies how to do lookahead in order to
595 detect unknown type names. If unsure, pick CLA_PREFER_ID. */
596
597 static inline bool
598 c_parser_next_tokens_start_typename (c_parser *parser, enum c_lookahead_kind la)
599 {
600 c_token *token = c_parser_peek_token (parser);
601 if (c_token_starts_typename (token))
602 return true;
603
604 /* Try a bit harder to detect an unknown typename. */
605 if (la != cla_prefer_id
606 && token->type == CPP_NAME
607 && token->id_kind == C_ID_ID
608
609 /* Do not try too hard when we could have "object in array". */
610 && !parser->objc_could_be_foreach_context
611
612 && (la == cla_prefer_type
613 || c_parser_peek_2nd_token (parser)->type == CPP_NAME
614 || c_parser_peek_2nd_token (parser)->type == CPP_MULT)
615
616 /* Only unknown identifiers. */
617 && !lookup_name (token->value))
618 return true;
619
620 return false;
621 }
622
623 /* Return true if TOKEN is a type qualifier, false otherwise. */
624 static bool
625 c_token_is_qualifier (c_token *token)
626 {
627 switch (token->type)
628 {
629 case CPP_NAME:
630 switch (token->id_kind)
631 {
632 case C_ID_ADDRSPACE:
633 return true;
634 default:
635 return false;
636 }
637 case CPP_KEYWORD:
638 switch (token->keyword)
639 {
640 case RID_CONST:
641 case RID_VOLATILE:
642 case RID_RESTRICT:
643 case RID_ATTRIBUTE:
644 case RID_ATOMIC:
645 return true;
646 default:
647 return false;
648 }
649 case CPP_LESS:
650 return false;
651 default:
652 gcc_unreachable ();
653 }
654 }
655
656 /* Return true if the next token from PARSER is a type qualifier,
657 false otherwise. */
658 static inline bool
659 c_parser_next_token_is_qualifier (c_parser *parser)
660 {
661 c_token *token = c_parser_peek_token (parser);
662 return c_token_is_qualifier (token);
663 }
664
665 /* Return true if TOKEN can start declaration specifiers, false
666 otherwise. */
667 static bool
668 c_token_starts_declspecs (c_token *token)
669 {
670 switch (token->type)
671 {
672 case CPP_NAME:
673 switch (token->id_kind)
674 {
675 case C_ID_ID:
676 return false;
677 case C_ID_ADDRSPACE:
678 return true;
679 case C_ID_TYPENAME:
680 return true;
681 case C_ID_CLASSNAME:
682 gcc_assert (c_dialect_objc ());
683 return true;
684 default:
685 gcc_unreachable ();
686 }
687 case CPP_KEYWORD:
688 switch (token->keyword)
689 {
690 case RID_STATIC:
691 case RID_EXTERN:
692 case RID_REGISTER:
693 case RID_TYPEDEF:
694 case RID_INLINE:
695 case RID_NORETURN:
696 case RID_AUTO:
697 case RID_THREAD:
698 case RID_UNSIGNED:
699 case RID_LONG:
700 case RID_SHORT:
701 case RID_SIGNED:
702 case RID_COMPLEX:
703 case RID_INT:
704 case RID_CHAR:
705 case RID_FLOAT:
706 case RID_DOUBLE:
707 case RID_VOID:
708 case RID_DFLOAT32:
709 case RID_DFLOAT64:
710 case RID_DFLOAT128:
711 case RID_BOOL:
712 case RID_ENUM:
713 case RID_STRUCT:
714 case RID_UNION:
715 case RID_TYPEOF:
716 case RID_CONST:
717 case RID_VOLATILE:
718 case RID_RESTRICT:
719 case RID_ATTRIBUTE:
720 case RID_FRACT:
721 case RID_ACCUM:
722 case RID_SAT:
723 case RID_ALIGNAS:
724 case RID_ATOMIC:
725 case RID_AUTO_TYPE:
726 return true;
727 default:
728 if (token->keyword >= RID_FIRST_INT_N
729 && token->keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
730 && int_n_enabled_p[token->keyword - RID_FIRST_INT_N])
731 return true;
732 return false;
733 }
734 case CPP_LESS:
735 if (c_dialect_objc ())
736 return true;
737 return false;
738 default:
739 return false;
740 }
741 }
742
743
744 /* Return true if TOKEN can start declaration specifiers or a static
745 assertion, false otherwise. */
746 static bool
747 c_token_starts_declaration (c_token *token)
748 {
749 if (c_token_starts_declspecs (token)
750 || token->keyword == RID_STATIC_ASSERT)
751 return true;
752 else
753 return false;
754 }
755
756 /* Return true if the next token from PARSER can start declaration
757 specifiers, false otherwise. */
758 static inline bool
759 c_parser_next_token_starts_declspecs (c_parser *parser)
760 {
761 c_token *token = c_parser_peek_token (parser);
762
763 /* In Objective-C, a classname normally starts a declspecs unless it
764 is immediately followed by a dot. In that case, it is the
765 Objective-C 2.0 "dot-syntax" for class objects, ie, calls the
766 setter/getter on the class. c_token_starts_declspecs() can't
767 differentiate between the two cases because it only checks the
768 current token, so we have a special check here. */
769 if (c_dialect_objc ()
770 && token->type == CPP_NAME
771 && token->id_kind == C_ID_CLASSNAME
772 && c_parser_peek_2nd_token (parser)->type == CPP_DOT)
773 return false;
774
775 return c_token_starts_declspecs (token);
776 }
777
778 /* Return true if the next tokens from PARSER can start declaration
779 specifiers or a static assertion, false otherwise. */
780 static inline bool
781 c_parser_next_tokens_start_declaration (c_parser *parser)
782 {
783 c_token *token = c_parser_peek_token (parser);
784
785 /* Same as above. */
786 if (c_dialect_objc ()
787 && token->type == CPP_NAME
788 && token->id_kind == C_ID_CLASSNAME
789 && c_parser_peek_2nd_token (parser)->type == CPP_DOT)
790 return false;
791
792 /* Labels do not start declarations. */
793 if (token->type == CPP_NAME
794 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
795 return false;
796
797 if (c_token_starts_declaration (token))
798 return true;
799
800 if (c_parser_next_tokens_start_typename (parser, cla_nonabstract_decl))
801 return true;
802
803 return false;
804 }
805
806 /* Consume the next token from PARSER. */
807
808 static void
809 c_parser_consume_token (c_parser *parser)
810 {
811 gcc_assert (parser->tokens_avail >= 1);
812 gcc_assert (parser->tokens[0].type != CPP_EOF);
813 gcc_assert (!parser->in_pragma || parser->tokens[0].type != CPP_PRAGMA_EOL);
814 gcc_assert (parser->error || parser->tokens[0].type != CPP_PRAGMA);
815 if (parser->tokens != &parser->tokens_buf[0])
816 parser->tokens++;
817 else if (parser->tokens_avail == 2)
818 parser->tokens[0] = parser->tokens[1];
819 parser->tokens_avail--;
820 }
821
822 /* Expect the current token to be a #pragma. Consume it and remember
823 that we've begun parsing a pragma. */
824
825 static void
826 c_parser_consume_pragma (c_parser *parser)
827 {
828 gcc_assert (!parser->in_pragma);
829 gcc_assert (parser->tokens_avail >= 1);
830 gcc_assert (parser->tokens[0].type == CPP_PRAGMA);
831 if (parser->tokens != &parser->tokens_buf[0])
832 parser->tokens++;
833 else if (parser->tokens_avail == 2)
834 parser->tokens[0] = parser->tokens[1];
835 parser->tokens_avail--;
836 parser->in_pragma = true;
837 }
838
839 /* Update the global input_location from TOKEN. */
840 static inline void
841 c_parser_set_source_position_from_token (c_token *token)
842 {
843 if (token->type != CPP_EOF)
844 {
845 input_location = token->location;
846 }
847 }
848
849 /* Helper function for c_parser_error.
850 Having peeked a token of kind TOK1_KIND that might signify
851 a conflict marker, peek successor tokens to determine
852 if we actually do have a conflict marker.
853 Specifically, we consider a run of 7 '<', '=' or '>' characters
854 at the start of a line as a conflict marker.
855 These come through the lexer as three pairs and a single,
856 e.g. three CPP_LSHIFT ("<<") and a CPP_LESS ('<').
857 If it returns true, *OUT_LOC is written to with the location/range
858 of the marker. */
859
860 static bool
861 c_parser_peek_conflict_marker (c_parser *parser, enum cpp_ttype tok1_kind,
862 location_t *out_loc)
863 {
864 c_token *token2 = c_parser_peek_2nd_token (parser);
865 if (token2->type != tok1_kind)
866 return false;
867 c_token *token3 = c_parser_peek_nth_token (parser, 3);
868 if (token3->type != tok1_kind)
869 return false;
870 c_token *token4 = c_parser_peek_nth_token (parser, 4);
871 if (token4->type != conflict_marker_get_final_tok_kind (tok1_kind))
872 return false;
873
874 /* It must be at the start of the line. */
875 location_t start_loc = c_parser_peek_token (parser)->location;
876 if (LOCATION_COLUMN (start_loc) != 1)
877 return false;
878
879 /* We have a conflict marker. Construct a location of the form:
880 <<<<<<<
881 ^~~~~~~
882 with start == caret, finishing at the end of the marker. */
883 location_t finish_loc = get_finish (token4->location);
884 *out_loc = make_location (start_loc, start_loc, finish_loc);
885
886 return true;
887 }
888
889 /* Issue a diagnostic of the form
890 FILE:LINE: MESSAGE before TOKEN
891 where TOKEN is the next token in the input stream of PARSER.
892 MESSAGE (specified by the caller) is usually of the form "expected
893 OTHER-TOKEN".
894
895 Do not issue a diagnostic if still recovering from an error.
896
897 ??? This is taken from the C++ parser, but building up messages in
898 this way is not i18n-friendly and some other approach should be
899 used. */
900
901 static void
902 c_parser_error (c_parser *parser, const char *gmsgid)
903 {
904 c_token *token = c_parser_peek_token (parser);
905 if (parser->error)
906 return;
907 parser->error = true;
908 if (!gmsgid)
909 return;
910
911 /* If this is actually a conflict marker, report it as such. */
912 if (token->type == CPP_LSHIFT
913 || token->type == CPP_RSHIFT
914 || token->type == CPP_EQ_EQ)
915 {
916 location_t loc;
917 if (c_parser_peek_conflict_marker (parser, token->type, &loc))
918 {
919 error_at (loc, "version control conflict marker in file");
920 return;
921 }
922 }
923
924 /* This diagnostic makes more sense if it is tagged to the line of
925 the token we just peeked at. */
926 c_parser_set_source_position_from_token (token);
927 c_parse_error (gmsgid,
928 /* Because c_parse_error does not understand
929 CPP_KEYWORD, keywords are treated like
930 identifiers. */
931 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
932 /* ??? The C parser does not save the cpp flags of a
933 token, we need to pass 0 here and we will not get
934 the source spelling of some tokens but rather the
935 canonical spelling. */
936 token->value, /*flags=*/0);
937 }
938
939 /* If the next token is of the indicated TYPE, consume it. Otherwise,
940 issue the error MSGID. If MSGID is NULL then a message has already
941 been produced and no message will be produced this time. Returns
942 true if found, false otherwise. */
943
944 static bool
945 c_parser_require (c_parser *parser,
946 enum cpp_ttype type,
947 const char *msgid)
948 {
949 if (c_parser_next_token_is (parser, type))
950 {
951 c_parser_consume_token (parser);
952 return true;
953 }
954 else
955 {
956 c_parser_error (parser, msgid);
957 return false;
958 }
959 }
960
961 /* If the next token is the indicated keyword, consume it. Otherwise,
962 issue the error MSGID. Returns true if found, false otherwise. */
963
964 static bool
965 c_parser_require_keyword (c_parser *parser,
966 enum rid keyword,
967 const char *msgid)
968 {
969 if (c_parser_next_token_is_keyword (parser, keyword))
970 {
971 c_parser_consume_token (parser);
972 return true;
973 }
974 else
975 {
976 c_parser_error (parser, msgid);
977 return false;
978 }
979 }
980
981 /* Like c_parser_require, except that tokens will be skipped until the
982 desired token is found. An error message is still produced if the
983 next token is not as expected. If MSGID is NULL then a message has
984 already been produced and no message will be produced this
985 time. */
986
987 static void
988 c_parser_skip_until_found (c_parser *parser,
989 enum cpp_ttype type,
990 const char *msgid)
991 {
992 unsigned nesting_depth = 0;
993
994 if (c_parser_require (parser, type, msgid))
995 return;
996
997 /* Skip tokens until the desired token is found. */
998 while (true)
999 {
1000 /* Peek at the next token. */
1001 c_token *token = c_parser_peek_token (parser);
1002 /* If we've reached the token we want, consume it and stop. */
1003 if (token->type == type && !nesting_depth)
1004 {
1005 c_parser_consume_token (parser);
1006 break;
1007 }
1008
1009 /* If we've run out of tokens, stop. */
1010 if (token->type == CPP_EOF)
1011 return;
1012 if (token->type == CPP_PRAGMA_EOL && parser->in_pragma)
1013 return;
1014 if (token->type == CPP_OPEN_BRACE
1015 || token->type == CPP_OPEN_PAREN
1016 || token->type == CPP_OPEN_SQUARE)
1017 ++nesting_depth;
1018 else if (token->type == CPP_CLOSE_BRACE
1019 || token->type == CPP_CLOSE_PAREN
1020 || token->type == CPP_CLOSE_SQUARE)
1021 {
1022 if (nesting_depth-- == 0)
1023 break;
1024 }
1025 /* Consume this token. */
1026 c_parser_consume_token (parser);
1027 }
1028 parser->error = false;
1029 }
1030
1031 /* Skip tokens until the end of a parameter is found, but do not
1032 consume the comma, semicolon or closing delimiter. */
1033
1034 static void
1035 c_parser_skip_to_end_of_parameter (c_parser *parser)
1036 {
1037 unsigned nesting_depth = 0;
1038
1039 while (true)
1040 {
1041 c_token *token = c_parser_peek_token (parser);
1042 if ((token->type == CPP_COMMA || token->type == CPP_SEMICOLON)
1043 && !nesting_depth)
1044 break;
1045 /* If we've run out of tokens, stop. */
1046 if (token->type == CPP_EOF)
1047 return;
1048 if (token->type == CPP_PRAGMA_EOL && parser->in_pragma)
1049 return;
1050 if (token->type == CPP_OPEN_BRACE
1051 || token->type == CPP_OPEN_PAREN
1052 || token->type == CPP_OPEN_SQUARE)
1053 ++nesting_depth;
1054 else if (token->type == CPP_CLOSE_BRACE
1055 || token->type == CPP_CLOSE_PAREN
1056 || token->type == CPP_CLOSE_SQUARE)
1057 {
1058 if (nesting_depth-- == 0)
1059 break;
1060 }
1061 /* Consume this token. */
1062 c_parser_consume_token (parser);
1063 }
1064 parser->error = false;
1065 }
1066
1067 /* Expect to be at the end of the pragma directive and consume an
1068 end of line marker. */
1069
1070 static void
1071 c_parser_skip_to_pragma_eol (c_parser *parser, bool error_if_not_eol = true)
1072 {
1073 gcc_assert (parser->in_pragma);
1074 parser->in_pragma = false;
1075
1076 if (error_if_not_eol && c_parser_peek_token (parser)->type != CPP_PRAGMA_EOL)
1077 c_parser_error (parser, "expected end of line");
1078
1079 cpp_ttype token_type;
1080 do
1081 {
1082 c_token *token = c_parser_peek_token (parser);
1083 token_type = token->type;
1084 if (token_type == CPP_EOF)
1085 break;
1086 c_parser_consume_token (parser);
1087 }
1088 while (token_type != CPP_PRAGMA_EOL);
1089
1090 parser->error = false;
1091 }
1092
1093 /* Skip tokens until we have consumed an entire block, or until we
1094 have consumed a non-nested ';'. */
1095
1096 static void
1097 c_parser_skip_to_end_of_block_or_statement (c_parser *parser)
1098 {
1099 unsigned nesting_depth = 0;
1100 bool save_error = parser->error;
1101
1102 while (true)
1103 {
1104 c_token *token;
1105
1106 /* Peek at the next token. */
1107 token = c_parser_peek_token (parser);
1108
1109 switch (token->type)
1110 {
1111 case CPP_EOF:
1112 return;
1113
1114 case CPP_PRAGMA_EOL:
1115 if (parser->in_pragma)
1116 return;
1117 break;
1118
1119 case CPP_SEMICOLON:
1120 /* If the next token is a ';', we have reached the
1121 end of the statement. */
1122 if (!nesting_depth)
1123 {
1124 /* Consume the ';'. */
1125 c_parser_consume_token (parser);
1126 goto finished;
1127 }
1128 break;
1129
1130 case CPP_CLOSE_BRACE:
1131 /* If the next token is a non-nested '}', then we have
1132 reached the end of the current block. */
1133 if (nesting_depth == 0 || --nesting_depth == 0)
1134 {
1135 c_parser_consume_token (parser);
1136 goto finished;
1137 }
1138 break;
1139
1140 case CPP_OPEN_BRACE:
1141 /* If it the next token is a '{', then we are entering a new
1142 block. Consume the entire block. */
1143 ++nesting_depth;
1144 break;
1145
1146 case CPP_PRAGMA:
1147 /* If we see a pragma, consume the whole thing at once. We
1148 have some safeguards against consuming pragmas willy-nilly.
1149 Normally, we'd expect to be here with parser->error set,
1150 which disables these safeguards. But it's possible to get
1151 here for secondary error recovery, after parser->error has
1152 been cleared. */
1153 c_parser_consume_pragma (parser);
1154 c_parser_skip_to_pragma_eol (parser);
1155 parser->error = save_error;
1156 continue;
1157
1158 default:
1159 break;
1160 }
1161
1162 c_parser_consume_token (parser);
1163 }
1164
1165 finished:
1166 parser->error = false;
1167 }
1168
1169 /* CPP's options (initialized by c-opts.c). */
1170 extern cpp_options *cpp_opts;
1171
1172 /* Save the warning flags which are controlled by __extension__. */
1173
1174 static inline int
1175 disable_extension_diagnostics (void)
1176 {
1177 int ret = (pedantic
1178 | (warn_pointer_arith << 1)
1179 | (warn_traditional << 2)
1180 | (flag_iso << 3)
1181 | (warn_long_long << 4)
1182 | (warn_cxx_compat << 5)
1183 | (warn_overlength_strings << 6)
1184 /* warn_c90_c99_compat has three states: -1/0/1, so we must
1185 play tricks to properly restore it. */
1186 | ((warn_c90_c99_compat == 1) << 7)
1187 | ((warn_c90_c99_compat == -1) << 8)
1188 /* Similarly for warn_c99_c11_compat. */
1189 | ((warn_c99_c11_compat == 1) << 9)
1190 | ((warn_c99_c11_compat == -1) << 10)
1191 );
1192 cpp_opts->cpp_pedantic = pedantic = 0;
1193 warn_pointer_arith = 0;
1194 cpp_opts->cpp_warn_traditional = warn_traditional = 0;
1195 flag_iso = 0;
1196 cpp_opts->cpp_warn_long_long = warn_long_long = 0;
1197 warn_cxx_compat = 0;
1198 warn_overlength_strings = 0;
1199 warn_c90_c99_compat = 0;
1200 warn_c99_c11_compat = 0;
1201 return ret;
1202 }
1203
1204 /* Restore the warning flags which are controlled by __extension__.
1205 FLAGS is the return value from disable_extension_diagnostics. */
1206
1207 static inline void
1208 restore_extension_diagnostics (int flags)
1209 {
1210 cpp_opts->cpp_pedantic = pedantic = flags & 1;
1211 warn_pointer_arith = (flags >> 1) & 1;
1212 cpp_opts->cpp_warn_traditional = warn_traditional = (flags >> 2) & 1;
1213 flag_iso = (flags >> 3) & 1;
1214 cpp_opts->cpp_warn_long_long = warn_long_long = (flags >> 4) & 1;
1215 warn_cxx_compat = (flags >> 5) & 1;
1216 warn_overlength_strings = (flags >> 6) & 1;
1217 /* See above for why is this needed. */
1218 warn_c90_c99_compat = (flags >> 7) & 1 ? 1 : ((flags >> 8) & 1 ? -1 : 0);
1219 warn_c99_c11_compat = (flags >> 9) & 1 ? 1 : ((flags >> 10) & 1 ? -1 : 0);
1220 }
1221
1222 /* Possibly kinds of declarator to parse. */
1223 enum c_dtr_syn {
1224 /* A normal declarator with an identifier. */
1225 C_DTR_NORMAL,
1226 /* An abstract declarator (maybe empty). */
1227 C_DTR_ABSTRACT,
1228 /* A parameter declarator: may be either, but after a type name does
1229 not redeclare a typedef name as an identifier if it can
1230 alternatively be interpreted as a typedef name; see DR#009,
1231 applied in C90 TC1, omitted from C99 and reapplied in C99 TC2
1232 following DR#249. For example, given a typedef T, "int T" and
1233 "int *T" are valid parameter declarations redeclaring T, while
1234 "int (T)" and "int * (T)" and "int (T[])" and "int (T (int))" are
1235 abstract declarators rather than involving redundant parentheses;
1236 the same applies with attributes inside the parentheses before
1237 "T". */
1238 C_DTR_PARM
1239 };
1240
1241 /* The binary operation precedence levels, where 0 is a dummy lowest level
1242 used for the bottom of the stack. */
1243 enum c_parser_prec {
1244 PREC_NONE,
1245 PREC_LOGOR,
1246 PREC_LOGAND,
1247 PREC_BITOR,
1248 PREC_BITXOR,
1249 PREC_BITAND,
1250 PREC_EQ,
1251 PREC_REL,
1252 PREC_SHIFT,
1253 PREC_ADD,
1254 PREC_MULT,
1255 NUM_PRECS
1256 };
1257
1258 static void c_parser_external_declaration (c_parser *);
1259 static void c_parser_asm_definition (c_parser *);
1260 static void c_parser_declaration_or_fndef (c_parser *, bool, bool, bool,
1261 bool, bool, tree *, vec<c_token>,
1262 tree = NULL_TREE);
1263 static void c_parser_static_assert_declaration_no_semi (c_parser *);
1264 static void c_parser_static_assert_declaration (c_parser *);
1265 static void c_parser_declspecs (c_parser *, struct c_declspecs *, bool, bool,
1266 bool, bool, bool, enum c_lookahead_kind);
1267 static struct c_typespec c_parser_enum_specifier (c_parser *);
1268 static struct c_typespec c_parser_struct_or_union_specifier (c_parser *);
1269 static tree c_parser_struct_declaration (c_parser *);
1270 static struct c_typespec c_parser_typeof_specifier (c_parser *);
1271 static tree c_parser_alignas_specifier (c_parser *);
1272 static struct c_declarator *c_parser_declarator (c_parser *, bool, c_dtr_syn,
1273 bool *);
1274 static struct c_declarator *c_parser_direct_declarator (c_parser *, bool,
1275 c_dtr_syn, bool *);
1276 static struct c_declarator *c_parser_direct_declarator_inner (c_parser *,
1277 bool,
1278 struct c_declarator *);
1279 static struct c_arg_info *c_parser_parms_declarator (c_parser *, bool, tree);
1280 static struct c_arg_info *c_parser_parms_list_declarator (c_parser *, tree,
1281 tree);
1282 static struct c_parm *c_parser_parameter_declaration (c_parser *, tree);
1283 static tree c_parser_simple_asm_expr (c_parser *);
1284 static tree c_parser_attributes (c_parser *);
1285 static struct c_type_name *c_parser_type_name (c_parser *);
1286 static struct c_expr c_parser_initializer (c_parser *);
1287 static struct c_expr c_parser_braced_init (c_parser *, tree, bool);
1288 static void c_parser_initelt (c_parser *, struct obstack *);
1289 static void c_parser_initval (c_parser *, struct c_expr *,
1290 struct obstack *);
1291 static tree c_parser_compound_statement (c_parser *);
1292 static void c_parser_compound_statement_nostart (c_parser *);
1293 static void c_parser_label (c_parser *);
1294 static void c_parser_statement (c_parser *);
1295 static void c_parser_statement_after_labels (c_parser *, vec<tree> * = NULL);
1296 static void c_parser_if_statement (c_parser *, vec<tree> *);
1297 static void c_parser_switch_statement (c_parser *);
1298 static void c_parser_while_statement (c_parser *, bool);
1299 static void c_parser_do_statement (c_parser *, bool);
1300 static void c_parser_for_statement (c_parser *, bool);
1301 static tree c_parser_asm_statement (c_parser *);
1302 static tree c_parser_asm_operands (c_parser *);
1303 static tree c_parser_asm_goto_operands (c_parser *);
1304 static tree c_parser_asm_clobbers (c_parser *);
1305 static struct c_expr c_parser_expr_no_commas (c_parser *, struct c_expr *,
1306 tree = NULL_TREE);
1307 static struct c_expr c_parser_conditional_expression (c_parser *,
1308 struct c_expr *, tree);
1309 static struct c_expr c_parser_binary_expression (c_parser *, struct c_expr *,
1310 tree);
1311 static struct c_expr c_parser_cast_expression (c_parser *, struct c_expr *);
1312 static struct c_expr c_parser_unary_expression (c_parser *);
1313 static struct c_expr c_parser_sizeof_expression (c_parser *);
1314 static struct c_expr c_parser_alignof_expression (c_parser *);
1315 static struct c_expr c_parser_postfix_expression (c_parser *);
1316 static struct c_expr c_parser_postfix_expression_after_paren_type (c_parser *,
1317 struct c_type_name *,
1318 location_t);
1319 static struct c_expr c_parser_postfix_expression_after_primary (c_parser *,
1320 location_t loc,
1321 struct c_expr);
1322 static tree c_parser_transaction (c_parser *, enum rid);
1323 static struct c_expr c_parser_transaction_expression (c_parser *, enum rid);
1324 static tree c_parser_transaction_cancel (c_parser *);
1325 static struct c_expr c_parser_expression (c_parser *);
1326 static struct c_expr c_parser_expression_conv (c_parser *);
1327 static vec<tree, va_gc> *c_parser_expr_list (c_parser *, bool, bool,
1328 vec<tree, va_gc> **, location_t *,
1329 tree *, vec<location_t> *,
1330 unsigned int * = NULL);
1331 static void c_parser_oacc_declare (c_parser *);
1332 static void c_parser_oacc_enter_exit_data (c_parser *, bool);
1333 static void c_parser_oacc_update (c_parser *);
1334 static void c_parser_omp_construct (c_parser *);
1335 static void c_parser_omp_threadprivate (c_parser *);
1336 static void c_parser_omp_barrier (c_parser *);
1337 static void c_parser_omp_flush (c_parser *);
1338 static tree c_parser_omp_for_loop (location_t, c_parser *, enum tree_code,
1339 tree, tree *);
1340 static void c_parser_omp_taskwait (c_parser *);
1341 static void c_parser_omp_taskyield (c_parser *);
1342 static void c_parser_omp_cancel (c_parser *);
1343 static void c_parser_omp_cancellation_point (c_parser *);
1344
1345 enum pragma_context { pragma_external, pragma_struct, pragma_param,
1346 pragma_stmt, pragma_compound };
1347 static bool c_parser_pragma (c_parser *, enum pragma_context);
1348 static bool c_parser_omp_target (c_parser *, enum pragma_context);
1349 static void c_parser_omp_end_declare_target (c_parser *);
1350 static void c_parser_omp_declare (c_parser *, enum pragma_context);
1351 static bool c_parser_omp_ordered (c_parser *, enum pragma_context);
1352 static void c_parser_oacc_routine (c_parser *parser, enum pragma_context);
1353
1354 /* These Objective-C parser functions are only ever called when
1355 compiling Objective-C. */
1356 static void c_parser_objc_class_definition (c_parser *, tree);
1357 static void c_parser_objc_class_instance_variables (c_parser *);
1358 static void c_parser_objc_class_declaration (c_parser *);
1359 static void c_parser_objc_alias_declaration (c_parser *);
1360 static void c_parser_objc_protocol_definition (c_parser *, tree);
1361 static bool c_parser_objc_method_type (c_parser *);
1362 static void c_parser_objc_method_definition (c_parser *);
1363 static void c_parser_objc_methodprotolist (c_parser *);
1364 static void c_parser_objc_methodproto (c_parser *);
1365 static tree c_parser_objc_method_decl (c_parser *, bool, tree *, tree *);
1366 static tree c_parser_objc_type_name (c_parser *);
1367 static tree c_parser_objc_protocol_refs (c_parser *);
1368 static void c_parser_objc_try_catch_finally_statement (c_parser *);
1369 static void c_parser_objc_synchronized_statement (c_parser *);
1370 static tree c_parser_objc_selector (c_parser *);
1371 static tree c_parser_objc_selector_arg (c_parser *);
1372 static tree c_parser_objc_receiver (c_parser *);
1373 static tree c_parser_objc_message_args (c_parser *);
1374 static tree c_parser_objc_keywordexpr (c_parser *);
1375 static void c_parser_objc_at_property_declaration (c_parser *);
1376 static void c_parser_objc_at_synthesize_declaration (c_parser *);
1377 static void c_parser_objc_at_dynamic_declaration (c_parser *);
1378 static bool c_parser_objc_diagnose_bad_element_prefix
1379 (c_parser *, struct c_declspecs *);
1380
1381 /* Cilk Plus supporting routines. */
1382 static void c_parser_cilk_simd (c_parser *);
1383 static void c_parser_cilk_for (c_parser *, tree);
1384 static bool c_parser_cilk_verify_simd (c_parser *, enum pragma_context);
1385 static tree c_parser_array_notation (location_t, c_parser *, tree, tree);
1386 static tree c_parser_cilk_clause_vectorlength (c_parser *, tree, bool);
1387 static void c_parser_cilk_grainsize (c_parser *);
1388
1389 /* Parse a translation unit (C90 6.7, C99 6.9).
1390
1391 translation-unit:
1392 external-declarations
1393
1394 external-declarations:
1395 external-declaration
1396 external-declarations external-declaration
1397
1398 GNU extensions:
1399
1400 translation-unit:
1401 empty
1402 */
1403
1404 static void
1405 c_parser_translation_unit (c_parser *parser)
1406 {
1407 if (c_parser_next_token_is (parser, CPP_EOF))
1408 {
1409 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
1410 "ISO C forbids an empty translation unit");
1411 }
1412 else
1413 {
1414 void *obstack_position = obstack_alloc (&parser_obstack, 0);
1415 mark_valid_location_for_stdc_pragma (false);
1416 do
1417 {
1418 ggc_collect ();
1419 c_parser_external_declaration (parser);
1420 obstack_free (&parser_obstack, obstack_position);
1421 }
1422 while (c_parser_next_token_is_not (parser, CPP_EOF));
1423 }
1424 }
1425
1426 /* Parse an external declaration (C90 6.7, C99 6.9).
1427
1428 external-declaration:
1429 function-definition
1430 declaration
1431
1432 GNU extensions:
1433
1434 external-declaration:
1435 asm-definition
1436 ;
1437 __extension__ external-declaration
1438
1439 Objective-C:
1440
1441 external-declaration:
1442 objc-class-definition
1443 objc-class-declaration
1444 objc-alias-declaration
1445 objc-protocol-definition
1446 objc-method-definition
1447 @end
1448 */
1449
1450 static void
1451 c_parser_external_declaration (c_parser *parser)
1452 {
1453 int ext;
1454 switch (c_parser_peek_token (parser)->type)
1455 {
1456 case CPP_KEYWORD:
1457 switch (c_parser_peek_token (parser)->keyword)
1458 {
1459 case RID_EXTENSION:
1460 ext = disable_extension_diagnostics ();
1461 c_parser_consume_token (parser);
1462 c_parser_external_declaration (parser);
1463 restore_extension_diagnostics (ext);
1464 break;
1465 case RID_ASM:
1466 c_parser_asm_definition (parser);
1467 break;
1468 case RID_AT_INTERFACE:
1469 case RID_AT_IMPLEMENTATION:
1470 gcc_assert (c_dialect_objc ());
1471 c_parser_objc_class_definition (parser, NULL_TREE);
1472 break;
1473 case RID_AT_CLASS:
1474 gcc_assert (c_dialect_objc ());
1475 c_parser_objc_class_declaration (parser);
1476 break;
1477 case RID_AT_ALIAS:
1478 gcc_assert (c_dialect_objc ());
1479 c_parser_objc_alias_declaration (parser);
1480 break;
1481 case RID_AT_PROTOCOL:
1482 gcc_assert (c_dialect_objc ());
1483 c_parser_objc_protocol_definition (parser, NULL_TREE);
1484 break;
1485 case RID_AT_PROPERTY:
1486 gcc_assert (c_dialect_objc ());
1487 c_parser_objc_at_property_declaration (parser);
1488 break;
1489 case RID_AT_SYNTHESIZE:
1490 gcc_assert (c_dialect_objc ());
1491 c_parser_objc_at_synthesize_declaration (parser);
1492 break;
1493 case RID_AT_DYNAMIC:
1494 gcc_assert (c_dialect_objc ());
1495 c_parser_objc_at_dynamic_declaration (parser);
1496 break;
1497 case RID_AT_END:
1498 gcc_assert (c_dialect_objc ());
1499 c_parser_consume_token (parser);
1500 objc_finish_implementation ();
1501 break;
1502 default:
1503 goto decl_or_fndef;
1504 }
1505 break;
1506 case CPP_SEMICOLON:
1507 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
1508 "ISO C does not allow extra %<;%> outside of a function");
1509 c_parser_consume_token (parser);
1510 break;
1511 case CPP_PRAGMA:
1512 mark_valid_location_for_stdc_pragma (true);
1513 c_parser_pragma (parser, pragma_external);
1514 mark_valid_location_for_stdc_pragma (false);
1515 break;
1516 case CPP_PLUS:
1517 case CPP_MINUS:
1518 if (c_dialect_objc ())
1519 {
1520 c_parser_objc_method_definition (parser);
1521 break;
1522 }
1523 /* Else fall through, and yield a syntax error trying to parse
1524 as a declaration or function definition. */
1525 default:
1526 decl_or_fndef:
1527 /* A declaration or a function definition (or, in Objective-C,
1528 an @interface or @protocol with prefix attributes). We can
1529 only tell which after parsing the declaration specifiers, if
1530 any, and the first declarator. */
1531 c_parser_declaration_or_fndef (parser, true, true, true, false, true,
1532 NULL, vNULL);
1533 break;
1534 }
1535 }
1536
1537 static void c_finish_omp_declare_simd (c_parser *, tree, tree, vec<c_token>);
1538 static void c_finish_oacc_routine (c_parser *, tree, tree, bool, bool, bool);
1539
1540 /* Parse a declaration or function definition (C90 6.5, 6.7.1, C99
1541 6.7, 6.9.1). If FNDEF_OK is true, a function definition is
1542 accepted; otherwise (old-style parameter declarations) only other
1543 declarations are accepted. If STATIC_ASSERT_OK is true, a static
1544 assertion is accepted; otherwise (old-style parameter declarations)
1545 it is not. If NESTED is true, we are inside a function or parsing
1546 old-style parameter declarations; any functions encountered are
1547 nested functions and declaration specifiers are required; otherwise
1548 we are at top level and functions are normal functions and
1549 declaration specifiers may be optional. If EMPTY_OK is true, empty
1550 declarations are OK (subject to all other constraints); otherwise
1551 (old-style parameter declarations) they are diagnosed. If
1552 START_ATTR_OK is true, the declaration specifiers may start with
1553 attributes; otherwise they may not.
1554 OBJC_FOREACH_OBJECT_DECLARATION can be used to get back the parsed
1555 declaration when parsing an Objective-C foreach statement.
1556
1557 declaration:
1558 declaration-specifiers init-declarator-list[opt] ;
1559 static_assert-declaration
1560
1561 function-definition:
1562 declaration-specifiers[opt] declarator declaration-list[opt]
1563 compound-statement
1564
1565 declaration-list:
1566 declaration
1567 declaration-list declaration
1568
1569 init-declarator-list:
1570 init-declarator
1571 init-declarator-list , init-declarator
1572
1573 init-declarator:
1574 declarator simple-asm-expr[opt] attributes[opt]
1575 declarator simple-asm-expr[opt] attributes[opt] = initializer
1576
1577 GNU extensions:
1578
1579 nested-function-definition:
1580 declaration-specifiers declarator declaration-list[opt]
1581 compound-statement
1582
1583 Objective-C:
1584 attributes objc-class-definition
1585 attributes objc-category-definition
1586 attributes objc-protocol-definition
1587
1588 The simple-asm-expr and attributes are GNU extensions.
1589
1590 This function does not handle __extension__; that is handled in its
1591 callers. ??? Following the old parser, __extension__ may start
1592 external declarations, declarations in functions and declarations
1593 at the start of "for" loops, but not old-style parameter
1594 declarations.
1595
1596 C99 requires declaration specifiers in a function definition; the
1597 absence is diagnosed through the diagnosis of implicit int. In GNU
1598 C we also allow but diagnose declarations without declaration
1599 specifiers, but only at top level (elsewhere they conflict with
1600 other syntax).
1601
1602 In Objective-C, declarations of the looping variable in a foreach
1603 statement are exceptionally terminated by 'in' (for example, 'for
1604 (NSObject *object in array) { ... }').
1605
1606 OpenMP:
1607
1608 declaration:
1609 threadprivate-directive */
1610
1611 static void
1612 c_parser_declaration_or_fndef (c_parser *parser, bool fndef_ok,
1613 bool static_assert_ok, bool empty_ok,
1614 bool nested, bool start_attr_ok,
1615 tree *objc_foreach_object_declaration,
1616 vec<c_token> omp_declare_simd_clauses,
1617 tree oacc_routine_clauses)
1618 {
1619 struct c_declspecs *specs;
1620 tree prefix_attrs;
1621 tree all_prefix_attrs;
1622 bool diagnosed_no_specs = false;
1623 location_t here = c_parser_peek_token (parser)->location;
1624
1625 if (static_assert_ok
1626 && c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT))
1627 {
1628 c_parser_static_assert_declaration (parser);
1629 return;
1630 }
1631 specs = build_null_declspecs ();
1632
1633 /* Try to detect an unknown type name when we have "A B" or "A *B". */
1634 if (c_parser_peek_token (parser)->type == CPP_NAME
1635 && c_parser_peek_token (parser)->id_kind == C_ID_ID
1636 && (c_parser_peek_2nd_token (parser)->type == CPP_NAME
1637 || c_parser_peek_2nd_token (parser)->type == CPP_MULT)
1638 && (!nested || !lookup_name (c_parser_peek_token (parser)->value)))
1639 {
1640 tree name = c_parser_peek_token (parser)->value;
1641 error_at (here, "unknown type name %qE", name);
1642 /* Give a hint to the user. This is not C++ with its implicit
1643 typedef. */
1644 if (tag_exists_p (RECORD_TYPE, name))
1645 inform (here, "use %<struct%> keyword to refer to the type");
1646 else if (tag_exists_p (UNION_TYPE, name))
1647 inform (here, "use %<union%> keyword to refer to the type");
1648 else if (tag_exists_p (ENUMERAL_TYPE, name))
1649 inform (here, "use %<enum%> keyword to refer to the type");
1650
1651 /* Parse declspecs normally to get a correct pointer type, but avoid
1652 a further "fails to be a type name" error. Refuse nested functions
1653 since it is not how the user likely wants us to recover. */
1654 c_parser_peek_token (parser)->type = CPP_KEYWORD;
1655 c_parser_peek_token (parser)->keyword = RID_VOID;
1656 c_parser_peek_token (parser)->value = error_mark_node;
1657 fndef_ok = !nested;
1658 }
1659
1660 c_parser_declspecs (parser, specs, true, true, start_attr_ok,
1661 true, true, cla_nonabstract_decl);
1662 if (parser->error)
1663 {
1664 c_parser_skip_to_end_of_block_or_statement (parser);
1665 return;
1666 }
1667 if (nested && !specs->declspecs_seen_p)
1668 {
1669 c_parser_error (parser, "expected declaration specifiers");
1670 c_parser_skip_to_end_of_block_or_statement (parser);
1671 return;
1672 }
1673 finish_declspecs (specs);
1674 bool auto_type_p = specs->typespec_word == cts_auto_type;
1675 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1676 {
1677 if (auto_type_p)
1678 error_at (here, "%<__auto_type%> in empty declaration");
1679 else if (empty_ok)
1680 shadow_tag (specs);
1681 else
1682 {
1683 shadow_tag_warned (specs, 1);
1684 pedwarn (here, 0, "empty declaration");
1685 }
1686 c_parser_consume_token (parser);
1687 if (oacc_routine_clauses)
1688 c_finish_oacc_routine (parser, NULL_TREE,
1689 oacc_routine_clauses, false, true, false);
1690 return;
1691 }
1692
1693 /* Provide better error recovery. Note that a type name here is usually
1694 better diagnosed as a redeclaration. */
1695 if (empty_ok
1696 && specs->typespec_kind == ctsk_tagdef
1697 && c_parser_next_token_starts_declspecs (parser)
1698 && !c_parser_next_token_is (parser, CPP_NAME))
1699 {
1700 c_parser_error (parser, "expected %<;%>, identifier or %<(%>");
1701 parser->error = false;
1702 shadow_tag_warned (specs, 1);
1703 return;
1704 }
1705 else if (c_dialect_objc () && !auto_type_p)
1706 {
1707 /* Prefix attributes are an error on method decls. */
1708 switch (c_parser_peek_token (parser)->type)
1709 {
1710 case CPP_PLUS:
1711 case CPP_MINUS:
1712 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1713 return;
1714 if (specs->attrs)
1715 {
1716 warning_at (c_parser_peek_token (parser)->location,
1717 OPT_Wattributes,
1718 "prefix attributes are ignored for methods");
1719 specs->attrs = NULL_TREE;
1720 }
1721 if (fndef_ok)
1722 c_parser_objc_method_definition (parser);
1723 else
1724 c_parser_objc_methodproto (parser);
1725 return;
1726 break;
1727 default:
1728 break;
1729 }
1730 /* This is where we parse 'attributes @interface ...',
1731 'attributes @implementation ...', 'attributes @protocol ...'
1732 (where attributes could be, for example, __attribute__
1733 ((deprecated)).
1734 */
1735 switch (c_parser_peek_token (parser)->keyword)
1736 {
1737 case RID_AT_INTERFACE:
1738 {
1739 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1740 return;
1741 c_parser_objc_class_definition (parser, specs->attrs);
1742 return;
1743 }
1744 break;
1745 case RID_AT_IMPLEMENTATION:
1746 {
1747 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1748 return;
1749 if (specs->attrs)
1750 {
1751 warning_at (c_parser_peek_token (parser)->location,
1752 OPT_Wattributes,
1753 "prefix attributes are ignored for implementations");
1754 specs->attrs = NULL_TREE;
1755 }
1756 c_parser_objc_class_definition (parser, NULL_TREE);
1757 return;
1758 }
1759 break;
1760 case RID_AT_PROTOCOL:
1761 {
1762 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1763 return;
1764 c_parser_objc_protocol_definition (parser, specs->attrs);
1765 return;
1766 }
1767 break;
1768 case RID_AT_ALIAS:
1769 case RID_AT_CLASS:
1770 case RID_AT_END:
1771 case RID_AT_PROPERTY:
1772 if (specs->attrs)
1773 {
1774 c_parser_error (parser, "unexpected attribute");
1775 specs->attrs = NULL;
1776 }
1777 break;
1778 default:
1779 break;
1780 }
1781 }
1782
1783 pending_xref_error ();
1784 prefix_attrs = specs->attrs;
1785 all_prefix_attrs = prefix_attrs;
1786 specs->attrs = NULL_TREE;
1787 for (bool first = true;; first = false)
1788 {
1789 struct c_declarator *declarator;
1790 bool dummy = false;
1791 timevar_id_t tv;
1792 tree fnbody;
1793 /* Declaring either one or more declarators (in which case we
1794 should diagnose if there were no declaration specifiers) or a
1795 function definition (in which case the diagnostic for
1796 implicit int suffices). */
1797 declarator = c_parser_declarator (parser,
1798 specs->typespec_kind != ctsk_none,
1799 C_DTR_NORMAL, &dummy);
1800 if (declarator == NULL)
1801 {
1802 if (omp_declare_simd_clauses.exists ()
1803 || !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
1804 c_finish_omp_declare_simd (parser, NULL_TREE, NULL_TREE,
1805 omp_declare_simd_clauses);
1806 if (oacc_routine_clauses)
1807 c_finish_oacc_routine (parser, NULL_TREE,
1808 oacc_routine_clauses,
1809 false, first, false);
1810 c_parser_skip_to_end_of_block_or_statement (parser);
1811 return;
1812 }
1813 if (auto_type_p && declarator->kind != cdk_id)
1814 {
1815 error_at (here,
1816 "%<__auto_type%> requires a plain identifier"
1817 " as declarator");
1818 c_parser_skip_to_end_of_block_or_statement (parser);
1819 return;
1820 }
1821 if (c_parser_next_token_is (parser, CPP_EQ)
1822 || c_parser_next_token_is (parser, CPP_COMMA)
1823 || c_parser_next_token_is (parser, CPP_SEMICOLON)
1824 || c_parser_next_token_is_keyword (parser, RID_ASM)
1825 || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE)
1826 || c_parser_next_token_is_keyword (parser, RID_IN))
1827 {
1828 tree asm_name = NULL_TREE;
1829 tree postfix_attrs = NULL_TREE;
1830 if (!diagnosed_no_specs && !specs->declspecs_seen_p)
1831 {
1832 diagnosed_no_specs = true;
1833 pedwarn (here, 0, "data definition has no type or storage class");
1834 }
1835 /* Having seen a data definition, there cannot now be a
1836 function definition. */
1837 fndef_ok = false;
1838 if (c_parser_next_token_is_keyword (parser, RID_ASM))
1839 asm_name = c_parser_simple_asm_expr (parser);
1840 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1841 {
1842 postfix_attrs = c_parser_attributes (parser);
1843 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
1844 {
1845 /* This means there is an attribute specifier after
1846 the declarator in a function definition. Provide
1847 some more information for the user. */
1848 error_at (here, "attributes should be specified before the "
1849 "declarator in a function definition");
1850 c_parser_skip_to_end_of_block_or_statement (parser);
1851 return;
1852 }
1853 }
1854 if (c_parser_next_token_is (parser, CPP_EQ))
1855 {
1856 tree d;
1857 struct c_expr init;
1858 location_t init_loc;
1859 c_parser_consume_token (parser);
1860 if (auto_type_p)
1861 {
1862 start_init (NULL_TREE, asm_name, global_bindings_p ());
1863 init_loc = c_parser_peek_token (parser)->location;
1864 init = c_parser_expr_no_commas (parser, NULL);
1865 if (TREE_CODE (init.value) == COMPONENT_REF
1866 && DECL_C_BIT_FIELD (TREE_OPERAND (init.value, 1)))
1867 error_at (here,
1868 "%<__auto_type%> used with a bit-field"
1869 " initializer");
1870 init = convert_lvalue_to_rvalue (init_loc, init, true, true);
1871 tree init_type = TREE_TYPE (init.value);
1872 /* As with typeof, remove all qualifiers from atomic types. */
1873 if (init_type != error_mark_node && TYPE_ATOMIC (init_type))
1874 init_type
1875 = c_build_qualified_type (init_type, TYPE_UNQUALIFIED);
1876 bool vm_type = variably_modified_type_p (init_type,
1877 NULL_TREE);
1878 if (vm_type)
1879 init.value = c_save_expr (init.value);
1880 finish_init ();
1881 specs->typespec_kind = ctsk_typeof;
1882 specs->locations[cdw_typedef] = init_loc;
1883 specs->typedef_p = true;
1884 specs->type = init_type;
1885 if (vm_type)
1886 {
1887 bool maybe_const = true;
1888 tree type_expr = c_fully_fold (init.value, false,
1889 &maybe_const);
1890 specs->expr_const_operands &= maybe_const;
1891 if (specs->expr)
1892 specs->expr = build2 (COMPOUND_EXPR,
1893 TREE_TYPE (type_expr),
1894 specs->expr, type_expr);
1895 else
1896 specs->expr = type_expr;
1897 }
1898 d = start_decl (declarator, specs, true,
1899 chainon (postfix_attrs, all_prefix_attrs));
1900 if (!d)
1901 d = error_mark_node;
1902 if (omp_declare_simd_clauses.exists ()
1903 || !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
1904 c_finish_omp_declare_simd (parser, d, NULL_TREE,
1905 omp_declare_simd_clauses);
1906 }
1907 else
1908 {
1909 /* The declaration of the variable is in effect while
1910 its initializer is parsed. */
1911 d = start_decl (declarator, specs, true,
1912 chainon (postfix_attrs, all_prefix_attrs));
1913 if (!d)
1914 d = error_mark_node;
1915 if (omp_declare_simd_clauses.exists ()
1916 || !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
1917 c_finish_omp_declare_simd (parser, d, NULL_TREE,
1918 omp_declare_simd_clauses);
1919 start_init (d, asm_name, global_bindings_p ());
1920 init_loc = c_parser_peek_token (parser)->location;
1921 init = c_parser_initializer (parser);
1922 finish_init ();
1923 }
1924 if (oacc_routine_clauses)
1925 c_finish_oacc_routine (parser, d, oacc_routine_clauses,
1926 false, first, false);
1927 if (d != error_mark_node)
1928 {
1929 maybe_warn_string_init (init_loc, TREE_TYPE (d), init);
1930 finish_decl (d, init_loc, init.value,
1931 init.original_type, asm_name);
1932 }
1933 }
1934 else
1935 {
1936 if (auto_type_p)
1937 {
1938 error_at (here,
1939 "%<__auto_type%> requires an initialized "
1940 "data declaration");
1941 c_parser_skip_to_end_of_block_or_statement (parser);
1942 return;
1943 }
1944 tree d = start_decl (declarator, specs, false,
1945 chainon (postfix_attrs,
1946 all_prefix_attrs));
1947 if (omp_declare_simd_clauses.exists ()
1948 || !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
1949 {
1950 tree parms = NULL_TREE;
1951 if (d && TREE_CODE (d) == FUNCTION_DECL)
1952 {
1953 struct c_declarator *ce = declarator;
1954 while (ce != NULL)
1955 if (ce->kind == cdk_function)
1956 {
1957 parms = ce->u.arg_info->parms;
1958 break;
1959 }
1960 else
1961 ce = ce->declarator;
1962 }
1963 if (parms)
1964 temp_store_parm_decls (d, parms);
1965 c_finish_omp_declare_simd (parser, d, parms,
1966 omp_declare_simd_clauses);
1967 if (parms)
1968 temp_pop_parm_decls ();
1969 }
1970 if (oacc_routine_clauses)
1971 c_finish_oacc_routine (parser, d, oacc_routine_clauses,
1972 false, first, false);
1973 if (d)
1974 finish_decl (d, UNKNOWN_LOCATION, NULL_TREE,
1975 NULL_TREE, asm_name);
1976
1977 if (c_parser_next_token_is_keyword (parser, RID_IN))
1978 {
1979 if (d)
1980 *objc_foreach_object_declaration = d;
1981 else
1982 *objc_foreach_object_declaration = error_mark_node;
1983 }
1984 }
1985 if (c_parser_next_token_is (parser, CPP_COMMA))
1986 {
1987 if (auto_type_p)
1988 {
1989 error_at (here,
1990 "%<__auto_type%> may only be used with"
1991 " a single declarator");
1992 c_parser_skip_to_end_of_block_or_statement (parser);
1993 return;
1994 }
1995 c_parser_consume_token (parser);
1996 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1997 all_prefix_attrs = chainon (c_parser_attributes (parser),
1998 prefix_attrs);
1999 else
2000 all_prefix_attrs = prefix_attrs;
2001 continue;
2002 }
2003 else if (c_parser_next_token_is (parser, CPP_SEMICOLON))
2004 {
2005 c_parser_consume_token (parser);
2006 return;
2007 }
2008 else if (c_parser_next_token_is_keyword (parser, RID_IN))
2009 {
2010 /* This can only happen in Objective-C: we found the
2011 'in' that terminates the declaration inside an
2012 Objective-C foreach statement. Do not consume the
2013 token, so that the caller can use it to determine
2014 that this indeed is a foreach context. */
2015 return;
2016 }
2017 else
2018 {
2019 c_parser_error (parser, "expected %<,%> or %<;%>");
2020 c_parser_skip_to_end_of_block_or_statement (parser);
2021 return;
2022 }
2023 }
2024 else if (auto_type_p)
2025 {
2026 error_at (here,
2027 "%<__auto_type%> requires an initialized data declaration");
2028 c_parser_skip_to_end_of_block_or_statement (parser);
2029 return;
2030 }
2031 else if (!fndef_ok)
2032 {
2033 c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, "
2034 "%<asm%> or %<__attribute__%>");
2035 c_parser_skip_to_end_of_block_or_statement (parser);
2036 return;
2037 }
2038 /* Function definition (nested or otherwise). */
2039 if (nested)
2040 {
2041 pedwarn (here, OPT_Wpedantic, "ISO C forbids nested functions");
2042 c_push_function_context ();
2043 }
2044 if (!start_function (specs, declarator, all_prefix_attrs))
2045 {
2046 /* This can appear in many cases looking nothing like a
2047 function definition, so we don't give a more specific
2048 error suggesting there was one. */
2049 c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, %<asm%> "
2050 "or %<__attribute__%>");
2051 if (nested)
2052 c_pop_function_context ();
2053 break;
2054 }
2055
2056 if (DECL_DECLARED_INLINE_P (current_function_decl))
2057 tv = TV_PARSE_INLINE;
2058 else
2059 tv = TV_PARSE_FUNC;
2060 timevar_push (tv);
2061
2062 /* Parse old-style parameter declarations. ??? Attributes are
2063 not allowed to start declaration specifiers here because of a
2064 syntax conflict between a function declaration with attribute
2065 suffix and a function definition with an attribute prefix on
2066 first old-style parameter declaration. Following the old
2067 parser, they are not accepted on subsequent old-style
2068 parameter declarations either. However, there is no
2069 ambiguity after the first declaration, nor indeed on the
2070 first as long as we don't allow postfix attributes after a
2071 declarator with a nonempty identifier list in a definition;
2072 and postfix attributes have never been accepted here in
2073 function definitions either. */
2074 while (c_parser_next_token_is_not (parser, CPP_EOF)
2075 && c_parser_next_token_is_not (parser, CPP_OPEN_BRACE))
2076 c_parser_declaration_or_fndef (parser, false, false, false,
2077 true, false, NULL, vNULL);
2078 store_parm_decls ();
2079 if (omp_declare_simd_clauses.exists ()
2080 || !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
2081 c_finish_omp_declare_simd (parser, current_function_decl, NULL_TREE,
2082 omp_declare_simd_clauses);
2083 if (oacc_routine_clauses)
2084 c_finish_oacc_routine (parser, current_function_decl,
2085 oacc_routine_clauses, false, first, true);
2086 DECL_STRUCT_FUNCTION (current_function_decl)->function_start_locus
2087 = c_parser_peek_token (parser)->location;
2088 fnbody = c_parser_compound_statement (parser);
2089 if (flag_cilkplus && contains_array_notation_expr (fnbody))
2090 fnbody = expand_array_notation_exprs (fnbody);
2091 if (nested)
2092 {
2093 tree decl = current_function_decl;
2094 /* Mark nested functions as needing static-chain initially.
2095 lower_nested_functions will recompute it but the
2096 DECL_STATIC_CHAIN flag is also used before that happens,
2097 by initializer_constant_valid_p. See gcc.dg/nested-fn-2.c. */
2098 DECL_STATIC_CHAIN (decl) = 1;
2099 add_stmt (fnbody);
2100 finish_function ();
2101 c_pop_function_context ();
2102 add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl));
2103 }
2104 else
2105 {
2106 add_stmt (fnbody);
2107 finish_function ();
2108 }
2109
2110 timevar_pop (tv);
2111 break;
2112 }
2113 }
2114
2115 /* Parse an asm-definition (asm() outside a function body). This is a
2116 GNU extension.
2117
2118 asm-definition:
2119 simple-asm-expr ;
2120 */
2121
2122 static void
2123 c_parser_asm_definition (c_parser *parser)
2124 {
2125 tree asm_str = c_parser_simple_asm_expr (parser);
2126 if (asm_str)
2127 symtab->finalize_toplevel_asm (asm_str);
2128 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
2129 }
2130
2131 /* Parse a static assertion (C11 6.7.10).
2132
2133 static_assert-declaration:
2134 static_assert-declaration-no-semi ;
2135 */
2136
2137 static void
2138 c_parser_static_assert_declaration (c_parser *parser)
2139 {
2140 c_parser_static_assert_declaration_no_semi (parser);
2141 if (parser->error
2142 || !c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
2143 c_parser_skip_to_end_of_block_or_statement (parser);
2144 }
2145
2146 /* Parse a static assertion (C11 6.7.10), without the trailing
2147 semicolon.
2148
2149 static_assert-declaration-no-semi:
2150 _Static_assert ( constant-expression , string-literal )
2151 */
2152
2153 static void
2154 c_parser_static_assert_declaration_no_semi (c_parser *parser)
2155 {
2156 location_t assert_loc, value_loc;
2157 tree value;
2158 tree string;
2159
2160 gcc_assert (c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT));
2161 assert_loc = c_parser_peek_token (parser)->location;
2162 if (flag_isoc99)
2163 pedwarn_c99 (assert_loc, OPT_Wpedantic,
2164 "ISO C99 does not support %<_Static_assert%>");
2165 else
2166 pedwarn_c99 (assert_loc, OPT_Wpedantic,
2167 "ISO C90 does not support %<_Static_assert%>");
2168 c_parser_consume_token (parser);
2169 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2170 return;
2171 location_t value_tok_loc = c_parser_peek_token (parser)->location;
2172 value = c_parser_expr_no_commas (parser, NULL).value;
2173 value_loc = EXPR_LOC_OR_LOC (value, value_tok_loc);
2174 parser->lex_untranslated_string = true;
2175 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
2176 {
2177 parser->lex_untranslated_string = false;
2178 return;
2179 }
2180 switch (c_parser_peek_token (parser)->type)
2181 {
2182 case CPP_STRING:
2183 case CPP_STRING16:
2184 case CPP_STRING32:
2185 case CPP_WSTRING:
2186 case CPP_UTF8STRING:
2187 string = c_parser_peek_token (parser)->value;
2188 c_parser_consume_token (parser);
2189 parser->lex_untranslated_string = false;
2190 break;
2191 default:
2192 c_parser_error (parser, "expected string literal");
2193 parser->lex_untranslated_string = false;
2194 return;
2195 }
2196 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
2197
2198 if (!INTEGRAL_TYPE_P (TREE_TYPE (value)))
2199 {
2200 error_at (value_loc, "expression in static assertion is not an integer");
2201 return;
2202 }
2203 if (TREE_CODE (value) != INTEGER_CST)
2204 {
2205 value = c_fully_fold (value, false, NULL);
2206 /* Strip no-op conversions. */
2207 STRIP_TYPE_NOPS (value);
2208 if (TREE_CODE (value) == INTEGER_CST)
2209 pedwarn (value_loc, OPT_Wpedantic, "expression in static assertion "
2210 "is not an integer constant expression");
2211 }
2212 if (TREE_CODE (value) != INTEGER_CST)
2213 {
2214 error_at (value_loc, "expression in static assertion is not constant");
2215 return;
2216 }
2217 constant_expression_warning (value);
2218 if (integer_zerop (value))
2219 error_at (assert_loc, "static assertion failed: %E", string);
2220 }
2221
2222 /* Parse some declaration specifiers (possibly none) (C90 6.5, C99
2223 6.7), adding them to SPECS (which may already include some).
2224 Storage class specifiers are accepted iff SCSPEC_OK; type
2225 specifiers are accepted iff TYPESPEC_OK; alignment specifiers are
2226 accepted iff ALIGNSPEC_OK; attributes are accepted at the start
2227 iff START_ATTR_OK; __auto_type is accepted iff AUTO_TYPE_OK.
2228
2229 declaration-specifiers:
2230 storage-class-specifier declaration-specifiers[opt]
2231 type-specifier declaration-specifiers[opt]
2232 type-qualifier declaration-specifiers[opt]
2233 function-specifier declaration-specifiers[opt]
2234 alignment-specifier declaration-specifiers[opt]
2235
2236 Function specifiers (inline) are from C99, and are currently
2237 handled as storage class specifiers, as is __thread. Alignment
2238 specifiers are from C11.
2239
2240 C90 6.5.1, C99 6.7.1:
2241 storage-class-specifier:
2242 typedef
2243 extern
2244 static
2245 auto
2246 register
2247 _Thread_local
2248
2249 (_Thread_local is new in C11.)
2250
2251 C99 6.7.4:
2252 function-specifier:
2253 inline
2254 _Noreturn
2255
2256 (_Noreturn is new in C11.)
2257
2258 C90 6.5.2, C99 6.7.2:
2259 type-specifier:
2260 void
2261 char
2262 short
2263 int
2264 long
2265 float
2266 double
2267 signed
2268 unsigned
2269 _Bool
2270 _Complex
2271 [_Imaginary removed in C99 TC2]
2272 struct-or-union-specifier
2273 enum-specifier
2274 typedef-name
2275 atomic-type-specifier
2276
2277 (_Bool and _Complex are new in C99.)
2278 (atomic-type-specifier is new in C11.)
2279
2280 C90 6.5.3, C99 6.7.3:
2281
2282 type-qualifier:
2283 const
2284 restrict
2285 volatile
2286 address-space-qualifier
2287 _Atomic
2288
2289 (restrict is new in C99.)
2290 (_Atomic is new in C11.)
2291
2292 GNU extensions:
2293
2294 declaration-specifiers:
2295 attributes declaration-specifiers[opt]
2296
2297 type-qualifier:
2298 address-space
2299
2300 address-space:
2301 identifier recognized by the target
2302
2303 storage-class-specifier:
2304 __thread
2305
2306 type-specifier:
2307 typeof-specifier
2308 __auto_type
2309 __intN
2310 _Decimal32
2311 _Decimal64
2312 _Decimal128
2313 _Fract
2314 _Accum
2315 _Sat
2316
2317 (_Fract, _Accum, and _Sat are new from ISO/IEC DTR 18037:
2318 http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1169.pdf)
2319
2320 atomic-type-specifier
2321 _Atomic ( type-name )
2322
2323 Objective-C:
2324
2325 type-specifier:
2326 class-name objc-protocol-refs[opt]
2327 typedef-name objc-protocol-refs
2328 objc-protocol-refs
2329 */
2330
2331 static void
2332 c_parser_declspecs (c_parser *parser, struct c_declspecs *specs,
2333 bool scspec_ok, bool typespec_ok, bool start_attr_ok,
2334 bool alignspec_ok, bool auto_type_ok,
2335 enum c_lookahead_kind la)
2336 {
2337 bool attrs_ok = start_attr_ok;
2338 bool seen_type = specs->typespec_kind != ctsk_none;
2339
2340 if (!typespec_ok)
2341 gcc_assert (la == cla_prefer_id);
2342
2343 while (c_parser_next_token_is (parser, CPP_NAME)
2344 || c_parser_next_token_is (parser, CPP_KEYWORD)
2345 || (c_dialect_objc () && c_parser_next_token_is (parser, CPP_LESS)))
2346 {
2347 struct c_typespec t;
2348 tree attrs;
2349 tree align;
2350 location_t loc = c_parser_peek_token (parser)->location;
2351
2352 /* If we cannot accept a type, exit if the next token must start
2353 one. Also, if we already have seen a tagged definition,
2354 a typename would be an error anyway and likely the user
2355 has simply forgotten a semicolon, so we exit. */
2356 if ((!typespec_ok || specs->typespec_kind == ctsk_tagdef)
2357 && c_parser_next_tokens_start_typename (parser, la)
2358 && !c_parser_next_token_is_qualifier (parser))
2359 break;
2360
2361 if (c_parser_next_token_is (parser, CPP_NAME))
2362 {
2363 c_token *name_token = c_parser_peek_token (parser);
2364 tree value = name_token->value;
2365 c_id_kind kind = name_token->id_kind;
2366
2367 if (kind == C_ID_ADDRSPACE)
2368 {
2369 addr_space_t as
2370 = name_token->keyword - RID_FIRST_ADDR_SPACE;
2371 declspecs_add_addrspace (name_token->location, specs, as);
2372 c_parser_consume_token (parser);
2373 attrs_ok = true;
2374 continue;
2375 }
2376
2377 gcc_assert (!c_parser_next_token_is_qualifier (parser));
2378
2379 /* If we cannot accept a type, and the next token must start one,
2380 exit. Do the same if we already have seen a tagged definition,
2381 since it would be an error anyway and likely the user has simply
2382 forgotten a semicolon. */
2383 if (seen_type || !c_parser_next_tokens_start_typename (parser, la))
2384 break;
2385
2386 /* Now at an unknown typename (C_ID_ID), a C_ID_TYPENAME or
2387 a C_ID_CLASSNAME. */
2388 c_parser_consume_token (parser);
2389 seen_type = true;
2390 attrs_ok = true;
2391 if (kind == C_ID_ID)
2392 {
2393 error_at (loc, "unknown type name %qE", value);
2394 t.kind = ctsk_typedef;
2395 t.spec = error_mark_node;
2396 }
2397 else if (kind == C_ID_TYPENAME
2398 && (!c_dialect_objc ()
2399 || c_parser_next_token_is_not (parser, CPP_LESS)))
2400 {
2401 t.kind = ctsk_typedef;
2402 /* For a typedef name, record the meaning, not the name.
2403 In case of 'foo foo, bar;'. */
2404 t.spec = lookup_name (value);
2405 }
2406 else
2407 {
2408 tree proto = NULL_TREE;
2409 gcc_assert (c_dialect_objc ());
2410 t.kind = ctsk_objc;
2411 if (c_parser_next_token_is (parser, CPP_LESS))
2412 proto = c_parser_objc_protocol_refs (parser);
2413 t.spec = objc_get_protocol_qualified_type (value, proto);
2414 }
2415 t.expr = NULL_TREE;
2416 t.expr_const_operands = true;
2417 declspecs_add_type (name_token->location, specs, t);
2418 continue;
2419 }
2420 if (c_parser_next_token_is (parser, CPP_LESS))
2421 {
2422 /* Make "<SomeProtocol>" equivalent to "id <SomeProtocol>" -
2423 nisse@lysator.liu.se. */
2424 tree proto;
2425 gcc_assert (c_dialect_objc ());
2426 if (!typespec_ok || seen_type)
2427 break;
2428 proto = c_parser_objc_protocol_refs (parser);
2429 t.kind = ctsk_objc;
2430 t.spec = objc_get_protocol_qualified_type (NULL_TREE, proto);
2431 t.expr = NULL_TREE;
2432 t.expr_const_operands = true;
2433 declspecs_add_type (loc, specs, t);
2434 continue;
2435 }
2436 gcc_assert (c_parser_next_token_is (parser, CPP_KEYWORD));
2437 switch (c_parser_peek_token (parser)->keyword)
2438 {
2439 case RID_STATIC:
2440 case RID_EXTERN:
2441 case RID_REGISTER:
2442 case RID_TYPEDEF:
2443 case RID_INLINE:
2444 case RID_NORETURN:
2445 case RID_AUTO:
2446 case RID_THREAD:
2447 if (!scspec_ok)
2448 goto out;
2449 attrs_ok = true;
2450 /* TODO: Distinguish between function specifiers (inline, noreturn)
2451 and storage class specifiers, either here or in
2452 declspecs_add_scspec. */
2453 declspecs_add_scspec (loc, specs,
2454 c_parser_peek_token (parser)->value);
2455 c_parser_consume_token (parser);
2456 break;
2457 case RID_AUTO_TYPE:
2458 if (!auto_type_ok)
2459 goto out;
2460 /* Fall through. */
2461 case RID_UNSIGNED:
2462 case RID_LONG:
2463 case RID_SHORT:
2464 case RID_SIGNED:
2465 case RID_COMPLEX:
2466 case RID_INT:
2467 case RID_CHAR:
2468 case RID_FLOAT:
2469 case RID_DOUBLE:
2470 case RID_VOID:
2471 case RID_DFLOAT32:
2472 case RID_DFLOAT64:
2473 case RID_DFLOAT128:
2474 case RID_BOOL:
2475 case RID_FRACT:
2476 case RID_ACCUM:
2477 case RID_SAT:
2478 case RID_INT_N_0:
2479 case RID_INT_N_1:
2480 case RID_INT_N_2:
2481 case RID_INT_N_3:
2482 if (!typespec_ok)
2483 goto out;
2484 attrs_ok = true;
2485 seen_type = true;
2486 if (c_dialect_objc ())
2487 parser->objc_need_raw_identifier = true;
2488 t.kind = ctsk_resword;
2489 t.spec = c_parser_peek_token (parser)->value;
2490 t.expr = NULL_TREE;
2491 t.expr_const_operands = true;
2492 declspecs_add_type (loc, specs, t);
2493 c_parser_consume_token (parser);
2494 break;
2495 case RID_ENUM:
2496 if (!typespec_ok)
2497 goto out;
2498 attrs_ok = true;
2499 seen_type = true;
2500 t = c_parser_enum_specifier (parser);
2501 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, t.spec);
2502 declspecs_add_type (loc, specs, t);
2503 break;
2504 case RID_STRUCT:
2505 case RID_UNION:
2506 if (!typespec_ok)
2507 goto out;
2508 attrs_ok = true;
2509 seen_type = true;
2510 t = c_parser_struct_or_union_specifier (parser);
2511 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, t.spec);
2512 declspecs_add_type (loc, specs, t);
2513 break;
2514 case RID_TYPEOF:
2515 /* ??? The old parser rejected typeof after other type
2516 specifiers, but is a syntax error the best way of
2517 handling this? */
2518 if (!typespec_ok || seen_type)
2519 goto out;
2520 attrs_ok = true;
2521 seen_type = true;
2522 t = c_parser_typeof_specifier (parser);
2523 declspecs_add_type (loc, specs, t);
2524 break;
2525 case RID_ATOMIC:
2526 /* C parser handling of Objective-C constructs needs
2527 checking for correct lvalue-to-rvalue conversions, and
2528 the code in build_modify_expr handling various
2529 Objective-C cases, and that in build_unary_op handling
2530 Objective-C cases for increment / decrement, also needs
2531 updating; uses of TYPE_MAIN_VARIANT in objc_compare_types
2532 and objc_types_are_equivalent may also need updates. */
2533 if (c_dialect_objc ())
2534 sorry ("%<_Atomic%> in Objective-C");
2535 /* C parser handling of OpenMP constructs needs checking for
2536 correct lvalue-to-rvalue conversions. */
2537 if (flag_openmp)
2538 sorry ("%<_Atomic%> with OpenMP");
2539 if (flag_isoc99)
2540 pedwarn_c99 (loc, OPT_Wpedantic,
2541 "ISO C99 does not support the %<_Atomic%> qualifier");
2542 else
2543 pedwarn_c99 (loc, OPT_Wpedantic,
2544 "ISO C90 does not support the %<_Atomic%> qualifier");
2545 attrs_ok = true;
2546 tree value;
2547 value = c_parser_peek_token (parser)->value;
2548 c_parser_consume_token (parser);
2549 if (typespec_ok && c_parser_next_token_is (parser, CPP_OPEN_PAREN))
2550 {
2551 /* _Atomic ( type-name ). */
2552 seen_type = true;
2553 c_parser_consume_token (parser);
2554 struct c_type_name *type = c_parser_type_name (parser);
2555 t.kind = ctsk_typeof;
2556 t.spec = error_mark_node;
2557 t.expr = NULL_TREE;
2558 t.expr_const_operands = true;
2559 if (type != NULL)
2560 t.spec = groktypename (type, &t.expr,
2561 &t.expr_const_operands);
2562 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2563 "expected %<)%>");
2564 if (t.spec != error_mark_node)
2565 {
2566 if (TREE_CODE (t.spec) == ARRAY_TYPE)
2567 error_at (loc, "%<_Atomic%>-qualified array type");
2568 else if (TREE_CODE (t.spec) == FUNCTION_TYPE)
2569 error_at (loc, "%<_Atomic%>-qualified function type");
2570 else if (TYPE_QUALS (t.spec) != TYPE_UNQUALIFIED)
2571 error_at (loc, "%<_Atomic%> applied to a qualified type");
2572 else
2573 t.spec = c_build_qualified_type (t.spec, TYPE_QUAL_ATOMIC);
2574 }
2575 declspecs_add_type (loc, specs, t);
2576 }
2577 else
2578 declspecs_add_qual (loc, specs, value);
2579 break;
2580 case RID_CONST:
2581 case RID_VOLATILE:
2582 case RID_RESTRICT:
2583 attrs_ok = true;
2584 declspecs_add_qual (loc, specs, c_parser_peek_token (parser)->value);
2585 c_parser_consume_token (parser);
2586 break;
2587 case RID_ATTRIBUTE:
2588 if (!attrs_ok)
2589 goto out;
2590 attrs = c_parser_attributes (parser);
2591 declspecs_add_attrs (loc, specs, attrs);
2592 break;
2593 case RID_ALIGNAS:
2594 if (!alignspec_ok)
2595 goto out;
2596 align = c_parser_alignas_specifier (parser);
2597 declspecs_add_alignas (loc, specs, align);
2598 break;
2599 default:
2600 goto out;
2601 }
2602 }
2603 out: ;
2604 }
2605
2606 /* Parse an enum specifier (C90 6.5.2.2, C99 6.7.2.2).
2607
2608 enum-specifier:
2609 enum attributes[opt] identifier[opt] { enumerator-list } attributes[opt]
2610 enum attributes[opt] identifier[opt] { enumerator-list , } attributes[opt]
2611 enum attributes[opt] identifier
2612
2613 The form with trailing comma is new in C99. The forms with
2614 attributes are GNU extensions. In GNU C, we accept any expression
2615 without commas in the syntax (assignment expressions, not just
2616 conditional expressions); assignment expressions will be diagnosed
2617 as non-constant.
2618
2619 enumerator-list:
2620 enumerator
2621 enumerator-list , enumerator
2622
2623 enumerator:
2624 enumeration-constant
2625 enumeration-constant = constant-expression
2626
2627 GNU Extensions:
2628
2629 enumerator:
2630 enumeration-constant attributes[opt]
2631 enumeration-constant attributes[opt] = constant-expression
2632
2633 */
2634
2635 static struct c_typespec
2636 c_parser_enum_specifier (c_parser *parser)
2637 {
2638 struct c_typespec ret;
2639 tree attrs;
2640 tree ident = NULL_TREE;
2641 location_t enum_loc;
2642 location_t ident_loc = UNKNOWN_LOCATION; /* Quiet warning. */
2643 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ENUM));
2644 enum_loc = c_parser_peek_token (parser)->location;
2645 c_parser_consume_token (parser);
2646 attrs = c_parser_attributes (parser);
2647 enum_loc = c_parser_peek_token (parser)->location;
2648 /* Set the location in case we create a decl now. */
2649 c_parser_set_source_position_from_token (c_parser_peek_token (parser));
2650 if (c_parser_next_token_is (parser, CPP_NAME))
2651 {
2652 ident = c_parser_peek_token (parser)->value;
2653 ident_loc = c_parser_peek_token (parser)->location;
2654 enum_loc = ident_loc;
2655 c_parser_consume_token (parser);
2656 }
2657 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
2658 {
2659 /* Parse an enum definition. */
2660 struct c_enum_contents the_enum;
2661 tree type;
2662 tree postfix_attrs;
2663 /* We chain the enumerators in reverse order, then put them in
2664 forward order at the end. */
2665 tree values;
2666 timevar_push (TV_PARSE_ENUM);
2667 type = start_enum (enum_loc, &the_enum, ident);
2668 values = NULL_TREE;
2669 c_parser_consume_token (parser);
2670 while (true)
2671 {
2672 tree enum_id;
2673 tree enum_value;
2674 tree enum_decl;
2675 bool seen_comma;
2676 c_token *token;
2677 location_t comma_loc = UNKNOWN_LOCATION; /* Quiet warning. */
2678 location_t decl_loc, value_loc;
2679 if (c_parser_next_token_is_not (parser, CPP_NAME))
2680 {
2681 /* Give a nicer error for "enum {}". */
2682 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE)
2683 && !parser->error)
2684 {
2685 error_at (c_parser_peek_token (parser)->location,
2686 "empty enum is invalid");
2687 parser->error = true;
2688 }
2689 else
2690 c_parser_error (parser, "expected identifier");
2691 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
2692 values = error_mark_node;
2693 break;
2694 }
2695 token = c_parser_peek_token (parser);
2696 enum_id = token->value;
2697 /* Set the location in case we create a decl now. */
2698 c_parser_set_source_position_from_token (token);
2699 decl_loc = value_loc = token->location;
2700 c_parser_consume_token (parser);
2701 /* Parse any specified attributes. */
2702 tree enum_attrs = c_parser_attributes (parser);
2703 if (c_parser_next_token_is (parser, CPP_EQ))
2704 {
2705 c_parser_consume_token (parser);
2706 value_loc = c_parser_peek_token (parser)->location;
2707 enum_value = c_parser_expr_no_commas (parser, NULL).value;
2708 }
2709 else
2710 enum_value = NULL_TREE;
2711 enum_decl = build_enumerator (decl_loc, value_loc,
2712 &the_enum, enum_id, enum_value);
2713 if (enum_attrs)
2714 decl_attributes (&TREE_PURPOSE (enum_decl), enum_attrs, 0);
2715 TREE_CHAIN (enum_decl) = values;
2716 values = enum_decl;
2717 seen_comma = false;
2718 if (c_parser_next_token_is (parser, CPP_COMMA))
2719 {
2720 comma_loc = c_parser_peek_token (parser)->location;
2721 seen_comma = true;
2722 c_parser_consume_token (parser);
2723 }
2724 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2725 {
2726 if (seen_comma)
2727 pedwarn_c90 (comma_loc, OPT_Wpedantic,
2728 "comma at end of enumerator list");
2729 c_parser_consume_token (parser);
2730 break;
2731 }
2732 if (!seen_comma)
2733 {
2734 c_parser_error (parser, "expected %<,%> or %<}%>");
2735 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
2736 values = error_mark_node;
2737 break;
2738 }
2739 }
2740 postfix_attrs = c_parser_attributes (parser);
2741 ret.spec = finish_enum (type, nreverse (values),
2742 chainon (attrs, postfix_attrs));
2743 ret.kind = ctsk_tagdef;
2744 ret.expr = NULL_TREE;
2745 ret.expr_const_operands = true;
2746 timevar_pop (TV_PARSE_ENUM);
2747 return ret;
2748 }
2749 else if (!ident)
2750 {
2751 c_parser_error (parser, "expected %<{%>");
2752 ret.spec = error_mark_node;
2753 ret.kind = ctsk_tagref;
2754 ret.expr = NULL_TREE;
2755 ret.expr_const_operands = true;
2756 return ret;
2757 }
2758 ret = parser_xref_tag (ident_loc, ENUMERAL_TYPE, ident);
2759 /* In ISO C, enumerated types can be referred to only if already
2760 defined. */
2761 if (pedantic && !COMPLETE_TYPE_P (ret.spec))
2762 {
2763 gcc_assert (ident);
2764 pedwarn (enum_loc, OPT_Wpedantic,
2765 "ISO C forbids forward references to %<enum%> types");
2766 }
2767 return ret;
2768 }
2769
2770 /* Parse a struct or union specifier (C90 6.5.2.1, C99 6.7.2.1).
2771
2772 struct-or-union-specifier:
2773 struct-or-union attributes[opt] identifier[opt]
2774 { struct-contents } attributes[opt]
2775 struct-or-union attributes[opt] identifier
2776
2777 struct-contents:
2778 struct-declaration-list
2779
2780 struct-declaration-list:
2781 struct-declaration ;
2782 struct-declaration-list struct-declaration ;
2783
2784 GNU extensions:
2785
2786 struct-contents:
2787 empty
2788 struct-declaration
2789 struct-declaration-list struct-declaration
2790
2791 struct-declaration-list:
2792 struct-declaration-list ;
2793 ;
2794
2795 (Note that in the syntax here, unlike that in ISO C, the semicolons
2796 are included here rather than in struct-declaration, in order to
2797 describe the syntax with extra semicolons and missing semicolon at
2798 end.)
2799
2800 Objective-C:
2801
2802 struct-declaration-list:
2803 @defs ( class-name )
2804
2805 (Note this does not include a trailing semicolon, but can be
2806 followed by further declarations, and gets a pedwarn-if-pedantic
2807 when followed by a semicolon.) */
2808
2809 static struct c_typespec
2810 c_parser_struct_or_union_specifier (c_parser *parser)
2811 {
2812 struct c_typespec ret;
2813 tree attrs;
2814 tree ident = NULL_TREE;
2815 location_t struct_loc;
2816 location_t ident_loc = UNKNOWN_LOCATION;
2817 enum tree_code code;
2818 switch (c_parser_peek_token (parser)->keyword)
2819 {
2820 case RID_STRUCT:
2821 code = RECORD_TYPE;
2822 break;
2823 case RID_UNION:
2824 code = UNION_TYPE;
2825 break;
2826 default:
2827 gcc_unreachable ();
2828 }
2829 struct_loc = c_parser_peek_token (parser)->location;
2830 c_parser_consume_token (parser);
2831 attrs = c_parser_attributes (parser);
2832
2833 /* Set the location in case we create a decl now. */
2834 c_parser_set_source_position_from_token (c_parser_peek_token (parser));
2835
2836 if (c_parser_next_token_is (parser, CPP_NAME))
2837 {
2838 ident = c_parser_peek_token (parser)->value;
2839 ident_loc = c_parser_peek_token (parser)->location;
2840 struct_loc = ident_loc;
2841 c_parser_consume_token (parser);
2842 }
2843 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
2844 {
2845 /* Parse a struct or union definition. Start the scope of the
2846 tag before parsing components. */
2847 struct c_struct_parse_info *struct_info;
2848 tree type = start_struct (struct_loc, code, ident, &struct_info);
2849 tree postfix_attrs;
2850 /* We chain the components in reverse order, then put them in
2851 forward order at the end. Each struct-declaration may
2852 declare multiple components (comma-separated), so we must use
2853 chainon to join them, although when parsing each
2854 struct-declaration we can use TREE_CHAIN directly.
2855
2856 The theory behind all this is that there will be more
2857 semicolon separated fields than comma separated fields, and
2858 so we'll be minimizing the number of node traversals required
2859 by chainon. */
2860 tree contents;
2861 timevar_push (TV_PARSE_STRUCT);
2862 contents = NULL_TREE;
2863 c_parser_consume_token (parser);
2864 /* Handle the Objective-C @defs construct,
2865 e.g. foo(sizeof(struct{ @defs(ClassName) }));. */
2866 if (c_parser_next_token_is_keyword (parser, RID_AT_DEFS))
2867 {
2868 tree name;
2869 gcc_assert (c_dialect_objc ());
2870 c_parser_consume_token (parser);
2871 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2872 goto end_at_defs;
2873 if (c_parser_next_token_is (parser, CPP_NAME)
2874 && c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME)
2875 {
2876 name = c_parser_peek_token (parser)->value;
2877 c_parser_consume_token (parser);
2878 }
2879 else
2880 {
2881 c_parser_error (parser, "expected class name");
2882 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
2883 goto end_at_defs;
2884 }
2885 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2886 "expected %<)%>");
2887 contents = nreverse (objc_get_class_ivars (name));
2888 }
2889 end_at_defs:
2890 /* Parse the struct-declarations and semicolons. Problems with
2891 semicolons are diagnosed here; empty structures are diagnosed
2892 elsewhere. */
2893 while (true)
2894 {
2895 tree decls;
2896 /* Parse any stray semicolon. */
2897 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
2898 {
2899 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
2900 "extra semicolon in struct or union specified");
2901 c_parser_consume_token (parser);
2902 continue;
2903 }
2904 /* Stop if at the end of the struct or union contents. */
2905 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2906 {
2907 c_parser_consume_token (parser);
2908 break;
2909 }
2910 /* Accept #pragmas at struct scope. */
2911 if (c_parser_next_token_is (parser, CPP_PRAGMA))
2912 {
2913 c_parser_pragma (parser, pragma_struct);
2914 continue;
2915 }
2916 /* Parse some comma-separated declarations, but not the
2917 trailing semicolon if any. */
2918 decls = c_parser_struct_declaration (parser);
2919 contents = chainon (decls, contents);
2920 /* If no semicolon follows, either we have a parse error or
2921 are at the end of the struct or union and should
2922 pedwarn. */
2923 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
2924 c_parser_consume_token (parser);
2925 else
2926 {
2927 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2928 pedwarn (c_parser_peek_token (parser)->location, 0,
2929 "no semicolon at end of struct or union");
2930 else if (parser->error
2931 || !c_parser_next_token_starts_declspecs (parser))
2932 {
2933 c_parser_error (parser, "expected %<;%>");
2934 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
2935 break;
2936 }
2937
2938 /* If we come here, we have already emitted an error
2939 for an expected `;', identifier or `(', and we also
2940 recovered already. Go on with the next field. */
2941 }
2942 }
2943 postfix_attrs = c_parser_attributes (parser);
2944 ret.spec = finish_struct (struct_loc, type, nreverse (contents),
2945 chainon (attrs, postfix_attrs), struct_info);
2946 ret.kind = ctsk_tagdef;
2947 ret.expr = NULL_TREE;
2948 ret.expr_const_operands = true;
2949 timevar_pop (TV_PARSE_STRUCT);
2950 return ret;
2951 }
2952 else if (!ident)
2953 {
2954 c_parser_error (parser, "expected %<{%>");
2955 ret.spec = error_mark_node;
2956 ret.kind = ctsk_tagref;
2957 ret.expr = NULL_TREE;
2958 ret.expr_const_operands = true;
2959 return ret;
2960 }
2961 ret = parser_xref_tag (ident_loc, code, ident);
2962 return ret;
2963 }
2964
2965 /* Parse a struct-declaration (C90 6.5.2.1, C99 6.7.2.1), *without*
2966 the trailing semicolon.
2967
2968 struct-declaration:
2969 specifier-qualifier-list struct-declarator-list
2970 static_assert-declaration-no-semi
2971
2972 specifier-qualifier-list:
2973 type-specifier specifier-qualifier-list[opt]
2974 type-qualifier specifier-qualifier-list[opt]
2975 attributes specifier-qualifier-list[opt]
2976
2977 struct-declarator-list:
2978 struct-declarator
2979 struct-declarator-list , attributes[opt] struct-declarator
2980
2981 struct-declarator:
2982 declarator attributes[opt]
2983 declarator[opt] : constant-expression attributes[opt]
2984
2985 GNU extensions:
2986
2987 struct-declaration:
2988 __extension__ struct-declaration
2989 specifier-qualifier-list
2990
2991 Unlike the ISO C syntax, semicolons are handled elsewhere. The use
2992 of attributes where shown is a GNU extension. In GNU C, we accept
2993 any expression without commas in the syntax (assignment
2994 expressions, not just conditional expressions); assignment
2995 expressions will be diagnosed as non-constant. */
2996
2997 static tree
2998 c_parser_struct_declaration (c_parser *parser)
2999 {
3000 struct c_declspecs *specs;
3001 tree prefix_attrs;
3002 tree all_prefix_attrs;
3003 tree decls;
3004 location_t decl_loc;
3005 if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
3006 {
3007 int ext;
3008 tree decl;
3009 ext = disable_extension_diagnostics ();
3010 c_parser_consume_token (parser);
3011 decl = c_parser_struct_declaration (parser);
3012 restore_extension_diagnostics (ext);
3013 return decl;
3014 }
3015 if (c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT))
3016 {
3017 c_parser_static_assert_declaration_no_semi (parser);
3018 return NULL_TREE;
3019 }
3020 specs = build_null_declspecs ();
3021 decl_loc = c_parser_peek_token (parser)->location;
3022 /* Strictly by the standard, we shouldn't allow _Alignas here,
3023 but it appears to have been intended to allow it there, so
3024 we're keeping it as it is until WG14 reaches a conclusion
3025 of N1731.
3026 <http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1731.pdf> */
3027 c_parser_declspecs (parser, specs, false, true, true,
3028 true, false, cla_nonabstract_decl);
3029 if (parser->error)
3030 return NULL_TREE;
3031 if (!specs->declspecs_seen_p)
3032 {
3033 c_parser_error (parser, "expected specifier-qualifier-list");
3034 return NULL_TREE;
3035 }
3036 finish_declspecs (specs);
3037 if (c_parser_next_token_is (parser, CPP_SEMICOLON)
3038 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3039 {
3040 tree ret;
3041 if (specs->typespec_kind == ctsk_none)
3042 {
3043 pedwarn (decl_loc, OPT_Wpedantic,
3044 "ISO C forbids member declarations with no members");
3045 shadow_tag_warned (specs, pedantic);
3046 ret = NULL_TREE;
3047 }
3048 else
3049 {
3050 /* Support for unnamed structs or unions as members of
3051 structs or unions (which is [a] useful and [b] supports
3052 MS P-SDK). */
3053 tree attrs = NULL;
3054
3055 ret = grokfield (c_parser_peek_token (parser)->location,
3056 build_id_declarator (NULL_TREE), specs,
3057 NULL_TREE, &attrs);
3058 if (ret)
3059 decl_attributes (&ret, attrs, 0);
3060 }
3061 return ret;
3062 }
3063
3064 /* Provide better error recovery. Note that a type name here is valid,
3065 and will be treated as a field name. */
3066 if (specs->typespec_kind == ctsk_tagdef
3067 && TREE_CODE (specs->type) != ENUMERAL_TYPE
3068 && c_parser_next_token_starts_declspecs (parser)
3069 && !c_parser_next_token_is (parser, CPP_NAME))
3070 {
3071 c_parser_error (parser, "expected %<;%>, identifier or %<(%>");
3072 parser->error = false;
3073 return NULL_TREE;
3074 }
3075
3076 pending_xref_error ();
3077 prefix_attrs = specs->attrs;
3078 all_prefix_attrs = prefix_attrs;
3079 specs->attrs = NULL_TREE;
3080 decls = NULL_TREE;
3081 while (true)
3082 {
3083 /* Declaring one or more declarators or un-named bit-fields. */
3084 struct c_declarator *declarator;
3085 bool dummy = false;
3086 if (c_parser_next_token_is (parser, CPP_COLON))
3087 declarator = build_id_declarator (NULL_TREE);
3088 else
3089 declarator = c_parser_declarator (parser,
3090 specs->typespec_kind != ctsk_none,
3091 C_DTR_NORMAL, &dummy);
3092 if (declarator == NULL)
3093 {
3094 c_parser_skip_to_end_of_block_or_statement (parser);
3095 break;
3096 }
3097 if (c_parser_next_token_is (parser, CPP_COLON)
3098 || c_parser_next_token_is (parser, CPP_COMMA)
3099 || c_parser_next_token_is (parser, CPP_SEMICOLON)
3100 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE)
3101 || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
3102 {
3103 tree postfix_attrs = NULL_TREE;
3104 tree width = NULL_TREE;
3105 tree d;
3106 if (c_parser_next_token_is (parser, CPP_COLON))
3107 {
3108 c_parser_consume_token (parser);
3109 width = c_parser_expr_no_commas (parser, NULL).value;
3110 }
3111 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
3112 postfix_attrs = c_parser_attributes (parser);
3113 d = grokfield (c_parser_peek_token (parser)->location,
3114 declarator, specs, width, &all_prefix_attrs);
3115 decl_attributes (&d, chainon (postfix_attrs,
3116 all_prefix_attrs), 0);
3117 DECL_CHAIN (d) = decls;
3118 decls = d;
3119 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
3120 all_prefix_attrs = chainon (c_parser_attributes (parser),
3121 prefix_attrs);
3122 else
3123 all_prefix_attrs = prefix_attrs;
3124 if (c_parser_next_token_is (parser, CPP_COMMA))
3125 c_parser_consume_token (parser);
3126 else if (c_parser_next_token_is (parser, CPP_SEMICOLON)
3127 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3128 {
3129 /* Semicolon consumed in caller. */
3130 break;
3131 }
3132 else
3133 {
3134 c_parser_error (parser, "expected %<,%>, %<;%> or %<}%>");
3135 break;
3136 }
3137 }
3138 else
3139 {
3140 c_parser_error (parser,
3141 "expected %<:%>, %<,%>, %<;%>, %<}%> or "
3142 "%<__attribute__%>");
3143 break;
3144 }
3145 }
3146 return decls;
3147 }
3148
3149 /* Parse a typeof specifier (a GNU extension).
3150
3151 typeof-specifier:
3152 typeof ( expression )
3153 typeof ( type-name )
3154 */
3155
3156 static struct c_typespec
3157 c_parser_typeof_specifier (c_parser *parser)
3158 {
3159 struct c_typespec ret;
3160 ret.kind = ctsk_typeof;
3161 ret.spec = error_mark_node;
3162 ret.expr = NULL_TREE;
3163 ret.expr_const_operands = true;
3164 gcc_assert (c_parser_next_token_is_keyword (parser, RID_TYPEOF));
3165 c_parser_consume_token (parser);
3166 c_inhibit_evaluation_warnings++;
3167 in_typeof++;
3168 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3169 {
3170 c_inhibit_evaluation_warnings--;
3171 in_typeof--;
3172 return ret;
3173 }
3174 if (c_parser_next_tokens_start_typename (parser, cla_prefer_id))
3175 {
3176 struct c_type_name *type = c_parser_type_name (parser);
3177 c_inhibit_evaluation_warnings--;
3178 in_typeof--;
3179 if (type != NULL)
3180 {
3181 ret.spec = groktypename (type, &ret.expr, &ret.expr_const_operands);
3182 pop_maybe_used (variably_modified_type_p (ret.spec, NULL_TREE));
3183 }
3184 }
3185 else
3186 {
3187 bool was_vm;
3188 location_t here = c_parser_peek_token (parser)->location;
3189 struct c_expr expr = c_parser_expression (parser);
3190 c_inhibit_evaluation_warnings--;
3191 in_typeof--;
3192 if (TREE_CODE (expr.value) == COMPONENT_REF
3193 && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1)))
3194 error_at (here, "%<typeof%> applied to a bit-field");
3195 mark_exp_read (expr.value);
3196 ret.spec = TREE_TYPE (expr.value);
3197 was_vm = variably_modified_type_p (ret.spec, NULL_TREE);
3198 /* This is returned with the type so that when the type is
3199 evaluated, this can be evaluated. */
3200 if (was_vm)
3201 ret.expr = c_fully_fold (expr.value, false, &ret.expr_const_operands);
3202 pop_maybe_used (was_vm);
3203 /* For use in macros such as those in <stdatomic.h>, remove all
3204 qualifiers from atomic types. (const can be an issue for more macros
3205 using typeof than just the <stdatomic.h> ones.) */
3206 if (ret.spec != error_mark_node && TYPE_ATOMIC (ret.spec))
3207 ret.spec = c_build_qualified_type (ret.spec, TYPE_UNQUALIFIED);
3208 }
3209 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
3210 return ret;
3211 }
3212
3213 /* Parse an alignment-specifier.
3214
3215 C11 6.7.5:
3216
3217 alignment-specifier:
3218 _Alignas ( type-name )
3219 _Alignas ( constant-expression )
3220 */
3221
3222 static tree
3223 c_parser_alignas_specifier (c_parser * parser)
3224 {
3225 tree ret = error_mark_node;
3226 location_t loc = c_parser_peek_token (parser)->location;
3227 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ALIGNAS));
3228 c_parser_consume_token (parser);
3229 if (flag_isoc99)
3230 pedwarn_c99 (loc, OPT_Wpedantic,
3231 "ISO C99 does not support %<_Alignas%>");
3232 else
3233 pedwarn_c99 (loc, OPT_Wpedantic,
3234 "ISO C90 does not support %<_Alignas%>");
3235 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3236 return ret;
3237 if (c_parser_next_tokens_start_typename (parser, cla_prefer_id))
3238 {
3239 struct c_type_name *type = c_parser_type_name (parser);
3240 if (type != NULL)
3241 ret = c_sizeof_or_alignof_type (loc, groktypename (type, NULL, NULL),
3242 false, true, 1);
3243 }
3244 else
3245 ret = c_parser_expr_no_commas (parser, NULL).value;
3246 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
3247 return ret;
3248 }
3249
3250 /* Parse a declarator, possibly an abstract declarator (C90 6.5.4,
3251 6.5.5, C99 6.7.5, 6.7.6). If TYPE_SEEN_P then a typedef name may
3252 be redeclared; otherwise it may not. KIND indicates which kind of
3253 declarator is wanted. Returns a valid declarator except in the
3254 case of a syntax error in which case NULL is returned. *SEEN_ID is
3255 set to true if an identifier being declared is seen; this is used
3256 to diagnose bad forms of abstract array declarators and to
3257 determine whether an identifier list is syntactically permitted.
3258
3259 declarator:
3260 pointer[opt] direct-declarator
3261
3262 direct-declarator:
3263 identifier
3264 ( attributes[opt] declarator )
3265 direct-declarator array-declarator
3266 direct-declarator ( parameter-type-list )
3267 direct-declarator ( identifier-list[opt] )
3268
3269 pointer:
3270 * type-qualifier-list[opt]
3271 * type-qualifier-list[opt] pointer
3272
3273 type-qualifier-list:
3274 type-qualifier
3275 attributes
3276 type-qualifier-list type-qualifier
3277 type-qualifier-list attributes
3278
3279 array-declarator:
3280 [ type-qualifier-list[opt] assignment-expression[opt] ]
3281 [ static type-qualifier-list[opt] assignment-expression ]
3282 [ type-qualifier-list static assignment-expression ]
3283 [ type-qualifier-list[opt] * ]
3284
3285 parameter-type-list:
3286 parameter-list
3287 parameter-list , ...
3288
3289 parameter-list:
3290 parameter-declaration
3291 parameter-list , parameter-declaration
3292
3293 parameter-declaration:
3294 declaration-specifiers declarator attributes[opt]
3295 declaration-specifiers abstract-declarator[opt] attributes[opt]
3296
3297 identifier-list:
3298 identifier
3299 identifier-list , identifier
3300
3301 abstract-declarator:
3302 pointer
3303 pointer[opt] direct-abstract-declarator
3304
3305 direct-abstract-declarator:
3306 ( attributes[opt] abstract-declarator )
3307 direct-abstract-declarator[opt] array-declarator
3308 direct-abstract-declarator[opt] ( parameter-type-list[opt] )
3309
3310 GNU extensions:
3311
3312 direct-declarator:
3313 direct-declarator ( parameter-forward-declarations
3314 parameter-type-list[opt] )
3315
3316 direct-abstract-declarator:
3317 direct-abstract-declarator[opt] ( parameter-forward-declarations
3318 parameter-type-list[opt] )
3319
3320 parameter-forward-declarations:
3321 parameter-list ;
3322 parameter-forward-declarations parameter-list ;
3323
3324 The uses of attributes shown above are GNU extensions.
3325
3326 Some forms of array declarator are not included in C99 in the
3327 syntax for abstract declarators; these are disallowed elsewhere.
3328 This may be a defect (DR#289).
3329
3330 This function also accepts an omitted abstract declarator as being
3331 an abstract declarator, although not part of the formal syntax. */
3332
3333 static struct c_declarator *
3334 c_parser_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
3335 bool *seen_id)
3336 {
3337 /* Parse any initial pointer part. */
3338 if (c_parser_next_token_is (parser, CPP_MULT))
3339 {
3340 struct c_declspecs *quals_attrs = build_null_declspecs ();
3341 struct c_declarator *inner;
3342 c_parser_consume_token (parser);
3343 c_parser_declspecs (parser, quals_attrs, false, false, true,
3344 false, false, cla_prefer_id);
3345 inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
3346 if (inner == NULL)
3347 return NULL;
3348 else
3349 return make_pointer_declarator (quals_attrs, inner);
3350 }
3351 /* Now we have a direct declarator, direct abstract declarator or
3352 nothing (which counts as a direct abstract declarator here). */
3353 return c_parser_direct_declarator (parser, type_seen_p, kind, seen_id);
3354 }
3355
3356 /* Parse a direct declarator or direct abstract declarator; arguments
3357 as c_parser_declarator. */
3358
3359 static struct c_declarator *
3360 c_parser_direct_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
3361 bool *seen_id)
3362 {
3363 /* The direct declarator must start with an identifier (possibly
3364 omitted) or a parenthesized declarator (possibly abstract). In
3365 an ordinary declarator, initial parentheses must start a
3366 parenthesized declarator. In an abstract declarator or parameter
3367 declarator, they could start a parenthesized declarator or a
3368 parameter list. To tell which, the open parenthesis and any
3369 following attributes must be read. If a declaration specifier
3370 follows, then it is a parameter list; if the specifier is a
3371 typedef name, there might be an ambiguity about redeclaring it,
3372 which is resolved in the direction of treating it as a typedef
3373 name. If a close parenthesis follows, it is also an empty
3374 parameter list, as the syntax does not permit empty abstract
3375 declarators. Otherwise, it is a parenthesized declarator (in
3376 which case the analysis may be repeated inside it, recursively).
3377
3378 ??? There is an ambiguity in a parameter declaration "int
3379 (__attribute__((foo)) x)", where x is not a typedef name: it
3380 could be an abstract declarator for a function, or declare x with
3381 parentheses. The proper resolution of this ambiguity needs
3382 documenting. At present we follow an accident of the old
3383 parser's implementation, whereby the first parameter must have
3384 some declaration specifiers other than just attributes. Thus as
3385 a parameter declaration it is treated as a parenthesized
3386 parameter named x, and as an abstract declarator it is
3387 rejected.
3388
3389 ??? Also following the old parser, attributes inside an empty
3390 parameter list are ignored, making it a list not yielding a
3391 prototype, rather than giving an error or making it have one
3392 parameter with implicit type int.
3393
3394 ??? Also following the old parser, typedef names may be
3395 redeclared in declarators, but not Objective-C class names. */
3396
3397 if (kind != C_DTR_ABSTRACT
3398 && c_parser_next_token_is (parser, CPP_NAME)
3399 && ((type_seen_p
3400 && (c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME
3401 || c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME))
3402 || c_parser_peek_token (parser)->id_kind == C_ID_ID))
3403 {
3404 struct c_declarator *inner
3405 = build_id_declarator (c_parser_peek_token (parser)->value);
3406 *seen_id = true;
3407 inner->id_loc = c_parser_peek_token (parser)->location;
3408 c_parser_consume_token (parser);
3409 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
3410 }
3411
3412 if (kind != C_DTR_NORMAL
3413 && c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
3414 {
3415 struct c_declarator *inner = build_id_declarator (NULL_TREE);
3416 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
3417 }
3418
3419 /* Either we are at the end of an abstract declarator, or we have
3420 parentheses. */
3421
3422 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
3423 {
3424 tree attrs;
3425 struct c_declarator *inner;
3426 c_parser_consume_token (parser);
3427 attrs = c_parser_attributes (parser);
3428 if (kind != C_DTR_NORMAL
3429 && (c_parser_next_token_starts_declspecs (parser)
3430 || c_parser_next_token_is (parser, CPP_CLOSE_PAREN)))
3431 {
3432 struct c_arg_info *args
3433 = c_parser_parms_declarator (parser, kind == C_DTR_NORMAL,
3434 attrs);
3435 if (args == NULL)
3436 return NULL;
3437 else
3438 {
3439 inner
3440 = build_function_declarator (args,
3441 build_id_declarator (NULL_TREE));
3442 return c_parser_direct_declarator_inner (parser, *seen_id,
3443 inner);
3444 }
3445 }
3446 /* A parenthesized declarator. */
3447 inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
3448 if (inner != NULL && attrs != NULL)
3449 inner = build_attrs_declarator (attrs, inner);
3450 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3451 {
3452 c_parser_consume_token (parser);
3453 if (inner == NULL)
3454 return NULL;
3455 else
3456 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
3457 }
3458 else
3459 {
3460 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3461 "expected %<)%>");
3462 return NULL;
3463 }
3464 }
3465 else
3466 {
3467 if (kind == C_DTR_NORMAL)
3468 {
3469 c_parser_error (parser, "expected identifier or %<(%>");
3470 return NULL;
3471 }
3472 else
3473 return build_id_declarator (NULL_TREE);
3474 }
3475 }
3476
3477 /* Parse part of a direct declarator or direct abstract declarator,
3478 given that some (in INNER) has already been parsed; ID_PRESENT is
3479 true if an identifier is present, false for an abstract
3480 declarator. */
3481
3482 static struct c_declarator *
3483 c_parser_direct_declarator_inner (c_parser *parser, bool id_present,
3484 struct c_declarator *inner)
3485 {
3486 /* Parse a sequence of array declarators and parameter lists. */
3487 if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
3488 {
3489 location_t brace_loc = c_parser_peek_token (parser)->location;
3490 struct c_declarator *declarator;
3491 struct c_declspecs *quals_attrs = build_null_declspecs ();
3492 bool static_seen;
3493 bool star_seen;
3494 struct c_expr dimen;
3495 dimen.value = NULL_TREE;
3496 dimen.original_code = ERROR_MARK;
3497 dimen.original_type = NULL_TREE;
3498 c_parser_consume_token (parser);
3499 c_parser_declspecs (parser, quals_attrs, false, false, true,
3500 false, false, cla_prefer_id);
3501 static_seen = c_parser_next_token_is_keyword (parser, RID_STATIC);
3502 if (static_seen)
3503 c_parser_consume_token (parser);
3504 if (static_seen && !quals_attrs->declspecs_seen_p)
3505 c_parser_declspecs (parser, quals_attrs, false, false, true,
3506 false, false, cla_prefer_id);
3507 if (!quals_attrs->declspecs_seen_p)
3508 quals_attrs = NULL;
3509 /* If "static" is present, there must be an array dimension.
3510 Otherwise, there may be a dimension, "*", or no
3511 dimension. */
3512 if (static_seen)
3513 {
3514 star_seen = false;
3515 dimen = c_parser_expr_no_commas (parser, NULL);
3516 }
3517 else
3518 {
3519 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3520 {
3521 dimen.value = NULL_TREE;
3522 star_seen = false;
3523 }
3524 else if (flag_cilkplus
3525 && c_parser_next_token_is (parser, CPP_COLON))
3526 {
3527 dimen.value = error_mark_node;
3528 star_seen = false;
3529 error_at (c_parser_peek_token (parser)->location,
3530 "array notations cannot be used in declaration");
3531 c_parser_consume_token (parser);
3532 }
3533 else if (c_parser_next_token_is (parser, CPP_MULT))
3534 {
3535 if (c_parser_peek_2nd_token (parser)->type == CPP_CLOSE_SQUARE)
3536 {
3537 dimen.value = NULL_TREE;
3538 star_seen = true;
3539 c_parser_consume_token (parser);
3540 }
3541 else
3542 {
3543 star_seen = false;
3544 dimen = c_parser_expr_no_commas (parser, NULL);
3545 }
3546 }
3547 else
3548 {
3549 star_seen = false;
3550 dimen = c_parser_expr_no_commas (parser, NULL);
3551 }
3552 }
3553 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3554 c_parser_consume_token (parser);
3555 else if (flag_cilkplus
3556 && c_parser_next_token_is (parser, CPP_COLON))
3557 {
3558 error_at (c_parser_peek_token (parser)->location,
3559 "array notations cannot be used in declaration");
3560 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
3561 return NULL;
3562 }
3563 else
3564 {
3565 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
3566 "expected %<]%>");
3567 return NULL;
3568 }
3569 if (dimen.value)
3570 dimen = convert_lvalue_to_rvalue (brace_loc, dimen, true, true);
3571 declarator = build_array_declarator (brace_loc, dimen.value, quals_attrs,
3572 static_seen, star_seen);
3573 if (declarator == NULL)
3574 return NULL;
3575 inner = set_array_declarator_inner (declarator, inner);
3576 return c_parser_direct_declarator_inner (parser, id_present, inner);
3577 }
3578 else if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
3579 {
3580 tree attrs;
3581 struct c_arg_info *args;
3582 c_parser_consume_token (parser);
3583 attrs = c_parser_attributes (parser);
3584 args = c_parser_parms_declarator (parser, id_present, attrs);
3585 if (args == NULL)
3586 return NULL;
3587 else
3588 {
3589 inner = build_function_declarator (args, inner);
3590 return c_parser_direct_declarator_inner (parser, id_present, inner);
3591 }
3592 }
3593 return inner;
3594 }
3595
3596 /* Parse a parameter list or identifier list, including the closing
3597 parenthesis but not the opening one. ATTRS are the attributes at
3598 the start of the list. ID_LIST_OK is true if an identifier list is
3599 acceptable; such a list must not have attributes at the start. */
3600
3601 static struct c_arg_info *
3602 c_parser_parms_declarator (c_parser *parser, bool id_list_ok, tree attrs)
3603 {
3604 push_scope ();
3605 declare_parm_level ();
3606 /* If the list starts with an identifier, it is an identifier list.
3607 Otherwise, it is either a prototype list or an empty list. */
3608 if (id_list_ok
3609 && !attrs
3610 && c_parser_next_token_is (parser, CPP_NAME)
3611 && c_parser_peek_token (parser)->id_kind == C_ID_ID
3612
3613 /* Look ahead to detect typos in type names. */
3614 && c_parser_peek_2nd_token (parser)->type != CPP_NAME
3615 && c_parser_peek_2nd_token (parser)->type != CPP_MULT
3616 && c_parser_peek_2nd_token (parser)->type != CPP_OPEN_PAREN
3617 && c_parser_peek_2nd_token (parser)->type != CPP_OPEN_SQUARE)
3618 {
3619 tree list = NULL_TREE, *nextp = &list;
3620 while (c_parser_next_token_is (parser, CPP_NAME)
3621 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
3622 {
3623 *nextp = build_tree_list (NULL_TREE,
3624 c_parser_peek_token (parser)->value);
3625 nextp = & TREE_CHAIN (*nextp);
3626 c_parser_consume_token (parser);
3627 if (c_parser_next_token_is_not (parser, CPP_COMMA))
3628 break;
3629 c_parser_consume_token (parser);
3630 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3631 {
3632 c_parser_error (parser, "expected identifier");
3633 break;
3634 }
3635 }
3636 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3637 {
3638 struct c_arg_info *ret = build_arg_info ();
3639 ret->types = list;
3640 c_parser_consume_token (parser);
3641 pop_scope ();
3642 return ret;
3643 }
3644 else
3645 {
3646 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3647 "expected %<)%>");
3648 pop_scope ();
3649 return NULL;
3650 }
3651 }
3652 else
3653 {
3654 struct c_arg_info *ret = c_parser_parms_list_declarator (parser, attrs,
3655 NULL);
3656 pop_scope ();
3657 return ret;
3658 }
3659 }
3660
3661 /* Parse a parameter list (possibly empty), including the closing
3662 parenthesis but not the opening one. ATTRS are the attributes at
3663 the start of the list. EXPR is NULL or an expression that needs to
3664 be evaluated for the side effects of array size expressions in the
3665 parameters. */
3666
3667 static struct c_arg_info *
3668 c_parser_parms_list_declarator (c_parser *parser, tree attrs, tree expr)
3669 {
3670 bool bad_parm = false;
3671
3672 /* ??? Following the old parser, forward parameter declarations may
3673 use abstract declarators, and if no real parameter declarations
3674 follow the forward declarations then this is not diagnosed. Also
3675 note as above that attributes are ignored as the only contents of
3676 the parentheses, or as the only contents after forward
3677 declarations. */
3678 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3679 {
3680 struct c_arg_info *ret = build_arg_info ();
3681 c_parser_consume_token (parser);
3682 return ret;
3683 }
3684 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
3685 {
3686 struct c_arg_info *ret = build_arg_info ();
3687
3688 if (flag_allow_parameterless_variadic_functions)
3689 {
3690 /* F (...) is allowed. */
3691 ret->types = NULL_TREE;
3692 }
3693 else
3694 {
3695 /* Suppress -Wold-style-definition for this case. */
3696 ret->types = error_mark_node;
3697 error_at (c_parser_peek_token (parser)->location,
3698 "ISO C requires a named argument before %<...%>");
3699 }
3700 c_parser_consume_token (parser);
3701 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3702 {
3703 c_parser_consume_token (parser);
3704 return ret;
3705 }
3706 else
3707 {
3708 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3709 "expected %<)%>");
3710 return NULL;
3711 }
3712 }
3713 /* Nonempty list of parameters, either terminated with semicolon
3714 (forward declarations; recurse) or with close parenthesis (normal
3715 function) or with ", ... )" (variadic function). */
3716 while (true)
3717 {
3718 /* Parse a parameter. */
3719 struct c_parm *parm = c_parser_parameter_declaration (parser, attrs);
3720 attrs = NULL_TREE;
3721 if (parm == NULL)
3722 bad_parm = true;
3723 else
3724 push_parm_decl (parm, &expr);
3725 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
3726 {
3727 tree new_attrs;
3728 c_parser_consume_token (parser);
3729 mark_forward_parm_decls ();
3730 new_attrs = c_parser_attributes (parser);
3731 return c_parser_parms_list_declarator (parser, new_attrs, expr);
3732 }
3733 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3734 {
3735 c_parser_consume_token (parser);
3736 if (bad_parm)
3737 return NULL;
3738 else
3739 return get_parm_info (false, expr);
3740 }
3741 if (!c_parser_require (parser, CPP_COMMA,
3742 "expected %<;%>, %<,%> or %<)%>"))
3743 {
3744 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
3745 return NULL;
3746 }
3747 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
3748 {
3749 c_parser_consume_token (parser);
3750 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3751 {
3752 c_parser_consume_token (parser);
3753 if (bad_parm)
3754 return NULL;
3755 else
3756 return get_parm_info (true, expr);
3757 }
3758 else
3759 {
3760 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3761 "expected %<)%>");
3762 return NULL;
3763 }
3764 }
3765 }
3766 }
3767
3768 /* Parse a parameter declaration. ATTRS are the attributes at the
3769 start of the declaration if it is the first parameter. */
3770
3771 static struct c_parm *
3772 c_parser_parameter_declaration (c_parser *parser, tree attrs)
3773 {
3774 struct c_declspecs *specs;
3775 struct c_declarator *declarator;
3776 tree prefix_attrs;
3777 tree postfix_attrs = NULL_TREE;
3778 bool dummy = false;
3779
3780 /* Accept #pragmas between parameter declarations. */
3781 while (c_parser_next_token_is (parser, CPP_PRAGMA))
3782 c_parser_pragma (parser, pragma_param);
3783
3784 if (!c_parser_next_token_starts_declspecs (parser))
3785 {
3786 c_token *token = c_parser_peek_token (parser);
3787 if (parser->error)
3788 return NULL;
3789 c_parser_set_source_position_from_token (token);
3790 if (c_parser_next_tokens_start_typename (parser, cla_prefer_type))
3791 {
3792 error_at (token->location, "unknown type name %qE", token->value);
3793 parser->error = true;
3794 }
3795 /* ??? In some Objective-C cases '...' isn't applicable so there
3796 should be a different message. */
3797 else
3798 c_parser_error (parser,
3799 "expected declaration specifiers or %<...%>");
3800 c_parser_skip_to_end_of_parameter (parser);
3801 return NULL;
3802 }
3803 specs = build_null_declspecs ();
3804 if (attrs)
3805 {
3806 declspecs_add_attrs (input_location, specs, attrs);
3807 attrs = NULL_TREE;
3808 }
3809 c_parser_declspecs (parser, specs, true, true, true, true, false,
3810 cla_nonabstract_decl);
3811 finish_declspecs (specs);
3812 pending_xref_error ();
3813 prefix_attrs = specs->attrs;
3814 specs->attrs = NULL_TREE;
3815 declarator = c_parser_declarator (parser,
3816 specs->typespec_kind != ctsk_none,
3817 C_DTR_PARM, &dummy);
3818 if (declarator == NULL)
3819 {
3820 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
3821 return NULL;
3822 }
3823 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
3824 postfix_attrs = c_parser_attributes (parser);
3825 return build_c_parm (specs, chainon (postfix_attrs, prefix_attrs),
3826 declarator);
3827 }
3828
3829 /* Parse a string literal in an asm expression. It should not be
3830 translated, and wide string literals are an error although
3831 permitted by the syntax. This is a GNU extension.
3832
3833 asm-string-literal:
3834 string-literal
3835
3836 ??? At present, following the old parser, the caller needs to have
3837 set lex_untranslated_string to 1. It would be better to follow the
3838 C++ parser rather than using this kludge. */
3839
3840 static tree
3841 c_parser_asm_string_literal (c_parser *parser)
3842 {
3843 tree str;
3844 int save_flag = warn_overlength_strings;
3845 warn_overlength_strings = 0;
3846 if (c_parser_next_token_is (parser, CPP_STRING))
3847 {
3848 str = c_parser_peek_token (parser)->value;
3849 c_parser_consume_token (parser);
3850 }
3851 else if (c_parser_next_token_is (parser, CPP_WSTRING))
3852 {
3853 error_at (c_parser_peek_token (parser)->location,
3854 "wide string literal in %<asm%>");
3855 str = build_string (1, "");
3856 c_parser_consume_token (parser);
3857 }
3858 else
3859 {
3860 c_parser_error (parser, "expected string literal");
3861 str = NULL_TREE;
3862 }
3863 warn_overlength_strings = save_flag;
3864 return str;
3865 }
3866
3867 /* Parse a simple asm expression. This is used in restricted
3868 contexts, where a full expression with inputs and outputs does not
3869 make sense. This is a GNU extension.
3870
3871 simple-asm-expr:
3872 asm ( asm-string-literal )
3873 */
3874
3875 static tree
3876 c_parser_simple_asm_expr (c_parser *parser)
3877 {
3878 tree str;
3879 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM));
3880 /* ??? Follow the C++ parser rather than using the
3881 lex_untranslated_string kludge. */
3882 parser->lex_untranslated_string = true;
3883 c_parser_consume_token (parser);
3884 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3885 {
3886 parser->lex_untranslated_string = false;
3887 return NULL_TREE;
3888 }
3889 str = c_parser_asm_string_literal (parser);
3890 parser->lex_untranslated_string = false;
3891 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
3892 {
3893 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
3894 return NULL_TREE;
3895 }
3896 return str;
3897 }
3898
3899 static tree
3900 c_parser_attribute_any_word (c_parser *parser)
3901 {
3902 tree attr_name = NULL_TREE;
3903
3904 if (c_parser_next_token_is (parser, CPP_KEYWORD))
3905 {
3906 /* ??? See comment above about what keywords are accepted here. */
3907 bool ok;
3908 switch (c_parser_peek_token (parser)->keyword)
3909 {
3910 case RID_STATIC:
3911 case RID_UNSIGNED:
3912 case RID_LONG:
3913 case RID_CONST:
3914 case RID_EXTERN:
3915 case RID_REGISTER:
3916 case RID_TYPEDEF:
3917 case RID_SHORT:
3918 case RID_INLINE:
3919 case RID_NORETURN:
3920 case RID_VOLATILE:
3921 case RID_SIGNED:
3922 case RID_AUTO:
3923 case RID_RESTRICT:
3924 case RID_COMPLEX:
3925 case RID_THREAD:
3926 case RID_INT:
3927 case RID_CHAR:
3928 case RID_FLOAT:
3929 case RID_DOUBLE:
3930 case RID_VOID:
3931 case RID_DFLOAT32:
3932 case RID_DFLOAT64:
3933 case RID_DFLOAT128:
3934 case RID_BOOL:
3935 case RID_FRACT:
3936 case RID_ACCUM:
3937 case RID_SAT:
3938 case RID_TRANSACTION_ATOMIC:
3939 case RID_TRANSACTION_CANCEL:
3940 case RID_ATOMIC:
3941 case RID_AUTO_TYPE:
3942 case RID_INT_N_0:
3943 case RID_INT_N_1:
3944 case RID_INT_N_2:
3945 case RID_INT_N_3:
3946 ok = true;
3947 break;
3948 default:
3949 ok = false;
3950 break;
3951 }
3952 if (!ok)
3953 return NULL_TREE;
3954
3955 /* Accept __attribute__((__const)) as __attribute__((const)) etc. */
3956 attr_name = ridpointers[(int) c_parser_peek_token (parser)->keyword];
3957 }
3958 else if (c_parser_next_token_is (parser, CPP_NAME))
3959 attr_name = c_parser_peek_token (parser)->value;
3960
3961 return attr_name;
3962 }
3963
3964 #define CILK_SIMD_FN_CLAUSE_MASK \
3965 ((OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_VECTORLENGTH) \
3966 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_LINEAR) \
3967 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_UNIFORM) \
3968 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_MASK) \
3969 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_NOMASK))
3970
3971 /* Parses the vector attribute of SIMD enabled functions in Cilk Plus.
3972 VEC_TOKEN is the "vector" token that is replaced with "simd" and
3973 pushed into the token list.
3974 Syntax:
3975 vector
3976 vector (<vector attributes>). */
3977
3978 static void
3979 c_parser_cilk_simd_fn_vector_attrs (c_parser *parser, c_token vec_token)
3980 {
3981 gcc_assert (is_cilkplus_vector_p (vec_token.value));
3982
3983 int paren_scope = 0;
3984 vec_safe_push (parser->cilk_simd_fn_tokens, vec_token);
3985 /* Consume the "vector" token. */
3986 c_parser_consume_token (parser);
3987
3988 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
3989 {
3990 c_parser_consume_token (parser);
3991 paren_scope++;
3992 }
3993 while (paren_scope > 0)
3994 {
3995 c_token *token = c_parser_peek_token (parser);
3996 if (token->type == CPP_OPEN_PAREN)
3997 paren_scope++;
3998 else if (token->type == CPP_CLOSE_PAREN)
3999 paren_scope--;
4000 /* Do not push the last ')' since we are not pushing the '('. */
4001 if (!(token->type == CPP_CLOSE_PAREN && paren_scope == 0))
4002 vec_safe_push (parser->cilk_simd_fn_tokens, *token);
4003 c_parser_consume_token (parser);
4004 }
4005
4006 /* Since we are converting an attribute to a pragma, we need to end the
4007 attribute with PRAGMA_EOL. */
4008 c_token eol_token;
4009 memset (&eol_token, 0, sizeof (eol_token));
4010 eol_token.type = CPP_PRAGMA_EOL;
4011 vec_safe_push (parser->cilk_simd_fn_tokens, eol_token);
4012 }
4013
4014 /* Add 2 CPP_EOF at the end of PARSER->ELEM_FN_TOKENS vector. */
4015
4016 static void
4017 c_finish_cilk_simd_fn_tokens (c_parser *parser)
4018 {
4019 c_token last_token = parser->cilk_simd_fn_tokens->last ();
4020
4021 /* c_parser_attributes is called in several places, so if these EOF
4022 tokens are already inserted, then don't do them again. */
4023 if (last_token.type == CPP_EOF)
4024 return;
4025
4026 /* Two CPP_EOF token are added as a safety net since the normal C
4027 front-end has two token look-ahead. */
4028 c_token eof_token;
4029 eof_token.type = CPP_EOF;
4030 vec_safe_push (parser->cilk_simd_fn_tokens, eof_token);
4031 vec_safe_push (parser->cilk_simd_fn_tokens, eof_token);
4032 }
4033
4034 /* Parse (possibly empty) attributes. This is a GNU extension.
4035
4036 attributes:
4037 empty
4038 attributes attribute
4039
4040 attribute:
4041 __attribute__ ( ( attribute-list ) )
4042
4043 attribute-list:
4044 attrib
4045 attribute_list , attrib
4046
4047 attrib:
4048 empty
4049 any-word
4050 any-word ( identifier )
4051 any-word ( identifier , nonempty-expr-list )
4052 any-word ( expr-list )
4053
4054 where the "identifier" must not be declared as a type, and
4055 "any-word" may be any identifier (including one declared as a
4056 type), a reserved word storage class specifier, type specifier or
4057 type qualifier. ??? This still leaves out most reserved keywords
4058 (following the old parser), shouldn't we include them, and why not
4059 allow identifiers declared as types to start the arguments? */
4060
4061 static tree
4062 c_parser_attributes (c_parser *parser)
4063 {
4064 tree attrs = NULL_TREE;
4065 while (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
4066 {
4067 /* ??? Follow the C++ parser rather than using the
4068 lex_untranslated_string kludge. */
4069 parser->lex_untranslated_string = true;
4070 /* Consume the `__attribute__' keyword. */
4071 c_parser_consume_token (parser);
4072 /* Look for the two `(' tokens. */
4073 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4074 {
4075 parser->lex_untranslated_string = false;
4076 return attrs;
4077 }
4078 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4079 {
4080 parser->lex_untranslated_string = false;
4081 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4082 return attrs;
4083 }
4084 /* Parse the attribute list. */
4085 while (c_parser_next_token_is (parser, CPP_COMMA)
4086 || c_parser_next_token_is (parser, CPP_NAME)
4087 || c_parser_next_token_is (parser, CPP_KEYWORD))
4088 {
4089 tree attr, attr_name, attr_args;
4090 vec<tree, va_gc> *expr_list;
4091 if (c_parser_next_token_is (parser, CPP_COMMA))
4092 {
4093 c_parser_consume_token (parser);
4094 continue;
4095 }
4096
4097 attr_name = c_parser_attribute_any_word (parser);
4098 if (attr_name == NULL)
4099 break;
4100 if (is_cilkplus_vector_p (attr_name))
4101 {
4102 c_token *v_token = c_parser_peek_token (parser);
4103 c_parser_cilk_simd_fn_vector_attrs (parser, *v_token);
4104 /* If the next token isn't a comma, we're done. */
4105 if (!c_parser_next_token_is (parser, CPP_COMMA))
4106 break;
4107 continue;
4108 }
4109 c_parser_consume_token (parser);
4110 if (c_parser_next_token_is_not (parser, CPP_OPEN_PAREN))
4111 {
4112 attr = build_tree_list (attr_name, NULL_TREE);
4113 /* Add this attribute to the list. */
4114 attrs = chainon (attrs, attr);
4115 /* If the next token isn't a comma, we're done. */
4116 if (!c_parser_next_token_is (parser, CPP_COMMA))
4117 break;
4118 continue;
4119 }
4120 c_parser_consume_token (parser);
4121 /* Parse the attribute contents. If they start with an
4122 identifier which is followed by a comma or close
4123 parenthesis, then the arguments start with that
4124 identifier; otherwise they are an expression list.
4125 In objective-c the identifier may be a classname. */
4126 if (c_parser_next_token_is (parser, CPP_NAME)
4127 && (c_parser_peek_token (parser)->id_kind == C_ID_ID
4128 || (c_dialect_objc ()
4129 && c_parser_peek_token (parser)->id_kind
4130 == C_ID_CLASSNAME))
4131 && ((c_parser_peek_2nd_token (parser)->type == CPP_COMMA)
4132 || (c_parser_peek_2nd_token (parser)->type
4133 == CPP_CLOSE_PAREN))
4134 && (attribute_takes_identifier_p (attr_name)
4135 || (c_dialect_objc ()
4136 && c_parser_peek_token (parser)->id_kind
4137 == C_ID_CLASSNAME)))
4138 {
4139 tree arg1 = c_parser_peek_token (parser)->value;
4140 c_parser_consume_token (parser);
4141 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4142 attr_args = build_tree_list (NULL_TREE, arg1);
4143 else
4144 {
4145 tree tree_list;
4146 c_parser_consume_token (parser);
4147 expr_list = c_parser_expr_list (parser, false, true,
4148 NULL, NULL, NULL, NULL);
4149 tree_list = build_tree_list_vec (expr_list);
4150 attr_args = tree_cons (NULL_TREE, arg1, tree_list);
4151 release_tree_vector (expr_list);
4152 }
4153 }
4154 else
4155 {
4156 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4157 attr_args = NULL_TREE;
4158 else
4159 {
4160 expr_list = c_parser_expr_list (parser, false, true,
4161 NULL, NULL, NULL, NULL);
4162 attr_args = build_tree_list_vec (expr_list);
4163 release_tree_vector (expr_list);
4164 }
4165 }
4166 attr = build_tree_list (attr_name, attr_args);
4167 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4168 c_parser_consume_token (parser);
4169 else
4170 {
4171 parser->lex_untranslated_string = false;
4172 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4173 "expected %<)%>");
4174 return attrs;
4175 }
4176 /* Add this attribute to the list. */
4177 attrs = chainon (attrs, attr);
4178 /* If the next token isn't a comma, we're done. */
4179 if (!c_parser_next_token_is (parser, CPP_COMMA))
4180 break;
4181 }
4182 /* Look for the two `)' tokens. */
4183 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4184 c_parser_consume_token (parser);
4185 else
4186 {
4187 parser->lex_untranslated_string = false;
4188 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4189 "expected %<)%>");
4190 return attrs;
4191 }
4192 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4193 c_parser_consume_token (parser);
4194 else
4195 {
4196 parser->lex_untranslated_string = false;
4197 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4198 "expected %<)%>");
4199 return attrs;
4200 }
4201 parser->lex_untranslated_string = false;
4202 }
4203
4204 if (flag_cilkplus && !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
4205 c_finish_cilk_simd_fn_tokens (parser);
4206 return attrs;
4207 }
4208
4209 /* Parse a type name (C90 6.5.5, C99 6.7.6).
4210
4211 type-name:
4212 specifier-qualifier-list abstract-declarator[opt]
4213 */
4214
4215 static struct c_type_name *
4216 c_parser_type_name (c_parser *parser)
4217 {
4218 struct c_declspecs *specs = build_null_declspecs ();
4219 struct c_declarator *declarator;
4220 struct c_type_name *ret;
4221 bool dummy = false;
4222 c_parser_declspecs (parser, specs, false, true, true, false, false,
4223 cla_prefer_type);
4224 if (!specs->declspecs_seen_p)
4225 {
4226 c_parser_error (parser, "expected specifier-qualifier-list");
4227 return NULL;
4228 }
4229 if (specs->type != error_mark_node)
4230 {
4231 pending_xref_error ();
4232 finish_declspecs (specs);
4233 }
4234 declarator = c_parser_declarator (parser,
4235 specs->typespec_kind != ctsk_none,
4236 C_DTR_ABSTRACT, &dummy);
4237 if (declarator == NULL)
4238 return NULL;
4239 ret = XOBNEW (&parser_obstack, struct c_type_name);
4240 ret->specs = specs;
4241 ret->declarator = declarator;
4242 return ret;
4243 }
4244
4245 /* Parse an initializer (C90 6.5.7, C99 6.7.8).
4246
4247 initializer:
4248 assignment-expression
4249 { initializer-list }
4250 { initializer-list , }
4251
4252 initializer-list:
4253 designation[opt] initializer
4254 initializer-list , designation[opt] initializer
4255
4256 designation:
4257 designator-list =
4258
4259 designator-list:
4260 designator
4261 designator-list designator
4262
4263 designator:
4264 array-designator
4265 . identifier
4266
4267 array-designator:
4268 [ constant-expression ]
4269
4270 GNU extensions:
4271
4272 initializer:
4273 { }
4274
4275 designation:
4276 array-designator
4277 identifier :
4278
4279 array-designator:
4280 [ constant-expression ... constant-expression ]
4281
4282 Any expression without commas is accepted in the syntax for the
4283 constant-expressions, with non-constant expressions rejected later.
4284
4285 This function is only used for top-level initializers; for nested
4286 ones, see c_parser_initval. */
4287
4288 static struct c_expr
4289 c_parser_initializer (c_parser *parser)
4290 {
4291 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
4292 return c_parser_braced_init (parser, NULL_TREE, false);
4293 else
4294 {
4295 struct c_expr ret;
4296 location_t loc = c_parser_peek_token (parser)->location;
4297 ret = c_parser_expr_no_commas (parser, NULL);
4298 if (TREE_CODE (ret.value) != STRING_CST
4299 && TREE_CODE (ret.value) != COMPOUND_LITERAL_EXPR)
4300 ret = convert_lvalue_to_rvalue (loc, ret, true, true);
4301 return ret;
4302 }
4303 }
4304
4305 /* Parse a braced initializer list. TYPE is the type specified for a
4306 compound literal, and NULL_TREE for other initializers and for
4307 nested braced lists. NESTED_P is true for nested braced lists,
4308 false for the list of a compound literal or the list that is the
4309 top-level initializer in a declaration. */
4310
4311 static struct c_expr
4312 c_parser_braced_init (c_parser *parser, tree type, bool nested_p)
4313 {
4314 struct c_expr ret;
4315 struct obstack braced_init_obstack;
4316 location_t brace_loc = c_parser_peek_token (parser)->location;
4317 gcc_obstack_init (&braced_init_obstack);
4318 gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE));
4319 c_parser_consume_token (parser);
4320 if (nested_p)
4321 push_init_level (brace_loc, 0, &braced_init_obstack);
4322 else
4323 really_start_incremental_init (type);
4324 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
4325 {
4326 pedwarn (brace_loc, OPT_Wpedantic, "ISO C forbids empty initializer braces");
4327 }
4328 else
4329 {
4330 /* Parse a non-empty initializer list, possibly with a trailing
4331 comma. */
4332 while (true)
4333 {
4334 c_parser_initelt (parser, &braced_init_obstack);
4335 if (parser->error)
4336 break;
4337 if (c_parser_next_token_is (parser, CPP_COMMA))
4338 c_parser_consume_token (parser);
4339 else
4340 break;
4341 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
4342 break;
4343 }
4344 }
4345 c_token *next_tok = c_parser_peek_token (parser);
4346 if (next_tok->type != CPP_CLOSE_BRACE)
4347 {
4348 ret.value = error_mark_node;
4349 ret.original_code = ERROR_MARK;
4350 ret.original_type = NULL;
4351 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, "expected %<}%>");
4352 pop_init_level (brace_loc, 0, &braced_init_obstack);
4353 obstack_free (&braced_init_obstack, NULL);
4354 return ret;
4355 }
4356 location_t close_loc = next_tok->location;
4357 c_parser_consume_token (parser);
4358 ret = pop_init_level (brace_loc, 0, &braced_init_obstack);
4359 obstack_free (&braced_init_obstack, NULL);
4360 set_c_expr_source_range (&ret, brace_loc, close_loc);
4361 return ret;
4362 }
4363
4364 /* Parse a nested initializer, including designators. */
4365
4366 static void
4367 c_parser_initelt (c_parser *parser, struct obstack * braced_init_obstack)
4368 {
4369 /* Parse any designator or designator list. A single array
4370 designator may have the subsequent "=" omitted in GNU C, but a
4371 longer list or a structure member designator may not. */
4372 if (c_parser_next_token_is (parser, CPP_NAME)
4373 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
4374 {
4375 /* Old-style structure member designator. */
4376 set_init_label (c_parser_peek_token (parser)->location,
4377 c_parser_peek_token (parser)->value,
4378 braced_init_obstack);
4379 /* Use the colon as the error location. */
4380 pedwarn (c_parser_peek_2nd_token (parser)->location, OPT_Wpedantic,
4381 "obsolete use of designated initializer with %<:%>");
4382 c_parser_consume_token (parser);
4383 c_parser_consume_token (parser);
4384 }
4385 else
4386 {
4387 /* des_seen is 0 if there have been no designators, 1 if there
4388 has been a single array designator and 2 otherwise. */
4389 int des_seen = 0;
4390 /* Location of a designator. */
4391 location_t des_loc = UNKNOWN_LOCATION; /* Quiet warning. */
4392 while (c_parser_next_token_is (parser, CPP_OPEN_SQUARE)
4393 || c_parser_next_token_is (parser, CPP_DOT))
4394 {
4395 int des_prev = des_seen;
4396 if (!des_seen)
4397 des_loc = c_parser_peek_token (parser)->location;
4398 if (des_seen < 2)
4399 des_seen++;
4400 if (c_parser_next_token_is (parser, CPP_DOT))
4401 {
4402 des_seen = 2;
4403 c_parser_consume_token (parser);
4404 if (c_parser_next_token_is (parser, CPP_NAME))
4405 {
4406 set_init_label (des_loc, c_parser_peek_token (parser)->value,
4407 braced_init_obstack);
4408 c_parser_consume_token (parser);
4409 }
4410 else
4411 {
4412 struct c_expr init;
4413 init.value = error_mark_node;
4414 init.original_code = ERROR_MARK;
4415 init.original_type = NULL;
4416 c_parser_error (parser, "expected identifier");
4417 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
4418 process_init_element (input_location, init, false,
4419 braced_init_obstack);
4420 return;
4421 }
4422 }
4423 else
4424 {
4425 tree first, second;
4426 location_t ellipsis_loc = UNKNOWN_LOCATION; /* Quiet warning. */
4427 location_t array_index_loc = UNKNOWN_LOCATION;
4428 /* ??? Following the old parser, [ objc-receiver
4429 objc-message-args ] is accepted as an initializer,
4430 being distinguished from a designator by what follows
4431 the first assignment expression inside the square
4432 brackets, but after a first array designator a
4433 subsequent square bracket is for Objective-C taken to
4434 start an expression, using the obsolete form of
4435 designated initializer without '=', rather than
4436 possibly being a second level of designation: in LALR
4437 terms, the '[' is shifted rather than reducing
4438 designator to designator-list. */
4439 if (des_prev == 1 && c_dialect_objc ())
4440 {
4441 des_seen = des_prev;
4442 break;
4443 }
4444 if (des_prev == 0 && c_dialect_objc ())
4445 {
4446 /* This might be an array designator or an
4447 Objective-C message expression. If the former,
4448 continue parsing here; if the latter, parse the
4449 remainder of the initializer given the starting
4450 primary-expression. ??? It might make sense to
4451 distinguish when des_prev == 1 as well; see
4452 previous comment. */
4453 tree rec, args;
4454 struct c_expr mexpr;
4455 c_parser_consume_token (parser);
4456 if (c_parser_peek_token (parser)->type == CPP_NAME
4457 && ((c_parser_peek_token (parser)->id_kind
4458 == C_ID_TYPENAME)
4459 || (c_parser_peek_token (parser)->id_kind
4460 == C_ID_CLASSNAME)))
4461 {
4462 /* Type name receiver. */
4463 tree id = c_parser_peek_token (parser)->value;
4464 c_parser_consume_token (parser);
4465 rec = objc_get_class_reference (id);
4466 goto parse_message_args;
4467 }
4468 first = c_parser_expr_no_commas (parser, NULL).value;
4469 mark_exp_read (first);
4470 if (c_parser_next_token_is (parser, CPP_ELLIPSIS)
4471 || c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
4472 goto array_desig_after_first;
4473 /* Expression receiver. So far only one part
4474 without commas has been parsed; there might be
4475 more of the expression. */
4476 rec = first;
4477 while (c_parser_next_token_is (parser, CPP_COMMA))
4478 {
4479 struct c_expr next;
4480 location_t comma_loc, exp_loc;
4481 comma_loc = c_parser_peek_token (parser)->location;
4482 c_parser_consume_token (parser);
4483 exp_loc = c_parser_peek_token (parser)->location;
4484 next = c_parser_expr_no_commas (parser, NULL);
4485 next = convert_lvalue_to_rvalue (exp_loc, next,
4486 true, true);
4487 rec = build_compound_expr (comma_loc, rec, next.value);
4488 }
4489 parse_message_args:
4490 /* Now parse the objc-message-args. */
4491 args = c_parser_objc_message_args (parser);
4492 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
4493 "expected %<]%>");
4494 mexpr.value
4495 = objc_build_message_expr (rec, args);
4496 mexpr.original_code = ERROR_MARK;
4497 mexpr.original_type = NULL;
4498 /* Now parse and process the remainder of the
4499 initializer, starting with this message
4500 expression as a primary-expression. */
4501 c_parser_initval (parser, &mexpr, braced_init_obstack);
4502 return;
4503 }
4504 c_parser_consume_token (parser);
4505 array_index_loc = c_parser_peek_token (parser)->location;
4506 first = c_parser_expr_no_commas (parser, NULL).value;
4507 mark_exp_read (first);
4508 array_desig_after_first:
4509 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
4510 {
4511 ellipsis_loc = c_parser_peek_token (parser)->location;
4512 c_parser_consume_token (parser);
4513 second = c_parser_expr_no_commas (parser, NULL).value;
4514 mark_exp_read (second);
4515 }
4516 else
4517 second = NULL_TREE;
4518 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
4519 {
4520 c_parser_consume_token (parser);
4521 set_init_index (array_index_loc, first, second,
4522 braced_init_obstack);
4523 if (second)
4524 pedwarn (ellipsis_loc, OPT_Wpedantic,
4525 "ISO C forbids specifying range of elements to initialize");
4526 }
4527 else
4528 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
4529 "expected %<]%>");
4530 }
4531 }
4532 if (des_seen >= 1)
4533 {
4534 if (c_parser_next_token_is (parser, CPP_EQ))
4535 {
4536 pedwarn_c90 (des_loc, OPT_Wpedantic,
4537 "ISO C90 forbids specifying subobject "
4538 "to initialize");
4539 c_parser_consume_token (parser);
4540 }
4541 else
4542 {
4543 if (des_seen == 1)
4544 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
4545 "obsolete use of designated initializer without %<=%>");
4546 else
4547 {
4548 struct c_expr init;
4549 init.value = error_mark_node;
4550 init.original_code = ERROR_MARK;
4551 init.original_type = NULL;
4552 c_parser_error (parser, "expected %<=%>");
4553 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
4554 process_init_element (input_location, init, false,
4555 braced_init_obstack);
4556 return;
4557 }
4558 }
4559 }
4560 }
4561 c_parser_initval (parser, NULL, braced_init_obstack);
4562 }
4563
4564 /* Parse a nested initializer; as c_parser_initializer but parses
4565 initializers within braced lists, after any designators have been
4566 applied. If AFTER is not NULL then it is an Objective-C message
4567 expression which is the primary-expression starting the
4568 initializer. */
4569
4570 static void
4571 c_parser_initval (c_parser *parser, struct c_expr *after,
4572 struct obstack * braced_init_obstack)
4573 {
4574 struct c_expr init;
4575 gcc_assert (!after || c_dialect_objc ());
4576 location_t loc = c_parser_peek_token (parser)->location;
4577
4578 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE) && !after)
4579 init = c_parser_braced_init (parser, NULL_TREE, true);
4580 else
4581 {
4582 init = c_parser_expr_no_commas (parser, after);
4583 if (init.value != NULL_TREE
4584 && TREE_CODE (init.value) != STRING_CST
4585 && TREE_CODE (init.value) != COMPOUND_LITERAL_EXPR)
4586 init = convert_lvalue_to_rvalue (loc, init, true, true);
4587 }
4588 process_init_element (loc, init, false, braced_init_obstack);
4589 }
4590
4591 /* Parse a compound statement (possibly a function body) (C90 6.6.2,
4592 C99 6.8.2).
4593
4594 compound-statement:
4595 { block-item-list[opt] }
4596 { label-declarations block-item-list }
4597
4598 block-item-list:
4599 block-item
4600 block-item-list block-item
4601
4602 block-item:
4603 nested-declaration
4604 statement
4605
4606 nested-declaration:
4607 declaration
4608
4609 GNU extensions:
4610
4611 compound-statement:
4612 { label-declarations block-item-list }
4613
4614 nested-declaration:
4615 __extension__ nested-declaration
4616 nested-function-definition
4617
4618 label-declarations:
4619 label-declaration
4620 label-declarations label-declaration
4621
4622 label-declaration:
4623 __label__ identifier-list ;
4624
4625 Allowing the mixing of declarations and code is new in C99. The
4626 GNU syntax also permits (not shown above) labels at the end of
4627 compound statements, which yield an error. We don't allow labels
4628 on declarations; this might seem like a natural extension, but
4629 there would be a conflict between attributes on the label and
4630 prefix attributes on the declaration. ??? The syntax follows the
4631 old parser in requiring something after label declarations.
4632 Although they are erroneous if the labels declared aren't defined,
4633 is it useful for the syntax to be this way?
4634
4635 OpenACC:
4636
4637 block-item:
4638 openacc-directive
4639
4640 openacc-directive:
4641 update-directive
4642
4643 OpenMP:
4644
4645 block-item:
4646 openmp-directive
4647
4648 openmp-directive:
4649 barrier-directive
4650 flush-directive
4651 taskwait-directive
4652 taskyield-directive
4653 cancel-directive
4654 cancellation-point-directive */
4655
4656 static tree
4657 c_parser_compound_statement (c_parser *parser)
4658 {
4659 tree stmt;
4660 location_t brace_loc;
4661 brace_loc = c_parser_peek_token (parser)->location;
4662 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
4663 {
4664 /* Ensure a scope is entered and left anyway to avoid confusion
4665 if we have just prepared to enter a function body. */
4666 stmt = c_begin_compound_stmt (true);
4667 c_end_compound_stmt (brace_loc, stmt, true);
4668 return error_mark_node;
4669 }
4670 stmt = c_begin_compound_stmt (true);
4671 c_parser_compound_statement_nostart (parser);
4672
4673 /* If the compound stmt contains array notations, then we expand them. */
4674 if (flag_cilkplus && contains_array_notation_expr (stmt))
4675 stmt = expand_array_notation_exprs (stmt);
4676 return c_end_compound_stmt (brace_loc, stmt, true);
4677 }
4678
4679 /* Parse a compound statement except for the opening brace. This is
4680 used for parsing both compound statements and statement expressions
4681 (which follow different paths to handling the opening). */
4682
4683 static void
4684 c_parser_compound_statement_nostart (c_parser *parser)
4685 {
4686 bool last_stmt = false;
4687 bool last_label = false;
4688 bool save_valid_for_pragma = valid_location_for_stdc_pragma_p ();
4689 location_t label_loc = UNKNOWN_LOCATION; /* Quiet warning. */
4690 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
4691 {
4692 c_parser_consume_token (parser);
4693 return;
4694 }
4695 mark_valid_location_for_stdc_pragma (true);
4696 if (c_parser_next_token_is_keyword (parser, RID_LABEL))
4697 {
4698 /* Read zero or more forward-declarations for labels that nested
4699 functions can jump to. */
4700 mark_valid_location_for_stdc_pragma (false);
4701 while (c_parser_next_token_is_keyword (parser, RID_LABEL))
4702 {
4703 label_loc = c_parser_peek_token (parser)->location;
4704 c_parser_consume_token (parser);
4705 /* Any identifiers, including those declared as type names,
4706 are OK here. */
4707 while (true)
4708 {
4709 tree label;
4710 if (c_parser_next_token_is_not (parser, CPP_NAME))
4711 {
4712 c_parser_error (parser, "expected identifier");
4713 break;
4714 }
4715 label
4716 = declare_label (c_parser_peek_token (parser)->value);
4717 C_DECLARED_LABEL_FLAG (label) = 1;
4718 add_stmt (build_stmt (label_loc, DECL_EXPR, label));
4719 c_parser_consume_token (parser);
4720 if (c_parser_next_token_is (parser, CPP_COMMA))
4721 c_parser_consume_token (parser);
4722 else
4723 break;
4724 }
4725 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
4726 }
4727 pedwarn (label_loc, OPT_Wpedantic, "ISO C forbids label declarations");
4728 }
4729 /* We must now have at least one statement, label or declaration. */
4730 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
4731 {
4732 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
4733 c_parser_error (parser, "expected declaration or statement");
4734 c_parser_consume_token (parser);
4735 return;
4736 }
4737 while (c_parser_next_token_is_not (parser, CPP_CLOSE_BRACE))
4738 {
4739 location_t loc = c_parser_peek_token (parser)->location;
4740 if (c_parser_next_token_is_keyword (parser, RID_CASE)
4741 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
4742 || (c_parser_next_token_is (parser, CPP_NAME)
4743 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
4744 {
4745 if (c_parser_next_token_is_keyword (parser, RID_CASE))
4746 label_loc = c_parser_peek_2nd_token (parser)->location;
4747 else
4748 label_loc = c_parser_peek_token (parser)->location;
4749 last_label = true;
4750 last_stmt = false;
4751 mark_valid_location_for_stdc_pragma (false);
4752 c_parser_label (parser);
4753 }
4754 else if (!last_label
4755 && c_parser_next_tokens_start_declaration (parser))
4756 {
4757 last_label = false;
4758 mark_valid_location_for_stdc_pragma (false);
4759 c_parser_declaration_or_fndef (parser, true, true, true, true,
4760 true, NULL, vNULL);
4761 if (last_stmt)
4762 pedwarn_c90 (loc, OPT_Wdeclaration_after_statement,
4763 "ISO C90 forbids mixed declarations and code");
4764 last_stmt = false;
4765 }
4766 else if (!last_label
4767 && c_parser_next_token_is_keyword (parser, RID_EXTENSION))
4768 {
4769 /* __extension__ can start a declaration, but is also an
4770 unary operator that can start an expression. Consume all
4771 but the last of a possible series of __extension__ to
4772 determine which. */
4773 while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
4774 && (c_parser_peek_2nd_token (parser)->keyword
4775 == RID_EXTENSION))
4776 c_parser_consume_token (parser);
4777 if (c_token_starts_declaration (c_parser_peek_2nd_token (parser)))
4778 {
4779 int ext;
4780 ext = disable_extension_diagnostics ();
4781 c_parser_consume_token (parser);
4782 last_label = false;
4783 mark_valid_location_for_stdc_pragma (false);
4784 c_parser_declaration_or_fndef (parser, true, true, true, true,
4785 true, NULL, vNULL);
4786 /* Following the old parser, __extension__ does not
4787 disable this diagnostic. */
4788 restore_extension_diagnostics (ext);
4789 if (last_stmt)
4790 pedwarn_c90 (loc, OPT_Wdeclaration_after_statement,
4791 "ISO C90 forbids mixed declarations and code");
4792 last_stmt = false;
4793 }
4794 else
4795 goto statement;
4796 }
4797 else if (c_parser_next_token_is (parser, CPP_PRAGMA))
4798 {
4799 /* External pragmas, and some omp pragmas, are not associated
4800 with regular c code, and so are not to be considered statements
4801 syntactically. This ensures that the user doesn't put them
4802 places that would turn into syntax errors if the directive
4803 were ignored. */
4804 if (c_parser_pragma (parser,
4805 last_label ? pragma_stmt : pragma_compound))
4806 last_label = false, last_stmt = true;
4807 }
4808 else if (c_parser_next_token_is (parser, CPP_EOF))
4809 {
4810 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
4811 c_parser_error (parser, "expected declaration or statement");
4812 return;
4813 }
4814 else if (c_parser_next_token_is_keyword (parser, RID_ELSE))
4815 {
4816 if (parser->in_if_block)
4817 {
4818 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
4819 error_at (loc, """expected %<}%> before %<else%>");
4820 return;
4821 }
4822 else
4823 {
4824 error_at (loc, "%<else%> without a previous %<if%>");
4825 c_parser_consume_token (parser);
4826 continue;
4827 }
4828 }
4829 else
4830 {
4831 statement:
4832 last_label = false;
4833 last_stmt = true;
4834 mark_valid_location_for_stdc_pragma (false);
4835 c_parser_statement_after_labels (parser);
4836 }
4837
4838 parser->error = false;
4839 }
4840 if (last_label)
4841 error_at (label_loc, "label at end of compound statement");
4842 c_parser_consume_token (parser);
4843 /* Restore the value we started with. */
4844 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
4845 }
4846
4847 /* Parse all consecutive labels. */
4848
4849 static void
4850 c_parser_all_labels (c_parser *parser)
4851 {
4852 while (c_parser_next_token_is_keyword (parser, RID_CASE)
4853 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
4854 || (c_parser_next_token_is (parser, CPP_NAME)
4855 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
4856 c_parser_label (parser);
4857 }
4858
4859 /* Parse a label (C90 6.6.1, C99 6.8.1).
4860
4861 label:
4862 identifier : attributes[opt]
4863 case constant-expression :
4864 default :
4865
4866 GNU extensions:
4867
4868 label:
4869 case constant-expression ... constant-expression :
4870
4871 The use of attributes on labels is a GNU extension. The syntax in
4872 GNU C accepts any expressions without commas, non-constant
4873 expressions being rejected later. */
4874
4875 static void
4876 c_parser_label (c_parser *parser)
4877 {
4878 location_t loc1 = c_parser_peek_token (parser)->location;
4879 tree label = NULL_TREE;
4880 if (c_parser_next_token_is_keyword (parser, RID_CASE))
4881 {
4882 tree exp1, exp2;
4883 c_parser_consume_token (parser);
4884 exp1 = c_parser_expr_no_commas (parser, NULL).value;
4885 if (c_parser_next_token_is (parser, CPP_COLON))
4886 {
4887 c_parser_consume_token (parser);
4888 label = do_case (loc1, exp1, NULL_TREE);
4889 }
4890 else if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
4891 {
4892 c_parser_consume_token (parser);
4893 exp2 = c_parser_expr_no_commas (parser, NULL).value;
4894 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
4895 label = do_case (loc1, exp1, exp2);
4896 }
4897 else
4898 c_parser_error (parser, "expected %<:%> or %<...%>");
4899 }
4900 else if (c_parser_next_token_is_keyword (parser, RID_DEFAULT))
4901 {
4902 c_parser_consume_token (parser);
4903 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
4904 label = do_case (loc1, NULL_TREE, NULL_TREE);
4905 }
4906 else
4907 {
4908 tree name = c_parser_peek_token (parser)->value;
4909 tree tlab;
4910 tree attrs;
4911 location_t loc2 = c_parser_peek_token (parser)->location;
4912 gcc_assert (c_parser_next_token_is (parser, CPP_NAME));
4913 c_parser_consume_token (parser);
4914 gcc_assert (c_parser_next_token_is (parser, CPP_COLON));
4915 c_parser_consume_token (parser);
4916 attrs = c_parser_attributes (parser);
4917 tlab = define_label (loc2, name);
4918 if (tlab)
4919 {
4920 decl_attributes (&tlab, attrs, 0);
4921 label = add_stmt (build_stmt (loc1, LABEL_EXPR, tlab));
4922 }
4923 }
4924 if (label)
4925 {
4926 if (c_parser_next_tokens_start_declaration (parser))
4927 {
4928 error_at (c_parser_peek_token (parser)->location,
4929 "a label can only be part of a statement and "
4930 "a declaration is not a statement");
4931 c_parser_declaration_or_fndef (parser, /*fndef_ok*/ false,
4932 /*static_assert_ok*/ true,
4933 /*empty_ok*/ true, /*nested*/ true,
4934 /*start_attr_ok*/ true, NULL,
4935 vNULL);
4936 }
4937 }
4938 }
4939
4940 /* Parse a statement (C90 6.6, C99 6.8).
4941
4942 statement:
4943 labeled-statement
4944 compound-statement
4945 expression-statement
4946 selection-statement
4947 iteration-statement
4948 jump-statement
4949
4950 labeled-statement:
4951 label statement
4952
4953 expression-statement:
4954 expression[opt] ;
4955
4956 selection-statement:
4957 if-statement
4958 switch-statement
4959
4960 iteration-statement:
4961 while-statement
4962 do-statement
4963 for-statement
4964
4965 jump-statement:
4966 goto identifier ;
4967 continue ;
4968 break ;
4969 return expression[opt] ;
4970
4971 GNU extensions:
4972
4973 statement:
4974 asm-statement
4975
4976 jump-statement:
4977 goto * expression ;
4978
4979 Objective-C:
4980
4981 statement:
4982 objc-throw-statement
4983 objc-try-catch-statement
4984 objc-synchronized-statement
4985
4986 objc-throw-statement:
4987 @throw expression ;
4988 @throw ;
4989
4990 OpenACC:
4991
4992 statement:
4993 openacc-construct
4994
4995 openacc-construct:
4996 parallel-construct
4997 kernels-construct
4998 data-construct
4999 loop-construct
5000
5001 parallel-construct:
5002 parallel-directive structured-block
5003
5004 kernels-construct:
5005 kernels-directive structured-block
5006
5007 data-construct:
5008 data-directive structured-block
5009
5010 loop-construct:
5011 loop-directive structured-block
5012
5013 OpenMP:
5014
5015 statement:
5016 openmp-construct
5017
5018 openmp-construct:
5019 parallel-construct
5020 for-construct
5021 simd-construct
5022 for-simd-construct
5023 sections-construct
5024 single-construct
5025 parallel-for-construct
5026 parallel-for-simd-construct
5027 parallel-sections-construct
5028 master-construct
5029 critical-construct
5030 atomic-construct
5031 ordered-construct
5032
5033 parallel-construct:
5034 parallel-directive structured-block
5035
5036 for-construct:
5037 for-directive iteration-statement
5038
5039 simd-construct:
5040 simd-directive iteration-statements
5041
5042 for-simd-construct:
5043 for-simd-directive iteration-statements
5044
5045 sections-construct:
5046 sections-directive section-scope
5047
5048 single-construct:
5049 single-directive structured-block
5050
5051 parallel-for-construct:
5052 parallel-for-directive iteration-statement
5053
5054 parallel-for-simd-construct:
5055 parallel-for-simd-directive iteration-statement
5056
5057 parallel-sections-construct:
5058 parallel-sections-directive section-scope
5059
5060 master-construct:
5061 master-directive structured-block
5062
5063 critical-construct:
5064 critical-directive structured-block
5065
5066 atomic-construct:
5067 atomic-directive expression-statement
5068
5069 ordered-construct:
5070 ordered-directive structured-block
5071
5072 Transactional Memory:
5073
5074 statement:
5075 transaction-statement
5076 transaction-cancel-statement
5077 */
5078
5079 static void
5080 c_parser_statement (c_parser *parser)
5081 {
5082 c_parser_all_labels (parser);
5083 c_parser_statement_after_labels (parser);
5084 }
5085
5086 /* Parse a statement, other than a labeled statement. CHAIN is a vector
5087 of if-else-if conditions. */
5088
5089 static void
5090 c_parser_statement_after_labels (c_parser *parser, vec<tree> *chain)
5091 {
5092 location_t loc = c_parser_peek_token (parser)->location;
5093 tree stmt = NULL_TREE;
5094 bool in_if_block = parser->in_if_block;
5095 parser->in_if_block = false;
5096 switch (c_parser_peek_token (parser)->type)
5097 {
5098 case CPP_OPEN_BRACE:
5099 add_stmt (c_parser_compound_statement (parser));
5100 break;
5101 case CPP_KEYWORD:
5102 switch (c_parser_peek_token (parser)->keyword)
5103 {
5104 case RID_IF:
5105 c_parser_if_statement (parser, chain);
5106 break;
5107 case RID_SWITCH:
5108 c_parser_switch_statement (parser);
5109 break;
5110 case RID_WHILE:
5111 c_parser_while_statement (parser, false);
5112 break;
5113 case RID_DO:
5114 c_parser_do_statement (parser, false);
5115 break;
5116 case RID_FOR:
5117 c_parser_for_statement (parser, false);
5118 break;
5119 case RID_CILK_FOR:
5120 if (!flag_cilkplus)
5121 {
5122 error_at (c_parser_peek_token (parser)->location,
5123 "-fcilkplus must be enabled to use %<_Cilk_for%>");
5124 c_parser_skip_to_end_of_block_or_statement (parser);
5125 }
5126 else
5127 c_parser_cilk_for (parser, integer_zero_node);
5128 break;
5129 case RID_CILK_SYNC:
5130 c_parser_consume_token (parser);
5131 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
5132 if (!flag_cilkplus)
5133 error_at (loc, "-fcilkplus must be enabled to use %<_Cilk_sync%>");
5134 else
5135 add_stmt (build_cilk_sync ());
5136 break;
5137 case RID_GOTO:
5138 c_parser_consume_token (parser);
5139 if (c_parser_next_token_is (parser, CPP_NAME))
5140 {
5141 stmt = c_finish_goto_label (loc,
5142 c_parser_peek_token (parser)->value);
5143 c_parser_consume_token (parser);
5144 }
5145 else if (c_parser_next_token_is (parser, CPP_MULT))
5146 {
5147 struct c_expr val;
5148
5149 c_parser_consume_token (parser);
5150 val = c_parser_expression (parser);
5151 if (check_no_cilk (val.value,
5152 "Cilk array notation cannot be used as a computed goto expression",
5153 "%<_Cilk_spawn%> statement cannot be used as a computed goto expression",
5154 loc))
5155 val.value = error_mark_node;
5156 val = convert_lvalue_to_rvalue (loc, val, false, true);
5157 stmt = c_finish_goto_ptr (loc, val.value);
5158 }
5159 else
5160 c_parser_error (parser, "expected identifier or %<*%>");
5161 goto expect_semicolon;
5162 case RID_CONTINUE:
5163 c_parser_consume_token (parser);
5164 stmt = c_finish_bc_stmt (loc, &c_cont_label, false);
5165 goto expect_semicolon;
5166 case RID_BREAK:
5167 c_parser_consume_token (parser);
5168 stmt = c_finish_bc_stmt (loc, &c_break_label, true);
5169 goto expect_semicolon;
5170 case RID_RETURN:
5171 c_parser_consume_token (parser);
5172 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5173 {
5174 stmt = c_finish_return (loc, NULL_TREE, NULL_TREE);
5175 c_parser_consume_token (parser);
5176 }
5177 else
5178 {
5179 location_t xloc = c_parser_peek_token (parser)->location;
5180 struct c_expr expr = c_parser_expression_conv (parser);
5181 mark_exp_read (expr.value);
5182 stmt = c_finish_return (EXPR_LOC_OR_LOC (expr.value, xloc),
5183 expr.value, expr.original_type);
5184 goto expect_semicolon;
5185 }
5186 break;
5187 case RID_ASM:
5188 stmt = c_parser_asm_statement (parser);
5189 break;
5190 case RID_TRANSACTION_ATOMIC:
5191 case RID_TRANSACTION_RELAXED:
5192 stmt = c_parser_transaction (parser,
5193 c_parser_peek_token (parser)->keyword);
5194 break;
5195 case RID_TRANSACTION_CANCEL:
5196 stmt = c_parser_transaction_cancel (parser);
5197 goto expect_semicolon;
5198 case RID_AT_THROW:
5199 gcc_assert (c_dialect_objc ());
5200 c_parser_consume_token (parser);
5201 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5202 {
5203 stmt = objc_build_throw_stmt (loc, NULL_TREE);
5204 c_parser_consume_token (parser);
5205 }
5206 else
5207 {
5208 struct c_expr expr = c_parser_expression (parser);
5209 expr = convert_lvalue_to_rvalue (loc, expr, false, false);
5210 if (check_no_cilk (expr.value,
5211 "Cilk array notation cannot be used for a throw expression",
5212 "%<_Cilk_spawn%> statement cannot be used for a throw expression"))
5213 expr.value = error_mark_node;
5214 else
5215 {
5216 expr.value = c_fully_fold (expr.value, false, NULL);
5217 stmt = objc_build_throw_stmt (loc, expr.value);
5218 }
5219 goto expect_semicolon;
5220 }
5221 break;
5222 case RID_AT_TRY:
5223 gcc_assert (c_dialect_objc ());
5224 c_parser_objc_try_catch_finally_statement (parser);
5225 break;
5226 case RID_AT_SYNCHRONIZED:
5227 gcc_assert (c_dialect_objc ());
5228 c_parser_objc_synchronized_statement (parser);
5229 break;
5230 default:
5231 goto expr_stmt;
5232 }
5233 break;
5234 case CPP_SEMICOLON:
5235 c_parser_consume_token (parser);
5236 break;
5237 case CPP_CLOSE_PAREN:
5238 case CPP_CLOSE_SQUARE:
5239 /* Avoid infinite loop in error recovery:
5240 c_parser_skip_until_found stops at a closing nesting
5241 delimiter without consuming it, but here we need to consume
5242 it to proceed further. */
5243 c_parser_error (parser, "expected statement");
5244 c_parser_consume_token (parser);
5245 break;
5246 case CPP_PRAGMA:
5247 c_parser_pragma (parser, pragma_stmt);
5248 break;
5249 default:
5250 expr_stmt:
5251 stmt = c_finish_expr_stmt (loc, c_parser_expression_conv (parser).value);
5252 expect_semicolon:
5253 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
5254 break;
5255 }
5256 /* Two cases cannot and do not have line numbers associated: If stmt
5257 is degenerate, such as "2;", then stmt is an INTEGER_CST, which
5258 cannot hold line numbers. But that's OK because the statement
5259 will either be changed to a MODIFY_EXPR during gimplification of
5260 the statement expr, or discarded. If stmt was compound, but
5261 without new variables, we will have skipped the creation of a
5262 BIND and will have a bare STATEMENT_LIST. But that's OK because
5263 (recursively) all of the component statements should already have
5264 line numbers assigned. ??? Can we discard no-op statements
5265 earlier? */
5266 if (EXPR_LOCATION (stmt) == UNKNOWN_LOCATION)
5267 protected_set_expr_location (stmt, loc);
5268
5269 parser->in_if_block = in_if_block;
5270 }
5271
5272 /* Parse the condition from an if, do, while or for statements. */
5273
5274 static tree
5275 c_parser_condition (c_parser *parser)
5276 {
5277 location_t loc = c_parser_peek_token (parser)->location;
5278 tree cond;
5279 cond = c_parser_expression_conv (parser).value;
5280 cond = c_objc_common_truthvalue_conversion (loc, cond);
5281 cond = c_fully_fold (cond, false, NULL);
5282 if (warn_sequence_point)
5283 verify_sequence_points (cond);
5284 return cond;
5285 }
5286
5287 /* Parse a parenthesized condition from an if, do or while statement.
5288
5289 condition:
5290 ( expression )
5291 */
5292 static tree
5293 c_parser_paren_condition (c_parser *parser)
5294 {
5295 tree cond;
5296 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5297 return error_mark_node;
5298 cond = c_parser_condition (parser);
5299 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
5300 return cond;
5301 }
5302
5303 /* Parse a statement which is a block in C99. */
5304
5305 static tree
5306 c_parser_c99_block_statement (c_parser *parser)
5307 {
5308 tree block = c_begin_compound_stmt (flag_isoc99);
5309 location_t loc = c_parser_peek_token (parser)->location;
5310 c_parser_statement (parser);
5311 return c_end_compound_stmt (loc, block, flag_isoc99);
5312 }
5313
5314 /* Parse the body of an if statement. This is just parsing a
5315 statement but (a) it is a block in C99, (b) we track whether the
5316 body is an if statement for the sake of -Wparentheses warnings, (c)
5317 we handle an empty body specially for the sake of -Wempty-body
5318 warnings, and (d) we call parser_compound_statement directly
5319 because c_parser_statement_after_labels resets
5320 parser->in_if_block. */
5321
5322 static tree
5323 c_parser_if_body (c_parser *parser, bool *if_p,
5324 const token_indent_info &if_tinfo)
5325 {
5326 tree block = c_begin_compound_stmt (flag_isoc99);
5327 location_t body_loc = c_parser_peek_token (parser)->location;
5328 token_indent_info body_tinfo
5329 = get_token_indent_info (c_parser_peek_token (parser));
5330
5331 c_parser_all_labels (parser);
5332 *if_p = c_parser_next_token_is_keyword (parser, RID_IF);
5333 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5334 {
5335 location_t loc = c_parser_peek_token (parser)->location;
5336 add_stmt (build_empty_stmt (loc));
5337 c_parser_consume_token (parser);
5338 if (!c_parser_next_token_is_keyword (parser, RID_ELSE))
5339 warning_at (loc, OPT_Wempty_body,
5340 "suggest braces around empty body in an %<if%> statement");
5341 }
5342 else if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
5343 add_stmt (c_parser_compound_statement (parser));
5344 else
5345 c_parser_statement_after_labels (parser);
5346
5347 token_indent_info next_tinfo
5348 = get_token_indent_info (c_parser_peek_token (parser));
5349 warn_for_misleading_indentation (if_tinfo, body_tinfo, next_tinfo);
5350
5351 return c_end_compound_stmt (body_loc, block, flag_isoc99);
5352 }
5353
5354 /* Parse the else body of an if statement. This is just parsing a
5355 statement but (a) it is a block in C99, (b) we handle an empty body
5356 specially for the sake of -Wempty-body warnings. CHAIN is a vector
5357 of if-else-if conditions. */
5358
5359 static tree
5360 c_parser_else_body (c_parser *parser, const token_indent_info &else_tinfo,
5361 vec<tree> *chain)
5362 {
5363 location_t body_loc = c_parser_peek_token (parser)->location;
5364 tree block = c_begin_compound_stmt (flag_isoc99);
5365 token_indent_info body_tinfo
5366 = get_token_indent_info (c_parser_peek_token (parser));
5367
5368 c_parser_all_labels (parser);
5369 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5370 {
5371 location_t loc = c_parser_peek_token (parser)->location;
5372 warning_at (loc,
5373 OPT_Wempty_body,
5374 "suggest braces around empty body in an %<else%> statement");
5375 add_stmt (build_empty_stmt (loc));
5376 c_parser_consume_token (parser);
5377 }
5378 else
5379 c_parser_statement_after_labels (parser, chain);
5380
5381 token_indent_info next_tinfo
5382 = get_token_indent_info (c_parser_peek_token (parser));
5383 warn_for_misleading_indentation (else_tinfo, body_tinfo, next_tinfo);
5384
5385 return c_end_compound_stmt (body_loc, block, flag_isoc99);
5386 }
5387
5388 /* Parse an if statement (C90 6.6.4, C99 6.8.4).
5389
5390 if-statement:
5391 if ( expression ) statement
5392 if ( expression ) statement else statement
5393
5394 CHAIN is a vector of if-else-if conditions. */
5395
5396 static void
5397 c_parser_if_statement (c_parser *parser, vec<tree> *chain)
5398 {
5399 tree block;
5400 location_t loc;
5401 tree cond;
5402 bool first_if = false;
5403 tree first_body, second_body;
5404 bool in_if_block;
5405 tree if_stmt;
5406
5407 gcc_assert (c_parser_next_token_is_keyword (parser, RID_IF));
5408 token_indent_info if_tinfo
5409 = get_token_indent_info (c_parser_peek_token (parser));
5410 c_parser_consume_token (parser);
5411 block = c_begin_compound_stmt (flag_isoc99);
5412 loc = c_parser_peek_token (parser)->location;
5413 cond = c_parser_paren_condition (parser);
5414 if (flag_cilkplus && contains_cilk_spawn_stmt (cond))
5415 {
5416 error_at (loc, "if statement cannot contain %<Cilk_spawn%>");
5417 cond = error_mark_node;
5418 }
5419 in_if_block = parser->in_if_block;
5420 parser->in_if_block = true;
5421 first_body = c_parser_if_body (parser, &first_if, if_tinfo);
5422 parser->in_if_block = in_if_block;
5423
5424 if (warn_duplicated_cond)
5425 warn_duplicated_cond_add_or_warn (EXPR_LOCATION (cond), cond, &chain);
5426
5427 if (c_parser_next_token_is_keyword (parser, RID_ELSE))
5428 {
5429 token_indent_info else_tinfo
5430 = get_token_indent_info (c_parser_peek_token (parser));
5431 c_parser_consume_token (parser);
5432 if (warn_duplicated_cond)
5433 {
5434 if (c_parser_next_token_is_keyword (parser, RID_IF)
5435 && chain == NULL)
5436 {
5437 /* We've got "if (COND) else if (COND2)". Start the
5438 condition chain and add COND as the first element. */
5439 chain = new vec<tree> ();
5440 if (!CONSTANT_CLASS_P (cond) && !TREE_SIDE_EFFECTS (cond))
5441 chain->safe_push (cond);
5442 }
5443 else if (!c_parser_next_token_is_keyword (parser, RID_IF))
5444 {
5445 /* This is if-else without subsequent if. Zap the condition
5446 chain; we would have already warned at this point. */
5447 delete chain;
5448 chain = NULL;
5449 }
5450 }
5451 second_body = c_parser_else_body (parser, else_tinfo, chain);
5452 }
5453 else
5454 {
5455 second_body = NULL_TREE;
5456 if (warn_duplicated_cond)
5457 {
5458 /* This if statement does not have an else clause. We don't
5459 need the condition chain anymore. */
5460 delete chain;
5461 chain = NULL;
5462 }
5463 }
5464 c_finish_if_stmt (loc, cond, first_body, second_body, first_if);
5465 if_stmt = c_end_compound_stmt (loc, block, flag_isoc99);
5466
5467 /* If the if statement contains array notations, then we expand them. */
5468 if (flag_cilkplus && contains_array_notation_expr (if_stmt))
5469 if_stmt = fix_conditional_array_notations (if_stmt);
5470 add_stmt (if_stmt);
5471 }
5472
5473 /* Parse a switch statement (C90 6.6.4, C99 6.8.4).
5474
5475 switch-statement:
5476 switch (expression) statement
5477 */
5478
5479 static void
5480 c_parser_switch_statement (c_parser *parser)
5481 {
5482 struct c_expr ce;
5483 tree block, expr, body, save_break;
5484 location_t switch_loc = c_parser_peek_token (parser)->location;
5485 location_t switch_cond_loc;
5486 gcc_assert (c_parser_next_token_is_keyword (parser, RID_SWITCH));
5487 c_parser_consume_token (parser);
5488 block = c_begin_compound_stmt (flag_isoc99);
5489 bool explicit_cast_p = false;
5490 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5491 {
5492 switch_cond_loc = c_parser_peek_token (parser)->location;
5493 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
5494 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
5495 explicit_cast_p = true;
5496 ce = c_parser_expression (parser);
5497 ce = convert_lvalue_to_rvalue (switch_cond_loc, ce, true, false);
5498 expr = ce.value;
5499 /* ??? expr has no valid location? */
5500 if (check_no_cilk (expr,
5501 "Cilk array notation cannot be used as a condition for switch statement",
5502 "%<_Cilk_spawn%> statement cannot be used as a condition for switch statement",
5503 switch_cond_loc))
5504 expr = error_mark_node;
5505 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
5506 }
5507 else
5508 {
5509 switch_cond_loc = UNKNOWN_LOCATION;
5510 expr = error_mark_node;
5511 }
5512 c_start_case (switch_loc, switch_cond_loc, expr, explicit_cast_p);
5513 save_break = c_break_label;
5514 c_break_label = NULL_TREE;
5515 body = c_parser_c99_block_statement (parser);
5516 c_finish_case (body, ce.original_type);
5517 if (c_break_label)
5518 {
5519 location_t here = c_parser_peek_token (parser)->location;
5520 tree t = build1 (LABEL_EXPR, void_type_node, c_break_label);
5521 SET_EXPR_LOCATION (t, here);
5522 add_stmt (t);
5523 }
5524 c_break_label = save_break;
5525 add_stmt (c_end_compound_stmt (switch_loc, block, flag_isoc99));
5526 }
5527
5528 /* Parse a while statement (C90 6.6.5, C99 6.8.5).
5529
5530 while-statement:
5531 while (expression) statement
5532 */
5533
5534 static void
5535 c_parser_while_statement (c_parser *parser, bool ivdep)
5536 {
5537 tree block, cond, body, save_break, save_cont;
5538 location_t loc;
5539 gcc_assert (c_parser_next_token_is_keyword (parser, RID_WHILE));
5540 token_indent_info while_tinfo
5541 = get_token_indent_info (c_parser_peek_token (parser));
5542 c_parser_consume_token (parser);
5543 block = c_begin_compound_stmt (flag_isoc99);
5544 loc = c_parser_peek_token (parser)->location;
5545 cond = c_parser_paren_condition (parser);
5546 if (check_no_cilk (cond,
5547 "Cilk array notation cannot be used as a condition for while statement",
5548 "%<_Cilk_spawn%> statement cannot be used as a condition for while statement"))
5549 cond = error_mark_node;
5550 if (ivdep && cond != error_mark_node)
5551 cond = build2 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
5552 build_int_cst (integer_type_node,
5553 annot_expr_ivdep_kind));
5554 save_break = c_break_label;
5555 c_break_label = NULL_TREE;
5556 save_cont = c_cont_label;
5557 c_cont_label = NULL_TREE;
5558
5559 token_indent_info body_tinfo
5560 = get_token_indent_info (c_parser_peek_token (parser));
5561
5562 body = c_parser_c99_block_statement (parser);
5563 c_finish_loop (loc, cond, NULL, body, c_break_label, c_cont_label, true);
5564 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
5565
5566 token_indent_info next_tinfo
5567 = get_token_indent_info (c_parser_peek_token (parser));
5568 warn_for_misleading_indentation (while_tinfo, body_tinfo, next_tinfo);
5569
5570 c_break_label = save_break;
5571 c_cont_label = save_cont;
5572 }
5573
5574 /* Parse a do statement (C90 6.6.5, C99 6.8.5).
5575
5576 do-statement:
5577 do statement while ( expression ) ;
5578 */
5579
5580 static void
5581 c_parser_do_statement (c_parser *parser, bool ivdep)
5582 {
5583 tree block, cond, body, save_break, save_cont, new_break, new_cont;
5584 location_t loc;
5585 gcc_assert (c_parser_next_token_is_keyword (parser, RID_DO));
5586 c_parser_consume_token (parser);
5587 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5588 warning_at (c_parser_peek_token (parser)->location,
5589 OPT_Wempty_body,
5590 "suggest braces around empty body in %<do%> statement");
5591 block = c_begin_compound_stmt (flag_isoc99);
5592 loc = c_parser_peek_token (parser)->location;
5593 save_break = c_break_label;
5594 c_break_label = NULL_TREE;
5595 save_cont = c_cont_label;
5596 c_cont_label = NULL_TREE;
5597 body = c_parser_c99_block_statement (parser);
5598 c_parser_require_keyword (parser, RID_WHILE, "expected %<while%>");
5599 new_break = c_break_label;
5600 c_break_label = save_break;
5601 new_cont = c_cont_label;
5602 c_cont_label = save_cont;
5603 cond = c_parser_paren_condition (parser);
5604 if (check_no_cilk (cond,
5605 "Cilk array notation cannot be used as a condition for a do-while statement",
5606 "%<_Cilk_spawn%> statement cannot be used as a condition for a do-while statement"))
5607 cond = error_mark_node;
5608 if (ivdep && cond != error_mark_node)
5609 cond = build2 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
5610 build_int_cst (integer_type_node,
5611 annot_expr_ivdep_kind));
5612 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
5613 c_parser_skip_to_end_of_block_or_statement (parser);
5614 c_finish_loop (loc, cond, NULL, body, new_break, new_cont, false);
5615 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
5616 }
5617
5618 /* Parse a for statement (C90 6.6.5, C99 6.8.5).
5619
5620 for-statement:
5621 for ( expression[opt] ; expression[opt] ; expression[opt] ) statement
5622 for ( nested-declaration expression[opt] ; expression[opt] ) statement
5623
5624 The form with a declaration is new in C99.
5625
5626 ??? In accordance with the old parser, the declaration may be a
5627 nested function, which is then rejected in check_for_loop_decls,
5628 but does it make any sense for this to be included in the grammar?
5629 Note in particular that the nested function does not include a
5630 trailing ';', whereas the "declaration" production includes one.
5631 Also, can we reject bad declarations earlier and cheaper than
5632 check_for_loop_decls?
5633
5634 In Objective-C, there are two additional variants:
5635
5636 foreach-statement:
5637 for ( expression in expresssion ) statement
5638 for ( declaration in expression ) statement
5639
5640 This is inconsistent with C, because the second variant is allowed
5641 even if c99 is not enabled.
5642
5643 The rest of the comment documents these Objective-C foreach-statement.
5644
5645 Here is the canonical example of the first variant:
5646 for (object in array) { do something with object }
5647 we call the first expression ("object") the "object_expression" and
5648 the second expression ("array") the "collection_expression".
5649 object_expression must be an lvalue of type "id" (a generic Objective-C
5650 object) because the loop works by assigning to object_expression the
5651 various objects from the collection_expression. collection_expression
5652 must evaluate to something of type "id" which responds to the method
5653 countByEnumeratingWithState:objects:count:.
5654
5655 The canonical example of the second variant is:
5656 for (id object in array) { do something with object }
5657 which is completely equivalent to
5658 {
5659 id object;
5660 for (object in array) { do something with object }
5661 }
5662 Note that initizializing 'object' in some way (eg, "for ((object =
5663 xxx) in array) { do something with object }") is possibly
5664 technically valid, but completely pointless as 'object' will be
5665 assigned to something else as soon as the loop starts. We should
5666 most likely reject it (TODO).
5667
5668 The beginning of the Objective-C foreach-statement looks exactly
5669 like the beginning of the for-statement, and we can tell it is a
5670 foreach-statement only because the initial declaration or
5671 expression is terminated by 'in' instead of ';'.
5672 */
5673
5674 static void
5675 c_parser_for_statement (c_parser *parser, bool ivdep)
5676 {
5677 tree block, cond, incr, save_break, save_cont, body;
5678 /* The following are only used when parsing an ObjC foreach statement. */
5679 tree object_expression;
5680 /* Silence the bogus uninitialized warning. */
5681 tree collection_expression = NULL;
5682 location_t loc = c_parser_peek_token (parser)->location;
5683 location_t for_loc = c_parser_peek_token (parser)->location;
5684 bool is_foreach_statement = false;
5685 gcc_assert (c_parser_next_token_is_keyword (parser, RID_FOR));
5686 token_indent_info for_tinfo
5687 = get_token_indent_info (c_parser_peek_token (parser));
5688 c_parser_consume_token (parser);
5689 /* Open a compound statement in Objective-C as well, just in case this is
5690 as foreach expression. */
5691 block = c_begin_compound_stmt (flag_isoc99 || c_dialect_objc ());
5692 cond = error_mark_node;
5693 incr = error_mark_node;
5694 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5695 {
5696 /* Parse the initialization declaration or expression. */
5697 object_expression = error_mark_node;
5698 parser->objc_could_be_foreach_context = c_dialect_objc ();
5699 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5700 {
5701 parser->objc_could_be_foreach_context = false;
5702 c_parser_consume_token (parser);
5703 c_finish_expr_stmt (loc, NULL_TREE);
5704 }
5705 else if (c_parser_next_tokens_start_declaration (parser))
5706 {
5707 c_parser_declaration_or_fndef (parser, true, true, true, true, true,
5708 &object_expression, vNULL);
5709 parser->objc_could_be_foreach_context = false;
5710
5711 if (c_parser_next_token_is_keyword (parser, RID_IN))
5712 {
5713 c_parser_consume_token (parser);
5714 is_foreach_statement = true;
5715 if (check_for_loop_decls (for_loc, true) == NULL_TREE)
5716 c_parser_error (parser, "multiple iterating variables in fast enumeration");
5717 }
5718 else
5719 check_for_loop_decls (for_loc, flag_isoc99);
5720 }
5721 else if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
5722 {
5723 /* __extension__ can start a declaration, but is also an
5724 unary operator that can start an expression. Consume all
5725 but the last of a possible series of __extension__ to
5726 determine which. */
5727 while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
5728 && (c_parser_peek_2nd_token (parser)->keyword
5729 == RID_EXTENSION))
5730 c_parser_consume_token (parser);
5731 if (c_token_starts_declaration (c_parser_peek_2nd_token (parser)))
5732 {
5733 int ext;
5734 ext = disable_extension_diagnostics ();
5735 c_parser_consume_token (parser);
5736 c_parser_declaration_or_fndef (parser, true, true, true, true,
5737 true, &object_expression, vNULL);
5738 parser->objc_could_be_foreach_context = false;
5739
5740 restore_extension_diagnostics (ext);
5741 if (c_parser_next_token_is_keyword (parser, RID_IN))
5742 {
5743 c_parser_consume_token (parser);
5744 is_foreach_statement = true;
5745 if (check_for_loop_decls (for_loc, true) == NULL_TREE)
5746 c_parser_error (parser, "multiple iterating variables in fast enumeration");
5747 }
5748 else
5749 check_for_loop_decls (for_loc, flag_isoc99);
5750 }
5751 else
5752 goto init_expr;
5753 }
5754 else
5755 {
5756 init_expr:
5757 {
5758 struct c_expr ce;
5759 tree init_expression;
5760 ce = c_parser_expression (parser);
5761 /* In theory we could forbid _Cilk_spawn here, as the spec says "only in top
5762 level statement", but it works just fine, so allow it. */
5763 init_expression = ce.value;
5764 parser->objc_could_be_foreach_context = false;
5765 if (c_parser_next_token_is_keyword (parser, RID_IN))
5766 {
5767 c_parser_consume_token (parser);
5768 is_foreach_statement = true;
5769 if (! lvalue_p (init_expression))
5770 c_parser_error (parser, "invalid iterating variable in fast enumeration");
5771 object_expression = c_fully_fold (init_expression, false, NULL);
5772 }
5773 else
5774 {
5775 ce = convert_lvalue_to_rvalue (loc, ce, true, false);
5776 init_expression = ce.value;
5777 c_finish_expr_stmt (loc, init_expression);
5778 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
5779 }
5780 }
5781 }
5782 /* Parse the loop condition. In the case of a foreach
5783 statement, there is no loop condition. */
5784 gcc_assert (!parser->objc_could_be_foreach_context);
5785 if (!is_foreach_statement)
5786 {
5787 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5788 {
5789 if (ivdep)
5790 {
5791 c_parser_error (parser, "missing loop condition in loop with "
5792 "%<GCC ivdep%> pragma");
5793 cond = error_mark_node;
5794 }
5795 else
5796 {
5797 c_parser_consume_token (parser);
5798 cond = NULL_TREE;
5799 }
5800 }
5801 else
5802 {
5803 cond = c_parser_condition (parser);
5804 if (check_no_cilk (cond,
5805 "Cilk array notation cannot be used in a condition for a for-loop",
5806 "%<_Cilk_spawn%> statement cannot be used in a condition for a for-loop"))
5807 cond = error_mark_node;
5808 c_parser_skip_until_found (parser, CPP_SEMICOLON,
5809 "expected %<;%>");
5810 }
5811 if (ivdep && cond != error_mark_node)
5812 cond = build2 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
5813 build_int_cst (integer_type_node,
5814 annot_expr_ivdep_kind));
5815 }
5816 /* Parse the increment expression (the third expression in a
5817 for-statement). In the case of a foreach-statement, this is
5818 the expression that follows the 'in'. */
5819 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
5820 {
5821 if (is_foreach_statement)
5822 {
5823 c_parser_error (parser, "missing collection in fast enumeration");
5824 collection_expression = error_mark_node;
5825 }
5826 else
5827 incr = c_process_expr_stmt (loc, NULL_TREE);
5828 }
5829 else
5830 {
5831 if (is_foreach_statement)
5832 collection_expression = c_fully_fold (c_parser_expression (parser).value,
5833 false, NULL);
5834 else
5835 {
5836 struct c_expr ce = c_parser_expression (parser);
5837 ce = convert_lvalue_to_rvalue (loc, ce, true, false);
5838 incr = c_process_expr_stmt (loc, ce.value);
5839 }
5840 }
5841 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
5842 }
5843 save_break = c_break_label;
5844 c_break_label = NULL_TREE;
5845 save_cont = c_cont_label;
5846 c_cont_label = NULL_TREE;
5847
5848 token_indent_info body_tinfo
5849 = get_token_indent_info (c_parser_peek_token (parser));
5850
5851 body = c_parser_c99_block_statement (parser);
5852
5853 if (is_foreach_statement)
5854 objc_finish_foreach_loop (loc, object_expression, collection_expression, body, c_break_label, c_cont_label);
5855 else
5856 c_finish_loop (loc, cond, incr, body, c_break_label, c_cont_label, true);
5857 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99 || c_dialect_objc ()));
5858
5859 /* We might need to reclassify any previously-lexed identifier, e.g.
5860 when we've left a for loop with an if-statement without else in the
5861 body - we might have used a wrong scope for the token. See PR67784. */
5862 if (c_parser_next_token_is (parser, CPP_NAME))
5863 {
5864 c_token *token = c_parser_peek_token (parser);
5865 tree decl = lookup_name (token->value);
5866 if (decl == NULL_TREE || VAR_P (decl))
5867 /* If DECL is null, we don't know what this token might be. Treat
5868 it as an ID for better diagnostics; we'll error later on. */
5869 token->id_kind = C_ID_ID;
5870 else if (TREE_CODE (decl) == TYPE_DECL)
5871 token->id_kind = C_ID_TYPENAME;
5872 }
5873
5874 token_indent_info next_tinfo
5875 = get_token_indent_info (c_parser_peek_token (parser));
5876 warn_for_misleading_indentation (for_tinfo, body_tinfo, next_tinfo);
5877
5878 c_break_label = save_break;
5879 c_cont_label = save_cont;
5880 }
5881
5882 /* Parse an asm statement, a GNU extension. This is a full-blown asm
5883 statement with inputs, outputs, clobbers, and volatile tag
5884 allowed.
5885
5886 asm-statement:
5887 asm type-qualifier[opt] ( asm-argument ) ;
5888 asm type-qualifier[opt] goto ( asm-goto-argument ) ;
5889
5890 asm-argument:
5891 asm-string-literal
5892 asm-string-literal : asm-operands[opt]
5893 asm-string-literal : asm-operands[opt] : asm-operands[opt]
5894 asm-string-literal : asm-operands[opt] : asm-operands[opt] : asm-clobbers[opt]
5895
5896 asm-goto-argument:
5897 asm-string-literal : : asm-operands[opt] : asm-clobbers[opt] \
5898 : asm-goto-operands
5899
5900 Qualifiers other than volatile are accepted in the syntax but
5901 warned for. */
5902
5903 static tree
5904 c_parser_asm_statement (c_parser *parser)
5905 {
5906 tree quals, str, outputs, inputs, clobbers, labels, ret;
5907 bool simple, is_goto;
5908 location_t asm_loc = c_parser_peek_token (parser)->location;
5909 int section, nsections;
5910
5911 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM));
5912 c_parser_consume_token (parser);
5913 if (c_parser_next_token_is_keyword (parser, RID_VOLATILE))
5914 {
5915 quals = c_parser_peek_token (parser)->value;
5916 c_parser_consume_token (parser);
5917 }
5918 else if (c_parser_next_token_is_keyword (parser, RID_CONST)
5919 || c_parser_next_token_is_keyword (parser, RID_RESTRICT))
5920 {
5921 warning_at (c_parser_peek_token (parser)->location,
5922 0,
5923 "%E qualifier ignored on asm",
5924 c_parser_peek_token (parser)->value);
5925 quals = NULL_TREE;
5926 c_parser_consume_token (parser);
5927 }
5928 else
5929 quals = NULL_TREE;
5930
5931 is_goto = false;
5932 if (c_parser_next_token_is_keyword (parser, RID_GOTO))
5933 {
5934 c_parser_consume_token (parser);
5935 is_goto = true;
5936 }
5937
5938 /* ??? Follow the C++ parser rather than using the
5939 lex_untranslated_string kludge. */
5940 parser->lex_untranslated_string = true;
5941 ret = NULL;
5942
5943 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5944 goto error;
5945
5946 str = c_parser_asm_string_literal (parser);
5947 if (str == NULL_TREE)
5948 goto error_close_paren;
5949
5950 simple = true;
5951 outputs = NULL_TREE;
5952 inputs = NULL_TREE;
5953 clobbers = NULL_TREE;
5954 labels = NULL_TREE;
5955
5956 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN) && !is_goto)
5957 goto done_asm;
5958
5959 /* Parse each colon-delimited section of operands. */
5960 nsections = 3 + is_goto;
5961 for (section = 0; section < nsections; ++section)
5962 {
5963 if (!c_parser_require (parser, CPP_COLON,
5964 is_goto
5965 ? "expected %<:%>"
5966 : "expected %<:%> or %<)%>"))
5967 goto error_close_paren;
5968
5969 /* Once past any colon, we're no longer a simple asm. */
5970 simple = false;
5971
5972 if ((!c_parser_next_token_is (parser, CPP_COLON)
5973 && !c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
5974 || section == 3)
5975 switch (section)
5976 {
5977 case 0:
5978 /* For asm goto, we don't allow output operands, but reserve
5979 the slot for a future extension that does allow them. */
5980 if (!is_goto)
5981 outputs = c_parser_asm_operands (parser);
5982 break;
5983 case 1:
5984 inputs = c_parser_asm_operands (parser);
5985 break;
5986 case 2:
5987 clobbers = c_parser_asm_clobbers (parser);
5988 break;
5989 case 3:
5990 labels = c_parser_asm_goto_operands (parser);
5991 break;
5992 default:
5993 gcc_unreachable ();
5994 }
5995
5996 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN) && !is_goto)
5997 goto done_asm;
5998 }
5999
6000 done_asm:
6001 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
6002 {
6003 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6004 goto error;
6005 }
6006
6007 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
6008 c_parser_skip_to_end_of_block_or_statement (parser);
6009
6010 ret = build_asm_stmt (quals, build_asm_expr (asm_loc, str, outputs, inputs,
6011 clobbers, labels, simple));
6012
6013 error:
6014 parser->lex_untranslated_string = false;
6015 return ret;
6016
6017 error_close_paren:
6018 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6019 goto error;
6020 }
6021
6022 /* Parse asm operands, a GNU extension.
6023
6024 asm-operands:
6025 asm-operand
6026 asm-operands , asm-operand
6027
6028 asm-operand:
6029 asm-string-literal ( expression )
6030 [ identifier ] asm-string-literal ( expression )
6031 */
6032
6033 static tree
6034 c_parser_asm_operands (c_parser *parser)
6035 {
6036 tree list = NULL_TREE;
6037 while (true)
6038 {
6039 tree name, str;
6040 struct c_expr expr;
6041 if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
6042 {
6043 c_parser_consume_token (parser);
6044 if (c_parser_next_token_is (parser, CPP_NAME))
6045 {
6046 tree id = c_parser_peek_token (parser)->value;
6047 c_parser_consume_token (parser);
6048 name = build_string (IDENTIFIER_LENGTH (id),
6049 IDENTIFIER_POINTER (id));
6050 }
6051 else
6052 {
6053 c_parser_error (parser, "expected identifier");
6054 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
6055 return NULL_TREE;
6056 }
6057 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
6058 "expected %<]%>");
6059 }
6060 else
6061 name = NULL_TREE;
6062 str = c_parser_asm_string_literal (parser);
6063 if (str == NULL_TREE)
6064 return NULL_TREE;
6065 parser->lex_untranslated_string = false;
6066 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6067 {
6068 parser->lex_untranslated_string = true;
6069 return NULL_TREE;
6070 }
6071 expr = c_parser_expression (parser);
6072 mark_exp_read (expr.value);
6073 parser->lex_untranslated_string = true;
6074 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
6075 {
6076 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6077 return NULL_TREE;
6078 }
6079 list = chainon (list, build_tree_list (build_tree_list (name, str),
6080 expr.value));
6081 if (c_parser_next_token_is (parser, CPP_COMMA))
6082 c_parser_consume_token (parser);
6083 else
6084 break;
6085 }
6086 return list;
6087 }
6088
6089 /* Parse asm clobbers, a GNU extension.
6090
6091 asm-clobbers:
6092 asm-string-literal
6093 asm-clobbers , asm-string-literal
6094 */
6095
6096 static tree
6097 c_parser_asm_clobbers (c_parser *parser)
6098 {
6099 tree list = NULL_TREE;
6100 while (true)
6101 {
6102 tree str = c_parser_asm_string_literal (parser);
6103 if (str)
6104 list = tree_cons (NULL_TREE, str, list);
6105 else
6106 return NULL_TREE;
6107 if (c_parser_next_token_is (parser, CPP_COMMA))
6108 c_parser_consume_token (parser);
6109 else
6110 break;
6111 }
6112 return list;
6113 }
6114
6115 /* Parse asm goto labels, a GNU extension.
6116
6117 asm-goto-operands:
6118 identifier
6119 asm-goto-operands , identifier
6120 */
6121
6122 static tree
6123 c_parser_asm_goto_operands (c_parser *parser)
6124 {
6125 tree list = NULL_TREE;
6126 while (true)
6127 {
6128 tree name, label;
6129
6130 if (c_parser_next_token_is (parser, CPP_NAME))
6131 {
6132 c_token *tok = c_parser_peek_token (parser);
6133 name = tok->value;
6134 label = lookup_label_for_goto (tok->location, name);
6135 c_parser_consume_token (parser);
6136 TREE_USED (label) = 1;
6137 }
6138 else
6139 {
6140 c_parser_error (parser, "expected identifier");
6141 return NULL_TREE;
6142 }
6143
6144 name = build_string (IDENTIFIER_LENGTH (name),
6145 IDENTIFIER_POINTER (name));
6146 list = tree_cons (name, label, list);
6147 if (c_parser_next_token_is (parser, CPP_COMMA))
6148 c_parser_consume_token (parser);
6149 else
6150 return nreverse (list);
6151 }
6152 }
6153
6154 /* Parse an expression other than a compound expression; that is, an
6155 assignment expression (C90 6.3.16, C99 6.5.16). If AFTER is not
6156 NULL then it is an Objective-C message expression which is the
6157 primary-expression starting the expression as an initializer.
6158
6159 assignment-expression:
6160 conditional-expression
6161 unary-expression assignment-operator assignment-expression
6162
6163 assignment-operator: one of
6164 = *= /= %= += -= <<= >>= &= ^= |=
6165
6166 In GNU C we accept any conditional expression on the LHS and
6167 diagnose the invalid lvalue rather than producing a syntax
6168 error. */
6169
6170 static struct c_expr
6171 c_parser_expr_no_commas (c_parser *parser, struct c_expr *after,
6172 tree omp_atomic_lhs)
6173 {
6174 struct c_expr lhs, rhs, ret;
6175 enum tree_code code;
6176 location_t op_location, exp_location;
6177 gcc_assert (!after || c_dialect_objc ());
6178 lhs = c_parser_conditional_expression (parser, after, omp_atomic_lhs);
6179 op_location = c_parser_peek_token (parser)->location;
6180 switch (c_parser_peek_token (parser)->type)
6181 {
6182 case CPP_EQ:
6183 code = NOP_EXPR;
6184 break;
6185 case CPP_MULT_EQ:
6186 code = MULT_EXPR;
6187 break;
6188 case CPP_DIV_EQ:
6189 code = TRUNC_DIV_EXPR;
6190 break;
6191 case CPP_MOD_EQ:
6192 code = TRUNC_MOD_EXPR;
6193 break;
6194 case CPP_PLUS_EQ:
6195 code = PLUS_EXPR;
6196 break;
6197 case CPP_MINUS_EQ:
6198 code = MINUS_EXPR;
6199 break;
6200 case CPP_LSHIFT_EQ:
6201 code = LSHIFT_EXPR;
6202 break;
6203 case CPP_RSHIFT_EQ:
6204 code = RSHIFT_EXPR;
6205 break;
6206 case CPP_AND_EQ:
6207 code = BIT_AND_EXPR;
6208 break;
6209 case CPP_XOR_EQ:
6210 code = BIT_XOR_EXPR;
6211 break;
6212 case CPP_OR_EQ:
6213 code = BIT_IOR_EXPR;
6214 break;
6215 default:
6216 return lhs;
6217 }
6218 c_parser_consume_token (parser);
6219 exp_location = c_parser_peek_token (parser)->location;
6220 rhs = c_parser_expr_no_commas (parser, NULL);
6221 rhs = convert_lvalue_to_rvalue (exp_location, rhs, true, true);
6222
6223 ret.value = build_modify_expr (op_location, lhs.value, lhs.original_type,
6224 code, exp_location, rhs.value,
6225 rhs.original_type);
6226 set_c_expr_source_range (&ret, lhs.get_start (), rhs.get_finish ());
6227 if (code == NOP_EXPR)
6228 ret.original_code = MODIFY_EXPR;
6229 else
6230 {
6231 TREE_NO_WARNING (ret.value) = 1;
6232 ret.original_code = ERROR_MARK;
6233 }
6234 ret.original_type = NULL;
6235 return ret;
6236 }
6237
6238 /* Parse a conditional expression (C90 6.3.15, C99 6.5.15). If AFTER
6239 is not NULL then it is an Objective-C message expression which is
6240 the primary-expression starting the expression as an initializer.
6241
6242 conditional-expression:
6243 logical-OR-expression
6244 logical-OR-expression ? expression : conditional-expression
6245
6246 GNU extensions:
6247
6248 conditional-expression:
6249 logical-OR-expression ? : conditional-expression
6250 */
6251
6252 static struct c_expr
6253 c_parser_conditional_expression (c_parser *parser, struct c_expr *after,
6254 tree omp_atomic_lhs)
6255 {
6256 struct c_expr cond, exp1, exp2, ret;
6257 location_t start, cond_loc, colon_loc, middle_loc;
6258
6259 gcc_assert (!after || c_dialect_objc ());
6260
6261 cond = c_parser_binary_expression (parser, after, omp_atomic_lhs);
6262
6263 if (c_parser_next_token_is_not (parser, CPP_QUERY))
6264 return cond;
6265 if (cond.value != error_mark_node)
6266 start = cond.get_start ();
6267 else
6268 start = UNKNOWN_LOCATION;
6269 cond_loc = c_parser_peek_token (parser)->location;
6270 cond = convert_lvalue_to_rvalue (cond_loc, cond, true, true);
6271 c_parser_consume_token (parser);
6272 if (c_parser_next_token_is (parser, CPP_COLON))
6273 {
6274 tree eptype = NULL_TREE;
6275
6276 middle_loc = c_parser_peek_token (parser)->location;
6277 pedwarn (middle_loc, OPT_Wpedantic,
6278 "ISO C forbids omitting the middle term of a ?: expression");
6279 warn_for_omitted_condop (middle_loc, cond.value);
6280 if (TREE_CODE (cond.value) == EXCESS_PRECISION_EXPR)
6281 {
6282 eptype = TREE_TYPE (cond.value);
6283 cond.value = TREE_OPERAND (cond.value, 0);
6284 }
6285 /* Make sure first operand is calculated only once. */
6286 exp1.value = c_save_expr (default_conversion (cond.value));
6287 if (eptype)
6288 exp1.value = build1 (EXCESS_PRECISION_EXPR, eptype, exp1.value);
6289 exp1.original_type = NULL;
6290 cond.value = c_objc_common_truthvalue_conversion (cond_loc, exp1.value);
6291 c_inhibit_evaluation_warnings += cond.value == truthvalue_true_node;
6292 }
6293 else
6294 {
6295 cond.value
6296 = c_objc_common_truthvalue_conversion
6297 (cond_loc, default_conversion (cond.value));
6298 c_inhibit_evaluation_warnings += cond.value == truthvalue_false_node;
6299 exp1 = c_parser_expression_conv (parser);
6300 mark_exp_read (exp1.value);
6301 c_inhibit_evaluation_warnings +=
6302 ((cond.value == truthvalue_true_node)
6303 - (cond.value == truthvalue_false_node));
6304 }
6305
6306 colon_loc = c_parser_peek_token (parser)->location;
6307 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
6308 {
6309 c_inhibit_evaluation_warnings -= cond.value == truthvalue_true_node;
6310 ret.value = error_mark_node;
6311 ret.original_code = ERROR_MARK;
6312 ret.original_type = NULL;
6313 return ret;
6314 }
6315 {
6316 location_t exp2_loc = c_parser_peek_token (parser)->location;
6317 exp2 = c_parser_conditional_expression (parser, NULL, NULL_TREE);
6318 exp2 = convert_lvalue_to_rvalue (exp2_loc, exp2, true, true);
6319 }
6320 c_inhibit_evaluation_warnings -= cond.value == truthvalue_true_node;
6321 ret.value = build_conditional_expr (colon_loc, cond.value,
6322 cond.original_code == C_MAYBE_CONST_EXPR,
6323 exp1.value, exp1.original_type,
6324 exp2.value, exp2.original_type);
6325 ret.original_code = ERROR_MARK;
6326 if (exp1.value == error_mark_node || exp2.value == error_mark_node)
6327 ret.original_type = NULL;
6328 else
6329 {
6330 tree t1, t2;
6331
6332 /* If both sides are enum type, the default conversion will have
6333 made the type of the result be an integer type. We want to
6334 remember the enum types we started with. */
6335 t1 = exp1.original_type ? exp1.original_type : TREE_TYPE (exp1.value);
6336 t2 = exp2.original_type ? exp2.original_type : TREE_TYPE (exp2.value);
6337 ret.original_type = ((t1 != error_mark_node
6338 && t2 != error_mark_node
6339 && (TYPE_MAIN_VARIANT (t1)
6340 == TYPE_MAIN_VARIANT (t2)))
6341 ? t1
6342 : NULL);
6343 }
6344 set_c_expr_source_range (&ret, start, exp2.get_finish ());
6345 return ret;
6346 }
6347
6348 /* Parse a binary expression; that is, a logical-OR-expression (C90
6349 6.3.5-6.3.14, C99 6.5.5-6.5.14). If AFTER is not NULL then it is
6350 an Objective-C message expression which is the primary-expression
6351 starting the expression as an initializer.
6352
6353 OMP_ATOMIC_LHS is NULL, unless parsing OpenMP #pragma omp atomic,
6354 when it should be the unfolded lhs. In a valid OpenMP source,
6355 one of the operands of the toplevel binary expression must be equal
6356 to it. In that case, just return a build2 created binary operation
6357 rather than result of parser_build_binary_op.
6358
6359 multiplicative-expression:
6360 cast-expression
6361 multiplicative-expression * cast-expression
6362 multiplicative-expression / cast-expression
6363 multiplicative-expression % cast-expression
6364
6365 additive-expression:
6366 multiplicative-expression
6367 additive-expression + multiplicative-expression
6368 additive-expression - multiplicative-expression
6369
6370 shift-expression:
6371 additive-expression
6372 shift-expression << additive-expression
6373 shift-expression >> additive-expression
6374
6375 relational-expression:
6376 shift-expression
6377 relational-expression < shift-expression
6378 relational-expression > shift-expression
6379 relational-expression <= shift-expression
6380 relational-expression >= shift-expression
6381
6382 equality-expression:
6383 relational-expression
6384 equality-expression == relational-expression
6385 equality-expression != relational-expression
6386
6387 AND-expression:
6388 equality-expression
6389 AND-expression & equality-expression
6390
6391 exclusive-OR-expression:
6392 AND-expression
6393 exclusive-OR-expression ^ AND-expression
6394
6395 inclusive-OR-expression:
6396 exclusive-OR-expression
6397 inclusive-OR-expression | exclusive-OR-expression
6398
6399 logical-AND-expression:
6400 inclusive-OR-expression
6401 logical-AND-expression && inclusive-OR-expression
6402
6403 logical-OR-expression:
6404 logical-AND-expression
6405 logical-OR-expression || logical-AND-expression
6406 */
6407
6408 static struct c_expr
6409 c_parser_binary_expression (c_parser *parser, struct c_expr *after,
6410 tree omp_atomic_lhs)
6411 {
6412 /* A binary expression is parsed using operator-precedence parsing,
6413 with the operands being cast expressions. All the binary
6414 operators are left-associative. Thus a binary expression is of
6415 form:
6416
6417 E0 op1 E1 op2 E2 ...
6418
6419 which we represent on a stack. On the stack, the precedence
6420 levels are strictly increasing. When a new operator is
6421 encountered of higher precedence than that at the top of the
6422 stack, it is pushed; its LHS is the top expression, and its RHS
6423 is everything parsed until it is popped. When a new operator is
6424 encountered with precedence less than or equal to that at the top
6425 of the stack, triples E[i-1] op[i] E[i] are popped and replaced
6426 by the result of the operation until the operator at the top of
6427 the stack has lower precedence than the new operator or there is
6428 only one element on the stack; then the top expression is the LHS
6429 of the new operator. In the case of logical AND and OR
6430 expressions, we also need to adjust c_inhibit_evaluation_warnings
6431 as appropriate when the operators are pushed and popped. */
6432
6433 struct {
6434 /* The expression at this stack level. */
6435 struct c_expr expr;
6436 /* The precedence of the operator on its left, PREC_NONE at the
6437 bottom of the stack. */
6438 enum c_parser_prec prec;
6439 /* The operation on its left. */
6440 enum tree_code op;
6441 /* The source location of this operation. */
6442 location_t loc;
6443 } stack[NUM_PRECS];
6444 int sp;
6445 /* Location of the binary operator. */
6446 location_t binary_loc = UNKNOWN_LOCATION; /* Quiet warning. */
6447 #define POP \
6448 do { \
6449 switch (stack[sp].op) \
6450 { \
6451 case TRUTH_ANDIF_EXPR: \
6452 c_inhibit_evaluation_warnings -= (stack[sp - 1].expr.value \
6453 == truthvalue_false_node); \
6454 break; \
6455 case TRUTH_ORIF_EXPR: \
6456 c_inhibit_evaluation_warnings -= (stack[sp - 1].expr.value \
6457 == truthvalue_true_node); \
6458 break; \
6459 default: \
6460 break; \
6461 } \
6462 stack[sp - 1].expr \
6463 = convert_lvalue_to_rvalue (stack[sp - 1].loc, \
6464 stack[sp - 1].expr, true, true); \
6465 stack[sp].expr \
6466 = convert_lvalue_to_rvalue (stack[sp].loc, \
6467 stack[sp].expr, true, true); \
6468 if (__builtin_expect (omp_atomic_lhs != NULL_TREE, 0) && sp == 1 \
6469 && c_parser_peek_token (parser)->type == CPP_SEMICOLON \
6470 && ((1 << stack[sp].prec) \
6471 & ((1 << PREC_BITOR) | (1 << PREC_BITXOR) | (1 << PREC_BITAND) \
6472 | (1 << PREC_SHIFT) | (1 << PREC_ADD) | (1 << PREC_MULT))) \
6473 && stack[sp].op != TRUNC_MOD_EXPR \
6474 && stack[0].expr.value != error_mark_node \
6475 && stack[1].expr.value != error_mark_node \
6476 && (c_tree_equal (stack[0].expr.value, omp_atomic_lhs) \
6477 || c_tree_equal (stack[1].expr.value, omp_atomic_lhs))) \
6478 stack[0].expr.value \
6479 = build2 (stack[1].op, TREE_TYPE (stack[0].expr.value), \
6480 stack[0].expr.value, stack[1].expr.value); \
6481 else \
6482 stack[sp - 1].expr = parser_build_binary_op (stack[sp].loc, \
6483 stack[sp].op, \
6484 stack[sp - 1].expr, \
6485 stack[sp].expr); \
6486 sp--; \
6487 } while (0)
6488 gcc_assert (!after || c_dialect_objc ());
6489 stack[0].loc = c_parser_peek_token (parser)->location;
6490 stack[0].expr = c_parser_cast_expression (parser, after);
6491 stack[0].prec = PREC_NONE;
6492 sp = 0;
6493 while (true)
6494 {
6495 enum c_parser_prec oprec;
6496 enum tree_code ocode;
6497 source_range src_range;
6498 if (parser->error)
6499 goto out;
6500 switch (c_parser_peek_token (parser)->type)
6501 {
6502 case CPP_MULT:
6503 oprec = PREC_MULT;
6504 ocode = MULT_EXPR;
6505 break;
6506 case CPP_DIV:
6507 oprec = PREC_MULT;
6508 ocode = TRUNC_DIV_EXPR;
6509 break;
6510 case CPP_MOD:
6511 oprec = PREC_MULT;
6512 ocode = TRUNC_MOD_EXPR;
6513 break;
6514 case CPP_PLUS:
6515 oprec = PREC_ADD;
6516 ocode = PLUS_EXPR;
6517 break;
6518 case CPP_MINUS:
6519 oprec = PREC_ADD;
6520 ocode = MINUS_EXPR;
6521 break;
6522 case CPP_LSHIFT:
6523 oprec = PREC_SHIFT;
6524 ocode = LSHIFT_EXPR;
6525 break;
6526 case CPP_RSHIFT:
6527 oprec = PREC_SHIFT;
6528 ocode = RSHIFT_EXPR;
6529 break;
6530 case CPP_LESS:
6531 oprec = PREC_REL;
6532 ocode = LT_EXPR;
6533 break;
6534 case CPP_GREATER:
6535 oprec = PREC_REL;
6536 ocode = GT_EXPR;
6537 break;
6538 case CPP_LESS_EQ:
6539 oprec = PREC_REL;
6540 ocode = LE_EXPR;
6541 break;
6542 case CPP_GREATER_EQ:
6543 oprec = PREC_REL;
6544 ocode = GE_EXPR;
6545 break;
6546 case CPP_EQ_EQ:
6547 oprec = PREC_EQ;
6548 ocode = EQ_EXPR;
6549 break;
6550 case CPP_NOT_EQ:
6551 oprec = PREC_EQ;
6552 ocode = NE_EXPR;
6553 break;
6554 case CPP_AND:
6555 oprec = PREC_BITAND;
6556 ocode = BIT_AND_EXPR;
6557 break;
6558 case CPP_XOR:
6559 oprec = PREC_BITXOR;
6560 ocode = BIT_XOR_EXPR;
6561 break;
6562 case CPP_OR:
6563 oprec = PREC_BITOR;
6564 ocode = BIT_IOR_EXPR;
6565 break;
6566 case CPP_AND_AND:
6567 oprec = PREC_LOGAND;
6568 ocode = TRUTH_ANDIF_EXPR;
6569 break;
6570 case CPP_OR_OR:
6571 oprec = PREC_LOGOR;
6572 ocode = TRUTH_ORIF_EXPR;
6573 break;
6574 default:
6575 /* Not a binary operator, so end of the binary
6576 expression. */
6577 goto out;
6578 }
6579 binary_loc = c_parser_peek_token (parser)->location;
6580 while (oprec <= stack[sp].prec)
6581 POP;
6582 c_parser_consume_token (parser);
6583 switch (ocode)
6584 {
6585 case TRUTH_ANDIF_EXPR:
6586 src_range = stack[sp].expr.src_range;
6587 stack[sp].expr
6588 = convert_lvalue_to_rvalue (stack[sp].loc,
6589 stack[sp].expr, true, true);
6590 stack[sp].expr.value = c_objc_common_truthvalue_conversion
6591 (stack[sp].loc, default_conversion (stack[sp].expr.value));
6592 c_inhibit_evaluation_warnings += (stack[sp].expr.value
6593 == truthvalue_false_node);
6594 set_c_expr_source_range (&stack[sp].expr, src_range);
6595 break;
6596 case TRUTH_ORIF_EXPR:
6597 src_range = stack[sp].expr.src_range;
6598 stack[sp].expr
6599 = convert_lvalue_to_rvalue (stack[sp].loc,
6600 stack[sp].expr, true, true);
6601 stack[sp].expr.value = c_objc_common_truthvalue_conversion
6602 (stack[sp].loc, default_conversion (stack[sp].expr.value));
6603 c_inhibit_evaluation_warnings += (stack[sp].expr.value
6604 == truthvalue_true_node);
6605 set_c_expr_source_range (&stack[sp].expr, src_range);
6606 break;
6607 default:
6608 break;
6609 }
6610 sp++;
6611 stack[sp].loc = binary_loc;
6612 stack[sp].expr = c_parser_cast_expression (parser, NULL);
6613 stack[sp].prec = oprec;
6614 stack[sp].op = ocode;
6615 }
6616 out:
6617 while (sp > 0)
6618 POP;
6619 return stack[0].expr;
6620 #undef POP
6621 }
6622
6623 /* Parse a cast expression (C90 6.3.4, C99 6.5.4). If AFTER is not
6624 NULL then it is an Objective-C message expression which is the
6625 primary-expression starting the expression as an initializer.
6626
6627 cast-expression:
6628 unary-expression
6629 ( type-name ) unary-expression
6630 */
6631
6632 static struct c_expr
6633 c_parser_cast_expression (c_parser *parser, struct c_expr *after)
6634 {
6635 location_t cast_loc = c_parser_peek_token (parser)->location;
6636 gcc_assert (!after || c_dialect_objc ());
6637 if (after)
6638 return c_parser_postfix_expression_after_primary (parser,
6639 cast_loc, *after);
6640 /* If the expression begins with a parenthesized type name, it may
6641 be either a cast or a compound literal; we need to see whether
6642 the next character is '{' to tell the difference. If not, it is
6643 an unary expression. Full detection of unknown typenames here
6644 would require a 3-token lookahead. */
6645 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
6646 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
6647 {
6648 struct c_type_name *type_name;
6649 struct c_expr ret;
6650 struct c_expr expr;
6651 c_parser_consume_token (parser);
6652 type_name = c_parser_type_name (parser);
6653 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
6654 if (type_name == NULL)
6655 {
6656 ret.value = error_mark_node;
6657 ret.original_code = ERROR_MARK;
6658 ret.original_type = NULL;
6659 return ret;
6660 }
6661
6662 /* Save casted types in the function's used types hash table. */
6663 used_types_insert (type_name->specs->type);
6664
6665 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
6666 return c_parser_postfix_expression_after_paren_type (parser, type_name,
6667 cast_loc);
6668 {
6669 location_t expr_loc = c_parser_peek_token (parser)->location;
6670 expr = c_parser_cast_expression (parser, NULL);
6671 expr = convert_lvalue_to_rvalue (expr_loc, expr, true, true);
6672 }
6673 ret.value = c_cast_expr (cast_loc, type_name, expr.value);
6674 if (ret.value && expr.value)
6675 set_c_expr_source_range (&ret, cast_loc, expr.get_finish ());
6676 ret.original_code = ERROR_MARK;
6677 ret.original_type = NULL;
6678 return ret;
6679 }
6680 else
6681 return c_parser_unary_expression (parser);
6682 }
6683
6684 /* Parse an unary expression (C90 6.3.3, C99 6.5.3).
6685
6686 unary-expression:
6687 postfix-expression
6688 ++ unary-expression
6689 -- unary-expression
6690 unary-operator cast-expression
6691 sizeof unary-expression
6692 sizeof ( type-name )
6693
6694 unary-operator: one of
6695 & * + - ~ !
6696
6697 GNU extensions:
6698
6699 unary-expression:
6700 __alignof__ unary-expression
6701 __alignof__ ( type-name )
6702 && identifier
6703
6704 (C11 permits _Alignof with type names only.)
6705
6706 unary-operator: one of
6707 __extension__ __real__ __imag__
6708
6709 Transactional Memory:
6710
6711 unary-expression:
6712 transaction-expression
6713
6714 In addition, the GNU syntax treats ++ and -- as unary operators, so
6715 they may be applied to cast expressions with errors for non-lvalues
6716 given later. */
6717
6718 static struct c_expr
6719 c_parser_unary_expression (c_parser *parser)
6720 {
6721 int ext;
6722 struct c_expr ret, op;
6723 location_t op_loc = c_parser_peek_token (parser)->location;
6724 location_t exp_loc;
6725 location_t finish;
6726 ret.original_code = ERROR_MARK;
6727 ret.original_type = NULL;
6728 switch (c_parser_peek_token (parser)->type)
6729 {
6730 case CPP_PLUS_PLUS:
6731 c_parser_consume_token (parser);
6732 exp_loc = c_parser_peek_token (parser)->location;
6733 op = c_parser_cast_expression (parser, NULL);
6734
6735 /* If there is array notations in op, we expand them. */
6736 if (flag_cilkplus && TREE_CODE (op.value) == ARRAY_NOTATION_REF)
6737 return fix_array_notation_expr (exp_loc, PREINCREMENT_EXPR, op);
6738 else
6739 {
6740 op = default_function_array_read_conversion (exp_loc, op);
6741 return parser_build_unary_op (op_loc, PREINCREMENT_EXPR, op);
6742 }
6743 case CPP_MINUS_MINUS:
6744 c_parser_consume_token (parser);
6745 exp_loc = c_parser_peek_token (parser)->location;
6746 op = c_parser_cast_expression (parser, NULL);
6747
6748 /* If there is array notations in op, we expand them. */
6749 if (flag_cilkplus && TREE_CODE (op.value) == ARRAY_NOTATION_REF)
6750 return fix_array_notation_expr (exp_loc, PREDECREMENT_EXPR, op);
6751 else
6752 {
6753 op = default_function_array_read_conversion (exp_loc, op);
6754 return parser_build_unary_op (op_loc, PREDECREMENT_EXPR, op);
6755 }
6756 case CPP_AND:
6757 c_parser_consume_token (parser);
6758 op = c_parser_cast_expression (parser, NULL);
6759 mark_exp_read (op.value);
6760 return parser_build_unary_op (op_loc, ADDR_EXPR, op);
6761 case CPP_MULT:
6762 c_parser_consume_token (parser);
6763 exp_loc = c_parser_peek_token (parser)->location;
6764 op = c_parser_cast_expression (parser, NULL);
6765 finish = op.get_finish ();
6766 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
6767 ret.value = build_indirect_ref (op_loc, op.value, RO_UNARY_STAR);
6768 set_c_expr_source_range (&ret, op_loc, finish);
6769 return ret;
6770 case CPP_PLUS:
6771 if (!c_dialect_objc () && !in_system_header_at (input_location))
6772 warning_at (op_loc,
6773 OPT_Wtraditional,
6774 "traditional C rejects the unary plus operator");
6775 c_parser_consume_token (parser);
6776 exp_loc = c_parser_peek_token (parser)->location;
6777 op = c_parser_cast_expression (parser, NULL);
6778 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
6779 return parser_build_unary_op (op_loc, CONVERT_EXPR, op);
6780 case CPP_MINUS:
6781 c_parser_consume_token (parser);
6782 exp_loc = c_parser_peek_token (parser)->location;
6783 op = c_parser_cast_expression (parser, NULL);
6784 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
6785 return parser_build_unary_op (op_loc, NEGATE_EXPR, op);
6786 case CPP_COMPL:
6787 c_parser_consume_token (parser);
6788 exp_loc = c_parser_peek_token (parser)->location;
6789 op = c_parser_cast_expression (parser, NULL);
6790 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
6791 return parser_build_unary_op (op_loc, BIT_NOT_EXPR, op);
6792 case CPP_NOT:
6793 c_parser_consume_token (parser);
6794 exp_loc = c_parser_peek_token (parser)->location;
6795 op = c_parser_cast_expression (parser, NULL);
6796 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
6797 return parser_build_unary_op (op_loc, TRUTH_NOT_EXPR, op);
6798 case CPP_AND_AND:
6799 /* Refer to the address of a label as a pointer. */
6800 c_parser_consume_token (parser);
6801 if (c_parser_next_token_is (parser, CPP_NAME))
6802 {
6803 ret.value = finish_label_address_expr
6804 (c_parser_peek_token (parser)->value, op_loc);
6805 set_c_expr_source_range (&ret, op_loc,
6806 c_parser_peek_token (parser)->get_finish ());
6807 c_parser_consume_token (parser);
6808 }
6809 else
6810 {
6811 c_parser_error (parser, "expected identifier");
6812 ret.value = error_mark_node;
6813 }
6814 return ret;
6815 case CPP_KEYWORD:
6816 switch (c_parser_peek_token (parser)->keyword)
6817 {
6818 case RID_SIZEOF:
6819 return c_parser_sizeof_expression (parser);
6820 case RID_ALIGNOF:
6821 return c_parser_alignof_expression (parser);
6822 case RID_EXTENSION:
6823 c_parser_consume_token (parser);
6824 ext = disable_extension_diagnostics ();
6825 ret = c_parser_cast_expression (parser, NULL);
6826 restore_extension_diagnostics (ext);
6827 return ret;
6828 case RID_REALPART:
6829 c_parser_consume_token (parser);
6830 exp_loc = c_parser_peek_token (parser)->location;
6831 op = c_parser_cast_expression (parser, NULL);
6832 op = default_function_array_conversion (exp_loc, op);
6833 return parser_build_unary_op (op_loc, REALPART_EXPR, op);
6834 case RID_IMAGPART:
6835 c_parser_consume_token (parser);
6836 exp_loc = c_parser_peek_token (parser)->location;
6837 op = c_parser_cast_expression (parser, NULL);
6838 op = default_function_array_conversion (exp_loc, op);
6839 return parser_build_unary_op (op_loc, IMAGPART_EXPR, op);
6840 case RID_TRANSACTION_ATOMIC:
6841 case RID_TRANSACTION_RELAXED:
6842 return c_parser_transaction_expression (parser,
6843 c_parser_peek_token (parser)->keyword);
6844 default:
6845 return c_parser_postfix_expression (parser);
6846 }
6847 default:
6848 return c_parser_postfix_expression (parser);
6849 }
6850 }
6851
6852 /* Parse a sizeof expression. */
6853
6854 static struct c_expr
6855 c_parser_sizeof_expression (c_parser *parser)
6856 {
6857 struct c_expr expr;
6858 struct c_expr result;
6859 location_t expr_loc;
6860 gcc_assert (c_parser_next_token_is_keyword (parser, RID_SIZEOF));
6861
6862 location_t start;
6863 location_t finish = UNKNOWN_LOCATION;
6864
6865 start = c_parser_peek_token (parser)->location;
6866
6867 c_parser_consume_token (parser);
6868 c_inhibit_evaluation_warnings++;
6869 in_sizeof++;
6870 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
6871 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
6872 {
6873 /* Either sizeof ( type-name ) or sizeof unary-expression
6874 starting with a compound literal. */
6875 struct c_type_name *type_name;
6876 c_parser_consume_token (parser);
6877 expr_loc = c_parser_peek_token (parser)->location;
6878 type_name = c_parser_type_name (parser);
6879 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
6880 finish = parser->tokens_buf[0].location;
6881 if (type_name == NULL)
6882 {
6883 struct c_expr ret;
6884 c_inhibit_evaluation_warnings--;
6885 in_sizeof--;
6886 ret.value = error_mark_node;
6887 ret.original_code = ERROR_MARK;
6888 ret.original_type = NULL;
6889 return ret;
6890 }
6891 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
6892 {
6893 expr = c_parser_postfix_expression_after_paren_type (parser,
6894 type_name,
6895 expr_loc);
6896 finish = expr.get_finish ();
6897 goto sizeof_expr;
6898 }
6899 /* sizeof ( type-name ). */
6900 c_inhibit_evaluation_warnings--;
6901 in_sizeof--;
6902 result = c_expr_sizeof_type (expr_loc, type_name);
6903 }
6904 else
6905 {
6906 expr_loc = c_parser_peek_token (parser)->location;
6907 expr = c_parser_unary_expression (parser);
6908 finish = expr.get_finish ();
6909 sizeof_expr:
6910 c_inhibit_evaluation_warnings--;
6911 in_sizeof--;
6912 mark_exp_read (expr.value);
6913 if (TREE_CODE (expr.value) == COMPONENT_REF
6914 && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1)))
6915 error_at (expr_loc, "%<sizeof%> applied to a bit-field");
6916 result = c_expr_sizeof_expr (expr_loc, expr);
6917 }
6918 if (finish != UNKNOWN_LOCATION)
6919 set_c_expr_source_range (&result, start, finish);
6920 return result;
6921 }
6922
6923 /* Parse an alignof expression. */
6924
6925 static struct c_expr
6926 c_parser_alignof_expression (c_parser *parser)
6927 {
6928 struct c_expr expr;
6929 location_t start_loc = c_parser_peek_token (parser)->location;
6930 location_t end_loc;
6931 tree alignof_spelling = c_parser_peek_token (parser)->value;
6932 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ALIGNOF));
6933 bool is_c11_alignof = strcmp (IDENTIFIER_POINTER (alignof_spelling),
6934 "_Alignof") == 0;
6935 /* A diagnostic is not required for the use of this identifier in
6936 the implementation namespace; only diagnose it for the C11
6937 spelling because of existing code using the other spellings. */
6938 if (is_c11_alignof)
6939 {
6940 if (flag_isoc99)
6941 pedwarn_c99 (start_loc, OPT_Wpedantic, "ISO C99 does not support %qE",
6942 alignof_spelling);
6943 else
6944 pedwarn_c99 (start_loc, OPT_Wpedantic, "ISO C90 does not support %qE",
6945 alignof_spelling);
6946 }
6947 c_parser_consume_token (parser);
6948 c_inhibit_evaluation_warnings++;
6949 in_alignof++;
6950 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
6951 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
6952 {
6953 /* Either __alignof__ ( type-name ) or __alignof__
6954 unary-expression starting with a compound literal. */
6955 location_t loc;
6956 struct c_type_name *type_name;
6957 struct c_expr ret;
6958 c_parser_consume_token (parser);
6959 loc = c_parser_peek_token (parser)->location;
6960 type_name = c_parser_type_name (parser);
6961 end_loc = c_parser_peek_token (parser)->location;
6962 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
6963 if (type_name == NULL)
6964 {
6965 struct c_expr ret;
6966 c_inhibit_evaluation_warnings--;
6967 in_alignof--;
6968 ret.value = error_mark_node;
6969 ret.original_code = ERROR_MARK;
6970 ret.original_type = NULL;
6971 return ret;
6972 }
6973 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
6974 {
6975 expr = c_parser_postfix_expression_after_paren_type (parser,
6976 type_name,
6977 loc);
6978 goto alignof_expr;
6979 }
6980 /* alignof ( type-name ). */
6981 c_inhibit_evaluation_warnings--;
6982 in_alignof--;
6983 ret.value = c_sizeof_or_alignof_type (loc, groktypename (type_name,
6984 NULL, NULL),
6985 false, is_c11_alignof, 1);
6986 ret.original_code = ERROR_MARK;
6987 ret.original_type = NULL;
6988 set_c_expr_source_range (&ret, start_loc, end_loc);
6989 return ret;
6990 }
6991 else
6992 {
6993 struct c_expr ret;
6994 expr = c_parser_unary_expression (parser);
6995 end_loc = expr.src_range.m_finish;
6996 alignof_expr:
6997 mark_exp_read (expr.value);
6998 c_inhibit_evaluation_warnings--;
6999 in_alignof--;
7000 pedwarn (start_loc,
7001 OPT_Wpedantic, "ISO C does not allow %<%E (expression)%>",
7002 alignof_spelling);
7003 ret.value = c_alignof_expr (start_loc, expr.value);
7004 ret.original_code = ERROR_MARK;
7005 ret.original_type = NULL;
7006 set_c_expr_source_range (&ret, start_loc, end_loc);
7007 return ret;
7008 }
7009 }
7010
7011 /* Helper function to read arguments of builtins which are interfaces
7012 for the middle-end nodes like COMPLEX_EXPR, VEC_PERM_EXPR and
7013 others. The name of the builtin is passed using BNAME parameter.
7014 Function returns true if there were no errors while parsing and
7015 stores the arguments in CEXPR_LIST. If it returns true,
7016 *OUT_CLOSE_PAREN_LOC is written to with the location of the closing
7017 parenthesis. */
7018 static bool
7019 c_parser_get_builtin_args (c_parser *parser, const char *bname,
7020 vec<c_expr_t, va_gc> **ret_cexpr_list,
7021 bool choose_expr_p,
7022 location_t *out_close_paren_loc)
7023 {
7024 location_t loc = c_parser_peek_token (parser)->location;
7025 vec<c_expr_t, va_gc> *cexpr_list;
7026 c_expr_t expr;
7027 bool saved_force_folding_builtin_constant_p;
7028
7029 *ret_cexpr_list = NULL;
7030 if (c_parser_next_token_is_not (parser, CPP_OPEN_PAREN))
7031 {
7032 error_at (loc, "cannot take address of %qs", bname);
7033 return false;
7034 }
7035
7036 c_parser_consume_token (parser);
7037
7038 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
7039 {
7040 *out_close_paren_loc = c_parser_peek_token (parser)->location;
7041 c_parser_consume_token (parser);
7042 return true;
7043 }
7044
7045 saved_force_folding_builtin_constant_p
7046 = force_folding_builtin_constant_p;
7047 force_folding_builtin_constant_p |= choose_expr_p;
7048 expr = c_parser_expr_no_commas (parser, NULL);
7049 force_folding_builtin_constant_p
7050 = saved_force_folding_builtin_constant_p;
7051 vec_alloc (cexpr_list, 1);
7052 vec_safe_push (cexpr_list, expr);
7053 while (c_parser_next_token_is (parser, CPP_COMMA))
7054 {
7055 c_parser_consume_token (parser);
7056 expr = c_parser_expr_no_commas (parser, NULL);
7057 vec_safe_push (cexpr_list, expr);
7058 }
7059
7060 *out_close_paren_loc = c_parser_peek_token (parser)->location;
7061 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
7062 return false;
7063
7064 *ret_cexpr_list = cexpr_list;
7065 return true;
7066 }
7067
7068 /* This represents a single generic-association. */
7069
7070 struct c_generic_association
7071 {
7072 /* The location of the starting token of the type. */
7073 location_t type_location;
7074 /* The association's type, or NULL_TREE for 'default'. */
7075 tree type;
7076 /* The association's expression. */
7077 struct c_expr expression;
7078 };
7079
7080 /* Parse a generic-selection. (C11 6.5.1.1).
7081
7082 generic-selection:
7083 _Generic ( assignment-expression , generic-assoc-list )
7084
7085 generic-assoc-list:
7086 generic-association
7087 generic-assoc-list , generic-association
7088
7089 generic-association:
7090 type-name : assignment-expression
7091 default : assignment-expression
7092 */
7093
7094 static struct c_expr
7095 c_parser_generic_selection (c_parser *parser)
7096 {
7097 vec<c_generic_association> associations = vNULL;
7098 struct c_expr selector, error_expr;
7099 tree selector_type;
7100 struct c_generic_association matched_assoc;
7101 bool match_found = false;
7102 location_t generic_loc, selector_loc;
7103
7104 error_expr.original_code = ERROR_MARK;
7105 error_expr.original_type = NULL;
7106 error_expr.value = error_mark_node;
7107 matched_assoc.type_location = UNKNOWN_LOCATION;
7108 matched_assoc.type = NULL_TREE;
7109 matched_assoc.expression = error_expr;
7110
7111 gcc_assert (c_parser_next_token_is_keyword (parser, RID_GENERIC));
7112 generic_loc = c_parser_peek_token (parser)->location;
7113 c_parser_consume_token (parser);
7114 if (flag_isoc99)
7115 pedwarn_c99 (generic_loc, OPT_Wpedantic,
7116 "ISO C99 does not support %<_Generic%>");
7117 else
7118 pedwarn_c99 (generic_loc, OPT_Wpedantic,
7119 "ISO C90 does not support %<_Generic%>");
7120
7121 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7122 return error_expr;
7123
7124 c_inhibit_evaluation_warnings++;
7125 selector_loc = c_parser_peek_token (parser)->location;
7126 selector = c_parser_expr_no_commas (parser, NULL);
7127 selector = default_function_array_conversion (selector_loc, selector);
7128 c_inhibit_evaluation_warnings--;
7129
7130 if (selector.value == error_mark_node)
7131 {
7132 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7133 return selector;
7134 }
7135 selector_type = TREE_TYPE (selector.value);
7136 /* In ISO C terms, rvalues (including the controlling expression of
7137 _Generic) do not have qualified types. */
7138 if (TREE_CODE (selector_type) != ARRAY_TYPE)
7139 selector_type = TYPE_MAIN_VARIANT (selector_type);
7140 /* In ISO C terms, _Noreturn is not part of the type of expressions
7141 such as &abort, but in GCC it is represented internally as a type
7142 qualifier. */
7143 if (FUNCTION_POINTER_TYPE_P (selector_type)
7144 && TYPE_QUALS (TREE_TYPE (selector_type)) != TYPE_UNQUALIFIED)
7145 selector_type
7146 = build_pointer_type (TYPE_MAIN_VARIANT (TREE_TYPE (selector_type)));
7147
7148 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
7149 {
7150 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7151 return error_expr;
7152 }
7153
7154 while (1)
7155 {
7156 struct c_generic_association assoc, *iter;
7157 unsigned int ix;
7158 c_token *token = c_parser_peek_token (parser);
7159
7160 assoc.type_location = token->location;
7161 if (token->type == CPP_KEYWORD && token->keyword == RID_DEFAULT)
7162 {
7163 c_parser_consume_token (parser);
7164 assoc.type = NULL_TREE;
7165 }
7166 else
7167 {
7168 struct c_type_name *type_name;
7169
7170 type_name = c_parser_type_name (parser);
7171 if (type_name == NULL)
7172 {
7173 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7174 goto error_exit;
7175 }
7176 assoc.type = groktypename (type_name, NULL, NULL);
7177 if (assoc.type == error_mark_node)
7178 {
7179 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7180 goto error_exit;
7181 }
7182
7183 if (TREE_CODE (assoc.type) == FUNCTION_TYPE)
7184 error_at (assoc.type_location,
7185 "%<_Generic%> association has function type");
7186 else if (!COMPLETE_TYPE_P (assoc.type))
7187 error_at (assoc.type_location,
7188 "%<_Generic%> association has incomplete type");
7189
7190 if (variably_modified_type_p (assoc.type, NULL_TREE))
7191 error_at (assoc.type_location,
7192 "%<_Generic%> association has "
7193 "variable length type");
7194 }
7195
7196 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
7197 {
7198 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7199 goto error_exit;
7200 }
7201
7202 assoc.expression = c_parser_expr_no_commas (parser, NULL);
7203 if (assoc.expression.value == error_mark_node)
7204 {
7205 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7206 goto error_exit;
7207 }
7208
7209 for (ix = 0; associations.iterate (ix, &iter); ++ix)
7210 {
7211 if (assoc.type == NULL_TREE)
7212 {
7213 if (iter->type == NULL_TREE)
7214 {
7215 error_at (assoc.type_location,
7216 "duplicate %<default%> case in %<_Generic%>");
7217 inform (iter->type_location, "original %<default%> is here");
7218 }
7219 }
7220 else if (iter->type != NULL_TREE)
7221 {
7222 if (comptypes (assoc.type, iter->type))
7223 {
7224 error_at (assoc.type_location,
7225 "%<_Generic%> specifies two compatible types");
7226 inform (iter->type_location, "compatible type is here");
7227 }
7228 }
7229 }
7230
7231 if (assoc.type == NULL_TREE)
7232 {
7233 if (!match_found)
7234 {
7235 matched_assoc = assoc;
7236 match_found = true;
7237 }
7238 }
7239 else if (comptypes (assoc.type, selector_type))
7240 {
7241 if (!match_found || matched_assoc.type == NULL_TREE)
7242 {
7243 matched_assoc = assoc;
7244 match_found = true;
7245 }
7246 else
7247 {
7248 error_at (assoc.type_location,
7249 "%<_Generic> selector matches multiple associations");
7250 inform (matched_assoc.type_location,
7251 "other match is here");
7252 }
7253 }
7254
7255 associations.safe_push (assoc);
7256
7257 if (c_parser_peek_token (parser)->type != CPP_COMMA)
7258 break;
7259 c_parser_consume_token (parser);
7260 }
7261
7262 associations.release ();
7263
7264 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
7265 {
7266 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7267 return error_expr;
7268 }
7269
7270 if (!match_found)
7271 {
7272 error_at (selector_loc, "%<_Generic%> selector of type %qT is not "
7273 "compatible with any association",
7274 selector_type);
7275 return error_expr;
7276 }
7277
7278 return matched_assoc.expression;
7279
7280 error_exit:
7281 associations.release ();
7282 return error_expr;
7283 }
7284
7285 /* Parse a postfix expression (C90 6.3.1-6.3.2, C99 6.5.1-6.5.2).
7286
7287 postfix-expression:
7288 primary-expression
7289 postfix-expression [ expression ]
7290 postfix-expression ( argument-expression-list[opt] )
7291 postfix-expression . identifier
7292 postfix-expression -> identifier
7293 postfix-expression ++
7294 postfix-expression --
7295 ( type-name ) { initializer-list }
7296 ( type-name ) { initializer-list , }
7297
7298 argument-expression-list:
7299 argument-expression
7300 argument-expression-list , argument-expression
7301
7302 primary-expression:
7303 identifier
7304 constant
7305 string-literal
7306 ( expression )
7307 generic-selection
7308
7309 GNU extensions:
7310
7311 primary-expression:
7312 __func__
7313 (treated as a keyword in GNU C)
7314 __FUNCTION__
7315 __PRETTY_FUNCTION__
7316 ( compound-statement )
7317 __builtin_va_arg ( assignment-expression , type-name )
7318 __builtin_offsetof ( type-name , offsetof-member-designator )
7319 __builtin_choose_expr ( assignment-expression ,
7320 assignment-expression ,
7321 assignment-expression )
7322 __builtin_types_compatible_p ( type-name , type-name )
7323 __builtin_complex ( assignment-expression , assignment-expression )
7324 __builtin_shuffle ( assignment-expression , assignment-expression )
7325 __builtin_shuffle ( assignment-expression ,
7326 assignment-expression ,
7327 assignment-expression, )
7328
7329 offsetof-member-designator:
7330 identifier
7331 offsetof-member-designator . identifier
7332 offsetof-member-designator [ expression ]
7333
7334 Objective-C:
7335
7336 primary-expression:
7337 [ objc-receiver objc-message-args ]
7338 @selector ( objc-selector-arg )
7339 @protocol ( identifier )
7340 @encode ( type-name )
7341 objc-string-literal
7342 Classname . identifier
7343 */
7344
7345 static struct c_expr
7346 c_parser_postfix_expression (c_parser *parser)
7347 {
7348 struct c_expr expr, e1;
7349 struct c_type_name *t1, *t2;
7350 location_t loc = c_parser_peek_token (parser)->location;;
7351 source_range tok_range = c_parser_peek_token (parser)->get_range ();
7352 expr.original_code = ERROR_MARK;
7353 expr.original_type = NULL;
7354 switch (c_parser_peek_token (parser)->type)
7355 {
7356 case CPP_NUMBER:
7357 expr.value = c_parser_peek_token (parser)->value;
7358 set_c_expr_source_range (&expr, tok_range);
7359 loc = c_parser_peek_token (parser)->location;
7360 c_parser_consume_token (parser);
7361 if (TREE_CODE (expr.value) == FIXED_CST
7362 && !targetm.fixed_point_supported_p ())
7363 {
7364 error_at (loc, "fixed-point types not supported for this target");
7365 expr.value = error_mark_node;
7366 }
7367 break;
7368 case CPP_CHAR:
7369 case CPP_CHAR16:
7370 case CPP_CHAR32:
7371 case CPP_WCHAR:
7372 expr.value = c_parser_peek_token (parser)->value;
7373 set_c_expr_source_range (&expr, tok_range);
7374 c_parser_consume_token (parser);
7375 break;
7376 case CPP_STRING:
7377 case CPP_STRING16:
7378 case CPP_STRING32:
7379 case CPP_WSTRING:
7380 case CPP_UTF8STRING:
7381 expr.value = c_parser_peek_token (parser)->value;
7382 set_c_expr_source_range (&expr, tok_range);
7383 expr.original_code = STRING_CST;
7384 c_parser_consume_token (parser);
7385 break;
7386 case CPP_OBJC_STRING:
7387 gcc_assert (c_dialect_objc ());
7388 expr.value
7389 = objc_build_string_object (c_parser_peek_token (parser)->value);
7390 set_c_expr_source_range (&expr, tok_range);
7391 c_parser_consume_token (parser);
7392 break;
7393 case CPP_NAME:
7394 switch (c_parser_peek_token (parser)->id_kind)
7395 {
7396 case C_ID_ID:
7397 {
7398 tree id = c_parser_peek_token (parser)->value;
7399 c_parser_consume_token (parser);
7400 expr.value = build_external_ref (loc, id,
7401 (c_parser_peek_token (parser)->type
7402 == CPP_OPEN_PAREN),
7403 &expr.original_type);
7404 set_c_expr_source_range (&expr, tok_range);
7405 break;
7406 }
7407 case C_ID_CLASSNAME:
7408 {
7409 /* Here we parse the Objective-C 2.0 Class.name dot
7410 syntax. */
7411 tree class_name = c_parser_peek_token (parser)->value;
7412 tree component;
7413 c_parser_consume_token (parser);
7414 gcc_assert (c_dialect_objc ());
7415 if (!c_parser_require (parser, CPP_DOT, "expected %<.%>"))
7416 {
7417 expr.value = error_mark_node;
7418 break;
7419 }
7420 if (c_parser_next_token_is_not (parser, CPP_NAME))
7421 {
7422 c_parser_error (parser, "expected identifier");
7423 expr.value = error_mark_node;
7424 break;
7425 }
7426 c_token *component_tok = c_parser_peek_token (parser);
7427 component = component_tok->value;
7428 location_t end_loc = component_tok->get_finish ();
7429 c_parser_consume_token (parser);
7430 expr.value = objc_build_class_component_ref (class_name,
7431 component);
7432 set_c_expr_source_range (&expr, loc, end_loc);
7433 break;
7434 }
7435 default:
7436 c_parser_error (parser, "expected expression");
7437 expr.value = error_mark_node;
7438 break;
7439 }
7440 break;
7441 case CPP_OPEN_PAREN:
7442 /* A parenthesized expression, statement expression or compound
7443 literal. */
7444 if (c_parser_peek_2nd_token (parser)->type == CPP_OPEN_BRACE)
7445 {
7446 /* A statement expression. */
7447 tree stmt;
7448 location_t brace_loc;
7449 c_parser_consume_token (parser);
7450 brace_loc = c_parser_peek_token (parser)->location;
7451 c_parser_consume_token (parser);
7452 if (!building_stmt_list_p ())
7453 {
7454 error_at (loc, "braced-group within expression allowed "
7455 "only inside a function");
7456 parser->error = true;
7457 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
7458 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7459 expr.value = error_mark_node;
7460 break;
7461 }
7462 stmt = c_begin_stmt_expr ();
7463 c_parser_compound_statement_nostart (parser);
7464 location_t close_loc = c_parser_peek_token (parser)->location;
7465 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7466 "expected %<)%>");
7467 pedwarn (loc, OPT_Wpedantic,
7468 "ISO C forbids braced-groups within expressions");
7469 expr.value = c_finish_stmt_expr (brace_loc, stmt);
7470 set_c_expr_source_range (&expr, loc, close_loc);
7471 mark_exp_read (expr.value);
7472 }
7473 else if (c_token_starts_typename (c_parser_peek_2nd_token (parser)))
7474 {
7475 /* A compound literal. ??? Can we actually get here rather
7476 than going directly to
7477 c_parser_postfix_expression_after_paren_type from
7478 elsewhere? */
7479 location_t loc;
7480 struct c_type_name *type_name;
7481 c_parser_consume_token (parser);
7482 loc = c_parser_peek_token (parser)->location;
7483 type_name = c_parser_type_name (parser);
7484 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7485 "expected %<)%>");
7486 if (type_name == NULL)
7487 {
7488 expr.value = error_mark_node;
7489 }
7490 else
7491 expr = c_parser_postfix_expression_after_paren_type (parser,
7492 type_name,
7493 loc);
7494 }
7495 else
7496 {
7497 /* A parenthesized expression. */
7498 location_t loc_open_paren = c_parser_peek_token (parser)->location;
7499 c_parser_consume_token (parser);
7500 expr = c_parser_expression (parser);
7501 if (TREE_CODE (expr.value) == MODIFY_EXPR)
7502 TREE_NO_WARNING (expr.value) = 1;
7503 if (expr.original_code != C_MAYBE_CONST_EXPR)
7504 expr.original_code = ERROR_MARK;
7505 /* Don't change EXPR.ORIGINAL_TYPE. */
7506 location_t loc_close_paren = c_parser_peek_token (parser)->location;
7507 set_c_expr_source_range (&expr, loc_open_paren, loc_close_paren);
7508 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7509 "expected %<)%>");
7510 }
7511 break;
7512 case CPP_KEYWORD:
7513 switch (c_parser_peek_token (parser)->keyword)
7514 {
7515 case RID_FUNCTION_NAME:
7516 pedwarn (loc, OPT_Wpedantic, "ISO C does not support "
7517 "%<__FUNCTION__%> predefined identifier");
7518 expr.value = fname_decl (loc,
7519 c_parser_peek_token (parser)->keyword,
7520 c_parser_peek_token (parser)->value);
7521 set_c_expr_source_range (&expr, loc, loc);
7522 c_parser_consume_token (parser);
7523 break;
7524 case RID_PRETTY_FUNCTION_NAME:
7525 pedwarn (loc, OPT_Wpedantic, "ISO C does not support "
7526 "%<__PRETTY_FUNCTION__%> predefined identifier");
7527 expr.value = fname_decl (loc,
7528 c_parser_peek_token (parser)->keyword,
7529 c_parser_peek_token (parser)->value);
7530 set_c_expr_source_range (&expr, loc, loc);
7531 c_parser_consume_token (parser);
7532 break;
7533 case RID_C99_FUNCTION_NAME:
7534 pedwarn_c90 (loc, OPT_Wpedantic, "ISO C90 does not support "
7535 "%<__func__%> predefined identifier");
7536 expr.value = fname_decl (loc,
7537 c_parser_peek_token (parser)->keyword,
7538 c_parser_peek_token (parser)->value);
7539 set_c_expr_source_range (&expr, loc, loc);
7540 c_parser_consume_token (parser);
7541 break;
7542 case RID_VA_ARG:
7543 {
7544 location_t start_loc = loc;
7545 c_parser_consume_token (parser);
7546 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7547 {
7548 expr.value = error_mark_node;
7549 break;
7550 }
7551 e1 = c_parser_expr_no_commas (parser, NULL);
7552 mark_exp_read (e1.value);
7553 e1.value = c_fully_fold (e1.value, false, NULL);
7554 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
7555 {
7556 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7557 expr.value = error_mark_node;
7558 break;
7559 }
7560 loc = c_parser_peek_token (parser)->location;
7561 t1 = c_parser_type_name (parser);
7562 location_t end_loc = c_parser_peek_token (parser)->get_finish ();
7563 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7564 "expected %<)%>");
7565 if (t1 == NULL)
7566 {
7567 expr.value = error_mark_node;
7568 }
7569 else
7570 {
7571 tree type_expr = NULL_TREE;
7572 expr.value = c_build_va_arg (start_loc, e1.value, loc,
7573 groktypename (t1, &type_expr, NULL));
7574 if (type_expr)
7575 {
7576 expr.value = build2 (C_MAYBE_CONST_EXPR,
7577 TREE_TYPE (expr.value), type_expr,
7578 expr.value);
7579 C_MAYBE_CONST_EXPR_NON_CONST (expr.value) = true;
7580 }
7581 set_c_expr_source_range (&expr, start_loc, end_loc);
7582 }
7583 }
7584 break;
7585 case RID_OFFSETOF:
7586 c_parser_consume_token (parser);
7587 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7588 {
7589 expr.value = error_mark_node;
7590 break;
7591 }
7592 t1 = c_parser_type_name (parser);
7593 if (t1 == NULL)
7594 parser->error = true;
7595 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
7596 gcc_assert (parser->error);
7597 if (parser->error)
7598 {
7599 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7600 expr.value = error_mark_node;
7601 break;
7602 }
7603
7604 {
7605 tree type = groktypename (t1, NULL, NULL);
7606 tree offsetof_ref;
7607 if (type == error_mark_node)
7608 offsetof_ref = error_mark_node;
7609 else
7610 {
7611 offsetof_ref = build1 (INDIRECT_REF, type, null_pointer_node);
7612 SET_EXPR_LOCATION (offsetof_ref, loc);
7613 }
7614 /* Parse the second argument to __builtin_offsetof. We
7615 must have one identifier, and beyond that we want to
7616 accept sub structure and sub array references. */
7617 if (c_parser_next_token_is (parser, CPP_NAME))
7618 {
7619 offsetof_ref = build_component_ref
7620 (loc, offsetof_ref, c_parser_peek_token (parser)->value);
7621 c_parser_consume_token (parser);
7622 while (c_parser_next_token_is (parser, CPP_DOT)
7623 || c_parser_next_token_is (parser,
7624 CPP_OPEN_SQUARE)
7625 || c_parser_next_token_is (parser,
7626 CPP_DEREF))
7627 {
7628 if (c_parser_next_token_is (parser, CPP_DEREF))
7629 {
7630 loc = c_parser_peek_token (parser)->location;
7631 offsetof_ref = build_array_ref (loc,
7632 offsetof_ref,
7633 integer_zero_node);
7634 goto do_dot;
7635 }
7636 else if (c_parser_next_token_is (parser, CPP_DOT))
7637 {
7638 do_dot:
7639 c_parser_consume_token (parser);
7640 if (c_parser_next_token_is_not (parser,
7641 CPP_NAME))
7642 {
7643 c_parser_error (parser, "expected identifier");
7644 break;
7645 }
7646 offsetof_ref = build_component_ref
7647 (loc, offsetof_ref,
7648 c_parser_peek_token (parser)->value);
7649 c_parser_consume_token (parser);
7650 }
7651 else
7652 {
7653 struct c_expr ce;
7654 tree idx;
7655 loc = c_parser_peek_token (parser)->location;
7656 c_parser_consume_token (parser);
7657 ce = c_parser_expression (parser);
7658 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
7659 idx = ce.value;
7660 idx = c_fully_fold (idx, false, NULL);
7661 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
7662 "expected %<]%>");
7663 offsetof_ref = build_array_ref (loc, offsetof_ref, idx);
7664 }
7665 }
7666 }
7667 else
7668 c_parser_error (parser, "expected identifier");
7669 location_t end_loc = c_parser_peek_token (parser)->get_finish ();
7670 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7671 "expected %<)%>");
7672 expr.value = fold_offsetof (offsetof_ref);
7673 set_c_expr_source_range (&expr, loc, end_loc);
7674 }
7675 break;
7676 case RID_CHOOSE_EXPR:
7677 {
7678 vec<c_expr_t, va_gc> *cexpr_list;
7679 c_expr_t *e1_p, *e2_p, *e3_p;
7680 tree c;
7681 location_t close_paren_loc;
7682
7683 c_parser_consume_token (parser);
7684 if (!c_parser_get_builtin_args (parser,
7685 "__builtin_choose_expr",
7686 &cexpr_list, true,
7687 &close_paren_loc))
7688 {
7689 expr.value = error_mark_node;
7690 break;
7691 }
7692
7693 if (vec_safe_length (cexpr_list) != 3)
7694 {
7695 error_at (loc, "wrong number of arguments to "
7696 "%<__builtin_choose_expr%>");
7697 expr.value = error_mark_node;
7698 break;
7699 }
7700
7701 e1_p = &(*cexpr_list)[0];
7702 e2_p = &(*cexpr_list)[1];
7703 e3_p = &(*cexpr_list)[2];
7704
7705 c = e1_p->value;
7706 mark_exp_read (e2_p->value);
7707 mark_exp_read (e3_p->value);
7708 if (TREE_CODE (c) != INTEGER_CST
7709 || !INTEGRAL_TYPE_P (TREE_TYPE (c)))
7710 error_at (loc,
7711 "first argument to %<__builtin_choose_expr%> not"
7712 " a constant");
7713 constant_expression_warning (c);
7714 expr = integer_zerop (c) ? *e3_p : *e2_p;
7715 set_c_expr_source_range (&expr, loc, close_paren_loc);
7716 break;
7717 }
7718 case RID_TYPES_COMPATIBLE_P:
7719 c_parser_consume_token (parser);
7720 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7721 {
7722 expr.value = error_mark_node;
7723 break;
7724 }
7725 t1 = c_parser_type_name (parser);
7726 if (t1 == NULL)
7727 {
7728 expr.value = error_mark_node;
7729 break;
7730 }
7731 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
7732 {
7733 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7734 expr.value = error_mark_node;
7735 break;
7736 }
7737 t2 = c_parser_type_name (parser);
7738 if (t2 == NULL)
7739 {
7740 expr.value = error_mark_node;
7741 break;
7742 }
7743 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7744 "expected %<)%>");
7745 {
7746 tree e1, e2;
7747 e1 = groktypename (t1, NULL, NULL);
7748 e2 = groktypename (t2, NULL, NULL);
7749 if (e1 == error_mark_node || e2 == error_mark_node)
7750 {
7751 expr.value = error_mark_node;
7752 break;
7753 }
7754
7755 e1 = TYPE_MAIN_VARIANT (e1);
7756 e2 = TYPE_MAIN_VARIANT (e2);
7757
7758 expr.value
7759 = comptypes (e1, e2) ? integer_one_node : integer_zero_node;
7760 }
7761 break;
7762 case RID_BUILTIN_CALL_WITH_STATIC_CHAIN:
7763 {
7764 vec<c_expr_t, va_gc> *cexpr_list;
7765 c_expr_t *e2_p;
7766 tree chain_value;
7767 location_t close_paren_loc;
7768
7769 c_parser_consume_token (parser);
7770 if (!c_parser_get_builtin_args (parser,
7771 "__builtin_call_with_static_chain",
7772 &cexpr_list, false,
7773 &close_paren_loc))
7774 {
7775 expr.value = error_mark_node;
7776 break;
7777 }
7778 if (vec_safe_length (cexpr_list) != 2)
7779 {
7780 error_at (loc, "wrong number of arguments to "
7781 "%<__builtin_call_with_static_chain%>");
7782 expr.value = error_mark_node;
7783 break;
7784 }
7785
7786 expr = (*cexpr_list)[0];
7787 e2_p = &(*cexpr_list)[1];
7788 *e2_p = convert_lvalue_to_rvalue (loc, *e2_p, true, true);
7789 chain_value = e2_p->value;
7790 mark_exp_read (chain_value);
7791
7792 if (TREE_CODE (expr.value) != CALL_EXPR)
7793 error_at (loc, "first argument to "
7794 "%<__builtin_call_with_static_chain%> "
7795 "must be a call expression");
7796 else if (TREE_CODE (TREE_TYPE (chain_value)) != POINTER_TYPE)
7797 error_at (loc, "second argument to "
7798 "%<__builtin_call_with_static_chain%> "
7799 "must be a pointer type");
7800 else
7801 CALL_EXPR_STATIC_CHAIN (expr.value) = chain_value;
7802 set_c_expr_source_range (&expr, loc, close_paren_loc);
7803 break;
7804 }
7805 case RID_BUILTIN_COMPLEX:
7806 {
7807 vec<c_expr_t, va_gc> *cexpr_list;
7808 c_expr_t *e1_p, *e2_p;
7809 location_t close_paren_loc;
7810
7811 c_parser_consume_token (parser);
7812 if (!c_parser_get_builtin_args (parser,
7813 "__builtin_complex",
7814 &cexpr_list, false,
7815 &close_paren_loc))
7816 {
7817 expr.value = error_mark_node;
7818 break;
7819 }
7820
7821 if (vec_safe_length (cexpr_list) != 2)
7822 {
7823 error_at (loc, "wrong number of arguments to "
7824 "%<__builtin_complex%>");
7825 expr.value = error_mark_node;
7826 break;
7827 }
7828
7829 e1_p = &(*cexpr_list)[0];
7830 e2_p = &(*cexpr_list)[1];
7831
7832 *e1_p = convert_lvalue_to_rvalue (loc, *e1_p, true, true);
7833 if (TREE_CODE (e1_p->value) == EXCESS_PRECISION_EXPR)
7834 e1_p->value = convert (TREE_TYPE (e1_p->value),
7835 TREE_OPERAND (e1_p->value, 0));
7836 *e2_p = convert_lvalue_to_rvalue (loc, *e2_p, true, true);
7837 if (TREE_CODE (e2_p->value) == EXCESS_PRECISION_EXPR)
7838 e2_p->value = convert (TREE_TYPE (e2_p->value),
7839 TREE_OPERAND (e2_p->value, 0));
7840 if (!SCALAR_FLOAT_TYPE_P (TREE_TYPE (e1_p->value))
7841 || DECIMAL_FLOAT_TYPE_P (TREE_TYPE (e1_p->value))
7842 || !SCALAR_FLOAT_TYPE_P (TREE_TYPE (e2_p->value))
7843 || DECIMAL_FLOAT_TYPE_P (TREE_TYPE (e2_p->value)))
7844 {
7845 error_at (loc, "%<__builtin_complex%> operand "
7846 "not of real binary floating-point type");
7847 expr.value = error_mark_node;
7848 break;
7849 }
7850 if (TYPE_MAIN_VARIANT (TREE_TYPE (e1_p->value))
7851 != TYPE_MAIN_VARIANT (TREE_TYPE (e2_p->value)))
7852 {
7853 error_at (loc,
7854 "%<__builtin_complex%> operands of different types");
7855 expr.value = error_mark_node;
7856 break;
7857 }
7858 pedwarn_c90 (loc, OPT_Wpedantic,
7859 "ISO C90 does not support complex types");
7860 expr.value = build2_loc (loc, COMPLEX_EXPR,
7861 build_complex_type
7862 (TYPE_MAIN_VARIANT
7863 (TREE_TYPE (e1_p->value))),
7864 e1_p->value, e2_p->value);
7865 set_c_expr_source_range (&expr, loc, close_paren_loc);
7866 break;
7867 }
7868 case RID_BUILTIN_SHUFFLE:
7869 {
7870 vec<c_expr_t, va_gc> *cexpr_list;
7871 unsigned int i;
7872 c_expr_t *p;
7873 location_t close_paren_loc;
7874
7875 c_parser_consume_token (parser);
7876 if (!c_parser_get_builtin_args (parser,
7877 "__builtin_shuffle",
7878 &cexpr_list, false,
7879 &close_paren_loc))
7880 {
7881 expr.value = error_mark_node;
7882 break;
7883 }
7884
7885 FOR_EACH_VEC_SAFE_ELT (cexpr_list, i, p)
7886 *p = convert_lvalue_to_rvalue (loc, *p, true, true);
7887
7888 if (vec_safe_length (cexpr_list) == 2)
7889 expr.value =
7890 c_build_vec_perm_expr
7891 (loc, (*cexpr_list)[0].value,
7892 NULL_TREE, (*cexpr_list)[1].value);
7893
7894 else if (vec_safe_length (cexpr_list) == 3)
7895 expr.value =
7896 c_build_vec_perm_expr
7897 (loc, (*cexpr_list)[0].value,
7898 (*cexpr_list)[1].value,
7899 (*cexpr_list)[2].value);
7900 else
7901 {
7902 error_at (loc, "wrong number of arguments to "
7903 "%<__builtin_shuffle%>");
7904 expr.value = error_mark_node;
7905 }
7906 set_c_expr_source_range (&expr, loc, close_paren_loc);
7907 break;
7908 }
7909 case RID_AT_SELECTOR:
7910 gcc_assert (c_dialect_objc ());
7911 c_parser_consume_token (parser);
7912 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7913 {
7914 expr.value = error_mark_node;
7915 break;
7916 }
7917 {
7918 tree sel = c_parser_objc_selector_arg (parser);
7919 location_t close_loc = c_parser_peek_token (parser)->location;
7920 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7921 "expected %<)%>");
7922 expr.value = objc_build_selector_expr (loc, sel);
7923 set_c_expr_source_range (&expr, loc, close_loc);
7924 }
7925 break;
7926 case RID_AT_PROTOCOL:
7927 gcc_assert (c_dialect_objc ());
7928 c_parser_consume_token (parser);
7929 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7930 {
7931 expr.value = error_mark_node;
7932 break;
7933 }
7934 if (c_parser_next_token_is_not (parser, CPP_NAME))
7935 {
7936 c_parser_error (parser, "expected identifier");
7937 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7938 expr.value = error_mark_node;
7939 break;
7940 }
7941 {
7942 tree id = c_parser_peek_token (parser)->value;
7943 c_parser_consume_token (parser);
7944 location_t close_loc = c_parser_peek_token (parser)->location;
7945 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7946 "expected %<)%>");
7947 expr.value = objc_build_protocol_expr (id);
7948 set_c_expr_source_range (&expr, loc, close_loc);
7949 }
7950 break;
7951 case RID_AT_ENCODE:
7952 /* Extension to support C-structures in the archiver. */
7953 gcc_assert (c_dialect_objc ());
7954 c_parser_consume_token (parser);
7955 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7956 {
7957 expr.value = error_mark_node;
7958 break;
7959 }
7960 t1 = c_parser_type_name (parser);
7961 if (t1 == NULL)
7962 {
7963 expr.value = error_mark_node;
7964 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7965 break;
7966 }
7967 {
7968 location_t close_loc = c_parser_peek_token (parser)->location;
7969 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7970 "expected %<)%>");
7971 tree type = groktypename (t1, NULL, NULL);
7972 expr.value = objc_build_encode_expr (type);
7973 set_c_expr_source_range (&expr, loc, close_loc);
7974 }
7975 break;
7976 case RID_GENERIC:
7977 expr = c_parser_generic_selection (parser);
7978 break;
7979 case RID_CILK_SPAWN:
7980 c_parser_consume_token (parser);
7981 if (!flag_cilkplus)
7982 {
7983 error_at (loc, "-fcilkplus must be enabled to use "
7984 "%<_Cilk_spawn%>");
7985 expr = c_parser_postfix_expression (parser);
7986 expr.value = error_mark_node;
7987 }
7988 else if (c_parser_peek_token (parser)->keyword == RID_CILK_SPAWN)
7989 {
7990 error_at (loc, "consecutive %<_Cilk_spawn%> keywords "
7991 "are not permitted");
7992 /* Now flush out all the _Cilk_spawns. */
7993 while (c_parser_peek_token (parser)->keyword == RID_CILK_SPAWN)
7994 c_parser_consume_token (parser);
7995 expr = c_parser_postfix_expression (parser);
7996 }
7997 else
7998 {
7999 expr = c_parser_postfix_expression (parser);
8000 expr.value = build_cilk_spawn (loc, expr.value);
8001 }
8002 break;
8003 default:
8004 c_parser_error (parser, "expected expression");
8005 expr.value = error_mark_node;
8006 break;
8007 }
8008 break;
8009 case CPP_OPEN_SQUARE:
8010 if (c_dialect_objc ())
8011 {
8012 tree receiver, args;
8013 c_parser_consume_token (parser);
8014 receiver = c_parser_objc_receiver (parser);
8015 args = c_parser_objc_message_args (parser);
8016 location_t close_loc = c_parser_peek_token (parser)->location;
8017 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
8018 "expected %<]%>");
8019 expr.value = objc_build_message_expr (receiver, args);
8020 set_c_expr_source_range (&expr, loc, close_loc);
8021 break;
8022 }
8023 /* Else fall through to report error. */
8024 default:
8025 c_parser_error (parser, "expected expression");
8026 expr.value = error_mark_node;
8027 break;
8028 }
8029 return c_parser_postfix_expression_after_primary
8030 (parser, EXPR_LOC_OR_LOC (expr.value, loc), expr);
8031 }
8032
8033 /* Parse a postfix expression after a parenthesized type name: the
8034 brace-enclosed initializer of a compound literal, possibly followed
8035 by some postfix operators. This is separate because it is not
8036 possible to tell until after the type name whether a cast
8037 expression has a cast or a compound literal, or whether the operand
8038 of sizeof is a parenthesized type name or starts with a compound
8039 literal. TYPE_LOC is the location where TYPE_NAME starts--the
8040 location of the first token after the parentheses around the type
8041 name. */
8042
8043 static struct c_expr
8044 c_parser_postfix_expression_after_paren_type (c_parser *parser,
8045 struct c_type_name *type_name,
8046 location_t type_loc)
8047 {
8048 tree type;
8049 struct c_expr init;
8050 bool non_const;
8051 struct c_expr expr;
8052 location_t start_loc;
8053 tree type_expr = NULL_TREE;
8054 bool type_expr_const = true;
8055 check_compound_literal_type (type_loc, type_name);
8056 start_init (NULL_TREE, NULL, 0);
8057 type = groktypename (type_name, &type_expr, &type_expr_const);
8058 start_loc = c_parser_peek_token (parser)->location;
8059 if (type != error_mark_node && C_TYPE_VARIABLE_SIZE (type))
8060 {
8061 error_at (type_loc, "compound literal has variable size");
8062 type = error_mark_node;
8063 }
8064 init = c_parser_braced_init (parser, type, false);
8065 finish_init ();
8066 maybe_warn_string_init (type_loc, type, init);
8067
8068 if (type != error_mark_node
8069 && !ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (type))
8070 && current_function_decl)
8071 {
8072 error ("compound literal qualified by address-space qualifier");
8073 type = error_mark_node;
8074 }
8075
8076 pedwarn_c90 (start_loc, OPT_Wpedantic, "ISO C90 forbids compound literals");
8077 non_const = ((init.value && TREE_CODE (init.value) == CONSTRUCTOR)
8078 ? CONSTRUCTOR_NON_CONST (init.value)
8079 : init.original_code == C_MAYBE_CONST_EXPR);
8080 non_const |= !type_expr_const;
8081 expr.value = build_compound_literal (start_loc, type, init.value, non_const);
8082 set_c_expr_source_range (&expr, init.src_range);
8083 expr.original_code = ERROR_MARK;
8084 expr.original_type = NULL;
8085 if (type != error_mark_node && type_expr)
8086 {
8087 if (TREE_CODE (expr.value) == C_MAYBE_CONST_EXPR)
8088 {
8089 gcc_assert (C_MAYBE_CONST_EXPR_PRE (expr.value) == NULL_TREE);
8090 C_MAYBE_CONST_EXPR_PRE (expr.value) = type_expr;
8091 }
8092 else
8093 {
8094 gcc_assert (!non_const);
8095 expr.value = build2 (C_MAYBE_CONST_EXPR, type,
8096 type_expr, expr.value);
8097 }
8098 }
8099 return c_parser_postfix_expression_after_primary (parser, start_loc, expr);
8100 }
8101
8102 /* Callback function for sizeof_pointer_memaccess_warning to compare
8103 types. */
8104
8105 static bool
8106 sizeof_ptr_memacc_comptypes (tree type1, tree type2)
8107 {
8108 return comptypes (type1, type2) == 1;
8109 }
8110
8111 /* Parse a postfix expression after the initial primary or compound
8112 literal; that is, parse a series of postfix operators.
8113
8114 EXPR_LOC is the location of the primary expression. */
8115
8116 static struct c_expr
8117 c_parser_postfix_expression_after_primary (c_parser *parser,
8118 location_t expr_loc,
8119 struct c_expr expr)
8120 {
8121 struct c_expr orig_expr;
8122 tree ident, idx;
8123 location_t sizeof_arg_loc[3];
8124 tree sizeof_arg[3];
8125 unsigned int literal_zero_mask;
8126 unsigned int i;
8127 vec<tree, va_gc> *exprlist;
8128 vec<tree, va_gc> *origtypes = NULL;
8129 vec<location_t> arg_loc = vNULL;
8130 location_t start;
8131 location_t finish;
8132
8133 while (true)
8134 {
8135 location_t op_loc = c_parser_peek_token (parser)->location;
8136 switch (c_parser_peek_token (parser)->type)
8137 {
8138 case CPP_OPEN_SQUARE:
8139 /* Array reference. */
8140 c_parser_consume_token (parser);
8141 if (flag_cilkplus
8142 && c_parser_peek_token (parser)->type == CPP_COLON)
8143 /* If we are here, then we have something like this:
8144 Array [ : ]
8145 */
8146 expr.value = c_parser_array_notation (expr_loc, parser, NULL_TREE,
8147 expr.value);
8148 else
8149 {
8150 idx = c_parser_expression (parser).value;
8151 /* Here we have 3 options:
8152 1. Array [EXPR] -- Normal Array call.
8153 2. Array [EXPR : EXPR] -- Array notation without stride.
8154 3. Array [EXPR : EXPR : EXPR] -- Array notation with stride.
8155
8156 For 1, we just handle it just like a normal array expression.
8157 For 2 and 3 we handle it like we handle array notations. The
8158 idx value we have above becomes the initial/start index.
8159 */
8160 if (flag_cilkplus
8161 && c_parser_peek_token (parser)->type == CPP_COLON)
8162 expr.value = c_parser_array_notation (expr_loc, parser, idx,
8163 expr.value);
8164 else
8165 {
8166 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
8167 "expected %<]%>");
8168 start = expr.get_start ();
8169 finish = parser->tokens_buf[0].location;
8170 expr.value = build_array_ref (op_loc, expr.value, idx);
8171 set_c_expr_source_range (&expr, start, finish);
8172 }
8173 }
8174 expr.original_code = ERROR_MARK;
8175 expr.original_type = NULL;
8176 break;
8177 case CPP_OPEN_PAREN:
8178 /* Function call. */
8179 c_parser_consume_token (parser);
8180 for (i = 0; i < 3; i++)
8181 {
8182 sizeof_arg[i] = NULL_TREE;
8183 sizeof_arg_loc[i] = UNKNOWN_LOCATION;
8184 }
8185 literal_zero_mask = 0;
8186 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
8187 exprlist = NULL;
8188 else
8189 exprlist = c_parser_expr_list (parser, true, false, &origtypes,
8190 sizeof_arg_loc, sizeof_arg,
8191 &arg_loc, &literal_zero_mask);
8192 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
8193 "expected %<)%>");
8194 orig_expr = expr;
8195 mark_exp_read (expr.value);
8196 if (warn_sizeof_pointer_memaccess)
8197 sizeof_pointer_memaccess_warning (sizeof_arg_loc,
8198 expr.value, exprlist,
8199 sizeof_arg,
8200 sizeof_ptr_memacc_comptypes);
8201 if (warn_memset_transposed_args
8202 && TREE_CODE (expr.value) == FUNCTION_DECL
8203 && DECL_BUILT_IN_CLASS (expr.value) == BUILT_IN_NORMAL
8204 && DECL_FUNCTION_CODE (expr.value) == BUILT_IN_MEMSET
8205 && vec_safe_length (exprlist) == 3
8206 && integer_zerop ((*exprlist)[2])
8207 && (literal_zero_mask & (1 << 2)) != 0
8208 && (!integer_zerop ((*exprlist)[1])
8209 || (literal_zero_mask & (1 << 1)) == 0))
8210 warning_at (expr_loc, OPT_Wmemset_transposed_args,
8211 "%<memset%> used with constant zero length parameter; "
8212 "this could be due to transposed parameters");
8213
8214 start = expr.get_start ();
8215 finish = parser->tokens_buf[0].get_finish ();
8216 expr.value
8217 = c_build_function_call_vec (expr_loc, arg_loc, expr.value,
8218 exprlist, origtypes);
8219 set_c_expr_source_range (&expr, start, finish);
8220
8221 expr.original_code = ERROR_MARK;
8222 if (TREE_CODE (expr.value) == INTEGER_CST
8223 && TREE_CODE (orig_expr.value) == FUNCTION_DECL
8224 && DECL_BUILT_IN_CLASS (orig_expr.value) == BUILT_IN_NORMAL
8225 && DECL_FUNCTION_CODE (orig_expr.value) == BUILT_IN_CONSTANT_P)
8226 expr.original_code = C_MAYBE_CONST_EXPR;
8227 expr.original_type = NULL;
8228 if (exprlist)
8229 {
8230 release_tree_vector (exprlist);
8231 release_tree_vector (origtypes);
8232 }
8233 arg_loc.release ();
8234 break;
8235 case CPP_DOT:
8236 /* Structure element reference. */
8237 c_parser_consume_token (parser);
8238 expr = default_function_array_conversion (expr_loc, expr);
8239 if (c_parser_next_token_is (parser, CPP_NAME))
8240 ident = c_parser_peek_token (parser)->value;
8241 else
8242 {
8243 c_parser_error (parser, "expected identifier");
8244 expr.value = error_mark_node;
8245 expr.original_code = ERROR_MARK;
8246 expr.original_type = NULL;
8247 return expr;
8248 }
8249 start = expr.get_start ();
8250 finish = c_parser_peek_token (parser)->get_finish ();
8251 c_parser_consume_token (parser);
8252 expr.value = build_component_ref (op_loc, expr.value, ident);
8253 set_c_expr_source_range (&expr, start, finish);
8254 expr.original_code = ERROR_MARK;
8255 if (TREE_CODE (expr.value) != COMPONENT_REF)
8256 expr.original_type = NULL;
8257 else
8258 {
8259 /* Remember the original type of a bitfield. */
8260 tree field = TREE_OPERAND (expr.value, 1);
8261 if (TREE_CODE (field) != FIELD_DECL)
8262 expr.original_type = NULL;
8263 else
8264 expr.original_type = DECL_BIT_FIELD_TYPE (field);
8265 }
8266 break;
8267 case CPP_DEREF:
8268 /* Structure element reference. */
8269 c_parser_consume_token (parser);
8270 expr = convert_lvalue_to_rvalue (expr_loc, expr, true, false);
8271 if (c_parser_next_token_is (parser, CPP_NAME))
8272 ident = c_parser_peek_token (parser)->value;
8273 else
8274 {
8275 c_parser_error (parser, "expected identifier");
8276 expr.value = error_mark_node;
8277 expr.original_code = ERROR_MARK;
8278 expr.original_type = NULL;
8279 return expr;
8280 }
8281 start = expr.get_start ();
8282 finish = c_parser_peek_token (parser)->get_finish ();
8283 c_parser_consume_token (parser);
8284 expr.value = build_component_ref (op_loc,
8285 build_indirect_ref (op_loc,
8286 expr.value,
8287 RO_ARROW),
8288 ident);
8289 set_c_expr_source_range (&expr, start, finish);
8290 expr.original_code = ERROR_MARK;
8291 if (TREE_CODE (expr.value) != COMPONENT_REF)
8292 expr.original_type = NULL;
8293 else
8294 {
8295 /* Remember the original type of a bitfield. */
8296 tree field = TREE_OPERAND (expr.value, 1);
8297 if (TREE_CODE (field) != FIELD_DECL)
8298 expr.original_type = NULL;
8299 else
8300 expr.original_type = DECL_BIT_FIELD_TYPE (field);
8301 }
8302 break;
8303 case CPP_PLUS_PLUS:
8304 /* Postincrement. */
8305 start = expr.get_start ();
8306 finish = c_parser_peek_token (parser)->get_finish ();
8307 c_parser_consume_token (parser);
8308 /* If the expressions have array notations, we expand them. */
8309 if (flag_cilkplus
8310 && TREE_CODE (expr.value) == ARRAY_NOTATION_REF)
8311 expr = fix_array_notation_expr (expr_loc, POSTINCREMENT_EXPR, expr);
8312 else
8313 {
8314 expr = default_function_array_read_conversion (expr_loc, expr);
8315 expr.value = build_unary_op (op_loc,
8316 POSTINCREMENT_EXPR, expr.value, 0);
8317 }
8318 set_c_expr_source_range (&expr, start, finish);
8319 expr.original_code = ERROR_MARK;
8320 expr.original_type = NULL;
8321 break;
8322 case CPP_MINUS_MINUS:
8323 /* Postdecrement. */
8324 start = expr.get_start ();
8325 finish = c_parser_peek_token (parser)->get_finish ();
8326 c_parser_consume_token (parser);
8327 /* If the expressions have array notations, we expand them. */
8328 if (flag_cilkplus
8329 && TREE_CODE (expr.value) == ARRAY_NOTATION_REF)
8330 expr = fix_array_notation_expr (expr_loc, POSTDECREMENT_EXPR, expr);
8331 else
8332 {
8333 expr = default_function_array_read_conversion (expr_loc, expr);
8334 expr.value = build_unary_op (op_loc,
8335 POSTDECREMENT_EXPR, expr.value, 0);
8336 }
8337 set_c_expr_source_range (&expr, start, finish);
8338 expr.original_code = ERROR_MARK;
8339 expr.original_type = NULL;
8340 break;
8341 default:
8342 return expr;
8343 }
8344 }
8345 }
8346
8347 /* Parse an expression (C90 6.3.17, C99 6.5.17).
8348
8349 expression:
8350 assignment-expression
8351 expression , assignment-expression
8352 */
8353
8354 static struct c_expr
8355 c_parser_expression (c_parser *parser)
8356 {
8357 location_t tloc = c_parser_peek_token (parser)->location;
8358 struct c_expr expr;
8359 expr = c_parser_expr_no_commas (parser, NULL);
8360 if (c_parser_next_token_is (parser, CPP_COMMA))
8361 expr = convert_lvalue_to_rvalue (tloc, expr, true, false);
8362 while (c_parser_next_token_is (parser, CPP_COMMA))
8363 {
8364 struct c_expr next;
8365 tree lhsval;
8366 location_t loc = c_parser_peek_token (parser)->location;
8367 location_t expr_loc;
8368 c_parser_consume_token (parser);
8369 expr_loc = c_parser_peek_token (parser)->location;
8370 lhsval = expr.value;
8371 while (TREE_CODE (lhsval) == COMPOUND_EXPR)
8372 lhsval = TREE_OPERAND (lhsval, 1);
8373 if (DECL_P (lhsval) || handled_component_p (lhsval))
8374 mark_exp_read (lhsval);
8375 next = c_parser_expr_no_commas (parser, NULL);
8376 next = convert_lvalue_to_rvalue (expr_loc, next, true, false);
8377 expr.value = build_compound_expr (loc, expr.value, next.value);
8378 expr.original_code = COMPOUND_EXPR;
8379 expr.original_type = next.original_type;
8380 }
8381 return expr;
8382 }
8383
8384 /* Parse an expression and convert functions or arrays to pointers and
8385 lvalues to rvalues. */
8386
8387 static struct c_expr
8388 c_parser_expression_conv (c_parser *parser)
8389 {
8390 struct c_expr expr;
8391 location_t loc = c_parser_peek_token (parser)->location;
8392 expr = c_parser_expression (parser);
8393 expr = convert_lvalue_to_rvalue (loc, expr, true, false);
8394 return expr;
8395 }
8396
8397 /* Helper function of c_parser_expr_list. Check if IDXth (0 based)
8398 argument is a literal zero alone and if so, set it in literal_zero_mask. */
8399
8400 static inline void
8401 c_parser_check_literal_zero (c_parser *parser, unsigned *literal_zero_mask,
8402 unsigned int idx)
8403 {
8404 if (idx >= HOST_BITS_PER_INT)
8405 return;
8406
8407 c_token *tok = c_parser_peek_token (parser);
8408 switch (tok->type)
8409 {
8410 case CPP_NUMBER:
8411 case CPP_CHAR:
8412 case CPP_WCHAR:
8413 case CPP_CHAR16:
8414 case CPP_CHAR32:
8415 /* If a parameter is literal zero alone, remember it
8416 for -Wmemset-transposed-args warning. */
8417 if (integer_zerop (tok->value)
8418 && !TREE_OVERFLOW (tok->value)
8419 && (c_parser_peek_2nd_token (parser)->type == CPP_COMMA
8420 || c_parser_peek_2nd_token (parser)->type == CPP_CLOSE_PAREN))
8421 *literal_zero_mask |= 1U << idx;
8422 default:
8423 break;
8424 }
8425 }
8426
8427 /* Parse a non-empty list of expressions. If CONVERT_P, convert
8428 functions and arrays to pointers and lvalues to rvalues. If
8429 FOLD_P, fold the expressions. If LOCATIONS is non-NULL, save the
8430 locations of function arguments into this vector.
8431
8432 nonempty-expr-list:
8433 assignment-expression
8434 nonempty-expr-list , assignment-expression
8435 */
8436
8437 static vec<tree, va_gc> *
8438 c_parser_expr_list (c_parser *parser, bool convert_p, bool fold_p,
8439 vec<tree, va_gc> **p_orig_types,
8440 location_t *sizeof_arg_loc, tree *sizeof_arg,
8441 vec<location_t> *locations,
8442 unsigned int *literal_zero_mask)
8443 {
8444 vec<tree, va_gc> *ret;
8445 vec<tree, va_gc> *orig_types;
8446 struct c_expr expr;
8447 location_t loc = c_parser_peek_token (parser)->location;
8448 location_t cur_sizeof_arg_loc = UNKNOWN_LOCATION;
8449 unsigned int idx = 0;
8450
8451 ret = make_tree_vector ();
8452 if (p_orig_types == NULL)
8453 orig_types = NULL;
8454 else
8455 orig_types = make_tree_vector ();
8456
8457 if (sizeof_arg != NULL
8458 && c_parser_next_token_is_keyword (parser, RID_SIZEOF))
8459 cur_sizeof_arg_loc = c_parser_peek_2nd_token (parser)->location;
8460 if (literal_zero_mask)
8461 c_parser_check_literal_zero (parser, literal_zero_mask, 0);
8462 expr = c_parser_expr_no_commas (parser, NULL);
8463 if (convert_p)
8464 expr = convert_lvalue_to_rvalue (loc, expr, true, true);
8465 if (fold_p)
8466 expr.value = c_fully_fold (expr.value, false, NULL);
8467 ret->quick_push (expr.value);
8468 if (orig_types)
8469 orig_types->quick_push (expr.original_type);
8470 if (locations)
8471 locations->safe_push (loc);
8472 if (sizeof_arg != NULL
8473 && cur_sizeof_arg_loc != UNKNOWN_LOCATION
8474 && expr.original_code == SIZEOF_EXPR)
8475 {
8476 sizeof_arg[0] = c_last_sizeof_arg;
8477 sizeof_arg_loc[0] = cur_sizeof_arg_loc;
8478 }
8479 while (c_parser_next_token_is (parser, CPP_COMMA))
8480 {
8481 c_parser_consume_token (parser);
8482 loc = c_parser_peek_token (parser)->location;
8483 if (sizeof_arg != NULL
8484 && c_parser_next_token_is_keyword (parser, RID_SIZEOF))
8485 cur_sizeof_arg_loc = c_parser_peek_2nd_token (parser)->location;
8486 else
8487 cur_sizeof_arg_loc = UNKNOWN_LOCATION;
8488 if (literal_zero_mask)
8489 c_parser_check_literal_zero (parser, literal_zero_mask, idx + 1);
8490 expr = c_parser_expr_no_commas (parser, NULL);
8491 if (convert_p)
8492 expr = convert_lvalue_to_rvalue (loc, expr, true, true);
8493 if (fold_p)
8494 expr.value = c_fully_fold (expr.value, false, NULL);
8495 vec_safe_push (ret, expr.value);
8496 if (orig_types)
8497 vec_safe_push (orig_types, expr.original_type);
8498 if (locations)
8499 locations->safe_push (loc);
8500 if (++idx < 3
8501 && sizeof_arg != NULL
8502 && cur_sizeof_arg_loc != UNKNOWN_LOCATION
8503 && expr.original_code == SIZEOF_EXPR)
8504 {
8505 sizeof_arg[idx] = c_last_sizeof_arg;
8506 sizeof_arg_loc[idx] = cur_sizeof_arg_loc;
8507 }
8508 }
8509 if (orig_types)
8510 *p_orig_types = orig_types;
8511 return ret;
8512 }
8513 \f
8514 /* Parse Objective-C-specific constructs. */
8515
8516 /* Parse an objc-class-definition.
8517
8518 objc-class-definition:
8519 @interface identifier objc-superclass[opt] objc-protocol-refs[opt]
8520 objc-class-instance-variables[opt] objc-methodprotolist @end
8521 @implementation identifier objc-superclass[opt]
8522 objc-class-instance-variables[opt]
8523 @interface identifier ( identifier ) objc-protocol-refs[opt]
8524 objc-methodprotolist @end
8525 @interface identifier ( ) objc-protocol-refs[opt]
8526 objc-methodprotolist @end
8527 @implementation identifier ( identifier )
8528
8529 objc-superclass:
8530 : identifier
8531
8532 "@interface identifier (" must start "@interface identifier (
8533 identifier ) ...": objc-methodprotolist in the first production may
8534 not start with a parenthesized identifier as a declarator of a data
8535 definition with no declaration specifiers if the objc-superclass,
8536 objc-protocol-refs and objc-class-instance-variables are omitted. */
8537
8538 static void
8539 c_parser_objc_class_definition (c_parser *parser, tree attributes)
8540 {
8541 bool iface_p;
8542 tree id1;
8543 tree superclass;
8544 if (c_parser_next_token_is_keyword (parser, RID_AT_INTERFACE))
8545 iface_p = true;
8546 else if (c_parser_next_token_is_keyword (parser, RID_AT_IMPLEMENTATION))
8547 iface_p = false;
8548 else
8549 gcc_unreachable ();
8550
8551 c_parser_consume_token (parser);
8552 if (c_parser_next_token_is_not (parser, CPP_NAME))
8553 {
8554 c_parser_error (parser, "expected identifier");
8555 return;
8556 }
8557 id1 = c_parser_peek_token (parser)->value;
8558 c_parser_consume_token (parser);
8559 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
8560 {
8561 /* We have a category or class extension. */
8562 tree id2;
8563 tree proto = NULL_TREE;
8564 c_parser_consume_token (parser);
8565 if (c_parser_next_token_is_not (parser, CPP_NAME))
8566 {
8567 if (iface_p && c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
8568 {
8569 /* We have a class extension. */
8570 id2 = NULL_TREE;
8571 }
8572 else
8573 {
8574 c_parser_error (parser, "expected identifier or %<)%>");
8575 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
8576 return;
8577 }
8578 }
8579 else
8580 {
8581 id2 = c_parser_peek_token (parser)->value;
8582 c_parser_consume_token (parser);
8583 }
8584 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
8585 if (!iface_p)
8586 {
8587 objc_start_category_implementation (id1, id2);
8588 return;
8589 }
8590 if (c_parser_next_token_is (parser, CPP_LESS))
8591 proto = c_parser_objc_protocol_refs (parser);
8592 objc_start_category_interface (id1, id2, proto, attributes);
8593 c_parser_objc_methodprotolist (parser);
8594 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
8595 objc_finish_interface ();
8596 return;
8597 }
8598 if (c_parser_next_token_is (parser, CPP_COLON))
8599 {
8600 c_parser_consume_token (parser);
8601 if (c_parser_next_token_is_not (parser, CPP_NAME))
8602 {
8603 c_parser_error (parser, "expected identifier");
8604 return;
8605 }
8606 superclass = c_parser_peek_token (parser)->value;
8607 c_parser_consume_token (parser);
8608 }
8609 else
8610 superclass = NULL_TREE;
8611 if (iface_p)
8612 {
8613 tree proto = NULL_TREE;
8614 if (c_parser_next_token_is (parser, CPP_LESS))
8615 proto = c_parser_objc_protocol_refs (parser);
8616 objc_start_class_interface (id1, superclass, proto, attributes);
8617 }
8618 else
8619 objc_start_class_implementation (id1, superclass);
8620 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
8621 c_parser_objc_class_instance_variables (parser);
8622 if (iface_p)
8623 {
8624 objc_continue_interface ();
8625 c_parser_objc_methodprotolist (parser);
8626 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
8627 objc_finish_interface ();
8628 }
8629 else
8630 {
8631 objc_continue_implementation ();
8632 return;
8633 }
8634 }
8635
8636 /* Parse objc-class-instance-variables.
8637
8638 objc-class-instance-variables:
8639 { objc-instance-variable-decl-list[opt] }
8640
8641 objc-instance-variable-decl-list:
8642 objc-visibility-spec
8643 objc-instance-variable-decl ;
8644 ;
8645 objc-instance-variable-decl-list objc-visibility-spec
8646 objc-instance-variable-decl-list objc-instance-variable-decl ;
8647 objc-instance-variable-decl-list ;
8648
8649 objc-visibility-spec:
8650 @private
8651 @protected
8652 @public
8653
8654 objc-instance-variable-decl:
8655 struct-declaration
8656 */
8657
8658 static void
8659 c_parser_objc_class_instance_variables (c_parser *parser)
8660 {
8661 gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE));
8662 c_parser_consume_token (parser);
8663 while (c_parser_next_token_is_not (parser, CPP_EOF))
8664 {
8665 tree decls;
8666 /* Parse any stray semicolon. */
8667 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
8668 {
8669 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
8670 "extra semicolon");
8671 c_parser_consume_token (parser);
8672 continue;
8673 }
8674 /* Stop if at the end of the instance variables. */
8675 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
8676 {
8677 c_parser_consume_token (parser);
8678 break;
8679 }
8680 /* Parse any objc-visibility-spec. */
8681 if (c_parser_next_token_is_keyword (parser, RID_AT_PRIVATE))
8682 {
8683 c_parser_consume_token (parser);
8684 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
8685 continue;
8686 }
8687 else if (c_parser_next_token_is_keyword (parser, RID_AT_PROTECTED))
8688 {
8689 c_parser_consume_token (parser);
8690 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
8691 continue;
8692 }
8693 else if (c_parser_next_token_is_keyword (parser, RID_AT_PUBLIC))
8694 {
8695 c_parser_consume_token (parser);
8696 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
8697 continue;
8698 }
8699 else if (c_parser_next_token_is_keyword (parser, RID_AT_PACKAGE))
8700 {
8701 c_parser_consume_token (parser);
8702 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
8703 continue;
8704 }
8705 else if (c_parser_next_token_is (parser, CPP_PRAGMA))
8706 {
8707 c_parser_pragma (parser, pragma_external);
8708 continue;
8709 }
8710
8711 /* Parse some comma-separated declarations. */
8712 decls = c_parser_struct_declaration (parser);
8713 if (decls == NULL)
8714 {
8715 /* There is a syntax error. We want to skip the offending
8716 tokens up to the next ';' (included) or '}'
8717 (excluded). */
8718
8719 /* First, skip manually a ')' or ']'. This is because they
8720 reduce the nesting level, so c_parser_skip_until_found()
8721 wouldn't be able to skip past them. */
8722 c_token *token = c_parser_peek_token (parser);
8723 if (token->type == CPP_CLOSE_PAREN || token->type == CPP_CLOSE_SQUARE)
8724 c_parser_consume_token (parser);
8725
8726 /* Then, do the standard skipping. */
8727 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
8728
8729 /* We hopefully recovered. Start normal parsing again. */
8730 parser->error = false;
8731 continue;
8732 }
8733 else
8734 {
8735 /* Comma-separated instance variables are chained together
8736 in reverse order; add them one by one. */
8737 tree ivar = nreverse (decls);
8738 for (; ivar; ivar = DECL_CHAIN (ivar))
8739 objc_add_instance_variable (copy_node (ivar));
8740 }
8741 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
8742 }
8743 }
8744
8745 /* Parse an objc-class-declaration.
8746
8747 objc-class-declaration:
8748 @class identifier-list ;
8749 */
8750
8751 static void
8752 c_parser_objc_class_declaration (c_parser *parser)
8753 {
8754 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_CLASS));
8755 c_parser_consume_token (parser);
8756 /* Any identifiers, including those declared as type names, are OK
8757 here. */
8758 while (true)
8759 {
8760 tree id;
8761 if (c_parser_next_token_is_not (parser, CPP_NAME))
8762 {
8763 c_parser_error (parser, "expected identifier");
8764 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
8765 parser->error = false;
8766 return;
8767 }
8768 id = c_parser_peek_token (parser)->value;
8769 objc_declare_class (id);
8770 c_parser_consume_token (parser);
8771 if (c_parser_next_token_is (parser, CPP_COMMA))
8772 c_parser_consume_token (parser);
8773 else
8774 break;
8775 }
8776 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
8777 }
8778
8779 /* Parse an objc-alias-declaration.
8780
8781 objc-alias-declaration:
8782 @compatibility_alias identifier identifier ;
8783 */
8784
8785 static void
8786 c_parser_objc_alias_declaration (c_parser *parser)
8787 {
8788 tree id1, id2;
8789 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_ALIAS));
8790 c_parser_consume_token (parser);
8791 if (c_parser_next_token_is_not (parser, CPP_NAME))
8792 {
8793 c_parser_error (parser, "expected identifier");
8794 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
8795 return;
8796 }
8797 id1 = c_parser_peek_token (parser)->value;
8798 c_parser_consume_token (parser);
8799 if (c_parser_next_token_is_not (parser, CPP_NAME))
8800 {
8801 c_parser_error (parser, "expected identifier");
8802 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
8803 return;
8804 }
8805 id2 = c_parser_peek_token (parser)->value;
8806 c_parser_consume_token (parser);
8807 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
8808 objc_declare_alias (id1, id2);
8809 }
8810
8811 /* Parse an objc-protocol-definition.
8812
8813 objc-protocol-definition:
8814 @protocol identifier objc-protocol-refs[opt] objc-methodprotolist @end
8815 @protocol identifier-list ;
8816
8817 "@protocol identifier ;" should be resolved as "@protocol
8818 identifier-list ;": objc-methodprotolist may not start with a
8819 semicolon in the first alternative if objc-protocol-refs are
8820 omitted. */
8821
8822 static void
8823 c_parser_objc_protocol_definition (c_parser *parser, tree attributes)
8824 {
8825 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_PROTOCOL));
8826
8827 c_parser_consume_token (parser);
8828 if (c_parser_next_token_is_not (parser, CPP_NAME))
8829 {
8830 c_parser_error (parser, "expected identifier");
8831 return;
8832 }
8833 if (c_parser_peek_2nd_token (parser)->type == CPP_COMMA
8834 || c_parser_peek_2nd_token (parser)->type == CPP_SEMICOLON)
8835 {
8836 /* Any identifiers, including those declared as type names, are
8837 OK here. */
8838 while (true)
8839 {
8840 tree id;
8841 if (c_parser_next_token_is_not (parser, CPP_NAME))
8842 {
8843 c_parser_error (parser, "expected identifier");
8844 break;
8845 }
8846 id = c_parser_peek_token (parser)->value;
8847 objc_declare_protocol (id, attributes);
8848 c_parser_consume_token (parser);
8849 if (c_parser_next_token_is (parser, CPP_COMMA))
8850 c_parser_consume_token (parser);
8851 else
8852 break;
8853 }
8854 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
8855 }
8856 else
8857 {
8858 tree id = c_parser_peek_token (parser)->value;
8859 tree proto = NULL_TREE;
8860 c_parser_consume_token (parser);
8861 if (c_parser_next_token_is (parser, CPP_LESS))
8862 proto = c_parser_objc_protocol_refs (parser);
8863 parser->objc_pq_context = true;
8864 objc_start_protocol (id, proto, attributes);
8865 c_parser_objc_methodprotolist (parser);
8866 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
8867 parser->objc_pq_context = false;
8868 objc_finish_interface ();
8869 }
8870 }
8871
8872 /* Parse an objc-method-type.
8873
8874 objc-method-type:
8875 +
8876 -
8877
8878 Return true if it is a class method (+) and false if it is
8879 an instance method (-).
8880 */
8881 static inline bool
8882 c_parser_objc_method_type (c_parser *parser)
8883 {
8884 switch (c_parser_peek_token (parser)->type)
8885 {
8886 case CPP_PLUS:
8887 c_parser_consume_token (parser);
8888 return true;
8889 case CPP_MINUS:
8890 c_parser_consume_token (parser);
8891 return false;
8892 default:
8893 gcc_unreachable ();
8894 }
8895 }
8896
8897 /* Parse an objc-method-definition.
8898
8899 objc-method-definition:
8900 objc-method-type objc-method-decl ;[opt] compound-statement
8901 */
8902
8903 static void
8904 c_parser_objc_method_definition (c_parser *parser)
8905 {
8906 bool is_class_method = c_parser_objc_method_type (parser);
8907 tree decl, attributes = NULL_TREE, expr = NULL_TREE;
8908 parser->objc_pq_context = true;
8909 decl = c_parser_objc_method_decl (parser, is_class_method, &attributes,
8910 &expr);
8911 if (decl == error_mark_node)
8912 return; /* Bail here. */
8913
8914 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
8915 {
8916 c_parser_consume_token (parser);
8917 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
8918 "extra semicolon in method definition specified");
8919 }
8920
8921 if (!c_parser_next_token_is (parser, CPP_OPEN_BRACE))
8922 {
8923 c_parser_error (parser, "expected %<{%>");
8924 return;
8925 }
8926
8927 parser->objc_pq_context = false;
8928 if (objc_start_method_definition (is_class_method, decl, attributes, expr))
8929 {
8930 add_stmt (c_parser_compound_statement (parser));
8931 objc_finish_method_definition (current_function_decl);
8932 }
8933 else
8934 {
8935 /* This code is executed when we find a method definition
8936 outside of an @implementation context (or invalid for other
8937 reasons). Parse the method (to keep going) but do not emit
8938 any code.
8939 */
8940 c_parser_compound_statement (parser);
8941 }
8942 }
8943
8944 /* Parse an objc-methodprotolist.
8945
8946 objc-methodprotolist:
8947 empty
8948 objc-methodprotolist objc-methodproto
8949 objc-methodprotolist declaration
8950 objc-methodprotolist ;
8951 @optional
8952 @required
8953
8954 The declaration is a data definition, which may be missing
8955 declaration specifiers under the same rules and diagnostics as
8956 other data definitions outside functions, and the stray semicolon
8957 is diagnosed the same way as a stray semicolon outside a
8958 function. */
8959
8960 static void
8961 c_parser_objc_methodprotolist (c_parser *parser)
8962 {
8963 while (true)
8964 {
8965 /* The list is terminated by @end. */
8966 switch (c_parser_peek_token (parser)->type)
8967 {
8968 case CPP_SEMICOLON:
8969 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
8970 "ISO C does not allow extra %<;%> outside of a function");
8971 c_parser_consume_token (parser);
8972 break;
8973 case CPP_PLUS:
8974 case CPP_MINUS:
8975 c_parser_objc_methodproto (parser);
8976 break;
8977 case CPP_PRAGMA:
8978 c_parser_pragma (parser, pragma_external);
8979 break;
8980 case CPP_EOF:
8981 return;
8982 default:
8983 if (c_parser_next_token_is_keyword (parser, RID_AT_END))
8984 return;
8985 else if (c_parser_next_token_is_keyword (parser, RID_AT_PROPERTY))
8986 c_parser_objc_at_property_declaration (parser);
8987 else if (c_parser_next_token_is_keyword (parser, RID_AT_OPTIONAL))
8988 {
8989 objc_set_method_opt (true);
8990 c_parser_consume_token (parser);
8991 }
8992 else if (c_parser_next_token_is_keyword (parser, RID_AT_REQUIRED))
8993 {
8994 objc_set_method_opt (false);
8995 c_parser_consume_token (parser);
8996 }
8997 else
8998 c_parser_declaration_or_fndef (parser, false, false, true,
8999 false, true, NULL, vNULL);
9000 break;
9001 }
9002 }
9003 }
9004
9005 /* Parse an objc-methodproto.
9006
9007 objc-methodproto:
9008 objc-method-type objc-method-decl ;
9009 */
9010
9011 static void
9012 c_parser_objc_methodproto (c_parser *parser)
9013 {
9014 bool is_class_method = c_parser_objc_method_type (parser);
9015 tree decl, attributes = NULL_TREE;
9016
9017 /* Remember protocol qualifiers in prototypes. */
9018 parser->objc_pq_context = true;
9019 decl = c_parser_objc_method_decl (parser, is_class_method, &attributes,
9020 NULL);
9021 /* Forget protocol qualifiers now. */
9022 parser->objc_pq_context = false;
9023
9024 /* Do not allow the presence of attributes to hide an erroneous
9025 method implementation in the interface section. */
9026 if (!c_parser_next_token_is (parser, CPP_SEMICOLON))
9027 {
9028 c_parser_error (parser, "expected %<;%>");
9029 return;
9030 }
9031
9032 if (decl != error_mark_node)
9033 objc_add_method_declaration (is_class_method, decl, attributes);
9034
9035 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9036 }
9037
9038 /* If we are at a position that method attributes may be present, check that
9039 there are not any parsed already (a syntax error) and then collect any
9040 specified at the current location. Finally, if new attributes were present,
9041 check that the next token is legal ( ';' for decls and '{' for defs). */
9042
9043 static bool
9044 c_parser_objc_maybe_method_attributes (c_parser* parser, tree* attributes)
9045 {
9046 bool bad = false;
9047 if (*attributes)
9048 {
9049 c_parser_error (parser,
9050 "method attributes must be specified at the end only");
9051 *attributes = NULL_TREE;
9052 bad = true;
9053 }
9054
9055 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
9056 *attributes = c_parser_attributes (parser);
9057
9058 /* If there were no attributes here, just report any earlier error. */
9059 if (*attributes == NULL_TREE || bad)
9060 return bad;
9061
9062 /* If the attributes are followed by a ; or {, then just report any earlier
9063 error. */
9064 if (c_parser_next_token_is (parser, CPP_SEMICOLON)
9065 || c_parser_next_token_is (parser, CPP_OPEN_BRACE))
9066 return bad;
9067
9068 /* We've got attributes, but not at the end. */
9069 c_parser_error (parser,
9070 "expected %<;%> or %<{%> after method attribute definition");
9071 return true;
9072 }
9073
9074 /* Parse an objc-method-decl.
9075
9076 objc-method-decl:
9077 ( objc-type-name ) objc-selector
9078 objc-selector
9079 ( objc-type-name ) objc-keyword-selector objc-optparmlist
9080 objc-keyword-selector objc-optparmlist
9081 attributes
9082
9083 objc-keyword-selector:
9084 objc-keyword-decl
9085 objc-keyword-selector objc-keyword-decl
9086
9087 objc-keyword-decl:
9088 objc-selector : ( objc-type-name ) identifier
9089 objc-selector : identifier
9090 : ( objc-type-name ) identifier
9091 : identifier
9092
9093 objc-optparmlist:
9094 objc-optparms objc-optellipsis
9095
9096 objc-optparms:
9097 empty
9098 objc-opt-parms , parameter-declaration
9099
9100 objc-optellipsis:
9101 empty
9102 , ...
9103 */
9104
9105 static tree
9106 c_parser_objc_method_decl (c_parser *parser, bool is_class_method,
9107 tree *attributes, tree *expr)
9108 {
9109 tree type = NULL_TREE;
9110 tree sel;
9111 tree parms = NULL_TREE;
9112 bool ellipsis = false;
9113 bool attr_err = false;
9114
9115 *attributes = NULL_TREE;
9116 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
9117 {
9118 c_parser_consume_token (parser);
9119 type = c_parser_objc_type_name (parser);
9120 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
9121 }
9122 sel = c_parser_objc_selector (parser);
9123 /* If there is no selector, or a colon follows, we have an
9124 objc-keyword-selector. If there is a selector, and a colon does
9125 not follow, that selector ends the objc-method-decl. */
9126 if (!sel || c_parser_next_token_is (parser, CPP_COLON))
9127 {
9128 tree tsel = sel;
9129 tree list = NULL_TREE;
9130 while (true)
9131 {
9132 tree atype = NULL_TREE, id, keyworddecl;
9133 tree param_attr = NULL_TREE;
9134 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
9135 break;
9136 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
9137 {
9138 c_parser_consume_token (parser);
9139 atype = c_parser_objc_type_name (parser);
9140 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
9141 "expected %<)%>");
9142 }
9143 /* New ObjC allows attributes on method parameters. */
9144 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
9145 param_attr = c_parser_attributes (parser);
9146 if (c_parser_next_token_is_not (parser, CPP_NAME))
9147 {
9148 c_parser_error (parser, "expected identifier");
9149 return error_mark_node;
9150 }
9151 id = c_parser_peek_token (parser)->value;
9152 c_parser_consume_token (parser);
9153 keyworddecl = objc_build_keyword_decl (tsel, atype, id, param_attr);
9154 list = chainon (list, keyworddecl);
9155 tsel = c_parser_objc_selector (parser);
9156 if (!tsel && c_parser_next_token_is_not (parser, CPP_COLON))
9157 break;
9158 }
9159
9160 attr_err |= c_parser_objc_maybe_method_attributes (parser, attributes) ;
9161
9162 /* Parse the optional parameter list. Optional Objective-C
9163 method parameters follow the C syntax, and may include '...'
9164 to denote a variable number of arguments. */
9165 parms = make_node (TREE_LIST);
9166 while (c_parser_next_token_is (parser, CPP_COMMA))
9167 {
9168 struct c_parm *parm;
9169 c_parser_consume_token (parser);
9170 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
9171 {
9172 ellipsis = true;
9173 c_parser_consume_token (parser);
9174 attr_err |= c_parser_objc_maybe_method_attributes
9175 (parser, attributes) ;
9176 break;
9177 }
9178 parm = c_parser_parameter_declaration (parser, NULL_TREE);
9179 if (parm == NULL)
9180 break;
9181 parms = chainon (parms,
9182 build_tree_list (NULL_TREE, grokparm (parm, expr)));
9183 }
9184 sel = list;
9185 }
9186 else
9187 attr_err |= c_parser_objc_maybe_method_attributes (parser, attributes) ;
9188
9189 if (sel == NULL)
9190 {
9191 c_parser_error (parser, "objective-c method declaration is expected");
9192 return error_mark_node;
9193 }
9194
9195 if (attr_err)
9196 return error_mark_node;
9197
9198 return objc_build_method_signature (is_class_method, type, sel, parms, ellipsis);
9199 }
9200
9201 /* Parse an objc-type-name.
9202
9203 objc-type-name:
9204 objc-type-qualifiers[opt] type-name
9205 objc-type-qualifiers[opt]
9206
9207 objc-type-qualifiers:
9208 objc-type-qualifier
9209 objc-type-qualifiers objc-type-qualifier
9210
9211 objc-type-qualifier: one of
9212 in out inout bycopy byref oneway
9213 */
9214
9215 static tree
9216 c_parser_objc_type_name (c_parser *parser)
9217 {
9218 tree quals = NULL_TREE;
9219 struct c_type_name *type_name = NULL;
9220 tree type = NULL_TREE;
9221 while (true)
9222 {
9223 c_token *token = c_parser_peek_token (parser);
9224 if (token->type == CPP_KEYWORD
9225 && (token->keyword == RID_IN
9226 || token->keyword == RID_OUT
9227 || token->keyword == RID_INOUT
9228 || token->keyword == RID_BYCOPY
9229 || token->keyword == RID_BYREF
9230 || token->keyword == RID_ONEWAY))
9231 {
9232 quals = chainon (build_tree_list (NULL_TREE, token->value), quals);
9233 c_parser_consume_token (parser);
9234 }
9235 else
9236 break;
9237 }
9238 if (c_parser_next_tokens_start_typename (parser, cla_prefer_type))
9239 type_name = c_parser_type_name (parser);
9240 if (type_name)
9241 type = groktypename (type_name, NULL, NULL);
9242
9243 /* If the type is unknown, and error has already been produced and
9244 we need to recover from the error. In that case, use NULL_TREE
9245 for the type, as if no type had been specified; this will use the
9246 default type ('id') which is good for error recovery. */
9247 if (type == error_mark_node)
9248 type = NULL_TREE;
9249
9250 return build_tree_list (quals, type);
9251 }
9252
9253 /* Parse objc-protocol-refs.
9254
9255 objc-protocol-refs:
9256 < identifier-list >
9257 */
9258
9259 static tree
9260 c_parser_objc_protocol_refs (c_parser *parser)
9261 {
9262 tree list = NULL_TREE;
9263 gcc_assert (c_parser_next_token_is (parser, CPP_LESS));
9264 c_parser_consume_token (parser);
9265 /* Any identifiers, including those declared as type names, are OK
9266 here. */
9267 while (true)
9268 {
9269 tree id;
9270 if (c_parser_next_token_is_not (parser, CPP_NAME))
9271 {
9272 c_parser_error (parser, "expected identifier");
9273 break;
9274 }
9275 id = c_parser_peek_token (parser)->value;
9276 list = chainon (list, build_tree_list (NULL_TREE, id));
9277 c_parser_consume_token (parser);
9278 if (c_parser_next_token_is (parser, CPP_COMMA))
9279 c_parser_consume_token (parser);
9280 else
9281 break;
9282 }
9283 c_parser_require (parser, CPP_GREATER, "expected %<>%>");
9284 return list;
9285 }
9286
9287 /* Parse an objc-try-catch-finally-statement.
9288
9289 objc-try-catch-finally-statement:
9290 @try compound-statement objc-catch-list[opt]
9291 @try compound-statement objc-catch-list[opt] @finally compound-statement
9292
9293 objc-catch-list:
9294 @catch ( objc-catch-parameter-declaration ) compound-statement
9295 objc-catch-list @catch ( objc-catch-parameter-declaration ) compound-statement
9296
9297 objc-catch-parameter-declaration:
9298 parameter-declaration
9299 '...'
9300
9301 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
9302
9303 PS: This function is identical to cp_parser_objc_try_catch_finally_statement
9304 for C++. Keep them in sync. */
9305
9306 static void
9307 c_parser_objc_try_catch_finally_statement (c_parser *parser)
9308 {
9309 location_t location;
9310 tree stmt;
9311
9312 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_TRY));
9313 c_parser_consume_token (parser);
9314 location = c_parser_peek_token (parser)->location;
9315 objc_maybe_warn_exceptions (location);
9316 stmt = c_parser_compound_statement (parser);
9317 objc_begin_try_stmt (location, stmt);
9318
9319 while (c_parser_next_token_is_keyword (parser, RID_AT_CATCH))
9320 {
9321 struct c_parm *parm;
9322 tree parameter_declaration = error_mark_node;
9323 bool seen_open_paren = false;
9324
9325 c_parser_consume_token (parser);
9326 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
9327 seen_open_paren = true;
9328 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
9329 {
9330 /* We have "@catch (...)" (where the '...' are literally
9331 what is in the code). Skip the '...'.
9332 parameter_declaration is set to NULL_TREE, and
9333 objc_being_catch_clauses() knows that that means
9334 '...'. */
9335 c_parser_consume_token (parser);
9336 parameter_declaration = NULL_TREE;
9337 }
9338 else
9339 {
9340 /* We have "@catch (NSException *exception)" or something
9341 like that. Parse the parameter declaration. */
9342 parm = c_parser_parameter_declaration (parser, NULL_TREE);
9343 if (parm == NULL)
9344 parameter_declaration = error_mark_node;
9345 else
9346 parameter_declaration = grokparm (parm, NULL);
9347 }
9348 if (seen_open_paren)
9349 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
9350 else
9351 {
9352 /* If there was no open parenthesis, we are recovering from
9353 an error, and we are trying to figure out what mistake
9354 the user has made. */
9355
9356 /* If there is an immediate closing parenthesis, the user
9357 probably forgot the opening one (ie, they typed "@catch
9358 NSException *e)". Parse the closing parenthesis and keep
9359 going. */
9360 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
9361 c_parser_consume_token (parser);
9362
9363 /* If these is no immediate closing parenthesis, the user
9364 probably doesn't know that parenthesis are required at
9365 all (ie, they typed "@catch NSException *e"). So, just
9366 forget about the closing parenthesis and keep going. */
9367 }
9368 objc_begin_catch_clause (parameter_declaration);
9369 if (c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
9370 c_parser_compound_statement_nostart (parser);
9371 objc_finish_catch_clause ();
9372 }
9373 if (c_parser_next_token_is_keyword (parser, RID_AT_FINALLY))
9374 {
9375 c_parser_consume_token (parser);
9376 location = c_parser_peek_token (parser)->location;
9377 stmt = c_parser_compound_statement (parser);
9378 objc_build_finally_clause (location, stmt);
9379 }
9380 objc_finish_try_stmt ();
9381 }
9382
9383 /* Parse an objc-synchronized-statement.
9384
9385 objc-synchronized-statement:
9386 @synchronized ( expression ) compound-statement
9387 */
9388
9389 static void
9390 c_parser_objc_synchronized_statement (c_parser *parser)
9391 {
9392 location_t loc;
9393 tree expr, stmt;
9394 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_SYNCHRONIZED));
9395 c_parser_consume_token (parser);
9396 loc = c_parser_peek_token (parser)->location;
9397 objc_maybe_warn_exceptions (loc);
9398 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
9399 {
9400 struct c_expr ce = c_parser_expression (parser);
9401 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
9402 expr = ce.value;
9403 expr = c_fully_fold (expr, false, NULL);
9404 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
9405 }
9406 else
9407 expr = error_mark_node;
9408 stmt = c_parser_compound_statement (parser);
9409 objc_build_synchronized (loc, expr, stmt);
9410 }
9411
9412 /* Parse an objc-selector; return NULL_TREE without an error if the
9413 next token is not an objc-selector.
9414
9415 objc-selector:
9416 identifier
9417 one of
9418 enum struct union if else while do for switch case default
9419 break continue return goto asm sizeof typeof __alignof
9420 unsigned long const short volatile signed restrict _Complex
9421 in out inout bycopy byref oneway int char float double void _Bool
9422 _Atomic
9423
9424 ??? Why this selection of keywords but not, for example, storage
9425 class specifiers? */
9426
9427 static tree
9428 c_parser_objc_selector (c_parser *parser)
9429 {
9430 c_token *token = c_parser_peek_token (parser);
9431 tree value = token->value;
9432 if (token->type == CPP_NAME)
9433 {
9434 c_parser_consume_token (parser);
9435 return value;
9436 }
9437 if (token->type != CPP_KEYWORD)
9438 return NULL_TREE;
9439 switch (token->keyword)
9440 {
9441 case RID_ENUM:
9442 case RID_STRUCT:
9443 case RID_UNION:
9444 case RID_IF:
9445 case RID_ELSE:
9446 case RID_WHILE:
9447 case RID_DO:
9448 case RID_FOR:
9449 case RID_SWITCH:
9450 case RID_CASE:
9451 case RID_DEFAULT:
9452 case RID_BREAK:
9453 case RID_CONTINUE:
9454 case RID_RETURN:
9455 case RID_GOTO:
9456 case RID_ASM:
9457 case RID_SIZEOF:
9458 case RID_TYPEOF:
9459 case RID_ALIGNOF:
9460 case RID_UNSIGNED:
9461 case RID_LONG:
9462 case RID_CONST:
9463 case RID_SHORT:
9464 case RID_VOLATILE:
9465 case RID_SIGNED:
9466 case RID_RESTRICT:
9467 case RID_COMPLEX:
9468 case RID_IN:
9469 case RID_OUT:
9470 case RID_INOUT:
9471 case RID_BYCOPY:
9472 case RID_BYREF:
9473 case RID_ONEWAY:
9474 case RID_INT:
9475 case RID_CHAR:
9476 case RID_FLOAT:
9477 case RID_DOUBLE:
9478 case RID_VOID:
9479 case RID_BOOL:
9480 case RID_ATOMIC:
9481 case RID_AUTO_TYPE:
9482 case RID_INT_N_0:
9483 case RID_INT_N_1:
9484 case RID_INT_N_2:
9485 case RID_INT_N_3:
9486 c_parser_consume_token (parser);
9487 return value;
9488 default:
9489 return NULL_TREE;
9490 }
9491 }
9492
9493 /* Parse an objc-selector-arg.
9494
9495 objc-selector-arg:
9496 objc-selector
9497 objc-keywordname-list
9498
9499 objc-keywordname-list:
9500 objc-keywordname
9501 objc-keywordname-list objc-keywordname
9502
9503 objc-keywordname:
9504 objc-selector :
9505 :
9506 */
9507
9508 static tree
9509 c_parser_objc_selector_arg (c_parser *parser)
9510 {
9511 tree sel = c_parser_objc_selector (parser);
9512 tree list = NULL_TREE;
9513 if (sel && c_parser_next_token_is_not (parser, CPP_COLON))
9514 return sel;
9515 while (true)
9516 {
9517 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
9518 return list;
9519 list = chainon (list, build_tree_list (sel, NULL_TREE));
9520 sel = c_parser_objc_selector (parser);
9521 if (!sel && c_parser_next_token_is_not (parser, CPP_COLON))
9522 break;
9523 }
9524 return list;
9525 }
9526
9527 /* Parse an objc-receiver.
9528
9529 objc-receiver:
9530 expression
9531 class-name
9532 type-name
9533 */
9534
9535 static tree
9536 c_parser_objc_receiver (c_parser *parser)
9537 {
9538 location_t loc = c_parser_peek_token (parser)->location;
9539
9540 if (c_parser_peek_token (parser)->type == CPP_NAME
9541 && (c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME
9542 || c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME))
9543 {
9544 tree id = c_parser_peek_token (parser)->value;
9545 c_parser_consume_token (parser);
9546 return objc_get_class_reference (id);
9547 }
9548 struct c_expr ce = c_parser_expression (parser);
9549 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
9550 return c_fully_fold (ce.value, false, NULL);
9551 }
9552
9553 /* Parse objc-message-args.
9554
9555 objc-message-args:
9556 objc-selector
9557 objc-keywordarg-list
9558
9559 objc-keywordarg-list:
9560 objc-keywordarg
9561 objc-keywordarg-list objc-keywordarg
9562
9563 objc-keywordarg:
9564 objc-selector : objc-keywordexpr
9565 : objc-keywordexpr
9566 */
9567
9568 static tree
9569 c_parser_objc_message_args (c_parser *parser)
9570 {
9571 tree sel = c_parser_objc_selector (parser);
9572 tree list = NULL_TREE;
9573 if (sel && c_parser_next_token_is_not (parser, CPP_COLON))
9574 return sel;
9575 while (true)
9576 {
9577 tree keywordexpr;
9578 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
9579 return error_mark_node;
9580 keywordexpr = c_parser_objc_keywordexpr (parser);
9581 list = chainon (list, build_tree_list (sel, keywordexpr));
9582 sel = c_parser_objc_selector (parser);
9583 if (!sel && c_parser_next_token_is_not (parser, CPP_COLON))
9584 break;
9585 }
9586 return list;
9587 }
9588
9589 /* Parse an objc-keywordexpr.
9590
9591 objc-keywordexpr:
9592 nonempty-expr-list
9593 */
9594
9595 static tree
9596 c_parser_objc_keywordexpr (c_parser *parser)
9597 {
9598 tree ret;
9599 vec<tree, va_gc> *expr_list = c_parser_expr_list (parser, true, true,
9600 NULL, NULL, NULL, NULL);
9601 if (vec_safe_length (expr_list) == 1)
9602 {
9603 /* Just return the expression, remove a level of
9604 indirection. */
9605 ret = (*expr_list)[0];
9606 }
9607 else
9608 {
9609 /* We have a comma expression, we will collapse later. */
9610 ret = build_tree_list_vec (expr_list);
9611 }
9612 release_tree_vector (expr_list);
9613 return ret;
9614 }
9615
9616 /* A check, needed in several places, that ObjC interface, implementation or
9617 method definitions are not prefixed by incorrect items. */
9618 static bool
9619 c_parser_objc_diagnose_bad_element_prefix (c_parser *parser,
9620 struct c_declspecs *specs)
9621 {
9622 if (!specs->declspecs_seen_p || specs->non_sc_seen_p
9623 || specs->typespec_kind != ctsk_none)
9624 {
9625 c_parser_error (parser,
9626 "no type or storage class may be specified here,");
9627 c_parser_skip_to_end_of_block_or_statement (parser);
9628 return true;
9629 }
9630 return false;
9631 }
9632
9633 /* Parse an Objective-C @property declaration. The syntax is:
9634
9635 objc-property-declaration:
9636 '@property' objc-property-attributes[opt] struct-declaration ;
9637
9638 objc-property-attributes:
9639 '(' objc-property-attribute-list ')'
9640
9641 objc-property-attribute-list:
9642 objc-property-attribute
9643 objc-property-attribute-list, objc-property-attribute
9644
9645 objc-property-attribute
9646 'getter' = identifier
9647 'setter' = identifier
9648 'readonly'
9649 'readwrite'
9650 'assign'
9651 'retain'
9652 'copy'
9653 'nonatomic'
9654
9655 For example:
9656 @property NSString *name;
9657 @property (readonly) id object;
9658 @property (retain, nonatomic, getter=getTheName) id name;
9659 @property int a, b, c;
9660
9661 PS: This function is identical to cp_parser_objc_at_propery_declaration
9662 for C++. Keep them in sync. */
9663 static void
9664 c_parser_objc_at_property_declaration (c_parser *parser)
9665 {
9666 /* The following variables hold the attributes of the properties as
9667 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
9668 seen. When we see an attribute, we set them to 'true' (if they
9669 are boolean properties) or to the identifier (if they have an
9670 argument, ie, for getter and setter). Note that here we only
9671 parse the list of attributes, check the syntax and accumulate the
9672 attributes that we find. objc_add_property_declaration() will
9673 then process the information. */
9674 bool property_assign = false;
9675 bool property_copy = false;
9676 tree property_getter_ident = NULL_TREE;
9677 bool property_nonatomic = false;
9678 bool property_readonly = false;
9679 bool property_readwrite = false;
9680 bool property_retain = false;
9681 tree property_setter_ident = NULL_TREE;
9682
9683 /* 'properties' is the list of properties that we read. Usually a
9684 single one, but maybe more (eg, in "@property int a, b, c;" there
9685 are three). */
9686 tree properties;
9687 location_t loc;
9688
9689 loc = c_parser_peek_token (parser)->location;
9690 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_PROPERTY));
9691
9692 c_parser_consume_token (parser); /* Eat '@property'. */
9693
9694 /* Parse the optional attribute list... */
9695 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
9696 {
9697 /* Eat the '(' */
9698 c_parser_consume_token (parser);
9699
9700 /* Property attribute keywords are valid now. */
9701 parser->objc_property_attr_context = true;
9702
9703 while (true)
9704 {
9705 bool syntax_error = false;
9706 c_token *token = c_parser_peek_token (parser);
9707 enum rid keyword;
9708
9709 if (token->type != CPP_KEYWORD)
9710 {
9711 if (token->type == CPP_CLOSE_PAREN)
9712 c_parser_error (parser, "expected identifier");
9713 else
9714 {
9715 c_parser_consume_token (parser);
9716 c_parser_error (parser, "unknown property attribute");
9717 }
9718 break;
9719 }
9720 keyword = token->keyword;
9721 c_parser_consume_token (parser);
9722 switch (keyword)
9723 {
9724 case RID_ASSIGN: property_assign = true; break;
9725 case RID_COPY: property_copy = true; break;
9726 case RID_NONATOMIC: property_nonatomic = true; break;
9727 case RID_READONLY: property_readonly = true; break;
9728 case RID_READWRITE: property_readwrite = true; break;
9729 case RID_RETAIN: property_retain = true; break;
9730
9731 case RID_GETTER:
9732 case RID_SETTER:
9733 if (c_parser_next_token_is_not (parser, CPP_EQ))
9734 {
9735 if (keyword == RID_GETTER)
9736 c_parser_error (parser,
9737 "missing %<=%> (after %<getter%> attribute)");
9738 else
9739 c_parser_error (parser,
9740 "missing %<=%> (after %<setter%> attribute)");
9741 syntax_error = true;
9742 break;
9743 }
9744 c_parser_consume_token (parser); /* eat the = */
9745 if (c_parser_next_token_is_not (parser, CPP_NAME))
9746 {
9747 c_parser_error (parser, "expected identifier");
9748 syntax_error = true;
9749 break;
9750 }
9751 if (keyword == RID_SETTER)
9752 {
9753 if (property_setter_ident != NULL_TREE)
9754 c_parser_error (parser, "the %<setter%> attribute may only be specified once");
9755 else
9756 property_setter_ident = c_parser_peek_token (parser)->value;
9757 c_parser_consume_token (parser);
9758 if (c_parser_next_token_is_not (parser, CPP_COLON))
9759 c_parser_error (parser, "setter name must terminate with %<:%>");
9760 else
9761 c_parser_consume_token (parser);
9762 }
9763 else
9764 {
9765 if (property_getter_ident != NULL_TREE)
9766 c_parser_error (parser, "the %<getter%> attribute may only be specified once");
9767 else
9768 property_getter_ident = c_parser_peek_token (parser)->value;
9769 c_parser_consume_token (parser);
9770 }
9771 break;
9772 default:
9773 c_parser_error (parser, "unknown property attribute");
9774 syntax_error = true;
9775 break;
9776 }
9777
9778 if (syntax_error)
9779 break;
9780
9781 if (c_parser_next_token_is (parser, CPP_COMMA))
9782 c_parser_consume_token (parser);
9783 else
9784 break;
9785 }
9786 parser->objc_property_attr_context = false;
9787 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
9788 }
9789 /* ... and the property declaration(s). */
9790 properties = c_parser_struct_declaration (parser);
9791
9792 if (properties == error_mark_node)
9793 {
9794 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
9795 parser->error = false;
9796 return;
9797 }
9798
9799 if (properties == NULL_TREE)
9800 c_parser_error (parser, "expected identifier");
9801 else
9802 {
9803 /* Comma-separated properties are chained together in
9804 reverse order; add them one by one. */
9805 properties = nreverse (properties);
9806
9807 for (; properties; properties = TREE_CHAIN (properties))
9808 objc_add_property_declaration (loc, copy_node (properties),
9809 property_readonly, property_readwrite,
9810 property_assign, property_retain,
9811 property_copy, property_nonatomic,
9812 property_getter_ident, property_setter_ident);
9813 }
9814
9815 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9816 parser->error = false;
9817 }
9818
9819 /* Parse an Objective-C @synthesize declaration. The syntax is:
9820
9821 objc-synthesize-declaration:
9822 @synthesize objc-synthesize-identifier-list ;
9823
9824 objc-synthesize-identifier-list:
9825 objc-synthesize-identifier
9826 objc-synthesize-identifier-list, objc-synthesize-identifier
9827
9828 objc-synthesize-identifier
9829 identifier
9830 identifier = identifier
9831
9832 For example:
9833 @synthesize MyProperty;
9834 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
9835
9836 PS: This function is identical to cp_parser_objc_at_synthesize_declaration
9837 for C++. Keep them in sync.
9838 */
9839 static void
9840 c_parser_objc_at_synthesize_declaration (c_parser *parser)
9841 {
9842 tree list = NULL_TREE;
9843 location_t loc;
9844 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_SYNTHESIZE));
9845 loc = c_parser_peek_token (parser)->location;
9846
9847 c_parser_consume_token (parser);
9848 while (true)
9849 {
9850 tree property, ivar;
9851 if (c_parser_next_token_is_not (parser, CPP_NAME))
9852 {
9853 c_parser_error (parser, "expected identifier");
9854 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
9855 /* Once we find the semicolon, we can resume normal parsing.
9856 We have to reset parser->error manually because
9857 c_parser_skip_until_found() won't reset it for us if the
9858 next token is precisely a semicolon. */
9859 parser->error = false;
9860 return;
9861 }
9862 property = c_parser_peek_token (parser)->value;
9863 c_parser_consume_token (parser);
9864 if (c_parser_next_token_is (parser, CPP_EQ))
9865 {
9866 c_parser_consume_token (parser);
9867 if (c_parser_next_token_is_not (parser, CPP_NAME))
9868 {
9869 c_parser_error (parser, "expected identifier");
9870 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
9871 parser->error = false;
9872 return;
9873 }
9874 ivar = c_parser_peek_token (parser)->value;
9875 c_parser_consume_token (parser);
9876 }
9877 else
9878 ivar = NULL_TREE;
9879 list = chainon (list, build_tree_list (ivar, property));
9880 if (c_parser_next_token_is (parser, CPP_COMMA))
9881 c_parser_consume_token (parser);
9882 else
9883 break;
9884 }
9885 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9886 objc_add_synthesize_declaration (loc, list);
9887 }
9888
9889 /* Parse an Objective-C @dynamic declaration. The syntax is:
9890
9891 objc-dynamic-declaration:
9892 @dynamic identifier-list ;
9893
9894 For example:
9895 @dynamic MyProperty;
9896 @dynamic MyProperty, AnotherProperty;
9897
9898 PS: This function is identical to cp_parser_objc_at_dynamic_declaration
9899 for C++. Keep them in sync.
9900 */
9901 static void
9902 c_parser_objc_at_dynamic_declaration (c_parser *parser)
9903 {
9904 tree list = NULL_TREE;
9905 location_t loc;
9906 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_DYNAMIC));
9907 loc = c_parser_peek_token (parser)->location;
9908
9909 c_parser_consume_token (parser);
9910 while (true)
9911 {
9912 tree property;
9913 if (c_parser_next_token_is_not (parser, CPP_NAME))
9914 {
9915 c_parser_error (parser, "expected identifier");
9916 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
9917 parser->error = false;
9918 return;
9919 }
9920 property = c_parser_peek_token (parser)->value;
9921 list = chainon (list, build_tree_list (NULL_TREE, property));
9922 c_parser_consume_token (parser);
9923 if (c_parser_next_token_is (parser, CPP_COMMA))
9924 c_parser_consume_token (parser);
9925 else
9926 break;
9927 }
9928 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9929 objc_add_dynamic_declaration (loc, list);
9930 }
9931
9932 \f
9933 /* Handle pragmas. Some OpenMP pragmas are associated with, and therefore
9934 should be considered, statements. ALLOW_STMT is true if we're within
9935 the context of a function and such pragmas are to be allowed. Returns
9936 true if we actually parsed such a pragma. */
9937
9938 static bool
9939 c_parser_pragma (c_parser *parser, enum pragma_context context)
9940 {
9941 unsigned int id;
9942
9943 id = c_parser_peek_token (parser)->pragma_kind;
9944 gcc_assert (id != PRAGMA_NONE);
9945
9946 switch (id)
9947 {
9948 case PRAGMA_OACC_DECLARE:
9949 c_parser_oacc_declare (parser);
9950 return false;
9951
9952 case PRAGMA_OACC_ENTER_DATA:
9953 c_parser_oacc_enter_exit_data (parser, true);
9954 return false;
9955
9956 case PRAGMA_OACC_EXIT_DATA:
9957 c_parser_oacc_enter_exit_data (parser, false);
9958 return false;
9959
9960 case PRAGMA_OACC_ROUTINE:
9961 c_parser_oacc_routine (parser, context);
9962 return false;
9963
9964 case PRAGMA_OACC_UPDATE:
9965 if (context != pragma_compound)
9966 {
9967 if (context == pragma_stmt)
9968 c_parser_error (parser, "%<#pragma acc update%> may only be "
9969 "used in compound statements");
9970 goto bad_stmt;
9971 }
9972 c_parser_oacc_update (parser);
9973 return false;
9974
9975 case PRAGMA_OMP_BARRIER:
9976 if (context != pragma_compound)
9977 {
9978 if (context == pragma_stmt)
9979 c_parser_error (parser, "%<#pragma omp barrier%> may only be "
9980 "used in compound statements");
9981 goto bad_stmt;
9982 }
9983 c_parser_omp_barrier (parser);
9984 return false;
9985
9986 case PRAGMA_OMP_FLUSH:
9987 if (context != pragma_compound)
9988 {
9989 if (context == pragma_stmt)
9990 c_parser_error (parser, "%<#pragma omp flush%> may only be "
9991 "used in compound statements");
9992 goto bad_stmt;
9993 }
9994 c_parser_omp_flush (parser);
9995 return false;
9996
9997 case PRAGMA_OMP_TASKWAIT:
9998 if (context != pragma_compound)
9999 {
10000 if (context == pragma_stmt)
10001 c_parser_error (parser, "%<#pragma omp taskwait%> may only be "
10002 "used in compound statements");
10003 goto bad_stmt;
10004 }
10005 c_parser_omp_taskwait (parser);
10006 return false;
10007
10008 case PRAGMA_OMP_TASKYIELD:
10009 if (context != pragma_compound)
10010 {
10011 if (context == pragma_stmt)
10012 c_parser_error (parser, "%<#pragma omp taskyield%> may only be "
10013 "used in compound statements");
10014 goto bad_stmt;
10015 }
10016 c_parser_omp_taskyield (parser);
10017 return false;
10018
10019 case PRAGMA_OMP_CANCEL:
10020 if (context != pragma_compound)
10021 {
10022 if (context == pragma_stmt)
10023 c_parser_error (parser, "%<#pragma omp cancel%> may only be "
10024 "used in compound statements");
10025 goto bad_stmt;
10026 }
10027 c_parser_omp_cancel (parser);
10028 return false;
10029
10030 case PRAGMA_OMP_CANCELLATION_POINT:
10031 if (context != pragma_compound)
10032 {
10033 if (context == pragma_stmt)
10034 c_parser_error (parser, "%<#pragma omp cancellation point%> may "
10035 "only be used in compound statements");
10036 goto bad_stmt;
10037 }
10038 c_parser_omp_cancellation_point (parser);
10039 return false;
10040
10041 case PRAGMA_OMP_THREADPRIVATE:
10042 c_parser_omp_threadprivate (parser);
10043 return false;
10044
10045 case PRAGMA_OMP_TARGET:
10046 return c_parser_omp_target (parser, context);
10047
10048 case PRAGMA_OMP_END_DECLARE_TARGET:
10049 c_parser_omp_end_declare_target (parser);
10050 return false;
10051
10052 case PRAGMA_OMP_SECTION:
10053 error_at (c_parser_peek_token (parser)->location,
10054 "%<#pragma omp section%> may only be used in "
10055 "%<#pragma omp sections%> construct");
10056 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
10057 return false;
10058
10059 case PRAGMA_OMP_DECLARE_REDUCTION:
10060 c_parser_omp_declare (parser, context);
10061 return false;
10062
10063 case PRAGMA_OMP_ORDERED:
10064 return c_parser_omp_ordered (parser, context);
10065
10066 case PRAGMA_IVDEP:
10067 c_parser_consume_pragma (parser);
10068 c_parser_skip_to_pragma_eol (parser);
10069 if (!c_parser_next_token_is_keyword (parser, RID_FOR)
10070 && !c_parser_next_token_is_keyword (parser, RID_WHILE)
10071 && !c_parser_next_token_is_keyword (parser, RID_DO))
10072 {
10073 c_parser_error (parser, "for, while or do statement expected");
10074 return false;
10075 }
10076 if (c_parser_next_token_is_keyword (parser, RID_FOR))
10077 c_parser_for_statement (parser, true);
10078 else if (c_parser_next_token_is_keyword (parser, RID_WHILE))
10079 c_parser_while_statement (parser, true);
10080 else
10081 c_parser_do_statement (parser, true);
10082 return false;
10083
10084 case PRAGMA_GCC_PCH_PREPROCESS:
10085 c_parser_error (parser, "%<#pragma GCC pch_preprocess%> must be first");
10086 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
10087 return false;
10088
10089 case PRAGMA_CILK_SIMD:
10090 if (!c_parser_cilk_verify_simd (parser, context))
10091 return false;
10092 c_parser_consume_pragma (parser);
10093 c_parser_cilk_simd (parser);
10094 return false;
10095 case PRAGMA_CILK_GRAINSIZE:
10096 if (!flag_cilkplus)
10097 {
10098 warning (0, "%<#pragma grainsize%> ignored because -fcilkplus is not"
10099 " enabled");
10100 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
10101 return false;
10102 }
10103 if (context == pragma_external)
10104 {
10105 error_at (c_parser_peek_token (parser)->location,
10106 "%<#pragma grainsize%> must be inside a function");
10107 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
10108 return false;
10109 }
10110 c_parser_cilk_grainsize (parser);
10111 return false;
10112
10113 default:
10114 if (id < PRAGMA_FIRST_EXTERNAL)
10115 {
10116 if (context != pragma_stmt && context != pragma_compound)
10117 {
10118 bad_stmt:
10119 c_parser_error (parser, "expected declaration specifiers");
10120 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
10121 return false;
10122 }
10123 c_parser_omp_construct (parser);
10124 return true;
10125 }
10126 break;
10127 }
10128
10129 c_parser_consume_pragma (parser);
10130 c_invoke_pragma_handler (id);
10131
10132 /* Skip to EOL, but suppress any error message. Those will have been
10133 generated by the handler routine through calling error, as opposed
10134 to calling c_parser_error. */
10135 parser->error = true;
10136 c_parser_skip_to_pragma_eol (parser);
10137
10138 return false;
10139 }
10140
10141 /* The interface the pragma parsers have to the lexer. */
10142
10143 enum cpp_ttype
10144 pragma_lex (tree *value, location_t *loc)
10145 {
10146 c_token *tok = c_parser_peek_token (the_parser);
10147 enum cpp_ttype ret = tok->type;
10148
10149 *value = tok->value;
10150 if (loc)
10151 *loc = tok->location;
10152
10153 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
10154 ret = CPP_EOF;
10155 else
10156 {
10157 if (ret == CPP_KEYWORD)
10158 ret = CPP_NAME;
10159 c_parser_consume_token (the_parser);
10160 }
10161
10162 return ret;
10163 }
10164
10165 static void
10166 c_parser_pragma_pch_preprocess (c_parser *parser)
10167 {
10168 tree name = NULL;
10169
10170 c_parser_consume_pragma (parser);
10171 if (c_parser_next_token_is (parser, CPP_STRING))
10172 {
10173 name = c_parser_peek_token (parser)->value;
10174 c_parser_consume_token (parser);
10175 }
10176 else
10177 c_parser_error (parser, "expected string literal");
10178 c_parser_skip_to_pragma_eol (parser);
10179
10180 if (name)
10181 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
10182 }
10183 \f
10184 /* OpenACC and OpenMP parsing routines. */
10185
10186 /* Returns name of the next clause.
10187 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
10188 the token is not consumed. Otherwise appropriate pragma_omp_clause is
10189 returned and the token is consumed. */
10190
10191 static pragma_omp_clause
10192 c_parser_omp_clause_name (c_parser *parser)
10193 {
10194 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
10195
10196 if (c_parser_next_token_is_keyword (parser, RID_AUTO))
10197 result = PRAGMA_OACC_CLAUSE_AUTO;
10198 else if (c_parser_next_token_is_keyword (parser, RID_IF))
10199 result = PRAGMA_OMP_CLAUSE_IF;
10200 else if (c_parser_next_token_is_keyword (parser, RID_DEFAULT))
10201 result = PRAGMA_OMP_CLAUSE_DEFAULT;
10202 else if (c_parser_next_token_is_keyword (parser, RID_FOR))
10203 result = PRAGMA_OMP_CLAUSE_FOR;
10204 else if (c_parser_next_token_is (parser, CPP_NAME))
10205 {
10206 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
10207
10208 switch (p[0])
10209 {
10210 case 'a':
10211 if (!strcmp ("aligned", p))
10212 result = PRAGMA_OMP_CLAUSE_ALIGNED;
10213 else if (!strcmp ("async", p))
10214 result = PRAGMA_OACC_CLAUSE_ASYNC;
10215 break;
10216 case 'c':
10217 if (!strcmp ("collapse", p))
10218 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
10219 else if (!strcmp ("copy", p))
10220 result = PRAGMA_OACC_CLAUSE_COPY;
10221 else if (!strcmp ("copyin", p))
10222 result = PRAGMA_OMP_CLAUSE_COPYIN;
10223 else if (!strcmp ("copyout", p))
10224 result = PRAGMA_OACC_CLAUSE_COPYOUT;
10225 else if (!strcmp ("copyprivate", p))
10226 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
10227 else if (!strcmp ("create", p))
10228 result = PRAGMA_OACC_CLAUSE_CREATE;
10229 break;
10230 case 'd':
10231 if (!strcmp ("defaultmap", p))
10232 result = PRAGMA_OMP_CLAUSE_DEFAULTMAP;
10233 else if (!strcmp ("delete", p))
10234 result = PRAGMA_OACC_CLAUSE_DELETE;
10235 else if (!strcmp ("depend", p))
10236 result = PRAGMA_OMP_CLAUSE_DEPEND;
10237 else if (!strcmp ("device", p))
10238 result = PRAGMA_OMP_CLAUSE_DEVICE;
10239 else if (!strcmp ("deviceptr", p))
10240 result = PRAGMA_OACC_CLAUSE_DEVICEPTR;
10241 else if (!strcmp ("device_resident", p))
10242 result = PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT;
10243 else if (!strcmp ("dist_schedule", p))
10244 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
10245 break;
10246 case 'f':
10247 if (!strcmp ("final", p))
10248 result = PRAGMA_OMP_CLAUSE_FINAL;
10249 else if (!strcmp ("firstprivate", p))
10250 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
10251 else if (!strcmp ("from", p))
10252 result = PRAGMA_OMP_CLAUSE_FROM;
10253 break;
10254 case 'g':
10255 if (!strcmp ("gang", p))
10256 result = PRAGMA_OACC_CLAUSE_GANG;
10257 else if (!strcmp ("grainsize", p))
10258 result = PRAGMA_OMP_CLAUSE_GRAINSIZE;
10259 break;
10260 case 'h':
10261 if (!strcmp ("hint", p))
10262 result = PRAGMA_OMP_CLAUSE_HINT;
10263 else if (!strcmp ("host", p))
10264 result = PRAGMA_OACC_CLAUSE_HOST;
10265 break;
10266 case 'i':
10267 if (!strcmp ("inbranch", p))
10268 result = PRAGMA_OMP_CLAUSE_INBRANCH;
10269 else if (!strcmp ("independent", p))
10270 result = PRAGMA_OACC_CLAUSE_INDEPENDENT;
10271 else if (!strcmp ("is_device_ptr", p))
10272 result = PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR;
10273 break;
10274 case 'l':
10275 if (!strcmp ("lastprivate", p))
10276 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
10277 else if (!strcmp ("linear", p))
10278 result = PRAGMA_OMP_CLAUSE_LINEAR;
10279 else if (!strcmp ("link", p))
10280 result = PRAGMA_OMP_CLAUSE_LINK;
10281 break;
10282 case 'm':
10283 if (!strcmp ("map", p))
10284 result = PRAGMA_OMP_CLAUSE_MAP;
10285 else if (!strcmp ("mergeable", p))
10286 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
10287 else if (flag_cilkplus && !strcmp ("mask", p))
10288 result = PRAGMA_CILK_CLAUSE_MASK;
10289 break;
10290 case 'n':
10291 if (!strcmp ("nogroup", p))
10292 result = PRAGMA_OMP_CLAUSE_NOGROUP;
10293 else if (!strcmp ("notinbranch", p))
10294 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
10295 else if (!strcmp ("nowait", p))
10296 result = PRAGMA_OMP_CLAUSE_NOWAIT;
10297 else if (!strcmp ("num_gangs", p))
10298 result = PRAGMA_OACC_CLAUSE_NUM_GANGS;
10299 else if (!strcmp ("num_tasks", p))
10300 result = PRAGMA_OMP_CLAUSE_NUM_TASKS;
10301 else if (!strcmp ("num_teams", p))
10302 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
10303 else if (!strcmp ("num_threads", p))
10304 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
10305 else if (!strcmp ("num_workers", p))
10306 result = PRAGMA_OACC_CLAUSE_NUM_WORKERS;
10307 else if (flag_cilkplus && !strcmp ("nomask", p))
10308 result = PRAGMA_CILK_CLAUSE_NOMASK;
10309 break;
10310 case 'o':
10311 if (!strcmp ("ordered", p))
10312 result = PRAGMA_OMP_CLAUSE_ORDERED;
10313 break;
10314 case 'p':
10315 if (!strcmp ("parallel", p))
10316 result = PRAGMA_OMP_CLAUSE_PARALLEL;
10317 else if (!strcmp ("present", p))
10318 result = PRAGMA_OACC_CLAUSE_PRESENT;
10319 else if (!strcmp ("present_or_copy", p)
10320 || !strcmp ("pcopy", p))
10321 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY;
10322 else if (!strcmp ("present_or_copyin", p)
10323 || !strcmp ("pcopyin", p))
10324 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN;
10325 else if (!strcmp ("present_or_copyout", p)
10326 || !strcmp ("pcopyout", p))
10327 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT;
10328 else if (!strcmp ("present_or_create", p)
10329 || !strcmp ("pcreate", p))
10330 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE;
10331 else if (!strcmp ("priority", p))
10332 result = PRAGMA_OMP_CLAUSE_PRIORITY;
10333 else if (!strcmp ("private", p))
10334 result = PRAGMA_OMP_CLAUSE_PRIVATE;
10335 else if (!strcmp ("proc_bind", p))
10336 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
10337 break;
10338 case 'r':
10339 if (!strcmp ("reduction", p))
10340 result = PRAGMA_OMP_CLAUSE_REDUCTION;
10341 break;
10342 case 's':
10343 if (!strcmp ("safelen", p))
10344 result = PRAGMA_OMP_CLAUSE_SAFELEN;
10345 else if (!strcmp ("schedule", p))
10346 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
10347 else if (!strcmp ("sections", p))
10348 result = PRAGMA_OMP_CLAUSE_SECTIONS;
10349 else if (!strcmp ("seq", p))
10350 result = PRAGMA_OACC_CLAUSE_SEQ;
10351 else if (!strcmp ("shared", p))
10352 result = PRAGMA_OMP_CLAUSE_SHARED;
10353 else if (!strcmp ("simd", p))
10354 result = PRAGMA_OMP_CLAUSE_SIMD;
10355 else if (!strcmp ("simdlen", p))
10356 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
10357 else if (!strcmp ("self", p))
10358 result = PRAGMA_OACC_CLAUSE_SELF;
10359 break;
10360 case 't':
10361 if (!strcmp ("taskgroup", p))
10362 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
10363 else if (!strcmp ("thread_limit", p))
10364 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
10365 else if (!strcmp ("threads", p))
10366 result = PRAGMA_OMP_CLAUSE_THREADS;
10367 else if (!strcmp ("tile", p))
10368 result = PRAGMA_OACC_CLAUSE_TILE;
10369 else if (!strcmp ("to", p))
10370 result = PRAGMA_OMP_CLAUSE_TO;
10371 break;
10372 case 'u':
10373 if (!strcmp ("uniform", p))
10374 result = PRAGMA_OMP_CLAUSE_UNIFORM;
10375 else if (!strcmp ("untied", p))
10376 result = PRAGMA_OMP_CLAUSE_UNTIED;
10377 else if (!strcmp ("use_device", p))
10378 result = PRAGMA_OACC_CLAUSE_USE_DEVICE;
10379 else if (!strcmp ("use_device_ptr", p))
10380 result = PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR;
10381 break;
10382 case 'v':
10383 if (!strcmp ("vector", p))
10384 result = PRAGMA_OACC_CLAUSE_VECTOR;
10385 else if (!strcmp ("vector_length", p))
10386 result = PRAGMA_OACC_CLAUSE_VECTOR_LENGTH;
10387 else if (flag_cilkplus && !strcmp ("vectorlength", p))
10388 result = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
10389 break;
10390 case 'w':
10391 if (!strcmp ("wait", p))
10392 result = PRAGMA_OACC_CLAUSE_WAIT;
10393 else if (!strcmp ("worker", p))
10394 result = PRAGMA_OACC_CLAUSE_WORKER;
10395 break;
10396 }
10397 }
10398
10399 if (result != PRAGMA_OMP_CLAUSE_NONE)
10400 c_parser_consume_token (parser);
10401
10402 return result;
10403 }
10404
10405 /* Validate that a clause of the given type does not already exist. */
10406
10407 static void
10408 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
10409 const char *name)
10410 {
10411 tree c;
10412
10413 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
10414 if (OMP_CLAUSE_CODE (c) == code)
10415 {
10416 location_t loc = OMP_CLAUSE_LOCATION (c);
10417 error_at (loc, "too many %qs clauses", name);
10418 break;
10419 }
10420 }
10421
10422 /* OpenACC 2.0
10423 Parse wait clause or wait directive parameters. */
10424
10425 static tree
10426 c_parser_oacc_wait_list (c_parser *parser, location_t clause_loc, tree list)
10427 {
10428 vec<tree, va_gc> *args;
10429 tree t, args_tree;
10430
10431 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10432 return list;
10433
10434 args = c_parser_expr_list (parser, false, true, NULL, NULL, NULL, NULL);
10435
10436 if (args->length () == 0)
10437 {
10438 c_parser_error (parser, "expected integer expression before ')'");
10439 release_tree_vector (args);
10440 return list;
10441 }
10442
10443 args_tree = build_tree_list_vec (args);
10444
10445 for (t = args_tree; t; t = TREE_CHAIN (t))
10446 {
10447 tree targ = TREE_VALUE (t);
10448
10449 if (targ != error_mark_node)
10450 {
10451 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ)))
10452 {
10453 c_parser_error (parser, "expression must be integral");
10454 targ = error_mark_node;
10455 }
10456 else
10457 {
10458 tree c = build_omp_clause (clause_loc, OMP_CLAUSE_WAIT);
10459
10460 OMP_CLAUSE_DECL (c) = targ;
10461 OMP_CLAUSE_CHAIN (c) = list;
10462 list = c;
10463 }
10464 }
10465 }
10466
10467 release_tree_vector (args);
10468 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10469 return list;
10470 }
10471
10472 /* OpenACC 2.0, OpenMP 2.5:
10473 variable-list:
10474 identifier
10475 variable-list , identifier
10476
10477 If KIND is nonzero, create the appropriate node and install the
10478 decl in OMP_CLAUSE_DECL and add the node to the head of the list.
10479 If KIND is nonzero, CLAUSE_LOC is the location of the clause.
10480
10481 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
10482 return the list created. */
10483
10484 static tree
10485 c_parser_omp_variable_list (c_parser *parser,
10486 location_t clause_loc,
10487 enum omp_clause_code kind, tree list)
10488 {
10489 if (c_parser_next_token_is_not (parser, CPP_NAME)
10490 || c_parser_peek_token (parser)->id_kind != C_ID_ID)
10491 c_parser_error (parser, "expected identifier");
10492
10493 while (c_parser_next_token_is (parser, CPP_NAME)
10494 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
10495 {
10496 tree t = lookup_name (c_parser_peek_token (parser)->value);
10497
10498 if (t == NULL_TREE)
10499 {
10500 undeclared_variable (c_parser_peek_token (parser)->location,
10501 c_parser_peek_token (parser)->value);
10502 t = error_mark_node;
10503 }
10504
10505 c_parser_consume_token (parser);
10506
10507 if (t == error_mark_node)
10508 ;
10509 else if (kind != 0)
10510 {
10511 switch (kind)
10512 {
10513 case OMP_CLAUSE__CACHE_:
10514 if (c_parser_peek_token (parser)->type != CPP_OPEN_SQUARE)
10515 {
10516 c_parser_error (parser, "expected %<[%>");
10517 t = error_mark_node;
10518 break;
10519 }
10520 /* FALLTHROUGH */
10521 case OMP_CLAUSE_MAP:
10522 case OMP_CLAUSE_FROM:
10523 case OMP_CLAUSE_TO:
10524 while (c_parser_next_token_is (parser, CPP_DOT))
10525 {
10526 location_t op_loc = c_parser_peek_token (parser)->location;
10527 c_parser_consume_token (parser);
10528 if (!c_parser_next_token_is (parser, CPP_NAME))
10529 {
10530 c_parser_error (parser, "expected identifier");
10531 t = error_mark_node;
10532 break;
10533 }
10534 tree ident = c_parser_peek_token (parser)->value;
10535 c_parser_consume_token (parser);
10536 t = build_component_ref (op_loc, t, ident);
10537 }
10538 /* FALLTHROUGH */
10539 case OMP_CLAUSE_DEPEND:
10540 case OMP_CLAUSE_REDUCTION:
10541 while (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
10542 {
10543 tree low_bound = NULL_TREE, length = NULL_TREE;
10544
10545 c_parser_consume_token (parser);
10546 if (!c_parser_next_token_is (parser, CPP_COLON))
10547 {
10548 low_bound = c_parser_expression (parser).value;
10549 mark_exp_read (low_bound);
10550 }
10551 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
10552 length = integer_one_node;
10553 else
10554 {
10555 /* Look for `:'. */
10556 if (!c_parser_require (parser, CPP_COLON,
10557 "expected %<:%>"))
10558 {
10559 t = error_mark_node;
10560 break;
10561 }
10562 if (!c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
10563 {
10564 length = c_parser_expression (parser).value;
10565 mark_exp_read (length);
10566 }
10567 }
10568 /* Look for the closing `]'. */
10569 if (!c_parser_require (parser, CPP_CLOSE_SQUARE,
10570 "expected %<]%>"))
10571 {
10572 t = error_mark_node;
10573 break;
10574 }
10575
10576 if (kind == OMP_CLAUSE__CACHE_)
10577 {
10578 if (TREE_CODE (low_bound) != INTEGER_CST
10579 && !TREE_READONLY (low_bound))
10580 {
10581 error_at (clause_loc,
10582 "%qD is not a constant", low_bound);
10583 t = error_mark_node;
10584 }
10585
10586 if (TREE_CODE (length) != INTEGER_CST
10587 && !TREE_READONLY (length))
10588 {
10589 error_at (clause_loc,
10590 "%qD is not a constant", length);
10591 t = error_mark_node;
10592 }
10593 }
10594
10595 t = tree_cons (low_bound, length, t);
10596 }
10597 break;
10598 default:
10599 break;
10600 }
10601
10602 if (t != error_mark_node)
10603 {
10604 tree u = build_omp_clause (clause_loc, kind);
10605 OMP_CLAUSE_DECL (u) = t;
10606 OMP_CLAUSE_CHAIN (u) = list;
10607 list = u;
10608 }
10609 }
10610 else
10611 list = tree_cons (t, NULL_TREE, list);
10612
10613 if (c_parser_next_token_is_not (parser, CPP_COMMA))
10614 break;
10615
10616 c_parser_consume_token (parser);
10617 }
10618
10619 return list;
10620 }
10621
10622 /* Similarly, but expect leading and trailing parenthesis. This is a very
10623 common case for OpenACC and OpenMP clauses. */
10624
10625 static tree
10626 c_parser_omp_var_list_parens (c_parser *parser, enum omp_clause_code kind,
10627 tree list)
10628 {
10629 /* The clauses location. */
10630 location_t loc = c_parser_peek_token (parser)->location;
10631
10632 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10633 {
10634 list = c_parser_omp_variable_list (parser, loc, kind, list);
10635 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10636 }
10637 return list;
10638 }
10639
10640 /* OpenACC 2.0:
10641 copy ( variable-list )
10642 copyin ( variable-list )
10643 copyout ( variable-list )
10644 create ( variable-list )
10645 delete ( variable-list )
10646 present ( variable-list )
10647 present_or_copy ( variable-list )
10648 pcopy ( variable-list )
10649 present_or_copyin ( variable-list )
10650 pcopyin ( variable-list )
10651 present_or_copyout ( variable-list )
10652 pcopyout ( variable-list )
10653 present_or_create ( variable-list )
10654 pcreate ( variable-list ) */
10655
10656 static tree
10657 c_parser_oacc_data_clause (c_parser *parser, pragma_omp_clause c_kind,
10658 tree list)
10659 {
10660 enum gomp_map_kind kind;
10661 switch (c_kind)
10662 {
10663 case PRAGMA_OACC_CLAUSE_COPY:
10664 kind = GOMP_MAP_FORCE_TOFROM;
10665 break;
10666 case PRAGMA_OACC_CLAUSE_COPYIN:
10667 kind = GOMP_MAP_FORCE_TO;
10668 break;
10669 case PRAGMA_OACC_CLAUSE_COPYOUT:
10670 kind = GOMP_MAP_FORCE_FROM;
10671 break;
10672 case PRAGMA_OACC_CLAUSE_CREATE:
10673 kind = GOMP_MAP_FORCE_ALLOC;
10674 break;
10675 case PRAGMA_OACC_CLAUSE_DELETE:
10676 kind = GOMP_MAP_FORCE_DEALLOC;
10677 break;
10678 case PRAGMA_OACC_CLAUSE_DEVICE:
10679 kind = GOMP_MAP_FORCE_TO;
10680 break;
10681 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
10682 kind = GOMP_MAP_DEVICE_RESIDENT;
10683 break;
10684 case PRAGMA_OACC_CLAUSE_HOST:
10685 case PRAGMA_OACC_CLAUSE_SELF:
10686 kind = GOMP_MAP_FORCE_FROM;
10687 break;
10688 case PRAGMA_OACC_CLAUSE_LINK:
10689 kind = GOMP_MAP_LINK;
10690 break;
10691 case PRAGMA_OACC_CLAUSE_PRESENT:
10692 kind = GOMP_MAP_FORCE_PRESENT;
10693 break;
10694 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
10695 kind = GOMP_MAP_TOFROM;
10696 break;
10697 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
10698 kind = GOMP_MAP_TO;
10699 break;
10700 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
10701 kind = GOMP_MAP_FROM;
10702 break;
10703 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
10704 kind = GOMP_MAP_ALLOC;
10705 break;
10706 default:
10707 gcc_unreachable ();
10708 }
10709 tree nl, c;
10710 nl = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_MAP, list);
10711
10712 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
10713 OMP_CLAUSE_SET_MAP_KIND (c, kind);
10714
10715 return nl;
10716 }
10717
10718 /* OpenACC 2.0:
10719 deviceptr ( variable-list ) */
10720
10721 static tree
10722 c_parser_oacc_data_clause_deviceptr (c_parser *parser, tree list)
10723 {
10724 location_t loc = c_parser_peek_token (parser)->location;
10725 tree vars, t;
10726
10727 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
10728 c_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
10729 variable-list must only allow for pointer variables. */
10730 vars = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
10731 for (t = vars; t && t; t = TREE_CHAIN (t))
10732 {
10733 tree v = TREE_PURPOSE (t);
10734
10735 /* FIXME diagnostics: Ideally we should keep individual
10736 locations for all the variables in the var list to make the
10737 following errors more precise. Perhaps
10738 c_parser_omp_var_list_parens() should construct a list of
10739 locations to go along with the var list. */
10740
10741 if (!VAR_P (v))
10742 error_at (loc, "%qD is not a variable", v);
10743 else if (TREE_TYPE (v) == error_mark_node)
10744 ;
10745 else if (!POINTER_TYPE_P (TREE_TYPE (v)))
10746 error_at (loc, "%qD is not a pointer variable", v);
10747
10748 tree u = build_omp_clause (loc, OMP_CLAUSE_MAP);
10749 OMP_CLAUSE_SET_MAP_KIND (u, GOMP_MAP_FORCE_DEVICEPTR);
10750 OMP_CLAUSE_DECL (u) = v;
10751 OMP_CLAUSE_CHAIN (u) = list;
10752 list = u;
10753 }
10754
10755 return list;
10756 }
10757
10758 /* OpenACC 2.0, OpenMP 3.0:
10759 collapse ( constant-expression ) */
10760
10761 static tree
10762 c_parser_omp_clause_collapse (c_parser *parser, tree list)
10763 {
10764 tree c, num = error_mark_node;
10765 HOST_WIDE_INT n;
10766 location_t loc;
10767
10768 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse");
10769
10770 loc = c_parser_peek_token (parser)->location;
10771 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10772 {
10773 num = c_parser_expr_no_commas (parser, NULL).value;
10774 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10775 }
10776 if (num == error_mark_node)
10777 return list;
10778 mark_exp_read (num);
10779 num = c_fully_fold (num, false, NULL);
10780 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
10781 || !tree_fits_shwi_p (num)
10782 || (n = tree_to_shwi (num)) <= 0
10783 || (int) n != n)
10784 {
10785 error_at (loc,
10786 "collapse argument needs positive constant integer expression");
10787 return list;
10788 }
10789 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
10790 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
10791 OMP_CLAUSE_CHAIN (c) = list;
10792 return c;
10793 }
10794
10795 /* OpenMP 2.5:
10796 copyin ( variable-list ) */
10797
10798 static tree
10799 c_parser_omp_clause_copyin (c_parser *parser, tree list)
10800 {
10801 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_COPYIN, list);
10802 }
10803
10804 /* OpenMP 2.5:
10805 copyprivate ( variable-list ) */
10806
10807 static tree
10808 c_parser_omp_clause_copyprivate (c_parser *parser, tree list)
10809 {
10810 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_COPYPRIVATE, list);
10811 }
10812
10813 /* OpenMP 2.5:
10814 default ( shared | none )
10815
10816 OpenACC 2.0:
10817 default (none) */
10818
10819 static tree
10820 c_parser_omp_clause_default (c_parser *parser, tree list, bool is_oacc)
10821 {
10822 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
10823 location_t loc = c_parser_peek_token (parser)->location;
10824 tree c;
10825
10826 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10827 return list;
10828 if (c_parser_next_token_is (parser, CPP_NAME))
10829 {
10830 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
10831
10832 switch (p[0])
10833 {
10834 case 'n':
10835 if (strcmp ("none", p) != 0)
10836 goto invalid_kind;
10837 kind = OMP_CLAUSE_DEFAULT_NONE;
10838 break;
10839
10840 case 's':
10841 if (strcmp ("shared", p) != 0 || is_oacc)
10842 goto invalid_kind;
10843 kind = OMP_CLAUSE_DEFAULT_SHARED;
10844 break;
10845
10846 default:
10847 goto invalid_kind;
10848 }
10849
10850 c_parser_consume_token (parser);
10851 }
10852 else
10853 {
10854 invalid_kind:
10855 if (is_oacc)
10856 c_parser_error (parser, "expected %<none%>");
10857 else
10858 c_parser_error (parser, "expected %<none%> or %<shared%>");
10859 }
10860 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10861
10862 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
10863 return list;
10864
10865 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default");
10866 c = build_omp_clause (loc, OMP_CLAUSE_DEFAULT);
10867 OMP_CLAUSE_CHAIN (c) = list;
10868 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
10869
10870 return c;
10871 }
10872
10873 /* OpenMP 2.5:
10874 firstprivate ( variable-list ) */
10875
10876 static tree
10877 c_parser_omp_clause_firstprivate (c_parser *parser, tree list)
10878 {
10879 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_FIRSTPRIVATE, list);
10880 }
10881
10882 /* OpenMP 3.1:
10883 final ( expression ) */
10884
10885 static tree
10886 c_parser_omp_clause_final (c_parser *parser, tree list)
10887 {
10888 location_t loc = c_parser_peek_token (parser)->location;
10889 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
10890 {
10891 tree t = c_parser_paren_condition (parser);
10892 tree c;
10893
10894 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final");
10895
10896 c = build_omp_clause (loc, OMP_CLAUSE_FINAL);
10897 OMP_CLAUSE_FINAL_EXPR (c) = t;
10898 OMP_CLAUSE_CHAIN (c) = list;
10899 list = c;
10900 }
10901 else
10902 c_parser_error (parser, "expected %<(%>");
10903
10904 return list;
10905 }
10906
10907 /* OpenACC, OpenMP 2.5:
10908 if ( expression )
10909
10910 OpenMP 4.5:
10911 if ( directive-name-modifier : expression )
10912
10913 directive-name-modifier:
10914 parallel | task | taskloop | target data | target | target update
10915 | target enter data | target exit data */
10916
10917 static tree
10918 c_parser_omp_clause_if (c_parser *parser, tree list, bool is_omp)
10919 {
10920 location_t location = c_parser_peek_token (parser)->location;
10921 enum tree_code if_modifier = ERROR_MARK;
10922
10923 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10924 return list;
10925
10926 if (is_omp && c_parser_next_token_is (parser, CPP_NAME))
10927 {
10928 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
10929 int n = 2;
10930 if (strcmp (p, "parallel") == 0)
10931 if_modifier = OMP_PARALLEL;
10932 else if (strcmp (p, "task") == 0)
10933 if_modifier = OMP_TASK;
10934 else if (strcmp (p, "taskloop") == 0)
10935 if_modifier = OMP_TASKLOOP;
10936 else if (strcmp (p, "target") == 0)
10937 {
10938 if_modifier = OMP_TARGET;
10939 if (c_parser_peek_2nd_token (parser)->type == CPP_NAME)
10940 {
10941 p = IDENTIFIER_POINTER (c_parser_peek_2nd_token (parser)->value);
10942 if (strcmp ("data", p) == 0)
10943 if_modifier = OMP_TARGET_DATA;
10944 else if (strcmp ("update", p) == 0)
10945 if_modifier = OMP_TARGET_UPDATE;
10946 else if (strcmp ("enter", p) == 0)
10947 if_modifier = OMP_TARGET_ENTER_DATA;
10948 else if (strcmp ("exit", p) == 0)
10949 if_modifier = OMP_TARGET_EXIT_DATA;
10950 if (if_modifier != OMP_TARGET)
10951 {
10952 n = 3;
10953 c_parser_consume_token (parser);
10954 }
10955 else
10956 {
10957 location_t loc = c_parser_peek_2nd_token (parser)->location;
10958 error_at (loc, "expected %<data%>, %<update%>, %<enter%> "
10959 "or %<exit%>");
10960 if_modifier = ERROR_MARK;
10961 }
10962 if (if_modifier == OMP_TARGET_ENTER_DATA
10963 || if_modifier == OMP_TARGET_EXIT_DATA)
10964 {
10965 if (c_parser_peek_2nd_token (parser)->type == CPP_NAME)
10966 {
10967 p = IDENTIFIER_POINTER
10968 (c_parser_peek_2nd_token (parser)->value);
10969 if (strcmp ("data", p) == 0)
10970 n = 4;
10971 }
10972 if (n == 4)
10973 c_parser_consume_token (parser);
10974 else
10975 {
10976 location_t loc
10977 = c_parser_peek_2nd_token (parser)->location;
10978 error_at (loc, "expected %<data%>");
10979 if_modifier = ERROR_MARK;
10980 }
10981 }
10982 }
10983 }
10984 if (if_modifier != ERROR_MARK)
10985 {
10986 if (c_parser_peek_2nd_token (parser)->type == CPP_COLON)
10987 {
10988 c_parser_consume_token (parser);
10989 c_parser_consume_token (parser);
10990 }
10991 else
10992 {
10993 if (n > 2)
10994 {
10995 location_t loc = c_parser_peek_2nd_token (parser)->location;
10996 error_at (loc, "expected %<:%>");
10997 }
10998 if_modifier = ERROR_MARK;
10999 }
11000 }
11001 }
11002
11003 tree t = c_parser_condition (parser), c;
11004 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11005
11006 for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
11007 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IF)
11008 {
11009 if (if_modifier != ERROR_MARK
11010 && OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
11011 {
11012 const char *p = NULL;
11013 switch (if_modifier)
11014 {
11015 case OMP_PARALLEL: p = "parallel"; break;
11016 case OMP_TASK: p = "task"; break;
11017 case OMP_TASKLOOP: p = "taskloop"; break;
11018 case OMP_TARGET_DATA: p = "target data"; break;
11019 case OMP_TARGET: p = "target"; break;
11020 case OMP_TARGET_UPDATE: p = "target update"; break;
11021 case OMP_TARGET_ENTER_DATA: p = "enter data"; break;
11022 case OMP_TARGET_EXIT_DATA: p = "exit data"; break;
11023 default: gcc_unreachable ();
11024 }
11025 error_at (location, "too many %<if%> clauses with %qs modifier",
11026 p);
11027 return list;
11028 }
11029 else if (OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
11030 {
11031 if (!is_omp)
11032 error_at (location, "too many %<if%> clauses");
11033 else
11034 error_at (location, "too many %<if%> clauses without modifier");
11035 return list;
11036 }
11037 else if (if_modifier == ERROR_MARK
11038 || OMP_CLAUSE_IF_MODIFIER (c) == ERROR_MARK)
11039 {
11040 error_at (location, "if any %<if%> clause has modifier, then all "
11041 "%<if%> clauses have to use modifier");
11042 return list;
11043 }
11044 }
11045
11046 c = build_omp_clause (location, OMP_CLAUSE_IF);
11047 OMP_CLAUSE_IF_MODIFIER (c) = if_modifier;
11048 OMP_CLAUSE_IF_EXPR (c) = t;
11049 OMP_CLAUSE_CHAIN (c) = list;
11050 return c;
11051 }
11052
11053 /* OpenMP 2.5:
11054 lastprivate ( variable-list ) */
11055
11056 static tree
11057 c_parser_omp_clause_lastprivate (c_parser *parser, tree list)
11058 {
11059 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_LASTPRIVATE, list);
11060 }
11061
11062 /* OpenMP 3.1:
11063 mergeable */
11064
11065 static tree
11066 c_parser_omp_clause_mergeable (c_parser *parser ATTRIBUTE_UNUSED, tree list)
11067 {
11068 tree c;
11069
11070 /* FIXME: Should we allow duplicates? */
11071 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable");
11072
11073 c = build_omp_clause (c_parser_peek_token (parser)->location,
11074 OMP_CLAUSE_MERGEABLE);
11075 OMP_CLAUSE_CHAIN (c) = list;
11076
11077 return c;
11078 }
11079
11080 /* OpenMP 2.5:
11081 nowait */
11082
11083 static tree
11084 c_parser_omp_clause_nowait (c_parser *parser ATTRIBUTE_UNUSED, tree list)
11085 {
11086 tree c;
11087 location_t loc = c_parser_peek_token (parser)->location;
11088
11089 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait");
11090
11091 c = build_omp_clause (loc, OMP_CLAUSE_NOWAIT);
11092 OMP_CLAUSE_CHAIN (c) = list;
11093 return c;
11094 }
11095
11096 /* OpenACC:
11097 num_gangs ( expression ) */
11098
11099 static tree
11100 c_parser_omp_clause_num_gangs (c_parser *parser, tree list)
11101 {
11102 location_t num_gangs_loc = c_parser_peek_token (parser)->location;
11103 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11104 {
11105 location_t expr_loc = c_parser_peek_token (parser)->location;
11106 tree c, t = c_parser_expression (parser).value;
11107 mark_exp_read (t);
11108 t = c_fully_fold (t, false, NULL);
11109
11110 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11111
11112 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
11113 {
11114 c_parser_error (parser, "expected integer expression");
11115 return list;
11116 }
11117
11118 /* Attempt to statically determine when the number isn't positive. */
11119 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
11120 build_int_cst (TREE_TYPE (t), 0));
11121 protected_set_expr_location (c, expr_loc);
11122 if (c == boolean_true_node)
11123 {
11124 warning_at (expr_loc, 0,
11125 "%<num_gangs%> value must be positive");
11126 t = integer_one_node;
11127 }
11128
11129 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_GANGS, "num_gangs");
11130
11131 c = build_omp_clause (num_gangs_loc, OMP_CLAUSE_NUM_GANGS);
11132 OMP_CLAUSE_NUM_GANGS_EXPR (c) = t;
11133 OMP_CLAUSE_CHAIN (c) = list;
11134 list = c;
11135 }
11136
11137 return list;
11138 }
11139
11140 /* OpenMP 2.5:
11141 num_threads ( expression ) */
11142
11143 static tree
11144 c_parser_omp_clause_num_threads (c_parser *parser, tree list)
11145 {
11146 location_t num_threads_loc = c_parser_peek_token (parser)->location;
11147 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11148 {
11149 location_t expr_loc = c_parser_peek_token (parser)->location;
11150 tree c, t = c_parser_expression (parser).value;
11151 mark_exp_read (t);
11152 t = c_fully_fold (t, false, NULL);
11153
11154 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11155
11156 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
11157 {
11158 c_parser_error (parser, "expected integer expression");
11159 return list;
11160 }
11161
11162 /* Attempt to statically determine when the number isn't positive. */
11163 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
11164 build_int_cst (TREE_TYPE (t), 0));
11165 protected_set_expr_location (c, expr_loc);
11166 if (c == boolean_true_node)
11167 {
11168 warning_at (expr_loc, 0,
11169 "%<num_threads%> value must be positive");
11170 t = integer_one_node;
11171 }
11172
11173 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS, "num_threads");
11174
11175 c = build_omp_clause (num_threads_loc, OMP_CLAUSE_NUM_THREADS);
11176 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
11177 OMP_CLAUSE_CHAIN (c) = list;
11178 list = c;
11179 }
11180
11181 return list;
11182 }
11183
11184 /* OpenMP 4.5:
11185 num_tasks ( expression ) */
11186
11187 static tree
11188 c_parser_omp_clause_num_tasks (c_parser *parser, tree list)
11189 {
11190 location_t num_tasks_loc = c_parser_peek_token (parser)->location;
11191 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11192 {
11193 location_t expr_loc = c_parser_peek_token (parser)->location;
11194 tree c, t = c_parser_expression (parser).value;
11195 mark_exp_read (t);
11196 t = c_fully_fold (t, false, NULL);
11197
11198 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11199
11200 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
11201 {
11202 c_parser_error (parser, "expected integer expression");
11203 return list;
11204 }
11205
11206 /* Attempt to statically determine when the number isn't positive. */
11207 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
11208 build_int_cst (TREE_TYPE (t), 0));
11209 if (CAN_HAVE_LOCATION_P (c))
11210 SET_EXPR_LOCATION (c, expr_loc);
11211 if (c == boolean_true_node)
11212 {
11213 warning_at (expr_loc, 0, "%<num_tasks%> value must be positive");
11214 t = integer_one_node;
11215 }
11216
11217 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TASKS, "num_tasks");
11218
11219 c = build_omp_clause (num_tasks_loc, OMP_CLAUSE_NUM_TASKS);
11220 OMP_CLAUSE_NUM_TASKS_EXPR (c) = t;
11221 OMP_CLAUSE_CHAIN (c) = list;
11222 list = c;
11223 }
11224
11225 return list;
11226 }
11227
11228 /* OpenMP 4.5:
11229 grainsize ( expression ) */
11230
11231 static tree
11232 c_parser_omp_clause_grainsize (c_parser *parser, tree list)
11233 {
11234 location_t grainsize_loc = c_parser_peek_token (parser)->location;
11235 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11236 {
11237 location_t expr_loc = c_parser_peek_token (parser)->location;
11238 tree c, t = c_parser_expression (parser).value;
11239 mark_exp_read (t);
11240 t = c_fully_fold (t, false, NULL);
11241
11242 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11243
11244 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
11245 {
11246 c_parser_error (parser, "expected integer expression");
11247 return list;
11248 }
11249
11250 /* Attempt to statically determine when the number isn't positive. */
11251 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
11252 build_int_cst (TREE_TYPE (t), 0));
11253 if (CAN_HAVE_LOCATION_P (c))
11254 SET_EXPR_LOCATION (c, expr_loc);
11255 if (c == boolean_true_node)
11256 {
11257 warning_at (expr_loc, 0, "%<grainsize%> value must be positive");
11258 t = integer_one_node;
11259 }
11260
11261 check_no_duplicate_clause (list, OMP_CLAUSE_GRAINSIZE, "grainsize");
11262
11263 c = build_omp_clause (grainsize_loc, OMP_CLAUSE_GRAINSIZE);
11264 OMP_CLAUSE_GRAINSIZE_EXPR (c) = t;
11265 OMP_CLAUSE_CHAIN (c) = list;
11266 list = c;
11267 }
11268
11269 return list;
11270 }
11271
11272 /* OpenMP 4.5:
11273 priority ( expression ) */
11274
11275 static tree
11276 c_parser_omp_clause_priority (c_parser *parser, tree list)
11277 {
11278 location_t priority_loc = c_parser_peek_token (parser)->location;
11279 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11280 {
11281 location_t expr_loc = c_parser_peek_token (parser)->location;
11282 tree c, t = c_parser_expression (parser).value;
11283 mark_exp_read (t);
11284 t = c_fully_fold (t, false, NULL);
11285
11286 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11287
11288 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
11289 {
11290 c_parser_error (parser, "expected integer expression");
11291 return list;
11292 }
11293
11294 /* Attempt to statically determine when the number isn't
11295 non-negative. */
11296 c = fold_build2_loc (expr_loc, LT_EXPR, boolean_type_node, t,
11297 build_int_cst (TREE_TYPE (t), 0));
11298 if (CAN_HAVE_LOCATION_P (c))
11299 SET_EXPR_LOCATION (c, expr_loc);
11300 if (c == boolean_true_node)
11301 {
11302 warning_at (expr_loc, 0, "%<priority%> value must be non-negative");
11303 t = integer_one_node;
11304 }
11305
11306 check_no_duplicate_clause (list, OMP_CLAUSE_PRIORITY, "priority");
11307
11308 c = build_omp_clause (priority_loc, OMP_CLAUSE_PRIORITY);
11309 OMP_CLAUSE_PRIORITY_EXPR (c) = t;
11310 OMP_CLAUSE_CHAIN (c) = list;
11311 list = c;
11312 }
11313
11314 return list;
11315 }
11316
11317 /* OpenMP 4.5:
11318 hint ( expression ) */
11319
11320 static tree
11321 c_parser_omp_clause_hint (c_parser *parser, tree list)
11322 {
11323 location_t hint_loc = c_parser_peek_token (parser)->location;
11324 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11325 {
11326 tree c, t = c_parser_expression (parser).value;
11327 mark_exp_read (t);
11328 t = c_fully_fold (t, false, NULL);
11329
11330 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11331
11332 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
11333 {
11334 c_parser_error (parser, "expected integer expression");
11335 return list;
11336 }
11337
11338 check_no_duplicate_clause (list, OMP_CLAUSE_HINT, "hint");
11339
11340 c = build_omp_clause (hint_loc, OMP_CLAUSE_HINT);
11341 OMP_CLAUSE_HINT_EXPR (c) = t;
11342 OMP_CLAUSE_CHAIN (c) = list;
11343 list = c;
11344 }
11345
11346 return list;
11347 }
11348
11349 /* OpenMP 4.5:
11350 defaultmap ( tofrom : scalar ) */
11351
11352 static tree
11353 c_parser_omp_clause_defaultmap (c_parser *parser, tree list)
11354 {
11355 location_t loc = c_parser_peek_token (parser)->location;
11356 tree c;
11357 const char *p;
11358
11359 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11360 return list;
11361 if (!c_parser_next_token_is (parser, CPP_NAME))
11362 {
11363 c_parser_error (parser, "expected %<tofrom%>");
11364 goto out_err;
11365 }
11366 p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
11367 if (strcmp (p, "tofrom") != 0)
11368 {
11369 c_parser_error (parser, "expected %<tofrom%>");
11370 goto out_err;
11371 }
11372 c_parser_consume_token (parser);
11373 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
11374 goto out_err;
11375 if (!c_parser_next_token_is (parser, CPP_NAME))
11376 {
11377 c_parser_error (parser, "expected %<scalar%>");
11378 goto out_err;
11379 }
11380 p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
11381 if (strcmp (p, "scalar") != 0)
11382 {
11383 c_parser_error (parser, "expected %<scalar%>");
11384 goto out_err;
11385 }
11386 c_parser_consume_token (parser);
11387 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11388 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULTMAP, "defaultmap");
11389 c = build_omp_clause (loc, OMP_CLAUSE_DEFAULTMAP);
11390 OMP_CLAUSE_CHAIN (c) = list;
11391 return c;
11392
11393 out_err:
11394 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11395 return list;
11396 }
11397
11398 /* OpenACC 2.0:
11399 use_device ( variable-list )
11400
11401 OpenMP 4.5:
11402 use_device_ptr ( variable-list ) */
11403
11404 static tree
11405 c_parser_omp_clause_use_device_ptr (c_parser *parser, tree list)
11406 {
11407 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_USE_DEVICE_PTR,
11408 list);
11409 }
11410
11411 /* OpenMP 4.5:
11412 is_device_ptr ( variable-list ) */
11413
11414 static tree
11415 c_parser_omp_clause_is_device_ptr (c_parser *parser, tree list)
11416 {
11417 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_IS_DEVICE_PTR, list);
11418 }
11419
11420 /* OpenACC:
11421 num_workers ( expression ) */
11422
11423 static tree
11424 c_parser_omp_clause_num_workers (c_parser *parser, tree list)
11425 {
11426 location_t num_workers_loc = c_parser_peek_token (parser)->location;
11427 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11428 {
11429 location_t expr_loc = c_parser_peek_token (parser)->location;
11430 tree c, t = c_parser_expression (parser).value;
11431 mark_exp_read (t);
11432 t = c_fully_fold (t, false, NULL);
11433
11434 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11435
11436 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
11437 {
11438 c_parser_error (parser, "expected integer expression");
11439 return list;
11440 }
11441
11442 /* Attempt to statically determine when the number isn't positive. */
11443 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
11444 build_int_cst (TREE_TYPE (t), 0));
11445 protected_set_expr_location (c, expr_loc);
11446 if (c == boolean_true_node)
11447 {
11448 warning_at (expr_loc, 0,
11449 "%<num_workers%> value must be positive");
11450 t = integer_one_node;
11451 }
11452
11453 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_WORKERS, "num_workers");
11454
11455 c = build_omp_clause (num_workers_loc, OMP_CLAUSE_NUM_WORKERS);
11456 OMP_CLAUSE_NUM_WORKERS_EXPR (c) = t;
11457 OMP_CLAUSE_CHAIN (c) = list;
11458 list = c;
11459 }
11460
11461 return list;
11462 }
11463
11464 /* OpenACC:
11465
11466 gang [( gang-arg-list )]
11467 worker [( [num:] int-expr )]
11468 vector [( [length:] int-expr )]
11469
11470 where gang-arg is one of:
11471
11472 [num:] int-expr
11473 static: size-expr
11474
11475 and size-expr may be:
11476
11477 *
11478 int-expr
11479 */
11480
11481 static tree
11482 c_parser_oacc_shape_clause (c_parser *parser, omp_clause_code kind,
11483 const char *str, tree list)
11484 {
11485 const char *id = "num";
11486 tree ops[2] = { NULL_TREE, NULL_TREE }, c;
11487 location_t loc = c_parser_peek_token (parser)->location;
11488
11489 if (kind == OMP_CLAUSE_VECTOR)
11490 id = "length";
11491
11492 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
11493 {
11494 c_parser_consume_token (parser);
11495
11496 do
11497 {
11498 c_token *next = c_parser_peek_token (parser);
11499 int idx = 0;
11500
11501 /* Gang static argument. */
11502 if (kind == OMP_CLAUSE_GANG
11503 && c_parser_next_token_is_keyword (parser, RID_STATIC))
11504 {
11505 c_parser_consume_token (parser);
11506
11507 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
11508 goto cleanup_error;
11509
11510 idx = 1;
11511 if (ops[idx] != NULL_TREE)
11512 {
11513 c_parser_error (parser, "too many %<static%> arguments");
11514 goto cleanup_error;
11515 }
11516
11517 /* Check for the '*' argument. */
11518 if (c_parser_next_token_is (parser, CPP_MULT)
11519 && (c_parser_peek_2nd_token (parser)->type == CPP_COMMA
11520 || c_parser_peek_2nd_token (parser)->type
11521 == CPP_CLOSE_PAREN))
11522 {
11523 c_parser_consume_token (parser);
11524 ops[idx] = integer_minus_one_node;
11525
11526 if (c_parser_next_token_is (parser, CPP_COMMA))
11527 {
11528 c_parser_consume_token (parser);
11529 continue;
11530 }
11531 else
11532 break;
11533 }
11534 }
11535 /* Worker num: argument and vector length: arguments. */
11536 else if (c_parser_next_token_is (parser, CPP_NAME)
11537 && strcmp (id, IDENTIFIER_POINTER (next->value)) == 0
11538 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
11539 {
11540 c_parser_consume_token (parser); /* id */
11541 c_parser_consume_token (parser); /* ':' */
11542 }
11543
11544 /* Now collect the actual argument. */
11545 if (ops[idx] != NULL_TREE)
11546 {
11547 c_parser_error (parser, "unexpected argument");
11548 goto cleanup_error;
11549 }
11550
11551 location_t expr_loc = c_parser_peek_token (parser)->location;
11552 tree expr = c_parser_expr_no_commas (parser, NULL).value;
11553 if (expr == error_mark_node)
11554 goto cleanup_error;
11555
11556 mark_exp_read (expr);
11557 expr = c_fully_fold (expr, false, NULL);
11558
11559 /* Attempt to statically determine when the number isn't a
11560 positive integer. */
11561
11562 if (!INTEGRAL_TYPE_P (TREE_TYPE (expr)))
11563 {
11564 c_parser_error (parser, "expected integer expression");
11565 return list;
11566 }
11567
11568 tree c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, expr,
11569 build_int_cst (TREE_TYPE (expr), 0));
11570 if (c == boolean_true_node)
11571 {
11572 warning_at (loc, 0,
11573 "%<%s%> value must be positive", str);
11574 expr = integer_one_node;
11575 }
11576
11577 ops[idx] = expr;
11578
11579 if (kind == OMP_CLAUSE_GANG
11580 && c_parser_next_token_is (parser, CPP_COMMA))
11581 {
11582 c_parser_consume_token (parser);
11583 continue;
11584 }
11585 break;
11586 }
11587 while (1);
11588
11589 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
11590 goto cleanup_error;
11591 }
11592
11593 check_no_duplicate_clause (list, kind, str);
11594
11595 c = build_omp_clause (loc, kind);
11596
11597 if (ops[1])
11598 OMP_CLAUSE_OPERAND (c, 1) = ops[1];
11599
11600 OMP_CLAUSE_OPERAND (c, 0) = ops[0];
11601 OMP_CLAUSE_CHAIN (c) = list;
11602
11603 return c;
11604
11605 cleanup_error:
11606 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
11607 return list;
11608 }
11609
11610 /* OpenACC:
11611 auto
11612 independent
11613 nohost
11614 seq */
11615
11616 static tree
11617 c_parser_oacc_simple_clause (c_parser *parser, enum omp_clause_code code,
11618 tree list)
11619 {
11620 check_no_duplicate_clause (list, code, omp_clause_code_name[code]);
11621
11622 tree c = build_omp_clause (c_parser_peek_token (parser)->location, code);
11623 OMP_CLAUSE_CHAIN (c) = list;
11624
11625 return c;
11626 }
11627
11628 /* OpenACC:
11629 async [( int-expr )] */
11630
11631 static tree
11632 c_parser_oacc_clause_async (c_parser *parser, tree list)
11633 {
11634 tree c, t;
11635 location_t loc = c_parser_peek_token (parser)->location;
11636
11637 t = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
11638
11639 if (c_parser_peek_token (parser)->type == CPP_OPEN_PAREN)
11640 {
11641 c_parser_consume_token (parser);
11642
11643 t = c_parser_expression (parser).value;
11644 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
11645 c_parser_error (parser, "expected integer expression");
11646 else if (t == error_mark_node
11647 || !c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
11648 return list;
11649 }
11650 else
11651 t = c_fully_fold (t, false, NULL);
11652
11653 check_no_duplicate_clause (list, OMP_CLAUSE_ASYNC, "async");
11654
11655 c = build_omp_clause (loc, OMP_CLAUSE_ASYNC);
11656 OMP_CLAUSE_ASYNC_EXPR (c) = t;
11657 OMP_CLAUSE_CHAIN (c) = list;
11658 list = c;
11659
11660 return list;
11661 }
11662
11663 /* OpenACC 2.0:
11664 tile ( size-expr-list ) */
11665
11666 static tree
11667 c_parser_oacc_clause_tile (c_parser *parser, tree list)
11668 {
11669 tree c, expr = error_mark_node;
11670 location_t loc, expr_loc;
11671 tree tile = NULL_TREE;
11672
11673 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile");
11674
11675 loc = c_parser_peek_token (parser)->location;
11676 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11677 return list;
11678
11679 do
11680 {
11681 if (c_parser_next_token_is (parser, CPP_MULT)
11682 && (c_parser_peek_2nd_token (parser)->type == CPP_COMMA
11683 || c_parser_peek_2nd_token (parser)->type == CPP_CLOSE_PAREN))
11684 {
11685 c_parser_consume_token (parser);
11686 expr = integer_minus_one_node;
11687 }
11688 else
11689 {
11690 expr_loc = c_parser_peek_token (parser)->location;
11691 expr = c_parser_expr_no_commas (parser, NULL).value;
11692
11693 if (expr == error_mark_node)
11694 {
11695 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
11696 "expected %<)%>");
11697 return list;
11698 }
11699
11700 if (!INTEGRAL_TYPE_P (TREE_TYPE (expr)))
11701 {
11702 c_parser_error (parser, "%<tile%> value must be integral");
11703 return list;
11704 }
11705
11706 mark_exp_read (expr);
11707 expr = c_fully_fold (expr, false, NULL);
11708
11709 /* Attempt to statically determine when expr isn't positive. */
11710 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, expr,
11711 build_int_cst (TREE_TYPE (expr), 0));
11712 protected_set_expr_location (c, expr_loc);
11713 if (c == boolean_true_node)
11714 {
11715 warning_at (expr_loc, 0,"%<tile%> value must be positive");
11716 expr = integer_one_node;
11717 }
11718 }
11719
11720 tile = tree_cons (NULL_TREE, expr, tile);
11721 if (c_parser_next_token_is (parser, CPP_COMMA))
11722 c_parser_consume_token (parser);
11723 }
11724 while (c_parser_next_token_is_not (parser, CPP_CLOSE_PAREN));
11725
11726 /* Consume the trailing ')'. */
11727 c_parser_consume_token (parser);
11728
11729 c = build_omp_clause (loc, OMP_CLAUSE_TILE);
11730 tile = nreverse (tile);
11731 OMP_CLAUSE_TILE_LIST (c) = tile;
11732 OMP_CLAUSE_CHAIN (c) = list;
11733 return c;
11734 }
11735
11736 /* OpenACC:
11737 wait ( int-expr-list ) */
11738
11739 static tree
11740 c_parser_oacc_clause_wait (c_parser *parser, tree list)
11741 {
11742 location_t clause_loc = c_parser_peek_token (parser)->location;
11743
11744 if (c_parser_peek_token (parser)->type == CPP_OPEN_PAREN)
11745 list = c_parser_oacc_wait_list (parser, clause_loc, list);
11746
11747 return list;
11748 }
11749
11750 /* OpenMP 2.5:
11751 ordered
11752
11753 OpenMP 4.5:
11754 ordered ( constant-expression ) */
11755
11756 static tree
11757 c_parser_omp_clause_ordered (c_parser *parser, tree list)
11758 {
11759 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED, "ordered");
11760
11761 tree c, num = NULL_TREE;
11762 HOST_WIDE_INT n;
11763 location_t loc = c_parser_peek_token (parser)->location;
11764 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
11765 {
11766 c_parser_consume_token (parser);
11767 num = c_parser_expr_no_commas (parser, NULL).value;
11768 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11769 }
11770 if (num == error_mark_node)
11771 return list;
11772 if (num)
11773 {
11774 mark_exp_read (num);
11775 num = c_fully_fold (num, false, NULL);
11776 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
11777 || !tree_fits_shwi_p (num)
11778 || (n = tree_to_shwi (num)) <= 0
11779 || (int) n != n)
11780 {
11781 error_at (loc, "ordered argument needs positive "
11782 "constant integer expression");
11783 return list;
11784 }
11785 }
11786 c = build_omp_clause (loc, OMP_CLAUSE_ORDERED);
11787 OMP_CLAUSE_ORDERED_EXPR (c) = num;
11788 OMP_CLAUSE_CHAIN (c) = list;
11789 return c;
11790 }
11791
11792 /* OpenMP 2.5:
11793 private ( variable-list ) */
11794
11795 static tree
11796 c_parser_omp_clause_private (c_parser *parser, tree list)
11797 {
11798 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_PRIVATE, list);
11799 }
11800
11801 /* OpenMP 2.5:
11802 reduction ( reduction-operator : variable-list )
11803
11804 reduction-operator:
11805 One of: + * - & ^ | && ||
11806
11807 OpenMP 3.1:
11808
11809 reduction-operator:
11810 One of: + * - & ^ | && || max min
11811
11812 OpenMP 4.0:
11813
11814 reduction-operator:
11815 One of: + * - & ^ | && ||
11816 identifier */
11817
11818 static tree
11819 c_parser_omp_clause_reduction (c_parser *parser, tree list)
11820 {
11821 location_t clause_loc = c_parser_peek_token (parser)->location;
11822 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11823 {
11824 enum tree_code code = ERROR_MARK;
11825 tree reduc_id = NULL_TREE;
11826
11827 switch (c_parser_peek_token (parser)->type)
11828 {
11829 case CPP_PLUS:
11830 code = PLUS_EXPR;
11831 break;
11832 case CPP_MULT:
11833 code = MULT_EXPR;
11834 break;
11835 case CPP_MINUS:
11836 code = MINUS_EXPR;
11837 break;
11838 case CPP_AND:
11839 code = BIT_AND_EXPR;
11840 break;
11841 case CPP_XOR:
11842 code = BIT_XOR_EXPR;
11843 break;
11844 case CPP_OR:
11845 code = BIT_IOR_EXPR;
11846 break;
11847 case CPP_AND_AND:
11848 code = TRUTH_ANDIF_EXPR;
11849 break;
11850 case CPP_OR_OR:
11851 code = TRUTH_ORIF_EXPR;
11852 break;
11853 case CPP_NAME:
11854 {
11855 const char *p
11856 = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
11857 if (strcmp (p, "min") == 0)
11858 {
11859 code = MIN_EXPR;
11860 break;
11861 }
11862 if (strcmp (p, "max") == 0)
11863 {
11864 code = MAX_EXPR;
11865 break;
11866 }
11867 reduc_id = c_parser_peek_token (parser)->value;
11868 break;
11869 }
11870 default:
11871 c_parser_error (parser,
11872 "expected %<+%>, %<*%>, %<-%>, %<&%>, "
11873 "%<^%>, %<|%>, %<&&%>, %<||%>, %<min%> or %<max%>");
11874 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
11875 return list;
11876 }
11877 c_parser_consume_token (parser);
11878 reduc_id = c_omp_reduction_id (code, reduc_id);
11879 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
11880 {
11881 tree nl, c;
11882
11883 nl = c_parser_omp_variable_list (parser, clause_loc,
11884 OMP_CLAUSE_REDUCTION, list);
11885 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
11886 {
11887 tree d = OMP_CLAUSE_DECL (c), type;
11888 if (TREE_CODE (d) != TREE_LIST)
11889 type = TREE_TYPE (d);
11890 else
11891 {
11892 int cnt = 0;
11893 tree t;
11894 for (t = d; TREE_CODE (t) == TREE_LIST; t = TREE_CHAIN (t))
11895 cnt++;
11896 type = TREE_TYPE (t);
11897 while (cnt > 0)
11898 {
11899 if (TREE_CODE (type) != POINTER_TYPE
11900 && TREE_CODE (type) != ARRAY_TYPE)
11901 break;
11902 type = TREE_TYPE (type);
11903 cnt--;
11904 }
11905 }
11906 while (TREE_CODE (type) == ARRAY_TYPE)
11907 type = TREE_TYPE (type);
11908 OMP_CLAUSE_REDUCTION_CODE (c) = code;
11909 if (code == ERROR_MARK
11910 || !(INTEGRAL_TYPE_P (type)
11911 || TREE_CODE (type) == REAL_TYPE
11912 || TREE_CODE (type) == COMPLEX_TYPE))
11913 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c)
11914 = c_omp_reduction_lookup (reduc_id,
11915 TYPE_MAIN_VARIANT (type));
11916 }
11917
11918 list = nl;
11919 }
11920 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11921 }
11922 return list;
11923 }
11924
11925 /* OpenMP 2.5:
11926 schedule ( schedule-kind )
11927 schedule ( schedule-kind , expression )
11928
11929 schedule-kind:
11930 static | dynamic | guided | runtime | auto
11931
11932 OpenMP 4.5:
11933 schedule ( schedule-modifier : schedule-kind )
11934 schedule ( schedule-modifier [ , schedule-modifier ] : schedule-kind , expression )
11935
11936 schedule-modifier:
11937 simd
11938 monotonic
11939 nonmonotonic */
11940
11941 static tree
11942 c_parser_omp_clause_schedule (c_parser *parser, tree list)
11943 {
11944 tree c, t;
11945 location_t loc = c_parser_peek_token (parser)->location;
11946 int modifiers = 0, nmodifiers = 0;
11947
11948 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11949 return list;
11950
11951 c = build_omp_clause (loc, OMP_CLAUSE_SCHEDULE);
11952
11953 while (c_parser_next_token_is (parser, CPP_NAME))
11954 {
11955 tree kind = c_parser_peek_token (parser)->value;
11956 const char *p = IDENTIFIER_POINTER (kind);
11957 if (strcmp ("simd", p) == 0)
11958 OMP_CLAUSE_SCHEDULE_SIMD (c) = 1;
11959 else if (strcmp ("monotonic", p) == 0)
11960 modifiers |= OMP_CLAUSE_SCHEDULE_MONOTONIC;
11961 else if (strcmp ("nonmonotonic", p) == 0)
11962 modifiers |= OMP_CLAUSE_SCHEDULE_NONMONOTONIC;
11963 else
11964 break;
11965 c_parser_consume_token (parser);
11966 if (nmodifiers++ == 0
11967 && c_parser_next_token_is (parser, CPP_COMMA))
11968 c_parser_consume_token (parser);
11969 else
11970 {
11971 c_parser_require (parser, CPP_COLON, "expected %<:%>");
11972 break;
11973 }
11974 }
11975
11976 if ((modifiers & (OMP_CLAUSE_SCHEDULE_MONOTONIC
11977 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
11978 == (OMP_CLAUSE_SCHEDULE_MONOTONIC
11979 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
11980 {
11981 error_at (loc, "both %<monotonic%> and %<nonmonotonic%> modifiers "
11982 "specified");
11983 modifiers = 0;
11984 }
11985
11986 if (c_parser_next_token_is (parser, CPP_NAME))
11987 {
11988 tree kind = c_parser_peek_token (parser)->value;
11989 const char *p = IDENTIFIER_POINTER (kind);
11990
11991 switch (p[0])
11992 {
11993 case 'd':
11994 if (strcmp ("dynamic", p) != 0)
11995 goto invalid_kind;
11996 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
11997 break;
11998
11999 case 'g':
12000 if (strcmp ("guided", p) != 0)
12001 goto invalid_kind;
12002 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
12003 break;
12004
12005 case 'r':
12006 if (strcmp ("runtime", p) != 0)
12007 goto invalid_kind;
12008 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
12009 break;
12010
12011 default:
12012 goto invalid_kind;
12013 }
12014 }
12015 else if (c_parser_next_token_is_keyword (parser, RID_STATIC))
12016 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
12017 else if (c_parser_next_token_is_keyword (parser, RID_AUTO))
12018 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
12019 else
12020 goto invalid_kind;
12021
12022 c_parser_consume_token (parser);
12023 if (c_parser_next_token_is (parser, CPP_COMMA))
12024 {
12025 location_t here;
12026 c_parser_consume_token (parser);
12027
12028 here = c_parser_peek_token (parser)->location;
12029 t = c_parser_expr_no_commas (parser, NULL).value;
12030 mark_exp_read (t);
12031 t = c_fully_fold (t, false, NULL);
12032
12033 if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
12034 error_at (here, "schedule %<runtime%> does not take "
12035 "a %<chunk_size%> parameter");
12036 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
12037 error_at (here,
12038 "schedule %<auto%> does not take "
12039 "a %<chunk_size%> parameter");
12040 else if (TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE)
12041 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
12042 else
12043 c_parser_error (parser, "expected integer expression");
12044
12045 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12046 }
12047 else
12048 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
12049 "expected %<,%> or %<)%>");
12050
12051 OMP_CLAUSE_SCHEDULE_KIND (c)
12052 = (enum omp_clause_schedule_kind)
12053 (OMP_CLAUSE_SCHEDULE_KIND (c) | modifiers);
12054
12055 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
12056 OMP_CLAUSE_CHAIN (c) = list;
12057 return c;
12058
12059 invalid_kind:
12060 c_parser_error (parser, "invalid schedule kind");
12061 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
12062 return list;
12063 }
12064
12065 /* OpenMP 2.5:
12066 shared ( variable-list ) */
12067
12068 static tree
12069 c_parser_omp_clause_shared (c_parser *parser, tree list)
12070 {
12071 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_SHARED, list);
12072 }
12073
12074 /* OpenMP 3.0:
12075 untied */
12076
12077 static tree
12078 c_parser_omp_clause_untied (c_parser *parser ATTRIBUTE_UNUSED, tree list)
12079 {
12080 tree c;
12081
12082 /* FIXME: Should we allow duplicates? */
12083 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied");
12084
12085 c = build_omp_clause (c_parser_peek_token (parser)->location,
12086 OMP_CLAUSE_UNTIED);
12087 OMP_CLAUSE_CHAIN (c) = list;
12088
12089 return c;
12090 }
12091
12092 /* OpenACC:
12093 vector_length ( expression ) */
12094
12095 static tree
12096 c_parser_omp_clause_vector_length (c_parser *parser, tree list)
12097 {
12098 location_t vector_length_loc = c_parser_peek_token (parser)->location;
12099 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
12100 {
12101 location_t expr_loc = c_parser_peek_token (parser)->location;
12102 tree c, t = c_parser_expression (parser).value;
12103 mark_exp_read (t);
12104 t = c_fully_fold (t, false, NULL);
12105
12106 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12107
12108 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
12109 {
12110 c_parser_error (parser, "expected integer expression");
12111 return list;
12112 }
12113
12114 /* Attempt to statically determine when the number isn't positive. */
12115 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
12116 build_int_cst (TREE_TYPE (t), 0));
12117 protected_set_expr_location (c, expr_loc);
12118 if (c == boolean_true_node)
12119 {
12120 warning_at (expr_loc, 0,
12121 "%<vector_length%> value must be positive");
12122 t = integer_one_node;
12123 }
12124
12125 check_no_duplicate_clause (list, OMP_CLAUSE_VECTOR_LENGTH, "vector_length");
12126
12127 c = build_omp_clause (vector_length_loc, OMP_CLAUSE_VECTOR_LENGTH);
12128 OMP_CLAUSE_VECTOR_LENGTH_EXPR (c) = t;
12129 OMP_CLAUSE_CHAIN (c) = list;
12130 list = c;
12131 }
12132
12133 return list;
12134 }
12135
12136 /* OpenMP 4.0:
12137 inbranch
12138 notinbranch */
12139
12140 static tree
12141 c_parser_omp_clause_branch (c_parser *parser ATTRIBUTE_UNUSED,
12142 enum omp_clause_code code, tree list)
12143 {
12144 check_no_duplicate_clause (list, code, omp_clause_code_name[code]);
12145
12146 tree c = build_omp_clause (c_parser_peek_token (parser)->location, code);
12147 OMP_CLAUSE_CHAIN (c) = list;
12148
12149 return c;
12150 }
12151
12152 /* OpenMP 4.0:
12153 parallel
12154 for
12155 sections
12156 taskgroup */
12157
12158 static tree
12159 c_parser_omp_clause_cancelkind (c_parser *parser ATTRIBUTE_UNUSED,
12160 enum omp_clause_code code, tree list)
12161 {
12162 tree c = build_omp_clause (c_parser_peek_token (parser)->location, code);
12163 OMP_CLAUSE_CHAIN (c) = list;
12164
12165 return c;
12166 }
12167
12168 /* OpenMP 4.5:
12169 nogroup */
12170
12171 static tree
12172 c_parser_omp_clause_nogroup (c_parser *parser ATTRIBUTE_UNUSED, tree list)
12173 {
12174 check_no_duplicate_clause (list, OMP_CLAUSE_NOGROUP, "nogroup");
12175 tree c = build_omp_clause (c_parser_peek_token (parser)->location,
12176 OMP_CLAUSE_NOGROUP);
12177 OMP_CLAUSE_CHAIN (c) = list;
12178 return c;
12179 }
12180
12181 /* OpenMP 4.5:
12182 simd
12183 threads */
12184
12185 static tree
12186 c_parser_omp_clause_orderedkind (c_parser *parser ATTRIBUTE_UNUSED,
12187 enum omp_clause_code code, tree list)
12188 {
12189 check_no_duplicate_clause (list, code, omp_clause_code_name[code]);
12190 tree c = build_omp_clause (c_parser_peek_token (parser)->location, code);
12191 OMP_CLAUSE_CHAIN (c) = list;
12192 return c;
12193 }
12194
12195 /* OpenMP 4.0:
12196 num_teams ( expression ) */
12197
12198 static tree
12199 c_parser_omp_clause_num_teams (c_parser *parser, tree list)
12200 {
12201 location_t num_teams_loc = c_parser_peek_token (parser)->location;
12202 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
12203 {
12204 location_t expr_loc = c_parser_peek_token (parser)->location;
12205 tree c, t = c_parser_expression (parser).value;
12206 mark_exp_read (t);
12207 t = c_fully_fold (t, false, NULL);
12208
12209 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12210
12211 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
12212 {
12213 c_parser_error (parser, "expected integer expression");
12214 return list;
12215 }
12216
12217 /* Attempt to statically determine when the number isn't positive. */
12218 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
12219 build_int_cst (TREE_TYPE (t), 0));
12220 protected_set_expr_location (c, expr_loc);
12221 if (c == boolean_true_node)
12222 {
12223 warning_at (expr_loc, 0, "%<num_teams%> value must be positive");
12224 t = integer_one_node;
12225 }
12226
12227 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS, "num_teams");
12228
12229 c = build_omp_clause (num_teams_loc, OMP_CLAUSE_NUM_TEAMS);
12230 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
12231 OMP_CLAUSE_CHAIN (c) = list;
12232 list = c;
12233 }
12234
12235 return list;
12236 }
12237
12238 /* OpenMP 4.0:
12239 thread_limit ( expression ) */
12240
12241 static tree
12242 c_parser_omp_clause_thread_limit (c_parser *parser, tree list)
12243 {
12244 location_t num_thread_limit_loc = c_parser_peek_token (parser)->location;
12245 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
12246 {
12247 location_t expr_loc = c_parser_peek_token (parser)->location;
12248 tree c, t = c_parser_expression (parser).value;
12249 mark_exp_read (t);
12250 t = c_fully_fold (t, false, NULL);
12251
12252 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12253
12254 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
12255 {
12256 c_parser_error (parser, "expected integer expression");
12257 return list;
12258 }
12259
12260 /* Attempt to statically determine when the number isn't positive. */
12261 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
12262 build_int_cst (TREE_TYPE (t), 0));
12263 protected_set_expr_location (c, expr_loc);
12264 if (c == boolean_true_node)
12265 {
12266 warning_at (expr_loc, 0, "%<thread_limit%> value must be positive");
12267 t = integer_one_node;
12268 }
12269
12270 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
12271 "thread_limit");
12272
12273 c = build_omp_clause (num_thread_limit_loc, OMP_CLAUSE_THREAD_LIMIT);
12274 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
12275 OMP_CLAUSE_CHAIN (c) = list;
12276 list = c;
12277 }
12278
12279 return list;
12280 }
12281
12282 /* OpenMP 4.0:
12283 aligned ( variable-list )
12284 aligned ( variable-list : constant-expression ) */
12285
12286 static tree
12287 c_parser_omp_clause_aligned (c_parser *parser, tree list)
12288 {
12289 location_t clause_loc = c_parser_peek_token (parser)->location;
12290 tree nl, c;
12291
12292 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
12293 return list;
12294
12295 nl = c_parser_omp_variable_list (parser, clause_loc,
12296 OMP_CLAUSE_ALIGNED, list);
12297
12298 if (c_parser_next_token_is (parser, CPP_COLON))
12299 {
12300 c_parser_consume_token (parser);
12301 tree alignment = c_parser_expr_no_commas (parser, NULL).value;
12302 mark_exp_read (alignment);
12303 alignment = c_fully_fold (alignment, false, NULL);
12304 if (TREE_CODE (alignment) != INTEGER_CST
12305 || !INTEGRAL_TYPE_P (TREE_TYPE (alignment))
12306 || tree_int_cst_sgn (alignment) != 1)
12307 {
12308 error_at (clause_loc, "%<aligned%> clause alignment expression must "
12309 "be positive constant integer expression");
12310 alignment = NULL_TREE;
12311 }
12312
12313 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
12314 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
12315 }
12316
12317 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12318 return nl;
12319 }
12320
12321 /* OpenMP 4.0:
12322 linear ( variable-list )
12323 linear ( variable-list : expression )
12324
12325 OpenMP 4.5:
12326 linear ( modifier ( variable-list ) )
12327 linear ( modifier ( variable-list ) : expression ) */
12328
12329 static tree
12330 c_parser_omp_clause_linear (c_parser *parser, tree list, bool is_cilk_simd_fn)
12331 {
12332 location_t clause_loc = c_parser_peek_token (parser)->location;
12333 tree nl, c, step;
12334 enum omp_clause_linear_kind kind = OMP_CLAUSE_LINEAR_DEFAULT;
12335
12336 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
12337 return list;
12338
12339 if (!is_cilk_simd_fn
12340 && c_parser_next_token_is (parser, CPP_NAME))
12341 {
12342 c_token *tok = c_parser_peek_token (parser);
12343 const char *p = IDENTIFIER_POINTER (tok->value);
12344 if (strcmp ("val", p) == 0)
12345 kind = OMP_CLAUSE_LINEAR_VAL;
12346 if (c_parser_peek_2nd_token (parser)->type != CPP_OPEN_PAREN)
12347 kind = OMP_CLAUSE_LINEAR_DEFAULT;
12348 if (kind != OMP_CLAUSE_LINEAR_DEFAULT)
12349 {
12350 c_parser_consume_token (parser);
12351 c_parser_consume_token (parser);
12352 }
12353 }
12354
12355 nl = c_parser_omp_variable_list (parser, clause_loc,
12356 OMP_CLAUSE_LINEAR, list);
12357
12358 if (kind != OMP_CLAUSE_LINEAR_DEFAULT)
12359 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12360
12361 if (c_parser_next_token_is (parser, CPP_COLON))
12362 {
12363 c_parser_consume_token (parser);
12364 step = c_parser_expression (parser).value;
12365 mark_exp_read (step);
12366 step = c_fully_fold (step, false, NULL);
12367 if (is_cilk_simd_fn && TREE_CODE (step) == PARM_DECL)
12368 {
12369 sorry ("using parameters for %<linear%> step is not supported yet");
12370 step = integer_one_node;
12371 }
12372 if (!INTEGRAL_TYPE_P (TREE_TYPE (step)))
12373 {
12374 error_at (clause_loc, "%<linear%> clause step expression must "
12375 "be integral");
12376 step = integer_one_node;
12377 }
12378
12379 }
12380 else
12381 step = integer_one_node;
12382
12383 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
12384 {
12385 OMP_CLAUSE_LINEAR_STEP (c) = step;
12386 OMP_CLAUSE_LINEAR_KIND (c) = kind;
12387 }
12388
12389 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12390 return nl;
12391 }
12392
12393 /* OpenMP 4.0:
12394 safelen ( constant-expression ) */
12395
12396 static tree
12397 c_parser_omp_clause_safelen (c_parser *parser, tree list)
12398 {
12399 location_t clause_loc = c_parser_peek_token (parser)->location;
12400 tree c, t;
12401
12402 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
12403 return list;
12404
12405 t = c_parser_expr_no_commas (parser, NULL).value;
12406 mark_exp_read (t);
12407 t = c_fully_fold (t, false, NULL);
12408 if (TREE_CODE (t) != INTEGER_CST
12409 || !INTEGRAL_TYPE_P (TREE_TYPE (t))
12410 || tree_int_cst_sgn (t) != 1)
12411 {
12412 error_at (clause_loc, "%<safelen%> clause expression must "
12413 "be positive constant integer expression");
12414 t = NULL_TREE;
12415 }
12416
12417 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12418 if (t == NULL_TREE || t == error_mark_node)
12419 return list;
12420
12421 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen");
12422
12423 c = build_omp_clause (clause_loc, OMP_CLAUSE_SAFELEN);
12424 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
12425 OMP_CLAUSE_CHAIN (c) = list;
12426 return c;
12427 }
12428
12429 /* OpenMP 4.0:
12430 simdlen ( constant-expression ) */
12431
12432 static tree
12433 c_parser_omp_clause_simdlen (c_parser *parser, tree list)
12434 {
12435 location_t clause_loc = c_parser_peek_token (parser)->location;
12436 tree c, t;
12437
12438 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
12439 return list;
12440
12441 t = c_parser_expr_no_commas (parser, NULL).value;
12442 mark_exp_read (t);
12443 t = c_fully_fold (t, false, NULL);
12444 if (TREE_CODE (t) != INTEGER_CST
12445 || !INTEGRAL_TYPE_P (TREE_TYPE (t))
12446 || tree_int_cst_sgn (t) != 1)
12447 {
12448 error_at (clause_loc, "%<simdlen%> clause expression must "
12449 "be positive constant integer expression");
12450 t = NULL_TREE;
12451 }
12452
12453 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12454 if (t == NULL_TREE || t == error_mark_node)
12455 return list;
12456
12457 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen");
12458
12459 c = build_omp_clause (clause_loc, OMP_CLAUSE_SIMDLEN);
12460 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
12461 OMP_CLAUSE_CHAIN (c) = list;
12462 return c;
12463 }
12464
12465 /* OpenMP 4.5:
12466 vec:
12467 identifier [+/- integer]
12468 vec , identifier [+/- integer]
12469 */
12470
12471 static tree
12472 c_parser_omp_clause_depend_sink (c_parser *parser, location_t clause_loc,
12473 tree list)
12474 {
12475 tree vec = NULL;
12476 if (c_parser_next_token_is_not (parser, CPP_NAME)
12477 || c_parser_peek_token (parser)->id_kind != C_ID_ID)
12478 {
12479 c_parser_error (parser, "expected identifier");
12480 return list;
12481 }
12482
12483 while (c_parser_next_token_is (parser, CPP_NAME)
12484 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
12485 {
12486 tree t = lookup_name (c_parser_peek_token (parser)->value);
12487 tree addend = NULL;
12488
12489 if (t == NULL_TREE)
12490 {
12491 undeclared_variable (c_parser_peek_token (parser)->location,
12492 c_parser_peek_token (parser)->value);
12493 t = error_mark_node;
12494 }
12495
12496 c_parser_consume_token (parser);
12497
12498 bool neg = false;
12499 if (c_parser_next_token_is (parser, CPP_MINUS))
12500 neg = true;
12501 else if (!c_parser_next_token_is (parser, CPP_PLUS))
12502 {
12503 addend = integer_zero_node;
12504 neg = false;
12505 goto add_to_vector;
12506 }
12507 c_parser_consume_token (parser);
12508
12509 if (c_parser_next_token_is_not (parser, CPP_NUMBER))
12510 {
12511 c_parser_error (parser, "expected integer");
12512 return list;
12513 }
12514
12515 addend = c_parser_peek_token (parser)->value;
12516 if (TREE_CODE (addend) != INTEGER_CST)
12517 {
12518 c_parser_error (parser, "expected integer");
12519 return list;
12520 }
12521 c_parser_consume_token (parser);
12522
12523 add_to_vector:
12524 if (t != error_mark_node)
12525 {
12526 vec = tree_cons (addend, t, vec);
12527 if (neg)
12528 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (vec) = 1;
12529 }
12530
12531 if (c_parser_next_token_is_not (parser, CPP_COMMA))
12532 break;
12533
12534 c_parser_consume_token (parser);
12535 }
12536
12537 if (vec == NULL_TREE)
12538 return list;
12539
12540 tree u = build_omp_clause (clause_loc, OMP_CLAUSE_DEPEND);
12541 OMP_CLAUSE_DEPEND_KIND (u) = OMP_CLAUSE_DEPEND_SINK;
12542 OMP_CLAUSE_DECL (u) = nreverse (vec);
12543 OMP_CLAUSE_CHAIN (u) = list;
12544 return u;
12545 }
12546
12547 /* OpenMP 4.0:
12548 depend ( depend-kind: variable-list )
12549
12550 depend-kind:
12551 in | out | inout
12552
12553 OpenMP 4.5:
12554 depend ( source )
12555
12556 depend ( sink : vec ) */
12557
12558 static tree
12559 c_parser_omp_clause_depend (c_parser *parser, tree list)
12560 {
12561 location_t clause_loc = c_parser_peek_token (parser)->location;
12562 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_INOUT;
12563 tree nl, c;
12564
12565 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
12566 return list;
12567
12568 if (c_parser_next_token_is (parser, CPP_NAME))
12569 {
12570 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
12571 if (strcmp ("in", p) == 0)
12572 kind = OMP_CLAUSE_DEPEND_IN;
12573 else if (strcmp ("inout", p) == 0)
12574 kind = OMP_CLAUSE_DEPEND_INOUT;
12575 else if (strcmp ("out", p) == 0)
12576 kind = OMP_CLAUSE_DEPEND_OUT;
12577 else if (strcmp ("source", p) == 0)
12578 kind = OMP_CLAUSE_DEPEND_SOURCE;
12579 else if (strcmp ("sink", p) == 0)
12580 kind = OMP_CLAUSE_DEPEND_SINK;
12581 else
12582 goto invalid_kind;
12583 }
12584 else
12585 goto invalid_kind;
12586
12587 c_parser_consume_token (parser);
12588
12589 if (kind == OMP_CLAUSE_DEPEND_SOURCE)
12590 {
12591 c = build_omp_clause (clause_loc, OMP_CLAUSE_DEPEND);
12592 OMP_CLAUSE_DEPEND_KIND (c) = kind;
12593 OMP_CLAUSE_DECL (c) = NULL_TREE;
12594 OMP_CLAUSE_CHAIN (c) = list;
12595 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12596 return c;
12597 }
12598
12599 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
12600 goto resync_fail;
12601
12602 if (kind == OMP_CLAUSE_DEPEND_SINK)
12603 nl = c_parser_omp_clause_depend_sink (parser, clause_loc, list);
12604 else
12605 {
12606 nl = c_parser_omp_variable_list (parser, clause_loc,
12607 OMP_CLAUSE_DEPEND, list);
12608
12609 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
12610 OMP_CLAUSE_DEPEND_KIND (c) = kind;
12611 }
12612
12613 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12614 return nl;
12615
12616 invalid_kind:
12617 c_parser_error (parser, "invalid depend kind");
12618 resync_fail:
12619 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12620 return list;
12621 }
12622
12623 /* OpenMP 4.0:
12624 map ( map-kind: variable-list )
12625 map ( variable-list )
12626
12627 map-kind:
12628 alloc | to | from | tofrom
12629
12630 OpenMP 4.5:
12631 map-kind:
12632 alloc | to | from | tofrom | release | delete
12633
12634 map ( always [,] map-kind: variable-list ) */
12635
12636 static tree
12637 c_parser_omp_clause_map (c_parser *parser, tree list)
12638 {
12639 location_t clause_loc = c_parser_peek_token (parser)->location;
12640 enum gomp_map_kind kind = GOMP_MAP_TOFROM;
12641 int always = 0;
12642 enum c_id_kind always_id_kind = C_ID_NONE;
12643 location_t always_loc = UNKNOWN_LOCATION;
12644 tree always_id = NULL_TREE;
12645 tree nl, c;
12646
12647 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
12648 return list;
12649
12650 if (c_parser_next_token_is (parser, CPP_NAME))
12651 {
12652 c_token *tok = c_parser_peek_token (parser);
12653 const char *p = IDENTIFIER_POINTER (tok->value);
12654 always_id_kind = tok->id_kind;
12655 always_loc = tok->location;
12656 always_id = tok->value;
12657 if (strcmp ("always", p) == 0)
12658 {
12659 c_token *sectok = c_parser_peek_2nd_token (parser);
12660 if (sectok->type == CPP_COMMA)
12661 {
12662 c_parser_consume_token (parser);
12663 c_parser_consume_token (parser);
12664 always = 2;
12665 }
12666 else if (sectok->type == CPP_NAME)
12667 {
12668 p = IDENTIFIER_POINTER (sectok->value);
12669 if (strcmp ("alloc", p) == 0
12670 || strcmp ("to", p) == 0
12671 || strcmp ("from", p) == 0
12672 || strcmp ("tofrom", p) == 0
12673 || strcmp ("release", p) == 0
12674 || strcmp ("delete", p) == 0)
12675 {
12676 c_parser_consume_token (parser);
12677 always = 1;
12678 }
12679 }
12680 }
12681 }
12682
12683 if (c_parser_next_token_is (parser, CPP_NAME)
12684 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
12685 {
12686 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
12687 if (strcmp ("alloc", p) == 0)
12688 kind = GOMP_MAP_ALLOC;
12689 else if (strcmp ("to", p) == 0)
12690 kind = always ? GOMP_MAP_ALWAYS_TO : GOMP_MAP_TO;
12691 else if (strcmp ("from", p) == 0)
12692 kind = always ? GOMP_MAP_ALWAYS_FROM : GOMP_MAP_FROM;
12693 else if (strcmp ("tofrom", p) == 0)
12694 kind = always ? GOMP_MAP_ALWAYS_TOFROM : GOMP_MAP_TOFROM;
12695 else if (strcmp ("release", p) == 0)
12696 kind = GOMP_MAP_RELEASE;
12697 else if (strcmp ("delete", p) == 0)
12698 kind = GOMP_MAP_DELETE;
12699 else
12700 {
12701 c_parser_error (parser, "invalid map kind");
12702 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
12703 "expected %<)%>");
12704 return list;
12705 }
12706 c_parser_consume_token (parser);
12707 c_parser_consume_token (parser);
12708 }
12709 else if (always)
12710 {
12711 if (always_id_kind != C_ID_ID)
12712 {
12713 c_parser_error (parser, "expected identifier");
12714 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12715 return list;
12716 }
12717
12718 tree t = lookup_name (always_id);
12719 if (t == NULL_TREE)
12720 {
12721 undeclared_variable (always_loc, always_id);
12722 t = error_mark_node;
12723 }
12724 if (t != error_mark_node)
12725 {
12726 tree u = build_omp_clause (clause_loc, OMP_CLAUSE_MAP);
12727 OMP_CLAUSE_DECL (u) = t;
12728 OMP_CLAUSE_CHAIN (u) = list;
12729 OMP_CLAUSE_SET_MAP_KIND (u, kind);
12730 list = u;
12731 }
12732 if (always == 1)
12733 {
12734 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12735 return list;
12736 }
12737 }
12738
12739 nl = c_parser_omp_variable_list (parser, clause_loc, OMP_CLAUSE_MAP, list);
12740
12741 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
12742 OMP_CLAUSE_SET_MAP_KIND (c, kind);
12743
12744 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12745 return nl;
12746 }
12747
12748 /* OpenMP 4.0:
12749 device ( expression ) */
12750
12751 static tree
12752 c_parser_omp_clause_device (c_parser *parser, tree list)
12753 {
12754 location_t clause_loc = c_parser_peek_token (parser)->location;
12755 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
12756 {
12757 tree c, t = c_parser_expr_no_commas (parser, NULL).value;
12758 mark_exp_read (t);
12759 t = c_fully_fold (t, false, NULL);
12760
12761 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12762
12763 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
12764 {
12765 c_parser_error (parser, "expected integer expression");
12766 return list;
12767 }
12768
12769 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE, "device");
12770
12771 c = build_omp_clause (clause_loc, OMP_CLAUSE_DEVICE);
12772 OMP_CLAUSE_DEVICE_ID (c) = t;
12773 OMP_CLAUSE_CHAIN (c) = list;
12774 list = c;
12775 }
12776
12777 return list;
12778 }
12779
12780 /* OpenMP 4.0:
12781 dist_schedule ( static )
12782 dist_schedule ( static , expression ) */
12783
12784 static tree
12785 c_parser_omp_clause_dist_schedule (c_parser *parser, tree list)
12786 {
12787 tree c, t = NULL_TREE;
12788 location_t loc = c_parser_peek_token (parser)->location;
12789
12790 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
12791 return list;
12792
12793 if (!c_parser_next_token_is_keyword (parser, RID_STATIC))
12794 {
12795 c_parser_error (parser, "invalid dist_schedule kind");
12796 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
12797 "expected %<)%>");
12798 return list;
12799 }
12800
12801 c_parser_consume_token (parser);
12802 if (c_parser_next_token_is (parser, CPP_COMMA))
12803 {
12804 c_parser_consume_token (parser);
12805
12806 t = c_parser_expr_no_commas (parser, NULL).value;
12807 mark_exp_read (t);
12808 t = c_fully_fold (t, false, NULL);
12809 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12810 }
12811 else
12812 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
12813 "expected %<,%> or %<)%>");
12814
12815 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
12816 if (t == error_mark_node)
12817 return list;
12818
12819 c = build_omp_clause (loc, OMP_CLAUSE_DIST_SCHEDULE);
12820 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
12821 OMP_CLAUSE_CHAIN (c) = list;
12822 return c;
12823 }
12824
12825 /* OpenMP 4.0:
12826 proc_bind ( proc-bind-kind )
12827
12828 proc-bind-kind:
12829 master | close | spread */
12830
12831 static tree
12832 c_parser_omp_clause_proc_bind (c_parser *parser, tree list)
12833 {
12834 location_t clause_loc = c_parser_peek_token (parser)->location;
12835 enum omp_clause_proc_bind_kind kind;
12836 tree c;
12837
12838 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
12839 return list;
12840
12841 if (c_parser_next_token_is (parser, CPP_NAME))
12842 {
12843 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
12844 if (strcmp ("master", p) == 0)
12845 kind = OMP_CLAUSE_PROC_BIND_MASTER;
12846 else if (strcmp ("close", p) == 0)
12847 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
12848 else if (strcmp ("spread", p) == 0)
12849 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
12850 else
12851 goto invalid_kind;
12852 }
12853 else
12854 goto invalid_kind;
12855
12856 c_parser_consume_token (parser);
12857 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12858 c = build_omp_clause (clause_loc, OMP_CLAUSE_PROC_BIND);
12859 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
12860 OMP_CLAUSE_CHAIN (c) = list;
12861 return c;
12862
12863 invalid_kind:
12864 c_parser_error (parser, "invalid proc_bind kind");
12865 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12866 return list;
12867 }
12868
12869 /* OpenMP 4.0:
12870 to ( variable-list ) */
12871
12872 static tree
12873 c_parser_omp_clause_to (c_parser *parser, tree list)
12874 {
12875 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_TO, list);
12876 }
12877
12878 /* OpenMP 4.0:
12879 from ( variable-list ) */
12880
12881 static tree
12882 c_parser_omp_clause_from (c_parser *parser, tree list)
12883 {
12884 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_FROM, list);
12885 }
12886
12887 /* OpenMP 4.0:
12888 uniform ( variable-list ) */
12889
12890 static tree
12891 c_parser_omp_clause_uniform (c_parser *parser, tree list)
12892 {
12893 /* The clauses location. */
12894 location_t loc = c_parser_peek_token (parser)->location;
12895
12896 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
12897 {
12898 list = c_parser_omp_variable_list (parser, loc, OMP_CLAUSE_UNIFORM,
12899 list);
12900 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12901 }
12902 return list;
12903 }
12904
12905 /* Parse all OpenACC clauses. The set clauses allowed by the directive
12906 is a bitmask in MASK. Return the list of clauses found. */
12907
12908 static tree
12909 c_parser_oacc_all_clauses (c_parser *parser, omp_clause_mask mask,
12910 const char *where, bool finish_p = true)
12911 {
12912 tree clauses = NULL;
12913 bool first = true;
12914
12915 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
12916 {
12917 location_t here;
12918 pragma_omp_clause c_kind;
12919 const char *c_name;
12920 tree prev = clauses;
12921
12922 if (!first && c_parser_next_token_is (parser, CPP_COMMA))
12923 c_parser_consume_token (parser);
12924
12925 here = c_parser_peek_token (parser)->location;
12926 c_kind = c_parser_omp_clause_name (parser);
12927
12928 switch (c_kind)
12929 {
12930 case PRAGMA_OACC_CLAUSE_ASYNC:
12931 clauses = c_parser_oacc_clause_async (parser, clauses);
12932 c_name = "async";
12933 break;
12934 case PRAGMA_OACC_CLAUSE_AUTO:
12935 clauses = c_parser_oacc_simple_clause (parser, OMP_CLAUSE_AUTO,
12936 clauses);
12937 c_name = "auto";
12938 break;
12939 case PRAGMA_OACC_CLAUSE_COLLAPSE:
12940 clauses = c_parser_omp_clause_collapse (parser, clauses);
12941 c_name = "collapse";
12942 break;
12943 case PRAGMA_OACC_CLAUSE_COPY:
12944 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
12945 c_name = "copy";
12946 break;
12947 case PRAGMA_OACC_CLAUSE_COPYIN:
12948 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
12949 c_name = "copyin";
12950 break;
12951 case PRAGMA_OACC_CLAUSE_COPYOUT:
12952 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
12953 c_name = "copyout";
12954 break;
12955 case PRAGMA_OACC_CLAUSE_CREATE:
12956 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
12957 c_name = "create";
12958 break;
12959 case PRAGMA_OACC_CLAUSE_DELETE:
12960 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
12961 c_name = "delete";
12962 break;
12963 case PRAGMA_OMP_CLAUSE_DEFAULT:
12964 clauses = c_parser_omp_clause_default (parser, clauses, true);
12965 c_name = "default";
12966 break;
12967 case PRAGMA_OACC_CLAUSE_DEVICE:
12968 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
12969 c_name = "device";
12970 break;
12971 case PRAGMA_OACC_CLAUSE_DEVICEPTR:
12972 clauses = c_parser_oacc_data_clause_deviceptr (parser, clauses);
12973 c_name = "deviceptr";
12974 break;
12975 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
12976 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
12977 c_name = "device_resident";
12978 break;
12979 case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE:
12980 clauses = c_parser_omp_clause_firstprivate (parser, clauses);
12981 c_name = "firstprivate";
12982 break;
12983 case PRAGMA_OACC_CLAUSE_GANG:
12984 c_name = "gang";
12985 clauses = c_parser_oacc_shape_clause (parser, OMP_CLAUSE_GANG,
12986 c_name, clauses);
12987 break;
12988 case PRAGMA_OACC_CLAUSE_HOST:
12989 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
12990 c_name = "host";
12991 break;
12992 case PRAGMA_OACC_CLAUSE_IF:
12993 clauses = c_parser_omp_clause_if (parser, clauses, false);
12994 c_name = "if";
12995 break;
12996 case PRAGMA_OACC_CLAUSE_INDEPENDENT:
12997 clauses = c_parser_oacc_simple_clause (parser, OMP_CLAUSE_INDEPENDENT,
12998 clauses);
12999 c_name = "independent";
13000 break;
13001 case PRAGMA_OACC_CLAUSE_LINK:
13002 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13003 c_name = "link";
13004 break;
13005 case PRAGMA_OACC_CLAUSE_NUM_GANGS:
13006 clauses = c_parser_omp_clause_num_gangs (parser, clauses);
13007 c_name = "num_gangs";
13008 break;
13009 case PRAGMA_OACC_CLAUSE_NUM_WORKERS:
13010 clauses = c_parser_omp_clause_num_workers (parser, clauses);
13011 c_name = "num_workers";
13012 break;
13013 case PRAGMA_OACC_CLAUSE_PRESENT:
13014 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13015 c_name = "present";
13016 break;
13017 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
13018 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13019 c_name = "present_or_copy";
13020 break;
13021 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
13022 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13023 c_name = "present_or_copyin";
13024 break;
13025 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
13026 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13027 c_name = "present_or_copyout";
13028 break;
13029 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
13030 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13031 c_name = "present_or_create";
13032 break;
13033 case PRAGMA_OACC_CLAUSE_PRIVATE:
13034 clauses = c_parser_omp_clause_private (parser, clauses);
13035 c_name = "private";
13036 break;
13037 case PRAGMA_OACC_CLAUSE_REDUCTION:
13038 clauses = c_parser_omp_clause_reduction (parser, clauses);
13039 c_name = "reduction";
13040 break;
13041 case PRAGMA_OACC_CLAUSE_SELF:
13042 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13043 c_name = "self";
13044 break;
13045 case PRAGMA_OACC_CLAUSE_SEQ:
13046 clauses = c_parser_oacc_simple_clause (parser, OMP_CLAUSE_SEQ,
13047 clauses);
13048 c_name = "seq";
13049 break;
13050 case PRAGMA_OACC_CLAUSE_TILE:
13051 clauses = c_parser_oacc_clause_tile (parser, clauses);
13052 c_name = "tile";
13053 break;
13054 case PRAGMA_OACC_CLAUSE_USE_DEVICE:
13055 clauses = c_parser_omp_clause_use_device_ptr (parser, clauses);
13056 c_name = "use_device";
13057 break;
13058 case PRAGMA_OACC_CLAUSE_VECTOR:
13059 c_name = "vector";
13060 clauses = c_parser_oacc_shape_clause (parser, OMP_CLAUSE_VECTOR,
13061 c_name, clauses);
13062 break;
13063 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH:
13064 clauses = c_parser_omp_clause_vector_length (parser, clauses);
13065 c_name = "vector_length";
13066 break;
13067 case PRAGMA_OACC_CLAUSE_WAIT:
13068 clauses = c_parser_oacc_clause_wait (parser, clauses);
13069 c_name = "wait";
13070 break;
13071 case PRAGMA_OACC_CLAUSE_WORKER:
13072 c_name = "worker";
13073 clauses = c_parser_oacc_shape_clause (parser, OMP_CLAUSE_WORKER,
13074 c_name, clauses);
13075 break;
13076 default:
13077 c_parser_error (parser, "expected %<#pragma acc%> clause");
13078 goto saw_error;
13079 }
13080
13081 first = false;
13082
13083 if (((mask >> c_kind) & 1) == 0)
13084 {
13085 /* Remove the invalid clause(s) from the list to avoid
13086 confusing the rest of the compiler. */
13087 clauses = prev;
13088 error_at (here, "%qs is not valid for %qs", c_name, where);
13089 }
13090 }
13091
13092 saw_error:
13093 c_parser_skip_to_pragma_eol (parser);
13094
13095 if (finish_p)
13096 return c_finish_omp_clauses (clauses, false);
13097
13098 return clauses;
13099 }
13100
13101 /* Parse all OpenMP clauses. The set clauses allowed by the directive
13102 is a bitmask in MASK. Return the list of clauses found. */
13103
13104 static tree
13105 c_parser_omp_all_clauses (c_parser *parser, omp_clause_mask mask,
13106 const char *where, bool finish_p = true)
13107 {
13108 tree clauses = NULL;
13109 bool first = true, cilk_simd_fn = false;
13110
13111 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
13112 {
13113 location_t here;
13114 pragma_omp_clause c_kind;
13115 const char *c_name;
13116 tree prev = clauses;
13117
13118 if (!first && c_parser_next_token_is (parser, CPP_COMMA))
13119 c_parser_consume_token (parser);
13120
13121 here = c_parser_peek_token (parser)->location;
13122 c_kind = c_parser_omp_clause_name (parser);
13123
13124 switch (c_kind)
13125 {
13126 case PRAGMA_OMP_CLAUSE_COLLAPSE:
13127 clauses = c_parser_omp_clause_collapse (parser, clauses);
13128 c_name = "collapse";
13129 break;
13130 case PRAGMA_OMP_CLAUSE_COPYIN:
13131 clauses = c_parser_omp_clause_copyin (parser, clauses);
13132 c_name = "copyin";
13133 break;
13134 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
13135 clauses = c_parser_omp_clause_copyprivate (parser, clauses);
13136 c_name = "copyprivate";
13137 break;
13138 case PRAGMA_OMP_CLAUSE_DEFAULT:
13139 clauses = c_parser_omp_clause_default (parser, clauses, false);
13140 c_name = "default";
13141 break;
13142 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
13143 clauses = c_parser_omp_clause_firstprivate (parser, clauses);
13144 c_name = "firstprivate";
13145 break;
13146 case PRAGMA_OMP_CLAUSE_FINAL:
13147 clauses = c_parser_omp_clause_final (parser, clauses);
13148 c_name = "final";
13149 break;
13150 case PRAGMA_OMP_CLAUSE_GRAINSIZE:
13151 clauses = c_parser_omp_clause_grainsize (parser, clauses);
13152 c_name = "grainsize";
13153 break;
13154 case PRAGMA_OMP_CLAUSE_HINT:
13155 clauses = c_parser_omp_clause_hint (parser, clauses);
13156 c_name = "hint";
13157 break;
13158 case PRAGMA_OMP_CLAUSE_DEFAULTMAP:
13159 clauses = c_parser_omp_clause_defaultmap (parser, clauses);
13160 c_name = "defaultmap";
13161 break;
13162 case PRAGMA_OMP_CLAUSE_IF:
13163 clauses = c_parser_omp_clause_if (parser, clauses, true);
13164 c_name = "if";
13165 break;
13166 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
13167 clauses = c_parser_omp_clause_lastprivate (parser, clauses);
13168 c_name = "lastprivate";
13169 break;
13170 case PRAGMA_OMP_CLAUSE_MERGEABLE:
13171 clauses = c_parser_omp_clause_mergeable (parser, clauses);
13172 c_name = "mergeable";
13173 break;
13174 case PRAGMA_OMP_CLAUSE_NOWAIT:
13175 clauses = c_parser_omp_clause_nowait (parser, clauses);
13176 c_name = "nowait";
13177 break;
13178 case PRAGMA_OMP_CLAUSE_NUM_TASKS:
13179 clauses = c_parser_omp_clause_num_tasks (parser, clauses);
13180 c_name = "num_tasks";
13181 break;
13182 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
13183 clauses = c_parser_omp_clause_num_threads (parser, clauses);
13184 c_name = "num_threads";
13185 break;
13186 case PRAGMA_OMP_CLAUSE_ORDERED:
13187 clauses = c_parser_omp_clause_ordered (parser, clauses);
13188 c_name = "ordered";
13189 break;
13190 case PRAGMA_OMP_CLAUSE_PRIORITY:
13191 clauses = c_parser_omp_clause_priority (parser, clauses);
13192 c_name = "priority";
13193 break;
13194 case PRAGMA_OMP_CLAUSE_PRIVATE:
13195 clauses = c_parser_omp_clause_private (parser, clauses);
13196 c_name = "private";
13197 break;
13198 case PRAGMA_OMP_CLAUSE_REDUCTION:
13199 clauses = c_parser_omp_clause_reduction (parser, clauses);
13200 c_name = "reduction";
13201 break;
13202 case PRAGMA_OMP_CLAUSE_SCHEDULE:
13203 clauses = c_parser_omp_clause_schedule (parser, clauses);
13204 c_name = "schedule";
13205 break;
13206 case PRAGMA_OMP_CLAUSE_SHARED:
13207 clauses = c_parser_omp_clause_shared (parser, clauses);
13208 c_name = "shared";
13209 break;
13210 case PRAGMA_OMP_CLAUSE_UNTIED:
13211 clauses = c_parser_omp_clause_untied (parser, clauses);
13212 c_name = "untied";
13213 break;
13214 case PRAGMA_OMP_CLAUSE_INBRANCH:
13215 case PRAGMA_CILK_CLAUSE_MASK:
13216 clauses = c_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
13217 clauses);
13218 c_name = "inbranch";
13219 break;
13220 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
13221 case PRAGMA_CILK_CLAUSE_NOMASK:
13222 clauses = c_parser_omp_clause_branch (parser, OMP_CLAUSE_NOTINBRANCH,
13223 clauses);
13224 c_name = "notinbranch";
13225 break;
13226 case PRAGMA_OMP_CLAUSE_PARALLEL:
13227 clauses
13228 = c_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
13229 clauses);
13230 c_name = "parallel";
13231 if (!first)
13232 {
13233 clause_not_first:
13234 error_at (here, "%qs must be the first clause of %qs",
13235 c_name, where);
13236 clauses = prev;
13237 }
13238 break;
13239 case PRAGMA_OMP_CLAUSE_FOR:
13240 clauses
13241 = c_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
13242 clauses);
13243 c_name = "for";
13244 if (!first)
13245 goto clause_not_first;
13246 break;
13247 case PRAGMA_OMP_CLAUSE_SECTIONS:
13248 clauses
13249 = c_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
13250 clauses);
13251 c_name = "sections";
13252 if (!first)
13253 goto clause_not_first;
13254 break;
13255 case PRAGMA_OMP_CLAUSE_TASKGROUP:
13256 clauses
13257 = c_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
13258 clauses);
13259 c_name = "taskgroup";
13260 if (!first)
13261 goto clause_not_first;
13262 break;
13263 case PRAGMA_OMP_CLAUSE_LINK:
13264 clauses
13265 = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_LINK, clauses);
13266 c_name = "link";
13267 break;
13268 case PRAGMA_OMP_CLAUSE_TO:
13269 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK)) != 0)
13270 clauses
13271 = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_TO_DECLARE,
13272 clauses);
13273 else
13274 clauses = c_parser_omp_clause_to (parser, clauses);
13275 c_name = "to";
13276 break;
13277 case PRAGMA_OMP_CLAUSE_FROM:
13278 clauses = c_parser_omp_clause_from (parser, clauses);
13279 c_name = "from";
13280 break;
13281 case PRAGMA_OMP_CLAUSE_UNIFORM:
13282 clauses = c_parser_omp_clause_uniform (parser, clauses);
13283 c_name = "uniform";
13284 break;
13285 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
13286 clauses = c_parser_omp_clause_num_teams (parser, clauses);
13287 c_name = "num_teams";
13288 break;
13289 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
13290 clauses = c_parser_omp_clause_thread_limit (parser, clauses);
13291 c_name = "thread_limit";
13292 break;
13293 case PRAGMA_OMP_CLAUSE_ALIGNED:
13294 clauses = c_parser_omp_clause_aligned (parser, clauses);
13295 c_name = "aligned";
13296 break;
13297 case PRAGMA_OMP_CLAUSE_LINEAR:
13298 if (((mask >> PRAGMA_CILK_CLAUSE_VECTORLENGTH) & 1) != 0)
13299 cilk_simd_fn = true;
13300 clauses = c_parser_omp_clause_linear (parser, clauses, cilk_simd_fn);
13301 c_name = "linear";
13302 break;
13303 case PRAGMA_OMP_CLAUSE_DEPEND:
13304 clauses = c_parser_omp_clause_depend (parser, clauses);
13305 c_name = "depend";
13306 break;
13307 case PRAGMA_OMP_CLAUSE_MAP:
13308 clauses = c_parser_omp_clause_map (parser, clauses);
13309 c_name = "map";
13310 break;
13311 case PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR:
13312 clauses = c_parser_omp_clause_use_device_ptr (parser, clauses);
13313 c_name = "use_device_ptr";
13314 break;
13315 case PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR:
13316 clauses = c_parser_omp_clause_is_device_ptr (parser, clauses);
13317 c_name = "is_device_ptr";
13318 break;
13319 case PRAGMA_OMP_CLAUSE_DEVICE:
13320 clauses = c_parser_omp_clause_device (parser, clauses);
13321 c_name = "device";
13322 break;
13323 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
13324 clauses = c_parser_omp_clause_dist_schedule (parser, clauses);
13325 c_name = "dist_schedule";
13326 break;
13327 case PRAGMA_OMP_CLAUSE_PROC_BIND:
13328 clauses = c_parser_omp_clause_proc_bind (parser, clauses);
13329 c_name = "proc_bind";
13330 break;
13331 case PRAGMA_OMP_CLAUSE_SAFELEN:
13332 clauses = c_parser_omp_clause_safelen (parser, clauses);
13333 c_name = "safelen";
13334 break;
13335 case PRAGMA_CILK_CLAUSE_VECTORLENGTH:
13336 clauses = c_parser_cilk_clause_vectorlength (parser, clauses, true);
13337 c_name = "simdlen";
13338 break;
13339 case PRAGMA_OMP_CLAUSE_SIMDLEN:
13340 clauses = c_parser_omp_clause_simdlen (parser, clauses);
13341 c_name = "simdlen";
13342 break;
13343 case PRAGMA_OMP_CLAUSE_NOGROUP:
13344 clauses = c_parser_omp_clause_nogroup (parser, clauses);
13345 c_name = "nogroup";
13346 break;
13347 case PRAGMA_OMP_CLAUSE_THREADS:
13348 clauses
13349 = c_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_THREADS,
13350 clauses);
13351 c_name = "threads";
13352 break;
13353 case PRAGMA_OMP_CLAUSE_SIMD:
13354 clauses
13355 = c_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_SIMD,
13356 clauses);
13357 c_name = "simd";
13358 break;
13359 default:
13360 c_parser_error (parser, "expected %<#pragma omp%> clause");
13361 goto saw_error;
13362 }
13363
13364 first = false;
13365
13366 if (((mask >> c_kind) & 1) == 0)
13367 {
13368 /* Remove the invalid clause(s) from the list to avoid
13369 confusing the rest of the compiler. */
13370 clauses = prev;
13371 error_at (here, "%qs is not valid for %qs", c_name, where);
13372 }
13373 }
13374
13375 saw_error:
13376 c_parser_skip_to_pragma_eol (parser);
13377
13378 if (finish_p)
13379 {
13380 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM)) != 0)
13381 return c_finish_omp_clauses (clauses, true, true);
13382 return c_finish_omp_clauses (clauses, true);
13383 }
13384
13385 return clauses;
13386 }
13387
13388 /* OpenACC 2.0, OpenMP 2.5:
13389 structured-block:
13390 statement
13391
13392 In practice, we're also interested in adding the statement to an
13393 outer node. So it is convenient if we work around the fact that
13394 c_parser_statement calls add_stmt. */
13395
13396 static tree
13397 c_parser_omp_structured_block (c_parser *parser)
13398 {
13399 tree stmt = push_stmt_list ();
13400 c_parser_statement (parser);
13401 return pop_stmt_list (stmt);
13402 }
13403
13404 /* OpenACC 2.0:
13405 # pragma acc cache (variable-list) new-line
13406
13407 LOC is the location of the #pragma token.
13408 */
13409
13410 static tree
13411 c_parser_oacc_cache (location_t loc, c_parser *parser)
13412 {
13413 tree stmt, clauses;
13414
13415 clauses = c_parser_omp_var_list_parens (parser, OMP_CLAUSE__CACHE_, NULL);
13416 clauses = c_finish_omp_clauses (clauses, false);
13417
13418 c_parser_skip_to_pragma_eol (parser);
13419
13420 stmt = make_node (OACC_CACHE);
13421 TREE_TYPE (stmt) = void_type_node;
13422 OACC_CACHE_CLAUSES (stmt) = clauses;
13423 SET_EXPR_LOCATION (stmt, loc);
13424 add_stmt (stmt);
13425
13426 return stmt;
13427 }
13428
13429 /* OpenACC 2.0:
13430 # pragma acc data oacc-data-clause[optseq] new-line
13431 structured-block
13432
13433 LOC is the location of the #pragma token.
13434 */
13435
13436 #define OACC_DATA_CLAUSE_MASK \
13437 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
13438 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
13439 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
13440 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
13441 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
13442 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
13443 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
13444 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
13445 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
13446 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
13447 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) )
13448
13449 static tree
13450 c_parser_oacc_data (location_t loc, c_parser *parser)
13451 {
13452 tree stmt, clauses, block;
13453
13454 clauses = c_parser_oacc_all_clauses (parser, OACC_DATA_CLAUSE_MASK,
13455 "#pragma acc data");
13456
13457 block = c_begin_omp_parallel ();
13458 add_stmt (c_parser_omp_structured_block (parser));
13459
13460 stmt = c_finish_oacc_data (loc, clauses, block);
13461
13462 return stmt;
13463 }
13464
13465 /* OpenACC 2.0:
13466 # pragma acc declare oacc-data-clause[optseq] new-line
13467 */
13468
13469 #define OACC_DECLARE_CLAUSE_MASK \
13470 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
13471 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
13472 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
13473 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
13474 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
13475 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT) \
13476 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_LINK) \
13477 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
13478 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
13479 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
13480 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
13481 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) )
13482
13483 static void
13484 c_parser_oacc_declare (c_parser *parser)
13485 {
13486 location_t pragma_loc = c_parser_peek_token (parser)->location;
13487 tree clauses, stmt, t, decl;
13488
13489 bool error = false;
13490
13491 c_parser_consume_pragma (parser);
13492
13493 clauses = c_parser_oacc_all_clauses (parser, OACC_DECLARE_CLAUSE_MASK,
13494 "#pragma acc declare");
13495 if (!clauses)
13496 {
13497 error_at (pragma_loc,
13498 "no valid clauses specified in %<#pragma acc declare%>");
13499 return;
13500 }
13501
13502 for (t = clauses; t; t = OMP_CLAUSE_CHAIN (t))
13503 {
13504 location_t loc = OMP_CLAUSE_LOCATION (t);
13505 decl = OMP_CLAUSE_DECL (t);
13506 if (!DECL_P (decl))
13507 {
13508 error_at (loc, "array section in %<#pragma acc declare%>");
13509 error = true;
13510 continue;
13511 }
13512
13513 switch (OMP_CLAUSE_MAP_KIND (t))
13514 {
13515 case GOMP_MAP_FORCE_ALLOC:
13516 case GOMP_MAP_FORCE_TO:
13517 case GOMP_MAP_FORCE_DEVICEPTR:
13518 case GOMP_MAP_DEVICE_RESIDENT:
13519 break;
13520
13521 case GOMP_MAP_POINTER:
13522 /* Generated by c_finish_omp_clauses from array sections;
13523 avoid spurious diagnostics. */
13524 break;
13525
13526 case GOMP_MAP_LINK:
13527 if (!global_bindings_p ()
13528 && (TREE_STATIC (decl)
13529 || !DECL_EXTERNAL (decl)))
13530 {
13531 error_at (loc,
13532 "%qD must be a global variable in"
13533 "%<#pragma acc declare link%>",
13534 decl);
13535 error = true;
13536 continue;
13537 }
13538 break;
13539
13540 default:
13541 if (global_bindings_p ())
13542 {
13543 error_at (loc, "invalid OpenACC clause at file scope");
13544 error = true;
13545 continue;
13546 }
13547 if (DECL_EXTERNAL (decl))
13548 {
13549 error_at (loc,
13550 "invalid use of %<extern%> variable %qD "
13551 "in %<#pragma acc declare%>", decl);
13552 error = true;
13553 continue;
13554 }
13555 else if (TREE_PUBLIC (decl))
13556 {
13557 error_at (loc,
13558 "invalid use of %<global%> variable %qD "
13559 "in %<#pragma acc declare%>", decl);
13560 error = true;
13561 continue;
13562 }
13563 break;
13564 }
13565
13566 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl))
13567 || lookup_attribute ("omp declare target link",
13568 DECL_ATTRIBUTES (decl)))
13569 {
13570 error_at (loc, "variable %qD used more than once with "
13571 "%<#pragma acc declare%>", decl);
13572 error = true;
13573 continue;
13574 }
13575
13576 if (!error)
13577 {
13578 tree id;
13579
13580 if (OMP_CLAUSE_MAP_KIND (t) == GOMP_MAP_LINK)
13581 id = get_identifier ("omp declare target link");
13582 else
13583 id = get_identifier ("omp declare target");
13584
13585 DECL_ATTRIBUTES (decl)
13586 = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (decl));
13587
13588 if (global_bindings_p ())
13589 {
13590 symtab_node *node = symtab_node::get (decl);
13591 if (node != NULL)
13592 {
13593 node->offloadable = 1;
13594 if (ENABLE_OFFLOADING)
13595 {
13596 g->have_offload = true;
13597 if (is_a <varpool_node *> (node))
13598 {
13599 vec_safe_push (offload_vars, decl);
13600 node->force_output = 1;
13601 }
13602 }
13603 }
13604 }
13605 }
13606 }
13607
13608 if (error || global_bindings_p ())
13609 return;
13610
13611 stmt = make_node (OACC_DECLARE);
13612 TREE_TYPE (stmt) = void_type_node;
13613 OACC_DECLARE_CLAUSES (stmt) = clauses;
13614 SET_EXPR_LOCATION (stmt, pragma_loc);
13615
13616 add_stmt (stmt);
13617
13618 return;
13619 }
13620
13621 /* OpenACC 2.0:
13622 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
13623
13624 or
13625
13626 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
13627
13628
13629 LOC is the location of the #pragma token.
13630 */
13631
13632 #define OACC_ENTER_DATA_CLAUSE_MASK \
13633 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
13634 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
13635 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
13636 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
13637 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
13638 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
13639 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
13640
13641 #define OACC_EXIT_DATA_CLAUSE_MASK \
13642 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
13643 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
13644 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
13645 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
13646 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
13647
13648 static void
13649 c_parser_oacc_enter_exit_data (c_parser *parser, bool enter)
13650 {
13651 location_t loc = c_parser_peek_token (parser)->location;
13652 tree clauses, stmt;
13653
13654 c_parser_consume_pragma (parser);
13655
13656 if (!c_parser_next_token_is (parser, CPP_NAME))
13657 {
13658 c_parser_error (parser, enter
13659 ? "expected %<data%> in %<#pragma acc enter data%>"
13660 : "expected %<data%> in %<#pragma acc exit data%>");
13661 c_parser_skip_to_pragma_eol (parser);
13662 return;
13663 }
13664
13665 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
13666 if (strcmp (p, "data") != 0)
13667 {
13668 c_parser_error (parser, "invalid pragma");
13669 c_parser_skip_to_pragma_eol (parser);
13670 return;
13671 }
13672
13673 c_parser_consume_token (parser);
13674
13675 if (enter)
13676 clauses = c_parser_oacc_all_clauses (parser, OACC_ENTER_DATA_CLAUSE_MASK,
13677 "#pragma acc enter data");
13678 else
13679 clauses = c_parser_oacc_all_clauses (parser, OACC_EXIT_DATA_CLAUSE_MASK,
13680 "#pragma acc exit data");
13681
13682 if (find_omp_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
13683 {
13684 error_at (loc, enter
13685 ? "%<#pragma acc enter data%> has no data movement clause"
13686 : "%<#pragma acc exit data%> has no data movement clause");
13687 return;
13688 }
13689
13690 stmt = enter ? make_node (OACC_ENTER_DATA) : make_node (OACC_EXIT_DATA);
13691 TREE_TYPE (stmt) = void_type_node;
13692 OMP_STANDALONE_CLAUSES (stmt) = clauses;
13693 SET_EXPR_LOCATION (stmt, loc);
13694 add_stmt (stmt);
13695 }
13696
13697
13698 /* OpenACC 2.0:
13699 # pragma acc host_data oacc-data-clause[optseq] new-line
13700 structured-block
13701 */
13702
13703 #define OACC_HOST_DATA_CLAUSE_MASK \
13704 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_USE_DEVICE) )
13705
13706 static tree
13707 c_parser_oacc_host_data (location_t loc, c_parser *parser)
13708 {
13709 tree stmt, clauses, block;
13710
13711 clauses = c_parser_oacc_all_clauses (parser, OACC_HOST_DATA_CLAUSE_MASK,
13712 "#pragma acc host_data");
13713
13714 block = c_begin_omp_parallel ();
13715 add_stmt (c_parser_omp_structured_block (parser));
13716 stmt = c_finish_oacc_host_data (loc, clauses, block);
13717 return stmt;
13718 }
13719
13720
13721 /* OpenACC 2.0:
13722
13723 # pragma acc loop oacc-loop-clause[optseq] new-line
13724 structured-block
13725
13726 LOC is the location of the #pragma token.
13727 */
13728
13729 #define OACC_LOOP_CLAUSE_MASK \
13730 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
13731 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
13732 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
13733 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
13734 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
13735 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
13736 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_AUTO) \
13737 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_INDEPENDENT) \
13738 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
13739 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_TILE) )
13740 static tree
13741 c_parser_oacc_loop (location_t loc, c_parser *parser, char *p_name,
13742 omp_clause_mask mask, tree *cclauses)
13743 {
13744 strcat (p_name, " loop");
13745 mask |= OACC_LOOP_CLAUSE_MASK;
13746
13747 tree clauses = c_parser_oacc_all_clauses (parser, mask, p_name,
13748 cclauses == NULL);
13749 if (cclauses)
13750 {
13751 clauses = c_oacc_split_loop_clauses (clauses, cclauses);
13752 if (*cclauses)
13753 c_finish_omp_clauses (*cclauses, false);
13754 if (clauses)
13755 c_finish_omp_clauses (clauses, false);
13756 }
13757
13758 tree block = c_begin_compound_stmt (true);
13759 tree stmt = c_parser_omp_for_loop (loc, parser, OACC_LOOP, clauses, NULL);
13760 block = c_end_compound_stmt (loc, block, true);
13761 add_stmt (block);
13762
13763 return stmt;
13764 }
13765
13766 /* OpenACC 2.0:
13767 # pragma acc kernels oacc-kernels-clause[optseq] new-line
13768 structured-block
13769
13770 or
13771
13772 # pragma acc parallel oacc-parallel-clause[optseq] new-line
13773 structured-block
13774
13775 LOC is the location of the #pragma token.
13776 */
13777
13778 #define OACC_KERNELS_CLAUSE_MASK \
13779 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
13780 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
13781 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
13782 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
13783 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
13784 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
13785 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
13786 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
13787 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
13788 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
13789 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
13790 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
13791 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
13792 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
13793
13794 #define OACC_PARALLEL_CLAUSE_MASK \
13795 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
13796 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
13797 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
13798 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
13799 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
13800 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
13801 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
13802 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
13803 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
13804 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
13805 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
13806 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
13807 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
13808 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
13809 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
13810 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
13811 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
13812 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
13813 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
13814 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
13815
13816 static tree
13817 c_parser_oacc_kernels_parallel (location_t loc, c_parser *parser,
13818 enum pragma_kind p_kind, char *p_name)
13819 {
13820 omp_clause_mask mask;
13821 enum tree_code code;
13822 switch (p_kind)
13823 {
13824 case PRAGMA_OACC_KERNELS:
13825 strcat (p_name, " kernels");
13826 mask = OACC_KERNELS_CLAUSE_MASK;
13827 code = OACC_KERNELS;
13828 break;
13829 case PRAGMA_OACC_PARALLEL:
13830 strcat (p_name, " parallel");
13831 mask = OACC_PARALLEL_CLAUSE_MASK;
13832 code = OACC_PARALLEL;
13833 break;
13834 default:
13835 gcc_unreachable ();
13836 }
13837
13838 if (c_parser_next_token_is (parser, CPP_NAME))
13839 {
13840 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
13841 if (strcmp (p, "loop") == 0)
13842 {
13843 c_parser_consume_token (parser);
13844 mask |= OACC_LOOP_CLAUSE_MASK;
13845
13846 tree block = c_begin_omp_parallel ();
13847 tree clauses;
13848 c_parser_oacc_loop (loc, parser, p_name, mask, &clauses);
13849 return c_finish_omp_construct (loc, code, block, clauses);
13850 }
13851 }
13852
13853 tree clauses = c_parser_oacc_all_clauses (parser, mask, p_name);
13854
13855 tree block = c_begin_omp_parallel ();
13856 add_stmt (c_parser_omp_structured_block (parser));
13857
13858 return c_finish_omp_construct (loc, code, block, clauses);
13859 }
13860
13861 /* OpenACC 2.0:
13862 # pragma acc routine oacc-routine-clause[optseq] new-line
13863 function-definition
13864
13865 # pragma acc routine ( name ) oacc-routine-clause[optseq] new-line
13866 */
13867
13868 #define OACC_ROUTINE_CLAUSE_MASK \
13869 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
13870 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
13871 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
13872 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) )
13873
13874 /* Parse an OpenACC routine directive. For named directives, we apply
13875 immediately to the named function. For unnamed ones we then parse
13876 a declaration or definition, which must be for a function. */
13877
13878 static void
13879 c_parser_oacc_routine (c_parser *parser, enum pragma_context context)
13880 {
13881 tree decl = NULL_TREE;
13882 /* Create a dummy claue, to record location. */
13883 tree c_head = build_omp_clause (c_parser_peek_token (parser)->location,
13884 OMP_CLAUSE_SEQ);
13885
13886 if (context != pragma_external)
13887 c_parser_error (parser, "%<#pragma acc routine%> not at file scope");
13888
13889 c_parser_consume_pragma (parser);
13890
13891 /* Scan for optional '( name )'. */
13892 if (c_parser_peek_token (parser)->type == CPP_OPEN_PAREN)
13893 {
13894 c_parser_consume_token (parser);
13895
13896 c_token *token = c_parser_peek_token (parser);
13897
13898 if (token->type == CPP_NAME && (token->id_kind == C_ID_ID
13899 || token->id_kind == C_ID_TYPENAME))
13900 {
13901 decl = lookup_name (token->value);
13902 if (!decl)
13903 {
13904 error_at (token->location, "%qE has not been declared",
13905 token->value);
13906 decl = error_mark_node;
13907 }
13908 }
13909 else
13910 c_parser_error (parser, "expected function name");
13911
13912 if (token->type != CPP_CLOSE_PAREN)
13913 c_parser_consume_token (parser);
13914
13915 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
13916 }
13917
13918 /* Build a chain of clauses. */
13919 parser->in_pragma = true;
13920 tree clauses = c_parser_oacc_all_clauses
13921 (parser, OACC_ROUTINE_CLAUSE_MASK, "#pragma acc routine");
13922
13923 /* Force clauses to be non-null, by attaching context to it. */
13924 clauses = tree_cons (c_head, clauses, NULL_TREE);
13925
13926 if (decl)
13927 c_finish_oacc_routine (parser, decl, clauses, true, true, false);
13928 else if (c_parser_peek_token (parser)->type == CPP_PRAGMA)
13929 /* This will emit an error. */
13930 c_finish_oacc_routine (parser, NULL_TREE, clauses, false, true, false);
13931 else
13932 c_parser_declaration_or_fndef (parser, true, false, false, false,
13933 true, NULL, vNULL, clauses);
13934 }
13935
13936 /* Finalize an OpenACC routine pragma, applying it to FNDECL. CLAUSES
13937 are the parsed clauses. IS_DEFN is true if we're applying it to
13938 the definition (so expect FNDEF to look somewhat defined. */
13939
13940 static void
13941 c_finish_oacc_routine (c_parser *ARG_UNUSED (parser), tree fndecl,
13942 tree clauses, bool named, bool first, bool is_defn)
13943 {
13944 location_t loc = OMP_CLAUSE_LOCATION (TREE_PURPOSE (clauses));
13945
13946 if (!fndecl || TREE_CODE (fndecl) != FUNCTION_DECL || !first)
13947 {
13948 if (fndecl != error_mark_node)
13949 error_at (loc, "%<#pragma acc routine%> %s",
13950 named ? "does not refer to a function"
13951 : "not followed by single function");
13952 return;
13953 }
13954
13955 if (get_oacc_fn_attrib (fndecl))
13956 error_at (loc, "%<#pragma acc routine%> already applied to %D", fndecl);
13957
13958 if (TREE_USED (fndecl) || (!is_defn && DECL_SAVED_TREE (fndecl)))
13959 error_at (loc, "%<#pragma acc routine%> must be applied before %s",
13960 TREE_USED (fndecl) ? "use" : "definition");
13961
13962 /* Process for function attrib */
13963 tree dims = build_oacc_routine_dims (TREE_VALUE (clauses));
13964 replace_oacc_fn_attrib (fndecl, dims);
13965
13966 /* Also attach as a declare. */
13967 DECL_ATTRIBUTES (fndecl)
13968 = tree_cons (get_identifier ("omp declare target"),
13969 clauses, DECL_ATTRIBUTES (fndecl));
13970 }
13971
13972 /* OpenACC 2.0:
13973 # pragma acc update oacc-update-clause[optseq] new-line
13974 */
13975
13976 #define OACC_UPDATE_CLAUSE_MASK \
13977 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
13978 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
13979 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
13980 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
13981 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SELF) \
13982 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
13983
13984 static void
13985 c_parser_oacc_update (c_parser *parser)
13986 {
13987 location_t loc = c_parser_peek_token (parser)->location;
13988
13989 c_parser_consume_pragma (parser);
13990
13991 tree clauses = c_parser_oacc_all_clauses (parser, OACC_UPDATE_CLAUSE_MASK,
13992 "#pragma acc update");
13993 if (find_omp_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
13994 {
13995 error_at (loc,
13996 "%<#pragma acc update%> must contain at least one "
13997 "%<device%> or %<host%> or %<self%> clause");
13998 return;
13999 }
14000
14001 if (parser->error)
14002 return;
14003
14004 tree stmt = make_node (OACC_UPDATE);
14005 TREE_TYPE (stmt) = void_type_node;
14006 OACC_UPDATE_CLAUSES (stmt) = clauses;
14007 SET_EXPR_LOCATION (stmt, loc);
14008 add_stmt (stmt);
14009 }
14010
14011 /* OpenACC 2.0:
14012 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
14013
14014 LOC is the location of the #pragma token.
14015 */
14016
14017 #define OACC_WAIT_CLAUSE_MASK \
14018 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) )
14019
14020 static tree
14021 c_parser_oacc_wait (location_t loc, c_parser *parser, char *p_name)
14022 {
14023 tree clauses, list = NULL_TREE, stmt = NULL_TREE;
14024
14025 if (c_parser_peek_token (parser)->type == CPP_OPEN_PAREN)
14026 list = c_parser_oacc_wait_list (parser, loc, list);
14027
14028 strcpy (p_name, " wait");
14029 clauses = c_parser_oacc_all_clauses (parser, OACC_WAIT_CLAUSE_MASK, p_name);
14030 stmt = c_finish_oacc_wait (loc, list, clauses);
14031 add_stmt (stmt);
14032
14033 return stmt;
14034 }
14035
14036 /* OpenMP 2.5:
14037 # pragma omp atomic new-line
14038 expression-stmt
14039
14040 expression-stmt:
14041 x binop= expr | x++ | ++x | x-- | --x
14042 binop:
14043 +, *, -, /, &, ^, |, <<, >>
14044
14045 where x is an lvalue expression with scalar type.
14046
14047 OpenMP 3.1:
14048 # pragma omp atomic new-line
14049 update-stmt
14050
14051 # pragma omp atomic read new-line
14052 read-stmt
14053
14054 # pragma omp atomic write new-line
14055 write-stmt
14056
14057 # pragma omp atomic update new-line
14058 update-stmt
14059
14060 # pragma omp atomic capture new-line
14061 capture-stmt
14062
14063 # pragma omp atomic capture new-line
14064 capture-block
14065
14066 read-stmt:
14067 v = x
14068 write-stmt:
14069 x = expr
14070 update-stmt:
14071 expression-stmt | x = x binop expr
14072 capture-stmt:
14073 v = expression-stmt
14074 capture-block:
14075 { v = x; update-stmt; } | { update-stmt; v = x; }
14076
14077 OpenMP 4.0:
14078 update-stmt:
14079 expression-stmt | x = x binop expr | x = expr binop x
14080 capture-stmt:
14081 v = update-stmt
14082 capture-block:
14083 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
14084
14085 where x and v are lvalue expressions with scalar type.
14086
14087 LOC is the location of the #pragma token. */
14088
14089 static void
14090 c_parser_omp_atomic (location_t loc, c_parser *parser)
14091 {
14092 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE;
14093 tree lhs1 = NULL_TREE, rhs1 = NULL_TREE;
14094 tree stmt, orig_lhs, unfolded_lhs = NULL_TREE, unfolded_lhs1 = NULL_TREE;
14095 enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
14096 struct c_expr expr;
14097 location_t eloc;
14098 bool structured_block = false;
14099 bool swapped = false;
14100 bool seq_cst = false;
14101 bool non_lvalue_p;
14102
14103 if (c_parser_next_token_is (parser, CPP_NAME))
14104 {
14105 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
14106 if (!strcmp (p, "seq_cst"))
14107 {
14108 seq_cst = true;
14109 c_parser_consume_token (parser);
14110 if (c_parser_next_token_is (parser, CPP_COMMA)
14111 && c_parser_peek_2nd_token (parser)->type == CPP_NAME)
14112 c_parser_consume_token (parser);
14113 }
14114 }
14115 if (c_parser_next_token_is (parser, CPP_NAME))
14116 {
14117 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
14118
14119 if (!strcmp (p, "read"))
14120 code = OMP_ATOMIC_READ;
14121 else if (!strcmp (p, "write"))
14122 code = NOP_EXPR;
14123 else if (!strcmp (p, "update"))
14124 code = OMP_ATOMIC;
14125 else if (!strcmp (p, "capture"))
14126 code = OMP_ATOMIC_CAPTURE_NEW;
14127 else
14128 p = NULL;
14129 if (p)
14130 c_parser_consume_token (parser);
14131 }
14132 if (!seq_cst)
14133 {
14134 if (c_parser_next_token_is (parser, CPP_COMMA)
14135 && c_parser_peek_2nd_token (parser)->type == CPP_NAME)
14136 c_parser_consume_token (parser);
14137
14138 if (c_parser_next_token_is (parser, CPP_NAME))
14139 {
14140 const char *p
14141 = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
14142 if (!strcmp (p, "seq_cst"))
14143 {
14144 seq_cst = true;
14145 c_parser_consume_token (parser);
14146 }
14147 }
14148 }
14149 c_parser_skip_to_pragma_eol (parser);
14150
14151 switch (code)
14152 {
14153 case OMP_ATOMIC_READ:
14154 case NOP_EXPR: /* atomic write */
14155 v = c_parser_cast_expression (parser, NULL).value;
14156 non_lvalue_p = !lvalue_p (v);
14157 v = c_fully_fold (v, false, NULL);
14158 if (v == error_mark_node)
14159 goto saw_error;
14160 if (non_lvalue_p)
14161 v = non_lvalue (v);
14162 loc = c_parser_peek_token (parser)->location;
14163 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
14164 goto saw_error;
14165 if (code == NOP_EXPR)
14166 {
14167 lhs = c_parser_expression (parser).value;
14168 lhs = c_fully_fold (lhs, false, NULL);
14169 if (lhs == error_mark_node)
14170 goto saw_error;
14171 }
14172 else
14173 {
14174 lhs = c_parser_cast_expression (parser, NULL).value;
14175 non_lvalue_p = !lvalue_p (lhs);
14176 lhs = c_fully_fold (lhs, false, NULL);
14177 if (lhs == error_mark_node)
14178 goto saw_error;
14179 if (non_lvalue_p)
14180 lhs = non_lvalue (lhs);
14181 }
14182 if (code == NOP_EXPR)
14183 {
14184 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
14185 opcode. */
14186 code = OMP_ATOMIC;
14187 rhs = lhs;
14188 lhs = v;
14189 v = NULL_TREE;
14190 }
14191 goto done;
14192 case OMP_ATOMIC_CAPTURE_NEW:
14193 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
14194 {
14195 c_parser_consume_token (parser);
14196 structured_block = true;
14197 }
14198 else
14199 {
14200 v = c_parser_cast_expression (parser, NULL).value;
14201 non_lvalue_p = !lvalue_p (v);
14202 v = c_fully_fold (v, false, NULL);
14203 if (v == error_mark_node)
14204 goto saw_error;
14205 if (non_lvalue_p)
14206 v = non_lvalue (v);
14207 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
14208 goto saw_error;
14209 }
14210 break;
14211 default:
14212 break;
14213 }
14214
14215 /* For structured_block case we don't know yet whether
14216 old or new x should be captured. */
14217 restart:
14218 eloc = c_parser_peek_token (parser)->location;
14219 expr = c_parser_cast_expression (parser, NULL);
14220 lhs = expr.value;
14221 expr = default_function_array_conversion (eloc, expr);
14222 unfolded_lhs = expr.value;
14223 lhs = c_fully_fold (lhs, false, NULL);
14224 orig_lhs = lhs;
14225 switch (TREE_CODE (lhs))
14226 {
14227 case ERROR_MARK:
14228 saw_error:
14229 c_parser_skip_to_end_of_block_or_statement (parser);
14230 if (structured_block)
14231 {
14232 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
14233 c_parser_consume_token (parser);
14234 else if (code == OMP_ATOMIC_CAPTURE_NEW)
14235 {
14236 c_parser_skip_to_end_of_block_or_statement (parser);
14237 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
14238 c_parser_consume_token (parser);
14239 }
14240 }
14241 return;
14242
14243 case POSTINCREMENT_EXPR:
14244 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
14245 code = OMP_ATOMIC_CAPTURE_OLD;
14246 /* FALLTHROUGH */
14247 case PREINCREMENT_EXPR:
14248 lhs = TREE_OPERAND (lhs, 0);
14249 unfolded_lhs = NULL_TREE;
14250 opcode = PLUS_EXPR;
14251 rhs = integer_one_node;
14252 break;
14253
14254 case POSTDECREMENT_EXPR:
14255 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
14256 code = OMP_ATOMIC_CAPTURE_OLD;
14257 /* FALLTHROUGH */
14258 case PREDECREMENT_EXPR:
14259 lhs = TREE_OPERAND (lhs, 0);
14260 unfolded_lhs = NULL_TREE;
14261 opcode = MINUS_EXPR;
14262 rhs = integer_one_node;
14263 break;
14264
14265 case COMPOUND_EXPR:
14266 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
14267 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
14268 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
14269 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
14270 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
14271 (TREE_OPERAND (lhs, 1), 0), 0)))
14272 == BOOLEAN_TYPE)
14273 /* Undo effects of boolean_increment for post {in,de}crement. */
14274 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
14275 /* FALLTHRU */
14276 case MODIFY_EXPR:
14277 if (TREE_CODE (lhs) == MODIFY_EXPR
14278 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
14279 {
14280 /* Undo effects of boolean_increment. */
14281 if (integer_onep (TREE_OPERAND (lhs, 1)))
14282 {
14283 /* This is pre or post increment. */
14284 rhs = TREE_OPERAND (lhs, 1);
14285 lhs = TREE_OPERAND (lhs, 0);
14286 unfolded_lhs = NULL_TREE;
14287 opcode = NOP_EXPR;
14288 if (code == OMP_ATOMIC_CAPTURE_NEW
14289 && !structured_block
14290 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
14291 code = OMP_ATOMIC_CAPTURE_OLD;
14292 break;
14293 }
14294 if (TREE_CODE (TREE_OPERAND (lhs, 1)) == TRUTH_NOT_EXPR
14295 && TREE_OPERAND (lhs, 0)
14296 == TREE_OPERAND (TREE_OPERAND (lhs, 1), 0))
14297 {
14298 /* This is pre or post decrement. */
14299 rhs = TREE_OPERAND (lhs, 1);
14300 lhs = TREE_OPERAND (lhs, 0);
14301 unfolded_lhs = NULL_TREE;
14302 opcode = NOP_EXPR;
14303 if (code == OMP_ATOMIC_CAPTURE_NEW
14304 && !structured_block
14305 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
14306 code = OMP_ATOMIC_CAPTURE_OLD;
14307 break;
14308 }
14309 }
14310 /* FALLTHRU */
14311 default:
14312 if (!lvalue_p (unfolded_lhs))
14313 lhs = non_lvalue (lhs);
14314 switch (c_parser_peek_token (parser)->type)
14315 {
14316 case CPP_MULT_EQ:
14317 opcode = MULT_EXPR;
14318 break;
14319 case CPP_DIV_EQ:
14320 opcode = TRUNC_DIV_EXPR;
14321 break;
14322 case CPP_PLUS_EQ:
14323 opcode = PLUS_EXPR;
14324 break;
14325 case CPP_MINUS_EQ:
14326 opcode = MINUS_EXPR;
14327 break;
14328 case CPP_LSHIFT_EQ:
14329 opcode = LSHIFT_EXPR;
14330 break;
14331 case CPP_RSHIFT_EQ:
14332 opcode = RSHIFT_EXPR;
14333 break;
14334 case CPP_AND_EQ:
14335 opcode = BIT_AND_EXPR;
14336 break;
14337 case CPP_OR_EQ:
14338 opcode = BIT_IOR_EXPR;
14339 break;
14340 case CPP_XOR_EQ:
14341 opcode = BIT_XOR_EXPR;
14342 break;
14343 case CPP_EQ:
14344 c_parser_consume_token (parser);
14345 eloc = c_parser_peek_token (parser)->location;
14346 expr = c_parser_expr_no_commas (parser, NULL, unfolded_lhs);
14347 rhs1 = expr.value;
14348 switch (TREE_CODE (rhs1))
14349 {
14350 case MULT_EXPR:
14351 case TRUNC_DIV_EXPR:
14352 case RDIV_EXPR:
14353 case PLUS_EXPR:
14354 case MINUS_EXPR:
14355 case LSHIFT_EXPR:
14356 case RSHIFT_EXPR:
14357 case BIT_AND_EXPR:
14358 case BIT_IOR_EXPR:
14359 case BIT_XOR_EXPR:
14360 if (c_tree_equal (TREE_OPERAND (rhs1, 0), unfolded_lhs))
14361 {
14362 opcode = TREE_CODE (rhs1);
14363 rhs = c_fully_fold (TREE_OPERAND (rhs1, 1), false, NULL);
14364 rhs1 = c_fully_fold (TREE_OPERAND (rhs1, 0), false, NULL);
14365 goto stmt_done;
14366 }
14367 if (c_tree_equal (TREE_OPERAND (rhs1, 1), unfolded_lhs))
14368 {
14369 opcode = TREE_CODE (rhs1);
14370 rhs = c_fully_fold (TREE_OPERAND (rhs1, 0), false, NULL);
14371 rhs1 = c_fully_fold (TREE_OPERAND (rhs1, 1), false, NULL);
14372 swapped = !commutative_tree_code (opcode);
14373 goto stmt_done;
14374 }
14375 break;
14376 case ERROR_MARK:
14377 goto saw_error;
14378 default:
14379 break;
14380 }
14381 if (c_parser_peek_token (parser)->type == CPP_SEMICOLON)
14382 {
14383 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
14384 {
14385 code = OMP_ATOMIC_CAPTURE_OLD;
14386 v = lhs;
14387 lhs = NULL_TREE;
14388 expr = default_function_array_read_conversion (eloc, expr);
14389 unfolded_lhs1 = expr.value;
14390 lhs1 = c_fully_fold (unfolded_lhs1, false, NULL);
14391 rhs1 = NULL_TREE;
14392 c_parser_consume_token (parser);
14393 goto restart;
14394 }
14395 if (structured_block)
14396 {
14397 opcode = NOP_EXPR;
14398 expr = default_function_array_read_conversion (eloc, expr);
14399 rhs = c_fully_fold (expr.value, false, NULL);
14400 rhs1 = NULL_TREE;
14401 goto stmt_done;
14402 }
14403 }
14404 c_parser_error (parser, "invalid form of %<#pragma omp atomic%>");
14405 goto saw_error;
14406 default:
14407 c_parser_error (parser,
14408 "invalid operator for %<#pragma omp atomic%>");
14409 goto saw_error;
14410 }
14411
14412 /* Arrange to pass the location of the assignment operator to
14413 c_finish_omp_atomic. */
14414 loc = c_parser_peek_token (parser)->location;
14415 c_parser_consume_token (parser);
14416 eloc = c_parser_peek_token (parser)->location;
14417 expr = c_parser_expression (parser);
14418 expr = default_function_array_read_conversion (eloc, expr);
14419 rhs = expr.value;
14420 rhs = c_fully_fold (rhs, false, NULL);
14421 break;
14422 }
14423 stmt_done:
14424 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
14425 {
14426 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
14427 goto saw_error;
14428 v = c_parser_cast_expression (parser, NULL).value;
14429 non_lvalue_p = !lvalue_p (v);
14430 v = c_fully_fold (v, false, NULL);
14431 if (v == error_mark_node)
14432 goto saw_error;
14433 if (non_lvalue_p)
14434 v = non_lvalue (v);
14435 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
14436 goto saw_error;
14437 eloc = c_parser_peek_token (parser)->location;
14438 expr = c_parser_cast_expression (parser, NULL);
14439 lhs1 = expr.value;
14440 expr = default_function_array_read_conversion (eloc, expr);
14441 unfolded_lhs1 = expr.value;
14442 lhs1 = c_fully_fold (lhs1, false, NULL);
14443 if (lhs1 == error_mark_node)
14444 goto saw_error;
14445 if (!lvalue_p (unfolded_lhs1))
14446 lhs1 = non_lvalue (lhs1);
14447 }
14448 if (structured_block)
14449 {
14450 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
14451 c_parser_require (parser, CPP_CLOSE_BRACE, "expected %<}%>");
14452 }
14453 done:
14454 if (unfolded_lhs && unfolded_lhs1
14455 && !c_tree_equal (unfolded_lhs, unfolded_lhs1))
14456 {
14457 error ("%<#pragma omp atomic capture%> uses two different "
14458 "expressions for memory");
14459 stmt = error_mark_node;
14460 }
14461 else
14462 stmt = c_finish_omp_atomic (loc, code, opcode, lhs, rhs, v, lhs1, rhs1,
14463 swapped, seq_cst);
14464 if (stmt != error_mark_node)
14465 add_stmt (stmt);
14466
14467 if (!structured_block)
14468 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
14469 }
14470
14471
14472 /* OpenMP 2.5:
14473 # pragma omp barrier new-line
14474 */
14475
14476 static void
14477 c_parser_omp_barrier (c_parser *parser)
14478 {
14479 location_t loc = c_parser_peek_token (parser)->location;
14480 c_parser_consume_pragma (parser);
14481 c_parser_skip_to_pragma_eol (parser);
14482
14483 c_finish_omp_barrier (loc);
14484 }
14485
14486 /* OpenMP 2.5:
14487 # pragma omp critical [(name)] new-line
14488 structured-block
14489
14490 OpenMP 4.5:
14491 # pragma omp critical [(name) [hint(expression)]] new-line
14492
14493 LOC is the location of the #pragma itself. */
14494
14495 #define OMP_CRITICAL_CLAUSE_MASK \
14496 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HINT) )
14497
14498 static tree
14499 c_parser_omp_critical (location_t loc, c_parser *parser)
14500 {
14501 tree stmt, name = NULL_TREE, clauses = NULL_TREE;
14502
14503 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
14504 {
14505 c_parser_consume_token (parser);
14506 if (c_parser_next_token_is (parser, CPP_NAME))
14507 {
14508 name = c_parser_peek_token (parser)->value;
14509 c_parser_consume_token (parser);
14510 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
14511 }
14512 else
14513 c_parser_error (parser, "expected identifier");
14514
14515 clauses = c_parser_omp_all_clauses (parser,
14516 OMP_CRITICAL_CLAUSE_MASK,
14517 "#pragma omp critical");
14518 }
14519 else
14520 {
14521 if (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
14522 c_parser_error (parser, "expected %<(%> or end of line");
14523 c_parser_skip_to_pragma_eol (parser);
14524 }
14525
14526 stmt = c_parser_omp_structured_block (parser);
14527 return c_finish_omp_critical (loc, stmt, name, clauses);
14528 }
14529
14530 /* OpenMP 2.5:
14531 # pragma omp flush flush-vars[opt] new-line
14532
14533 flush-vars:
14534 ( variable-list ) */
14535
14536 static void
14537 c_parser_omp_flush (c_parser *parser)
14538 {
14539 location_t loc = c_parser_peek_token (parser)->location;
14540 c_parser_consume_pragma (parser);
14541 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
14542 c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
14543 else if (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
14544 c_parser_error (parser, "expected %<(%> or end of line");
14545 c_parser_skip_to_pragma_eol (parser);
14546
14547 c_finish_omp_flush (loc);
14548 }
14549
14550 /* Parse the restricted form of loop statements allowed by OpenACC and OpenMP.
14551 The real trick here is to determine the loop control variable early
14552 so that we can push a new decl if necessary to make it private.
14553 LOC is the location of the "acc" or "omp" in "#pragma acc" or "#pragma omp",
14554 respectively. */
14555
14556 static tree
14557 c_parser_omp_for_loop (location_t loc, c_parser *parser, enum tree_code code,
14558 tree clauses, tree *cclauses)
14559 {
14560 tree decl, cond, incr, save_break, save_cont, body, init, stmt, cl;
14561 tree declv, condv, incrv, initv, ret = NULL_TREE;
14562 tree pre_body = NULL_TREE, this_pre_body;
14563 tree ordered_cl = NULL_TREE;
14564 bool fail = false, open_brace_parsed = false;
14565 int i, collapse = 1, ordered = 0, count, nbraces = 0;
14566 location_t for_loc;
14567 vec<tree, va_gc> *for_block = make_tree_vector ();
14568
14569 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
14570 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
14571 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
14572 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_ORDERED
14573 && OMP_CLAUSE_ORDERED_EXPR (cl))
14574 {
14575 ordered_cl = cl;
14576 ordered = tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl));
14577 }
14578
14579 if (ordered && ordered < collapse)
14580 {
14581 error_at (OMP_CLAUSE_LOCATION (ordered_cl),
14582 "%<ordered%> clause parameter is less than %<collapse%>");
14583 OMP_CLAUSE_ORDERED_EXPR (ordered_cl)
14584 = build_int_cst (NULL_TREE, collapse);
14585 ordered = collapse;
14586 }
14587 if (ordered)
14588 {
14589 for (tree *pc = &clauses; *pc; )
14590 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LINEAR)
14591 {
14592 error_at (OMP_CLAUSE_LOCATION (*pc),
14593 "%<linear%> clause may not be specified together "
14594 "with %<ordered%> clause with a parameter");
14595 *pc = OMP_CLAUSE_CHAIN (*pc);
14596 }
14597 else
14598 pc = &OMP_CLAUSE_CHAIN (*pc);
14599 }
14600
14601 gcc_assert (collapse >= 1 && ordered >= 0);
14602 count = ordered ? ordered : collapse;
14603
14604 declv = make_tree_vec (count);
14605 initv = make_tree_vec (count);
14606 condv = make_tree_vec (count);
14607 incrv = make_tree_vec (count);
14608
14609 if (code != CILK_FOR
14610 && !c_parser_next_token_is_keyword (parser, RID_FOR))
14611 {
14612 c_parser_error (parser, "for statement expected");
14613 return NULL;
14614 }
14615 if (code == CILK_FOR
14616 && !c_parser_next_token_is_keyword (parser, RID_CILK_FOR))
14617 {
14618 c_parser_error (parser, "_Cilk_for statement expected");
14619 return NULL;
14620 }
14621 for_loc = c_parser_peek_token (parser)->location;
14622 c_parser_consume_token (parser);
14623
14624 for (i = 0; i < count; i++)
14625 {
14626 int bracecount = 0;
14627
14628 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
14629 goto pop_scopes;
14630
14631 /* Parse the initialization declaration or expression. */
14632 if (c_parser_next_tokens_start_declaration (parser))
14633 {
14634 if (i > 0)
14635 vec_safe_push (for_block, c_begin_compound_stmt (true));
14636 this_pre_body = push_stmt_list ();
14637 c_parser_declaration_or_fndef (parser, true, true, true, true, true,
14638 NULL, vNULL);
14639 if (this_pre_body)
14640 {
14641 this_pre_body = pop_stmt_list (this_pre_body);
14642 if (pre_body)
14643 {
14644 tree t = pre_body;
14645 pre_body = push_stmt_list ();
14646 add_stmt (t);
14647 add_stmt (this_pre_body);
14648 pre_body = pop_stmt_list (pre_body);
14649 }
14650 else
14651 pre_body = this_pre_body;
14652 }
14653 decl = check_for_loop_decls (for_loc, flag_isoc99);
14654 if (decl == NULL)
14655 goto error_init;
14656 if (DECL_INITIAL (decl) == error_mark_node)
14657 decl = error_mark_node;
14658 init = decl;
14659 }
14660 else if (c_parser_next_token_is (parser, CPP_NAME)
14661 && c_parser_peek_2nd_token (parser)->type == CPP_EQ)
14662 {
14663 struct c_expr decl_exp;
14664 struct c_expr init_exp;
14665 location_t init_loc;
14666
14667 decl_exp = c_parser_postfix_expression (parser);
14668 decl = decl_exp.value;
14669
14670 c_parser_require (parser, CPP_EQ, "expected %<=%>");
14671
14672 init_loc = c_parser_peek_token (parser)->location;
14673 init_exp = c_parser_expr_no_commas (parser, NULL);
14674 init_exp = default_function_array_read_conversion (init_loc,
14675 init_exp);
14676 init = build_modify_expr (init_loc, decl, decl_exp.original_type,
14677 NOP_EXPR, init_loc, init_exp.value,
14678 init_exp.original_type);
14679 init = c_process_expr_stmt (init_loc, init);
14680
14681 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
14682 }
14683 else
14684 {
14685 error_init:
14686 c_parser_error (parser,
14687 "expected iteration declaration or initialization");
14688 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
14689 "expected %<)%>");
14690 fail = true;
14691 goto parse_next;
14692 }
14693
14694 /* Parse the loop condition. */
14695 cond = NULL_TREE;
14696 if (c_parser_next_token_is_not (parser, CPP_SEMICOLON))
14697 {
14698 location_t cond_loc = c_parser_peek_token (parser)->location;
14699 struct c_expr cond_expr
14700 = c_parser_binary_expression (parser, NULL, NULL_TREE);
14701
14702 cond = cond_expr.value;
14703 cond = c_objc_common_truthvalue_conversion (cond_loc, cond);
14704 cond = c_fully_fold (cond, false, NULL);
14705 switch (cond_expr.original_code)
14706 {
14707 case GT_EXPR:
14708 case GE_EXPR:
14709 case LT_EXPR:
14710 case LE_EXPR:
14711 break;
14712 case NE_EXPR:
14713 if (code == CILK_SIMD || code == CILK_FOR)
14714 break;
14715 /* FALLTHRU. */
14716 default:
14717 /* Can't be cond = error_mark_node, because we want to preserve
14718 the location until c_finish_omp_for. */
14719 cond = build1 (NOP_EXPR, boolean_type_node, error_mark_node);
14720 break;
14721 }
14722 protected_set_expr_location (cond, cond_loc);
14723 }
14724 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
14725
14726 /* Parse the increment expression. */
14727 incr = NULL_TREE;
14728 if (c_parser_next_token_is_not (parser, CPP_CLOSE_PAREN))
14729 {
14730 location_t incr_loc = c_parser_peek_token (parser)->location;
14731
14732 incr = c_process_expr_stmt (incr_loc,
14733 c_parser_expression (parser).value);
14734 }
14735 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
14736
14737 if (decl == NULL || decl == error_mark_node || init == error_mark_node)
14738 fail = true;
14739 else
14740 {
14741 TREE_VEC_ELT (declv, i) = decl;
14742 TREE_VEC_ELT (initv, i) = init;
14743 TREE_VEC_ELT (condv, i) = cond;
14744 TREE_VEC_ELT (incrv, i) = incr;
14745 }
14746
14747 parse_next:
14748 if (i == count - 1)
14749 break;
14750
14751 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
14752 in between the collapsed for loops to be still considered perfectly
14753 nested. Hopefully the final version clarifies this.
14754 For now handle (multiple) {'s and empty statements. */
14755 do
14756 {
14757 if (c_parser_next_token_is_keyword (parser, RID_FOR))
14758 {
14759 c_parser_consume_token (parser);
14760 break;
14761 }
14762 else if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
14763 {
14764 c_parser_consume_token (parser);
14765 bracecount++;
14766 }
14767 else if (bracecount
14768 && c_parser_next_token_is (parser, CPP_SEMICOLON))
14769 c_parser_consume_token (parser);
14770 else
14771 {
14772 c_parser_error (parser, "not enough perfectly nested loops");
14773 if (bracecount)
14774 {
14775 open_brace_parsed = true;
14776 bracecount--;
14777 }
14778 fail = true;
14779 count = 0;
14780 break;
14781 }
14782 }
14783 while (1);
14784
14785 nbraces += bracecount;
14786 }
14787
14788 save_break = c_break_label;
14789 if (code == CILK_SIMD)
14790 c_break_label = build_int_cst (size_type_node, 2);
14791 else
14792 c_break_label = size_one_node;
14793 save_cont = c_cont_label;
14794 c_cont_label = NULL_TREE;
14795 body = push_stmt_list ();
14796
14797 if (open_brace_parsed)
14798 {
14799 location_t here = c_parser_peek_token (parser)->location;
14800 stmt = c_begin_compound_stmt (true);
14801 c_parser_compound_statement_nostart (parser);
14802 add_stmt (c_end_compound_stmt (here, stmt, true));
14803 }
14804 else
14805 add_stmt (c_parser_c99_block_statement (parser));
14806 if (c_cont_label)
14807 {
14808 tree t = build1 (LABEL_EXPR, void_type_node, c_cont_label);
14809 SET_EXPR_LOCATION (t, loc);
14810 add_stmt (t);
14811 }
14812
14813 body = pop_stmt_list (body);
14814 c_break_label = save_break;
14815 c_cont_label = save_cont;
14816
14817 while (nbraces)
14818 {
14819 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
14820 {
14821 c_parser_consume_token (parser);
14822 nbraces--;
14823 }
14824 else if (c_parser_next_token_is (parser, CPP_SEMICOLON))
14825 c_parser_consume_token (parser);
14826 else
14827 {
14828 c_parser_error (parser, "collapsed loops not perfectly nested");
14829 while (nbraces)
14830 {
14831 location_t here = c_parser_peek_token (parser)->location;
14832 stmt = c_begin_compound_stmt (true);
14833 add_stmt (body);
14834 c_parser_compound_statement_nostart (parser);
14835 body = c_end_compound_stmt (here, stmt, true);
14836 nbraces--;
14837 }
14838 goto pop_scopes;
14839 }
14840 }
14841
14842 /* Only bother calling c_finish_omp_for if we haven't already generated
14843 an error from the initialization parsing. */
14844 if (!fail)
14845 {
14846 stmt = c_finish_omp_for (loc, code, declv, NULL, initv, condv,
14847 incrv, body, pre_body);
14848
14849 /* Check for iterators appearing in lb, b or incr expressions. */
14850 if (stmt && !c_omp_check_loop_iv (stmt, declv, NULL))
14851 stmt = NULL_TREE;
14852
14853 if (stmt)
14854 {
14855 add_stmt (stmt);
14856
14857 if (cclauses != NULL
14858 && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL)
14859 {
14860 tree *c;
14861 for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
14862 if (OMP_CLAUSE_CODE (*c) != OMP_CLAUSE_FIRSTPRIVATE
14863 && OMP_CLAUSE_CODE (*c) != OMP_CLAUSE_LASTPRIVATE)
14864 c = &OMP_CLAUSE_CHAIN (*c);
14865 else
14866 {
14867 for (i = 0; i < count; i++)
14868 if (TREE_VEC_ELT (declv, i) == OMP_CLAUSE_DECL (*c))
14869 break;
14870 if (i == count)
14871 c = &OMP_CLAUSE_CHAIN (*c);
14872 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE)
14873 {
14874 error_at (loc,
14875 "iteration variable %qD should not be firstprivate",
14876 OMP_CLAUSE_DECL (*c));
14877 *c = OMP_CLAUSE_CHAIN (*c);
14878 }
14879 else
14880 {
14881 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
14882 tree l = *c;
14883 *c = OMP_CLAUSE_CHAIN (*c);
14884 if (code == OMP_SIMD)
14885 {
14886 OMP_CLAUSE_CHAIN (l)
14887 = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
14888 cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
14889 }
14890 else
14891 {
14892 OMP_CLAUSE_CHAIN (l) = clauses;
14893 clauses = l;
14894 }
14895 }
14896 }
14897 }
14898 OMP_FOR_CLAUSES (stmt) = clauses;
14899 }
14900 ret = stmt;
14901 }
14902 pop_scopes:
14903 while (!for_block->is_empty ())
14904 {
14905 /* FIXME diagnostics: LOC below should be the actual location of
14906 this particular for block. We need to build a list of
14907 locations to go along with FOR_BLOCK. */
14908 stmt = c_end_compound_stmt (loc, for_block->pop (), true);
14909 add_stmt (stmt);
14910 }
14911 release_tree_vector (for_block);
14912 return ret;
14913 }
14914
14915 /* Helper function for OpenMP parsing, split clauses and call
14916 finish_omp_clauses on each of the set of clauses afterwards. */
14917
14918 static void
14919 omp_split_clauses (location_t loc, enum tree_code code,
14920 omp_clause_mask mask, tree clauses, tree *cclauses)
14921 {
14922 int i;
14923 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
14924 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
14925 if (cclauses[i])
14926 cclauses[i] = c_finish_omp_clauses (cclauses[i], true);
14927 }
14928
14929 /* OpenMP 4.0:
14930 #pragma omp simd simd-clause[optseq] new-line
14931 for-loop
14932
14933 LOC is the location of the #pragma token.
14934 */
14935
14936 #define OMP_SIMD_CLAUSE_MASK \
14937 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
14938 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
14939 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
14940 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
14941 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
14942 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
14943 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
14944 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
14945
14946 static tree
14947 c_parser_omp_simd (location_t loc, c_parser *parser,
14948 char *p_name, omp_clause_mask mask, tree *cclauses)
14949 {
14950 tree block, clauses, ret;
14951
14952 strcat (p_name, " simd");
14953 mask |= OMP_SIMD_CLAUSE_MASK;
14954
14955 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
14956 if (cclauses)
14957 {
14958 omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
14959 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
14960 tree c = find_omp_clause (cclauses[C_OMP_CLAUSE_SPLIT_FOR],
14961 OMP_CLAUSE_ORDERED);
14962 if (c && OMP_CLAUSE_ORDERED_EXPR (c))
14963 {
14964 error_at (OMP_CLAUSE_LOCATION (c),
14965 "%<ordered%> clause with parameter may not be specified "
14966 "on %qs construct", p_name);
14967 OMP_CLAUSE_ORDERED_EXPR (c) = NULL_TREE;
14968 }
14969 }
14970
14971 block = c_begin_compound_stmt (true);
14972 ret = c_parser_omp_for_loop (loc, parser, OMP_SIMD, clauses, cclauses);
14973 block = c_end_compound_stmt (loc, block, true);
14974 add_stmt (block);
14975
14976 return ret;
14977 }
14978
14979 /* OpenMP 2.5:
14980 #pragma omp for for-clause[optseq] new-line
14981 for-loop
14982
14983 OpenMP 4.0:
14984 #pragma omp for simd for-simd-clause[optseq] new-line
14985 for-loop
14986
14987 LOC is the location of the #pragma token.
14988 */
14989
14990 #define OMP_FOR_CLAUSE_MASK \
14991 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
14992 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
14993 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
14994 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
14995 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
14996 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
14997 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
14998 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
14999 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
15000
15001 static tree
15002 c_parser_omp_for (location_t loc, c_parser *parser,
15003 char *p_name, omp_clause_mask mask, tree *cclauses)
15004 {
15005 tree block, clauses, ret;
15006
15007 strcat (p_name, " for");
15008 mask |= OMP_FOR_CLAUSE_MASK;
15009 if (cclauses)
15010 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
15011 /* Composite distribute parallel for{, simd} disallows ordered clause. */
15012 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
15013 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
15014
15015 if (c_parser_next_token_is (parser, CPP_NAME))
15016 {
15017 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
15018
15019 if (strcmp (p, "simd") == 0)
15020 {
15021 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
15022 if (cclauses == NULL)
15023 cclauses = cclauses_buf;
15024
15025 c_parser_consume_token (parser);
15026 if (!flag_openmp) /* flag_openmp_simd */
15027 return c_parser_omp_simd (loc, parser, p_name, mask, cclauses);
15028 block = c_begin_compound_stmt (true);
15029 ret = c_parser_omp_simd (loc, parser, p_name, mask, cclauses);
15030 block = c_end_compound_stmt (loc, block, true);
15031 if (ret == NULL_TREE)
15032 return ret;
15033 ret = make_node (OMP_FOR);
15034 TREE_TYPE (ret) = void_type_node;
15035 OMP_FOR_BODY (ret) = block;
15036 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
15037 SET_EXPR_LOCATION (ret, loc);
15038 add_stmt (ret);
15039 return ret;
15040 }
15041 }
15042 if (!flag_openmp) /* flag_openmp_simd */
15043 {
15044 c_parser_skip_to_pragma_eol (parser, false);
15045 return NULL_TREE;
15046 }
15047
15048 /* Composite distribute parallel for disallows linear clause. */
15049 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
15050 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR);
15051
15052 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
15053 if (cclauses)
15054 {
15055 omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
15056 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
15057 }
15058
15059 block = c_begin_compound_stmt (true);
15060 ret = c_parser_omp_for_loop (loc, parser, OMP_FOR, clauses, cclauses);
15061 block = c_end_compound_stmt (loc, block, true);
15062 add_stmt (block);
15063
15064 return ret;
15065 }
15066
15067 /* OpenMP 2.5:
15068 # pragma omp master new-line
15069 structured-block
15070
15071 LOC is the location of the #pragma token.
15072 */
15073
15074 static tree
15075 c_parser_omp_master (location_t loc, c_parser *parser)
15076 {
15077 c_parser_skip_to_pragma_eol (parser);
15078 return c_finish_omp_master (loc, c_parser_omp_structured_block (parser));
15079 }
15080
15081 /* OpenMP 2.5:
15082 # pragma omp ordered new-line
15083 structured-block
15084
15085 OpenMP 4.5:
15086 # pragma omp ordered ordered-clauses new-line
15087 structured-block
15088
15089 # pragma omp ordered depend-clauses new-line */
15090
15091 #define OMP_ORDERED_CLAUSE_MASK \
15092 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREADS) \
15093 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMD))
15094
15095 #define OMP_ORDERED_DEPEND_CLAUSE_MASK \
15096 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
15097
15098 static bool
15099 c_parser_omp_ordered (c_parser *parser, enum pragma_context context)
15100 {
15101 location_t loc = c_parser_peek_token (parser)->location;
15102 c_parser_consume_pragma (parser);
15103
15104 if (context != pragma_stmt && context != pragma_compound)
15105 {
15106 c_parser_error (parser, "expected declaration specifiers");
15107 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
15108 return false;
15109 }
15110
15111 if (c_parser_next_token_is (parser, CPP_NAME))
15112 {
15113 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
15114
15115 if (!strcmp ("depend", p))
15116 {
15117 if (context == pragma_stmt)
15118 {
15119 error_at (loc,
15120 "%<#pragma omp ordered%> with %<depend> clause may "
15121 "only be used in compound statements");
15122 c_parser_skip_to_pragma_eol (parser, false);
15123 return false;
15124 }
15125
15126 tree clauses
15127 = c_parser_omp_all_clauses (parser,
15128 OMP_ORDERED_DEPEND_CLAUSE_MASK,
15129 "#pragma omp ordered");
15130 c_finish_omp_ordered (loc, clauses, NULL_TREE);
15131 return false;
15132 }
15133 }
15134
15135 tree clauses = c_parser_omp_all_clauses (parser, OMP_ORDERED_CLAUSE_MASK,
15136 "#pragma omp ordered");
15137 c_finish_omp_ordered (loc, clauses,
15138 c_parser_omp_structured_block (parser));
15139 return true;
15140 }
15141
15142 /* OpenMP 2.5:
15143
15144 section-scope:
15145 { section-sequence }
15146
15147 section-sequence:
15148 section-directive[opt] structured-block
15149 section-sequence section-directive structured-block
15150
15151 SECTIONS_LOC is the location of the #pragma omp sections. */
15152
15153 static tree
15154 c_parser_omp_sections_scope (location_t sections_loc, c_parser *parser)
15155 {
15156 tree stmt, substmt;
15157 bool error_suppress = false;
15158 location_t loc;
15159
15160 loc = c_parser_peek_token (parser)->location;
15161 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
15162 {
15163 /* Avoid skipping until the end of the block. */
15164 parser->error = false;
15165 return NULL_TREE;
15166 }
15167
15168 stmt = push_stmt_list ();
15169
15170 if (c_parser_peek_token (parser)->pragma_kind != PRAGMA_OMP_SECTION)
15171 {
15172 substmt = c_parser_omp_structured_block (parser);
15173 substmt = build1 (OMP_SECTION, void_type_node, substmt);
15174 SET_EXPR_LOCATION (substmt, loc);
15175 add_stmt (substmt);
15176 }
15177
15178 while (1)
15179 {
15180 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
15181 break;
15182 if (c_parser_next_token_is (parser, CPP_EOF))
15183 break;
15184
15185 loc = c_parser_peek_token (parser)->location;
15186 if (c_parser_peek_token (parser)->pragma_kind == PRAGMA_OMP_SECTION)
15187 {
15188 c_parser_consume_pragma (parser);
15189 c_parser_skip_to_pragma_eol (parser);
15190 error_suppress = false;
15191 }
15192 else if (!error_suppress)
15193 {
15194 error_at (loc, "expected %<#pragma omp section%> or %<}%>");
15195 error_suppress = true;
15196 }
15197
15198 substmt = c_parser_omp_structured_block (parser);
15199 substmt = build1 (OMP_SECTION, void_type_node, substmt);
15200 SET_EXPR_LOCATION (substmt, loc);
15201 add_stmt (substmt);
15202 }
15203 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE,
15204 "expected %<#pragma omp section%> or %<}%>");
15205
15206 substmt = pop_stmt_list (stmt);
15207
15208 stmt = make_node (OMP_SECTIONS);
15209 SET_EXPR_LOCATION (stmt, sections_loc);
15210 TREE_TYPE (stmt) = void_type_node;
15211 OMP_SECTIONS_BODY (stmt) = substmt;
15212
15213 return add_stmt (stmt);
15214 }
15215
15216 /* OpenMP 2.5:
15217 # pragma omp sections sections-clause[optseq] newline
15218 sections-scope
15219
15220 LOC is the location of the #pragma token.
15221 */
15222
15223 #define OMP_SECTIONS_CLAUSE_MASK \
15224 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
15225 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
15226 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
15227 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
15228 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
15229
15230 static tree
15231 c_parser_omp_sections (location_t loc, c_parser *parser,
15232 char *p_name, omp_clause_mask mask, tree *cclauses)
15233 {
15234 tree block, clauses, ret;
15235
15236 strcat (p_name, " sections");
15237 mask |= OMP_SECTIONS_CLAUSE_MASK;
15238 if (cclauses)
15239 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
15240
15241 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
15242 if (cclauses)
15243 {
15244 omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
15245 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
15246 }
15247
15248 block = c_begin_compound_stmt (true);
15249 ret = c_parser_omp_sections_scope (loc, parser);
15250 if (ret)
15251 OMP_SECTIONS_CLAUSES (ret) = clauses;
15252 block = c_end_compound_stmt (loc, block, true);
15253 add_stmt (block);
15254
15255 return ret;
15256 }
15257
15258 /* OpenMP 2.5:
15259 # pragma omp parallel parallel-clause[optseq] new-line
15260 structured-block
15261 # pragma omp parallel for parallel-for-clause[optseq] new-line
15262 structured-block
15263 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
15264 structured-block
15265
15266 OpenMP 4.0:
15267 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
15268 structured-block
15269
15270 LOC is the location of the #pragma token.
15271 */
15272
15273 #define OMP_PARALLEL_CLAUSE_MASK \
15274 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
15275 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
15276 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
15277 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
15278 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
15279 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
15280 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
15281 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
15282 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
15283
15284 static tree
15285 c_parser_omp_parallel (location_t loc, c_parser *parser,
15286 char *p_name, omp_clause_mask mask, tree *cclauses)
15287 {
15288 tree stmt, clauses, block;
15289
15290 strcat (p_name, " parallel");
15291 mask |= OMP_PARALLEL_CLAUSE_MASK;
15292 /* #pragma omp target parallel{, for, for simd} disallow copyin clause. */
15293 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) != 0
15294 && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) == 0)
15295 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN);
15296
15297 if (c_parser_next_token_is_keyword (parser, RID_FOR))
15298 {
15299 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
15300 if (cclauses == NULL)
15301 cclauses = cclauses_buf;
15302
15303 c_parser_consume_token (parser);
15304 if (!flag_openmp) /* flag_openmp_simd */
15305 return c_parser_omp_for (loc, parser, p_name, mask, cclauses);
15306 block = c_begin_omp_parallel ();
15307 tree ret = c_parser_omp_for (loc, parser, p_name, mask, cclauses);
15308 stmt
15309 = c_finish_omp_parallel (loc, cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
15310 block);
15311 if (ret == NULL_TREE)
15312 return ret;
15313 OMP_PARALLEL_COMBINED (stmt) = 1;
15314 return stmt;
15315 }
15316 /* When combined with distribute, parallel has to be followed by for.
15317 #pragma omp target parallel is allowed though. */
15318 else if (cclauses
15319 && (mask & (OMP_CLAUSE_MASK_1
15320 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
15321 {
15322 error_at (loc, "expected %<for%> after %qs", p_name);
15323 c_parser_skip_to_pragma_eol (parser);
15324 return NULL_TREE;
15325 }
15326 else if (!flag_openmp) /* flag_openmp_simd */
15327 {
15328 c_parser_skip_to_pragma_eol (parser, false);
15329 return NULL_TREE;
15330 }
15331 else if (cclauses == NULL && c_parser_next_token_is (parser, CPP_NAME))
15332 {
15333 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
15334 if (strcmp (p, "sections") == 0)
15335 {
15336 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
15337 if (cclauses == NULL)
15338 cclauses = cclauses_buf;
15339
15340 c_parser_consume_token (parser);
15341 block = c_begin_omp_parallel ();
15342 c_parser_omp_sections (loc, parser, p_name, mask, cclauses);
15343 stmt = c_finish_omp_parallel (loc,
15344 cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
15345 block);
15346 OMP_PARALLEL_COMBINED (stmt) = 1;
15347 return stmt;
15348 }
15349 }
15350
15351 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
15352 if (cclauses)
15353 {
15354 omp_split_clauses (loc, OMP_PARALLEL, mask, clauses, cclauses);
15355 clauses = cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL];
15356 }
15357
15358 block = c_begin_omp_parallel ();
15359 c_parser_statement (parser);
15360 stmt = c_finish_omp_parallel (loc, clauses, block);
15361
15362 return stmt;
15363 }
15364
15365 /* OpenMP 2.5:
15366 # pragma omp single single-clause[optseq] new-line
15367 structured-block
15368
15369 LOC is the location of the #pragma.
15370 */
15371
15372 #define OMP_SINGLE_CLAUSE_MASK \
15373 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
15374 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
15375 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
15376 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
15377
15378 static tree
15379 c_parser_omp_single (location_t loc, c_parser *parser)
15380 {
15381 tree stmt = make_node (OMP_SINGLE);
15382 SET_EXPR_LOCATION (stmt, loc);
15383 TREE_TYPE (stmt) = void_type_node;
15384
15385 OMP_SINGLE_CLAUSES (stmt)
15386 = c_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
15387 "#pragma omp single");
15388 OMP_SINGLE_BODY (stmt) = c_parser_omp_structured_block (parser);
15389
15390 return add_stmt (stmt);
15391 }
15392
15393 /* OpenMP 3.0:
15394 # pragma omp task task-clause[optseq] new-line
15395
15396 LOC is the location of the #pragma.
15397 */
15398
15399 #define OMP_TASK_CLAUSE_MASK \
15400 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
15401 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
15402 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
15403 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
15404 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
15405 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
15406 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
15407 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
15408 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
15409 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
15410
15411 static tree
15412 c_parser_omp_task (location_t loc, c_parser *parser)
15413 {
15414 tree clauses, block;
15415
15416 clauses = c_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
15417 "#pragma omp task");
15418
15419 block = c_begin_omp_task ();
15420 c_parser_statement (parser);
15421 return c_finish_omp_task (loc, clauses, block);
15422 }
15423
15424 /* OpenMP 3.0:
15425 # pragma omp taskwait new-line
15426 */
15427
15428 static void
15429 c_parser_omp_taskwait (c_parser *parser)
15430 {
15431 location_t loc = c_parser_peek_token (parser)->location;
15432 c_parser_consume_pragma (parser);
15433 c_parser_skip_to_pragma_eol (parser);
15434
15435 c_finish_omp_taskwait (loc);
15436 }
15437
15438 /* OpenMP 3.1:
15439 # pragma omp taskyield new-line
15440 */
15441
15442 static void
15443 c_parser_omp_taskyield (c_parser *parser)
15444 {
15445 location_t loc = c_parser_peek_token (parser)->location;
15446 c_parser_consume_pragma (parser);
15447 c_parser_skip_to_pragma_eol (parser);
15448
15449 c_finish_omp_taskyield (loc);
15450 }
15451
15452 /* OpenMP 4.0:
15453 # pragma omp taskgroup new-line
15454 */
15455
15456 static tree
15457 c_parser_omp_taskgroup (c_parser *parser)
15458 {
15459 location_t loc = c_parser_peek_token (parser)->location;
15460 c_parser_skip_to_pragma_eol (parser);
15461 return c_finish_omp_taskgroup (loc, c_parser_omp_structured_block (parser));
15462 }
15463
15464 /* OpenMP 4.0:
15465 # pragma omp cancel cancel-clause[optseq] new-line
15466
15467 LOC is the location of the #pragma.
15468 */
15469
15470 #define OMP_CANCEL_CLAUSE_MASK \
15471 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
15472 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
15473 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
15474 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
15475 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
15476
15477 static void
15478 c_parser_omp_cancel (c_parser *parser)
15479 {
15480 location_t loc = c_parser_peek_token (parser)->location;
15481
15482 c_parser_consume_pragma (parser);
15483 tree clauses = c_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
15484 "#pragma omp cancel");
15485
15486 c_finish_omp_cancel (loc, clauses);
15487 }
15488
15489 /* OpenMP 4.0:
15490 # pragma omp cancellation point cancelpt-clause[optseq] new-line
15491
15492 LOC is the location of the #pragma.
15493 */
15494
15495 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
15496 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
15497 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
15498 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
15499 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
15500
15501 static void
15502 c_parser_omp_cancellation_point (c_parser *parser)
15503 {
15504 location_t loc = c_parser_peek_token (parser)->location;
15505 tree clauses;
15506 bool point_seen = false;
15507
15508 c_parser_consume_pragma (parser);
15509 if (c_parser_next_token_is (parser, CPP_NAME))
15510 {
15511 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
15512 if (strcmp (p, "point") == 0)
15513 {
15514 c_parser_consume_token (parser);
15515 point_seen = true;
15516 }
15517 }
15518 if (!point_seen)
15519 {
15520 c_parser_error (parser, "expected %<point%>");
15521 c_parser_skip_to_pragma_eol (parser);
15522 return;
15523 }
15524
15525 clauses
15526 = c_parser_omp_all_clauses (parser, OMP_CANCELLATION_POINT_CLAUSE_MASK,
15527 "#pragma omp cancellation point");
15528
15529 c_finish_omp_cancellation_point (loc, clauses);
15530 }
15531
15532 /* OpenMP 4.0:
15533 #pragma omp distribute distribute-clause[optseq] new-line
15534 for-loop */
15535
15536 #define OMP_DISTRIBUTE_CLAUSE_MASK \
15537 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
15538 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
15539 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
15540 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
15541 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
15542
15543 static tree
15544 c_parser_omp_distribute (location_t loc, c_parser *parser,
15545 char *p_name, omp_clause_mask mask, tree *cclauses)
15546 {
15547 tree clauses, block, ret;
15548
15549 strcat (p_name, " distribute");
15550 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
15551
15552 if (c_parser_next_token_is (parser, CPP_NAME))
15553 {
15554 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
15555 bool simd = false;
15556 bool parallel = false;
15557
15558 if (strcmp (p, "simd") == 0)
15559 simd = true;
15560 else
15561 parallel = strcmp (p, "parallel") == 0;
15562 if (parallel || simd)
15563 {
15564 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
15565 if (cclauses == NULL)
15566 cclauses = cclauses_buf;
15567 c_parser_consume_token (parser);
15568 if (!flag_openmp) /* flag_openmp_simd */
15569 {
15570 if (simd)
15571 return c_parser_omp_simd (loc, parser, p_name, mask, cclauses);
15572 else
15573 return c_parser_omp_parallel (loc, parser, p_name, mask,
15574 cclauses);
15575 }
15576 block = c_begin_compound_stmt (true);
15577 if (simd)
15578 ret = c_parser_omp_simd (loc, parser, p_name, mask, cclauses);
15579 else
15580 ret = c_parser_omp_parallel (loc, parser, p_name, mask, cclauses);
15581 block = c_end_compound_stmt (loc, block, true);
15582 if (ret == NULL)
15583 return ret;
15584 ret = make_node (OMP_DISTRIBUTE);
15585 TREE_TYPE (ret) = void_type_node;
15586 OMP_FOR_BODY (ret) = block;
15587 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
15588 SET_EXPR_LOCATION (ret, loc);
15589 add_stmt (ret);
15590 return ret;
15591 }
15592 }
15593 if (!flag_openmp) /* flag_openmp_simd */
15594 {
15595 c_parser_skip_to_pragma_eol (parser, false);
15596 return NULL_TREE;
15597 }
15598
15599 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
15600 if (cclauses)
15601 {
15602 omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
15603 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
15604 }
15605
15606 block = c_begin_compound_stmt (true);
15607 ret = c_parser_omp_for_loop (loc, parser, OMP_DISTRIBUTE, clauses, NULL);
15608 block = c_end_compound_stmt (loc, block, true);
15609 add_stmt (block);
15610
15611 return ret;
15612 }
15613
15614 /* OpenMP 4.0:
15615 # pragma omp teams teams-clause[optseq] new-line
15616 structured-block */
15617
15618 #define OMP_TEAMS_CLAUSE_MASK \
15619 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
15620 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
15621 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
15622 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
15623 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
15624 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
15625 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
15626
15627 static tree
15628 c_parser_omp_teams (location_t loc, c_parser *parser,
15629 char *p_name, omp_clause_mask mask, tree *cclauses)
15630 {
15631 tree clauses, block, ret;
15632
15633 strcat (p_name, " teams");
15634 mask |= OMP_TEAMS_CLAUSE_MASK;
15635
15636 if (c_parser_next_token_is (parser, CPP_NAME))
15637 {
15638 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
15639 if (strcmp (p, "distribute") == 0)
15640 {
15641 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
15642 if (cclauses == NULL)
15643 cclauses = cclauses_buf;
15644
15645 c_parser_consume_token (parser);
15646 if (!flag_openmp) /* flag_openmp_simd */
15647 return c_parser_omp_distribute (loc, parser, p_name, mask, cclauses);
15648 block = c_begin_compound_stmt (true);
15649 ret = c_parser_omp_distribute (loc, parser, p_name, mask, cclauses);
15650 block = c_end_compound_stmt (loc, block, true);
15651 if (ret == NULL)
15652 return ret;
15653 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
15654 ret = make_node (OMP_TEAMS);
15655 TREE_TYPE (ret) = void_type_node;
15656 OMP_TEAMS_CLAUSES (ret) = clauses;
15657 OMP_TEAMS_BODY (ret) = block;
15658 OMP_TEAMS_COMBINED (ret) = 1;
15659 return add_stmt (ret);
15660 }
15661 }
15662 if (!flag_openmp) /* flag_openmp_simd */
15663 {
15664 c_parser_skip_to_pragma_eol (parser, false);
15665 return NULL_TREE;
15666 }
15667
15668 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
15669 if (cclauses)
15670 {
15671 omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
15672 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
15673 }
15674
15675 tree stmt = make_node (OMP_TEAMS);
15676 TREE_TYPE (stmt) = void_type_node;
15677 OMP_TEAMS_CLAUSES (stmt) = clauses;
15678 OMP_TEAMS_BODY (stmt) = c_parser_omp_structured_block (parser);
15679
15680 return add_stmt (stmt);
15681 }
15682
15683 /* OpenMP 4.0:
15684 # pragma omp target data target-data-clause[optseq] new-line
15685 structured-block */
15686
15687 #define OMP_TARGET_DATA_CLAUSE_MASK \
15688 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
15689 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
15690 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
15691 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR))
15692
15693 static tree
15694 c_parser_omp_target_data (location_t loc, c_parser *parser)
15695 {
15696 tree clauses
15697 = c_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
15698 "#pragma omp target data");
15699 int map_seen = 0;
15700 for (tree *pc = &clauses; *pc;)
15701 {
15702 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
15703 switch (OMP_CLAUSE_MAP_KIND (*pc))
15704 {
15705 case GOMP_MAP_TO:
15706 case GOMP_MAP_ALWAYS_TO:
15707 case GOMP_MAP_FROM:
15708 case GOMP_MAP_ALWAYS_FROM:
15709 case GOMP_MAP_TOFROM:
15710 case GOMP_MAP_ALWAYS_TOFROM:
15711 case GOMP_MAP_ALLOC:
15712 map_seen = 3;
15713 break;
15714 case GOMP_MAP_FIRSTPRIVATE_POINTER:
15715 case GOMP_MAP_ALWAYS_POINTER:
15716 break;
15717 default:
15718 map_seen |= 1;
15719 error_at (OMP_CLAUSE_LOCATION (*pc),
15720 "%<#pragma omp target data%> with map-type other "
15721 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
15722 "on %<map%> clause");
15723 *pc = OMP_CLAUSE_CHAIN (*pc);
15724 continue;
15725 }
15726 pc = &OMP_CLAUSE_CHAIN (*pc);
15727 }
15728
15729 if (map_seen != 3)
15730 {
15731 if (map_seen == 0)
15732 error_at (loc,
15733 "%<#pragma omp target data%> must contain at least "
15734 "one %<map%> clause");
15735 return NULL_TREE;
15736 }
15737
15738 tree stmt = make_node (OMP_TARGET_DATA);
15739 TREE_TYPE (stmt) = void_type_node;
15740 OMP_TARGET_DATA_CLAUSES (stmt) = clauses;
15741 keep_next_level ();
15742 tree block = c_begin_compound_stmt (true);
15743 add_stmt (c_parser_omp_structured_block (parser));
15744 OMP_TARGET_DATA_BODY (stmt) = c_end_compound_stmt (loc, block, true);
15745
15746 SET_EXPR_LOCATION (stmt, loc);
15747 return add_stmt (stmt);
15748 }
15749
15750 /* OpenMP 4.0:
15751 # pragma omp target update target-update-clause[optseq] new-line */
15752
15753 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
15754 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
15755 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
15756 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
15757 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
15758 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
15759 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
15760
15761 static bool
15762 c_parser_omp_target_update (location_t loc, c_parser *parser,
15763 enum pragma_context context)
15764 {
15765 if (context == pragma_stmt)
15766 {
15767 error_at (loc,
15768 "%<#pragma omp target update%> may only be "
15769 "used in compound statements");
15770 c_parser_skip_to_pragma_eol (parser, false);
15771 return false;
15772 }
15773
15774 tree clauses
15775 = c_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
15776 "#pragma omp target update");
15777 if (find_omp_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
15778 && find_omp_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
15779 {
15780 error_at (loc,
15781 "%<#pragma omp target update%> must contain at least one "
15782 "%<from%> or %<to%> clauses");
15783 return false;
15784 }
15785
15786 tree stmt = make_node (OMP_TARGET_UPDATE);
15787 TREE_TYPE (stmt) = void_type_node;
15788 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
15789 SET_EXPR_LOCATION (stmt, loc);
15790 add_stmt (stmt);
15791 return false;
15792 }
15793
15794 /* OpenMP 4.5:
15795 # pragma omp target enter data target-data-clause[optseq] new-line */
15796
15797 #define OMP_TARGET_ENTER_DATA_CLAUSE_MASK \
15798 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
15799 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
15800 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
15801 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
15802 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
15803
15804 static tree
15805 c_parser_omp_target_enter_data (location_t loc, c_parser *parser,
15806 enum pragma_context context)
15807 {
15808 bool data_seen = false;
15809 if (c_parser_next_token_is (parser, CPP_NAME))
15810 {
15811 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
15812 if (strcmp (p, "data") == 0)
15813 {
15814 c_parser_consume_token (parser);
15815 data_seen = true;
15816 }
15817 }
15818 if (!data_seen)
15819 {
15820 c_parser_error (parser, "expected %<data%>");
15821 c_parser_skip_to_pragma_eol (parser);
15822 return NULL_TREE;
15823 }
15824
15825 if (context == pragma_stmt)
15826 {
15827 error_at (loc,
15828 "%<#pragma omp target enter data%> may only be "
15829 "used in compound statements");
15830 c_parser_skip_to_pragma_eol (parser, false);
15831 return NULL_TREE;
15832 }
15833
15834 tree clauses
15835 = c_parser_omp_all_clauses (parser, OMP_TARGET_ENTER_DATA_CLAUSE_MASK,
15836 "#pragma omp target enter data");
15837 int map_seen = 0;
15838 for (tree *pc = &clauses; *pc;)
15839 {
15840 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
15841 switch (OMP_CLAUSE_MAP_KIND (*pc))
15842 {
15843 case GOMP_MAP_TO:
15844 case GOMP_MAP_ALWAYS_TO:
15845 case GOMP_MAP_ALLOC:
15846 map_seen = 3;
15847 break;
15848 case GOMP_MAP_FIRSTPRIVATE_POINTER:
15849 case GOMP_MAP_ALWAYS_POINTER:
15850 break;
15851 default:
15852 map_seen |= 1;
15853 error_at (OMP_CLAUSE_LOCATION (*pc),
15854 "%<#pragma omp target enter data%> with map-type other "
15855 "than %<to%> or %<alloc%> on %<map%> clause");
15856 *pc = OMP_CLAUSE_CHAIN (*pc);
15857 continue;
15858 }
15859 pc = &OMP_CLAUSE_CHAIN (*pc);
15860 }
15861
15862 if (map_seen != 3)
15863 {
15864 if (map_seen == 0)
15865 error_at (loc,
15866 "%<#pragma omp target enter data%> must contain at least "
15867 "one %<map%> clause");
15868 return NULL_TREE;
15869 }
15870
15871 tree stmt = make_node (OMP_TARGET_ENTER_DATA);
15872 TREE_TYPE (stmt) = void_type_node;
15873 OMP_TARGET_ENTER_DATA_CLAUSES (stmt) = clauses;
15874 SET_EXPR_LOCATION (stmt, loc);
15875 add_stmt (stmt);
15876 return stmt;
15877 }
15878
15879 /* OpenMP 4.5:
15880 # pragma omp target exit data target-data-clause[optseq] new-line */
15881
15882 #define OMP_TARGET_EXIT_DATA_CLAUSE_MASK \
15883 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
15884 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
15885 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
15886 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
15887 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
15888
15889 static tree
15890 c_parser_omp_target_exit_data (location_t loc, c_parser *parser,
15891 enum pragma_context context)
15892 {
15893 bool data_seen = false;
15894 if (c_parser_next_token_is (parser, CPP_NAME))
15895 {
15896 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
15897 if (strcmp (p, "data") == 0)
15898 {
15899 c_parser_consume_token (parser);
15900 data_seen = true;
15901 }
15902 }
15903 if (!data_seen)
15904 {
15905 c_parser_error (parser, "expected %<data%>");
15906 c_parser_skip_to_pragma_eol (parser);
15907 return NULL_TREE;
15908 }
15909
15910 if (context == pragma_stmt)
15911 {
15912 error_at (loc,
15913 "%<#pragma omp target exit data%> may only be "
15914 "used in compound statements");
15915 c_parser_skip_to_pragma_eol (parser, false);
15916 return NULL_TREE;
15917 }
15918
15919 tree clauses
15920 = c_parser_omp_all_clauses (parser, OMP_TARGET_EXIT_DATA_CLAUSE_MASK,
15921 "#pragma omp target exit data");
15922
15923 int map_seen = 0;
15924 for (tree *pc = &clauses; *pc;)
15925 {
15926 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
15927 switch (OMP_CLAUSE_MAP_KIND (*pc))
15928 {
15929 case GOMP_MAP_FROM:
15930 case GOMP_MAP_ALWAYS_FROM:
15931 case GOMP_MAP_RELEASE:
15932 case GOMP_MAP_DELETE:
15933 map_seen = 3;
15934 break;
15935 case GOMP_MAP_FIRSTPRIVATE_POINTER:
15936 case GOMP_MAP_ALWAYS_POINTER:
15937 break;
15938 default:
15939 map_seen |= 1;
15940 error_at (OMP_CLAUSE_LOCATION (*pc),
15941 "%<#pragma omp target exit data%> with map-type other "
15942 "than %<from%>, %<release> or %<delete%> on %<map%>"
15943 " clause");
15944 *pc = OMP_CLAUSE_CHAIN (*pc);
15945 continue;
15946 }
15947 pc = &OMP_CLAUSE_CHAIN (*pc);
15948 }
15949
15950 if (map_seen != 3)
15951 {
15952 if (map_seen == 0)
15953 error_at (loc,
15954 "%<#pragma omp target exit data%> must contain at least one "
15955 "%<map%> clause");
15956 return NULL_TREE;
15957 }
15958
15959 tree stmt = make_node (OMP_TARGET_EXIT_DATA);
15960 TREE_TYPE (stmt) = void_type_node;
15961 OMP_TARGET_EXIT_DATA_CLAUSES (stmt) = clauses;
15962 SET_EXPR_LOCATION (stmt, loc);
15963 add_stmt (stmt);
15964 return stmt;
15965 }
15966
15967 /* OpenMP 4.0:
15968 # pragma omp target target-clause[optseq] new-line
15969 structured-block */
15970
15971 #define OMP_TARGET_CLAUSE_MASK \
15972 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
15973 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
15974 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
15975 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
15976 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
15977 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
15978 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
15979 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULTMAP) \
15980 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR))
15981
15982 static bool
15983 c_parser_omp_target (c_parser *parser, enum pragma_context context)
15984 {
15985 location_t loc = c_parser_peek_token (parser)->location;
15986 c_parser_consume_pragma (parser);
15987 tree *pc = NULL, stmt, block;
15988
15989 if (context != pragma_stmt && context != pragma_compound)
15990 {
15991 c_parser_error (parser, "expected declaration specifiers");
15992 c_parser_skip_to_pragma_eol (parser);
15993 return false;
15994 }
15995
15996 if (c_parser_next_token_is (parser, CPP_NAME))
15997 {
15998 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
15999 enum tree_code ccode = ERROR_MARK;
16000
16001 if (strcmp (p, "teams") == 0)
16002 ccode = OMP_TEAMS;
16003 else if (strcmp (p, "parallel") == 0)
16004 ccode = OMP_PARALLEL;
16005 else if (strcmp (p, "simd") == 0)
16006 ccode = OMP_SIMD;
16007 if (ccode != ERROR_MARK)
16008 {
16009 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
16010 char p_name[sizeof ("#pragma omp target teams distribute "
16011 "parallel for simd")];
16012
16013 c_parser_consume_token (parser);
16014 strcpy (p_name, "#pragma omp target");
16015 if (!flag_openmp) /* flag_openmp_simd */
16016 {
16017 tree stmt;
16018 switch (ccode)
16019 {
16020 case OMP_TEAMS:
16021 stmt = c_parser_omp_teams (loc, parser, p_name,
16022 OMP_TARGET_CLAUSE_MASK,
16023 cclauses);
16024 break;
16025 case OMP_PARALLEL:
16026 stmt = c_parser_omp_parallel (loc, parser, p_name,
16027 OMP_TARGET_CLAUSE_MASK,
16028 cclauses);
16029 break;
16030 case OMP_SIMD:
16031 stmt = c_parser_omp_simd (loc, parser, p_name,
16032 OMP_TARGET_CLAUSE_MASK,
16033 cclauses);
16034 break;
16035 default:
16036 gcc_unreachable ();
16037 }
16038 return stmt != NULL_TREE;
16039 }
16040 keep_next_level ();
16041 tree block = c_begin_compound_stmt (true), ret;
16042 switch (ccode)
16043 {
16044 case OMP_TEAMS:
16045 ret = c_parser_omp_teams (loc, parser, p_name,
16046 OMP_TARGET_CLAUSE_MASK, cclauses);
16047 break;
16048 case OMP_PARALLEL:
16049 ret = c_parser_omp_parallel (loc, parser, p_name,
16050 OMP_TARGET_CLAUSE_MASK, cclauses);
16051 break;
16052 case OMP_SIMD:
16053 ret = c_parser_omp_simd (loc, parser, p_name,
16054 OMP_TARGET_CLAUSE_MASK, cclauses);
16055 break;
16056 default:
16057 gcc_unreachable ();
16058 }
16059 block = c_end_compound_stmt (loc, block, true);
16060 if (ret == NULL_TREE)
16061 return false;
16062 if (ccode == OMP_TEAMS)
16063 {
16064 /* For combined target teams, ensure the num_teams and
16065 thread_limit clause expressions are evaluated on the host,
16066 before entering the target construct. */
16067 tree c;
16068 for (c = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
16069 c; c = OMP_CLAUSE_CHAIN (c))
16070 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
16071 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
16072 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
16073 {
16074 tree expr = OMP_CLAUSE_OPERAND (c, 0);
16075 tree tmp = create_tmp_var_raw (TREE_TYPE (expr));
16076 expr = build4 (TARGET_EXPR, TREE_TYPE (expr), tmp,
16077 expr, NULL_TREE, NULL_TREE);
16078 add_stmt (expr);
16079 OMP_CLAUSE_OPERAND (c, 0) = expr;
16080 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
16081 OMP_CLAUSE_FIRSTPRIVATE);
16082 OMP_CLAUSE_DECL (tc) = tmp;
16083 OMP_CLAUSE_CHAIN (tc)
16084 = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
16085 cclauses[C_OMP_CLAUSE_SPLIT_TARGET] = tc;
16086 }
16087 }
16088 tree stmt = make_node (OMP_TARGET);
16089 TREE_TYPE (stmt) = void_type_node;
16090 OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
16091 OMP_TARGET_BODY (stmt) = block;
16092 OMP_TARGET_COMBINED (stmt) = 1;
16093 add_stmt (stmt);
16094 pc = &OMP_TARGET_CLAUSES (stmt);
16095 goto check_clauses;
16096 }
16097 else if (!flag_openmp) /* flag_openmp_simd */
16098 {
16099 c_parser_skip_to_pragma_eol (parser, false);
16100 return false;
16101 }
16102 else if (strcmp (p, "data") == 0)
16103 {
16104 c_parser_consume_token (parser);
16105 c_parser_omp_target_data (loc, parser);
16106 return true;
16107 }
16108 else if (strcmp (p, "enter") == 0)
16109 {
16110 c_parser_consume_token (parser);
16111 c_parser_omp_target_enter_data (loc, parser, context);
16112 return false;
16113 }
16114 else if (strcmp (p, "exit") == 0)
16115 {
16116 c_parser_consume_token (parser);
16117 c_parser_omp_target_exit_data (loc, parser, context);
16118 return false;
16119 }
16120 else if (strcmp (p, "update") == 0)
16121 {
16122 c_parser_consume_token (parser);
16123 return c_parser_omp_target_update (loc, parser, context);
16124 }
16125 }
16126
16127 stmt = make_node (OMP_TARGET);
16128 TREE_TYPE (stmt) = void_type_node;
16129
16130 OMP_TARGET_CLAUSES (stmt)
16131 = c_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
16132 "#pragma omp target");
16133 pc = &OMP_TARGET_CLAUSES (stmt);
16134 keep_next_level ();
16135 block = c_begin_compound_stmt (true);
16136 add_stmt (c_parser_omp_structured_block (parser));
16137 OMP_TARGET_BODY (stmt) = c_end_compound_stmt (loc, block, true);
16138
16139 SET_EXPR_LOCATION (stmt, loc);
16140 add_stmt (stmt);
16141
16142 check_clauses:
16143 while (*pc)
16144 {
16145 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
16146 switch (OMP_CLAUSE_MAP_KIND (*pc))
16147 {
16148 case GOMP_MAP_TO:
16149 case GOMP_MAP_ALWAYS_TO:
16150 case GOMP_MAP_FROM:
16151 case GOMP_MAP_ALWAYS_FROM:
16152 case GOMP_MAP_TOFROM:
16153 case GOMP_MAP_ALWAYS_TOFROM:
16154 case GOMP_MAP_ALLOC:
16155 case GOMP_MAP_FIRSTPRIVATE_POINTER:
16156 case GOMP_MAP_ALWAYS_POINTER:
16157 break;
16158 default:
16159 error_at (OMP_CLAUSE_LOCATION (*pc),
16160 "%<#pragma omp target%> with map-type other "
16161 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
16162 "on %<map%> clause");
16163 *pc = OMP_CLAUSE_CHAIN (*pc);
16164 continue;
16165 }
16166 pc = &OMP_CLAUSE_CHAIN (*pc);
16167 }
16168 return true;
16169 }
16170
16171 /* OpenMP 4.0:
16172 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
16173
16174 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
16175 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
16176 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
16177 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
16178 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
16179 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
16180 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
16181
16182 static void
16183 c_parser_omp_declare_simd (c_parser *parser, enum pragma_context context)
16184 {
16185 vec<c_token> clauses = vNULL;
16186 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
16187 {
16188 c_token *token = c_parser_peek_token (parser);
16189 if (token->type == CPP_EOF)
16190 {
16191 c_parser_skip_to_pragma_eol (parser);
16192 clauses.release ();
16193 return;
16194 }
16195 clauses.safe_push (*token);
16196 c_parser_consume_token (parser);
16197 }
16198 clauses.safe_push (*c_parser_peek_token (parser));
16199 c_parser_skip_to_pragma_eol (parser);
16200
16201 while (c_parser_next_token_is (parser, CPP_PRAGMA))
16202 {
16203 if (c_parser_peek_token (parser)->pragma_kind
16204 != PRAGMA_OMP_DECLARE_REDUCTION
16205 || c_parser_peek_2nd_token (parser)->type != CPP_NAME
16206 || strcmp (IDENTIFIER_POINTER
16207 (c_parser_peek_2nd_token (parser)->value),
16208 "simd") != 0)
16209 {
16210 c_parser_error (parser,
16211 "%<#pragma omp declare simd%> must be followed by "
16212 "function declaration or definition or another "
16213 "%<#pragma omp declare simd%>");
16214 clauses.release ();
16215 return;
16216 }
16217 c_parser_consume_pragma (parser);
16218 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
16219 {
16220 c_token *token = c_parser_peek_token (parser);
16221 if (token->type == CPP_EOF)
16222 {
16223 c_parser_skip_to_pragma_eol (parser);
16224 clauses.release ();
16225 return;
16226 }
16227 clauses.safe_push (*token);
16228 c_parser_consume_token (parser);
16229 }
16230 clauses.safe_push (*c_parser_peek_token (parser));
16231 c_parser_skip_to_pragma_eol (parser);
16232 }
16233
16234 /* Make sure nothing tries to read past the end of the tokens. */
16235 c_token eof_token;
16236 memset (&eof_token, 0, sizeof (eof_token));
16237 eof_token.type = CPP_EOF;
16238 clauses.safe_push (eof_token);
16239 clauses.safe_push (eof_token);
16240
16241 switch (context)
16242 {
16243 case pragma_external:
16244 if (c_parser_next_token_is (parser, CPP_KEYWORD)
16245 && c_parser_peek_token (parser)->keyword == RID_EXTENSION)
16246 {
16247 int ext = disable_extension_diagnostics ();
16248 do
16249 c_parser_consume_token (parser);
16250 while (c_parser_next_token_is (parser, CPP_KEYWORD)
16251 && c_parser_peek_token (parser)->keyword == RID_EXTENSION);
16252 c_parser_declaration_or_fndef (parser, true, true, true, false, true,
16253 NULL, clauses);
16254 restore_extension_diagnostics (ext);
16255 }
16256 else
16257 c_parser_declaration_or_fndef (parser, true, true, true, false, true,
16258 NULL, clauses);
16259 break;
16260 case pragma_struct:
16261 case pragma_param:
16262 c_parser_error (parser, "%<#pragma omp declare simd%> must be followed by "
16263 "function declaration or definition");
16264 break;
16265 case pragma_compound:
16266 case pragma_stmt:
16267 if (c_parser_next_token_is (parser, CPP_KEYWORD)
16268 && c_parser_peek_token (parser)->keyword == RID_EXTENSION)
16269 {
16270 int ext = disable_extension_diagnostics ();
16271 do
16272 c_parser_consume_token (parser);
16273 while (c_parser_next_token_is (parser, CPP_KEYWORD)
16274 && c_parser_peek_token (parser)->keyword == RID_EXTENSION);
16275 if (c_parser_next_tokens_start_declaration (parser))
16276 {
16277 c_parser_declaration_or_fndef (parser, true, true, true, true,
16278 true, NULL, clauses);
16279 restore_extension_diagnostics (ext);
16280 break;
16281 }
16282 restore_extension_diagnostics (ext);
16283 }
16284 else if (c_parser_next_tokens_start_declaration (parser))
16285 {
16286 c_parser_declaration_or_fndef (parser, true, true, true, true, true,
16287 NULL, clauses);
16288 break;
16289 }
16290 c_parser_error (parser, "%<#pragma omp declare simd%> must be followed by "
16291 "function declaration or definition");
16292 break;
16293 default:
16294 gcc_unreachable ();
16295 }
16296 clauses.release ();
16297 }
16298
16299 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
16300 and put that into "omp declare simd" attribute. */
16301
16302 static void
16303 c_finish_omp_declare_simd (c_parser *parser, tree fndecl, tree parms,
16304 vec<c_token> clauses)
16305 {
16306 if (flag_cilkplus
16307 && (clauses.exists ()
16308 || lookup_attribute ("simd", DECL_ATTRIBUTES (fndecl)))
16309 && !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
16310 {
16311 error ("%<#pragma omp declare simd%> or %<simd%> attribute cannot be "
16312 "used in the same function marked as a Cilk Plus SIMD-enabled "
16313 "function");
16314 vec_free (parser->cilk_simd_fn_tokens);
16315 return;
16316 }
16317
16318 /* Normally first token is CPP_NAME "simd". CPP_EOF there indicates
16319 error has been reported and CPP_PRAGMA that c_finish_omp_declare_simd
16320 has already processed the tokens. */
16321 if (clauses.exists () && clauses[0].type == CPP_EOF)
16322 return;
16323 if (fndecl == NULL_TREE || TREE_CODE (fndecl) != FUNCTION_DECL)
16324 {
16325 error ("%<#pragma omp declare simd%> not immediately followed by "
16326 "a function declaration or definition");
16327 clauses[0].type = CPP_EOF;
16328 return;
16329 }
16330 if (clauses.exists () && clauses[0].type != CPP_NAME)
16331 {
16332 error_at (DECL_SOURCE_LOCATION (fndecl),
16333 "%<#pragma omp declare simd%> not immediately followed by "
16334 "a single function declaration or definition");
16335 clauses[0].type = CPP_EOF;
16336 return;
16337 }
16338
16339 if (parms == NULL_TREE)
16340 parms = DECL_ARGUMENTS (fndecl);
16341
16342 unsigned int tokens_avail = parser->tokens_avail;
16343 gcc_assert (parser->tokens == &parser->tokens_buf[0]);
16344 bool is_cilkplus_cilk_simd_fn = false;
16345
16346 if (flag_cilkplus && !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
16347 {
16348 parser->tokens = parser->cilk_simd_fn_tokens->address ();
16349 parser->tokens_avail = vec_safe_length (parser->cilk_simd_fn_tokens);
16350 is_cilkplus_cilk_simd_fn = true;
16351
16352 if (lookup_attribute ("simd", DECL_ATTRIBUTES (fndecl)) != NULL)
16353 {
16354 error_at (DECL_SOURCE_LOCATION (fndecl),
16355 "%<__simd__%> attribute cannot be used in the same "
16356 "function marked as a Cilk Plus SIMD-enabled function");
16357 vec_free (parser->cilk_simd_fn_tokens);
16358 return;
16359 }
16360
16361 }
16362 else
16363 {
16364 parser->tokens = clauses.address ();
16365 parser->tokens_avail = clauses.length ();
16366 }
16367
16368 /* c_parser_omp_declare_simd pushed 2 extra CPP_EOF tokens at the end. */
16369 while (parser->tokens_avail > 3)
16370 {
16371 c_token *token = c_parser_peek_token (parser);
16372 if (!is_cilkplus_cilk_simd_fn)
16373 gcc_assert (token->type == CPP_NAME
16374 && strcmp (IDENTIFIER_POINTER (token->value), "simd") == 0);
16375 else
16376 gcc_assert (token->type == CPP_NAME
16377 && is_cilkplus_vector_p (token->value));
16378 c_parser_consume_token (parser);
16379 parser->in_pragma = true;
16380
16381 tree c = NULL_TREE;
16382 if (is_cilkplus_cilk_simd_fn)
16383 c = c_parser_omp_all_clauses (parser, CILK_SIMD_FN_CLAUSE_MASK,
16384 "SIMD-enabled functions attribute");
16385 else
16386 c = c_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
16387 "#pragma omp declare simd");
16388 c = c_omp_declare_simd_clauses_to_numbers (parms, c);
16389 if (c != NULL_TREE)
16390 c = tree_cons (NULL_TREE, c, NULL_TREE);
16391 if (is_cilkplus_cilk_simd_fn)
16392 {
16393 tree k = build_tree_list (get_identifier ("cilk simd function"),
16394 NULL_TREE);
16395 TREE_CHAIN (k) = DECL_ATTRIBUTES (fndecl);
16396 DECL_ATTRIBUTES (fndecl) = k;
16397 }
16398 c = build_tree_list (get_identifier ("omp declare simd"), c);
16399 TREE_CHAIN (c) = DECL_ATTRIBUTES (fndecl);
16400 DECL_ATTRIBUTES (fndecl) = c;
16401 }
16402
16403 parser->tokens = &parser->tokens_buf[0];
16404 parser->tokens_avail = tokens_avail;
16405 if (clauses.exists ())
16406 clauses[0].type = CPP_PRAGMA;
16407
16408 if (!vec_safe_is_empty (parser->cilk_simd_fn_tokens))
16409 vec_free (parser->cilk_simd_fn_tokens);
16410 }
16411
16412
16413 /* OpenMP 4.0:
16414 # pragma omp declare target new-line
16415 declarations and definitions
16416 # pragma omp end declare target new-line
16417
16418 OpenMP 4.5:
16419 # pragma omp declare target ( extended-list ) new-line
16420
16421 # pragma omp declare target declare-target-clauses[seq] new-line */
16422
16423 #define OMP_DECLARE_TARGET_CLAUSE_MASK \
16424 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
16425 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK))
16426
16427 static void
16428 c_parser_omp_declare_target (c_parser *parser)
16429 {
16430 location_t loc = c_parser_peek_token (parser)->location;
16431 tree clauses = NULL_TREE;
16432 if (c_parser_next_token_is (parser, CPP_NAME))
16433 clauses = c_parser_omp_all_clauses (parser, OMP_DECLARE_TARGET_CLAUSE_MASK,
16434 "#pragma omp declare target");
16435 else if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
16436 {
16437 clauses = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_TO_DECLARE,
16438 clauses);
16439 clauses = c_finish_omp_clauses (clauses, true);
16440 c_parser_skip_to_pragma_eol (parser);
16441 }
16442 else
16443 {
16444 c_parser_skip_to_pragma_eol (parser);
16445 current_omp_declare_target_attribute++;
16446 return;
16447 }
16448 if (current_omp_declare_target_attribute)
16449 error_at (loc, "%<#pragma omp declare target%> with clauses in between "
16450 "%<#pragma omp declare target%> without clauses and "
16451 "%<#pragma omp end declare target%>");
16452 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
16453 {
16454 tree t = OMP_CLAUSE_DECL (c), id;
16455 tree at1 = lookup_attribute ("omp declare target", DECL_ATTRIBUTES (t));
16456 tree at2 = lookup_attribute ("omp declare target link",
16457 DECL_ATTRIBUTES (t));
16458 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINK)
16459 {
16460 id = get_identifier ("omp declare target link");
16461 std::swap (at1, at2);
16462 }
16463 else
16464 id = get_identifier ("omp declare target");
16465 if (at2)
16466 {
16467 error_at (OMP_CLAUSE_LOCATION (c),
16468 "%qD specified both in declare target %<link%> and %<to%>"
16469 " clauses", t);
16470 continue;
16471 }
16472 if (!at1)
16473 {
16474 symtab_node *node = symtab_node::get (t);
16475 DECL_ATTRIBUTES (t) = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
16476 if (node != NULL)
16477 {
16478 node->offloadable = 1;
16479 if (ENABLE_OFFLOADING)
16480 {
16481 g->have_offload = true;
16482 if (is_a <varpool_node *> (node))
16483 {
16484 vec_safe_push (offload_vars, t);
16485 node->force_output = 1;
16486 }
16487 }
16488 }
16489 }
16490 }
16491 }
16492
16493 static void
16494 c_parser_omp_end_declare_target (c_parser *parser)
16495 {
16496 location_t loc = c_parser_peek_token (parser)->location;
16497 c_parser_consume_pragma (parser);
16498 if (c_parser_next_token_is (parser, CPP_NAME)
16499 && strcmp (IDENTIFIER_POINTER (c_parser_peek_token (parser)->value),
16500 "declare") == 0)
16501 {
16502 c_parser_consume_token (parser);
16503 if (c_parser_next_token_is (parser, CPP_NAME)
16504 && strcmp (IDENTIFIER_POINTER (c_parser_peek_token (parser)->value),
16505 "target") == 0)
16506 c_parser_consume_token (parser);
16507 else
16508 {
16509 c_parser_error (parser, "expected %<target%>");
16510 c_parser_skip_to_pragma_eol (parser);
16511 return;
16512 }
16513 }
16514 else
16515 {
16516 c_parser_error (parser, "expected %<declare%>");
16517 c_parser_skip_to_pragma_eol (parser);
16518 return;
16519 }
16520 c_parser_skip_to_pragma_eol (parser);
16521 if (!current_omp_declare_target_attribute)
16522 error_at (loc, "%<#pragma omp end declare target%> without corresponding "
16523 "%<#pragma omp declare target%>");
16524 else
16525 current_omp_declare_target_attribute--;
16526 }
16527
16528
16529 /* OpenMP 4.0
16530 #pragma omp declare reduction (reduction-id : typename-list : expression) \
16531 initializer-clause[opt] new-line
16532
16533 initializer-clause:
16534 initializer (omp_priv = initializer)
16535 initializer (function-name (argument-list)) */
16536
16537 static void
16538 c_parser_omp_declare_reduction (c_parser *parser, enum pragma_context context)
16539 {
16540 unsigned int tokens_avail = 0, i;
16541 vec<tree> types = vNULL;
16542 vec<c_token> clauses = vNULL;
16543 enum tree_code reduc_code = ERROR_MARK;
16544 tree reduc_id = NULL_TREE;
16545 tree type;
16546 location_t rloc = c_parser_peek_token (parser)->location;
16547
16548 if (context == pragma_struct || context == pragma_param)
16549 {
16550 error ("%<#pragma omp declare reduction%> not at file or block scope");
16551 goto fail;
16552 }
16553
16554 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
16555 goto fail;
16556
16557 switch (c_parser_peek_token (parser)->type)
16558 {
16559 case CPP_PLUS:
16560 reduc_code = PLUS_EXPR;
16561 break;
16562 case CPP_MULT:
16563 reduc_code = MULT_EXPR;
16564 break;
16565 case CPP_MINUS:
16566 reduc_code = MINUS_EXPR;
16567 break;
16568 case CPP_AND:
16569 reduc_code = BIT_AND_EXPR;
16570 break;
16571 case CPP_XOR:
16572 reduc_code = BIT_XOR_EXPR;
16573 break;
16574 case CPP_OR:
16575 reduc_code = BIT_IOR_EXPR;
16576 break;
16577 case CPP_AND_AND:
16578 reduc_code = TRUTH_ANDIF_EXPR;
16579 break;
16580 case CPP_OR_OR:
16581 reduc_code = TRUTH_ORIF_EXPR;
16582 break;
16583 case CPP_NAME:
16584 const char *p;
16585 p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
16586 if (strcmp (p, "min") == 0)
16587 {
16588 reduc_code = MIN_EXPR;
16589 break;
16590 }
16591 if (strcmp (p, "max") == 0)
16592 {
16593 reduc_code = MAX_EXPR;
16594 break;
16595 }
16596 reduc_id = c_parser_peek_token (parser)->value;
16597 break;
16598 default:
16599 c_parser_error (parser,
16600 "expected %<+%>, %<*%>, %<-%>, %<&%>, "
16601 "%<^%>, %<|%>, %<&&%>, %<||%>, %<min%> or identifier");
16602 goto fail;
16603 }
16604
16605 tree orig_reduc_id, reduc_decl;
16606 orig_reduc_id = reduc_id;
16607 reduc_id = c_omp_reduction_id (reduc_code, reduc_id);
16608 reduc_decl = c_omp_reduction_decl (reduc_id);
16609 c_parser_consume_token (parser);
16610
16611 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
16612 goto fail;
16613
16614 while (true)
16615 {
16616 location_t loc = c_parser_peek_token (parser)->location;
16617 struct c_type_name *ctype = c_parser_type_name (parser);
16618 if (ctype != NULL)
16619 {
16620 type = groktypename (ctype, NULL, NULL);
16621 if (type == error_mark_node)
16622 ;
16623 else if ((INTEGRAL_TYPE_P (type)
16624 || TREE_CODE (type) == REAL_TYPE
16625 || TREE_CODE (type) == COMPLEX_TYPE)
16626 && orig_reduc_id == NULL_TREE)
16627 error_at (loc, "predeclared arithmetic type in "
16628 "%<#pragma omp declare reduction%>");
16629 else if (TREE_CODE (type) == FUNCTION_TYPE
16630 || TREE_CODE (type) == ARRAY_TYPE)
16631 error_at (loc, "function or array type in "
16632 "%<#pragma omp declare reduction%>");
16633 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
16634 error_at (loc, "const, volatile or restrict qualified type in "
16635 "%<#pragma omp declare reduction%>");
16636 else
16637 {
16638 tree t;
16639 for (t = DECL_INITIAL (reduc_decl); t; t = TREE_CHAIN (t))
16640 if (comptypes (TREE_PURPOSE (t), type))
16641 {
16642 error_at (loc, "redeclaration of %qs "
16643 "%<#pragma omp declare reduction%> for "
16644 "type %qT",
16645 IDENTIFIER_POINTER (reduc_id)
16646 + sizeof ("omp declare reduction ") - 1,
16647 type);
16648 location_t ploc
16649 = DECL_SOURCE_LOCATION (TREE_VEC_ELT (TREE_VALUE (t),
16650 0));
16651 error_at (ploc, "previous %<#pragma omp declare "
16652 "reduction%>");
16653 break;
16654 }
16655 if (t == NULL_TREE)
16656 types.safe_push (type);
16657 }
16658 if (c_parser_next_token_is (parser, CPP_COMMA))
16659 c_parser_consume_token (parser);
16660 else
16661 break;
16662 }
16663 else
16664 break;
16665 }
16666
16667 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>")
16668 || types.is_empty ())
16669 {
16670 fail:
16671 clauses.release ();
16672 types.release ();
16673 while (true)
16674 {
16675 c_token *token = c_parser_peek_token (parser);
16676 if (token->type == CPP_EOF || token->type == CPP_PRAGMA_EOL)
16677 break;
16678 c_parser_consume_token (parser);
16679 }
16680 c_parser_skip_to_pragma_eol (parser);
16681 return;
16682 }
16683
16684 if (types.length () > 1)
16685 {
16686 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
16687 {
16688 c_token *token = c_parser_peek_token (parser);
16689 if (token->type == CPP_EOF)
16690 goto fail;
16691 clauses.safe_push (*token);
16692 c_parser_consume_token (parser);
16693 }
16694 clauses.safe_push (*c_parser_peek_token (parser));
16695 c_parser_skip_to_pragma_eol (parser);
16696
16697 /* Make sure nothing tries to read past the end of the tokens. */
16698 c_token eof_token;
16699 memset (&eof_token, 0, sizeof (eof_token));
16700 eof_token.type = CPP_EOF;
16701 clauses.safe_push (eof_token);
16702 clauses.safe_push (eof_token);
16703 }
16704
16705 int errs = errorcount;
16706 FOR_EACH_VEC_ELT (types, i, type)
16707 {
16708 tokens_avail = parser->tokens_avail;
16709 gcc_assert (parser->tokens == &parser->tokens_buf[0]);
16710 if (!clauses.is_empty ())
16711 {
16712 parser->tokens = clauses.address ();
16713 parser->tokens_avail = clauses.length ();
16714 parser->in_pragma = true;
16715 }
16716
16717 bool nested = current_function_decl != NULL_TREE;
16718 if (nested)
16719 c_push_function_context ();
16720 tree fndecl = build_decl (BUILTINS_LOCATION, FUNCTION_DECL,
16721 reduc_id, default_function_type);
16722 current_function_decl = fndecl;
16723 allocate_struct_function (fndecl, true);
16724 push_scope ();
16725 tree stmt = push_stmt_list ();
16726 /* Intentionally BUILTINS_LOCATION, so that -Wshadow doesn't
16727 warn about these. */
16728 tree omp_out = build_decl (BUILTINS_LOCATION, VAR_DECL,
16729 get_identifier ("omp_out"), type);
16730 DECL_ARTIFICIAL (omp_out) = 1;
16731 DECL_CONTEXT (omp_out) = fndecl;
16732 pushdecl (omp_out);
16733 tree omp_in = build_decl (BUILTINS_LOCATION, VAR_DECL,
16734 get_identifier ("omp_in"), type);
16735 DECL_ARTIFICIAL (omp_in) = 1;
16736 DECL_CONTEXT (omp_in) = fndecl;
16737 pushdecl (omp_in);
16738 struct c_expr combiner = c_parser_expression (parser);
16739 struct c_expr initializer;
16740 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE;
16741 bool bad = false;
16742 initializer.value = error_mark_node;
16743 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
16744 bad = true;
16745 else if (c_parser_next_token_is (parser, CPP_NAME)
16746 && strcmp (IDENTIFIER_POINTER
16747 (c_parser_peek_token (parser)->value),
16748 "initializer") == 0)
16749 {
16750 c_parser_consume_token (parser);
16751 pop_scope ();
16752 push_scope ();
16753 omp_priv = build_decl (BUILTINS_LOCATION, VAR_DECL,
16754 get_identifier ("omp_priv"), type);
16755 DECL_ARTIFICIAL (omp_priv) = 1;
16756 DECL_INITIAL (omp_priv) = error_mark_node;
16757 DECL_CONTEXT (omp_priv) = fndecl;
16758 pushdecl (omp_priv);
16759 omp_orig = build_decl (BUILTINS_LOCATION, VAR_DECL,
16760 get_identifier ("omp_orig"), type);
16761 DECL_ARTIFICIAL (omp_orig) = 1;
16762 DECL_CONTEXT (omp_orig) = fndecl;
16763 pushdecl (omp_orig);
16764 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
16765 bad = true;
16766 else if (!c_parser_next_token_is (parser, CPP_NAME))
16767 {
16768 c_parser_error (parser, "expected %<omp_priv%> or "
16769 "function-name");
16770 bad = true;
16771 }
16772 else if (strcmp (IDENTIFIER_POINTER
16773 (c_parser_peek_token (parser)->value),
16774 "omp_priv") != 0)
16775 {
16776 if (c_parser_peek_2nd_token (parser)->type != CPP_OPEN_PAREN
16777 || c_parser_peek_token (parser)->id_kind != C_ID_ID)
16778 {
16779 c_parser_error (parser, "expected function-name %<(%>");
16780 bad = true;
16781 }
16782 else
16783 initializer = c_parser_postfix_expression (parser);
16784 if (initializer.value
16785 && TREE_CODE (initializer.value) == CALL_EXPR)
16786 {
16787 int j;
16788 tree c = initializer.value;
16789 for (j = 0; j < call_expr_nargs (c); j++)
16790 {
16791 tree a = CALL_EXPR_ARG (c, j);
16792 STRIP_NOPS (a);
16793 if (TREE_CODE (a) == ADDR_EXPR
16794 && TREE_OPERAND (a, 0) == omp_priv)
16795 break;
16796 }
16797 if (j == call_expr_nargs (c))
16798 error ("one of the initializer call arguments should be "
16799 "%<&omp_priv%>");
16800 }
16801 }
16802 else
16803 {
16804 c_parser_consume_token (parser);
16805 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
16806 bad = true;
16807 else
16808 {
16809 tree st = push_stmt_list ();
16810 start_init (omp_priv, NULL_TREE, 0);
16811 location_t loc = c_parser_peek_token (parser)->location;
16812 struct c_expr init = c_parser_initializer (parser);
16813 finish_init ();
16814 finish_decl (omp_priv, loc, init.value,
16815 init.original_type, NULL_TREE);
16816 pop_stmt_list (st);
16817 }
16818 }
16819 if (!bad
16820 && !c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
16821 bad = true;
16822 }
16823
16824 if (!bad)
16825 {
16826 c_parser_skip_to_pragma_eol (parser);
16827
16828 tree t = tree_cons (type, make_tree_vec (omp_priv ? 6 : 3),
16829 DECL_INITIAL (reduc_decl));
16830 DECL_INITIAL (reduc_decl) = t;
16831 DECL_SOURCE_LOCATION (omp_out) = rloc;
16832 TREE_VEC_ELT (TREE_VALUE (t), 0) = omp_out;
16833 TREE_VEC_ELT (TREE_VALUE (t), 1) = omp_in;
16834 TREE_VEC_ELT (TREE_VALUE (t), 2) = combiner.value;
16835 walk_tree (&combiner.value, c_check_omp_declare_reduction_r,
16836 &TREE_VEC_ELT (TREE_VALUE (t), 0), NULL);
16837 if (omp_priv)
16838 {
16839 DECL_SOURCE_LOCATION (omp_priv) = rloc;
16840 TREE_VEC_ELT (TREE_VALUE (t), 3) = omp_priv;
16841 TREE_VEC_ELT (TREE_VALUE (t), 4) = omp_orig;
16842 TREE_VEC_ELT (TREE_VALUE (t), 5) = initializer.value;
16843 walk_tree (&initializer.value, c_check_omp_declare_reduction_r,
16844 &TREE_VEC_ELT (TREE_VALUE (t), 3), NULL);
16845 walk_tree (&DECL_INITIAL (omp_priv),
16846 c_check_omp_declare_reduction_r,
16847 &TREE_VEC_ELT (TREE_VALUE (t), 3), NULL);
16848 }
16849 }
16850
16851 pop_stmt_list (stmt);
16852 pop_scope ();
16853 if (cfun->language != NULL)
16854 {
16855 ggc_free (cfun->language);
16856 cfun->language = NULL;
16857 }
16858 set_cfun (NULL);
16859 current_function_decl = NULL_TREE;
16860 if (nested)
16861 c_pop_function_context ();
16862
16863 if (!clauses.is_empty ())
16864 {
16865 parser->tokens = &parser->tokens_buf[0];
16866 parser->tokens_avail = tokens_avail;
16867 }
16868 if (bad)
16869 goto fail;
16870 if (errs != errorcount)
16871 break;
16872 }
16873
16874 clauses.release ();
16875 types.release ();
16876 }
16877
16878
16879 /* OpenMP 4.0
16880 #pragma omp declare simd declare-simd-clauses[optseq] new-line
16881 #pragma omp declare reduction (reduction-id : typename-list : expression) \
16882 initializer-clause[opt] new-line
16883 #pragma omp declare target new-line */
16884
16885 static void
16886 c_parser_omp_declare (c_parser *parser, enum pragma_context context)
16887 {
16888 c_parser_consume_pragma (parser);
16889 if (c_parser_next_token_is (parser, CPP_NAME))
16890 {
16891 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
16892 if (strcmp (p, "simd") == 0)
16893 {
16894 /* c_parser_consume_token (parser); done in
16895 c_parser_omp_declare_simd. */
16896 c_parser_omp_declare_simd (parser, context);
16897 return;
16898 }
16899 if (strcmp (p, "reduction") == 0)
16900 {
16901 c_parser_consume_token (parser);
16902 c_parser_omp_declare_reduction (parser, context);
16903 return;
16904 }
16905 if (!flag_openmp) /* flag_openmp_simd */
16906 {
16907 c_parser_skip_to_pragma_eol (parser, false);
16908 return;
16909 }
16910 if (strcmp (p, "target") == 0)
16911 {
16912 c_parser_consume_token (parser);
16913 c_parser_omp_declare_target (parser);
16914 return;
16915 }
16916 }
16917
16918 c_parser_error (parser, "expected %<simd%> or %<reduction%> "
16919 "or %<target%>");
16920 c_parser_skip_to_pragma_eol (parser);
16921 }
16922
16923 /* OpenMP 4.5:
16924 #pragma omp taskloop taskloop-clause[optseq] new-line
16925 for-loop
16926
16927 #pragma omp taskloop simd taskloop-simd-clause[optseq] new-line
16928 for-loop */
16929
16930 #define OMP_TASKLOOP_CLAUSE_MASK \
16931 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
16932 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
16933 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
16934 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
16935 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
16936 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_GRAINSIZE) \
16937 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TASKS) \
16938 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
16939 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
16940 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
16941 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
16942 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
16943 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP) \
16944 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
16945
16946 static tree
16947 c_parser_omp_taskloop (location_t loc, c_parser *parser,
16948 char *p_name, omp_clause_mask mask, tree *cclauses)
16949 {
16950 tree clauses, block, ret;
16951
16952 strcat (p_name, " taskloop");
16953 mask |= OMP_TASKLOOP_CLAUSE_MASK;
16954
16955 if (c_parser_next_token_is (parser, CPP_NAME))
16956 {
16957 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
16958
16959 if (strcmp (p, "simd") == 0)
16960 {
16961 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
16962 if (cclauses == NULL)
16963 cclauses = cclauses_buf;
16964 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION);
16965 c_parser_consume_token (parser);
16966 if (!flag_openmp) /* flag_openmp_simd */
16967 return c_parser_omp_simd (loc, parser, p_name, mask, cclauses);
16968 block = c_begin_compound_stmt (true);
16969 ret = c_parser_omp_simd (loc, parser, p_name, mask, cclauses);
16970 block = c_end_compound_stmt (loc, block, true);
16971 if (ret == NULL)
16972 return ret;
16973 ret = make_node (OMP_TASKLOOP);
16974 TREE_TYPE (ret) = void_type_node;
16975 OMP_FOR_BODY (ret) = block;
16976 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
16977 SET_EXPR_LOCATION (ret, loc);
16978 add_stmt (ret);
16979 return ret;
16980 }
16981 }
16982 if (!flag_openmp) /* flag_openmp_simd */
16983 {
16984 c_parser_skip_to_pragma_eol (parser, false);
16985 return NULL_TREE;
16986 }
16987
16988 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
16989 if (cclauses)
16990 {
16991 omp_split_clauses (loc, OMP_TASKLOOP, mask, clauses, cclauses);
16992 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
16993 }
16994
16995 block = c_begin_compound_stmt (true);
16996 ret = c_parser_omp_for_loop (loc, parser, OMP_TASKLOOP, clauses, NULL);
16997 block = c_end_compound_stmt (loc, block, true);
16998 add_stmt (block);
16999
17000 return ret;
17001 }
17002
17003 /* Main entry point to parsing most OpenMP pragmas. */
17004
17005 static void
17006 c_parser_omp_construct (c_parser *parser)
17007 {
17008 enum pragma_kind p_kind;
17009 location_t loc;
17010 tree stmt;
17011 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
17012 omp_clause_mask mask (0);
17013
17014 loc = c_parser_peek_token (parser)->location;
17015 p_kind = c_parser_peek_token (parser)->pragma_kind;
17016 c_parser_consume_pragma (parser);
17017
17018 switch (p_kind)
17019 {
17020 case PRAGMA_OACC_ATOMIC:
17021 c_parser_omp_atomic (loc, parser);
17022 return;
17023 case PRAGMA_OACC_CACHE:
17024 strcpy (p_name, "#pragma acc");
17025 stmt = c_parser_oacc_cache (loc, parser);
17026 break;
17027 case PRAGMA_OACC_DATA:
17028 stmt = c_parser_oacc_data (loc, parser);
17029 break;
17030 case PRAGMA_OACC_HOST_DATA:
17031 stmt = c_parser_oacc_host_data (loc, parser);
17032 break;
17033 case PRAGMA_OACC_KERNELS:
17034 case PRAGMA_OACC_PARALLEL:
17035 strcpy (p_name, "#pragma acc");
17036 stmt = c_parser_oacc_kernels_parallel (loc, parser, p_kind, p_name);
17037 break;
17038 case PRAGMA_OACC_LOOP:
17039 strcpy (p_name, "#pragma acc");
17040 stmt = c_parser_oacc_loop (loc, parser, p_name, mask, NULL);
17041 break;
17042 case PRAGMA_OACC_WAIT:
17043 strcpy (p_name, "#pragma wait");
17044 stmt = c_parser_oacc_wait (loc, parser, p_name);
17045 break;
17046 case PRAGMA_OMP_ATOMIC:
17047 c_parser_omp_atomic (loc, parser);
17048 return;
17049 case PRAGMA_OMP_CRITICAL:
17050 stmt = c_parser_omp_critical (loc, parser);
17051 break;
17052 case PRAGMA_OMP_DISTRIBUTE:
17053 strcpy (p_name, "#pragma omp");
17054 stmt = c_parser_omp_distribute (loc, parser, p_name, mask, NULL);
17055 break;
17056 case PRAGMA_OMP_FOR:
17057 strcpy (p_name, "#pragma omp");
17058 stmt = c_parser_omp_for (loc, parser, p_name, mask, NULL);
17059 break;
17060 case PRAGMA_OMP_MASTER:
17061 stmt = c_parser_omp_master (loc, parser);
17062 break;
17063 case PRAGMA_OMP_PARALLEL:
17064 strcpy (p_name, "#pragma omp");
17065 stmt = c_parser_omp_parallel (loc, parser, p_name, mask, NULL);
17066 break;
17067 case PRAGMA_OMP_SECTIONS:
17068 strcpy (p_name, "#pragma omp");
17069 stmt = c_parser_omp_sections (loc, parser, p_name, mask, NULL);
17070 break;
17071 case PRAGMA_OMP_SIMD:
17072 strcpy (p_name, "#pragma omp");
17073 stmt = c_parser_omp_simd (loc, parser, p_name, mask, NULL);
17074 break;
17075 case PRAGMA_OMP_SINGLE:
17076 stmt = c_parser_omp_single (loc, parser);
17077 break;
17078 case PRAGMA_OMP_TASK:
17079 stmt = c_parser_omp_task (loc, parser);
17080 break;
17081 case PRAGMA_OMP_TASKGROUP:
17082 stmt = c_parser_omp_taskgroup (parser);
17083 break;
17084 case PRAGMA_OMP_TASKLOOP:
17085 strcpy (p_name, "#pragma omp");
17086 stmt = c_parser_omp_taskloop (loc, parser, p_name, mask, NULL);
17087 break;
17088 case PRAGMA_OMP_TEAMS:
17089 strcpy (p_name, "#pragma omp");
17090 stmt = c_parser_omp_teams (loc, parser, p_name, mask, NULL);
17091 break;
17092 default:
17093 gcc_unreachable ();
17094 }
17095
17096 if (stmt)
17097 gcc_assert (EXPR_LOCATION (stmt) != UNKNOWN_LOCATION);
17098 }
17099
17100
17101 /* OpenMP 2.5:
17102 # pragma omp threadprivate (variable-list) */
17103
17104 static void
17105 c_parser_omp_threadprivate (c_parser *parser)
17106 {
17107 tree vars, t;
17108 location_t loc;
17109
17110 c_parser_consume_pragma (parser);
17111 loc = c_parser_peek_token (parser)->location;
17112 vars = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
17113
17114 /* Mark every variable in VARS to be assigned thread local storage. */
17115 for (t = vars; t; t = TREE_CHAIN (t))
17116 {
17117 tree v = TREE_PURPOSE (t);
17118
17119 /* FIXME diagnostics: Ideally we should keep individual
17120 locations for all the variables in the var list to make the
17121 following errors more precise. Perhaps
17122 c_parser_omp_var_list_parens() should construct a list of
17123 locations to go along with the var list. */
17124
17125 /* If V had already been marked threadprivate, it doesn't matter
17126 whether it had been used prior to this point. */
17127 if (!VAR_P (v))
17128 error_at (loc, "%qD is not a variable", v);
17129 else if (TREE_USED (v) && !C_DECL_THREADPRIVATE_P (v))
17130 error_at (loc, "%qE declared %<threadprivate%> after first use", v);
17131 else if (! is_global_var (v))
17132 error_at (loc, "automatic variable %qE cannot be %<threadprivate%>", v);
17133 else if (TREE_TYPE (v) == error_mark_node)
17134 ;
17135 else if (! COMPLETE_TYPE_P (TREE_TYPE (v)))
17136 error_at (loc, "%<threadprivate%> %qE has incomplete type", v);
17137 else
17138 {
17139 if (! DECL_THREAD_LOCAL_P (v))
17140 {
17141 set_decl_tls_model (v, decl_default_tls_model (v));
17142 /* If rtl has been already set for this var, call
17143 make_decl_rtl once again, so that encode_section_info
17144 has a chance to look at the new decl flags. */
17145 if (DECL_RTL_SET_P (v))
17146 make_decl_rtl (v);
17147 }
17148 C_DECL_THREADPRIVATE_P (v) = 1;
17149 }
17150 }
17151
17152 c_parser_skip_to_pragma_eol (parser);
17153 }
17154 \f
17155 /* Cilk Plus <#pragma simd> parsing routines. */
17156
17157 /* Helper function for c_parser_pragma. Perform some sanity checking
17158 for <#pragma simd> constructs. Returns FALSE if there was a
17159 problem. */
17160
17161 static bool
17162 c_parser_cilk_verify_simd (c_parser *parser,
17163 enum pragma_context context)
17164 {
17165 if (!flag_cilkplus)
17166 {
17167 warning (0, "pragma simd ignored because -fcilkplus is not enabled");
17168 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
17169 return false;
17170 }
17171 if (context == pragma_external)
17172 {
17173 c_parser_error (parser,"pragma simd must be inside a function");
17174 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
17175 return false;
17176 }
17177 return true;
17178 }
17179
17180 /* Cilk Plus:
17181 This function is shared by SIMD-enabled functions and #pragma simd.
17182 If IS_SIMD_FN is true then it is parsing a SIMD-enabled function and
17183 CLAUSES is unused. The main purpose of this function is to parse a
17184 vectorlength attribute or clause and check for parse errors.
17185 When IS_SIMD_FN is true then the function is merely caching the tokens
17186 in PARSER->CILK_SIMD_FN_TOKENS. If errors are found then the token
17187 cache is cleared since there is no reason to continue.
17188 Syntax:
17189 vectorlength ( constant-expression ) */
17190
17191 static tree
17192 c_parser_cilk_clause_vectorlength (c_parser *parser, tree clauses,
17193 bool is_simd_fn)
17194 {
17195 if (is_simd_fn)
17196 check_no_duplicate_clause (clauses, OMP_CLAUSE_SIMDLEN, "vectorlength");
17197 else
17198 /* The vectorlength clause behaves exactly like OpenMP's safelen
17199 clause. Represent it in OpenMP terms. */
17200 check_no_duplicate_clause (clauses, OMP_CLAUSE_SAFELEN, "vectorlength");
17201
17202 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
17203 return clauses;
17204
17205 location_t loc = c_parser_peek_token (parser)->location;
17206 tree expr = c_parser_expr_no_commas (parser, NULL).value;
17207 expr = c_fully_fold (expr, false, NULL);
17208
17209 /* If expr is an error_mark_node then the above function would have
17210 emitted an error. No reason to do it twice. */
17211 if (expr == error_mark_node)
17212 ;
17213 else if (!TREE_TYPE (expr)
17214 || !TREE_CONSTANT (expr)
17215 || !INTEGRAL_TYPE_P (TREE_TYPE (expr)))
17216
17217 error_at (loc, "vectorlength must be an integer constant");
17218 else if (wi::exact_log2 (expr) == -1)
17219 error_at (loc, "vectorlength must be a power of 2");
17220 else
17221 {
17222 if (is_simd_fn)
17223 {
17224 tree u = build_omp_clause (loc, OMP_CLAUSE_SIMDLEN);
17225 OMP_CLAUSE_SIMDLEN_EXPR (u) = expr;
17226 OMP_CLAUSE_CHAIN (u) = clauses;
17227 clauses = u;
17228 }
17229 else
17230 {
17231 tree u = build_omp_clause (loc, OMP_CLAUSE_SAFELEN);
17232 OMP_CLAUSE_SAFELEN_EXPR (u) = expr;
17233 OMP_CLAUSE_CHAIN (u) = clauses;
17234 clauses = u;
17235 }
17236 }
17237
17238 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
17239
17240 return clauses;
17241 }
17242
17243 /* Cilk Plus:
17244 linear ( simd-linear-variable-list )
17245
17246 simd-linear-variable-list:
17247 simd-linear-variable
17248 simd-linear-variable-list , simd-linear-variable
17249
17250 simd-linear-variable:
17251 id-expression
17252 id-expression : simd-linear-step
17253
17254 simd-linear-step:
17255 conditional-expression */
17256
17257 static tree
17258 c_parser_cilk_clause_linear (c_parser *parser, tree clauses)
17259 {
17260 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
17261 return clauses;
17262
17263 location_t loc = c_parser_peek_token (parser)->location;
17264
17265 if (c_parser_next_token_is_not (parser, CPP_NAME)
17266 || c_parser_peek_token (parser)->id_kind != C_ID_ID)
17267 c_parser_error (parser, "expected identifier");
17268
17269 while (c_parser_next_token_is (parser, CPP_NAME)
17270 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
17271 {
17272 tree var = lookup_name (c_parser_peek_token (parser)->value);
17273
17274 if (var == NULL)
17275 {
17276 undeclared_variable (c_parser_peek_token (parser)->location,
17277 c_parser_peek_token (parser)->value);
17278 c_parser_consume_token (parser);
17279 }
17280 else if (var == error_mark_node)
17281 c_parser_consume_token (parser);
17282 else
17283 {
17284 tree step = integer_one_node;
17285
17286 /* Parse the linear step if present. */
17287 if (c_parser_peek_2nd_token (parser)->type == CPP_COLON)
17288 {
17289 c_parser_consume_token (parser);
17290 c_parser_consume_token (parser);
17291
17292 tree expr = c_parser_expr_no_commas (parser, NULL).value;
17293 expr = c_fully_fold (expr, false, NULL);
17294
17295 if (TREE_TYPE (expr)
17296 && INTEGRAL_TYPE_P (TREE_TYPE (expr))
17297 && (TREE_CONSTANT (expr)
17298 || DECL_P (expr)))
17299 step = expr;
17300 else
17301 c_parser_error (parser,
17302 "step size must be an integer constant "
17303 "expression or an integer variable");
17304 }
17305 else
17306 c_parser_consume_token (parser);
17307
17308 /* Use OMP_CLAUSE_LINEAR, which has the same semantics. */
17309 tree u = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
17310 OMP_CLAUSE_DECL (u) = var;
17311 OMP_CLAUSE_LINEAR_STEP (u) = step;
17312 OMP_CLAUSE_CHAIN (u) = clauses;
17313 clauses = u;
17314 }
17315
17316 if (c_parser_next_token_is_not (parser, CPP_COMMA))
17317 break;
17318
17319 c_parser_consume_token (parser);
17320 }
17321
17322 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
17323
17324 return clauses;
17325 }
17326
17327 /* Returns the name of the next clause. If the clause is not
17328 recognized SIMD_OMP_CLAUSE_NONE is returned and the next token is
17329 not consumed. Otherwise, the appropriate pragma_simd_clause is
17330 returned and the token is consumed. */
17331
17332 static pragma_omp_clause
17333 c_parser_cilk_clause_name (c_parser *parser)
17334 {
17335 pragma_omp_clause result;
17336 c_token *token = c_parser_peek_token (parser);
17337
17338 if (!token->value || token->type != CPP_NAME)
17339 return PRAGMA_CILK_CLAUSE_NONE;
17340
17341 const char *p = IDENTIFIER_POINTER (token->value);
17342
17343 if (!strcmp (p, "vectorlength"))
17344 result = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
17345 else if (!strcmp (p, "linear"))
17346 result = PRAGMA_CILK_CLAUSE_LINEAR;
17347 else if (!strcmp (p, "private"))
17348 result = PRAGMA_CILK_CLAUSE_PRIVATE;
17349 else if (!strcmp (p, "firstprivate"))
17350 result = PRAGMA_CILK_CLAUSE_FIRSTPRIVATE;
17351 else if (!strcmp (p, "lastprivate"))
17352 result = PRAGMA_CILK_CLAUSE_LASTPRIVATE;
17353 else if (!strcmp (p, "reduction"))
17354 result = PRAGMA_CILK_CLAUSE_REDUCTION;
17355 else
17356 return PRAGMA_CILK_CLAUSE_NONE;
17357
17358 c_parser_consume_token (parser);
17359 return result;
17360 }
17361
17362 /* Parse all #<pragma simd> clauses. Return the list of clauses
17363 found. */
17364
17365 static tree
17366 c_parser_cilk_all_clauses (c_parser *parser)
17367 {
17368 tree clauses = NULL;
17369
17370 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
17371 {
17372 pragma_omp_clause c_kind;
17373
17374 c_kind = c_parser_cilk_clause_name (parser);
17375
17376 switch (c_kind)
17377 {
17378 case PRAGMA_CILK_CLAUSE_VECTORLENGTH:
17379 clauses = c_parser_cilk_clause_vectorlength (parser, clauses, false);
17380 break;
17381 case PRAGMA_CILK_CLAUSE_LINEAR:
17382 clauses = c_parser_cilk_clause_linear (parser, clauses);
17383 break;
17384 case PRAGMA_CILK_CLAUSE_PRIVATE:
17385 /* Use the OpenMP counterpart. */
17386 clauses = c_parser_omp_clause_private (parser, clauses);
17387 break;
17388 case PRAGMA_CILK_CLAUSE_FIRSTPRIVATE:
17389 /* Use the OpenMP counterpart. */
17390 clauses = c_parser_omp_clause_firstprivate (parser, clauses);
17391 break;
17392 case PRAGMA_CILK_CLAUSE_LASTPRIVATE:
17393 /* Use the OpenMP counterpart. */
17394 clauses = c_parser_omp_clause_lastprivate (parser, clauses);
17395 break;
17396 case PRAGMA_CILK_CLAUSE_REDUCTION:
17397 /* Use the OpenMP counterpart. */
17398 clauses = c_parser_omp_clause_reduction (parser, clauses);
17399 break;
17400 default:
17401 c_parser_error (parser, "expected %<#pragma simd%> clause");
17402 goto saw_error;
17403 }
17404 }
17405
17406 saw_error:
17407 c_parser_skip_to_pragma_eol (parser);
17408 return c_finish_cilk_clauses (clauses);
17409 }
17410
17411 /* This function helps parse the grainsize pragma for a _Cilk_for statement.
17412 Here is the correct syntax of this pragma:
17413 #pragma cilk grainsize = <EXP>
17414 */
17415
17416 static void
17417 c_parser_cilk_grainsize (c_parser *parser)
17418 {
17419 extern tree convert_to_integer (tree, tree);
17420
17421 /* consume the 'grainsize' keyword. */
17422 c_parser_consume_pragma (parser);
17423
17424 if (c_parser_require (parser, CPP_EQ, "expected %<=%>") != 0)
17425 {
17426 struct c_expr g_expr = c_parser_binary_expression (parser, NULL, NULL);
17427 if (g_expr.value == error_mark_node)
17428 {
17429 c_parser_skip_to_pragma_eol (parser);
17430 return;
17431 }
17432 tree grain = convert_to_integer (long_integer_type_node,
17433 c_fully_fold (g_expr.value, false,
17434 NULL));
17435 c_parser_skip_to_pragma_eol (parser);
17436 c_token *token = c_parser_peek_token (parser);
17437 if (token && token->type == CPP_KEYWORD
17438 && token->keyword == RID_CILK_FOR)
17439 {
17440 if (grain == NULL_TREE || grain == error_mark_node)
17441 grain = integer_zero_node;
17442 c_parser_cilk_for (parser, grain);
17443 }
17444 else
17445 warning (0, "%<#pragma cilk grainsize%> is not followed by "
17446 "%<_Cilk_for%>");
17447 }
17448 else
17449 c_parser_skip_to_pragma_eol (parser);
17450 }
17451
17452 /* Main entry point for parsing Cilk Plus <#pragma simd> for loops. */
17453
17454 static void
17455 c_parser_cilk_simd (c_parser *parser)
17456 {
17457 tree clauses = c_parser_cilk_all_clauses (parser);
17458 tree block = c_begin_compound_stmt (true);
17459 location_t loc = c_parser_peek_token (parser)->location;
17460 c_parser_omp_for_loop (loc, parser, CILK_SIMD, clauses, NULL);
17461 block = c_end_compound_stmt (loc, block, true);
17462 add_stmt (block);
17463 }
17464
17465 /* Create an artificial decl with TYPE and emit initialization of it with
17466 INIT. */
17467
17468 static tree
17469 c_get_temp_regvar (tree type, tree init)
17470 {
17471 location_t loc = EXPR_LOCATION (init);
17472 tree decl = build_decl (loc, VAR_DECL, NULL_TREE, type);
17473 DECL_ARTIFICIAL (decl) = 1;
17474 DECL_IGNORED_P (decl) = 1;
17475 pushdecl (decl);
17476 tree t = build2 (INIT_EXPR, type, decl, init);
17477 add_stmt (t);
17478 return decl;
17479 }
17480
17481 /* Main entry point for parsing Cilk Plus _Cilk_for loops.
17482 GRAIN is the grain value passed in through pragma or 0. */
17483
17484 static void
17485 c_parser_cilk_for (c_parser *parser, tree grain)
17486 {
17487 tree clauses = build_omp_clause (EXPR_LOCATION (grain), OMP_CLAUSE_SCHEDULE);
17488 OMP_CLAUSE_SCHEDULE_KIND (clauses) = OMP_CLAUSE_SCHEDULE_CILKFOR;
17489 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clauses) = grain;
17490 clauses = c_finish_omp_clauses (clauses, false);
17491
17492 tree block = c_begin_compound_stmt (true);
17493 tree sb = push_stmt_list ();
17494 location_t loc = c_parser_peek_token (parser)->location;
17495 tree omp_for = c_parser_omp_for_loop (loc, parser, CILK_FOR, clauses, NULL);
17496 sb = pop_stmt_list (sb);
17497
17498 if (omp_for)
17499 {
17500 tree omp_par = make_node (OMP_PARALLEL);
17501 TREE_TYPE (omp_par) = void_type_node;
17502 OMP_PARALLEL_CLAUSES (omp_par) = NULL_TREE;
17503 tree bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL);
17504 TREE_SIDE_EFFECTS (bind) = 1;
17505 BIND_EXPR_BODY (bind) = sb;
17506 OMP_PARALLEL_BODY (omp_par) = bind;
17507 if (OMP_FOR_PRE_BODY (omp_for))
17508 {
17509 add_stmt (OMP_FOR_PRE_BODY (omp_for));
17510 OMP_FOR_PRE_BODY (omp_for) = NULL_TREE;
17511 }
17512 tree init = TREE_VEC_ELT (OMP_FOR_INIT (omp_for), 0);
17513 tree decl = TREE_OPERAND (init, 0);
17514 tree cond = TREE_VEC_ELT (OMP_FOR_COND (omp_for), 0);
17515 tree incr = TREE_VEC_ELT (OMP_FOR_INCR (omp_for), 0);
17516 tree t = TREE_OPERAND (cond, 1), c, clauses = NULL_TREE;
17517 if (TREE_CODE (t) != INTEGER_CST)
17518 {
17519 TREE_OPERAND (cond, 1) = c_get_temp_regvar (TREE_TYPE (t), t);
17520 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
17521 OMP_CLAUSE_DECL (c) = TREE_OPERAND (cond, 1);
17522 OMP_CLAUSE_CHAIN (c) = clauses;
17523 clauses = c;
17524 }
17525 if (TREE_CODE (incr) == MODIFY_EXPR)
17526 {
17527 t = TREE_OPERAND (TREE_OPERAND (incr, 1), 1);
17528 if (TREE_CODE (t) != INTEGER_CST)
17529 {
17530 TREE_OPERAND (TREE_OPERAND (incr, 1), 1)
17531 = c_get_temp_regvar (TREE_TYPE (t), t);
17532 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
17533 OMP_CLAUSE_DECL (c) = TREE_OPERAND (TREE_OPERAND (incr, 1), 1);
17534 OMP_CLAUSE_CHAIN (c) = clauses;
17535 clauses = c;
17536 }
17537 }
17538 t = TREE_OPERAND (init, 1);
17539 if (TREE_CODE (t) != INTEGER_CST)
17540 {
17541 TREE_OPERAND (init, 1) = c_get_temp_regvar (TREE_TYPE (t), t);
17542 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
17543 OMP_CLAUSE_DECL (c) = TREE_OPERAND (init, 1);
17544 OMP_CLAUSE_CHAIN (c) = clauses;
17545 clauses = c;
17546 }
17547 c = build_omp_clause (input_location, OMP_CLAUSE_PRIVATE);
17548 OMP_CLAUSE_DECL (c) = decl;
17549 OMP_CLAUSE_CHAIN (c) = clauses;
17550 clauses = c;
17551 c = build_omp_clause (input_location, OMP_CLAUSE__CILK_FOR_COUNT_);
17552 OMP_CLAUSE_OPERAND (c, 0)
17553 = cilk_for_number_of_iterations (omp_for);
17554 OMP_CLAUSE_CHAIN (c) = clauses;
17555 OMP_PARALLEL_CLAUSES (omp_par) = c_finish_omp_clauses (c, true);
17556 add_stmt (omp_par);
17557 }
17558
17559 block = c_end_compound_stmt (loc, block, true);
17560 add_stmt (block);
17561 }
17562
17563 \f
17564 /* Parse a transaction attribute (GCC Extension).
17565
17566 transaction-attribute:
17567 attributes
17568 [ [ any-word ] ]
17569
17570 The transactional memory language description is written for C++,
17571 and uses the C++0x attribute syntax. For compatibility, allow the
17572 bracket style for transactions in C as well. */
17573
17574 static tree
17575 c_parser_transaction_attributes (c_parser *parser)
17576 {
17577 tree attr_name, attr = NULL;
17578
17579 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
17580 return c_parser_attributes (parser);
17581
17582 if (!c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
17583 return NULL_TREE;
17584 c_parser_consume_token (parser);
17585 if (!c_parser_require (parser, CPP_OPEN_SQUARE, "expected %<[%>"))
17586 goto error1;
17587
17588 attr_name = c_parser_attribute_any_word (parser);
17589 if (attr_name)
17590 {
17591 c_parser_consume_token (parser);
17592 attr = build_tree_list (attr_name, NULL_TREE);
17593 }
17594 else
17595 c_parser_error (parser, "expected identifier");
17596
17597 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, "expected %<]%>");
17598 error1:
17599 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, "expected %<]%>");
17600 return attr;
17601 }
17602
17603 /* Parse a __transaction_atomic or __transaction_relaxed statement
17604 (GCC Extension).
17605
17606 transaction-statement:
17607 __transaction_atomic transaction-attribute[opt] compound-statement
17608 __transaction_relaxed compound-statement
17609
17610 Note that the only valid attribute is: "outer".
17611 */
17612
17613 static tree
17614 c_parser_transaction (c_parser *parser, enum rid keyword)
17615 {
17616 unsigned int old_in = parser->in_transaction;
17617 unsigned int this_in = 1, new_in;
17618 location_t loc = c_parser_peek_token (parser)->location;
17619 tree stmt, attrs;
17620
17621 gcc_assert ((keyword == RID_TRANSACTION_ATOMIC
17622 || keyword == RID_TRANSACTION_RELAXED)
17623 && c_parser_next_token_is_keyword (parser, keyword));
17624 c_parser_consume_token (parser);
17625
17626 if (keyword == RID_TRANSACTION_RELAXED)
17627 this_in |= TM_STMT_ATTR_RELAXED;
17628 else
17629 {
17630 attrs = c_parser_transaction_attributes (parser);
17631 if (attrs)
17632 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
17633 }
17634
17635 /* Keep track if we're in the lexical scope of an outer transaction. */
17636 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
17637
17638 parser->in_transaction = new_in;
17639 stmt = c_parser_compound_statement (parser);
17640 parser->in_transaction = old_in;
17641
17642 if (flag_tm)
17643 stmt = c_finish_transaction (loc, stmt, this_in);
17644 else
17645 error_at (loc, (keyword == RID_TRANSACTION_ATOMIC ?
17646 "%<__transaction_atomic%> without transactional memory support enabled"
17647 : "%<__transaction_relaxed %> "
17648 "without transactional memory support enabled"));
17649
17650 return stmt;
17651 }
17652
17653 /* Parse a __transaction_atomic or __transaction_relaxed expression
17654 (GCC Extension).
17655
17656 transaction-expression:
17657 __transaction_atomic ( expression )
17658 __transaction_relaxed ( expression )
17659 */
17660
17661 static struct c_expr
17662 c_parser_transaction_expression (c_parser *parser, enum rid keyword)
17663 {
17664 struct c_expr ret;
17665 unsigned int old_in = parser->in_transaction;
17666 unsigned int this_in = 1;
17667 location_t loc = c_parser_peek_token (parser)->location;
17668 tree attrs;
17669
17670 gcc_assert ((keyword == RID_TRANSACTION_ATOMIC
17671 || keyword == RID_TRANSACTION_RELAXED)
17672 && c_parser_next_token_is_keyword (parser, keyword));
17673 c_parser_consume_token (parser);
17674
17675 if (keyword == RID_TRANSACTION_RELAXED)
17676 this_in |= TM_STMT_ATTR_RELAXED;
17677 else
17678 {
17679 attrs = c_parser_transaction_attributes (parser);
17680 if (attrs)
17681 this_in |= parse_tm_stmt_attr (attrs, 0);
17682 }
17683
17684 parser->in_transaction = this_in;
17685 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
17686 {
17687 tree expr = c_parser_expression (parser).value;
17688 ret.original_type = TREE_TYPE (expr);
17689 ret.value = build1 (TRANSACTION_EXPR, ret.original_type, expr);
17690 if (this_in & TM_STMT_ATTR_RELAXED)
17691 TRANSACTION_EXPR_RELAXED (ret.value) = 1;
17692 SET_EXPR_LOCATION (ret.value, loc);
17693 ret.original_code = TRANSACTION_EXPR;
17694 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
17695 {
17696 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
17697 goto error;
17698 }
17699 }
17700 else
17701 {
17702 error:
17703 ret.value = error_mark_node;
17704 ret.original_code = ERROR_MARK;
17705 ret.original_type = NULL;
17706 }
17707 parser->in_transaction = old_in;
17708
17709 if (!flag_tm)
17710 error_at (loc, (keyword == RID_TRANSACTION_ATOMIC ?
17711 "%<__transaction_atomic%> without transactional memory support enabled"
17712 : "%<__transaction_relaxed %> "
17713 "without transactional memory support enabled"));
17714
17715 set_c_expr_source_range (&ret, loc, loc);
17716
17717 return ret;
17718 }
17719
17720 /* Parse a __transaction_cancel statement (GCC Extension).
17721
17722 transaction-cancel-statement:
17723 __transaction_cancel transaction-attribute[opt] ;
17724
17725 Note that the only valid attribute is "outer".
17726 */
17727
17728 static tree
17729 c_parser_transaction_cancel (c_parser *parser)
17730 {
17731 location_t loc = c_parser_peek_token (parser)->location;
17732 tree attrs;
17733 bool is_outer = false;
17734
17735 gcc_assert (c_parser_next_token_is_keyword (parser, RID_TRANSACTION_CANCEL));
17736 c_parser_consume_token (parser);
17737
17738 attrs = c_parser_transaction_attributes (parser);
17739 if (attrs)
17740 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
17741
17742 if (!flag_tm)
17743 {
17744 error_at (loc, "%<__transaction_cancel%> without "
17745 "transactional memory support enabled");
17746 goto ret_error;
17747 }
17748 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
17749 {
17750 error_at (loc, "%<__transaction_cancel%> within a "
17751 "%<__transaction_relaxed%>");
17752 goto ret_error;
17753 }
17754 else if (is_outer)
17755 {
17756 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
17757 && !is_tm_may_cancel_outer (current_function_decl))
17758 {
17759 error_at (loc, "outer %<__transaction_cancel%> not "
17760 "within outer %<__transaction_atomic%>");
17761 error_at (loc, " or a %<transaction_may_cancel_outer%> function");
17762 goto ret_error;
17763 }
17764 }
17765 else if (parser->in_transaction == 0)
17766 {
17767 error_at (loc, "%<__transaction_cancel%> not within "
17768 "%<__transaction_atomic%>");
17769 goto ret_error;
17770 }
17771
17772 return add_stmt (build_tm_abort_call (loc, is_outer));
17773
17774 ret_error:
17775 return build1 (NOP_EXPR, void_type_node, error_mark_node);
17776 }
17777 \f
17778 /* Parse a single source file. */
17779
17780 void
17781 c_parse_file (void)
17782 {
17783 /* Use local storage to begin. If the first token is a pragma, parse it.
17784 If it is #pragma GCC pch_preprocess, then this will load a PCH file
17785 which will cause garbage collection. */
17786 c_parser tparser;
17787
17788 memset (&tparser, 0, sizeof tparser);
17789 tparser.tokens = &tparser.tokens_buf[0];
17790 the_parser = &tparser;
17791
17792 if (c_parser_peek_token (&tparser)->pragma_kind == PRAGMA_GCC_PCH_PREPROCESS)
17793 c_parser_pragma_pch_preprocess (&tparser);
17794
17795 the_parser = ggc_alloc<c_parser> ();
17796 *the_parser = tparser;
17797 if (tparser.tokens == &tparser.tokens_buf[0])
17798 the_parser->tokens = &the_parser->tokens_buf[0];
17799
17800 /* Initialize EH, if we've been told to do so. */
17801 if (flag_exceptions)
17802 using_eh_for_cleanups ();
17803
17804 c_parser_translation_unit (the_parser);
17805 the_parser = NULL;
17806 }
17807
17808 /* This function parses Cilk Plus array notation. The starting index is
17809 passed in INITIAL_INDEX and the array name is passes in ARRAY_VALUE. The
17810 return value of this function is a tree_node called VALUE_TREE of type
17811 ARRAY_NOTATION_REF. */
17812
17813 static tree
17814 c_parser_array_notation (location_t loc, c_parser *parser, tree initial_index,
17815 tree array_value)
17816 {
17817 c_token *token = NULL;
17818 tree start_index = NULL_TREE, end_index = NULL_TREE, stride = NULL_TREE;
17819 tree value_tree = NULL_TREE, type = NULL_TREE, array_type = NULL_TREE;
17820 tree array_type_domain = NULL_TREE;
17821
17822 if (array_value == error_mark_node || initial_index == error_mark_node)
17823 {
17824 /* No need to continue. If either of these 2 were true, then an error
17825 must be emitted already. Thus, no need to emit them twice. */
17826 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
17827 return error_mark_node;
17828 }
17829
17830 array_type = TREE_TYPE (array_value);
17831 gcc_assert (array_type);
17832 if (TREE_CODE (array_type) != ARRAY_TYPE
17833 && TREE_CODE (array_type) != POINTER_TYPE)
17834 {
17835 error_at (loc, "base of array section must be pointer or array type");
17836 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
17837 return error_mark_node;
17838 }
17839 type = TREE_TYPE (array_type);
17840 token = c_parser_peek_token (parser);
17841
17842 if (token->type == CPP_EOF)
17843 {
17844 c_parser_error (parser, "expected %<:%> or numeral");
17845 return value_tree;
17846 }
17847 else if (token->type == CPP_COLON)
17848 {
17849 if (!initial_index)
17850 {
17851 /* If we are here, then we have a case like this A[:]. */
17852 c_parser_consume_token (parser);
17853 if (TREE_CODE (array_type) == POINTER_TYPE)
17854 {
17855 error_at (loc, "start-index and length fields necessary for "
17856 "using array notations in pointers");
17857 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
17858 return error_mark_node;
17859 }
17860 if (TREE_CODE (array_type) == FUNCTION_TYPE)
17861 {
17862 error_at (loc, "array notations cannot be used with function "
17863 "type");
17864 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
17865 return error_mark_node;
17866 }
17867 array_type_domain = TYPE_DOMAIN (array_type);
17868
17869 if (!array_type_domain)
17870 {
17871 error_at (loc, "start-index and length fields necessary for "
17872 "using array notations in dimensionless arrays");
17873 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
17874 return error_mark_node;
17875 }
17876
17877 start_index = TYPE_MINVAL (array_type_domain);
17878 start_index = fold_build1 (CONVERT_EXPR, ptrdiff_type_node,
17879 start_index);
17880 if (!TYPE_MAXVAL (array_type_domain)
17881 || !TREE_CONSTANT (TYPE_MAXVAL (array_type_domain)))
17882 {
17883 error_at (loc, "start-index and length fields necessary for "
17884 "using array notations in variable-length arrays");
17885 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
17886 return error_mark_node;
17887 }
17888 end_index = TYPE_MAXVAL (array_type_domain);
17889 end_index = fold_build2 (PLUS_EXPR, TREE_TYPE (end_index),
17890 end_index, integer_one_node);
17891 end_index = fold_build1 (CONVERT_EXPR, ptrdiff_type_node, end_index);
17892 stride = build_int_cst (integer_type_node, 1);
17893 stride = fold_build1 (CONVERT_EXPR, ptrdiff_type_node, stride);
17894 }
17895 else if (initial_index != error_mark_node)
17896 {
17897 /* If we are here, then there should be 2 possibilities:
17898 1. Array [EXPR : EXPR]
17899 2. Array [EXPR : EXPR : EXPR]
17900 */
17901 start_index = initial_index;
17902
17903 if (TREE_CODE (array_type) == FUNCTION_TYPE)
17904 {
17905 error_at (loc, "array notations cannot be used with function "
17906 "type");
17907 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
17908 return error_mark_node;
17909 }
17910 c_parser_consume_token (parser); /* consume the ':' */
17911 struct c_expr ce = c_parser_expression (parser);
17912 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
17913 end_index = ce.value;
17914 if (!end_index || end_index == error_mark_node)
17915 {
17916 c_parser_skip_to_end_of_block_or_statement (parser);
17917 return error_mark_node;
17918 }
17919 if (c_parser_peek_token (parser)->type == CPP_COLON)
17920 {
17921 c_parser_consume_token (parser);
17922 ce = c_parser_expression (parser);
17923 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
17924 stride = ce.value;
17925 if (!stride || stride == error_mark_node)
17926 {
17927 c_parser_skip_to_end_of_block_or_statement (parser);
17928 return error_mark_node;
17929 }
17930 }
17931 }
17932 else
17933 c_parser_error (parser, "expected array notation expression");
17934 }
17935 else
17936 c_parser_error (parser, "expected array notation expression");
17937
17938 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, "expected %<]%>");
17939
17940 value_tree = build_array_notation_ref (loc, array_value, start_index,
17941 end_index, stride, type);
17942 if (value_tree != error_mark_node)
17943 SET_EXPR_LOCATION (value_tree, loc);
17944 return value_tree;
17945 }
17946
17947 #include "gt-c-c-parser.h"