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