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