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