]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/c/c-parser.c
2015-06-17 Andrew MacLeod <amacleod@redhat.com>
[thirdparty/gcc.git] / gcc / c / c-parser.c
1 /* Parser for C and Objective-C.
2 Copyright (C) 1987-2015 Free Software Foundation, Inc.
3
4 Parser actions based on the old Bison parser; structure somewhat
5 influenced by and fragments based on the C++ parser.
6
7 This file is part of GCC.
8
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 3, or (at your option) any later
12 version.
13
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
22
23 /* TODO:
24
25 Make sure all relevant comments, and all relevant code from all
26 actions, brought over from old parser. Verify exact correspondence
27 of syntax accepted.
28
29 Add testcases covering every input symbol in every state in old and
30 new parsers.
31
32 Include full syntax for GNU C, including erroneous cases accepted
33 with error messages, in syntax productions in comments.
34
35 Make more diagnostics in the front end generally take an explicit
36 location rather than implicitly using input_location. */
37
38 #include "config.h"
39 #include "system.h"
40 #include "coretypes.h"
41 #include "tm.h" /* For rtl.h: needs enum reg_class. */
42 #include "symtab.h"
43 #include "alias.h"
44 #include "flags.h"
45 #include "tree.h"
46 #include "fold-const.h"
47 #include "stringpool.h"
48 #include "attribs.h"
49 #include "stor-layout.h"
50 #include "varasm.h"
51 #include "trans-mem.h"
52 #include "langhooks.h"
53 #include "cpplib.h"
54 #include "timevar.h"
55 #include "c-family/c-pragma.h"
56 #include "c-tree.h"
57 #include "c-lang.h"
58 #include "flags.h"
59 #include "c-family/c-common.h"
60 #include "c-family/c-objc.h"
61 #include "target.h"
62 #include "plugin-api.h"
63 #include "hard-reg-set.h"
64 #include "function.h"
65 #include "ipa-ref.h"
66 #include "cgraph.h"
67 #include "plugin.h"
68 #include "omp-low.h"
69 #include "builtins.h"
70 #include "gomp-constants.h"
71
72 \f
73 /* Initialization routine for this file. */
74
75 void
76 c_parse_init (void)
77 {
78 /* The only initialization required is of the reserved word
79 identifiers. */
80 unsigned int i;
81 tree id;
82 int mask = 0;
83
84 /* Make sure RID_MAX hasn't grown past the 8 bits used to hold the keyword in
85 the c_token structure. */
86 gcc_assert (RID_MAX <= 255);
87
88 mask |= D_CXXONLY;
89 if (!flag_isoc99)
90 mask |= D_C99;
91 if (flag_no_asm)
92 {
93 mask |= D_ASM | D_EXT;
94 if (!flag_isoc99)
95 mask |= D_EXT89;
96 }
97 if (!c_dialect_objc ())
98 mask |= D_OBJC | D_CXX_OBJC;
99
100 ridpointers = ggc_cleared_vec_alloc<tree> ((int) RID_MAX);
101 for (i = 0; i < num_c_common_reswords; i++)
102 {
103 /* If a keyword is disabled, do not enter it into the table
104 and so create a canonical spelling that isn't a keyword. */
105 if (c_common_reswords[i].disable & mask)
106 {
107 if (warn_cxx_compat
108 && (c_common_reswords[i].disable & D_CXXWARN))
109 {
110 id = get_identifier (c_common_reswords[i].word);
111 C_SET_RID_CODE (id, RID_CXX_COMPAT_WARN);
112 C_IS_RESERVED_WORD (id) = 1;
113 }
114 continue;
115 }
116
117 id = get_identifier (c_common_reswords[i].word);
118 C_SET_RID_CODE (id, c_common_reswords[i].rid);
119 C_IS_RESERVED_WORD (id) = 1;
120 ridpointers [(int) c_common_reswords[i].rid] = id;
121 }
122
123 for (i = 0; i < NUM_INT_N_ENTS; i++)
124 {
125 /* We always create the symbols but they aren't always supported. */
126 char name[50];
127 sprintf (name, "__int%d", int_n_data[i].bitsize);
128 id = get_identifier (name);
129 C_SET_RID_CODE (id, RID_FIRST_INT_N + i);
130 C_IS_RESERVED_WORD (id) = 1;
131 }
132 }
133 \f
134 /* The C lexer intermediates between the lexer in cpplib and c-lex.c
135 and the C parser. Unlike the C++ lexer, the parser structure
136 stores the lexer information instead of using a separate structure.
137 Identifiers are separated into ordinary identifiers, type names,
138 keywords and some other Objective-C types of identifiers, and some
139 look-ahead is maintained.
140
141 ??? It might be a good idea to lex the whole file up front (as for
142 C++). It would then be possible to share more of the C and C++
143 lexer code, if desired. */
144
145 /* More information about the type of a CPP_NAME token. */
146 typedef enum c_id_kind {
147 /* An ordinary identifier. */
148 C_ID_ID,
149 /* An identifier declared as a typedef name. */
150 C_ID_TYPENAME,
151 /* An identifier declared as an Objective-C class name. */
152 C_ID_CLASSNAME,
153 /* An address space identifier. */
154 C_ID_ADDRSPACE,
155 /* Not an identifier. */
156 C_ID_NONE
157 } c_id_kind;
158
159 /* A single C token after string literal concatenation and conversion
160 of preprocessing tokens to tokens. */
161 typedef struct GTY (()) c_token {
162 /* The kind of token. */
163 ENUM_BITFIELD (cpp_ttype) type : 8;
164 /* If this token is a CPP_NAME, this value indicates whether also
165 declared as some kind of type. Otherwise, it is C_ID_NONE. */
166 ENUM_BITFIELD (c_id_kind) id_kind : 8;
167 /* If this token is a keyword, this value indicates which keyword.
168 Otherwise, this value is RID_MAX. */
169 ENUM_BITFIELD (rid) keyword : 8;
170 /* If this token is a CPP_PRAGMA, this indicates the pragma that
171 was seen. Otherwise it is PRAGMA_NONE. */
172 ENUM_BITFIELD (pragma_kind) pragma_kind : 8;
173 /* The location at which this token was found. */
174 location_t location;
175 /* The value associated with this token, if any. */
176 tree value;
177 } c_token;
178
179 /* A parser structure recording information about the state and
180 context of parsing. Includes lexer information with up to two
181 tokens of look-ahead; more are not needed for C. */
182 typedef struct GTY(()) c_parser {
183 /* The look-ahead tokens. */
184 c_token * GTY((skip)) tokens;
185 /* Buffer for look-ahead tokens. */
186 c_token tokens_buf[2];
187 /* How many look-ahead tokens are available (0, 1 or 2, or
188 more if parsing from pre-lexed tokens). */
189 unsigned int tokens_avail;
190 /* True if a syntax error is being recovered from; false otherwise.
191 c_parser_error sets this flag. It should clear this flag when
192 enough tokens have been consumed to recover from the error. */
193 BOOL_BITFIELD error : 1;
194 /* True if we're processing a pragma, and shouldn't automatically
195 consume CPP_PRAGMA_EOL. */
196 BOOL_BITFIELD in_pragma : 1;
197 /* True if we're parsing the outermost block of an if statement. */
198 BOOL_BITFIELD in_if_block : 1;
199 /* True if we want to lex an untranslated string. */
200 BOOL_BITFIELD lex_untranslated_string : 1;
201
202 /* Objective-C specific parser/lexer information. */
203
204 /* True if we are in a context where the Objective-C "PQ" keywords
205 are considered keywords. */
206 BOOL_BITFIELD objc_pq_context : 1;
207 /* True if we are parsing a (potential) Objective-C foreach
208 statement. This is set to true after we parsed 'for (' and while
209 we wait for 'in' or ';' to decide if it's a standard C for loop or an
210 Objective-C foreach loop. */
211 BOOL_BITFIELD objc_could_be_foreach_context : 1;
212 /* The following flag is needed to contextualize Objective-C lexical
213 analysis. In some cases (e.g., 'int NSObject;'), it is
214 undesirable to bind an identifier to an Objective-C class, even
215 if a class with that name exists. */
216 BOOL_BITFIELD objc_need_raw_identifier : 1;
217 /* Nonzero if we're processing a __transaction statement. The value
218 is 1 | TM_STMT_ATTR_*. */
219 unsigned int in_transaction : 4;
220 /* True if we are in a context where the Objective-C "Property attribute"
221 keywords are valid. */
222 BOOL_BITFIELD objc_property_attr_context : 1;
223
224 /* Cilk Plus specific parser/lexer information. */
225
226 /* Buffer to hold all the tokens from parsing the vector attribute for the
227 SIMD-enabled functions (formerly known as elemental functions). */
228 vec <c_token, va_gc> *cilk_simd_fn_tokens;
229 } c_parser;
230
231
232 /* The actual parser and external interface. ??? Does this need to be
233 garbage-collected? */
234
235 static GTY (()) c_parser *the_parser;
236
237 /* Read in and lex a single token, storing it in *TOKEN. */
238
239 static void
240 c_lex_one_token (c_parser *parser, c_token *token)
241 {
242 timevar_push (TV_LEX);
243
244 token->type = c_lex_with_flags (&token->value, &token->location, NULL,
245 (parser->lex_untranslated_string
246 ? C_LEX_STRING_NO_TRANSLATE : 0));
247 token->id_kind = C_ID_NONE;
248 token->keyword = RID_MAX;
249 token->pragma_kind = PRAGMA_NONE;
250
251 switch (token->type)
252 {
253 case CPP_NAME:
254 {
255 tree decl;
256
257 bool objc_force_identifier = parser->objc_need_raw_identifier;
258 if (c_dialect_objc ())
259 parser->objc_need_raw_identifier = false;
260
261 if (C_IS_RESERVED_WORD (token->value))
262 {
263 enum rid rid_code = C_RID_CODE (token->value);
264
265 if (rid_code == RID_CXX_COMPAT_WARN)
266 {
267 warning_at (token->location,
268 OPT_Wc___compat,
269 "identifier %qE conflicts with C++ keyword",
270 token->value);
271 }
272 else if (rid_code >= RID_FIRST_ADDR_SPACE
273 && rid_code <= RID_LAST_ADDR_SPACE)
274 {
275 token->id_kind = C_ID_ADDRSPACE;
276 token->keyword = rid_code;
277 break;
278 }
279 else if (c_dialect_objc () && OBJC_IS_PQ_KEYWORD (rid_code))
280 {
281 /* We found an Objective-C "pq" keyword (in, out,
282 inout, bycopy, byref, oneway). They need special
283 care because the interpretation depends on the
284 context. */
285 if (parser->objc_pq_context)
286 {
287 token->type = CPP_KEYWORD;
288 token->keyword = rid_code;
289 break;
290 }
291 else if (parser->objc_could_be_foreach_context
292 && rid_code == RID_IN)
293 {
294 /* We are in Objective-C, inside a (potential)
295 foreach context (which means after having
296 parsed 'for (', but before having parsed ';'),
297 and we found 'in'. We consider it the keyword
298 which terminates the declaration at the
299 beginning of a foreach-statement. Note that
300 this means you can't use 'in' for anything else
301 in that context; in particular, in Objective-C
302 you can't use 'in' as the name of the running
303 variable in a C for loop. We could potentially
304 try to add code here to disambiguate, but it
305 seems a reasonable limitation. */
306 token->type = CPP_KEYWORD;
307 token->keyword = rid_code;
308 break;
309 }
310 /* Else, "pq" keywords outside of the "pq" context are
311 not keywords, and we fall through to the code for
312 normal tokens. */
313 }
314 else if (c_dialect_objc () && OBJC_IS_PATTR_KEYWORD (rid_code))
315 {
316 /* We found an Objective-C "property attribute"
317 keyword (getter, setter, readonly, etc). These are
318 only valid in the property context. */
319 if (parser->objc_property_attr_context)
320 {
321 token->type = CPP_KEYWORD;
322 token->keyword = rid_code;
323 break;
324 }
325 /* Else they are not special keywords.
326 */
327 }
328 else if (c_dialect_objc ()
329 && (OBJC_IS_AT_KEYWORD (rid_code)
330 || OBJC_IS_CXX_KEYWORD (rid_code)))
331 {
332 /* We found one of the Objective-C "@" keywords (defs,
333 selector, synchronized, etc) or one of the
334 Objective-C "cxx" keywords (class, private,
335 protected, public, try, catch, throw) without a
336 preceding '@' sign. Do nothing and fall through to
337 the code for normal tokens (in C++ we would still
338 consider the CXX ones keywords, but not in C). */
339 ;
340 }
341 else
342 {
343 token->type = CPP_KEYWORD;
344 token->keyword = rid_code;
345 break;
346 }
347 }
348
349 decl = lookup_name (token->value);
350 if (decl)
351 {
352 if (TREE_CODE (decl) == TYPE_DECL)
353 {
354 token->id_kind = C_ID_TYPENAME;
355 break;
356 }
357 }
358 else if (c_dialect_objc ())
359 {
360 tree objc_interface_decl = objc_is_class_name (token->value);
361 /* Objective-C class names are in the same namespace as
362 variables and typedefs, and hence are shadowed by local
363 declarations. */
364 if (objc_interface_decl
365 && (!objc_force_identifier || global_bindings_p ()))
366 {
367 token->value = objc_interface_decl;
368 token->id_kind = C_ID_CLASSNAME;
369 break;
370 }
371 }
372 token->id_kind = C_ID_ID;
373 }
374 break;
375 case CPP_AT_NAME:
376 /* This only happens in Objective-C; it must be a keyword. */
377 token->type = CPP_KEYWORD;
378 switch (C_RID_CODE (token->value))
379 {
380 /* Replace 'class' with '@class', 'private' with '@private',
381 etc. This prevents confusion with the C++ keyword
382 'class', and makes the tokens consistent with other
383 Objective-C 'AT' keywords. For example '@class' is
384 reported as RID_AT_CLASS which is consistent with
385 '@synchronized', which is reported as
386 RID_AT_SYNCHRONIZED.
387 */
388 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
389 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
390 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
391 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
392 case RID_THROW: token->keyword = RID_AT_THROW; break;
393 case RID_TRY: token->keyword = RID_AT_TRY; break;
394 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
395 default: token->keyword = C_RID_CODE (token->value);
396 }
397 break;
398 case CPP_COLON:
399 case CPP_COMMA:
400 case CPP_CLOSE_PAREN:
401 case CPP_SEMICOLON:
402 /* These tokens may affect the interpretation of any identifiers
403 following, if doing Objective-C. */
404 if (c_dialect_objc ())
405 parser->objc_need_raw_identifier = false;
406 break;
407 case CPP_PRAGMA:
408 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
409 token->pragma_kind = (enum pragma_kind) TREE_INT_CST_LOW (token->value);
410 token->value = NULL;
411 break;
412 default:
413 break;
414 }
415 timevar_pop (TV_LEX);
416 }
417
418 /* Return a pointer to the next token from PARSER, reading it in if
419 necessary. */
420
421 static inline c_token *
422 c_parser_peek_token (c_parser *parser)
423 {
424 if (parser->tokens_avail == 0)
425 {
426 c_lex_one_token (parser, &parser->tokens[0]);
427 parser->tokens_avail = 1;
428 }
429 return &parser->tokens[0];
430 }
431
432 /* Return true if the next token from PARSER has the indicated
433 TYPE. */
434
435 static inline bool
436 c_parser_next_token_is (c_parser *parser, enum cpp_ttype type)
437 {
438 return c_parser_peek_token (parser)->type == type;
439 }
440
441 /* Return true if the next token from PARSER does not have the
442 indicated TYPE. */
443
444 static inline bool
445 c_parser_next_token_is_not (c_parser *parser, enum cpp_ttype type)
446 {
447 return !c_parser_next_token_is (parser, type);
448 }
449
450 /* Return true if the next token from PARSER is the indicated
451 KEYWORD. */
452
453 static inline bool
454 c_parser_next_token_is_keyword (c_parser *parser, enum rid keyword)
455 {
456 return c_parser_peek_token (parser)->keyword == keyword;
457 }
458
459 /* Return a pointer to the next-but-one token from PARSER, reading it
460 in if necessary. The next token is already read in. */
461
462 static c_token *
463 c_parser_peek_2nd_token (c_parser *parser)
464 {
465 if (parser->tokens_avail >= 2)
466 return &parser->tokens[1];
467 gcc_assert (parser->tokens_avail == 1);
468 gcc_assert (parser->tokens[0].type != CPP_EOF);
469 gcc_assert (parser->tokens[0].type != CPP_PRAGMA_EOL);
470 c_lex_one_token (parser, &parser->tokens[1]);
471 parser->tokens_avail = 2;
472 return &parser->tokens[1];
473 }
474
475 /* Return true if TOKEN can start a type name,
476 false otherwise. */
477 static bool
478 c_token_starts_typename (c_token *token)
479 {
480 switch (token->type)
481 {
482 case CPP_NAME:
483 switch (token->id_kind)
484 {
485 case C_ID_ID:
486 return false;
487 case C_ID_ADDRSPACE:
488 return true;
489 case C_ID_TYPENAME:
490 return true;
491 case C_ID_CLASSNAME:
492 gcc_assert (c_dialect_objc ());
493 return true;
494 default:
495 gcc_unreachable ();
496 }
497 case CPP_KEYWORD:
498 switch (token->keyword)
499 {
500 case RID_UNSIGNED:
501 case RID_LONG:
502 case RID_SHORT:
503 case RID_SIGNED:
504 case RID_COMPLEX:
505 case RID_INT:
506 case RID_CHAR:
507 case RID_FLOAT:
508 case RID_DOUBLE:
509 case RID_VOID:
510 case RID_DFLOAT32:
511 case RID_DFLOAT64:
512 case RID_DFLOAT128:
513 case RID_BOOL:
514 case RID_ENUM:
515 case RID_STRUCT:
516 case RID_UNION:
517 case RID_TYPEOF:
518 case RID_CONST:
519 case RID_ATOMIC:
520 case RID_VOLATILE:
521 case RID_RESTRICT:
522 case RID_ATTRIBUTE:
523 case RID_FRACT:
524 case RID_ACCUM:
525 case RID_SAT:
526 case RID_AUTO_TYPE:
527 return true;
528 default:
529 if (token->keyword >= RID_FIRST_INT_N
530 && token->keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
531 && int_n_enabled_p[token->keyword - RID_FIRST_INT_N])
532 return true;
533 return false;
534 }
535 case CPP_LESS:
536 if (c_dialect_objc ())
537 return true;
538 return false;
539 default:
540 return false;
541 }
542 }
543
544 enum c_lookahead_kind {
545 /* Always treat unknown identifiers as typenames. */
546 cla_prefer_type,
547
548 /* Could be parsing a nonabstract declarator. Only treat an identifier
549 as a typename if followed by another identifier or a star. */
550 cla_nonabstract_decl,
551
552 /* Never treat identifiers as typenames. */
553 cla_prefer_id
554 };
555
556 /* Return true if the next token from PARSER can start a type name,
557 false otherwise. LA specifies how to do lookahead in order to
558 detect unknown type names. If unsure, pick CLA_PREFER_ID. */
559
560 static inline bool
561 c_parser_next_tokens_start_typename (c_parser *parser, enum c_lookahead_kind la)
562 {
563 c_token *token = c_parser_peek_token (parser);
564 if (c_token_starts_typename (token))
565 return true;
566
567 /* Try a bit harder to detect an unknown typename. */
568 if (la != cla_prefer_id
569 && token->type == CPP_NAME
570 && token->id_kind == C_ID_ID
571
572 /* Do not try too hard when we could have "object in array". */
573 && !parser->objc_could_be_foreach_context
574
575 && (la == cla_prefer_type
576 || c_parser_peek_2nd_token (parser)->type == CPP_NAME
577 || c_parser_peek_2nd_token (parser)->type == CPP_MULT)
578
579 /* Only unknown identifiers. */
580 && !lookup_name (token->value))
581 return true;
582
583 return false;
584 }
585
586 /* Return true if TOKEN is a type qualifier, false otherwise. */
587 static bool
588 c_token_is_qualifier (c_token *token)
589 {
590 switch (token->type)
591 {
592 case CPP_NAME:
593 switch (token->id_kind)
594 {
595 case C_ID_ADDRSPACE:
596 return true;
597 default:
598 return false;
599 }
600 case CPP_KEYWORD:
601 switch (token->keyword)
602 {
603 case RID_CONST:
604 case RID_VOLATILE:
605 case RID_RESTRICT:
606 case RID_ATTRIBUTE:
607 case RID_ATOMIC:
608 return true;
609 default:
610 return false;
611 }
612 case CPP_LESS:
613 return false;
614 default:
615 gcc_unreachable ();
616 }
617 }
618
619 /* Return true if the next token from PARSER is a type qualifier,
620 false otherwise. */
621 static inline bool
622 c_parser_next_token_is_qualifier (c_parser *parser)
623 {
624 c_token *token = c_parser_peek_token (parser);
625 return c_token_is_qualifier (token);
626 }
627
628 /* Return true if TOKEN can start declaration specifiers, false
629 otherwise. */
630 static bool
631 c_token_starts_declspecs (c_token *token)
632 {
633 switch (token->type)
634 {
635 case CPP_NAME:
636 switch (token->id_kind)
637 {
638 case C_ID_ID:
639 return false;
640 case C_ID_ADDRSPACE:
641 return true;
642 case C_ID_TYPENAME:
643 return true;
644 case C_ID_CLASSNAME:
645 gcc_assert (c_dialect_objc ());
646 return true;
647 default:
648 gcc_unreachable ();
649 }
650 case CPP_KEYWORD:
651 switch (token->keyword)
652 {
653 case RID_STATIC:
654 case RID_EXTERN:
655 case RID_REGISTER:
656 case RID_TYPEDEF:
657 case RID_INLINE:
658 case RID_NORETURN:
659 case RID_AUTO:
660 case RID_THREAD:
661 case RID_UNSIGNED:
662 case RID_LONG:
663 case RID_SHORT:
664 case RID_SIGNED:
665 case RID_COMPLEX:
666 case RID_INT:
667 case RID_CHAR:
668 case RID_FLOAT:
669 case RID_DOUBLE:
670 case RID_VOID:
671 case RID_DFLOAT32:
672 case RID_DFLOAT64:
673 case RID_DFLOAT128:
674 case RID_BOOL:
675 case RID_ENUM:
676 case RID_STRUCT:
677 case RID_UNION:
678 case RID_TYPEOF:
679 case RID_CONST:
680 case RID_VOLATILE:
681 case RID_RESTRICT:
682 case RID_ATTRIBUTE:
683 case RID_FRACT:
684 case RID_ACCUM:
685 case RID_SAT:
686 case RID_ALIGNAS:
687 case RID_ATOMIC:
688 case RID_AUTO_TYPE:
689 return true;
690 default:
691 if (token->keyword >= RID_FIRST_INT_N
692 && token->keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
693 && int_n_enabled_p[token->keyword - RID_FIRST_INT_N])
694 return true;
695 return false;
696 }
697 case CPP_LESS:
698 if (c_dialect_objc ())
699 return true;
700 return false;
701 default:
702 return false;
703 }
704 }
705
706
707 /* Return true if TOKEN can start declaration specifiers or a static
708 assertion, false otherwise. */
709 static bool
710 c_token_starts_declaration (c_token *token)
711 {
712 if (c_token_starts_declspecs (token)
713 || token->keyword == RID_STATIC_ASSERT)
714 return true;
715 else
716 return false;
717 }
718
719 /* Return true if the next token from PARSER can start declaration
720 specifiers, false otherwise. */
721 static inline bool
722 c_parser_next_token_starts_declspecs (c_parser *parser)
723 {
724 c_token *token = c_parser_peek_token (parser);
725
726 /* In Objective-C, a classname normally starts a declspecs unless it
727 is immediately followed by a dot. In that case, it is the
728 Objective-C 2.0 "dot-syntax" for class objects, ie, calls the
729 setter/getter on the class. c_token_starts_declspecs() can't
730 differentiate between the two cases because it only checks the
731 current token, so we have a special check here. */
732 if (c_dialect_objc ()
733 && token->type == CPP_NAME
734 && token->id_kind == C_ID_CLASSNAME
735 && c_parser_peek_2nd_token (parser)->type == CPP_DOT)
736 return false;
737
738 return c_token_starts_declspecs (token);
739 }
740
741 /* Return true if the next tokens from PARSER can start declaration
742 specifiers or a static assertion, false otherwise. */
743 static inline bool
744 c_parser_next_tokens_start_declaration (c_parser *parser)
745 {
746 c_token *token = c_parser_peek_token (parser);
747
748 /* Same as above. */
749 if (c_dialect_objc ()
750 && token->type == CPP_NAME
751 && token->id_kind == C_ID_CLASSNAME
752 && c_parser_peek_2nd_token (parser)->type == CPP_DOT)
753 return false;
754
755 /* Labels do not start declarations. */
756 if (token->type == CPP_NAME
757 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
758 return false;
759
760 if (c_token_starts_declaration (token))
761 return true;
762
763 if (c_parser_next_tokens_start_typename (parser, cla_nonabstract_decl))
764 return true;
765
766 return false;
767 }
768
769 /* Consume the next token from PARSER. */
770
771 static void
772 c_parser_consume_token (c_parser *parser)
773 {
774 gcc_assert (parser->tokens_avail >= 1);
775 gcc_assert (parser->tokens[0].type != CPP_EOF);
776 gcc_assert (!parser->in_pragma || parser->tokens[0].type != CPP_PRAGMA_EOL);
777 gcc_assert (parser->error || parser->tokens[0].type != CPP_PRAGMA);
778 if (parser->tokens != &parser->tokens_buf[0])
779 parser->tokens++;
780 else if (parser->tokens_avail == 2)
781 parser->tokens[0] = parser->tokens[1];
782 parser->tokens_avail--;
783 }
784
785 /* Expect the current token to be a #pragma. Consume it and remember
786 that we've begun parsing a pragma. */
787
788 static void
789 c_parser_consume_pragma (c_parser *parser)
790 {
791 gcc_assert (!parser->in_pragma);
792 gcc_assert (parser->tokens_avail >= 1);
793 gcc_assert (parser->tokens[0].type == CPP_PRAGMA);
794 if (parser->tokens != &parser->tokens_buf[0])
795 parser->tokens++;
796 else if (parser->tokens_avail == 2)
797 parser->tokens[0] = parser->tokens[1];
798 parser->tokens_avail--;
799 parser->in_pragma = true;
800 }
801
802 /* Update the global input_location from TOKEN. */
803 static inline void
804 c_parser_set_source_position_from_token (c_token *token)
805 {
806 if (token->type != CPP_EOF)
807 {
808 input_location = token->location;
809 }
810 }
811
812 /* Issue a diagnostic of the form
813 FILE:LINE: MESSAGE before TOKEN
814 where TOKEN is the next token in the input stream of PARSER.
815 MESSAGE (specified by the caller) is usually of the form "expected
816 OTHER-TOKEN".
817
818 Do not issue a diagnostic if still recovering from an error.
819
820 ??? This is taken from the C++ parser, but building up messages in
821 this way is not i18n-friendly and some other approach should be
822 used. */
823
824 static void
825 c_parser_error (c_parser *parser, const char *gmsgid)
826 {
827 c_token *token = c_parser_peek_token (parser);
828 if (parser->error)
829 return;
830 parser->error = true;
831 if (!gmsgid)
832 return;
833 /* This diagnostic makes more sense if it is tagged to the line of
834 the token we just peeked at. */
835 c_parser_set_source_position_from_token (token);
836 c_parse_error (gmsgid,
837 /* Because c_parse_error does not understand
838 CPP_KEYWORD, keywords are treated like
839 identifiers. */
840 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
841 /* ??? The C parser does not save the cpp flags of a
842 token, we need to pass 0 here and we will not get
843 the source spelling of some tokens but rather the
844 canonical spelling. */
845 token->value, /*flags=*/0);
846 }
847
848 /* If the next token is of the indicated TYPE, consume it. Otherwise,
849 issue the error MSGID. If MSGID is NULL then a message has already
850 been produced and no message will be produced this time. Returns
851 true if found, false otherwise. */
852
853 static bool
854 c_parser_require (c_parser *parser,
855 enum cpp_ttype type,
856 const char *msgid)
857 {
858 if (c_parser_next_token_is (parser, type))
859 {
860 c_parser_consume_token (parser);
861 return true;
862 }
863 else
864 {
865 c_parser_error (parser, msgid);
866 return false;
867 }
868 }
869
870 /* If the next token is the indicated keyword, consume it. Otherwise,
871 issue the error MSGID. Returns true if found, false otherwise. */
872
873 static bool
874 c_parser_require_keyword (c_parser *parser,
875 enum rid keyword,
876 const char *msgid)
877 {
878 if (c_parser_next_token_is_keyword (parser, keyword))
879 {
880 c_parser_consume_token (parser);
881 return true;
882 }
883 else
884 {
885 c_parser_error (parser, msgid);
886 return false;
887 }
888 }
889
890 /* Like c_parser_require, except that tokens will be skipped until the
891 desired token is found. An error message is still produced if the
892 next token is not as expected. If MSGID is NULL then a message has
893 already been produced and no message will be produced this
894 time. */
895
896 static void
897 c_parser_skip_until_found (c_parser *parser,
898 enum cpp_ttype type,
899 const char *msgid)
900 {
901 unsigned nesting_depth = 0;
902
903 if (c_parser_require (parser, type, msgid))
904 return;
905
906 /* Skip tokens until the desired token is found. */
907 while (true)
908 {
909 /* Peek at the next token. */
910 c_token *token = c_parser_peek_token (parser);
911 /* If we've reached the token we want, consume it and stop. */
912 if (token->type == type && !nesting_depth)
913 {
914 c_parser_consume_token (parser);
915 break;
916 }
917
918 /* If we've run out of tokens, stop. */
919 if (token->type == CPP_EOF)
920 return;
921 if (token->type == CPP_PRAGMA_EOL && parser->in_pragma)
922 return;
923 if (token->type == CPP_OPEN_BRACE
924 || token->type == CPP_OPEN_PAREN
925 || token->type == CPP_OPEN_SQUARE)
926 ++nesting_depth;
927 else if (token->type == CPP_CLOSE_BRACE
928 || token->type == CPP_CLOSE_PAREN
929 || token->type == CPP_CLOSE_SQUARE)
930 {
931 if (nesting_depth-- == 0)
932 break;
933 }
934 /* Consume this token. */
935 c_parser_consume_token (parser);
936 }
937 parser->error = false;
938 }
939
940 /* Skip tokens until the end of a parameter is found, but do not
941 consume the comma, semicolon or closing delimiter. */
942
943 static void
944 c_parser_skip_to_end_of_parameter (c_parser *parser)
945 {
946 unsigned nesting_depth = 0;
947
948 while (true)
949 {
950 c_token *token = c_parser_peek_token (parser);
951 if ((token->type == CPP_COMMA || token->type == CPP_SEMICOLON)
952 && !nesting_depth)
953 break;
954 /* If we've run out of tokens, stop. */
955 if (token->type == CPP_EOF)
956 return;
957 if (token->type == CPP_PRAGMA_EOL && parser->in_pragma)
958 return;
959 if (token->type == CPP_OPEN_BRACE
960 || token->type == CPP_OPEN_PAREN
961 || token->type == CPP_OPEN_SQUARE)
962 ++nesting_depth;
963 else if (token->type == CPP_CLOSE_BRACE
964 || token->type == CPP_CLOSE_PAREN
965 || token->type == CPP_CLOSE_SQUARE)
966 {
967 if (nesting_depth-- == 0)
968 break;
969 }
970 /* Consume this token. */
971 c_parser_consume_token (parser);
972 }
973 parser->error = false;
974 }
975
976 /* Expect to be at the end of the pragma directive and consume an
977 end of line marker. */
978
979 static void
980 c_parser_skip_to_pragma_eol (c_parser *parser, bool error_if_not_eol = true)
981 {
982 gcc_assert (parser->in_pragma);
983 parser->in_pragma = false;
984
985 if (error_if_not_eol && c_parser_peek_token (parser)->type != CPP_PRAGMA_EOL)
986 c_parser_error (parser, "expected end of line");
987
988 cpp_ttype token_type;
989 do
990 {
991 c_token *token = c_parser_peek_token (parser);
992 token_type = token->type;
993 if (token_type == CPP_EOF)
994 break;
995 c_parser_consume_token (parser);
996 }
997 while (token_type != CPP_PRAGMA_EOL);
998
999 parser->error = false;
1000 }
1001
1002 /* Skip tokens until we have consumed an entire block, or until we
1003 have consumed a non-nested ';'. */
1004
1005 static void
1006 c_parser_skip_to_end_of_block_or_statement (c_parser *parser)
1007 {
1008 unsigned nesting_depth = 0;
1009 bool save_error = parser->error;
1010
1011 while (true)
1012 {
1013 c_token *token;
1014
1015 /* Peek at the next token. */
1016 token = c_parser_peek_token (parser);
1017
1018 switch (token->type)
1019 {
1020 case CPP_EOF:
1021 return;
1022
1023 case CPP_PRAGMA_EOL:
1024 if (parser->in_pragma)
1025 return;
1026 break;
1027
1028 case CPP_SEMICOLON:
1029 /* If the next token is a ';', we have reached the
1030 end of the statement. */
1031 if (!nesting_depth)
1032 {
1033 /* Consume the ';'. */
1034 c_parser_consume_token (parser);
1035 goto finished;
1036 }
1037 break;
1038
1039 case CPP_CLOSE_BRACE:
1040 /* If the next token is a non-nested '}', then we have
1041 reached the end of the current block. */
1042 if (nesting_depth == 0 || --nesting_depth == 0)
1043 {
1044 c_parser_consume_token (parser);
1045 goto finished;
1046 }
1047 break;
1048
1049 case CPP_OPEN_BRACE:
1050 /* If it the next token is a '{', then we are entering a new
1051 block. Consume the entire block. */
1052 ++nesting_depth;
1053 break;
1054
1055 case CPP_PRAGMA:
1056 /* If we see a pragma, consume the whole thing at once. We
1057 have some safeguards against consuming pragmas willy-nilly.
1058 Normally, we'd expect to be here with parser->error set,
1059 which disables these safeguards. But it's possible to get
1060 here for secondary error recovery, after parser->error has
1061 been cleared. */
1062 c_parser_consume_pragma (parser);
1063 c_parser_skip_to_pragma_eol (parser);
1064 parser->error = save_error;
1065 continue;
1066
1067 default:
1068 break;
1069 }
1070
1071 c_parser_consume_token (parser);
1072 }
1073
1074 finished:
1075 parser->error = false;
1076 }
1077
1078 /* CPP's options (initialized by c-opts.c). */
1079 extern cpp_options *cpp_opts;
1080
1081 /* Save the warning flags which are controlled by __extension__. */
1082
1083 static inline int
1084 disable_extension_diagnostics (void)
1085 {
1086 int ret = (pedantic
1087 | (warn_pointer_arith << 1)
1088 | (warn_traditional << 2)
1089 | (flag_iso << 3)
1090 | (warn_long_long << 4)
1091 | (warn_cxx_compat << 5)
1092 | (warn_overlength_strings << 6)
1093 /* warn_c90_c99_compat has three states: -1/0/1, so we must
1094 play tricks to properly restore it. */
1095 | ((warn_c90_c99_compat == 1) << 7)
1096 | ((warn_c90_c99_compat == -1) << 8)
1097 /* Similarly for warn_c99_c11_compat. */
1098 | ((warn_c99_c11_compat == 1) << 9)
1099 | ((warn_c99_c11_compat == -1) << 10)
1100 );
1101 cpp_opts->cpp_pedantic = pedantic = 0;
1102 warn_pointer_arith = 0;
1103 cpp_opts->cpp_warn_traditional = warn_traditional = 0;
1104 flag_iso = 0;
1105 cpp_opts->cpp_warn_long_long = warn_long_long = 0;
1106 warn_cxx_compat = 0;
1107 warn_overlength_strings = 0;
1108 warn_c90_c99_compat = 0;
1109 warn_c99_c11_compat = 0;
1110 return ret;
1111 }
1112
1113 /* Restore the warning flags which are controlled by __extension__.
1114 FLAGS is the return value from disable_extension_diagnostics. */
1115
1116 static inline void
1117 restore_extension_diagnostics (int flags)
1118 {
1119 cpp_opts->cpp_pedantic = pedantic = flags & 1;
1120 warn_pointer_arith = (flags >> 1) & 1;
1121 cpp_opts->cpp_warn_traditional = warn_traditional = (flags >> 2) & 1;
1122 flag_iso = (flags >> 3) & 1;
1123 cpp_opts->cpp_warn_long_long = warn_long_long = (flags >> 4) & 1;
1124 warn_cxx_compat = (flags >> 5) & 1;
1125 warn_overlength_strings = (flags >> 6) & 1;
1126 /* See above for why is this needed. */
1127 warn_c90_c99_compat = (flags >> 7) & 1 ? 1 : ((flags >> 8) & 1 ? -1 : 0);
1128 warn_c99_c11_compat = (flags >> 9) & 1 ? 1 : ((flags >> 10) & 1 ? -1 : 0);
1129 }
1130
1131 /* Possibly kinds of declarator to parse. */
1132 typedef enum c_dtr_syn {
1133 /* A normal declarator with an identifier. */
1134 C_DTR_NORMAL,
1135 /* An abstract declarator (maybe empty). */
1136 C_DTR_ABSTRACT,
1137 /* A parameter declarator: may be either, but after a type name does
1138 not redeclare a typedef name as an identifier if it can
1139 alternatively be interpreted as a typedef name; see DR#009,
1140 applied in C90 TC1, omitted from C99 and reapplied in C99 TC2
1141 following DR#249. For example, given a typedef T, "int T" and
1142 "int *T" are valid parameter declarations redeclaring T, while
1143 "int (T)" and "int * (T)" and "int (T[])" and "int (T (int))" are
1144 abstract declarators rather than involving redundant parentheses;
1145 the same applies with attributes inside the parentheses before
1146 "T". */
1147 C_DTR_PARM
1148 } c_dtr_syn;
1149
1150 /* The binary operation precedence levels, where 0 is a dummy lowest level
1151 used for the bottom of the stack. */
1152 enum c_parser_prec {
1153 PREC_NONE,
1154 PREC_LOGOR,
1155 PREC_LOGAND,
1156 PREC_BITOR,
1157 PREC_BITXOR,
1158 PREC_BITAND,
1159 PREC_EQ,
1160 PREC_REL,
1161 PREC_SHIFT,
1162 PREC_ADD,
1163 PREC_MULT,
1164 NUM_PRECS
1165 };
1166
1167 static void c_parser_external_declaration (c_parser *);
1168 static void c_parser_asm_definition (c_parser *);
1169 static void c_parser_declaration_or_fndef (c_parser *, bool, bool, bool,
1170 bool, bool, tree *, vec<c_token>);
1171 static void c_parser_static_assert_declaration_no_semi (c_parser *);
1172 static void c_parser_static_assert_declaration (c_parser *);
1173 static void c_parser_declspecs (c_parser *, struct c_declspecs *, bool, bool,
1174 bool, bool, bool, enum c_lookahead_kind);
1175 static struct c_typespec c_parser_enum_specifier (c_parser *);
1176 static struct c_typespec c_parser_struct_or_union_specifier (c_parser *);
1177 static tree c_parser_struct_declaration (c_parser *);
1178 static struct c_typespec c_parser_typeof_specifier (c_parser *);
1179 static tree c_parser_alignas_specifier (c_parser *);
1180 static struct c_declarator *c_parser_declarator (c_parser *, bool, c_dtr_syn,
1181 bool *);
1182 static struct c_declarator *c_parser_direct_declarator (c_parser *, bool,
1183 c_dtr_syn, bool *);
1184 static struct c_declarator *c_parser_direct_declarator_inner (c_parser *,
1185 bool,
1186 struct c_declarator *);
1187 static struct c_arg_info *c_parser_parms_declarator (c_parser *, bool, tree);
1188 static struct c_arg_info *c_parser_parms_list_declarator (c_parser *, tree,
1189 tree);
1190 static struct c_parm *c_parser_parameter_declaration (c_parser *, tree);
1191 static tree c_parser_simple_asm_expr (c_parser *);
1192 static tree c_parser_attributes (c_parser *);
1193 static struct c_type_name *c_parser_type_name (c_parser *);
1194 static struct c_expr c_parser_initializer (c_parser *);
1195 static struct c_expr c_parser_braced_init (c_parser *, tree, bool);
1196 static void c_parser_initelt (c_parser *, struct obstack *);
1197 static void c_parser_initval (c_parser *, struct c_expr *,
1198 struct obstack *);
1199 static tree c_parser_compound_statement (c_parser *);
1200 static void c_parser_compound_statement_nostart (c_parser *);
1201 static void c_parser_label (c_parser *);
1202 static void c_parser_statement (c_parser *);
1203 static void c_parser_statement_after_labels (c_parser *);
1204 static void c_parser_if_statement (c_parser *);
1205 static void c_parser_switch_statement (c_parser *);
1206 static void c_parser_while_statement (c_parser *, bool);
1207 static void c_parser_do_statement (c_parser *, bool);
1208 static void c_parser_for_statement (c_parser *, bool);
1209 static tree c_parser_asm_statement (c_parser *);
1210 static tree c_parser_asm_operands (c_parser *);
1211 static tree c_parser_asm_goto_operands (c_parser *);
1212 static tree c_parser_asm_clobbers (c_parser *);
1213 static struct c_expr c_parser_expr_no_commas (c_parser *, struct c_expr *,
1214 tree = NULL_TREE);
1215 static struct c_expr c_parser_conditional_expression (c_parser *,
1216 struct c_expr *, tree);
1217 static struct c_expr c_parser_binary_expression (c_parser *, struct c_expr *,
1218 tree);
1219 static struct c_expr c_parser_cast_expression (c_parser *, struct c_expr *);
1220 static struct c_expr c_parser_unary_expression (c_parser *);
1221 static struct c_expr c_parser_sizeof_expression (c_parser *);
1222 static struct c_expr c_parser_alignof_expression (c_parser *);
1223 static struct c_expr c_parser_postfix_expression (c_parser *);
1224 static struct c_expr c_parser_postfix_expression_after_paren_type (c_parser *,
1225 struct c_type_name *,
1226 location_t);
1227 static struct c_expr c_parser_postfix_expression_after_primary (c_parser *,
1228 location_t loc,
1229 struct c_expr);
1230 static tree c_parser_transaction (c_parser *, enum rid);
1231 static struct c_expr c_parser_transaction_expression (c_parser *, enum rid);
1232 static tree c_parser_transaction_cancel (c_parser *);
1233 static struct c_expr c_parser_expression (c_parser *);
1234 static struct c_expr c_parser_expression_conv (c_parser *);
1235 static vec<tree, va_gc> *c_parser_expr_list (c_parser *, bool, bool,
1236 vec<tree, va_gc> **, location_t *,
1237 tree *, vec<location_t> *,
1238 unsigned int * = NULL);
1239 static void c_parser_oacc_enter_exit_data (c_parser *, bool);
1240 static void c_parser_oacc_update (c_parser *);
1241 static tree c_parser_oacc_loop (location_t, c_parser *, char *);
1242 static void c_parser_omp_construct (c_parser *);
1243 static void c_parser_omp_threadprivate (c_parser *);
1244 static void c_parser_omp_barrier (c_parser *);
1245 static void c_parser_omp_flush (c_parser *);
1246 static tree c_parser_omp_for_loop (location_t, c_parser *, enum tree_code,
1247 tree, tree *);
1248 static void c_parser_omp_taskwait (c_parser *);
1249 static void c_parser_omp_taskyield (c_parser *);
1250 static void c_parser_omp_cancel (c_parser *);
1251 static void c_parser_omp_cancellation_point (c_parser *);
1252
1253 enum pragma_context { pragma_external, pragma_struct, pragma_param,
1254 pragma_stmt, pragma_compound };
1255 static bool c_parser_pragma (c_parser *, enum pragma_context);
1256 static bool c_parser_omp_target (c_parser *, enum pragma_context);
1257 static void c_parser_omp_end_declare_target (c_parser *);
1258 static void c_parser_omp_declare (c_parser *, enum pragma_context);
1259
1260 /* These Objective-C parser functions are only ever called when
1261 compiling Objective-C. */
1262 static void c_parser_objc_class_definition (c_parser *, tree);
1263 static void c_parser_objc_class_instance_variables (c_parser *);
1264 static void c_parser_objc_class_declaration (c_parser *);
1265 static void c_parser_objc_alias_declaration (c_parser *);
1266 static void c_parser_objc_protocol_definition (c_parser *, tree);
1267 static bool c_parser_objc_method_type (c_parser *);
1268 static void c_parser_objc_method_definition (c_parser *);
1269 static void c_parser_objc_methodprotolist (c_parser *);
1270 static void c_parser_objc_methodproto (c_parser *);
1271 static tree c_parser_objc_method_decl (c_parser *, bool, tree *, tree *);
1272 static tree c_parser_objc_type_name (c_parser *);
1273 static tree c_parser_objc_protocol_refs (c_parser *);
1274 static void c_parser_objc_try_catch_finally_statement (c_parser *);
1275 static void c_parser_objc_synchronized_statement (c_parser *);
1276 static tree c_parser_objc_selector (c_parser *);
1277 static tree c_parser_objc_selector_arg (c_parser *);
1278 static tree c_parser_objc_receiver (c_parser *);
1279 static tree c_parser_objc_message_args (c_parser *);
1280 static tree c_parser_objc_keywordexpr (c_parser *);
1281 static void c_parser_objc_at_property_declaration (c_parser *);
1282 static void c_parser_objc_at_synthesize_declaration (c_parser *);
1283 static void c_parser_objc_at_dynamic_declaration (c_parser *);
1284 static bool c_parser_objc_diagnose_bad_element_prefix
1285 (c_parser *, struct c_declspecs *);
1286
1287 /* Cilk Plus supporting routines. */
1288 static void c_parser_cilk_simd (c_parser *);
1289 static void c_parser_cilk_for (c_parser *, tree);
1290 static bool c_parser_cilk_verify_simd (c_parser *, enum pragma_context);
1291 static tree c_parser_array_notation (location_t, c_parser *, tree, tree);
1292 static tree c_parser_cilk_clause_vectorlength (c_parser *, tree, bool);
1293 static void c_parser_cilk_grainsize (c_parser *);
1294
1295 /* Parse a translation unit (C90 6.7, C99 6.9).
1296
1297 translation-unit:
1298 external-declarations
1299
1300 external-declarations:
1301 external-declaration
1302 external-declarations external-declaration
1303
1304 GNU extensions:
1305
1306 translation-unit:
1307 empty
1308 */
1309
1310 static void
1311 c_parser_translation_unit (c_parser *parser)
1312 {
1313 if (c_parser_next_token_is (parser, CPP_EOF))
1314 {
1315 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
1316 "ISO C forbids an empty translation unit");
1317 }
1318 else
1319 {
1320 void *obstack_position = obstack_alloc (&parser_obstack, 0);
1321 mark_valid_location_for_stdc_pragma (false);
1322 do
1323 {
1324 ggc_collect ();
1325 c_parser_external_declaration (parser);
1326 obstack_free (&parser_obstack, obstack_position);
1327 }
1328 while (c_parser_next_token_is_not (parser, CPP_EOF));
1329 }
1330 }
1331
1332 /* Parse an external declaration (C90 6.7, C99 6.9).
1333
1334 external-declaration:
1335 function-definition
1336 declaration
1337
1338 GNU extensions:
1339
1340 external-declaration:
1341 asm-definition
1342 ;
1343 __extension__ external-declaration
1344
1345 Objective-C:
1346
1347 external-declaration:
1348 objc-class-definition
1349 objc-class-declaration
1350 objc-alias-declaration
1351 objc-protocol-definition
1352 objc-method-definition
1353 @end
1354 */
1355
1356 static void
1357 c_parser_external_declaration (c_parser *parser)
1358 {
1359 int ext;
1360 switch (c_parser_peek_token (parser)->type)
1361 {
1362 case CPP_KEYWORD:
1363 switch (c_parser_peek_token (parser)->keyword)
1364 {
1365 case RID_EXTENSION:
1366 ext = disable_extension_diagnostics ();
1367 c_parser_consume_token (parser);
1368 c_parser_external_declaration (parser);
1369 restore_extension_diagnostics (ext);
1370 break;
1371 case RID_ASM:
1372 c_parser_asm_definition (parser);
1373 break;
1374 case RID_AT_INTERFACE:
1375 case RID_AT_IMPLEMENTATION:
1376 gcc_assert (c_dialect_objc ());
1377 c_parser_objc_class_definition (parser, NULL_TREE);
1378 break;
1379 case RID_AT_CLASS:
1380 gcc_assert (c_dialect_objc ());
1381 c_parser_objc_class_declaration (parser);
1382 break;
1383 case RID_AT_ALIAS:
1384 gcc_assert (c_dialect_objc ());
1385 c_parser_objc_alias_declaration (parser);
1386 break;
1387 case RID_AT_PROTOCOL:
1388 gcc_assert (c_dialect_objc ());
1389 c_parser_objc_protocol_definition (parser, NULL_TREE);
1390 break;
1391 case RID_AT_PROPERTY:
1392 gcc_assert (c_dialect_objc ());
1393 c_parser_objc_at_property_declaration (parser);
1394 break;
1395 case RID_AT_SYNTHESIZE:
1396 gcc_assert (c_dialect_objc ());
1397 c_parser_objc_at_synthesize_declaration (parser);
1398 break;
1399 case RID_AT_DYNAMIC:
1400 gcc_assert (c_dialect_objc ());
1401 c_parser_objc_at_dynamic_declaration (parser);
1402 break;
1403 case RID_AT_END:
1404 gcc_assert (c_dialect_objc ());
1405 c_parser_consume_token (parser);
1406 objc_finish_implementation ();
1407 break;
1408 default:
1409 goto decl_or_fndef;
1410 }
1411 break;
1412 case CPP_SEMICOLON:
1413 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
1414 "ISO C does not allow extra %<;%> outside of a function");
1415 c_parser_consume_token (parser);
1416 break;
1417 case CPP_PRAGMA:
1418 mark_valid_location_for_stdc_pragma (true);
1419 c_parser_pragma (parser, pragma_external);
1420 mark_valid_location_for_stdc_pragma (false);
1421 break;
1422 case CPP_PLUS:
1423 case CPP_MINUS:
1424 if (c_dialect_objc ())
1425 {
1426 c_parser_objc_method_definition (parser);
1427 break;
1428 }
1429 /* Else fall through, and yield a syntax error trying to parse
1430 as a declaration or function definition. */
1431 default:
1432 decl_or_fndef:
1433 /* A declaration or a function definition (or, in Objective-C,
1434 an @interface or @protocol with prefix attributes). We can
1435 only tell which after parsing the declaration specifiers, if
1436 any, and the first declarator. */
1437 c_parser_declaration_or_fndef (parser, true, true, true, false, true,
1438 NULL, vNULL);
1439 break;
1440 }
1441 }
1442
1443 static void c_finish_omp_declare_simd (c_parser *, tree, tree, vec<c_token>);
1444
1445 /* Parse a declaration or function definition (C90 6.5, 6.7.1, C99
1446 6.7, 6.9.1). If FNDEF_OK is true, a function definition is
1447 accepted; otherwise (old-style parameter declarations) only other
1448 declarations are accepted. If STATIC_ASSERT_OK is true, a static
1449 assertion is accepted; otherwise (old-style parameter declarations)
1450 it is not. If NESTED is true, we are inside a function or parsing
1451 old-style parameter declarations; any functions encountered are
1452 nested functions and declaration specifiers are required; otherwise
1453 we are at top level and functions are normal functions and
1454 declaration specifiers may be optional. If EMPTY_OK is true, empty
1455 declarations are OK (subject to all other constraints); otherwise
1456 (old-style parameter declarations) they are diagnosed. If
1457 START_ATTR_OK is true, the declaration specifiers may start with
1458 attributes; otherwise they may not.
1459 OBJC_FOREACH_OBJECT_DECLARATION can be used to get back the parsed
1460 declaration when parsing an Objective-C foreach statement.
1461
1462 declaration:
1463 declaration-specifiers init-declarator-list[opt] ;
1464 static_assert-declaration
1465
1466 function-definition:
1467 declaration-specifiers[opt] declarator declaration-list[opt]
1468 compound-statement
1469
1470 declaration-list:
1471 declaration
1472 declaration-list declaration
1473
1474 init-declarator-list:
1475 init-declarator
1476 init-declarator-list , init-declarator
1477
1478 init-declarator:
1479 declarator simple-asm-expr[opt] attributes[opt]
1480 declarator simple-asm-expr[opt] attributes[opt] = initializer
1481
1482 GNU extensions:
1483
1484 nested-function-definition:
1485 declaration-specifiers declarator declaration-list[opt]
1486 compound-statement
1487
1488 Objective-C:
1489 attributes objc-class-definition
1490 attributes objc-category-definition
1491 attributes objc-protocol-definition
1492
1493 The simple-asm-expr and attributes are GNU extensions.
1494
1495 This function does not handle __extension__; that is handled in its
1496 callers. ??? Following the old parser, __extension__ may start
1497 external declarations, declarations in functions and declarations
1498 at the start of "for" loops, but not old-style parameter
1499 declarations.
1500
1501 C99 requires declaration specifiers in a function definition; the
1502 absence is diagnosed through the diagnosis of implicit int. In GNU
1503 C we also allow but diagnose declarations without declaration
1504 specifiers, but only at top level (elsewhere they conflict with
1505 other syntax).
1506
1507 In Objective-C, declarations of the looping variable in a foreach
1508 statement are exceptionally terminated by 'in' (for example, 'for
1509 (NSObject *object in array) { ... }').
1510
1511 OpenMP:
1512
1513 declaration:
1514 threadprivate-directive */
1515
1516 static void
1517 c_parser_declaration_or_fndef (c_parser *parser, bool fndef_ok,
1518 bool static_assert_ok, bool empty_ok,
1519 bool nested, bool start_attr_ok,
1520 tree *objc_foreach_object_declaration,
1521 vec<c_token> omp_declare_simd_clauses)
1522 {
1523 struct c_declspecs *specs;
1524 tree prefix_attrs;
1525 tree all_prefix_attrs;
1526 bool diagnosed_no_specs = false;
1527 location_t here = c_parser_peek_token (parser)->location;
1528
1529 if (static_assert_ok
1530 && c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT))
1531 {
1532 c_parser_static_assert_declaration (parser);
1533 return;
1534 }
1535 specs = build_null_declspecs ();
1536
1537 /* Try to detect an unknown type name when we have "A B" or "A *B". */
1538 if (c_parser_peek_token (parser)->type == CPP_NAME
1539 && c_parser_peek_token (parser)->id_kind == C_ID_ID
1540 && (c_parser_peek_2nd_token (parser)->type == CPP_NAME
1541 || c_parser_peek_2nd_token (parser)->type == CPP_MULT)
1542 && (!nested || !lookup_name (c_parser_peek_token (parser)->value)))
1543 {
1544 error_at (here, "unknown type name %qE",
1545 c_parser_peek_token (parser)->value);
1546
1547 /* Parse declspecs normally to get a correct pointer type, but avoid
1548 a further "fails to be a type name" error. Refuse nested functions
1549 since it is not how the user likely wants us to recover. */
1550 c_parser_peek_token (parser)->type = CPP_KEYWORD;
1551 c_parser_peek_token (parser)->keyword = RID_VOID;
1552 c_parser_peek_token (parser)->value = error_mark_node;
1553 fndef_ok = !nested;
1554 }
1555
1556 c_parser_declspecs (parser, specs, true, true, start_attr_ok,
1557 true, true, cla_nonabstract_decl);
1558 if (parser->error)
1559 {
1560 c_parser_skip_to_end_of_block_or_statement (parser);
1561 return;
1562 }
1563 if (nested && !specs->declspecs_seen_p)
1564 {
1565 c_parser_error (parser, "expected declaration specifiers");
1566 c_parser_skip_to_end_of_block_or_statement (parser);
1567 return;
1568 }
1569 finish_declspecs (specs);
1570 bool auto_type_p = specs->typespec_word == cts_auto_type;
1571 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1572 {
1573 if (auto_type_p)
1574 error_at (here, "%<__auto_type%> in empty declaration");
1575 else if (empty_ok)
1576 shadow_tag (specs);
1577 else
1578 {
1579 shadow_tag_warned (specs, 1);
1580 pedwarn (here, 0, "empty declaration");
1581 }
1582 c_parser_consume_token (parser);
1583 return;
1584 }
1585
1586 /* Provide better error recovery. Note that a type name here is usually
1587 better diagnosed as a redeclaration. */
1588 if (empty_ok
1589 && specs->typespec_kind == ctsk_tagdef
1590 && c_parser_next_token_starts_declspecs (parser)
1591 && !c_parser_next_token_is (parser, CPP_NAME))
1592 {
1593 c_parser_error (parser, "expected %<;%>, identifier or %<(%>");
1594 parser->error = false;
1595 shadow_tag_warned (specs, 1);
1596 return;
1597 }
1598 else if (c_dialect_objc () && !auto_type_p)
1599 {
1600 /* Prefix attributes are an error on method decls. */
1601 switch (c_parser_peek_token (parser)->type)
1602 {
1603 case CPP_PLUS:
1604 case CPP_MINUS:
1605 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1606 return;
1607 if (specs->attrs)
1608 {
1609 warning_at (c_parser_peek_token (parser)->location,
1610 OPT_Wattributes,
1611 "prefix attributes are ignored for methods");
1612 specs->attrs = NULL_TREE;
1613 }
1614 if (fndef_ok)
1615 c_parser_objc_method_definition (parser);
1616 else
1617 c_parser_objc_methodproto (parser);
1618 return;
1619 break;
1620 default:
1621 break;
1622 }
1623 /* This is where we parse 'attributes @interface ...',
1624 'attributes @implementation ...', 'attributes @protocol ...'
1625 (where attributes could be, for example, __attribute__
1626 ((deprecated)).
1627 */
1628 switch (c_parser_peek_token (parser)->keyword)
1629 {
1630 case RID_AT_INTERFACE:
1631 {
1632 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1633 return;
1634 c_parser_objc_class_definition (parser, specs->attrs);
1635 return;
1636 }
1637 break;
1638 case RID_AT_IMPLEMENTATION:
1639 {
1640 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1641 return;
1642 if (specs->attrs)
1643 {
1644 warning_at (c_parser_peek_token (parser)->location,
1645 OPT_Wattributes,
1646 "prefix attributes are ignored for implementations");
1647 specs->attrs = NULL_TREE;
1648 }
1649 c_parser_objc_class_definition (parser, NULL_TREE);
1650 return;
1651 }
1652 break;
1653 case RID_AT_PROTOCOL:
1654 {
1655 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1656 return;
1657 c_parser_objc_protocol_definition (parser, specs->attrs);
1658 return;
1659 }
1660 break;
1661 case RID_AT_ALIAS:
1662 case RID_AT_CLASS:
1663 case RID_AT_END:
1664 case RID_AT_PROPERTY:
1665 if (specs->attrs)
1666 {
1667 c_parser_error (parser, "unexpected attribute");
1668 specs->attrs = NULL;
1669 }
1670 break;
1671 default:
1672 break;
1673 }
1674 }
1675
1676 pending_xref_error ();
1677 prefix_attrs = specs->attrs;
1678 all_prefix_attrs = prefix_attrs;
1679 specs->attrs = NULL_TREE;
1680 while (true)
1681 {
1682 struct c_declarator *declarator;
1683 bool dummy = false;
1684 timevar_id_t tv;
1685 tree fnbody;
1686 /* Declaring either one or more declarators (in which case we
1687 should diagnose if there were no declaration specifiers) or a
1688 function definition (in which case the diagnostic for
1689 implicit int suffices). */
1690 declarator = c_parser_declarator (parser,
1691 specs->typespec_kind != ctsk_none,
1692 C_DTR_NORMAL, &dummy);
1693 if (declarator == NULL)
1694 {
1695 if (omp_declare_simd_clauses.exists ()
1696 || !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
1697 c_finish_omp_declare_simd (parser, NULL_TREE, NULL_TREE,
1698 omp_declare_simd_clauses);
1699 c_parser_skip_to_end_of_block_or_statement (parser);
1700 return;
1701 }
1702 if (auto_type_p && declarator->kind != cdk_id)
1703 {
1704 error_at (here,
1705 "%<__auto_type%> requires a plain identifier"
1706 " as declarator");
1707 c_parser_skip_to_end_of_block_or_statement (parser);
1708 return;
1709 }
1710 if (c_parser_next_token_is (parser, CPP_EQ)
1711 || c_parser_next_token_is (parser, CPP_COMMA)
1712 || c_parser_next_token_is (parser, CPP_SEMICOLON)
1713 || c_parser_next_token_is_keyword (parser, RID_ASM)
1714 || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE)
1715 || c_parser_next_token_is_keyword (parser, RID_IN))
1716 {
1717 tree asm_name = NULL_TREE;
1718 tree postfix_attrs = NULL_TREE;
1719 if (!diagnosed_no_specs && !specs->declspecs_seen_p)
1720 {
1721 diagnosed_no_specs = true;
1722 pedwarn (here, 0, "data definition has no type or storage class");
1723 }
1724 /* Having seen a data definition, there cannot now be a
1725 function definition. */
1726 fndef_ok = false;
1727 if (c_parser_next_token_is_keyword (parser, RID_ASM))
1728 asm_name = c_parser_simple_asm_expr (parser);
1729 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1730 {
1731 postfix_attrs = c_parser_attributes (parser);
1732 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
1733 {
1734 /* This means there is an attribute specifier after
1735 the declarator in a function definition. Provide
1736 some more information for the user. */
1737 error_at (here, "attributes should be specified before the "
1738 "declarator in a function definition");
1739 c_parser_skip_to_end_of_block_or_statement (parser);
1740 return;
1741 }
1742 }
1743 if (c_parser_next_token_is (parser, CPP_EQ))
1744 {
1745 tree d;
1746 struct c_expr init;
1747 location_t init_loc;
1748 c_parser_consume_token (parser);
1749 if (auto_type_p)
1750 {
1751 start_init (NULL_TREE, asm_name, global_bindings_p ());
1752 init_loc = c_parser_peek_token (parser)->location;
1753 init = c_parser_expr_no_commas (parser, NULL);
1754 if (TREE_CODE (init.value) == COMPONENT_REF
1755 && DECL_C_BIT_FIELD (TREE_OPERAND (init.value, 1)))
1756 error_at (here,
1757 "%<__auto_type%> used with a bit-field"
1758 " initializer");
1759 init = convert_lvalue_to_rvalue (init_loc, init, true, true);
1760 tree init_type = TREE_TYPE (init.value);
1761 /* As with typeof, remove all qualifiers from atomic types. */
1762 if (init_type != error_mark_node && TYPE_ATOMIC (init_type))
1763 init_type
1764 = c_build_qualified_type (init_type, TYPE_UNQUALIFIED);
1765 bool vm_type = variably_modified_type_p (init_type,
1766 NULL_TREE);
1767 if (vm_type)
1768 init.value = c_save_expr (init.value);
1769 finish_init ();
1770 specs->typespec_kind = ctsk_typeof;
1771 specs->locations[cdw_typedef] = init_loc;
1772 specs->typedef_p = true;
1773 specs->type = init_type;
1774 if (vm_type)
1775 {
1776 bool maybe_const = true;
1777 tree type_expr = c_fully_fold (init.value, false,
1778 &maybe_const);
1779 specs->expr_const_operands &= maybe_const;
1780 if (specs->expr)
1781 specs->expr = build2 (COMPOUND_EXPR,
1782 TREE_TYPE (type_expr),
1783 specs->expr, type_expr);
1784 else
1785 specs->expr = type_expr;
1786 }
1787 d = start_decl (declarator, specs, true,
1788 chainon (postfix_attrs, all_prefix_attrs));
1789 if (!d)
1790 d = error_mark_node;
1791 if (omp_declare_simd_clauses.exists ()
1792 || !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
1793 c_finish_omp_declare_simd (parser, d, NULL_TREE,
1794 omp_declare_simd_clauses);
1795 }
1796 else
1797 {
1798 /* The declaration of the variable is in effect while
1799 its initializer is parsed. */
1800 d = start_decl (declarator, specs, true,
1801 chainon (postfix_attrs, all_prefix_attrs));
1802 if (!d)
1803 d = error_mark_node;
1804 if (omp_declare_simd_clauses.exists ()
1805 || !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
1806 c_finish_omp_declare_simd (parser, d, NULL_TREE,
1807 omp_declare_simd_clauses);
1808 start_init (d, asm_name, global_bindings_p ());
1809 init_loc = c_parser_peek_token (parser)->location;
1810 init = c_parser_initializer (parser);
1811 finish_init ();
1812 }
1813 if (d != error_mark_node)
1814 {
1815 maybe_warn_string_init (init_loc, TREE_TYPE (d), init);
1816 finish_decl (d, init_loc, init.value,
1817 init.original_type, asm_name);
1818 }
1819 }
1820 else
1821 {
1822 if (auto_type_p)
1823 {
1824 error_at (here,
1825 "%<__auto_type%> requires an initialized "
1826 "data declaration");
1827 c_parser_skip_to_end_of_block_or_statement (parser);
1828 return;
1829 }
1830 tree d = start_decl (declarator, specs, false,
1831 chainon (postfix_attrs,
1832 all_prefix_attrs));
1833 if (omp_declare_simd_clauses.exists ()
1834 || !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
1835 {
1836 tree parms = NULL_TREE;
1837 if (d && TREE_CODE (d) == FUNCTION_DECL)
1838 {
1839 struct c_declarator *ce = declarator;
1840 while (ce != NULL)
1841 if (ce->kind == cdk_function)
1842 {
1843 parms = ce->u.arg_info->parms;
1844 break;
1845 }
1846 else
1847 ce = ce->declarator;
1848 }
1849 if (parms)
1850 temp_store_parm_decls (d, parms);
1851 c_finish_omp_declare_simd (parser, d, parms,
1852 omp_declare_simd_clauses);
1853 if (parms)
1854 temp_pop_parm_decls ();
1855 }
1856 if (d)
1857 finish_decl (d, UNKNOWN_LOCATION, NULL_TREE,
1858 NULL_TREE, asm_name);
1859
1860 if (c_parser_next_token_is_keyword (parser, RID_IN))
1861 {
1862 if (d)
1863 *objc_foreach_object_declaration = d;
1864 else
1865 *objc_foreach_object_declaration = error_mark_node;
1866 }
1867 }
1868 if (c_parser_next_token_is (parser, CPP_COMMA))
1869 {
1870 if (auto_type_p)
1871 {
1872 error_at (here,
1873 "%<__auto_type%> may only be used with"
1874 " a single declarator");
1875 c_parser_skip_to_end_of_block_or_statement (parser);
1876 return;
1877 }
1878 c_parser_consume_token (parser);
1879 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1880 all_prefix_attrs = chainon (c_parser_attributes (parser),
1881 prefix_attrs);
1882 else
1883 all_prefix_attrs = prefix_attrs;
1884 continue;
1885 }
1886 else if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1887 {
1888 c_parser_consume_token (parser);
1889 return;
1890 }
1891 else if (c_parser_next_token_is_keyword (parser, RID_IN))
1892 {
1893 /* This can only happen in Objective-C: we found the
1894 'in' that terminates the declaration inside an
1895 Objective-C foreach statement. Do not consume the
1896 token, so that the caller can use it to determine
1897 that this indeed is a foreach context. */
1898 return;
1899 }
1900 else
1901 {
1902 c_parser_error (parser, "expected %<,%> or %<;%>");
1903 c_parser_skip_to_end_of_block_or_statement (parser);
1904 return;
1905 }
1906 }
1907 else if (auto_type_p)
1908 {
1909 error_at (here,
1910 "%<__auto_type%> requires an initialized data declaration");
1911 c_parser_skip_to_end_of_block_or_statement (parser);
1912 return;
1913 }
1914 else if (!fndef_ok)
1915 {
1916 c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, "
1917 "%<asm%> or %<__attribute__%>");
1918 c_parser_skip_to_end_of_block_or_statement (parser);
1919 return;
1920 }
1921 /* Function definition (nested or otherwise). */
1922 if (nested)
1923 {
1924 pedwarn (here, OPT_Wpedantic, "ISO C forbids nested functions");
1925 c_push_function_context ();
1926 }
1927 if (!start_function (specs, declarator, all_prefix_attrs))
1928 {
1929 /* This can appear in many cases looking nothing like a
1930 function definition, so we don't give a more specific
1931 error suggesting there was one. */
1932 c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, %<asm%> "
1933 "or %<__attribute__%>");
1934 if (nested)
1935 c_pop_function_context ();
1936 break;
1937 }
1938
1939 if (DECL_DECLARED_INLINE_P (current_function_decl))
1940 tv = TV_PARSE_INLINE;
1941 else
1942 tv = TV_PARSE_FUNC;
1943 timevar_push (tv);
1944
1945 /* Parse old-style parameter declarations. ??? Attributes are
1946 not allowed to start declaration specifiers here because of a
1947 syntax conflict between a function declaration with attribute
1948 suffix and a function definition with an attribute prefix on
1949 first old-style parameter declaration. Following the old
1950 parser, they are not accepted on subsequent old-style
1951 parameter declarations either. However, there is no
1952 ambiguity after the first declaration, nor indeed on the
1953 first as long as we don't allow postfix attributes after a
1954 declarator with a nonempty identifier list in a definition;
1955 and postfix attributes have never been accepted here in
1956 function definitions either. */
1957 while (c_parser_next_token_is_not (parser, CPP_EOF)
1958 && c_parser_next_token_is_not (parser, CPP_OPEN_BRACE))
1959 c_parser_declaration_or_fndef (parser, false, false, false,
1960 true, false, NULL, vNULL);
1961 store_parm_decls ();
1962 if (omp_declare_simd_clauses.exists ()
1963 || !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
1964 c_finish_omp_declare_simd (parser, current_function_decl, NULL_TREE,
1965 omp_declare_simd_clauses);
1966 DECL_STRUCT_FUNCTION (current_function_decl)->function_start_locus
1967 = c_parser_peek_token (parser)->location;
1968 fnbody = c_parser_compound_statement (parser);
1969 if (flag_cilkplus && contains_array_notation_expr (fnbody))
1970 fnbody = expand_array_notation_exprs (fnbody);
1971 if (nested)
1972 {
1973 tree decl = current_function_decl;
1974 /* Mark nested functions as needing static-chain initially.
1975 lower_nested_functions will recompute it but the
1976 DECL_STATIC_CHAIN flag is also used before that happens,
1977 by initializer_constant_valid_p. See gcc.dg/nested-fn-2.c. */
1978 DECL_STATIC_CHAIN (decl) = 1;
1979 add_stmt (fnbody);
1980 finish_function ();
1981 c_pop_function_context ();
1982 add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl));
1983 }
1984 else
1985 {
1986 add_stmt (fnbody);
1987 finish_function ();
1988 }
1989
1990 timevar_pop (tv);
1991 break;
1992 }
1993 }
1994
1995 /* Parse an asm-definition (asm() outside a function body). This is a
1996 GNU extension.
1997
1998 asm-definition:
1999 simple-asm-expr ;
2000 */
2001
2002 static void
2003 c_parser_asm_definition (c_parser *parser)
2004 {
2005 tree asm_str = c_parser_simple_asm_expr (parser);
2006 if (asm_str)
2007 symtab->finalize_toplevel_asm (asm_str);
2008 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
2009 }
2010
2011 /* Parse a static assertion (C11 6.7.10).
2012
2013 static_assert-declaration:
2014 static_assert-declaration-no-semi ;
2015 */
2016
2017 static void
2018 c_parser_static_assert_declaration (c_parser *parser)
2019 {
2020 c_parser_static_assert_declaration_no_semi (parser);
2021 if (parser->error
2022 || !c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
2023 c_parser_skip_to_end_of_block_or_statement (parser);
2024 }
2025
2026 /* Parse a static assertion (C11 6.7.10), without the trailing
2027 semicolon.
2028
2029 static_assert-declaration-no-semi:
2030 _Static_assert ( constant-expression , string-literal )
2031 */
2032
2033 static void
2034 c_parser_static_assert_declaration_no_semi (c_parser *parser)
2035 {
2036 location_t assert_loc, value_loc;
2037 tree value;
2038 tree string;
2039
2040 gcc_assert (c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT));
2041 assert_loc = c_parser_peek_token (parser)->location;
2042 if (flag_isoc99)
2043 pedwarn_c99 (assert_loc, OPT_Wpedantic,
2044 "ISO C99 does not support %<_Static_assert%>");
2045 else
2046 pedwarn_c99 (assert_loc, OPT_Wpedantic,
2047 "ISO C90 does not support %<_Static_assert%>");
2048 c_parser_consume_token (parser);
2049 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2050 return;
2051 value_loc = c_parser_peek_token (parser)->location;
2052 value = c_parser_expr_no_commas (parser, NULL).value;
2053 parser->lex_untranslated_string = true;
2054 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
2055 {
2056 parser->lex_untranslated_string = false;
2057 return;
2058 }
2059 switch (c_parser_peek_token (parser)->type)
2060 {
2061 case CPP_STRING:
2062 case CPP_STRING16:
2063 case CPP_STRING32:
2064 case CPP_WSTRING:
2065 case CPP_UTF8STRING:
2066 string = c_parser_peek_token (parser)->value;
2067 c_parser_consume_token (parser);
2068 parser->lex_untranslated_string = false;
2069 break;
2070 default:
2071 c_parser_error (parser, "expected string literal");
2072 parser->lex_untranslated_string = false;
2073 return;
2074 }
2075 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
2076
2077 if (!INTEGRAL_TYPE_P (TREE_TYPE (value)))
2078 {
2079 error_at (value_loc, "expression in static assertion is not an integer");
2080 return;
2081 }
2082 if (TREE_CODE (value) != INTEGER_CST)
2083 {
2084 value = c_fully_fold (value, false, NULL);
2085 /* Strip no-op conversions. */
2086 STRIP_TYPE_NOPS (value);
2087 if (TREE_CODE (value) == INTEGER_CST)
2088 pedwarn (value_loc, OPT_Wpedantic, "expression in static assertion "
2089 "is not an integer constant expression");
2090 }
2091 if (TREE_CODE (value) != INTEGER_CST)
2092 {
2093 error_at (value_loc, "expression in static assertion is not constant");
2094 return;
2095 }
2096 constant_expression_warning (value);
2097 if (integer_zerop (value))
2098 error_at (assert_loc, "static assertion failed: %E", string);
2099 }
2100
2101 /* Parse some declaration specifiers (possibly none) (C90 6.5, C99
2102 6.7), adding them to SPECS (which may already include some).
2103 Storage class specifiers are accepted iff SCSPEC_OK; type
2104 specifiers are accepted iff TYPESPEC_OK; alignment specifiers are
2105 accepted iff ALIGNSPEC_OK; attributes are accepted at the start
2106 iff START_ATTR_OK; __auto_type is accepted iff AUTO_TYPE_OK.
2107
2108 declaration-specifiers:
2109 storage-class-specifier declaration-specifiers[opt]
2110 type-specifier declaration-specifiers[opt]
2111 type-qualifier declaration-specifiers[opt]
2112 function-specifier declaration-specifiers[opt]
2113 alignment-specifier declaration-specifiers[opt]
2114
2115 Function specifiers (inline) are from C99, and are currently
2116 handled as storage class specifiers, as is __thread. Alignment
2117 specifiers are from C11.
2118
2119 C90 6.5.1, C99 6.7.1:
2120 storage-class-specifier:
2121 typedef
2122 extern
2123 static
2124 auto
2125 register
2126 _Thread_local
2127
2128 (_Thread_local is new in C11.)
2129
2130 C99 6.7.4:
2131 function-specifier:
2132 inline
2133 _Noreturn
2134
2135 (_Noreturn is new in C11.)
2136
2137 C90 6.5.2, C99 6.7.2:
2138 type-specifier:
2139 void
2140 char
2141 short
2142 int
2143 long
2144 float
2145 double
2146 signed
2147 unsigned
2148 _Bool
2149 _Complex
2150 [_Imaginary removed in C99 TC2]
2151 struct-or-union-specifier
2152 enum-specifier
2153 typedef-name
2154 atomic-type-specifier
2155
2156 (_Bool and _Complex are new in C99.)
2157 (atomic-type-specifier is new in C11.)
2158
2159 C90 6.5.3, C99 6.7.3:
2160
2161 type-qualifier:
2162 const
2163 restrict
2164 volatile
2165 address-space-qualifier
2166 _Atomic
2167
2168 (restrict is new in C99.)
2169 (_Atomic is new in C11.)
2170
2171 GNU extensions:
2172
2173 declaration-specifiers:
2174 attributes declaration-specifiers[opt]
2175
2176 type-qualifier:
2177 address-space
2178
2179 address-space:
2180 identifier recognized by the target
2181
2182 storage-class-specifier:
2183 __thread
2184
2185 type-specifier:
2186 typeof-specifier
2187 __auto_type
2188 __intN
2189 _Decimal32
2190 _Decimal64
2191 _Decimal128
2192 _Fract
2193 _Accum
2194 _Sat
2195
2196 (_Fract, _Accum, and _Sat are new from ISO/IEC DTR 18037:
2197 http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1169.pdf)
2198
2199 atomic-type-specifier
2200 _Atomic ( type-name )
2201
2202 Objective-C:
2203
2204 type-specifier:
2205 class-name objc-protocol-refs[opt]
2206 typedef-name objc-protocol-refs
2207 objc-protocol-refs
2208 */
2209
2210 static void
2211 c_parser_declspecs (c_parser *parser, struct c_declspecs *specs,
2212 bool scspec_ok, bool typespec_ok, bool start_attr_ok,
2213 bool alignspec_ok, bool auto_type_ok,
2214 enum c_lookahead_kind la)
2215 {
2216 bool attrs_ok = start_attr_ok;
2217 bool seen_type = specs->typespec_kind != ctsk_none;
2218
2219 if (!typespec_ok)
2220 gcc_assert (la == cla_prefer_id);
2221
2222 while (c_parser_next_token_is (parser, CPP_NAME)
2223 || c_parser_next_token_is (parser, CPP_KEYWORD)
2224 || (c_dialect_objc () && c_parser_next_token_is (parser, CPP_LESS)))
2225 {
2226 struct c_typespec t;
2227 tree attrs;
2228 tree align;
2229 location_t loc = c_parser_peek_token (parser)->location;
2230
2231 /* If we cannot accept a type, exit if the next token must start
2232 one. Also, if we already have seen a tagged definition,
2233 a typename would be an error anyway and likely the user
2234 has simply forgotten a semicolon, so we exit. */
2235 if ((!typespec_ok || specs->typespec_kind == ctsk_tagdef)
2236 && c_parser_next_tokens_start_typename (parser, la)
2237 && !c_parser_next_token_is_qualifier (parser))
2238 break;
2239
2240 if (c_parser_next_token_is (parser, CPP_NAME))
2241 {
2242 c_token *name_token = c_parser_peek_token (parser);
2243 tree value = name_token->value;
2244 c_id_kind kind = name_token->id_kind;
2245
2246 if (kind == C_ID_ADDRSPACE)
2247 {
2248 addr_space_t as
2249 = name_token->keyword - RID_FIRST_ADDR_SPACE;
2250 declspecs_add_addrspace (name_token->location, specs, as);
2251 c_parser_consume_token (parser);
2252 attrs_ok = true;
2253 continue;
2254 }
2255
2256 gcc_assert (!c_parser_next_token_is_qualifier (parser));
2257
2258 /* If we cannot accept a type, and the next token must start one,
2259 exit. Do the same if we already have seen a tagged definition,
2260 since it would be an error anyway and likely the user has simply
2261 forgotten a semicolon. */
2262 if (seen_type || !c_parser_next_tokens_start_typename (parser, la))
2263 break;
2264
2265 /* Now at an unknown typename (C_ID_ID), a C_ID_TYPENAME or
2266 a C_ID_CLASSNAME. */
2267 c_parser_consume_token (parser);
2268 seen_type = true;
2269 attrs_ok = true;
2270 if (kind == C_ID_ID)
2271 {
2272 error_at (loc, "unknown type name %qE", value);
2273 t.kind = ctsk_typedef;
2274 t.spec = error_mark_node;
2275 }
2276 else if (kind == C_ID_TYPENAME
2277 && (!c_dialect_objc ()
2278 || c_parser_next_token_is_not (parser, CPP_LESS)))
2279 {
2280 t.kind = ctsk_typedef;
2281 /* For a typedef name, record the meaning, not the name.
2282 In case of 'foo foo, bar;'. */
2283 t.spec = lookup_name (value);
2284 }
2285 else
2286 {
2287 tree proto = NULL_TREE;
2288 gcc_assert (c_dialect_objc ());
2289 t.kind = ctsk_objc;
2290 if (c_parser_next_token_is (parser, CPP_LESS))
2291 proto = c_parser_objc_protocol_refs (parser);
2292 t.spec = objc_get_protocol_qualified_type (value, proto);
2293 }
2294 t.expr = NULL_TREE;
2295 t.expr_const_operands = true;
2296 declspecs_add_type (name_token->location, specs, t);
2297 continue;
2298 }
2299 if (c_parser_next_token_is (parser, CPP_LESS))
2300 {
2301 /* Make "<SomeProtocol>" equivalent to "id <SomeProtocol>" -
2302 nisse@lysator.liu.se. */
2303 tree proto;
2304 gcc_assert (c_dialect_objc ());
2305 if (!typespec_ok || seen_type)
2306 break;
2307 proto = c_parser_objc_protocol_refs (parser);
2308 t.kind = ctsk_objc;
2309 t.spec = objc_get_protocol_qualified_type (NULL_TREE, proto);
2310 t.expr = NULL_TREE;
2311 t.expr_const_operands = true;
2312 declspecs_add_type (loc, specs, t);
2313 continue;
2314 }
2315 gcc_assert (c_parser_next_token_is (parser, CPP_KEYWORD));
2316 switch (c_parser_peek_token (parser)->keyword)
2317 {
2318 case RID_STATIC:
2319 case RID_EXTERN:
2320 case RID_REGISTER:
2321 case RID_TYPEDEF:
2322 case RID_INLINE:
2323 case RID_NORETURN:
2324 case RID_AUTO:
2325 case RID_THREAD:
2326 if (!scspec_ok)
2327 goto out;
2328 attrs_ok = true;
2329 /* TODO: Distinguish between function specifiers (inline, noreturn)
2330 and storage class specifiers, either here or in
2331 declspecs_add_scspec. */
2332 declspecs_add_scspec (loc, specs,
2333 c_parser_peek_token (parser)->value);
2334 c_parser_consume_token (parser);
2335 break;
2336 case RID_AUTO_TYPE:
2337 if (!auto_type_ok)
2338 goto out;
2339 /* Fall through. */
2340 case RID_UNSIGNED:
2341 case RID_LONG:
2342 case RID_SHORT:
2343 case RID_SIGNED:
2344 case RID_COMPLEX:
2345 case RID_INT:
2346 case RID_CHAR:
2347 case RID_FLOAT:
2348 case RID_DOUBLE:
2349 case RID_VOID:
2350 case RID_DFLOAT32:
2351 case RID_DFLOAT64:
2352 case RID_DFLOAT128:
2353 case RID_BOOL:
2354 case RID_FRACT:
2355 case RID_ACCUM:
2356 case RID_SAT:
2357 case RID_INT_N_0:
2358 case RID_INT_N_1:
2359 case RID_INT_N_2:
2360 case RID_INT_N_3:
2361 if (!typespec_ok)
2362 goto out;
2363 attrs_ok = true;
2364 seen_type = true;
2365 if (c_dialect_objc ())
2366 parser->objc_need_raw_identifier = true;
2367 t.kind = ctsk_resword;
2368 t.spec = c_parser_peek_token (parser)->value;
2369 t.expr = NULL_TREE;
2370 t.expr_const_operands = true;
2371 declspecs_add_type (loc, specs, t);
2372 c_parser_consume_token (parser);
2373 break;
2374 case RID_ENUM:
2375 if (!typespec_ok)
2376 goto out;
2377 attrs_ok = true;
2378 seen_type = true;
2379 t = c_parser_enum_specifier (parser);
2380 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, t.spec);
2381 declspecs_add_type (loc, specs, t);
2382 break;
2383 case RID_STRUCT:
2384 case RID_UNION:
2385 if (!typespec_ok)
2386 goto out;
2387 attrs_ok = true;
2388 seen_type = true;
2389 t = c_parser_struct_or_union_specifier (parser);
2390 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, t.spec);
2391 declspecs_add_type (loc, specs, t);
2392 break;
2393 case RID_TYPEOF:
2394 /* ??? The old parser rejected typeof after other type
2395 specifiers, but is a syntax error the best way of
2396 handling this? */
2397 if (!typespec_ok || seen_type)
2398 goto out;
2399 attrs_ok = true;
2400 seen_type = true;
2401 t = c_parser_typeof_specifier (parser);
2402 declspecs_add_type (loc, specs, t);
2403 break;
2404 case RID_ATOMIC:
2405 /* C parser handling of Objective-C constructs needs
2406 checking for correct lvalue-to-rvalue conversions, and
2407 the code in build_modify_expr handling various
2408 Objective-C cases, and that in build_unary_op handling
2409 Objective-C cases for increment / decrement, also needs
2410 updating; uses of TYPE_MAIN_VARIANT in objc_compare_types
2411 and objc_types_are_equivalent may also need updates. */
2412 if (c_dialect_objc ())
2413 sorry ("%<_Atomic%> in Objective-C");
2414 /* C parser handling of OpenMP constructs needs checking for
2415 correct lvalue-to-rvalue conversions. */
2416 if (flag_openmp)
2417 sorry ("%<_Atomic%> with OpenMP");
2418 if (flag_isoc99)
2419 pedwarn_c99 (loc, OPT_Wpedantic,
2420 "ISO C99 does not support the %<_Atomic%> qualifier");
2421 else
2422 pedwarn_c99 (loc, OPT_Wpedantic,
2423 "ISO C90 does not support the %<_Atomic%> qualifier");
2424 attrs_ok = true;
2425 tree value;
2426 value = c_parser_peek_token (parser)->value;
2427 c_parser_consume_token (parser);
2428 if (typespec_ok && c_parser_next_token_is (parser, CPP_OPEN_PAREN))
2429 {
2430 /* _Atomic ( type-name ). */
2431 seen_type = true;
2432 c_parser_consume_token (parser);
2433 struct c_type_name *type = c_parser_type_name (parser);
2434 t.kind = ctsk_typeof;
2435 t.spec = error_mark_node;
2436 t.expr = NULL_TREE;
2437 t.expr_const_operands = true;
2438 if (type != NULL)
2439 t.spec = groktypename (type, &t.expr,
2440 &t.expr_const_operands);
2441 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2442 "expected %<)%>");
2443 if (t.spec != error_mark_node)
2444 {
2445 if (TREE_CODE (t.spec) == ARRAY_TYPE)
2446 error_at (loc, "%<_Atomic%>-qualified array type");
2447 else if (TREE_CODE (t.spec) == FUNCTION_TYPE)
2448 error_at (loc, "%<_Atomic%>-qualified function type");
2449 else if (TYPE_QUALS (t.spec) != TYPE_UNQUALIFIED)
2450 error_at (loc, "%<_Atomic%> applied to a qualified type");
2451 else
2452 t.spec = c_build_qualified_type (t.spec, TYPE_QUAL_ATOMIC);
2453 }
2454 declspecs_add_type (loc, specs, t);
2455 }
2456 else
2457 declspecs_add_qual (loc, specs, value);
2458 break;
2459 case RID_CONST:
2460 case RID_VOLATILE:
2461 case RID_RESTRICT:
2462 attrs_ok = true;
2463 declspecs_add_qual (loc, specs, c_parser_peek_token (parser)->value);
2464 c_parser_consume_token (parser);
2465 break;
2466 case RID_ATTRIBUTE:
2467 if (!attrs_ok)
2468 goto out;
2469 attrs = c_parser_attributes (parser);
2470 declspecs_add_attrs (loc, specs, attrs);
2471 break;
2472 case RID_ALIGNAS:
2473 if (!alignspec_ok)
2474 goto out;
2475 align = c_parser_alignas_specifier (parser);
2476 declspecs_add_alignas (loc, specs, align);
2477 break;
2478 default:
2479 goto out;
2480 }
2481 }
2482 out: ;
2483 }
2484
2485 /* Parse an enum specifier (C90 6.5.2.2, C99 6.7.2.2).
2486
2487 enum-specifier:
2488 enum attributes[opt] identifier[opt] { enumerator-list } attributes[opt]
2489 enum attributes[opt] identifier[opt] { enumerator-list , } attributes[opt]
2490 enum attributes[opt] identifier
2491
2492 The form with trailing comma is new in C99. The forms with
2493 attributes are GNU extensions. In GNU C, we accept any expression
2494 without commas in the syntax (assignment expressions, not just
2495 conditional expressions); assignment expressions will be diagnosed
2496 as non-constant.
2497
2498 enumerator-list:
2499 enumerator
2500 enumerator-list , enumerator
2501
2502 enumerator:
2503 enumeration-constant
2504 enumeration-constant = constant-expression
2505
2506 GNU Extensions:
2507
2508 enumerator:
2509 enumeration-constant attributes[opt]
2510 enumeration-constant attributes[opt] = constant-expression
2511
2512 */
2513
2514 static struct c_typespec
2515 c_parser_enum_specifier (c_parser *parser)
2516 {
2517 struct c_typespec ret;
2518 tree attrs;
2519 tree ident = NULL_TREE;
2520 location_t enum_loc;
2521 location_t ident_loc = UNKNOWN_LOCATION; /* Quiet warning. */
2522 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ENUM));
2523 enum_loc = c_parser_peek_token (parser)->location;
2524 c_parser_consume_token (parser);
2525 attrs = c_parser_attributes (parser);
2526 enum_loc = c_parser_peek_token (parser)->location;
2527 /* Set the location in case we create a decl now. */
2528 c_parser_set_source_position_from_token (c_parser_peek_token (parser));
2529 if (c_parser_next_token_is (parser, CPP_NAME))
2530 {
2531 ident = c_parser_peek_token (parser)->value;
2532 ident_loc = c_parser_peek_token (parser)->location;
2533 enum_loc = ident_loc;
2534 c_parser_consume_token (parser);
2535 }
2536 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
2537 {
2538 /* Parse an enum definition. */
2539 struct c_enum_contents the_enum;
2540 tree type;
2541 tree postfix_attrs;
2542 /* We chain the enumerators in reverse order, then put them in
2543 forward order at the end. */
2544 tree values;
2545 timevar_push (TV_PARSE_ENUM);
2546 type = start_enum (enum_loc, &the_enum, ident);
2547 values = NULL_TREE;
2548 c_parser_consume_token (parser);
2549 while (true)
2550 {
2551 tree enum_id;
2552 tree enum_value;
2553 tree enum_decl;
2554 bool seen_comma;
2555 c_token *token;
2556 location_t comma_loc = UNKNOWN_LOCATION; /* Quiet warning. */
2557 location_t decl_loc, value_loc;
2558 if (c_parser_next_token_is_not (parser, CPP_NAME))
2559 {
2560 c_parser_error (parser, "expected identifier");
2561 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
2562 values = error_mark_node;
2563 break;
2564 }
2565 token = c_parser_peek_token (parser);
2566 enum_id = token->value;
2567 /* Set the location in case we create a decl now. */
2568 c_parser_set_source_position_from_token (token);
2569 decl_loc = value_loc = token->location;
2570 c_parser_consume_token (parser);
2571 /* Parse any specified attributes. */
2572 tree enum_attrs = c_parser_attributes (parser);
2573 if (c_parser_next_token_is (parser, CPP_EQ))
2574 {
2575 c_parser_consume_token (parser);
2576 value_loc = c_parser_peek_token (parser)->location;
2577 enum_value = c_parser_expr_no_commas (parser, NULL).value;
2578 }
2579 else
2580 enum_value = NULL_TREE;
2581 enum_decl = build_enumerator (decl_loc, value_loc,
2582 &the_enum, enum_id, enum_value);
2583 if (enum_attrs)
2584 decl_attributes (&TREE_PURPOSE (enum_decl), enum_attrs, 0);
2585 TREE_CHAIN (enum_decl) = values;
2586 values = enum_decl;
2587 seen_comma = false;
2588 if (c_parser_next_token_is (parser, CPP_COMMA))
2589 {
2590 comma_loc = c_parser_peek_token (parser)->location;
2591 seen_comma = true;
2592 c_parser_consume_token (parser);
2593 }
2594 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2595 {
2596 if (seen_comma)
2597 pedwarn_c90 (comma_loc, OPT_Wpedantic,
2598 "comma at end of enumerator list");
2599 c_parser_consume_token (parser);
2600 break;
2601 }
2602 if (!seen_comma)
2603 {
2604 c_parser_error (parser, "expected %<,%> or %<}%>");
2605 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
2606 values = error_mark_node;
2607 break;
2608 }
2609 }
2610 postfix_attrs = c_parser_attributes (parser);
2611 ret.spec = finish_enum (type, nreverse (values),
2612 chainon (attrs, postfix_attrs));
2613 ret.kind = ctsk_tagdef;
2614 ret.expr = NULL_TREE;
2615 ret.expr_const_operands = true;
2616 timevar_pop (TV_PARSE_ENUM);
2617 return ret;
2618 }
2619 else if (!ident)
2620 {
2621 c_parser_error (parser, "expected %<{%>");
2622 ret.spec = error_mark_node;
2623 ret.kind = ctsk_tagref;
2624 ret.expr = NULL_TREE;
2625 ret.expr_const_operands = true;
2626 return ret;
2627 }
2628 ret = parser_xref_tag (ident_loc, ENUMERAL_TYPE, ident);
2629 /* In ISO C, enumerated types can be referred to only if already
2630 defined. */
2631 if (pedantic && !COMPLETE_TYPE_P (ret.spec))
2632 {
2633 gcc_assert (ident);
2634 pedwarn (enum_loc, OPT_Wpedantic,
2635 "ISO C forbids forward references to %<enum%> types");
2636 }
2637 return ret;
2638 }
2639
2640 /* Parse a struct or union specifier (C90 6.5.2.1, C99 6.7.2.1).
2641
2642 struct-or-union-specifier:
2643 struct-or-union attributes[opt] identifier[opt]
2644 { struct-contents } attributes[opt]
2645 struct-or-union attributes[opt] identifier
2646
2647 struct-contents:
2648 struct-declaration-list
2649
2650 struct-declaration-list:
2651 struct-declaration ;
2652 struct-declaration-list struct-declaration ;
2653
2654 GNU extensions:
2655
2656 struct-contents:
2657 empty
2658 struct-declaration
2659 struct-declaration-list struct-declaration
2660
2661 struct-declaration-list:
2662 struct-declaration-list ;
2663 ;
2664
2665 (Note that in the syntax here, unlike that in ISO C, the semicolons
2666 are included here rather than in struct-declaration, in order to
2667 describe the syntax with extra semicolons and missing semicolon at
2668 end.)
2669
2670 Objective-C:
2671
2672 struct-declaration-list:
2673 @defs ( class-name )
2674
2675 (Note this does not include a trailing semicolon, but can be
2676 followed by further declarations, and gets a pedwarn-if-pedantic
2677 when followed by a semicolon.) */
2678
2679 static struct c_typespec
2680 c_parser_struct_or_union_specifier (c_parser *parser)
2681 {
2682 struct c_typespec ret;
2683 tree attrs;
2684 tree ident = NULL_TREE;
2685 location_t struct_loc;
2686 location_t ident_loc = UNKNOWN_LOCATION;
2687 enum tree_code code;
2688 switch (c_parser_peek_token (parser)->keyword)
2689 {
2690 case RID_STRUCT:
2691 code = RECORD_TYPE;
2692 break;
2693 case RID_UNION:
2694 code = UNION_TYPE;
2695 break;
2696 default:
2697 gcc_unreachable ();
2698 }
2699 struct_loc = c_parser_peek_token (parser)->location;
2700 c_parser_consume_token (parser);
2701 attrs = c_parser_attributes (parser);
2702
2703 /* Set the location in case we create a decl now. */
2704 c_parser_set_source_position_from_token (c_parser_peek_token (parser));
2705
2706 if (c_parser_next_token_is (parser, CPP_NAME))
2707 {
2708 ident = c_parser_peek_token (parser)->value;
2709 ident_loc = c_parser_peek_token (parser)->location;
2710 struct_loc = ident_loc;
2711 c_parser_consume_token (parser);
2712 }
2713 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
2714 {
2715 /* Parse a struct or union definition. Start the scope of the
2716 tag before parsing components. */
2717 struct c_struct_parse_info *struct_info;
2718 tree type = start_struct (struct_loc, code, ident, &struct_info);
2719 tree postfix_attrs;
2720 /* We chain the components in reverse order, then put them in
2721 forward order at the end. Each struct-declaration may
2722 declare multiple components (comma-separated), so we must use
2723 chainon to join them, although when parsing each
2724 struct-declaration we can use TREE_CHAIN directly.
2725
2726 The theory behind all this is that there will be more
2727 semicolon separated fields than comma separated fields, and
2728 so we'll be minimizing the number of node traversals required
2729 by chainon. */
2730 tree contents;
2731 timevar_push (TV_PARSE_STRUCT);
2732 contents = NULL_TREE;
2733 c_parser_consume_token (parser);
2734 /* Handle the Objective-C @defs construct,
2735 e.g. foo(sizeof(struct{ @defs(ClassName) }));. */
2736 if (c_parser_next_token_is_keyword (parser, RID_AT_DEFS))
2737 {
2738 tree name;
2739 gcc_assert (c_dialect_objc ());
2740 c_parser_consume_token (parser);
2741 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2742 goto end_at_defs;
2743 if (c_parser_next_token_is (parser, CPP_NAME)
2744 && c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME)
2745 {
2746 name = c_parser_peek_token (parser)->value;
2747 c_parser_consume_token (parser);
2748 }
2749 else
2750 {
2751 c_parser_error (parser, "expected class name");
2752 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
2753 goto end_at_defs;
2754 }
2755 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2756 "expected %<)%>");
2757 contents = nreverse (objc_get_class_ivars (name));
2758 }
2759 end_at_defs:
2760 /* Parse the struct-declarations and semicolons. Problems with
2761 semicolons are diagnosed here; empty structures are diagnosed
2762 elsewhere. */
2763 while (true)
2764 {
2765 tree decls;
2766 /* Parse any stray semicolon. */
2767 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
2768 {
2769 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
2770 "extra semicolon in struct or union specified");
2771 c_parser_consume_token (parser);
2772 continue;
2773 }
2774 /* Stop if at the end of the struct or union contents. */
2775 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2776 {
2777 c_parser_consume_token (parser);
2778 break;
2779 }
2780 /* Accept #pragmas at struct scope. */
2781 if (c_parser_next_token_is (parser, CPP_PRAGMA))
2782 {
2783 c_parser_pragma (parser, pragma_struct);
2784 continue;
2785 }
2786 /* Parse some comma-separated declarations, but not the
2787 trailing semicolon if any. */
2788 decls = c_parser_struct_declaration (parser);
2789 contents = chainon (decls, contents);
2790 /* If no semicolon follows, either we have a parse error or
2791 are at the end of the struct or union and should
2792 pedwarn. */
2793 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
2794 c_parser_consume_token (parser);
2795 else
2796 {
2797 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2798 pedwarn (c_parser_peek_token (parser)->location, 0,
2799 "no semicolon at end of struct or union");
2800 else if (parser->error
2801 || !c_parser_next_token_starts_declspecs (parser))
2802 {
2803 c_parser_error (parser, "expected %<;%>");
2804 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
2805 break;
2806 }
2807
2808 /* If we come here, we have already emitted an error
2809 for an expected `;', identifier or `(', and we also
2810 recovered already. Go on with the next field. */
2811 }
2812 }
2813 postfix_attrs = c_parser_attributes (parser);
2814 ret.spec = finish_struct (struct_loc, type, nreverse (contents),
2815 chainon (attrs, postfix_attrs), struct_info);
2816 ret.kind = ctsk_tagdef;
2817 ret.expr = NULL_TREE;
2818 ret.expr_const_operands = true;
2819 timevar_pop (TV_PARSE_STRUCT);
2820 return ret;
2821 }
2822 else if (!ident)
2823 {
2824 c_parser_error (parser, "expected %<{%>");
2825 ret.spec = error_mark_node;
2826 ret.kind = ctsk_tagref;
2827 ret.expr = NULL_TREE;
2828 ret.expr_const_operands = true;
2829 return ret;
2830 }
2831 ret = parser_xref_tag (ident_loc, code, ident);
2832 return ret;
2833 }
2834
2835 /* Parse a struct-declaration (C90 6.5.2.1, C99 6.7.2.1), *without*
2836 the trailing semicolon.
2837
2838 struct-declaration:
2839 specifier-qualifier-list struct-declarator-list
2840 static_assert-declaration-no-semi
2841
2842 specifier-qualifier-list:
2843 type-specifier specifier-qualifier-list[opt]
2844 type-qualifier specifier-qualifier-list[opt]
2845 attributes specifier-qualifier-list[opt]
2846
2847 struct-declarator-list:
2848 struct-declarator
2849 struct-declarator-list , attributes[opt] struct-declarator
2850
2851 struct-declarator:
2852 declarator attributes[opt]
2853 declarator[opt] : constant-expression attributes[opt]
2854
2855 GNU extensions:
2856
2857 struct-declaration:
2858 __extension__ struct-declaration
2859 specifier-qualifier-list
2860
2861 Unlike the ISO C syntax, semicolons are handled elsewhere. The use
2862 of attributes where shown is a GNU extension. In GNU C, we accept
2863 any expression without commas in the syntax (assignment
2864 expressions, not just conditional expressions); assignment
2865 expressions will be diagnosed as non-constant. */
2866
2867 static tree
2868 c_parser_struct_declaration (c_parser *parser)
2869 {
2870 struct c_declspecs *specs;
2871 tree prefix_attrs;
2872 tree all_prefix_attrs;
2873 tree decls;
2874 location_t decl_loc;
2875 if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
2876 {
2877 int ext;
2878 tree decl;
2879 ext = disable_extension_diagnostics ();
2880 c_parser_consume_token (parser);
2881 decl = c_parser_struct_declaration (parser);
2882 restore_extension_diagnostics (ext);
2883 return decl;
2884 }
2885 if (c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT))
2886 {
2887 c_parser_static_assert_declaration_no_semi (parser);
2888 return NULL_TREE;
2889 }
2890 specs = build_null_declspecs ();
2891 decl_loc = c_parser_peek_token (parser)->location;
2892 /* Strictly by the standard, we shouldn't allow _Alignas here,
2893 but it appears to have been intended to allow it there, so
2894 we're keeping it as it is until WG14 reaches a conclusion
2895 of N1731.
2896 <http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1731.pdf> */
2897 c_parser_declspecs (parser, specs, false, true, true,
2898 true, false, cla_nonabstract_decl);
2899 if (parser->error)
2900 return NULL_TREE;
2901 if (!specs->declspecs_seen_p)
2902 {
2903 c_parser_error (parser, "expected specifier-qualifier-list");
2904 return NULL_TREE;
2905 }
2906 finish_declspecs (specs);
2907 if (c_parser_next_token_is (parser, CPP_SEMICOLON)
2908 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2909 {
2910 tree ret;
2911 if (specs->typespec_kind == ctsk_none)
2912 {
2913 pedwarn (decl_loc, OPT_Wpedantic,
2914 "ISO C forbids member declarations with no members");
2915 shadow_tag_warned (specs, pedantic);
2916 ret = NULL_TREE;
2917 }
2918 else
2919 {
2920 /* Support for unnamed structs or unions as members of
2921 structs or unions (which is [a] useful and [b] supports
2922 MS P-SDK). */
2923 tree attrs = NULL;
2924
2925 ret = grokfield (c_parser_peek_token (parser)->location,
2926 build_id_declarator (NULL_TREE), specs,
2927 NULL_TREE, &attrs);
2928 if (ret)
2929 decl_attributes (&ret, attrs, 0);
2930 }
2931 return ret;
2932 }
2933
2934 /* Provide better error recovery. Note that a type name here is valid,
2935 and will be treated as a field name. */
2936 if (specs->typespec_kind == ctsk_tagdef
2937 && TREE_CODE (specs->type) != ENUMERAL_TYPE
2938 && c_parser_next_token_starts_declspecs (parser)
2939 && !c_parser_next_token_is (parser, CPP_NAME))
2940 {
2941 c_parser_error (parser, "expected %<;%>, identifier or %<(%>");
2942 parser->error = false;
2943 return NULL_TREE;
2944 }
2945
2946 pending_xref_error ();
2947 prefix_attrs = specs->attrs;
2948 all_prefix_attrs = prefix_attrs;
2949 specs->attrs = NULL_TREE;
2950 decls = NULL_TREE;
2951 while (true)
2952 {
2953 /* Declaring one or more declarators or un-named bit-fields. */
2954 struct c_declarator *declarator;
2955 bool dummy = false;
2956 if (c_parser_next_token_is (parser, CPP_COLON))
2957 declarator = build_id_declarator (NULL_TREE);
2958 else
2959 declarator = c_parser_declarator (parser,
2960 specs->typespec_kind != ctsk_none,
2961 C_DTR_NORMAL, &dummy);
2962 if (declarator == NULL)
2963 {
2964 c_parser_skip_to_end_of_block_or_statement (parser);
2965 break;
2966 }
2967 if (c_parser_next_token_is (parser, CPP_COLON)
2968 || c_parser_next_token_is (parser, CPP_COMMA)
2969 || c_parser_next_token_is (parser, CPP_SEMICOLON)
2970 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE)
2971 || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2972 {
2973 tree postfix_attrs = NULL_TREE;
2974 tree width = NULL_TREE;
2975 tree d;
2976 if (c_parser_next_token_is (parser, CPP_COLON))
2977 {
2978 c_parser_consume_token (parser);
2979 width = c_parser_expr_no_commas (parser, NULL).value;
2980 }
2981 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2982 postfix_attrs = c_parser_attributes (parser);
2983 d = grokfield (c_parser_peek_token (parser)->location,
2984 declarator, specs, width, &all_prefix_attrs);
2985 decl_attributes (&d, chainon (postfix_attrs,
2986 all_prefix_attrs), 0);
2987 DECL_CHAIN (d) = decls;
2988 decls = d;
2989 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2990 all_prefix_attrs = chainon (c_parser_attributes (parser),
2991 prefix_attrs);
2992 else
2993 all_prefix_attrs = prefix_attrs;
2994 if (c_parser_next_token_is (parser, CPP_COMMA))
2995 c_parser_consume_token (parser);
2996 else if (c_parser_next_token_is (parser, CPP_SEMICOLON)
2997 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2998 {
2999 /* Semicolon consumed in caller. */
3000 break;
3001 }
3002 else
3003 {
3004 c_parser_error (parser, "expected %<,%>, %<;%> or %<}%>");
3005 break;
3006 }
3007 }
3008 else
3009 {
3010 c_parser_error (parser,
3011 "expected %<:%>, %<,%>, %<;%>, %<}%> or "
3012 "%<__attribute__%>");
3013 break;
3014 }
3015 }
3016 return decls;
3017 }
3018
3019 /* Parse a typeof specifier (a GNU extension).
3020
3021 typeof-specifier:
3022 typeof ( expression )
3023 typeof ( type-name )
3024 */
3025
3026 static struct c_typespec
3027 c_parser_typeof_specifier (c_parser *parser)
3028 {
3029 struct c_typespec ret;
3030 ret.kind = ctsk_typeof;
3031 ret.spec = error_mark_node;
3032 ret.expr = NULL_TREE;
3033 ret.expr_const_operands = true;
3034 gcc_assert (c_parser_next_token_is_keyword (parser, RID_TYPEOF));
3035 c_parser_consume_token (parser);
3036 c_inhibit_evaluation_warnings++;
3037 in_typeof++;
3038 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3039 {
3040 c_inhibit_evaluation_warnings--;
3041 in_typeof--;
3042 return ret;
3043 }
3044 if (c_parser_next_tokens_start_typename (parser, cla_prefer_id))
3045 {
3046 struct c_type_name *type = c_parser_type_name (parser);
3047 c_inhibit_evaluation_warnings--;
3048 in_typeof--;
3049 if (type != NULL)
3050 {
3051 ret.spec = groktypename (type, &ret.expr, &ret.expr_const_operands);
3052 pop_maybe_used (variably_modified_type_p (ret.spec, NULL_TREE));
3053 }
3054 }
3055 else
3056 {
3057 bool was_vm;
3058 location_t here = c_parser_peek_token (parser)->location;
3059 struct c_expr expr = c_parser_expression (parser);
3060 c_inhibit_evaluation_warnings--;
3061 in_typeof--;
3062 if (TREE_CODE (expr.value) == COMPONENT_REF
3063 && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1)))
3064 error_at (here, "%<typeof%> applied to a bit-field");
3065 mark_exp_read (expr.value);
3066 ret.spec = TREE_TYPE (expr.value);
3067 was_vm = variably_modified_type_p (ret.spec, NULL_TREE);
3068 /* This is returned with the type so that when the type is
3069 evaluated, this can be evaluated. */
3070 if (was_vm)
3071 ret.expr = c_fully_fold (expr.value, false, &ret.expr_const_operands);
3072 pop_maybe_used (was_vm);
3073 /* For use in macros such as those in <stdatomic.h>, remove all
3074 qualifiers from atomic types. (const can be an issue for more macros
3075 using typeof than just the <stdatomic.h> ones.) */
3076 if (ret.spec != error_mark_node && TYPE_ATOMIC (ret.spec))
3077 ret.spec = c_build_qualified_type (ret.spec, TYPE_UNQUALIFIED);
3078 }
3079 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
3080 return ret;
3081 }
3082
3083 /* Parse an alignment-specifier.
3084
3085 C11 6.7.5:
3086
3087 alignment-specifier:
3088 _Alignas ( type-name )
3089 _Alignas ( constant-expression )
3090 */
3091
3092 static tree
3093 c_parser_alignas_specifier (c_parser * parser)
3094 {
3095 tree ret = error_mark_node;
3096 location_t loc = c_parser_peek_token (parser)->location;
3097 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ALIGNAS));
3098 c_parser_consume_token (parser);
3099 if (flag_isoc99)
3100 pedwarn_c99 (loc, OPT_Wpedantic,
3101 "ISO C99 does not support %<_Alignas%>");
3102 else
3103 pedwarn_c99 (loc, OPT_Wpedantic,
3104 "ISO C90 does not support %<_Alignas%>");
3105 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3106 return ret;
3107 if (c_parser_next_tokens_start_typename (parser, cla_prefer_id))
3108 {
3109 struct c_type_name *type = c_parser_type_name (parser);
3110 if (type != NULL)
3111 ret = c_sizeof_or_alignof_type (loc, groktypename (type, NULL, NULL),
3112 false, true, 1);
3113 }
3114 else
3115 ret = c_parser_expr_no_commas (parser, NULL).value;
3116 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
3117 return ret;
3118 }
3119
3120 /* Parse a declarator, possibly an abstract declarator (C90 6.5.4,
3121 6.5.5, C99 6.7.5, 6.7.6). If TYPE_SEEN_P then a typedef name may
3122 be redeclared; otherwise it may not. KIND indicates which kind of
3123 declarator is wanted. Returns a valid declarator except in the
3124 case of a syntax error in which case NULL is returned. *SEEN_ID is
3125 set to true if an identifier being declared is seen; this is used
3126 to diagnose bad forms of abstract array declarators and to
3127 determine whether an identifier list is syntactically permitted.
3128
3129 declarator:
3130 pointer[opt] direct-declarator
3131
3132 direct-declarator:
3133 identifier
3134 ( attributes[opt] declarator )
3135 direct-declarator array-declarator
3136 direct-declarator ( parameter-type-list )
3137 direct-declarator ( identifier-list[opt] )
3138
3139 pointer:
3140 * type-qualifier-list[opt]
3141 * type-qualifier-list[opt] pointer
3142
3143 type-qualifier-list:
3144 type-qualifier
3145 attributes
3146 type-qualifier-list type-qualifier
3147 type-qualifier-list attributes
3148
3149 array-declarator:
3150 [ type-qualifier-list[opt] assignment-expression[opt] ]
3151 [ static type-qualifier-list[opt] assignment-expression ]
3152 [ type-qualifier-list static assignment-expression ]
3153 [ type-qualifier-list[opt] * ]
3154
3155 parameter-type-list:
3156 parameter-list
3157 parameter-list , ...
3158
3159 parameter-list:
3160 parameter-declaration
3161 parameter-list , parameter-declaration
3162
3163 parameter-declaration:
3164 declaration-specifiers declarator attributes[opt]
3165 declaration-specifiers abstract-declarator[opt] attributes[opt]
3166
3167 identifier-list:
3168 identifier
3169 identifier-list , identifier
3170
3171 abstract-declarator:
3172 pointer
3173 pointer[opt] direct-abstract-declarator
3174
3175 direct-abstract-declarator:
3176 ( attributes[opt] abstract-declarator )
3177 direct-abstract-declarator[opt] array-declarator
3178 direct-abstract-declarator[opt] ( parameter-type-list[opt] )
3179
3180 GNU extensions:
3181
3182 direct-declarator:
3183 direct-declarator ( parameter-forward-declarations
3184 parameter-type-list[opt] )
3185
3186 direct-abstract-declarator:
3187 direct-abstract-declarator[opt] ( parameter-forward-declarations
3188 parameter-type-list[opt] )
3189
3190 parameter-forward-declarations:
3191 parameter-list ;
3192 parameter-forward-declarations parameter-list ;
3193
3194 The uses of attributes shown above are GNU extensions.
3195
3196 Some forms of array declarator are not included in C99 in the
3197 syntax for abstract declarators; these are disallowed elsewhere.
3198 This may be a defect (DR#289).
3199
3200 This function also accepts an omitted abstract declarator as being
3201 an abstract declarator, although not part of the formal syntax. */
3202
3203 static struct c_declarator *
3204 c_parser_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
3205 bool *seen_id)
3206 {
3207 /* Parse any initial pointer part. */
3208 if (c_parser_next_token_is (parser, CPP_MULT))
3209 {
3210 struct c_declspecs *quals_attrs = build_null_declspecs ();
3211 struct c_declarator *inner;
3212 c_parser_consume_token (parser);
3213 c_parser_declspecs (parser, quals_attrs, false, false, true,
3214 false, false, cla_prefer_id);
3215 inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
3216 if (inner == NULL)
3217 return NULL;
3218 else
3219 return make_pointer_declarator (quals_attrs, inner);
3220 }
3221 /* Now we have a direct declarator, direct abstract declarator or
3222 nothing (which counts as a direct abstract declarator here). */
3223 return c_parser_direct_declarator (parser, type_seen_p, kind, seen_id);
3224 }
3225
3226 /* Parse a direct declarator or direct abstract declarator; arguments
3227 as c_parser_declarator. */
3228
3229 static struct c_declarator *
3230 c_parser_direct_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
3231 bool *seen_id)
3232 {
3233 /* The direct declarator must start with an identifier (possibly
3234 omitted) or a parenthesized declarator (possibly abstract). In
3235 an ordinary declarator, initial parentheses must start a
3236 parenthesized declarator. In an abstract declarator or parameter
3237 declarator, they could start a parenthesized declarator or a
3238 parameter list. To tell which, the open parenthesis and any
3239 following attributes must be read. If a declaration specifier
3240 follows, then it is a parameter list; if the specifier is a
3241 typedef name, there might be an ambiguity about redeclaring it,
3242 which is resolved in the direction of treating it as a typedef
3243 name. If a close parenthesis follows, it is also an empty
3244 parameter list, as the syntax does not permit empty abstract
3245 declarators. Otherwise, it is a parenthesized declarator (in
3246 which case the analysis may be repeated inside it, recursively).
3247
3248 ??? There is an ambiguity in a parameter declaration "int
3249 (__attribute__((foo)) x)", where x is not a typedef name: it
3250 could be an abstract declarator for a function, or declare x with
3251 parentheses. The proper resolution of this ambiguity needs
3252 documenting. At present we follow an accident of the old
3253 parser's implementation, whereby the first parameter must have
3254 some declaration specifiers other than just attributes. Thus as
3255 a parameter declaration it is treated as a parenthesized
3256 parameter named x, and as an abstract declarator it is
3257 rejected.
3258
3259 ??? Also following the old parser, attributes inside an empty
3260 parameter list are ignored, making it a list not yielding a
3261 prototype, rather than giving an error or making it have one
3262 parameter with implicit type int.
3263
3264 ??? Also following the old parser, typedef names may be
3265 redeclared in declarators, but not Objective-C class names. */
3266
3267 if (kind != C_DTR_ABSTRACT
3268 && c_parser_next_token_is (parser, CPP_NAME)
3269 && ((type_seen_p
3270 && (c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME
3271 || c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME))
3272 || c_parser_peek_token (parser)->id_kind == C_ID_ID))
3273 {
3274 struct c_declarator *inner
3275 = build_id_declarator (c_parser_peek_token (parser)->value);
3276 *seen_id = true;
3277 inner->id_loc = c_parser_peek_token (parser)->location;
3278 c_parser_consume_token (parser);
3279 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
3280 }
3281
3282 if (kind != C_DTR_NORMAL
3283 && c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
3284 {
3285 struct c_declarator *inner = build_id_declarator (NULL_TREE);
3286 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
3287 }
3288
3289 /* Either we are at the end of an abstract declarator, or we have
3290 parentheses. */
3291
3292 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
3293 {
3294 tree attrs;
3295 struct c_declarator *inner;
3296 c_parser_consume_token (parser);
3297 attrs = c_parser_attributes (parser);
3298 if (kind != C_DTR_NORMAL
3299 && (c_parser_next_token_starts_declspecs (parser)
3300 || c_parser_next_token_is (parser, CPP_CLOSE_PAREN)))
3301 {
3302 struct c_arg_info *args
3303 = c_parser_parms_declarator (parser, kind == C_DTR_NORMAL,
3304 attrs);
3305 if (args == NULL)
3306 return NULL;
3307 else
3308 {
3309 inner
3310 = build_function_declarator (args,
3311 build_id_declarator (NULL_TREE));
3312 return c_parser_direct_declarator_inner (parser, *seen_id,
3313 inner);
3314 }
3315 }
3316 /* A parenthesized declarator. */
3317 inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
3318 if (inner != NULL && attrs != NULL)
3319 inner = build_attrs_declarator (attrs, inner);
3320 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3321 {
3322 c_parser_consume_token (parser);
3323 if (inner == NULL)
3324 return NULL;
3325 else
3326 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
3327 }
3328 else
3329 {
3330 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3331 "expected %<)%>");
3332 return NULL;
3333 }
3334 }
3335 else
3336 {
3337 if (kind == C_DTR_NORMAL)
3338 {
3339 c_parser_error (parser, "expected identifier or %<(%>");
3340 return NULL;
3341 }
3342 else
3343 return build_id_declarator (NULL_TREE);
3344 }
3345 }
3346
3347 /* Parse part of a direct declarator or direct abstract declarator,
3348 given that some (in INNER) has already been parsed; ID_PRESENT is
3349 true if an identifier is present, false for an abstract
3350 declarator. */
3351
3352 static struct c_declarator *
3353 c_parser_direct_declarator_inner (c_parser *parser, bool id_present,
3354 struct c_declarator *inner)
3355 {
3356 /* Parse a sequence of array declarators and parameter lists. */
3357 if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
3358 {
3359 location_t brace_loc = c_parser_peek_token (parser)->location;
3360 struct c_declarator *declarator;
3361 struct c_declspecs *quals_attrs = build_null_declspecs ();
3362 bool static_seen;
3363 bool star_seen;
3364 struct c_expr dimen;
3365 dimen.value = NULL_TREE;
3366 dimen.original_code = ERROR_MARK;
3367 dimen.original_type = NULL_TREE;
3368 c_parser_consume_token (parser);
3369 c_parser_declspecs (parser, quals_attrs, false, false, true,
3370 false, false, cla_prefer_id);
3371 static_seen = c_parser_next_token_is_keyword (parser, RID_STATIC);
3372 if (static_seen)
3373 c_parser_consume_token (parser);
3374 if (static_seen && !quals_attrs->declspecs_seen_p)
3375 c_parser_declspecs (parser, quals_attrs, false, false, true,
3376 false, false, cla_prefer_id);
3377 if (!quals_attrs->declspecs_seen_p)
3378 quals_attrs = NULL;
3379 /* If "static" is present, there must be an array dimension.
3380 Otherwise, there may be a dimension, "*", or no
3381 dimension. */
3382 if (static_seen)
3383 {
3384 star_seen = false;
3385 dimen = c_parser_expr_no_commas (parser, NULL);
3386 }
3387 else
3388 {
3389 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3390 {
3391 dimen.value = NULL_TREE;
3392 star_seen = false;
3393 }
3394 else if (flag_cilkplus
3395 && c_parser_next_token_is (parser, CPP_COLON))
3396 {
3397 dimen.value = error_mark_node;
3398 star_seen = false;
3399 error_at (c_parser_peek_token (parser)->location,
3400 "array notations cannot be used in declaration");
3401 c_parser_consume_token (parser);
3402 }
3403 else if (c_parser_next_token_is (parser, CPP_MULT))
3404 {
3405 if (c_parser_peek_2nd_token (parser)->type == CPP_CLOSE_SQUARE)
3406 {
3407 dimen.value = NULL_TREE;
3408 star_seen = true;
3409 c_parser_consume_token (parser);
3410 }
3411 else
3412 {
3413 star_seen = false;
3414 dimen = c_parser_expr_no_commas (parser, NULL);
3415 }
3416 }
3417 else
3418 {
3419 star_seen = false;
3420 dimen = c_parser_expr_no_commas (parser, NULL);
3421 }
3422 }
3423 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3424 c_parser_consume_token (parser);
3425 else if (flag_cilkplus
3426 && c_parser_next_token_is (parser, CPP_COLON))
3427 {
3428 error_at (c_parser_peek_token (parser)->location,
3429 "array notations cannot be used in declaration");
3430 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
3431 return NULL;
3432 }
3433 else
3434 {
3435 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
3436 "expected %<]%>");
3437 return NULL;
3438 }
3439 if (dimen.value)
3440 dimen = convert_lvalue_to_rvalue (brace_loc, dimen, true, true);
3441 declarator = build_array_declarator (brace_loc, dimen.value, quals_attrs,
3442 static_seen, star_seen);
3443 if (declarator == NULL)
3444 return NULL;
3445 inner = set_array_declarator_inner (declarator, inner);
3446 return c_parser_direct_declarator_inner (parser, id_present, inner);
3447 }
3448 else if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
3449 {
3450 tree attrs;
3451 struct c_arg_info *args;
3452 c_parser_consume_token (parser);
3453 attrs = c_parser_attributes (parser);
3454 args = c_parser_parms_declarator (parser, id_present, attrs);
3455 if (args == NULL)
3456 return NULL;
3457 else
3458 {
3459 inner = build_function_declarator (args, inner);
3460 return c_parser_direct_declarator_inner (parser, id_present, inner);
3461 }
3462 }
3463 return inner;
3464 }
3465
3466 /* Parse a parameter list or identifier list, including the closing
3467 parenthesis but not the opening one. ATTRS are the attributes at
3468 the start of the list. ID_LIST_OK is true if an identifier list is
3469 acceptable; such a list must not have attributes at the start. */
3470
3471 static struct c_arg_info *
3472 c_parser_parms_declarator (c_parser *parser, bool id_list_ok, tree attrs)
3473 {
3474 push_scope ();
3475 declare_parm_level ();
3476 /* If the list starts with an identifier, it is an identifier list.
3477 Otherwise, it is either a prototype list or an empty list. */
3478 if (id_list_ok
3479 && !attrs
3480 && c_parser_next_token_is (parser, CPP_NAME)
3481 && c_parser_peek_token (parser)->id_kind == C_ID_ID
3482
3483 /* Look ahead to detect typos in type names. */
3484 && c_parser_peek_2nd_token (parser)->type != CPP_NAME
3485 && c_parser_peek_2nd_token (parser)->type != CPP_MULT
3486 && c_parser_peek_2nd_token (parser)->type != CPP_OPEN_PAREN
3487 && c_parser_peek_2nd_token (parser)->type != CPP_OPEN_SQUARE)
3488 {
3489 tree list = NULL_TREE, *nextp = &list;
3490 while (c_parser_next_token_is (parser, CPP_NAME)
3491 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
3492 {
3493 *nextp = build_tree_list (NULL_TREE,
3494 c_parser_peek_token (parser)->value);
3495 nextp = & TREE_CHAIN (*nextp);
3496 c_parser_consume_token (parser);
3497 if (c_parser_next_token_is_not (parser, CPP_COMMA))
3498 break;
3499 c_parser_consume_token (parser);
3500 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3501 {
3502 c_parser_error (parser, "expected identifier");
3503 break;
3504 }
3505 }
3506 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3507 {
3508 struct c_arg_info *ret = build_arg_info ();
3509 ret->types = list;
3510 c_parser_consume_token (parser);
3511 pop_scope ();
3512 return ret;
3513 }
3514 else
3515 {
3516 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3517 "expected %<)%>");
3518 pop_scope ();
3519 return NULL;
3520 }
3521 }
3522 else
3523 {
3524 struct c_arg_info *ret = c_parser_parms_list_declarator (parser, attrs,
3525 NULL);
3526 pop_scope ();
3527 return ret;
3528 }
3529 }
3530
3531 /* Parse a parameter list (possibly empty), including the closing
3532 parenthesis but not the opening one. ATTRS are the attributes at
3533 the start of the list. EXPR is NULL or an expression that needs to
3534 be evaluated for the side effects of array size expressions in the
3535 parameters. */
3536
3537 static struct c_arg_info *
3538 c_parser_parms_list_declarator (c_parser *parser, tree attrs, tree expr)
3539 {
3540 bool bad_parm = false;
3541
3542 /* ??? Following the old parser, forward parameter declarations may
3543 use abstract declarators, and if no real parameter declarations
3544 follow the forward declarations then this is not diagnosed. Also
3545 note as above that attributes are ignored as the only contents of
3546 the parentheses, or as the only contents after forward
3547 declarations. */
3548 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3549 {
3550 struct c_arg_info *ret = build_arg_info ();
3551 c_parser_consume_token (parser);
3552 return ret;
3553 }
3554 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
3555 {
3556 struct c_arg_info *ret = build_arg_info ();
3557
3558 if (flag_allow_parameterless_variadic_functions)
3559 {
3560 /* F (...) is allowed. */
3561 ret->types = NULL_TREE;
3562 }
3563 else
3564 {
3565 /* Suppress -Wold-style-definition for this case. */
3566 ret->types = error_mark_node;
3567 error_at (c_parser_peek_token (parser)->location,
3568 "ISO C requires a named argument before %<...%>");
3569 }
3570 c_parser_consume_token (parser);
3571 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3572 {
3573 c_parser_consume_token (parser);
3574 return ret;
3575 }
3576 else
3577 {
3578 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3579 "expected %<)%>");
3580 return NULL;
3581 }
3582 }
3583 /* Nonempty list of parameters, either terminated with semicolon
3584 (forward declarations; recurse) or with close parenthesis (normal
3585 function) or with ", ... )" (variadic function). */
3586 while (true)
3587 {
3588 /* Parse a parameter. */
3589 struct c_parm *parm = c_parser_parameter_declaration (parser, attrs);
3590 attrs = NULL_TREE;
3591 if (parm == NULL)
3592 bad_parm = true;
3593 else
3594 push_parm_decl (parm, &expr);
3595 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
3596 {
3597 tree new_attrs;
3598 c_parser_consume_token (parser);
3599 mark_forward_parm_decls ();
3600 new_attrs = c_parser_attributes (parser);
3601 return c_parser_parms_list_declarator (parser, new_attrs, expr);
3602 }
3603 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3604 {
3605 c_parser_consume_token (parser);
3606 if (bad_parm)
3607 return NULL;
3608 else
3609 return get_parm_info (false, expr);
3610 }
3611 if (!c_parser_require (parser, CPP_COMMA,
3612 "expected %<;%>, %<,%> or %<)%>"))
3613 {
3614 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
3615 return NULL;
3616 }
3617 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
3618 {
3619 c_parser_consume_token (parser);
3620 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3621 {
3622 c_parser_consume_token (parser);
3623 if (bad_parm)
3624 return NULL;
3625 else
3626 return get_parm_info (true, expr);
3627 }
3628 else
3629 {
3630 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3631 "expected %<)%>");
3632 return NULL;
3633 }
3634 }
3635 }
3636 }
3637
3638 /* Parse a parameter declaration. ATTRS are the attributes at the
3639 start of the declaration if it is the first parameter. */
3640
3641 static struct c_parm *
3642 c_parser_parameter_declaration (c_parser *parser, tree attrs)
3643 {
3644 struct c_declspecs *specs;
3645 struct c_declarator *declarator;
3646 tree prefix_attrs;
3647 tree postfix_attrs = NULL_TREE;
3648 bool dummy = false;
3649
3650 /* Accept #pragmas between parameter declarations. */
3651 while (c_parser_next_token_is (parser, CPP_PRAGMA))
3652 c_parser_pragma (parser, pragma_param);
3653
3654 if (!c_parser_next_token_starts_declspecs (parser))
3655 {
3656 c_token *token = c_parser_peek_token (parser);
3657 if (parser->error)
3658 return NULL;
3659 c_parser_set_source_position_from_token (token);
3660 if (c_parser_next_tokens_start_typename (parser, cla_prefer_type))
3661 {
3662 error_at (token->location, "unknown type name %qE", token->value);
3663 parser->error = true;
3664 }
3665 /* ??? In some Objective-C cases '...' isn't applicable so there
3666 should be a different message. */
3667 else
3668 c_parser_error (parser,
3669 "expected declaration specifiers or %<...%>");
3670 c_parser_skip_to_end_of_parameter (parser);
3671 return NULL;
3672 }
3673 specs = build_null_declspecs ();
3674 if (attrs)
3675 {
3676 declspecs_add_attrs (input_location, specs, attrs);
3677 attrs = NULL_TREE;
3678 }
3679 c_parser_declspecs (parser, specs, true, true, true, true, false,
3680 cla_nonabstract_decl);
3681 finish_declspecs (specs);
3682 pending_xref_error ();
3683 prefix_attrs = specs->attrs;
3684 specs->attrs = NULL_TREE;
3685 declarator = c_parser_declarator (parser,
3686 specs->typespec_kind != ctsk_none,
3687 C_DTR_PARM, &dummy);
3688 if (declarator == NULL)
3689 {
3690 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
3691 return NULL;
3692 }
3693 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
3694 postfix_attrs = c_parser_attributes (parser);
3695 return build_c_parm (specs, chainon (postfix_attrs, prefix_attrs),
3696 declarator);
3697 }
3698
3699 /* Parse a string literal in an asm expression. It should not be
3700 translated, and wide string literals are an error although
3701 permitted by the syntax. This is a GNU extension.
3702
3703 asm-string-literal:
3704 string-literal
3705
3706 ??? At present, following the old parser, the caller needs to have
3707 set lex_untranslated_string to 1. It would be better to follow the
3708 C++ parser rather than using this kludge. */
3709
3710 static tree
3711 c_parser_asm_string_literal (c_parser *parser)
3712 {
3713 tree str;
3714 int save_flag = warn_overlength_strings;
3715 warn_overlength_strings = 0;
3716 if (c_parser_next_token_is (parser, CPP_STRING))
3717 {
3718 str = c_parser_peek_token (parser)->value;
3719 c_parser_consume_token (parser);
3720 }
3721 else if (c_parser_next_token_is (parser, CPP_WSTRING))
3722 {
3723 error_at (c_parser_peek_token (parser)->location,
3724 "wide string literal in %<asm%>");
3725 str = build_string (1, "");
3726 c_parser_consume_token (parser);
3727 }
3728 else
3729 {
3730 c_parser_error (parser, "expected string literal");
3731 str = NULL_TREE;
3732 }
3733 warn_overlength_strings = save_flag;
3734 return str;
3735 }
3736
3737 /* Parse a simple asm expression. This is used in restricted
3738 contexts, where a full expression with inputs and outputs does not
3739 make sense. This is a GNU extension.
3740
3741 simple-asm-expr:
3742 asm ( asm-string-literal )
3743 */
3744
3745 static tree
3746 c_parser_simple_asm_expr (c_parser *parser)
3747 {
3748 tree str;
3749 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM));
3750 /* ??? Follow the C++ parser rather than using the
3751 lex_untranslated_string kludge. */
3752 parser->lex_untranslated_string = true;
3753 c_parser_consume_token (parser);
3754 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3755 {
3756 parser->lex_untranslated_string = false;
3757 return NULL_TREE;
3758 }
3759 str = c_parser_asm_string_literal (parser);
3760 parser->lex_untranslated_string = false;
3761 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
3762 {
3763 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
3764 return NULL_TREE;
3765 }
3766 return str;
3767 }
3768
3769 static tree
3770 c_parser_attribute_any_word (c_parser *parser)
3771 {
3772 tree attr_name = NULL_TREE;
3773
3774 if (c_parser_next_token_is (parser, CPP_KEYWORD))
3775 {
3776 /* ??? See comment above about what keywords are accepted here. */
3777 bool ok;
3778 switch (c_parser_peek_token (parser)->keyword)
3779 {
3780 case RID_STATIC:
3781 case RID_UNSIGNED:
3782 case RID_LONG:
3783 case RID_CONST:
3784 case RID_EXTERN:
3785 case RID_REGISTER:
3786 case RID_TYPEDEF:
3787 case RID_SHORT:
3788 case RID_INLINE:
3789 case RID_NORETURN:
3790 case RID_VOLATILE:
3791 case RID_SIGNED:
3792 case RID_AUTO:
3793 case RID_RESTRICT:
3794 case RID_COMPLEX:
3795 case RID_THREAD:
3796 case RID_INT:
3797 case RID_CHAR:
3798 case RID_FLOAT:
3799 case RID_DOUBLE:
3800 case RID_VOID:
3801 case RID_DFLOAT32:
3802 case RID_DFLOAT64:
3803 case RID_DFLOAT128:
3804 case RID_BOOL:
3805 case RID_FRACT:
3806 case RID_ACCUM:
3807 case RID_SAT:
3808 case RID_TRANSACTION_ATOMIC:
3809 case RID_TRANSACTION_CANCEL:
3810 case RID_ATOMIC:
3811 case RID_AUTO_TYPE:
3812 case RID_INT_N_0:
3813 case RID_INT_N_1:
3814 case RID_INT_N_2:
3815 case RID_INT_N_3:
3816 ok = true;
3817 break;
3818 default:
3819 ok = false;
3820 break;
3821 }
3822 if (!ok)
3823 return NULL_TREE;
3824
3825 /* Accept __attribute__((__const)) as __attribute__((const)) etc. */
3826 attr_name = ridpointers[(int) c_parser_peek_token (parser)->keyword];
3827 }
3828 else if (c_parser_next_token_is (parser, CPP_NAME))
3829 attr_name = c_parser_peek_token (parser)->value;
3830
3831 return attr_name;
3832 }
3833
3834 /* Returns true of NAME is an IDENTIFIER_NODE with identiifer "vector,"
3835 "__vector" or "__vector__." */
3836
3837 static inline bool
3838 is_cilkplus_vector_p (tree name)
3839 {
3840 if (flag_cilkplus && is_attribute_p ("vector", name))
3841 return true;
3842 return false;
3843 }
3844
3845 #define CILK_SIMD_FN_CLAUSE_MASK \
3846 ((OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_VECTORLENGTH) \
3847 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_LINEAR) \
3848 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_UNIFORM) \
3849 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_MASK) \
3850 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_NOMASK))
3851
3852 /* Parses the vector attribute of SIMD enabled functions in Cilk Plus.
3853 VEC_TOKEN is the "vector" token that is replaced with "simd" and
3854 pushed into the token list.
3855 Syntax:
3856 vector
3857 vector (<vector attributes>). */
3858
3859 static void
3860 c_parser_cilk_simd_fn_vector_attrs (c_parser *parser, c_token vec_token)
3861 {
3862 gcc_assert (is_cilkplus_vector_p (vec_token.value));
3863
3864 int paren_scope = 0;
3865 vec_safe_push (parser->cilk_simd_fn_tokens, vec_token);
3866 /* Consume the "vector" token. */
3867 c_parser_consume_token (parser);
3868
3869 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
3870 {
3871 c_parser_consume_token (parser);
3872 paren_scope++;
3873 }
3874 while (paren_scope > 0)
3875 {
3876 c_token *token = c_parser_peek_token (parser);
3877 if (token->type == CPP_OPEN_PAREN)
3878 paren_scope++;
3879 else if (token->type == CPP_CLOSE_PAREN)
3880 paren_scope--;
3881 /* Do not push the last ')' since we are not pushing the '('. */
3882 if (!(token->type == CPP_CLOSE_PAREN && paren_scope == 0))
3883 vec_safe_push (parser->cilk_simd_fn_tokens, *token);
3884 c_parser_consume_token (parser);
3885 }
3886
3887 /* Since we are converting an attribute to a pragma, we need to end the
3888 attribute with PRAGMA_EOL. */
3889 c_token eol_token;
3890 memset (&eol_token, 0, sizeof (eol_token));
3891 eol_token.type = CPP_PRAGMA_EOL;
3892 vec_safe_push (parser->cilk_simd_fn_tokens, eol_token);
3893 }
3894
3895 /* Add 2 CPP_EOF at the end of PARSER->ELEM_FN_TOKENS vector. */
3896
3897 static void
3898 c_finish_cilk_simd_fn_tokens (c_parser *parser)
3899 {
3900 c_token last_token = parser->cilk_simd_fn_tokens->last ();
3901
3902 /* c_parser_attributes is called in several places, so if these EOF
3903 tokens are already inserted, then don't do them again. */
3904 if (last_token.type == CPP_EOF)
3905 return;
3906
3907 /* Two CPP_EOF token are added as a safety net since the normal C
3908 front-end has two token look-ahead. */
3909 c_token eof_token;
3910 eof_token.type = CPP_EOF;
3911 vec_safe_push (parser->cilk_simd_fn_tokens, eof_token);
3912 vec_safe_push (parser->cilk_simd_fn_tokens, eof_token);
3913 }
3914
3915 /* Parse (possibly empty) attributes. This is a GNU extension.
3916
3917 attributes:
3918 empty
3919 attributes attribute
3920
3921 attribute:
3922 __attribute__ ( ( attribute-list ) )
3923
3924 attribute-list:
3925 attrib
3926 attribute_list , attrib
3927
3928 attrib:
3929 empty
3930 any-word
3931 any-word ( identifier )
3932 any-word ( identifier , nonempty-expr-list )
3933 any-word ( expr-list )
3934
3935 where the "identifier" must not be declared as a type, and
3936 "any-word" may be any identifier (including one declared as a
3937 type), a reserved word storage class specifier, type specifier or
3938 type qualifier. ??? This still leaves out most reserved keywords
3939 (following the old parser), shouldn't we include them, and why not
3940 allow identifiers declared as types to start the arguments? */
3941
3942 static tree
3943 c_parser_attributes (c_parser *parser)
3944 {
3945 tree attrs = NULL_TREE;
3946 while (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
3947 {
3948 /* ??? Follow the C++ parser rather than using the
3949 lex_untranslated_string kludge. */
3950 parser->lex_untranslated_string = true;
3951 c_parser_consume_token (parser);
3952 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3953 {
3954 parser->lex_untranslated_string = false;
3955 return attrs;
3956 }
3957 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3958 {
3959 parser->lex_untranslated_string = false;
3960 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
3961 return attrs;
3962 }
3963 /* Parse the attribute list. */
3964 while (c_parser_next_token_is (parser, CPP_COMMA)
3965 || c_parser_next_token_is (parser, CPP_NAME)
3966 || c_parser_next_token_is (parser, CPP_KEYWORD))
3967 {
3968 tree attr, attr_name, attr_args;
3969 vec<tree, va_gc> *expr_list;
3970 if (c_parser_next_token_is (parser, CPP_COMMA))
3971 {
3972 c_parser_consume_token (parser);
3973 continue;
3974 }
3975
3976 attr_name = c_parser_attribute_any_word (parser);
3977 if (attr_name == NULL)
3978 break;
3979 if (is_cilkplus_vector_p (attr_name))
3980 {
3981 c_token *v_token = c_parser_peek_token (parser);
3982 c_parser_cilk_simd_fn_vector_attrs (parser, *v_token);
3983 continue;
3984 }
3985 c_parser_consume_token (parser);
3986 if (c_parser_next_token_is_not (parser, CPP_OPEN_PAREN))
3987 {
3988 attr = build_tree_list (attr_name, NULL_TREE);
3989 attrs = chainon (attrs, attr);
3990 continue;
3991 }
3992 c_parser_consume_token (parser);
3993 /* Parse the attribute contents. If they start with an
3994 identifier which is followed by a comma or close
3995 parenthesis, then the arguments start with that
3996 identifier; otherwise they are an expression list.
3997 In objective-c the identifier may be a classname. */
3998 if (c_parser_next_token_is (parser, CPP_NAME)
3999 && (c_parser_peek_token (parser)->id_kind == C_ID_ID
4000 || (c_dialect_objc ()
4001 && c_parser_peek_token (parser)->id_kind
4002 == C_ID_CLASSNAME))
4003 && ((c_parser_peek_2nd_token (parser)->type == CPP_COMMA)
4004 || (c_parser_peek_2nd_token (parser)->type
4005 == CPP_CLOSE_PAREN))
4006 && (attribute_takes_identifier_p (attr_name)
4007 || (c_dialect_objc ()
4008 && c_parser_peek_token (parser)->id_kind
4009 == C_ID_CLASSNAME)))
4010 {
4011 tree arg1 = c_parser_peek_token (parser)->value;
4012 c_parser_consume_token (parser);
4013 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4014 attr_args = build_tree_list (NULL_TREE, arg1);
4015 else
4016 {
4017 tree tree_list;
4018 c_parser_consume_token (parser);
4019 expr_list = c_parser_expr_list (parser, false, true,
4020 NULL, NULL, NULL, NULL);
4021 tree_list = build_tree_list_vec (expr_list);
4022 attr_args = tree_cons (NULL_TREE, arg1, tree_list);
4023 release_tree_vector (expr_list);
4024 }
4025 }
4026 else
4027 {
4028 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4029 attr_args = NULL_TREE;
4030 else
4031 {
4032 expr_list = c_parser_expr_list (parser, false, true,
4033 NULL, NULL, NULL, NULL);
4034 attr_args = build_tree_list_vec (expr_list);
4035 release_tree_vector (expr_list);
4036 }
4037 }
4038 attr = build_tree_list (attr_name, attr_args);
4039 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4040 c_parser_consume_token (parser);
4041 else
4042 {
4043 parser->lex_untranslated_string = false;
4044 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4045 "expected %<)%>");
4046 return attrs;
4047 }
4048 attrs = chainon (attrs, attr);
4049 }
4050 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4051 c_parser_consume_token (parser);
4052 else
4053 {
4054 parser->lex_untranslated_string = false;
4055 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4056 "expected %<)%>");
4057 return attrs;
4058 }
4059 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4060 c_parser_consume_token (parser);
4061 else
4062 {
4063 parser->lex_untranslated_string = false;
4064 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4065 "expected %<)%>");
4066 return attrs;
4067 }
4068 parser->lex_untranslated_string = false;
4069 }
4070
4071 if (flag_cilkplus && !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
4072 c_finish_cilk_simd_fn_tokens (parser);
4073 return attrs;
4074 }
4075
4076 /* Parse a type name (C90 6.5.5, C99 6.7.6).
4077
4078 type-name:
4079 specifier-qualifier-list abstract-declarator[opt]
4080 */
4081
4082 static struct c_type_name *
4083 c_parser_type_name (c_parser *parser)
4084 {
4085 struct c_declspecs *specs = build_null_declspecs ();
4086 struct c_declarator *declarator;
4087 struct c_type_name *ret;
4088 bool dummy = false;
4089 c_parser_declspecs (parser, specs, false, true, true, false, false,
4090 cla_prefer_type);
4091 if (!specs->declspecs_seen_p)
4092 {
4093 c_parser_error (parser, "expected specifier-qualifier-list");
4094 return NULL;
4095 }
4096 if (specs->type != error_mark_node)
4097 {
4098 pending_xref_error ();
4099 finish_declspecs (specs);
4100 }
4101 declarator = c_parser_declarator (parser,
4102 specs->typespec_kind != ctsk_none,
4103 C_DTR_ABSTRACT, &dummy);
4104 if (declarator == NULL)
4105 return NULL;
4106 ret = XOBNEW (&parser_obstack, struct c_type_name);
4107 ret->specs = specs;
4108 ret->declarator = declarator;
4109 return ret;
4110 }
4111
4112 /* Parse an initializer (C90 6.5.7, C99 6.7.8).
4113
4114 initializer:
4115 assignment-expression
4116 { initializer-list }
4117 { initializer-list , }
4118
4119 initializer-list:
4120 designation[opt] initializer
4121 initializer-list , designation[opt] initializer
4122
4123 designation:
4124 designator-list =
4125
4126 designator-list:
4127 designator
4128 designator-list designator
4129
4130 designator:
4131 array-designator
4132 . identifier
4133
4134 array-designator:
4135 [ constant-expression ]
4136
4137 GNU extensions:
4138
4139 initializer:
4140 { }
4141
4142 designation:
4143 array-designator
4144 identifier :
4145
4146 array-designator:
4147 [ constant-expression ... constant-expression ]
4148
4149 Any expression without commas is accepted in the syntax for the
4150 constant-expressions, with non-constant expressions rejected later.
4151
4152 This function is only used for top-level initializers; for nested
4153 ones, see c_parser_initval. */
4154
4155 static struct c_expr
4156 c_parser_initializer (c_parser *parser)
4157 {
4158 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
4159 return c_parser_braced_init (parser, NULL_TREE, false);
4160 else
4161 {
4162 struct c_expr ret;
4163 location_t loc = c_parser_peek_token (parser)->location;
4164 ret = c_parser_expr_no_commas (parser, NULL);
4165 if (TREE_CODE (ret.value) != STRING_CST
4166 && TREE_CODE (ret.value) != COMPOUND_LITERAL_EXPR)
4167 ret = convert_lvalue_to_rvalue (loc, ret, true, true);
4168 return ret;
4169 }
4170 }
4171
4172 /* Parse a braced initializer list. TYPE is the type specified for a
4173 compound literal, and NULL_TREE for other initializers and for
4174 nested braced lists. NESTED_P is true for nested braced lists,
4175 false for the list of a compound literal or the list that is the
4176 top-level initializer in a declaration. */
4177
4178 static struct c_expr
4179 c_parser_braced_init (c_parser *parser, tree type, bool nested_p)
4180 {
4181 struct c_expr ret;
4182 struct obstack braced_init_obstack;
4183 location_t brace_loc = c_parser_peek_token (parser)->location;
4184 gcc_obstack_init (&braced_init_obstack);
4185 gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE));
4186 c_parser_consume_token (parser);
4187 if (nested_p)
4188 push_init_level (brace_loc, 0, &braced_init_obstack);
4189 else
4190 really_start_incremental_init (type);
4191 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
4192 {
4193 pedwarn (brace_loc, OPT_Wpedantic, "ISO C forbids empty initializer braces");
4194 }
4195 else
4196 {
4197 /* Parse a non-empty initializer list, possibly with a trailing
4198 comma. */
4199 while (true)
4200 {
4201 c_parser_initelt (parser, &braced_init_obstack);
4202 if (parser->error)
4203 break;
4204 if (c_parser_next_token_is (parser, CPP_COMMA))
4205 c_parser_consume_token (parser);
4206 else
4207 break;
4208 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
4209 break;
4210 }
4211 }
4212 if (c_parser_next_token_is_not (parser, CPP_CLOSE_BRACE))
4213 {
4214 ret.value = error_mark_node;
4215 ret.original_code = ERROR_MARK;
4216 ret.original_type = NULL;
4217 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, "expected %<}%>");
4218 pop_init_level (brace_loc, 0, &braced_init_obstack);
4219 obstack_free (&braced_init_obstack, NULL);
4220 return ret;
4221 }
4222 c_parser_consume_token (parser);
4223 ret = pop_init_level (brace_loc, 0, &braced_init_obstack);
4224 obstack_free (&braced_init_obstack, NULL);
4225 return ret;
4226 }
4227
4228 /* Parse a nested initializer, including designators. */
4229
4230 static void
4231 c_parser_initelt (c_parser *parser, struct obstack * braced_init_obstack)
4232 {
4233 /* Parse any designator or designator list. A single array
4234 designator may have the subsequent "=" omitted in GNU C, but a
4235 longer list or a structure member designator may not. */
4236 if (c_parser_next_token_is (parser, CPP_NAME)
4237 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
4238 {
4239 /* Old-style structure member designator. */
4240 set_init_label (c_parser_peek_token (parser)->location,
4241 c_parser_peek_token (parser)->value,
4242 braced_init_obstack);
4243 /* Use the colon as the error location. */
4244 pedwarn (c_parser_peek_2nd_token (parser)->location, OPT_Wpedantic,
4245 "obsolete use of designated initializer with %<:%>");
4246 c_parser_consume_token (parser);
4247 c_parser_consume_token (parser);
4248 }
4249 else
4250 {
4251 /* des_seen is 0 if there have been no designators, 1 if there
4252 has been a single array designator and 2 otherwise. */
4253 int des_seen = 0;
4254 /* Location of a designator. */
4255 location_t des_loc = UNKNOWN_LOCATION; /* Quiet warning. */
4256 while (c_parser_next_token_is (parser, CPP_OPEN_SQUARE)
4257 || c_parser_next_token_is (parser, CPP_DOT))
4258 {
4259 int des_prev = des_seen;
4260 if (!des_seen)
4261 des_loc = c_parser_peek_token (parser)->location;
4262 if (des_seen < 2)
4263 des_seen++;
4264 if (c_parser_next_token_is (parser, CPP_DOT))
4265 {
4266 des_seen = 2;
4267 c_parser_consume_token (parser);
4268 if (c_parser_next_token_is (parser, CPP_NAME))
4269 {
4270 set_init_label (des_loc, c_parser_peek_token (parser)->value,
4271 braced_init_obstack);
4272 c_parser_consume_token (parser);
4273 }
4274 else
4275 {
4276 struct c_expr init;
4277 init.value = error_mark_node;
4278 init.original_code = ERROR_MARK;
4279 init.original_type = NULL;
4280 c_parser_error (parser, "expected identifier");
4281 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
4282 process_init_element (input_location, init, false,
4283 braced_init_obstack);
4284 return;
4285 }
4286 }
4287 else
4288 {
4289 tree first, second;
4290 location_t ellipsis_loc = UNKNOWN_LOCATION; /* Quiet warning. */
4291 location_t array_index_loc = UNKNOWN_LOCATION;
4292 /* ??? Following the old parser, [ objc-receiver
4293 objc-message-args ] is accepted as an initializer,
4294 being distinguished from a designator by what follows
4295 the first assignment expression inside the square
4296 brackets, but after a first array designator a
4297 subsequent square bracket is for Objective-C taken to
4298 start an expression, using the obsolete form of
4299 designated initializer without '=', rather than
4300 possibly being a second level of designation: in LALR
4301 terms, the '[' is shifted rather than reducing
4302 designator to designator-list. */
4303 if (des_prev == 1 && c_dialect_objc ())
4304 {
4305 des_seen = des_prev;
4306 break;
4307 }
4308 if (des_prev == 0 && c_dialect_objc ())
4309 {
4310 /* This might be an array designator or an
4311 Objective-C message expression. If the former,
4312 continue parsing here; if the latter, parse the
4313 remainder of the initializer given the starting
4314 primary-expression. ??? It might make sense to
4315 distinguish when des_prev == 1 as well; see
4316 previous comment. */
4317 tree rec, args;
4318 struct c_expr mexpr;
4319 c_parser_consume_token (parser);
4320 if (c_parser_peek_token (parser)->type == CPP_NAME
4321 && ((c_parser_peek_token (parser)->id_kind
4322 == C_ID_TYPENAME)
4323 || (c_parser_peek_token (parser)->id_kind
4324 == C_ID_CLASSNAME)))
4325 {
4326 /* Type name receiver. */
4327 tree id = c_parser_peek_token (parser)->value;
4328 c_parser_consume_token (parser);
4329 rec = objc_get_class_reference (id);
4330 goto parse_message_args;
4331 }
4332 first = c_parser_expr_no_commas (parser, NULL).value;
4333 mark_exp_read (first);
4334 if (c_parser_next_token_is (parser, CPP_ELLIPSIS)
4335 || c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
4336 goto array_desig_after_first;
4337 /* Expression receiver. So far only one part
4338 without commas has been parsed; there might be
4339 more of the expression. */
4340 rec = first;
4341 while (c_parser_next_token_is (parser, CPP_COMMA))
4342 {
4343 struct c_expr next;
4344 location_t comma_loc, exp_loc;
4345 comma_loc = c_parser_peek_token (parser)->location;
4346 c_parser_consume_token (parser);
4347 exp_loc = c_parser_peek_token (parser)->location;
4348 next = c_parser_expr_no_commas (parser, NULL);
4349 next = convert_lvalue_to_rvalue (exp_loc, next,
4350 true, true);
4351 rec = build_compound_expr (comma_loc, rec, next.value);
4352 }
4353 parse_message_args:
4354 /* Now parse the objc-message-args. */
4355 args = c_parser_objc_message_args (parser);
4356 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
4357 "expected %<]%>");
4358 mexpr.value
4359 = objc_build_message_expr (rec, args);
4360 mexpr.original_code = ERROR_MARK;
4361 mexpr.original_type = NULL;
4362 /* Now parse and process the remainder of the
4363 initializer, starting with this message
4364 expression as a primary-expression. */
4365 c_parser_initval (parser, &mexpr, braced_init_obstack);
4366 return;
4367 }
4368 c_parser_consume_token (parser);
4369 array_index_loc = c_parser_peek_token (parser)->location;
4370 first = c_parser_expr_no_commas (parser, NULL).value;
4371 mark_exp_read (first);
4372 array_desig_after_first:
4373 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
4374 {
4375 ellipsis_loc = c_parser_peek_token (parser)->location;
4376 c_parser_consume_token (parser);
4377 second = c_parser_expr_no_commas (parser, NULL).value;
4378 mark_exp_read (second);
4379 }
4380 else
4381 second = NULL_TREE;
4382 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
4383 {
4384 c_parser_consume_token (parser);
4385 set_init_index (array_index_loc, first, second,
4386 braced_init_obstack);
4387 if (second)
4388 pedwarn (ellipsis_loc, OPT_Wpedantic,
4389 "ISO C forbids specifying range of elements to initialize");
4390 }
4391 else
4392 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
4393 "expected %<]%>");
4394 }
4395 }
4396 if (des_seen >= 1)
4397 {
4398 if (c_parser_next_token_is (parser, CPP_EQ))
4399 {
4400 pedwarn_c90 (des_loc, OPT_Wpedantic,
4401 "ISO C90 forbids specifying subobject "
4402 "to initialize");
4403 c_parser_consume_token (parser);
4404 }
4405 else
4406 {
4407 if (des_seen == 1)
4408 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
4409 "obsolete use of designated initializer without %<=%>");
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 %<=%>");
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 }
4424 }
4425 c_parser_initval (parser, NULL, braced_init_obstack);
4426 }
4427
4428 /* Parse a nested initializer; as c_parser_initializer but parses
4429 initializers within braced lists, after any designators have been
4430 applied. If AFTER is not NULL then it is an Objective-C message
4431 expression which is the primary-expression starting the
4432 initializer. */
4433
4434 static void
4435 c_parser_initval (c_parser *parser, struct c_expr *after,
4436 struct obstack * braced_init_obstack)
4437 {
4438 struct c_expr init;
4439 gcc_assert (!after || c_dialect_objc ());
4440 location_t loc = c_parser_peek_token (parser)->location;
4441
4442 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE) && !after)
4443 init = c_parser_braced_init (parser, NULL_TREE, true);
4444 else
4445 {
4446 init = c_parser_expr_no_commas (parser, after);
4447 if (init.value != NULL_TREE
4448 && TREE_CODE (init.value) != STRING_CST
4449 && TREE_CODE (init.value) != COMPOUND_LITERAL_EXPR)
4450 init = convert_lvalue_to_rvalue (loc, init, true, true);
4451 }
4452 process_init_element (loc, init, false, braced_init_obstack);
4453 }
4454
4455 /* Parse a compound statement (possibly a function body) (C90 6.6.2,
4456 C99 6.8.2).
4457
4458 compound-statement:
4459 { block-item-list[opt] }
4460 { label-declarations block-item-list }
4461
4462 block-item-list:
4463 block-item
4464 block-item-list block-item
4465
4466 block-item:
4467 nested-declaration
4468 statement
4469
4470 nested-declaration:
4471 declaration
4472
4473 GNU extensions:
4474
4475 compound-statement:
4476 { label-declarations block-item-list }
4477
4478 nested-declaration:
4479 __extension__ nested-declaration
4480 nested-function-definition
4481
4482 label-declarations:
4483 label-declaration
4484 label-declarations label-declaration
4485
4486 label-declaration:
4487 __label__ identifier-list ;
4488
4489 Allowing the mixing of declarations and code is new in C99. The
4490 GNU syntax also permits (not shown above) labels at the end of
4491 compound statements, which yield an error. We don't allow labels
4492 on declarations; this might seem like a natural extension, but
4493 there would be a conflict between attributes on the label and
4494 prefix attributes on the declaration. ??? The syntax follows the
4495 old parser in requiring something after label declarations.
4496 Although they are erroneous if the labels declared aren't defined,
4497 is it useful for the syntax to be this way?
4498
4499 OpenACC:
4500
4501 block-item:
4502 openacc-directive
4503
4504 openacc-directive:
4505 update-directive
4506
4507 OpenMP:
4508
4509 block-item:
4510 openmp-directive
4511
4512 openmp-directive:
4513 barrier-directive
4514 flush-directive
4515 taskwait-directive
4516 taskyield-directive
4517 cancel-directive
4518 cancellation-point-directive */
4519
4520 static tree
4521 c_parser_compound_statement (c_parser *parser)
4522 {
4523 tree stmt;
4524 location_t brace_loc;
4525 brace_loc = c_parser_peek_token (parser)->location;
4526 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
4527 {
4528 /* Ensure a scope is entered and left anyway to avoid confusion
4529 if we have just prepared to enter a function body. */
4530 stmt = c_begin_compound_stmt (true);
4531 c_end_compound_stmt (brace_loc, stmt, true);
4532 return error_mark_node;
4533 }
4534 stmt = c_begin_compound_stmt (true);
4535 c_parser_compound_statement_nostart (parser);
4536
4537 /* If the compound stmt contains array notations, then we expand them. */
4538 if (flag_cilkplus && contains_array_notation_expr (stmt))
4539 stmt = expand_array_notation_exprs (stmt);
4540 return c_end_compound_stmt (brace_loc, stmt, true);
4541 }
4542
4543 /* Parse a compound statement except for the opening brace. This is
4544 used for parsing both compound statements and statement expressions
4545 (which follow different paths to handling the opening). */
4546
4547 static void
4548 c_parser_compound_statement_nostart (c_parser *parser)
4549 {
4550 bool last_stmt = false;
4551 bool last_label = false;
4552 bool save_valid_for_pragma = valid_location_for_stdc_pragma_p ();
4553 location_t label_loc = UNKNOWN_LOCATION; /* Quiet warning. */
4554 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
4555 {
4556 c_parser_consume_token (parser);
4557 return;
4558 }
4559 mark_valid_location_for_stdc_pragma (true);
4560 if (c_parser_next_token_is_keyword (parser, RID_LABEL))
4561 {
4562 /* Read zero or more forward-declarations for labels that nested
4563 functions can jump to. */
4564 mark_valid_location_for_stdc_pragma (false);
4565 while (c_parser_next_token_is_keyword (parser, RID_LABEL))
4566 {
4567 label_loc = c_parser_peek_token (parser)->location;
4568 c_parser_consume_token (parser);
4569 /* Any identifiers, including those declared as type names,
4570 are OK here. */
4571 while (true)
4572 {
4573 tree label;
4574 if (c_parser_next_token_is_not (parser, CPP_NAME))
4575 {
4576 c_parser_error (parser, "expected identifier");
4577 break;
4578 }
4579 label
4580 = declare_label (c_parser_peek_token (parser)->value);
4581 C_DECLARED_LABEL_FLAG (label) = 1;
4582 add_stmt (build_stmt (label_loc, DECL_EXPR, label));
4583 c_parser_consume_token (parser);
4584 if (c_parser_next_token_is (parser, CPP_COMMA))
4585 c_parser_consume_token (parser);
4586 else
4587 break;
4588 }
4589 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
4590 }
4591 pedwarn (label_loc, OPT_Wpedantic, "ISO C forbids label declarations");
4592 }
4593 /* We must now have at least one statement, label or declaration. */
4594 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
4595 {
4596 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
4597 c_parser_error (parser, "expected declaration or statement");
4598 c_parser_consume_token (parser);
4599 return;
4600 }
4601 while (c_parser_next_token_is_not (parser, CPP_CLOSE_BRACE))
4602 {
4603 location_t loc = c_parser_peek_token (parser)->location;
4604 if (c_parser_next_token_is_keyword (parser, RID_CASE)
4605 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
4606 || (c_parser_next_token_is (parser, CPP_NAME)
4607 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
4608 {
4609 if (c_parser_next_token_is_keyword (parser, RID_CASE))
4610 label_loc = c_parser_peek_2nd_token (parser)->location;
4611 else
4612 label_loc = c_parser_peek_token (parser)->location;
4613 last_label = true;
4614 last_stmt = false;
4615 mark_valid_location_for_stdc_pragma (false);
4616 c_parser_label (parser);
4617 }
4618 else if (!last_label
4619 && c_parser_next_tokens_start_declaration (parser))
4620 {
4621 last_label = false;
4622 mark_valid_location_for_stdc_pragma (false);
4623 c_parser_declaration_or_fndef (parser, true, true, true, true,
4624 true, NULL, vNULL);
4625 if (last_stmt)
4626 pedwarn_c90 (loc, OPT_Wdeclaration_after_statement,
4627 "ISO C90 forbids mixed declarations and code");
4628 last_stmt = false;
4629 }
4630 else if (!last_label
4631 && c_parser_next_token_is_keyword (parser, RID_EXTENSION))
4632 {
4633 /* __extension__ can start a declaration, but is also an
4634 unary operator that can start an expression. Consume all
4635 but the last of a possible series of __extension__ to
4636 determine which. */
4637 while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
4638 && (c_parser_peek_2nd_token (parser)->keyword
4639 == RID_EXTENSION))
4640 c_parser_consume_token (parser);
4641 if (c_token_starts_declaration (c_parser_peek_2nd_token (parser)))
4642 {
4643 int ext;
4644 ext = disable_extension_diagnostics ();
4645 c_parser_consume_token (parser);
4646 last_label = false;
4647 mark_valid_location_for_stdc_pragma (false);
4648 c_parser_declaration_or_fndef (parser, true, true, true, true,
4649 true, NULL, vNULL);
4650 /* Following the old parser, __extension__ does not
4651 disable this diagnostic. */
4652 restore_extension_diagnostics (ext);
4653 if (last_stmt)
4654 pedwarn_c90 (loc, OPT_Wdeclaration_after_statement,
4655 "ISO C90 forbids mixed declarations and code");
4656 last_stmt = false;
4657 }
4658 else
4659 goto statement;
4660 }
4661 else if (c_parser_next_token_is (parser, CPP_PRAGMA))
4662 {
4663 /* External pragmas, and some omp pragmas, are not associated
4664 with regular c code, and so are not to be considered statements
4665 syntactically. This ensures that the user doesn't put them
4666 places that would turn into syntax errors if the directive
4667 were ignored. */
4668 if (c_parser_pragma (parser, pragma_compound))
4669 last_label = false, last_stmt = true;
4670 }
4671 else if (c_parser_next_token_is (parser, CPP_EOF))
4672 {
4673 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
4674 c_parser_error (parser, "expected declaration or statement");
4675 return;
4676 }
4677 else if (c_parser_next_token_is_keyword (parser, RID_ELSE))
4678 {
4679 if (parser->in_if_block)
4680 {
4681 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
4682 error_at (loc, """expected %<}%> before %<else%>");
4683 return;
4684 }
4685 else
4686 {
4687 error_at (loc, "%<else%> without a previous %<if%>");
4688 c_parser_consume_token (parser);
4689 continue;
4690 }
4691 }
4692 else
4693 {
4694 statement:
4695 last_label = false;
4696 last_stmt = true;
4697 mark_valid_location_for_stdc_pragma (false);
4698 c_parser_statement_after_labels (parser);
4699 }
4700
4701 parser->error = false;
4702 }
4703 if (last_label)
4704 error_at (label_loc, "label at end of compound statement");
4705 c_parser_consume_token (parser);
4706 /* Restore the value we started with. */
4707 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
4708 }
4709
4710 /* Parse all consecutive labels. */
4711
4712 static void
4713 c_parser_all_labels (c_parser *parser)
4714 {
4715 while (c_parser_next_token_is_keyword (parser, RID_CASE)
4716 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
4717 || (c_parser_next_token_is (parser, CPP_NAME)
4718 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
4719 c_parser_label (parser);
4720 }
4721
4722 /* Parse a label (C90 6.6.1, C99 6.8.1).
4723
4724 label:
4725 identifier : attributes[opt]
4726 case constant-expression :
4727 default :
4728
4729 GNU extensions:
4730
4731 label:
4732 case constant-expression ... constant-expression :
4733
4734 The use of attributes on labels is a GNU extension. The syntax in
4735 GNU C accepts any expressions without commas, non-constant
4736 expressions being rejected later. */
4737
4738 static void
4739 c_parser_label (c_parser *parser)
4740 {
4741 location_t loc1 = c_parser_peek_token (parser)->location;
4742 tree label = NULL_TREE;
4743 if (c_parser_next_token_is_keyword (parser, RID_CASE))
4744 {
4745 tree exp1, exp2;
4746 c_parser_consume_token (parser);
4747 exp1 = c_parser_expr_no_commas (parser, NULL).value;
4748 if (c_parser_next_token_is (parser, CPP_COLON))
4749 {
4750 c_parser_consume_token (parser);
4751 label = do_case (loc1, exp1, NULL_TREE);
4752 }
4753 else if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
4754 {
4755 c_parser_consume_token (parser);
4756 exp2 = c_parser_expr_no_commas (parser, NULL).value;
4757 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
4758 label = do_case (loc1, exp1, exp2);
4759 }
4760 else
4761 c_parser_error (parser, "expected %<:%> or %<...%>");
4762 }
4763 else if (c_parser_next_token_is_keyword (parser, RID_DEFAULT))
4764 {
4765 c_parser_consume_token (parser);
4766 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
4767 label = do_case (loc1, NULL_TREE, NULL_TREE);
4768 }
4769 else
4770 {
4771 tree name = c_parser_peek_token (parser)->value;
4772 tree tlab;
4773 tree attrs;
4774 location_t loc2 = c_parser_peek_token (parser)->location;
4775 gcc_assert (c_parser_next_token_is (parser, CPP_NAME));
4776 c_parser_consume_token (parser);
4777 gcc_assert (c_parser_next_token_is (parser, CPP_COLON));
4778 c_parser_consume_token (parser);
4779 attrs = c_parser_attributes (parser);
4780 tlab = define_label (loc2, name);
4781 if (tlab)
4782 {
4783 decl_attributes (&tlab, attrs, 0);
4784 label = add_stmt (build_stmt (loc1, LABEL_EXPR, tlab));
4785 }
4786 }
4787 if (label)
4788 {
4789 if (c_parser_next_tokens_start_declaration (parser))
4790 {
4791 error_at (c_parser_peek_token (parser)->location,
4792 "a label can only be part of a statement and "
4793 "a declaration is not a statement");
4794 c_parser_declaration_or_fndef (parser, /*fndef_ok*/ false,
4795 /*static_assert_ok*/ true,
4796 /*empty_ok*/ true, /*nested*/ true,
4797 /*start_attr_ok*/ true, NULL,
4798 vNULL);
4799 }
4800 }
4801 }
4802
4803 /* Parse a statement (C90 6.6, C99 6.8).
4804
4805 statement:
4806 labeled-statement
4807 compound-statement
4808 expression-statement
4809 selection-statement
4810 iteration-statement
4811 jump-statement
4812
4813 labeled-statement:
4814 label statement
4815
4816 expression-statement:
4817 expression[opt] ;
4818
4819 selection-statement:
4820 if-statement
4821 switch-statement
4822
4823 iteration-statement:
4824 while-statement
4825 do-statement
4826 for-statement
4827
4828 jump-statement:
4829 goto identifier ;
4830 continue ;
4831 break ;
4832 return expression[opt] ;
4833
4834 GNU extensions:
4835
4836 statement:
4837 asm-statement
4838
4839 jump-statement:
4840 goto * expression ;
4841
4842 Objective-C:
4843
4844 statement:
4845 objc-throw-statement
4846 objc-try-catch-statement
4847 objc-synchronized-statement
4848
4849 objc-throw-statement:
4850 @throw expression ;
4851 @throw ;
4852
4853 OpenACC:
4854
4855 statement:
4856 openacc-construct
4857
4858 openacc-construct:
4859 parallel-construct
4860 kernels-construct
4861 data-construct
4862 loop-construct
4863
4864 parallel-construct:
4865 parallel-directive structured-block
4866
4867 kernels-construct:
4868 kernels-directive structured-block
4869
4870 data-construct:
4871 data-directive structured-block
4872
4873 loop-construct:
4874 loop-directive structured-block
4875
4876 OpenMP:
4877
4878 statement:
4879 openmp-construct
4880
4881 openmp-construct:
4882 parallel-construct
4883 for-construct
4884 simd-construct
4885 for-simd-construct
4886 sections-construct
4887 single-construct
4888 parallel-for-construct
4889 parallel-for-simd-construct
4890 parallel-sections-construct
4891 master-construct
4892 critical-construct
4893 atomic-construct
4894 ordered-construct
4895
4896 parallel-construct:
4897 parallel-directive structured-block
4898
4899 for-construct:
4900 for-directive iteration-statement
4901
4902 simd-construct:
4903 simd-directive iteration-statements
4904
4905 for-simd-construct:
4906 for-simd-directive iteration-statements
4907
4908 sections-construct:
4909 sections-directive section-scope
4910
4911 single-construct:
4912 single-directive structured-block
4913
4914 parallel-for-construct:
4915 parallel-for-directive iteration-statement
4916
4917 parallel-for-simd-construct:
4918 parallel-for-simd-directive iteration-statement
4919
4920 parallel-sections-construct:
4921 parallel-sections-directive section-scope
4922
4923 master-construct:
4924 master-directive structured-block
4925
4926 critical-construct:
4927 critical-directive structured-block
4928
4929 atomic-construct:
4930 atomic-directive expression-statement
4931
4932 ordered-construct:
4933 ordered-directive structured-block
4934
4935 Transactional Memory:
4936
4937 statement:
4938 transaction-statement
4939 transaction-cancel-statement
4940 */
4941
4942 static void
4943 c_parser_statement (c_parser *parser)
4944 {
4945 c_parser_all_labels (parser);
4946 c_parser_statement_after_labels (parser);
4947 }
4948
4949 /* Parse a statement, other than a labeled statement. */
4950
4951 static void
4952 c_parser_statement_after_labels (c_parser *parser)
4953 {
4954 location_t loc = c_parser_peek_token (parser)->location;
4955 tree stmt = NULL_TREE;
4956 bool in_if_block = parser->in_if_block;
4957 parser->in_if_block = false;
4958 switch (c_parser_peek_token (parser)->type)
4959 {
4960 case CPP_OPEN_BRACE:
4961 add_stmt (c_parser_compound_statement (parser));
4962 break;
4963 case CPP_KEYWORD:
4964 switch (c_parser_peek_token (parser)->keyword)
4965 {
4966 case RID_IF:
4967 c_parser_if_statement (parser);
4968 break;
4969 case RID_SWITCH:
4970 c_parser_switch_statement (parser);
4971 break;
4972 case RID_WHILE:
4973 c_parser_while_statement (parser, false);
4974 break;
4975 case RID_DO:
4976 c_parser_do_statement (parser, false);
4977 break;
4978 case RID_FOR:
4979 c_parser_for_statement (parser, false);
4980 break;
4981 case RID_CILK_FOR:
4982 if (!flag_cilkplus)
4983 {
4984 error_at (c_parser_peek_token (parser)->location,
4985 "-fcilkplus must be enabled to use %<_Cilk_for%>");
4986 c_parser_skip_to_end_of_block_or_statement (parser);
4987 }
4988 else
4989 c_parser_cilk_for (parser, integer_zero_node);
4990 break;
4991 case RID_CILK_SYNC:
4992 c_parser_consume_token (parser);
4993 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
4994 if (!flag_cilkplus)
4995 error_at (loc, "-fcilkplus must be enabled to use %<_Cilk_sync%>");
4996 else
4997 add_stmt (build_cilk_sync ());
4998 break;
4999 case RID_GOTO:
5000 c_parser_consume_token (parser);
5001 if (c_parser_next_token_is (parser, CPP_NAME))
5002 {
5003 stmt = c_finish_goto_label (loc,
5004 c_parser_peek_token (parser)->value);
5005 c_parser_consume_token (parser);
5006 }
5007 else if (c_parser_next_token_is (parser, CPP_MULT))
5008 {
5009 struct c_expr val;
5010
5011 c_parser_consume_token (parser);
5012 val = c_parser_expression (parser);
5013 if (check_no_cilk (val.value,
5014 "Cilk array notation cannot be used as a computed goto expression",
5015 "%<_Cilk_spawn%> statement cannot be used as a computed goto expression",
5016 loc))
5017 val.value = error_mark_node;
5018 val = convert_lvalue_to_rvalue (loc, val, false, true);
5019 stmt = c_finish_goto_ptr (loc, val.value);
5020 }
5021 else
5022 c_parser_error (parser, "expected identifier or %<*%>");
5023 goto expect_semicolon;
5024 case RID_CONTINUE:
5025 c_parser_consume_token (parser);
5026 stmt = c_finish_bc_stmt (loc, &c_cont_label, false);
5027 goto expect_semicolon;
5028 case RID_BREAK:
5029 c_parser_consume_token (parser);
5030 stmt = c_finish_bc_stmt (loc, &c_break_label, true);
5031 goto expect_semicolon;
5032 case RID_RETURN:
5033 c_parser_consume_token (parser);
5034 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5035 {
5036 stmt = c_finish_return (loc, NULL_TREE, NULL_TREE);
5037 c_parser_consume_token (parser);
5038 }
5039 else
5040 {
5041 location_t xloc = c_parser_peek_token (parser)->location;
5042 struct c_expr expr = c_parser_expression_conv (parser);
5043 mark_exp_read (expr.value);
5044 stmt = c_finish_return (xloc, expr.value, expr.original_type);
5045 goto expect_semicolon;
5046 }
5047 break;
5048 case RID_ASM:
5049 stmt = c_parser_asm_statement (parser);
5050 break;
5051 case RID_TRANSACTION_ATOMIC:
5052 case RID_TRANSACTION_RELAXED:
5053 stmt = c_parser_transaction (parser,
5054 c_parser_peek_token (parser)->keyword);
5055 break;
5056 case RID_TRANSACTION_CANCEL:
5057 stmt = c_parser_transaction_cancel (parser);
5058 goto expect_semicolon;
5059 case RID_AT_THROW:
5060 gcc_assert (c_dialect_objc ());
5061 c_parser_consume_token (parser);
5062 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5063 {
5064 stmt = objc_build_throw_stmt (loc, NULL_TREE);
5065 c_parser_consume_token (parser);
5066 }
5067 else
5068 {
5069 struct c_expr expr = c_parser_expression (parser);
5070 expr = convert_lvalue_to_rvalue (loc, expr, false, false);
5071 if (check_no_cilk (expr.value,
5072 "Cilk array notation cannot be used for a throw expression",
5073 "%<_Cilk_spawn%> statement cannot be used for a throw expression"))
5074 expr.value = error_mark_node;
5075 else
5076 {
5077 expr.value = c_fully_fold (expr.value, false, NULL);
5078 stmt = objc_build_throw_stmt (loc, expr.value);
5079 }
5080 goto expect_semicolon;
5081 }
5082 break;
5083 case RID_AT_TRY:
5084 gcc_assert (c_dialect_objc ());
5085 c_parser_objc_try_catch_finally_statement (parser);
5086 break;
5087 case RID_AT_SYNCHRONIZED:
5088 gcc_assert (c_dialect_objc ());
5089 c_parser_objc_synchronized_statement (parser);
5090 break;
5091 default:
5092 goto expr_stmt;
5093 }
5094 break;
5095 case CPP_SEMICOLON:
5096 c_parser_consume_token (parser);
5097 break;
5098 case CPP_CLOSE_PAREN:
5099 case CPP_CLOSE_SQUARE:
5100 /* Avoid infinite loop in error recovery:
5101 c_parser_skip_until_found stops at a closing nesting
5102 delimiter without consuming it, but here we need to consume
5103 it to proceed further. */
5104 c_parser_error (parser, "expected statement");
5105 c_parser_consume_token (parser);
5106 break;
5107 case CPP_PRAGMA:
5108 c_parser_pragma (parser, pragma_stmt);
5109 break;
5110 default:
5111 expr_stmt:
5112 stmt = c_finish_expr_stmt (loc, c_parser_expression_conv (parser).value);
5113 expect_semicolon:
5114 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
5115 break;
5116 }
5117 /* Two cases cannot and do not have line numbers associated: If stmt
5118 is degenerate, such as "2;", then stmt is an INTEGER_CST, which
5119 cannot hold line numbers. But that's OK because the statement
5120 will either be changed to a MODIFY_EXPR during gimplification of
5121 the statement expr, or discarded. If stmt was compound, but
5122 without new variables, we will have skipped the creation of a
5123 BIND and will have a bare STATEMENT_LIST. But that's OK because
5124 (recursively) all of the component statements should already have
5125 line numbers assigned. ??? Can we discard no-op statements
5126 earlier? */
5127 if (CAN_HAVE_LOCATION_P (stmt)
5128 && EXPR_LOCATION (stmt) == UNKNOWN_LOCATION)
5129 SET_EXPR_LOCATION (stmt, loc);
5130
5131 parser->in_if_block = in_if_block;
5132 }
5133
5134 /* Parse the condition from an if, do, while or for statements. */
5135
5136 static tree
5137 c_parser_condition (c_parser *parser)
5138 {
5139 location_t loc = c_parser_peek_token (parser)->location;
5140 tree cond;
5141 cond = c_parser_expression_conv (parser).value;
5142 cond = c_objc_common_truthvalue_conversion (loc, cond);
5143 cond = c_fully_fold (cond, false, NULL);
5144 if (warn_sequence_point)
5145 verify_sequence_points (cond);
5146 return cond;
5147 }
5148
5149 /* Parse a parenthesized condition from an if, do or while statement.
5150
5151 condition:
5152 ( expression )
5153 */
5154 static tree
5155 c_parser_paren_condition (c_parser *parser)
5156 {
5157 tree cond;
5158 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5159 return error_mark_node;
5160 cond = c_parser_condition (parser);
5161 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
5162 return cond;
5163 }
5164
5165 /* Parse a statement which is a block in C99. */
5166
5167 static tree
5168 c_parser_c99_block_statement (c_parser *parser)
5169 {
5170 tree block = c_begin_compound_stmt (flag_isoc99);
5171 location_t loc = c_parser_peek_token (parser)->location;
5172 c_parser_statement (parser);
5173 return c_end_compound_stmt (loc, block, flag_isoc99);
5174 }
5175
5176 /* Parse the body of an if statement. This is just parsing a
5177 statement but (a) it is a block in C99, (b) we track whether the
5178 body is an if statement for the sake of -Wparentheses warnings, (c)
5179 we handle an empty body specially for the sake of -Wempty-body
5180 warnings, and (d) we call parser_compound_statement directly
5181 because c_parser_statement_after_labels resets
5182 parser->in_if_block. */
5183
5184 static tree
5185 c_parser_if_body (c_parser *parser, bool *if_p, location_t if_loc)
5186 {
5187 tree block = c_begin_compound_stmt (flag_isoc99);
5188 location_t body_loc = c_parser_peek_token (parser)->location;
5189 c_parser_all_labels (parser);
5190 *if_p = c_parser_next_token_is_keyword (parser, RID_IF);
5191 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5192 {
5193 location_t loc = c_parser_peek_token (parser)->location;
5194 add_stmt (build_empty_stmt (loc));
5195 c_parser_consume_token (parser);
5196 if (!c_parser_next_token_is_keyword (parser, RID_ELSE))
5197 warning_at (loc, OPT_Wempty_body,
5198 "suggest braces around empty body in an %<if%> statement");
5199 }
5200 else if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
5201 add_stmt (c_parser_compound_statement (parser));
5202 else
5203 {
5204 c_parser_statement_after_labels (parser);
5205 if (!c_parser_next_token_is_keyword (parser, RID_ELSE))
5206 warn_for_misleading_indentation (if_loc, body_loc,
5207 c_parser_peek_token (parser)->location,
5208 c_parser_peek_token (parser)->type,
5209 "if");
5210 }
5211
5212 return c_end_compound_stmt (body_loc, block, flag_isoc99);
5213 }
5214
5215 /* Parse the else body of an if statement. This is just parsing a
5216 statement but (a) it is a block in C99, (b) we handle an empty body
5217 specially for the sake of -Wempty-body warnings. */
5218
5219 static tree
5220 c_parser_else_body (c_parser *parser, location_t else_loc)
5221 {
5222 location_t body_loc = c_parser_peek_token (parser)->location;
5223 tree block = c_begin_compound_stmt (flag_isoc99);
5224 c_parser_all_labels (parser);
5225 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5226 {
5227 location_t loc = c_parser_peek_token (parser)->location;
5228 warning_at (loc,
5229 OPT_Wempty_body,
5230 "suggest braces around empty body in an %<else%> statement");
5231 add_stmt (build_empty_stmt (loc));
5232 c_parser_consume_token (parser);
5233 }
5234 else
5235 {
5236 c_parser_statement_after_labels (parser);
5237 warn_for_misleading_indentation (else_loc, body_loc,
5238 c_parser_peek_token (parser)->location,
5239 c_parser_peek_token (parser)->type,
5240 "else");
5241 }
5242 return c_end_compound_stmt (body_loc, block, flag_isoc99);
5243 }
5244
5245 /* Parse an if statement (C90 6.6.4, C99 6.8.4).
5246
5247 if-statement:
5248 if ( expression ) statement
5249 if ( expression ) statement else statement
5250 */
5251
5252 static void
5253 c_parser_if_statement (c_parser *parser)
5254 {
5255 tree block;
5256 location_t loc;
5257 tree cond;
5258 bool first_if = false;
5259 tree first_body, second_body;
5260 bool in_if_block;
5261 tree if_stmt;
5262
5263 gcc_assert (c_parser_next_token_is_keyword (parser, RID_IF));
5264 location_t if_loc = c_parser_peek_token (parser)->location;
5265 c_parser_consume_token (parser);
5266 block = c_begin_compound_stmt (flag_isoc99);
5267 loc = c_parser_peek_token (parser)->location;
5268 cond = c_parser_paren_condition (parser);
5269 if (flag_cilkplus && contains_cilk_spawn_stmt (cond))
5270 {
5271 error_at (loc, "if statement cannot contain %<Cilk_spawn%>");
5272 cond = error_mark_node;
5273 }
5274 in_if_block = parser->in_if_block;
5275 parser->in_if_block = true;
5276 first_body = c_parser_if_body (parser, &first_if, if_loc);
5277 parser->in_if_block = in_if_block;
5278 if (c_parser_next_token_is_keyword (parser, RID_ELSE))
5279 {
5280 location_t else_loc = c_parser_peek_token (parser)->location;
5281 c_parser_consume_token (parser);
5282 second_body = c_parser_else_body (parser, else_loc);
5283 }
5284 else
5285 second_body = NULL_TREE;
5286 c_finish_if_stmt (loc, cond, first_body, second_body, first_if);
5287 if_stmt = c_end_compound_stmt (loc, block, flag_isoc99);
5288
5289 /* If the if statement contains array notations, then we expand them. */
5290 if (flag_cilkplus && contains_array_notation_expr (if_stmt))
5291 if_stmt = fix_conditional_array_notations (if_stmt);
5292 add_stmt (if_stmt);
5293 }
5294
5295 /* Parse a switch statement (C90 6.6.4, C99 6.8.4).
5296
5297 switch-statement:
5298 switch (expression) statement
5299 */
5300
5301 static void
5302 c_parser_switch_statement (c_parser *parser)
5303 {
5304 struct c_expr ce;
5305 tree block, expr, body, save_break;
5306 location_t switch_loc = c_parser_peek_token (parser)->location;
5307 location_t switch_cond_loc;
5308 gcc_assert (c_parser_next_token_is_keyword (parser, RID_SWITCH));
5309 c_parser_consume_token (parser);
5310 block = c_begin_compound_stmt (flag_isoc99);
5311 bool explicit_cast_p = false;
5312 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5313 {
5314 switch_cond_loc = c_parser_peek_token (parser)->location;
5315 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
5316 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
5317 explicit_cast_p = true;
5318 ce = c_parser_expression (parser);
5319 ce = convert_lvalue_to_rvalue (switch_cond_loc, ce, true, false);
5320 expr = ce.value;
5321 /* ??? expr has no valid location? */
5322 if (check_no_cilk (expr,
5323 "Cilk array notation cannot be used as a condition for switch statement",
5324 "%<_Cilk_spawn%> statement cannot be used as a condition for switch statement",
5325 switch_cond_loc))
5326 expr = error_mark_node;
5327 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
5328 }
5329 else
5330 {
5331 switch_cond_loc = UNKNOWN_LOCATION;
5332 expr = error_mark_node;
5333 }
5334 c_start_case (switch_loc, switch_cond_loc, expr, explicit_cast_p);
5335 save_break = c_break_label;
5336 c_break_label = NULL_TREE;
5337 body = c_parser_c99_block_statement (parser);
5338 c_finish_case (body, ce.original_type);
5339 if (c_break_label)
5340 {
5341 location_t here = c_parser_peek_token (parser)->location;
5342 tree t = build1 (LABEL_EXPR, void_type_node, c_break_label);
5343 SET_EXPR_LOCATION (t, here);
5344 add_stmt (t);
5345 }
5346 c_break_label = save_break;
5347 add_stmt (c_end_compound_stmt (switch_loc, block, flag_isoc99));
5348 }
5349
5350 /* Parse a while statement (C90 6.6.5, C99 6.8.5).
5351
5352 while-statement:
5353 while (expression) statement
5354 */
5355
5356 static void
5357 c_parser_while_statement (c_parser *parser, bool ivdep)
5358 {
5359 tree block, cond, body, save_break, save_cont;
5360 location_t loc;
5361 gcc_assert (c_parser_next_token_is_keyword (parser, RID_WHILE));
5362 location_t while_loc = c_parser_peek_token (parser)->location;
5363 c_parser_consume_token (parser);
5364 block = c_begin_compound_stmt (flag_isoc99);
5365 loc = c_parser_peek_token (parser)->location;
5366 cond = c_parser_paren_condition (parser);
5367 if (check_no_cilk (cond,
5368 "Cilk array notation cannot be used as a condition for while statement",
5369 "%<_Cilk_spawn%> statement cannot be used as a condition for while statement"))
5370 cond = error_mark_node;
5371 if (ivdep && cond != error_mark_node)
5372 cond = build2 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
5373 build_int_cst (integer_type_node,
5374 annot_expr_ivdep_kind));
5375 save_break = c_break_label;
5376 c_break_label = NULL_TREE;
5377 save_cont = c_cont_label;
5378 c_cont_label = NULL_TREE;
5379
5380 location_t body_loc = UNKNOWN_LOCATION;
5381 if (c_parser_peek_token (parser)->type != CPP_OPEN_BRACE)
5382 body_loc = c_parser_peek_token (parser)->location;
5383 body = c_parser_c99_block_statement (parser);
5384 warn_for_misleading_indentation (while_loc, body_loc,
5385 c_parser_peek_token (parser)->location,
5386 c_parser_peek_token (parser)->type,
5387 "while");
5388
5389 c_finish_loop (loc, cond, NULL, body, c_break_label, c_cont_label, true);
5390 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
5391 c_break_label = save_break;
5392 c_cont_label = save_cont;
5393 }
5394
5395 /* Parse a do statement (C90 6.6.5, C99 6.8.5).
5396
5397 do-statement:
5398 do statement while ( expression ) ;
5399 */
5400
5401 static void
5402 c_parser_do_statement (c_parser *parser, bool ivdep)
5403 {
5404 tree block, cond, body, save_break, save_cont, new_break, new_cont;
5405 location_t loc;
5406 gcc_assert (c_parser_next_token_is_keyword (parser, RID_DO));
5407 c_parser_consume_token (parser);
5408 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5409 warning_at (c_parser_peek_token (parser)->location,
5410 OPT_Wempty_body,
5411 "suggest braces around empty body in %<do%> statement");
5412 block = c_begin_compound_stmt (flag_isoc99);
5413 loc = c_parser_peek_token (parser)->location;
5414 save_break = c_break_label;
5415 c_break_label = NULL_TREE;
5416 save_cont = c_cont_label;
5417 c_cont_label = NULL_TREE;
5418 body = c_parser_c99_block_statement (parser);
5419 c_parser_require_keyword (parser, RID_WHILE, "expected %<while%>");
5420 new_break = c_break_label;
5421 c_break_label = save_break;
5422 new_cont = c_cont_label;
5423 c_cont_label = save_cont;
5424 cond = c_parser_paren_condition (parser);
5425 if (check_no_cilk (cond,
5426 "Cilk array notation cannot be used as a condition for a do-while statement",
5427 "%<_Cilk_spawn%> statement cannot be used as a condition for a do-while statement"))
5428 cond = error_mark_node;
5429 if (ivdep && cond != error_mark_node)
5430 cond = build2 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
5431 build_int_cst (integer_type_node,
5432 annot_expr_ivdep_kind));
5433 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
5434 c_parser_skip_to_end_of_block_or_statement (parser);
5435 c_finish_loop (loc, cond, NULL, body, new_break, new_cont, false);
5436 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
5437 }
5438
5439 /* Parse a for statement (C90 6.6.5, C99 6.8.5).
5440
5441 for-statement:
5442 for ( expression[opt] ; expression[opt] ; expression[opt] ) statement
5443 for ( nested-declaration expression[opt] ; expression[opt] ) statement
5444
5445 The form with a declaration is new in C99.
5446
5447 ??? In accordance with the old parser, the declaration may be a
5448 nested function, which is then rejected in check_for_loop_decls,
5449 but does it make any sense for this to be included in the grammar?
5450 Note in particular that the nested function does not include a
5451 trailing ';', whereas the "declaration" production includes one.
5452 Also, can we reject bad declarations earlier and cheaper than
5453 check_for_loop_decls?
5454
5455 In Objective-C, there are two additional variants:
5456
5457 foreach-statement:
5458 for ( expression in expresssion ) statement
5459 for ( declaration in expression ) statement
5460
5461 This is inconsistent with C, because the second variant is allowed
5462 even if c99 is not enabled.
5463
5464 The rest of the comment documents these Objective-C foreach-statement.
5465
5466 Here is the canonical example of the first variant:
5467 for (object in array) { do something with object }
5468 we call the first expression ("object") the "object_expression" and
5469 the second expression ("array") the "collection_expression".
5470 object_expression must be an lvalue of type "id" (a generic Objective-C
5471 object) because the loop works by assigning to object_expression the
5472 various objects from the collection_expression. collection_expression
5473 must evaluate to something of type "id" which responds to the method
5474 countByEnumeratingWithState:objects:count:.
5475
5476 The canonical example of the second variant is:
5477 for (id object in array) { do something with object }
5478 which is completely equivalent to
5479 {
5480 id object;
5481 for (object in array) { do something with object }
5482 }
5483 Note that initizializing 'object' in some way (eg, "for ((object =
5484 xxx) in array) { do something with object }") is possibly
5485 technically valid, but completely pointless as 'object' will be
5486 assigned to something else as soon as the loop starts. We should
5487 most likely reject it (TODO).
5488
5489 The beginning of the Objective-C foreach-statement looks exactly
5490 like the beginning of the for-statement, and we can tell it is a
5491 foreach-statement only because the initial declaration or
5492 expression is terminated by 'in' instead of ';'.
5493 */
5494
5495 static void
5496 c_parser_for_statement (c_parser *parser, bool ivdep)
5497 {
5498 tree block, cond, incr, save_break, save_cont, body;
5499 /* The following are only used when parsing an ObjC foreach statement. */
5500 tree object_expression;
5501 /* Silence the bogus uninitialized warning. */
5502 tree collection_expression = NULL;
5503 location_t loc = c_parser_peek_token (parser)->location;
5504 location_t for_loc = c_parser_peek_token (parser)->location;
5505 bool is_foreach_statement = false;
5506 gcc_assert (c_parser_next_token_is_keyword (parser, RID_FOR));
5507 c_parser_consume_token (parser);
5508 /* Open a compound statement in Objective-C as well, just in case this is
5509 as foreach expression. */
5510 block = c_begin_compound_stmt (flag_isoc99 || c_dialect_objc ());
5511 cond = error_mark_node;
5512 incr = error_mark_node;
5513 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5514 {
5515 /* Parse the initialization declaration or expression. */
5516 object_expression = error_mark_node;
5517 parser->objc_could_be_foreach_context = c_dialect_objc ();
5518 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5519 {
5520 parser->objc_could_be_foreach_context = false;
5521 c_parser_consume_token (parser);
5522 c_finish_expr_stmt (loc, NULL_TREE);
5523 }
5524 else if (c_parser_next_tokens_start_declaration (parser))
5525 {
5526 c_parser_declaration_or_fndef (parser, true, true, true, true, true,
5527 &object_expression, vNULL);
5528 parser->objc_could_be_foreach_context = false;
5529
5530 if (c_parser_next_token_is_keyword (parser, RID_IN))
5531 {
5532 c_parser_consume_token (parser);
5533 is_foreach_statement = true;
5534 if (check_for_loop_decls (for_loc, true) == NULL_TREE)
5535 c_parser_error (parser, "multiple iterating variables in fast enumeration");
5536 }
5537 else
5538 check_for_loop_decls (for_loc, flag_isoc99);
5539 }
5540 else if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
5541 {
5542 /* __extension__ can start a declaration, but is also an
5543 unary operator that can start an expression. Consume all
5544 but the last of a possible series of __extension__ to
5545 determine which. */
5546 while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
5547 && (c_parser_peek_2nd_token (parser)->keyword
5548 == RID_EXTENSION))
5549 c_parser_consume_token (parser);
5550 if (c_token_starts_declaration (c_parser_peek_2nd_token (parser)))
5551 {
5552 int ext;
5553 ext = disable_extension_diagnostics ();
5554 c_parser_consume_token (parser);
5555 c_parser_declaration_or_fndef (parser, true, true, true, true,
5556 true, &object_expression, vNULL);
5557 parser->objc_could_be_foreach_context = false;
5558
5559 restore_extension_diagnostics (ext);
5560 if (c_parser_next_token_is_keyword (parser, RID_IN))
5561 {
5562 c_parser_consume_token (parser);
5563 is_foreach_statement = true;
5564 if (check_for_loop_decls (for_loc, true) == NULL_TREE)
5565 c_parser_error (parser, "multiple iterating variables in fast enumeration");
5566 }
5567 else
5568 check_for_loop_decls (for_loc, flag_isoc99);
5569 }
5570 else
5571 goto init_expr;
5572 }
5573 else
5574 {
5575 init_expr:
5576 {
5577 struct c_expr ce;
5578 tree init_expression;
5579 ce = c_parser_expression (parser);
5580 /* In theory we could forbid _Cilk_spawn here, as the spec says "only in top
5581 level statement", but it works just fine, so allow it. */
5582 init_expression = ce.value;
5583 parser->objc_could_be_foreach_context = false;
5584 if (c_parser_next_token_is_keyword (parser, RID_IN))
5585 {
5586 c_parser_consume_token (parser);
5587 is_foreach_statement = true;
5588 if (! lvalue_p (init_expression))
5589 c_parser_error (parser, "invalid iterating variable in fast enumeration");
5590 object_expression = c_fully_fold (init_expression, false, NULL);
5591 }
5592 else
5593 {
5594 ce = convert_lvalue_to_rvalue (loc, ce, true, false);
5595 init_expression = ce.value;
5596 c_finish_expr_stmt (loc, init_expression);
5597 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
5598 }
5599 }
5600 }
5601 /* Parse the loop condition. In the case of a foreach
5602 statement, there is no loop condition. */
5603 gcc_assert (!parser->objc_could_be_foreach_context);
5604 if (!is_foreach_statement)
5605 {
5606 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5607 {
5608 if (ivdep)
5609 {
5610 c_parser_error (parser, "missing loop condition in loop with "
5611 "%<GCC ivdep%> pragma");
5612 cond = error_mark_node;
5613 }
5614 else
5615 {
5616 c_parser_consume_token (parser);
5617 cond = NULL_TREE;
5618 }
5619 }
5620 else
5621 {
5622 cond = c_parser_condition (parser);
5623 if (check_no_cilk (cond,
5624 "Cilk array notation cannot be used in a condition for a for-loop",
5625 "%<_Cilk_spawn%> statement cannot be used in a condition for a for-loop"))
5626 cond = error_mark_node;
5627 c_parser_skip_until_found (parser, CPP_SEMICOLON,
5628 "expected %<;%>");
5629 }
5630 if (ivdep && cond != error_mark_node)
5631 cond = build2 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
5632 build_int_cst (integer_type_node,
5633 annot_expr_ivdep_kind));
5634 }
5635 /* Parse the increment expression (the third expression in a
5636 for-statement). In the case of a foreach-statement, this is
5637 the expression that follows the 'in'. */
5638 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
5639 {
5640 if (is_foreach_statement)
5641 {
5642 c_parser_error (parser, "missing collection in fast enumeration");
5643 collection_expression = error_mark_node;
5644 }
5645 else
5646 incr = c_process_expr_stmt (loc, NULL_TREE);
5647 }
5648 else
5649 {
5650 if (is_foreach_statement)
5651 collection_expression = c_fully_fold (c_parser_expression (parser).value,
5652 false, NULL);
5653 else
5654 {
5655 struct c_expr ce = c_parser_expression (parser);
5656 ce = convert_lvalue_to_rvalue (loc, ce, true, false);
5657 incr = c_process_expr_stmt (loc, ce.value);
5658 }
5659 }
5660 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
5661 }
5662 save_break = c_break_label;
5663 c_break_label = NULL_TREE;
5664 save_cont = c_cont_label;
5665 c_cont_label = NULL_TREE;
5666
5667 location_t body_loc = UNKNOWN_LOCATION;
5668 if (c_parser_peek_token (parser)->type != CPP_OPEN_BRACE)
5669 body_loc = c_parser_peek_token (parser)->location;
5670 body = c_parser_c99_block_statement (parser);
5671 warn_for_misleading_indentation (for_loc, body_loc,
5672 c_parser_peek_token (parser)->location,
5673 c_parser_peek_token (parser)->type,
5674 "for");
5675
5676 if (is_foreach_statement)
5677 objc_finish_foreach_loop (loc, object_expression, collection_expression, body, c_break_label, c_cont_label);
5678 else
5679 c_finish_loop (loc, cond, incr, body, c_break_label, c_cont_label, true);
5680 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99 || c_dialect_objc ()));
5681 c_break_label = save_break;
5682 c_cont_label = save_cont;
5683 }
5684
5685 /* Parse an asm statement, a GNU extension. This is a full-blown asm
5686 statement with inputs, outputs, clobbers, and volatile tag
5687 allowed.
5688
5689 asm-statement:
5690 asm type-qualifier[opt] ( asm-argument ) ;
5691 asm type-qualifier[opt] goto ( asm-goto-argument ) ;
5692
5693 asm-argument:
5694 asm-string-literal
5695 asm-string-literal : asm-operands[opt]
5696 asm-string-literal : asm-operands[opt] : asm-operands[opt]
5697 asm-string-literal : asm-operands[opt] : asm-operands[opt] : asm-clobbers[opt]
5698
5699 asm-goto-argument:
5700 asm-string-literal : : asm-operands[opt] : asm-clobbers[opt] \
5701 : asm-goto-operands
5702
5703 Qualifiers other than volatile are accepted in the syntax but
5704 warned for. */
5705
5706 static tree
5707 c_parser_asm_statement (c_parser *parser)
5708 {
5709 tree quals, str, outputs, inputs, clobbers, labels, ret;
5710 bool simple, is_goto;
5711 location_t asm_loc = c_parser_peek_token (parser)->location;
5712 int section, nsections;
5713
5714 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM));
5715 c_parser_consume_token (parser);
5716 if (c_parser_next_token_is_keyword (parser, RID_VOLATILE))
5717 {
5718 quals = c_parser_peek_token (parser)->value;
5719 c_parser_consume_token (parser);
5720 }
5721 else if (c_parser_next_token_is_keyword (parser, RID_CONST)
5722 || c_parser_next_token_is_keyword (parser, RID_RESTRICT))
5723 {
5724 warning_at (c_parser_peek_token (parser)->location,
5725 0,
5726 "%E qualifier ignored on asm",
5727 c_parser_peek_token (parser)->value);
5728 quals = NULL_TREE;
5729 c_parser_consume_token (parser);
5730 }
5731 else
5732 quals = NULL_TREE;
5733
5734 is_goto = false;
5735 if (c_parser_next_token_is_keyword (parser, RID_GOTO))
5736 {
5737 c_parser_consume_token (parser);
5738 is_goto = true;
5739 }
5740
5741 /* ??? Follow the C++ parser rather than using the
5742 lex_untranslated_string kludge. */
5743 parser->lex_untranslated_string = true;
5744 ret = NULL;
5745
5746 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5747 goto error;
5748
5749 str = c_parser_asm_string_literal (parser);
5750 if (str == NULL_TREE)
5751 goto error_close_paren;
5752
5753 simple = true;
5754 outputs = NULL_TREE;
5755 inputs = NULL_TREE;
5756 clobbers = NULL_TREE;
5757 labels = NULL_TREE;
5758
5759 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN) && !is_goto)
5760 goto done_asm;
5761
5762 /* Parse each colon-delimited section of operands. */
5763 nsections = 3 + is_goto;
5764 for (section = 0; section < nsections; ++section)
5765 {
5766 if (!c_parser_require (parser, CPP_COLON,
5767 is_goto
5768 ? "expected %<:%>"
5769 : "expected %<:%> or %<)%>"))
5770 goto error_close_paren;
5771
5772 /* Once past any colon, we're no longer a simple asm. */
5773 simple = false;
5774
5775 if ((!c_parser_next_token_is (parser, CPP_COLON)
5776 && !c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
5777 || section == 3)
5778 switch (section)
5779 {
5780 case 0:
5781 /* For asm goto, we don't allow output operands, but reserve
5782 the slot for a future extension that does allow them. */
5783 if (!is_goto)
5784 outputs = c_parser_asm_operands (parser);
5785 break;
5786 case 1:
5787 inputs = c_parser_asm_operands (parser);
5788 break;
5789 case 2:
5790 clobbers = c_parser_asm_clobbers (parser);
5791 break;
5792 case 3:
5793 labels = c_parser_asm_goto_operands (parser);
5794 break;
5795 default:
5796 gcc_unreachable ();
5797 }
5798
5799 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN) && !is_goto)
5800 goto done_asm;
5801 }
5802
5803 done_asm:
5804 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
5805 {
5806 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5807 goto error;
5808 }
5809
5810 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
5811 c_parser_skip_to_end_of_block_or_statement (parser);
5812
5813 ret = build_asm_stmt (quals, build_asm_expr (asm_loc, str, outputs, inputs,
5814 clobbers, labels, simple));
5815
5816 error:
5817 parser->lex_untranslated_string = false;
5818 return ret;
5819
5820 error_close_paren:
5821 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5822 goto error;
5823 }
5824
5825 /* Parse asm operands, a GNU extension.
5826
5827 asm-operands:
5828 asm-operand
5829 asm-operands , asm-operand
5830
5831 asm-operand:
5832 asm-string-literal ( expression )
5833 [ identifier ] asm-string-literal ( expression )
5834 */
5835
5836 static tree
5837 c_parser_asm_operands (c_parser *parser)
5838 {
5839 tree list = NULL_TREE;
5840 while (true)
5841 {
5842 tree name, str;
5843 struct c_expr expr;
5844 if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
5845 {
5846 c_parser_consume_token (parser);
5847 if (c_parser_next_token_is (parser, CPP_NAME))
5848 {
5849 tree id = c_parser_peek_token (parser)->value;
5850 c_parser_consume_token (parser);
5851 name = build_string (IDENTIFIER_LENGTH (id),
5852 IDENTIFIER_POINTER (id));
5853 }
5854 else
5855 {
5856 c_parser_error (parser, "expected identifier");
5857 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
5858 return NULL_TREE;
5859 }
5860 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
5861 "expected %<]%>");
5862 }
5863 else
5864 name = NULL_TREE;
5865 str = c_parser_asm_string_literal (parser);
5866 if (str == NULL_TREE)
5867 return NULL_TREE;
5868 parser->lex_untranslated_string = false;
5869 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5870 {
5871 parser->lex_untranslated_string = true;
5872 return NULL_TREE;
5873 }
5874 expr = c_parser_expression (parser);
5875 mark_exp_read (expr.value);
5876 parser->lex_untranslated_string = true;
5877 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
5878 {
5879 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5880 return NULL_TREE;
5881 }
5882 list = chainon (list, build_tree_list (build_tree_list (name, str),
5883 expr.value));
5884 if (c_parser_next_token_is (parser, CPP_COMMA))
5885 c_parser_consume_token (parser);
5886 else
5887 break;
5888 }
5889 return list;
5890 }
5891
5892 /* Parse asm clobbers, a GNU extension.
5893
5894 asm-clobbers:
5895 asm-string-literal
5896 asm-clobbers , asm-string-literal
5897 */
5898
5899 static tree
5900 c_parser_asm_clobbers (c_parser *parser)
5901 {
5902 tree list = NULL_TREE;
5903 while (true)
5904 {
5905 tree str = c_parser_asm_string_literal (parser);
5906 if (str)
5907 list = tree_cons (NULL_TREE, str, list);
5908 else
5909 return NULL_TREE;
5910 if (c_parser_next_token_is (parser, CPP_COMMA))
5911 c_parser_consume_token (parser);
5912 else
5913 break;
5914 }
5915 return list;
5916 }
5917
5918 /* Parse asm goto labels, a GNU extension.
5919
5920 asm-goto-operands:
5921 identifier
5922 asm-goto-operands , identifier
5923 */
5924
5925 static tree
5926 c_parser_asm_goto_operands (c_parser *parser)
5927 {
5928 tree list = NULL_TREE;
5929 while (true)
5930 {
5931 tree name, label;
5932
5933 if (c_parser_next_token_is (parser, CPP_NAME))
5934 {
5935 c_token *tok = c_parser_peek_token (parser);
5936 name = tok->value;
5937 label = lookup_label_for_goto (tok->location, name);
5938 c_parser_consume_token (parser);
5939 TREE_USED (label) = 1;
5940 }
5941 else
5942 {
5943 c_parser_error (parser, "expected identifier");
5944 return NULL_TREE;
5945 }
5946
5947 name = build_string (IDENTIFIER_LENGTH (name),
5948 IDENTIFIER_POINTER (name));
5949 list = tree_cons (name, label, list);
5950 if (c_parser_next_token_is (parser, CPP_COMMA))
5951 c_parser_consume_token (parser);
5952 else
5953 return nreverse (list);
5954 }
5955 }
5956
5957 /* Parse an expression other than a compound expression; that is, an
5958 assignment expression (C90 6.3.16, C99 6.5.16). If AFTER is not
5959 NULL then it is an Objective-C message expression which is the
5960 primary-expression starting the expression as an initializer.
5961
5962 assignment-expression:
5963 conditional-expression
5964 unary-expression assignment-operator assignment-expression
5965
5966 assignment-operator: one of
5967 = *= /= %= += -= <<= >>= &= ^= |=
5968
5969 In GNU C we accept any conditional expression on the LHS and
5970 diagnose the invalid lvalue rather than producing a syntax
5971 error. */
5972
5973 static struct c_expr
5974 c_parser_expr_no_commas (c_parser *parser, struct c_expr *after,
5975 tree omp_atomic_lhs)
5976 {
5977 struct c_expr lhs, rhs, ret;
5978 enum tree_code code;
5979 location_t op_location, exp_location;
5980 gcc_assert (!after || c_dialect_objc ());
5981 lhs = c_parser_conditional_expression (parser, after, omp_atomic_lhs);
5982 op_location = c_parser_peek_token (parser)->location;
5983 switch (c_parser_peek_token (parser)->type)
5984 {
5985 case CPP_EQ:
5986 code = NOP_EXPR;
5987 break;
5988 case CPP_MULT_EQ:
5989 code = MULT_EXPR;
5990 break;
5991 case CPP_DIV_EQ:
5992 code = TRUNC_DIV_EXPR;
5993 break;
5994 case CPP_MOD_EQ:
5995 code = TRUNC_MOD_EXPR;
5996 break;
5997 case CPP_PLUS_EQ:
5998 code = PLUS_EXPR;
5999 break;
6000 case CPP_MINUS_EQ:
6001 code = MINUS_EXPR;
6002 break;
6003 case CPP_LSHIFT_EQ:
6004 code = LSHIFT_EXPR;
6005 break;
6006 case CPP_RSHIFT_EQ:
6007 code = RSHIFT_EXPR;
6008 break;
6009 case CPP_AND_EQ:
6010 code = BIT_AND_EXPR;
6011 break;
6012 case CPP_XOR_EQ:
6013 code = BIT_XOR_EXPR;
6014 break;
6015 case CPP_OR_EQ:
6016 code = BIT_IOR_EXPR;
6017 break;
6018 default:
6019 return lhs;
6020 }
6021 c_parser_consume_token (parser);
6022 exp_location = c_parser_peek_token (parser)->location;
6023 rhs = c_parser_expr_no_commas (parser, NULL);
6024 rhs = convert_lvalue_to_rvalue (exp_location, rhs, true, true);
6025
6026 ret.value = build_modify_expr (op_location, lhs.value, lhs.original_type,
6027 code, exp_location, rhs.value,
6028 rhs.original_type);
6029 if (code == NOP_EXPR)
6030 ret.original_code = MODIFY_EXPR;
6031 else
6032 {
6033 TREE_NO_WARNING (ret.value) = 1;
6034 ret.original_code = ERROR_MARK;
6035 }
6036 ret.original_type = NULL;
6037 return ret;
6038 }
6039
6040 /* Parse a conditional expression (C90 6.3.15, C99 6.5.15). If AFTER
6041 is not NULL then it is an Objective-C message expression which is
6042 the primary-expression starting the expression as an initializer.
6043
6044 conditional-expression:
6045 logical-OR-expression
6046 logical-OR-expression ? expression : conditional-expression
6047
6048 GNU extensions:
6049
6050 conditional-expression:
6051 logical-OR-expression ? : conditional-expression
6052 */
6053
6054 static struct c_expr
6055 c_parser_conditional_expression (c_parser *parser, struct c_expr *after,
6056 tree omp_atomic_lhs)
6057 {
6058 struct c_expr cond, exp1, exp2, ret;
6059 location_t cond_loc, colon_loc, middle_loc;
6060
6061 gcc_assert (!after || c_dialect_objc ());
6062
6063 cond = c_parser_binary_expression (parser, after, omp_atomic_lhs);
6064
6065 if (c_parser_next_token_is_not (parser, CPP_QUERY))
6066 return cond;
6067 cond_loc = c_parser_peek_token (parser)->location;
6068 cond = convert_lvalue_to_rvalue (cond_loc, cond, true, true);
6069 c_parser_consume_token (parser);
6070 if (c_parser_next_token_is (parser, CPP_COLON))
6071 {
6072 tree eptype = NULL_TREE;
6073
6074 middle_loc = c_parser_peek_token (parser)->location;
6075 pedwarn (middle_loc, OPT_Wpedantic,
6076 "ISO C forbids omitting the middle term of a ?: expression");
6077 warn_for_omitted_condop (middle_loc, cond.value);
6078 if (TREE_CODE (cond.value) == EXCESS_PRECISION_EXPR)
6079 {
6080 eptype = TREE_TYPE (cond.value);
6081 cond.value = TREE_OPERAND (cond.value, 0);
6082 }
6083 /* Make sure first operand is calculated only once. */
6084 exp1.value = c_save_expr (default_conversion (cond.value));
6085 if (eptype)
6086 exp1.value = build1 (EXCESS_PRECISION_EXPR, eptype, exp1.value);
6087 exp1.original_type = NULL;
6088 cond.value = c_objc_common_truthvalue_conversion (cond_loc, exp1.value);
6089 c_inhibit_evaluation_warnings += cond.value == truthvalue_true_node;
6090 }
6091 else
6092 {
6093 cond.value
6094 = c_objc_common_truthvalue_conversion
6095 (cond_loc, default_conversion (cond.value));
6096 c_inhibit_evaluation_warnings += cond.value == truthvalue_false_node;
6097 exp1 = c_parser_expression_conv (parser);
6098 mark_exp_read (exp1.value);
6099 c_inhibit_evaluation_warnings +=
6100 ((cond.value == truthvalue_true_node)
6101 - (cond.value == truthvalue_false_node));
6102 }
6103
6104 colon_loc = c_parser_peek_token (parser)->location;
6105 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
6106 {
6107 c_inhibit_evaluation_warnings -= cond.value == truthvalue_true_node;
6108 ret.value = error_mark_node;
6109 ret.original_code = ERROR_MARK;
6110 ret.original_type = NULL;
6111 return ret;
6112 }
6113 {
6114 location_t exp2_loc = c_parser_peek_token (parser)->location;
6115 exp2 = c_parser_conditional_expression (parser, NULL, NULL_TREE);
6116 exp2 = convert_lvalue_to_rvalue (exp2_loc, exp2, true, true);
6117 }
6118 c_inhibit_evaluation_warnings -= cond.value == truthvalue_true_node;
6119 ret.value = build_conditional_expr (colon_loc, cond.value,
6120 cond.original_code == C_MAYBE_CONST_EXPR,
6121 exp1.value, exp1.original_type,
6122 exp2.value, exp2.original_type);
6123 ret.original_code = ERROR_MARK;
6124 if (exp1.value == error_mark_node || exp2.value == error_mark_node)
6125 ret.original_type = NULL;
6126 else
6127 {
6128 tree t1, t2;
6129
6130 /* If both sides are enum type, the default conversion will have
6131 made the type of the result be an integer type. We want to
6132 remember the enum types we started with. */
6133 t1 = exp1.original_type ? exp1.original_type : TREE_TYPE (exp1.value);
6134 t2 = exp2.original_type ? exp2.original_type : TREE_TYPE (exp2.value);
6135 ret.original_type = ((t1 != error_mark_node
6136 && t2 != error_mark_node
6137 && (TYPE_MAIN_VARIANT (t1)
6138 == TYPE_MAIN_VARIANT (t2)))
6139 ? t1
6140 : NULL);
6141 }
6142 return ret;
6143 }
6144
6145 /* Parse a binary expression; that is, a logical-OR-expression (C90
6146 6.3.5-6.3.14, C99 6.5.5-6.5.14). If AFTER is not NULL then it is
6147 an Objective-C message expression which is the primary-expression
6148 starting the expression as an initializer.
6149
6150 OMP_ATOMIC_LHS is NULL, unless parsing OpenMP #pragma omp atomic,
6151 when it should be the unfolded lhs. In a valid OpenMP source,
6152 one of the operands of the toplevel binary expression must be equal
6153 to it. In that case, just return a build2 created binary operation
6154 rather than result of parser_build_binary_op.
6155
6156 multiplicative-expression:
6157 cast-expression
6158 multiplicative-expression * cast-expression
6159 multiplicative-expression / cast-expression
6160 multiplicative-expression % cast-expression
6161
6162 additive-expression:
6163 multiplicative-expression
6164 additive-expression + multiplicative-expression
6165 additive-expression - multiplicative-expression
6166
6167 shift-expression:
6168 additive-expression
6169 shift-expression << additive-expression
6170 shift-expression >> additive-expression
6171
6172 relational-expression:
6173 shift-expression
6174 relational-expression < shift-expression
6175 relational-expression > shift-expression
6176 relational-expression <= shift-expression
6177 relational-expression >= shift-expression
6178
6179 equality-expression:
6180 relational-expression
6181 equality-expression == relational-expression
6182 equality-expression != relational-expression
6183
6184 AND-expression:
6185 equality-expression
6186 AND-expression & equality-expression
6187
6188 exclusive-OR-expression:
6189 AND-expression
6190 exclusive-OR-expression ^ AND-expression
6191
6192 inclusive-OR-expression:
6193 exclusive-OR-expression
6194 inclusive-OR-expression | exclusive-OR-expression
6195
6196 logical-AND-expression:
6197 inclusive-OR-expression
6198 logical-AND-expression && inclusive-OR-expression
6199
6200 logical-OR-expression:
6201 logical-AND-expression
6202 logical-OR-expression || logical-AND-expression
6203 */
6204
6205 static struct c_expr
6206 c_parser_binary_expression (c_parser *parser, struct c_expr *after,
6207 tree omp_atomic_lhs)
6208 {
6209 /* A binary expression is parsed using operator-precedence parsing,
6210 with the operands being cast expressions. All the binary
6211 operators are left-associative. Thus a binary expression is of
6212 form:
6213
6214 E0 op1 E1 op2 E2 ...
6215
6216 which we represent on a stack. On the stack, the precedence
6217 levels are strictly increasing. When a new operator is
6218 encountered of higher precedence than that at the top of the
6219 stack, it is pushed; its LHS is the top expression, and its RHS
6220 is everything parsed until it is popped. When a new operator is
6221 encountered with precedence less than or equal to that at the top
6222 of the stack, triples E[i-1] op[i] E[i] are popped and replaced
6223 by the result of the operation until the operator at the top of
6224 the stack has lower precedence than the new operator or there is
6225 only one element on the stack; then the top expression is the LHS
6226 of the new operator. In the case of logical AND and OR
6227 expressions, we also need to adjust c_inhibit_evaluation_warnings
6228 as appropriate when the operators are pushed and popped. */
6229
6230 struct {
6231 /* The expression at this stack level. */
6232 struct c_expr expr;
6233 /* The precedence of the operator on its left, PREC_NONE at the
6234 bottom of the stack. */
6235 enum c_parser_prec prec;
6236 /* The operation on its left. */
6237 enum tree_code op;
6238 /* The source location of this operation. */
6239 location_t loc;
6240 } stack[NUM_PRECS];
6241 int sp;
6242 /* Location of the binary operator. */
6243 location_t binary_loc = UNKNOWN_LOCATION; /* Quiet warning. */
6244 #define POP \
6245 do { \
6246 switch (stack[sp].op) \
6247 { \
6248 case TRUTH_ANDIF_EXPR: \
6249 c_inhibit_evaluation_warnings -= (stack[sp - 1].expr.value \
6250 == truthvalue_false_node); \
6251 break; \
6252 case TRUTH_ORIF_EXPR: \
6253 c_inhibit_evaluation_warnings -= (stack[sp - 1].expr.value \
6254 == truthvalue_true_node); \
6255 break; \
6256 default: \
6257 break; \
6258 } \
6259 stack[sp - 1].expr \
6260 = convert_lvalue_to_rvalue (stack[sp - 1].loc, \
6261 stack[sp - 1].expr, true, true); \
6262 stack[sp].expr \
6263 = convert_lvalue_to_rvalue (stack[sp].loc, \
6264 stack[sp].expr, true, true); \
6265 if (__builtin_expect (omp_atomic_lhs != NULL_TREE, 0) && sp == 1 \
6266 && c_parser_peek_token (parser)->type == CPP_SEMICOLON \
6267 && ((1 << stack[sp].prec) \
6268 & ((1 << PREC_BITOR) | (1 << PREC_BITXOR) | (1 << PREC_BITAND) \
6269 | (1 << PREC_SHIFT) | (1 << PREC_ADD) | (1 << PREC_MULT))) \
6270 && stack[sp].op != TRUNC_MOD_EXPR \
6271 && stack[0].expr.value != error_mark_node \
6272 && stack[1].expr.value != error_mark_node \
6273 && (c_tree_equal (stack[0].expr.value, omp_atomic_lhs) \
6274 || c_tree_equal (stack[1].expr.value, omp_atomic_lhs))) \
6275 stack[0].expr.value \
6276 = build2 (stack[1].op, TREE_TYPE (stack[0].expr.value), \
6277 stack[0].expr.value, stack[1].expr.value); \
6278 else \
6279 stack[sp - 1].expr = parser_build_binary_op (stack[sp].loc, \
6280 stack[sp].op, \
6281 stack[sp - 1].expr, \
6282 stack[sp].expr); \
6283 sp--; \
6284 } while (0)
6285 gcc_assert (!after || c_dialect_objc ());
6286 stack[0].loc = c_parser_peek_token (parser)->location;
6287 stack[0].expr = c_parser_cast_expression (parser, after);
6288 stack[0].prec = PREC_NONE;
6289 sp = 0;
6290 while (true)
6291 {
6292 enum c_parser_prec oprec;
6293 enum tree_code ocode;
6294 if (parser->error)
6295 goto out;
6296 switch (c_parser_peek_token (parser)->type)
6297 {
6298 case CPP_MULT:
6299 oprec = PREC_MULT;
6300 ocode = MULT_EXPR;
6301 break;
6302 case CPP_DIV:
6303 oprec = PREC_MULT;
6304 ocode = TRUNC_DIV_EXPR;
6305 break;
6306 case CPP_MOD:
6307 oprec = PREC_MULT;
6308 ocode = TRUNC_MOD_EXPR;
6309 break;
6310 case CPP_PLUS:
6311 oprec = PREC_ADD;
6312 ocode = PLUS_EXPR;
6313 break;
6314 case CPP_MINUS:
6315 oprec = PREC_ADD;
6316 ocode = MINUS_EXPR;
6317 break;
6318 case CPP_LSHIFT:
6319 oprec = PREC_SHIFT;
6320 ocode = LSHIFT_EXPR;
6321 break;
6322 case CPP_RSHIFT:
6323 oprec = PREC_SHIFT;
6324 ocode = RSHIFT_EXPR;
6325 break;
6326 case CPP_LESS:
6327 oprec = PREC_REL;
6328 ocode = LT_EXPR;
6329 break;
6330 case CPP_GREATER:
6331 oprec = PREC_REL;
6332 ocode = GT_EXPR;
6333 break;
6334 case CPP_LESS_EQ:
6335 oprec = PREC_REL;
6336 ocode = LE_EXPR;
6337 break;
6338 case CPP_GREATER_EQ:
6339 oprec = PREC_REL;
6340 ocode = GE_EXPR;
6341 break;
6342 case CPP_EQ_EQ:
6343 oprec = PREC_EQ;
6344 ocode = EQ_EXPR;
6345 break;
6346 case CPP_NOT_EQ:
6347 oprec = PREC_EQ;
6348 ocode = NE_EXPR;
6349 break;
6350 case CPP_AND:
6351 oprec = PREC_BITAND;
6352 ocode = BIT_AND_EXPR;
6353 break;
6354 case CPP_XOR:
6355 oprec = PREC_BITXOR;
6356 ocode = BIT_XOR_EXPR;
6357 break;
6358 case CPP_OR:
6359 oprec = PREC_BITOR;
6360 ocode = BIT_IOR_EXPR;
6361 break;
6362 case CPP_AND_AND:
6363 oprec = PREC_LOGAND;
6364 ocode = TRUTH_ANDIF_EXPR;
6365 break;
6366 case CPP_OR_OR:
6367 oprec = PREC_LOGOR;
6368 ocode = TRUTH_ORIF_EXPR;
6369 break;
6370 default:
6371 /* Not a binary operator, so end of the binary
6372 expression. */
6373 goto out;
6374 }
6375 binary_loc = c_parser_peek_token (parser)->location;
6376 while (oprec <= stack[sp].prec)
6377 POP;
6378 c_parser_consume_token (parser);
6379 switch (ocode)
6380 {
6381 case TRUTH_ANDIF_EXPR:
6382 stack[sp].expr
6383 = convert_lvalue_to_rvalue (stack[sp].loc,
6384 stack[sp].expr, true, true);
6385 stack[sp].expr.value = c_objc_common_truthvalue_conversion
6386 (stack[sp].loc, default_conversion (stack[sp].expr.value));
6387 c_inhibit_evaluation_warnings += (stack[sp].expr.value
6388 == truthvalue_false_node);
6389 break;
6390 case TRUTH_ORIF_EXPR:
6391 stack[sp].expr
6392 = convert_lvalue_to_rvalue (stack[sp].loc,
6393 stack[sp].expr, true, true);
6394 stack[sp].expr.value = c_objc_common_truthvalue_conversion
6395 (stack[sp].loc, default_conversion (stack[sp].expr.value));
6396 c_inhibit_evaluation_warnings += (stack[sp].expr.value
6397 == truthvalue_true_node);
6398 break;
6399 default:
6400 break;
6401 }
6402 sp++;
6403 stack[sp].loc = binary_loc;
6404 stack[sp].expr = c_parser_cast_expression (parser, NULL);
6405 stack[sp].prec = oprec;
6406 stack[sp].op = ocode;
6407 }
6408 out:
6409 while (sp > 0)
6410 POP;
6411 return stack[0].expr;
6412 #undef POP
6413 }
6414
6415 /* Parse a cast expression (C90 6.3.4, C99 6.5.4). If AFTER is not
6416 NULL then it is an Objective-C message expression which is the
6417 primary-expression starting the expression as an initializer.
6418
6419 cast-expression:
6420 unary-expression
6421 ( type-name ) unary-expression
6422 */
6423
6424 static struct c_expr
6425 c_parser_cast_expression (c_parser *parser, struct c_expr *after)
6426 {
6427 location_t cast_loc = c_parser_peek_token (parser)->location;
6428 gcc_assert (!after || c_dialect_objc ());
6429 if (after)
6430 return c_parser_postfix_expression_after_primary (parser,
6431 cast_loc, *after);
6432 /* If the expression begins with a parenthesized type name, it may
6433 be either a cast or a compound literal; we need to see whether
6434 the next character is '{' to tell the difference. If not, it is
6435 an unary expression. Full detection of unknown typenames here
6436 would require a 3-token lookahead. */
6437 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
6438 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
6439 {
6440 struct c_type_name *type_name;
6441 struct c_expr ret;
6442 struct c_expr expr;
6443 c_parser_consume_token (parser);
6444 type_name = c_parser_type_name (parser);
6445 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
6446 if (type_name == NULL)
6447 {
6448 ret.value = error_mark_node;
6449 ret.original_code = ERROR_MARK;
6450 ret.original_type = NULL;
6451 return ret;
6452 }
6453
6454 /* Save casted types in the function's used types hash table. */
6455 used_types_insert (type_name->specs->type);
6456
6457 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
6458 return c_parser_postfix_expression_after_paren_type (parser, type_name,
6459 cast_loc);
6460 {
6461 location_t expr_loc = c_parser_peek_token (parser)->location;
6462 expr = c_parser_cast_expression (parser, NULL);
6463 expr = convert_lvalue_to_rvalue (expr_loc, expr, true, true);
6464 }
6465 ret.value = c_cast_expr (cast_loc, type_name, expr.value);
6466 ret.original_code = ERROR_MARK;
6467 ret.original_type = NULL;
6468 return ret;
6469 }
6470 else
6471 return c_parser_unary_expression (parser);
6472 }
6473
6474 /* Parse an unary expression (C90 6.3.3, C99 6.5.3).
6475
6476 unary-expression:
6477 postfix-expression
6478 ++ unary-expression
6479 -- unary-expression
6480 unary-operator cast-expression
6481 sizeof unary-expression
6482 sizeof ( type-name )
6483
6484 unary-operator: one of
6485 & * + - ~ !
6486
6487 GNU extensions:
6488
6489 unary-expression:
6490 __alignof__ unary-expression
6491 __alignof__ ( type-name )
6492 && identifier
6493
6494 (C11 permits _Alignof with type names only.)
6495
6496 unary-operator: one of
6497 __extension__ __real__ __imag__
6498
6499 Transactional Memory:
6500
6501 unary-expression:
6502 transaction-expression
6503
6504 In addition, the GNU syntax treats ++ and -- as unary operators, so
6505 they may be applied to cast expressions with errors for non-lvalues
6506 given later. */
6507
6508 static struct c_expr
6509 c_parser_unary_expression (c_parser *parser)
6510 {
6511 int ext;
6512 struct c_expr ret, op;
6513 location_t op_loc = c_parser_peek_token (parser)->location;
6514 location_t exp_loc;
6515 ret.original_code = ERROR_MARK;
6516 ret.original_type = NULL;
6517 switch (c_parser_peek_token (parser)->type)
6518 {
6519 case CPP_PLUS_PLUS:
6520 c_parser_consume_token (parser);
6521 exp_loc = c_parser_peek_token (parser)->location;
6522 op = c_parser_cast_expression (parser, NULL);
6523
6524 /* If there is array notations in op, we expand them. */
6525 if (flag_cilkplus && TREE_CODE (op.value) == ARRAY_NOTATION_REF)
6526 return fix_array_notation_expr (exp_loc, PREINCREMENT_EXPR, op);
6527 else
6528 {
6529 op = default_function_array_read_conversion (exp_loc, op);
6530 return parser_build_unary_op (op_loc, PREINCREMENT_EXPR, op);
6531 }
6532 case CPP_MINUS_MINUS:
6533 c_parser_consume_token (parser);
6534 exp_loc = c_parser_peek_token (parser)->location;
6535 op = c_parser_cast_expression (parser, NULL);
6536
6537 /* If there is array notations in op, we expand them. */
6538 if (flag_cilkplus && TREE_CODE (op.value) == ARRAY_NOTATION_REF)
6539 return fix_array_notation_expr (exp_loc, PREDECREMENT_EXPR, op);
6540 else
6541 {
6542 op = default_function_array_read_conversion (exp_loc, op);
6543 return parser_build_unary_op (op_loc, PREDECREMENT_EXPR, op);
6544 }
6545 case CPP_AND:
6546 c_parser_consume_token (parser);
6547 op = c_parser_cast_expression (parser, NULL);
6548 mark_exp_read (op.value);
6549 return parser_build_unary_op (op_loc, ADDR_EXPR, op);
6550 case CPP_MULT:
6551 c_parser_consume_token (parser);
6552 exp_loc = c_parser_peek_token (parser)->location;
6553 op = c_parser_cast_expression (parser, NULL);
6554 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
6555 ret.value = build_indirect_ref (op_loc, op.value, RO_UNARY_STAR);
6556 return ret;
6557 case CPP_PLUS:
6558 if (!c_dialect_objc () && !in_system_header_at (input_location))
6559 warning_at (op_loc,
6560 OPT_Wtraditional,
6561 "traditional C rejects the unary plus operator");
6562 c_parser_consume_token (parser);
6563 exp_loc = c_parser_peek_token (parser)->location;
6564 op = c_parser_cast_expression (parser, NULL);
6565 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
6566 return parser_build_unary_op (op_loc, CONVERT_EXPR, op);
6567 case CPP_MINUS:
6568 c_parser_consume_token (parser);
6569 exp_loc = c_parser_peek_token (parser)->location;
6570 op = c_parser_cast_expression (parser, NULL);
6571 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
6572 return parser_build_unary_op (op_loc, NEGATE_EXPR, op);
6573 case CPP_COMPL:
6574 c_parser_consume_token (parser);
6575 exp_loc = c_parser_peek_token (parser)->location;
6576 op = c_parser_cast_expression (parser, NULL);
6577 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
6578 return parser_build_unary_op (op_loc, BIT_NOT_EXPR, op);
6579 case CPP_NOT:
6580 c_parser_consume_token (parser);
6581 exp_loc = c_parser_peek_token (parser)->location;
6582 op = c_parser_cast_expression (parser, NULL);
6583 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
6584 return parser_build_unary_op (op_loc, TRUTH_NOT_EXPR, op);
6585 case CPP_AND_AND:
6586 /* Refer to the address of a label as a pointer. */
6587 c_parser_consume_token (parser);
6588 if (c_parser_next_token_is (parser, CPP_NAME))
6589 {
6590 ret.value = finish_label_address_expr
6591 (c_parser_peek_token (parser)->value, op_loc);
6592 c_parser_consume_token (parser);
6593 }
6594 else
6595 {
6596 c_parser_error (parser, "expected identifier");
6597 ret.value = error_mark_node;
6598 }
6599 return ret;
6600 case CPP_KEYWORD:
6601 switch (c_parser_peek_token (parser)->keyword)
6602 {
6603 case RID_SIZEOF:
6604 return c_parser_sizeof_expression (parser);
6605 case RID_ALIGNOF:
6606 return c_parser_alignof_expression (parser);
6607 case RID_EXTENSION:
6608 c_parser_consume_token (parser);
6609 ext = disable_extension_diagnostics ();
6610 ret = c_parser_cast_expression (parser, NULL);
6611 restore_extension_diagnostics (ext);
6612 return ret;
6613 case RID_REALPART:
6614 c_parser_consume_token (parser);
6615 exp_loc = c_parser_peek_token (parser)->location;
6616 op = c_parser_cast_expression (parser, NULL);
6617 op = default_function_array_conversion (exp_loc, op);
6618 return parser_build_unary_op (op_loc, REALPART_EXPR, op);
6619 case RID_IMAGPART:
6620 c_parser_consume_token (parser);
6621 exp_loc = c_parser_peek_token (parser)->location;
6622 op = c_parser_cast_expression (parser, NULL);
6623 op = default_function_array_conversion (exp_loc, op);
6624 return parser_build_unary_op (op_loc, IMAGPART_EXPR, op);
6625 case RID_TRANSACTION_ATOMIC:
6626 case RID_TRANSACTION_RELAXED:
6627 return c_parser_transaction_expression (parser,
6628 c_parser_peek_token (parser)->keyword);
6629 default:
6630 return c_parser_postfix_expression (parser);
6631 }
6632 default:
6633 return c_parser_postfix_expression (parser);
6634 }
6635 }
6636
6637 /* Parse a sizeof expression. */
6638
6639 static struct c_expr
6640 c_parser_sizeof_expression (c_parser *parser)
6641 {
6642 struct c_expr expr;
6643 location_t expr_loc;
6644 gcc_assert (c_parser_next_token_is_keyword (parser, RID_SIZEOF));
6645 c_parser_consume_token (parser);
6646 c_inhibit_evaluation_warnings++;
6647 in_sizeof++;
6648 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
6649 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
6650 {
6651 /* Either sizeof ( type-name ) or sizeof unary-expression
6652 starting with a compound literal. */
6653 struct c_type_name *type_name;
6654 c_parser_consume_token (parser);
6655 expr_loc = c_parser_peek_token (parser)->location;
6656 type_name = c_parser_type_name (parser);
6657 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
6658 if (type_name == NULL)
6659 {
6660 struct c_expr ret;
6661 c_inhibit_evaluation_warnings--;
6662 in_sizeof--;
6663 ret.value = error_mark_node;
6664 ret.original_code = ERROR_MARK;
6665 ret.original_type = NULL;
6666 return ret;
6667 }
6668 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
6669 {
6670 expr = c_parser_postfix_expression_after_paren_type (parser,
6671 type_name,
6672 expr_loc);
6673 goto sizeof_expr;
6674 }
6675 /* sizeof ( type-name ). */
6676 c_inhibit_evaluation_warnings--;
6677 in_sizeof--;
6678 return c_expr_sizeof_type (expr_loc, type_name);
6679 }
6680 else
6681 {
6682 expr_loc = c_parser_peek_token (parser)->location;
6683 expr = c_parser_unary_expression (parser);
6684 sizeof_expr:
6685 c_inhibit_evaluation_warnings--;
6686 in_sizeof--;
6687 mark_exp_read (expr.value);
6688 if (TREE_CODE (expr.value) == COMPONENT_REF
6689 && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1)))
6690 error_at (expr_loc, "%<sizeof%> applied to a bit-field");
6691 return c_expr_sizeof_expr (expr_loc, expr);
6692 }
6693 }
6694
6695 /* Parse an alignof expression. */
6696
6697 static struct c_expr
6698 c_parser_alignof_expression (c_parser *parser)
6699 {
6700 struct c_expr expr;
6701 location_t loc = c_parser_peek_token (parser)->location;
6702 tree alignof_spelling = c_parser_peek_token (parser)->value;
6703 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ALIGNOF));
6704 bool is_c11_alignof = strcmp (IDENTIFIER_POINTER (alignof_spelling),
6705 "_Alignof") == 0;
6706 /* A diagnostic is not required for the use of this identifier in
6707 the implementation namespace; only diagnose it for the C11
6708 spelling because of existing code using the other spellings. */
6709 if (is_c11_alignof)
6710 {
6711 if (flag_isoc99)
6712 pedwarn_c99 (loc, OPT_Wpedantic, "ISO C99 does not support %qE",
6713 alignof_spelling);
6714 else
6715 pedwarn_c99 (loc, OPT_Wpedantic, "ISO C90 does not support %qE",
6716 alignof_spelling);
6717 }
6718 c_parser_consume_token (parser);
6719 c_inhibit_evaluation_warnings++;
6720 in_alignof++;
6721 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
6722 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
6723 {
6724 /* Either __alignof__ ( type-name ) or __alignof__
6725 unary-expression starting with a compound literal. */
6726 location_t loc;
6727 struct c_type_name *type_name;
6728 struct c_expr ret;
6729 c_parser_consume_token (parser);
6730 loc = c_parser_peek_token (parser)->location;
6731 type_name = c_parser_type_name (parser);
6732 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
6733 if (type_name == NULL)
6734 {
6735 struct c_expr ret;
6736 c_inhibit_evaluation_warnings--;
6737 in_alignof--;
6738 ret.value = error_mark_node;
6739 ret.original_code = ERROR_MARK;
6740 ret.original_type = NULL;
6741 return ret;
6742 }
6743 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
6744 {
6745 expr = c_parser_postfix_expression_after_paren_type (parser,
6746 type_name,
6747 loc);
6748 goto alignof_expr;
6749 }
6750 /* alignof ( type-name ). */
6751 c_inhibit_evaluation_warnings--;
6752 in_alignof--;
6753 ret.value = c_sizeof_or_alignof_type (loc, groktypename (type_name,
6754 NULL, NULL),
6755 false, is_c11_alignof, 1);
6756 ret.original_code = ERROR_MARK;
6757 ret.original_type = NULL;
6758 return ret;
6759 }
6760 else
6761 {
6762 struct c_expr ret;
6763 expr = c_parser_unary_expression (parser);
6764 alignof_expr:
6765 mark_exp_read (expr.value);
6766 c_inhibit_evaluation_warnings--;
6767 in_alignof--;
6768 pedwarn (loc, OPT_Wpedantic, "ISO C does not allow %<%E (expression)%>",
6769 alignof_spelling);
6770 ret.value = c_alignof_expr (loc, expr.value);
6771 ret.original_code = ERROR_MARK;
6772 ret.original_type = NULL;
6773 return ret;
6774 }
6775 }
6776
6777 /* Helper function to read arguments of builtins which are interfaces
6778 for the middle-end nodes like COMPLEX_EXPR, VEC_PERM_EXPR and
6779 others. The name of the builtin is passed using BNAME parameter.
6780 Function returns true if there were no errors while parsing and
6781 stores the arguments in CEXPR_LIST. */
6782 static bool
6783 c_parser_get_builtin_args (c_parser *parser, const char *bname,
6784 vec<c_expr_t, va_gc> **ret_cexpr_list,
6785 bool choose_expr_p)
6786 {
6787 location_t loc = c_parser_peek_token (parser)->location;
6788 vec<c_expr_t, va_gc> *cexpr_list;
6789 c_expr_t expr;
6790 bool saved_force_folding_builtin_constant_p;
6791
6792 *ret_cexpr_list = NULL;
6793 if (c_parser_next_token_is_not (parser, CPP_OPEN_PAREN))
6794 {
6795 error_at (loc, "cannot take address of %qs", bname);
6796 return false;
6797 }
6798
6799 c_parser_consume_token (parser);
6800
6801 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
6802 {
6803 c_parser_consume_token (parser);
6804 return true;
6805 }
6806
6807 saved_force_folding_builtin_constant_p
6808 = force_folding_builtin_constant_p;
6809 force_folding_builtin_constant_p |= choose_expr_p;
6810 expr = c_parser_expr_no_commas (parser, NULL);
6811 force_folding_builtin_constant_p
6812 = saved_force_folding_builtin_constant_p;
6813 vec_alloc (cexpr_list, 1);
6814 vec_safe_push (cexpr_list, expr);
6815 while (c_parser_next_token_is (parser, CPP_COMMA))
6816 {
6817 c_parser_consume_token (parser);
6818 expr = c_parser_expr_no_commas (parser, NULL);
6819 vec_safe_push (cexpr_list, expr);
6820 }
6821
6822 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
6823 return false;
6824
6825 *ret_cexpr_list = cexpr_list;
6826 return true;
6827 }
6828
6829 /* This represents a single generic-association. */
6830
6831 struct c_generic_association
6832 {
6833 /* The location of the starting token of the type. */
6834 location_t type_location;
6835 /* The association's type, or NULL_TREE for 'default'. */
6836 tree type;
6837 /* The association's expression. */
6838 struct c_expr expression;
6839 };
6840
6841 /* Parse a generic-selection. (C11 6.5.1.1).
6842
6843 generic-selection:
6844 _Generic ( assignment-expression , generic-assoc-list )
6845
6846 generic-assoc-list:
6847 generic-association
6848 generic-assoc-list , generic-association
6849
6850 generic-association:
6851 type-name : assignment-expression
6852 default : assignment-expression
6853 */
6854
6855 static struct c_expr
6856 c_parser_generic_selection (c_parser *parser)
6857 {
6858 vec<c_generic_association> associations = vNULL;
6859 struct c_expr selector, error_expr;
6860 tree selector_type;
6861 struct c_generic_association matched_assoc;
6862 bool match_found = false;
6863 location_t generic_loc, selector_loc;
6864
6865 error_expr.original_code = ERROR_MARK;
6866 error_expr.original_type = NULL;
6867 error_expr.value = error_mark_node;
6868 matched_assoc.type_location = UNKNOWN_LOCATION;
6869 matched_assoc.type = NULL_TREE;
6870 matched_assoc.expression = error_expr;
6871
6872 gcc_assert (c_parser_next_token_is_keyword (parser, RID_GENERIC));
6873 generic_loc = c_parser_peek_token (parser)->location;
6874 c_parser_consume_token (parser);
6875 if (flag_isoc99)
6876 pedwarn_c99 (generic_loc, OPT_Wpedantic,
6877 "ISO C99 does not support %<_Generic%>");
6878 else
6879 pedwarn_c99 (generic_loc, OPT_Wpedantic,
6880 "ISO C90 does not support %<_Generic%>");
6881
6882 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6883 return error_expr;
6884
6885 c_inhibit_evaluation_warnings++;
6886 selector_loc = c_parser_peek_token (parser)->location;
6887 selector = c_parser_expr_no_commas (parser, NULL);
6888 selector = default_function_array_conversion (selector_loc, selector);
6889 c_inhibit_evaluation_warnings--;
6890
6891 if (selector.value == error_mark_node)
6892 {
6893 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6894 return selector;
6895 }
6896 selector_type = TREE_TYPE (selector.value);
6897 /* In ISO C terms, rvalues (including the controlling expression of
6898 _Generic) do not have qualified types. */
6899 if (TREE_CODE (selector_type) != ARRAY_TYPE)
6900 selector_type = TYPE_MAIN_VARIANT (selector_type);
6901 /* In ISO C terms, _Noreturn is not part of the type of expressions
6902 such as &abort, but in GCC it is represented internally as a type
6903 qualifier. */
6904 if (FUNCTION_POINTER_TYPE_P (selector_type)
6905 && TYPE_QUALS (TREE_TYPE (selector_type)) != TYPE_UNQUALIFIED)
6906 selector_type
6907 = build_pointer_type (TYPE_MAIN_VARIANT (TREE_TYPE (selector_type)));
6908
6909 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
6910 {
6911 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6912 return error_expr;
6913 }
6914
6915 while (1)
6916 {
6917 struct c_generic_association assoc, *iter;
6918 unsigned int ix;
6919 c_token *token = c_parser_peek_token (parser);
6920
6921 assoc.type_location = token->location;
6922 if (token->type == CPP_KEYWORD && token->keyword == RID_DEFAULT)
6923 {
6924 c_parser_consume_token (parser);
6925 assoc.type = NULL_TREE;
6926 }
6927 else
6928 {
6929 struct c_type_name *type_name;
6930
6931 type_name = c_parser_type_name (parser);
6932 if (type_name == NULL)
6933 {
6934 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6935 goto error_exit;
6936 }
6937 assoc.type = groktypename (type_name, NULL, NULL);
6938 if (assoc.type == error_mark_node)
6939 {
6940 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6941 goto error_exit;
6942 }
6943
6944 if (TREE_CODE (assoc.type) == FUNCTION_TYPE)
6945 error_at (assoc.type_location,
6946 "%<_Generic%> association has function type");
6947 else if (!COMPLETE_TYPE_P (assoc.type))
6948 error_at (assoc.type_location,
6949 "%<_Generic%> association has incomplete type");
6950
6951 if (variably_modified_type_p (assoc.type, NULL_TREE))
6952 error_at (assoc.type_location,
6953 "%<_Generic%> association has "
6954 "variable length type");
6955 }
6956
6957 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
6958 {
6959 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6960 goto error_exit;
6961 }
6962
6963 assoc.expression = c_parser_expr_no_commas (parser, NULL);
6964 if (assoc.expression.value == error_mark_node)
6965 {
6966 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6967 goto error_exit;
6968 }
6969
6970 for (ix = 0; associations.iterate (ix, &iter); ++ix)
6971 {
6972 if (assoc.type == NULL_TREE)
6973 {
6974 if (iter->type == NULL_TREE)
6975 {
6976 error_at (assoc.type_location,
6977 "duplicate %<default%> case in %<_Generic%>");
6978 inform (iter->type_location, "original %<default%> is here");
6979 }
6980 }
6981 else if (iter->type != NULL_TREE)
6982 {
6983 if (comptypes (assoc.type, iter->type))
6984 {
6985 error_at (assoc.type_location,
6986 "%<_Generic%> specifies two compatible types");
6987 inform (iter->type_location, "compatible type is here");
6988 }
6989 }
6990 }
6991
6992 if (assoc.type == NULL_TREE)
6993 {
6994 if (!match_found)
6995 {
6996 matched_assoc = assoc;
6997 match_found = true;
6998 }
6999 }
7000 else if (comptypes (assoc.type, selector_type))
7001 {
7002 if (!match_found || matched_assoc.type == NULL_TREE)
7003 {
7004 matched_assoc = assoc;
7005 match_found = true;
7006 }
7007 else
7008 {
7009 error_at (assoc.type_location,
7010 "%<_Generic> selector matches multiple associations");
7011 inform (matched_assoc.type_location,
7012 "other match is here");
7013 }
7014 }
7015
7016 associations.safe_push (assoc);
7017
7018 if (c_parser_peek_token (parser)->type != CPP_COMMA)
7019 break;
7020 c_parser_consume_token (parser);
7021 }
7022
7023 associations.release ();
7024
7025 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
7026 {
7027 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7028 return error_expr;
7029 }
7030
7031 if (!match_found)
7032 {
7033 error_at (selector_loc, "%<_Generic%> selector of type %qT is not "
7034 "compatible with any association",
7035 selector_type);
7036 return error_expr;
7037 }
7038
7039 return matched_assoc.expression;
7040
7041 error_exit:
7042 associations.release ();
7043 return error_expr;
7044 }
7045
7046 /* Parse a postfix expression (C90 6.3.1-6.3.2, C99 6.5.1-6.5.2).
7047
7048 postfix-expression:
7049 primary-expression
7050 postfix-expression [ expression ]
7051 postfix-expression ( argument-expression-list[opt] )
7052 postfix-expression . identifier
7053 postfix-expression -> identifier
7054 postfix-expression ++
7055 postfix-expression --
7056 ( type-name ) { initializer-list }
7057 ( type-name ) { initializer-list , }
7058
7059 argument-expression-list:
7060 argument-expression
7061 argument-expression-list , argument-expression
7062
7063 primary-expression:
7064 identifier
7065 constant
7066 string-literal
7067 ( expression )
7068 generic-selection
7069
7070 GNU extensions:
7071
7072 primary-expression:
7073 __func__
7074 (treated as a keyword in GNU C)
7075 __FUNCTION__
7076 __PRETTY_FUNCTION__
7077 ( compound-statement )
7078 __builtin_va_arg ( assignment-expression , type-name )
7079 __builtin_offsetof ( type-name , offsetof-member-designator )
7080 __builtin_choose_expr ( assignment-expression ,
7081 assignment-expression ,
7082 assignment-expression )
7083 __builtin_types_compatible_p ( type-name , type-name )
7084 __builtin_complex ( assignment-expression , assignment-expression )
7085 __builtin_shuffle ( assignment-expression , assignment-expression )
7086 __builtin_shuffle ( assignment-expression ,
7087 assignment-expression ,
7088 assignment-expression, )
7089
7090 offsetof-member-designator:
7091 identifier
7092 offsetof-member-designator . identifier
7093 offsetof-member-designator [ expression ]
7094
7095 Objective-C:
7096
7097 primary-expression:
7098 [ objc-receiver objc-message-args ]
7099 @selector ( objc-selector-arg )
7100 @protocol ( identifier )
7101 @encode ( type-name )
7102 objc-string-literal
7103 Classname . identifier
7104 */
7105
7106 static struct c_expr
7107 c_parser_postfix_expression (c_parser *parser)
7108 {
7109 struct c_expr expr, e1;
7110 struct c_type_name *t1, *t2;
7111 location_t loc = c_parser_peek_token (parser)->location;;
7112 expr.original_code = ERROR_MARK;
7113 expr.original_type = NULL;
7114 switch (c_parser_peek_token (parser)->type)
7115 {
7116 case CPP_NUMBER:
7117 expr.value = c_parser_peek_token (parser)->value;
7118 loc = c_parser_peek_token (parser)->location;
7119 c_parser_consume_token (parser);
7120 if (TREE_CODE (expr.value) == FIXED_CST
7121 && !targetm.fixed_point_supported_p ())
7122 {
7123 error_at (loc, "fixed-point types not supported for this target");
7124 expr.value = error_mark_node;
7125 }
7126 break;
7127 case CPP_CHAR:
7128 case CPP_CHAR16:
7129 case CPP_CHAR32:
7130 case CPP_WCHAR:
7131 expr.value = c_parser_peek_token (parser)->value;
7132 c_parser_consume_token (parser);
7133 break;
7134 case CPP_STRING:
7135 case CPP_STRING16:
7136 case CPP_STRING32:
7137 case CPP_WSTRING:
7138 case CPP_UTF8STRING:
7139 expr.value = c_parser_peek_token (parser)->value;
7140 expr.original_code = STRING_CST;
7141 c_parser_consume_token (parser);
7142 break;
7143 case CPP_OBJC_STRING:
7144 gcc_assert (c_dialect_objc ());
7145 expr.value
7146 = objc_build_string_object (c_parser_peek_token (parser)->value);
7147 c_parser_consume_token (parser);
7148 break;
7149 case CPP_NAME:
7150 switch (c_parser_peek_token (parser)->id_kind)
7151 {
7152 case C_ID_ID:
7153 {
7154 tree id = c_parser_peek_token (parser)->value;
7155 c_parser_consume_token (parser);
7156 expr.value = build_external_ref (loc, id,
7157 (c_parser_peek_token (parser)->type
7158 == CPP_OPEN_PAREN),
7159 &expr.original_type);
7160 break;
7161 }
7162 case C_ID_CLASSNAME:
7163 {
7164 /* Here we parse the Objective-C 2.0 Class.name dot
7165 syntax. */
7166 tree class_name = c_parser_peek_token (parser)->value;
7167 tree component;
7168 c_parser_consume_token (parser);
7169 gcc_assert (c_dialect_objc ());
7170 if (!c_parser_require (parser, CPP_DOT, "expected %<.%>"))
7171 {
7172 expr.value = error_mark_node;
7173 break;
7174 }
7175 if (c_parser_next_token_is_not (parser, CPP_NAME))
7176 {
7177 c_parser_error (parser, "expected identifier");
7178 expr.value = error_mark_node;
7179 break;
7180 }
7181 component = c_parser_peek_token (parser)->value;
7182 c_parser_consume_token (parser);
7183 expr.value = objc_build_class_component_ref (class_name,
7184 component);
7185 break;
7186 }
7187 default:
7188 c_parser_error (parser, "expected expression");
7189 expr.value = error_mark_node;
7190 break;
7191 }
7192 break;
7193 case CPP_OPEN_PAREN:
7194 /* A parenthesized expression, statement expression or compound
7195 literal. */
7196 if (c_parser_peek_2nd_token (parser)->type == CPP_OPEN_BRACE)
7197 {
7198 /* A statement expression. */
7199 tree stmt;
7200 location_t brace_loc;
7201 c_parser_consume_token (parser);
7202 brace_loc = c_parser_peek_token (parser)->location;
7203 c_parser_consume_token (parser);
7204 if (!building_stmt_list_p ())
7205 {
7206 error_at (loc, "braced-group within expression allowed "
7207 "only inside a function");
7208 parser->error = true;
7209 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
7210 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7211 expr.value = error_mark_node;
7212 break;
7213 }
7214 stmt = c_begin_stmt_expr ();
7215 c_parser_compound_statement_nostart (parser);
7216 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7217 "expected %<)%>");
7218 pedwarn (loc, OPT_Wpedantic,
7219 "ISO C forbids braced-groups within expressions");
7220 expr.value = c_finish_stmt_expr (brace_loc, stmt);
7221 mark_exp_read (expr.value);
7222 }
7223 else if (c_token_starts_typename (c_parser_peek_2nd_token (parser)))
7224 {
7225 /* A compound literal. ??? Can we actually get here rather
7226 than going directly to
7227 c_parser_postfix_expression_after_paren_type from
7228 elsewhere? */
7229 location_t loc;
7230 struct c_type_name *type_name;
7231 c_parser_consume_token (parser);
7232 loc = c_parser_peek_token (parser)->location;
7233 type_name = c_parser_type_name (parser);
7234 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7235 "expected %<)%>");
7236 if (type_name == NULL)
7237 {
7238 expr.value = error_mark_node;
7239 }
7240 else
7241 expr = c_parser_postfix_expression_after_paren_type (parser,
7242 type_name,
7243 loc);
7244 }
7245 else
7246 {
7247 /* A parenthesized expression. */
7248 c_parser_consume_token (parser);
7249 expr = c_parser_expression (parser);
7250 if (TREE_CODE (expr.value) == MODIFY_EXPR)
7251 TREE_NO_WARNING (expr.value) = 1;
7252 if (expr.original_code != C_MAYBE_CONST_EXPR)
7253 expr.original_code = ERROR_MARK;
7254 /* Don't change EXPR.ORIGINAL_TYPE. */
7255 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7256 "expected %<)%>");
7257 }
7258 break;
7259 case CPP_KEYWORD:
7260 switch (c_parser_peek_token (parser)->keyword)
7261 {
7262 case RID_FUNCTION_NAME:
7263 pedwarn (loc, OPT_Wpedantic, "ISO C does not support "
7264 "%<__FUNCTION__%> predefined identifier");
7265 expr.value = fname_decl (loc,
7266 c_parser_peek_token (parser)->keyword,
7267 c_parser_peek_token (parser)->value);
7268 c_parser_consume_token (parser);
7269 break;
7270 case RID_PRETTY_FUNCTION_NAME:
7271 pedwarn (loc, OPT_Wpedantic, "ISO C does not support "
7272 "%<__PRETTY_FUNCTION__%> predefined identifier");
7273 expr.value = fname_decl (loc,
7274 c_parser_peek_token (parser)->keyword,
7275 c_parser_peek_token (parser)->value);
7276 c_parser_consume_token (parser);
7277 break;
7278 case RID_C99_FUNCTION_NAME:
7279 pedwarn_c90 (loc, OPT_Wpedantic, "ISO C90 does not support "
7280 "%<__func__%> predefined identifier");
7281 expr.value = fname_decl (loc,
7282 c_parser_peek_token (parser)->keyword,
7283 c_parser_peek_token (parser)->value);
7284 c_parser_consume_token (parser);
7285 break;
7286 case RID_VA_ARG:
7287 c_parser_consume_token (parser);
7288 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7289 {
7290 expr.value = error_mark_node;
7291 break;
7292 }
7293 e1 = c_parser_expr_no_commas (parser, NULL);
7294 mark_exp_read (e1.value);
7295 e1.value = c_fully_fold (e1.value, false, NULL);
7296 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
7297 {
7298 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7299 expr.value = error_mark_node;
7300 break;
7301 }
7302 loc = c_parser_peek_token (parser)->location;
7303 t1 = c_parser_type_name (parser);
7304 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7305 "expected %<)%>");
7306 if (t1 == NULL)
7307 {
7308 expr.value = error_mark_node;
7309 }
7310 else
7311 {
7312 tree type_expr = NULL_TREE;
7313 expr.value = c_build_va_arg (loc, e1.value,
7314 groktypename (t1, &type_expr, NULL));
7315 if (type_expr)
7316 {
7317 expr.value = build2 (C_MAYBE_CONST_EXPR,
7318 TREE_TYPE (expr.value), type_expr,
7319 expr.value);
7320 C_MAYBE_CONST_EXPR_NON_CONST (expr.value) = true;
7321 }
7322 }
7323 break;
7324 case RID_OFFSETOF:
7325 c_parser_consume_token (parser);
7326 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7327 {
7328 expr.value = error_mark_node;
7329 break;
7330 }
7331 t1 = c_parser_type_name (parser);
7332 if (t1 == NULL)
7333 parser->error = true;
7334 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
7335 gcc_assert (parser->error);
7336 if (parser->error)
7337 {
7338 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7339 expr.value = error_mark_node;
7340 break;
7341 }
7342
7343 {
7344 tree type = groktypename (t1, NULL, NULL);
7345 tree offsetof_ref;
7346 if (type == error_mark_node)
7347 offsetof_ref = error_mark_node;
7348 else
7349 {
7350 offsetof_ref = build1 (INDIRECT_REF, type, null_pointer_node);
7351 SET_EXPR_LOCATION (offsetof_ref, loc);
7352 }
7353 /* Parse the second argument to __builtin_offsetof. We
7354 must have one identifier, and beyond that we want to
7355 accept sub structure and sub array references. */
7356 if (c_parser_next_token_is (parser, CPP_NAME))
7357 {
7358 offsetof_ref = build_component_ref
7359 (loc, offsetof_ref, c_parser_peek_token (parser)->value);
7360 c_parser_consume_token (parser);
7361 while (c_parser_next_token_is (parser, CPP_DOT)
7362 || c_parser_next_token_is (parser,
7363 CPP_OPEN_SQUARE)
7364 || c_parser_next_token_is (parser,
7365 CPP_DEREF))
7366 {
7367 if (c_parser_next_token_is (parser, CPP_DEREF))
7368 {
7369 loc = c_parser_peek_token (parser)->location;
7370 offsetof_ref = build_array_ref (loc,
7371 offsetof_ref,
7372 integer_zero_node);
7373 goto do_dot;
7374 }
7375 else if (c_parser_next_token_is (parser, CPP_DOT))
7376 {
7377 do_dot:
7378 c_parser_consume_token (parser);
7379 if (c_parser_next_token_is_not (parser,
7380 CPP_NAME))
7381 {
7382 c_parser_error (parser, "expected identifier");
7383 break;
7384 }
7385 offsetof_ref = build_component_ref
7386 (loc, offsetof_ref,
7387 c_parser_peek_token (parser)->value);
7388 c_parser_consume_token (parser);
7389 }
7390 else
7391 {
7392 struct c_expr ce;
7393 tree idx;
7394 loc = c_parser_peek_token (parser)->location;
7395 c_parser_consume_token (parser);
7396 ce = c_parser_expression (parser);
7397 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
7398 idx = ce.value;
7399 idx = c_fully_fold (idx, false, NULL);
7400 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
7401 "expected %<]%>");
7402 offsetof_ref = build_array_ref (loc, offsetof_ref, idx);
7403 }
7404 }
7405 }
7406 else
7407 c_parser_error (parser, "expected identifier");
7408 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7409 "expected %<)%>");
7410 expr.value = fold_offsetof (offsetof_ref);
7411 }
7412 break;
7413 case RID_CHOOSE_EXPR:
7414 {
7415 vec<c_expr_t, va_gc> *cexpr_list;
7416 c_expr_t *e1_p, *e2_p, *e3_p;
7417 tree c;
7418
7419 c_parser_consume_token (parser);
7420 if (!c_parser_get_builtin_args (parser,
7421 "__builtin_choose_expr",
7422 &cexpr_list, true))
7423 {
7424 expr.value = error_mark_node;
7425 break;
7426 }
7427
7428 if (vec_safe_length (cexpr_list) != 3)
7429 {
7430 error_at (loc, "wrong number of arguments to "
7431 "%<__builtin_choose_expr%>");
7432 expr.value = error_mark_node;
7433 break;
7434 }
7435
7436 e1_p = &(*cexpr_list)[0];
7437 e2_p = &(*cexpr_list)[1];
7438 e3_p = &(*cexpr_list)[2];
7439
7440 c = e1_p->value;
7441 mark_exp_read (e2_p->value);
7442 mark_exp_read (e3_p->value);
7443 if (TREE_CODE (c) != INTEGER_CST
7444 || !INTEGRAL_TYPE_P (TREE_TYPE (c)))
7445 error_at (loc,
7446 "first argument to %<__builtin_choose_expr%> not"
7447 " a constant");
7448 constant_expression_warning (c);
7449 expr = integer_zerop (c) ? *e3_p : *e2_p;
7450 break;
7451 }
7452 case RID_TYPES_COMPATIBLE_P:
7453 c_parser_consume_token (parser);
7454 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7455 {
7456 expr.value = error_mark_node;
7457 break;
7458 }
7459 t1 = c_parser_type_name (parser);
7460 if (t1 == NULL)
7461 {
7462 expr.value = error_mark_node;
7463 break;
7464 }
7465 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
7466 {
7467 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7468 expr.value = error_mark_node;
7469 break;
7470 }
7471 t2 = c_parser_type_name (parser);
7472 if (t2 == NULL)
7473 {
7474 expr.value = error_mark_node;
7475 break;
7476 }
7477 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7478 "expected %<)%>");
7479 {
7480 tree e1, e2;
7481 e1 = groktypename (t1, NULL, NULL);
7482 e2 = groktypename (t2, NULL, NULL);
7483 if (e1 == error_mark_node || e2 == error_mark_node)
7484 {
7485 expr.value = error_mark_node;
7486 break;
7487 }
7488
7489 e1 = TYPE_MAIN_VARIANT (e1);
7490 e2 = TYPE_MAIN_VARIANT (e2);
7491
7492 expr.value
7493 = comptypes (e1, e2) ? integer_one_node : integer_zero_node;
7494 }
7495 break;
7496 case RID_BUILTIN_CALL_WITH_STATIC_CHAIN:
7497 {
7498 vec<c_expr_t, va_gc> *cexpr_list;
7499 c_expr_t *e2_p;
7500 tree chain_value;
7501
7502 c_parser_consume_token (parser);
7503 if (!c_parser_get_builtin_args (parser,
7504 "__builtin_call_with_static_chain",
7505 &cexpr_list, false))
7506 {
7507 expr.value = error_mark_node;
7508 break;
7509 }
7510 if (vec_safe_length (cexpr_list) != 2)
7511 {
7512 error_at (loc, "wrong number of arguments to "
7513 "%<__builtin_call_with_static_chain%>");
7514 expr.value = error_mark_node;
7515 break;
7516 }
7517
7518 expr = (*cexpr_list)[0];
7519 e2_p = &(*cexpr_list)[1];
7520 *e2_p = convert_lvalue_to_rvalue (loc, *e2_p, true, true);
7521 chain_value = e2_p->value;
7522 mark_exp_read (chain_value);
7523
7524 if (TREE_CODE (expr.value) != CALL_EXPR)
7525 error_at (loc, "first argument to "
7526 "%<__builtin_call_with_static_chain%> "
7527 "must be a call expression");
7528 else if (TREE_CODE (TREE_TYPE (chain_value)) != POINTER_TYPE)
7529 error_at (loc, "second argument to "
7530 "%<__builtin_call_with_static_chain%> "
7531 "must be a pointer type");
7532 else
7533 CALL_EXPR_STATIC_CHAIN (expr.value) = chain_value;
7534 break;
7535 }
7536 case RID_BUILTIN_COMPLEX:
7537 {
7538 vec<c_expr_t, va_gc> *cexpr_list;
7539 c_expr_t *e1_p, *e2_p;
7540
7541 c_parser_consume_token (parser);
7542 if (!c_parser_get_builtin_args (parser,
7543 "__builtin_complex",
7544 &cexpr_list, false))
7545 {
7546 expr.value = error_mark_node;
7547 break;
7548 }
7549
7550 if (vec_safe_length (cexpr_list) != 2)
7551 {
7552 error_at (loc, "wrong number of arguments to "
7553 "%<__builtin_complex%>");
7554 expr.value = error_mark_node;
7555 break;
7556 }
7557
7558 e1_p = &(*cexpr_list)[0];
7559 e2_p = &(*cexpr_list)[1];
7560
7561 *e1_p = convert_lvalue_to_rvalue (loc, *e1_p, true, true);
7562 if (TREE_CODE (e1_p->value) == EXCESS_PRECISION_EXPR)
7563 e1_p->value = convert (TREE_TYPE (e1_p->value),
7564 TREE_OPERAND (e1_p->value, 0));
7565 *e2_p = convert_lvalue_to_rvalue (loc, *e2_p, true, true);
7566 if (TREE_CODE (e2_p->value) == EXCESS_PRECISION_EXPR)
7567 e2_p->value = convert (TREE_TYPE (e2_p->value),
7568 TREE_OPERAND (e2_p->value, 0));
7569 if (!SCALAR_FLOAT_TYPE_P (TREE_TYPE (e1_p->value))
7570 || DECIMAL_FLOAT_TYPE_P (TREE_TYPE (e1_p->value))
7571 || !SCALAR_FLOAT_TYPE_P (TREE_TYPE (e2_p->value))
7572 || DECIMAL_FLOAT_TYPE_P (TREE_TYPE (e2_p->value)))
7573 {
7574 error_at (loc, "%<__builtin_complex%> operand "
7575 "not of real binary floating-point type");
7576 expr.value = error_mark_node;
7577 break;
7578 }
7579 if (TYPE_MAIN_VARIANT (TREE_TYPE (e1_p->value))
7580 != TYPE_MAIN_VARIANT (TREE_TYPE (e2_p->value)))
7581 {
7582 error_at (loc,
7583 "%<__builtin_complex%> operands of different types");
7584 expr.value = error_mark_node;
7585 break;
7586 }
7587 pedwarn_c90 (loc, OPT_Wpedantic,
7588 "ISO C90 does not support complex types");
7589 expr.value = build2 (COMPLEX_EXPR,
7590 build_complex_type
7591 (TYPE_MAIN_VARIANT
7592 (TREE_TYPE (e1_p->value))),
7593 e1_p->value, e2_p->value);
7594 break;
7595 }
7596 case RID_BUILTIN_SHUFFLE:
7597 {
7598 vec<c_expr_t, va_gc> *cexpr_list;
7599 unsigned int i;
7600 c_expr_t *p;
7601
7602 c_parser_consume_token (parser);
7603 if (!c_parser_get_builtin_args (parser,
7604 "__builtin_shuffle",
7605 &cexpr_list, false))
7606 {
7607 expr.value = error_mark_node;
7608 break;
7609 }
7610
7611 FOR_EACH_VEC_SAFE_ELT (cexpr_list, i, p)
7612 *p = convert_lvalue_to_rvalue (loc, *p, true, true);
7613
7614 if (vec_safe_length (cexpr_list) == 2)
7615 expr.value =
7616 c_build_vec_perm_expr
7617 (loc, (*cexpr_list)[0].value,
7618 NULL_TREE, (*cexpr_list)[1].value);
7619
7620 else if (vec_safe_length (cexpr_list) == 3)
7621 expr.value =
7622 c_build_vec_perm_expr
7623 (loc, (*cexpr_list)[0].value,
7624 (*cexpr_list)[1].value,
7625 (*cexpr_list)[2].value);
7626 else
7627 {
7628 error_at (loc, "wrong number of arguments to "
7629 "%<__builtin_shuffle%>");
7630 expr.value = error_mark_node;
7631 }
7632 break;
7633 }
7634 case RID_AT_SELECTOR:
7635 gcc_assert (c_dialect_objc ());
7636 c_parser_consume_token (parser);
7637 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7638 {
7639 expr.value = error_mark_node;
7640 break;
7641 }
7642 {
7643 tree sel = c_parser_objc_selector_arg (parser);
7644 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7645 "expected %<)%>");
7646 expr.value = objc_build_selector_expr (loc, sel);
7647 }
7648 break;
7649 case RID_AT_PROTOCOL:
7650 gcc_assert (c_dialect_objc ());
7651 c_parser_consume_token (parser);
7652 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7653 {
7654 expr.value = error_mark_node;
7655 break;
7656 }
7657 if (c_parser_next_token_is_not (parser, CPP_NAME))
7658 {
7659 c_parser_error (parser, "expected identifier");
7660 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7661 expr.value = error_mark_node;
7662 break;
7663 }
7664 {
7665 tree id = c_parser_peek_token (parser)->value;
7666 c_parser_consume_token (parser);
7667 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7668 "expected %<)%>");
7669 expr.value = objc_build_protocol_expr (id);
7670 }
7671 break;
7672 case RID_AT_ENCODE:
7673 /* Extension to support C-structures in the archiver. */
7674 gcc_assert (c_dialect_objc ());
7675 c_parser_consume_token (parser);
7676 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7677 {
7678 expr.value = error_mark_node;
7679 break;
7680 }
7681 t1 = c_parser_type_name (parser);
7682 if (t1 == NULL)
7683 {
7684 expr.value = error_mark_node;
7685 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7686 break;
7687 }
7688 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7689 "expected %<)%>");
7690 {
7691 tree type = groktypename (t1, NULL, NULL);
7692 expr.value = objc_build_encode_expr (type);
7693 }
7694 break;
7695 case RID_GENERIC:
7696 expr = c_parser_generic_selection (parser);
7697 break;
7698 case RID_CILK_SPAWN:
7699 c_parser_consume_token (parser);
7700 if (!flag_cilkplus)
7701 {
7702 error_at (loc, "-fcilkplus must be enabled to use "
7703 "%<_Cilk_spawn%>");
7704 expr = c_parser_postfix_expression (parser);
7705 expr.value = error_mark_node;
7706 }
7707 else if (c_parser_peek_token (parser)->keyword == RID_CILK_SPAWN)
7708 {
7709 error_at (loc, "consecutive %<_Cilk_spawn%> keywords "
7710 "are not permitted");
7711 /* Now flush out all the _Cilk_spawns. */
7712 while (c_parser_peek_token (parser)->keyword == RID_CILK_SPAWN)
7713 c_parser_consume_token (parser);
7714 expr = c_parser_postfix_expression (parser);
7715 }
7716 else
7717 {
7718 expr = c_parser_postfix_expression (parser);
7719 expr.value = build_cilk_spawn (loc, expr.value);
7720 }
7721 break;
7722 default:
7723 c_parser_error (parser, "expected expression");
7724 expr.value = error_mark_node;
7725 break;
7726 }
7727 break;
7728 case CPP_OPEN_SQUARE:
7729 if (c_dialect_objc ())
7730 {
7731 tree receiver, args;
7732 c_parser_consume_token (parser);
7733 receiver = c_parser_objc_receiver (parser);
7734 args = c_parser_objc_message_args (parser);
7735 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
7736 "expected %<]%>");
7737 expr.value = objc_build_message_expr (receiver, args);
7738 break;
7739 }
7740 /* Else fall through to report error. */
7741 default:
7742 c_parser_error (parser, "expected expression");
7743 expr.value = error_mark_node;
7744 break;
7745 }
7746 return c_parser_postfix_expression_after_primary (parser, loc, expr);
7747 }
7748
7749 /* Parse a postfix expression after a parenthesized type name: the
7750 brace-enclosed initializer of a compound literal, possibly followed
7751 by some postfix operators. This is separate because it is not
7752 possible to tell until after the type name whether a cast
7753 expression has a cast or a compound literal, or whether the operand
7754 of sizeof is a parenthesized type name or starts with a compound
7755 literal. TYPE_LOC is the location where TYPE_NAME starts--the
7756 location of the first token after the parentheses around the type
7757 name. */
7758
7759 static struct c_expr
7760 c_parser_postfix_expression_after_paren_type (c_parser *parser,
7761 struct c_type_name *type_name,
7762 location_t type_loc)
7763 {
7764 tree type;
7765 struct c_expr init;
7766 bool non_const;
7767 struct c_expr expr;
7768 location_t start_loc;
7769 tree type_expr = NULL_TREE;
7770 bool type_expr_const = true;
7771 check_compound_literal_type (type_loc, type_name);
7772 start_init (NULL_TREE, NULL, 0);
7773 type = groktypename (type_name, &type_expr, &type_expr_const);
7774 start_loc = c_parser_peek_token (parser)->location;
7775 if (type != error_mark_node && C_TYPE_VARIABLE_SIZE (type))
7776 {
7777 error_at (type_loc, "compound literal has variable size");
7778 type = error_mark_node;
7779 }
7780 init = c_parser_braced_init (parser, type, false);
7781 finish_init ();
7782 maybe_warn_string_init (type_loc, type, init);
7783
7784 if (type != error_mark_node
7785 && !ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (type))
7786 && current_function_decl)
7787 {
7788 error ("compound literal qualified by address-space qualifier");
7789 type = error_mark_node;
7790 }
7791
7792 pedwarn_c90 (start_loc, OPT_Wpedantic, "ISO C90 forbids compound literals");
7793 non_const = ((init.value && TREE_CODE (init.value) == CONSTRUCTOR)
7794 ? CONSTRUCTOR_NON_CONST (init.value)
7795 : init.original_code == C_MAYBE_CONST_EXPR);
7796 non_const |= !type_expr_const;
7797 expr.value = build_compound_literal (start_loc, type, init.value, non_const);
7798 expr.original_code = ERROR_MARK;
7799 expr.original_type = NULL;
7800 if (type_expr)
7801 {
7802 if (TREE_CODE (expr.value) == C_MAYBE_CONST_EXPR)
7803 {
7804 gcc_assert (C_MAYBE_CONST_EXPR_PRE (expr.value) == NULL_TREE);
7805 C_MAYBE_CONST_EXPR_PRE (expr.value) = type_expr;
7806 }
7807 else
7808 {
7809 gcc_assert (!non_const);
7810 expr.value = build2 (C_MAYBE_CONST_EXPR, type,
7811 type_expr, expr.value);
7812 }
7813 }
7814 return c_parser_postfix_expression_after_primary (parser, start_loc, expr);
7815 }
7816
7817 /* Callback function for sizeof_pointer_memaccess_warning to compare
7818 types. */
7819
7820 static bool
7821 sizeof_ptr_memacc_comptypes (tree type1, tree type2)
7822 {
7823 return comptypes (type1, type2) == 1;
7824 }
7825
7826 /* Parse a postfix expression after the initial primary or compound
7827 literal; that is, parse a series of postfix operators.
7828
7829 EXPR_LOC is the location of the primary expression. */
7830
7831 static struct c_expr
7832 c_parser_postfix_expression_after_primary (c_parser *parser,
7833 location_t expr_loc,
7834 struct c_expr expr)
7835 {
7836 struct c_expr orig_expr;
7837 tree ident, idx;
7838 location_t sizeof_arg_loc[3];
7839 tree sizeof_arg[3];
7840 unsigned int literal_zero_mask;
7841 unsigned int i;
7842 vec<tree, va_gc> *exprlist;
7843 vec<tree, va_gc> *origtypes = NULL;
7844 vec<location_t> arg_loc = vNULL;
7845
7846 while (true)
7847 {
7848 location_t op_loc = c_parser_peek_token (parser)->location;
7849 switch (c_parser_peek_token (parser)->type)
7850 {
7851 case CPP_OPEN_SQUARE:
7852 /* Array reference. */
7853 c_parser_consume_token (parser);
7854 if (flag_cilkplus
7855 && c_parser_peek_token (parser)->type == CPP_COLON)
7856 /* If we are here, then we have something like this:
7857 Array [ : ]
7858 */
7859 expr.value = c_parser_array_notation (expr_loc, parser, NULL_TREE,
7860 expr.value);
7861 else
7862 {
7863 idx = c_parser_expression (parser).value;
7864 /* Here we have 3 options:
7865 1. Array [EXPR] -- Normal Array call.
7866 2. Array [EXPR : EXPR] -- Array notation without stride.
7867 3. Array [EXPR : EXPR : EXPR] -- Array notation with stride.
7868
7869 For 1, we just handle it just like a normal array expression.
7870 For 2 and 3 we handle it like we handle array notations. The
7871 idx value we have above becomes the initial/start index.
7872 */
7873 if (flag_cilkplus
7874 && c_parser_peek_token (parser)->type == CPP_COLON)
7875 expr.value = c_parser_array_notation (expr_loc, parser, idx,
7876 expr.value);
7877 else
7878 {
7879 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
7880 "expected %<]%>");
7881 expr.value = build_array_ref (op_loc, expr.value, idx);
7882 }
7883 }
7884 expr.original_code = ERROR_MARK;
7885 expr.original_type = NULL;
7886 break;
7887 case CPP_OPEN_PAREN:
7888 /* Function call. */
7889 c_parser_consume_token (parser);
7890 for (i = 0; i < 3; i++)
7891 {
7892 sizeof_arg[i] = NULL_TREE;
7893 sizeof_arg_loc[i] = UNKNOWN_LOCATION;
7894 }
7895 literal_zero_mask = 0;
7896 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
7897 exprlist = NULL;
7898 else
7899 exprlist = c_parser_expr_list (parser, true, false, &origtypes,
7900 sizeof_arg_loc, sizeof_arg,
7901 &arg_loc, &literal_zero_mask);
7902 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7903 "expected %<)%>");
7904 orig_expr = expr;
7905 mark_exp_read (expr.value);
7906 if (warn_sizeof_pointer_memaccess)
7907 sizeof_pointer_memaccess_warning (sizeof_arg_loc,
7908 expr.value, exprlist,
7909 sizeof_arg,
7910 sizeof_ptr_memacc_comptypes);
7911 if (warn_memset_transposed_args
7912 && TREE_CODE (expr.value) == FUNCTION_DECL
7913 && DECL_BUILT_IN_CLASS (expr.value) == BUILT_IN_NORMAL
7914 && DECL_FUNCTION_CODE (expr.value) == BUILT_IN_MEMSET
7915 && vec_safe_length (exprlist) == 3
7916 && integer_zerop ((*exprlist)[2])
7917 && (literal_zero_mask & (1 << 2)) != 0
7918 && (!integer_zerop ((*exprlist)[1])
7919 || (literal_zero_mask & (1 << 1)) == 0))
7920 warning_at (expr_loc, OPT_Wmemset_transposed_args,
7921 "%<memset%> used with constant zero length parameter; "
7922 "this could be due to transposed parameters");
7923
7924 expr.value
7925 = c_build_function_call_vec (expr_loc, arg_loc, expr.value,
7926 exprlist, origtypes);
7927 expr.original_code = ERROR_MARK;
7928 if (TREE_CODE (expr.value) == INTEGER_CST
7929 && TREE_CODE (orig_expr.value) == FUNCTION_DECL
7930 && DECL_BUILT_IN_CLASS (orig_expr.value) == BUILT_IN_NORMAL
7931 && DECL_FUNCTION_CODE (orig_expr.value) == BUILT_IN_CONSTANT_P)
7932 expr.original_code = C_MAYBE_CONST_EXPR;
7933 expr.original_type = NULL;
7934 if (exprlist)
7935 {
7936 release_tree_vector (exprlist);
7937 release_tree_vector (origtypes);
7938 }
7939 arg_loc.release ();
7940 break;
7941 case CPP_DOT:
7942 /* Structure element reference. */
7943 c_parser_consume_token (parser);
7944 expr = default_function_array_conversion (expr_loc, expr);
7945 if (c_parser_next_token_is (parser, CPP_NAME))
7946 ident = c_parser_peek_token (parser)->value;
7947 else
7948 {
7949 c_parser_error (parser, "expected identifier");
7950 expr.value = error_mark_node;
7951 expr.original_code = ERROR_MARK;
7952 expr.original_type = NULL;
7953 return expr;
7954 }
7955 c_parser_consume_token (parser);
7956 expr.value = build_component_ref (op_loc, expr.value, ident);
7957 expr.original_code = ERROR_MARK;
7958 if (TREE_CODE (expr.value) != COMPONENT_REF)
7959 expr.original_type = NULL;
7960 else
7961 {
7962 /* Remember the original type of a bitfield. */
7963 tree field = TREE_OPERAND (expr.value, 1);
7964 if (TREE_CODE (field) != FIELD_DECL)
7965 expr.original_type = NULL;
7966 else
7967 expr.original_type = DECL_BIT_FIELD_TYPE (field);
7968 }
7969 break;
7970 case CPP_DEREF:
7971 /* Structure element reference. */
7972 c_parser_consume_token (parser);
7973 expr = convert_lvalue_to_rvalue (expr_loc, expr, true, false);
7974 if (c_parser_next_token_is (parser, CPP_NAME))
7975 ident = c_parser_peek_token (parser)->value;
7976 else
7977 {
7978 c_parser_error (parser, "expected identifier");
7979 expr.value = error_mark_node;
7980 expr.original_code = ERROR_MARK;
7981 expr.original_type = NULL;
7982 return expr;
7983 }
7984 c_parser_consume_token (parser);
7985 expr.value = build_component_ref (op_loc,
7986 build_indirect_ref (op_loc,
7987 expr.value,
7988 RO_ARROW),
7989 ident);
7990 expr.original_code = ERROR_MARK;
7991 if (TREE_CODE (expr.value) != COMPONENT_REF)
7992 expr.original_type = NULL;
7993 else
7994 {
7995 /* Remember the original type of a bitfield. */
7996 tree field = TREE_OPERAND (expr.value, 1);
7997 if (TREE_CODE (field) != FIELD_DECL)
7998 expr.original_type = NULL;
7999 else
8000 expr.original_type = DECL_BIT_FIELD_TYPE (field);
8001 }
8002 break;
8003 case CPP_PLUS_PLUS:
8004 /* Postincrement. */
8005 c_parser_consume_token (parser);
8006 /* If the expressions have array notations, we expand them. */
8007 if (flag_cilkplus
8008 && TREE_CODE (expr.value) == ARRAY_NOTATION_REF)
8009 expr = fix_array_notation_expr (expr_loc, POSTINCREMENT_EXPR, expr);
8010 else
8011 {
8012 expr = default_function_array_read_conversion (expr_loc, expr);
8013 expr.value = build_unary_op (op_loc,
8014 POSTINCREMENT_EXPR, expr.value, 0);
8015 }
8016 expr.original_code = ERROR_MARK;
8017 expr.original_type = NULL;
8018 break;
8019 case CPP_MINUS_MINUS:
8020 /* Postdecrement. */
8021 c_parser_consume_token (parser);
8022 /* If the expressions have array notations, we expand them. */
8023 if (flag_cilkplus
8024 && TREE_CODE (expr.value) == ARRAY_NOTATION_REF)
8025 expr = fix_array_notation_expr (expr_loc, POSTDECREMENT_EXPR, expr);
8026 else
8027 {
8028 expr = default_function_array_read_conversion (expr_loc, expr);
8029 expr.value = build_unary_op (op_loc,
8030 POSTDECREMENT_EXPR, expr.value, 0);
8031 }
8032 expr.original_code = ERROR_MARK;
8033 expr.original_type = NULL;
8034 break;
8035 default:
8036 return expr;
8037 }
8038 }
8039 }
8040
8041 /* Parse an expression (C90 6.3.17, C99 6.5.17).
8042
8043 expression:
8044 assignment-expression
8045 expression , assignment-expression
8046 */
8047
8048 static struct c_expr
8049 c_parser_expression (c_parser *parser)
8050 {
8051 location_t tloc = c_parser_peek_token (parser)->location;
8052 struct c_expr expr;
8053 expr = c_parser_expr_no_commas (parser, NULL);
8054 if (c_parser_next_token_is (parser, CPP_COMMA))
8055 expr = convert_lvalue_to_rvalue (tloc, expr, true, false);
8056 while (c_parser_next_token_is (parser, CPP_COMMA))
8057 {
8058 struct c_expr next;
8059 tree lhsval;
8060 location_t loc = c_parser_peek_token (parser)->location;
8061 location_t expr_loc;
8062 c_parser_consume_token (parser);
8063 expr_loc = c_parser_peek_token (parser)->location;
8064 lhsval = expr.value;
8065 while (TREE_CODE (lhsval) == COMPOUND_EXPR)
8066 lhsval = TREE_OPERAND (lhsval, 1);
8067 if (DECL_P (lhsval) || handled_component_p (lhsval))
8068 mark_exp_read (lhsval);
8069 next = c_parser_expr_no_commas (parser, NULL);
8070 next = convert_lvalue_to_rvalue (expr_loc, next, true, false);
8071 expr.value = build_compound_expr (loc, expr.value, next.value);
8072 expr.original_code = COMPOUND_EXPR;
8073 expr.original_type = next.original_type;
8074 }
8075 return expr;
8076 }
8077
8078 /* Parse an expression and convert functions or arrays to pointers and
8079 lvalues to rvalues. */
8080
8081 static struct c_expr
8082 c_parser_expression_conv (c_parser *parser)
8083 {
8084 struct c_expr expr;
8085 location_t loc = c_parser_peek_token (parser)->location;
8086 expr = c_parser_expression (parser);
8087 expr = convert_lvalue_to_rvalue (loc, expr, true, false);
8088 return expr;
8089 }
8090
8091 /* Helper function of c_parser_expr_list. Check if IDXth (0 based)
8092 argument is a literal zero alone and if so, set it in literal_zero_mask. */
8093
8094 static inline void
8095 c_parser_check_literal_zero (c_parser *parser, unsigned *literal_zero_mask,
8096 unsigned int idx)
8097 {
8098 if (idx >= HOST_BITS_PER_INT)
8099 return;
8100
8101 c_token *tok = c_parser_peek_token (parser);
8102 switch (tok->type)
8103 {
8104 case CPP_NUMBER:
8105 case CPP_CHAR:
8106 case CPP_WCHAR:
8107 case CPP_CHAR16:
8108 case CPP_CHAR32:
8109 /* If a parameter is literal zero alone, remember it
8110 for -Wmemset-transposed-args warning. */
8111 if (integer_zerop (tok->value)
8112 && !TREE_OVERFLOW (tok->value)
8113 && (c_parser_peek_2nd_token (parser)->type == CPP_COMMA
8114 || c_parser_peek_2nd_token (parser)->type == CPP_CLOSE_PAREN))
8115 *literal_zero_mask |= 1U << idx;
8116 default:
8117 break;
8118 }
8119 }
8120
8121 /* Parse a non-empty list of expressions. If CONVERT_P, convert
8122 functions and arrays to pointers and lvalues to rvalues. If
8123 FOLD_P, fold the expressions. If LOCATIONS is non-NULL, save the
8124 locations of function arguments into this vector.
8125
8126 nonempty-expr-list:
8127 assignment-expression
8128 nonempty-expr-list , assignment-expression
8129 */
8130
8131 static vec<tree, va_gc> *
8132 c_parser_expr_list (c_parser *parser, bool convert_p, bool fold_p,
8133 vec<tree, va_gc> **p_orig_types,
8134 location_t *sizeof_arg_loc, tree *sizeof_arg,
8135 vec<location_t> *locations,
8136 unsigned int *literal_zero_mask)
8137 {
8138 vec<tree, va_gc> *ret;
8139 vec<tree, va_gc> *orig_types;
8140 struct c_expr expr;
8141 location_t loc = c_parser_peek_token (parser)->location;
8142 location_t cur_sizeof_arg_loc = UNKNOWN_LOCATION;
8143 unsigned int idx = 0;
8144
8145 ret = make_tree_vector ();
8146 if (p_orig_types == NULL)
8147 orig_types = NULL;
8148 else
8149 orig_types = make_tree_vector ();
8150
8151 if (sizeof_arg != NULL
8152 && c_parser_next_token_is_keyword (parser, RID_SIZEOF))
8153 cur_sizeof_arg_loc = c_parser_peek_2nd_token (parser)->location;
8154 if (literal_zero_mask)
8155 c_parser_check_literal_zero (parser, literal_zero_mask, 0);
8156 expr = c_parser_expr_no_commas (parser, NULL);
8157 if (convert_p)
8158 expr = convert_lvalue_to_rvalue (loc, expr, true, true);
8159 if (fold_p)
8160 expr.value = c_fully_fold (expr.value, false, NULL);
8161 ret->quick_push (expr.value);
8162 if (orig_types)
8163 orig_types->quick_push (expr.original_type);
8164 if (locations)
8165 locations->safe_push (loc);
8166 if (sizeof_arg != NULL
8167 && cur_sizeof_arg_loc != UNKNOWN_LOCATION
8168 && expr.original_code == SIZEOF_EXPR)
8169 {
8170 sizeof_arg[0] = c_last_sizeof_arg;
8171 sizeof_arg_loc[0] = cur_sizeof_arg_loc;
8172 }
8173 while (c_parser_next_token_is (parser, CPP_COMMA))
8174 {
8175 c_parser_consume_token (parser);
8176 loc = c_parser_peek_token (parser)->location;
8177 if (sizeof_arg != NULL
8178 && c_parser_next_token_is_keyword (parser, RID_SIZEOF))
8179 cur_sizeof_arg_loc = c_parser_peek_2nd_token (parser)->location;
8180 else
8181 cur_sizeof_arg_loc = UNKNOWN_LOCATION;
8182 if (literal_zero_mask)
8183 c_parser_check_literal_zero (parser, literal_zero_mask, idx + 1);
8184 expr = c_parser_expr_no_commas (parser, NULL);
8185 if (convert_p)
8186 expr = convert_lvalue_to_rvalue (loc, expr, true, true);
8187 if (fold_p)
8188 expr.value = c_fully_fold (expr.value, false, NULL);
8189 vec_safe_push (ret, expr.value);
8190 if (orig_types)
8191 vec_safe_push (orig_types, expr.original_type);
8192 if (locations)
8193 locations->safe_push (loc);
8194 if (++idx < 3
8195 && sizeof_arg != NULL
8196 && cur_sizeof_arg_loc != UNKNOWN_LOCATION
8197 && expr.original_code == SIZEOF_EXPR)
8198 {
8199 sizeof_arg[idx] = c_last_sizeof_arg;
8200 sizeof_arg_loc[idx] = cur_sizeof_arg_loc;
8201 }
8202 }
8203 if (orig_types)
8204 *p_orig_types = orig_types;
8205 return ret;
8206 }
8207 \f
8208 /* Parse Objective-C-specific constructs. */
8209
8210 /* Parse an objc-class-definition.
8211
8212 objc-class-definition:
8213 @interface identifier objc-superclass[opt] objc-protocol-refs[opt]
8214 objc-class-instance-variables[opt] objc-methodprotolist @end
8215 @implementation identifier objc-superclass[opt]
8216 objc-class-instance-variables[opt]
8217 @interface identifier ( identifier ) objc-protocol-refs[opt]
8218 objc-methodprotolist @end
8219 @interface identifier ( ) objc-protocol-refs[opt]
8220 objc-methodprotolist @end
8221 @implementation identifier ( identifier )
8222
8223 objc-superclass:
8224 : identifier
8225
8226 "@interface identifier (" must start "@interface identifier (
8227 identifier ) ...": objc-methodprotolist in the first production may
8228 not start with a parenthesized identifier as a declarator of a data
8229 definition with no declaration specifiers if the objc-superclass,
8230 objc-protocol-refs and objc-class-instance-variables are omitted. */
8231
8232 static void
8233 c_parser_objc_class_definition (c_parser *parser, tree attributes)
8234 {
8235 bool iface_p;
8236 tree id1;
8237 tree superclass;
8238 if (c_parser_next_token_is_keyword (parser, RID_AT_INTERFACE))
8239 iface_p = true;
8240 else if (c_parser_next_token_is_keyword (parser, RID_AT_IMPLEMENTATION))
8241 iface_p = false;
8242 else
8243 gcc_unreachable ();
8244
8245 c_parser_consume_token (parser);
8246 if (c_parser_next_token_is_not (parser, CPP_NAME))
8247 {
8248 c_parser_error (parser, "expected identifier");
8249 return;
8250 }
8251 id1 = c_parser_peek_token (parser)->value;
8252 c_parser_consume_token (parser);
8253 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
8254 {
8255 /* We have a category or class extension. */
8256 tree id2;
8257 tree proto = NULL_TREE;
8258 c_parser_consume_token (parser);
8259 if (c_parser_next_token_is_not (parser, CPP_NAME))
8260 {
8261 if (iface_p && c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
8262 {
8263 /* We have a class extension. */
8264 id2 = NULL_TREE;
8265 }
8266 else
8267 {
8268 c_parser_error (parser, "expected identifier or %<)%>");
8269 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
8270 return;
8271 }
8272 }
8273 else
8274 {
8275 id2 = c_parser_peek_token (parser)->value;
8276 c_parser_consume_token (parser);
8277 }
8278 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
8279 if (!iface_p)
8280 {
8281 objc_start_category_implementation (id1, id2);
8282 return;
8283 }
8284 if (c_parser_next_token_is (parser, CPP_LESS))
8285 proto = c_parser_objc_protocol_refs (parser);
8286 objc_start_category_interface (id1, id2, proto, attributes);
8287 c_parser_objc_methodprotolist (parser);
8288 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
8289 objc_finish_interface ();
8290 return;
8291 }
8292 if (c_parser_next_token_is (parser, CPP_COLON))
8293 {
8294 c_parser_consume_token (parser);
8295 if (c_parser_next_token_is_not (parser, CPP_NAME))
8296 {
8297 c_parser_error (parser, "expected identifier");
8298 return;
8299 }
8300 superclass = c_parser_peek_token (parser)->value;
8301 c_parser_consume_token (parser);
8302 }
8303 else
8304 superclass = NULL_TREE;
8305 if (iface_p)
8306 {
8307 tree proto = NULL_TREE;
8308 if (c_parser_next_token_is (parser, CPP_LESS))
8309 proto = c_parser_objc_protocol_refs (parser);
8310 objc_start_class_interface (id1, superclass, proto, attributes);
8311 }
8312 else
8313 objc_start_class_implementation (id1, superclass);
8314 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
8315 c_parser_objc_class_instance_variables (parser);
8316 if (iface_p)
8317 {
8318 objc_continue_interface ();
8319 c_parser_objc_methodprotolist (parser);
8320 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
8321 objc_finish_interface ();
8322 }
8323 else
8324 {
8325 objc_continue_implementation ();
8326 return;
8327 }
8328 }
8329
8330 /* Parse objc-class-instance-variables.
8331
8332 objc-class-instance-variables:
8333 { objc-instance-variable-decl-list[opt] }
8334
8335 objc-instance-variable-decl-list:
8336 objc-visibility-spec
8337 objc-instance-variable-decl ;
8338 ;
8339 objc-instance-variable-decl-list objc-visibility-spec
8340 objc-instance-variable-decl-list objc-instance-variable-decl ;
8341 objc-instance-variable-decl-list ;
8342
8343 objc-visibility-spec:
8344 @private
8345 @protected
8346 @public
8347
8348 objc-instance-variable-decl:
8349 struct-declaration
8350 */
8351
8352 static void
8353 c_parser_objc_class_instance_variables (c_parser *parser)
8354 {
8355 gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE));
8356 c_parser_consume_token (parser);
8357 while (c_parser_next_token_is_not (parser, CPP_EOF))
8358 {
8359 tree decls;
8360 /* Parse any stray semicolon. */
8361 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
8362 {
8363 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
8364 "extra semicolon");
8365 c_parser_consume_token (parser);
8366 continue;
8367 }
8368 /* Stop if at the end of the instance variables. */
8369 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
8370 {
8371 c_parser_consume_token (parser);
8372 break;
8373 }
8374 /* Parse any objc-visibility-spec. */
8375 if (c_parser_next_token_is_keyword (parser, RID_AT_PRIVATE))
8376 {
8377 c_parser_consume_token (parser);
8378 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
8379 continue;
8380 }
8381 else if (c_parser_next_token_is_keyword (parser, RID_AT_PROTECTED))
8382 {
8383 c_parser_consume_token (parser);
8384 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
8385 continue;
8386 }
8387 else if (c_parser_next_token_is_keyword (parser, RID_AT_PUBLIC))
8388 {
8389 c_parser_consume_token (parser);
8390 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
8391 continue;
8392 }
8393 else if (c_parser_next_token_is_keyword (parser, RID_AT_PACKAGE))
8394 {
8395 c_parser_consume_token (parser);
8396 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
8397 continue;
8398 }
8399 else if (c_parser_next_token_is (parser, CPP_PRAGMA))
8400 {
8401 c_parser_pragma (parser, pragma_external);
8402 continue;
8403 }
8404
8405 /* Parse some comma-separated declarations. */
8406 decls = c_parser_struct_declaration (parser);
8407 if (decls == NULL)
8408 {
8409 /* There is a syntax error. We want to skip the offending
8410 tokens up to the next ';' (included) or '}'
8411 (excluded). */
8412
8413 /* First, skip manually a ')' or ']'. This is because they
8414 reduce the nesting level, so c_parser_skip_until_found()
8415 wouldn't be able to skip past them. */
8416 c_token *token = c_parser_peek_token (parser);
8417 if (token->type == CPP_CLOSE_PAREN || token->type == CPP_CLOSE_SQUARE)
8418 c_parser_consume_token (parser);
8419
8420 /* Then, do the standard skipping. */
8421 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
8422
8423 /* We hopefully recovered. Start normal parsing again. */
8424 parser->error = false;
8425 continue;
8426 }
8427 else
8428 {
8429 /* Comma-separated instance variables are chained together
8430 in reverse order; add them one by one. */
8431 tree ivar = nreverse (decls);
8432 for (; ivar; ivar = DECL_CHAIN (ivar))
8433 objc_add_instance_variable (copy_node (ivar));
8434 }
8435 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
8436 }
8437 }
8438
8439 /* Parse an objc-class-declaration.
8440
8441 objc-class-declaration:
8442 @class identifier-list ;
8443 */
8444
8445 static void
8446 c_parser_objc_class_declaration (c_parser *parser)
8447 {
8448 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_CLASS));
8449 c_parser_consume_token (parser);
8450 /* Any identifiers, including those declared as type names, are OK
8451 here. */
8452 while (true)
8453 {
8454 tree id;
8455 if (c_parser_next_token_is_not (parser, CPP_NAME))
8456 {
8457 c_parser_error (parser, "expected identifier");
8458 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
8459 parser->error = false;
8460 return;
8461 }
8462 id = c_parser_peek_token (parser)->value;
8463 objc_declare_class (id);
8464 c_parser_consume_token (parser);
8465 if (c_parser_next_token_is (parser, CPP_COMMA))
8466 c_parser_consume_token (parser);
8467 else
8468 break;
8469 }
8470 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
8471 }
8472
8473 /* Parse an objc-alias-declaration.
8474
8475 objc-alias-declaration:
8476 @compatibility_alias identifier identifier ;
8477 */
8478
8479 static void
8480 c_parser_objc_alias_declaration (c_parser *parser)
8481 {
8482 tree id1, id2;
8483 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_ALIAS));
8484 c_parser_consume_token (parser);
8485 if (c_parser_next_token_is_not (parser, CPP_NAME))
8486 {
8487 c_parser_error (parser, "expected identifier");
8488 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
8489 return;
8490 }
8491 id1 = c_parser_peek_token (parser)->value;
8492 c_parser_consume_token (parser);
8493 if (c_parser_next_token_is_not (parser, CPP_NAME))
8494 {
8495 c_parser_error (parser, "expected identifier");
8496 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
8497 return;
8498 }
8499 id2 = c_parser_peek_token (parser)->value;
8500 c_parser_consume_token (parser);
8501 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
8502 objc_declare_alias (id1, id2);
8503 }
8504
8505 /* Parse an objc-protocol-definition.
8506
8507 objc-protocol-definition:
8508 @protocol identifier objc-protocol-refs[opt] objc-methodprotolist @end
8509 @protocol identifier-list ;
8510
8511 "@protocol identifier ;" should be resolved as "@protocol
8512 identifier-list ;": objc-methodprotolist may not start with a
8513 semicolon in the first alternative if objc-protocol-refs are
8514 omitted. */
8515
8516 static void
8517 c_parser_objc_protocol_definition (c_parser *parser, tree attributes)
8518 {
8519 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_PROTOCOL));
8520
8521 c_parser_consume_token (parser);
8522 if (c_parser_next_token_is_not (parser, CPP_NAME))
8523 {
8524 c_parser_error (parser, "expected identifier");
8525 return;
8526 }
8527 if (c_parser_peek_2nd_token (parser)->type == CPP_COMMA
8528 || c_parser_peek_2nd_token (parser)->type == CPP_SEMICOLON)
8529 {
8530 /* Any identifiers, including those declared as type names, are
8531 OK here. */
8532 while (true)
8533 {
8534 tree id;
8535 if (c_parser_next_token_is_not (parser, CPP_NAME))
8536 {
8537 c_parser_error (parser, "expected identifier");
8538 break;
8539 }
8540 id = c_parser_peek_token (parser)->value;
8541 objc_declare_protocol (id, attributes);
8542 c_parser_consume_token (parser);
8543 if (c_parser_next_token_is (parser, CPP_COMMA))
8544 c_parser_consume_token (parser);
8545 else
8546 break;
8547 }
8548 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
8549 }
8550 else
8551 {
8552 tree id = c_parser_peek_token (parser)->value;
8553 tree proto = NULL_TREE;
8554 c_parser_consume_token (parser);
8555 if (c_parser_next_token_is (parser, CPP_LESS))
8556 proto = c_parser_objc_protocol_refs (parser);
8557 parser->objc_pq_context = true;
8558 objc_start_protocol (id, proto, attributes);
8559 c_parser_objc_methodprotolist (parser);
8560 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
8561 parser->objc_pq_context = false;
8562 objc_finish_interface ();
8563 }
8564 }
8565
8566 /* Parse an objc-method-type.
8567
8568 objc-method-type:
8569 +
8570 -
8571
8572 Return true if it is a class method (+) and false if it is
8573 an instance method (-).
8574 */
8575 static inline bool
8576 c_parser_objc_method_type (c_parser *parser)
8577 {
8578 switch (c_parser_peek_token (parser)->type)
8579 {
8580 case CPP_PLUS:
8581 c_parser_consume_token (parser);
8582 return true;
8583 case CPP_MINUS:
8584 c_parser_consume_token (parser);
8585 return false;
8586 default:
8587 gcc_unreachable ();
8588 }
8589 }
8590
8591 /* Parse an objc-method-definition.
8592
8593 objc-method-definition:
8594 objc-method-type objc-method-decl ;[opt] compound-statement
8595 */
8596
8597 static void
8598 c_parser_objc_method_definition (c_parser *parser)
8599 {
8600 bool is_class_method = c_parser_objc_method_type (parser);
8601 tree decl, attributes = NULL_TREE, expr = NULL_TREE;
8602 parser->objc_pq_context = true;
8603 decl = c_parser_objc_method_decl (parser, is_class_method, &attributes,
8604 &expr);
8605 if (decl == error_mark_node)
8606 return; /* Bail here. */
8607
8608 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
8609 {
8610 c_parser_consume_token (parser);
8611 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
8612 "extra semicolon in method definition specified");
8613 }
8614
8615 if (!c_parser_next_token_is (parser, CPP_OPEN_BRACE))
8616 {
8617 c_parser_error (parser, "expected %<{%>");
8618 return;
8619 }
8620
8621 parser->objc_pq_context = false;
8622 if (objc_start_method_definition (is_class_method, decl, attributes, expr))
8623 {
8624 add_stmt (c_parser_compound_statement (parser));
8625 objc_finish_method_definition (current_function_decl);
8626 }
8627 else
8628 {
8629 /* This code is executed when we find a method definition
8630 outside of an @implementation context (or invalid for other
8631 reasons). Parse the method (to keep going) but do not emit
8632 any code.
8633 */
8634 c_parser_compound_statement (parser);
8635 }
8636 }
8637
8638 /* Parse an objc-methodprotolist.
8639
8640 objc-methodprotolist:
8641 empty
8642 objc-methodprotolist objc-methodproto
8643 objc-methodprotolist declaration
8644 objc-methodprotolist ;
8645 @optional
8646 @required
8647
8648 The declaration is a data definition, which may be missing
8649 declaration specifiers under the same rules and diagnostics as
8650 other data definitions outside functions, and the stray semicolon
8651 is diagnosed the same way as a stray semicolon outside a
8652 function. */
8653
8654 static void
8655 c_parser_objc_methodprotolist (c_parser *parser)
8656 {
8657 while (true)
8658 {
8659 /* The list is terminated by @end. */
8660 switch (c_parser_peek_token (parser)->type)
8661 {
8662 case CPP_SEMICOLON:
8663 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
8664 "ISO C does not allow extra %<;%> outside of a function");
8665 c_parser_consume_token (parser);
8666 break;
8667 case CPP_PLUS:
8668 case CPP_MINUS:
8669 c_parser_objc_methodproto (parser);
8670 break;
8671 case CPP_PRAGMA:
8672 c_parser_pragma (parser, pragma_external);
8673 break;
8674 case CPP_EOF:
8675 return;
8676 default:
8677 if (c_parser_next_token_is_keyword (parser, RID_AT_END))
8678 return;
8679 else if (c_parser_next_token_is_keyword (parser, RID_AT_PROPERTY))
8680 c_parser_objc_at_property_declaration (parser);
8681 else if (c_parser_next_token_is_keyword (parser, RID_AT_OPTIONAL))
8682 {
8683 objc_set_method_opt (true);
8684 c_parser_consume_token (parser);
8685 }
8686 else if (c_parser_next_token_is_keyword (parser, RID_AT_REQUIRED))
8687 {
8688 objc_set_method_opt (false);
8689 c_parser_consume_token (parser);
8690 }
8691 else
8692 c_parser_declaration_or_fndef (parser, false, false, true,
8693 false, true, NULL, vNULL);
8694 break;
8695 }
8696 }
8697 }
8698
8699 /* Parse an objc-methodproto.
8700
8701 objc-methodproto:
8702 objc-method-type objc-method-decl ;
8703 */
8704
8705 static void
8706 c_parser_objc_methodproto (c_parser *parser)
8707 {
8708 bool is_class_method = c_parser_objc_method_type (parser);
8709 tree decl, attributes = NULL_TREE;
8710
8711 /* Remember protocol qualifiers in prototypes. */
8712 parser->objc_pq_context = true;
8713 decl = c_parser_objc_method_decl (parser, is_class_method, &attributes,
8714 NULL);
8715 /* Forget protocol qualifiers now. */
8716 parser->objc_pq_context = false;
8717
8718 /* Do not allow the presence of attributes to hide an erroneous
8719 method implementation in the interface section. */
8720 if (!c_parser_next_token_is (parser, CPP_SEMICOLON))
8721 {
8722 c_parser_error (parser, "expected %<;%>");
8723 return;
8724 }
8725
8726 if (decl != error_mark_node)
8727 objc_add_method_declaration (is_class_method, decl, attributes);
8728
8729 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
8730 }
8731
8732 /* If we are at a position that method attributes may be present, check that
8733 there are not any parsed already (a syntax error) and then collect any
8734 specified at the current location. Finally, if new attributes were present,
8735 check that the next token is legal ( ';' for decls and '{' for defs). */
8736
8737 static bool
8738 c_parser_objc_maybe_method_attributes (c_parser* parser, tree* attributes)
8739 {
8740 bool bad = false;
8741 if (*attributes)
8742 {
8743 c_parser_error (parser,
8744 "method attributes must be specified at the end only");
8745 *attributes = NULL_TREE;
8746 bad = true;
8747 }
8748
8749 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
8750 *attributes = c_parser_attributes (parser);
8751
8752 /* If there were no attributes here, just report any earlier error. */
8753 if (*attributes == NULL_TREE || bad)
8754 return bad;
8755
8756 /* If the attributes are followed by a ; or {, then just report any earlier
8757 error. */
8758 if (c_parser_next_token_is (parser, CPP_SEMICOLON)
8759 || c_parser_next_token_is (parser, CPP_OPEN_BRACE))
8760 return bad;
8761
8762 /* We've got attributes, but not at the end. */
8763 c_parser_error (parser,
8764 "expected %<;%> or %<{%> after method attribute definition");
8765 return true;
8766 }
8767
8768 /* Parse an objc-method-decl.
8769
8770 objc-method-decl:
8771 ( objc-type-name ) objc-selector
8772 objc-selector
8773 ( objc-type-name ) objc-keyword-selector objc-optparmlist
8774 objc-keyword-selector objc-optparmlist
8775 attributes
8776
8777 objc-keyword-selector:
8778 objc-keyword-decl
8779 objc-keyword-selector objc-keyword-decl
8780
8781 objc-keyword-decl:
8782 objc-selector : ( objc-type-name ) identifier
8783 objc-selector : identifier
8784 : ( objc-type-name ) identifier
8785 : identifier
8786
8787 objc-optparmlist:
8788 objc-optparms objc-optellipsis
8789
8790 objc-optparms:
8791 empty
8792 objc-opt-parms , parameter-declaration
8793
8794 objc-optellipsis:
8795 empty
8796 , ...
8797 */
8798
8799 static tree
8800 c_parser_objc_method_decl (c_parser *parser, bool is_class_method,
8801 tree *attributes, tree *expr)
8802 {
8803 tree type = NULL_TREE;
8804 tree sel;
8805 tree parms = NULL_TREE;
8806 bool ellipsis = false;
8807 bool attr_err = false;
8808
8809 *attributes = NULL_TREE;
8810 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
8811 {
8812 c_parser_consume_token (parser);
8813 type = c_parser_objc_type_name (parser);
8814 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
8815 }
8816 sel = c_parser_objc_selector (parser);
8817 /* If there is no selector, or a colon follows, we have an
8818 objc-keyword-selector. If there is a selector, and a colon does
8819 not follow, that selector ends the objc-method-decl. */
8820 if (!sel || c_parser_next_token_is (parser, CPP_COLON))
8821 {
8822 tree tsel = sel;
8823 tree list = NULL_TREE;
8824 while (true)
8825 {
8826 tree atype = NULL_TREE, id, keyworddecl;
8827 tree param_attr = NULL_TREE;
8828 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
8829 break;
8830 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
8831 {
8832 c_parser_consume_token (parser);
8833 atype = c_parser_objc_type_name (parser);
8834 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
8835 "expected %<)%>");
8836 }
8837 /* New ObjC allows attributes on method parameters. */
8838 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
8839 param_attr = c_parser_attributes (parser);
8840 if (c_parser_next_token_is_not (parser, CPP_NAME))
8841 {
8842 c_parser_error (parser, "expected identifier");
8843 return error_mark_node;
8844 }
8845 id = c_parser_peek_token (parser)->value;
8846 c_parser_consume_token (parser);
8847 keyworddecl = objc_build_keyword_decl (tsel, atype, id, param_attr);
8848 list = chainon (list, keyworddecl);
8849 tsel = c_parser_objc_selector (parser);
8850 if (!tsel && c_parser_next_token_is_not (parser, CPP_COLON))
8851 break;
8852 }
8853
8854 attr_err |= c_parser_objc_maybe_method_attributes (parser, attributes) ;
8855
8856 /* Parse the optional parameter list. Optional Objective-C
8857 method parameters follow the C syntax, and may include '...'
8858 to denote a variable number of arguments. */
8859 parms = make_node (TREE_LIST);
8860 while (c_parser_next_token_is (parser, CPP_COMMA))
8861 {
8862 struct c_parm *parm;
8863 c_parser_consume_token (parser);
8864 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
8865 {
8866 ellipsis = true;
8867 c_parser_consume_token (parser);
8868 attr_err |= c_parser_objc_maybe_method_attributes
8869 (parser, attributes) ;
8870 break;
8871 }
8872 parm = c_parser_parameter_declaration (parser, NULL_TREE);
8873 if (parm == NULL)
8874 break;
8875 parms = chainon (parms,
8876 build_tree_list (NULL_TREE, grokparm (parm, expr)));
8877 }
8878 sel = list;
8879 }
8880 else
8881 attr_err |= c_parser_objc_maybe_method_attributes (parser, attributes) ;
8882
8883 if (sel == NULL)
8884 {
8885 c_parser_error (parser, "objective-c method declaration is expected");
8886 return error_mark_node;
8887 }
8888
8889 if (attr_err)
8890 return error_mark_node;
8891
8892 return objc_build_method_signature (is_class_method, type, sel, parms, ellipsis);
8893 }
8894
8895 /* Parse an objc-type-name.
8896
8897 objc-type-name:
8898 objc-type-qualifiers[opt] type-name
8899 objc-type-qualifiers[opt]
8900
8901 objc-type-qualifiers:
8902 objc-type-qualifier
8903 objc-type-qualifiers objc-type-qualifier
8904
8905 objc-type-qualifier: one of
8906 in out inout bycopy byref oneway
8907 */
8908
8909 static tree
8910 c_parser_objc_type_name (c_parser *parser)
8911 {
8912 tree quals = NULL_TREE;
8913 struct c_type_name *type_name = NULL;
8914 tree type = NULL_TREE;
8915 while (true)
8916 {
8917 c_token *token = c_parser_peek_token (parser);
8918 if (token->type == CPP_KEYWORD
8919 && (token->keyword == RID_IN
8920 || token->keyword == RID_OUT
8921 || token->keyword == RID_INOUT
8922 || token->keyword == RID_BYCOPY
8923 || token->keyword == RID_BYREF
8924 || token->keyword == RID_ONEWAY))
8925 {
8926 quals = chainon (build_tree_list (NULL_TREE, token->value), quals);
8927 c_parser_consume_token (parser);
8928 }
8929 else
8930 break;
8931 }
8932 if (c_parser_next_tokens_start_typename (parser, cla_prefer_type))
8933 type_name = c_parser_type_name (parser);
8934 if (type_name)
8935 type = groktypename (type_name, NULL, NULL);
8936
8937 /* If the type is unknown, and error has already been produced and
8938 we need to recover from the error. In that case, use NULL_TREE
8939 for the type, as if no type had been specified; this will use the
8940 default type ('id') which is good for error recovery. */
8941 if (type == error_mark_node)
8942 type = NULL_TREE;
8943
8944 return build_tree_list (quals, type);
8945 }
8946
8947 /* Parse objc-protocol-refs.
8948
8949 objc-protocol-refs:
8950 < identifier-list >
8951 */
8952
8953 static tree
8954 c_parser_objc_protocol_refs (c_parser *parser)
8955 {
8956 tree list = NULL_TREE;
8957 gcc_assert (c_parser_next_token_is (parser, CPP_LESS));
8958 c_parser_consume_token (parser);
8959 /* Any identifiers, including those declared as type names, are OK
8960 here. */
8961 while (true)
8962 {
8963 tree id;
8964 if (c_parser_next_token_is_not (parser, CPP_NAME))
8965 {
8966 c_parser_error (parser, "expected identifier");
8967 break;
8968 }
8969 id = c_parser_peek_token (parser)->value;
8970 list = chainon (list, build_tree_list (NULL_TREE, id));
8971 c_parser_consume_token (parser);
8972 if (c_parser_next_token_is (parser, CPP_COMMA))
8973 c_parser_consume_token (parser);
8974 else
8975 break;
8976 }
8977 c_parser_require (parser, CPP_GREATER, "expected %<>%>");
8978 return list;
8979 }
8980
8981 /* Parse an objc-try-catch-finally-statement.
8982
8983 objc-try-catch-finally-statement:
8984 @try compound-statement objc-catch-list[opt]
8985 @try compound-statement objc-catch-list[opt] @finally compound-statement
8986
8987 objc-catch-list:
8988 @catch ( objc-catch-parameter-declaration ) compound-statement
8989 objc-catch-list @catch ( objc-catch-parameter-declaration ) compound-statement
8990
8991 objc-catch-parameter-declaration:
8992 parameter-declaration
8993 '...'
8994
8995 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
8996
8997 PS: This function is identical to cp_parser_objc_try_catch_finally_statement
8998 for C++. Keep them in sync. */
8999
9000 static void
9001 c_parser_objc_try_catch_finally_statement (c_parser *parser)
9002 {
9003 location_t location;
9004 tree stmt;
9005
9006 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_TRY));
9007 c_parser_consume_token (parser);
9008 location = c_parser_peek_token (parser)->location;
9009 objc_maybe_warn_exceptions (location);
9010 stmt = c_parser_compound_statement (parser);
9011 objc_begin_try_stmt (location, stmt);
9012
9013 while (c_parser_next_token_is_keyword (parser, RID_AT_CATCH))
9014 {
9015 struct c_parm *parm;
9016 tree parameter_declaration = error_mark_node;
9017 bool seen_open_paren = false;
9018
9019 c_parser_consume_token (parser);
9020 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
9021 seen_open_paren = true;
9022 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
9023 {
9024 /* We have "@catch (...)" (where the '...' are literally
9025 what is in the code). Skip the '...'.
9026 parameter_declaration is set to NULL_TREE, and
9027 objc_being_catch_clauses() knows that that means
9028 '...'. */
9029 c_parser_consume_token (parser);
9030 parameter_declaration = NULL_TREE;
9031 }
9032 else
9033 {
9034 /* We have "@catch (NSException *exception)" or something
9035 like that. Parse the parameter declaration. */
9036 parm = c_parser_parameter_declaration (parser, NULL_TREE);
9037 if (parm == NULL)
9038 parameter_declaration = error_mark_node;
9039 else
9040 parameter_declaration = grokparm (parm, NULL);
9041 }
9042 if (seen_open_paren)
9043 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
9044 else
9045 {
9046 /* If there was no open parenthesis, we are recovering from
9047 an error, and we are trying to figure out what mistake
9048 the user has made. */
9049
9050 /* If there is an immediate closing parenthesis, the user
9051 probably forgot the opening one (ie, they typed "@catch
9052 NSException *e)". Parse the closing parenthesis and keep
9053 going. */
9054 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
9055 c_parser_consume_token (parser);
9056
9057 /* If these is no immediate closing parenthesis, the user
9058 probably doesn't know that parenthesis are required at
9059 all (ie, they typed "@catch NSException *e"). So, just
9060 forget about the closing parenthesis and keep going. */
9061 }
9062 objc_begin_catch_clause (parameter_declaration);
9063 if (c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
9064 c_parser_compound_statement_nostart (parser);
9065 objc_finish_catch_clause ();
9066 }
9067 if (c_parser_next_token_is_keyword (parser, RID_AT_FINALLY))
9068 {
9069 c_parser_consume_token (parser);
9070 location = c_parser_peek_token (parser)->location;
9071 stmt = c_parser_compound_statement (parser);
9072 objc_build_finally_clause (location, stmt);
9073 }
9074 objc_finish_try_stmt ();
9075 }
9076
9077 /* Parse an objc-synchronized-statement.
9078
9079 objc-synchronized-statement:
9080 @synchronized ( expression ) compound-statement
9081 */
9082
9083 static void
9084 c_parser_objc_synchronized_statement (c_parser *parser)
9085 {
9086 location_t loc;
9087 tree expr, stmt;
9088 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_SYNCHRONIZED));
9089 c_parser_consume_token (parser);
9090 loc = c_parser_peek_token (parser)->location;
9091 objc_maybe_warn_exceptions (loc);
9092 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
9093 {
9094 struct c_expr ce = c_parser_expression (parser);
9095 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
9096 expr = ce.value;
9097 expr = c_fully_fold (expr, false, NULL);
9098 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
9099 }
9100 else
9101 expr = error_mark_node;
9102 stmt = c_parser_compound_statement (parser);
9103 objc_build_synchronized (loc, expr, stmt);
9104 }
9105
9106 /* Parse an objc-selector; return NULL_TREE without an error if the
9107 next token is not an objc-selector.
9108
9109 objc-selector:
9110 identifier
9111 one of
9112 enum struct union if else while do for switch case default
9113 break continue return goto asm sizeof typeof __alignof
9114 unsigned long const short volatile signed restrict _Complex
9115 in out inout bycopy byref oneway int char float double void _Bool
9116 _Atomic
9117
9118 ??? Why this selection of keywords but not, for example, storage
9119 class specifiers? */
9120
9121 static tree
9122 c_parser_objc_selector (c_parser *parser)
9123 {
9124 c_token *token = c_parser_peek_token (parser);
9125 tree value = token->value;
9126 if (token->type == CPP_NAME)
9127 {
9128 c_parser_consume_token (parser);
9129 return value;
9130 }
9131 if (token->type != CPP_KEYWORD)
9132 return NULL_TREE;
9133 switch (token->keyword)
9134 {
9135 case RID_ENUM:
9136 case RID_STRUCT:
9137 case RID_UNION:
9138 case RID_IF:
9139 case RID_ELSE:
9140 case RID_WHILE:
9141 case RID_DO:
9142 case RID_FOR:
9143 case RID_SWITCH:
9144 case RID_CASE:
9145 case RID_DEFAULT:
9146 case RID_BREAK:
9147 case RID_CONTINUE:
9148 case RID_RETURN:
9149 case RID_GOTO:
9150 case RID_ASM:
9151 case RID_SIZEOF:
9152 case RID_TYPEOF:
9153 case RID_ALIGNOF:
9154 case RID_UNSIGNED:
9155 case RID_LONG:
9156 case RID_CONST:
9157 case RID_SHORT:
9158 case RID_VOLATILE:
9159 case RID_SIGNED:
9160 case RID_RESTRICT:
9161 case RID_COMPLEX:
9162 case RID_IN:
9163 case RID_OUT:
9164 case RID_INOUT:
9165 case RID_BYCOPY:
9166 case RID_BYREF:
9167 case RID_ONEWAY:
9168 case RID_INT:
9169 case RID_CHAR:
9170 case RID_FLOAT:
9171 case RID_DOUBLE:
9172 case RID_VOID:
9173 case RID_BOOL:
9174 case RID_ATOMIC:
9175 case RID_AUTO_TYPE:
9176 case RID_INT_N_0:
9177 case RID_INT_N_1:
9178 case RID_INT_N_2:
9179 case RID_INT_N_3:
9180 c_parser_consume_token (parser);
9181 return value;
9182 default:
9183 return NULL_TREE;
9184 }
9185 }
9186
9187 /* Parse an objc-selector-arg.
9188
9189 objc-selector-arg:
9190 objc-selector
9191 objc-keywordname-list
9192
9193 objc-keywordname-list:
9194 objc-keywordname
9195 objc-keywordname-list objc-keywordname
9196
9197 objc-keywordname:
9198 objc-selector :
9199 :
9200 */
9201
9202 static tree
9203 c_parser_objc_selector_arg (c_parser *parser)
9204 {
9205 tree sel = c_parser_objc_selector (parser);
9206 tree list = NULL_TREE;
9207 if (sel && c_parser_next_token_is_not (parser, CPP_COLON))
9208 return sel;
9209 while (true)
9210 {
9211 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
9212 return list;
9213 list = chainon (list, build_tree_list (sel, NULL_TREE));
9214 sel = c_parser_objc_selector (parser);
9215 if (!sel && c_parser_next_token_is_not (parser, CPP_COLON))
9216 break;
9217 }
9218 return list;
9219 }
9220
9221 /* Parse an objc-receiver.
9222
9223 objc-receiver:
9224 expression
9225 class-name
9226 type-name
9227 */
9228
9229 static tree
9230 c_parser_objc_receiver (c_parser *parser)
9231 {
9232 location_t loc = c_parser_peek_token (parser)->location;
9233
9234 if (c_parser_peek_token (parser)->type == CPP_NAME
9235 && (c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME
9236 || c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME))
9237 {
9238 tree id = c_parser_peek_token (parser)->value;
9239 c_parser_consume_token (parser);
9240 return objc_get_class_reference (id);
9241 }
9242 struct c_expr ce = c_parser_expression (parser);
9243 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
9244 return c_fully_fold (ce.value, false, NULL);
9245 }
9246
9247 /* Parse objc-message-args.
9248
9249 objc-message-args:
9250 objc-selector
9251 objc-keywordarg-list
9252
9253 objc-keywordarg-list:
9254 objc-keywordarg
9255 objc-keywordarg-list objc-keywordarg
9256
9257 objc-keywordarg:
9258 objc-selector : objc-keywordexpr
9259 : objc-keywordexpr
9260 */
9261
9262 static tree
9263 c_parser_objc_message_args (c_parser *parser)
9264 {
9265 tree sel = c_parser_objc_selector (parser);
9266 tree list = NULL_TREE;
9267 if (sel && c_parser_next_token_is_not (parser, CPP_COLON))
9268 return sel;
9269 while (true)
9270 {
9271 tree keywordexpr;
9272 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
9273 return error_mark_node;
9274 keywordexpr = c_parser_objc_keywordexpr (parser);
9275 list = chainon (list, build_tree_list (sel, keywordexpr));
9276 sel = c_parser_objc_selector (parser);
9277 if (!sel && c_parser_next_token_is_not (parser, CPP_COLON))
9278 break;
9279 }
9280 return list;
9281 }
9282
9283 /* Parse an objc-keywordexpr.
9284
9285 objc-keywordexpr:
9286 nonempty-expr-list
9287 */
9288
9289 static tree
9290 c_parser_objc_keywordexpr (c_parser *parser)
9291 {
9292 tree ret;
9293 vec<tree, va_gc> *expr_list = c_parser_expr_list (parser, true, true,
9294 NULL, NULL, NULL, NULL);
9295 if (vec_safe_length (expr_list) == 1)
9296 {
9297 /* Just return the expression, remove a level of
9298 indirection. */
9299 ret = (*expr_list)[0];
9300 }
9301 else
9302 {
9303 /* We have a comma expression, we will collapse later. */
9304 ret = build_tree_list_vec (expr_list);
9305 }
9306 release_tree_vector (expr_list);
9307 return ret;
9308 }
9309
9310 /* A check, needed in several places, that ObjC interface, implementation or
9311 method definitions are not prefixed by incorrect items. */
9312 static bool
9313 c_parser_objc_diagnose_bad_element_prefix (c_parser *parser,
9314 struct c_declspecs *specs)
9315 {
9316 if (!specs->declspecs_seen_p || specs->non_sc_seen_p
9317 || specs->typespec_kind != ctsk_none)
9318 {
9319 c_parser_error (parser,
9320 "no type or storage class may be specified here,");
9321 c_parser_skip_to_end_of_block_or_statement (parser);
9322 return true;
9323 }
9324 return false;
9325 }
9326
9327 /* Parse an Objective-C @property declaration. The syntax is:
9328
9329 objc-property-declaration:
9330 '@property' objc-property-attributes[opt] struct-declaration ;
9331
9332 objc-property-attributes:
9333 '(' objc-property-attribute-list ')'
9334
9335 objc-property-attribute-list:
9336 objc-property-attribute
9337 objc-property-attribute-list, objc-property-attribute
9338
9339 objc-property-attribute
9340 'getter' = identifier
9341 'setter' = identifier
9342 'readonly'
9343 'readwrite'
9344 'assign'
9345 'retain'
9346 'copy'
9347 'nonatomic'
9348
9349 For example:
9350 @property NSString *name;
9351 @property (readonly) id object;
9352 @property (retain, nonatomic, getter=getTheName) id name;
9353 @property int a, b, c;
9354
9355 PS: This function is identical to cp_parser_objc_at_propery_declaration
9356 for C++. Keep them in sync. */
9357 static void
9358 c_parser_objc_at_property_declaration (c_parser *parser)
9359 {
9360 /* The following variables hold the attributes of the properties as
9361 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
9362 seen. When we see an attribute, we set them to 'true' (if they
9363 are boolean properties) or to the identifier (if they have an
9364 argument, ie, for getter and setter). Note that here we only
9365 parse the list of attributes, check the syntax and accumulate the
9366 attributes that we find. objc_add_property_declaration() will
9367 then process the information. */
9368 bool property_assign = false;
9369 bool property_copy = false;
9370 tree property_getter_ident = NULL_TREE;
9371 bool property_nonatomic = false;
9372 bool property_readonly = false;
9373 bool property_readwrite = false;
9374 bool property_retain = false;
9375 tree property_setter_ident = NULL_TREE;
9376
9377 /* 'properties' is the list of properties that we read. Usually a
9378 single one, but maybe more (eg, in "@property int a, b, c;" there
9379 are three). */
9380 tree properties;
9381 location_t loc;
9382
9383 loc = c_parser_peek_token (parser)->location;
9384 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_PROPERTY));
9385
9386 c_parser_consume_token (parser); /* Eat '@property'. */
9387
9388 /* Parse the optional attribute list... */
9389 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
9390 {
9391 /* Eat the '(' */
9392 c_parser_consume_token (parser);
9393
9394 /* Property attribute keywords are valid now. */
9395 parser->objc_property_attr_context = true;
9396
9397 while (true)
9398 {
9399 bool syntax_error = false;
9400 c_token *token = c_parser_peek_token (parser);
9401 enum rid keyword;
9402
9403 if (token->type != CPP_KEYWORD)
9404 {
9405 if (token->type == CPP_CLOSE_PAREN)
9406 c_parser_error (parser, "expected identifier");
9407 else
9408 {
9409 c_parser_consume_token (parser);
9410 c_parser_error (parser, "unknown property attribute");
9411 }
9412 break;
9413 }
9414 keyword = token->keyword;
9415 c_parser_consume_token (parser);
9416 switch (keyword)
9417 {
9418 case RID_ASSIGN: property_assign = true; break;
9419 case RID_COPY: property_copy = true; break;
9420 case RID_NONATOMIC: property_nonatomic = true; break;
9421 case RID_READONLY: property_readonly = true; break;
9422 case RID_READWRITE: property_readwrite = true; break;
9423 case RID_RETAIN: property_retain = true; break;
9424
9425 case RID_GETTER:
9426 case RID_SETTER:
9427 if (c_parser_next_token_is_not (parser, CPP_EQ))
9428 {
9429 if (keyword == RID_GETTER)
9430 c_parser_error (parser,
9431 "missing %<=%> (after %<getter%> attribute)");
9432 else
9433 c_parser_error (parser,
9434 "missing %<=%> (after %<setter%> attribute)");
9435 syntax_error = true;
9436 break;
9437 }
9438 c_parser_consume_token (parser); /* eat the = */
9439 if (c_parser_next_token_is_not (parser, CPP_NAME))
9440 {
9441 c_parser_error (parser, "expected identifier");
9442 syntax_error = true;
9443 break;
9444 }
9445 if (keyword == RID_SETTER)
9446 {
9447 if (property_setter_ident != NULL_TREE)
9448 c_parser_error (parser, "the %<setter%> attribute may only be specified once");
9449 else
9450 property_setter_ident = c_parser_peek_token (parser)->value;
9451 c_parser_consume_token (parser);
9452 if (c_parser_next_token_is_not (parser, CPP_COLON))
9453 c_parser_error (parser, "setter name must terminate with %<:%>");
9454 else
9455 c_parser_consume_token (parser);
9456 }
9457 else
9458 {
9459 if (property_getter_ident != NULL_TREE)
9460 c_parser_error (parser, "the %<getter%> attribute may only be specified once");
9461 else
9462 property_getter_ident = c_parser_peek_token (parser)->value;
9463 c_parser_consume_token (parser);
9464 }
9465 break;
9466 default:
9467 c_parser_error (parser, "unknown property attribute");
9468 syntax_error = true;
9469 break;
9470 }
9471
9472 if (syntax_error)
9473 break;
9474
9475 if (c_parser_next_token_is (parser, CPP_COMMA))
9476 c_parser_consume_token (parser);
9477 else
9478 break;
9479 }
9480 parser->objc_property_attr_context = false;
9481 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
9482 }
9483 /* ... and the property declaration(s). */
9484 properties = c_parser_struct_declaration (parser);
9485
9486 if (properties == error_mark_node)
9487 {
9488 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
9489 parser->error = false;
9490 return;
9491 }
9492
9493 if (properties == NULL_TREE)
9494 c_parser_error (parser, "expected identifier");
9495 else
9496 {
9497 /* Comma-separated properties are chained together in
9498 reverse order; add them one by one. */
9499 properties = nreverse (properties);
9500
9501 for (; properties; properties = TREE_CHAIN (properties))
9502 objc_add_property_declaration (loc, copy_node (properties),
9503 property_readonly, property_readwrite,
9504 property_assign, property_retain,
9505 property_copy, property_nonatomic,
9506 property_getter_ident, property_setter_ident);
9507 }
9508
9509 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9510 parser->error = false;
9511 }
9512
9513 /* Parse an Objective-C @synthesize declaration. The syntax is:
9514
9515 objc-synthesize-declaration:
9516 @synthesize objc-synthesize-identifier-list ;
9517
9518 objc-synthesize-identifier-list:
9519 objc-synthesize-identifier
9520 objc-synthesize-identifier-list, objc-synthesize-identifier
9521
9522 objc-synthesize-identifier
9523 identifier
9524 identifier = identifier
9525
9526 For example:
9527 @synthesize MyProperty;
9528 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
9529
9530 PS: This function is identical to cp_parser_objc_at_synthesize_declaration
9531 for C++. Keep them in sync.
9532 */
9533 static void
9534 c_parser_objc_at_synthesize_declaration (c_parser *parser)
9535 {
9536 tree list = NULL_TREE;
9537 location_t loc;
9538 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_SYNTHESIZE));
9539 loc = c_parser_peek_token (parser)->location;
9540
9541 c_parser_consume_token (parser);
9542 while (true)
9543 {
9544 tree property, ivar;
9545 if (c_parser_next_token_is_not (parser, CPP_NAME))
9546 {
9547 c_parser_error (parser, "expected identifier");
9548 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
9549 /* Once we find the semicolon, we can resume normal parsing.
9550 We have to reset parser->error manually because
9551 c_parser_skip_until_found() won't reset it for us if the
9552 next token is precisely a semicolon. */
9553 parser->error = false;
9554 return;
9555 }
9556 property = c_parser_peek_token (parser)->value;
9557 c_parser_consume_token (parser);
9558 if (c_parser_next_token_is (parser, CPP_EQ))
9559 {
9560 c_parser_consume_token (parser);
9561 if (c_parser_next_token_is_not (parser, CPP_NAME))
9562 {
9563 c_parser_error (parser, "expected identifier");
9564 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
9565 parser->error = false;
9566 return;
9567 }
9568 ivar = c_parser_peek_token (parser)->value;
9569 c_parser_consume_token (parser);
9570 }
9571 else
9572 ivar = NULL_TREE;
9573 list = chainon (list, build_tree_list (ivar, property));
9574 if (c_parser_next_token_is (parser, CPP_COMMA))
9575 c_parser_consume_token (parser);
9576 else
9577 break;
9578 }
9579 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9580 objc_add_synthesize_declaration (loc, list);
9581 }
9582
9583 /* Parse an Objective-C @dynamic declaration. The syntax is:
9584
9585 objc-dynamic-declaration:
9586 @dynamic identifier-list ;
9587
9588 For example:
9589 @dynamic MyProperty;
9590 @dynamic MyProperty, AnotherProperty;
9591
9592 PS: This function is identical to cp_parser_objc_at_dynamic_declaration
9593 for C++. Keep them in sync.
9594 */
9595 static void
9596 c_parser_objc_at_dynamic_declaration (c_parser *parser)
9597 {
9598 tree list = NULL_TREE;
9599 location_t loc;
9600 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_DYNAMIC));
9601 loc = c_parser_peek_token (parser)->location;
9602
9603 c_parser_consume_token (parser);
9604 while (true)
9605 {
9606 tree property;
9607 if (c_parser_next_token_is_not (parser, CPP_NAME))
9608 {
9609 c_parser_error (parser, "expected identifier");
9610 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
9611 parser->error = false;
9612 return;
9613 }
9614 property = c_parser_peek_token (parser)->value;
9615 list = chainon (list, build_tree_list (NULL_TREE, property));
9616 c_parser_consume_token (parser);
9617 if (c_parser_next_token_is (parser, CPP_COMMA))
9618 c_parser_consume_token (parser);
9619 else
9620 break;
9621 }
9622 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9623 objc_add_dynamic_declaration (loc, list);
9624 }
9625
9626 \f
9627 /* Handle pragmas. Some OpenMP pragmas are associated with, and therefore
9628 should be considered, statements. ALLOW_STMT is true if we're within
9629 the context of a function and such pragmas are to be allowed. Returns
9630 true if we actually parsed such a pragma. */
9631
9632 static bool
9633 c_parser_pragma (c_parser *parser, enum pragma_context context)
9634 {
9635 unsigned int id;
9636
9637 id = c_parser_peek_token (parser)->pragma_kind;
9638 gcc_assert (id != PRAGMA_NONE);
9639
9640 switch (id)
9641 {
9642 case PRAGMA_OACC_ENTER_DATA:
9643 c_parser_oacc_enter_exit_data (parser, true);
9644 return false;
9645
9646 case PRAGMA_OACC_EXIT_DATA:
9647 c_parser_oacc_enter_exit_data (parser, false);
9648 return false;
9649
9650 case PRAGMA_OACC_UPDATE:
9651 if (context != pragma_compound)
9652 {
9653 if (context == pragma_stmt)
9654 c_parser_error (parser, "%<#pragma acc update%> may only be "
9655 "used in compound statements");
9656 goto bad_stmt;
9657 }
9658 c_parser_oacc_update (parser);
9659 return false;
9660
9661 case PRAGMA_OMP_BARRIER:
9662 if (context != pragma_compound)
9663 {
9664 if (context == pragma_stmt)
9665 c_parser_error (parser, "%<#pragma omp barrier%> may only be "
9666 "used in compound statements");
9667 goto bad_stmt;
9668 }
9669 c_parser_omp_barrier (parser);
9670 return false;
9671
9672 case PRAGMA_OMP_FLUSH:
9673 if (context != pragma_compound)
9674 {
9675 if (context == pragma_stmt)
9676 c_parser_error (parser, "%<#pragma omp flush%> may only be "
9677 "used in compound statements");
9678 goto bad_stmt;
9679 }
9680 c_parser_omp_flush (parser);
9681 return false;
9682
9683 case PRAGMA_OMP_TASKWAIT:
9684 if (context != pragma_compound)
9685 {
9686 if (context == pragma_stmt)
9687 c_parser_error (parser, "%<#pragma omp taskwait%> may only be "
9688 "used in compound statements");
9689 goto bad_stmt;
9690 }
9691 c_parser_omp_taskwait (parser);
9692 return false;
9693
9694 case PRAGMA_OMP_TASKYIELD:
9695 if (context != pragma_compound)
9696 {
9697 if (context == pragma_stmt)
9698 c_parser_error (parser, "%<#pragma omp taskyield%> may only be "
9699 "used in compound statements");
9700 goto bad_stmt;
9701 }
9702 c_parser_omp_taskyield (parser);
9703 return false;
9704
9705 case PRAGMA_OMP_CANCEL:
9706 if (context != pragma_compound)
9707 {
9708 if (context == pragma_stmt)
9709 c_parser_error (parser, "%<#pragma omp cancel%> may only be "
9710 "used in compound statements");
9711 goto bad_stmt;
9712 }
9713 c_parser_omp_cancel (parser);
9714 return false;
9715
9716 case PRAGMA_OMP_CANCELLATION_POINT:
9717 if (context != pragma_compound)
9718 {
9719 if (context == pragma_stmt)
9720 c_parser_error (parser, "%<#pragma omp cancellation point%> may "
9721 "only be used in compound statements");
9722 goto bad_stmt;
9723 }
9724 c_parser_omp_cancellation_point (parser);
9725 return false;
9726
9727 case PRAGMA_OMP_THREADPRIVATE:
9728 c_parser_omp_threadprivate (parser);
9729 return false;
9730
9731 case PRAGMA_OMP_TARGET:
9732 return c_parser_omp_target (parser, context);
9733
9734 case PRAGMA_OMP_END_DECLARE_TARGET:
9735 c_parser_omp_end_declare_target (parser);
9736 return false;
9737
9738 case PRAGMA_OMP_SECTION:
9739 error_at (c_parser_peek_token (parser)->location,
9740 "%<#pragma omp section%> may only be used in "
9741 "%<#pragma omp sections%> construct");
9742 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
9743 return false;
9744
9745 case PRAGMA_OMP_DECLARE_REDUCTION:
9746 c_parser_omp_declare (parser, context);
9747 return false;
9748 case PRAGMA_IVDEP:
9749 c_parser_consume_pragma (parser);
9750 c_parser_skip_to_pragma_eol (parser);
9751 if (!c_parser_next_token_is_keyword (parser, RID_FOR)
9752 && !c_parser_next_token_is_keyword (parser, RID_WHILE)
9753 && !c_parser_next_token_is_keyword (parser, RID_DO))
9754 {
9755 c_parser_error (parser, "for, while or do statement expected");
9756 return false;
9757 }
9758 if (c_parser_next_token_is_keyword (parser, RID_FOR))
9759 c_parser_for_statement (parser, true);
9760 else if (c_parser_next_token_is_keyword (parser, RID_WHILE))
9761 c_parser_while_statement (parser, true);
9762 else
9763 c_parser_do_statement (parser, true);
9764 return false;
9765
9766 case PRAGMA_GCC_PCH_PREPROCESS:
9767 c_parser_error (parser, "%<#pragma GCC pch_preprocess%> must be first");
9768 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
9769 return false;
9770
9771 case PRAGMA_CILK_SIMD:
9772 if (!c_parser_cilk_verify_simd (parser, context))
9773 return false;
9774 c_parser_consume_pragma (parser);
9775 c_parser_cilk_simd (parser);
9776 return false;
9777 case PRAGMA_CILK_GRAINSIZE:
9778 if (!flag_cilkplus)
9779 {
9780 warning (0, "%<#pragma grainsize%> ignored because -fcilkplus is not"
9781 " enabled");
9782 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
9783 return false;
9784 }
9785 if (context == pragma_external)
9786 {
9787 error_at (c_parser_peek_token (parser)->location,
9788 "%<#pragma grainsize%> must be inside a function");
9789 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
9790 return false;
9791 }
9792 c_parser_cilk_grainsize (parser);
9793 return false;
9794
9795 default:
9796 if (id < PRAGMA_FIRST_EXTERNAL)
9797 {
9798 if (context != pragma_stmt && context != pragma_compound)
9799 {
9800 bad_stmt:
9801 c_parser_error (parser, "expected declaration specifiers");
9802 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
9803 return false;
9804 }
9805 c_parser_omp_construct (parser);
9806 return true;
9807 }
9808 break;
9809 }
9810
9811 c_parser_consume_pragma (parser);
9812 c_invoke_pragma_handler (id);
9813
9814 /* Skip to EOL, but suppress any error message. Those will have been
9815 generated by the handler routine through calling error, as opposed
9816 to calling c_parser_error. */
9817 parser->error = true;
9818 c_parser_skip_to_pragma_eol (parser);
9819
9820 return false;
9821 }
9822
9823 /* The interface the pragma parsers have to the lexer. */
9824
9825 enum cpp_ttype
9826 pragma_lex (tree *value)
9827 {
9828 c_token *tok = c_parser_peek_token (the_parser);
9829 enum cpp_ttype ret = tok->type;
9830
9831 *value = tok->value;
9832 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
9833 ret = CPP_EOF;
9834 else
9835 {
9836 if (ret == CPP_KEYWORD)
9837 ret = CPP_NAME;
9838 c_parser_consume_token (the_parser);
9839 }
9840
9841 return ret;
9842 }
9843
9844 static void
9845 c_parser_pragma_pch_preprocess (c_parser *parser)
9846 {
9847 tree name = NULL;
9848
9849 c_parser_consume_pragma (parser);
9850 if (c_parser_next_token_is (parser, CPP_STRING))
9851 {
9852 name = c_parser_peek_token (parser)->value;
9853 c_parser_consume_token (parser);
9854 }
9855 else
9856 c_parser_error (parser, "expected string literal");
9857 c_parser_skip_to_pragma_eol (parser);
9858
9859 if (name)
9860 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
9861 }
9862 \f
9863 /* OpenACC and OpenMP parsing routines. */
9864
9865 /* Returns name of the next clause.
9866 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
9867 the token is not consumed. Otherwise appropriate pragma_omp_clause is
9868 returned and the token is consumed. */
9869
9870 static pragma_omp_clause
9871 c_parser_omp_clause_name (c_parser *parser)
9872 {
9873 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
9874
9875 if (c_parser_next_token_is_keyword (parser, RID_AUTO))
9876 result = PRAGMA_OACC_CLAUSE_AUTO;
9877 else if (c_parser_next_token_is_keyword (parser, RID_IF))
9878 result = PRAGMA_OMP_CLAUSE_IF;
9879 else if (c_parser_next_token_is_keyword (parser, RID_DEFAULT))
9880 result = PRAGMA_OMP_CLAUSE_DEFAULT;
9881 else if (c_parser_next_token_is_keyword (parser, RID_FOR))
9882 result = PRAGMA_OMP_CLAUSE_FOR;
9883 else if (c_parser_next_token_is (parser, CPP_NAME))
9884 {
9885 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
9886
9887 switch (p[0])
9888 {
9889 case 'a':
9890 if (!strcmp ("aligned", p))
9891 result = PRAGMA_OMP_CLAUSE_ALIGNED;
9892 else if (!strcmp ("async", p))
9893 result = PRAGMA_OACC_CLAUSE_ASYNC;
9894 break;
9895 case 'c':
9896 if (!strcmp ("collapse", p))
9897 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
9898 else if (!strcmp ("copy", p))
9899 result = PRAGMA_OACC_CLAUSE_COPY;
9900 else if (!strcmp ("copyin", p))
9901 result = PRAGMA_OMP_CLAUSE_COPYIN;
9902 else if (!strcmp ("copyout", p))
9903 result = PRAGMA_OACC_CLAUSE_COPYOUT;
9904 else if (!strcmp ("copyprivate", p))
9905 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
9906 else if (!strcmp ("create", p))
9907 result = PRAGMA_OACC_CLAUSE_CREATE;
9908 break;
9909 case 'd':
9910 if (!strcmp ("delete", p))
9911 result = PRAGMA_OACC_CLAUSE_DELETE;
9912 else if (!strcmp ("depend", p))
9913 result = PRAGMA_OMP_CLAUSE_DEPEND;
9914 else if (!strcmp ("device", p))
9915 result = PRAGMA_OMP_CLAUSE_DEVICE;
9916 else if (!strcmp ("deviceptr", p))
9917 result = PRAGMA_OACC_CLAUSE_DEVICEPTR;
9918 else if (!strcmp ("dist_schedule", p))
9919 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
9920 break;
9921 case 'f':
9922 if (!strcmp ("final", p))
9923 result = PRAGMA_OMP_CLAUSE_FINAL;
9924 else if (!strcmp ("firstprivate", p))
9925 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
9926 else if (!strcmp ("from", p))
9927 result = PRAGMA_OMP_CLAUSE_FROM;
9928 break;
9929 case 'g':
9930 if (!strcmp ("gang", p))
9931 result = PRAGMA_OACC_CLAUSE_GANG;
9932 break;
9933 case 'h':
9934 if (!strcmp ("host", p))
9935 result = PRAGMA_OACC_CLAUSE_HOST;
9936 break;
9937 case 'i':
9938 if (!strcmp ("inbranch", p))
9939 result = PRAGMA_OMP_CLAUSE_INBRANCH;
9940 break;
9941 case 'l':
9942 if (!strcmp ("lastprivate", p))
9943 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
9944 else if (!strcmp ("linear", p))
9945 result = PRAGMA_OMP_CLAUSE_LINEAR;
9946 break;
9947 case 'm':
9948 if (!strcmp ("map", p))
9949 result = PRAGMA_OMP_CLAUSE_MAP;
9950 else if (!strcmp ("mergeable", p))
9951 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
9952 else if (flag_cilkplus && !strcmp ("mask", p))
9953 result = PRAGMA_CILK_CLAUSE_MASK;
9954 break;
9955 case 'n':
9956 if (!strcmp ("notinbranch", p))
9957 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
9958 else if (!strcmp ("nowait", p))
9959 result = PRAGMA_OMP_CLAUSE_NOWAIT;
9960 else if (!strcmp ("num_gangs", p))
9961 result = PRAGMA_OACC_CLAUSE_NUM_GANGS;
9962 else if (!strcmp ("num_teams", p))
9963 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
9964 else if (!strcmp ("num_threads", p))
9965 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
9966 else if (!strcmp ("num_workers", p))
9967 result = PRAGMA_OACC_CLAUSE_NUM_WORKERS;
9968 else if (flag_cilkplus && !strcmp ("nomask", p))
9969 result = PRAGMA_CILK_CLAUSE_NOMASK;
9970 break;
9971 case 'o':
9972 if (!strcmp ("ordered", p))
9973 result = PRAGMA_OMP_CLAUSE_ORDERED;
9974 break;
9975 case 'p':
9976 if (!strcmp ("parallel", p))
9977 result = PRAGMA_OMP_CLAUSE_PARALLEL;
9978 else if (!strcmp ("present", p))
9979 result = PRAGMA_OACC_CLAUSE_PRESENT;
9980 else if (!strcmp ("present_or_copy", p)
9981 || !strcmp ("pcopy", p))
9982 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY;
9983 else if (!strcmp ("present_or_copyin", p)
9984 || !strcmp ("pcopyin", p))
9985 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN;
9986 else if (!strcmp ("present_or_copyout", p)
9987 || !strcmp ("pcopyout", p))
9988 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT;
9989 else if (!strcmp ("present_or_create", p)
9990 || !strcmp ("pcreate", p))
9991 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE;
9992 else if (!strcmp ("private", p))
9993 result = PRAGMA_OMP_CLAUSE_PRIVATE;
9994 else if (!strcmp ("proc_bind", p))
9995 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
9996 break;
9997 case 'r':
9998 if (!strcmp ("reduction", p))
9999 result = PRAGMA_OMP_CLAUSE_REDUCTION;
10000 break;
10001 case 's':
10002 if (!strcmp ("safelen", p))
10003 result = PRAGMA_OMP_CLAUSE_SAFELEN;
10004 else if (!strcmp ("schedule", p))
10005 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
10006 else if (!strcmp ("sections", p))
10007 result = PRAGMA_OMP_CLAUSE_SECTIONS;
10008 else if (!strcmp ("seq", p))
10009 result = PRAGMA_OACC_CLAUSE_SEQ;
10010 else if (!strcmp ("shared", p))
10011 result = PRAGMA_OMP_CLAUSE_SHARED;
10012 else if (!strcmp ("simdlen", p))
10013 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
10014 else if (!strcmp ("self", p))
10015 result = PRAGMA_OACC_CLAUSE_SELF;
10016 break;
10017 case 't':
10018 if (!strcmp ("taskgroup", p))
10019 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
10020 else if (!strcmp ("thread_limit", p))
10021 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
10022 else if (!strcmp ("to", p))
10023 result = PRAGMA_OMP_CLAUSE_TO;
10024 break;
10025 case 'u':
10026 if (!strcmp ("uniform", p))
10027 result = PRAGMA_OMP_CLAUSE_UNIFORM;
10028 else if (!strcmp ("untied", p))
10029 result = PRAGMA_OMP_CLAUSE_UNTIED;
10030 break;
10031 case 'v':
10032 if (!strcmp ("vector", p))
10033 result = PRAGMA_OACC_CLAUSE_VECTOR;
10034 else if (!strcmp ("vector_length", p))
10035 result = PRAGMA_OACC_CLAUSE_VECTOR_LENGTH;
10036 else if (flag_cilkplus && !strcmp ("vectorlength", p))
10037 result = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
10038 break;
10039 case 'w':
10040 if (!strcmp ("wait", p))
10041 result = PRAGMA_OACC_CLAUSE_WAIT;
10042 else if (!strcmp ("worker", p))
10043 result = PRAGMA_OACC_CLAUSE_WORKER;
10044 break;
10045 }
10046 }
10047
10048 if (result != PRAGMA_OMP_CLAUSE_NONE)
10049 c_parser_consume_token (parser);
10050
10051 return result;
10052 }
10053
10054 /* Validate that a clause of the given type does not already exist. */
10055
10056 static void
10057 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
10058 const char *name)
10059 {
10060 tree c;
10061
10062 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
10063 if (OMP_CLAUSE_CODE (c) == code)
10064 {
10065 location_t loc = OMP_CLAUSE_LOCATION (c);
10066 error_at (loc, "too many %qs clauses", name);
10067 break;
10068 }
10069 }
10070
10071 /* OpenACC 2.0
10072 Parse wait clause or wait directive parameters. */
10073
10074 static tree
10075 c_parser_oacc_wait_list (c_parser *parser, location_t clause_loc, tree list)
10076 {
10077 vec<tree, va_gc> *args;
10078 tree t, args_tree;
10079
10080 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10081 return list;
10082
10083 args = c_parser_expr_list (parser, false, true, NULL, NULL, NULL, NULL);
10084
10085 if (args->length () == 0)
10086 {
10087 c_parser_error (parser, "expected integer expression before ')'");
10088 release_tree_vector (args);
10089 return list;
10090 }
10091
10092 args_tree = build_tree_list_vec (args);
10093
10094 for (t = args_tree; t; t = TREE_CHAIN (t))
10095 {
10096 tree targ = TREE_VALUE (t);
10097
10098 if (targ != error_mark_node)
10099 {
10100 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ)))
10101 {
10102 c_parser_error (parser, "expression must be integral");
10103 targ = error_mark_node;
10104 }
10105 else
10106 {
10107 tree c = build_omp_clause (clause_loc, OMP_CLAUSE_WAIT);
10108
10109 OMP_CLAUSE_DECL (c) = targ;
10110 OMP_CLAUSE_CHAIN (c) = list;
10111 list = c;
10112 }
10113 }
10114 }
10115
10116 release_tree_vector (args);
10117 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10118 return list;
10119 }
10120
10121 /* OpenACC 2.0, OpenMP 2.5:
10122 variable-list:
10123 identifier
10124 variable-list , identifier
10125
10126 If KIND is nonzero, create the appropriate node and install the
10127 decl in OMP_CLAUSE_DECL and add the node to the head of the list.
10128 If KIND is nonzero, CLAUSE_LOC is the location of the clause.
10129
10130 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
10131 return the list created. */
10132
10133 static tree
10134 c_parser_omp_variable_list (c_parser *parser,
10135 location_t clause_loc,
10136 enum omp_clause_code kind, tree list)
10137 {
10138 if (c_parser_next_token_is_not (parser, CPP_NAME)
10139 || c_parser_peek_token (parser)->id_kind != C_ID_ID)
10140 c_parser_error (parser, "expected identifier");
10141
10142 while (c_parser_next_token_is (parser, CPP_NAME)
10143 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
10144 {
10145 tree t = lookup_name (c_parser_peek_token (parser)->value);
10146
10147 if (t == NULL_TREE)
10148 {
10149 undeclared_variable (c_parser_peek_token (parser)->location,
10150 c_parser_peek_token (parser)->value);
10151 t = error_mark_node;
10152 }
10153
10154 c_parser_consume_token (parser);
10155
10156 if (t == error_mark_node)
10157 ;
10158 else if (kind != 0)
10159 {
10160 switch (kind)
10161 {
10162 case OMP_CLAUSE__CACHE_:
10163 if (c_parser_peek_token (parser)->type != CPP_OPEN_SQUARE)
10164 {
10165 c_parser_error (parser, "expected %<[%>");
10166 t = error_mark_node;
10167 break;
10168 }
10169 /* FALL THROUGH. */
10170 case OMP_CLAUSE_MAP:
10171 case OMP_CLAUSE_FROM:
10172 case OMP_CLAUSE_TO:
10173 case OMP_CLAUSE_DEPEND:
10174 while (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
10175 {
10176 tree low_bound = NULL_TREE, length = NULL_TREE;
10177
10178 c_parser_consume_token (parser);
10179 if (!c_parser_next_token_is (parser, CPP_COLON))
10180 {
10181 low_bound = c_parser_expression (parser).value;
10182 mark_exp_read (low_bound);
10183 }
10184 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
10185 length = integer_one_node;
10186 else
10187 {
10188 /* Look for `:'. */
10189 if (!c_parser_require (parser, CPP_COLON,
10190 "expected %<:%>"))
10191 {
10192 t = error_mark_node;
10193 break;
10194 }
10195 if (!c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
10196 {
10197 length = c_parser_expression (parser).value;
10198 mark_exp_read (length);
10199 }
10200 }
10201 /* Look for the closing `]'. */
10202 if (!c_parser_require (parser, CPP_CLOSE_SQUARE,
10203 "expected %<]%>"))
10204 {
10205 t = error_mark_node;
10206 break;
10207 }
10208
10209 if (kind == OMP_CLAUSE__CACHE_)
10210 {
10211 if (TREE_CODE (low_bound) != INTEGER_CST
10212 && !TREE_READONLY (low_bound))
10213 {
10214 error_at (clause_loc,
10215 "%qD is not a constant", low_bound);
10216 t = error_mark_node;
10217 }
10218
10219 if (TREE_CODE (length) != INTEGER_CST
10220 && !TREE_READONLY (length))
10221 {
10222 error_at (clause_loc,
10223 "%qD is not a constant", length);
10224 t = error_mark_node;
10225 }
10226 }
10227
10228 t = tree_cons (low_bound, length, t);
10229 }
10230 break;
10231 default:
10232 break;
10233 }
10234
10235 if (t != error_mark_node)
10236 {
10237 tree u = build_omp_clause (clause_loc, kind);
10238 OMP_CLAUSE_DECL (u) = t;
10239 OMP_CLAUSE_CHAIN (u) = list;
10240 list = u;
10241 }
10242 }
10243 else
10244 list = tree_cons (t, NULL_TREE, list);
10245
10246 if (c_parser_next_token_is_not (parser, CPP_COMMA))
10247 break;
10248
10249 c_parser_consume_token (parser);
10250 }
10251
10252 return list;
10253 }
10254
10255 /* Similarly, but expect leading and trailing parenthesis. This is a very
10256 common case for OpenACC and OpenMP clauses. */
10257
10258 static tree
10259 c_parser_omp_var_list_parens (c_parser *parser, enum omp_clause_code kind,
10260 tree list)
10261 {
10262 /* The clauses location. */
10263 location_t loc = c_parser_peek_token (parser)->location;
10264
10265 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10266 {
10267 list = c_parser_omp_variable_list (parser, loc, kind, list);
10268 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10269 }
10270 return list;
10271 }
10272
10273 /* OpenACC 2.0:
10274 copy ( variable-list )
10275 copyin ( variable-list )
10276 copyout ( variable-list )
10277 create ( variable-list )
10278 delete ( variable-list )
10279 present ( variable-list )
10280 present_or_copy ( variable-list )
10281 pcopy ( variable-list )
10282 present_or_copyin ( variable-list )
10283 pcopyin ( variable-list )
10284 present_or_copyout ( variable-list )
10285 pcopyout ( variable-list )
10286 present_or_create ( variable-list )
10287 pcreate ( variable-list ) */
10288
10289 static tree
10290 c_parser_oacc_data_clause (c_parser *parser, pragma_omp_clause c_kind,
10291 tree list)
10292 {
10293 enum gomp_map_kind kind;
10294 switch (c_kind)
10295 {
10296 case PRAGMA_OACC_CLAUSE_COPY:
10297 kind = GOMP_MAP_FORCE_TOFROM;
10298 break;
10299 case PRAGMA_OACC_CLAUSE_COPYIN:
10300 kind = GOMP_MAP_FORCE_TO;
10301 break;
10302 case PRAGMA_OACC_CLAUSE_COPYOUT:
10303 kind = GOMP_MAP_FORCE_FROM;
10304 break;
10305 case PRAGMA_OACC_CLAUSE_CREATE:
10306 kind = GOMP_MAP_FORCE_ALLOC;
10307 break;
10308 case PRAGMA_OACC_CLAUSE_DELETE:
10309 kind = GOMP_MAP_FORCE_DEALLOC;
10310 break;
10311 case PRAGMA_OACC_CLAUSE_DEVICE:
10312 kind = GOMP_MAP_FORCE_TO;
10313 break;
10314 case PRAGMA_OACC_CLAUSE_HOST:
10315 case PRAGMA_OACC_CLAUSE_SELF:
10316 kind = GOMP_MAP_FORCE_FROM;
10317 break;
10318 case PRAGMA_OACC_CLAUSE_PRESENT:
10319 kind = GOMP_MAP_FORCE_PRESENT;
10320 break;
10321 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
10322 kind = GOMP_MAP_TOFROM;
10323 break;
10324 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
10325 kind = GOMP_MAP_TO;
10326 break;
10327 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
10328 kind = GOMP_MAP_FROM;
10329 break;
10330 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
10331 kind = GOMP_MAP_ALLOC;
10332 break;
10333 default:
10334 gcc_unreachable ();
10335 }
10336 tree nl, c;
10337 nl = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_MAP, list);
10338
10339 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
10340 OMP_CLAUSE_SET_MAP_KIND (c, kind);
10341
10342 return nl;
10343 }
10344
10345 /* OpenACC 2.0:
10346 deviceptr ( variable-list ) */
10347
10348 static tree
10349 c_parser_oacc_data_clause_deviceptr (c_parser *parser, tree list)
10350 {
10351 location_t loc = c_parser_peek_token (parser)->location;
10352 tree vars, t;
10353
10354 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
10355 c_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
10356 variable-list must only allow for pointer variables. */
10357 vars = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
10358 for (t = vars; t && t; t = TREE_CHAIN (t))
10359 {
10360 tree v = TREE_PURPOSE (t);
10361
10362 /* FIXME diagnostics: Ideally we should keep individual
10363 locations for all the variables in the var list to make the
10364 following errors more precise. Perhaps
10365 c_parser_omp_var_list_parens() should construct a list of
10366 locations to go along with the var list. */
10367
10368 if (TREE_CODE (v) != VAR_DECL)
10369 error_at (loc, "%qD is not a variable", v);
10370 else if (TREE_TYPE (v) == error_mark_node)
10371 ;
10372 else if (!POINTER_TYPE_P (TREE_TYPE (v)))
10373 error_at (loc, "%qD is not a pointer variable", v);
10374
10375 tree u = build_omp_clause (loc, OMP_CLAUSE_MAP);
10376 OMP_CLAUSE_SET_MAP_KIND (u, GOMP_MAP_FORCE_DEVICEPTR);
10377 OMP_CLAUSE_DECL (u) = v;
10378 OMP_CLAUSE_CHAIN (u) = list;
10379 list = u;
10380 }
10381
10382 return list;
10383 }
10384
10385 /* OpenACC 2.0, OpenMP 3.0:
10386 collapse ( constant-expression ) */
10387
10388 static tree
10389 c_parser_omp_clause_collapse (c_parser *parser, tree list)
10390 {
10391 tree c, num = error_mark_node;
10392 HOST_WIDE_INT n;
10393 location_t loc;
10394
10395 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse");
10396
10397 loc = c_parser_peek_token (parser)->location;
10398 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10399 {
10400 num = c_parser_expr_no_commas (parser, NULL).value;
10401 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10402 }
10403 if (num == error_mark_node)
10404 return list;
10405 mark_exp_read (num);
10406 num = c_fully_fold (num, false, NULL);
10407 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
10408 || !tree_fits_shwi_p (num)
10409 || (n = tree_to_shwi (num)) <= 0
10410 || (int) n != n)
10411 {
10412 error_at (loc,
10413 "collapse argument needs positive constant integer expression");
10414 return list;
10415 }
10416 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
10417 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
10418 OMP_CLAUSE_CHAIN (c) = list;
10419 return c;
10420 }
10421
10422 /* OpenMP 2.5:
10423 copyin ( variable-list ) */
10424
10425 static tree
10426 c_parser_omp_clause_copyin (c_parser *parser, tree list)
10427 {
10428 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_COPYIN, list);
10429 }
10430
10431 /* OpenMP 2.5:
10432 copyprivate ( variable-list ) */
10433
10434 static tree
10435 c_parser_omp_clause_copyprivate (c_parser *parser, tree list)
10436 {
10437 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_COPYPRIVATE, list);
10438 }
10439
10440 /* OpenMP 2.5:
10441 default ( shared | none ) */
10442
10443 static tree
10444 c_parser_omp_clause_default (c_parser *parser, tree list)
10445 {
10446 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
10447 location_t loc = c_parser_peek_token (parser)->location;
10448 tree c;
10449
10450 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10451 return list;
10452 if (c_parser_next_token_is (parser, CPP_NAME))
10453 {
10454 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
10455
10456 switch (p[0])
10457 {
10458 case 'n':
10459 if (strcmp ("none", p) != 0)
10460 goto invalid_kind;
10461 kind = OMP_CLAUSE_DEFAULT_NONE;
10462 break;
10463
10464 case 's':
10465 if (strcmp ("shared", p) != 0)
10466 goto invalid_kind;
10467 kind = OMP_CLAUSE_DEFAULT_SHARED;
10468 break;
10469
10470 default:
10471 goto invalid_kind;
10472 }
10473
10474 c_parser_consume_token (parser);
10475 }
10476 else
10477 {
10478 invalid_kind:
10479 c_parser_error (parser, "expected %<none%> or %<shared%>");
10480 }
10481 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10482
10483 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
10484 return list;
10485
10486 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default");
10487 c = build_omp_clause (loc, OMP_CLAUSE_DEFAULT);
10488 OMP_CLAUSE_CHAIN (c) = list;
10489 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
10490
10491 return c;
10492 }
10493
10494 /* OpenMP 2.5:
10495 firstprivate ( variable-list ) */
10496
10497 static tree
10498 c_parser_omp_clause_firstprivate (c_parser *parser, tree list)
10499 {
10500 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_FIRSTPRIVATE, list);
10501 }
10502
10503 /* OpenMP 3.1:
10504 final ( expression ) */
10505
10506 static tree
10507 c_parser_omp_clause_final (c_parser *parser, tree list)
10508 {
10509 location_t loc = c_parser_peek_token (parser)->location;
10510 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
10511 {
10512 tree t = c_parser_paren_condition (parser);
10513 tree c;
10514
10515 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final");
10516
10517 c = build_omp_clause (loc, OMP_CLAUSE_FINAL);
10518 OMP_CLAUSE_FINAL_EXPR (c) = t;
10519 OMP_CLAUSE_CHAIN (c) = list;
10520 list = c;
10521 }
10522 else
10523 c_parser_error (parser, "expected %<(%>");
10524
10525 return list;
10526 }
10527
10528 /* OpenACC, OpenMP 2.5:
10529 if ( expression ) */
10530
10531 static tree
10532 c_parser_omp_clause_if (c_parser *parser, tree list)
10533 {
10534 location_t loc = c_parser_peek_token (parser)->location;
10535 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
10536 {
10537 tree t = c_parser_paren_condition (parser);
10538 tree c;
10539
10540 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if");
10541
10542 c = build_omp_clause (loc, OMP_CLAUSE_IF);
10543 OMP_CLAUSE_IF_EXPR (c) = t;
10544 OMP_CLAUSE_CHAIN (c) = list;
10545 list = c;
10546 }
10547 else
10548 c_parser_error (parser, "expected %<(%>");
10549
10550 return list;
10551 }
10552
10553 /* OpenMP 2.5:
10554 lastprivate ( variable-list ) */
10555
10556 static tree
10557 c_parser_omp_clause_lastprivate (c_parser *parser, tree list)
10558 {
10559 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_LASTPRIVATE, list);
10560 }
10561
10562 /* OpenMP 3.1:
10563 mergeable */
10564
10565 static tree
10566 c_parser_omp_clause_mergeable (c_parser *parser ATTRIBUTE_UNUSED, tree list)
10567 {
10568 tree c;
10569
10570 /* FIXME: Should we allow duplicates? */
10571 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable");
10572
10573 c = build_omp_clause (c_parser_peek_token (parser)->location,
10574 OMP_CLAUSE_MERGEABLE);
10575 OMP_CLAUSE_CHAIN (c) = list;
10576
10577 return c;
10578 }
10579
10580 /* OpenMP 2.5:
10581 nowait */
10582
10583 static tree
10584 c_parser_omp_clause_nowait (c_parser *parser ATTRIBUTE_UNUSED, tree list)
10585 {
10586 tree c;
10587 location_t loc = c_parser_peek_token (parser)->location;
10588
10589 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait");
10590
10591 c = build_omp_clause (loc, OMP_CLAUSE_NOWAIT);
10592 OMP_CLAUSE_CHAIN (c) = list;
10593 return c;
10594 }
10595
10596 /* OpenACC:
10597 num_gangs ( expression ) */
10598
10599 static tree
10600 c_parser_omp_clause_num_gangs (c_parser *parser, tree list)
10601 {
10602 location_t num_gangs_loc = c_parser_peek_token (parser)->location;
10603 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10604 {
10605 location_t expr_loc = c_parser_peek_token (parser)->location;
10606 tree c, t = c_parser_expression (parser).value;
10607 mark_exp_read (t);
10608 t = c_fully_fold (t, false, NULL);
10609
10610 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10611
10612 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
10613 {
10614 c_parser_error (parser, "expected integer expression");
10615 return list;
10616 }
10617
10618 /* Attempt to statically determine when the number isn't positive. */
10619 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
10620 build_int_cst (TREE_TYPE (t), 0));
10621 if (CAN_HAVE_LOCATION_P (c))
10622 SET_EXPR_LOCATION (c, expr_loc);
10623 if (c == boolean_true_node)
10624 {
10625 warning_at (expr_loc, 0,
10626 "%<num_gangs%> value must be positive");
10627 t = integer_one_node;
10628 }
10629
10630 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_GANGS, "num_gangs");
10631
10632 c = build_omp_clause (num_gangs_loc, OMP_CLAUSE_NUM_GANGS);
10633 OMP_CLAUSE_NUM_GANGS_EXPR (c) = t;
10634 OMP_CLAUSE_CHAIN (c) = list;
10635 list = c;
10636 }
10637
10638 return list;
10639 }
10640
10641 /* OpenMP 2.5:
10642 num_threads ( expression ) */
10643
10644 static tree
10645 c_parser_omp_clause_num_threads (c_parser *parser, tree list)
10646 {
10647 location_t num_threads_loc = c_parser_peek_token (parser)->location;
10648 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10649 {
10650 location_t expr_loc = c_parser_peek_token (parser)->location;
10651 tree c, t = c_parser_expression (parser).value;
10652 mark_exp_read (t);
10653 t = c_fully_fold (t, false, NULL);
10654
10655 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10656
10657 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
10658 {
10659 c_parser_error (parser, "expected integer expression");
10660 return list;
10661 }
10662
10663 /* Attempt to statically determine when the number isn't positive. */
10664 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
10665 build_int_cst (TREE_TYPE (t), 0));
10666 if (CAN_HAVE_LOCATION_P (c))
10667 SET_EXPR_LOCATION (c, expr_loc);
10668 if (c == boolean_true_node)
10669 {
10670 warning_at (expr_loc, 0,
10671 "%<num_threads%> value must be positive");
10672 t = integer_one_node;
10673 }
10674
10675 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS, "num_threads");
10676
10677 c = build_omp_clause (num_threads_loc, OMP_CLAUSE_NUM_THREADS);
10678 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
10679 OMP_CLAUSE_CHAIN (c) = list;
10680 list = c;
10681 }
10682
10683 return list;
10684 }
10685
10686 /* OpenACC:
10687 num_workers ( expression ) */
10688
10689 static tree
10690 c_parser_omp_clause_num_workers (c_parser *parser, tree list)
10691 {
10692 location_t num_workers_loc = c_parser_peek_token (parser)->location;
10693 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10694 {
10695 location_t expr_loc = c_parser_peek_token (parser)->location;
10696 tree c, t = c_parser_expression (parser).value;
10697 mark_exp_read (t);
10698 t = c_fully_fold (t, false, NULL);
10699
10700 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10701
10702 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
10703 {
10704 c_parser_error (parser, "expected integer expression");
10705 return list;
10706 }
10707
10708 /* Attempt to statically determine when the number isn't positive. */
10709 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
10710 build_int_cst (TREE_TYPE (t), 0));
10711 if (CAN_HAVE_LOCATION_P (c))
10712 SET_EXPR_LOCATION (c, expr_loc);
10713 if (c == boolean_true_node)
10714 {
10715 warning_at (expr_loc, 0,
10716 "%<num_workers%> value must be positive");
10717 t = integer_one_node;
10718 }
10719
10720 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_WORKERS, "num_workers");
10721
10722 c = build_omp_clause (num_workers_loc, OMP_CLAUSE_NUM_WORKERS);
10723 OMP_CLAUSE_NUM_WORKERS_EXPR (c) = t;
10724 OMP_CLAUSE_CHAIN (c) = list;
10725 list = c;
10726 }
10727
10728 return list;
10729 }
10730
10731 /* OpenACC:
10732 async [( int-expr )] */
10733
10734 static tree
10735 c_parser_oacc_clause_async (c_parser *parser, tree list)
10736 {
10737 tree c, t;
10738 location_t loc = c_parser_peek_token (parser)->location;
10739
10740 t = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
10741
10742 if (c_parser_peek_token (parser)->type == CPP_OPEN_PAREN)
10743 {
10744 c_parser_consume_token (parser);
10745
10746 t = c_parser_expression (parser).value;
10747 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
10748 c_parser_error (parser, "expected integer expression");
10749 else if (t == error_mark_node
10750 || !c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
10751 return list;
10752 }
10753 else
10754 t = c_fully_fold (t, false, NULL);
10755
10756 check_no_duplicate_clause (list, OMP_CLAUSE_ASYNC, "async");
10757
10758 c = build_omp_clause (loc, OMP_CLAUSE_ASYNC);
10759 OMP_CLAUSE_ASYNC_EXPR (c) = t;
10760 OMP_CLAUSE_CHAIN (c) = list;
10761 list = c;
10762
10763 return list;
10764 }
10765
10766 /* OpenACC:
10767 wait ( int-expr-list ) */
10768
10769 static tree
10770 c_parser_oacc_clause_wait (c_parser *parser, tree list)
10771 {
10772 location_t clause_loc = c_parser_peek_token (parser)->location;
10773
10774 if (c_parser_peek_token (parser)->type == CPP_OPEN_PAREN)
10775 list = c_parser_oacc_wait_list (parser, clause_loc, list);
10776
10777 return list;
10778 }
10779
10780 /* OpenMP 2.5:
10781 ordered */
10782
10783 static tree
10784 c_parser_omp_clause_ordered (c_parser *parser, tree list)
10785 {
10786 tree c;
10787
10788 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED, "ordered");
10789
10790 c = build_omp_clause (c_parser_peek_token (parser)->location,
10791 OMP_CLAUSE_ORDERED);
10792 OMP_CLAUSE_CHAIN (c) = list;
10793
10794 return c;
10795 }
10796
10797 /* OpenMP 2.5:
10798 private ( variable-list ) */
10799
10800 static tree
10801 c_parser_omp_clause_private (c_parser *parser, tree list)
10802 {
10803 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_PRIVATE, list);
10804 }
10805
10806 /* OpenMP 2.5:
10807 reduction ( reduction-operator : variable-list )
10808
10809 reduction-operator:
10810 One of: + * - & ^ | && ||
10811
10812 OpenMP 3.1:
10813
10814 reduction-operator:
10815 One of: + * - & ^ | && || max min
10816
10817 OpenMP 4.0:
10818
10819 reduction-operator:
10820 One of: + * - & ^ | && ||
10821 identifier */
10822
10823 static tree
10824 c_parser_omp_clause_reduction (c_parser *parser, tree list)
10825 {
10826 location_t clause_loc = c_parser_peek_token (parser)->location;
10827 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10828 {
10829 enum tree_code code = ERROR_MARK;
10830 tree reduc_id = NULL_TREE;
10831
10832 switch (c_parser_peek_token (parser)->type)
10833 {
10834 case CPP_PLUS:
10835 code = PLUS_EXPR;
10836 break;
10837 case CPP_MULT:
10838 code = MULT_EXPR;
10839 break;
10840 case CPP_MINUS:
10841 code = MINUS_EXPR;
10842 break;
10843 case CPP_AND:
10844 code = BIT_AND_EXPR;
10845 break;
10846 case CPP_XOR:
10847 code = BIT_XOR_EXPR;
10848 break;
10849 case CPP_OR:
10850 code = BIT_IOR_EXPR;
10851 break;
10852 case CPP_AND_AND:
10853 code = TRUTH_ANDIF_EXPR;
10854 break;
10855 case CPP_OR_OR:
10856 code = TRUTH_ORIF_EXPR;
10857 break;
10858 case CPP_NAME:
10859 {
10860 const char *p
10861 = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
10862 if (strcmp (p, "min") == 0)
10863 {
10864 code = MIN_EXPR;
10865 break;
10866 }
10867 if (strcmp (p, "max") == 0)
10868 {
10869 code = MAX_EXPR;
10870 break;
10871 }
10872 reduc_id = c_parser_peek_token (parser)->value;
10873 break;
10874 }
10875 default:
10876 c_parser_error (parser,
10877 "expected %<+%>, %<*%>, %<-%>, %<&%>, "
10878 "%<^%>, %<|%>, %<&&%>, %<||%>, %<min%> or %<max%>");
10879 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
10880 return list;
10881 }
10882 c_parser_consume_token (parser);
10883 reduc_id = c_omp_reduction_id (code, reduc_id);
10884 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
10885 {
10886 tree nl, c;
10887
10888 nl = c_parser_omp_variable_list (parser, clause_loc,
10889 OMP_CLAUSE_REDUCTION, list);
10890 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
10891 {
10892 tree type = TREE_TYPE (OMP_CLAUSE_DECL (c));
10893 OMP_CLAUSE_REDUCTION_CODE (c) = code;
10894 if (code == ERROR_MARK
10895 || !(INTEGRAL_TYPE_P (type)
10896 || TREE_CODE (type) == REAL_TYPE
10897 || TREE_CODE (type) == COMPLEX_TYPE))
10898 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c)
10899 = c_omp_reduction_lookup (reduc_id,
10900 TYPE_MAIN_VARIANT (type));
10901 }
10902
10903 list = nl;
10904 }
10905 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10906 }
10907 return list;
10908 }
10909
10910 /* OpenMP 2.5:
10911 schedule ( schedule-kind )
10912 schedule ( schedule-kind , expression )
10913
10914 schedule-kind:
10915 static | dynamic | guided | runtime | auto
10916 */
10917
10918 static tree
10919 c_parser_omp_clause_schedule (c_parser *parser, tree list)
10920 {
10921 tree c, t;
10922 location_t loc = c_parser_peek_token (parser)->location;
10923
10924 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10925 return list;
10926
10927 c = build_omp_clause (loc, OMP_CLAUSE_SCHEDULE);
10928
10929 if (c_parser_next_token_is (parser, CPP_NAME))
10930 {
10931 tree kind = c_parser_peek_token (parser)->value;
10932 const char *p = IDENTIFIER_POINTER (kind);
10933
10934 switch (p[0])
10935 {
10936 case 'd':
10937 if (strcmp ("dynamic", p) != 0)
10938 goto invalid_kind;
10939 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
10940 break;
10941
10942 case 'g':
10943 if (strcmp ("guided", p) != 0)
10944 goto invalid_kind;
10945 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
10946 break;
10947
10948 case 'r':
10949 if (strcmp ("runtime", p) != 0)
10950 goto invalid_kind;
10951 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
10952 break;
10953
10954 default:
10955 goto invalid_kind;
10956 }
10957 }
10958 else if (c_parser_next_token_is_keyword (parser, RID_STATIC))
10959 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
10960 else if (c_parser_next_token_is_keyword (parser, RID_AUTO))
10961 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
10962 else
10963 goto invalid_kind;
10964
10965 c_parser_consume_token (parser);
10966 if (c_parser_next_token_is (parser, CPP_COMMA))
10967 {
10968 location_t here;
10969 c_parser_consume_token (parser);
10970
10971 here = c_parser_peek_token (parser)->location;
10972 t = c_parser_expr_no_commas (parser, NULL).value;
10973 mark_exp_read (t);
10974 t = c_fully_fold (t, false, NULL);
10975
10976 if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
10977 error_at (here, "schedule %<runtime%> does not take "
10978 "a %<chunk_size%> parameter");
10979 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
10980 error_at (here,
10981 "schedule %<auto%> does not take "
10982 "a %<chunk_size%> parameter");
10983 else if (TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE)
10984 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
10985 else
10986 c_parser_error (parser, "expected integer expression");
10987
10988 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10989 }
10990 else
10991 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
10992 "expected %<,%> or %<)%>");
10993
10994 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
10995 OMP_CLAUSE_CHAIN (c) = list;
10996 return c;
10997
10998 invalid_kind:
10999 c_parser_error (parser, "invalid schedule kind");
11000 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
11001 return list;
11002 }
11003
11004 /* OpenMP 2.5:
11005 shared ( variable-list ) */
11006
11007 static tree
11008 c_parser_omp_clause_shared (c_parser *parser, tree list)
11009 {
11010 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_SHARED, list);
11011 }
11012
11013 /* OpenMP 3.0:
11014 untied */
11015
11016 static tree
11017 c_parser_omp_clause_untied (c_parser *parser ATTRIBUTE_UNUSED, tree list)
11018 {
11019 tree c;
11020
11021 /* FIXME: Should we allow duplicates? */
11022 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied");
11023
11024 c = build_omp_clause (c_parser_peek_token (parser)->location,
11025 OMP_CLAUSE_UNTIED);
11026 OMP_CLAUSE_CHAIN (c) = list;
11027
11028 return c;
11029 }
11030
11031 /* OpenACC:
11032 vector_length ( expression ) */
11033
11034 static tree
11035 c_parser_omp_clause_vector_length (c_parser *parser, tree list)
11036 {
11037 location_t vector_length_loc = c_parser_peek_token (parser)->location;
11038 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11039 {
11040 location_t expr_loc = c_parser_peek_token (parser)->location;
11041 tree c, t = c_parser_expression (parser).value;
11042 mark_exp_read (t);
11043 t = c_fully_fold (t, false, NULL);
11044
11045 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11046
11047 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
11048 {
11049 c_parser_error (parser, "expected integer expression");
11050 return list;
11051 }
11052
11053 /* Attempt to statically determine when the number isn't positive. */
11054 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
11055 build_int_cst (TREE_TYPE (t), 0));
11056 if (CAN_HAVE_LOCATION_P (c))
11057 SET_EXPR_LOCATION (c, expr_loc);
11058 if (c == boolean_true_node)
11059 {
11060 warning_at (expr_loc, 0,
11061 "%<vector_length%> value must be positive");
11062 t = integer_one_node;
11063 }
11064
11065 check_no_duplicate_clause (list, OMP_CLAUSE_VECTOR_LENGTH, "vector_length");
11066
11067 c = build_omp_clause (vector_length_loc, OMP_CLAUSE_VECTOR_LENGTH);
11068 OMP_CLAUSE_VECTOR_LENGTH_EXPR (c) = t;
11069 OMP_CLAUSE_CHAIN (c) = list;
11070 list = c;
11071 }
11072
11073 return list;
11074 }
11075
11076 /* OpenMP 4.0:
11077 inbranch
11078 notinbranch */
11079
11080 static tree
11081 c_parser_omp_clause_branch (c_parser *parser ATTRIBUTE_UNUSED,
11082 enum omp_clause_code code, tree list)
11083 {
11084 check_no_duplicate_clause (list, code, omp_clause_code_name[code]);
11085
11086 tree c = build_omp_clause (c_parser_peek_token (parser)->location, code);
11087 OMP_CLAUSE_CHAIN (c) = list;
11088
11089 return c;
11090 }
11091
11092 /* OpenMP 4.0:
11093 parallel
11094 for
11095 sections
11096 taskgroup */
11097
11098 static tree
11099 c_parser_omp_clause_cancelkind (c_parser *parser ATTRIBUTE_UNUSED,
11100 enum omp_clause_code code, tree list)
11101 {
11102 tree c = build_omp_clause (c_parser_peek_token (parser)->location, code);
11103 OMP_CLAUSE_CHAIN (c) = list;
11104
11105 return c;
11106 }
11107
11108 /* OpenMP 4.0:
11109 num_teams ( expression ) */
11110
11111 static tree
11112 c_parser_omp_clause_num_teams (c_parser *parser, tree list)
11113 {
11114 location_t num_teams_loc = c_parser_peek_token (parser)->location;
11115 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11116 {
11117 location_t expr_loc = c_parser_peek_token (parser)->location;
11118 tree c, t = c_parser_expression (parser).value;
11119 mark_exp_read (t);
11120 t = c_fully_fold (t, false, NULL);
11121
11122 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11123
11124 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
11125 {
11126 c_parser_error (parser, "expected integer expression");
11127 return list;
11128 }
11129
11130 /* Attempt to statically determine when the number isn't positive. */
11131 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
11132 build_int_cst (TREE_TYPE (t), 0));
11133 if (CAN_HAVE_LOCATION_P (c))
11134 SET_EXPR_LOCATION (c, expr_loc);
11135 if (c == boolean_true_node)
11136 {
11137 warning_at (expr_loc, 0, "%<num_teams%> value must be positive");
11138 t = integer_one_node;
11139 }
11140
11141 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS, "num_teams");
11142
11143 c = build_omp_clause (num_teams_loc, OMP_CLAUSE_NUM_TEAMS);
11144 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
11145 OMP_CLAUSE_CHAIN (c) = list;
11146 list = c;
11147 }
11148
11149 return list;
11150 }
11151
11152 /* OpenMP 4.0:
11153 thread_limit ( expression ) */
11154
11155 static tree
11156 c_parser_omp_clause_thread_limit (c_parser *parser, tree list)
11157 {
11158 location_t num_thread_limit_loc = c_parser_peek_token (parser)->location;
11159 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11160 {
11161 location_t expr_loc = c_parser_peek_token (parser)->location;
11162 tree c, t = c_parser_expression (parser).value;
11163 mark_exp_read (t);
11164 t = c_fully_fold (t, false, NULL);
11165
11166 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11167
11168 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
11169 {
11170 c_parser_error (parser, "expected integer expression");
11171 return list;
11172 }
11173
11174 /* Attempt to statically determine when the number isn't positive. */
11175 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
11176 build_int_cst (TREE_TYPE (t), 0));
11177 if (CAN_HAVE_LOCATION_P (c))
11178 SET_EXPR_LOCATION (c, expr_loc);
11179 if (c == boolean_true_node)
11180 {
11181 warning_at (expr_loc, 0, "%<thread_limit%> value must be positive");
11182 t = integer_one_node;
11183 }
11184
11185 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
11186 "thread_limit");
11187
11188 c = build_omp_clause (num_thread_limit_loc, OMP_CLAUSE_THREAD_LIMIT);
11189 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
11190 OMP_CLAUSE_CHAIN (c) = list;
11191 list = c;
11192 }
11193
11194 return list;
11195 }
11196
11197 /* OpenMP 4.0:
11198 aligned ( variable-list )
11199 aligned ( variable-list : constant-expression ) */
11200
11201 static tree
11202 c_parser_omp_clause_aligned (c_parser *parser, tree list)
11203 {
11204 location_t clause_loc = c_parser_peek_token (parser)->location;
11205 tree nl, c;
11206
11207 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11208 return list;
11209
11210 nl = c_parser_omp_variable_list (parser, clause_loc,
11211 OMP_CLAUSE_ALIGNED, list);
11212
11213 if (c_parser_next_token_is (parser, CPP_COLON))
11214 {
11215 c_parser_consume_token (parser);
11216 tree alignment = c_parser_expr_no_commas (parser, NULL).value;
11217 mark_exp_read (alignment);
11218 alignment = c_fully_fold (alignment, false, NULL);
11219 if (!INTEGRAL_TYPE_P (TREE_TYPE (alignment))
11220 && TREE_CODE (alignment) != INTEGER_CST
11221 && tree_int_cst_sgn (alignment) != 1)
11222 {
11223 error_at (clause_loc, "%<aligned%> clause alignment expression must "
11224 "be positive constant integer expression");
11225 alignment = NULL_TREE;
11226 }
11227
11228 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
11229 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
11230 }
11231
11232 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11233 return nl;
11234 }
11235
11236 /* OpenMP 4.0:
11237 linear ( variable-list )
11238 linear ( variable-list : expression ) */
11239
11240 static tree
11241 c_parser_omp_clause_linear (c_parser *parser, tree list, bool is_cilk_simd_fn)
11242 {
11243 location_t clause_loc = c_parser_peek_token (parser)->location;
11244 tree nl, c, step;
11245
11246 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11247 return list;
11248
11249 nl = c_parser_omp_variable_list (parser, clause_loc,
11250 OMP_CLAUSE_LINEAR, list);
11251
11252 if (c_parser_next_token_is (parser, CPP_COLON))
11253 {
11254 c_parser_consume_token (parser);
11255 step = c_parser_expression (parser).value;
11256 mark_exp_read (step);
11257 step = c_fully_fold (step, false, NULL);
11258 if (is_cilk_simd_fn && TREE_CODE (step) == PARM_DECL)
11259 {
11260 sorry ("using parameters for %<linear%> step is not supported yet");
11261 step = integer_one_node;
11262 }
11263 if (!INTEGRAL_TYPE_P (TREE_TYPE (step)))
11264 {
11265 error_at (clause_loc, "%<linear%> clause step expression must "
11266 "be integral");
11267 step = integer_one_node;
11268 }
11269
11270 }
11271 else
11272 step = integer_one_node;
11273
11274 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
11275 {
11276 OMP_CLAUSE_LINEAR_STEP (c) = step;
11277 }
11278
11279 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11280 return nl;
11281 }
11282
11283 /* OpenMP 4.0:
11284 safelen ( constant-expression ) */
11285
11286 static tree
11287 c_parser_omp_clause_safelen (c_parser *parser, tree list)
11288 {
11289 location_t clause_loc = c_parser_peek_token (parser)->location;
11290 tree c, t;
11291
11292 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11293 return list;
11294
11295 t = c_parser_expr_no_commas (parser, NULL).value;
11296 mark_exp_read (t);
11297 t = c_fully_fold (t, false, NULL);
11298 if (!INTEGRAL_TYPE_P (TREE_TYPE (t))
11299 && TREE_CODE (t) != INTEGER_CST
11300 && tree_int_cst_sgn (t) != 1)
11301 {
11302 error_at (clause_loc, "%<safelen%> clause expression must "
11303 "be positive constant integer expression");
11304 t = NULL_TREE;
11305 }
11306
11307 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11308 if (t == NULL_TREE || t == error_mark_node)
11309 return list;
11310
11311 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen");
11312
11313 c = build_omp_clause (clause_loc, OMP_CLAUSE_SAFELEN);
11314 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
11315 OMP_CLAUSE_CHAIN (c) = list;
11316 return c;
11317 }
11318
11319 /* OpenMP 4.0:
11320 simdlen ( constant-expression ) */
11321
11322 static tree
11323 c_parser_omp_clause_simdlen (c_parser *parser, tree list)
11324 {
11325 location_t clause_loc = c_parser_peek_token (parser)->location;
11326 tree c, t;
11327
11328 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11329 return list;
11330
11331 t = c_parser_expr_no_commas (parser, NULL).value;
11332 mark_exp_read (t);
11333 t = c_fully_fold (t, false, NULL);
11334 if (!INTEGRAL_TYPE_P (TREE_TYPE (t))
11335 && TREE_CODE (t) != INTEGER_CST
11336 && tree_int_cst_sgn (t) != 1)
11337 {
11338 error_at (clause_loc, "%<simdlen%> clause expression must "
11339 "be positive constant integer expression");
11340 t = NULL_TREE;
11341 }
11342
11343 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11344 if (t == NULL_TREE || t == error_mark_node)
11345 return list;
11346
11347 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen");
11348
11349 c = build_omp_clause (clause_loc, OMP_CLAUSE_SIMDLEN);
11350 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
11351 OMP_CLAUSE_CHAIN (c) = list;
11352 return c;
11353 }
11354
11355 /* OpenMP 4.0:
11356 depend ( depend-kind: variable-list )
11357
11358 depend-kind:
11359 in | out | inout */
11360
11361 static tree
11362 c_parser_omp_clause_depend (c_parser *parser, tree list)
11363 {
11364 location_t clause_loc = c_parser_peek_token (parser)->location;
11365 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_INOUT;
11366 tree nl, c;
11367
11368 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11369 return list;
11370
11371 if (c_parser_next_token_is (parser, CPP_NAME))
11372 {
11373 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
11374 if (strcmp ("in", p) == 0)
11375 kind = OMP_CLAUSE_DEPEND_IN;
11376 else if (strcmp ("inout", p) == 0)
11377 kind = OMP_CLAUSE_DEPEND_INOUT;
11378 else if (strcmp ("out", p) == 0)
11379 kind = OMP_CLAUSE_DEPEND_OUT;
11380 else
11381 goto invalid_kind;
11382 }
11383 else
11384 goto invalid_kind;
11385
11386 c_parser_consume_token (parser);
11387 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
11388 goto resync_fail;
11389
11390 nl = c_parser_omp_variable_list (parser, clause_loc,
11391 OMP_CLAUSE_DEPEND, list);
11392
11393 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
11394 OMP_CLAUSE_DEPEND_KIND (c) = kind;
11395
11396 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11397 return nl;
11398
11399 invalid_kind:
11400 c_parser_error (parser, "invalid depend kind");
11401 resync_fail:
11402 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11403 return list;
11404 }
11405
11406 /* OpenMP 4.0:
11407 map ( map-kind: variable-list )
11408 map ( variable-list )
11409
11410 map-kind:
11411 alloc | to | from | tofrom */
11412
11413 static tree
11414 c_parser_omp_clause_map (c_parser *parser, tree list)
11415 {
11416 location_t clause_loc = c_parser_peek_token (parser)->location;
11417 enum gomp_map_kind kind = GOMP_MAP_TOFROM;
11418 tree nl, c;
11419
11420 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11421 return list;
11422
11423 if (c_parser_next_token_is (parser, CPP_NAME)
11424 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
11425 {
11426 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
11427 if (strcmp ("alloc", p) == 0)
11428 kind = GOMP_MAP_ALLOC;
11429 else if (strcmp ("to", p) == 0)
11430 kind = GOMP_MAP_TO;
11431 else if (strcmp ("from", p) == 0)
11432 kind = GOMP_MAP_FROM;
11433 else if (strcmp ("tofrom", p) == 0)
11434 kind = GOMP_MAP_TOFROM;
11435 else
11436 {
11437 c_parser_error (parser, "invalid map kind");
11438 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
11439 "expected %<)%>");
11440 return list;
11441 }
11442 c_parser_consume_token (parser);
11443 c_parser_consume_token (parser);
11444 }
11445
11446 nl = c_parser_omp_variable_list (parser, clause_loc, OMP_CLAUSE_MAP, list);
11447
11448 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
11449 OMP_CLAUSE_SET_MAP_KIND (c, kind);
11450
11451 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11452 return nl;
11453 }
11454
11455 /* OpenMP 4.0:
11456 device ( expression ) */
11457
11458 static tree
11459 c_parser_omp_clause_device (c_parser *parser, tree list)
11460 {
11461 location_t clause_loc = c_parser_peek_token (parser)->location;
11462 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11463 {
11464 tree c, t = c_parser_expr_no_commas (parser, NULL).value;
11465 mark_exp_read (t);
11466 t = c_fully_fold (t, false, NULL);
11467
11468 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11469
11470 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
11471 {
11472 c_parser_error (parser, "expected integer expression");
11473 return list;
11474 }
11475
11476 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE, "device");
11477
11478 c = build_omp_clause (clause_loc, OMP_CLAUSE_DEVICE);
11479 OMP_CLAUSE_DEVICE_ID (c) = t;
11480 OMP_CLAUSE_CHAIN (c) = list;
11481 list = c;
11482 }
11483
11484 return list;
11485 }
11486
11487 /* OpenMP 4.0:
11488 dist_schedule ( static )
11489 dist_schedule ( static , expression ) */
11490
11491 static tree
11492 c_parser_omp_clause_dist_schedule (c_parser *parser, tree list)
11493 {
11494 tree c, t = NULL_TREE;
11495 location_t loc = c_parser_peek_token (parser)->location;
11496
11497 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11498 return list;
11499
11500 if (!c_parser_next_token_is_keyword (parser, RID_STATIC))
11501 {
11502 c_parser_error (parser, "invalid dist_schedule kind");
11503 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
11504 "expected %<)%>");
11505 return list;
11506 }
11507
11508 c_parser_consume_token (parser);
11509 if (c_parser_next_token_is (parser, CPP_COMMA))
11510 {
11511 c_parser_consume_token (parser);
11512
11513 t = c_parser_expr_no_commas (parser, NULL).value;
11514 mark_exp_read (t);
11515 t = c_fully_fold (t, false, NULL);
11516 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11517 }
11518 else
11519 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
11520 "expected %<,%> or %<)%>");
11521
11522 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
11523 if (t == error_mark_node)
11524 return list;
11525
11526 c = build_omp_clause (loc, OMP_CLAUSE_DIST_SCHEDULE);
11527 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
11528 OMP_CLAUSE_CHAIN (c) = list;
11529 return c;
11530 }
11531
11532 /* OpenMP 4.0:
11533 proc_bind ( proc-bind-kind )
11534
11535 proc-bind-kind:
11536 master | close | spread */
11537
11538 static tree
11539 c_parser_omp_clause_proc_bind (c_parser *parser, tree list)
11540 {
11541 location_t clause_loc = c_parser_peek_token (parser)->location;
11542 enum omp_clause_proc_bind_kind kind;
11543 tree c;
11544
11545 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11546 return list;
11547
11548 if (c_parser_next_token_is (parser, CPP_NAME))
11549 {
11550 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
11551 if (strcmp ("master", p) == 0)
11552 kind = OMP_CLAUSE_PROC_BIND_MASTER;
11553 else if (strcmp ("close", p) == 0)
11554 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
11555 else if (strcmp ("spread", p) == 0)
11556 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
11557 else
11558 goto invalid_kind;
11559 }
11560 else
11561 goto invalid_kind;
11562
11563 c_parser_consume_token (parser);
11564 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11565 c = build_omp_clause (clause_loc, OMP_CLAUSE_PROC_BIND);
11566 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
11567 OMP_CLAUSE_CHAIN (c) = list;
11568 return c;
11569
11570 invalid_kind:
11571 c_parser_error (parser, "invalid proc_bind kind");
11572 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11573 return list;
11574 }
11575
11576 /* OpenMP 4.0:
11577 to ( variable-list ) */
11578
11579 static tree
11580 c_parser_omp_clause_to (c_parser *parser, tree list)
11581 {
11582 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_TO, list);
11583 }
11584
11585 /* OpenMP 4.0:
11586 from ( variable-list ) */
11587
11588 static tree
11589 c_parser_omp_clause_from (c_parser *parser, tree list)
11590 {
11591 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_FROM, list);
11592 }
11593
11594 /* OpenMP 4.0:
11595 uniform ( variable-list ) */
11596
11597 static tree
11598 c_parser_omp_clause_uniform (c_parser *parser, tree list)
11599 {
11600 /* The clauses location. */
11601 location_t loc = c_parser_peek_token (parser)->location;
11602
11603 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11604 {
11605 list = c_parser_omp_variable_list (parser, loc, OMP_CLAUSE_UNIFORM,
11606 list);
11607 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11608 }
11609 return list;
11610 }
11611
11612 /* Parse all OpenACC clauses. The set clauses allowed by the directive
11613 is a bitmask in MASK. Return the list of clauses found. */
11614
11615 static tree
11616 c_parser_oacc_all_clauses (c_parser *parser, omp_clause_mask mask,
11617 const char *where, bool finish_p = true)
11618 {
11619 tree clauses = NULL;
11620 bool first = true;
11621
11622 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
11623 {
11624 location_t here;
11625 pragma_omp_clause c_kind;
11626 const char *c_name;
11627 tree prev = clauses;
11628
11629 if (!first && c_parser_next_token_is (parser, CPP_COMMA))
11630 c_parser_consume_token (parser);
11631
11632 here = c_parser_peek_token (parser)->location;
11633 c_kind = c_parser_omp_clause_name (parser);
11634
11635 switch (c_kind)
11636 {
11637 case PRAGMA_OACC_CLAUSE_ASYNC:
11638 clauses = c_parser_oacc_clause_async (parser, clauses);
11639 c_name = "async";
11640 break;
11641 case PRAGMA_OACC_CLAUSE_COLLAPSE:
11642 clauses = c_parser_omp_clause_collapse (parser, clauses);
11643 c_name = "collapse";
11644 break;
11645 case PRAGMA_OACC_CLAUSE_COPY:
11646 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
11647 c_name = "copy";
11648 break;
11649 case PRAGMA_OACC_CLAUSE_COPYIN:
11650 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
11651 c_name = "copyin";
11652 break;
11653 case PRAGMA_OACC_CLAUSE_COPYOUT:
11654 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
11655 c_name = "copyout";
11656 break;
11657 case PRAGMA_OACC_CLAUSE_CREATE:
11658 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
11659 c_name = "create";
11660 break;
11661 case PRAGMA_OACC_CLAUSE_DELETE:
11662 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
11663 c_name = "delete";
11664 break;
11665 case PRAGMA_OACC_CLAUSE_DEVICE:
11666 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
11667 c_name = "device";
11668 break;
11669 case PRAGMA_OACC_CLAUSE_DEVICEPTR:
11670 clauses = c_parser_oacc_data_clause_deviceptr (parser, clauses);
11671 c_name = "deviceptr";
11672 break;
11673 case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE:
11674 clauses = c_parser_omp_clause_firstprivate (parser, clauses);
11675 c_name = "firstprivate";
11676 break;
11677 case PRAGMA_OACC_CLAUSE_HOST:
11678 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
11679 c_name = "host";
11680 break;
11681 case PRAGMA_OACC_CLAUSE_IF:
11682 clauses = c_parser_omp_clause_if (parser, clauses);
11683 c_name = "if";
11684 break;
11685 case PRAGMA_OACC_CLAUSE_NUM_GANGS:
11686 clauses = c_parser_omp_clause_num_gangs (parser, clauses);
11687 c_name = "num_gangs";
11688 break;
11689 case PRAGMA_OACC_CLAUSE_NUM_WORKERS:
11690 clauses = c_parser_omp_clause_num_workers (parser, clauses);
11691 c_name = "num_workers";
11692 break;
11693 case PRAGMA_OACC_CLAUSE_PRESENT:
11694 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
11695 c_name = "present";
11696 break;
11697 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
11698 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
11699 c_name = "present_or_copy";
11700 break;
11701 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
11702 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
11703 c_name = "present_or_copyin";
11704 break;
11705 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
11706 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
11707 c_name = "present_or_copyout";
11708 break;
11709 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
11710 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
11711 c_name = "present_or_create";
11712 break;
11713 case PRAGMA_OACC_CLAUSE_PRIVATE:
11714 clauses = c_parser_omp_clause_private (parser, clauses);
11715 c_name = "private";
11716 break;
11717 case PRAGMA_OACC_CLAUSE_REDUCTION:
11718 clauses = c_parser_omp_clause_reduction (parser, clauses);
11719 c_name = "reduction";
11720 break;
11721 case PRAGMA_OACC_CLAUSE_SELF:
11722 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
11723 c_name = "self";
11724 break;
11725 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH:
11726 clauses = c_parser_omp_clause_vector_length (parser, clauses);
11727 c_name = "vector_length";
11728 break;
11729 case PRAGMA_OACC_CLAUSE_WAIT:
11730 clauses = c_parser_oacc_clause_wait (parser, clauses);
11731 c_name = "wait";
11732 break;
11733 default:
11734 c_parser_error (parser, "expected %<#pragma acc%> clause");
11735 goto saw_error;
11736 }
11737
11738 first = false;
11739
11740 if (((mask >> c_kind) & 1) == 0 && !parser->error)
11741 {
11742 /* Remove the invalid clause(s) from the list to avoid
11743 confusing the rest of the compiler. */
11744 clauses = prev;
11745 error_at (here, "%qs is not valid for %qs", c_name, where);
11746 }
11747 }
11748
11749 saw_error:
11750 c_parser_skip_to_pragma_eol (parser);
11751
11752 if (finish_p)
11753 return c_finish_omp_clauses (clauses);
11754
11755 return clauses;
11756 }
11757
11758 /* Parse all OpenMP clauses. The set clauses allowed by the directive
11759 is a bitmask in MASK. Return the list of clauses found. */
11760
11761 static tree
11762 c_parser_omp_all_clauses (c_parser *parser, omp_clause_mask mask,
11763 const char *where, bool finish_p = true)
11764 {
11765 tree clauses = NULL;
11766 bool first = true, cilk_simd_fn = false;
11767
11768 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
11769 {
11770 location_t here;
11771 pragma_omp_clause c_kind;
11772 const char *c_name;
11773 tree prev = clauses;
11774
11775 if (!first && c_parser_next_token_is (parser, CPP_COMMA))
11776 c_parser_consume_token (parser);
11777
11778 here = c_parser_peek_token (parser)->location;
11779 c_kind = c_parser_omp_clause_name (parser);
11780
11781 switch (c_kind)
11782 {
11783 case PRAGMA_OMP_CLAUSE_COLLAPSE:
11784 clauses = c_parser_omp_clause_collapse (parser, clauses);
11785 c_name = "collapse";
11786 break;
11787 case PRAGMA_OMP_CLAUSE_COPYIN:
11788 clauses = c_parser_omp_clause_copyin (parser, clauses);
11789 c_name = "copyin";
11790 break;
11791 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
11792 clauses = c_parser_omp_clause_copyprivate (parser, clauses);
11793 c_name = "copyprivate";
11794 break;
11795 case PRAGMA_OMP_CLAUSE_DEFAULT:
11796 clauses = c_parser_omp_clause_default (parser, clauses);
11797 c_name = "default";
11798 break;
11799 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
11800 clauses = c_parser_omp_clause_firstprivate (parser, clauses);
11801 c_name = "firstprivate";
11802 break;
11803 case PRAGMA_OMP_CLAUSE_FINAL:
11804 clauses = c_parser_omp_clause_final (parser, clauses);
11805 c_name = "final";
11806 break;
11807 case PRAGMA_OMP_CLAUSE_IF:
11808 clauses = c_parser_omp_clause_if (parser, clauses);
11809 c_name = "if";
11810 break;
11811 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
11812 clauses = c_parser_omp_clause_lastprivate (parser, clauses);
11813 c_name = "lastprivate";
11814 break;
11815 case PRAGMA_OMP_CLAUSE_MERGEABLE:
11816 clauses = c_parser_omp_clause_mergeable (parser, clauses);
11817 c_name = "mergeable";
11818 break;
11819 case PRAGMA_OMP_CLAUSE_NOWAIT:
11820 clauses = c_parser_omp_clause_nowait (parser, clauses);
11821 c_name = "nowait";
11822 break;
11823 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
11824 clauses = c_parser_omp_clause_num_threads (parser, clauses);
11825 c_name = "num_threads";
11826 break;
11827 case PRAGMA_OMP_CLAUSE_ORDERED:
11828 clauses = c_parser_omp_clause_ordered (parser, clauses);
11829 c_name = "ordered";
11830 break;
11831 case PRAGMA_OMP_CLAUSE_PRIVATE:
11832 clauses = c_parser_omp_clause_private (parser, clauses);
11833 c_name = "private";
11834 break;
11835 case PRAGMA_OMP_CLAUSE_REDUCTION:
11836 clauses = c_parser_omp_clause_reduction (parser, clauses);
11837 c_name = "reduction";
11838 break;
11839 case PRAGMA_OMP_CLAUSE_SCHEDULE:
11840 clauses = c_parser_omp_clause_schedule (parser, clauses);
11841 c_name = "schedule";
11842 break;
11843 case PRAGMA_OMP_CLAUSE_SHARED:
11844 clauses = c_parser_omp_clause_shared (parser, clauses);
11845 c_name = "shared";
11846 break;
11847 case PRAGMA_OMP_CLAUSE_UNTIED:
11848 clauses = c_parser_omp_clause_untied (parser, clauses);
11849 c_name = "untied";
11850 break;
11851 case PRAGMA_OMP_CLAUSE_INBRANCH:
11852 case PRAGMA_CILK_CLAUSE_MASK:
11853 clauses = c_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
11854 clauses);
11855 c_name = "inbranch";
11856 break;
11857 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
11858 case PRAGMA_CILK_CLAUSE_NOMASK:
11859 clauses = c_parser_omp_clause_branch (parser, OMP_CLAUSE_NOTINBRANCH,
11860 clauses);
11861 c_name = "notinbranch";
11862 break;
11863 case PRAGMA_OMP_CLAUSE_PARALLEL:
11864 clauses
11865 = c_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
11866 clauses);
11867 c_name = "parallel";
11868 if (!first)
11869 {
11870 clause_not_first:
11871 error_at (here, "%qs must be the first clause of %qs",
11872 c_name, where);
11873 clauses = prev;
11874 }
11875 break;
11876 case PRAGMA_OMP_CLAUSE_FOR:
11877 clauses
11878 = c_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
11879 clauses);
11880 c_name = "for";
11881 if (!first)
11882 goto clause_not_first;
11883 break;
11884 case PRAGMA_OMP_CLAUSE_SECTIONS:
11885 clauses
11886 = c_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
11887 clauses);
11888 c_name = "sections";
11889 if (!first)
11890 goto clause_not_first;
11891 break;
11892 case PRAGMA_OMP_CLAUSE_TASKGROUP:
11893 clauses
11894 = c_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
11895 clauses);
11896 c_name = "taskgroup";
11897 if (!first)
11898 goto clause_not_first;
11899 break;
11900 case PRAGMA_OMP_CLAUSE_TO:
11901 clauses = c_parser_omp_clause_to (parser, clauses);
11902 c_name = "to";
11903 break;
11904 case PRAGMA_OMP_CLAUSE_FROM:
11905 clauses = c_parser_omp_clause_from (parser, clauses);
11906 c_name = "from";
11907 break;
11908 case PRAGMA_OMP_CLAUSE_UNIFORM:
11909 clauses = c_parser_omp_clause_uniform (parser, clauses);
11910 c_name = "uniform";
11911 break;
11912 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
11913 clauses = c_parser_omp_clause_num_teams (parser, clauses);
11914 c_name = "num_teams";
11915 break;
11916 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
11917 clauses = c_parser_omp_clause_thread_limit (parser, clauses);
11918 c_name = "thread_limit";
11919 break;
11920 case PRAGMA_OMP_CLAUSE_ALIGNED:
11921 clauses = c_parser_omp_clause_aligned (parser, clauses);
11922 c_name = "aligned";
11923 break;
11924 case PRAGMA_OMP_CLAUSE_LINEAR:
11925 if (((mask >> PRAGMA_CILK_CLAUSE_VECTORLENGTH) & 1) != 0)
11926 cilk_simd_fn = true;
11927 clauses = c_parser_omp_clause_linear (parser, clauses, cilk_simd_fn);
11928 c_name = "linear";
11929 break;
11930 case PRAGMA_OMP_CLAUSE_DEPEND:
11931 clauses = c_parser_omp_clause_depend (parser, clauses);
11932 c_name = "depend";
11933 break;
11934 case PRAGMA_OMP_CLAUSE_MAP:
11935 clauses = c_parser_omp_clause_map (parser, clauses);
11936 c_name = "map";
11937 break;
11938 case PRAGMA_OMP_CLAUSE_DEVICE:
11939 clauses = c_parser_omp_clause_device (parser, clauses);
11940 c_name = "device";
11941 break;
11942 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
11943 clauses = c_parser_omp_clause_dist_schedule (parser, clauses);
11944 c_name = "dist_schedule";
11945 break;
11946 case PRAGMA_OMP_CLAUSE_PROC_BIND:
11947 clauses = c_parser_omp_clause_proc_bind (parser, clauses);
11948 c_name = "proc_bind";
11949 break;
11950 case PRAGMA_OMP_CLAUSE_SAFELEN:
11951 clauses = c_parser_omp_clause_safelen (parser, clauses);
11952 c_name = "safelen";
11953 break;
11954 case PRAGMA_CILK_CLAUSE_VECTORLENGTH:
11955 clauses = c_parser_cilk_clause_vectorlength (parser, clauses, true);
11956 c_name = "simdlen";
11957 break;
11958 case PRAGMA_OMP_CLAUSE_SIMDLEN:
11959 clauses = c_parser_omp_clause_simdlen (parser, clauses);
11960 c_name = "simdlen";
11961 break;
11962 default:
11963 c_parser_error (parser, "expected %<#pragma omp%> clause");
11964 goto saw_error;
11965 }
11966
11967 first = false;
11968
11969 if (((mask >> c_kind) & 1) == 0 && !parser->error)
11970 {
11971 /* Remove the invalid clause(s) from the list to avoid
11972 confusing the rest of the compiler. */
11973 clauses = prev;
11974 error_at (here, "%qs is not valid for %qs", c_name, where);
11975 }
11976 }
11977
11978 saw_error:
11979 c_parser_skip_to_pragma_eol (parser);
11980
11981 if (finish_p)
11982 return c_finish_omp_clauses (clauses);
11983
11984 return clauses;
11985 }
11986
11987 /* OpenACC 2.0, OpenMP 2.5:
11988 structured-block:
11989 statement
11990
11991 In practice, we're also interested in adding the statement to an
11992 outer node. So it is convenient if we work around the fact that
11993 c_parser_statement calls add_stmt. */
11994
11995 static tree
11996 c_parser_omp_structured_block (c_parser *parser)
11997 {
11998 tree stmt = push_stmt_list ();
11999 c_parser_statement (parser);
12000 return pop_stmt_list (stmt);
12001 }
12002
12003 /* OpenACC 2.0:
12004 # pragma acc cache (variable-list) new-line
12005
12006 LOC is the location of the #pragma token.
12007 */
12008
12009 static tree
12010 c_parser_oacc_cache (location_t loc, c_parser *parser)
12011 {
12012 tree stmt, clauses;
12013
12014 clauses = c_parser_omp_var_list_parens (parser, OMP_CLAUSE__CACHE_, NULL);
12015 clauses = c_finish_omp_clauses (clauses);
12016
12017 c_parser_skip_to_pragma_eol (parser);
12018
12019 stmt = make_node (OACC_CACHE);
12020 TREE_TYPE (stmt) = void_type_node;
12021 OACC_CACHE_CLAUSES (stmt) = clauses;
12022 SET_EXPR_LOCATION (stmt, loc);
12023 add_stmt (stmt);
12024
12025 return stmt;
12026 }
12027
12028 /* OpenACC 2.0:
12029 # pragma acc data oacc-data-clause[optseq] new-line
12030 structured-block
12031
12032 LOC is the location of the #pragma token.
12033 */
12034
12035 #define OACC_DATA_CLAUSE_MASK \
12036 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
12037 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
12038 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
12039 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
12040 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
12041 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
12042 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
12043 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
12044 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
12045 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
12046 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) )
12047
12048 static tree
12049 c_parser_oacc_data (location_t loc, c_parser *parser)
12050 {
12051 tree stmt, clauses, block;
12052
12053 clauses = c_parser_oacc_all_clauses (parser, OACC_DATA_CLAUSE_MASK,
12054 "#pragma acc data");
12055
12056 block = c_begin_omp_parallel ();
12057 add_stmt (c_parser_omp_structured_block (parser));
12058
12059 stmt = c_finish_oacc_data (loc, clauses, block);
12060
12061 return stmt;
12062 }
12063
12064 /* OpenACC 2.0:
12065 # pragma acc kernels oacc-kernels-clause[optseq] new-line
12066 structured-block
12067
12068 LOC is the location of the #pragma token.
12069 */
12070
12071 #define OACC_KERNELS_CLAUSE_MASK \
12072 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
12073 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
12074 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
12075 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
12076 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
12077 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
12078 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
12079 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
12080 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
12081 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
12082 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
12083 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
12084 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
12085
12086 static tree
12087 c_parser_oacc_kernels (location_t loc, c_parser *parser, char *p_name)
12088 {
12089 tree stmt, clauses = NULL_TREE, block;
12090
12091 strcat (p_name, " kernels");
12092
12093 if (c_parser_next_token_is (parser, CPP_NAME))
12094 {
12095 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
12096 if (strcmp (p, "loop") == 0)
12097 {
12098 c_parser_consume_token (parser);
12099 block = c_begin_omp_parallel ();
12100 c_parser_oacc_loop (loc, parser, p_name);
12101 stmt = c_finish_oacc_kernels (loc, clauses, block);
12102 OACC_KERNELS_COMBINED (stmt) = 1;
12103 return stmt;
12104 }
12105 }
12106
12107 clauses = c_parser_oacc_all_clauses (parser, OACC_KERNELS_CLAUSE_MASK,
12108 p_name);
12109
12110 block = c_begin_omp_parallel ();
12111 add_stmt (c_parser_omp_structured_block (parser));
12112
12113 stmt = c_finish_oacc_kernels (loc, clauses, block);
12114
12115 return stmt;
12116 }
12117
12118 /* OpenACC 2.0:
12119 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
12120
12121 or
12122
12123 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
12124
12125
12126 LOC is the location of the #pragma token.
12127 */
12128
12129 #define OACC_ENTER_DATA_CLAUSE_MASK \
12130 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
12131 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
12132 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
12133 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
12134 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
12135 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
12136 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
12137
12138 #define OACC_EXIT_DATA_CLAUSE_MASK \
12139 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
12140 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
12141 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
12142 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
12143 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
12144
12145 static void
12146 c_parser_oacc_enter_exit_data (c_parser *parser, bool enter)
12147 {
12148 location_t loc = c_parser_peek_token (parser)->location;
12149 tree clauses, stmt;
12150
12151 c_parser_consume_pragma (parser);
12152
12153 if (!c_parser_next_token_is (parser, CPP_NAME))
12154 {
12155 c_parser_error (parser, enter
12156 ? "expected %<data%> in %<#pragma acc enter data%>"
12157 : "expected %<data%> in %<#pragma acc exit data%>");
12158 c_parser_skip_to_pragma_eol (parser);
12159 return;
12160 }
12161
12162 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
12163 if (strcmp (p, "data") != 0)
12164 {
12165 c_parser_error (parser, "invalid pragma");
12166 c_parser_skip_to_pragma_eol (parser);
12167 return;
12168 }
12169
12170 c_parser_consume_token (parser);
12171
12172 if (enter)
12173 clauses = c_parser_oacc_all_clauses (parser, OACC_ENTER_DATA_CLAUSE_MASK,
12174 "#pragma acc enter data");
12175 else
12176 clauses = c_parser_oacc_all_clauses (parser, OACC_EXIT_DATA_CLAUSE_MASK,
12177 "#pragma acc exit data");
12178
12179 if (find_omp_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
12180 {
12181 error_at (loc, enter
12182 ? "%<#pragma acc enter data%> has no data movement clause"
12183 : "%<#pragma acc exit data%> has no data movement clause");
12184 return;
12185 }
12186
12187 stmt = enter ? make_node (OACC_ENTER_DATA) : make_node (OACC_EXIT_DATA);
12188 TREE_TYPE (stmt) = void_type_node;
12189 OMP_STANDALONE_CLAUSES (stmt) = clauses;
12190 SET_EXPR_LOCATION (stmt, loc);
12191 add_stmt (stmt);
12192 }
12193
12194
12195 /* OpenACC 2.0:
12196
12197 # pragma acc loop oacc-loop-clause[optseq] new-line
12198 structured-block
12199
12200 LOC is the location of the #pragma token.
12201 */
12202
12203 #define OACC_LOOP_CLAUSE_MASK \
12204 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
12205 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) )
12206
12207 static tree
12208 c_parser_oacc_loop (location_t loc, c_parser *parser, char *p_name)
12209 {
12210 tree stmt, clauses, block;
12211
12212 strcat (p_name, " loop");
12213
12214 clauses = c_parser_oacc_all_clauses (parser, OACC_LOOP_CLAUSE_MASK, p_name);
12215
12216 block = c_begin_compound_stmt (true);
12217 stmt = c_parser_omp_for_loop (loc, parser, OACC_LOOP, clauses, NULL);
12218 block = c_end_compound_stmt (loc, block, true);
12219 add_stmt (block);
12220
12221 return stmt;
12222 }
12223
12224 /* OpenACC 2.0:
12225 # pragma acc parallel oacc-parallel-clause[optseq] new-line
12226 structured-block
12227
12228 LOC is the location of the #pragma token.
12229 */
12230
12231 #define OACC_PARALLEL_CLAUSE_MASK \
12232 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
12233 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
12234 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
12235 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
12236 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
12237 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
12238 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
12239 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
12240 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
12241 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
12242 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
12243 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
12244 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
12245 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
12246 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
12247 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
12248 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
12249
12250 static tree
12251 c_parser_oacc_parallel (location_t loc, c_parser *parser, char *p_name)
12252 {
12253 tree stmt, clauses = NULL_TREE, block;
12254
12255 strcat (p_name, " parallel");
12256
12257 if (c_parser_next_token_is (parser, CPP_NAME))
12258 {
12259 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
12260 if (strcmp (p, "loop") == 0)
12261 {
12262 c_parser_consume_token (parser);
12263 block = c_begin_omp_parallel ();
12264 c_parser_oacc_loop (loc, parser, p_name);
12265 stmt = c_finish_oacc_parallel (loc, clauses, block);
12266 OACC_PARALLEL_COMBINED (stmt) = 1;
12267 return stmt;
12268 }
12269 }
12270
12271 clauses = c_parser_oacc_all_clauses (parser, OACC_PARALLEL_CLAUSE_MASK,
12272 p_name);
12273
12274 block = c_begin_omp_parallel ();
12275 add_stmt (c_parser_omp_structured_block (parser));
12276
12277 stmt = c_finish_oacc_parallel (loc, clauses, block);
12278
12279 return stmt;
12280 }
12281
12282 /* OpenACC 2.0:
12283 # pragma acc update oacc-update-clause[optseq] new-line
12284 */
12285
12286 #define OACC_UPDATE_CLAUSE_MASK \
12287 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
12288 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
12289 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
12290 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
12291 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SELF) \
12292 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
12293
12294 static void
12295 c_parser_oacc_update (c_parser *parser)
12296 {
12297 location_t loc = c_parser_peek_token (parser)->location;
12298
12299 c_parser_consume_pragma (parser);
12300
12301 tree clauses = c_parser_oacc_all_clauses (parser, OACC_UPDATE_CLAUSE_MASK,
12302 "#pragma acc update");
12303 if (find_omp_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
12304 {
12305 error_at (loc,
12306 "%<#pragma acc update%> must contain at least one "
12307 "%<device%> or %<host/self%> clause");
12308 return;
12309 }
12310
12311 if (parser->error)
12312 return;
12313
12314 tree stmt = make_node (OACC_UPDATE);
12315 TREE_TYPE (stmt) = void_type_node;
12316 OACC_UPDATE_CLAUSES (stmt) = clauses;
12317 SET_EXPR_LOCATION (stmt, loc);
12318 add_stmt (stmt);
12319 }
12320
12321 /* OpenACC 2.0:
12322 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
12323
12324 LOC is the location of the #pragma token.
12325 */
12326
12327 #define OACC_WAIT_CLAUSE_MASK \
12328 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) )
12329
12330 static tree
12331 c_parser_oacc_wait (location_t loc, c_parser *parser, char *p_name)
12332 {
12333 tree clauses, list = NULL_TREE, stmt = NULL_TREE;
12334
12335 if (c_parser_peek_token (parser)->type == CPP_OPEN_PAREN)
12336 list = c_parser_oacc_wait_list (parser, loc, list);
12337
12338 strcpy (p_name, " wait");
12339 clauses = c_parser_oacc_all_clauses (parser, OACC_WAIT_CLAUSE_MASK, p_name);
12340 stmt = c_finish_oacc_wait (loc, list, clauses);
12341
12342 return stmt;
12343 }
12344
12345 /* OpenMP 2.5:
12346 # pragma omp atomic new-line
12347 expression-stmt
12348
12349 expression-stmt:
12350 x binop= expr | x++ | ++x | x-- | --x
12351 binop:
12352 +, *, -, /, &, ^, |, <<, >>
12353
12354 where x is an lvalue expression with scalar type.
12355
12356 OpenMP 3.1:
12357 # pragma omp atomic new-line
12358 update-stmt
12359
12360 # pragma omp atomic read new-line
12361 read-stmt
12362
12363 # pragma omp atomic write new-line
12364 write-stmt
12365
12366 # pragma omp atomic update new-line
12367 update-stmt
12368
12369 # pragma omp atomic capture new-line
12370 capture-stmt
12371
12372 # pragma omp atomic capture new-line
12373 capture-block
12374
12375 read-stmt:
12376 v = x
12377 write-stmt:
12378 x = expr
12379 update-stmt:
12380 expression-stmt | x = x binop expr
12381 capture-stmt:
12382 v = expression-stmt
12383 capture-block:
12384 { v = x; update-stmt; } | { update-stmt; v = x; }
12385
12386 OpenMP 4.0:
12387 update-stmt:
12388 expression-stmt | x = x binop expr | x = expr binop x
12389 capture-stmt:
12390 v = update-stmt
12391 capture-block:
12392 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
12393
12394 where x and v are lvalue expressions with scalar type.
12395
12396 LOC is the location of the #pragma token. */
12397
12398 static void
12399 c_parser_omp_atomic (location_t loc, c_parser *parser)
12400 {
12401 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE;
12402 tree lhs1 = NULL_TREE, rhs1 = NULL_TREE;
12403 tree stmt, orig_lhs, unfolded_lhs = NULL_TREE, unfolded_lhs1 = NULL_TREE;
12404 enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
12405 struct c_expr expr;
12406 location_t eloc;
12407 bool structured_block = false;
12408 bool swapped = false;
12409 bool seq_cst = false;
12410
12411 if (c_parser_next_token_is (parser, CPP_NAME))
12412 {
12413 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
12414 if (!strcmp (p, "seq_cst"))
12415 {
12416 seq_cst = true;
12417 c_parser_consume_token (parser);
12418 if (c_parser_next_token_is (parser, CPP_COMMA)
12419 && c_parser_peek_2nd_token (parser)->type == CPP_NAME)
12420 c_parser_consume_token (parser);
12421 }
12422 }
12423 if (c_parser_next_token_is (parser, CPP_NAME))
12424 {
12425 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
12426
12427 if (!strcmp (p, "read"))
12428 code = OMP_ATOMIC_READ;
12429 else if (!strcmp (p, "write"))
12430 code = NOP_EXPR;
12431 else if (!strcmp (p, "update"))
12432 code = OMP_ATOMIC;
12433 else if (!strcmp (p, "capture"))
12434 code = OMP_ATOMIC_CAPTURE_NEW;
12435 else
12436 p = NULL;
12437 if (p)
12438 c_parser_consume_token (parser);
12439 }
12440 if (!seq_cst)
12441 {
12442 if (c_parser_next_token_is (parser, CPP_COMMA)
12443 && c_parser_peek_2nd_token (parser)->type == CPP_NAME)
12444 c_parser_consume_token (parser);
12445
12446 if (c_parser_next_token_is (parser, CPP_NAME))
12447 {
12448 const char *p
12449 = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
12450 if (!strcmp (p, "seq_cst"))
12451 {
12452 seq_cst = true;
12453 c_parser_consume_token (parser);
12454 }
12455 }
12456 }
12457 c_parser_skip_to_pragma_eol (parser);
12458
12459 switch (code)
12460 {
12461 case OMP_ATOMIC_READ:
12462 case NOP_EXPR: /* atomic write */
12463 v = c_parser_unary_expression (parser).value;
12464 v = c_fully_fold (v, false, NULL);
12465 if (v == error_mark_node)
12466 goto saw_error;
12467 loc = c_parser_peek_token (parser)->location;
12468 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
12469 goto saw_error;
12470 if (code == NOP_EXPR)
12471 lhs = c_parser_expression (parser).value;
12472 else
12473 lhs = c_parser_unary_expression (parser).value;
12474 lhs = c_fully_fold (lhs, false, NULL);
12475 if (lhs == error_mark_node)
12476 goto saw_error;
12477 if (code == NOP_EXPR)
12478 {
12479 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
12480 opcode. */
12481 code = OMP_ATOMIC;
12482 rhs = lhs;
12483 lhs = v;
12484 v = NULL_TREE;
12485 }
12486 goto done;
12487 case OMP_ATOMIC_CAPTURE_NEW:
12488 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
12489 {
12490 c_parser_consume_token (parser);
12491 structured_block = true;
12492 }
12493 else
12494 {
12495 v = c_parser_unary_expression (parser).value;
12496 v = c_fully_fold (v, false, NULL);
12497 if (v == error_mark_node)
12498 goto saw_error;
12499 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
12500 goto saw_error;
12501 }
12502 break;
12503 default:
12504 break;
12505 }
12506
12507 /* For structured_block case we don't know yet whether
12508 old or new x should be captured. */
12509 restart:
12510 eloc = c_parser_peek_token (parser)->location;
12511 expr = c_parser_unary_expression (parser);
12512 lhs = expr.value;
12513 expr = default_function_array_conversion (eloc, expr);
12514 unfolded_lhs = expr.value;
12515 lhs = c_fully_fold (lhs, false, NULL);
12516 orig_lhs = lhs;
12517 switch (TREE_CODE (lhs))
12518 {
12519 case ERROR_MARK:
12520 saw_error:
12521 c_parser_skip_to_end_of_block_or_statement (parser);
12522 if (structured_block)
12523 {
12524 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
12525 c_parser_consume_token (parser);
12526 else if (code == OMP_ATOMIC_CAPTURE_NEW)
12527 {
12528 c_parser_skip_to_end_of_block_or_statement (parser);
12529 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
12530 c_parser_consume_token (parser);
12531 }
12532 }
12533 return;
12534
12535 case POSTINCREMENT_EXPR:
12536 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
12537 code = OMP_ATOMIC_CAPTURE_OLD;
12538 /* FALLTHROUGH */
12539 case PREINCREMENT_EXPR:
12540 lhs = TREE_OPERAND (lhs, 0);
12541 unfolded_lhs = NULL_TREE;
12542 opcode = PLUS_EXPR;
12543 rhs = integer_one_node;
12544 break;
12545
12546 case POSTDECREMENT_EXPR:
12547 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
12548 code = OMP_ATOMIC_CAPTURE_OLD;
12549 /* FALLTHROUGH */
12550 case PREDECREMENT_EXPR:
12551 lhs = TREE_OPERAND (lhs, 0);
12552 unfolded_lhs = NULL_TREE;
12553 opcode = MINUS_EXPR;
12554 rhs = integer_one_node;
12555 break;
12556
12557 case COMPOUND_EXPR:
12558 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
12559 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
12560 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
12561 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
12562 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
12563 (TREE_OPERAND (lhs, 1), 0), 0)))
12564 == BOOLEAN_TYPE)
12565 /* Undo effects of boolean_increment for post {in,de}crement. */
12566 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
12567 /* FALLTHRU */
12568 case MODIFY_EXPR:
12569 if (TREE_CODE (lhs) == MODIFY_EXPR
12570 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
12571 {
12572 /* Undo effects of boolean_increment. */
12573 if (integer_onep (TREE_OPERAND (lhs, 1)))
12574 {
12575 /* This is pre or post increment. */
12576 rhs = TREE_OPERAND (lhs, 1);
12577 lhs = TREE_OPERAND (lhs, 0);
12578 unfolded_lhs = NULL_TREE;
12579 opcode = NOP_EXPR;
12580 if (code == OMP_ATOMIC_CAPTURE_NEW
12581 && !structured_block
12582 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
12583 code = OMP_ATOMIC_CAPTURE_OLD;
12584 break;
12585 }
12586 if (TREE_CODE (TREE_OPERAND (lhs, 1)) == TRUTH_NOT_EXPR
12587 && TREE_OPERAND (lhs, 0)
12588 == TREE_OPERAND (TREE_OPERAND (lhs, 1), 0))
12589 {
12590 /* This is pre or post decrement. */
12591 rhs = TREE_OPERAND (lhs, 1);
12592 lhs = TREE_OPERAND (lhs, 0);
12593 unfolded_lhs = NULL_TREE;
12594 opcode = NOP_EXPR;
12595 if (code == OMP_ATOMIC_CAPTURE_NEW
12596 && !structured_block
12597 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
12598 code = OMP_ATOMIC_CAPTURE_OLD;
12599 break;
12600 }
12601 }
12602 /* FALLTHRU */
12603 default:
12604 switch (c_parser_peek_token (parser)->type)
12605 {
12606 case CPP_MULT_EQ:
12607 opcode = MULT_EXPR;
12608 break;
12609 case CPP_DIV_EQ:
12610 opcode = TRUNC_DIV_EXPR;
12611 break;
12612 case CPP_PLUS_EQ:
12613 opcode = PLUS_EXPR;
12614 break;
12615 case CPP_MINUS_EQ:
12616 opcode = MINUS_EXPR;
12617 break;
12618 case CPP_LSHIFT_EQ:
12619 opcode = LSHIFT_EXPR;
12620 break;
12621 case CPP_RSHIFT_EQ:
12622 opcode = RSHIFT_EXPR;
12623 break;
12624 case CPP_AND_EQ:
12625 opcode = BIT_AND_EXPR;
12626 break;
12627 case CPP_OR_EQ:
12628 opcode = BIT_IOR_EXPR;
12629 break;
12630 case CPP_XOR_EQ:
12631 opcode = BIT_XOR_EXPR;
12632 break;
12633 case CPP_EQ:
12634 c_parser_consume_token (parser);
12635 eloc = c_parser_peek_token (parser)->location;
12636 expr = c_parser_expr_no_commas (parser, NULL, unfolded_lhs);
12637 rhs1 = expr.value;
12638 switch (TREE_CODE (rhs1))
12639 {
12640 case MULT_EXPR:
12641 case TRUNC_DIV_EXPR:
12642 case RDIV_EXPR:
12643 case PLUS_EXPR:
12644 case MINUS_EXPR:
12645 case LSHIFT_EXPR:
12646 case RSHIFT_EXPR:
12647 case BIT_AND_EXPR:
12648 case BIT_IOR_EXPR:
12649 case BIT_XOR_EXPR:
12650 if (c_tree_equal (TREE_OPERAND (rhs1, 0), unfolded_lhs))
12651 {
12652 opcode = TREE_CODE (rhs1);
12653 rhs = c_fully_fold (TREE_OPERAND (rhs1, 1), false, NULL);
12654 rhs1 = c_fully_fold (TREE_OPERAND (rhs1, 0), false, NULL);
12655 goto stmt_done;
12656 }
12657 if (c_tree_equal (TREE_OPERAND (rhs1, 1), unfolded_lhs))
12658 {
12659 opcode = TREE_CODE (rhs1);
12660 rhs = c_fully_fold (TREE_OPERAND (rhs1, 0), false, NULL);
12661 rhs1 = c_fully_fold (TREE_OPERAND (rhs1, 1), false, NULL);
12662 swapped = !commutative_tree_code (opcode);
12663 goto stmt_done;
12664 }
12665 break;
12666 case ERROR_MARK:
12667 goto saw_error;
12668 default:
12669 break;
12670 }
12671 if (c_parser_peek_token (parser)->type == CPP_SEMICOLON)
12672 {
12673 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
12674 {
12675 code = OMP_ATOMIC_CAPTURE_OLD;
12676 v = lhs;
12677 lhs = NULL_TREE;
12678 expr = default_function_array_read_conversion (eloc, expr);
12679 unfolded_lhs1 = expr.value;
12680 lhs1 = c_fully_fold (unfolded_lhs1, false, NULL);
12681 rhs1 = NULL_TREE;
12682 c_parser_consume_token (parser);
12683 goto restart;
12684 }
12685 if (structured_block)
12686 {
12687 opcode = NOP_EXPR;
12688 expr = default_function_array_read_conversion (eloc, expr);
12689 rhs = c_fully_fold (expr.value, false, NULL);
12690 rhs1 = NULL_TREE;
12691 goto stmt_done;
12692 }
12693 }
12694 c_parser_error (parser, "invalid form of %<#pragma omp atomic%>");
12695 goto saw_error;
12696 default:
12697 c_parser_error (parser,
12698 "invalid operator for %<#pragma omp atomic%>");
12699 goto saw_error;
12700 }
12701
12702 /* Arrange to pass the location of the assignment operator to
12703 c_finish_omp_atomic. */
12704 loc = c_parser_peek_token (parser)->location;
12705 c_parser_consume_token (parser);
12706 eloc = c_parser_peek_token (parser)->location;
12707 expr = c_parser_expression (parser);
12708 expr = default_function_array_read_conversion (eloc, expr);
12709 rhs = expr.value;
12710 rhs = c_fully_fold (rhs, false, NULL);
12711 break;
12712 }
12713 stmt_done:
12714 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
12715 {
12716 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
12717 goto saw_error;
12718 v = c_parser_unary_expression (parser).value;
12719 v = c_fully_fold (v, false, NULL);
12720 if (v == error_mark_node)
12721 goto saw_error;
12722 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
12723 goto saw_error;
12724 eloc = c_parser_peek_token (parser)->location;
12725 expr = c_parser_unary_expression (parser);
12726 lhs1 = expr.value;
12727 expr = default_function_array_read_conversion (eloc, expr);
12728 unfolded_lhs1 = expr.value;
12729 lhs1 = c_fully_fold (lhs1, false, NULL);
12730 if (lhs1 == error_mark_node)
12731 goto saw_error;
12732 }
12733 if (structured_block)
12734 {
12735 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
12736 c_parser_require (parser, CPP_CLOSE_BRACE, "expected %<}%>");
12737 }
12738 done:
12739 if (unfolded_lhs && unfolded_lhs1
12740 && !c_tree_equal (unfolded_lhs, unfolded_lhs1))
12741 {
12742 error ("%<#pragma omp atomic capture%> uses two different "
12743 "expressions for memory");
12744 stmt = error_mark_node;
12745 }
12746 else
12747 stmt = c_finish_omp_atomic (loc, code, opcode, lhs, rhs, v, lhs1, rhs1,
12748 swapped, seq_cst);
12749 if (stmt != error_mark_node)
12750 add_stmt (stmt);
12751
12752 if (!structured_block)
12753 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
12754 }
12755
12756
12757 /* OpenMP 2.5:
12758 # pragma omp barrier new-line
12759 */
12760
12761 static void
12762 c_parser_omp_barrier (c_parser *parser)
12763 {
12764 location_t loc = c_parser_peek_token (parser)->location;
12765 c_parser_consume_pragma (parser);
12766 c_parser_skip_to_pragma_eol (parser);
12767
12768 c_finish_omp_barrier (loc);
12769 }
12770
12771 /* OpenMP 2.5:
12772 # pragma omp critical [(name)] new-line
12773 structured-block
12774
12775 LOC is the location of the #pragma itself. */
12776
12777 static tree
12778 c_parser_omp_critical (location_t loc, c_parser *parser)
12779 {
12780 tree stmt, name = NULL;
12781
12782 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
12783 {
12784 c_parser_consume_token (parser);
12785 if (c_parser_next_token_is (parser, CPP_NAME))
12786 {
12787 name = c_parser_peek_token (parser)->value;
12788 c_parser_consume_token (parser);
12789 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12790 }
12791 else
12792 c_parser_error (parser, "expected identifier");
12793 }
12794 else if (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
12795 c_parser_error (parser, "expected %<(%> or end of line");
12796 c_parser_skip_to_pragma_eol (parser);
12797
12798 stmt = c_parser_omp_structured_block (parser);
12799 return c_finish_omp_critical (loc, stmt, name);
12800 }
12801
12802 /* OpenMP 2.5:
12803 # pragma omp flush flush-vars[opt] new-line
12804
12805 flush-vars:
12806 ( variable-list ) */
12807
12808 static void
12809 c_parser_omp_flush (c_parser *parser)
12810 {
12811 location_t loc = c_parser_peek_token (parser)->location;
12812 c_parser_consume_pragma (parser);
12813 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
12814 c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
12815 else if (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
12816 c_parser_error (parser, "expected %<(%> or end of line");
12817 c_parser_skip_to_pragma_eol (parser);
12818
12819 c_finish_omp_flush (loc);
12820 }
12821
12822 /* Parse the restricted form of loop statements allowed by OpenACC and OpenMP.
12823 The real trick here is to determine the loop control variable early
12824 so that we can push a new decl if necessary to make it private.
12825 LOC is the location of the "acc" or "omp" in "#pragma acc" or "#pragma omp",
12826 respectively. */
12827
12828 static tree
12829 c_parser_omp_for_loop (location_t loc, c_parser *parser, enum tree_code code,
12830 tree clauses, tree *cclauses)
12831 {
12832 tree decl, cond, incr, save_break, save_cont, body, init, stmt, cl;
12833 tree declv, condv, incrv, initv, ret = NULL;
12834 bool fail = false, open_brace_parsed = false;
12835 int i, collapse = 1, nbraces = 0;
12836 location_t for_loc;
12837 vec<tree, va_gc> *for_block = make_tree_vector ();
12838
12839 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
12840 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
12841 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
12842
12843 gcc_assert (collapse >= 1);
12844
12845 declv = make_tree_vec (collapse);
12846 initv = make_tree_vec (collapse);
12847 condv = make_tree_vec (collapse);
12848 incrv = make_tree_vec (collapse);
12849
12850 if (code != CILK_FOR
12851 && !c_parser_next_token_is_keyword (parser, RID_FOR))
12852 {
12853 c_parser_error (parser, "for statement expected");
12854 return NULL;
12855 }
12856 if (code == CILK_FOR
12857 && !c_parser_next_token_is_keyword (parser, RID_CILK_FOR))
12858 {
12859 c_parser_error (parser, "_Cilk_for statement expected");
12860 return NULL;
12861 }
12862 for_loc = c_parser_peek_token (parser)->location;
12863 c_parser_consume_token (parser);
12864
12865 for (i = 0; i < collapse; i++)
12866 {
12867 int bracecount = 0;
12868
12869 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
12870 goto pop_scopes;
12871
12872 /* Parse the initialization declaration or expression. */
12873 if (c_parser_next_tokens_start_declaration (parser))
12874 {
12875 if (i > 0)
12876 vec_safe_push (for_block, c_begin_compound_stmt (true));
12877 c_parser_declaration_or_fndef (parser, true, true, true, true, true,
12878 NULL, vNULL);
12879 decl = check_for_loop_decls (for_loc, flag_isoc99);
12880 if (decl == NULL)
12881 goto error_init;
12882 if (DECL_INITIAL (decl) == error_mark_node)
12883 decl = error_mark_node;
12884 init = decl;
12885 }
12886 else if (c_parser_next_token_is (parser, CPP_NAME)
12887 && c_parser_peek_2nd_token (parser)->type == CPP_EQ)
12888 {
12889 struct c_expr decl_exp;
12890 struct c_expr init_exp;
12891 location_t init_loc;
12892
12893 decl_exp = c_parser_postfix_expression (parser);
12894 decl = decl_exp.value;
12895
12896 c_parser_require (parser, CPP_EQ, "expected %<=%>");
12897
12898 init_loc = c_parser_peek_token (parser)->location;
12899 init_exp = c_parser_expr_no_commas (parser, NULL);
12900 init_exp = default_function_array_read_conversion (init_loc,
12901 init_exp);
12902 init = build_modify_expr (init_loc, decl, decl_exp.original_type,
12903 NOP_EXPR, init_loc, init_exp.value,
12904 init_exp.original_type);
12905 init = c_process_expr_stmt (init_loc, init);
12906
12907 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
12908 }
12909 else
12910 {
12911 error_init:
12912 c_parser_error (parser,
12913 "expected iteration declaration or initialization");
12914 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
12915 "expected %<)%>");
12916 fail = true;
12917 goto parse_next;
12918 }
12919
12920 /* Parse the loop condition. */
12921 cond = NULL_TREE;
12922 if (c_parser_next_token_is_not (parser, CPP_SEMICOLON))
12923 {
12924 location_t cond_loc = c_parser_peek_token (parser)->location;
12925 struct c_expr cond_expr
12926 = c_parser_binary_expression (parser, NULL, NULL_TREE);
12927
12928 cond = cond_expr.value;
12929 cond = c_objc_common_truthvalue_conversion (cond_loc, cond);
12930 cond = c_fully_fold (cond, false, NULL);
12931 switch (cond_expr.original_code)
12932 {
12933 case GT_EXPR:
12934 case GE_EXPR:
12935 case LT_EXPR:
12936 case LE_EXPR:
12937 break;
12938 case NE_EXPR:
12939 if (code == CILK_SIMD || code == CILK_FOR)
12940 break;
12941 /* FALLTHRU. */
12942 default:
12943 /* Can't be cond = error_mark_node, because we want to preserve
12944 the location until c_finish_omp_for. */
12945 cond = build1 (NOP_EXPR, boolean_type_node, error_mark_node);
12946 break;
12947 }
12948 protected_set_expr_location (cond, cond_loc);
12949 }
12950 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
12951
12952 /* Parse the increment expression. */
12953 incr = NULL_TREE;
12954 if (c_parser_next_token_is_not (parser, CPP_CLOSE_PAREN))
12955 {
12956 location_t incr_loc = c_parser_peek_token (parser)->location;
12957
12958 incr = c_process_expr_stmt (incr_loc,
12959 c_parser_expression (parser).value);
12960 }
12961 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12962
12963 if (decl == NULL || decl == error_mark_node || init == error_mark_node)
12964 fail = true;
12965 else
12966 {
12967 TREE_VEC_ELT (declv, i) = decl;
12968 TREE_VEC_ELT (initv, i) = init;
12969 TREE_VEC_ELT (condv, i) = cond;
12970 TREE_VEC_ELT (incrv, i) = incr;
12971 }
12972
12973 parse_next:
12974 if (i == collapse - 1)
12975 break;
12976
12977 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
12978 in between the collapsed for loops to be still considered perfectly
12979 nested. Hopefully the final version clarifies this.
12980 For now handle (multiple) {'s and empty statements. */
12981 do
12982 {
12983 if (c_parser_next_token_is_keyword (parser, RID_FOR))
12984 {
12985 c_parser_consume_token (parser);
12986 break;
12987 }
12988 else if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
12989 {
12990 c_parser_consume_token (parser);
12991 bracecount++;
12992 }
12993 else if (bracecount
12994 && c_parser_next_token_is (parser, CPP_SEMICOLON))
12995 c_parser_consume_token (parser);
12996 else
12997 {
12998 c_parser_error (parser, "not enough perfectly nested loops");
12999 if (bracecount)
13000 {
13001 open_brace_parsed = true;
13002 bracecount--;
13003 }
13004 fail = true;
13005 collapse = 0;
13006 break;
13007 }
13008 }
13009 while (1);
13010
13011 nbraces += bracecount;
13012 }
13013
13014 save_break = c_break_label;
13015 if (code == CILK_SIMD)
13016 c_break_label = build_int_cst (size_type_node, 2);
13017 else
13018 c_break_label = size_one_node;
13019 save_cont = c_cont_label;
13020 c_cont_label = NULL_TREE;
13021 body = push_stmt_list ();
13022
13023 if (open_brace_parsed)
13024 {
13025 location_t here = c_parser_peek_token (parser)->location;
13026 stmt = c_begin_compound_stmt (true);
13027 c_parser_compound_statement_nostart (parser);
13028 add_stmt (c_end_compound_stmt (here, stmt, true));
13029 }
13030 else
13031 add_stmt (c_parser_c99_block_statement (parser));
13032 if (c_cont_label)
13033 {
13034 tree t = build1 (LABEL_EXPR, void_type_node, c_cont_label);
13035 SET_EXPR_LOCATION (t, loc);
13036 add_stmt (t);
13037 }
13038
13039 body = pop_stmt_list (body);
13040 c_break_label = save_break;
13041 c_cont_label = save_cont;
13042
13043 while (nbraces)
13044 {
13045 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
13046 {
13047 c_parser_consume_token (parser);
13048 nbraces--;
13049 }
13050 else if (c_parser_next_token_is (parser, CPP_SEMICOLON))
13051 c_parser_consume_token (parser);
13052 else
13053 {
13054 c_parser_error (parser, "collapsed loops not perfectly nested");
13055 while (nbraces)
13056 {
13057 location_t here = c_parser_peek_token (parser)->location;
13058 stmt = c_begin_compound_stmt (true);
13059 add_stmt (body);
13060 c_parser_compound_statement_nostart (parser);
13061 body = c_end_compound_stmt (here, stmt, true);
13062 nbraces--;
13063 }
13064 goto pop_scopes;
13065 }
13066 }
13067
13068 /* Only bother calling c_finish_omp_for if we haven't already generated
13069 an error from the initialization parsing. */
13070 if (!fail)
13071 {
13072 stmt = c_finish_omp_for (loc, code, declv, initv, condv,
13073 incrv, body, NULL);
13074 if (stmt)
13075 {
13076 if (cclauses != NULL
13077 && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL)
13078 {
13079 tree *c;
13080 for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
13081 if (OMP_CLAUSE_CODE (*c) != OMP_CLAUSE_FIRSTPRIVATE
13082 && OMP_CLAUSE_CODE (*c) != OMP_CLAUSE_LASTPRIVATE)
13083 c = &OMP_CLAUSE_CHAIN (*c);
13084 else
13085 {
13086 for (i = 0; i < collapse; i++)
13087 if (TREE_VEC_ELT (declv, i) == OMP_CLAUSE_DECL (*c))
13088 break;
13089 if (i == collapse)
13090 c = &OMP_CLAUSE_CHAIN (*c);
13091 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE)
13092 {
13093 error_at (loc,
13094 "iteration variable %qD should not be firstprivate",
13095 OMP_CLAUSE_DECL (*c));
13096 *c = OMP_CLAUSE_CHAIN (*c);
13097 }
13098 else
13099 {
13100 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
13101 tree l = *c;
13102 *c = OMP_CLAUSE_CHAIN (*c);
13103 if (code == OMP_SIMD)
13104 {
13105 OMP_CLAUSE_CHAIN (l)
13106 = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
13107 cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
13108 }
13109 else
13110 {
13111 OMP_CLAUSE_CHAIN (l) = clauses;
13112 clauses = l;
13113 }
13114 }
13115 }
13116 }
13117 OMP_FOR_CLAUSES (stmt) = clauses;
13118 }
13119 ret = stmt;
13120 }
13121 pop_scopes:
13122 while (!for_block->is_empty ())
13123 {
13124 /* FIXME diagnostics: LOC below should be the actual location of
13125 this particular for block. We need to build a list of
13126 locations to go along with FOR_BLOCK. */
13127 stmt = c_end_compound_stmt (loc, for_block->pop (), true);
13128 add_stmt (stmt);
13129 }
13130 release_tree_vector (for_block);
13131 return ret;
13132 }
13133
13134 /* Helper function for OpenMP parsing, split clauses and call
13135 finish_omp_clauses on each of the set of clauses afterwards. */
13136
13137 static void
13138 omp_split_clauses (location_t loc, enum tree_code code,
13139 omp_clause_mask mask, tree clauses, tree *cclauses)
13140 {
13141 int i;
13142 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
13143 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
13144 if (cclauses[i])
13145 cclauses[i] = c_finish_omp_clauses (cclauses[i]);
13146 }
13147
13148 /* OpenMP 4.0:
13149 #pragma omp simd simd-clause[optseq] new-line
13150 for-loop
13151
13152 LOC is the location of the #pragma token.
13153 */
13154
13155 #define OMP_SIMD_CLAUSE_MASK \
13156 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
13157 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
13158 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
13159 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
13160 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
13161 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
13162 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
13163
13164 static tree
13165 c_parser_omp_simd (location_t loc, c_parser *parser,
13166 char *p_name, omp_clause_mask mask, tree *cclauses)
13167 {
13168 tree block, clauses, ret;
13169
13170 strcat (p_name, " simd");
13171 mask |= OMP_SIMD_CLAUSE_MASK;
13172 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
13173
13174 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
13175 if (cclauses)
13176 {
13177 omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
13178 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
13179 }
13180
13181 block = c_begin_compound_stmt (true);
13182 ret = c_parser_omp_for_loop (loc, parser, OMP_SIMD, clauses, cclauses);
13183 block = c_end_compound_stmt (loc, block, true);
13184 add_stmt (block);
13185
13186 return ret;
13187 }
13188
13189 /* OpenMP 2.5:
13190 #pragma omp for for-clause[optseq] new-line
13191 for-loop
13192
13193 OpenMP 4.0:
13194 #pragma omp for simd for-simd-clause[optseq] new-line
13195 for-loop
13196
13197 LOC is the location of the #pragma token.
13198 */
13199
13200 #define OMP_FOR_CLAUSE_MASK \
13201 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
13202 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
13203 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
13204 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
13205 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
13206 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
13207 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
13208 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
13209
13210 static tree
13211 c_parser_omp_for (location_t loc, c_parser *parser,
13212 char *p_name, omp_clause_mask mask, tree *cclauses)
13213 {
13214 tree block, clauses, ret;
13215
13216 strcat (p_name, " for");
13217 mask |= OMP_FOR_CLAUSE_MASK;
13218 if (cclauses)
13219 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
13220
13221 if (c_parser_next_token_is (parser, CPP_NAME))
13222 {
13223 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
13224
13225 if (strcmp (p, "simd") == 0)
13226 {
13227 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
13228 if (cclauses == NULL)
13229 cclauses = cclauses_buf;
13230
13231 c_parser_consume_token (parser);
13232 if (!flag_openmp) /* flag_openmp_simd */
13233 return c_parser_omp_simd (loc, parser, p_name, mask, cclauses);
13234 block = c_begin_compound_stmt (true);
13235 ret = c_parser_omp_simd (loc, parser, p_name, mask, cclauses);
13236 block = c_end_compound_stmt (loc, block, true);
13237 if (ret == NULL_TREE)
13238 return ret;
13239 ret = make_node (OMP_FOR);
13240 TREE_TYPE (ret) = void_type_node;
13241 OMP_FOR_BODY (ret) = block;
13242 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
13243 SET_EXPR_LOCATION (ret, loc);
13244 add_stmt (ret);
13245 return ret;
13246 }
13247 }
13248 if (!flag_openmp) /* flag_openmp_simd */
13249 {
13250 c_parser_skip_to_pragma_eol (parser, false);
13251 return NULL_TREE;
13252 }
13253
13254 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
13255 if (cclauses)
13256 {
13257 omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
13258 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
13259 }
13260
13261 block = c_begin_compound_stmt (true);
13262 ret = c_parser_omp_for_loop (loc, parser, OMP_FOR, clauses, cclauses);
13263 block = c_end_compound_stmt (loc, block, true);
13264 add_stmt (block);
13265
13266 return ret;
13267 }
13268
13269 /* OpenMP 2.5:
13270 # pragma omp master new-line
13271 structured-block
13272
13273 LOC is the location of the #pragma token.
13274 */
13275
13276 static tree
13277 c_parser_omp_master (location_t loc, c_parser *parser)
13278 {
13279 c_parser_skip_to_pragma_eol (parser);
13280 return c_finish_omp_master (loc, c_parser_omp_structured_block (parser));
13281 }
13282
13283 /* OpenMP 2.5:
13284 # pragma omp ordered new-line
13285 structured-block
13286
13287 LOC is the location of the #pragma itself.
13288 */
13289
13290 static tree
13291 c_parser_omp_ordered (location_t loc, c_parser *parser)
13292 {
13293 c_parser_skip_to_pragma_eol (parser);
13294 return c_finish_omp_ordered (loc, c_parser_omp_structured_block (parser));
13295 }
13296
13297 /* OpenMP 2.5:
13298
13299 section-scope:
13300 { section-sequence }
13301
13302 section-sequence:
13303 section-directive[opt] structured-block
13304 section-sequence section-directive structured-block
13305
13306 SECTIONS_LOC is the location of the #pragma omp sections. */
13307
13308 static tree
13309 c_parser_omp_sections_scope (location_t sections_loc, c_parser *parser)
13310 {
13311 tree stmt, substmt;
13312 bool error_suppress = false;
13313 location_t loc;
13314
13315 loc = c_parser_peek_token (parser)->location;
13316 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
13317 {
13318 /* Avoid skipping until the end of the block. */
13319 parser->error = false;
13320 return NULL_TREE;
13321 }
13322
13323 stmt = push_stmt_list ();
13324
13325 if (c_parser_peek_token (parser)->pragma_kind != PRAGMA_OMP_SECTION)
13326 {
13327 substmt = c_parser_omp_structured_block (parser);
13328 substmt = build1 (OMP_SECTION, void_type_node, substmt);
13329 SET_EXPR_LOCATION (substmt, loc);
13330 add_stmt (substmt);
13331 }
13332
13333 while (1)
13334 {
13335 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
13336 break;
13337 if (c_parser_next_token_is (parser, CPP_EOF))
13338 break;
13339
13340 loc = c_parser_peek_token (parser)->location;
13341 if (c_parser_peek_token (parser)->pragma_kind == PRAGMA_OMP_SECTION)
13342 {
13343 c_parser_consume_pragma (parser);
13344 c_parser_skip_to_pragma_eol (parser);
13345 error_suppress = false;
13346 }
13347 else if (!error_suppress)
13348 {
13349 error_at (loc, "expected %<#pragma omp section%> or %<}%>");
13350 error_suppress = true;
13351 }
13352
13353 substmt = c_parser_omp_structured_block (parser);
13354 substmt = build1 (OMP_SECTION, void_type_node, substmt);
13355 SET_EXPR_LOCATION (substmt, loc);
13356 add_stmt (substmt);
13357 }
13358 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE,
13359 "expected %<#pragma omp section%> or %<}%>");
13360
13361 substmt = pop_stmt_list (stmt);
13362
13363 stmt = make_node (OMP_SECTIONS);
13364 SET_EXPR_LOCATION (stmt, sections_loc);
13365 TREE_TYPE (stmt) = void_type_node;
13366 OMP_SECTIONS_BODY (stmt) = substmt;
13367
13368 return add_stmt (stmt);
13369 }
13370
13371 /* OpenMP 2.5:
13372 # pragma omp sections sections-clause[optseq] newline
13373 sections-scope
13374
13375 LOC is the location of the #pragma token.
13376 */
13377
13378 #define OMP_SECTIONS_CLAUSE_MASK \
13379 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
13380 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
13381 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
13382 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
13383 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
13384
13385 static tree
13386 c_parser_omp_sections (location_t loc, c_parser *parser,
13387 char *p_name, omp_clause_mask mask, tree *cclauses)
13388 {
13389 tree block, clauses, ret;
13390
13391 strcat (p_name, " sections");
13392 mask |= OMP_SECTIONS_CLAUSE_MASK;
13393 if (cclauses)
13394 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
13395
13396 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
13397 if (cclauses)
13398 {
13399 omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
13400 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
13401 }
13402
13403 block = c_begin_compound_stmt (true);
13404 ret = c_parser_omp_sections_scope (loc, parser);
13405 if (ret)
13406 OMP_SECTIONS_CLAUSES (ret) = clauses;
13407 block = c_end_compound_stmt (loc, block, true);
13408 add_stmt (block);
13409
13410 return ret;
13411 }
13412
13413 /* OpenMP 2.5:
13414 # pragma omp parallel parallel-clause[optseq] new-line
13415 structured-block
13416 # pragma omp parallel for parallel-for-clause[optseq] new-line
13417 structured-block
13418 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
13419 structured-block
13420
13421 OpenMP 4.0:
13422 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
13423 structured-block
13424
13425 LOC is the location of the #pragma token.
13426 */
13427
13428 #define OMP_PARALLEL_CLAUSE_MASK \
13429 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
13430 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
13431 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
13432 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
13433 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
13434 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
13435 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
13436 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
13437 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
13438
13439 static tree
13440 c_parser_omp_parallel (location_t loc, c_parser *parser,
13441 char *p_name, omp_clause_mask mask, tree *cclauses)
13442 {
13443 tree stmt, clauses, block;
13444
13445 strcat (p_name, " parallel");
13446 mask |= OMP_PARALLEL_CLAUSE_MASK;
13447
13448 if (c_parser_next_token_is_keyword (parser, RID_FOR))
13449 {
13450 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
13451 if (cclauses == NULL)
13452 cclauses = cclauses_buf;
13453
13454 c_parser_consume_token (parser);
13455 if (!flag_openmp) /* flag_openmp_simd */
13456 return c_parser_omp_for (loc, parser, p_name, mask, cclauses);
13457 block = c_begin_omp_parallel ();
13458 tree ret = c_parser_omp_for (loc, parser, p_name, mask, cclauses);
13459 stmt
13460 = c_finish_omp_parallel (loc, cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
13461 block);
13462 if (ret == NULL_TREE)
13463 return ret;
13464 OMP_PARALLEL_COMBINED (stmt) = 1;
13465 return stmt;
13466 }
13467 else if (cclauses)
13468 {
13469 error_at (loc, "expected %<for%> after %qs", p_name);
13470 c_parser_skip_to_pragma_eol (parser);
13471 return NULL_TREE;
13472 }
13473 else if (!flag_openmp) /* flag_openmp_simd */
13474 {
13475 c_parser_skip_to_pragma_eol (parser, false);
13476 return NULL_TREE;
13477 }
13478 else if (c_parser_next_token_is (parser, CPP_NAME))
13479 {
13480 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
13481 if (strcmp (p, "sections") == 0)
13482 {
13483 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
13484 if (cclauses == NULL)
13485 cclauses = cclauses_buf;
13486
13487 c_parser_consume_token (parser);
13488 block = c_begin_omp_parallel ();
13489 c_parser_omp_sections (loc, parser, p_name, mask, cclauses);
13490 stmt = c_finish_omp_parallel (loc,
13491 cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
13492 block);
13493 OMP_PARALLEL_COMBINED (stmt) = 1;
13494 return stmt;
13495 }
13496 }
13497
13498 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
13499
13500 block = c_begin_omp_parallel ();
13501 c_parser_statement (parser);
13502 stmt = c_finish_omp_parallel (loc, clauses, block);
13503
13504 return stmt;
13505 }
13506
13507 /* OpenMP 2.5:
13508 # pragma omp single single-clause[optseq] new-line
13509 structured-block
13510
13511 LOC is the location of the #pragma.
13512 */
13513
13514 #define OMP_SINGLE_CLAUSE_MASK \
13515 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
13516 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
13517 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
13518 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
13519
13520 static tree
13521 c_parser_omp_single (location_t loc, c_parser *parser)
13522 {
13523 tree stmt = make_node (OMP_SINGLE);
13524 SET_EXPR_LOCATION (stmt, loc);
13525 TREE_TYPE (stmt) = void_type_node;
13526
13527 OMP_SINGLE_CLAUSES (stmt)
13528 = c_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
13529 "#pragma omp single");
13530 OMP_SINGLE_BODY (stmt) = c_parser_omp_structured_block (parser);
13531
13532 return add_stmt (stmt);
13533 }
13534
13535 /* OpenMP 3.0:
13536 # pragma omp task task-clause[optseq] new-line
13537
13538 LOC is the location of the #pragma.
13539 */
13540
13541 #define OMP_TASK_CLAUSE_MASK \
13542 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
13543 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
13544 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
13545 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
13546 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
13547 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
13548 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
13549 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
13550 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND))
13551
13552 static tree
13553 c_parser_omp_task (location_t loc, c_parser *parser)
13554 {
13555 tree clauses, block;
13556
13557 clauses = c_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
13558 "#pragma omp task");
13559
13560 block = c_begin_omp_task ();
13561 c_parser_statement (parser);
13562 return c_finish_omp_task (loc, clauses, block);
13563 }
13564
13565 /* OpenMP 3.0:
13566 # pragma omp taskwait new-line
13567 */
13568
13569 static void
13570 c_parser_omp_taskwait (c_parser *parser)
13571 {
13572 location_t loc = c_parser_peek_token (parser)->location;
13573 c_parser_consume_pragma (parser);
13574 c_parser_skip_to_pragma_eol (parser);
13575
13576 c_finish_omp_taskwait (loc);
13577 }
13578
13579 /* OpenMP 3.1:
13580 # pragma omp taskyield new-line
13581 */
13582
13583 static void
13584 c_parser_omp_taskyield (c_parser *parser)
13585 {
13586 location_t loc = c_parser_peek_token (parser)->location;
13587 c_parser_consume_pragma (parser);
13588 c_parser_skip_to_pragma_eol (parser);
13589
13590 c_finish_omp_taskyield (loc);
13591 }
13592
13593 /* OpenMP 4.0:
13594 # pragma omp taskgroup new-line
13595 */
13596
13597 static tree
13598 c_parser_omp_taskgroup (c_parser *parser)
13599 {
13600 location_t loc = c_parser_peek_token (parser)->location;
13601 c_parser_skip_to_pragma_eol (parser);
13602 return c_finish_omp_taskgroup (loc, c_parser_omp_structured_block (parser));
13603 }
13604
13605 /* OpenMP 4.0:
13606 # pragma omp cancel cancel-clause[optseq] new-line
13607
13608 LOC is the location of the #pragma.
13609 */
13610
13611 #define OMP_CANCEL_CLAUSE_MASK \
13612 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
13613 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
13614 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
13615 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
13616 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
13617
13618 static void
13619 c_parser_omp_cancel (c_parser *parser)
13620 {
13621 location_t loc = c_parser_peek_token (parser)->location;
13622
13623 c_parser_consume_pragma (parser);
13624 tree clauses = c_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
13625 "#pragma omp cancel");
13626
13627 c_finish_omp_cancel (loc, clauses);
13628 }
13629
13630 /* OpenMP 4.0:
13631 # pragma omp cancellation point cancelpt-clause[optseq] new-line
13632
13633 LOC is the location of the #pragma.
13634 */
13635
13636 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
13637 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
13638 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
13639 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
13640 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
13641
13642 static void
13643 c_parser_omp_cancellation_point (c_parser *parser)
13644 {
13645 location_t loc = c_parser_peek_token (parser)->location;
13646 tree clauses;
13647 bool point_seen = false;
13648
13649 c_parser_consume_pragma (parser);
13650 if (c_parser_next_token_is (parser, CPP_NAME))
13651 {
13652 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
13653 if (strcmp (p, "point") == 0)
13654 {
13655 c_parser_consume_token (parser);
13656 point_seen = true;
13657 }
13658 }
13659 if (!point_seen)
13660 {
13661 c_parser_error (parser, "expected %<point%>");
13662 c_parser_skip_to_pragma_eol (parser);
13663 return;
13664 }
13665
13666 clauses
13667 = c_parser_omp_all_clauses (parser, OMP_CANCELLATION_POINT_CLAUSE_MASK,
13668 "#pragma omp cancellation point");
13669
13670 c_finish_omp_cancellation_point (loc, clauses);
13671 }
13672
13673 /* OpenMP 4.0:
13674 #pragma omp distribute distribute-clause[optseq] new-line
13675 for-loop */
13676
13677 #define OMP_DISTRIBUTE_CLAUSE_MASK \
13678 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
13679 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
13680 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
13681 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
13682
13683 static tree
13684 c_parser_omp_distribute (location_t loc, c_parser *parser,
13685 char *p_name, omp_clause_mask mask, tree *cclauses)
13686 {
13687 tree clauses, block, ret;
13688
13689 strcat (p_name, " distribute");
13690 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
13691
13692 if (c_parser_next_token_is (parser, CPP_NAME))
13693 {
13694 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
13695 bool simd = false;
13696 bool parallel = false;
13697
13698 if (strcmp (p, "simd") == 0)
13699 simd = true;
13700 else
13701 parallel = strcmp (p, "parallel") == 0;
13702 if (parallel || simd)
13703 {
13704 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
13705 if (cclauses == NULL)
13706 cclauses = cclauses_buf;
13707 c_parser_consume_token (parser);
13708 if (!flag_openmp) /* flag_openmp_simd */
13709 {
13710 if (simd)
13711 return c_parser_omp_simd (loc, parser, p_name, mask, cclauses);
13712 else
13713 return c_parser_omp_parallel (loc, parser, p_name, mask,
13714 cclauses);
13715 }
13716 block = c_begin_compound_stmt (true);
13717 if (simd)
13718 ret = c_parser_omp_simd (loc, parser, p_name, mask, cclauses);
13719 else
13720 ret = c_parser_omp_parallel (loc, parser, p_name, mask, cclauses);
13721 block = c_end_compound_stmt (loc, block, true);
13722 if (ret == NULL)
13723 return ret;
13724 ret = make_node (OMP_DISTRIBUTE);
13725 TREE_TYPE (ret) = void_type_node;
13726 OMP_FOR_BODY (ret) = block;
13727 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
13728 SET_EXPR_LOCATION (ret, loc);
13729 add_stmt (ret);
13730 return ret;
13731 }
13732 }
13733 if (!flag_openmp) /* flag_openmp_simd */
13734 {
13735 c_parser_skip_to_pragma_eol (parser, false);
13736 return NULL_TREE;
13737 }
13738
13739 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
13740 if (cclauses)
13741 {
13742 omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
13743 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
13744 }
13745
13746 block = c_begin_compound_stmt (true);
13747 ret = c_parser_omp_for_loop (loc, parser, OMP_DISTRIBUTE, clauses, NULL);
13748 block = c_end_compound_stmt (loc, block, true);
13749 add_stmt (block);
13750
13751 return ret;
13752 }
13753
13754 /* OpenMP 4.0:
13755 # pragma omp teams teams-clause[optseq] new-line
13756 structured-block */
13757
13758 #define OMP_TEAMS_CLAUSE_MASK \
13759 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
13760 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
13761 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
13762 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
13763 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
13764 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
13765 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
13766
13767 static tree
13768 c_parser_omp_teams (location_t loc, c_parser *parser,
13769 char *p_name, omp_clause_mask mask, tree *cclauses)
13770 {
13771 tree clauses, block, ret;
13772
13773 strcat (p_name, " teams");
13774 mask |= OMP_TEAMS_CLAUSE_MASK;
13775
13776 if (c_parser_next_token_is (parser, CPP_NAME))
13777 {
13778 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
13779 if (strcmp (p, "distribute") == 0)
13780 {
13781 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
13782 if (cclauses == NULL)
13783 cclauses = cclauses_buf;
13784
13785 c_parser_consume_token (parser);
13786 if (!flag_openmp) /* flag_openmp_simd */
13787 return c_parser_omp_distribute (loc, parser, p_name, mask, cclauses);
13788 block = c_begin_compound_stmt (true);
13789 ret = c_parser_omp_distribute (loc, parser, p_name, mask, cclauses);
13790 block = c_end_compound_stmt (loc, block, true);
13791 if (ret == NULL)
13792 return ret;
13793 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
13794 ret = make_node (OMP_TEAMS);
13795 TREE_TYPE (ret) = void_type_node;
13796 OMP_TEAMS_CLAUSES (ret) = clauses;
13797 OMP_TEAMS_BODY (ret) = block;
13798 OMP_TEAMS_COMBINED (ret) = 1;
13799 return add_stmt (ret);
13800 }
13801 }
13802 if (!flag_openmp) /* flag_openmp_simd */
13803 {
13804 c_parser_skip_to_pragma_eol (parser, false);
13805 return NULL_TREE;
13806 }
13807
13808 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
13809 if (cclauses)
13810 {
13811 omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
13812 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
13813 }
13814
13815 tree stmt = make_node (OMP_TEAMS);
13816 TREE_TYPE (stmt) = void_type_node;
13817 OMP_TEAMS_CLAUSES (stmt) = clauses;
13818 OMP_TEAMS_BODY (stmt) = c_parser_omp_structured_block (parser);
13819
13820 return add_stmt (stmt);
13821 }
13822
13823 /* OpenMP 4.0:
13824 # pragma omp target data target-data-clause[optseq] new-line
13825 structured-block */
13826
13827 #define OMP_TARGET_DATA_CLAUSE_MASK \
13828 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
13829 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
13830 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
13831
13832 static tree
13833 c_parser_omp_target_data (location_t loc, c_parser *parser)
13834 {
13835 tree stmt = make_node (OMP_TARGET_DATA);
13836 TREE_TYPE (stmt) = void_type_node;
13837
13838 OMP_TARGET_DATA_CLAUSES (stmt)
13839 = c_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
13840 "#pragma omp target data");
13841 keep_next_level ();
13842 tree block = c_begin_compound_stmt (true);
13843 add_stmt (c_parser_omp_structured_block (parser));
13844 OMP_TARGET_DATA_BODY (stmt) = c_end_compound_stmt (loc, block, true);
13845
13846 SET_EXPR_LOCATION (stmt, loc);
13847 return add_stmt (stmt);
13848 }
13849
13850 /* OpenMP 4.0:
13851 # pragma omp target update target-update-clause[optseq] new-line */
13852
13853 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
13854 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
13855 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
13856 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
13857 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
13858
13859 static bool
13860 c_parser_omp_target_update (location_t loc, c_parser *parser,
13861 enum pragma_context context)
13862 {
13863 if (context == pragma_stmt)
13864 {
13865 error_at (loc,
13866 "%<#pragma omp target update%> may only be "
13867 "used in compound statements");
13868 c_parser_skip_to_pragma_eol (parser);
13869 return false;
13870 }
13871
13872 tree clauses
13873 = c_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
13874 "#pragma omp target update");
13875 if (find_omp_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
13876 && find_omp_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
13877 {
13878 error_at (loc,
13879 "%<#pragma omp target update%> must contain at least one "
13880 "%<from%> or %<to%> clauses");
13881 return false;
13882 }
13883
13884 tree stmt = make_node (OMP_TARGET_UPDATE);
13885 TREE_TYPE (stmt) = void_type_node;
13886 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
13887 SET_EXPR_LOCATION (stmt, loc);
13888 add_stmt (stmt);
13889 return false;
13890 }
13891
13892 /* OpenMP 4.0:
13893 # pragma omp target target-clause[optseq] new-line
13894 structured-block */
13895
13896 #define OMP_TARGET_CLAUSE_MASK \
13897 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
13898 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
13899 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
13900
13901 static bool
13902 c_parser_omp_target (c_parser *parser, enum pragma_context context)
13903 {
13904 location_t loc = c_parser_peek_token (parser)->location;
13905 c_parser_consume_pragma (parser);
13906
13907 if (context != pragma_stmt && context != pragma_compound)
13908 {
13909 c_parser_error (parser, "expected declaration specifiers");
13910 c_parser_skip_to_pragma_eol (parser);
13911 return false;
13912 }
13913
13914 if (c_parser_next_token_is (parser, CPP_NAME))
13915 {
13916 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
13917
13918 if (strcmp (p, "teams") == 0)
13919 {
13920 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
13921 char p_name[sizeof ("#pragma omp target teams distribute "
13922 "parallel for simd")];
13923
13924 c_parser_consume_token (parser);
13925 strcpy (p_name, "#pragma omp target");
13926 if (!flag_openmp) /* flag_openmp_simd */
13927 {
13928 tree stmt = c_parser_omp_teams (loc, parser, p_name,
13929 OMP_TARGET_CLAUSE_MASK,
13930 cclauses);
13931 return stmt != NULL_TREE;
13932 }
13933 keep_next_level ();
13934 tree block = c_begin_compound_stmt (true);
13935 tree ret = c_parser_omp_teams (loc, parser, p_name,
13936 OMP_TARGET_CLAUSE_MASK, cclauses);
13937 block = c_end_compound_stmt (loc, block, true);
13938 if (ret == NULL_TREE)
13939 return false;
13940 tree stmt = make_node (OMP_TARGET);
13941 TREE_TYPE (stmt) = void_type_node;
13942 OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
13943 OMP_TARGET_BODY (stmt) = block;
13944 add_stmt (stmt);
13945 return true;
13946 }
13947 else if (!flag_openmp) /* flag_openmp_simd */
13948 {
13949 c_parser_skip_to_pragma_eol (parser, false);
13950 return false;
13951 }
13952 else if (strcmp (p, "data") == 0)
13953 {
13954 c_parser_consume_token (parser);
13955 c_parser_omp_target_data (loc, parser);
13956 return true;
13957 }
13958 else if (strcmp (p, "update") == 0)
13959 {
13960 c_parser_consume_token (parser);
13961 return c_parser_omp_target_update (loc, parser, context);
13962 }
13963 }
13964
13965 tree stmt = make_node (OMP_TARGET);
13966 TREE_TYPE (stmt) = void_type_node;
13967
13968 OMP_TARGET_CLAUSES (stmt)
13969 = c_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
13970 "#pragma omp target");
13971 keep_next_level ();
13972 tree block = c_begin_compound_stmt (true);
13973 add_stmt (c_parser_omp_structured_block (parser));
13974 OMP_TARGET_BODY (stmt) = c_end_compound_stmt (loc, block, true);
13975
13976 SET_EXPR_LOCATION (stmt, loc);
13977 add_stmt (stmt);
13978 return true;
13979 }
13980
13981 /* OpenMP 4.0:
13982 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
13983
13984 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
13985 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
13986 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
13987 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
13988 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
13989 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
13990 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
13991
13992 static void
13993 c_parser_omp_declare_simd (c_parser *parser, enum pragma_context context)
13994 {
13995 vec<c_token> clauses = vNULL;
13996 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
13997 {
13998 c_token *token = c_parser_peek_token (parser);
13999 if (token->type == CPP_EOF)
14000 {
14001 c_parser_skip_to_pragma_eol (parser);
14002 clauses.release ();
14003 return;
14004 }
14005 clauses.safe_push (*token);
14006 c_parser_consume_token (parser);
14007 }
14008 clauses.safe_push (*c_parser_peek_token (parser));
14009 c_parser_skip_to_pragma_eol (parser);
14010
14011 while (c_parser_next_token_is (parser, CPP_PRAGMA))
14012 {
14013 if (c_parser_peek_token (parser)->pragma_kind
14014 != PRAGMA_OMP_DECLARE_REDUCTION
14015 || c_parser_peek_2nd_token (parser)->type != CPP_NAME
14016 || strcmp (IDENTIFIER_POINTER
14017 (c_parser_peek_2nd_token (parser)->value),
14018 "simd") != 0)
14019 {
14020 c_parser_error (parser,
14021 "%<#pragma omp declare simd%> must be followed by "
14022 "function declaration or definition or another "
14023 "%<#pragma omp declare simd%>");
14024 clauses.release ();
14025 return;
14026 }
14027 c_parser_consume_pragma (parser);
14028 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
14029 {
14030 c_token *token = c_parser_peek_token (parser);
14031 if (token->type == CPP_EOF)
14032 {
14033 c_parser_skip_to_pragma_eol (parser);
14034 clauses.release ();
14035 return;
14036 }
14037 clauses.safe_push (*token);
14038 c_parser_consume_token (parser);
14039 }
14040 clauses.safe_push (*c_parser_peek_token (parser));
14041 c_parser_skip_to_pragma_eol (parser);
14042 }
14043
14044 /* Make sure nothing tries to read past the end of the tokens. */
14045 c_token eof_token;
14046 memset (&eof_token, 0, sizeof (eof_token));
14047 eof_token.type = CPP_EOF;
14048 clauses.safe_push (eof_token);
14049 clauses.safe_push (eof_token);
14050
14051 switch (context)
14052 {
14053 case pragma_external:
14054 if (c_parser_next_token_is (parser, CPP_KEYWORD)
14055 && c_parser_peek_token (parser)->keyword == RID_EXTENSION)
14056 {
14057 int ext = disable_extension_diagnostics ();
14058 do
14059 c_parser_consume_token (parser);
14060 while (c_parser_next_token_is (parser, CPP_KEYWORD)
14061 && c_parser_peek_token (parser)->keyword == RID_EXTENSION);
14062 c_parser_declaration_or_fndef (parser, true, true, true, false, true,
14063 NULL, clauses);
14064 restore_extension_diagnostics (ext);
14065 }
14066 else
14067 c_parser_declaration_or_fndef (parser, true, true, true, false, true,
14068 NULL, clauses);
14069 break;
14070 case pragma_struct:
14071 case pragma_param:
14072 c_parser_error (parser, "%<#pragma omp declare simd%> must be followed by "
14073 "function declaration or definition");
14074 break;
14075 case pragma_compound:
14076 case pragma_stmt:
14077 if (c_parser_next_token_is (parser, CPP_KEYWORD)
14078 && c_parser_peek_token (parser)->keyword == RID_EXTENSION)
14079 {
14080 int ext = disable_extension_diagnostics ();
14081 do
14082 c_parser_consume_token (parser);
14083 while (c_parser_next_token_is (parser, CPP_KEYWORD)
14084 && c_parser_peek_token (parser)->keyword == RID_EXTENSION);
14085 if (c_parser_next_tokens_start_declaration (parser))
14086 {
14087 c_parser_declaration_or_fndef (parser, true, true, true, true,
14088 true, NULL, clauses);
14089 restore_extension_diagnostics (ext);
14090 break;
14091 }
14092 restore_extension_diagnostics (ext);
14093 }
14094 else if (c_parser_next_tokens_start_declaration (parser))
14095 {
14096 c_parser_declaration_or_fndef (parser, true, true, true, true, true,
14097 NULL, clauses);
14098 break;
14099 }
14100 c_parser_error (parser, "%<#pragma omp declare simd%> must be followed by "
14101 "function declaration or definition");
14102 break;
14103 default:
14104 gcc_unreachable ();
14105 }
14106 clauses.release ();
14107 }
14108
14109 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
14110 and put that into "omp declare simd" attribute. */
14111
14112 static void
14113 c_finish_omp_declare_simd (c_parser *parser, tree fndecl, tree parms,
14114 vec<c_token> clauses)
14115 {
14116 if (flag_cilkplus
14117 && clauses.exists () && !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
14118 {
14119 error ("%<#pragma omp declare simd%> cannot be used in the same "
14120 "function marked as a Cilk Plus SIMD-enabled function");
14121 vec_free (parser->cilk_simd_fn_tokens);
14122 return;
14123 }
14124
14125 /* Normally first token is CPP_NAME "simd". CPP_EOF there indicates
14126 error has been reported and CPP_PRAGMA that c_finish_omp_declare_simd
14127 has already processed the tokens. */
14128 if (clauses.exists () && clauses[0].type == CPP_EOF)
14129 return;
14130 if (fndecl == NULL_TREE || TREE_CODE (fndecl) != FUNCTION_DECL)
14131 {
14132 error ("%<#pragma omp declare simd%> not immediately followed by "
14133 "a function declaration or definition");
14134 clauses[0].type = CPP_EOF;
14135 return;
14136 }
14137 if (clauses.exists () && clauses[0].type != CPP_NAME)
14138 {
14139 error_at (DECL_SOURCE_LOCATION (fndecl),
14140 "%<#pragma omp declare simd%> not immediately followed by "
14141 "a single function declaration or definition");
14142 clauses[0].type = CPP_EOF;
14143 return;
14144 }
14145
14146 if (parms == NULL_TREE)
14147 parms = DECL_ARGUMENTS (fndecl);
14148
14149 unsigned int tokens_avail = parser->tokens_avail;
14150 gcc_assert (parser->tokens == &parser->tokens_buf[0]);
14151 bool is_cilkplus_cilk_simd_fn = false;
14152
14153 if (flag_cilkplus && !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
14154 {
14155 parser->tokens = parser->cilk_simd_fn_tokens->address ();
14156 parser->tokens_avail = vec_safe_length (parser->cilk_simd_fn_tokens);
14157 is_cilkplus_cilk_simd_fn = true;
14158 }
14159 else
14160 {
14161 parser->tokens = clauses.address ();
14162 parser->tokens_avail = clauses.length ();
14163 }
14164
14165 /* c_parser_omp_declare_simd pushed 2 extra CPP_EOF tokens at the end. */
14166 while (parser->tokens_avail > 3)
14167 {
14168 c_token *token = c_parser_peek_token (parser);
14169 if (!is_cilkplus_cilk_simd_fn)
14170 gcc_assert (token->type == CPP_NAME
14171 && strcmp (IDENTIFIER_POINTER (token->value), "simd") == 0);
14172 else
14173 gcc_assert (token->type == CPP_NAME
14174 && is_cilkplus_vector_p (token->value));
14175 c_parser_consume_token (parser);
14176 parser->in_pragma = true;
14177
14178 tree c = NULL_TREE;
14179 if (is_cilkplus_cilk_simd_fn)
14180 c = c_parser_omp_all_clauses (parser, CILK_SIMD_FN_CLAUSE_MASK,
14181 "SIMD-enabled functions attribute");
14182 else
14183 c = c_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
14184 "#pragma omp declare simd");
14185 c = c_omp_declare_simd_clauses_to_numbers (parms, c);
14186 if (c != NULL_TREE)
14187 c = tree_cons (NULL_TREE, c, NULL_TREE);
14188 if (is_cilkplus_cilk_simd_fn)
14189 {
14190 tree k = build_tree_list (get_identifier ("cilk simd function"),
14191 NULL_TREE);
14192 TREE_CHAIN (k) = DECL_ATTRIBUTES (fndecl);
14193 DECL_ATTRIBUTES (fndecl) = k;
14194 }
14195 c = build_tree_list (get_identifier ("omp declare simd"), c);
14196 TREE_CHAIN (c) = DECL_ATTRIBUTES (fndecl);
14197 DECL_ATTRIBUTES (fndecl) = c;
14198 }
14199
14200 parser->tokens = &parser->tokens_buf[0];
14201 parser->tokens_avail = tokens_avail;
14202 if (clauses.exists ())
14203 clauses[0].type = CPP_PRAGMA;
14204
14205 if (!vec_safe_is_empty (parser->cilk_simd_fn_tokens))
14206 vec_free (parser->cilk_simd_fn_tokens);
14207 }
14208
14209
14210 /* OpenMP 4.0:
14211 # pragma omp declare target new-line
14212 declarations and definitions
14213 # pragma omp end declare target new-line */
14214
14215 static void
14216 c_parser_omp_declare_target (c_parser *parser)
14217 {
14218 c_parser_skip_to_pragma_eol (parser);
14219 current_omp_declare_target_attribute++;
14220 }
14221
14222 static void
14223 c_parser_omp_end_declare_target (c_parser *parser)
14224 {
14225 location_t loc = c_parser_peek_token (parser)->location;
14226 c_parser_consume_pragma (parser);
14227 if (c_parser_next_token_is (parser, CPP_NAME)
14228 && strcmp (IDENTIFIER_POINTER (c_parser_peek_token (parser)->value),
14229 "declare") == 0)
14230 {
14231 c_parser_consume_token (parser);
14232 if (c_parser_next_token_is (parser, CPP_NAME)
14233 && strcmp (IDENTIFIER_POINTER (c_parser_peek_token (parser)->value),
14234 "target") == 0)
14235 c_parser_consume_token (parser);
14236 else
14237 {
14238 c_parser_error (parser, "expected %<target%>");
14239 c_parser_skip_to_pragma_eol (parser);
14240 return;
14241 }
14242 }
14243 else
14244 {
14245 c_parser_error (parser, "expected %<declare%>");
14246 c_parser_skip_to_pragma_eol (parser);
14247 return;
14248 }
14249 c_parser_skip_to_pragma_eol (parser);
14250 if (!current_omp_declare_target_attribute)
14251 error_at (loc, "%<#pragma omp end declare target%> without corresponding "
14252 "%<#pragma omp declare target%>");
14253 else
14254 current_omp_declare_target_attribute--;
14255 }
14256
14257
14258 /* OpenMP 4.0
14259 #pragma omp declare reduction (reduction-id : typename-list : expression) \
14260 initializer-clause[opt] new-line
14261
14262 initializer-clause:
14263 initializer (omp_priv = initializer)
14264 initializer (function-name (argument-list)) */
14265
14266 static void
14267 c_parser_omp_declare_reduction (c_parser *parser, enum pragma_context context)
14268 {
14269 unsigned int tokens_avail = 0, i;
14270 vec<tree> types = vNULL;
14271 vec<c_token> clauses = vNULL;
14272 enum tree_code reduc_code = ERROR_MARK;
14273 tree reduc_id = NULL_TREE;
14274 tree type;
14275 location_t rloc = c_parser_peek_token (parser)->location;
14276
14277 if (context == pragma_struct || context == pragma_param)
14278 {
14279 error ("%<#pragma omp declare reduction%> not at file or block scope");
14280 goto fail;
14281 }
14282
14283 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
14284 goto fail;
14285
14286 switch (c_parser_peek_token (parser)->type)
14287 {
14288 case CPP_PLUS:
14289 reduc_code = PLUS_EXPR;
14290 break;
14291 case CPP_MULT:
14292 reduc_code = MULT_EXPR;
14293 break;
14294 case CPP_MINUS:
14295 reduc_code = MINUS_EXPR;
14296 break;
14297 case CPP_AND:
14298 reduc_code = BIT_AND_EXPR;
14299 break;
14300 case CPP_XOR:
14301 reduc_code = BIT_XOR_EXPR;
14302 break;
14303 case CPP_OR:
14304 reduc_code = BIT_IOR_EXPR;
14305 break;
14306 case CPP_AND_AND:
14307 reduc_code = TRUTH_ANDIF_EXPR;
14308 break;
14309 case CPP_OR_OR:
14310 reduc_code = TRUTH_ORIF_EXPR;
14311 break;
14312 case CPP_NAME:
14313 const char *p;
14314 p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
14315 if (strcmp (p, "min") == 0)
14316 {
14317 reduc_code = MIN_EXPR;
14318 break;
14319 }
14320 if (strcmp (p, "max") == 0)
14321 {
14322 reduc_code = MAX_EXPR;
14323 break;
14324 }
14325 reduc_id = c_parser_peek_token (parser)->value;
14326 break;
14327 default:
14328 c_parser_error (parser,
14329 "expected %<+%>, %<*%>, %<-%>, %<&%>, "
14330 "%<^%>, %<|%>, %<&&%>, %<||%>, %<min%> or identifier");
14331 goto fail;
14332 }
14333
14334 tree orig_reduc_id, reduc_decl;
14335 orig_reduc_id = reduc_id;
14336 reduc_id = c_omp_reduction_id (reduc_code, reduc_id);
14337 reduc_decl = c_omp_reduction_decl (reduc_id);
14338 c_parser_consume_token (parser);
14339
14340 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
14341 goto fail;
14342
14343 while (true)
14344 {
14345 location_t loc = c_parser_peek_token (parser)->location;
14346 struct c_type_name *ctype = c_parser_type_name (parser);
14347 if (ctype != NULL)
14348 {
14349 type = groktypename (ctype, NULL, NULL);
14350 if (type == error_mark_node)
14351 ;
14352 else if ((INTEGRAL_TYPE_P (type)
14353 || TREE_CODE (type) == REAL_TYPE
14354 || TREE_CODE (type) == COMPLEX_TYPE)
14355 && orig_reduc_id == NULL_TREE)
14356 error_at (loc, "predeclared arithmetic type in "
14357 "%<#pragma omp declare reduction%>");
14358 else if (TREE_CODE (type) == FUNCTION_TYPE
14359 || TREE_CODE (type) == ARRAY_TYPE)
14360 error_at (loc, "function or array type in "
14361 "%<#pragma omp declare reduction%>");
14362 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
14363 error_at (loc, "const, volatile or restrict qualified type in "
14364 "%<#pragma omp declare reduction%>");
14365 else
14366 {
14367 tree t;
14368 for (t = DECL_INITIAL (reduc_decl); t; t = TREE_CHAIN (t))
14369 if (comptypes (TREE_PURPOSE (t), type))
14370 {
14371 error_at (loc, "redeclaration of %qs "
14372 "%<#pragma omp declare reduction%> for "
14373 "type %qT",
14374 IDENTIFIER_POINTER (reduc_id)
14375 + sizeof ("omp declare reduction ") - 1,
14376 type);
14377 location_t ploc
14378 = DECL_SOURCE_LOCATION (TREE_VEC_ELT (TREE_VALUE (t),
14379 0));
14380 error_at (ploc, "previous %<#pragma omp declare "
14381 "reduction%>");
14382 break;
14383 }
14384 if (t == NULL_TREE)
14385 types.safe_push (type);
14386 }
14387 if (c_parser_next_token_is (parser, CPP_COMMA))
14388 c_parser_consume_token (parser);
14389 else
14390 break;
14391 }
14392 else
14393 break;
14394 }
14395
14396 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>")
14397 || types.is_empty ())
14398 {
14399 fail:
14400 clauses.release ();
14401 types.release ();
14402 while (true)
14403 {
14404 c_token *token = c_parser_peek_token (parser);
14405 if (token->type == CPP_EOF || token->type == CPP_PRAGMA_EOL)
14406 break;
14407 c_parser_consume_token (parser);
14408 }
14409 c_parser_skip_to_pragma_eol (parser);
14410 return;
14411 }
14412
14413 if (types.length () > 1)
14414 {
14415 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
14416 {
14417 c_token *token = c_parser_peek_token (parser);
14418 if (token->type == CPP_EOF)
14419 goto fail;
14420 clauses.safe_push (*token);
14421 c_parser_consume_token (parser);
14422 }
14423 clauses.safe_push (*c_parser_peek_token (parser));
14424 c_parser_skip_to_pragma_eol (parser);
14425
14426 /* Make sure nothing tries to read past the end of the tokens. */
14427 c_token eof_token;
14428 memset (&eof_token, 0, sizeof (eof_token));
14429 eof_token.type = CPP_EOF;
14430 clauses.safe_push (eof_token);
14431 clauses.safe_push (eof_token);
14432 }
14433
14434 int errs = errorcount;
14435 FOR_EACH_VEC_ELT (types, i, type)
14436 {
14437 tokens_avail = parser->tokens_avail;
14438 gcc_assert (parser->tokens == &parser->tokens_buf[0]);
14439 if (!clauses.is_empty ())
14440 {
14441 parser->tokens = clauses.address ();
14442 parser->tokens_avail = clauses.length ();
14443 parser->in_pragma = true;
14444 }
14445
14446 bool nested = current_function_decl != NULL_TREE;
14447 if (nested)
14448 c_push_function_context ();
14449 tree fndecl = build_decl (BUILTINS_LOCATION, FUNCTION_DECL,
14450 reduc_id, default_function_type);
14451 current_function_decl = fndecl;
14452 allocate_struct_function (fndecl, true);
14453 push_scope ();
14454 tree stmt = push_stmt_list ();
14455 /* Intentionally BUILTINS_LOCATION, so that -Wshadow doesn't
14456 warn about these. */
14457 tree omp_out = build_decl (BUILTINS_LOCATION, VAR_DECL,
14458 get_identifier ("omp_out"), type);
14459 DECL_ARTIFICIAL (omp_out) = 1;
14460 DECL_CONTEXT (omp_out) = fndecl;
14461 pushdecl (omp_out);
14462 tree omp_in = build_decl (BUILTINS_LOCATION, VAR_DECL,
14463 get_identifier ("omp_in"), type);
14464 DECL_ARTIFICIAL (omp_in) = 1;
14465 DECL_CONTEXT (omp_in) = fndecl;
14466 pushdecl (omp_in);
14467 struct c_expr combiner = c_parser_expression (parser);
14468 struct c_expr initializer;
14469 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE;
14470 bool bad = false;
14471 initializer.value = error_mark_node;
14472 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
14473 bad = true;
14474 else if (c_parser_next_token_is (parser, CPP_NAME)
14475 && strcmp (IDENTIFIER_POINTER
14476 (c_parser_peek_token (parser)->value),
14477 "initializer") == 0)
14478 {
14479 c_parser_consume_token (parser);
14480 pop_scope ();
14481 push_scope ();
14482 omp_priv = build_decl (BUILTINS_LOCATION, VAR_DECL,
14483 get_identifier ("omp_priv"), type);
14484 DECL_ARTIFICIAL (omp_priv) = 1;
14485 DECL_INITIAL (omp_priv) = error_mark_node;
14486 DECL_CONTEXT (omp_priv) = fndecl;
14487 pushdecl (omp_priv);
14488 omp_orig = build_decl (BUILTINS_LOCATION, VAR_DECL,
14489 get_identifier ("omp_orig"), type);
14490 DECL_ARTIFICIAL (omp_orig) = 1;
14491 DECL_CONTEXT (omp_orig) = fndecl;
14492 pushdecl (omp_orig);
14493 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
14494 bad = true;
14495 else if (!c_parser_next_token_is (parser, CPP_NAME))
14496 {
14497 c_parser_error (parser, "expected %<omp_priv%> or "
14498 "function-name");
14499 bad = true;
14500 }
14501 else if (strcmp (IDENTIFIER_POINTER
14502 (c_parser_peek_token (parser)->value),
14503 "omp_priv") != 0)
14504 {
14505 if (c_parser_peek_2nd_token (parser)->type != CPP_OPEN_PAREN
14506 || c_parser_peek_token (parser)->id_kind != C_ID_ID)
14507 {
14508 c_parser_error (parser, "expected function-name %<(%>");
14509 bad = true;
14510 }
14511 else
14512 initializer = c_parser_postfix_expression (parser);
14513 if (initializer.value
14514 && TREE_CODE (initializer.value) == CALL_EXPR)
14515 {
14516 int j;
14517 tree c = initializer.value;
14518 for (j = 0; j < call_expr_nargs (c); j++)
14519 if (TREE_CODE (CALL_EXPR_ARG (c, j)) == ADDR_EXPR
14520 && TREE_OPERAND (CALL_EXPR_ARG (c, j), 0) == omp_priv)
14521 break;
14522 if (j == call_expr_nargs (c))
14523 error ("one of the initializer call arguments should be "
14524 "%<&omp_priv%>");
14525 }
14526 }
14527 else
14528 {
14529 c_parser_consume_token (parser);
14530 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
14531 bad = true;
14532 else
14533 {
14534 tree st = push_stmt_list ();
14535 start_init (omp_priv, NULL_TREE, 0);
14536 location_t loc = c_parser_peek_token (parser)->location;
14537 struct c_expr init = c_parser_initializer (parser);
14538 finish_init ();
14539 finish_decl (omp_priv, loc, init.value,
14540 init.original_type, NULL_TREE);
14541 pop_stmt_list (st);
14542 }
14543 }
14544 if (!bad
14545 && !c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
14546 bad = true;
14547 }
14548
14549 if (!bad)
14550 {
14551 c_parser_skip_to_pragma_eol (parser);
14552
14553 tree t = tree_cons (type, make_tree_vec (omp_priv ? 6 : 3),
14554 DECL_INITIAL (reduc_decl));
14555 DECL_INITIAL (reduc_decl) = t;
14556 DECL_SOURCE_LOCATION (omp_out) = rloc;
14557 TREE_VEC_ELT (TREE_VALUE (t), 0) = omp_out;
14558 TREE_VEC_ELT (TREE_VALUE (t), 1) = omp_in;
14559 TREE_VEC_ELT (TREE_VALUE (t), 2) = combiner.value;
14560 walk_tree (&combiner.value, c_check_omp_declare_reduction_r,
14561 &TREE_VEC_ELT (TREE_VALUE (t), 0), NULL);
14562 if (omp_priv)
14563 {
14564 DECL_SOURCE_LOCATION (omp_priv) = rloc;
14565 TREE_VEC_ELT (TREE_VALUE (t), 3) = omp_priv;
14566 TREE_VEC_ELT (TREE_VALUE (t), 4) = omp_orig;
14567 TREE_VEC_ELT (TREE_VALUE (t), 5) = initializer.value;
14568 walk_tree (&initializer.value, c_check_omp_declare_reduction_r,
14569 &TREE_VEC_ELT (TREE_VALUE (t), 3), NULL);
14570 walk_tree (&DECL_INITIAL (omp_priv),
14571 c_check_omp_declare_reduction_r,
14572 &TREE_VEC_ELT (TREE_VALUE (t), 3), NULL);
14573 }
14574 }
14575
14576 pop_stmt_list (stmt);
14577 pop_scope ();
14578 if (cfun->language != NULL)
14579 {
14580 ggc_free (cfun->language);
14581 cfun->language = NULL;
14582 }
14583 set_cfun (NULL);
14584 current_function_decl = NULL_TREE;
14585 if (nested)
14586 c_pop_function_context ();
14587
14588 if (!clauses.is_empty ())
14589 {
14590 parser->tokens = &parser->tokens_buf[0];
14591 parser->tokens_avail = tokens_avail;
14592 }
14593 if (bad)
14594 goto fail;
14595 if (errs != errorcount)
14596 break;
14597 }
14598
14599 clauses.release ();
14600 types.release ();
14601 }
14602
14603
14604 /* OpenMP 4.0
14605 #pragma omp declare simd declare-simd-clauses[optseq] new-line
14606 #pragma omp declare reduction (reduction-id : typename-list : expression) \
14607 initializer-clause[opt] new-line
14608 #pragma omp declare target new-line */
14609
14610 static void
14611 c_parser_omp_declare (c_parser *parser, enum pragma_context context)
14612 {
14613 c_parser_consume_pragma (parser);
14614 if (c_parser_next_token_is (parser, CPP_NAME))
14615 {
14616 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
14617 if (strcmp (p, "simd") == 0)
14618 {
14619 /* c_parser_consume_token (parser); done in
14620 c_parser_omp_declare_simd. */
14621 c_parser_omp_declare_simd (parser, context);
14622 return;
14623 }
14624 if (strcmp (p, "reduction") == 0)
14625 {
14626 c_parser_consume_token (parser);
14627 c_parser_omp_declare_reduction (parser, context);
14628 return;
14629 }
14630 if (!flag_openmp) /* flag_openmp_simd */
14631 {
14632 c_parser_skip_to_pragma_eol (parser, false);
14633 return;
14634 }
14635 if (strcmp (p, "target") == 0)
14636 {
14637 c_parser_consume_token (parser);
14638 c_parser_omp_declare_target (parser);
14639 return;
14640 }
14641 }
14642
14643 c_parser_error (parser, "expected %<simd%> or %<reduction%> "
14644 "or %<target%>");
14645 c_parser_skip_to_pragma_eol (parser);
14646 }
14647
14648 /* Main entry point to parsing most OpenMP pragmas. */
14649
14650 static void
14651 c_parser_omp_construct (c_parser *parser)
14652 {
14653 enum pragma_kind p_kind;
14654 location_t loc;
14655 tree stmt;
14656 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
14657 omp_clause_mask mask (0);
14658
14659 loc = c_parser_peek_token (parser)->location;
14660 p_kind = c_parser_peek_token (parser)->pragma_kind;
14661 c_parser_consume_pragma (parser);
14662
14663 switch (p_kind)
14664 {
14665 case PRAGMA_OACC_CACHE:
14666 strcpy (p_name, "#pragma acc");
14667 stmt = c_parser_oacc_cache (loc, parser);
14668 break;
14669 case PRAGMA_OACC_DATA:
14670 stmt = c_parser_oacc_data (loc, parser);
14671 break;
14672 case PRAGMA_OACC_KERNELS:
14673 strcpy (p_name, "#pragma acc");
14674 stmt = c_parser_oacc_kernels (loc, parser, p_name);
14675 break;
14676 case PRAGMA_OACC_LOOP:
14677 strcpy (p_name, "#pragma acc");
14678 stmt = c_parser_oacc_loop (loc, parser, p_name);
14679 break;
14680 case PRAGMA_OACC_PARALLEL:
14681 strcpy (p_name, "#pragma acc");
14682 stmt = c_parser_oacc_parallel (loc, parser, p_name);
14683 break;
14684 case PRAGMA_OACC_WAIT:
14685 strcpy (p_name, "#pragma wait");
14686 stmt = c_parser_oacc_wait (loc, parser, p_name);
14687 break;
14688 case PRAGMA_OMP_ATOMIC:
14689 c_parser_omp_atomic (loc, parser);
14690 return;
14691 case PRAGMA_OMP_CRITICAL:
14692 stmt = c_parser_omp_critical (loc, parser);
14693 break;
14694 case PRAGMA_OMP_DISTRIBUTE:
14695 strcpy (p_name, "#pragma omp");
14696 stmt = c_parser_omp_distribute (loc, parser, p_name, mask, NULL);
14697 break;
14698 case PRAGMA_OMP_FOR:
14699 strcpy (p_name, "#pragma omp");
14700 stmt = c_parser_omp_for (loc, parser, p_name, mask, NULL);
14701 break;
14702 case PRAGMA_OMP_MASTER:
14703 stmt = c_parser_omp_master (loc, parser);
14704 break;
14705 case PRAGMA_OMP_ORDERED:
14706 stmt = c_parser_omp_ordered (loc, parser);
14707 break;
14708 case PRAGMA_OMP_PARALLEL:
14709 strcpy (p_name, "#pragma omp");
14710 stmt = c_parser_omp_parallel (loc, parser, p_name, mask, NULL);
14711 break;
14712 case PRAGMA_OMP_SECTIONS:
14713 strcpy (p_name, "#pragma omp");
14714 stmt = c_parser_omp_sections (loc, parser, p_name, mask, NULL);
14715 break;
14716 case PRAGMA_OMP_SIMD:
14717 strcpy (p_name, "#pragma omp");
14718 stmt = c_parser_omp_simd (loc, parser, p_name, mask, NULL);
14719 break;
14720 case PRAGMA_OMP_SINGLE:
14721 stmt = c_parser_omp_single (loc, parser);
14722 break;
14723 case PRAGMA_OMP_TASK:
14724 stmt = c_parser_omp_task (loc, parser);
14725 break;
14726 case PRAGMA_OMP_TASKGROUP:
14727 stmt = c_parser_omp_taskgroup (parser);
14728 break;
14729 case PRAGMA_OMP_TEAMS:
14730 strcpy (p_name, "#pragma omp");
14731 stmt = c_parser_omp_teams (loc, parser, p_name, mask, NULL);
14732 break;
14733 default:
14734 gcc_unreachable ();
14735 }
14736
14737 if (stmt)
14738 gcc_assert (EXPR_LOCATION (stmt) != UNKNOWN_LOCATION);
14739 }
14740
14741
14742 /* OpenMP 2.5:
14743 # pragma omp threadprivate (variable-list) */
14744
14745 static void
14746 c_parser_omp_threadprivate (c_parser *parser)
14747 {
14748 tree vars, t;
14749 location_t loc;
14750
14751 c_parser_consume_pragma (parser);
14752 loc = c_parser_peek_token (parser)->location;
14753 vars = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
14754
14755 /* Mark every variable in VARS to be assigned thread local storage. */
14756 for (t = vars; t; t = TREE_CHAIN (t))
14757 {
14758 tree v = TREE_PURPOSE (t);
14759
14760 /* FIXME diagnostics: Ideally we should keep individual
14761 locations for all the variables in the var list to make the
14762 following errors more precise. Perhaps
14763 c_parser_omp_var_list_parens() should construct a list of
14764 locations to go along with the var list. */
14765
14766 /* If V had already been marked threadprivate, it doesn't matter
14767 whether it had been used prior to this point. */
14768 if (TREE_CODE (v) != VAR_DECL)
14769 error_at (loc, "%qD is not a variable", v);
14770 else if (TREE_USED (v) && !C_DECL_THREADPRIVATE_P (v))
14771 error_at (loc, "%qE declared %<threadprivate%> after first use", v);
14772 else if (! TREE_STATIC (v) && ! DECL_EXTERNAL (v))
14773 error_at (loc, "automatic variable %qE cannot be %<threadprivate%>", v);
14774 else if (TREE_TYPE (v) == error_mark_node)
14775 ;
14776 else if (! COMPLETE_TYPE_P (TREE_TYPE (v)))
14777 error_at (loc, "%<threadprivate%> %qE has incomplete type", v);
14778 else
14779 {
14780 if (! DECL_THREAD_LOCAL_P (v))
14781 {
14782 set_decl_tls_model (v, decl_default_tls_model (v));
14783 /* If rtl has been already set for this var, call
14784 make_decl_rtl once again, so that encode_section_info
14785 has a chance to look at the new decl flags. */
14786 if (DECL_RTL_SET_P (v))
14787 make_decl_rtl (v);
14788 }
14789 C_DECL_THREADPRIVATE_P (v) = 1;
14790 }
14791 }
14792
14793 c_parser_skip_to_pragma_eol (parser);
14794 }
14795 \f
14796 /* Cilk Plus <#pragma simd> parsing routines. */
14797
14798 /* Helper function for c_parser_pragma. Perform some sanity checking
14799 for <#pragma simd> constructs. Returns FALSE if there was a
14800 problem. */
14801
14802 static bool
14803 c_parser_cilk_verify_simd (c_parser *parser,
14804 enum pragma_context context)
14805 {
14806 if (!flag_cilkplus)
14807 {
14808 warning (0, "pragma simd ignored because -fcilkplus is not enabled");
14809 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
14810 return false;
14811 }
14812 if (context == pragma_external)
14813 {
14814 c_parser_error (parser,"pragma simd must be inside a function");
14815 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
14816 return false;
14817 }
14818 return true;
14819 }
14820
14821 /* Cilk Plus:
14822 This function is shared by SIMD-enabled functions and #pragma simd.
14823 If IS_SIMD_FN is true then it is parsing a SIMD-enabled function and
14824 CLAUSES is unused. The main purpose of this function is to parse a
14825 vectorlength attribute or clause and check for parse errors.
14826 When IS_SIMD_FN is true then the function is merely caching the tokens
14827 in PARSER->CILK_SIMD_FN_TOKENS. If errors are found then the token
14828 cache is cleared since there is no reason to continue.
14829 Syntax:
14830 vectorlength ( constant-expression ) */
14831
14832 static tree
14833 c_parser_cilk_clause_vectorlength (c_parser *parser, tree clauses,
14834 bool is_simd_fn)
14835 {
14836 if (is_simd_fn)
14837 check_no_duplicate_clause (clauses, OMP_CLAUSE_SIMDLEN, "vectorlength");
14838 else
14839 /* The vectorlength clause behaves exactly like OpenMP's safelen
14840 clause. Represent it in OpenMP terms. */
14841 check_no_duplicate_clause (clauses, OMP_CLAUSE_SAFELEN, "vectorlength");
14842
14843 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
14844 return clauses;
14845
14846 location_t loc = c_parser_peek_token (parser)->location;
14847 tree expr = c_parser_expr_no_commas (parser, NULL).value;
14848 expr = c_fully_fold (expr, false, NULL);
14849
14850 /* If expr is an error_mark_node then the above function would have
14851 emitted an error. No reason to do it twice. */
14852 if (expr == error_mark_node)
14853 ;
14854 else if (!TREE_TYPE (expr)
14855 || !TREE_CONSTANT (expr)
14856 || !INTEGRAL_TYPE_P (TREE_TYPE (expr)))
14857
14858 error_at (loc, "vectorlength must be an integer constant");
14859 else if (wi::exact_log2 (expr) == -1)
14860 error_at (loc, "vectorlength must be a power of 2");
14861 else
14862 {
14863 if (is_simd_fn)
14864 {
14865 tree u = build_omp_clause (loc, OMP_CLAUSE_SIMDLEN);
14866 OMP_CLAUSE_SIMDLEN_EXPR (u) = expr;
14867 OMP_CLAUSE_CHAIN (u) = clauses;
14868 clauses = u;
14869 }
14870 else
14871 {
14872 tree u = build_omp_clause (loc, OMP_CLAUSE_SAFELEN);
14873 OMP_CLAUSE_SAFELEN_EXPR (u) = expr;
14874 OMP_CLAUSE_CHAIN (u) = clauses;
14875 clauses = u;
14876 }
14877 }
14878
14879 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
14880
14881 return clauses;
14882 }
14883
14884 /* Cilk Plus:
14885 linear ( simd-linear-variable-list )
14886
14887 simd-linear-variable-list:
14888 simd-linear-variable
14889 simd-linear-variable-list , simd-linear-variable
14890
14891 simd-linear-variable:
14892 id-expression
14893 id-expression : simd-linear-step
14894
14895 simd-linear-step:
14896 conditional-expression */
14897
14898 static tree
14899 c_parser_cilk_clause_linear (c_parser *parser, tree clauses)
14900 {
14901 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
14902 return clauses;
14903
14904 location_t loc = c_parser_peek_token (parser)->location;
14905
14906 if (c_parser_next_token_is_not (parser, CPP_NAME)
14907 || c_parser_peek_token (parser)->id_kind != C_ID_ID)
14908 c_parser_error (parser, "expected identifier");
14909
14910 while (c_parser_next_token_is (parser, CPP_NAME)
14911 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
14912 {
14913 tree var = lookup_name (c_parser_peek_token (parser)->value);
14914
14915 if (var == NULL)
14916 {
14917 undeclared_variable (c_parser_peek_token (parser)->location,
14918 c_parser_peek_token (parser)->value);
14919 c_parser_consume_token (parser);
14920 }
14921 else if (var == error_mark_node)
14922 c_parser_consume_token (parser);
14923 else
14924 {
14925 tree step = integer_one_node;
14926
14927 /* Parse the linear step if present. */
14928 if (c_parser_peek_2nd_token (parser)->type == CPP_COLON)
14929 {
14930 c_parser_consume_token (parser);
14931 c_parser_consume_token (parser);
14932
14933 tree expr = c_parser_expr_no_commas (parser, NULL).value;
14934 expr = c_fully_fold (expr, false, NULL);
14935
14936 if (TREE_TYPE (expr)
14937 && INTEGRAL_TYPE_P (TREE_TYPE (expr))
14938 && (TREE_CONSTANT (expr)
14939 || DECL_P (expr)))
14940 step = expr;
14941 else
14942 c_parser_error (parser,
14943 "step size must be an integer constant "
14944 "expression or an integer variable");
14945 }
14946 else
14947 c_parser_consume_token (parser);
14948
14949 /* Use OMP_CLAUSE_LINEAR, which has the same semantics. */
14950 tree u = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
14951 OMP_CLAUSE_DECL (u) = var;
14952 OMP_CLAUSE_LINEAR_STEP (u) = step;
14953 OMP_CLAUSE_CHAIN (u) = clauses;
14954 clauses = u;
14955 }
14956
14957 if (c_parser_next_token_is_not (parser, CPP_COMMA))
14958 break;
14959
14960 c_parser_consume_token (parser);
14961 }
14962
14963 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
14964
14965 return clauses;
14966 }
14967
14968 /* Returns the name of the next clause. If the clause is not
14969 recognized SIMD_OMP_CLAUSE_NONE is returned and the next token is
14970 not consumed. Otherwise, the appropriate pragma_simd_clause is
14971 returned and the token is consumed. */
14972
14973 static pragma_omp_clause
14974 c_parser_cilk_clause_name (c_parser *parser)
14975 {
14976 pragma_omp_clause result;
14977 c_token *token = c_parser_peek_token (parser);
14978
14979 if (!token->value || token->type != CPP_NAME)
14980 return PRAGMA_CILK_CLAUSE_NONE;
14981
14982 const char *p = IDENTIFIER_POINTER (token->value);
14983
14984 if (!strcmp (p, "vectorlength"))
14985 result = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
14986 else if (!strcmp (p, "linear"))
14987 result = PRAGMA_CILK_CLAUSE_LINEAR;
14988 else if (!strcmp (p, "private"))
14989 result = PRAGMA_CILK_CLAUSE_PRIVATE;
14990 else if (!strcmp (p, "firstprivate"))
14991 result = PRAGMA_CILK_CLAUSE_FIRSTPRIVATE;
14992 else if (!strcmp (p, "lastprivate"))
14993 result = PRAGMA_CILK_CLAUSE_LASTPRIVATE;
14994 else if (!strcmp (p, "reduction"))
14995 result = PRAGMA_CILK_CLAUSE_REDUCTION;
14996 else
14997 return PRAGMA_CILK_CLAUSE_NONE;
14998
14999 c_parser_consume_token (parser);
15000 return result;
15001 }
15002
15003 /* Parse all #<pragma simd> clauses. Return the list of clauses
15004 found. */
15005
15006 static tree
15007 c_parser_cilk_all_clauses (c_parser *parser)
15008 {
15009 tree clauses = NULL;
15010
15011 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
15012 {
15013 pragma_omp_clause c_kind;
15014
15015 c_kind = c_parser_cilk_clause_name (parser);
15016
15017 switch (c_kind)
15018 {
15019 case PRAGMA_CILK_CLAUSE_VECTORLENGTH:
15020 clauses = c_parser_cilk_clause_vectorlength (parser, clauses, false);
15021 break;
15022 case PRAGMA_CILK_CLAUSE_LINEAR:
15023 clauses = c_parser_cilk_clause_linear (parser, clauses);
15024 break;
15025 case PRAGMA_CILK_CLAUSE_PRIVATE:
15026 /* Use the OpenMP counterpart. */
15027 clauses = c_parser_omp_clause_private (parser, clauses);
15028 break;
15029 case PRAGMA_CILK_CLAUSE_FIRSTPRIVATE:
15030 /* Use the OpenMP counterpart. */
15031 clauses = c_parser_omp_clause_firstprivate (parser, clauses);
15032 break;
15033 case PRAGMA_CILK_CLAUSE_LASTPRIVATE:
15034 /* Use the OpenMP counterpart. */
15035 clauses = c_parser_omp_clause_lastprivate (parser, clauses);
15036 break;
15037 case PRAGMA_CILK_CLAUSE_REDUCTION:
15038 /* Use the OpenMP counterpart. */
15039 clauses = c_parser_omp_clause_reduction (parser, clauses);
15040 break;
15041 default:
15042 c_parser_error (parser, "expected %<#pragma simd%> clause");
15043 goto saw_error;
15044 }
15045 }
15046
15047 saw_error:
15048 c_parser_skip_to_pragma_eol (parser);
15049 return c_finish_cilk_clauses (clauses);
15050 }
15051
15052 /* This function helps parse the grainsize pragma for a _Cilk_for statement.
15053 Here is the correct syntax of this pragma:
15054 #pragma cilk grainsize = <EXP>
15055 */
15056
15057 static void
15058 c_parser_cilk_grainsize (c_parser *parser)
15059 {
15060 extern tree convert_to_integer (tree, tree);
15061
15062 /* consume the 'grainsize' keyword. */
15063 c_parser_consume_pragma (parser);
15064
15065 if (c_parser_require (parser, CPP_EQ, "expected %<=%>") != 0)
15066 {
15067 struct c_expr g_expr = c_parser_binary_expression (parser, NULL, NULL);
15068 if (g_expr.value == error_mark_node)
15069 {
15070 c_parser_skip_to_pragma_eol (parser);
15071 return;
15072 }
15073 tree grain = convert_to_integer (long_integer_type_node,
15074 c_fully_fold (g_expr.value, false,
15075 NULL));
15076 c_parser_skip_to_pragma_eol (parser);
15077 c_token *token = c_parser_peek_token (parser);
15078 if (token && token->type == CPP_KEYWORD
15079 && token->keyword == RID_CILK_FOR)
15080 {
15081 if (grain == NULL_TREE || grain == error_mark_node)
15082 grain = integer_zero_node;
15083 c_parser_cilk_for (parser, grain);
15084 }
15085 else
15086 warning (0, "%<#pragma cilk grainsize%> is not followed by "
15087 "%<_Cilk_for%>");
15088 }
15089 else
15090 c_parser_skip_to_pragma_eol (parser);
15091 }
15092
15093 /* Main entry point for parsing Cilk Plus <#pragma simd> for loops. */
15094
15095 static void
15096 c_parser_cilk_simd (c_parser *parser)
15097 {
15098 tree clauses = c_parser_cilk_all_clauses (parser);
15099 tree block = c_begin_compound_stmt (true);
15100 location_t loc = c_parser_peek_token (parser)->location;
15101 c_parser_omp_for_loop (loc, parser, CILK_SIMD, clauses, NULL);
15102 block = c_end_compound_stmt (loc, block, true);
15103 add_stmt (block);
15104 }
15105
15106 /* Create an artificial decl with TYPE and emit initialization of it with
15107 INIT. */
15108
15109 static tree
15110 c_get_temp_regvar (tree type, tree init)
15111 {
15112 location_t loc = EXPR_LOCATION (init);
15113 tree decl = build_decl (loc, VAR_DECL, NULL_TREE, type);
15114 DECL_ARTIFICIAL (decl) = 1;
15115 DECL_IGNORED_P (decl) = 1;
15116 pushdecl (decl);
15117 tree t = build2 (INIT_EXPR, type, decl, init);
15118 add_stmt (t);
15119 return decl;
15120 }
15121
15122 /* Main entry point for parsing Cilk Plus _Cilk_for loops.
15123 GRAIN is the grain value passed in through pragma or 0. */
15124
15125 static void
15126 c_parser_cilk_for (c_parser *parser, tree grain)
15127 {
15128 tree clauses = build_omp_clause (EXPR_LOCATION (grain), OMP_CLAUSE_SCHEDULE);
15129 OMP_CLAUSE_SCHEDULE_KIND (clauses) = OMP_CLAUSE_SCHEDULE_CILKFOR;
15130 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clauses) = grain;
15131 clauses = c_finish_omp_clauses (clauses);
15132
15133 tree block = c_begin_compound_stmt (true);
15134 tree sb = push_stmt_list ();
15135 location_t loc = c_parser_peek_token (parser)->location;
15136 tree omp_for = c_parser_omp_for_loop (loc, parser, CILK_FOR, clauses, NULL);
15137 sb = pop_stmt_list (sb);
15138
15139 if (omp_for)
15140 {
15141 tree omp_par = make_node (OMP_PARALLEL);
15142 TREE_TYPE (omp_par) = void_type_node;
15143 OMP_PARALLEL_CLAUSES (omp_par) = NULL_TREE;
15144 tree bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL);
15145 TREE_SIDE_EFFECTS (bind) = 1;
15146 BIND_EXPR_BODY (bind) = sb;
15147 OMP_PARALLEL_BODY (omp_par) = bind;
15148 if (OMP_FOR_PRE_BODY (omp_for))
15149 {
15150 add_stmt (OMP_FOR_PRE_BODY (omp_for));
15151 OMP_FOR_PRE_BODY (omp_for) = NULL_TREE;
15152 }
15153 tree init = TREE_VEC_ELT (OMP_FOR_INIT (omp_for), 0);
15154 tree decl = TREE_OPERAND (init, 0);
15155 tree cond = TREE_VEC_ELT (OMP_FOR_COND (omp_for), 0);
15156 tree incr = TREE_VEC_ELT (OMP_FOR_INCR (omp_for), 0);
15157 tree t = TREE_OPERAND (cond, 1), c, clauses = NULL_TREE;
15158 if (TREE_CODE (t) != INTEGER_CST)
15159 {
15160 TREE_OPERAND (cond, 1) = c_get_temp_regvar (TREE_TYPE (t), t);
15161 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
15162 OMP_CLAUSE_DECL (c) = TREE_OPERAND (cond, 1);
15163 OMP_CLAUSE_CHAIN (c) = clauses;
15164 clauses = c;
15165 }
15166 if (TREE_CODE (incr) == MODIFY_EXPR)
15167 {
15168 t = TREE_OPERAND (TREE_OPERAND (incr, 1), 1);
15169 if (TREE_CODE (t) != INTEGER_CST)
15170 {
15171 TREE_OPERAND (TREE_OPERAND (incr, 1), 1)
15172 = c_get_temp_regvar (TREE_TYPE (t), t);
15173 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
15174 OMP_CLAUSE_DECL (c) = TREE_OPERAND (TREE_OPERAND (incr, 1), 1);
15175 OMP_CLAUSE_CHAIN (c) = clauses;
15176 clauses = c;
15177 }
15178 }
15179 t = TREE_OPERAND (init, 1);
15180 if (TREE_CODE (t) != INTEGER_CST)
15181 {
15182 TREE_OPERAND (init, 1) = c_get_temp_regvar (TREE_TYPE (t), t);
15183 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
15184 OMP_CLAUSE_DECL (c) = TREE_OPERAND (init, 1);
15185 OMP_CLAUSE_CHAIN (c) = clauses;
15186 clauses = c;
15187 }
15188 c = build_omp_clause (input_location, OMP_CLAUSE_PRIVATE);
15189 OMP_CLAUSE_DECL (c) = decl;
15190 OMP_CLAUSE_CHAIN (c) = clauses;
15191 clauses = c;
15192 c = build_omp_clause (input_location, OMP_CLAUSE__CILK_FOR_COUNT_);
15193 OMP_CLAUSE_OPERAND (c, 0)
15194 = cilk_for_number_of_iterations (omp_for);
15195 OMP_CLAUSE_CHAIN (c) = clauses;
15196 OMP_PARALLEL_CLAUSES (omp_par) = c_finish_omp_clauses (c);
15197 add_stmt (omp_par);
15198 }
15199
15200 block = c_end_compound_stmt (loc, block, true);
15201 add_stmt (block);
15202 }
15203
15204 \f
15205 /* Parse a transaction attribute (GCC Extension).
15206
15207 transaction-attribute:
15208 attributes
15209 [ [ any-word ] ]
15210
15211 The transactional memory language description is written for C++,
15212 and uses the C++0x attribute syntax. For compatibility, allow the
15213 bracket style for transactions in C as well. */
15214
15215 static tree
15216 c_parser_transaction_attributes (c_parser *parser)
15217 {
15218 tree attr_name, attr = NULL;
15219
15220 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
15221 return c_parser_attributes (parser);
15222
15223 if (!c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
15224 return NULL_TREE;
15225 c_parser_consume_token (parser);
15226 if (!c_parser_require (parser, CPP_OPEN_SQUARE, "expected %<[%>"))
15227 goto error1;
15228
15229 attr_name = c_parser_attribute_any_word (parser);
15230 if (attr_name)
15231 {
15232 c_parser_consume_token (parser);
15233 attr = build_tree_list (attr_name, NULL_TREE);
15234 }
15235 else
15236 c_parser_error (parser, "expected identifier");
15237
15238 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, "expected %<]%>");
15239 error1:
15240 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, "expected %<]%>");
15241 return attr;
15242 }
15243
15244 /* Parse a __transaction_atomic or __transaction_relaxed statement
15245 (GCC Extension).
15246
15247 transaction-statement:
15248 __transaction_atomic transaction-attribute[opt] compound-statement
15249 __transaction_relaxed compound-statement
15250
15251 Note that the only valid attribute is: "outer".
15252 */
15253
15254 static tree
15255 c_parser_transaction (c_parser *parser, enum rid keyword)
15256 {
15257 unsigned int old_in = parser->in_transaction;
15258 unsigned int this_in = 1, new_in;
15259 location_t loc = c_parser_peek_token (parser)->location;
15260 tree stmt, attrs;
15261
15262 gcc_assert ((keyword == RID_TRANSACTION_ATOMIC
15263 || keyword == RID_TRANSACTION_RELAXED)
15264 && c_parser_next_token_is_keyword (parser, keyword));
15265 c_parser_consume_token (parser);
15266
15267 if (keyword == RID_TRANSACTION_RELAXED)
15268 this_in |= TM_STMT_ATTR_RELAXED;
15269 else
15270 {
15271 attrs = c_parser_transaction_attributes (parser);
15272 if (attrs)
15273 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
15274 }
15275
15276 /* Keep track if we're in the lexical scope of an outer transaction. */
15277 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
15278
15279 parser->in_transaction = new_in;
15280 stmt = c_parser_compound_statement (parser);
15281 parser->in_transaction = old_in;
15282
15283 if (flag_tm)
15284 stmt = c_finish_transaction (loc, stmt, this_in);
15285 else
15286 error_at (loc, (keyword == RID_TRANSACTION_ATOMIC ?
15287 "%<__transaction_atomic%> without transactional memory support enabled"
15288 : "%<__transaction_relaxed %> "
15289 "without transactional memory support enabled"));
15290
15291 return stmt;
15292 }
15293
15294 /* Parse a __transaction_atomic or __transaction_relaxed expression
15295 (GCC Extension).
15296
15297 transaction-expression:
15298 __transaction_atomic ( expression )
15299 __transaction_relaxed ( expression )
15300 */
15301
15302 static struct c_expr
15303 c_parser_transaction_expression (c_parser *parser, enum rid keyword)
15304 {
15305 struct c_expr ret;
15306 unsigned int old_in = parser->in_transaction;
15307 unsigned int this_in = 1;
15308 location_t loc = c_parser_peek_token (parser)->location;
15309 tree attrs;
15310
15311 gcc_assert ((keyword == RID_TRANSACTION_ATOMIC
15312 || keyword == RID_TRANSACTION_RELAXED)
15313 && c_parser_next_token_is_keyword (parser, keyword));
15314 c_parser_consume_token (parser);
15315
15316 if (keyword == RID_TRANSACTION_RELAXED)
15317 this_in |= TM_STMT_ATTR_RELAXED;
15318 else
15319 {
15320 attrs = c_parser_transaction_attributes (parser);
15321 if (attrs)
15322 this_in |= parse_tm_stmt_attr (attrs, 0);
15323 }
15324
15325 parser->in_transaction = this_in;
15326 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
15327 {
15328 tree expr = c_parser_expression (parser).value;
15329 ret.original_type = TREE_TYPE (expr);
15330 ret.value = build1 (TRANSACTION_EXPR, ret.original_type, expr);
15331 if (this_in & TM_STMT_ATTR_RELAXED)
15332 TRANSACTION_EXPR_RELAXED (ret.value) = 1;
15333 SET_EXPR_LOCATION (ret.value, loc);
15334 ret.original_code = TRANSACTION_EXPR;
15335 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
15336 {
15337 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
15338 goto error;
15339 }
15340 }
15341 else
15342 {
15343 error:
15344 ret.value = error_mark_node;
15345 ret.original_code = ERROR_MARK;
15346 ret.original_type = NULL;
15347 }
15348 parser->in_transaction = old_in;
15349
15350 if (!flag_tm)
15351 error_at (loc, (keyword == RID_TRANSACTION_ATOMIC ?
15352 "%<__transaction_atomic%> without transactional memory support enabled"
15353 : "%<__transaction_relaxed %> "
15354 "without transactional memory support enabled"));
15355
15356 return ret;
15357 }
15358
15359 /* Parse a __transaction_cancel statement (GCC Extension).
15360
15361 transaction-cancel-statement:
15362 __transaction_cancel transaction-attribute[opt] ;
15363
15364 Note that the only valid attribute is "outer".
15365 */
15366
15367 static tree
15368 c_parser_transaction_cancel (c_parser *parser)
15369 {
15370 location_t loc = c_parser_peek_token (parser)->location;
15371 tree attrs;
15372 bool is_outer = false;
15373
15374 gcc_assert (c_parser_next_token_is_keyword (parser, RID_TRANSACTION_CANCEL));
15375 c_parser_consume_token (parser);
15376
15377 attrs = c_parser_transaction_attributes (parser);
15378 if (attrs)
15379 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
15380
15381 if (!flag_tm)
15382 {
15383 error_at (loc, "%<__transaction_cancel%> without "
15384 "transactional memory support enabled");
15385 goto ret_error;
15386 }
15387 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
15388 {
15389 error_at (loc, "%<__transaction_cancel%> within a "
15390 "%<__transaction_relaxed%>");
15391 goto ret_error;
15392 }
15393 else if (is_outer)
15394 {
15395 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
15396 && !is_tm_may_cancel_outer (current_function_decl))
15397 {
15398 error_at (loc, "outer %<__transaction_cancel%> not "
15399 "within outer %<__transaction_atomic%>");
15400 error_at (loc, " or a %<transaction_may_cancel_outer%> function");
15401 goto ret_error;
15402 }
15403 }
15404 else if (parser->in_transaction == 0)
15405 {
15406 error_at (loc, "%<__transaction_cancel%> not within "
15407 "%<__transaction_atomic%>");
15408 goto ret_error;
15409 }
15410
15411 return add_stmt (build_tm_abort_call (loc, is_outer));
15412
15413 ret_error:
15414 return build1 (NOP_EXPR, void_type_node, error_mark_node);
15415 }
15416 \f
15417 /* Parse a single source file. */
15418
15419 void
15420 c_parse_file (void)
15421 {
15422 /* Use local storage to begin. If the first token is a pragma, parse it.
15423 If it is #pragma GCC pch_preprocess, then this will load a PCH file
15424 which will cause garbage collection. */
15425 c_parser tparser;
15426
15427 memset (&tparser, 0, sizeof tparser);
15428 tparser.tokens = &tparser.tokens_buf[0];
15429 the_parser = &tparser;
15430
15431 if (c_parser_peek_token (&tparser)->pragma_kind == PRAGMA_GCC_PCH_PREPROCESS)
15432 c_parser_pragma_pch_preprocess (&tparser);
15433
15434 the_parser = ggc_alloc<c_parser> ();
15435 *the_parser = tparser;
15436 if (tparser.tokens == &tparser.tokens_buf[0])
15437 the_parser->tokens = &the_parser->tokens_buf[0];
15438
15439 /* Initialize EH, if we've been told to do so. */
15440 if (flag_exceptions)
15441 using_eh_for_cleanups ();
15442
15443 c_parser_translation_unit (the_parser);
15444 the_parser = NULL;
15445 }
15446
15447 /* This function parses Cilk Plus array notation. The starting index is
15448 passed in INITIAL_INDEX and the array name is passes in ARRAY_VALUE. The
15449 return value of this function is a tree_node called VALUE_TREE of type
15450 ARRAY_NOTATION_REF. */
15451
15452 static tree
15453 c_parser_array_notation (location_t loc, c_parser *parser, tree initial_index,
15454 tree array_value)
15455 {
15456 c_token *token = NULL;
15457 tree start_index = NULL_TREE, end_index = NULL_TREE, stride = NULL_TREE;
15458 tree value_tree = NULL_TREE, type = NULL_TREE, array_type = NULL_TREE;
15459 tree array_type_domain = NULL_TREE;
15460
15461 if (array_value == error_mark_node || initial_index == error_mark_node)
15462 {
15463 /* No need to continue. If either of these 2 were true, then an error
15464 must be emitted already. Thus, no need to emit them twice. */
15465 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
15466 return error_mark_node;
15467 }
15468
15469 array_type = TREE_TYPE (array_value);
15470 gcc_assert (array_type);
15471 if (TREE_CODE (array_type) != ARRAY_TYPE
15472 && TREE_CODE (array_type) != POINTER_TYPE)
15473 {
15474 error_at (loc, "base of array section must be pointer or array type");
15475 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
15476 return error_mark_node;
15477 }
15478 type = TREE_TYPE (array_type);
15479 token = c_parser_peek_token (parser);
15480
15481 if (token->type == CPP_EOF)
15482 {
15483 c_parser_error (parser, "expected %<:%> or numeral");
15484 return value_tree;
15485 }
15486 else if (token->type == CPP_COLON)
15487 {
15488 if (!initial_index)
15489 {
15490 /* If we are here, then we have a case like this A[:]. */
15491 c_parser_consume_token (parser);
15492 if (TREE_CODE (array_type) == POINTER_TYPE)
15493 {
15494 error_at (loc, "start-index and length fields necessary for "
15495 "using array notations in pointers");
15496 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
15497 return error_mark_node;
15498 }
15499 if (TREE_CODE (array_type) == FUNCTION_TYPE)
15500 {
15501 error_at (loc, "array notations cannot be used with function "
15502 "type");
15503 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
15504 return error_mark_node;
15505 }
15506 array_type_domain = TYPE_DOMAIN (array_type);
15507
15508 if (!array_type_domain)
15509 {
15510 error_at (loc, "start-index and length fields necessary for "
15511 "using array notations in dimensionless arrays");
15512 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
15513 return error_mark_node;
15514 }
15515
15516 start_index = TYPE_MINVAL (array_type_domain);
15517 start_index = fold_build1 (CONVERT_EXPR, ptrdiff_type_node,
15518 start_index);
15519 if (!TYPE_MAXVAL (array_type_domain)
15520 || !TREE_CONSTANT (TYPE_MAXVAL (array_type_domain)))
15521 {
15522 error_at (loc, "start-index and length fields necessary for "
15523 "using array notations in variable-length arrays");
15524 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
15525 return error_mark_node;
15526 }
15527 end_index = TYPE_MAXVAL (array_type_domain);
15528 end_index = fold_build2 (PLUS_EXPR, TREE_TYPE (end_index),
15529 end_index, integer_one_node);
15530 end_index = fold_build1 (CONVERT_EXPR, ptrdiff_type_node, end_index);
15531 stride = build_int_cst (integer_type_node, 1);
15532 stride = fold_build1 (CONVERT_EXPR, ptrdiff_type_node, stride);
15533 }
15534 else if (initial_index != error_mark_node)
15535 {
15536 /* If we are here, then there should be 2 possibilities:
15537 1. Array [EXPR : EXPR]
15538 2. Array [EXPR : EXPR : EXPR]
15539 */
15540 start_index = initial_index;
15541
15542 if (TREE_CODE (array_type) == FUNCTION_TYPE)
15543 {
15544 error_at (loc, "array notations cannot be used with function "
15545 "type");
15546 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
15547 return error_mark_node;
15548 }
15549 c_parser_consume_token (parser); /* consume the ':' */
15550 struct c_expr ce = c_parser_expression (parser);
15551 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
15552 end_index = ce.value;
15553 if (!end_index || end_index == error_mark_node)
15554 {
15555 c_parser_skip_to_end_of_block_or_statement (parser);
15556 return error_mark_node;
15557 }
15558 if (c_parser_peek_token (parser)->type == CPP_COLON)
15559 {
15560 c_parser_consume_token (parser);
15561 ce = c_parser_expression (parser);
15562 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
15563 stride = ce.value;
15564 if (!stride || stride == error_mark_node)
15565 {
15566 c_parser_skip_to_end_of_block_or_statement (parser);
15567 return error_mark_node;
15568 }
15569 }
15570 }
15571 else
15572 c_parser_error (parser, "expected array notation expression");
15573 }
15574 else
15575 c_parser_error (parser, "expected array notation expression");
15576
15577 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, "expected %<]%>");
15578
15579 value_tree = build_array_notation_ref (loc, array_value, start_index,
15580 end_index, stride, type);
15581 if (value_tree != error_mark_node)
15582 SET_EXPR_LOCATION (value_tree, loc);
15583 return value_tree;
15584 }
15585
15586 #include "gt-c-c-parser.h"