1 /* Parser for C and Objective-C.
2    Copyright (C) 1987-2018 Free Software Foundation, Inc.
3 
4    Parser actions based on the old Bison parser; structure somewhat
5    influenced by and fragments based on the C++ parser.
6 
7 This file is part of GCC.
8 
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 3, or (at your option) any later
12 version.
13 
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
17 for more details.
18 
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3.  If not see
21 <http://www.gnu.org/licenses/>.  */
22 
23 /* TODO:
24 
25    Make sure all relevant comments, and all relevant code from all
26    actions, brought over from old parser.  Verify exact correspondence
27    of syntax accepted.
28 
29    Add testcases covering every input symbol in every state in old and
30    new parsers.
31 
32    Include full syntax for GNU C, including erroneous cases accepted
33    with error messages, in syntax productions in comments.
34 
35    Make more diagnostics in the front end generally take an explicit
36    location rather than implicitly using input_location.  */
37 
38 #include "config.h"
39 #define INCLUDE_UNIQUE_PTR
40 #include "system.h"
41 #include "coretypes.h"
42 #include "target.h"
43 #include "function.h"
44 #include "c-tree.h"
45 #include "timevar.h"
46 #include "stringpool.h"
47 #include "cgraph.h"
48 #include "attribs.h"
49 #include "stor-layout.h"
50 #include "varasm.h"
51 #include "trans-mem.h"
52 #include "c-family/c-pragma.h"
53 #include "c-lang.h"
54 #include "c-family/c-objc.h"
55 #include "plugin.h"
56 #include "omp-general.h"
57 #include "omp-offload.h"
58 #include "builtins.h"
59 #include "gomp-constants.h"
60 #include "c-family/c-indentation.h"
61 #include "gimple-expr.h"
62 #include "context.h"
63 #include "gcc-rich-location.h"
64 #include "c-parser.h"
65 #include "gimple-parser.h"
66 #include "read-rtl-function.h"
67 #include "run-rtl-passes.h"
68 #include "intl.h"
69 #include "c-family/name-hint.h"
70 #include "tree-iterator.h"
71 
72 /* We need to walk over decls with incomplete struct/union/enum types
73    after parsing the whole translation unit.
74    In finish_decl(), if the decl is static, has incomplete
75    struct/union/enum type, it is appeneded to incomplete_record_decls.
76    In c_parser_translation_unit(), we iterate over incomplete_record_decls
77    and report error if any of the decls are still incomplete.  */
78 
79 vec<tree> incomplete_record_decls;
80 
81 void
set_c_expr_source_range(c_expr * expr,location_t start,location_t finish)82 set_c_expr_source_range (c_expr *expr,
83 			 location_t start, location_t finish)
84 {
85   expr->src_range.m_start = start;
86   expr->src_range.m_finish = finish;
87   if (expr->value)
88     set_source_range (expr->value, start, finish);
89 }
90 
91 void
set_c_expr_source_range(c_expr * expr,source_range src_range)92 set_c_expr_source_range (c_expr *expr,
93 			 source_range src_range)
94 {
95   expr->src_range = src_range;
96   if (expr->value)
97     set_source_range (expr->value, src_range);
98 }
99 
100 
101 /* Initialization routine for this file.  */
102 
103 void
c_parse_init(void)104 c_parse_init (void)
105 {
106   /* The only initialization required is of the reserved word
107      identifiers.  */
108   unsigned int i;
109   tree id;
110   int mask = 0;
111 
112   /* Make sure RID_MAX hasn't grown past the 8 bits used to hold the keyword in
113      the c_token structure.  */
114   gcc_assert (RID_MAX <= 255);
115 
116   mask |= D_CXXONLY;
117   if (!flag_isoc99)
118     mask |= D_C99;
119   if (flag_no_asm)
120     {
121       mask |= D_ASM | D_EXT;
122       if (!flag_isoc99)
123 	mask |= D_EXT89;
124     }
125   if (!c_dialect_objc ())
126     mask |= D_OBJC | D_CXX_OBJC;
127 
128   ridpointers = ggc_cleared_vec_alloc<tree> ((int) RID_MAX);
129   for (i = 0; i < num_c_common_reswords; i++)
130     {
131       /* If a keyword is disabled, do not enter it into the table
132 	 and so create a canonical spelling that isn't a keyword.  */
133       if (c_common_reswords[i].disable & mask)
134 	{
135 	  if (warn_cxx_compat
136 	      && (c_common_reswords[i].disable & D_CXXWARN))
137 	    {
138 	      id = get_identifier (c_common_reswords[i].word);
139 	      C_SET_RID_CODE (id, RID_CXX_COMPAT_WARN);
140 	      C_IS_RESERVED_WORD (id) = 1;
141 	    }
142 	  continue;
143 	}
144 
145       id = get_identifier (c_common_reswords[i].word);
146       C_SET_RID_CODE (id, c_common_reswords[i].rid);
147       C_IS_RESERVED_WORD (id) = 1;
148       ridpointers [(int) c_common_reswords[i].rid] = id;
149     }
150 
151   for (i = 0; i < NUM_INT_N_ENTS; i++)
152     {
153       /* We always create the symbols but they aren't always supported.  */
154       char name[50];
155       sprintf (name, "__int%d", int_n_data[i].bitsize);
156       id = get_identifier (name);
157       C_SET_RID_CODE (id, RID_FIRST_INT_N + i);
158       C_IS_RESERVED_WORD (id) = 1;
159     }
160 }
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 struct GTY(()) c_parser {
166   /* The look-ahead tokens.  */
167   c_token * GTY((skip)) tokens;
168   /* Buffer for look-ahead tokens.  */
169   c_token tokens_buf[4];
170   /* How many look-ahead tokens are available (0 - 4, or
171      more if parsing from pre-lexed tokens).  */
172   unsigned int tokens_avail;
173   /* True if a syntax error is being recovered from; false otherwise.
174      c_parser_error sets this flag.  It should clear this flag when
175      enough tokens have been consumed to recover from the error.  */
176   BOOL_BITFIELD error : 1;
177   /* True if we're processing a pragma, and shouldn't automatically
178      consume CPP_PRAGMA_EOL.  */
179   BOOL_BITFIELD in_pragma : 1;
180   /* True if we're parsing the outermost block of an if statement.  */
181   BOOL_BITFIELD in_if_block : 1;
182   /* True if we want to lex an untranslated string.  */
183   BOOL_BITFIELD lex_untranslated_string : 1;
184 
185   /* Objective-C specific parser/lexer information.  */
186 
187   /* True if we are in a context where the Objective-C "PQ" keywords
188      are considered keywords.  */
189   BOOL_BITFIELD objc_pq_context : 1;
190   /* True if we are parsing a (potential) Objective-C foreach
191      statement.  This is set to true after we parsed 'for (' and while
192      we wait for 'in' or ';' to decide if it's a standard C for loop or an
193      Objective-C foreach loop.  */
194   BOOL_BITFIELD objc_could_be_foreach_context : 1;
195   /* The following flag is needed to contextualize Objective-C lexical
196      analysis.  In some cases (e.g., 'int NSObject;'), it is
197      undesirable to bind an identifier to an Objective-C class, even
198      if a class with that name exists.  */
199   BOOL_BITFIELD objc_need_raw_identifier : 1;
200   /* Nonzero if we're processing a __transaction statement.  The value
201      is 1 | TM_STMT_ATTR_*.  */
202   unsigned int in_transaction : 4;
203   /* True if we are in a context where the Objective-C "Property attribute"
204      keywords are valid.  */
205   BOOL_BITFIELD objc_property_attr_context : 1;
206 
207   /* Location of the last consumed token.  */
208   location_t last_token_location;
209 };
210 
211 /* Return a pointer to the Nth token in PARSERs tokens_buf.  */
212 
213 c_token *
c_parser_tokens_buf(c_parser * parser,unsigned n)214 c_parser_tokens_buf (c_parser *parser, unsigned n)
215 {
216   return &parser->tokens_buf[n];
217 }
218 
219 /* Return the error state of PARSER.  */
220 
221 bool
c_parser_error(c_parser * parser)222 c_parser_error (c_parser *parser)
223 {
224   return parser->error;
225 }
226 
227 /* Set the error state of PARSER to ERR.  */
228 
229 void
c_parser_set_error(c_parser * parser,bool err)230 c_parser_set_error (c_parser *parser, bool err)
231 {
232   parser->error = err;
233 }
234 
235 
236 /* The actual parser and external interface.  ??? Does this need to be
237    garbage-collected?  */
238 
239 static GTY (()) c_parser *the_parser;
240 
241 /* Read in and lex a single token, storing it in *TOKEN.  */
242 
243 static void
c_lex_one_token(c_parser * parser,c_token * token)244 c_lex_one_token (c_parser *parser, c_token *token)
245 {
246   timevar_push (TV_LEX);
247 
248   token->type = c_lex_with_flags (&token->value, &token->location,
249 				  &token->flags,
250 				  (parser->lex_untranslated_string
251 				   ? C_LEX_STRING_NO_TRANSLATE : 0));
252   token->id_kind = C_ID_NONE;
253   token->keyword = RID_MAX;
254   token->pragma_kind = PRAGMA_NONE;
255 
256   switch (token->type)
257     {
258     case CPP_NAME:
259       {
260 	tree decl;
261 
262 	bool objc_force_identifier = parser->objc_need_raw_identifier;
263 	if (c_dialect_objc ())
264 	  parser->objc_need_raw_identifier = false;
265 
266 	if (C_IS_RESERVED_WORD (token->value))
267 	  {
268 	    enum rid rid_code = C_RID_CODE (token->value);
269 
270 	    if (rid_code == RID_CXX_COMPAT_WARN)
271 	      {
272 		warning_at (token->location,
273 			    OPT_Wc___compat,
274 			    "identifier %qE conflicts with C++ keyword",
275 			    token->value);
276 	      }
277 	    else if (rid_code >= RID_FIRST_ADDR_SPACE
278 		     && rid_code <= RID_LAST_ADDR_SPACE)
279 	      {
280 		addr_space_t as;
281 		as = (addr_space_t) (rid_code - RID_FIRST_ADDR_SPACE);
282 		targetm.addr_space.diagnose_usage (as, token->location);
283 		token->id_kind = C_ID_ADDRSPACE;
284 		token->keyword = rid_code;
285 		break;
286 	      }
287 	    else if (c_dialect_objc () && OBJC_IS_PQ_KEYWORD (rid_code))
288 	      {
289 		/* We found an Objective-C "pq" keyword (in, out,
290 		   inout, bycopy, byref, oneway).  They need special
291 		   care because the interpretation depends on the
292 		   context.  */
293 		if (parser->objc_pq_context)
294 		  {
295 		    token->type = CPP_KEYWORD;
296 		    token->keyword = rid_code;
297 		    break;
298 		  }
299 		else if (parser->objc_could_be_foreach_context
300 			 && rid_code == RID_IN)
301 		  {
302 		    /* We are in Objective-C, inside a (potential)
303 		       foreach context (which means after having
304 		       parsed 'for (', but before having parsed ';'),
305 		       and we found 'in'.  We consider it the keyword
306 		       which terminates the declaration at the
307 		       beginning of a foreach-statement.  Note that
308 		       this means you can't use 'in' for anything else
309 		       in that context; in particular, in Objective-C
310 		       you can't use 'in' as the name of the running
311 		       variable in a C for loop.  We could potentially
312 		       try to add code here to disambiguate, but it
313 		       seems a reasonable limitation.  */
314 		    token->type = CPP_KEYWORD;
315 		    token->keyword = rid_code;
316 		    break;
317 		  }
318 		/* Else, "pq" keywords outside of the "pq" context are
319 		   not keywords, and we fall through to the code for
320 		   normal tokens.  */
321 	      }
322 	    else if (c_dialect_objc () && OBJC_IS_PATTR_KEYWORD (rid_code))
323 	      {
324 		/* We found an Objective-C "property attribute"
325 		   keyword (getter, setter, readonly, etc). These are
326 		   only valid in the property context.  */
327 		if (parser->objc_property_attr_context)
328 		  {
329 		    token->type = CPP_KEYWORD;
330 		    token->keyword = rid_code;
331 		    break;
332 		  }
333 		/* Else they are not special keywords.
334 		*/
335 	      }
336 	    else if (c_dialect_objc ()
337 		     && (OBJC_IS_AT_KEYWORD (rid_code)
338 			 || OBJC_IS_CXX_KEYWORD (rid_code)))
339 	      {
340 		/* We found one of the Objective-C "@" keywords (defs,
341 		   selector, synchronized, etc) or one of the
342 		   Objective-C "cxx" keywords (class, private,
343 		   protected, public, try, catch, throw) without a
344 		   preceding '@' sign.  Do nothing and fall through to
345 		   the code for normal tokens (in C++ we would still
346 		   consider the CXX ones keywords, but not in C).  */
347 		;
348 	      }
349 	    else
350 	      {
351 		token->type = CPP_KEYWORD;
352 		token->keyword = rid_code;
353 		break;
354 	      }
355 	  }
356 
357 	decl = lookup_name (token->value);
358 	if (decl)
359 	  {
360 	    if (TREE_CODE (decl) == TYPE_DECL)
361 	      {
362 		token->id_kind = C_ID_TYPENAME;
363 		break;
364 	      }
365 	  }
366 	else if (c_dialect_objc ())
367 	  {
368 	    tree objc_interface_decl = objc_is_class_name (token->value);
369 	    /* Objective-C class names are in the same namespace as
370 	       variables and typedefs, and hence are shadowed by local
371 	       declarations.  */
372 	    if (objc_interface_decl
373                 && (!objc_force_identifier || global_bindings_p ()))
374 	      {
375 		token->value = objc_interface_decl;
376 		token->id_kind = C_ID_CLASSNAME;
377 		break;
378 	      }
379 	  }
380         token->id_kind = C_ID_ID;
381       }
382       break;
383     case CPP_AT_NAME:
384       /* This only happens in Objective-C; it must be a keyword.  */
385       token->type = CPP_KEYWORD;
386       switch (C_RID_CODE (token->value))
387 	{
388 	  /* Replace 'class' with '@class', 'private' with '@private',
389 	     etc.  This prevents confusion with the C++ keyword
390 	     'class', and makes the tokens consistent with other
391 	     Objective-C 'AT' keywords.  For example '@class' is
392 	     reported as RID_AT_CLASS which is consistent with
393 	     '@synchronized', which is reported as
394 	     RID_AT_SYNCHRONIZED.
395 	  */
396 	case RID_CLASS:     token->keyword = RID_AT_CLASS; break;
397 	case RID_PRIVATE:   token->keyword = RID_AT_PRIVATE; break;
398 	case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
399 	case RID_PUBLIC:    token->keyword = RID_AT_PUBLIC; break;
400 	case RID_THROW:     token->keyword = RID_AT_THROW; break;
401 	case RID_TRY:       token->keyword = RID_AT_TRY; break;
402 	case RID_CATCH:     token->keyword = RID_AT_CATCH; break;
403 	case RID_SYNCHRONIZED: token->keyword = RID_AT_SYNCHRONIZED; break;
404 	default:            token->keyword = C_RID_CODE (token->value);
405 	}
406       break;
407     case CPP_COLON:
408     case CPP_COMMA:
409     case CPP_CLOSE_PAREN:
410     case CPP_SEMICOLON:
411       /* These tokens may affect the interpretation of any identifiers
412 	 following, if doing Objective-C.  */
413       if (c_dialect_objc ())
414 	parser->objc_need_raw_identifier = false;
415       break;
416     case CPP_PRAGMA:
417       /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST.  */
418       token->pragma_kind = (enum pragma_kind) TREE_INT_CST_LOW (token->value);
419       token->value = NULL;
420       break;
421     default:
422       break;
423     }
424   timevar_pop (TV_LEX);
425 }
426 
427 /* Return a pointer to the next token from PARSER, reading it in if
428    necessary.  */
429 
430 c_token *
c_parser_peek_token(c_parser * parser)431 c_parser_peek_token (c_parser *parser)
432 {
433   if (parser->tokens_avail == 0)
434     {
435       c_lex_one_token (parser, &parser->tokens[0]);
436       parser->tokens_avail = 1;
437     }
438   return &parser->tokens[0];
439 }
440 
441 /* Return a pointer to the next-but-one token from PARSER, reading it
442    in if necessary.  The next token is already read in.  */
443 
444 c_token *
c_parser_peek_2nd_token(c_parser * parser)445 c_parser_peek_2nd_token (c_parser *parser)
446 {
447   if (parser->tokens_avail >= 2)
448     return &parser->tokens[1];
449   gcc_assert (parser->tokens_avail == 1);
450   gcc_assert (parser->tokens[0].type != CPP_EOF);
451   gcc_assert (parser->tokens[0].type != CPP_PRAGMA_EOL);
452   c_lex_one_token (parser, &parser->tokens[1]);
453   parser->tokens_avail = 2;
454   return &parser->tokens[1];
455 }
456 
457 /* Return a pointer to the Nth token from PARSER, reading it
458    in if necessary.  The N-1th token is already read in.  */
459 
460 c_token *
c_parser_peek_nth_token(c_parser * parser,unsigned int n)461 c_parser_peek_nth_token (c_parser *parser, unsigned int n)
462 {
463   /* N is 1-based, not zero-based.  */
464   gcc_assert (n > 0);
465 
466   if (parser->tokens_avail >= n)
467     return &parser->tokens[n - 1];
468   gcc_assert (parser->tokens_avail == n - 1);
469   c_lex_one_token (parser, &parser->tokens[n - 1]);
470   parser->tokens_avail = n;
471   return &parser->tokens[n - 1];
472 }
473 
474 bool
c_keyword_starts_typename(enum rid keyword)475 c_keyword_starts_typename (enum rid keyword)
476 {
477   switch (keyword)
478     {
479     case RID_UNSIGNED:
480     case RID_LONG:
481     case RID_SHORT:
482     case RID_SIGNED:
483     case RID_COMPLEX:
484     case RID_INT:
485     case RID_CHAR:
486     case RID_FLOAT:
487     case RID_DOUBLE:
488     case RID_VOID:
489     case RID_DFLOAT32:
490     case RID_DFLOAT64:
491     case RID_DFLOAT128:
492     CASE_RID_FLOATN_NX:
493     case RID_BOOL:
494     case RID_ENUM:
495     case RID_STRUCT:
496     case RID_UNION:
497     case RID_TYPEOF:
498     case RID_CONST:
499     case RID_ATOMIC:
500     case RID_VOLATILE:
501     case RID_RESTRICT:
502     case RID_ATTRIBUTE:
503     case RID_FRACT:
504     case RID_ACCUM:
505     case RID_SAT:
506     case RID_AUTO_TYPE:
507     case RID_ALIGNAS:
508       return true;
509     default:
510       if (keyword >= RID_FIRST_INT_N
511 	  && keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
512 	  && int_n_enabled_p[keyword - RID_FIRST_INT_N])
513 	return true;
514       return false;
515     }
516 }
517 
518 /* Return true if TOKEN can start a type name,
519    false otherwise.  */
520 bool
c_token_starts_typename(c_token * token)521 c_token_starts_typename (c_token *token)
522 {
523   switch (token->type)
524     {
525     case CPP_NAME:
526       switch (token->id_kind)
527 	{
528 	case C_ID_ID:
529 	  return false;
530 	case C_ID_ADDRSPACE:
531 	  return true;
532 	case C_ID_TYPENAME:
533 	  return true;
534 	case C_ID_CLASSNAME:
535 	  gcc_assert (c_dialect_objc ());
536 	  return true;
537 	default:
538 	  gcc_unreachable ();
539 	}
540     case CPP_KEYWORD:
541       return c_keyword_starts_typename (token->keyword);
542     case CPP_LESS:
543       if (c_dialect_objc ())
544 	return true;
545       return false;
546     default:
547       return false;
548     }
549 }
550 
551 /* Return true if the next token from PARSER can start a type name,
552    false otherwise.  LA specifies how to do lookahead in order to
553    detect unknown type names.  If unsure, pick CLA_PREFER_ID.  */
554 
555 static inline bool
c_parser_next_tokens_start_typename(c_parser * parser,enum c_lookahead_kind la)556 c_parser_next_tokens_start_typename (c_parser *parser, enum c_lookahead_kind la)
557 {
558   c_token *token = c_parser_peek_token (parser);
559   if (c_token_starts_typename (token))
560     return true;
561 
562   /* Try a bit harder to detect an unknown typename.  */
563   if (la != cla_prefer_id
564       && token->type == CPP_NAME
565       && token->id_kind == C_ID_ID
566 
567       /* Do not try too hard when we could have "object in array".  */
568       && !parser->objc_could_be_foreach_context
569 
570       && (la == cla_prefer_type
571 	  || c_parser_peek_2nd_token (parser)->type == CPP_NAME
572 	  || c_parser_peek_2nd_token (parser)->type == CPP_MULT)
573 
574       /* Only unknown identifiers.  */
575       && !lookup_name (token->value))
576     return true;
577 
578   return false;
579 }
580 
581 /* Return true if TOKEN is a type qualifier, false otherwise.  */
582 static bool
c_token_is_qualifier(c_token * token)583 c_token_is_qualifier (c_token *token)
584 {
585   switch (token->type)
586     {
587     case CPP_NAME:
588       switch (token->id_kind)
589 	{
590 	case C_ID_ADDRSPACE:
591 	  return true;
592 	default:
593 	  return false;
594 	}
595     case CPP_KEYWORD:
596       switch (token->keyword)
597 	{
598 	case RID_CONST:
599 	case RID_VOLATILE:
600 	case RID_RESTRICT:
601 	case RID_ATTRIBUTE:
602 	case RID_ATOMIC:
603 	  return true;
604 	default:
605 	  return false;
606 	}
607     case CPP_LESS:
608       return false;
609     default:
610       gcc_unreachable ();
611     }
612 }
613 
614 /* Return true if the next token from PARSER is a type qualifier,
615    false otherwise.  */
616 static inline bool
c_parser_next_token_is_qualifier(c_parser * parser)617 c_parser_next_token_is_qualifier (c_parser *parser)
618 {
619   c_token *token = c_parser_peek_token (parser);
620   return c_token_is_qualifier (token);
621 }
622 
623 /* Return true if TOKEN can start declaration specifiers, false
624    otherwise.  */
625 static bool
c_token_starts_declspecs(c_token * token)626 c_token_starts_declspecs (c_token *token)
627 {
628   switch (token->type)
629     {
630     case CPP_NAME:
631       switch (token->id_kind)
632 	{
633 	case C_ID_ID:
634 	  return false;
635 	case C_ID_ADDRSPACE:
636 	  return true;
637 	case C_ID_TYPENAME:
638 	  return true;
639 	case C_ID_CLASSNAME:
640 	  gcc_assert (c_dialect_objc ());
641 	  return true;
642 	default:
643 	  gcc_unreachable ();
644 	}
645     case CPP_KEYWORD:
646       switch (token->keyword)
647 	{
648 	case RID_STATIC:
649 	case RID_EXTERN:
650 	case RID_REGISTER:
651 	case RID_TYPEDEF:
652 	case RID_INLINE:
653 	case RID_NORETURN:
654 	case RID_AUTO:
655 	case RID_THREAD:
656 	case RID_UNSIGNED:
657 	case RID_LONG:
658 	case RID_SHORT:
659 	case RID_SIGNED:
660 	case RID_COMPLEX:
661 	case RID_INT:
662 	case RID_CHAR:
663 	case RID_FLOAT:
664 	case RID_DOUBLE:
665 	case RID_VOID:
666 	case RID_DFLOAT32:
667 	case RID_DFLOAT64:
668 	case RID_DFLOAT128:
669 	CASE_RID_FLOATN_NX:
670 	case RID_BOOL:
671 	case RID_ENUM:
672 	case RID_STRUCT:
673 	case RID_UNION:
674 	case RID_TYPEOF:
675 	case RID_CONST:
676 	case RID_VOLATILE:
677 	case RID_RESTRICT:
678 	case RID_ATTRIBUTE:
679 	case RID_FRACT:
680 	case RID_ACCUM:
681 	case RID_SAT:
682 	case RID_ALIGNAS:
683 	case RID_ATOMIC:
684 	case RID_AUTO_TYPE:
685 	  return true;
686 	default:
687 	  if (token->keyword >= RID_FIRST_INT_N
688 	      && token->keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
689 	      && int_n_enabled_p[token->keyword - RID_FIRST_INT_N])
690 	    return true;
691 	  return false;
692 	}
693     case CPP_LESS:
694       if (c_dialect_objc ())
695 	return true;
696       return false;
697     default:
698       return false;
699     }
700 }
701 
702 
703 /* Return true if TOKEN can start declaration specifiers or a static
704    assertion, false otherwise.  */
705 static bool
c_token_starts_declaration(c_token * token)706 c_token_starts_declaration (c_token *token)
707 {
708   if (c_token_starts_declspecs (token)
709       || token->keyword == RID_STATIC_ASSERT)
710     return true;
711   else
712     return false;
713 }
714 
715 /* Return true if the next token from PARSER can start declaration
716    specifiers, false otherwise.  */
717 bool
c_parser_next_token_starts_declspecs(c_parser * parser)718 c_parser_next_token_starts_declspecs (c_parser *parser)
719 {
720   c_token *token = c_parser_peek_token (parser);
721 
722   /* In Objective-C, a classname normally starts a declspecs unless it
723      is immediately followed by a dot.  In that case, it is the
724      Objective-C 2.0 "dot-syntax" for class objects, ie, calls the
725      setter/getter on the class.  c_token_starts_declspecs() can't
726      differentiate between the two cases because it only checks the
727      current token, so we have a special check here.  */
728   if (c_dialect_objc ()
729       && token->type == CPP_NAME
730       && token->id_kind == C_ID_CLASSNAME
731       && c_parser_peek_2nd_token (parser)->type == CPP_DOT)
732     return false;
733 
734   return c_token_starts_declspecs (token);
735 }
736 
737 /* Return true if the next tokens from PARSER can start declaration
738    specifiers or a static assertion, false otherwise.  */
739 bool
c_parser_next_tokens_start_declaration(c_parser * parser)740 c_parser_next_tokens_start_declaration (c_parser *parser)
741 {
742   c_token *token = c_parser_peek_token (parser);
743 
744   /* Same as above.  */
745   if (c_dialect_objc ()
746       && token->type == CPP_NAME
747       && token->id_kind == C_ID_CLASSNAME
748       && c_parser_peek_2nd_token (parser)->type == CPP_DOT)
749     return false;
750 
751   /* Labels do not start declarations.  */
752   if (token->type == CPP_NAME
753       && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
754     return false;
755 
756   if (c_token_starts_declaration (token))
757     return true;
758 
759   if (c_parser_next_tokens_start_typename (parser, cla_nonabstract_decl))
760     return true;
761 
762   return false;
763 }
764 
765 /* Consume the next token from PARSER.  */
766 
767 void
c_parser_consume_token(c_parser * parser)768 c_parser_consume_token (c_parser *parser)
769 {
770   gcc_assert (parser->tokens_avail >= 1);
771   gcc_assert (parser->tokens[0].type != CPP_EOF);
772   gcc_assert (!parser->in_pragma || parser->tokens[0].type != CPP_PRAGMA_EOL);
773   gcc_assert (parser->error || parser->tokens[0].type != CPP_PRAGMA);
774   parser->last_token_location = parser->tokens[0].location;
775   if (parser->tokens != &parser->tokens_buf[0])
776     parser->tokens++;
777   else if (parser->tokens_avail == 2)
778     parser->tokens[0] = parser->tokens[1];
779   parser->tokens_avail--;
780 }
781 
782 /* Expect the current token to be a #pragma.  Consume it and remember
783    that we've begun parsing a pragma.  */
784 
785 static void
c_parser_consume_pragma(c_parser * parser)786 c_parser_consume_pragma (c_parser *parser)
787 {
788   gcc_assert (!parser->in_pragma);
789   gcc_assert (parser->tokens_avail >= 1);
790   gcc_assert (parser->tokens[0].type == CPP_PRAGMA);
791   if (parser->tokens != &parser->tokens_buf[0])
792     parser->tokens++;
793   else if (parser->tokens_avail == 2)
794     parser->tokens[0] = parser->tokens[1];
795   parser->tokens_avail--;
796   parser->in_pragma = true;
797 }
798 
799 /* Update the global input_location from TOKEN.  */
800 static inline void
c_parser_set_source_position_from_token(c_token * token)801 c_parser_set_source_position_from_token (c_token *token)
802 {
803   if (token->type != CPP_EOF)
804     {
805       input_location = token->location;
806     }
807 }
808 
809 /* Helper function for c_parser_error.
810    Having peeked a token of kind TOK1_KIND that might signify
811    a conflict marker, peek successor tokens to determine
812    if we actually do have a conflict marker.
813    Specifically, we consider a run of 7 '<', '=' or '>' characters
814    at the start of a line as a conflict marker.
815    These come through the lexer as three pairs and a single,
816    e.g. three CPP_LSHIFT ("<<") and a CPP_LESS ('<').
817    If it returns true, *OUT_LOC is written to with the location/range
818    of the marker.  */
819 
820 static bool
c_parser_peek_conflict_marker(c_parser * parser,enum cpp_ttype tok1_kind,location_t * out_loc)821 c_parser_peek_conflict_marker (c_parser *parser, enum cpp_ttype tok1_kind,
822 			       location_t *out_loc)
823 {
824   c_token *token2 = c_parser_peek_2nd_token (parser);
825   if (token2->type != tok1_kind)
826     return false;
827   c_token *token3 = c_parser_peek_nth_token (parser, 3);
828   if (token3->type != tok1_kind)
829     return false;
830   c_token *token4 = c_parser_peek_nth_token (parser, 4);
831   if (token4->type != conflict_marker_get_final_tok_kind (tok1_kind))
832     return false;
833 
834   /* It must be at the start of the line.  */
835   location_t start_loc = c_parser_peek_token (parser)->location;
836   if (LOCATION_COLUMN (start_loc) != 1)
837     return false;
838 
839   /* We have a conflict marker.  Construct a location of the form:
840        <<<<<<<
841        ^~~~~~~
842      with start == caret, finishing at the end of the marker.  */
843   location_t finish_loc = get_finish (token4->location);
844   *out_loc = make_location (start_loc, start_loc, finish_loc);
845 
846   return true;
847 }
848 
849 /* Issue a diagnostic of the form
850       FILE:LINE: MESSAGE before TOKEN
851    where TOKEN is the next token in the input stream of PARSER.
852    MESSAGE (specified by the caller) is usually of the form "expected
853    OTHER-TOKEN".
854 
855    Use RICHLOC as the location of the diagnostic.
856 
857    Do not issue a diagnostic if still recovering from an error.
858 
859    Return true iff an error was actually emitted.
860 
861    ??? This is taken from the C++ parser, but building up messages in
862    this way is not i18n-friendly and some other approach should be
863    used.  */
864 
865 static bool
c_parser_error_richloc(c_parser * parser,const char * gmsgid,rich_location * richloc)866 c_parser_error_richloc (c_parser *parser, const char *gmsgid,
867 			rich_location *richloc)
868 {
869   c_token *token = c_parser_peek_token (parser);
870   if (parser->error)
871     return false;
872   parser->error = true;
873   if (!gmsgid)
874     return false;
875 
876   /* If this is actually a conflict marker, report it as such.  */
877   if (token->type == CPP_LSHIFT
878       || token->type == CPP_RSHIFT
879       || token->type == CPP_EQ_EQ)
880     {
881       location_t loc;
882       if (c_parser_peek_conflict_marker (parser, token->type, &loc))
883 	{
884 	  error_at (loc, "version control conflict marker in file");
885 	  return true;
886 	}
887     }
888 
889   c_parse_error (gmsgid,
890 		 /* Because c_parse_error does not understand
891 		    CPP_KEYWORD, keywords are treated like
892 		    identifiers.  */
893 		 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
894 		 /* ??? The C parser does not save the cpp flags of a
895 		    token, we need to pass 0 here and we will not get
896 		    the source spelling of some tokens but rather the
897 		    canonical spelling.  */
898 		 token->value, /*flags=*/0, richloc);
899   return true;
900 }
901 
902 /* As c_parser_error_richloc, but issue the message at the
903    location of PARSER's next token, or at input_location
904    if the next token is EOF.  */
905 
906 bool
c_parser_error(c_parser * parser,const char * gmsgid)907 c_parser_error (c_parser *parser, const char *gmsgid)
908 {
909   c_token *token = c_parser_peek_token (parser);
910   c_parser_set_source_position_from_token (token);
911   rich_location richloc (line_table, input_location);
912   return c_parser_error_richloc (parser, gmsgid, &richloc);
913 }
914 
915 /* Some tokens naturally come in pairs e.g.'(' and ')'.
916    This class is for tracking such a matching pair of symbols.
917    In particular, it tracks the location of the first token,
918    so that if the second token is missing, we can highlight the
919    location of the first token when notifying the user about the
920    problem.  */
921 
922 template <typename traits_t>
923 class token_pair
924 {
925  public:
926   /* token_pair's ctor.  */
token_pair()927   token_pair () : m_open_loc (UNKNOWN_LOCATION) {}
928 
929   /* If the next token is the opening symbol for this pair, consume it and
930      return true.
931      Otherwise, issue an error and return false.
932      In either case, record the location of the opening token.  */
933 
require_open(c_parser * parser)934   bool require_open (c_parser *parser)
935   {
936     c_token *token = c_parser_peek_token (parser);
937     if (token)
938       m_open_loc = token->location;
939 
940     return c_parser_require (parser, traits_t::open_token_type,
941 			     traits_t::open_gmsgid);
942   }
943 
944   /* Consume the next token from PARSER, recording its location as
945      that of the opening token within the pair.  */
946 
consume_open(c_parser * parser)947   void consume_open (c_parser *parser)
948   {
949     c_token *token = c_parser_peek_token (parser);
950     gcc_assert (token->type == traits_t::open_token_type);
951     m_open_loc = token->location;
952     c_parser_consume_token (parser);
953   }
954 
955   /* If the next token is the closing symbol for this pair, consume it
956      and return true.
957      Otherwise, issue an error, highlighting the location of the
958      corresponding opening token, and return false.  */
959 
require_close(c_parser * parser)960   bool require_close (c_parser *parser) const
961   {
962     return c_parser_require (parser, traits_t::close_token_type,
963 			     traits_t::close_gmsgid, m_open_loc);
964   }
965 
966   /* Like token_pair::require_close, except that tokens will be skipped
967      until the desired token is found.  An error message is still produced
968      if the next token is not as expected.  */
969 
skip_until_found_close(c_parser * parser)970   void skip_until_found_close (c_parser *parser) const
971   {
972     c_parser_skip_until_found (parser, traits_t::close_token_type,
973 			       traits_t::close_gmsgid, m_open_loc);
974   }
975 
976  private:
977   location_t m_open_loc;
978 };
979 
980 /* Traits for token_pair<T> for tracking matching pairs of parentheses.  */
981 
982 struct matching_paren_traits
983 {
984   static const enum cpp_ttype open_token_type = CPP_OPEN_PAREN;
985   static const char * const open_gmsgid;
986   static const enum cpp_ttype close_token_type = CPP_CLOSE_PAREN;
987   static const char * const close_gmsgid;
988 };
989 
990 const char * const matching_paren_traits::open_gmsgid = "expected %<(%>";
991 const char * const matching_paren_traits::close_gmsgid = "expected %<)%>";
992 
993 /* "matching_parens" is a token_pair<T> class for tracking matching
994    pairs of parentheses.  */
995 
996 typedef token_pair<matching_paren_traits> matching_parens;
997 
998 /* Traits for token_pair<T> for tracking matching pairs of braces.  */
999 
1000 struct matching_brace_traits
1001 {
1002   static const enum cpp_ttype open_token_type = CPP_OPEN_BRACE;
1003   static const char * const open_gmsgid;
1004   static const enum cpp_ttype close_token_type = CPP_CLOSE_BRACE;
1005   static const char * const close_gmsgid;
1006 };
1007 
1008 const char * const matching_brace_traits::open_gmsgid = "expected %<{%>";
1009 const char * const matching_brace_traits::close_gmsgid = "expected %<}%>";
1010 
1011 /* "matching_braces" is a token_pair<T> class for tracking matching
1012    pairs of braces.  */
1013 
1014 typedef token_pair<matching_brace_traits> matching_braces;
1015 
1016 /* Get a description of the matching symbol to TYPE e.g. "(" for
1017    CPP_CLOSE_PAREN.  */
1018 
1019 static const char *
get_matching_symbol(enum cpp_ttype type)1020 get_matching_symbol (enum cpp_ttype type)
1021 {
1022   switch (type)
1023     {
1024     default:
1025       gcc_unreachable ();
1026       return "";
1027     case CPP_CLOSE_PAREN:
1028       return "(";
1029     case CPP_CLOSE_BRACE:
1030       return "{";
1031     }
1032 }
1033 
1034 /* If the next token is of the indicated TYPE, consume it.  Otherwise,
1035    issue the error MSGID.  If MSGID is NULL then a message has already
1036    been produced and no message will be produced this time.  Returns
1037    true if found, false otherwise.
1038 
1039    If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
1040    within any error as the location of an "opening" token matching
1041    the close token TYPE (e.g. the location of the '(' when TYPE is
1042    CPP_CLOSE_PAREN).
1043 
1044    If TYPE_IS_UNIQUE is true (the default) then msgid describes exactly
1045    one type (e.g. "expected %<)%>") and thus it may be reasonable to
1046    attempt to generate a fix-it hint for the problem.
1047    Otherwise msgid describes multiple token types (e.g.
1048    "expected %<;%>, %<,%> or %<)%>"), and thus we shouldn't attempt to
1049    generate a fix-it hint.  */
1050 
1051 bool
c_parser_require(c_parser * parser,enum cpp_ttype type,const char * msgid,location_t matching_location,bool type_is_unique)1052 c_parser_require (c_parser *parser,
1053 		  enum cpp_ttype type,
1054 		  const char *msgid,
1055 		  location_t matching_location,
1056 		  bool type_is_unique)
1057 {
1058   if (c_parser_next_token_is (parser, type))
1059     {
1060       c_parser_consume_token (parser);
1061       return true;
1062     }
1063   else
1064     {
1065       location_t next_token_loc = c_parser_peek_token (parser)->location;
1066       gcc_rich_location richloc (next_token_loc);
1067 
1068       /* Potentially supply a fix-it hint, suggesting to add the
1069 	 missing token immediately after the *previous* token.
1070 	 This may move the primary location within richloc.  */
1071       if (!parser->error && type_is_unique)
1072 	maybe_suggest_missing_token_insertion (&richloc, type,
1073 					       parser->last_token_location);
1074 
1075       /* If matching_location != UNKNOWN_LOCATION, highlight it.
1076 	 Attempt to consolidate diagnostics by printing it as a
1077 	 secondary range within the main diagnostic.  */
1078       bool added_matching_location = false;
1079       if (matching_location != UNKNOWN_LOCATION)
1080 	added_matching_location
1081 	  = richloc.add_location_if_nearby (matching_location);
1082 
1083       if (c_parser_error_richloc (parser, msgid, &richloc))
1084 	/* If we weren't able to consolidate matching_location, then
1085 	   print it as a secondary diagnostic.  */
1086 	if (matching_location != UNKNOWN_LOCATION && !added_matching_location)
1087 	  inform (matching_location, "to match this %qs",
1088 		  get_matching_symbol (type));
1089 
1090       return false;
1091     }
1092 }
1093 
1094 /* If the next token is the indicated keyword, consume it.  Otherwise,
1095    issue the error MSGID.  Returns true if found, false otherwise.  */
1096 
1097 static bool
c_parser_require_keyword(c_parser * parser,enum rid keyword,const char * msgid)1098 c_parser_require_keyword (c_parser *parser,
1099 			  enum rid keyword,
1100 			  const char *msgid)
1101 {
1102   if (c_parser_next_token_is_keyword (parser, keyword))
1103     {
1104       c_parser_consume_token (parser);
1105       return true;
1106     }
1107   else
1108     {
1109       c_parser_error (parser, msgid);
1110       return false;
1111     }
1112 }
1113 
1114 /* Like c_parser_require, except that tokens will be skipped until the
1115    desired token is found.  An error message is still produced if the
1116    next token is not as expected.  If MSGID is NULL then a message has
1117    already been produced and no message will be produced this
1118    time.
1119 
1120    If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
1121    within any error as the location of an "opening" token matching
1122    the close token TYPE (e.g. the location of the '(' when TYPE is
1123    CPP_CLOSE_PAREN).  */
1124 
1125 void
c_parser_skip_until_found(c_parser * parser,enum cpp_ttype type,const char * msgid,location_t matching_location)1126 c_parser_skip_until_found (c_parser *parser,
1127 			   enum cpp_ttype type,
1128 			   const char *msgid,
1129 			   location_t matching_location)
1130 {
1131   unsigned nesting_depth = 0;
1132 
1133   if (c_parser_require (parser, type, msgid, matching_location))
1134     return;
1135 
1136   /* Skip tokens until the desired token is found.  */
1137   while (true)
1138     {
1139       /* Peek at the next token.  */
1140       c_token *token = c_parser_peek_token (parser);
1141       /* If we've reached the token we want, consume it and stop.  */
1142       if (token->type == type && !nesting_depth)
1143 	{
1144 	  c_parser_consume_token (parser);
1145 	  break;
1146 	}
1147 
1148       /* If we've run out of tokens, stop.  */
1149       if (token->type == CPP_EOF)
1150 	return;
1151       if (token->type == CPP_PRAGMA_EOL && parser->in_pragma)
1152 	return;
1153       if (token->type == CPP_OPEN_BRACE
1154 	  || token->type == CPP_OPEN_PAREN
1155 	  || token->type == CPP_OPEN_SQUARE)
1156 	++nesting_depth;
1157       else if (token->type == CPP_CLOSE_BRACE
1158 	       || token->type == CPP_CLOSE_PAREN
1159 	       || token->type == CPP_CLOSE_SQUARE)
1160 	{
1161 	  if (nesting_depth-- == 0)
1162 	    break;
1163 	}
1164       /* Consume this token.  */
1165       c_parser_consume_token (parser);
1166     }
1167   parser->error = false;
1168 }
1169 
1170 /* Skip tokens until the end of a parameter is found, but do not
1171    consume the comma, semicolon or closing delimiter.  */
1172 
1173 static void
c_parser_skip_to_end_of_parameter(c_parser * parser)1174 c_parser_skip_to_end_of_parameter (c_parser *parser)
1175 {
1176   unsigned nesting_depth = 0;
1177 
1178   while (true)
1179     {
1180       c_token *token = c_parser_peek_token (parser);
1181       if ((token->type == CPP_COMMA || token->type == CPP_SEMICOLON)
1182 	  && !nesting_depth)
1183 	break;
1184       /* If we've run out of tokens, stop.  */
1185       if (token->type == CPP_EOF)
1186 	return;
1187       if (token->type == CPP_PRAGMA_EOL && parser->in_pragma)
1188 	return;
1189       if (token->type == CPP_OPEN_BRACE
1190 	  || token->type == CPP_OPEN_PAREN
1191 	  || token->type == CPP_OPEN_SQUARE)
1192 	++nesting_depth;
1193       else if (token->type == CPP_CLOSE_BRACE
1194 	       || token->type == CPP_CLOSE_PAREN
1195 	       || token->type == CPP_CLOSE_SQUARE)
1196 	{
1197 	  if (nesting_depth-- == 0)
1198 	    break;
1199 	}
1200       /* Consume this token.  */
1201       c_parser_consume_token (parser);
1202     }
1203   parser->error = false;
1204 }
1205 
1206 /* Expect to be at the end of the pragma directive and consume an
1207    end of line marker.  */
1208 
1209 static void
1210 c_parser_skip_to_pragma_eol (c_parser *parser, bool error_if_not_eol = true)
1211 {
1212   gcc_assert (parser->in_pragma);
1213   parser->in_pragma = false;
1214 
1215   if (error_if_not_eol && c_parser_peek_token (parser)->type != CPP_PRAGMA_EOL)
1216     c_parser_error (parser, "expected end of line");
1217 
1218   cpp_ttype token_type;
1219   do
1220     {
1221       c_token *token = c_parser_peek_token (parser);
1222       token_type = token->type;
1223       if (token_type == CPP_EOF)
1224 	break;
1225       c_parser_consume_token (parser);
1226     }
1227   while (token_type != CPP_PRAGMA_EOL);
1228 
1229   parser->error = false;
1230 }
1231 
1232 /* Skip tokens until we have consumed an entire block, or until we
1233    have consumed a non-nested ';'.  */
1234 
1235 static void
c_parser_skip_to_end_of_block_or_statement(c_parser * parser)1236 c_parser_skip_to_end_of_block_or_statement (c_parser *parser)
1237 {
1238   unsigned nesting_depth = 0;
1239   bool save_error = parser->error;
1240 
1241   while (true)
1242     {
1243       c_token *token;
1244 
1245       /* Peek at the next token.  */
1246       token = c_parser_peek_token (parser);
1247 
1248       switch (token->type)
1249 	{
1250 	case CPP_EOF:
1251 	  return;
1252 
1253 	case CPP_PRAGMA_EOL:
1254 	  if (parser->in_pragma)
1255 	    return;
1256 	  break;
1257 
1258 	case CPP_SEMICOLON:
1259 	  /* If the next token is a ';', we have reached the
1260 	     end of the statement.  */
1261 	  if (!nesting_depth)
1262 	    {
1263 	      /* Consume the ';'.  */
1264 	      c_parser_consume_token (parser);
1265 	      goto finished;
1266 	    }
1267 	  break;
1268 
1269 	case CPP_CLOSE_BRACE:
1270 	  /* If the next token is a non-nested '}', then we have
1271 	     reached the end of the current block.  */
1272 	  if (nesting_depth == 0 || --nesting_depth == 0)
1273 	    {
1274 	      c_parser_consume_token (parser);
1275 	      goto finished;
1276 	    }
1277 	  break;
1278 
1279 	case CPP_OPEN_BRACE:
1280 	  /* If it the next token is a '{', then we are entering a new
1281 	     block.  Consume the entire block.  */
1282 	  ++nesting_depth;
1283 	  break;
1284 
1285 	case CPP_PRAGMA:
1286 	  /* If we see a pragma, consume the whole thing at once.  We
1287 	     have some safeguards against consuming pragmas willy-nilly.
1288 	     Normally, we'd expect to be here with parser->error set,
1289 	     which disables these safeguards.  But it's possible to get
1290 	     here for secondary error recovery, after parser->error has
1291 	     been cleared.  */
1292 	  c_parser_consume_pragma (parser);
1293 	  c_parser_skip_to_pragma_eol (parser);
1294 	  parser->error = save_error;
1295 	  continue;
1296 
1297 	default:
1298 	  break;
1299 	}
1300 
1301       c_parser_consume_token (parser);
1302     }
1303 
1304  finished:
1305   parser->error = false;
1306 }
1307 
1308 /* CPP's options (initialized by c-opts.c).  */
1309 extern cpp_options *cpp_opts;
1310 
1311 /* Save the warning flags which are controlled by __extension__.  */
1312 
1313 static inline int
disable_extension_diagnostics(void)1314 disable_extension_diagnostics (void)
1315 {
1316   int ret = (pedantic
1317 	     | (warn_pointer_arith << 1)
1318 	     | (warn_traditional << 2)
1319 	     | (flag_iso << 3)
1320 	     | (warn_long_long << 4)
1321 	     | (warn_cxx_compat << 5)
1322 	     | (warn_overlength_strings << 6)
1323 	     /* warn_c90_c99_compat has three states: -1/0/1, so we must
1324 		play tricks to properly restore it.  */
1325 	     | ((warn_c90_c99_compat == 1) << 7)
1326 	     | ((warn_c90_c99_compat == -1) << 8)
1327 	     /* Similarly for warn_c99_c11_compat.  */
1328 	     | ((warn_c99_c11_compat == 1) << 9)
1329 	     | ((warn_c99_c11_compat == -1) << 10)
1330 	     );
1331   cpp_opts->cpp_pedantic = pedantic = 0;
1332   warn_pointer_arith = 0;
1333   cpp_opts->cpp_warn_traditional = warn_traditional = 0;
1334   flag_iso = 0;
1335   cpp_opts->cpp_warn_long_long = warn_long_long = 0;
1336   warn_cxx_compat = 0;
1337   warn_overlength_strings = 0;
1338   warn_c90_c99_compat = 0;
1339   warn_c99_c11_compat = 0;
1340   return ret;
1341 }
1342 
1343 /* Restore the warning flags which are controlled by __extension__.
1344    FLAGS is the return value from disable_extension_diagnostics.  */
1345 
1346 static inline void
restore_extension_diagnostics(int flags)1347 restore_extension_diagnostics (int flags)
1348 {
1349   cpp_opts->cpp_pedantic = pedantic = flags & 1;
1350   warn_pointer_arith = (flags >> 1) & 1;
1351   cpp_opts->cpp_warn_traditional = warn_traditional = (flags >> 2) & 1;
1352   flag_iso = (flags >> 3) & 1;
1353   cpp_opts->cpp_warn_long_long = warn_long_long = (flags >> 4) & 1;
1354   warn_cxx_compat = (flags >> 5) & 1;
1355   warn_overlength_strings = (flags >> 6) & 1;
1356   /* See above for why is this needed.  */
1357   warn_c90_c99_compat = (flags >> 7) & 1 ? 1 : ((flags >> 8) & 1 ? -1 : 0);
1358   warn_c99_c11_compat = (flags >> 9) & 1 ? 1 : ((flags >> 10) & 1 ? -1 : 0);
1359 }
1360 
1361 /* Helper data structure for parsing #pragma acc routine.  */
1362 struct oacc_routine_data {
1363   bool error_seen; /* Set if error has been reported.  */
1364   bool fndecl_seen; /* Set if one fn decl/definition has been seen already.  */
1365   tree clauses;
1366   location_t loc;
1367 };
1368 
1369 static void c_parser_external_declaration (c_parser *);
1370 static void c_parser_asm_definition (c_parser *);
1371 static void c_parser_declaration_or_fndef (c_parser *, bool, bool, bool,
1372 					   bool, bool, tree *, vec<c_token>,
1373 					   struct oacc_routine_data * = NULL,
1374 					   bool * = NULL);
1375 static void c_parser_static_assert_declaration_no_semi (c_parser *);
1376 static void c_parser_static_assert_declaration (c_parser *);
1377 static struct c_typespec c_parser_enum_specifier (c_parser *);
1378 static struct c_typespec c_parser_struct_or_union_specifier (c_parser *);
1379 static tree c_parser_struct_declaration (c_parser *);
1380 static struct c_typespec c_parser_typeof_specifier (c_parser *);
1381 static tree c_parser_alignas_specifier (c_parser *);
1382 static struct c_declarator *c_parser_direct_declarator (c_parser *, bool,
1383 							c_dtr_syn, bool *);
1384 static struct c_declarator *c_parser_direct_declarator_inner (c_parser *,
1385 							      bool,
1386 							      struct c_declarator *);
1387 static struct c_arg_info *c_parser_parms_declarator (c_parser *, bool, tree);
1388 static struct c_arg_info *c_parser_parms_list_declarator (c_parser *, tree,
1389 							  tree);
1390 static struct c_parm *c_parser_parameter_declaration (c_parser *, tree);
1391 static tree c_parser_simple_asm_expr (c_parser *);
1392 static tree c_parser_attributes (c_parser *);
1393 static struct c_expr c_parser_initializer (c_parser *);
1394 static struct c_expr c_parser_braced_init (c_parser *, tree, bool,
1395 					   struct obstack *);
1396 static void c_parser_initelt (c_parser *, struct obstack *);
1397 static void c_parser_initval (c_parser *, struct c_expr *,
1398 			      struct obstack *);
1399 static tree c_parser_compound_statement (c_parser *);
1400 static void c_parser_compound_statement_nostart (c_parser *);
1401 static void c_parser_label (c_parser *);
1402 static void c_parser_statement (c_parser *, bool *, location_t * = NULL);
1403 static void c_parser_statement_after_labels (c_parser *, bool *,
1404 					     vec<tree> * = NULL);
1405 static tree c_parser_c99_block_statement (c_parser *, bool *,
1406 					  location_t * = NULL);
1407 static void c_parser_if_statement (c_parser *, bool *, vec<tree> *);
1408 static void c_parser_switch_statement (c_parser *, bool *);
1409 static void c_parser_while_statement (c_parser *, bool, unsigned short, bool *);
1410 static void c_parser_do_statement (c_parser *, bool, unsigned short);
1411 static void c_parser_for_statement (c_parser *, bool, unsigned short, bool *);
1412 static tree c_parser_asm_statement (c_parser *);
1413 static tree c_parser_asm_operands (c_parser *);
1414 static tree c_parser_asm_goto_operands (c_parser *);
1415 static tree c_parser_asm_clobbers (c_parser *);
1416 static struct c_expr c_parser_expr_no_commas (c_parser *, struct c_expr *,
1417 					      tree = NULL_TREE);
1418 static struct c_expr c_parser_conditional_expression (c_parser *,
1419 						      struct c_expr *, tree);
1420 static struct c_expr c_parser_binary_expression (c_parser *, struct c_expr *,
1421 						 tree);
1422 static struct c_expr c_parser_cast_expression (c_parser *, struct c_expr *);
1423 static struct c_expr c_parser_unary_expression (c_parser *);
1424 static struct c_expr c_parser_sizeof_expression (c_parser *);
1425 static struct c_expr c_parser_alignof_expression (c_parser *);
1426 static struct c_expr c_parser_postfix_expression (c_parser *);
1427 static struct c_expr c_parser_postfix_expression_after_paren_type (c_parser *,
1428 								   struct c_type_name *,
1429 								   location_t);
1430 static struct c_expr c_parser_postfix_expression_after_primary (c_parser *,
1431 								location_t loc,
1432 								struct c_expr);
1433 static tree c_parser_transaction (c_parser *, enum rid);
1434 static struct c_expr c_parser_transaction_expression (c_parser *, enum rid);
1435 static tree c_parser_transaction_cancel (c_parser *);
1436 static struct c_expr c_parser_expression (c_parser *);
1437 static struct c_expr c_parser_expression_conv (c_parser *);
1438 static vec<tree, va_gc> *c_parser_expr_list (c_parser *, bool, bool,
1439 					     vec<tree, va_gc> **, location_t *,
1440 					     tree *, vec<location_t> *,
1441 					     unsigned int * = NULL);
1442 static void c_parser_oacc_declare (c_parser *);
1443 static void c_parser_oacc_enter_exit_data (c_parser *, bool);
1444 static void c_parser_oacc_update (c_parser *);
1445 static void c_parser_omp_construct (c_parser *, bool *);
1446 static void c_parser_omp_threadprivate (c_parser *);
1447 static void c_parser_omp_barrier (c_parser *);
1448 static void c_parser_omp_flush (c_parser *);
1449 static tree c_parser_omp_for_loop (location_t, c_parser *, enum tree_code,
1450 				   tree, tree *, bool *);
1451 static void c_parser_omp_taskwait (c_parser *);
1452 static void c_parser_omp_taskyield (c_parser *);
1453 static void c_parser_omp_cancel (c_parser *);
1454 
1455 enum pragma_context { pragma_external, pragma_struct, pragma_param,
1456 		      pragma_stmt, pragma_compound };
1457 static bool c_parser_pragma (c_parser *, enum pragma_context, bool *);
1458 static void c_parser_omp_cancellation_point (c_parser *, enum pragma_context);
1459 static bool c_parser_omp_target (c_parser *, enum pragma_context, bool *);
1460 static void c_parser_omp_end_declare_target (c_parser *);
1461 static void c_parser_omp_declare (c_parser *, enum pragma_context);
1462 static bool c_parser_omp_ordered (c_parser *, enum pragma_context, bool *);
1463 static void c_parser_oacc_routine (c_parser *, enum pragma_context);
1464 
1465 /* These Objective-C parser functions are only ever called when
1466    compiling Objective-C.  */
1467 static void c_parser_objc_class_definition (c_parser *, tree);
1468 static void c_parser_objc_class_instance_variables (c_parser *);
1469 static void c_parser_objc_class_declaration (c_parser *);
1470 static void c_parser_objc_alias_declaration (c_parser *);
1471 static void c_parser_objc_protocol_definition (c_parser *, tree);
1472 static bool c_parser_objc_method_type (c_parser *);
1473 static void c_parser_objc_method_definition (c_parser *);
1474 static void c_parser_objc_methodprotolist (c_parser *);
1475 static void c_parser_objc_methodproto (c_parser *);
1476 static tree c_parser_objc_method_decl (c_parser *, bool, tree *, tree *);
1477 static tree c_parser_objc_type_name (c_parser *);
1478 static tree c_parser_objc_protocol_refs (c_parser *);
1479 static void c_parser_objc_try_catch_finally_statement (c_parser *);
1480 static void c_parser_objc_synchronized_statement (c_parser *);
1481 static tree c_parser_objc_selector (c_parser *);
1482 static tree c_parser_objc_selector_arg (c_parser *);
1483 static tree c_parser_objc_receiver (c_parser *);
1484 static tree c_parser_objc_message_args (c_parser *);
1485 static tree c_parser_objc_keywordexpr (c_parser *);
1486 static void c_parser_objc_at_property_declaration (c_parser *);
1487 static void c_parser_objc_at_synthesize_declaration (c_parser *);
1488 static void c_parser_objc_at_dynamic_declaration (c_parser *);
1489 static bool c_parser_objc_diagnose_bad_element_prefix
1490   (c_parser *, struct c_declspecs *);
1491 
1492 static void c_parser_parse_rtl_body (c_parser *parser, char *start_with_pass);
1493 
1494 /* Parse a translation unit (C90 6.7, C99 6.9, C11 6.9).
1495 
1496    translation-unit:
1497      external-declarations
1498 
1499    external-declarations:
1500      external-declaration
1501      external-declarations external-declaration
1502 
1503    GNU extensions:
1504 
1505    translation-unit:
1506      empty
1507 */
1508 
1509 static void
c_parser_translation_unit(c_parser * parser)1510 c_parser_translation_unit (c_parser *parser)
1511 {
1512   if (c_parser_next_token_is (parser, CPP_EOF))
1513     {
1514       pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
1515 	       "ISO C forbids an empty translation unit");
1516     }
1517   else
1518     {
1519       void *obstack_position = obstack_alloc (&parser_obstack, 0);
1520       mark_valid_location_for_stdc_pragma (false);
1521       do
1522 	{
1523 	  ggc_collect ();
1524 	  c_parser_external_declaration (parser);
1525 	  obstack_free (&parser_obstack, obstack_position);
1526 	}
1527       while (c_parser_next_token_is_not (parser, CPP_EOF));
1528     }
1529 
1530   unsigned int i;
1531   tree decl;
1532   FOR_EACH_VEC_ELT (incomplete_record_decls, i, decl)
1533     if (DECL_SIZE (decl) == NULL_TREE && TREE_TYPE (decl) != error_mark_node)
1534       error ("storage size of %q+D isn%'t known", decl);
1535 }
1536 
1537 /* Parse an external declaration (C90 6.7, C99 6.9, C11 6.9).
1538 
1539    external-declaration:
1540      function-definition
1541      declaration
1542 
1543    GNU extensions:
1544 
1545    external-declaration:
1546      asm-definition
1547      ;
1548      __extension__ external-declaration
1549 
1550    Objective-C:
1551 
1552    external-declaration:
1553      objc-class-definition
1554      objc-class-declaration
1555      objc-alias-declaration
1556      objc-protocol-definition
1557      objc-method-definition
1558      @end
1559 */
1560 
1561 static void
c_parser_external_declaration(c_parser * parser)1562 c_parser_external_declaration (c_parser *parser)
1563 {
1564   int ext;
1565   switch (c_parser_peek_token (parser)->type)
1566     {
1567     case CPP_KEYWORD:
1568       switch (c_parser_peek_token (parser)->keyword)
1569 	{
1570 	case RID_EXTENSION:
1571 	  ext = disable_extension_diagnostics ();
1572 	  c_parser_consume_token (parser);
1573 	  c_parser_external_declaration (parser);
1574 	  restore_extension_diagnostics (ext);
1575 	  break;
1576 	case RID_ASM:
1577 	  c_parser_asm_definition (parser);
1578 	  break;
1579 	case RID_AT_INTERFACE:
1580 	case RID_AT_IMPLEMENTATION:
1581 	  gcc_assert (c_dialect_objc ());
1582 	  c_parser_objc_class_definition (parser, NULL_TREE);
1583 	  break;
1584 	case RID_AT_CLASS:
1585 	  gcc_assert (c_dialect_objc ());
1586 	  c_parser_objc_class_declaration (parser);
1587 	  break;
1588 	case RID_AT_ALIAS:
1589 	  gcc_assert (c_dialect_objc ());
1590 	  c_parser_objc_alias_declaration (parser);
1591 	  break;
1592 	case RID_AT_PROTOCOL:
1593 	  gcc_assert (c_dialect_objc ());
1594 	  c_parser_objc_protocol_definition (parser, NULL_TREE);
1595 	  break;
1596 	case RID_AT_PROPERTY:
1597 	  gcc_assert (c_dialect_objc ());
1598 	  c_parser_objc_at_property_declaration (parser);
1599 	  break;
1600 	case RID_AT_SYNTHESIZE:
1601 	  gcc_assert (c_dialect_objc ());
1602 	  c_parser_objc_at_synthesize_declaration (parser);
1603 	  break;
1604 	case RID_AT_DYNAMIC:
1605 	  gcc_assert (c_dialect_objc ());
1606 	  c_parser_objc_at_dynamic_declaration (parser);
1607 	  break;
1608 	case RID_AT_END:
1609 	  gcc_assert (c_dialect_objc ());
1610 	  c_parser_consume_token (parser);
1611 	  objc_finish_implementation ();
1612 	  break;
1613 	default:
1614 	  goto decl_or_fndef;
1615 	}
1616       break;
1617     case CPP_SEMICOLON:
1618       pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
1619 	       "ISO C does not allow extra %<;%> outside of a function");
1620       c_parser_consume_token (parser);
1621       break;
1622     case CPP_PRAGMA:
1623       mark_valid_location_for_stdc_pragma (true);
1624       c_parser_pragma (parser, pragma_external, NULL);
1625       mark_valid_location_for_stdc_pragma (false);
1626       break;
1627     case CPP_PLUS:
1628     case CPP_MINUS:
1629       if (c_dialect_objc ())
1630 	{
1631 	  c_parser_objc_method_definition (parser);
1632 	  break;
1633 	}
1634       /* Else fall through, and yield a syntax error trying to parse
1635 	 as a declaration or function definition.  */
1636       /* FALLTHRU */
1637     default:
1638     decl_or_fndef:
1639       /* A declaration or a function definition (or, in Objective-C,
1640 	 an @interface or @protocol with prefix attributes).  We can
1641 	 only tell which after parsing the declaration specifiers, if
1642 	 any, and the first declarator.  */
1643       c_parser_declaration_or_fndef (parser, true, true, true, false, true,
1644 				     NULL, vNULL);
1645       break;
1646     }
1647 }
1648 
1649 static void c_finish_omp_declare_simd (c_parser *, tree, tree, vec<c_token>);
1650 static void c_finish_oacc_routine (struct oacc_routine_data *, tree, bool);
1651 
1652 /* Build and add a DEBUG_BEGIN_STMT statement with location LOC.  */
1653 
1654 static void
add_debug_begin_stmt(location_t loc)1655 add_debug_begin_stmt (location_t loc)
1656 {
1657   /* Don't add DEBUG_BEGIN_STMTs outside of functions, see PR84721.  */
1658   if (!MAY_HAVE_DEBUG_MARKER_STMTS || !building_stmt_list_p ())
1659     return;
1660 
1661   tree stmt = build0 (DEBUG_BEGIN_STMT, void_type_node);
1662   SET_EXPR_LOCATION (stmt, loc);
1663   add_stmt (stmt);
1664 }
1665 
1666 /* Parse a declaration or function definition (C90 6.5, 6.7.1, C99
1667    6.7, 6.9.1, C11 6.7, 6.9.1).  If FNDEF_OK is true, a function definition
1668    is accepted; otherwise (old-style parameter declarations) only other
1669    declarations are accepted.  If STATIC_ASSERT_OK is true, a static
1670    assertion is accepted; otherwise (old-style parameter declarations)
1671    it is not.  If NESTED is true, we are inside a function or parsing
1672    old-style parameter declarations; any functions encountered are
1673    nested functions and declaration specifiers are required; otherwise
1674    we are at top level and functions are normal functions and
1675    declaration specifiers may be optional.  If EMPTY_OK is true, empty
1676    declarations are OK (subject to all other constraints); otherwise
1677    (old-style parameter declarations) they are diagnosed.  If
1678    START_ATTR_OK is true, the declaration specifiers may start with
1679    attributes; otherwise they may not.
1680    OBJC_FOREACH_OBJECT_DECLARATION can be used to get back the parsed
1681    declaration when parsing an Objective-C foreach statement.
1682    FALLTHRU_ATTR_P is used to signal whether this function parsed
1683    "__attribute__((fallthrough));".
1684 
1685    declaration:
1686      declaration-specifiers init-declarator-list[opt] ;
1687      static_assert-declaration
1688 
1689    function-definition:
1690      declaration-specifiers[opt] declarator declaration-list[opt]
1691        compound-statement
1692 
1693    declaration-list:
1694      declaration
1695      declaration-list declaration
1696 
1697    init-declarator-list:
1698      init-declarator
1699      init-declarator-list , init-declarator
1700 
1701    init-declarator:
1702      declarator simple-asm-expr[opt] attributes[opt]
1703      declarator simple-asm-expr[opt] attributes[opt] = initializer
1704 
1705    GNU extensions:
1706 
1707    nested-function-definition:
1708      declaration-specifiers declarator declaration-list[opt]
1709        compound-statement
1710 
1711    attribute ;
1712 
1713    Objective-C:
1714      attributes objc-class-definition
1715      attributes objc-category-definition
1716      attributes objc-protocol-definition
1717 
1718    The simple-asm-expr and attributes are GNU extensions.
1719 
1720    This function does not handle __extension__; that is handled in its
1721    callers.  ??? Following the old parser, __extension__ may start
1722    external declarations, declarations in functions and declarations
1723    at the start of "for" loops, but not old-style parameter
1724    declarations.
1725 
1726    C99 requires declaration specifiers in a function definition; the
1727    absence is diagnosed through the diagnosis of implicit int.  In GNU
1728    C we also allow but diagnose declarations without declaration
1729    specifiers, but only at top level (elsewhere they conflict with
1730    other syntax).
1731 
1732    In Objective-C, declarations of the looping variable in a foreach
1733    statement are exceptionally terminated by 'in' (for example, 'for
1734    (NSObject *object in array) { ... }').
1735 
1736    OpenMP:
1737 
1738    declaration:
1739      threadprivate-directive
1740 
1741    GIMPLE:
1742 
1743    gimple-function-definition:
1744      declaration-specifiers[opt] __GIMPLE (gimple-or-rtl-pass-list) declarator
1745        declaration-list[opt] compound-statement
1746 
1747    rtl-function-definition:
1748      declaration-specifiers[opt] __RTL (gimple-or-rtl-pass-list) declarator
1749        declaration-list[opt] compound-statement  */
1750 
1751 static void
c_parser_declaration_or_fndef(c_parser * parser,bool fndef_ok,bool static_assert_ok,bool empty_ok,bool nested,bool start_attr_ok,tree * objc_foreach_object_declaration,vec<c_token> omp_declare_simd_clauses,struct oacc_routine_data * oacc_routine_data,bool * fallthru_attr_p)1752 c_parser_declaration_or_fndef (c_parser *parser, bool fndef_ok,
1753 			       bool static_assert_ok, bool empty_ok,
1754 			       bool nested, bool start_attr_ok,
1755 			       tree *objc_foreach_object_declaration,
1756 			       vec<c_token> omp_declare_simd_clauses,
1757 			       struct oacc_routine_data *oacc_routine_data,
1758 			       bool *fallthru_attr_p)
1759 {
1760   struct c_declspecs *specs;
1761   tree prefix_attrs;
1762   tree all_prefix_attrs;
1763   bool diagnosed_no_specs = false;
1764   location_t here = c_parser_peek_token (parser)->location;
1765 
1766   add_debug_begin_stmt (c_parser_peek_token (parser)->location);
1767 
1768   if (static_assert_ok
1769       && c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT))
1770     {
1771       c_parser_static_assert_declaration (parser);
1772       return;
1773     }
1774   specs = build_null_declspecs ();
1775 
1776   /* Try to detect an unknown type name when we have "A B" or "A *B".  */
1777   if (c_parser_peek_token (parser)->type == CPP_NAME
1778       && c_parser_peek_token (parser)->id_kind == C_ID_ID
1779       && (c_parser_peek_2nd_token (parser)->type == CPP_NAME
1780           || c_parser_peek_2nd_token (parser)->type == CPP_MULT)
1781       && (!nested || !lookup_name (c_parser_peek_token (parser)->value)))
1782     {
1783       tree name = c_parser_peek_token (parser)->value;
1784 
1785       /* Issue a warning about NAME being an unknown type name, perhaps
1786 	 with some kind of hint.
1787 	 If the user forgot a "struct" etc, suggest inserting
1788 	 it.  Otherwise, attempt to look for misspellings.  */
1789       gcc_rich_location richloc (here);
1790       if (tag_exists_p (RECORD_TYPE, name))
1791 	{
1792 	  /* This is not C++ with its implicit typedef.  */
1793 	  richloc.add_fixit_insert_before ("struct ");
1794 	  error_at (&richloc,
1795 		    "unknown type name %qE;"
1796 		    " use %<struct%> keyword to refer to the type",
1797 		    name);
1798 	}
1799       else if (tag_exists_p (UNION_TYPE, name))
1800 	{
1801 	  richloc.add_fixit_insert_before ("union ");
1802 	  error_at (&richloc,
1803 		    "unknown type name %qE;"
1804 		    " use %<union%> keyword to refer to the type",
1805 		    name);
1806 	}
1807       else if (tag_exists_p (ENUMERAL_TYPE, name))
1808 	{
1809 	  richloc.add_fixit_insert_before ("enum ");
1810 	  error_at (&richloc,
1811 		    "unknown type name %qE;"
1812 		    " use %<enum%> keyword to refer to the type",
1813 		    name);
1814 	}
1815       else
1816 	{
1817 	  name_hint hint = lookup_name_fuzzy (name, FUZZY_LOOKUP_TYPENAME,
1818 					      here);
1819 	  if (hint)
1820 	    {
1821 	      richloc.add_fixit_replace (hint.suggestion ());
1822 	      error_at (&richloc,
1823 			"unknown type name %qE; did you mean %qs?",
1824 			name, hint.suggestion ());
1825 	    }
1826 	  else
1827 	    error_at (here, "unknown type name %qE", name);
1828 	}
1829 
1830       /* Parse declspecs normally to get a correct pointer type, but avoid
1831          a further "fails to be a type name" error.  Refuse nested functions
1832          since it is not how the user likely wants us to recover.  */
1833       c_parser_peek_token (parser)->type = CPP_KEYWORD;
1834       c_parser_peek_token (parser)->keyword = RID_VOID;
1835       c_parser_peek_token (parser)->value = error_mark_node;
1836       fndef_ok = !nested;
1837     }
1838 
1839   c_parser_declspecs (parser, specs, true, true, start_attr_ok,
1840 		      true, true, cla_nonabstract_decl);
1841   if (parser->error)
1842     {
1843       c_parser_skip_to_end_of_block_or_statement (parser);
1844       return;
1845     }
1846   if (nested && !specs->declspecs_seen_p)
1847     {
1848       c_parser_error (parser, "expected declaration specifiers");
1849       c_parser_skip_to_end_of_block_or_statement (parser);
1850       return;
1851     }
1852 
1853   finish_declspecs (specs);
1854   bool auto_type_p = specs->typespec_word == cts_auto_type;
1855   if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1856     {
1857       if (auto_type_p)
1858 	error_at (here, "%<__auto_type%> in empty declaration");
1859       else if (specs->typespec_kind == ctsk_none
1860 	       && attribute_fallthrough_p (specs->attrs))
1861 	{
1862 	  if (fallthru_attr_p != NULL)
1863 	    *fallthru_attr_p = true;
1864 	  tree fn = build_call_expr_internal_loc (here, IFN_FALLTHROUGH,
1865 						  void_type_node, 0);
1866 	  add_stmt (fn);
1867 	}
1868       else if (empty_ok)
1869 	shadow_tag (specs);
1870       else
1871 	{
1872 	  shadow_tag_warned (specs, 1);
1873 	  pedwarn (here, 0, "empty declaration");
1874 	}
1875       c_parser_consume_token (parser);
1876       if (oacc_routine_data)
1877 	c_finish_oacc_routine (oacc_routine_data, NULL_TREE, false);
1878       return;
1879     }
1880 
1881   /* Provide better error recovery.  Note that a type name here is usually
1882      better diagnosed as a redeclaration.  */
1883   if (empty_ok
1884       && specs->typespec_kind == ctsk_tagdef
1885       && c_parser_next_token_starts_declspecs (parser)
1886       && !c_parser_next_token_is (parser, CPP_NAME))
1887     {
1888       c_parser_error (parser, "expected %<;%>, identifier or %<(%>");
1889       parser->error = false;
1890       shadow_tag_warned (specs, 1);
1891       return;
1892     }
1893   else if (c_dialect_objc () && !auto_type_p)
1894     {
1895       /* Prefix attributes are an error on method decls.  */
1896       switch (c_parser_peek_token (parser)->type)
1897 	{
1898 	  case CPP_PLUS:
1899 	  case CPP_MINUS:
1900 	    if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1901 	      return;
1902 	    if (specs->attrs)
1903 	      {
1904 		warning_at (c_parser_peek_token (parser)->location,
1905 			    OPT_Wattributes,
1906 	       		    "prefix attributes are ignored for methods");
1907 		specs->attrs = NULL_TREE;
1908 	      }
1909 	    if (fndef_ok)
1910 	      c_parser_objc_method_definition (parser);
1911 	    else
1912 	      c_parser_objc_methodproto (parser);
1913 	    return;
1914 	    break;
1915 	  default:
1916 	    break;
1917 	}
1918       /* This is where we parse 'attributes @interface ...',
1919 	 'attributes @implementation ...', 'attributes @protocol ...'
1920 	 (where attributes could be, for example, __attribute__
1921 	 ((deprecated)).
1922       */
1923       switch (c_parser_peek_token (parser)->keyword)
1924 	{
1925 	case RID_AT_INTERFACE:
1926 	  {
1927 	    if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1928 	      return;
1929 	    c_parser_objc_class_definition (parser, specs->attrs);
1930 	    return;
1931 	  }
1932 	  break;
1933 	case RID_AT_IMPLEMENTATION:
1934 	  {
1935 	    if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1936 	      return;
1937 	    if (specs->attrs)
1938 	      {
1939 		warning_at (c_parser_peek_token (parser)->location,
1940 			OPT_Wattributes,
1941 			"prefix attributes are ignored for implementations");
1942 		specs->attrs = NULL_TREE;
1943 	      }
1944 	    c_parser_objc_class_definition (parser, NULL_TREE);
1945 	    return;
1946 	  }
1947 	  break;
1948 	case RID_AT_PROTOCOL:
1949 	  {
1950 	    if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1951 	      return;
1952 	    c_parser_objc_protocol_definition (parser, specs->attrs);
1953 	    return;
1954 	  }
1955 	  break;
1956 	case RID_AT_ALIAS:
1957 	case RID_AT_CLASS:
1958 	case RID_AT_END:
1959 	case RID_AT_PROPERTY:
1960 	  if (specs->attrs)
1961 	    {
1962 	      c_parser_error (parser, "unexpected attribute");
1963 	      specs->attrs = NULL;
1964 	    }
1965 	  break;
1966 	default:
1967 	  break;
1968 	}
1969     }
1970   else if (attribute_fallthrough_p (specs->attrs))
1971     warning_at (here, OPT_Wattributes,
1972 		"%<fallthrough%> attribute not followed by %<;%>");
1973 
1974   pending_xref_error ();
1975   prefix_attrs = specs->attrs;
1976   all_prefix_attrs = prefix_attrs;
1977   specs->attrs = NULL_TREE;
1978   while (true)
1979     {
1980       struct c_declarator *declarator;
1981       bool dummy = false;
1982       timevar_id_t tv;
1983       tree fnbody = NULL_TREE;
1984       /* Declaring either one or more declarators (in which case we
1985 	 should diagnose if there were no declaration specifiers) or a
1986 	 function definition (in which case the diagnostic for
1987 	 implicit int suffices).  */
1988       declarator = c_parser_declarator (parser,
1989 					specs->typespec_kind != ctsk_none,
1990 					C_DTR_NORMAL, &dummy);
1991       if (declarator == NULL)
1992 	{
1993 	  if (omp_declare_simd_clauses.exists ())
1994 	    c_finish_omp_declare_simd (parser, NULL_TREE, NULL_TREE,
1995 				       omp_declare_simd_clauses);
1996 	  if (oacc_routine_data)
1997 	    c_finish_oacc_routine (oacc_routine_data, NULL_TREE, false);
1998 	  c_parser_skip_to_end_of_block_or_statement (parser);
1999 	  return;
2000 	}
2001       if (auto_type_p && declarator->kind != cdk_id)
2002 	{
2003 	  error_at (here,
2004 		    "%<__auto_type%> requires a plain identifier"
2005 		    " as declarator");
2006 	  c_parser_skip_to_end_of_block_or_statement (parser);
2007 	  return;
2008 	}
2009       if (c_parser_next_token_is (parser, CPP_EQ)
2010 	  || c_parser_next_token_is (parser, CPP_COMMA)
2011 	  || c_parser_next_token_is (parser, CPP_SEMICOLON)
2012 	  || c_parser_next_token_is_keyword (parser, RID_ASM)
2013 	  || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE)
2014 	  || c_parser_next_token_is_keyword (parser, RID_IN))
2015 	{
2016 	  tree asm_name = NULL_TREE;
2017 	  tree postfix_attrs = NULL_TREE;
2018 	  if (!diagnosed_no_specs && !specs->declspecs_seen_p)
2019 	    {
2020 	      diagnosed_no_specs = true;
2021 	      pedwarn (here, 0, "data definition has no type or storage class");
2022 	    }
2023 	  /* Having seen a data definition, there cannot now be a
2024 	     function definition.  */
2025 	  fndef_ok = false;
2026 	  if (c_parser_next_token_is_keyword (parser, RID_ASM))
2027 	    asm_name = c_parser_simple_asm_expr (parser);
2028 	  if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2029 	    {
2030 	      postfix_attrs = c_parser_attributes (parser);
2031 	      if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
2032 		{
2033 		  /* This means there is an attribute specifier after
2034 		     the declarator in a function definition.  Provide
2035 		     some more information for the user.  */
2036 		  error_at (here, "attributes should be specified before the "
2037 			    "declarator in a function definition");
2038 		  c_parser_skip_to_end_of_block_or_statement (parser);
2039 		  return;
2040 		}
2041 	    }
2042 	  if (c_parser_next_token_is (parser, CPP_EQ))
2043 	    {
2044 	      tree d;
2045 	      struct c_expr init;
2046 	      location_t init_loc;
2047 	      c_parser_consume_token (parser);
2048 	      if (auto_type_p)
2049 		{
2050 		  init_loc = c_parser_peek_token (parser)->location;
2051 		  rich_location richloc (line_table, init_loc);
2052 		  start_init (NULL_TREE, asm_name, global_bindings_p (), &richloc);
2053 		  /* A parameter is initialized, which is invalid.  Don't
2054 		     attempt to instrument the initializer.  */
2055 		  int flag_sanitize_save = flag_sanitize;
2056 		  if (nested && !empty_ok)
2057 		    flag_sanitize = 0;
2058 		  init = c_parser_expr_no_commas (parser, NULL);
2059 		  flag_sanitize = flag_sanitize_save;
2060 		  if (TREE_CODE (init.value) == COMPONENT_REF
2061 		      && DECL_C_BIT_FIELD (TREE_OPERAND (init.value, 1)))
2062 		    error_at (here,
2063 			      "%<__auto_type%> used with a bit-field"
2064 			      " initializer");
2065 		  init = convert_lvalue_to_rvalue (init_loc, init, true, true);
2066 		  tree init_type = TREE_TYPE (init.value);
2067 		  /* As with typeof, remove all qualifiers from atomic types.  */
2068 		  if (init_type != error_mark_node && TYPE_ATOMIC (init_type))
2069 		    init_type
2070 		      = c_build_qualified_type (init_type, TYPE_UNQUALIFIED);
2071 		  bool vm_type = variably_modified_type_p (init_type,
2072 							   NULL_TREE);
2073 		  if (vm_type)
2074 		    init.value = save_expr (init.value);
2075 		  finish_init ();
2076 		  specs->typespec_kind = ctsk_typeof;
2077 		  specs->locations[cdw_typedef] = init_loc;
2078 		  specs->typedef_p = true;
2079 		  specs->type = init_type;
2080 		  if (vm_type)
2081 		    {
2082 		      bool maybe_const = true;
2083 		      tree type_expr = c_fully_fold (init.value, false,
2084 						     &maybe_const);
2085 		      specs->expr_const_operands &= maybe_const;
2086 		      if (specs->expr)
2087 			specs->expr = build2 (COMPOUND_EXPR,
2088 					      TREE_TYPE (type_expr),
2089 					      specs->expr, type_expr);
2090 		      else
2091 			specs->expr = type_expr;
2092 		    }
2093 		  d = start_decl (declarator, specs, true,
2094 				  chainon (postfix_attrs, all_prefix_attrs));
2095 		  if (!d)
2096 		    d = error_mark_node;
2097 		  if (omp_declare_simd_clauses.exists ())
2098 		    c_finish_omp_declare_simd (parser, d, NULL_TREE,
2099 					       omp_declare_simd_clauses);
2100 		}
2101 	      else
2102 		{
2103 		  /* The declaration of the variable is in effect while
2104 		     its initializer is parsed.  */
2105 		  d = start_decl (declarator, specs, true,
2106 				  chainon (postfix_attrs, all_prefix_attrs));
2107 		  if (!d)
2108 		    d = error_mark_node;
2109 		  if (omp_declare_simd_clauses.exists ())
2110 		    c_finish_omp_declare_simd (parser, d, NULL_TREE,
2111 					       omp_declare_simd_clauses);
2112 		  init_loc = c_parser_peek_token (parser)->location;
2113 		  rich_location richloc (line_table, init_loc);
2114 		  start_init (d, asm_name, global_bindings_p (), &richloc);
2115 		  /* A parameter is initialized, which is invalid.  Don't
2116 		     attempt to instrument the initializer.  */
2117 		  int flag_sanitize_save = flag_sanitize;
2118 		  if (TREE_CODE (d) == PARM_DECL)
2119 		    flag_sanitize = 0;
2120 		  init = c_parser_initializer (parser);
2121 		  flag_sanitize = flag_sanitize_save;
2122 		  finish_init ();
2123 		}
2124 	      if (oacc_routine_data)
2125 		c_finish_oacc_routine (oacc_routine_data, d, false);
2126 	      if (d != error_mark_node)
2127 		{
2128 		  maybe_warn_string_init (init_loc, TREE_TYPE (d), init);
2129 		  finish_decl (d, init_loc, init.value,
2130 			       init.original_type, asm_name);
2131 		}
2132 	    }
2133 	  else
2134 	    {
2135 	      if (auto_type_p)
2136 		{
2137 		  error_at (here,
2138 			    "%<__auto_type%> requires an initialized "
2139 			    "data declaration");
2140 		  c_parser_skip_to_end_of_block_or_statement (parser);
2141 		  return;
2142 		}
2143 	      tree d = start_decl (declarator, specs, false,
2144 				   chainon (postfix_attrs,
2145 					    all_prefix_attrs));
2146 	      if (d
2147 		  && TREE_CODE (d) == FUNCTION_DECL
2148 		  && declarator->kind == cdk_function
2149 		  && DECL_ARGUMENTS (d) == NULL_TREE
2150 		  && DECL_INITIAL (d) == NULL_TREE)
2151 		DECL_ARGUMENTS (d) = declarator->u.arg_info->parms;
2152 	      if (omp_declare_simd_clauses.exists ())
2153 		{
2154 		  tree parms = NULL_TREE;
2155 		  if (d && TREE_CODE (d) == FUNCTION_DECL)
2156 		    {
2157 		      struct c_declarator *ce = declarator;
2158 		      while (ce != NULL)
2159 			if (ce->kind == cdk_function)
2160 			  {
2161 			    parms = ce->u.arg_info->parms;
2162 			    break;
2163 			  }
2164 			else
2165 			  ce = ce->declarator;
2166 		    }
2167 		  if (parms)
2168 		    temp_store_parm_decls (d, parms);
2169 		  c_finish_omp_declare_simd (parser, d, parms,
2170 					     omp_declare_simd_clauses);
2171 		  if (parms)
2172 		    temp_pop_parm_decls ();
2173 		}
2174 	      if (oacc_routine_data)
2175 		c_finish_oacc_routine (oacc_routine_data, d, false);
2176 	      if (d)
2177 		finish_decl (d, UNKNOWN_LOCATION, NULL_TREE,
2178 			     NULL_TREE, asm_name);
2179 
2180 	      if (c_parser_next_token_is_keyword (parser, RID_IN))
2181 		{
2182 		  if (d)
2183 		    *objc_foreach_object_declaration = d;
2184 		  else
2185 		    *objc_foreach_object_declaration = error_mark_node;
2186 		}
2187 	    }
2188 	  if (c_parser_next_token_is (parser, CPP_COMMA))
2189 	    {
2190 	      if (auto_type_p)
2191 		{
2192 		  error_at (here,
2193 			    "%<__auto_type%> may only be used with"
2194 			    " a single declarator");
2195 		  c_parser_skip_to_end_of_block_or_statement (parser);
2196 		  return;
2197 		}
2198 	      c_parser_consume_token (parser);
2199 	      if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2200 		all_prefix_attrs = chainon (c_parser_attributes (parser),
2201 					    prefix_attrs);
2202 	      else
2203 		all_prefix_attrs = prefix_attrs;
2204 	      continue;
2205 	    }
2206 	  else if (c_parser_next_token_is (parser, CPP_SEMICOLON))
2207 	    {
2208 	      c_parser_consume_token (parser);
2209 	      return;
2210 	    }
2211 	  else if (c_parser_next_token_is_keyword (parser, RID_IN))
2212 	    {
2213 	      /* This can only happen in Objective-C: we found the
2214 		 'in' that terminates the declaration inside an
2215 		 Objective-C foreach statement.  Do not consume the
2216 		 token, so that the caller can use it to determine
2217 		 that this indeed is a foreach context.  */
2218 	      return;
2219 	    }
2220 	  else
2221 	    {
2222 	      c_parser_error (parser, "expected %<,%> or %<;%>");
2223 	      c_parser_skip_to_end_of_block_or_statement (parser);
2224 	      return;
2225 	    }
2226 	}
2227       else if (auto_type_p)
2228 	{
2229 	  error_at (here,
2230 		    "%<__auto_type%> requires an initialized data declaration");
2231 	  c_parser_skip_to_end_of_block_or_statement (parser);
2232 	  return;
2233 	}
2234       else if (!fndef_ok)
2235 	{
2236 	  c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, "
2237 			  "%<asm%> or %<__attribute__%>");
2238 	  c_parser_skip_to_end_of_block_or_statement (parser);
2239 	  return;
2240 	}
2241       /* Function definition (nested or otherwise).  */
2242       if (nested)
2243 	{
2244 	  pedwarn (here, OPT_Wpedantic, "ISO C forbids nested functions");
2245 	  c_push_function_context ();
2246 	}
2247       if (!start_function (specs, declarator, all_prefix_attrs))
2248 	{
2249 	  /* At this point we've consumed:
2250 	       declaration-specifiers declarator
2251 	     and the next token isn't CPP_EQ, CPP_COMMA, CPP_SEMICOLON,
2252 	     RID_ASM, RID_ATTRIBUTE, or RID_IN,
2253 	     but the
2254 	       declaration-specifiers declarator
2255 	     aren't grokkable as a function definition, so we have
2256 	     an error.  */
2257 	  gcc_assert (!c_parser_next_token_is (parser, CPP_SEMICOLON));
2258 	  if (c_parser_next_token_starts_declspecs (parser))
2259 	    {
2260 	      /* If we have
2261 		   declaration-specifiers declarator decl-specs
2262 		 then assume we have a missing semicolon, which would
2263 		 give us:
2264 		   declaration-specifiers declarator  decl-specs
2265 						    ^
2266 						    ;
2267 		   <~~~~~~~~~ declaration ~~~~~~~~~~>
2268 		 Use c_parser_require to get an error with a fix-it hint.  */
2269 	      c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>");
2270 	      parser->error = false;
2271 	    }
2272 	  else
2273 	    {
2274 	      /* This can appear in many cases looking nothing like a
2275 		 function definition, so we don't give a more specific
2276 		 error suggesting there was one.  */
2277 	      c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, %<asm%> "
2278 			      "or %<__attribute__%>");
2279 	    }
2280 	  if (nested)
2281 	    c_pop_function_context ();
2282 	  break;
2283 	}
2284 
2285       if (DECL_DECLARED_INLINE_P (current_function_decl))
2286         tv = TV_PARSE_INLINE;
2287       else
2288         tv = TV_PARSE_FUNC;
2289       auto_timevar at (g_timer, tv);
2290 
2291       /* Parse old-style parameter declarations.  ??? Attributes are
2292 	 not allowed to start declaration specifiers here because of a
2293 	 syntax conflict between a function declaration with attribute
2294 	 suffix and a function definition with an attribute prefix on
2295 	 first old-style parameter declaration.  Following the old
2296 	 parser, they are not accepted on subsequent old-style
2297 	 parameter declarations either.  However, there is no
2298 	 ambiguity after the first declaration, nor indeed on the
2299 	 first as long as we don't allow postfix attributes after a
2300 	 declarator with a nonempty identifier list in a definition;
2301 	 and postfix attributes have never been accepted here in
2302 	 function definitions either.  */
2303       while (c_parser_next_token_is_not (parser, CPP_EOF)
2304 	     && c_parser_next_token_is_not (parser, CPP_OPEN_BRACE))
2305 	c_parser_declaration_or_fndef (parser, false, false, false,
2306 				       true, false, NULL, vNULL);
2307       store_parm_decls ();
2308       if (omp_declare_simd_clauses.exists ())
2309 	c_finish_omp_declare_simd (parser, current_function_decl, NULL_TREE,
2310 				   omp_declare_simd_clauses);
2311       if (oacc_routine_data)
2312 	c_finish_oacc_routine (oacc_routine_data, current_function_decl, true);
2313       DECL_STRUCT_FUNCTION (current_function_decl)->function_start_locus
2314 	= c_parser_peek_token (parser)->location;
2315 
2316       /* If the definition was marked with __GIMPLE then parse the
2317          function body as GIMPLE.  */
2318       if (specs->gimple_p)
2319 	{
2320 	  cfun->pass_startwith = specs->gimple_or_rtl_pass;
2321 	  bool saved = in_late_binary_op;
2322 	  in_late_binary_op = true;
2323 	  c_parser_parse_gimple_body (parser);
2324 	  in_late_binary_op = saved;
2325 	}
2326       /* Similarly, if it was marked with __RTL, use the RTL parser now,
2327 	 consuming the function body.  */
2328       else if (specs->rtl_p)
2329 	{
2330 	  c_parser_parse_rtl_body (parser, specs->gimple_or_rtl_pass);
2331 
2332 	  /* Normally, store_parm_decls sets next_is_function_body,
2333 	     anticipating a function body.  We need a push_scope/pop_scope
2334 	     pair to flush out this state, or subsequent function parsing
2335 	     will go wrong.  */
2336 	  push_scope ();
2337 	  pop_scope ();
2338 
2339 	  finish_function ();
2340 	  return;
2341 	}
2342       else
2343 	fnbody = c_parser_compound_statement (parser);
2344       tree fndecl = current_function_decl;
2345       if (nested)
2346 	{
2347 	  tree decl = current_function_decl;
2348 	  /* Mark nested functions as needing static-chain initially.
2349 	     lower_nested_functions will recompute it but the
2350 	     DECL_STATIC_CHAIN flag is also used before that happens,
2351 	     by initializer_constant_valid_p.  See gcc.dg/nested-fn-2.c.  */
2352 	  DECL_STATIC_CHAIN (decl) = 1;
2353 	  add_stmt (fnbody);
2354 	  finish_function ();
2355 	  c_pop_function_context ();
2356 	  add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl));
2357 	}
2358       else
2359 	{
2360 	  if (fnbody)
2361 	    add_stmt (fnbody);
2362 	  finish_function ();
2363 	}
2364       /* Get rid of the empty stmt list for GIMPLE.  */
2365       if (specs->gimple_p)
2366 	DECL_SAVED_TREE (fndecl) = NULL_TREE;
2367 
2368       break;
2369     }
2370 }
2371 
2372 /* Parse an asm-definition (asm() outside a function body).  This is a
2373    GNU extension.
2374 
2375    asm-definition:
2376      simple-asm-expr ;
2377 */
2378 
2379 static void
c_parser_asm_definition(c_parser * parser)2380 c_parser_asm_definition (c_parser *parser)
2381 {
2382   tree asm_str = c_parser_simple_asm_expr (parser);
2383   if (asm_str)
2384     symtab->finalize_toplevel_asm (asm_str);
2385   c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
2386 }
2387 
2388 /* Parse a static assertion (C11 6.7.10).
2389 
2390    static_assert-declaration:
2391      static_assert-declaration-no-semi ;
2392 */
2393 
2394 static void
c_parser_static_assert_declaration(c_parser * parser)2395 c_parser_static_assert_declaration (c_parser *parser)
2396 {
2397   c_parser_static_assert_declaration_no_semi (parser);
2398   if (parser->error
2399       || !c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
2400     c_parser_skip_to_end_of_block_or_statement (parser);
2401 }
2402 
2403 /* Parse a static assertion (C11 6.7.10), without the trailing
2404    semicolon.
2405 
2406    static_assert-declaration-no-semi:
2407      _Static_assert ( constant-expression , string-literal )
2408 */
2409 
2410 static void
c_parser_static_assert_declaration_no_semi(c_parser * parser)2411 c_parser_static_assert_declaration_no_semi (c_parser *parser)
2412 {
2413   location_t assert_loc, value_loc;
2414   tree value;
2415   tree string;
2416 
2417   gcc_assert (c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT));
2418   assert_loc = c_parser_peek_token (parser)->location;
2419   if (flag_isoc99)
2420     pedwarn_c99 (assert_loc, OPT_Wpedantic,
2421 		 "ISO C99 does not support %<_Static_assert%>");
2422   else
2423     pedwarn_c99 (assert_loc, OPT_Wpedantic,
2424 		 "ISO C90 does not support %<_Static_assert%>");
2425   c_parser_consume_token (parser);
2426   matching_parens parens;
2427   if (!parens.require_open (parser))
2428     return;
2429   location_t value_tok_loc = c_parser_peek_token (parser)->location;
2430   value = c_parser_expr_no_commas (parser, NULL).value;
2431   value_loc = EXPR_LOC_OR_LOC (value, value_tok_loc);
2432   parser->lex_untranslated_string = true;
2433   if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
2434     {
2435       parser->lex_untranslated_string = false;
2436       return;
2437     }
2438   switch (c_parser_peek_token (parser)->type)
2439     {
2440     case CPP_STRING:
2441     case CPP_STRING16:
2442     case CPP_STRING32:
2443     case CPP_WSTRING:
2444     case CPP_UTF8STRING:
2445       string = c_parser_peek_token (parser)->value;
2446       c_parser_consume_token (parser);
2447       parser->lex_untranslated_string = false;
2448       break;
2449     default:
2450       c_parser_error (parser, "expected string literal");
2451       parser->lex_untranslated_string = false;
2452       return;
2453     }
2454   parens.require_close (parser);
2455 
2456   if (!INTEGRAL_TYPE_P (TREE_TYPE (value)))
2457     {
2458       error_at (value_loc, "expression in static assertion is not an integer");
2459       return;
2460     }
2461   if (TREE_CODE (value) != INTEGER_CST)
2462     {
2463       value = c_fully_fold (value, false, NULL);
2464       /* Strip no-op conversions.  */
2465       STRIP_TYPE_NOPS (value);
2466       if (TREE_CODE (value) == INTEGER_CST)
2467 	pedwarn (value_loc, OPT_Wpedantic, "expression in static assertion "
2468 		 "is not an integer constant expression");
2469     }
2470   if (TREE_CODE (value) != INTEGER_CST)
2471     {
2472       error_at (value_loc, "expression in static assertion is not constant");
2473       return;
2474     }
2475   constant_expression_warning (value);
2476   if (integer_zerop (value))
2477     error_at (assert_loc, "static assertion failed: %E", string);
2478 }
2479 
2480 /* Parse some declaration specifiers (possibly none) (C90 6.5, C99
2481    6.7, C11 6.7), adding them to SPECS (which may already include some).
2482    Storage class specifiers are accepted iff SCSPEC_OK; type
2483    specifiers are accepted iff TYPESPEC_OK; alignment specifiers are
2484    accepted iff ALIGNSPEC_OK; attributes are accepted at the start
2485    iff START_ATTR_OK; __auto_type is accepted iff AUTO_TYPE_OK.
2486 
2487    declaration-specifiers:
2488      storage-class-specifier declaration-specifiers[opt]
2489      type-specifier declaration-specifiers[opt]
2490      type-qualifier declaration-specifiers[opt]
2491      function-specifier declaration-specifiers[opt]
2492      alignment-specifier declaration-specifiers[opt]
2493 
2494    Function specifiers (inline) are from C99, and are currently
2495    handled as storage class specifiers, as is __thread.  Alignment
2496    specifiers are from C11.
2497 
2498    C90 6.5.1, C99 6.7.1, C11 6.7.1:
2499    storage-class-specifier:
2500      typedef
2501      extern
2502      static
2503      auto
2504      register
2505      _Thread_local
2506 
2507    (_Thread_local is new in C11.)
2508 
2509    C99 6.7.4, C11 6.7.4:
2510    function-specifier:
2511      inline
2512      _Noreturn
2513 
2514    (_Noreturn is new in C11.)
2515 
2516    C90 6.5.2, C99 6.7.2, C11 6.7.2:
2517    type-specifier:
2518      void
2519      char
2520      short
2521      int
2522      long
2523      float
2524      double
2525      signed
2526      unsigned
2527      _Bool
2528      _Complex
2529      [_Imaginary removed in C99 TC2]
2530      struct-or-union-specifier
2531      enum-specifier
2532      typedef-name
2533      atomic-type-specifier
2534 
2535    (_Bool and _Complex are new in C99.)
2536    (atomic-type-specifier is new in C11.)
2537 
2538    C90 6.5.3, C99 6.7.3, C11 6.7.3:
2539 
2540    type-qualifier:
2541      const
2542      restrict
2543      volatile
2544      address-space-qualifier
2545      _Atomic
2546 
2547    (restrict is new in C99.)
2548    (_Atomic is new in C11.)
2549 
2550    GNU extensions:
2551 
2552    declaration-specifiers:
2553      attributes declaration-specifiers[opt]
2554 
2555    type-qualifier:
2556      address-space
2557 
2558    address-space:
2559      identifier recognized by the target
2560 
2561    storage-class-specifier:
2562      __thread
2563 
2564    type-specifier:
2565      typeof-specifier
2566      __auto_type
2567      __intN
2568      _Decimal32
2569      _Decimal64
2570      _Decimal128
2571      _Fract
2572      _Accum
2573      _Sat
2574 
2575   (_Fract, _Accum, and _Sat are new from ISO/IEC DTR 18037:
2576    http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1169.pdf)
2577 
2578    atomic-type-specifier
2579     _Atomic ( type-name )
2580 
2581    Objective-C:
2582 
2583    type-specifier:
2584      class-name objc-protocol-refs[opt]
2585      typedef-name objc-protocol-refs
2586      objc-protocol-refs
2587 */
2588 
2589 void
c_parser_declspecs(c_parser * parser,struct c_declspecs * specs,bool scspec_ok,bool typespec_ok,bool start_attr_ok,bool alignspec_ok,bool auto_type_ok,enum c_lookahead_kind la)2590 c_parser_declspecs (c_parser *parser, struct c_declspecs *specs,
2591 		    bool scspec_ok, bool typespec_ok, bool start_attr_ok,
2592 		    bool alignspec_ok, bool auto_type_ok,
2593 		    enum c_lookahead_kind la)
2594 {
2595   bool attrs_ok = start_attr_ok;
2596   bool seen_type = specs->typespec_kind != ctsk_none;
2597 
2598   if (!typespec_ok)
2599     gcc_assert (la == cla_prefer_id);
2600 
2601   while (c_parser_next_token_is (parser, CPP_NAME)
2602 	 || c_parser_next_token_is (parser, CPP_KEYWORD)
2603 	 || (c_dialect_objc () && c_parser_next_token_is (parser, CPP_LESS)))
2604     {
2605       struct c_typespec t;
2606       tree attrs;
2607       tree align;
2608       location_t loc = c_parser_peek_token (parser)->location;
2609 
2610       /* If we cannot accept a type, exit if the next token must start
2611 	 one.  Also, if we already have seen a tagged definition,
2612 	 a typename would be an error anyway and likely the user
2613 	 has simply forgotten a semicolon, so we exit.  */
2614       if ((!typespec_ok || specs->typespec_kind == ctsk_tagdef)
2615 	  && c_parser_next_tokens_start_typename (parser, la)
2616 	  && !c_parser_next_token_is_qualifier (parser)
2617 	  && !c_parser_next_token_is_keyword (parser, RID_ALIGNAS))
2618 	break;
2619 
2620       if (c_parser_next_token_is (parser, CPP_NAME))
2621 	{
2622 	  c_token *name_token = c_parser_peek_token (parser);
2623 	  tree value = name_token->value;
2624 	  c_id_kind kind = name_token->id_kind;
2625 
2626 	  if (kind == C_ID_ADDRSPACE)
2627 	    {
2628 	      addr_space_t as
2629 		= name_token->keyword - RID_FIRST_ADDR_SPACE;
2630 	      declspecs_add_addrspace (name_token->location, specs, as);
2631 	      c_parser_consume_token (parser);
2632 	      attrs_ok = true;
2633 	      continue;
2634 	    }
2635 
2636 	  gcc_assert (!c_parser_next_token_is_qualifier (parser));
2637 
2638 	  /* If we cannot accept a type, and the next token must start one,
2639 	     exit.  Do the same if we already have seen a tagged definition,
2640 	     since it would be an error anyway and likely the user has simply
2641 	     forgotten a semicolon.  */
2642 	  if (seen_type || !c_parser_next_tokens_start_typename (parser, la))
2643 	    break;
2644 
2645 	  /* Now at an unknown typename (C_ID_ID), a C_ID_TYPENAME or
2646 	     a C_ID_CLASSNAME.  */
2647 	  c_parser_consume_token (parser);
2648 	  seen_type = true;
2649 	  attrs_ok = true;
2650 	  if (kind == C_ID_ID)
2651 	    {
2652 	      error_at (loc, "unknown type name %qE", value);
2653 	      t.kind = ctsk_typedef;
2654 	      t.spec = error_mark_node;
2655 	    }
2656 	  else if (kind == C_ID_TYPENAME
2657 	           && (!c_dialect_objc ()
2658 	               || c_parser_next_token_is_not (parser, CPP_LESS)))
2659 	    {
2660 	      t.kind = ctsk_typedef;
2661 	      /* For a typedef name, record the meaning, not the name.
2662 		 In case of 'foo foo, bar;'.  */
2663 	      t.spec = lookup_name (value);
2664 	    }
2665 	  else
2666 	    {
2667 	      tree proto = NULL_TREE;
2668 	      gcc_assert (c_dialect_objc ());
2669 	      t.kind = ctsk_objc;
2670 	      if (c_parser_next_token_is (parser, CPP_LESS))
2671 		proto = c_parser_objc_protocol_refs (parser);
2672 	      t.spec = objc_get_protocol_qualified_type (value, proto);
2673 	    }
2674 	  t.expr = NULL_TREE;
2675 	  t.expr_const_operands = true;
2676 	  declspecs_add_type (name_token->location, specs, t);
2677 	  continue;
2678 	}
2679       if (c_parser_next_token_is (parser, CPP_LESS))
2680 	{
2681 	  /* Make "<SomeProtocol>" equivalent to "id <SomeProtocol>" -
2682 	     nisse@lysator.liu.se.  */
2683 	  tree proto;
2684 	  gcc_assert (c_dialect_objc ());
2685 	  if (!typespec_ok || seen_type)
2686 	    break;
2687 	  proto = c_parser_objc_protocol_refs (parser);
2688 	  t.kind = ctsk_objc;
2689 	  t.spec = objc_get_protocol_qualified_type (NULL_TREE, proto);
2690 	  t.expr = NULL_TREE;
2691 	  t.expr_const_operands = true;
2692 	  declspecs_add_type (loc, specs, t);
2693 	  continue;
2694 	}
2695       gcc_assert (c_parser_next_token_is (parser, CPP_KEYWORD));
2696       switch (c_parser_peek_token (parser)->keyword)
2697 	{
2698 	case RID_STATIC:
2699 	case RID_EXTERN:
2700 	case RID_REGISTER:
2701 	case RID_TYPEDEF:
2702 	case RID_INLINE:
2703 	case RID_NORETURN:
2704 	case RID_AUTO:
2705 	case RID_THREAD:
2706 	  if (!scspec_ok)
2707 	    goto out;
2708 	  attrs_ok = true;
2709 	  /* TODO: Distinguish between function specifiers (inline, noreturn)
2710 	     and storage class specifiers, either here or in
2711 	     declspecs_add_scspec.  */
2712 	  declspecs_add_scspec (loc, specs,
2713 				c_parser_peek_token (parser)->value);
2714 	  c_parser_consume_token (parser);
2715 	  break;
2716 	case RID_AUTO_TYPE:
2717 	  if (!auto_type_ok)
2718 	    goto out;
2719 	  /* Fall through.  */
2720 	case RID_UNSIGNED:
2721 	case RID_LONG:
2722 	case RID_SHORT:
2723 	case RID_SIGNED:
2724 	case RID_COMPLEX:
2725 	case RID_INT:
2726 	case RID_CHAR:
2727 	case RID_FLOAT:
2728 	case RID_DOUBLE:
2729 	case RID_VOID:
2730 	case RID_DFLOAT32:
2731 	case RID_DFLOAT64:
2732 	case RID_DFLOAT128:
2733 	CASE_RID_FLOATN_NX:
2734 	case RID_BOOL:
2735 	case RID_FRACT:
2736 	case RID_ACCUM:
2737 	case RID_SAT:
2738 	case RID_INT_N_0:
2739 	case RID_INT_N_1:
2740 	case RID_INT_N_2:
2741 	case RID_INT_N_3:
2742 	  if (!typespec_ok)
2743 	    goto out;
2744 	  attrs_ok = true;
2745 	  seen_type = true;
2746 	  if (c_dialect_objc ())
2747 	    parser->objc_need_raw_identifier = true;
2748 	  t.kind = ctsk_resword;
2749 	  t.spec = c_parser_peek_token (parser)->value;
2750 	  t.expr = NULL_TREE;
2751 	  t.expr_const_operands = true;
2752 	  declspecs_add_type (loc, specs, t);
2753 	  c_parser_consume_token (parser);
2754 	  break;
2755 	case RID_ENUM:
2756 	  if (!typespec_ok)
2757 	    goto out;
2758 	  attrs_ok = true;
2759 	  seen_type = true;
2760 	  t = c_parser_enum_specifier (parser);
2761           invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, t.spec);
2762 	  declspecs_add_type (loc, specs, t);
2763 	  break;
2764 	case RID_STRUCT:
2765 	case RID_UNION:
2766 	  if (!typespec_ok)
2767 	    goto out;
2768 	  attrs_ok = true;
2769 	  seen_type = true;
2770 	  t = c_parser_struct_or_union_specifier (parser);
2771           invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, t.spec);
2772 	  declspecs_add_type (loc, specs, t);
2773 	  break;
2774 	case RID_TYPEOF:
2775 	  /* ??? The old parser rejected typeof after other type
2776 	     specifiers, but is a syntax error the best way of
2777 	     handling this?  */
2778 	  if (!typespec_ok || seen_type)
2779 	    goto out;
2780 	  attrs_ok = true;
2781 	  seen_type = true;
2782 	  t = c_parser_typeof_specifier (parser);
2783 	  declspecs_add_type (loc, specs, t);
2784 	  break;
2785 	case RID_ATOMIC:
2786 	  /* C parser handling of Objective-C constructs needs
2787 	     checking for correct lvalue-to-rvalue conversions, and
2788 	     the code in build_modify_expr handling various
2789 	     Objective-C cases, and that in build_unary_op handling
2790 	     Objective-C cases for increment / decrement, also needs
2791 	     updating; uses of TYPE_MAIN_VARIANT in objc_compare_types
2792 	     and objc_types_are_equivalent may also need updates.  */
2793 	  if (c_dialect_objc ())
2794 	    sorry ("%<_Atomic%> in Objective-C");
2795 	  if (flag_isoc99)
2796 	    pedwarn_c99 (loc, OPT_Wpedantic,
2797 			 "ISO C99 does not support the %<_Atomic%> qualifier");
2798 	  else
2799 	    pedwarn_c99 (loc, OPT_Wpedantic,
2800 			 "ISO C90 does not support the %<_Atomic%> qualifier");
2801 	  attrs_ok = true;
2802 	  tree value;
2803 	  value = c_parser_peek_token (parser)->value;
2804 	  c_parser_consume_token (parser);
2805 	  if (typespec_ok && c_parser_next_token_is (parser, CPP_OPEN_PAREN))
2806 	    {
2807 	      /* _Atomic ( type-name ).  */
2808 	      seen_type = true;
2809 	      c_parser_consume_token (parser);
2810 	      struct c_type_name *type = c_parser_type_name (parser);
2811 	      t.kind = ctsk_typeof;
2812 	      t.spec = error_mark_node;
2813 	      t.expr = NULL_TREE;
2814 	      t.expr_const_operands = true;
2815 	      if (type != NULL)
2816 		t.spec = groktypename (type, &t.expr,
2817 				       &t.expr_const_operands);
2818 	      c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2819 					 "expected %<)%>");
2820 	      if (t.spec != error_mark_node)
2821 		{
2822 		  if (TREE_CODE (t.spec) == ARRAY_TYPE)
2823 		    error_at (loc, "%<_Atomic%>-qualified array type");
2824 		  else if (TREE_CODE (t.spec) == FUNCTION_TYPE)
2825 		    error_at (loc, "%<_Atomic%>-qualified function type");
2826 		  else if (TYPE_QUALS (t.spec) != TYPE_UNQUALIFIED)
2827 		    error_at (loc, "%<_Atomic%> applied to a qualified type");
2828 		  else
2829 		    t.spec = c_build_qualified_type (t.spec, TYPE_QUAL_ATOMIC);
2830 		}
2831 	      declspecs_add_type (loc, specs, t);
2832 	    }
2833 	  else
2834 	    declspecs_add_qual (loc, specs, value);
2835 	  break;
2836 	case RID_CONST:
2837 	case RID_VOLATILE:
2838 	case RID_RESTRICT:
2839 	  attrs_ok = true;
2840 	  declspecs_add_qual (loc, specs, c_parser_peek_token (parser)->value);
2841 	  c_parser_consume_token (parser);
2842 	  break;
2843 	case RID_ATTRIBUTE:
2844 	  if (!attrs_ok)
2845 	    goto out;
2846 	  attrs = c_parser_attributes (parser);
2847 	  declspecs_add_attrs (loc, specs, attrs);
2848 	  break;
2849 	case RID_ALIGNAS:
2850 	  if (!alignspec_ok)
2851 	    goto out;
2852 	  align = c_parser_alignas_specifier (parser);
2853 	  declspecs_add_alignas (loc, specs, align);
2854 	  break;
2855 	case RID_GIMPLE:
2856 	  if (! flag_gimple)
2857 	    error_at (loc, "%<__GIMPLE%> only valid with -fgimple");
2858 	  c_parser_consume_token (parser);
2859 	  specs->gimple_p = true;
2860 	  specs->locations[cdw_gimple] = loc;
2861 	  specs->gimple_or_rtl_pass = c_parser_gimple_or_rtl_pass_list (parser);
2862 	  break;
2863 	case RID_RTL:
2864 	  c_parser_consume_token (parser);
2865 	  specs->rtl_p = true;
2866 	  specs->locations[cdw_rtl] = loc;
2867 	  specs->gimple_or_rtl_pass = c_parser_gimple_or_rtl_pass_list (parser);
2868 	  break;
2869 	default:
2870 	  goto out;
2871 	}
2872     }
2873  out: ;
2874 }
2875 
2876 /* Parse an enum specifier (C90 6.5.2.2, C99 6.7.2.2, C11 6.7.2.2).
2877 
2878    enum-specifier:
2879      enum attributes[opt] identifier[opt] { enumerator-list } attributes[opt]
2880      enum attributes[opt] identifier[opt] { enumerator-list , } attributes[opt]
2881      enum attributes[opt] identifier
2882 
2883    The form with trailing comma is new in C99.  The forms with
2884    attributes are GNU extensions.  In GNU C, we accept any expression
2885    without commas in the syntax (assignment expressions, not just
2886    conditional expressions); assignment expressions will be diagnosed
2887    as non-constant.
2888 
2889    enumerator-list:
2890      enumerator
2891      enumerator-list , enumerator
2892 
2893    enumerator:
2894      enumeration-constant
2895      enumeration-constant = constant-expression
2896 
2897    GNU Extensions:
2898 
2899    enumerator:
2900      enumeration-constant attributes[opt]
2901      enumeration-constant attributes[opt] = constant-expression
2902 
2903 */
2904 
2905 static struct c_typespec
c_parser_enum_specifier(c_parser * parser)2906 c_parser_enum_specifier (c_parser *parser)
2907 {
2908   struct c_typespec ret;
2909   tree attrs;
2910   tree ident = NULL_TREE;
2911   location_t enum_loc;
2912   location_t ident_loc = UNKNOWN_LOCATION;  /* Quiet warning.  */
2913   gcc_assert (c_parser_next_token_is_keyword (parser, RID_ENUM));
2914   c_parser_consume_token (parser);
2915   attrs = c_parser_attributes (parser);
2916   enum_loc = c_parser_peek_token (parser)->location;
2917   /* Set the location in case we create a decl now.  */
2918   c_parser_set_source_position_from_token (c_parser_peek_token (parser));
2919   if (c_parser_next_token_is (parser, CPP_NAME))
2920     {
2921       ident = c_parser_peek_token (parser)->value;
2922       ident_loc = c_parser_peek_token (parser)->location;
2923       enum_loc = ident_loc;
2924       c_parser_consume_token (parser);
2925     }
2926   if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
2927     {
2928       /* Parse an enum definition.  */
2929       struct c_enum_contents the_enum;
2930       tree type;
2931       tree postfix_attrs;
2932       /* We chain the enumerators in reverse order, then put them in
2933 	 forward order at the end.  */
2934       tree values;
2935       timevar_push (TV_PARSE_ENUM);
2936       type = start_enum (enum_loc, &the_enum, ident);
2937       values = NULL_TREE;
2938       c_parser_consume_token (parser);
2939       while (true)
2940 	{
2941 	  tree enum_id;
2942 	  tree enum_value;
2943 	  tree enum_decl;
2944 	  bool seen_comma;
2945 	  c_token *token;
2946 	  location_t comma_loc = UNKNOWN_LOCATION;  /* Quiet warning.  */
2947 	  location_t decl_loc, value_loc;
2948 	  if (c_parser_next_token_is_not (parser, CPP_NAME))
2949 	    {
2950 	      /* Give a nicer error for "enum {}".  */
2951 	      if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE)
2952 		  && !parser->error)
2953 		{
2954 		  error_at (c_parser_peek_token (parser)->location,
2955 			    "empty enum is invalid");
2956 		  parser->error = true;
2957 		}
2958 	      else
2959 		c_parser_error (parser, "expected identifier");
2960 	      c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
2961 	      values = error_mark_node;
2962 	      break;
2963 	    }
2964 	  token = c_parser_peek_token (parser);
2965 	  enum_id = token->value;
2966 	  /* Set the location in case we create a decl now.  */
2967 	  c_parser_set_source_position_from_token (token);
2968 	  decl_loc = value_loc = token->location;
2969 	  c_parser_consume_token (parser);
2970 	  /* Parse any specified attributes.  */
2971 	  tree enum_attrs = c_parser_attributes (parser);
2972 	  if (c_parser_next_token_is (parser, CPP_EQ))
2973 	    {
2974 	      c_parser_consume_token (parser);
2975 	      value_loc = c_parser_peek_token (parser)->location;
2976 	      enum_value = c_parser_expr_no_commas (parser, NULL).value;
2977 	    }
2978 	  else
2979 	    enum_value = NULL_TREE;
2980 	  enum_decl = build_enumerator (decl_loc, value_loc,
2981 					&the_enum, enum_id, enum_value);
2982 	  if (enum_attrs)
2983 	    decl_attributes (&TREE_PURPOSE (enum_decl), enum_attrs, 0);
2984 	  TREE_CHAIN (enum_decl) = values;
2985 	  values = enum_decl;
2986 	  seen_comma = false;
2987 	  if (c_parser_next_token_is (parser, CPP_COMMA))
2988 	    {
2989 	      comma_loc = c_parser_peek_token (parser)->location;
2990 	      seen_comma = true;
2991 	      c_parser_consume_token (parser);
2992 	    }
2993 	  if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2994 	    {
2995 	      if (seen_comma)
2996 		pedwarn_c90 (comma_loc, OPT_Wpedantic,
2997 			     "comma at end of enumerator list");
2998 	      c_parser_consume_token (parser);
2999 	      break;
3000 	    }
3001 	  if (!seen_comma)
3002 	    {
3003 	      c_parser_error (parser, "expected %<,%> or %<}%>");
3004 	      c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
3005 	      values = error_mark_node;
3006 	      break;
3007 	    }
3008 	}
3009       postfix_attrs = c_parser_attributes (parser);
3010       ret.spec = finish_enum (type, nreverse (values),
3011 			      chainon (attrs, postfix_attrs));
3012       ret.kind = ctsk_tagdef;
3013       ret.expr = NULL_TREE;
3014       ret.expr_const_operands = true;
3015       timevar_pop (TV_PARSE_ENUM);
3016       return ret;
3017     }
3018   else if (!ident)
3019     {
3020       c_parser_error (parser, "expected %<{%>");
3021       ret.spec = error_mark_node;
3022       ret.kind = ctsk_tagref;
3023       ret.expr = NULL_TREE;
3024       ret.expr_const_operands = true;
3025       return ret;
3026     }
3027   ret = parser_xref_tag (ident_loc, ENUMERAL_TYPE, ident);
3028   /* In ISO C, enumerated types can be referred to only if already
3029      defined.  */
3030   if (pedantic && !COMPLETE_TYPE_P (ret.spec))
3031     {
3032       gcc_assert (ident);
3033       pedwarn (enum_loc, OPT_Wpedantic,
3034 	       "ISO C forbids forward references to %<enum%> types");
3035     }
3036   return ret;
3037 }
3038 
3039 /* Parse a struct or union specifier (C90 6.5.2.1, C99 6.7.2.1, C11 6.7.2.1).
3040 
3041    struct-or-union-specifier:
3042      struct-or-union attributes[opt] identifier[opt]
3043        { struct-contents } attributes[opt]
3044      struct-or-union attributes[opt] identifier
3045 
3046    struct-contents:
3047      struct-declaration-list
3048 
3049    struct-declaration-list:
3050      struct-declaration ;
3051      struct-declaration-list struct-declaration ;
3052 
3053    GNU extensions:
3054 
3055    struct-contents:
3056      empty
3057      struct-declaration
3058      struct-declaration-list struct-declaration
3059 
3060    struct-declaration-list:
3061      struct-declaration-list ;
3062      ;
3063 
3064    (Note that in the syntax here, unlike that in ISO C, the semicolons
3065    are included here rather than in struct-declaration, in order to
3066    describe the syntax with extra semicolons and missing semicolon at
3067    end.)
3068 
3069    Objective-C:
3070 
3071    struct-declaration-list:
3072      @defs ( class-name )
3073 
3074    (Note this does not include a trailing semicolon, but can be
3075    followed by further declarations, and gets a pedwarn-if-pedantic
3076    when followed by a semicolon.)  */
3077 
3078 static struct c_typespec
c_parser_struct_or_union_specifier(c_parser * parser)3079 c_parser_struct_or_union_specifier (c_parser *parser)
3080 {
3081   struct c_typespec ret;
3082   tree attrs;
3083   tree ident = NULL_TREE;
3084   location_t struct_loc;
3085   location_t ident_loc = UNKNOWN_LOCATION;
3086   enum tree_code code;
3087   switch (c_parser_peek_token (parser)->keyword)
3088     {
3089     case RID_STRUCT:
3090       code = RECORD_TYPE;
3091       break;
3092     case RID_UNION:
3093       code = UNION_TYPE;
3094       break;
3095     default:
3096       gcc_unreachable ();
3097     }
3098   struct_loc = c_parser_peek_token (parser)->location;
3099   c_parser_consume_token (parser);
3100   attrs = c_parser_attributes (parser);
3101 
3102   /* Set the location in case we create a decl now.  */
3103   c_parser_set_source_position_from_token (c_parser_peek_token (parser));
3104 
3105   if (c_parser_next_token_is (parser, CPP_NAME))
3106     {
3107       ident = c_parser_peek_token (parser)->value;
3108       ident_loc = c_parser_peek_token (parser)->location;
3109       struct_loc = ident_loc;
3110       c_parser_consume_token (parser);
3111     }
3112   if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
3113     {
3114       /* Parse a struct or union definition.  Start the scope of the
3115 	 tag before parsing components.  */
3116       struct c_struct_parse_info *struct_info;
3117       tree type = start_struct (struct_loc, code, ident, &struct_info);
3118       tree postfix_attrs;
3119       /* We chain the components in reverse order, then put them in
3120 	 forward order at the end.  Each struct-declaration may
3121 	 declare multiple components (comma-separated), so we must use
3122 	 chainon to join them, although when parsing each
3123 	 struct-declaration we can use TREE_CHAIN directly.
3124 
3125 	 The theory behind all this is that there will be more
3126 	 semicolon separated fields than comma separated fields, and
3127 	 so we'll be minimizing the number of node traversals required
3128 	 by chainon.  */
3129       tree contents;
3130       timevar_push (TV_PARSE_STRUCT);
3131       contents = NULL_TREE;
3132       c_parser_consume_token (parser);
3133       /* Handle the Objective-C @defs construct,
3134 	 e.g. foo(sizeof(struct{ @defs(ClassName) }));.  */
3135       if (c_parser_next_token_is_keyword (parser, RID_AT_DEFS))
3136 	{
3137 	  tree name;
3138 	  gcc_assert (c_dialect_objc ());
3139 	  c_parser_consume_token (parser);
3140 	  matching_parens parens;
3141 	  if (!parens.require_open (parser))
3142 	    goto end_at_defs;
3143 	  if (c_parser_next_token_is (parser, CPP_NAME)
3144 	      && c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME)
3145 	    {
3146 	      name = c_parser_peek_token (parser)->value;
3147 	      c_parser_consume_token (parser);
3148 	    }
3149 	  else
3150 	    {
3151 	      c_parser_error (parser, "expected class name");
3152 	      c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
3153 	      goto end_at_defs;
3154 	    }
3155 	  parens.skip_until_found_close (parser);
3156 	  contents = nreverse (objc_get_class_ivars (name));
3157 	}
3158     end_at_defs:
3159       /* Parse the struct-declarations and semicolons.  Problems with
3160 	 semicolons are diagnosed here; empty structures are diagnosed
3161 	 elsewhere.  */
3162       while (true)
3163 	{
3164 	  tree decls;
3165 	  /* Parse any stray semicolon.  */
3166 	  if (c_parser_next_token_is (parser, CPP_SEMICOLON))
3167 	    {
3168 	      location_t semicolon_loc
3169 		= c_parser_peek_token (parser)->location;
3170 	      gcc_rich_location richloc (semicolon_loc);
3171 	      richloc.add_fixit_remove ();
3172 	      pedwarn (&richloc, OPT_Wpedantic,
3173 		       "extra semicolon in struct or union specified");
3174 	      c_parser_consume_token (parser);
3175 	      continue;
3176 	    }
3177 	  /* Stop if at the end of the struct or union contents.  */
3178 	  if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3179 	    {
3180 	      c_parser_consume_token (parser);
3181 	      break;
3182 	    }
3183 	  /* Accept #pragmas at struct scope.  */
3184 	  if (c_parser_next_token_is (parser, CPP_PRAGMA))
3185 	    {
3186 	      c_parser_pragma (parser, pragma_struct, NULL);
3187 	      continue;
3188 	    }
3189 	  /* Parse some comma-separated declarations, but not the
3190 	     trailing semicolon if any.  */
3191 	  decls = c_parser_struct_declaration (parser);
3192 	  contents = chainon (decls, contents);
3193 	  /* If no semicolon follows, either we have a parse error or
3194 	     are at the end of the struct or union and should
3195 	     pedwarn.  */
3196 	  if (c_parser_next_token_is (parser, CPP_SEMICOLON))
3197 	    c_parser_consume_token (parser);
3198 	  else
3199 	    {
3200 	      if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3201 		pedwarn (c_parser_peek_token (parser)->location, 0,
3202 			 "no semicolon at end of struct or union");
3203 	      else if (parser->error
3204 		       || !c_parser_next_token_starts_declspecs (parser))
3205 		{
3206 		  c_parser_error (parser, "expected %<;%>");
3207 		  c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
3208 		  break;
3209 		}
3210 
3211 	      /* If we come here, we have already emitted an error
3212 		 for an expected `;', identifier or `(', and we also
3213 	         recovered already.  Go on with the next field. */
3214 	    }
3215 	}
3216       postfix_attrs = c_parser_attributes (parser);
3217       ret.spec = finish_struct (struct_loc, type, nreverse (contents),
3218 				chainon (attrs, postfix_attrs), struct_info);
3219       ret.kind = ctsk_tagdef;
3220       ret.expr = NULL_TREE;
3221       ret.expr_const_operands = true;
3222       timevar_pop (TV_PARSE_STRUCT);
3223       return ret;
3224     }
3225   else if (!ident)
3226     {
3227       c_parser_error (parser, "expected %<{%>");
3228       ret.spec = error_mark_node;
3229       ret.kind = ctsk_tagref;
3230       ret.expr = NULL_TREE;
3231       ret.expr_const_operands = true;
3232       return ret;
3233     }
3234   ret = parser_xref_tag (ident_loc, code, ident);
3235   return ret;
3236 }
3237 
3238 /* Parse a struct-declaration (C90 6.5.2.1, C99 6.7.2.1, C11 6.7.2.1),
3239    *without* the trailing semicolon.
3240 
3241    struct-declaration:
3242      specifier-qualifier-list struct-declarator-list
3243      static_assert-declaration-no-semi
3244 
3245    specifier-qualifier-list:
3246      type-specifier specifier-qualifier-list[opt]
3247      type-qualifier specifier-qualifier-list[opt]
3248      alignment-specifier specifier-qualifier-list[opt]
3249      attributes specifier-qualifier-list[opt]
3250 
3251    struct-declarator-list:
3252      struct-declarator
3253      struct-declarator-list , attributes[opt] struct-declarator
3254 
3255    struct-declarator:
3256      declarator attributes[opt]
3257      declarator[opt] : constant-expression attributes[opt]
3258 
3259    GNU extensions:
3260 
3261    struct-declaration:
3262      __extension__ struct-declaration
3263      specifier-qualifier-list
3264 
3265    Unlike the ISO C syntax, semicolons are handled elsewhere.  The use
3266    of attributes where shown is a GNU extension.  In GNU C, we accept
3267    any expression without commas in the syntax (assignment
3268    expressions, not just conditional expressions); assignment
3269    expressions will be diagnosed as non-constant.  */
3270 
3271 static tree
c_parser_struct_declaration(c_parser * parser)3272 c_parser_struct_declaration (c_parser *parser)
3273 {
3274   struct c_declspecs *specs;
3275   tree prefix_attrs;
3276   tree all_prefix_attrs;
3277   tree decls;
3278   location_t decl_loc;
3279   if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
3280     {
3281       int ext;
3282       tree decl;
3283       ext = disable_extension_diagnostics ();
3284       c_parser_consume_token (parser);
3285       decl = c_parser_struct_declaration (parser);
3286       restore_extension_diagnostics (ext);
3287       return decl;
3288     }
3289   if (c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT))
3290     {
3291       c_parser_static_assert_declaration_no_semi (parser);
3292       return NULL_TREE;
3293     }
3294   specs = build_null_declspecs ();
3295   decl_loc = c_parser_peek_token (parser)->location;
3296   /* Strictly by the standard, we shouldn't allow _Alignas here,
3297      but it appears to have been intended to allow it there, so
3298      we're keeping it as it is until WG14 reaches a conclusion
3299      of N1731.
3300      <http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1731.pdf>  */
3301   c_parser_declspecs (parser, specs, false, true, true,
3302 		      true, false, cla_nonabstract_decl);
3303   if (parser->error)
3304     return NULL_TREE;
3305   if (!specs->declspecs_seen_p)
3306     {
3307       c_parser_error (parser, "expected specifier-qualifier-list");
3308       return NULL_TREE;
3309     }
3310   finish_declspecs (specs);
3311   if (c_parser_next_token_is (parser, CPP_SEMICOLON)
3312       || c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3313     {
3314       tree ret;
3315       if (specs->typespec_kind == ctsk_none)
3316 	{
3317 	  pedwarn (decl_loc, OPT_Wpedantic,
3318 		   "ISO C forbids member declarations with no members");
3319 	  shadow_tag_warned (specs, pedantic);
3320 	  ret = NULL_TREE;
3321 	}
3322       else
3323 	{
3324 	  /* Support for unnamed structs or unions as members of
3325 	     structs or unions (which is [a] useful and [b] supports
3326 	     MS P-SDK).  */
3327 	  tree attrs = NULL;
3328 
3329 	  ret = grokfield (c_parser_peek_token (parser)->location,
3330 			   build_id_declarator (NULL_TREE), specs,
3331 			   NULL_TREE, &attrs);
3332 	  if (ret)
3333 	    decl_attributes (&ret, attrs, 0);
3334 	}
3335       return ret;
3336     }
3337 
3338   /* Provide better error recovery.  Note that a type name here is valid,
3339      and will be treated as a field name.  */
3340   if (specs->typespec_kind == ctsk_tagdef
3341       && TREE_CODE (specs->type) != ENUMERAL_TYPE
3342       && c_parser_next_token_starts_declspecs (parser)
3343       && !c_parser_next_token_is (parser, CPP_NAME))
3344     {
3345       c_parser_error (parser, "expected %<;%>, identifier or %<(%>");
3346       parser->error = false;
3347       return NULL_TREE;
3348     }
3349 
3350   pending_xref_error ();
3351   prefix_attrs = specs->attrs;
3352   all_prefix_attrs = prefix_attrs;
3353   specs->attrs = NULL_TREE;
3354   decls = NULL_TREE;
3355   while (true)
3356     {
3357       /* Declaring one or more declarators or un-named bit-fields.  */
3358       struct c_declarator *declarator;
3359       bool dummy = false;
3360       if (c_parser_next_token_is (parser, CPP_COLON))
3361 	declarator = build_id_declarator (NULL_TREE);
3362       else
3363 	declarator = c_parser_declarator (parser,
3364 					  specs->typespec_kind != ctsk_none,
3365 					  C_DTR_NORMAL, &dummy);
3366       if (declarator == NULL)
3367 	{
3368 	  c_parser_skip_to_end_of_block_or_statement (parser);
3369 	  break;
3370 	}
3371       if (c_parser_next_token_is (parser, CPP_COLON)
3372 	  || c_parser_next_token_is (parser, CPP_COMMA)
3373 	  || c_parser_next_token_is (parser, CPP_SEMICOLON)
3374 	  || c_parser_next_token_is (parser, CPP_CLOSE_BRACE)
3375 	  || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
3376 	{
3377 	  tree postfix_attrs = NULL_TREE;
3378 	  tree width = NULL_TREE;
3379 	  tree d;
3380 	  if (c_parser_next_token_is (parser, CPP_COLON))
3381 	    {
3382 	      c_parser_consume_token (parser);
3383 	      width = c_parser_expr_no_commas (parser, NULL).value;
3384 	    }
3385 	  if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
3386 	    postfix_attrs = c_parser_attributes (parser);
3387 	  d = grokfield (c_parser_peek_token (parser)->location,
3388 			 declarator, specs, width, &all_prefix_attrs);
3389 	  decl_attributes (&d, chainon (postfix_attrs,
3390 					all_prefix_attrs), 0);
3391 	  DECL_CHAIN (d) = decls;
3392 	  decls = d;
3393 	  if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
3394 	    all_prefix_attrs = chainon (c_parser_attributes (parser),
3395 					prefix_attrs);
3396 	  else
3397 	    all_prefix_attrs = prefix_attrs;
3398 	  if (c_parser_next_token_is (parser, CPP_COMMA))
3399 	    c_parser_consume_token (parser);
3400 	  else if (c_parser_next_token_is (parser, CPP_SEMICOLON)
3401 		   || c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3402 	    {
3403 	      /* Semicolon consumed in caller.  */
3404 	      break;
3405 	    }
3406 	  else
3407 	    {
3408 	      c_parser_error (parser, "expected %<,%>, %<;%> or %<}%>");
3409 	      break;
3410 	    }
3411 	}
3412       else
3413 	{
3414 	  c_parser_error (parser,
3415 			  "expected %<:%>, %<,%>, %<;%>, %<}%> or "
3416 			  "%<__attribute__%>");
3417 	  break;
3418 	}
3419     }
3420   return decls;
3421 }
3422 
3423 /* Parse a typeof specifier (a GNU extension).
3424 
3425    typeof-specifier:
3426      typeof ( expression )
3427      typeof ( type-name )
3428 */
3429 
3430 static struct c_typespec
c_parser_typeof_specifier(c_parser * parser)3431 c_parser_typeof_specifier (c_parser *parser)
3432 {
3433   struct c_typespec ret;
3434   ret.kind = ctsk_typeof;
3435   ret.spec = error_mark_node;
3436   ret.expr = NULL_TREE;
3437   ret.expr_const_operands = true;
3438   gcc_assert (c_parser_next_token_is_keyword (parser, RID_TYPEOF));
3439   c_parser_consume_token (parser);
3440   c_inhibit_evaluation_warnings++;
3441   in_typeof++;
3442   matching_parens parens;
3443   if (!parens.require_open (parser))
3444     {
3445       c_inhibit_evaluation_warnings--;
3446       in_typeof--;
3447       return ret;
3448     }
3449   if (c_parser_next_tokens_start_typename (parser, cla_prefer_id))
3450     {
3451       struct c_type_name *type = c_parser_type_name (parser);
3452       c_inhibit_evaluation_warnings--;
3453       in_typeof--;
3454       if (type != NULL)
3455 	{
3456 	  ret.spec = groktypename (type, &ret.expr, &ret.expr_const_operands);
3457 	  pop_maybe_used (variably_modified_type_p (ret.spec, NULL_TREE));
3458 	}
3459     }
3460   else
3461     {
3462       bool was_vm;
3463       location_t here = c_parser_peek_token (parser)->location;
3464       struct c_expr expr = c_parser_expression (parser);
3465       c_inhibit_evaluation_warnings--;
3466       in_typeof--;
3467       if (TREE_CODE (expr.value) == COMPONENT_REF
3468 	  && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1)))
3469 	error_at (here, "%<typeof%> applied to a bit-field");
3470       mark_exp_read (expr.value);
3471       ret.spec = TREE_TYPE (expr.value);
3472       was_vm = variably_modified_type_p (ret.spec, NULL_TREE);
3473       /* This is returned with the type so that when the type is
3474 	 evaluated, this can be evaluated.  */
3475       if (was_vm)
3476 	ret.expr = c_fully_fold (expr.value, false, &ret.expr_const_operands);
3477       pop_maybe_used (was_vm);
3478       /* For use in macros such as those in <stdatomic.h>, remove all
3479 	 qualifiers from atomic types.  (const can be an issue for more macros
3480 	 using typeof than just the <stdatomic.h> ones.)  */
3481       if (ret.spec != error_mark_node && TYPE_ATOMIC (ret.spec))
3482 	ret.spec = c_build_qualified_type (ret.spec, TYPE_UNQUALIFIED);
3483     }
3484   parens.skip_until_found_close (parser);
3485   return ret;
3486 }
3487 
3488 /* Parse an alignment-specifier.
3489 
3490    C11 6.7.5:
3491 
3492    alignment-specifier:
3493      _Alignas ( type-name )
3494      _Alignas ( constant-expression )
3495 */
3496 
3497 static tree
c_parser_alignas_specifier(c_parser * parser)3498 c_parser_alignas_specifier (c_parser * parser)
3499 {
3500   tree ret = error_mark_node;
3501   location_t loc = c_parser_peek_token (parser)->location;
3502   gcc_assert (c_parser_next_token_is_keyword (parser, RID_ALIGNAS));
3503   c_parser_consume_token (parser);
3504   if (flag_isoc99)
3505     pedwarn_c99 (loc, OPT_Wpedantic,
3506 		 "ISO C99 does not support %<_Alignas%>");
3507   else
3508     pedwarn_c99 (loc, OPT_Wpedantic,
3509 		 "ISO C90 does not support %<_Alignas%>");
3510   matching_parens parens;
3511   if (!parens.require_open (parser))
3512     return ret;
3513   if (c_parser_next_tokens_start_typename (parser, cla_prefer_id))
3514     {
3515       struct c_type_name *type = c_parser_type_name (parser);
3516       if (type != NULL)
3517 	ret = c_sizeof_or_alignof_type (loc, groktypename (type, NULL, NULL),
3518 					false, true, 1);
3519     }
3520   else
3521     ret = c_parser_expr_no_commas (parser, NULL).value;
3522   parens.skip_until_found_close (parser);
3523   return ret;
3524 }
3525 
3526 /* Parse a declarator, possibly an abstract declarator (C90 6.5.4,
3527    6.5.5, C99 6.7.5, 6.7.6, C11 6.7.6, 6.7.7).  If TYPE_SEEN_P then
3528    a typedef name may be redeclared; otherwise it may not.  KIND
3529    indicates which kind of declarator is wanted.  Returns a valid
3530    declarator except in the case of a syntax error in which case NULL is
3531    returned.  *SEEN_ID is set to true if an identifier being declared is
3532    seen; this is used to diagnose bad forms of abstract array declarators
3533    and to determine whether an identifier list is syntactically permitted.
3534 
3535    declarator:
3536      pointer[opt] direct-declarator
3537 
3538    direct-declarator:
3539      identifier
3540      ( attributes[opt] declarator )
3541      direct-declarator array-declarator
3542      direct-declarator ( parameter-type-list )
3543      direct-declarator ( identifier-list[opt] )
3544 
3545    pointer:
3546      * type-qualifier-list[opt]
3547      * type-qualifier-list[opt] pointer
3548 
3549    type-qualifier-list:
3550      type-qualifier
3551      attributes
3552      type-qualifier-list type-qualifier
3553      type-qualifier-list attributes
3554 
3555    array-declarator:
3556      [ type-qualifier-list[opt] assignment-expression[opt] ]
3557      [ static type-qualifier-list[opt] assignment-expression ]
3558      [ type-qualifier-list static assignment-expression ]
3559      [ type-qualifier-list[opt] * ]
3560 
3561    parameter-type-list:
3562      parameter-list
3563      parameter-list , ...
3564 
3565    parameter-list:
3566      parameter-declaration
3567      parameter-list , parameter-declaration
3568 
3569    parameter-declaration:
3570      declaration-specifiers declarator attributes[opt]
3571      declaration-specifiers abstract-declarator[opt] attributes[opt]
3572 
3573    identifier-list:
3574      identifier
3575      identifier-list , identifier
3576 
3577    abstract-declarator:
3578      pointer
3579      pointer[opt] direct-abstract-declarator
3580 
3581    direct-abstract-declarator:
3582      ( attributes[opt] abstract-declarator )
3583      direct-abstract-declarator[opt] array-declarator
3584      direct-abstract-declarator[opt] ( parameter-type-list[opt] )
3585 
3586    GNU extensions:
3587 
3588    direct-declarator:
3589      direct-declarator ( parameter-forward-declarations
3590 			 parameter-type-list[opt] )
3591 
3592    direct-abstract-declarator:
3593      direct-abstract-declarator[opt] ( parameter-forward-declarations
3594 				       parameter-type-list[opt] )
3595 
3596    parameter-forward-declarations:
3597      parameter-list ;
3598      parameter-forward-declarations parameter-list ;
3599 
3600    The uses of attributes shown above are GNU extensions.
3601 
3602    Some forms of array declarator are not included in C99 in the
3603    syntax for abstract declarators; these are disallowed elsewhere.
3604    This may be a defect (DR#289).
3605 
3606    This function also accepts an omitted abstract declarator as being
3607    an abstract declarator, although not part of the formal syntax.  */
3608 
3609 struct c_declarator *
c_parser_declarator(c_parser * parser,bool type_seen_p,c_dtr_syn kind,bool * seen_id)3610 c_parser_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
3611 		     bool *seen_id)
3612 {
3613   /* Parse any initial pointer part.  */
3614   if (c_parser_next_token_is (parser, CPP_MULT))
3615     {
3616       struct c_declspecs *quals_attrs = build_null_declspecs ();
3617       struct c_declarator *inner;
3618       c_parser_consume_token (parser);
3619       c_parser_declspecs (parser, quals_attrs, false, false, true,
3620 			  false, false, cla_prefer_id);
3621       inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
3622       if (inner == NULL)
3623 	return NULL;
3624       else
3625 	return make_pointer_declarator (quals_attrs, inner);
3626     }
3627   /* Now we have a direct declarator, direct abstract declarator or
3628      nothing (which counts as a direct abstract declarator here).  */
3629   return c_parser_direct_declarator (parser, type_seen_p, kind, seen_id);
3630 }
3631 
3632 /* Parse a direct declarator or direct abstract declarator; arguments
3633    as c_parser_declarator.  */
3634 
3635 static struct c_declarator *
c_parser_direct_declarator(c_parser * parser,bool type_seen_p,c_dtr_syn kind,bool * seen_id)3636 c_parser_direct_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
3637 			    bool *seen_id)
3638 {
3639   /* The direct declarator must start with an identifier (possibly
3640      omitted) or a parenthesized declarator (possibly abstract).  In
3641      an ordinary declarator, initial parentheses must start a
3642      parenthesized declarator.  In an abstract declarator or parameter
3643      declarator, they could start a parenthesized declarator or a
3644      parameter list.  To tell which, the open parenthesis and any
3645      following attributes must be read.  If a declaration specifier
3646      follows, then it is a parameter list; if the specifier is a
3647      typedef name, there might be an ambiguity about redeclaring it,
3648      which is resolved in the direction of treating it as a typedef
3649      name.  If a close parenthesis follows, it is also an empty
3650      parameter list, as the syntax does not permit empty abstract
3651      declarators.  Otherwise, it is a parenthesized declarator (in
3652      which case the analysis may be repeated inside it, recursively).
3653 
3654      ??? There is an ambiguity in a parameter declaration "int
3655      (__attribute__((foo)) x)", where x is not a typedef name: it
3656      could be an abstract declarator for a function, or declare x with
3657      parentheses.  The proper resolution of this ambiguity needs
3658      documenting.  At present we follow an accident of the old
3659      parser's implementation, whereby the first parameter must have
3660      some declaration specifiers other than just attributes.  Thus as
3661      a parameter declaration it is treated as a parenthesized
3662      parameter named x, and as an abstract declarator it is
3663      rejected.
3664 
3665      ??? Also following the old parser, attributes inside an empty
3666      parameter list are ignored, making it a list not yielding a
3667      prototype, rather than giving an error or making it have one
3668      parameter with implicit type int.
3669 
3670      ??? Also following the old parser, typedef names may be
3671      redeclared in declarators, but not Objective-C class names.  */
3672 
3673   if (kind != C_DTR_ABSTRACT
3674       && c_parser_next_token_is (parser, CPP_NAME)
3675       && ((type_seen_p
3676 	   && (c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME
3677 	       || c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME))
3678 	  || c_parser_peek_token (parser)->id_kind == C_ID_ID))
3679     {
3680       struct c_declarator *inner
3681 	= build_id_declarator (c_parser_peek_token (parser)->value);
3682       *seen_id = true;
3683       inner->id_loc = c_parser_peek_token (parser)->location;
3684       c_parser_consume_token (parser);
3685       return c_parser_direct_declarator_inner (parser, *seen_id, inner);
3686     }
3687 
3688   if (kind != C_DTR_NORMAL
3689       && c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
3690     {
3691       struct c_declarator *inner = build_id_declarator (NULL_TREE);
3692       inner->id_loc = c_parser_peek_token (parser)->location;
3693       return c_parser_direct_declarator_inner (parser, *seen_id, inner);
3694     }
3695 
3696   /* Either we are at the end of an abstract declarator, or we have
3697      parentheses.  */
3698 
3699   if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
3700     {
3701       tree attrs;
3702       struct c_declarator *inner;
3703       c_parser_consume_token (parser);
3704       attrs = c_parser_attributes (parser);
3705       if (kind != C_DTR_NORMAL
3706 	  && (c_parser_next_token_starts_declspecs (parser)
3707 	      || c_parser_next_token_is (parser, CPP_CLOSE_PAREN)))
3708 	{
3709 	  struct c_arg_info *args
3710 	    = c_parser_parms_declarator (parser, kind == C_DTR_NORMAL,
3711 					 attrs);
3712 	  if (args == NULL)
3713 	    return NULL;
3714 	  else
3715 	    {
3716 	      inner
3717 		= build_function_declarator (args,
3718 					     build_id_declarator (NULL_TREE));
3719 	      return c_parser_direct_declarator_inner (parser, *seen_id,
3720 						       inner);
3721 	    }
3722 	}
3723       /* A parenthesized declarator.  */
3724       inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
3725       if (inner != NULL && attrs != NULL)
3726 	inner = build_attrs_declarator (attrs, inner);
3727       if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3728 	{
3729 	  c_parser_consume_token (parser);
3730 	  if (inner == NULL)
3731 	    return NULL;
3732 	  else
3733 	    return c_parser_direct_declarator_inner (parser, *seen_id, inner);
3734 	}
3735       else
3736 	{
3737 	  c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3738 				     "expected %<)%>");
3739 	  return NULL;
3740 	}
3741     }
3742   else
3743     {
3744       if (kind == C_DTR_NORMAL)
3745 	{
3746 	  c_parser_error (parser, "expected identifier or %<(%>");
3747 	  return NULL;
3748 	}
3749       else
3750 	return build_id_declarator (NULL_TREE);
3751     }
3752 }
3753 
3754 /* Parse part of a direct declarator or direct abstract declarator,
3755    given that some (in INNER) has already been parsed; ID_PRESENT is
3756    true if an identifier is present, false for an abstract
3757    declarator.  */
3758 
3759 static struct c_declarator *
c_parser_direct_declarator_inner(c_parser * parser,bool id_present,struct c_declarator * inner)3760 c_parser_direct_declarator_inner (c_parser *parser, bool id_present,
3761 				  struct c_declarator *inner)
3762 {
3763   /* Parse a sequence of array declarators and parameter lists.  */
3764   if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
3765     {
3766       location_t brace_loc = c_parser_peek_token (parser)->location;
3767       struct c_declarator *declarator;
3768       struct c_declspecs *quals_attrs = build_null_declspecs ();
3769       bool static_seen;
3770       bool star_seen;
3771       struct c_expr dimen;
3772       dimen.value = NULL_TREE;
3773       dimen.original_code = ERROR_MARK;
3774       dimen.original_type = NULL_TREE;
3775       c_parser_consume_token (parser);
3776       c_parser_declspecs (parser, quals_attrs, false, false, true,
3777 			  false, false, cla_prefer_id);
3778       static_seen = c_parser_next_token_is_keyword (parser, RID_STATIC);
3779       if (static_seen)
3780 	c_parser_consume_token (parser);
3781       if (static_seen && !quals_attrs->declspecs_seen_p)
3782 	c_parser_declspecs (parser, quals_attrs, false, false, true,
3783 			    false, false, cla_prefer_id);
3784       if (!quals_attrs->declspecs_seen_p)
3785 	quals_attrs = NULL;
3786       /* If "static" is present, there must be an array dimension.
3787 	 Otherwise, there may be a dimension, "*", or no
3788 	 dimension.  */
3789       if (static_seen)
3790 	{
3791 	  star_seen = false;
3792 	  dimen = c_parser_expr_no_commas (parser, NULL);
3793 	}
3794       else
3795 	{
3796 	  if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3797 	    {
3798 	      dimen.value = NULL_TREE;
3799 	      star_seen = false;
3800 	    }
3801 	  else if (c_parser_next_token_is (parser, CPP_MULT))
3802 	    {
3803 	      if (c_parser_peek_2nd_token (parser)->type == CPP_CLOSE_SQUARE)
3804 		{
3805 		  dimen.value = NULL_TREE;
3806 		  star_seen = true;
3807 		  c_parser_consume_token (parser);
3808 		}
3809 	      else
3810 		{
3811 		  star_seen = false;
3812 		  dimen = c_parser_expr_no_commas (parser, NULL);
3813 		}
3814 	    }
3815 	  else
3816 	    {
3817 	      star_seen = false;
3818 	      dimen = c_parser_expr_no_commas (parser, NULL);
3819 	    }
3820 	}
3821       if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3822 	c_parser_consume_token (parser);
3823       else
3824 	{
3825 	  c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
3826 				     "expected %<]%>");
3827 	  return NULL;
3828 	}
3829       if (dimen.value)
3830 	dimen = convert_lvalue_to_rvalue (brace_loc, dimen, true, true);
3831       declarator = build_array_declarator (brace_loc, dimen.value, quals_attrs,
3832 					   static_seen, star_seen);
3833       if (declarator == NULL)
3834 	return NULL;
3835       inner = set_array_declarator_inner (declarator, inner);
3836       return c_parser_direct_declarator_inner (parser, id_present, inner);
3837     }
3838   else if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
3839     {
3840       tree attrs;
3841       struct c_arg_info *args;
3842       c_parser_consume_token (parser);
3843       attrs = c_parser_attributes (parser);
3844       args = c_parser_parms_declarator (parser, id_present, attrs);
3845       if (args == NULL)
3846 	return NULL;
3847       else
3848 	{
3849 	  inner = build_function_declarator (args, inner);
3850 	  return c_parser_direct_declarator_inner (parser, id_present, inner);
3851 	}
3852     }
3853   return inner;
3854 }
3855 
3856 /* Parse a parameter list or identifier list, including the closing
3857    parenthesis but not the opening one.  ATTRS are the attributes at
3858    the start of the list.  ID_LIST_OK is true if an identifier list is
3859    acceptable; such a list must not have attributes at the start.  */
3860 
3861 static struct c_arg_info *
c_parser_parms_declarator(c_parser * parser,bool id_list_ok,tree attrs)3862 c_parser_parms_declarator (c_parser *parser, bool id_list_ok, tree attrs)
3863 {
3864   push_scope ();
3865   declare_parm_level ();
3866   /* If the list starts with an identifier, it is an identifier list.
3867      Otherwise, it is either a prototype list or an empty list.  */
3868   if (id_list_ok
3869       && !attrs
3870       && c_parser_next_token_is (parser, CPP_NAME)
3871       && c_parser_peek_token (parser)->id_kind == C_ID_ID
3872 
3873       /* Look ahead to detect typos in type names.  */
3874       && c_parser_peek_2nd_token (parser)->type != CPP_NAME
3875       && c_parser_peek_2nd_token (parser)->type != CPP_MULT
3876       && c_parser_peek_2nd_token (parser)->type != CPP_OPEN_PAREN
3877       && c_parser_peek_2nd_token (parser)->type != CPP_OPEN_SQUARE
3878       && c_parser_peek_2nd_token (parser)->type != CPP_KEYWORD)
3879     {
3880       tree list = NULL_TREE, *nextp = &list;
3881       while (c_parser_next_token_is (parser, CPP_NAME)
3882 	     && c_parser_peek_token (parser)->id_kind == C_ID_ID)
3883 	{
3884 	  *nextp = build_tree_list (NULL_TREE,
3885 				    c_parser_peek_token (parser)->value);
3886 	  nextp = & TREE_CHAIN (*nextp);
3887 	  c_parser_consume_token (parser);
3888 	  if (c_parser_next_token_is_not (parser, CPP_COMMA))
3889 	    break;
3890 	  c_parser_consume_token (parser);
3891 	  if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3892 	    {
3893 	      c_parser_error (parser, "expected identifier");
3894 	      break;
3895 	    }
3896 	}
3897       if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3898 	{
3899 	  struct c_arg_info *ret = build_arg_info ();
3900 	  ret->types = list;
3901 	  c_parser_consume_token (parser);
3902 	  pop_scope ();
3903 	  return ret;
3904 	}
3905       else
3906 	{
3907 	  c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3908 				     "expected %<)%>");
3909 	  pop_scope ();
3910 	  return NULL;
3911 	}
3912     }
3913   else
3914     {
3915       struct c_arg_info *ret = c_parser_parms_list_declarator (parser, attrs,
3916 							       NULL);
3917       pop_scope ();
3918       return ret;
3919     }
3920 }
3921 
3922 /* Parse a parameter list (possibly empty), including the closing
3923    parenthesis but not the opening one.  ATTRS are the attributes at
3924    the start of the list.  EXPR is NULL or an expression that needs to
3925    be evaluated for the side effects of array size expressions in the
3926    parameters.  */
3927 
3928 static struct c_arg_info *
c_parser_parms_list_declarator(c_parser * parser,tree attrs,tree expr)3929 c_parser_parms_list_declarator (c_parser *parser, tree attrs, tree expr)
3930 {
3931   bool bad_parm = false;
3932 
3933   /* ??? Following the old parser, forward parameter declarations may
3934      use abstract declarators, and if no real parameter declarations
3935      follow the forward declarations then this is not diagnosed.  Also
3936      note as above that attributes are ignored as the only contents of
3937      the parentheses, or as the only contents after forward
3938      declarations.  */
3939   if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3940     {
3941       struct c_arg_info *ret = build_arg_info ();
3942       c_parser_consume_token (parser);
3943       return ret;
3944     }
3945   if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
3946     {
3947       struct c_arg_info *ret = build_arg_info ();
3948 
3949       if (flag_allow_parameterless_variadic_functions)
3950         {
3951           /* F (...) is allowed.  */
3952           ret->types = NULL_TREE;
3953         }
3954       else
3955         {
3956           /* Suppress -Wold-style-definition for this case.  */
3957           ret->types = error_mark_node;
3958           error_at (c_parser_peek_token (parser)->location,
3959                     "ISO C requires a named argument before %<...%>");
3960         }
3961       c_parser_consume_token (parser);
3962       if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3963 	{
3964 	  c_parser_consume_token (parser);
3965 	  return ret;
3966 	}
3967       else
3968 	{
3969 	  c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3970 				     "expected %<)%>");
3971 	  return NULL;
3972 	}
3973     }
3974   /* Nonempty list of parameters, either terminated with semicolon
3975      (forward declarations; recurse) or with close parenthesis (normal
3976      function) or with ", ... )" (variadic function).  */
3977   while (true)
3978     {
3979       /* Parse a parameter.  */
3980       struct c_parm *parm = c_parser_parameter_declaration (parser, attrs);
3981       attrs = NULL_TREE;
3982       if (parm == NULL)
3983 	bad_parm = true;
3984       else
3985 	push_parm_decl (parm, &expr);
3986       if (c_parser_next_token_is (parser, CPP_SEMICOLON))
3987 	{
3988 	  tree new_attrs;
3989 	  c_parser_consume_token (parser);
3990 	  mark_forward_parm_decls ();
3991 	  new_attrs = c_parser_attributes (parser);
3992 	  return c_parser_parms_list_declarator (parser, new_attrs, expr);
3993 	}
3994       if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3995 	{
3996 	  c_parser_consume_token (parser);
3997 	  if (bad_parm)
3998 	    return NULL;
3999 	  else
4000 	    return get_parm_info (false, expr);
4001 	}
4002       if (!c_parser_require (parser, CPP_COMMA,
4003 			     "expected %<;%>, %<,%> or %<)%>",
4004 			     UNKNOWN_LOCATION, false))
4005 	{
4006 	  c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4007 	  return NULL;
4008 	}
4009       if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
4010 	{
4011 	  c_parser_consume_token (parser);
4012 	  if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4013 	    {
4014 	      c_parser_consume_token (parser);
4015 	      if (bad_parm)
4016 		return NULL;
4017 	      else
4018 		return get_parm_info (true, expr);
4019 	    }
4020 	  else
4021 	    {
4022 	      c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4023 					 "expected %<)%>");
4024 	      return NULL;
4025 	    }
4026 	}
4027     }
4028 }
4029 
4030 /* Parse a parameter declaration.  ATTRS are the attributes at the
4031    start of the declaration if it is the first parameter.  */
4032 
4033 static struct c_parm *
c_parser_parameter_declaration(c_parser * parser,tree attrs)4034 c_parser_parameter_declaration (c_parser *parser, tree attrs)
4035 {
4036   struct c_declspecs *specs;
4037   struct c_declarator *declarator;
4038   tree prefix_attrs;
4039   tree postfix_attrs = NULL_TREE;
4040   bool dummy = false;
4041 
4042   /* Accept #pragmas between parameter declarations.  */
4043   while (c_parser_next_token_is (parser, CPP_PRAGMA))
4044     c_parser_pragma (parser, pragma_param, NULL);
4045 
4046   if (!c_parser_next_token_starts_declspecs (parser))
4047     {
4048       c_token *token = c_parser_peek_token (parser);
4049       if (parser->error)
4050 	return NULL;
4051       c_parser_set_source_position_from_token (token);
4052       if (c_parser_next_tokens_start_typename (parser, cla_prefer_type))
4053 	{
4054 	  name_hint hint = lookup_name_fuzzy (token->value,
4055 					      FUZZY_LOOKUP_TYPENAME,
4056 					      token->location);
4057 	  if (hint)
4058 	    {
4059 	      gcc_rich_location richloc (token->location);
4060 	      richloc.add_fixit_replace (hint.suggestion ());
4061 	      error_at (&richloc,
4062 			"unknown type name %qE; did you mean %qs?",
4063 			token->value, hint.suggestion ());
4064 	    }
4065 	  else
4066 	    error_at (token->location, "unknown type name %qE", token->value);
4067 	  parser->error = true;
4068 	}
4069       /* ??? In some Objective-C cases '...' isn't applicable so there
4070 	 should be a different message.  */
4071       else
4072 	c_parser_error (parser,
4073 			"expected declaration specifiers or %<...%>");
4074       c_parser_skip_to_end_of_parameter (parser);
4075       return NULL;
4076     }
4077 
4078   location_t start_loc = c_parser_peek_token (parser)->location;
4079 
4080   specs = build_null_declspecs ();
4081   if (attrs)
4082     {
4083       declspecs_add_attrs (input_location, specs, attrs);
4084       attrs = NULL_TREE;
4085     }
4086   c_parser_declspecs (parser, specs, true, true, true, true, false,
4087 		      cla_nonabstract_decl);
4088   finish_declspecs (specs);
4089   pending_xref_error ();
4090   prefix_attrs = specs->attrs;
4091   specs->attrs = NULL_TREE;
4092   declarator = c_parser_declarator (parser,
4093 				    specs->typespec_kind != ctsk_none,
4094 				    C_DTR_PARM, &dummy);
4095   if (declarator == NULL)
4096     {
4097       c_parser_skip_until_found (parser, CPP_COMMA, NULL);
4098       return NULL;
4099     }
4100   if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
4101     postfix_attrs = c_parser_attributes (parser);
4102 
4103   /* Generate a location for the parameter, ranging from the start of the
4104      initial token to the end of the final token.
4105 
4106      If we have a identifier, then use it for the caret location, e.g.
4107 
4108        extern int callee (int one, int (*two)(int, int), float three);
4109                                    ~~~~~~^~~~~~~~~~~~~~
4110 
4111      otherwise, reuse the start location for the caret location e.g.:
4112 
4113        extern int callee (int one, int (*)(int, int), float three);
4114                                    ^~~~~~~~~~~~~~~~~
4115   */
4116   location_t end_loc = parser->last_token_location;
4117 
4118   /* Find any cdk_id declarator; determine if we have an identifier.  */
4119   c_declarator *id_declarator = declarator;
4120   while (id_declarator && id_declarator->kind != cdk_id)
4121     id_declarator = id_declarator->declarator;
4122   location_t caret_loc = (id_declarator->u.id
4123 			  ? id_declarator->id_loc
4124 			  : start_loc);
4125   location_t param_loc = make_location (caret_loc, start_loc, end_loc);
4126 
4127   return build_c_parm (specs, chainon (postfix_attrs, prefix_attrs),
4128 		       declarator, param_loc);
4129 }
4130 
4131 /* Parse a string literal in an asm expression.  It should not be
4132    translated, and wide string literals are an error although
4133    permitted by the syntax.  This is a GNU extension.
4134 
4135    asm-string-literal:
4136      string-literal
4137 
4138    ??? At present, following the old parser, the caller needs to have
4139    set lex_untranslated_string to 1.  It would be better to follow the
4140    C++ parser rather than using this kludge.  */
4141 
4142 static tree
c_parser_asm_string_literal(c_parser * parser)4143 c_parser_asm_string_literal (c_parser *parser)
4144 {
4145   tree str;
4146   int save_flag = warn_overlength_strings;
4147   warn_overlength_strings = 0;
4148   if (c_parser_next_token_is (parser, CPP_STRING))
4149     {
4150       str = c_parser_peek_token (parser)->value;
4151       c_parser_consume_token (parser);
4152     }
4153   else if (c_parser_next_token_is (parser, CPP_WSTRING))
4154     {
4155       error_at (c_parser_peek_token (parser)->location,
4156 		"wide string literal in %<asm%>");
4157       str = build_string (1, "");
4158       c_parser_consume_token (parser);
4159     }
4160   else
4161     {
4162       c_parser_error (parser, "expected string literal");
4163       str = NULL_TREE;
4164     }
4165   warn_overlength_strings = save_flag;
4166   return str;
4167 }
4168 
4169 /* Parse a simple asm expression.  This is used in restricted
4170    contexts, where a full expression with inputs and outputs does not
4171    make sense.  This is a GNU extension.
4172 
4173    simple-asm-expr:
4174      asm ( asm-string-literal )
4175 */
4176 
4177 static tree
c_parser_simple_asm_expr(c_parser * parser)4178 c_parser_simple_asm_expr (c_parser *parser)
4179 {
4180   tree str;
4181   gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM));
4182   /* ??? Follow the C++ parser rather than using the
4183      lex_untranslated_string kludge.  */
4184   parser->lex_untranslated_string = true;
4185   c_parser_consume_token (parser);
4186   matching_parens parens;
4187   if (!parens.require_open (parser))
4188     {
4189       parser->lex_untranslated_string = false;
4190       return NULL_TREE;
4191     }
4192   str = c_parser_asm_string_literal (parser);
4193   parser->lex_untranslated_string = false;
4194   if (!parens.require_close (parser))
4195     {
4196       c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4197       return NULL_TREE;
4198     }
4199   return str;
4200 }
4201 
4202 static tree
c_parser_attribute_any_word(c_parser * parser)4203 c_parser_attribute_any_word (c_parser *parser)
4204 {
4205   tree attr_name = NULL_TREE;
4206 
4207   if (c_parser_next_token_is (parser, CPP_KEYWORD))
4208     {
4209       /* ??? See comment above about what keywords are accepted here.  */
4210       bool ok;
4211       switch (c_parser_peek_token (parser)->keyword)
4212 	{
4213 	case RID_STATIC:
4214 	case RID_UNSIGNED:
4215 	case RID_LONG:
4216 	case RID_CONST:
4217 	case RID_EXTERN:
4218 	case RID_REGISTER:
4219 	case RID_TYPEDEF:
4220 	case RID_SHORT:
4221 	case RID_INLINE:
4222 	case RID_NORETURN:
4223 	case RID_VOLATILE:
4224 	case RID_SIGNED:
4225 	case RID_AUTO:
4226 	case RID_RESTRICT:
4227 	case RID_COMPLEX:
4228 	case RID_THREAD:
4229 	case RID_INT:
4230 	case RID_CHAR:
4231 	case RID_FLOAT:
4232 	case RID_DOUBLE:
4233 	case RID_VOID:
4234 	case RID_DFLOAT32:
4235 	case RID_DFLOAT64:
4236 	case RID_DFLOAT128:
4237 	CASE_RID_FLOATN_NX:
4238 	case RID_BOOL:
4239 	case RID_FRACT:
4240 	case RID_ACCUM:
4241 	case RID_SAT:
4242 	case RID_TRANSACTION_ATOMIC:
4243 	case RID_TRANSACTION_CANCEL:
4244 	case RID_ATOMIC:
4245 	case RID_AUTO_TYPE:
4246 	case RID_INT_N_0:
4247 	case RID_INT_N_1:
4248 	case RID_INT_N_2:
4249 	case RID_INT_N_3:
4250 	  ok = true;
4251 	  break;
4252 	default:
4253 	  ok = false;
4254 	  break;
4255 	}
4256       if (!ok)
4257 	return NULL_TREE;
4258 
4259       /* Accept __attribute__((__const)) as __attribute__((const)) etc.  */
4260       attr_name = ridpointers[(int) c_parser_peek_token (parser)->keyword];
4261     }
4262   else if (c_parser_next_token_is (parser, CPP_NAME))
4263     attr_name = c_parser_peek_token (parser)->value;
4264 
4265   return attr_name;
4266 }
4267 
4268 /* Parse (possibly empty) attributes.  This is a GNU extension.
4269 
4270    attributes:
4271      empty
4272      attributes attribute
4273 
4274    attribute:
4275      __attribute__ ( ( attribute-list ) )
4276 
4277    attribute-list:
4278      attrib
4279      attribute_list , attrib
4280 
4281    attrib:
4282      empty
4283      any-word
4284      any-word ( identifier )
4285      any-word ( identifier , nonempty-expr-list )
4286      any-word ( expr-list )
4287 
4288    where the "identifier" must not be declared as a type, and
4289    "any-word" may be any identifier (including one declared as a
4290    type), a reserved word storage class specifier, type specifier or
4291    type qualifier.  ??? This still leaves out most reserved keywords
4292    (following the old parser), shouldn't we include them, and why not
4293    allow identifiers declared as types to start the arguments?  */
4294 
4295 static tree
c_parser_attributes(c_parser * parser)4296 c_parser_attributes (c_parser *parser)
4297 {
4298   tree attrs = NULL_TREE;
4299   while (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
4300     {
4301       /* ??? Follow the C++ parser rather than using the
4302 	 lex_untranslated_string kludge.  */
4303       parser->lex_untranslated_string = true;
4304       /* Consume the `__attribute__' keyword.  */
4305       c_parser_consume_token (parser);
4306       /* Look for the two `(' tokens.  */
4307       if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4308 	{
4309 	  parser->lex_untranslated_string = false;
4310 	  return attrs;
4311 	}
4312       if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4313 	{
4314 	  parser->lex_untranslated_string = false;
4315 	  c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4316 	  return attrs;
4317 	}
4318       /* Parse the attribute list.  */
4319       while (c_parser_next_token_is (parser, CPP_COMMA)
4320 	     || c_parser_next_token_is (parser, CPP_NAME)
4321 	     || c_parser_next_token_is (parser, CPP_KEYWORD))
4322 	{
4323 	  tree attr, attr_name, attr_args;
4324 	  vec<tree, va_gc> *expr_list;
4325 	  if (c_parser_next_token_is (parser, CPP_COMMA))
4326 	    {
4327 	      c_parser_consume_token (parser);
4328 	      continue;
4329 	    }
4330 
4331 	  attr_name = c_parser_attribute_any_word (parser);
4332 	  if (attr_name == NULL)
4333 	    break;
4334 	  attr_name = canonicalize_attr_name (attr_name);
4335 	  c_parser_consume_token (parser);
4336 	  if (c_parser_next_token_is_not (parser, CPP_OPEN_PAREN))
4337 	    {
4338 	      attr = build_tree_list (attr_name, NULL_TREE);
4339 	      /* Add this attribute to the list.  */
4340 	      attrs = chainon (attrs, attr);
4341 	      /* If the next token isn't a comma, we're done.  */
4342 	      if (!c_parser_next_token_is (parser, CPP_COMMA))
4343 		break;
4344 	      continue;
4345 	    }
4346 	  c_parser_consume_token (parser);
4347 	  /* Parse the attribute contents.  If they start with an
4348 	     identifier which is followed by a comma or close
4349 	     parenthesis, then the arguments start with that
4350 	     identifier; otherwise they are an expression list.
4351 	     In objective-c the identifier may be a classname.  */
4352 	  if (c_parser_next_token_is (parser, CPP_NAME)
4353 	      && (c_parser_peek_token (parser)->id_kind == C_ID_ID
4354 		  || (c_dialect_objc ()
4355 		      && c_parser_peek_token (parser)->id_kind
4356 			 == C_ID_CLASSNAME))
4357 	      && ((c_parser_peek_2nd_token (parser)->type == CPP_COMMA)
4358 		  || (c_parser_peek_2nd_token (parser)->type
4359 		      == CPP_CLOSE_PAREN))
4360 	      && (attribute_takes_identifier_p (attr_name)
4361 		  || (c_dialect_objc ()
4362 		      && c_parser_peek_token (parser)->id_kind
4363 			 == C_ID_CLASSNAME)))
4364 	    {
4365 	      tree arg1 = c_parser_peek_token (parser)->value;
4366 	      c_parser_consume_token (parser);
4367 	      if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4368 		attr_args = build_tree_list (NULL_TREE, arg1);
4369 	      else
4370 		{
4371 		  tree tree_list;
4372 		  c_parser_consume_token (parser);
4373 		  expr_list = c_parser_expr_list (parser, false, true,
4374 						  NULL, NULL, NULL, NULL);
4375 		  tree_list = build_tree_list_vec (expr_list);
4376 		  attr_args = tree_cons (NULL_TREE, arg1, tree_list);
4377 		  release_tree_vector (expr_list);
4378 		}
4379 	    }
4380 	  else
4381 	    {
4382 	      if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4383 		attr_args = NULL_TREE;
4384 	      else
4385 		{
4386 		  expr_list = c_parser_expr_list (parser, false, true,
4387 						  NULL, NULL, NULL, NULL);
4388 		  attr_args = build_tree_list_vec (expr_list);
4389 		  release_tree_vector (expr_list);
4390 		}
4391 	    }
4392 
4393 	  attr = build_tree_list (attr_name, attr_args);
4394 	  if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4395 	    c_parser_consume_token (parser);
4396 	  else
4397 	    {
4398 	      parser->lex_untranslated_string = false;
4399 	      c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4400 					 "expected %<)%>");
4401 	      return attrs;
4402 	    }
4403 	  /* Add this attribute to the list.  */
4404 	  attrs = chainon (attrs, attr);
4405 	  /* If the next token isn't a comma, we're done.  */
4406 	  if (!c_parser_next_token_is (parser, CPP_COMMA))
4407 	    break;
4408 	}
4409       /* Look for the two `)' tokens.  */
4410       if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4411 	c_parser_consume_token (parser);
4412       else
4413 	{
4414 	  parser->lex_untranslated_string = false;
4415 	  c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4416 				     "expected %<)%>");
4417 	  return attrs;
4418 	}
4419       if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4420 	c_parser_consume_token (parser);
4421       else
4422 	{
4423 	  parser->lex_untranslated_string = false;
4424 	  c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4425 				     "expected %<)%>");
4426 	  return attrs;
4427 	}
4428       parser->lex_untranslated_string = false;
4429     }
4430 
4431   return attrs;
4432 }
4433 
4434 /* Parse a type name (C90 6.5.5, C99 6.7.6, C11 6.7.7).  ALIGNAS_OK
4435    says whether alignment specifiers are OK (only in cases that might
4436    be the type name of a compound literal).
4437 
4438    type-name:
4439      specifier-qualifier-list abstract-declarator[opt]
4440 */
4441 
4442 struct c_type_name *
c_parser_type_name(c_parser * parser,bool alignas_ok)4443 c_parser_type_name (c_parser *parser, bool alignas_ok)
4444 {
4445   struct c_declspecs *specs = build_null_declspecs ();
4446   struct c_declarator *declarator;
4447   struct c_type_name *ret;
4448   bool dummy = false;
4449   c_parser_declspecs (parser, specs, false, true, true, alignas_ok, false,
4450 		      cla_prefer_type);
4451   if (!specs->declspecs_seen_p)
4452     {
4453       c_parser_error (parser, "expected specifier-qualifier-list");
4454       return NULL;
4455     }
4456   if (specs->type != error_mark_node)
4457     {
4458       pending_xref_error ();
4459       finish_declspecs (specs);
4460     }
4461   declarator = c_parser_declarator (parser,
4462 				    specs->typespec_kind != ctsk_none,
4463 				    C_DTR_ABSTRACT, &dummy);
4464   if (declarator == NULL)
4465     return NULL;
4466   ret = XOBNEW (&parser_obstack, struct c_type_name);
4467   ret->specs = specs;
4468   ret->declarator = declarator;
4469   return ret;
4470 }
4471 
4472 /* Parse an initializer (C90 6.5.7, C99 6.7.8, C11 6.7.9).
4473 
4474    initializer:
4475      assignment-expression
4476      { initializer-list }
4477      { initializer-list , }
4478 
4479    initializer-list:
4480      designation[opt] initializer
4481      initializer-list , designation[opt] initializer
4482 
4483    designation:
4484      designator-list =
4485 
4486    designator-list:
4487      designator
4488      designator-list designator
4489 
4490    designator:
4491      array-designator
4492      . identifier
4493 
4494    array-designator:
4495      [ constant-expression ]
4496 
4497    GNU extensions:
4498 
4499    initializer:
4500      { }
4501 
4502    designation:
4503      array-designator
4504      identifier :
4505 
4506    array-designator:
4507      [ constant-expression ... constant-expression ]
4508 
4509    Any expression without commas is accepted in the syntax for the
4510    constant-expressions, with non-constant expressions rejected later.
4511 
4512    This function is only used for top-level initializers; for nested
4513    ones, see c_parser_initval.  */
4514 
4515 static struct c_expr
c_parser_initializer(c_parser * parser)4516 c_parser_initializer (c_parser *parser)
4517 {
4518   if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
4519     return c_parser_braced_init (parser, NULL_TREE, false, NULL);
4520   else
4521     {
4522       struct c_expr ret;
4523       location_t loc = c_parser_peek_token (parser)->location;
4524       ret = c_parser_expr_no_commas (parser, NULL);
4525       if (TREE_CODE (ret.value) != STRING_CST
4526 	  && TREE_CODE (ret.value) != COMPOUND_LITERAL_EXPR)
4527 	ret = convert_lvalue_to_rvalue (loc, ret, true, true);
4528       return ret;
4529     }
4530 }
4531 
4532 /* The location of the last comma within the current initializer list,
4533    or UNKNOWN_LOCATION if not within one.  */
4534 
4535 location_t last_init_list_comma;
4536 
4537 /* Parse a braced initializer list.  TYPE is the type specified for a
4538    compound literal, and NULL_TREE for other initializers and for
4539    nested braced lists.  NESTED_P is true for nested braced lists,
4540    false for the list of a compound literal or the list that is the
4541    top-level initializer in a declaration.  */
4542 
4543 static struct c_expr
c_parser_braced_init(c_parser * parser,tree type,bool nested_p,struct obstack * outer_obstack)4544 c_parser_braced_init (c_parser *parser, tree type, bool nested_p,
4545 		      struct obstack *outer_obstack)
4546 {
4547   struct c_expr ret;
4548   struct obstack braced_init_obstack;
4549   location_t brace_loc = c_parser_peek_token (parser)->location;
4550   gcc_obstack_init (&braced_init_obstack);
4551   gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE));
4552   matching_braces braces;
4553   braces.consume_open (parser);
4554   if (nested_p)
4555     {
4556       finish_implicit_inits (brace_loc, outer_obstack);
4557       push_init_level (brace_loc, 0, &braced_init_obstack);
4558     }
4559   else
4560     really_start_incremental_init (type);
4561   if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
4562     {
4563       pedwarn (brace_loc, OPT_Wpedantic, "ISO C forbids empty initializer braces");
4564     }
4565   else
4566     {
4567       /* Parse a non-empty initializer list, possibly with a trailing
4568 	 comma.  */
4569       while (true)
4570 	{
4571 	  c_parser_initelt (parser, &braced_init_obstack);
4572 	  if (parser->error)
4573 	    break;
4574 	  if (c_parser_next_token_is (parser, CPP_COMMA))
4575 	    {
4576 	      last_init_list_comma = c_parser_peek_token (parser)->location;
4577 	      c_parser_consume_token (parser);
4578 	    }
4579 	  else
4580 	    break;
4581 	  if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
4582 	    break;
4583 	}
4584     }
4585   c_token *next_tok = c_parser_peek_token (parser);
4586   if (next_tok->type != CPP_CLOSE_BRACE)
4587     {
4588       ret.set_error ();
4589       ret.original_code = ERROR_MARK;
4590       ret.original_type = NULL;
4591       braces.skip_until_found_close (parser);
4592       pop_init_level (brace_loc, 0, &braced_init_obstack, last_init_list_comma);
4593       obstack_free (&braced_init_obstack, NULL);
4594       return ret;
4595     }
4596   location_t close_loc = next_tok->location;
4597   c_parser_consume_token (parser);
4598   ret = pop_init_level (brace_loc, 0, &braced_init_obstack, close_loc);
4599   obstack_free (&braced_init_obstack, NULL);
4600   set_c_expr_source_range (&ret, brace_loc, close_loc);
4601   return ret;
4602 }
4603 
4604 /* Parse a nested initializer, including designators.  */
4605 
4606 static void
c_parser_initelt(c_parser * parser,struct obstack * braced_init_obstack)4607 c_parser_initelt (c_parser *parser, struct obstack * braced_init_obstack)
4608 {
4609   /* Parse any designator or designator list.  A single array
4610      designator may have the subsequent "=" omitted in GNU C, but a
4611      longer list or a structure member designator may not.  */
4612   if (c_parser_next_token_is (parser, CPP_NAME)
4613       && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
4614     {
4615       /* Old-style structure member designator.  */
4616       set_init_label (c_parser_peek_token (parser)->location,
4617 		      c_parser_peek_token (parser)->value,
4618 		      c_parser_peek_token (parser)->location,
4619 		      braced_init_obstack);
4620       /* Use the colon as the error location.  */
4621       pedwarn (c_parser_peek_2nd_token (parser)->location, OPT_Wpedantic,
4622 	       "obsolete use of designated initializer with %<:%>");
4623       c_parser_consume_token (parser);
4624       c_parser_consume_token (parser);
4625     }
4626   else
4627     {
4628       /* des_seen is 0 if there have been no designators, 1 if there
4629 	 has been a single array designator and 2 otherwise.  */
4630       int des_seen = 0;
4631       /* Location of a designator.  */
4632       location_t des_loc = UNKNOWN_LOCATION;  /* Quiet warning.  */
4633       while (c_parser_next_token_is (parser, CPP_OPEN_SQUARE)
4634 	     || c_parser_next_token_is (parser, CPP_DOT))
4635 	{
4636 	  int des_prev = des_seen;
4637 	  if (!des_seen)
4638 	    des_loc = c_parser_peek_token (parser)->location;
4639 	  if (des_seen < 2)
4640 	    des_seen++;
4641 	  if (c_parser_next_token_is (parser, CPP_DOT))
4642 	    {
4643 	      des_seen = 2;
4644 	      c_parser_consume_token (parser);
4645 	      if (c_parser_next_token_is (parser, CPP_NAME))
4646 		{
4647 		  set_init_label (des_loc, c_parser_peek_token (parser)->value,
4648 				  c_parser_peek_token (parser)->location,
4649 				  braced_init_obstack);
4650 		  c_parser_consume_token (parser);
4651 		}
4652 	      else
4653 		{
4654 		  struct c_expr init;
4655 		  init.set_error ();
4656 		  init.original_code = ERROR_MARK;
4657 		  init.original_type = NULL;
4658 		  c_parser_error (parser, "expected identifier");
4659 		  c_parser_skip_until_found (parser, CPP_COMMA, NULL);
4660 		  process_init_element (input_location, init, false,
4661 					braced_init_obstack);
4662 		  return;
4663 		}
4664 	    }
4665 	  else
4666 	    {
4667 	      tree first, second;
4668 	      location_t ellipsis_loc = UNKNOWN_LOCATION;  /* Quiet warning.  */
4669 	      location_t array_index_loc = UNKNOWN_LOCATION;
4670 	      /* ??? Following the old parser, [ objc-receiver
4671 		 objc-message-args ] is accepted as an initializer,
4672 		 being distinguished from a designator by what follows
4673 		 the first assignment expression inside the square
4674 		 brackets, but after a first array designator a
4675 		 subsequent square bracket is for Objective-C taken to
4676 		 start an expression, using the obsolete form of
4677 		 designated initializer without '=', rather than
4678 		 possibly being a second level of designation: in LALR
4679 		 terms, the '[' is shifted rather than reducing
4680 		 designator to designator-list.  */
4681 	      if (des_prev == 1 && c_dialect_objc ())
4682 		{
4683 		  des_seen = des_prev;
4684 		  break;
4685 		}
4686 	      if (des_prev == 0 && c_dialect_objc ())
4687 		{
4688 		  /* This might be an array designator or an
4689 		     Objective-C message expression.  If the former,
4690 		     continue parsing here; if the latter, parse the
4691 		     remainder of the initializer given the starting
4692 		     primary-expression.  ??? It might make sense to
4693 		     distinguish when des_prev == 1 as well; see
4694 		     previous comment.  */
4695 		  tree rec, args;
4696 		  struct c_expr mexpr;
4697 		  c_parser_consume_token (parser);
4698 		  if (c_parser_peek_token (parser)->type == CPP_NAME
4699 		      && ((c_parser_peek_token (parser)->id_kind
4700 			   == C_ID_TYPENAME)
4701 			  || (c_parser_peek_token (parser)->id_kind
4702 			      == C_ID_CLASSNAME)))
4703 		    {
4704 		      /* Type name receiver.  */
4705 		      tree id = c_parser_peek_token (parser)->value;
4706 		      c_parser_consume_token (parser);
4707 		      rec = objc_get_class_reference (id);
4708 		      goto parse_message_args;
4709 		    }
4710 		  first = c_parser_expr_no_commas (parser, NULL).value;
4711 		  mark_exp_read (first);
4712 		  if (c_parser_next_token_is (parser, CPP_ELLIPSIS)
4713 		      || c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
4714 		    goto array_desig_after_first;
4715 		  /* Expression receiver.  So far only one part
4716 		     without commas has been parsed; there might be
4717 		     more of the expression.  */
4718 		  rec = first;
4719 		  while (c_parser_next_token_is (parser, CPP_COMMA))
4720 		    {
4721 		      struct c_expr next;
4722 		      location_t comma_loc, exp_loc;
4723 		      comma_loc = c_parser_peek_token (parser)->location;
4724 		      c_parser_consume_token (parser);
4725 		      exp_loc = c_parser_peek_token (parser)->location;
4726 		      next = c_parser_expr_no_commas (parser, NULL);
4727 		      next = convert_lvalue_to_rvalue (exp_loc, next,
4728 						       true, true);
4729 		      rec = build_compound_expr (comma_loc, rec, next.value);
4730 		    }
4731 		parse_message_args:
4732 		  /* Now parse the objc-message-args.  */
4733 		  args = c_parser_objc_message_args (parser);
4734 		  c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
4735 					     "expected %<]%>");
4736 		  mexpr.value
4737 		    = objc_build_message_expr (rec, args);
4738 		  mexpr.original_code = ERROR_MARK;
4739 		  mexpr.original_type = NULL;
4740 		  /* Now parse and process the remainder of the
4741 		     initializer, starting with this message
4742 		     expression as a primary-expression.  */
4743 		  c_parser_initval (parser, &mexpr, braced_init_obstack);
4744 		  return;
4745 		}
4746 	      c_parser_consume_token (parser);
4747 	      array_index_loc = c_parser_peek_token (parser)->location;
4748 	      first = c_parser_expr_no_commas (parser, NULL).value;
4749 	      mark_exp_read (first);
4750 	    array_desig_after_first:
4751 	      if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
4752 		{
4753 		  ellipsis_loc = c_parser_peek_token (parser)->location;
4754 		  c_parser_consume_token (parser);
4755 		  second = c_parser_expr_no_commas (parser, NULL).value;
4756 		  mark_exp_read (second);
4757 		}
4758 	      else
4759 		second = NULL_TREE;
4760 	      if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
4761 		{
4762 		  c_parser_consume_token (parser);
4763 		  set_init_index (array_index_loc, first, second,
4764 				  braced_init_obstack);
4765 		  if (second)
4766 		    pedwarn (ellipsis_loc, OPT_Wpedantic,
4767 			     "ISO C forbids specifying range of elements to initialize");
4768 		}
4769 	      else
4770 		c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
4771 					   "expected %<]%>");
4772 	    }
4773 	}
4774       if (des_seen >= 1)
4775 	{
4776 	  if (c_parser_next_token_is (parser, CPP_EQ))
4777 	    {
4778 	      pedwarn_c90 (des_loc, OPT_Wpedantic,
4779 			   "ISO C90 forbids specifying subobject "
4780 			   "to initialize");
4781 	      c_parser_consume_token (parser);
4782 	    }
4783 	  else
4784 	    {
4785 	      if (des_seen == 1)
4786 		pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
4787 			 "obsolete use of designated initializer without %<=%>");
4788 	      else
4789 		{
4790 		  struct c_expr init;
4791 		  init.set_error ();
4792 		  init.original_code = ERROR_MARK;
4793 		  init.original_type = NULL;
4794 		  c_parser_error (parser, "expected %<=%>");
4795 		  c_parser_skip_until_found (parser, CPP_COMMA, NULL);
4796 		  process_init_element (input_location, init, false,
4797 					braced_init_obstack);
4798 		  return;
4799 		}
4800 	    }
4801 	}
4802     }
4803   c_parser_initval (parser, NULL, braced_init_obstack);
4804 }
4805 
4806 /* Parse a nested initializer; as c_parser_initializer but parses
4807    initializers within braced lists, after any designators have been
4808    applied.  If AFTER is not NULL then it is an Objective-C message
4809    expression which is the primary-expression starting the
4810    initializer.  */
4811 
4812 static void
c_parser_initval(c_parser * parser,struct c_expr * after,struct obstack * braced_init_obstack)4813 c_parser_initval (c_parser *parser, struct c_expr *after,
4814 		  struct obstack * braced_init_obstack)
4815 {
4816   struct c_expr init;
4817   gcc_assert (!after || c_dialect_objc ());
4818   location_t loc = c_parser_peek_token (parser)->location;
4819 
4820   if (c_parser_next_token_is (parser, CPP_OPEN_BRACE) && !after)
4821     init = c_parser_braced_init (parser, NULL_TREE, true,
4822 				 braced_init_obstack);
4823   else
4824     {
4825       init = c_parser_expr_no_commas (parser, after);
4826       if (init.value != NULL_TREE
4827 	  && TREE_CODE (init.value) != STRING_CST
4828 	  && TREE_CODE (init.value) != COMPOUND_LITERAL_EXPR)
4829 	init = convert_lvalue_to_rvalue (loc, init, true, true);
4830     }
4831   process_init_element (loc, init, false, braced_init_obstack);
4832 }
4833 
4834 /* Parse a compound statement (possibly a function body) (C90 6.6.2,
4835    C99 6.8.2, C11 6.8.2).
4836 
4837    compound-statement:
4838      { block-item-list[opt] }
4839      { label-declarations block-item-list }
4840 
4841    block-item-list:
4842      block-item
4843      block-item-list block-item
4844 
4845    block-item:
4846      nested-declaration
4847      statement
4848 
4849    nested-declaration:
4850      declaration
4851 
4852    GNU extensions:
4853 
4854    compound-statement:
4855      { label-declarations block-item-list }
4856 
4857    nested-declaration:
4858      __extension__ nested-declaration
4859      nested-function-definition
4860 
4861    label-declarations:
4862      label-declaration
4863      label-declarations label-declaration
4864 
4865    label-declaration:
4866      __label__ identifier-list ;
4867 
4868    Allowing the mixing of declarations and code is new in C99.  The
4869    GNU syntax also permits (not shown above) labels at the end of
4870    compound statements, which yield an error.  We don't allow labels
4871    on declarations; this might seem like a natural extension, but
4872    there would be a conflict between attributes on the label and
4873    prefix attributes on the declaration.  ??? The syntax follows the
4874    old parser in requiring something after label declarations.
4875    Although they are erroneous if the labels declared aren't defined,
4876    is it useful for the syntax to be this way?
4877 
4878    OpenACC:
4879 
4880    block-item:
4881      openacc-directive
4882 
4883    openacc-directive:
4884      update-directive
4885 
4886    OpenMP:
4887 
4888    block-item:
4889      openmp-directive
4890 
4891    openmp-directive:
4892      barrier-directive
4893      flush-directive
4894      taskwait-directive
4895      taskyield-directive
4896      cancel-directive
4897      cancellation-point-directive  */
4898 
4899 static tree
c_parser_compound_statement(c_parser * parser)4900 c_parser_compound_statement (c_parser *parser)
4901 {
4902   tree stmt;
4903   location_t brace_loc;
4904   brace_loc = c_parser_peek_token (parser)->location;
4905   if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
4906     {
4907       /* Ensure a scope is entered and left anyway to avoid confusion
4908 	 if we have just prepared to enter a function body.  */
4909       stmt = c_begin_compound_stmt (true);
4910       c_end_compound_stmt (brace_loc, stmt, true);
4911       return error_mark_node;
4912     }
4913   stmt = c_begin_compound_stmt (true);
4914   c_parser_compound_statement_nostart (parser);
4915 
4916   return c_end_compound_stmt (brace_loc, stmt, true);
4917 }
4918 
4919 /* Parse a compound statement except for the opening brace.  This is
4920    used for parsing both compound statements and statement expressions
4921    (which follow different paths to handling the opening).  */
4922 
4923 static void
c_parser_compound_statement_nostart(c_parser * parser)4924 c_parser_compound_statement_nostart (c_parser *parser)
4925 {
4926   bool last_stmt = false;
4927   bool last_label = false;
4928   bool save_valid_for_pragma = valid_location_for_stdc_pragma_p ();
4929   location_t label_loc = UNKNOWN_LOCATION;  /* Quiet warning.  */
4930   if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
4931     {
4932       add_debug_begin_stmt (c_parser_peek_token (parser)->location);
4933       c_parser_consume_token (parser);
4934       return;
4935     }
4936   mark_valid_location_for_stdc_pragma (true);
4937   if (c_parser_next_token_is_keyword (parser, RID_LABEL))
4938     {
4939       /* Read zero or more forward-declarations for labels that nested
4940 	 functions can jump to.  */
4941       mark_valid_location_for_stdc_pragma (false);
4942       while (c_parser_next_token_is_keyword (parser, RID_LABEL))
4943 	{
4944 	  label_loc = c_parser_peek_token (parser)->location;
4945 	  c_parser_consume_token (parser);
4946 	  /* Any identifiers, including those declared as type names,
4947 	     are OK here.  */
4948 	  while (true)
4949 	    {
4950 	      tree label;
4951 	      if (c_parser_next_token_is_not (parser, CPP_NAME))
4952 		{
4953 		  c_parser_error (parser, "expected identifier");
4954 		  break;
4955 		}
4956 	      label
4957 		= declare_label (c_parser_peek_token (parser)->value);
4958 	      C_DECLARED_LABEL_FLAG (label) = 1;
4959 	      add_stmt (build_stmt (label_loc, DECL_EXPR, label));
4960 	      c_parser_consume_token (parser);
4961 	      if (c_parser_next_token_is (parser, CPP_COMMA))
4962 		c_parser_consume_token (parser);
4963 	      else
4964 		break;
4965 	    }
4966 	  c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
4967 	}
4968       pedwarn (label_loc, OPT_Wpedantic, "ISO C forbids label declarations");
4969     }
4970   /* We must now have at least one statement, label or declaration.  */
4971   if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
4972     {
4973       mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
4974       c_parser_error (parser, "expected declaration or statement");
4975       c_parser_consume_token (parser);
4976       return;
4977     }
4978   while (c_parser_next_token_is_not (parser, CPP_CLOSE_BRACE))
4979     {
4980       location_t loc = c_parser_peek_token (parser)->location;
4981       loc = expansion_point_location_if_in_system_header (loc);
4982       if (c_parser_next_token_is_keyword (parser, RID_CASE)
4983 	  || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
4984 	  || (c_parser_next_token_is (parser, CPP_NAME)
4985 	      && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
4986 	{
4987 	  if (c_parser_next_token_is_keyword (parser, RID_CASE))
4988 	    label_loc = c_parser_peek_2nd_token (parser)->location;
4989 	  else
4990 	    label_loc = c_parser_peek_token (parser)->location;
4991 	  last_label = true;
4992 	  last_stmt = false;
4993 	  mark_valid_location_for_stdc_pragma (false);
4994 	  c_parser_label (parser);
4995 	}
4996       else if (!last_label
4997 	       && c_parser_next_tokens_start_declaration (parser))
4998 	{
4999 	  last_label = false;
5000 	  mark_valid_location_for_stdc_pragma (false);
5001 	  bool fallthru_attr_p = false;
5002 	  c_parser_declaration_or_fndef (parser, true, true, true, true,
5003 					 true, NULL, vNULL, NULL,
5004 					 &fallthru_attr_p);
5005 	  if (last_stmt && !fallthru_attr_p)
5006 	    pedwarn_c90 (loc, OPT_Wdeclaration_after_statement,
5007 			 "ISO C90 forbids mixed declarations and code");
5008 	  last_stmt = fallthru_attr_p;
5009 	}
5010       else if (!last_label
5011 	       && c_parser_next_token_is_keyword (parser, RID_EXTENSION))
5012 	{
5013 	  /* __extension__ can start a declaration, but is also an
5014 	     unary operator that can start an expression.  Consume all
5015 	     but the last of a possible series of __extension__ to
5016 	     determine which.  */
5017 	  while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
5018 		 && (c_parser_peek_2nd_token (parser)->keyword
5019 		     == RID_EXTENSION))
5020 	    c_parser_consume_token (parser);
5021 	  if (c_token_starts_declaration (c_parser_peek_2nd_token (parser)))
5022 	    {
5023 	      int ext;
5024 	      ext = disable_extension_diagnostics ();
5025 	      c_parser_consume_token (parser);
5026 	      last_label = false;
5027 	      mark_valid_location_for_stdc_pragma (false);
5028 	      c_parser_declaration_or_fndef (parser, true, true, true, true,
5029 					     true, NULL, vNULL);
5030 	      /* Following the old parser, __extension__ does not
5031 		 disable this diagnostic.  */
5032 	      restore_extension_diagnostics (ext);
5033 	      if (last_stmt)
5034 		pedwarn_c90 (loc, OPT_Wdeclaration_after_statement,
5035 			     "ISO C90 forbids mixed declarations and code");
5036 	      last_stmt = false;
5037 	    }
5038 	  else
5039 	    goto statement;
5040 	}
5041       else if (c_parser_next_token_is (parser, CPP_PRAGMA))
5042 	{
5043 	  /* External pragmas, and some omp pragmas, are not associated
5044 	     with regular c code, and so are not to be considered statements
5045 	     syntactically.  This ensures that the user doesn't put them
5046 	     places that would turn into syntax errors if the directive
5047 	     were ignored.  */
5048 	  if (c_parser_pragma (parser,
5049 			       last_label ? pragma_stmt : pragma_compound,
5050 			       NULL))
5051 	    last_label = false, last_stmt = true;
5052 	}
5053       else if (c_parser_next_token_is (parser, CPP_EOF))
5054 	{
5055 	  mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
5056 	  c_parser_error (parser, "expected declaration or statement");
5057 	  return;
5058 	}
5059       else if (c_parser_next_token_is_keyword (parser, RID_ELSE))
5060         {
5061           if (parser->in_if_block)
5062             {
5063 	      mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
5064 	      error_at (loc, "expected %<}%> before %<else%>");
5065               return;
5066             }
5067           else
5068             {
5069               error_at (loc, "%<else%> without a previous %<if%>");
5070               c_parser_consume_token (parser);
5071               continue;
5072             }
5073         }
5074       else
5075 	{
5076 	statement:
5077 	  last_label = false;
5078 	  last_stmt = true;
5079 	  mark_valid_location_for_stdc_pragma (false);
5080 	  c_parser_statement_after_labels (parser, NULL);
5081 	}
5082 
5083       parser->error = false;
5084     }
5085   if (last_label)
5086     error_at (label_loc, "label at end of compound statement");
5087   c_parser_consume_token (parser);
5088   /* Restore the value we started with.  */
5089   mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
5090 }
5091 
5092 /* Parse all consecutive labels. */
5093 
5094 static void
c_parser_all_labels(c_parser * parser)5095 c_parser_all_labels (c_parser *parser)
5096 {
5097   while (c_parser_next_token_is_keyword (parser, RID_CASE)
5098 	 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
5099 	 || (c_parser_next_token_is (parser, CPP_NAME)
5100 	     && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
5101     c_parser_label (parser);
5102 }
5103 
5104 /* Parse a label (C90 6.6.1, C99 6.8.1, C11 6.8.1).
5105 
5106    label:
5107      identifier : attributes[opt]
5108      case constant-expression :
5109      default :
5110 
5111    GNU extensions:
5112 
5113    label:
5114      case constant-expression ... constant-expression :
5115 
5116    The use of attributes on labels is a GNU extension.  The syntax in
5117    GNU C accepts any expressions without commas, non-constant
5118    expressions being rejected later.  */
5119 
5120 static void
c_parser_label(c_parser * parser)5121 c_parser_label (c_parser *parser)
5122 {
5123   location_t loc1 = c_parser_peek_token (parser)->location;
5124   tree label = NULL_TREE;
5125 
5126   /* Remember whether this case or a user-defined label is allowed to fall
5127      through to.  */
5128   bool fallthrough_p = c_parser_peek_token (parser)->flags & PREV_FALLTHROUGH;
5129 
5130   if (c_parser_next_token_is_keyword (parser, RID_CASE))
5131     {
5132       tree exp1, exp2;
5133       c_parser_consume_token (parser);
5134       exp1 = c_parser_expr_no_commas (parser, NULL).value;
5135       if (c_parser_next_token_is (parser, CPP_COLON))
5136 	{
5137 	  c_parser_consume_token (parser);
5138 	  label = do_case (loc1, exp1, NULL_TREE);
5139 	}
5140       else if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
5141 	{
5142 	  c_parser_consume_token (parser);
5143 	  exp2 = c_parser_expr_no_commas (parser, NULL).value;
5144 	  if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
5145 	    label = do_case (loc1, exp1, exp2);
5146 	}
5147       else
5148 	c_parser_error (parser, "expected %<:%> or %<...%>");
5149     }
5150   else if (c_parser_next_token_is_keyword (parser, RID_DEFAULT))
5151     {
5152       c_parser_consume_token (parser);
5153       if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
5154 	label = do_case (loc1, NULL_TREE, NULL_TREE);
5155     }
5156   else
5157     {
5158       tree name = c_parser_peek_token (parser)->value;
5159       tree tlab;
5160       tree attrs;
5161       location_t loc2 = c_parser_peek_token (parser)->location;
5162       gcc_assert (c_parser_next_token_is (parser, CPP_NAME));
5163       c_parser_consume_token (parser);
5164       gcc_assert (c_parser_next_token_is (parser, CPP_COLON));
5165       c_parser_consume_token (parser);
5166       attrs = c_parser_attributes (parser);
5167       tlab = define_label (loc2, name);
5168       if (tlab)
5169 	{
5170 	  decl_attributes (&tlab, attrs, 0);
5171 	  label = add_stmt (build_stmt (loc1, LABEL_EXPR, tlab));
5172 	}
5173     }
5174   if (label)
5175     {
5176       if (TREE_CODE (label) == LABEL_EXPR)
5177 	FALLTHROUGH_LABEL_P (LABEL_EXPR_LABEL (label)) = fallthrough_p;
5178       else
5179 	FALLTHROUGH_LABEL_P (CASE_LABEL (label)) = fallthrough_p;
5180 
5181       /* Allow '__attribute__((fallthrough));'.  */
5182       if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
5183 	{
5184 	  location_t loc = c_parser_peek_token (parser)->location;
5185 	  tree attrs = c_parser_attributes (parser);
5186 	  if (attribute_fallthrough_p (attrs))
5187 	    {
5188 	      if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5189 		{
5190 		  tree fn = build_call_expr_internal_loc (loc,
5191 							  IFN_FALLTHROUGH,
5192 							  void_type_node, 0);
5193 		  add_stmt (fn);
5194 		}
5195 	      else
5196 		warning_at (loc, OPT_Wattributes, "%<fallthrough%> attribute "
5197 			    "not followed by %<;%>");
5198 	    }
5199 	  else if (attrs != NULL_TREE)
5200 	    warning_at (loc, OPT_Wattributes, "only attribute %<fallthrough%>"
5201 			" can be applied to a null statement");
5202 	}
5203       if (c_parser_next_tokens_start_declaration (parser))
5204 	{
5205 	  error_at (c_parser_peek_token (parser)->location,
5206 		    "a label can only be part of a statement and "
5207 		    "a declaration is not a statement");
5208 	  c_parser_declaration_or_fndef (parser, /*fndef_ok*/ false,
5209 					 /*static_assert_ok*/ true,
5210 					 /*empty_ok*/ true, /*nested*/ true,
5211 					 /*start_attr_ok*/ true, NULL,
5212 					 vNULL);
5213 	}
5214     }
5215 }
5216 
5217 /* Parse a statement (C90 6.6, C99 6.8, C11 6.8).
5218 
5219    statement:
5220      labeled-statement
5221      compound-statement
5222      expression-statement
5223      selection-statement
5224      iteration-statement
5225      jump-statement
5226 
5227    labeled-statement:
5228      label statement
5229 
5230    expression-statement:
5231      expression[opt] ;
5232 
5233    selection-statement:
5234      if-statement
5235      switch-statement
5236 
5237    iteration-statement:
5238      while-statement
5239      do-statement
5240      for-statement
5241 
5242    jump-statement:
5243      goto identifier ;
5244      continue ;
5245      break ;
5246      return expression[opt] ;
5247 
5248    GNU extensions:
5249 
5250    statement:
5251      asm-statement
5252 
5253    jump-statement:
5254      goto * expression ;
5255 
5256    expression-statement:
5257      attributes ;
5258 
5259    Objective-C:
5260 
5261    statement:
5262      objc-throw-statement
5263      objc-try-catch-statement
5264      objc-synchronized-statement
5265 
5266    objc-throw-statement:
5267      @throw expression ;
5268      @throw ;
5269 
5270    OpenACC:
5271 
5272    statement:
5273      openacc-construct
5274 
5275    openacc-construct:
5276      parallel-construct
5277      kernels-construct
5278      data-construct
5279      loop-construct
5280 
5281    parallel-construct:
5282      parallel-directive structured-block
5283 
5284    kernels-construct:
5285      kernels-directive structured-block
5286 
5287    data-construct:
5288      data-directive structured-block
5289 
5290    loop-construct:
5291      loop-directive structured-block
5292 
5293    OpenMP:
5294 
5295    statement:
5296      openmp-construct
5297 
5298    openmp-construct:
5299      parallel-construct
5300      for-construct
5301      simd-construct
5302      for-simd-construct
5303      sections-construct
5304      single-construct
5305      parallel-for-construct
5306      parallel-for-simd-construct
5307      parallel-sections-construct
5308      master-construct
5309      critical-construct
5310      atomic-construct
5311      ordered-construct
5312 
5313    parallel-construct:
5314      parallel-directive structured-block
5315 
5316    for-construct:
5317      for-directive iteration-statement
5318 
5319    simd-construct:
5320      simd-directive iteration-statements
5321 
5322    for-simd-construct:
5323      for-simd-directive iteration-statements
5324 
5325    sections-construct:
5326      sections-directive section-scope
5327 
5328    single-construct:
5329      single-directive structured-block
5330 
5331    parallel-for-construct:
5332      parallel-for-directive iteration-statement
5333 
5334    parallel-for-simd-construct:
5335      parallel-for-simd-directive iteration-statement
5336 
5337    parallel-sections-construct:
5338      parallel-sections-directive section-scope
5339 
5340    master-construct:
5341      master-directive structured-block
5342 
5343    critical-construct:
5344      critical-directive structured-block
5345 
5346    atomic-construct:
5347      atomic-directive expression-statement
5348 
5349    ordered-construct:
5350      ordered-directive structured-block
5351 
5352    Transactional Memory:
5353 
5354    statement:
5355      transaction-statement
5356      transaction-cancel-statement
5357 
5358    IF_P is used to track whether there's a (possibly labeled) if statement
5359    which is not enclosed in braces and has an else clause.  This is used to
5360    implement -Wparentheses.  */
5361 
5362 static void
c_parser_statement(c_parser * parser,bool * if_p,location_t * loc_after_labels)5363 c_parser_statement (c_parser *parser, bool *if_p, location_t *loc_after_labels)
5364 {
5365   c_parser_all_labels (parser);
5366   if (loc_after_labels)
5367     *loc_after_labels = c_parser_peek_token (parser)->location;
5368   c_parser_statement_after_labels (parser, if_p, NULL);
5369 }
5370 
5371 /* Parse a statement, other than a labeled statement.  CHAIN is a vector
5372    of if-else-if conditions.
5373 
5374    IF_P is used to track whether there's a (possibly labeled) if statement
5375    which is not enclosed in braces and has an else clause.  This is used to
5376    implement -Wparentheses.  */
5377 
5378 static void
c_parser_statement_after_labels(c_parser * parser,bool * if_p,vec<tree> * chain)5379 c_parser_statement_after_labels (c_parser *parser, bool *if_p,
5380 				 vec<tree> *chain)
5381 {
5382   location_t loc = c_parser_peek_token (parser)->location;
5383   tree stmt = NULL_TREE;
5384   bool in_if_block = parser->in_if_block;
5385   parser->in_if_block = false;
5386   if (if_p != NULL)
5387     *if_p = false;
5388 
5389   if (c_parser_peek_token (parser)->type != CPP_OPEN_BRACE)
5390     add_debug_begin_stmt (loc);
5391 
5392   switch (c_parser_peek_token (parser)->type)
5393     {
5394     case CPP_OPEN_BRACE:
5395       add_stmt (c_parser_compound_statement (parser));
5396       break;
5397     case CPP_KEYWORD:
5398       switch (c_parser_peek_token (parser)->keyword)
5399 	{
5400 	case RID_IF:
5401 	  c_parser_if_statement (parser, if_p, chain);
5402 	  break;
5403 	case RID_SWITCH:
5404 	  c_parser_switch_statement (parser, if_p);
5405 	  break;
5406 	case RID_WHILE:
5407 	  c_parser_while_statement (parser, false, 0, if_p);
5408 	  break;
5409 	case RID_DO:
5410 	  c_parser_do_statement (parser, 0, false);
5411 	  break;
5412 	case RID_FOR:
5413 	  c_parser_for_statement (parser, false, 0, if_p);
5414 	  break;
5415 	case RID_GOTO:
5416 	  c_parser_consume_token (parser);
5417 	  if (c_parser_next_token_is (parser, CPP_NAME))
5418 	    {
5419 	      stmt = c_finish_goto_label (loc,
5420 					  c_parser_peek_token (parser)->value);
5421 	      c_parser_consume_token (parser);
5422 	    }
5423 	  else if (c_parser_next_token_is (parser, CPP_MULT))
5424 	    {
5425 	      struct c_expr val;
5426 
5427 	      c_parser_consume_token (parser);
5428 	      val = c_parser_expression (parser);
5429 	      val = convert_lvalue_to_rvalue (loc, val, false, true);
5430 	      stmt = c_finish_goto_ptr (loc, val.value);
5431 	    }
5432 	  else
5433 	    c_parser_error (parser, "expected identifier or %<*%>");
5434 	  goto expect_semicolon;
5435 	case RID_CONTINUE:
5436 	  c_parser_consume_token (parser);
5437 	  stmt = c_finish_bc_stmt (loc, &c_cont_label, false);
5438 	  goto expect_semicolon;
5439 	case RID_BREAK:
5440 	  c_parser_consume_token (parser);
5441 	  stmt = c_finish_bc_stmt (loc, &c_break_label, true);
5442 	  goto expect_semicolon;
5443 	case RID_RETURN:
5444 	  c_parser_consume_token (parser);
5445 	  if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5446 	    {
5447 	      stmt = c_finish_return (loc, NULL_TREE, NULL_TREE);
5448 	      c_parser_consume_token (parser);
5449 	    }
5450 	  else
5451 	    {
5452 	      location_t xloc = c_parser_peek_token (parser)->location;
5453 	      struct c_expr expr = c_parser_expression_conv (parser);
5454 	      mark_exp_read (expr.value);
5455 	      stmt = c_finish_return (EXPR_LOC_OR_LOC (expr.value, xloc),
5456 				      expr.value, expr.original_type);
5457 	      goto expect_semicolon;
5458 	    }
5459 	  break;
5460 	case RID_ASM:
5461 	  stmt = c_parser_asm_statement (parser);
5462 	  break;
5463 	case RID_TRANSACTION_ATOMIC:
5464 	case RID_TRANSACTION_RELAXED:
5465 	  stmt = c_parser_transaction (parser,
5466 	      c_parser_peek_token (parser)->keyword);
5467 	  break;
5468 	case RID_TRANSACTION_CANCEL:
5469 	  stmt = c_parser_transaction_cancel (parser);
5470 	  goto expect_semicolon;
5471 	case RID_AT_THROW:
5472 	  gcc_assert (c_dialect_objc ());
5473 	  c_parser_consume_token (parser);
5474 	  if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5475 	    {
5476 	      stmt = objc_build_throw_stmt (loc, NULL_TREE);
5477 	      c_parser_consume_token (parser);
5478 	    }
5479 	  else
5480 	    {
5481 	      struct c_expr expr = c_parser_expression (parser);
5482 	      expr = convert_lvalue_to_rvalue (loc, expr, false, false);
5483 	      expr.value = c_fully_fold (expr.value, false, NULL);
5484 	      stmt = objc_build_throw_stmt (loc, expr.value);
5485 	      goto expect_semicolon;
5486 	    }
5487 	  break;
5488 	case RID_AT_TRY:
5489 	  gcc_assert (c_dialect_objc ());
5490 	  c_parser_objc_try_catch_finally_statement (parser);
5491 	  break;
5492 	case RID_AT_SYNCHRONIZED:
5493 	  gcc_assert (c_dialect_objc ());
5494 	  c_parser_objc_synchronized_statement (parser);
5495 	  break;
5496 	case RID_ATTRIBUTE:
5497 	  {
5498 	    /* Allow '__attribute__((fallthrough));'.  */
5499 	    tree attrs = c_parser_attributes (parser);
5500 	    if (attribute_fallthrough_p (attrs))
5501 	      {
5502 		if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5503 		  {
5504 		    tree fn = build_call_expr_internal_loc (loc,
5505 							    IFN_FALLTHROUGH,
5506 							    void_type_node, 0);
5507 		    add_stmt (fn);
5508 		    /* Eat the ';'.  */
5509 		    c_parser_consume_token (parser);
5510 		  }
5511 		else
5512 		  warning_at (loc, OPT_Wattributes,
5513 			      "%<fallthrough%> attribute not followed "
5514 			      "by %<;%>");
5515 	      }
5516 	    else if (attrs != NULL_TREE)
5517 	      warning_at (loc, OPT_Wattributes, "only attribute %<fallthrough%>"
5518 			  " can be applied to a null statement");
5519 	    break;
5520 	  }
5521 	default:
5522 	  goto expr_stmt;
5523 	}
5524       break;
5525     case CPP_SEMICOLON:
5526       c_parser_consume_token (parser);
5527       break;
5528     case CPP_CLOSE_PAREN:
5529     case CPP_CLOSE_SQUARE:
5530       /* Avoid infinite loop in error recovery:
5531 	 c_parser_skip_until_found stops at a closing nesting
5532 	 delimiter without consuming it, but here we need to consume
5533 	 it to proceed further.  */
5534       c_parser_error (parser, "expected statement");
5535       c_parser_consume_token (parser);
5536       break;
5537     case CPP_PRAGMA:
5538       c_parser_pragma (parser, pragma_stmt, if_p);
5539       break;
5540     default:
5541     expr_stmt:
5542       stmt = c_finish_expr_stmt (loc, c_parser_expression_conv (parser).value);
5543     expect_semicolon:
5544       c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
5545       break;
5546     }
5547   /* Two cases cannot and do not have line numbers associated: If stmt
5548      is degenerate, such as "2;", then stmt is an INTEGER_CST, which
5549      cannot hold line numbers.  But that's OK because the statement
5550      will either be changed to a MODIFY_EXPR during gimplification of
5551      the statement expr, or discarded.  If stmt was compound, but
5552      without new variables, we will have skipped the creation of a
5553      BIND and will have a bare STATEMENT_LIST.  But that's OK because
5554      (recursively) all of the component statements should already have
5555      line numbers assigned.  ??? Can we discard no-op statements
5556      earlier?  */
5557   if (EXPR_LOCATION (stmt) == UNKNOWN_LOCATION)
5558     protected_set_expr_location (stmt, loc);
5559 
5560   parser->in_if_block = in_if_block;
5561 }
5562 
5563 /* Parse the condition from an if, do, while or for statements.  */
5564 
5565 static tree
c_parser_condition(c_parser * parser)5566 c_parser_condition (c_parser *parser)
5567 {
5568   location_t loc = c_parser_peek_token (parser)->location;
5569   tree cond;
5570   cond = c_parser_expression_conv (parser).value;
5571   cond = c_objc_common_truthvalue_conversion (loc, cond);
5572   cond = c_fully_fold (cond, false, NULL);
5573   if (warn_sequence_point)
5574     verify_sequence_points (cond);
5575   return cond;
5576 }
5577 
5578 /* Parse a parenthesized condition from an if, do or while statement.
5579 
5580    condition:
5581      ( expression )
5582 */
5583 static tree
c_parser_paren_condition(c_parser * parser)5584 c_parser_paren_condition (c_parser *parser)
5585 {
5586   tree cond;
5587   matching_parens parens;
5588   if (!parens.require_open (parser))
5589     return error_mark_node;
5590   cond = c_parser_condition (parser);
5591   parens.skip_until_found_close (parser);
5592   return cond;
5593 }
5594 
5595 /* Parse a statement which is a block in C99.
5596 
5597    IF_P is used to track whether there's a (possibly labeled) if statement
5598    which is not enclosed in braces and has an else clause.  This is used to
5599    implement -Wparentheses.  */
5600 
5601 static tree
c_parser_c99_block_statement(c_parser * parser,bool * if_p,location_t * loc_after_labels)5602 c_parser_c99_block_statement (c_parser *parser, bool *if_p,
5603 			      location_t *loc_after_labels)
5604 {
5605   tree block = c_begin_compound_stmt (flag_isoc99);
5606   location_t loc = c_parser_peek_token (parser)->location;
5607   c_parser_statement (parser, if_p, loc_after_labels);
5608   return c_end_compound_stmt (loc, block, flag_isoc99);
5609 }
5610 
5611 /* Parse the body of an if statement.  This is just parsing a
5612    statement but (a) it is a block in C99, (b) we track whether the
5613    body is an if statement for the sake of -Wparentheses warnings, (c)
5614    we handle an empty body specially for the sake of -Wempty-body
5615    warnings, and (d) we call parser_compound_statement directly
5616    because c_parser_statement_after_labels resets
5617    parser->in_if_block.
5618 
5619    IF_P is used to track whether there's a (possibly labeled) if statement
5620    which is not enclosed in braces and has an else clause.  This is used to
5621    implement -Wparentheses.  */
5622 
5623 static tree
c_parser_if_body(c_parser * parser,bool * if_p,const token_indent_info & if_tinfo)5624 c_parser_if_body (c_parser *parser, bool *if_p,
5625 		  const token_indent_info &if_tinfo)
5626 {
5627   tree block = c_begin_compound_stmt (flag_isoc99);
5628   location_t body_loc = c_parser_peek_token (parser)->location;
5629   location_t body_loc_after_labels = UNKNOWN_LOCATION;
5630   token_indent_info body_tinfo
5631     = get_token_indent_info (c_parser_peek_token (parser));
5632 
5633   c_parser_all_labels (parser);
5634   if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5635     {
5636       location_t loc = c_parser_peek_token (parser)->location;
5637       add_stmt (build_empty_stmt (loc));
5638       c_parser_consume_token (parser);
5639       if (!c_parser_next_token_is_keyword (parser, RID_ELSE))
5640 	warning_at (loc, OPT_Wempty_body,
5641 		    "suggest braces around empty body in an %<if%> statement");
5642     }
5643   else if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
5644     add_stmt (c_parser_compound_statement (parser));
5645   else
5646     {
5647       body_loc_after_labels = c_parser_peek_token (parser)->location;
5648       c_parser_statement_after_labels (parser, if_p);
5649     }
5650 
5651   token_indent_info next_tinfo
5652     = get_token_indent_info (c_parser_peek_token (parser));
5653   warn_for_misleading_indentation (if_tinfo, body_tinfo, next_tinfo);
5654   if (body_loc_after_labels != UNKNOWN_LOCATION
5655       && next_tinfo.type != CPP_SEMICOLON)
5656     warn_for_multistatement_macros (body_loc_after_labels, next_tinfo.location,
5657 				    if_tinfo.location, RID_IF);
5658 
5659   return c_end_compound_stmt (body_loc, block, flag_isoc99);
5660 }
5661 
5662 /* Parse the else body of an if statement.  This is just parsing a
5663    statement but (a) it is a block in C99, (b) we handle an empty body
5664    specially for the sake of -Wempty-body warnings.  CHAIN is a vector
5665    of if-else-if conditions.  */
5666 
5667 static tree
c_parser_else_body(c_parser * parser,const token_indent_info & else_tinfo,vec<tree> * chain)5668 c_parser_else_body (c_parser *parser, const token_indent_info &else_tinfo,
5669 		    vec<tree> *chain)
5670 {
5671   location_t body_loc = c_parser_peek_token (parser)->location;
5672   tree block = c_begin_compound_stmt (flag_isoc99);
5673   token_indent_info body_tinfo
5674     = get_token_indent_info (c_parser_peek_token (parser));
5675   location_t body_loc_after_labels = UNKNOWN_LOCATION;
5676 
5677   c_parser_all_labels (parser);
5678   if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5679     {
5680       location_t loc = c_parser_peek_token (parser)->location;
5681       warning_at (loc,
5682 		  OPT_Wempty_body,
5683 	         "suggest braces around empty body in an %<else%> statement");
5684       add_stmt (build_empty_stmt (loc));
5685       c_parser_consume_token (parser);
5686     }
5687   else
5688     {
5689       if (!c_parser_next_token_is (parser, CPP_OPEN_BRACE))
5690 	body_loc_after_labels = c_parser_peek_token (parser)->location;
5691       c_parser_statement_after_labels (parser, NULL, chain);
5692     }
5693 
5694   token_indent_info next_tinfo
5695     = get_token_indent_info (c_parser_peek_token (parser));
5696   warn_for_misleading_indentation (else_tinfo, body_tinfo, next_tinfo);
5697   if (body_loc_after_labels != UNKNOWN_LOCATION
5698       && next_tinfo.type != CPP_SEMICOLON)
5699     warn_for_multistatement_macros (body_loc_after_labels, next_tinfo.location,
5700 				    else_tinfo.location, RID_ELSE);
5701 
5702   return c_end_compound_stmt (body_loc, block, flag_isoc99);
5703 }
5704 
5705 /* We might need to reclassify any previously-lexed identifier, e.g.
5706    when we've left a for loop with an if-statement without else in the
5707    body - we might have used a wrong scope for the token.  See PR67784.  */
5708 
5709 static void
c_parser_maybe_reclassify_token(c_parser * parser)5710 c_parser_maybe_reclassify_token (c_parser *parser)
5711 {
5712   if (c_parser_next_token_is (parser, CPP_NAME))
5713     {
5714       c_token *token = c_parser_peek_token (parser);
5715 
5716       if (token->id_kind != C_ID_CLASSNAME)
5717 	{
5718 	  tree decl = lookup_name (token->value);
5719 
5720 	  token->id_kind = C_ID_ID;
5721 	  if (decl)
5722 	    {
5723 	      if (TREE_CODE (decl) == TYPE_DECL)
5724 		token->id_kind = C_ID_TYPENAME;
5725 	    }
5726 	  else if (c_dialect_objc ())
5727 	    {
5728 	      tree objc_interface_decl = objc_is_class_name (token->value);
5729 	      /* Objective-C class names are in the same namespace as
5730 		 variables and typedefs, and hence are shadowed by local
5731 		 declarations.  */
5732 	      if (objc_interface_decl)
5733 		{
5734 		  token->value = objc_interface_decl;
5735 		  token->id_kind = C_ID_CLASSNAME;
5736 		}
5737 	    }
5738 	}
5739     }
5740 }
5741 
5742 /* Parse an if statement (C90 6.6.4, C99 6.8.4, C11 6.8.4).
5743 
5744    if-statement:
5745      if ( expression ) statement
5746      if ( expression ) statement else statement
5747 
5748    CHAIN is a vector of if-else-if conditions.
5749    IF_P is used to track whether there's a (possibly labeled) if statement
5750    which is not enclosed in braces and has an else clause.  This is used to
5751    implement -Wparentheses.  */
5752 
5753 static void
c_parser_if_statement(c_parser * parser,bool * if_p,vec<tree> * chain)5754 c_parser_if_statement (c_parser *parser, bool *if_p, vec<tree> *chain)
5755 {
5756   tree block;
5757   location_t loc;
5758   tree cond;
5759   bool nested_if = false;
5760   tree first_body, second_body;
5761   bool in_if_block;
5762 
5763   gcc_assert (c_parser_next_token_is_keyword (parser, RID_IF));
5764   token_indent_info if_tinfo
5765     = get_token_indent_info (c_parser_peek_token (parser));
5766   c_parser_consume_token (parser);
5767   block = c_begin_compound_stmt (flag_isoc99);
5768   loc = c_parser_peek_token (parser)->location;
5769   cond = c_parser_paren_condition (parser);
5770   in_if_block = parser->in_if_block;
5771   parser->in_if_block = true;
5772   first_body = c_parser_if_body (parser, &nested_if, if_tinfo);
5773   parser->in_if_block = in_if_block;
5774 
5775   if (warn_duplicated_cond)
5776     warn_duplicated_cond_add_or_warn (EXPR_LOCATION (cond), cond, &chain);
5777 
5778   if (c_parser_next_token_is_keyword (parser, RID_ELSE))
5779     {
5780       token_indent_info else_tinfo
5781 	= get_token_indent_info (c_parser_peek_token (parser));
5782       c_parser_consume_token (parser);
5783       if (warn_duplicated_cond)
5784 	{
5785 	  if (c_parser_next_token_is_keyword (parser, RID_IF)
5786 	      && chain == NULL)
5787 	    {
5788 	      /* We've got "if (COND) else if (COND2)".  Start the
5789 		 condition chain and add COND as the first element.  */
5790 	      chain = new vec<tree> ();
5791 	      if (!CONSTANT_CLASS_P (cond) && !TREE_SIDE_EFFECTS (cond))
5792 		chain->safe_push (cond);
5793 	    }
5794 	  else if (!c_parser_next_token_is_keyword (parser, RID_IF))
5795 	    {
5796 	      /* This is if-else without subsequent if.  Zap the condition
5797 		 chain; we would have already warned at this point.  */
5798 	      delete chain;
5799 	      chain = NULL;
5800 	    }
5801 	}
5802       second_body = c_parser_else_body (parser, else_tinfo, chain);
5803       /* Set IF_P to true to indicate that this if statement has an
5804 	 else clause.  This may trigger the Wparentheses warning
5805 	 below when we get back up to the parent if statement.  */
5806       if (if_p != NULL)
5807 	*if_p = true;
5808     }
5809   else
5810     {
5811       second_body = NULL_TREE;
5812 
5813       /* Diagnose an ambiguous else if if-then-else is nested inside
5814 	 if-then.  */
5815       if (nested_if)
5816 	warning_at (loc, OPT_Wdangling_else,
5817 		    "suggest explicit braces to avoid ambiguous %<else%>");
5818 
5819       if (warn_duplicated_cond)
5820 	{
5821 	  /* This if statement does not have an else clause.  We don't
5822 	     need the condition chain anymore.  */
5823 	  delete chain;
5824 	  chain = NULL;
5825 	}
5826     }
5827   c_finish_if_stmt (loc, cond, first_body, second_body);
5828   add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
5829 
5830   c_parser_maybe_reclassify_token (parser);
5831 }
5832 
5833 /* Parse a switch statement (C90 6.6.4, C99 6.8.4, C11 6.8.4).
5834 
5835    switch-statement:
5836      switch (expression) statement
5837 */
5838 
5839 static void
c_parser_switch_statement(c_parser * parser,bool * if_p)5840 c_parser_switch_statement (c_parser *parser, bool *if_p)
5841 {
5842   struct c_expr ce;
5843   tree block, expr, body, save_break;
5844   location_t switch_loc = c_parser_peek_token (parser)->location;
5845   location_t switch_cond_loc;
5846   gcc_assert (c_parser_next_token_is_keyword (parser, RID_SWITCH));
5847   c_parser_consume_token (parser);
5848   block = c_begin_compound_stmt (flag_isoc99);
5849   bool explicit_cast_p = false;
5850   matching_parens parens;
5851   if (parens.require_open (parser))
5852     {
5853       switch_cond_loc = c_parser_peek_token (parser)->location;
5854       if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
5855 	  && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
5856 	explicit_cast_p = true;
5857       ce = c_parser_expression (parser);
5858       ce = convert_lvalue_to_rvalue (switch_cond_loc, ce, true, false);
5859       expr = ce.value;
5860       /* ??? expr has no valid location?  */
5861       parens.skip_until_found_close (parser);
5862     }
5863   else
5864     {
5865       switch_cond_loc = UNKNOWN_LOCATION;
5866       expr = error_mark_node;
5867       ce.original_type = error_mark_node;
5868     }
5869   c_start_case (switch_loc, switch_cond_loc, expr, explicit_cast_p);
5870   save_break = c_break_label;
5871   c_break_label = NULL_TREE;
5872   location_t loc_after_labels;
5873   bool open_brace_p = c_parser_peek_token (parser)->type == CPP_OPEN_BRACE;
5874   body = c_parser_c99_block_statement (parser, if_p, &loc_after_labels);
5875   location_t next_loc = c_parser_peek_token (parser)->location;
5876   if (!open_brace_p && c_parser_peek_token (parser)->type != CPP_SEMICOLON)
5877     warn_for_multistatement_macros (loc_after_labels, next_loc, switch_loc,
5878 				    RID_SWITCH);
5879   if (c_break_label)
5880     {
5881       location_t here = c_parser_peek_token (parser)->location;
5882       tree t = build1 (LABEL_EXPR, void_type_node, c_break_label);
5883       SET_EXPR_LOCATION (t, here);
5884       SWITCH_BREAK_LABEL_P (c_break_label) = 1;
5885       append_to_statement_list_force (t, &body);
5886     }
5887   c_finish_case (body, ce.original_type);
5888   c_break_label = save_break;
5889   add_stmt (c_end_compound_stmt (switch_loc, block, flag_isoc99));
5890   c_parser_maybe_reclassify_token (parser);
5891 }
5892 
5893 /* Parse a while statement (C90 6.6.5, C99 6.8.5, C11 6.8.5).
5894 
5895    while-statement:
5896       while (expression) statement
5897 
5898    IF_P is used to track whether there's a (possibly labeled) if statement
5899    which is not enclosed in braces and has an else clause.  This is used to
5900    implement -Wparentheses.  */
5901 
5902 static void
c_parser_while_statement(c_parser * parser,bool ivdep,unsigned short unroll,bool * if_p)5903 c_parser_while_statement (c_parser *parser, bool ivdep, unsigned short unroll,
5904 			  bool *if_p)
5905 {
5906   tree block, cond, body, save_break, save_cont;
5907   location_t loc;
5908   gcc_assert (c_parser_next_token_is_keyword (parser, RID_WHILE));
5909   token_indent_info while_tinfo
5910     = get_token_indent_info (c_parser_peek_token (parser));
5911   c_parser_consume_token (parser);
5912   block = c_begin_compound_stmt (flag_isoc99);
5913   loc = c_parser_peek_token (parser)->location;
5914   cond = c_parser_paren_condition (parser);
5915   if (ivdep && cond != error_mark_node)
5916     cond = build3 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
5917 		   build_int_cst (integer_type_node,
5918 				  annot_expr_ivdep_kind),
5919 		   integer_zero_node);
5920   if (unroll && cond != error_mark_node)
5921     cond = build3 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
5922 		   build_int_cst (integer_type_node,
5923 				  annot_expr_unroll_kind),
5924 		   build_int_cst (integer_type_node, unroll));
5925   save_break = c_break_label;
5926   c_break_label = NULL_TREE;
5927   save_cont = c_cont_label;
5928   c_cont_label = NULL_TREE;
5929 
5930   token_indent_info body_tinfo
5931     = get_token_indent_info (c_parser_peek_token (parser));
5932 
5933   location_t loc_after_labels;
5934   bool open_brace = c_parser_next_token_is (parser, CPP_OPEN_BRACE);
5935   body = c_parser_c99_block_statement (parser, if_p, &loc_after_labels);
5936   c_finish_loop (loc, loc, cond, UNKNOWN_LOCATION, NULL, body,
5937 		 c_break_label, c_cont_label, true);
5938   add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
5939   c_parser_maybe_reclassify_token (parser);
5940 
5941   token_indent_info next_tinfo
5942     = get_token_indent_info (c_parser_peek_token (parser));
5943   warn_for_misleading_indentation (while_tinfo, body_tinfo, next_tinfo);
5944 
5945   if (next_tinfo.type != CPP_SEMICOLON && !open_brace)
5946     warn_for_multistatement_macros (loc_after_labels, next_tinfo.location,
5947 				    while_tinfo.location, RID_WHILE);
5948 
5949   c_break_label = save_break;
5950   c_cont_label = save_cont;
5951 }
5952 
5953 /* Parse a do statement (C90 6.6.5, C99 6.8.5, C11 6.8.5).
5954 
5955    do-statement:
5956      do statement while ( expression ) ;
5957 */
5958 
5959 static void
c_parser_do_statement(c_parser * parser,bool ivdep,unsigned short unroll)5960 c_parser_do_statement (c_parser *parser, bool ivdep, unsigned short unroll)
5961 {
5962   tree block, cond, body, save_break, save_cont, new_break, new_cont;
5963   location_t loc;
5964   gcc_assert (c_parser_next_token_is_keyword (parser, RID_DO));
5965   c_parser_consume_token (parser);
5966   if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5967     warning_at (c_parser_peek_token (parser)->location,
5968 		OPT_Wempty_body,
5969 		"suggest braces around empty body in %<do%> statement");
5970   block = c_begin_compound_stmt (flag_isoc99);
5971   loc = c_parser_peek_token (parser)->location;
5972   save_break = c_break_label;
5973   c_break_label = NULL_TREE;
5974   save_cont = c_cont_label;
5975   c_cont_label = NULL_TREE;
5976   body = c_parser_c99_block_statement (parser, NULL);
5977   c_parser_require_keyword (parser, RID_WHILE, "expected %<while%>");
5978   new_break = c_break_label;
5979   c_break_label = save_break;
5980   new_cont = c_cont_label;
5981   c_cont_label = save_cont;
5982   location_t cond_loc = c_parser_peek_token (parser)->location;
5983   cond = c_parser_paren_condition (parser);
5984   if (ivdep && cond != error_mark_node)
5985     cond = build3 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
5986 		   build_int_cst (integer_type_node,
5987 				  annot_expr_ivdep_kind),
5988 		   integer_zero_node);
5989   if (unroll && cond != error_mark_node)
5990     cond = build3 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
5991 		   build_int_cst (integer_type_node,
5992 				  annot_expr_unroll_kind),
5993  		   build_int_cst (integer_type_node, unroll));
5994   if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
5995     c_parser_skip_to_end_of_block_or_statement (parser);
5996   c_finish_loop (loc, cond_loc, cond, UNKNOWN_LOCATION, NULL, body,
5997 		 new_break, new_cont, false);
5998   add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
5999 }
6000 
6001 /* Parse a for statement (C90 6.6.5, C99 6.8.5, C11 6.8.5).
6002 
6003    for-statement:
6004      for ( expression[opt] ; expression[opt] ; expression[opt] ) statement
6005      for ( nested-declaration expression[opt] ; expression[opt] ) statement
6006 
6007    The form with a declaration is new in C99.
6008 
6009    ??? In accordance with the old parser, the declaration may be a
6010    nested function, which is then rejected in check_for_loop_decls,
6011    but does it make any sense for this to be included in the grammar?
6012    Note in particular that the nested function does not include a
6013    trailing ';', whereas the "declaration" production includes one.
6014    Also, can we reject bad declarations earlier and cheaper than
6015    check_for_loop_decls?
6016 
6017    In Objective-C, there are two additional variants:
6018 
6019    foreach-statement:
6020      for ( expression in expresssion ) statement
6021      for ( declaration in expression ) statement
6022 
6023    This is inconsistent with C, because the second variant is allowed
6024    even if c99 is not enabled.
6025 
6026    The rest of the comment documents these Objective-C foreach-statement.
6027 
6028    Here is the canonical example of the first variant:
6029     for (object in array)    { do something with object }
6030    we call the first expression ("object") the "object_expression" and
6031    the second expression ("array") the "collection_expression".
6032    object_expression must be an lvalue of type "id" (a generic Objective-C
6033    object) because the loop works by assigning to object_expression the
6034    various objects from the collection_expression.  collection_expression
6035    must evaluate to something of type "id" which responds to the method
6036    countByEnumeratingWithState:objects:count:.
6037 
6038    The canonical example of the second variant is:
6039     for (id object in array)    { do something with object }
6040    which is completely equivalent to
6041     {
6042       id object;
6043       for (object in array) { do something with object }
6044     }
6045    Note that initizializing 'object' in some way (eg, "for ((object =
6046    xxx) in array) { do something with object }") is possibly
6047    technically valid, but completely pointless as 'object' will be
6048    assigned to something else as soon as the loop starts.  We should
6049    most likely reject it (TODO).
6050 
6051    The beginning of the Objective-C foreach-statement looks exactly
6052    like the beginning of the for-statement, and we can tell it is a
6053    foreach-statement only because the initial declaration or
6054    expression is terminated by 'in' instead of ';'.
6055 
6056    IF_P is used to track whether there's a (possibly labeled) if statement
6057    which is not enclosed in braces and has an else clause.  This is used to
6058    implement -Wparentheses.  */
6059 
6060 static void
c_parser_for_statement(c_parser * parser,bool ivdep,unsigned short unroll,bool * if_p)6061 c_parser_for_statement (c_parser *parser, bool ivdep, unsigned short unroll,
6062 			bool *if_p)
6063 {
6064   tree block, cond, incr, save_break, save_cont, body;
6065   /* The following are only used when parsing an ObjC foreach statement.  */
6066   tree object_expression;
6067   /* Silence the bogus uninitialized warning.  */
6068   tree collection_expression = NULL;
6069   location_t loc = c_parser_peek_token (parser)->location;
6070   location_t for_loc = loc;
6071   location_t cond_loc = UNKNOWN_LOCATION;
6072   location_t incr_loc = UNKNOWN_LOCATION;
6073   bool is_foreach_statement = false;
6074   gcc_assert (c_parser_next_token_is_keyword (parser, RID_FOR));
6075   token_indent_info for_tinfo
6076     = get_token_indent_info (c_parser_peek_token (parser));
6077   c_parser_consume_token (parser);
6078   /* Open a compound statement in Objective-C as well, just in case this is
6079      as foreach expression.  */
6080   block = c_begin_compound_stmt (flag_isoc99 || c_dialect_objc ());
6081   cond = error_mark_node;
6082   incr = error_mark_node;
6083   matching_parens parens;
6084   if (parens.require_open (parser))
6085     {
6086       /* Parse the initialization declaration or expression.  */
6087       object_expression = error_mark_node;
6088       parser->objc_could_be_foreach_context = c_dialect_objc ();
6089       if (c_parser_next_token_is (parser, CPP_SEMICOLON))
6090 	{
6091 	  parser->objc_could_be_foreach_context = false;
6092 	  c_parser_consume_token (parser);
6093 	  c_finish_expr_stmt (loc, NULL_TREE);
6094 	}
6095       else if (c_parser_next_tokens_start_declaration (parser))
6096 	{
6097 	  c_parser_declaration_or_fndef (parser, true, true, true, true, true,
6098 					 &object_expression, vNULL);
6099 	  parser->objc_could_be_foreach_context = false;
6100 
6101 	  if (c_parser_next_token_is_keyword (parser, RID_IN))
6102 	    {
6103 	      c_parser_consume_token (parser);
6104 	      is_foreach_statement = true;
6105 	      if (check_for_loop_decls (for_loc, true) == NULL_TREE)
6106 		c_parser_error (parser, "multiple iterating variables in "
6107 					"fast enumeration");
6108 	    }
6109 	  else
6110 	    check_for_loop_decls (for_loc, flag_isoc99);
6111 	}
6112       else if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
6113 	{
6114 	  /* __extension__ can start a declaration, but is also an
6115 	     unary operator that can start an expression.  Consume all
6116 	     but the last of a possible series of __extension__ to
6117 	     determine which.  */
6118 	  while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
6119 		 && (c_parser_peek_2nd_token (parser)->keyword
6120 		     == RID_EXTENSION))
6121 	    c_parser_consume_token (parser);
6122 	  if (c_token_starts_declaration (c_parser_peek_2nd_token (parser)))
6123 	    {
6124 	      int ext;
6125 	      ext = disable_extension_diagnostics ();
6126 	      c_parser_consume_token (parser);
6127 	      c_parser_declaration_or_fndef (parser, true, true, true, true,
6128 					     true, &object_expression, vNULL);
6129 	      parser->objc_could_be_foreach_context = false;
6130 
6131 	      restore_extension_diagnostics (ext);
6132 	      if (c_parser_next_token_is_keyword (parser, RID_IN))
6133 		{
6134 		  c_parser_consume_token (parser);
6135 		  is_foreach_statement = true;
6136 		  if (check_for_loop_decls (for_loc, true) == NULL_TREE)
6137 		    c_parser_error (parser, "multiple iterating variables in "
6138 					    "fast enumeration");
6139 		}
6140 	      else
6141 		check_for_loop_decls (for_loc, flag_isoc99);
6142 	    }
6143 	  else
6144 	    goto init_expr;
6145 	}
6146       else
6147 	{
6148 	init_expr:
6149 	  {
6150 	    struct c_expr ce;
6151 	    tree init_expression;
6152 	    ce = c_parser_expression (parser);
6153 	    init_expression = ce.value;
6154 	    parser->objc_could_be_foreach_context = false;
6155 	    if (c_parser_next_token_is_keyword (parser, RID_IN))
6156 	      {
6157 		c_parser_consume_token (parser);
6158 		is_foreach_statement = true;
6159 		if (! lvalue_p (init_expression))
6160 		  c_parser_error (parser, "invalid iterating variable in "
6161 					  "fast enumeration");
6162 		object_expression
6163 		  = c_fully_fold (init_expression, false, NULL);
6164 	      }
6165 	    else
6166 	      {
6167 		ce = convert_lvalue_to_rvalue (loc, ce, true, false);
6168 		init_expression = ce.value;
6169 		c_finish_expr_stmt (loc, init_expression);
6170 		c_parser_skip_until_found (parser, CPP_SEMICOLON,
6171 					   "expected %<;%>");
6172 	      }
6173 	  }
6174 	}
6175       /* Parse the loop condition.  In the case of a foreach
6176 	 statement, there is no loop condition.  */
6177       gcc_assert (!parser->objc_could_be_foreach_context);
6178       if (!is_foreach_statement)
6179 	{
6180 	  cond_loc = c_parser_peek_token (parser)->location;
6181 	  if (c_parser_next_token_is (parser, CPP_SEMICOLON))
6182 	    {
6183 	      if (ivdep)
6184 		{
6185 		  c_parser_error (parser, "missing loop condition in loop "
6186 					  "with %<GCC ivdep%> pragma");
6187 		  cond = error_mark_node;
6188 		}
6189 	      else if (unroll)
6190 		{
6191 		  c_parser_error (parser, "missing loop condition in loop "
6192 					  "with %<GCC unroll%> pragma");
6193 		  cond = error_mark_node;
6194 		}
6195 	      else
6196 		{
6197 		  c_parser_consume_token (parser);
6198 		  cond = NULL_TREE;
6199 		}
6200 	    }
6201 	  else
6202 	    {
6203 	      cond = c_parser_condition (parser);
6204 	      c_parser_skip_until_found (parser, CPP_SEMICOLON,
6205 					 "expected %<;%>");
6206 	    }
6207 	  if (ivdep && cond != error_mark_node)
6208 	    cond = build3 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
6209 			   build_int_cst (integer_type_node,
6210 					  annot_expr_ivdep_kind),
6211 			   integer_zero_node);
6212 	  if (unroll && cond != error_mark_node)
6213 	    cond = build3 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
6214  			   build_int_cst (integer_type_node,
6215 					  annot_expr_unroll_kind),
6216 			   build_int_cst (integer_type_node, unroll));
6217 	}
6218       /* Parse the increment expression (the third expression in a
6219 	 for-statement).  In the case of a foreach-statement, this is
6220 	 the expression that follows the 'in'.  */
6221       loc = incr_loc = c_parser_peek_token (parser)->location;
6222       if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
6223 	{
6224 	  if (is_foreach_statement)
6225 	    {
6226 	      c_parser_error (parser,
6227 			      "missing collection in fast enumeration");
6228 	      collection_expression = error_mark_node;
6229 	    }
6230 	  else
6231 	    incr = c_process_expr_stmt (loc, NULL_TREE);
6232 	}
6233       else
6234 	{
6235 	  if (is_foreach_statement)
6236 	    collection_expression
6237 	      = c_fully_fold (c_parser_expression (parser).value, false, NULL);
6238 	  else
6239 	    {
6240 	      struct c_expr ce = c_parser_expression (parser);
6241 	      ce = convert_lvalue_to_rvalue (loc, ce, true, false);
6242 	      incr = c_process_expr_stmt (loc, ce.value);
6243 	    }
6244 	}
6245       parens.skip_until_found_close (parser);
6246     }
6247   save_break = c_break_label;
6248   c_break_label = NULL_TREE;
6249   save_cont = c_cont_label;
6250   c_cont_label = NULL_TREE;
6251 
6252   token_indent_info body_tinfo
6253     = get_token_indent_info (c_parser_peek_token (parser));
6254 
6255   location_t loc_after_labels;
6256   bool open_brace = c_parser_next_token_is (parser, CPP_OPEN_BRACE);
6257   body = c_parser_c99_block_statement (parser, if_p, &loc_after_labels);
6258 
6259   if (is_foreach_statement)
6260     objc_finish_foreach_loop (for_loc, object_expression,
6261 			      collection_expression, body, c_break_label,
6262 			      c_cont_label);
6263   else
6264     c_finish_loop (for_loc, cond_loc, cond, incr_loc, incr, body,
6265 		   c_break_label, c_cont_label, true);
6266   add_stmt (c_end_compound_stmt (for_loc, block,
6267 				 flag_isoc99 || c_dialect_objc ()));
6268   c_parser_maybe_reclassify_token (parser);
6269 
6270   token_indent_info next_tinfo
6271     = get_token_indent_info (c_parser_peek_token (parser));
6272   warn_for_misleading_indentation (for_tinfo, body_tinfo, next_tinfo);
6273 
6274   if (next_tinfo.type != CPP_SEMICOLON && !open_brace)
6275     warn_for_multistatement_macros (loc_after_labels, next_tinfo.location,
6276 				    for_tinfo.location, RID_FOR);
6277 
6278   c_break_label = save_break;
6279   c_cont_label = save_cont;
6280 }
6281 
6282 /* Parse an asm statement, a GNU extension.  This is a full-blown asm
6283    statement with inputs, outputs, clobbers, and volatile, inline, and goto
6284    tags allowed.
6285 
6286    asm-qualifier:
6287      volatile
6288      inline
6289      goto
6290 
6291    asm-qualifier-list:
6292      asm-qualifier-list asm-qualifier
6293      asm-qualifier
6294 
6295    asm-statement:
6296      asm asm-qualifier-list[opt] ( asm-argument ) ;
6297 
6298    asm-argument:
6299      asm-string-literal
6300      asm-string-literal : asm-operands[opt]
6301      asm-string-literal : asm-operands[opt] : asm-operands[opt]
6302      asm-string-literal : asm-operands[opt] : asm-operands[opt] \
6303        : asm-clobbers[opt]
6304      asm-string-literal : : asm-operands[opt] : asm-clobbers[opt] \
6305        : asm-goto-operands
6306 
6307    The form with asm-goto-operands is valid if and only if the
6308    asm-qualifier-list contains goto, and is the only allowed form in that case.
6309    Duplicate asm-qualifiers are not allowed.  */
6310 
6311 static tree
c_parser_asm_statement(c_parser * parser)6312 c_parser_asm_statement (c_parser *parser)
6313 {
6314   tree str, outputs, inputs, clobbers, labels, ret;
6315   bool simple;
6316   location_t asm_loc = c_parser_peek_token (parser)->location;
6317   int section, nsections;
6318 
6319   gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM));
6320   c_parser_consume_token (parser);
6321 
6322   /* Handle the asm-qualifier-list.  */
6323   location_t volatile_loc = UNKNOWN_LOCATION;
6324   location_t inline_loc = UNKNOWN_LOCATION;
6325   location_t goto_loc = UNKNOWN_LOCATION;
6326   for (;;)
6327     {
6328       c_token *token = c_parser_peek_token (parser);
6329       location_t loc = token->location;
6330       switch (token->keyword)
6331 	{
6332 	case RID_VOLATILE:
6333 	  if (volatile_loc)
6334 	    {
6335 	      error_at (loc, "duplicate asm qualifier %qE", token->value);
6336 	      inform (volatile_loc, "first seen here");
6337 	    }
6338 	  else
6339 	    volatile_loc = loc;
6340 	  c_parser_consume_token (parser);
6341 	  continue;
6342 
6343 	case RID_INLINE:
6344 	  if (inline_loc)
6345 	    {
6346 	      error_at (loc, "duplicate asm qualifier %qE", token->value);
6347 	      inform (inline_loc, "first seen here");
6348 	    }
6349 	  else
6350 	    inline_loc = loc;
6351 	  c_parser_consume_token (parser);
6352 	  continue;
6353 
6354 	case RID_GOTO:
6355 	  if (goto_loc)
6356 	    {
6357 	      error_at (loc, "duplicate asm qualifier %qE", token->value);
6358 	      inform (goto_loc, "first seen here");
6359 	    }
6360 	  else
6361 	    goto_loc = loc;
6362 	  c_parser_consume_token (parser);
6363 	  continue;
6364 
6365 	case RID_CONST:
6366 	case RID_RESTRICT:
6367 	  warning_at (loc, 0, "%qE is not an asm qualifier", token->value);
6368 	  c_parser_consume_token (parser);
6369 	  continue;
6370 
6371 	default:
6372 	  break;
6373 	}
6374       break;
6375     }
6376 
6377   bool is_volatile = (volatile_loc != UNKNOWN_LOCATION);
6378   bool is_inline = (inline_loc != UNKNOWN_LOCATION);
6379   bool is_goto = (goto_loc != UNKNOWN_LOCATION);
6380 
6381   /* ??? Follow the C++ parser rather than using the
6382      lex_untranslated_string kludge.  */
6383   parser->lex_untranslated_string = true;
6384   ret = NULL;
6385 
6386   matching_parens parens;
6387   if (!parens.require_open (parser))
6388     goto error;
6389 
6390   str = c_parser_asm_string_literal (parser);
6391   if (str == NULL_TREE)
6392     goto error_close_paren;
6393 
6394   simple = true;
6395   outputs = NULL_TREE;
6396   inputs = NULL_TREE;
6397   clobbers = NULL_TREE;
6398   labels = NULL_TREE;
6399 
6400   if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN) && !is_goto)
6401     goto done_asm;
6402 
6403   /* Parse each colon-delimited section of operands.  */
6404   nsections = 3 + is_goto;
6405   for (section = 0; section < nsections; ++section)
6406     {
6407       if (!c_parser_require (parser, CPP_COLON,
6408 			     is_goto
6409 			     ? G_("expected %<:%>")
6410 			     : G_("expected %<:%> or %<)%>"),
6411 			     UNKNOWN_LOCATION, is_goto))
6412 	goto error_close_paren;
6413 
6414       /* Once past any colon, we're no longer a simple asm.  */
6415       simple = false;
6416 
6417       if ((!c_parser_next_token_is (parser, CPP_COLON)
6418 	   && !c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
6419 	  || section == 3)
6420 	switch (section)
6421 	  {
6422 	  case 0:
6423 	    /* For asm goto, we don't allow output operands, but reserve
6424 	       the slot for a future extension that does allow them.  */
6425 	    if (!is_goto)
6426 	      outputs = c_parser_asm_operands (parser);
6427 	    break;
6428 	  case 1:
6429 	    inputs = c_parser_asm_operands (parser);
6430 	    break;
6431 	  case 2:
6432 	    clobbers = c_parser_asm_clobbers (parser);
6433 	    break;
6434 	  case 3:
6435 	    labels = c_parser_asm_goto_operands (parser);
6436 	    break;
6437 	  default:
6438 	    gcc_unreachable ();
6439 	  }
6440 
6441       if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN) && !is_goto)
6442 	goto done_asm;
6443     }
6444 
6445  done_asm:
6446   if (!parens.require_close (parser))
6447     {
6448       c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6449       goto error;
6450     }
6451 
6452   if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
6453     c_parser_skip_to_end_of_block_or_statement (parser);
6454 
6455   ret = build_asm_stmt (is_volatile,
6456 			build_asm_expr (asm_loc, str, outputs, inputs,
6457 					clobbers, labels, simple, is_inline));
6458 
6459  error:
6460   parser->lex_untranslated_string = false;
6461   return ret;
6462 
6463  error_close_paren:
6464   c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6465   goto error;
6466 }
6467 
6468 /* Parse asm operands, a GNU extension.
6469 
6470    asm-operands:
6471      asm-operand
6472      asm-operands , asm-operand
6473 
6474    asm-operand:
6475      asm-string-literal ( expression )
6476      [ identifier ] asm-string-literal ( expression )
6477 */
6478 
6479 static tree
c_parser_asm_operands(c_parser * parser)6480 c_parser_asm_operands (c_parser *parser)
6481 {
6482   tree list = NULL_TREE;
6483   while (true)
6484     {
6485       tree name, str;
6486       struct c_expr expr;
6487       if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
6488 	{
6489 	  c_parser_consume_token (parser);
6490 	  if (c_parser_next_token_is (parser, CPP_NAME))
6491 	    {
6492 	      tree id = c_parser_peek_token (parser)->value;
6493 	      c_parser_consume_token (parser);
6494 	      name = build_string (IDENTIFIER_LENGTH (id),
6495 				   IDENTIFIER_POINTER (id));
6496 	    }
6497 	  else
6498 	    {
6499 	      c_parser_error (parser, "expected identifier");
6500 	      c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
6501 	      return NULL_TREE;
6502 	    }
6503 	  c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
6504 				     "expected %<]%>");
6505 	}
6506       else
6507 	name = NULL_TREE;
6508       str = c_parser_asm_string_literal (parser);
6509       if (str == NULL_TREE)
6510 	return NULL_TREE;
6511       parser->lex_untranslated_string = false;
6512       matching_parens parens;
6513       if (!parens.require_open (parser))
6514 	{
6515 	  parser->lex_untranslated_string = true;
6516 	  return NULL_TREE;
6517 	}
6518       expr = c_parser_expression (parser);
6519       mark_exp_read (expr.value);
6520       parser->lex_untranslated_string = true;
6521       if (!parens.require_close (parser))
6522 	{
6523 	  c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6524 	  return NULL_TREE;
6525 	}
6526       list = chainon (list, build_tree_list (build_tree_list (name, str),
6527 					     expr.value));
6528       if (c_parser_next_token_is (parser, CPP_COMMA))
6529 	c_parser_consume_token (parser);
6530       else
6531 	break;
6532     }
6533   return list;
6534 }
6535 
6536 /* Parse asm clobbers, a GNU extension.
6537 
6538    asm-clobbers:
6539      asm-string-literal
6540      asm-clobbers , asm-string-literal
6541 */
6542 
6543 static tree
c_parser_asm_clobbers(c_parser * parser)6544 c_parser_asm_clobbers (c_parser *parser)
6545 {
6546   tree list = NULL_TREE;
6547   while (true)
6548     {
6549       tree str = c_parser_asm_string_literal (parser);
6550       if (str)
6551 	list = tree_cons (NULL_TREE, str, list);
6552       else
6553 	return NULL_TREE;
6554       if (c_parser_next_token_is (parser, CPP_COMMA))
6555 	c_parser_consume_token (parser);
6556       else
6557 	break;
6558     }
6559   return list;
6560 }
6561 
6562 /* Parse asm goto labels, a GNU extension.
6563 
6564    asm-goto-operands:
6565      identifier
6566      asm-goto-operands , identifier
6567 */
6568 
6569 static tree
c_parser_asm_goto_operands(c_parser * parser)6570 c_parser_asm_goto_operands (c_parser *parser)
6571 {
6572   tree list = NULL_TREE;
6573   while (true)
6574     {
6575       tree name, label;
6576 
6577       if (c_parser_next_token_is (parser, CPP_NAME))
6578 	{
6579 	  c_token *tok = c_parser_peek_token (parser);
6580 	  name = tok->value;
6581 	  label = lookup_label_for_goto (tok->location, name);
6582 	  c_parser_consume_token (parser);
6583 	  TREE_USED (label) = 1;
6584 	}
6585       else
6586 	{
6587 	  c_parser_error (parser, "expected identifier");
6588 	  return NULL_TREE;
6589 	}
6590 
6591       name = build_string (IDENTIFIER_LENGTH (name),
6592 			   IDENTIFIER_POINTER (name));
6593       list = tree_cons (name, label, list);
6594       if (c_parser_next_token_is (parser, CPP_COMMA))
6595 	c_parser_consume_token (parser);
6596       else
6597 	return nreverse (list);
6598     }
6599 }
6600 
6601 /* Parse an expression other than a compound expression; that is, an
6602    assignment expression (C90 6.3.16, C99 6.5.16, C11 6.5.16).  If
6603    AFTER is not NULL then it is an Objective-C message expression which
6604    is the primary-expression starting the expression as an initializer.
6605 
6606    assignment-expression:
6607      conditional-expression
6608      unary-expression assignment-operator assignment-expression
6609 
6610    assignment-operator: one of
6611      = *= /= %= += -= <<= >>= &= ^= |=
6612 
6613    In GNU C we accept any conditional expression on the LHS and
6614    diagnose the invalid lvalue rather than producing a syntax
6615    error.  */
6616 
6617 static struct c_expr
c_parser_expr_no_commas(c_parser * parser,struct c_expr * after,tree omp_atomic_lhs)6618 c_parser_expr_no_commas (c_parser *parser, struct c_expr *after,
6619 			 tree omp_atomic_lhs)
6620 {
6621   struct c_expr lhs, rhs, ret;
6622   enum tree_code code;
6623   location_t op_location, exp_location;
6624   gcc_assert (!after || c_dialect_objc ());
6625   lhs = c_parser_conditional_expression (parser, after, omp_atomic_lhs);
6626   op_location = c_parser_peek_token (parser)->location;
6627   switch (c_parser_peek_token (parser)->type)
6628     {
6629     case CPP_EQ:
6630       code = NOP_EXPR;
6631       break;
6632     case CPP_MULT_EQ:
6633       code = MULT_EXPR;
6634       break;
6635     case CPP_DIV_EQ:
6636       code = TRUNC_DIV_EXPR;
6637       break;
6638     case CPP_MOD_EQ:
6639       code = TRUNC_MOD_EXPR;
6640       break;
6641     case CPP_PLUS_EQ:
6642       code = PLUS_EXPR;
6643       break;
6644     case CPP_MINUS_EQ:
6645       code = MINUS_EXPR;
6646       break;
6647     case CPP_LSHIFT_EQ:
6648       code = LSHIFT_EXPR;
6649       break;
6650     case CPP_RSHIFT_EQ:
6651       code = RSHIFT_EXPR;
6652       break;
6653     case CPP_AND_EQ:
6654       code = BIT_AND_EXPR;
6655       break;
6656     case CPP_XOR_EQ:
6657       code = BIT_XOR_EXPR;
6658       break;
6659     case CPP_OR_EQ:
6660       code = BIT_IOR_EXPR;
6661       break;
6662     default:
6663       return lhs;
6664     }
6665   c_parser_consume_token (parser);
6666   exp_location = c_parser_peek_token (parser)->location;
6667   rhs = c_parser_expr_no_commas (parser, NULL);
6668   rhs = convert_lvalue_to_rvalue (exp_location, rhs, true, true);
6669 
6670   ret.value = build_modify_expr (op_location, lhs.value, lhs.original_type,
6671 				 code, exp_location, rhs.value,
6672 				 rhs.original_type);
6673   set_c_expr_source_range (&ret, lhs.get_start (), rhs.get_finish ());
6674   if (code == NOP_EXPR)
6675     ret.original_code = MODIFY_EXPR;
6676   else
6677     {
6678       TREE_NO_WARNING (ret.value) = 1;
6679       ret.original_code = ERROR_MARK;
6680     }
6681   ret.original_type = NULL;
6682   return ret;
6683 }
6684 
6685 /* Parse a conditional expression (C90 6.3.15, C99 6.5.15, C11 6.5.15).  If
6686    AFTER is not NULL then it is an Objective-C message expression which is
6687    the primary-expression starting the expression as an initializer.
6688 
6689    conditional-expression:
6690      logical-OR-expression
6691      logical-OR-expression ? expression : conditional-expression
6692 
6693    GNU extensions:
6694 
6695    conditional-expression:
6696      logical-OR-expression ? : conditional-expression
6697 */
6698 
6699 static struct c_expr
c_parser_conditional_expression(c_parser * parser,struct c_expr * after,tree omp_atomic_lhs)6700 c_parser_conditional_expression (c_parser *parser, struct c_expr *after,
6701 				 tree omp_atomic_lhs)
6702 {
6703   struct c_expr cond, exp1, exp2, ret;
6704   location_t start, cond_loc, colon_loc;
6705 
6706   gcc_assert (!after || c_dialect_objc ());
6707 
6708   cond = c_parser_binary_expression (parser, after, omp_atomic_lhs);
6709 
6710   if (c_parser_next_token_is_not (parser, CPP_QUERY))
6711     return cond;
6712   if (cond.value != error_mark_node)
6713     start = cond.get_start ();
6714   else
6715     start = UNKNOWN_LOCATION;
6716   cond_loc = c_parser_peek_token (parser)->location;
6717   cond = convert_lvalue_to_rvalue (cond_loc, cond, true, true);
6718   c_parser_consume_token (parser);
6719   if (c_parser_next_token_is (parser, CPP_COLON))
6720     {
6721       tree eptype = NULL_TREE;
6722 
6723       location_t middle_loc = c_parser_peek_token (parser)->location;
6724       pedwarn (middle_loc, OPT_Wpedantic,
6725 	       "ISO C forbids omitting the middle term of a ?: expression");
6726       if (TREE_CODE (cond.value) == EXCESS_PRECISION_EXPR)
6727 	{
6728 	  eptype = TREE_TYPE (cond.value);
6729 	  cond.value = TREE_OPERAND (cond.value, 0);
6730 	}
6731       tree e = cond.value;
6732       while (TREE_CODE (e) == COMPOUND_EXPR)
6733 	e = TREE_OPERAND (e, 1);
6734       warn_for_omitted_condop (middle_loc, e);
6735       /* Make sure first operand is calculated only once.  */
6736       exp1.value = save_expr (default_conversion (cond.value));
6737       if (eptype)
6738 	exp1.value = build1 (EXCESS_PRECISION_EXPR, eptype, exp1.value);
6739       exp1.original_type = NULL;
6740       exp1.src_range = cond.src_range;
6741       cond.value = c_objc_common_truthvalue_conversion (cond_loc, exp1.value);
6742       c_inhibit_evaluation_warnings += cond.value == truthvalue_true_node;
6743     }
6744   else
6745     {
6746       cond.value
6747 	= c_objc_common_truthvalue_conversion
6748 	(cond_loc, default_conversion (cond.value));
6749       c_inhibit_evaluation_warnings += cond.value == truthvalue_false_node;
6750       exp1 = c_parser_expression_conv (parser);
6751       mark_exp_read (exp1.value);
6752       c_inhibit_evaluation_warnings +=
6753 	((cond.value == truthvalue_true_node)
6754 	 - (cond.value == truthvalue_false_node));
6755     }
6756 
6757   colon_loc = c_parser_peek_token (parser)->location;
6758   if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
6759     {
6760       c_inhibit_evaluation_warnings -= cond.value == truthvalue_true_node;
6761       ret.set_error ();
6762       ret.original_code = ERROR_MARK;
6763       ret.original_type = NULL;
6764       return ret;
6765     }
6766   {
6767     location_t exp2_loc = c_parser_peek_token (parser)->location;
6768     exp2 = c_parser_conditional_expression (parser, NULL, NULL_TREE);
6769     exp2 = convert_lvalue_to_rvalue (exp2_loc, exp2, true, true);
6770   }
6771   c_inhibit_evaluation_warnings -= cond.value == truthvalue_true_node;
6772   location_t loc1 = make_location (exp1.get_start (), exp1.src_range);
6773   location_t loc2 = make_location (exp2.get_start (), exp2.src_range);
6774   ret.value = build_conditional_expr (colon_loc, cond.value,
6775 				      cond.original_code == C_MAYBE_CONST_EXPR,
6776 				      exp1.value, exp1.original_type, loc1,
6777 				      exp2.value, exp2.original_type, loc2);
6778   ret.original_code = ERROR_MARK;
6779   if (exp1.value == error_mark_node || exp2.value == error_mark_node)
6780     ret.original_type = NULL;
6781   else
6782     {
6783       tree t1, t2;
6784 
6785       /* If both sides are enum type, the default conversion will have
6786 	 made the type of the result be an integer type.  We want to
6787 	 remember the enum types we started with.  */
6788       t1 = exp1.original_type ? exp1.original_type : TREE_TYPE (exp1.value);
6789       t2 = exp2.original_type ? exp2.original_type : TREE_TYPE (exp2.value);
6790       ret.original_type = ((t1 != error_mark_node
6791 			    && t2 != error_mark_node
6792 			    && (TYPE_MAIN_VARIANT (t1)
6793 				== TYPE_MAIN_VARIANT (t2)))
6794 			   ? t1
6795 			   : NULL);
6796     }
6797   set_c_expr_source_range (&ret, start, exp2.get_finish ());
6798   return ret;
6799 }
6800 
6801 /* Parse a binary expression; that is, a logical-OR-expression (C90
6802    6.3.5-6.3.14, C99 6.5.5-6.5.14, C11 6.5.5-6.5.14).  If AFTER is not
6803    NULL then it is an Objective-C message expression which is the
6804    primary-expression starting the expression as an initializer.
6805 
6806    OMP_ATOMIC_LHS is NULL, unless parsing OpenMP #pragma omp atomic,
6807    when it should be the unfolded lhs.  In a valid OpenMP source,
6808    one of the operands of the toplevel binary expression must be equal
6809    to it.  In that case, just return a build2 created binary operation
6810    rather than result of parser_build_binary_op.
6811 
6812    multiplicative-expression:
6813      cast-expression
6814      multiplicative-expression * cast-expression
6815      multiplicative-expression / cast-expression
6816      multiplicative-expression % cast-expression
6817 
6818    additive-expression:
6819      multiplicative-expression
6820      additive-expression + multiplicative-expression
6821      additive-expression - multiplicative-expression
6822 
6823    shift-expression:
6824      additive-expression
6825      shift-expression << additive-expression
6826      shift-expression >> additive-expression
6827 
6828    relational-expression:
6829      shift-expression
6830      relational-expression < shift-expression
6831      relational-expression > shift-expression
6832      relational-expression <= shift-expression
6833      relational-expression >= shift-expression
6834 
6835    equality-expression:
6836      relational-expression
6837      equality-expression == relational-expression
6838      equality-expression != relational-expression
6839 
6840    AND-expression:
6841      equality-expression
6842      AND-expression & equality-expression
6843 
6844    exclusive-OR-expression:
6845      AND-expression
6846      exclusive-OR-expression ^ AND-expression
6847 
6848    inclusive-OR-expression:
6849      exclusive-OR-expression
6850      inclusive-OR-expression | exclusive-OR-expression
6851 
6852    logical-AND-expression:
6853      inclusive-OR-expression
6854      logical-AND-expression && inclusive-OR-expression
6855 
6856    logical-OR-expression:
6857      logical-AND-expression
6858      logical-OR-expression || logical-AND-expression
6859 */
6860 
6861 static struct c_expr
c_parser_binary_expression(c_parser * parser,struct c_expr * after,tree omp_atomic_lhs)6862 c_parser_binary_expression (c_parser *parser, struct c_expr *after,
6863 			    tree omp_atomic_lhs)
6864 {
6865   /* A binary expression is parsed using operator-precedence parsing,
6866      with the operands being cast expressions.  All the binary
6867      operators are left-associative.  Thus a binary expression is of
6868      form:
6869 
6870      E0 op1 E1 op2 E2 ...
6871 
6872      which we represent on a stack.  On the stack, the precedence
6873      levels are strictly increasing.  When a new operator is
6874      encountered of higher precedence than that at the top of the
6875      stack, it is pushed; its LHS is the top expression, and its RHS
6876      is everything parsed until it is popped.  When a new operator is
6877      encountered with precedence less than or equal to that at the top
6878      of the stack, triples E[i-1] op[i] E[i] are popped and replaced
6879      by the result of the operation until the operator at the top of
6880      the stack has lower precedence than the new operator or there is
6881      only one element on the stack; then the top expression is the LHS
6882      of the new operator.  In the case of logical AND and OR
6883      expressions, we also need to adjust c_inhibit_evaluation_warnings
6884      as appropriate when the operators are pushed and popped.  */
6885 
6886   struct {
6887     /* The expression at this stack level.  */
6888     struct c_expr expr;
6889     /* The precedence of the operator on its left, PREC_NONE at the
6890        bottom of the stack.  */
6891     enum c_parser_prec prec;
6892     /* The operation on its left.  */
6893     enum tree_code op;
6894     /* The source location of this operation.  */
6895     location_t loc;
6896     /* The sizeof argument if expr.original_code == SIZEOF_EXPR.  */
6897     tree sizeof_arg;
6898   } stack[NUM_PRECS];
6899   int sp;
6900   /* Location of the binary operator.  */
6901   location_t binary_loc = UNKNOWN_LOCATION;  /* Quiet warning.  */
6902 #define POP								      \
6903   do {									      \
6904     switch (stack[sp].op)						      \
6905       {									      \
6906       case TRUTH_ANDIF_EXPR:						      \
6907 	c_inhibit_evaluation_warnings -= (stack[sp - 1].expr.value	      \
6908 					  == truthvalue_false_node);	      \
6909 	break;								      \
6910       case TRUTH_ORIF_EXPR:						      \
6911 	c_inhibit_evaluation_warnings -= (stack[sp - 1].expr.value	      \
6912 					  == truthvalue_true_node);	      \
6913 	break;								      \
6914       case TRUNC_DIV_EXPR: 						      \
6915 	if (stack[sp - 1].expr.original_code == SIZEOF_EXPR		      \
6916 	    && stack[sp].expr.original_code == SIZEOF_EXPR)		      \
6917 	  {								      \
6918 	    tree type0 = stack[sp - 1].sizeof_arg;			      \
6919 	    tree type1 = stack[sp].sizeof_arg;				      \
6920 	    tree first_arg = type0;					      \
6921 	    if (!TYPE_P (type0))					      \
6922 	      type0 = TREE_TYPE (type0);				      \
6923 	    if (!TYPE_P (type1))					      \
6924 	      type1 = TREE_TYPE (type1);				      \
6925 	    if (POINTER_TYPE_P (type0)					      \
6926 		&& comptypes (TREE_TYPE (type0), type1)			      \
6927 		&& !(TREE_CODE (first_arg) == PARM_DECL			      \
6928 		     && C_ARRAY_PARAMETER (first_arg)			      \
6929 		     && warn_sizeof_array_argument))			      \
6930 	      if (warning_at (stack[sp].loc, OPT_Wsizeof_pointer_div,	      \
6931 			      "division %<sizeof (%T) / sizeof (%T)%> does "  \
6932 			      "not compute the number of array elements",     \
6933 			      type0, type1))				      \
6934 		if (DECL_P (first_arg))					      \
6935 		  inform (DECL_SOURCE_LOCATION (first_arg),		      \
6936 			  "first %<sizeof%> operand was declared here");      \
6937 	  }								      \
6938 	break;								      \
6939       default:								      \
6940 	break;								      \
6941       }									      \
6942     stack[sp - 1].expr							      \
6943       = convert_lvalue_to_rvalue (stack[sp - 1].loc,			      \
6944 				  stack[sp - 1].expr, true, true);	      \
6945     stack[sp].expr							      \
6946       = convert_lvalue_to_rvalue (stack[sp].loc,			      \
6947 				  stack[sp].expr, true, true);		      \
6948     if (__builtin_expect (omp_atomic_lhs != NULL_TREE, 0) && sp == 1	      \
6949 	&& c_parser_peek_token (parser)->type == CPP_SEMICOLON		      \
6950 	&& ((1 << stack[sp].prec)					      \
6951 	    & ((1 << PREC_BITOR) | (1 << PREC_BITXOR) | (1 << PREC_BITAND)    \
6952 	       | (1 << PREC_SHIFT) | (1 << PREC_ADD) | (1 << PREC_MULT)))     \
6953 	&& stack[sp].op != TRUNC_MOD_EXPR				      \
6954 	&& stack[0].expr.value != error_mark_node			      \
6955 	&& stack[1].expr.value != error_mark_node			      \
6956 	&& (c_tree_equal (stack[0].expr.value, omp_atomic_lhs)		      \
6957 	    || c_tree_equal (stack[1].expr.value, omp_atomic_lhs)))	      \
6958       stack[0].expr.value						      \
6959 	= build2 (stack[1].op, TREE_TYPE (stack[0].expr.value),		      \
6960 		  stack[0].expr.value, stack[1].expr.value);		      \
6961     else								      \
6962       stack[sp - 1].expr = parser_build_binary_op (stack[sp].loc,	      \
6963 						   stack[sp].op,	      \
6964 						   stack[sp - 1].expr,	      \
6965 						   stack[sp].expr);	      \
6966     sp--;								      \
6967   } while (0)
6968   gcc_assert (!after || c_dialect_objc ());
6969   stack[0].loc = c_parser_peek_token (parser)->location;
6970   stack[0].expr = c_parser_cast_expression (parser, after);
6971   stack[0].prec = PREC_NONE;
6972   stack[0].sizeof_arg = c_last_sizeof_arg;
6973   sp = 0;
6974   while (true)
6975     {
6976       enum c_parser_prec oprec;
6977       enum tree_code ocode;
6978       source_range src_range;
6979       if (parser->error)
6980 	goto out;
6981       switch (c_parser_peek_token (parser)->type)
6982 	{
6983 	case CPP_MULT:
6984 	  oprec = PREC_MULT;
6985 	  ocode = MULT_EXPR;
6986 	  break;
6987 	case CPP_DIV:
6988 	  oprec = PREC_MULT;
6989 	  ocode = TRUNC_DIV_EXPR;
6990 	  break;
6991 	case CPP_MOD:
6992 	  oprec = PREC_MULT;
6993 	  ocode = TRUNC_MOD_EXPR;
6994 	  break;
6995 	case CPP_PLUS:
6996 	  oprec = PREC_ADD;
6997 	  ocode = PLUS_EXPR;
6998 	  break;
6999 	case CPP_MINUS:
7000 	  oprec = PREC_ADD;
7001 	  ocode = MINUS_EXPR;
7002 	  break;
7003 	case CPP_LSHIFT:
7004 	  oprec = PREC_SHIFT;
7005 	  ocode = LSHIFT_EXPR;
7006 	  break;
7007 	case CPP_RSHIFT:
7008 	  oprec = PREC_SHIFT;
7009 	  ocode = RSHIFT_EXPR;
7010 	  break;
7011 	case CPP_LESS:
7012 	  oprec = PREC_REL;
7013 	  ocode = LT_EXPR;
7014 	  break;
7015 	case CPP_GREATER:
7016 	  oprec = PREC_REL;
7017 	  ocode = GT_EXPR;
7018 	  break;
7019 	case CPP_LESS_EQ:
7020 	  oprec = PREC_REL;
7021 	  ocode = LE_EXPR;
7022 	  break;
7023 	case CPP_GREATER_EQ:
7024 	  oprec = PREC_REL;
7025 	  ocode = GE_EXPR;
7026 	  break;
7027 	case CPP_EQ_EQ:
7028 	  oprec = PREC_EQ;
7029 	  ocode = EQ_EXPR;
7030 	  break;
7031 	case CPP_NOT_EQ:
7032 	  oprec = PREC_EQ;
7033 	  ocode = NE_EXPR;
7034 	  break;
7035 	case CPP_AND:
7036 	  oprec = PREC_BITAND;
7037 	  ocode = BIT_AND_EXPR;
7038 	  break;
7039 	case CPP_XOR:
7040 	  oprec = PREC_BITXOR;
7041 	  ocode = BIT_XOR_EXPR;
7042 	  break;
7043 	case CPP_OR:
7044 	  oprec = PREC_BITOR;
7045 	  ocode = BIT_IOR_EXPR;
7046 	  break;
7047 	case CPP_AND_AND:
7048 	  oprec = PREC_LOGAND;
7049 	  ocode = TRUTH_ANDIF_EXPR;
7050 	  break;
7051 	case CPP_OR_OR:
7052 	  oprec = PREC_LOGOR;
7053 	  ocode = TRUTH_ORIF_EXPR;
7054 	  break;
7055 	default:
7056 	  /* Not a binary operator, so end of the binary
7057 	     expression.  */
7058 	  goto out;
7059 	}
7060       binary_loc = c_parser_peek_token (parser)->location;
7061       while (oprec <= stack[sp].prec)
7062 	POP;
7063       c_parser_consume_token (parser);
7064       switch (ocode)
7065 	{
7066 	case TRUTH_ANDIF_EXPR:
7067 	  src_range = stack[sp].expr.src_range;
7068 	  stack[sp].expr
7069 	    = convert_lvalue_to_rvalue (stack[sp].loc,
7070 					stack[sp].expr, true, true);
7071 	  stack[sp].expr.value = c_objc_common_truthvalue_conversion
7072 	    (stack[sp].loc, default_conversion (stack[sp].expr.value));
7073 	  c_inhibit_evaluation_warnings += (stack[sp].expr.value
7074 					    == truthvalue_false_node);
7075 	  set_c_expr_source_range (&stack[sp].expr, src_range);
7076 	  break;
7077 	case TRUTH_ORIF_EXPR:
7078 	  src_range = stack[sp].expr.src_range;
7079 	  stack[sp].expr
7080 	    = convert_lvalue_to_rvalue (stack[sp].loc,
7081 					stack[sp].expr, true, true);
7082 	  stack[sp].expr.value = c_objc_common_truthvalue_conversion
7083 	    (stack[sp].loc, default_conversion (stack[sp].expr.value));
7084 	  c_inhibit_evaluation_warnings += (stack[sp].expr.value
7085 					    == truthvalue_true_node);
7086 	  set_c_expr_source_range (&stack[sp].expr, src_range);
7087 	  break;
7088 	default:
7089 	  break;
7090 	}
7091       sp++;
7092       stack[sp].loc = binary_loc;
7093       stack[sp].expr = c_parser_cast_expression (parser, NULL);
7094       stack[sp].prec = oprec;
7095       stack[sp].op = ocode;
7096       stack[sp].sizeof_arg = c_last_sizeof_arg;
7097     }
7098  out:
7099   while (sp > 0)
7100     POP;
7101   return stack[0].expr;
7102 #undef POP
7103 }
7104 
7105 /* Parse a cast expression (C90 6.3.4, C99 6.5.4, C11 6.5.4).  If AFTER
7106    is not NULL then it is an Objective-C message expression which is the
7107    primary-expression starting the expression as an initializer.
7108 
7109    cast-expression:
7110      unary-expression
7111      ( type-name ) unary-expression
7112 */
7113 
7114 static struct c_expr
c_parser_cast_expression(c_parser * parser,struct c_expr * after)7115 c_parser_cast_expression (c_parser *parser, struct c_expr *after)
7116 {
7117   location_t cast_loc = c_parser_peek_token (parser)->location;
7118   gcc_assert (!after || c_dialect_objc ());
7119   if (after)
7120     return c_parser_postfix_expression_after_primary (parser,
7121 						      cast_loc, *after);
7122   /* If the expression begins with a parenthesized type name, it may
7123      be either a cast or a compound literal; we need to see whether
7124      the next character is '{' to tell the difference.  If not, it is
7125      an unary expression.  Full detection of unknown typenames here
7126      would require a 3-token lookahead.  */
7127   if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
7128       && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
7129     {
7130       struct c_type_name *type_name;
7131       struct c_expr ret;
7132       struct c_expr expr;
7133       matching_parens parens;
7134       parens.consume_open (parser);
7135       type_name = c_parser_type_name (parser, true);
7136       parens.skip_until_found_close (parser);
7137       if (type_name == NULL)
7138 	{
7139 	  ret.set_error ();
7140 	  ret.original_code = ERROR_MARK;
7141 	  ret.original_type = NULL;
7142 	  return ret;
7143 	}
7144 
7145       /* Save casted types in the function's used types hash table.  */
7146       used_types_insert (type_name->specs->type);
7147 
7148       if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
7149 	return c_parser_postfix_expression_after_paren_type (parser, type_name,
7150 							     cast_loc);
7151       if (type_name->specs->alignas_p)
7152 	error_at (type_name->specs->locations[cdw_alignas],
7153 		  "alignment specified for type name in cast");
7154       {
7155 	location_t expr_loc = c_parser_peek_token (parser)->location;
7156 	expr = c_parser_cast_expression (parser, NULL);
7157 	expr = convert_lvalue_to_rvalue (expr_loc, expr, true, true);
7158       }
7159       ret.value = c_cast_expr (cast_loc, type_name, expr.value);
7160       if (ret.value && expr.value)
7161 	set_c_expr_source_range (&ret, cast_loc, expr.get_finish ());
7162       ret.original_code = ERROR_MARK;
7163       ret.original_type = NULL;
7164       return ret;
7165     }
7166   else
7167     return c_parser_unary_expression (parser);
7168 }
7169 
7170 /* Parse an unary expression (C90 6.3.3, C99 6.5.3, C11 6.5.3).
7171 
7172    unary-expression:
7173      postfix-expression
7174      ++ unary-expression
7175      -- unary-expression
7176      unary-operator cast-expression
7177      sizeof unary-expression
7178      sizeof ( type-name )
7179 
7180    unary-operator: one of
7181      & * + - ~ !
7182 
7183    GNU extensions:
7184 
7185    unary-expression:
7186      __alignof__ unary-expression
7187      __alignof__ ( type-name )
7188      && identifier
7189 
7190    (C11 permits _Alignof with type names only.)
7191 
7192    unary-operator: one of
7193      __extension__ __real__ __imag__
7194 
7195    Transactional Memory:
7196 
7197    unary-expression:
7198      transaction-expression
7199 
7200    In addition, the GNU syntax treats ++ and -- as unary operators, so
7201    they may be applied to cast expressions with errors for non-lvalues
7202    given later.  */
7203 
7204 static struct c_expr
c_parser_unary_expression(c_parser * parser)7205 c_parser_unary_expression (c_parser *parser)
7206 {
7207   int ext;
7208   struct c_expr ret, op;
7209   location_t op_loc = c_parser_peek_token (parser)->location;
7210   location_t exp_loc;
7211   location_t finish;
7212   ret.original_code = ERROR_MARK;
7213   ret.original_type = NULL;
7214   switch (c_parser_peek_token (parser)->type)
7215     {
7216     case CPP_PLUS_PLUS:
7217       c_parser_consume_token (parser);
7218       exp_loc = c_parser_peek_token (parser)->location;
7219       op = c_parser_cast_expression (parser, NULL);
7220 
7221       op = default_function_array_read_conversion (exp_loc, op);
7222       return parser_build_unary_op (op_loc, PREINCREMENT_EXPR, op);
7223     case CPP_MINUS_MINUS:
7224       c_parser_consume_token (parser);
7225       exp_loc = c_parser_peek_token (parser)->location;
7226       op = c_parser_cast_expression (parser, NULL);
7227 
7228       op = default_function_array_read_conversion (exp_loc, op);
7229       return parser_build_unary_op (op_loc, PREDECREMENT_EXPR, op);
7230     case CPP_AND:
7231       c_parser_consume_token (parser);
7232       op = c_parser_cast_expression (parser, NULL);
7233       mark_exp_read (op.value);
7234       return parser_build_unary_op (op_loc, ADDR_EXPR, op);
7235     case CPP_MULT:
7236       {
7237 	c_parser_consume_token (parser);
7238 	exp_loc = c_parser_peek_token (parser)->location;
7239 	op = c_parser_cast_expression (parser, NULL);
7240 	finish = op.get_finish ();
7241 	op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
7242 	location_t combined_loc = make_location (op_loc, op_loc, finish);
7243 	ret.value = build_indirect_ref (combined_loc, op.value, RO_UNARY_STAR);
7244 	ret.src_range.m_start = op_loc;
7245 	ret.src_range.m_finish = finish;
7246 	return ret;
7247       }
7248     case CPP_PLUS:
7249       if (!c_dialect_objc () && !in_system_header_at (input_location))
7250 	warning_at (op_loc,
7251 		    OPT_Wtraditional,
7252 		    "traditional C rejects the unary plus operator");
7253       c_parser_consume_token (parser);
7254       exp_loc = c_parser_peek_token (parser)->location;
7255       op = c_parser_cast_expression (parser, NULL);
7256       op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
7257       return parser_build_unary_op (op_loc, CONVERT_EXPR, op);
7258     case CPP_MINUS:
7259       c_parser_consume_token (parser);
7260       exp_loc = c_parser_peek_token (parser)->location;
7261       op = c_parser_cast_expression (parser, NULL);
7262       op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
7263       return parser_build_unary_op (op_loc, NEGATE_EXPR, op);
7264     case CPP_COMPL:
7265       c_parser_consume_token (parser);
7266       exp_loc = c_parser_peek_token (parser)->location;
7267       op = c_parser_cast_expression (parser, NULL);
7268       op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
7269       return parser_build_unary_op (op_loc, BIT_NOT_EXPR, op);
7270     case CPP_NOT:
7271       c_parser_consume_token (parser);
7272       exp_loc = c_parser_peek_token (parser)->location;
7273       op = c_parser_cast_expression (parser, NULL);
7274       op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
7275       return parser_build_unary_op (op_loc, TRUTH_NOT_EXPR, op);
7276     case CPP_AND_AND:
7277       /* Refer to the address of a label as a pointer.  */
7278       c_parser_consume_token (parser);
7279       if (c_parser_next_token_is (parser, CPP_NAME))
7280 	{
7281 	  ret.value = finish_label_address_expr
7282 	    (c_parser_peek_token (parser)->value, op_loc);
7283 	  set_c_expr_source_range (&ret, op_loc,
7284 				   c_parser_peek_token (parser)->get_finish ());
7285 	  c_parser_consume_token (parser);
7286 	}
7287       else
7288 	{
7289 	  c_parser_error (parser, "expected identifier");
7290 	  ret.set_error ();
7291 	}
7292       return ret;
7293     case CPP_KEYWORD:
7294       switch (c_parser_peek_token (parser)->keyword)
7295 	{
7296 	case RID_SIZEOF:
7297 	  return c_parser_sizeof_expression (parser);
7298 	case RID_ALIGNOF:
7299 	  return c_parser_alignof_expression (parser);
7300 	case RID_EXTENSION:
7301 	  c_parser_consume_token (parser);
7302 	  ext = disable_extension_diagnostics ();
7303 	  ret = c_parser_cast_expression (parser, NULL);
7304 	  restore_extension_diagnostics (ext);
7305 	  return ret;
7306 	case RID_REALPART:
7307 	  c_parser_consume_token (parser);
7308 	  exp_loc = c_parser_peek_token (parser)->location;
7309 	  op = c_parser_cast_expression (parser, NULL);
7310 	  op = default_function_array_conversion (exp_loc, op);
7311 	  return parser_build_unary_op (op_loc, REALPART_EXPR, op);
7312 	case RID_IMAGPART:
7313 	  c_parser_consume_token (parser);
7314 	  exp_loc = c_parser_peek_token (parser)->location;
7315 	  op = c_parser_cast_expression (parser, NULL);
7316 	  op = default_function_array_conversion (exp_loc, op);
7317 	  return parser_build_unary_op (op_loc, IMAGPART_EXPR, op);
7318 	case RID_TRANSACTION_ATOMIC:
7319 	case RID_TRANSACTION_RELAXED:
7320 	  return c_parser_transaction_expression (parser,
7321 	      c_parser_peek_token (parser)->keyword);
7322 	default:
7323 	  return c_parser_postfix_expression (parser);
7324 	}
7325     default:
7326       return c_parser_postfix_expression (parser);
7327     }
7328 }
7329 
7330 /* Parse a sizeof expression.  */
7331 
7332 static struct c_expr
c_parser_sizeof_expression(c_parser * parser)7333 c_parser_sizeof_expression (c_parser *parser)
7334 {
7335   struct c_expr expr;
7336   struct c_expr result;
7337   location_t expr_loc;
7338   gcc_assert (c_parser_next_token_is_keyword (parser, RID_SIZEOF));
7339 
7340   location_t start;
7341   location_t finish = UNKNOWN_LOCATION;
7342 
7343   start = c_parser_peek_token (parser)->location;
7344 
7345   c_parser_consume_token (parser);
7346   c_inhibit_evaluation_warnings++;
7347   in_sizeof++;
7348   if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
7349       && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
7350     {
7351       /* Either sizeof ( type-name ) or sizeof unary-expression
7352 	 starting with a compound literal.  */
7353       struct c_type_name *type_name;
7354       matching_parens parens;
7355       parens.consume_open (parser);
7356       expr_loc = c_parser_peek_token (parser)->location;
7357       type_name = c_parser_type_name (parser, true);
7358       parens.skip_until_found_close (parser);
7359       finish = parser->tokens_buf[0].location;
7360       if (type_name == NULL)
7361 	{
7362 	  struct c_expr ret;
7363 	  c_inhibit_evaluation_warnings--;
7364 	  in_sizeof--;
7365 	  ret.set_error ();
7366 	  ret.original_code = ERROR_MARK;
7367 	  ret.original_type = NULL;
7368 	  return ret;
7369 	}
7370       if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
7371 	{
7372 	  expr = c_parser_postfix_expression_after_paren_type (parser,
7373 							       type_name,
7374 							       expr_loc);
7375 	  finish = expr.get_finish ();
7376 	  goto sizeof_expr;
7377 	}
7378       /* sizeof ( type-name ).  */
7379       if (type_name->specs->alignas_p)
7380 	error_at (type_name->specs->locations[cdw_alignas],
7381 		  "alignment specified for type name in %<sizeof%>");
7382       c_inhibit_evaluation_warnings--;
7383       in_sizeof--;
7384       result = c_expr_sizeof_type (expr_loc, type_name);
7385     }
7386   else
7387     {
7388       expr_loc = c_parser_peek_token (parser)->location;
7389       expr = c_parser_unary_expression (parser);
7390       finish = expr.get_finish ();
7391     sizeof_expr:
7392       c_inhibit_evaluation_warnings--;
7393       in_sizeof--;
7394       mark_exp_read (expr.value);
7395       if (TREE_CODE (expr.value) == COMPONENT_REF
7396 	  && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1)))
7397 	error_at (expr_loc, "%<sizeof%> applied to a bit-field");
7398       result = c_expr_sizeof_expr (expr_loc, expr);
7399     }
7400   if (finish != UNKNOWN_LOCATION)
7401     set_c_expr_source_range (&result, start, finish);
7402   return result;
7403 }
7404 
7405 /* Parse an alignof expression.  */
7406 
7407 static struct c_expr
c_parser_alignof_expression(c_parser * parser)7408 c_parser_alignof_expression (c_parser *parser)
7409 {
7410   struct c_expr expr;
7411   location_t start_loc = c_parser_peek_token (parser)->location;
7412   location_t end_loc;
7413   tree alignof_spelling = c_parser_peek_token (parser)->value;
7414   gcc_assert (c_parser_next_token_is_keyword (parser, RID_ALIGNOF));
7415   bool is_c11_alignof = strcmp (IDENTIFIER_POINTER (alignof_spelling),
7416 				"_Alignof") == 0;
7417   /* A diagnostic is not required for the use of this identifier in
7418      the implementation namespace; only diagnose it for the C11
7419      spelling because of existing code using the other spellings.  */
7420   if (is_c11_alignof)
7421     {
7422       if (flag_isoc99)
7423 	pedwarn_c99 (start_loc, OPT_Wpedantic, "ISO C99 does not support %qE",
7424 		     alignof_spelling);
7425       else
7426 	pedwarn_c99 (start_loc, OPT_Wpedantic, "ISO C90 does not support %qE",
7427 		     alignof_spelling);
7428     }
7429   c_parser_consume_token (parser);
7430   c_inhibit_evaluation_warnings++;
7431   in_alignof++;
7432   if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
7433       && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
7434     {
7435       /* Either __alignof__ ( type-name ) or __alignof__
7436 	 unary-expression starting with a compound literal.  */
7437       location_t loc;
7438       struct c_type_name *type_name;
7439       struct c_expr ret;
7440       matching_parens parens;
7441       parens.consume_open (parser);
7442       loc = c_parser_peek_token (parser)->location;
7443       type_name = c_parser_type_name (parser, true);
7444       end_loc = c_parser_peek_token (parser)->location;
7445       parens.skip_until_found_close (parser);
7446       if (type_name == NULL)
7447 	{
7448 	  struct c_expr ret;
7449 	  c_inhibit_evaluation_warnings--;
7450 	  in_alignof--;
7451 	  ret.set_error ();
7452 	  ret.original_code = ERROR_MARK;
7453 	  ret.original_type = NULL;
7454 	  return ret;
7455 	}
7456       if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
7457 	{
7458 	  expr = c_parser_postfix_expression_after_paren_type (parser,
7459 							       type_name,
7460 							       loc);
7461 	  goto alignof_expr;
7462 	}
7463       /* alignof ( type-name ).  */
7464       if (type_name->specs->alignas_p)
7465 	error_at (type_name->specs->locations[cdw_alignas],
7466 		  "alignment specified for type name in %qE",
7467 		  alignof_spelling);
7468       c_inhibit_evaluation_warnings--;
7469       in_alignof--;
7470       ret.value = c_sizeof_or_alignof_type (loc, groktypename (type_name,
7471 							       NULL, NULL),
7472 					    false, is_c11_alignof, 1);
7473       ret.original_code = ERROR_MARK;
7474       ret.original_type = NULL;
7475       set_c_expr_source_range (&ret, start_loc, end_loc);
7476       return ret;
7477     }
7478   else
7479     {
7480       struct c_expr ret;
7481       expr = c_parser_unary_expression (parser);
7482       end_loc = expr.src_range.m_finish;
7483     alignof_expr:
7484       mark_exp_read (expr.value);
7485       c_inhibit_evaluation_warnings--;
7486       in_alignof--;
7487       if (is_c11_alignof)
7488 	pedwarn (start_loc,
7489 		 OPT_Wpedantic, "ISO C does not allow %<%E (expression)%>",
7490 		 alignof_spelling);
7491       ret.value = c_alignof_expr (start_loc, expr.value);
7492       ret.original_code = ERROR_MARK;
7493       ret.original_type = NULL;
7494       set_c_expr_source_range (&ret, start_loc, end_loc);
7495       return ret;
7496     }
7497 }
7498 
7499 /* Helper function to read arguments of builtins which are interfaces
7500    for the middle-end nodes like COMPLEX_EXPR, VEC_PERM_EXPR and
7501    others.  The name of the builtin is passed using BNAME parameter.
7502    Function returns true if there were no errors while parsing and
7503    stores the arguments in CEXPR_LIST.  If it returns true,
7504    *OUT_CLOSE_PAREN_LOC is written to with the location of the closing
7505    parenthesis.  */
7506 static bool
c_parser_get_builtin_args(c_parser * parser,const char * bname,vec<c_expr_t,va_gc> ** ret_cexpr_list,bool choose_expr_p,location_t * out_close_paren_loc)7507 c_parser_get_builtin_args (c_parser *parser, const char *bname,
7508 			   vec<c_expr_t, va_gc> **ret_cexpr_list,
7509 			   bool choose_expr_p,
7510 			   location_t *out_close_paren_loc)
7511 {
7512   location_t loc = c_parser_peek_token (parser)->location;
7513   vec<c_expr_t, va_gc> *cexpr_list;
7514   c_expr_t expr;
7515   bool saved_force_folding_builtin_constant_p;
7516 
7517   *ret_cexpr_list = NULL;
7518   if (c_parser_next_token_is_not (parser, CPP_OPEN_PAREN))
7519     {
7520       error_at (loc, "cannot take address of %qs", bname);
7521       return false;
7522     }
7523 
7524   c_parser_consume_token (parser);
7525 
7526   if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
7527     {
7528       *out_close_paren_loc = c_parser_peek_token (parser)->location;
7529       c_parser_consume_token (parser);
7530       return true;
7531     }
7532 
7533   saved_force_folding_builtin_constant_p
7534     = force_folding_builtin_constant_p;
7535   force_folding_builtin_constant_p |= choose_expr_p;
7536   expr = c_parser_expr_no_commas (parser, NULL);
7537   force_folding_builtin_constant_p
7538     = saved_force_folding_builtin_constant_p;
7539   vec_alloc (cexpr_list, 1);
7540   vec_safe_push (cexpr_list, expr);
7541   while (c_parser_next_token_is (parser, CPP_COMMA))
7542     {
7543       c_parser_consume_token (parser);
7544       expr = c_parser_expr_no_commas (parser, NULL);
7545       vec_safe_push (cexpr_list, expr);
7546     }
7547 
7548   *out_close_paren_loc = c_parser_peek_token (parser)->location;
7549   if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
7550     return false;
7551 
7552   *ret_cexpr_list = cexpr_list;
7553   return true;
7554 }
7555 
7556 /* This represents a single generic-association.  */
7557 
7558 struct c_generic_association
7559 {
7560   /* The location of the starting token of the type.  */
7561   location_t type_location;
7562   /* The association's type, or NULL_TREE for 'default'.  */
7563   tree type;
7564   /* The association's expression.  */
7565   struct c_expr expression;
7566 };
7567 
7568 /* Parse a generic-selection.  (C11 6.5.1.1).
7569 
7570    generic-selection:
7571      _Generic ( assignment-expression , generic-assoc-list )
7572 
7573    generic-assoc-list:
7574      generic-association
7575      generic-assoc-list , generic-association
7576 
7577    generic-association:
7578      type-name : assignment-expression
7579      default : assignment-expression
7580 */
7581 
7582 static struct c_expr
c_parser_generic_selection(c_parser * parser)7583 c_parser_generic_selection (c_parser *parser)
7584 {
7585   struct c_expr selector, error_expr;
7586   tree selector_type;
7587   struct c_generic_association matched_assoc;
7588   bool match_found = false;
7589   location_t generic_loc, selector_loc;
7590 
7591   error_expr.original_code = ERROR_MARK;
7592   error_expr.original_type = NULL;
7593   error_expr.set_error ();
7594   matched_assoc.type_location = UNKNOWN_LOCATION;
7595   matched_assoc.type = NULL_TREE;
7596   matched_assoc.expression = error_expr;
7597 
7598   gcc_assert (c_parser_next_token_is_keyword (parser, RID_GENERIC));
7599   generic_loc = c_parser_peek_token (parser)->location;
7600   c_parser_consume_token (parser);
7601   if (flag_isoc99)
7602     pedwarn_c99 (generic_loc, OPT_Wpedantic,
7603 		 "ISO C99 does not support %<_Generic%>");
7604   else
7605     pedwarn_c99 (generic_loc, OPT_Wpedantic,
7606 		 "ISO C90 does not support %<_Generic%>");
7607 
7608   matching_parens parens;
7609   if (!parens.require_open (parser))
7610     return error_expr;
7611 
7612   c_inhibit_evaluation_warnings++;
7613   selector_loc = c_parser_peek_token (parser)->location;
7614   selector = c_parser_expr_no_commas (parser, NULL);
7615   selector = default_function_array_conversion (selector_loc, selector);
7616   c_inhibit_evaluation_warnings--;
7617 
7618   if (selector.value == error_mark_node)
7619     {
7620       c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7621       return selector;
7622     }
7623   selector_type = TREE_TYPE (selector.value);
7624   /* In ISO C terms, rvalues (including the controlling expression of
7625      _Generic) do not have qualified types.  */
7626   if (TREE_CODE (selector_type) != ARRAY_TYPE)
7627     selector_type = TYPE_MAIN_VARIANT (selector_type);
7628   /* In ISO C terms, _Noreturn is not part of the type of expressions
7629      such as &abort, but in GCC it is represented internally as a type
7630      qualifier.  */
7631   if (FUNCTION_POINTER_TYPE_P (selector_type)
7632       && TYPE_QUALS (TREE_TYPE (selector_type)) != TYPE_UNQUALIFIED)
7633     selector_type
7634       = build_pointer_type (TYPE_MAIN_VARIANT (TREE_TYPE (selector_type)));
7635 
7636   if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
7637     {
7638       c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7639       return error_expr;
7640     }
7641 
7642   auto_vec<c_generic_association> associations;
7643   while (1)
7644     {
7645       struct c_generic_association assoc, *iter;
7646       unsigned int ix;
7647       c_token *token = c_parser_peek_token (parser);
7648 
7649       assoc.type_location = token->location;
7650       if (token->type == CPP_KEYWORD && token->keyword == RID_DEFAULT)
7651 	{
7652 	  c_parser_consume_token (parser);
7653 	  assoc.type = NULL_TREE;
7654 	}
7655       else
7656 	{
7657 	  struct c_type_name *type_name;
7658 
7659 	  type_name = c_parser_type_name (parser);
7660 	  if (type_name == NULL)
7661 	    {
7662 	      c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7663 	      return error_expr;
7664 	    }
7665 	  assoc.type = groktypename (type_name, NULL, NULL);
7666 	  if (assoc.type == error_mark_node)
7667 	    {
7668 	      c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7669 	      return error_expr;
7670 	    }
7671 
7672 	  if (TREE_CODE (assoc.type) == FUNCTION_TYPE)
7673 	    error_at (assoc.type_location,
7674 		      "%<_Generic%> association has function type");
7675 	  else if (!COMPLETE_TYPE_P (assoc.type))
7676 	    error_at (assoc.type_location,
7677 		      "%<_Generic%> association has incomplete type");
7678 
7679 	  if (variably_modified_type_p (assoc.type, NULL_TREE))
7680 	    error_at (assoc.type_location,
7681 		      "%<_Generic%> association has "
7682 		      "variable length type");
7683 	}
7684 
7685       if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
7686 	{
7687 	  c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7688 	  return error_expr;
7689 	}
7690 
7691       assoc.expression = c_parser_expr_no_commas (parser, NULL);
7692       if (assoc.expression.value == error_mark_node)
7693 	{
7694 	  c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7695 	  return error_expr;
7696 	}
7697 
7698       for (ix = 0; associations.iterate (ix, &iter); ++ix)
7699 	{
7700 	  if (assoc.type == NULL_TREE)
7701 	    {
7702 	      if (iter->type == NULL_TREE)
7703 		{
7704 		  error_at (assoc.type_location,
7705 			    "duplicate %<default%> case in %<_Generic%>");
7706 		  inform (iter->type_location, "original %<default%> is here");
7707 		}
7708 	    }
7709 	  else if (iter->type != NULL_TREE)
7710 	    {
7711 	      if (comptypes (assoc.type, iter->type))
7712 		{
7713 		  error_at (assoc.type_location,
7714 			    "%<_Generic%> specifies two compatible types");
7715 		  inform (iter->type_location, "compatible type is here");
7716 		}
7717 	    }
7718 	}
7719 
7720       if (assoc.type == NULL_TREE)
7721 	{
7722 	  if (!match_found)
7723 	    {
7724 	      matched_assoc = assoc;
7725 	      match_found = true;
7726 	    }
7727 	}
7728       else if (comptypes (assoc.type, selector_type))
7729 	{
7730 	  if (!match_found || matched_assoc.type == NULL_TREE)
7731 	    {
7732 	      matched_assoc = assoc;
7733 	      match_found = true;
7734 	    }
7735 	  else
7736 	    {
7737 	      error_at (assoc.type_location,
7738 			"%<_Generic%> selector matches multiple associations");
7739 	      inform (matched_assoc.type_location,
7740 		      "other match is here");
7741 	    }
7742 	}
7743 
7744       associations.safe_push (assoc);
7745 
7746       if (c_parser_peek_token (parser)->type != CPP_COMMA)
7747 	break;
7748       c_parser_consume_token (parser);
7749     }
7750 
7751   if (!parens.require_close (parser))
7752     {
7753       c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7754       return error_expr;
7755     }
7756 
7757   if (!match_found)
7758     {
7759       error_at (selector_loc, "%<_Generic%> selector of type %qT is not "
7760 		"compatible with any association",
7761 		selector_type);
7762       return error_expr;
7763     }
7764 
7765   return matched_assoc.expression;
7766 }
7767 
7768 /* Check the validity of a function pointer argument *EXPR (argument
7769    position POS) to __builtin_tgmath.  Return the number of function
7770    arguments if possibly valid; return 0 having reported an error if
7771    not valid.  */
7772 
7773 static unsigned int
check_tgmath_function(c_expr * expr,unsigned int pos)7774 check_tgmath_function (c_expr *expr, unsigned int pos)
7775 {
7776   tree type = TREE_TYPE (expr->value);
7777   if (!FUNCTION_POINTER_TYPE_P (type))
7778     {
7779       error_at (expr->get_location (),
7780 		"argument %u of %<__builtin_tgmath%> is not a function pointer",
7781 		pos);
7782       return 0;
7783     }
7784   type = TREE_TYPE (type);
7785   if (!prototype_p (type))
7786     {
7787       error_at (expr->get_location (),
7788 		"argument %u of %<__builtin_tgmath%> is unprototyped", pos);
7789       return 0;
7790     }
7791   if (stdarg_p (type))
7792     {
7793       error_at (expr->get_location (),
7794 		"argument %u of %<__builtin_tgmath%> has variable arguments",
7795 		pos);
7796       return 0;
7797     }
7798   unsigned int nargs = 0;
7799   function_args_iterator iter;
7800   tree t;
7801   FOREACH_FUNCTION_ARGS (type, t, iter)
7802     {
7803       if (t == void_type_node)
7804 	break;
7805       nargs++;
7806     }
7807   if (nargs == 0)
7808     {
7809       error_at (expr->get_location (),
7810 		"argument %u of %<__builtin_tgmath%> has no arguments", pos);
7811       return 0;
7812     }
7813   return nargs;
7814 }
7815 
7816 /* Ways in which a parameter or return value of a type-generic macro
7817    may vary between the different functions the macro may call.  */
7818 enum tgmath_parm_kind
7819   {
7820     tgmath_fixed, tgmath_real, tgmath_complex
7821   };
7822 
7823 /* Parse a postfix expression (C90 6.3.1-6.3.2, C99 6.5.1-6.5.2,
7824    C11 6.5.1-6.5.2).  Compound literals aren't handled here; callers have to
7825    call c_parser_postfix_expression_after_paren_type on encountering them.
7826 
7827    postfix-expression:
7828      primary-expression
7829      postfix-expression [ expression ]
7830      postfix-expression ( argument-expression-list[opt] )
7831      postfix-expression . identifier
7832      postfix-expression -> identifier
7833      postfix-expression ++
7834      postfix-expression --
7835      ( type-name ) { initializer-list }
7836      ( type-name ) { initializer-list , }
7837 
7838    argument-expression-list:
7839      argument-expression
7840      argument-expression-list , argument-expression
7841 
7842    primary-expression:
7843      identifier
7844      constant
7845      string-literal
7846      ( expression )
7847      generic-selection
7848 
7849    GNU extensions:
7850 
7851    primary-expression:
7852      __func__
7853        (treated as a keyword in GNU C)
7854      __FUNCTION__
7855      __PRETTY_FUNCTION__
7856      ( compound-statement )
7857      __builtin_va_arg ( assignment-expression , type-name )
7858      __builtin_offsetof ( type-name , offsetof-member-designator )
7859      __builtin_choose_expr ( assignment-expression ,
7860 			     assignment-expression ,
7861 			     assignment-expression )
7862      __builtin_types_compatible_p ( type-name , type-name )
7863      __builtin_tgmath ( expr-list )
7864      __builtin_complex ( assignment-expression , assignment-expression )
7865      __builtin_shuffle ( assignment-expression , assignment-expression )
7866      __builtin_shuffle ( assignment-expression ,
7867 			 assignment-expression ,
7868 			 assignment-expression, )
7869 
7870    offsetof-member-designator:
7871      identifier
7872      offsetof-member-designator . identifier
7873      offsetof-member-designator [ expression ]
7874 
7875    Objective-C:
7876 
7877    primary-expression:
7878      [ objc-receiver objc-message-args ]
7879      @selector ( objc-selector-arg )
7880      @protocol ( identifier )
7881      @encode ( type-name )
7882      objc-string-literal
7883      Classname . identifier
7884 */
7885 
7886 static struct c_expr
c_parser_postfix_expression(c_parser * parser)7887 c_parser_postfix_expression (c_parser *parser)
7888 {
7889   struct c_expr expr, e1;
7890   struct c_type_name *t1, *t2;
7891   location_t loc = c_parser_peek_token (parser)->location;
7892   source_range tok_range = c_parser_peek_token (parser)->get_range ();
7893   expr.original_code = ERROR_MARK;
7894   expr.original_type = NULL;
7895   switch (c_parser_peek_token (parser)->type)
7896     {
7897     case CPP_NUMBER:
7898       expr.value = c_parser_peek_token (parser)->value;
7899       set_c_expr_source_range (&expr, tok_range);
7900       loc = c_parser_peek_token (parser)->location;
7901       c_parser_consume_token (parser);
7902       if (TREE_CODE (expr.value) == FIXED_CST
7903 	  && !targetm.fixed_point_supported_p ())
7904 	{
7905 	  error_at (loc, "fixed-point types not supported for this target");
7906 	  expr.set_error ();
7907 	}
7908       break;
7909     case CPP_CHAR:
7910     case CPP_CHAR16:
7911     case CPP_CHAR32:
7912     case CPP_WCHAR:
7913       expr.value = c_parser_peek_token (parser)->value;
7914       /* For the purpose of warning when a pointer is compared with
7915 	 a zero character constant.  */
7916       expr.original_type = char_type_node;
7917       set_c_expr_source_range (&expr, tok_range);
7918       c_parser_consume_token (parser);
7919       break;
7920     case CPP_STRING:
7921     case CPP_STRING16:
7922     case CPP_STRING32:
7923     case CPP_WSTRING:
7924     case CPP_UTF8STRING:
7925       expr.value = c_parser_peek_token (parser)->value;
7926       set_c_expr_source_range (&expr, tok_range);
7927       expr.original_code = STRING_CST;
7928       c_parser_consume_token (parser);
7929       break;
7930     case CPP_OBJC_STRING:
7931       gcc_assert (c_dialect_objc ());
7932       expr.value
7933 	= objc_build_string_object (c_parser_peek_token (parser)->value);
7934       set_c_expr_source_range (&expr, tok_range);
7935       c_parser_consume_token (parser);
7936       break;
7937     case CPP_NAME:
7938       switch (c_parser_peek_token (parser)->id_kind)
7939 	{
7940 	case C_ID_ID:
7941 	  {
7942 	    tree id = c_parser_peek_token (parser)->value;
7943 	    c_parser_consume_token (parser);
7944 	    expr.value = build_external_ref (loc, id,
7945 					     (c_parser_peek_token (parser)->type
7946 					      == CPP_OPEN_PAREN),
7947 					     &expr.original_type);
7948 	    set_c_expr_source_range (&expr, tok_range);
7949 	    break;
7950 	  }
7951 	case C_ID_CLASSNAME:
7952 	  {
7953 	    /* Here we parse the Objective-C 2.0 Class.name dot
7954 	       syntax.  */
7955 	    tree class_name = c_parser_peek_token (parser)->value;
7956 	    tree component;
7957 	    c_parser_consume_token (parser);
7958 	    gcc_assert (c_dialect_objc ());
7959 	    if (!c_parser_require (parser, CPP_DOT, "expected %<.%>"))
7960 	      {
7961 		expr.set_error ();
7962 		break;
7963 	      }
7964 	    if (c_parser_next_token_is_not (parser, CPP_NAME))
7965 	      {
7966 		c_parser_error (parser, "expected identifier");
7967 		expr.set_error ();
7968 		break;
7969 	      }
7970 	    c_token *component_tok = c_parser_peek_token (parser);
7971 	    component = component_tok->value;
7972 	    location_t end_loc = component_tok->get_finish ();
7973 	    c_parser_consume_token (parser);
7974 	    expr.value = objc_build_class_component_ref (class_name,
7975 							 component);
7976 	    set_c_expr_source_range (&expr, loc, end_loc);
7977 	    break;
7978 	  }
7979 	default:
7980 	  c_parser_error (parser, "expected expression");
7981 	  expr.set_error ();
7982 	  break;
7983 	}
7984       break;
7985     case CPP_OPEN_PAREN:
7986       /* A parenthesized expression, statement expression or compound
7987 	 literal.  */
7988       if (c_parser_peek_2nd_token (parser)->type == CPP_OPEN_BRACE)
7989 	{
7990 	  /* A statement expression.  */
7991 	  tree stmt;
7992 	  location_t brace_loc;
7993 	  c_parser_consume_token (parser);
7994 	  brace_loc = c_parser_peek_token (parser)->location;
7995 	  c_parser_consume_token (parser);
7996 	  if (!building_stmt_list_p ())
7997 	    {
7998 	      error_at (loc, "braced-group within expression allowed "
7999 			"only inside a function");
8000 	      parser->error = true;
8001 	      c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
8002 	      c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
8003 	      expr.set_error ();
8004 	      break;
8005 	    }
8006 	  stmt = c_begin_stmt_expr ();
8007 	  c_parser_compound_statement_nostart (parser);
8008 	  location_t close_loc = c_parser_peek_token (parser)->location;
8009 	  c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
8010 				     "expected %<)%>");
8011 	  pedwarn (loc, OPT_Wpedantic,
8012 		   "ISO C forbids braced-groups within expressions");
8013 	  expr.value = c_finish_stmt_expr (brace_loc, stmt);
8014 	  set_c_expr_source_range (&expr, loc, close_loc);
8015 	  mark_exp_read (expr.value);
8016 	}
8017       else
8018 	{
8019 	  /* A parenthesized expression.  */
8020 	  location_t loc_open_paren = c_parser_peek_token (parser)->location;
8021 	  c_parser_consume_token (parser);
8022 	  expr = c_parser_expression (parser);
8023 	  if (TREE_CODE (expr.value) == MODIFY_EXPR)
8024 	    TREE_NO_WARNING (expr.value) = 1;
8025 	  if (expr.original_code != C_MAYBE_CONST_EXPR
8026 	      && expr.original_code != SIZEOF_EXPR)
8027 	    expr.original_code = ERROR_MARK;
8028 	  /* Don't change EXPR.ORIGINAL_TYPE.  */
8029 	  location_t loc_close_paren = c_parser_peek_token (parser)->location;
8030 	  set_c_expr_source_range (&expr, loc_open_paren, loc_close_paren);
8031 	  c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
8032 				     "expected %<)%>", loc_open_paren);
8033 	}
8034       break;
8035     case CPP_KEYWORD:
8036       switch (c_parser_peek_token (parser)->keyword)
8037 	{
8038 	case RID_FUNCTION_NAME:
8039 	  pedwarn (loc, OPT_Wpedantic,  "ISO C does not support "
8040 		   "%<__FUNCTION__%> predefined identifier");
8041 	  expr.value = fname_decl (loc,
8042 				   c_parser_peek_token (parser)->keyword,
8043 				   c_parser_peek_token (parser)->value);
8044 	  set_c_expr_source_range (&expr, loc, loc);
8045 	  c_parser_consume_token (parser);
8046 	  break;
8047 	case RID_PRETTY_FUNCTION_NAME:
8048 	  pedwarn (loc, OPT_Wpedantic,  "ISO C does not support "
8049 		   "%<__PRETTY_FUNCTION__%> predefined identifier");
8050 	  expr.value = fname_decl (loc,
8051 				   c_parser_peek_token (parser)->keyword,
8052 				   c_parser_peek_token (parser)->value);
8053 	  set_c_expr_source_range (&expr, loc, loc);
8054 	  c_parser_consume_token (parser);
8055 	  break;
8056 	case RID_C99_FUNCTION_NAME:
8057 	  pedwarn_c90 (loc, OPT_Wpedantic,  "ISO C90 does not support "
8058 		   "%<__func__%> predefined identifier");
8059 	  expr.value = fname_decl (loc,
8060 				   c_parser_peek_token (parser)->keyword,
8061 				   c_parser_peek_token (parser)->value);
8062 	  set_c_expr_source_range (&expr, loc, loc);
8063 	  c_parser_consume_token (parser);
8064 	  break;
8065 	case RID_VA_ARG:
8066 	  {
8067 	    location_t start_loc = loc;
8068 	    c_parser_consume_token (parser);
8069 	    matching_parens parens;
8070 	    if (!parens.require_open (parser))
8071 	      {
8072 		expr.set_error ();
8073 		break;
8074 	      }
8075 	    e1 = c_parser_expr_no_commas (parser, NULL);
8076 	    mark_exp_read (e1.value);
8077 	    e1.value = c_fully_fold (e1.value, false, NULL);
8078 	    if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
8079 	      {
8080 		c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
8081 		expr.set_error ();
8082 		break;
8083 	      }
8084 	    loc = c_parser_peek_token (parser)->location;
8085 	    t1 = c_parser_type_name (parser);
8086 	    location_t end_loc = c_parser_peek_token (parser)->get_finish ();
8087 	    c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
8088 				       "expected %<)%>");
8089 	    if (t1 == NULL)
8090 	      {
8091 		expr.set_error ();
8092 	      }
8093 	    else
8094 	      {
8095 		tree type_expr = NULL_TREE;
8096 		expr.value = c_build_va_arg (start_loc, e1.value, loc,
8097 					     groktypename (t1, &type_expr, NULL));
8098 		if (type_expr)
8099 		  {
8100 		    expr.value = build2 (C_MAYBE_CONST_EXPR,
8101 					 TREE_TYPE (expr.value), type_expr,
8102 					 expr.value);
8103 		    C_MAYBE_CONST_EXPR_NON_CONST (expr.value) = true;
8104 		  }
8105 		set_c_expr_source_range (&expr, start_loc, end_loc);
8106 	      }
8107 	  }
8108 	  break;
8109 	case RID_OFFSETOF:
8110 	  {
8111 	    c_parser_consume_token (parser);
8112 	    matching_parens parens;
8113 	    if (!parens.require_open (parser))
8114 	      {
8115 		expr.set_error ();
8116 		break;
8117 	      }
8118 	    t1 = c_parser_type_name (parser);
8119 	    if (t1 == NULL)
8120 	      parser->error = true;
8121 	    if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
8122 	      gcc_assert (parser->error);
8123 	    if (parser->error)
8124 	      {
8125 		c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
8126 		expr.set_error ();
8127 		break;
8128 	      }
8129 	    tree type = groktypename (t1, NULL, NULL);
8130 	    tree offsetof_ref;
8131 	    if (type == error_mark_node)
8132 	      offsetof_ref = error_mark_node;
8133 	    else
8134 	      {
8135 		offsetof_ref = build1 (INDIRECT_REF, type, null_pointer_node);
8136 		SET_EXPR_LOCATION (offsetof_ref, loc);
8137 	      }
8138 	    /* Parse the second argument to __builtin_offsetof.  We
8139 	       must have one identifier, and beyond that we want to
8140 	       accept sub structure and sub array references.  */
8141 	    if (c_parser_next_token_is (parser, CPP_NAME))
8142 	      {
8143 		c_token *comp_tok = c_parser_peek_token (parser);
8144 		offsetof_ref = build_component_ref
8145 		  (loc, offsetof_ref, comp_tok->value, comp_tok->location);
8146 		c_parser_consume_token (parser);
8147 		while (c_parser_next_token_is (parser, CPP_DOT)
8148 		       || c_parser_next_token_is (parser,
8149 						  CPP_OPEN_SQUARE)
8150 		       || c_parser_next_token_is (parser,
8151 						  CPP_DEREF))
8152 		  {
8153 		    if (c_parser_next_token_is (parser, CPP_DEREF))
8154 		      {
8155 			loc = c_parser_peek_token (parser)->location;
8156 			offsetof_ref = build_array_ref (loc,
8157 							offsetof_ref,
8158 							integer_zero_node);
8159 			goto do_dot;
8160 		      }
8161 		    else if (c_parser_next_token_is (parser, CPP_DOT))
8162 		      {
8163 		      do_dot:
8164 			c_parser_consume_token (parser);
8165 			if (c_parser_next_token_is_not (parser,
8166 							CPP_NAME))
8167 			  {
8168 			    c_parser_error (parser, "expected identifier");
8169 			    break;
8170 			  }
8171 			c_token *comp_tok = c_parser_peek_token (parser);
8172 			offsetof_ref = build_component_ref
8173 			  (loc, offsetof_ref, comp_tok->value,
8174 			   comp_tok->location);
8175 			c_parser_consume_token (parser);
8176 		      }
8177 		    else
8178 		      {
8179 			struct c_expr ce;
8180 			tree idx;
8181 			loc = c_parser_peek_token (parser)->location;
8182 			c_parser_consume_token (parser);
8183 			ce = c_parser_expression (parser);
8184 			ce = convert_lvalue_to_rvalue (loc, ce, false, false);
8185 			idx = ce.value;
8186 			idx = c_fully_fold (idx, false, NULL);
8187 			c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
8188 						   "expected %<]%>");
8189 			offsetof_ref = build_array_ref (loc, offsetof_ref, idx);
8190 		      }
8191 		  }
8192 	      }
8193 	    else
8194 	      c_parser_error (parser, "expected identifier");
8195 	    location_t end_loc = c_parser_peek_token (parser)->get_finish ();
8196 	    c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
8197 				       "expected %<)%>");
8198 	    expr.value = fold_offsetof (offsetof_ref);
8199 	    set_c_expr_source_range (&expr, loc, end_loc);
8200 	  }
8201 	  break;
8202 	case RID_CHOOSE_EXPR:
8203 	  {
8204 	    vec<c_expr_t, va_gc> *cexpr_list;
8205 	    c_expr_t *e1_p, *e2_p, *e3_p;
8206 	    tree c;
8207 	    location_t close_paren_loc;
8208 
8209 	    c_parser_consume_token (parser);
8210 	    if (!c_parser_get_builtin_args (parser,
8211 					    "__builtin_choose_expr",
8212 					    &cexpr_list, true,
8213 					    &close_paren_loc))
8214 	      {
8215 		expr.set_error ();
8216 		break;
8217 	      }
8218 
8219 	    if (vec_safe_length (cexpr_list) != 3)
8220 	      {
8221 		error_at (loc, "wrong number of arguments to "
8222 			       "%<__builtin_choose_expr%>");
8223 		expr.set_error ();
8224 		break;
8225 	      }
8226 
8227 	    e1_p = &(*cexpr_list)[0];
8228 	    e2_p = &(*cexpr_list)[1];
8229 	    e3_p = &(*cexpr_list)[2];
8230 
8231 	    c = e1_p->value;
8232 	    mark_exp_read (e2_p->value);
8233 	    mark_exp_read (e3_p->value);
8234 	    if (TREE_CODE (c) != INTEGER_CST
8235 		|| !INTEGRAL_TYPE_P (TREE_TYPE (c)))
8236 	      error_at (loc,
8237 			"first argument to %<__builtin_choose_expr%> not"
8238 			" a constant");
8239 	    constant_expression_warning (c);
8240 	    expr = integer_zerop (c) ? *e3_p : *e2_p;
8241 	    set_c_expr_source_range (&expr, loc, close_paren_loc);
8242 	    break;
8243 	  }
8244 	case RID_TYPES_COMPATIBLE_P:
8245 	  {
8246 	    c_parser_consume_token (parser);
8247 	    matching_parens parens;
8248 	    if (!parens.require_open (parser))
8249 	      {
8250 		expr.set_error ();
8251 		break;
8252 	      }
8253 	    t1 = c_parser_type_name (parser);
8254 	    if (t1 == NULL)
8255 	      {
8256 		expr.set_error ();
8257 		break;
8258 	      }
8259 	    if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
8260 	      {
8261 		c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
8262 		expr.set_error ();
8263 		break;
8264 	      }
8265 	    t2 = c_parser_type_name (parser);
8266 	    if (t2 == NULL)
8267 	      {
8268 		expr.set_error ();
8269 		break;
8270 	      }
8271 	    location_t close_paren_loc = c_parser_peek_token (parser)->location;
8272 	    parens.skip_until_found_close (parser);
8273 	    tree e1, e2;
8274 	    e1 = groktypename (t1, NULL, NULL);
8275 	    e2 = groktypename (t2, NULL, NULL);
8276 	    if (e1 == error_mark_node || e2 == error_mark_node)
8277 	      {
8278 		expr.set_error ();
8279 		break;
8280 	      }
8281 
8282 	    e1 = TYPE_MAIN_VARIANT (e1);
8283 	    e2 = TYPE_MAIN_VARIANT (e2);
8284 
8285 	    expr.value
8286 	      = comptypes (e1, e2) ? integer_one_node : integer_zero_node;
8287 	    set_c_expr_source_range (&expr, loc, close_paren_loc);
8288 	  }
8289 	  break;
8290 	case RID_BUILTIN_TGMATH:
8291 	  {
8292 	    vec<c_expr_t, va_gc> *cexpr_list;
8293 	    location_t close_paren_loc;
8294 
8295 	    c_parser_consume_token (parser);
8296 	    if (!c_parser_get_builtin_args (parser,
8297 					    "__builtin_tgmath",
8298 					    &cexpr_list, false,
8299 					    &close_paren_loc))
8300 	      {
8301 		expr.set_error ();
8302 		break;
8303 	      }
8304 
8305 	    if (vec_safe_length (cexpr_list) < 3)
8306 	      {
8307 		error_at (loc, "too few arguments to %<__builtin_tgmath%>");
8308 		expr.set_error ();
8309 		break;
8310 	      }
8311 
8312 	    unsigned int i;
8313 	    c_expr_t *p;
8314 	    FOR_EACH_VEC_ELT (*cexpr_list, i, p)
8315 	      *p = convert_lvalue_to_rvalue (loc, *p, true, true);
8316 	    unsigned int nargs = check_tgmath_function (&(*cexpr_list)[0], 1);
8317 	    if (nargs == 0)
8318 	      {
8319 		expr.set_error ();
8320 		break;
8321 	      }
8322 	    if (vec_safe_length (cexpr_list) < nargs)
8323 	      {
8324 		error_at (loc, "too few arguments to %<__builtin_tgmath%>");
8325 		expr.set_error ();
8326 		break;
8327 	      }
8328 	    unsigned int num_functions = vec_safe_length (cexpr_list) - nargs;
8329 	    if (num_functions < 2)
8330 	      {
8331 		error_at (loc, "too few arguments to %<__builtin_tgmath%>");
8332 		expr.set_error ();
8333 		break;
8334 	      }
8335 
8336 	    /* The first NUM_FUNCTIONS expressions are the function
8337 	       pointers.  The remaining NARGS expressions are the
8338 	       arguments that are to be passed to one of those
8339 	       functions, chosen following <tgmath.h> rules.  */
8340 	    for (unsigned int j = 1; j < num_functions; j++)
8341 	      {
8342 		unsigned int this_nargs
8343 		  = check_tgmath_function (&(*cexpr_list)[j], j + 1);
8344 		if (this_nargs == 0)
8345 		  {
8346 		    expr.set_error ();
8347 		    goto out;
8348 		  }
8349 		if (this_nargs != nargs)
8350 		  {
8351 		    error_at ((*cexpr_list)[j].get_location (),
8352 			      "argument %u of %<__builtin_tgmath%> has "
8353 			      "wrong number of arguments", j + 1);
8354 		    expr.set_error ();
8355 		    goto out;
8356 		  }
8357 	      }
8358 
8359 	    /* The functions all have the same number of arguments.
8360 	       Determine whether arguments and return types vary in
8361 	       ways permitted for <tgmath.h> functions.  */
8362 	    /* The first entry in each of these vectors is for the
8363 	       return type, subsequent entries for parameter
8364 	       types.  */
8365 	    auto_vec<enum tgmath_parm_kind> parm_kind (nargs + 1);
8366 	    auto_vec<tree> parm_first (nargs + 1);
8367 	    auto_vec<bool> parm_complex (nargs + 1);
8368 	    auto_vec<bool> parm_varies (nargs + 1);
8369 	    tree first_type = TREE_TYPE (TREE_TYPE ((*cexpr_list)[0].value));
8370 	    tree first_ret = TYPE_MAIN_VARIANT (TREE_TYPE (first_type));
8371 	    parm_first.quick_push (first_ret);
8372 	    parm_complex.quick_push (TREE_CODE (first_ret) == COMPLEX_TYPE);
8373 	    parm_varies.quick_push (false);
8374 	    function_args_iterator iter;
8375 	    tree t;
8376 	    unsigned int argpos;
8377 	    FOREACH_FUNCTION_ARGS (first_type, t, iter)
8378 	      {
8379 		if (t == void_type_node)
8380 		  break;
8381 		parm_first.quick_push (TYPE_MAIN_VARIANT (t));
8382 		parm_complex.quick_push (TREE_CODE (t) == COMPLEX_TYPE);
8383 		parm_varies.quick_push (false);
8384 	      }
8385 	    for (unsigned int j = 1; j < num_functions; j++)
8386 	      {
8387 		tree type = TREE_TYPE (TREE_TYPE ((*cexpr_list)[j].value));
8388 		tree ret = TYPE_MAIN_VARIANT (TREE_TYPE (type));
8389 		if (ret != parm_first[0])
8390 		  {
8391 		    parm_varies[0] = true;
8392 		    if (!SCALAR_FLOAT_TYPE_P (parm_first[0])
8393 			&& !COMPLEX_FLOAT_TYPE_P (parm_first[0]))
8394 		      {
8395 			error_at ((*cexpr_list)[0].get_location (),
8396 				  "invalid type-generic return type for "
8397 				  "argument %u of %<__builtin_tgmath%>",
8398 				  1);
8399 			expr.set_error ();
8400 			goto out;
8401 		      }
8402 		    if (!SCALAR_FLOAT_TYPE_P (ret)
8403 			&& !COMPLEX_FLOAT_TYPE_P (ret))
8404 		      {
8405 			error_at ((*cexpr_list)[j].get_location (),
8406 				  "invalid type-generic return type for "
8407 				  "argument %u of %<__builtin_tgmath%>",
8408 				  j + 1);
8409 			expr.set_error ();
8410 			goto out;
8411 		      }
8412 		  }
8413 		if (TREE_CODE (ret) == COMPLEX_TYPE)
8414 		  parm_complex[0] = true;
8415 		argpos = 1;
8416 		FOREACH_FUNCTION_ARGS (type, t, iter)
8417 		  {
8418 		    if (t == void_type_node)
8419 		      break;
8420 		    t = TYPE_MAIN_VARIANT (t);
8421 		    if (t != parm_first[argpos])
8422 		      {
8423 			parm_varies[argpos] = true;
8424 			if (!SCALAR_FLOAT_TYPE_P (parm_first[argpos])
8425 			    && !COMPLEX_FLOAT_TYPE_P (parm_first[argpos]))
8426 			  {
8427 			    error_at ((*cexpr_list)[0].get_location (),
8428 				      "invalid type-generic type for "
8429 				      "argument %u of argument %u of "
8430 				      "%<__builtin_tgmath%>", argpos, 1);
8431 			    expr.set_error ();
8432 			    goto out;
8433 			  }
8434 			if (!SCALAR_FLOAT_TYPE_P (t)
8435 			    && !COMPLEX_FLOAT_TYPE_P (t))
8436 			  {
8437 			    error_at ((*cexpr_list)[j].get_location (),
8438 				      "invalid type-generic type for "
8439 				      "argument %u of argument %u of "
8440 				      "%<__builtin_tgmath%>", argpos, j + 1);
8441 			    expr.set_error ();
8442 			    goto out;
8443 			  }
8444 		      }
8445 		    if (TREE_CODE (t) == COMPLEX_TYPE)
8446 		      parm_complex[argpos] = true;
8447 		    argpos++;
8448 		  }
8449 	      }
8450 	    enum tgmath_parm_kind max_variation = tgmath_fixed;
8451 	    for (unsigned int j = 0; j <= nargs; j++)
8452 	      {
8453 		enum tgmath_parm_kind this_kind;
8454 		if (parm_varies[j])
8455 		  {
8456 		    if (parm_complex[j])
8457 		      max_variation = this_kind = tgmath_complex;
8458 		    else
8459 		      {
8460 			this_kind = tgmath_real;
8461 			if (max_variation != tgmath_complex)
8462 			  max_variation = tgmath_real;
8463 		      }
8464 		  }
8465 		else
8466 		  this_kind = tgmath_fixed;
8467 		parm_kind.quick_push (this_kind);
8468 	      }
8469 	    if (max_variation == tgmath_fixed)
8470 	      {
8471 		error_at (loc, "function arguments of %<__builtin_tgmath%> "
8472 			  "all have the same type");
8473 		expr.set_error ();
8474 		break;
8475 	      }
8476 
8477 	    /* Identify a parameter (not the return type) that varies,
8478 	       including with complex types if any variation includes
8479 	       complex types; there must be at least one such
8480 	       parameter.  */
8481 	    unsigned int tgarg = 0;
8482 	    for (unsigned int j = 1; j <= nargs; j++)
8483 	      if (parm_kind[j] == max_variation)
8484 		{
8485 		  tgarg = j;
8486 		  break;
8487 		}
8488 	    if (tgarg == 0)
8489 	      {
8490 		error_at (loc, "function arguments of %<__builtin_tgmath%> "
8491 			  "lack type-generic parameter");
8492 		expr.set_error ();
8493 		break;
8494 	      }
8495 
8496 	    /* Determine the type of the relevant parameter for each
8497 	       function.  */
8498 	    auto_vec<tree> tg_type (num_functions);
8499 	    for (unsigned int j = 0; j < num_functions; j++)
8500 	      {
8501 		tree type = TREE_TYPE (TREE_TYPE ((*cexpr_list)[j].value));
8502 		argpos = 1;
8503 		FOREACH_FUNCTION_ARGS (type, t, iter)
8504 		  {
8505 		    if (argpos == tgarg)
8506 		      {
8507 			tg_type.quick_push (TYPE_MAIN_VARIANT (t));
8508 			break;
8509 		      }
8510 		    argpos++;
8511 		  }
8512 	      }
8513 
8514 	    /* Verify that the corresponding types are different for
8515 	       all the listed functions.  Also determine whether all
8516 	       the types are complex, whether all the types are
8517 	       standard or binary, and whether all the types are
8518 	       decimal.  */
8519 	    bool all_complex = true;
8520 	    bool all_binary = true;
8521 	    bool all_decimal = true;
8522 	    hash_set<tree> tg_types;
8523 	    FOR_EACH_VEC_ELT (tg_type, i, t)
8524 	      {
8525 		if (TREE_CODE (t) == COMPLEX_TYPE)
8526 		  all_decimal = false;
8527 		else
8528 		  {
8529 		    all_complex = false;
8530 		    if (DECIMAL_FLOAT_TYPE_P (t))
8531 		      all_binary = false;
8532 		    else
8533 		      all_decimal = false;
8534 		  }
8535 		if (tg_types.add (t))
8536 		  {
8537 		    error_at ((*cexpr_list)[i].get_location (),
8538 			      "duplicate type-generic parameter type for "
8539 			      "function argument %u of %<__builtin_tgmath%>",
8540 			      i + 1);
8541 		    expr.set_error ();
8542 		    goto out;
8543 		  }
8544 	      }
8545 
8546 	    /* Verify that other parameters and the return type whose
8547 	       types vary have their types varying in the correct
8548 	       way.  */
8549 	    for (unsigned int j = 0; j < num_functions; j++)
8550 	      {
8551 		tree exp_type = tg_type[j];
8552 		tree exp_real_type = exp_type;
8553 		if (TREE_CODE (exp_type) == COMPLEX_TYPE)
8554 		  exp_real_type = TREE_TYPE (exp_type);
8555 		tree type = TREE_TYPE (TREE_TYPE ((*cexpr_list)[j].value));
8556 		tree ret = TYPE_MAIN_VARIANT (TREE_TYPE (type));
8557 		if ((parm_kind[0] == tgmath_complex && ret != exp_type)
8558 		    || (parm_kind[0] == tgmath_real && ret != exp_real_type))
8559 		  {
8560 		    error_at ((*cexpr_list)[j].get_location (),
8561 			      "bad return type for function argument %u "
8562 			      "of %<__builtin_tgmath%>", j + 1);
8563 		    expr.set_error ();
8564 		    goto out;
8565 		  }
8566 		argpos = 1;
8567 		FOREACH_FUNCTION_ARGS (type, t, iter)
8568 		  {
8569 		    if (t == void_type_node)
8570 		      break;
8571 		    t = TYPE_MAIN_VARIANT (t);
8572 		    if ((parm_kind[argpos] == tgmath_complex
8573 			 && t != exp_type)
8574 			|| (parm_kind[argpos] == tgmath_real
8575 			    && t != exp_real_type))
8576 		      {
8577 			error_at ((*cexpr_list)[j].get_location (),
8578 				  "bad type for argument %u of "
8579 				  "function argument %u of "
8580 				  "%<__builtin_tgmath%>", argpos, j + 1);
8581 			expr.set_error ();
8582 			goto out;
8583 		      }
8584 		    argpos++;
8585 		  }
8586 	      }
8587 
8588 	    /* The functions listed are a valid set of functions for a
8589 	       <tgmath.h> macro to select between.  Identify the
8590 	       matching function, if any.  First, the argument types
8591 	       must be combined following <tgmath.h> rules.  Integer
8592 	       types are treated as _Decimal64 if any type-generic
8593 	       argument is decimal, or if the only alternatives for
8594 	       type-generic arguments are of decimal types, and are
8595 	       otherwise treated as double (or _Complex double for
8596 	       complex integer types, or _Float64 or _Complex _Float64
8597 	       if all the return types are the same _FloatN or
8598 	       _FloatNx type).  After that adjustment, types are
8599 	       combined following the usual arithmetic conversions.
8600 	       If the function only accepts complex arguments, a
8601 	       complex type is produced.  */
8602 	    bool arg_complex = all_complex;
8603 	    bool arg_binary = all_binary;
8604 	    bool arg_int_decimal = all_decimal;
8605 	    for (unsigned int j = 1; j <= nargs; j++)
8606 	      {
8607 		if (parm_kind[j] == tgmath_fixed)
8608 		  continue;
8609 		c_expr_t *ce = &(*cexpr_list)[num_functions + j - 1];
8610 		tree type = TREE_TYPE (ce->value);
8611 		if (!INTEGRAL_TYPE_P (type)
8612 		    && !SCALAR_FLOAT_TYPE_P (type)
8613 		    && TREE_CODE (type) != COMPLEX_TYPE)
8614 		  {
8615 		    error_at (ce->get_location (),
8616 			      "invalid type of argument %u of type-generic "
8617 			      "function", j);
8618 		    expr.set_error ();
8619 		    goto out;
8620 		  }
8621 		if (DECIMAL_FLOAT_TYPE_P (type))
8622 		  {
8623 		    arg_int_decimal = true;
8624 		    if (all_complex)
8625 		      {
8626 			error_at (ce->get_location (),
8627 				  "decimal floating-point argument %u to "
8628 				  "complex-only type-generic function", j);
8629 			expr.set_error ();
8630 			goto out;
8631 		      }
8632 		    else if (all_binary)
8633 		      {
8634 			error_at (ce->get_location (),
8635 				  "decimal floating-point argument %u to "
8636 				  "binary-only type-generic function", j);
8637 			expr.set_error ();
8638 			goto out;
8639 		      }
8640 		    else if (arg_complex)
8641 		      {
8642 			error_at (ce->get_location (),
8643 				  "both complex and decimal floating-point "
8644 				  "arguments to type-generic function");
8645 			expr.set_error ();
8646 			goto out;
8647 		      }
8648 		    else if (arg_binary)
8649 		      {
8650 			error_at (ce->get_location (),
8651 				  "both binary and decimal floating-point "
8652 				  "arguments to type-generic function");
8653 			expr.set_error ();
8654 			goto out;
8655 		      }
8656 		  }
8657 		else if (TREE_CODE (type) == COMPLEX_TYPE)
8658 		  {
8659 		    arg_complex = true;
8660 		    if (COMPLEX_FLOAT_TYPE_P (type))
8661 		      arg_binary = true;
8662 		    if (all_decimal)
8663 		      {
8664 			error_at (ce->get_location (),
8665 				  "complex argument %u to "
8666 				  "decimal-only type-generic function", j);
8667 			expr.set_error ();
8668 			goto out;
8669 		      }
8670 		    else if (arg_int_decimal)
8671 		      {
8672 			error_at (ce->get_location (),
8673 				  "both complex and decimal floating-point "
8674 				  "arguments to type-generic function");
8675 			expr.set_error ();
8676 			goto out;
8677 		      }
8678 		  }
8679 		else if (SCALAR_FLOAT_TYPE_P (type))
8680 		  {
8681 		    arg_binary = true;
8682 		    if (all_decimal)
8683 		      {
8684 			error_at (ce->get_location (),
8685 				  "binary argument %u to "
8686 				  "decimal-only type-generic function", j);
8687 			expr.set_error ();
8688 			goto out;
8689 		      }
8690 		    else if (arg_int_decimal)
8691 		      {
8692 			error_at (ce->get_location (),
8693 				  "both binary and decimal floating-point "
8694 				  "arguments to type-generic function");
8695 			expr.set_error ();
8696 			goto out;
8697 		      }
8698 		  }
8699 	      }
8700 	    /* For a macro rounding its result to a narrower type, map
8701 	       integer types to _Float64 not double if the return type
8702 	       is a _FloatN or _FloatNx type.  */
8703 	    bool arg_int_float64 = false;
8704 	    if (parm_kind[0] == tgmath_fixed
8705 		&& SCALAR_FLOAT_TYPE_P (parm_first[0])
8706 		&& float64_type_node != NULL_TREE)
8707 	      for (unsigned int j = 0; j < NUM_FLOATN_NX_TYPES; j++)
8708 		if (parm_first[0] == FLOATN_TYPE_NODE (j))
8709 		  {
8710 		    arg_int_float64 = true;
8711 		    break;
8712 		  }
8713 	    tree arg_real = NULL_TREE;
8714 	    for (unsigned int j = 1; j <= nargs; j++)
8715 	      {
8716 		if (parm_kind[j] == tgmath_fixed)
8717 		  continue;
8718 		c_expr_t *ce = &(*cexpr_list)[num_functions + j - 1];
8719 		tree type = TYPE_MAIN_VARIANT (TREE_TYPE (ce->value));
8720 		if (TREE_CODE (type) == COMPLEX_TYPE)
8721 		  type = TREE_TYPE (type);
8722 		if (INTEGRAL_TYPE_P (type))
8723 		  type = (arg_int_decimal
8724 			  ? dfloat64_type_node
8725 			  : arg_int_float64
8726 			  ? float64_type_node
8727 			  : double_type_node);
8728 		if (arg_real == NULL_TREE)
8729 		  arg_real = type;
8730 		else
8731 		  arg_real = common_type (arg_real, type);
8732 		if (arg_real == error_mark_node)
8733 		  {
8734 		    expr.set_error ();
8735 		    goto out;
8736 		  }
8737 	      }
8738 	    tree arg_type = (arg_complex
8739 			     ? build_complex_type (arg_real)
8740 			     : arg_real);
8741 
8742 	    /* Look for a function to call with type-generic parameter
8743 	       type ARG_TYPE.  */
8744 	    c_expr_t *fn = NULL;
8745 	    for (unsigned int j = 0; j < num_functions; j++)
8746 	      {
8747 		if (tg_type[j] == arg_type)
8748 		  {
8749 		    fn = &(*cexpr_list)[j];
8750 		    break;
8751 		  }
8752 	      }
8753 	    if (fn == NULL
8754 		&& parm_kind[0] == tgmath_fixed
8755 		&& SCALAR_FLOAT_TYPE_P (parm_first[0]))
8756 	      {
8757 		/* Presume this is a macro that rounds its result to a
8758 		   narrower type, and look for the first function with
8759 		   at least the range and precision of the argument
8760 		   type.  */
8761 		for (unsigned int j = 0; j < num_functions; j++)
8762 		  {
8763 		    if (arg_complex
8764 			!= (TREE_CODE (tg_type[j]) == COMPLEX_TYPE))
8765 		      continue;
8766 		    tree real_tg_type = (arg_complex
8767 					 ? TREE_TYPE (tg_type[j])
8768 					 : tg_type[j]);
8769 		    if (DECIMAL_FLOAT_TYPE_P (arg_real)
8770 			!= DECIMAL_FLOAT_TYPE_P (real_tg_type))
8771 		      continue;
8772 		    scalar_float_mode arg_mode
8773 		      = SCALAR_FLOAT_TYPE_MODE (arg_real);
8774 		    scalar_float_mode tg_mode
8775 		      = SCALAR_FLOAT_TYPE_MODE (real_tg_type);
8776 		    const real_format *arg_fmt = REAL_MODE_FORMAT (arg_mode);
8777 		    const real_format *tg_fmt = REAL_MODE_FORMAT (tg_mode);
8778 		    if (arg_fmt->b == tg_fmt->b
8779 			&& arg_fmt->p <= tg_fmt->p
8780 			&& arg_fmt->emax <= tg_fmt->emax
8781 			&& (arg_fmt->emin - arg_fmt->p
8782 			    >= tg_fmt->emin - tg_fmt->p))
8783 		      {
8784 			fn = &(*cexpr_list)[j];
8785 			break;
8786 		      }
8787 		  }
8788 	      }
8789 	    if (fn == NULL)
8790 	      {
8791 		error_at (loc, "no matching function for type-generic call");
8792 		expr.set_error ();
8793 		break;
8794 	      }
8795 
8796 	    /* Construct a call to FN.  */
8797 	    vec<tree, va_gc> *args;
8798 	    vec_alloc (args, nargs);
8799 	    vec<tree, va_gc> *origtypes;
8800 	    vec_alloc (origtypes, nargs);
8801 	    auto_vec<location_t> arg_loc (nargs);
8802 	    for (unsigned int j = 0; j < nargs; j++)
8803 	      {
8804 		c_expr_t *ce = &(*cexpr_list)[num_functions + j];
8805 		args->quick_push (ce->value);
8806 		arg_loc.quick_push (ce->get_location ());
8807 		origtypes->quick_push (ce->original_type);
8808 	      }
8809 	    expr.value = c_build_function_call_vec (loc, arg_loc, fn->value,
8810 						    args, origtypes);
8811 	    set_c_expr_source_range (&expr, loc, close_paren_loc);
8812 	    break;
8813 	  }
8814 	case RID_BUILTIN_CALL_WITH_STATIC_CHAIN:
8815 	  {
8816 	    vec<c_expr_t, va_gc> *cexpr_list;
8817 	    c_expr_t *e2_p;
8818 	    tree chain_value;
8819 	    location_t close_paren_loc;
8820 
8821 	    c_parser_consume_token (parser);
8822 	    if (!c_parser_get_builtin_args (parser,
8823 					    "__builtin_call_with_static_chain",
8824 					    &cexpr_list, false,
8825 					    &close_paren_loc))
8826 	      {
8827 		expr.set_error ();
8828 		break;
8829 	      }
8830 	    if (vec_safe_length (cexpr_list) != 2)
8831 	      {
8832 		error_at (loc, "wrong number of arguments to "
8833 			       "%<__builtin_call_with_static_chain%>");
8834 		expr.set_error ();
8835 		break;
8836 	      }
8837 
8838 	    expr = (*cexpr_list)[0];
8839 	    e2_p = &(*cexpr_list)[1];
8840 	    *e2_p = convert_lvalue_to_rvalue (loc, *e2_p, true, true);
8841 	    chain_value = e2_p->value;
8842 	    mark_exp_read (chain_value);
8843 
8844 	    if (TREE_CODE (expr.value) != CALL_EXPR)
8845 	      error_at (loc, "first argument to "
8846 			"%<__builtin_call_with_static_chain%> "
8847 			"must be a call expression");
8848 	    else if (TREE_CODE (TREE_TYPE (chain_value)) != POINTER_TYPE)
8849 	      error_at (loc, "second argument to "
8850 			"%<__builtin_call_with_static_chain%> "
8851 			"must be a pointer type");
8852 	    else
8853 	      CALL_EXPR_STATIC_CHAIN (expr.value) = chain_value;
8854 	    set_c_expr_source_range (&expr, loc, close_paren_loc);
8855 	    break;
8856 	  }
8857 	case RID_BUILTIN_COMPLEX:
8858 	  {
8859 	    vec<c_expr_t, va_gc> *cexpr_list;
8860 	    c_expr_t *e1_p, *e2_p;
8861 	    location_t close_paren_loc;
8862 
8863 	    c_parser_consume_token (parser);
8864 	    if (!c_parser_get_builtin_args (parser,
8865 					    "__builtin_complex",
8866 					    &cexpr_list, false,
8867 					    &close_paren_loc))
8868 	      {
8869 		expr.set_error ();
8870 		break;
8871 	      }
8872 
8873 	    if (vec_safe_length (cexpr_list) != 2)
8874 	      {
8875 		error_at (loc, "wrong number of arguments to "
8876 			       "%<__builtin_complex%>");
8877 		expr.set_error ();
8878 		break;
8879 	      }
8880 
8881 	    e1_p = &(*cexpr_list)[0];
8882 	    e2_p = &(*cexpr_list)[1];
8883 
8884 	    *e1_p = convert_lvalue_to_rvalue (loc, *e1_p, true, true);
8885 	    if (TREE_CODE (e1_p->value) == EXCESS_PRECISION_EXPR)
8886 	      e1_p->value = convert (TREE_TYPE (e1_p->value),
8887 				     TREE_OPERAND (e1_p->value, 0));
8888 	    *e2_p = convert_lvalue_to_rvalue (loc, *e2_p, true, true);
8889 	    if (TREE_CODE (e2_p->value) == EXCESS_PRECISION_EXPR)
8890 	      e2_p->value = convert (TREE_TYPE (e2_p->value),
8891 				     TREE_OPERAND (e2_p->value, 0));
8892 	    if (!SCALAR_FLOAT_TYPE_P (TREE_TYPE (e1_p->value))
8893 		|| DECIMAL_FLOAT_TYPE_P (TREE_TYPE (e1_p->value))
8894 		|| !SCALAR_FLOAT_TYPE_P (TREE_TYPE (e2_p->value))
8895 		|| DECIMAL_FLOAT_TYPE_P (TREE_TYPE (e2_p->value)))
8896 	      {
8897 		error_at (loc, "%<__builtin_complex%> operand "
8898 			  "not of real binary floating-point type");
8899 		expr.set_error ();
8900 		break;
8901 	      }
8902 	    if (TYPE_MAIN_VARIANT (TREE_TYPE (e1_p->value))
8903 		!= TYPE_MAIN_VARIANT (TREE_TYPE (e2_p->value)))
8904 	      {
8905 		error_at (loc,
8906 			  "%<__builtin_complex%> operands of different types");
8907 		expr.set_error ();
8908 		break;
8909 	      }
8910 	    pedwarn_c90 (loc, OPT_Wpedantic,
8911 			 "ISO C90 does not support complex types");
8912 	    expr.value = build2_loc (loc, COMPLEX_EXPR,
8913 				     build_complex_type
8914 				     (TYPE_MAIN_VARIANT
8915 				      (TREE_TYPE (e1_p->value))),
8916 				     e1_p->value, e2_p->value);
8917 	    set_c_expr_source_range (&expr, loc, close_paren_loc);
8918 	    break;
8919 	  }
8920 	case RID_BUILTIN_SHUFFLE:
8921 	  {
8922 	    vec<c_expr_t, va_gc> *cexpr_list;
8923 	    unsigned int i;
8924 	    c_expr_t *p;
8925 	    location_t close_paren_loc;
8926 
8927 	    c_parser_consume_token (parser);
8928 	    if (!c_parser_get_builtin_args (parser,
8929 					    "__builtin_shuffle",
8930 					    &cexpr_list, false,
8931 					    &close_paren_loc))
8932 	      {
8933 		expr.set_error ();
8934 		break;
8935 	      }
8936 
8937 	    FOR_EACH_VEC_SAFE_ELT (cexpr_list, i, p)
8938 	      *p = convert_lvalue_to_rvalue (loc, *p, true, true);
8939 
8940 	    if (vec_safe_length (cexpr_list) == 2)
8941 	      expr.value =
8942 		c_build_vec_perm_expr
8943 		  (loc, (*cexpr_list)[0].value,
8944 		   NULL_TREE, (*cexpr_list)[1].value);
8945 
8946 	    else if (vec_safe_length (cexpr_list) == 3)
8947 	      expr.value =
8948 		c_build_vec_perm_expr
8949 		  (loc, (*cexpr_list)[0].value,
8950 		   (*cexpr_list)[1].value,
8951 		   (*cexpr_list)[2].value);
8952 	    else
8953 	      {
8954 		error_at (loc, "wrong number of arguments to "
8955 			       "%<__builtin_shuffle%>");
8956 		expr.set_error ();
8957 	      }
8958 	    set_c_expr_source_range (&expr, loc, close_paren_loc);
8959 	    break;
8960 	  }
8961 	case RID_AT_SELECTOR:
8962 	  {
8963 	    gcc_assert (c_dialect_objc ());
8964 	    c_parser_consume_token (parser);
8965 	    matching_parens parens;
8966 	    if (!parens.require_open (parser))
8967 	      {
8968 		expr.set_error ();
8969 		break;
8970 	      }
8971 	    tree sel = c_parser_objc_selector_arg (parser);
8972 	    location_t close_loc = c_parser_peek_token (parser)->location;
8973 	    parens.skip_until_found_close (parser);
8974 	    expr.value = objc_build_selector_expr (loc, sel);
8975 	    set_c_expr_source_range (&expr, loc, close_loc);
8976 	  }
8977 	  break;
8978 	case RID_AT_PROTOCOL:
8979 	  {
8980 	    gcc_assert (c_dialect_objc ());
8981 	    c_parser_consume_token (parser);
8982 	    matching_parens parens;
8983 	    if (!parens.require_open (parser))
8984 	      {
8985 		expr.set_error ();
8986 		break;
8987 	      }
8988 	    if (c_parser_next_token_is_not (parser, CPP_NAME))
8989 	      {
8990 		c_parser_error (parser, "expected identifier");
8991 		c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
8992 		expr.set_error ();
8993 		break;
8994 	      }
8995 	    tree id = c_parser_peek_token (parser)->value;
8996 	    c_parser_consume_token (parser);
8997 	    location_t close_loc = c_parser_peek_token (parser)->location;
8998 	    parens.skip_until_found_close (parser);
8999 	    expr.value = objc_build_protocol_expr (id);
9000 	    set_c_expr_source_range (&expr, loc, close_loc);
9001 	  }
9002 	  break;
9003 	case RID_AT_ENCODE:
9004 	  {
9005 	    /* Extension to support C-structures in the archiver.  */
9006 	    gcc_assert (c_dialect_objc ());
9007 	    c_parser_consume_token (parser);
9008 	    matching_parens parens;
9009 	    if (!parens.require_open (parser))
9010 	      {
9011 		expr.set_error ();
9012 		break;
9013 	      }
9014 	    t1 = c_parser_type_name (parser);
9015 	    if (t1 == NULL)
9016 	      {
9017 		expr.set_error ();
9018 		c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
9019 		break;
9020 	      }
9021 	    location_t close_loc = c_parser_peek_token (parser)->location;
9022 	    parens.skip_until_found_close (parser);
9023 	    tree type = groktypename (t1, NULL, NULL);
9024 	    expr.value = objc_build_encode_expr (type);
9025 	    set_c_expr_source_range (&expr, loc, close_loc);
9026 	  }
9027 	  break;
9028 	case RID_GENERIC:
9029 	  expr = c_parser_generic_selection (parser);
9030 	  break;
9031 	default:
9032 	  c_parser_error (parser, "expected expression");
9033 	  expr.set_error ();
9034 	  break;
9035 	}
9036       break;
9037     case CPP_OPEN_SQUARE:
9038       if (c_dialect_objc ())
9039 	{
9040 	  tree receiver, args;
9041 	  c_parser_consume_token (parser);
9042 	  receiver = c_parser_objc_receiver (parser);
9043 	  args = c_parser_objc_message_args (parser);
9044 	  location_t close_loc = c_parser_peek_token (parser)->location;
9045 	  c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
9046 				     "expected %<]%>");
9047 	  expr.value = objc_build_message_expr (receiver, args);
9048 	  set_c_expr_source_range (&expr, loc, close_loc);
9049 	  break;
9050 	}
9051       /* Else fall through to report error.  */
9052       /* FALLTHRU */
9053     default:
9054       c_parser_error (parser, "expected expression");
9055       expr.set_error ();
9056       break;
9057     }
9058  out:
9059   return c_parser_postfix_expression_after_primary
9060     (parser, EXPR_LOC_OR_LOC (expr.value, loc), expr);
9061 }
9062 
9063 /* Parse a postfix expression after a parenthesized type name: the
9064    brace-enclosed initializer of a compound literal, possibly followed
9065    by some postfix operators.  This is separate because it is not
9066    possible to tell until after the type name whether a cast
9067    expression has a cast or a compound literal, or whether the operand
9068    of sizeof is a parenthesized type name or starts with a compound
9069    literal.  TYPE_LOC is the location where TYPE_NAME starts--the
9070    location of the first token after the parentheses around the type
9071    name.  */
9072 
9073 static struct c_expr
c_parser_postfix_expression_after_paren_type(c_parser * parser,struct c_type_name * type_name,location_t type_loc)9074 c_parser_postfix_expression_after_paren_type (c_parser *parser,
9075 					      struct c_type_name *type_name,
9076 					      location_t type_loc)
9077 {
9078   tree type;
9079   struct c_expr init;
9080   bool non_const;
9081   struct c_expr expr;
9082   location_t start_loc;
9083   tree type_expr = NULL_TREE;
9084   bool type_expr_const = true;
9085   check_compound_literal_type (type_loc, type_name);
9086   rich_location richloc (line_table, type_loc);
9087   start_init (NULL_TREE, NULL, 0, &richloc);
9088   type = groktypename (type_name, &type_expr, &type_expr_const);
9089   start_loc = c_parser_peek_token (parser)->location;
9090   if (type != error_mark_node && C_TYPE_VARIABLE_SIZE (type))
9091     {
9092       error_at (type_loc, "compound literal has variable size");
9093       type = error_mark_node;
9094     }
9095   init = c_parser_braced_init (parser, type, false, NULL);
9096   finish_init ();
9097   maybe_warn_string_init (type_loc, type, init);
9098 
9099   if (type != error_mark_node
9100       && !ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (type))
9101       && current_function_decl)
9102     {
9103       error ("compound literal qualified by address-space qualifier");
9104       type = error_mark_node;
9105     }
9106 
9107   pedwarn_c90 (start_loc, OPT_Wpedantic, "ISO C90 forbids compound literals");
9108   non_const = ((init.value && TREE_CODE (init.value) == CONSTRUCTOR)
9109 	       ? CONSTRUCTOR_NON_CONST (init.value)
9110 	       : init.original_code == C_MAYBE_CONST_EXPR);
9111   non_const |= !type_expr_const;
9112   unsigned int alignas_align = 0;
9113   if (type != error_mark_node
9114       && type_name->specs->align_log != -1)
9115     {
9116       alignas_align = 1U << type_name->specs->align_log;
9117       if (alignas_align < min_align_of_type (type))
9118 	{
9119 	  error_at (type_name->specs->locations[cdw_alignas],
9120 		    "%<_Alignas%> specifiers cannot reduce "
9121 		    "alignment of compound literal");
9122 	  alignas_align = 0;
9123 	}
9124     }
9125   expr.value = build_compound_literal (start_loc, type, init.value, non_const,
9126 				       alignas_align);
9127   set_c_expr_source_range (&expr, init.src_range);
9128   expr.original_code = ERROR_MARK;
9129   expr.original_type = NULL;
9130   if (type != error_mark_node
9131       && expr.value != error_mark_node
9132       && type_expr)
9133     {
9134       if (TREE_CODE (expr.value) == C_MAYBE_CONST_EXPR)
9135 	{
9136 	  gcc_assert (C_MAYBE_CONST_EXPR_PRE (expr.value) == NULL_TREE);
9137 	  C_MAYBE_CONST_EXPR_PRE (expr.value) = type_expr;
9138 	}
9139       else
9140 	{
9141 	  gcc_assert (!non_const);
9142 	  expr.value = build2 (C_MAYBE_CONST_EXPR, type,
9143 			       type_expr, expr.value);
9144 	}
9145     }
9146   return c_parser_postfix_expression_after_primary (parser, start_loc, expr);
9147 }
9148 
9149 /* Callback function for sizeof_pointer_memaccess_warning to compare
9150    types.  */
9151 
9152 static bool
sizeof_ptr_memacc_comptypes(tree type1,tree type2)9153 sizeof_ptr_memacc_comptypes (tree type1, tree type2)
9154 {
9155   return comptypes (type1, type2) == 1;
9156 }
9157 
9158 /* Parse a postfix expression after the initial primary or compound
9159    literal; that is, parse a series of postfix operators.
9160 
9161    EXPR_LOC is the location of the primary expression.  */
9162 
9163 static struct c_expr
c_parser_postfix_expression_after_primary(c_parser * parser,location_t expr_loc,struct c_expr expr)9164 c_parser_postfix_expression_after_primary (c_parser *parser,
9165 					   location_t expr_loc,
9166 					   struct c_expr expr)
9167 {
9168   struct c_expr orig_expr;
9169   tree ident, idx;
9170   location_t sizeof_arg_loc[3], comp_loc;
9171   tree sizeof_arg[3];
9172   unsigned int literal_zero_mask;
9173   unsigned int i;
9174   vec<tree, va_gc> *exprlist;
9175   vec<tree, va_gc> *origtypes = NULL;
9176   vec<location_t> arg_loc = vNULL;
9177   location_t start;
9178   location_t finish;
9179 
9180   while (true)
9181     {
9182       location_t op_loc = c_parser_peek_token (parser)->location;
9183       switch (c_parser_peek_token (parser)->type)
9184 	{
9185 	case CPP_OPEN_SQUARE:
9186 	  /* Array reference.  */
9187 	  c_parser_consume_token (parser);
9188 	  idx = c_parser_expression (parser).value;
9189 	  c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
9190 				     "expected %<]%>");
9191 	  start = expr.get_start ();
9192 	  finish = parser->tokens_buf[0].location;
9193 	  expr.value = build_array_ref (op_loc, expr.value, idx);
9194 	  set_c_expr_source_range (&expr, start, finish);
9195 	  expr.original_code = ERROR_MARK;
9196 	  expr.original_type = NULL;
9197 	  break;
9198 	case CPP_OPEN_PAREN:
9199 	  /* Function call.  */
9200 	  c_parser_consume_token (parser);
9201 	  for (i = 0; i < 3; i++)
9202 	    {
9203 	      sizeof_arg[i] = NULL_TREE;
9204 	      sizeof_arg_loc[i] = UNKNOWN_LOCATION;
9205 	    }
9206 	  literal_zero_mask = 0;
9207 	  if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
9208 	    exprlist = NULL;
9209 	  else
9210 	    exprlist = c_parser_expr_list (parser, true, false, &origtypes,
9211 					   sizeof_arg_loc, sizeof_arg,
9212 					   &arg_loc, &literal_zero_mask);
9213 	  c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
9214 				     "expected %<)%>");
9215 	  orig_expr = expr;
9216 	  mark_exp_read (expr.value);
9217 	  if (warn_sizeof_pointer_memaccess)
9218 	    sizeof_pointer_memaccess_warning (sizeof_arg_loc,
9219 					      expr.value, exprlist,
9220 					      sizeof_arg,
9221 					      sizeof_ptr_memacc_comptypes);
9222 	  if (TREE_CODE (expr.value) == FUNCTION_DECL
9223 	      && DECL_BUILT_IN_CLASS (expr.value) == BUILT_IN_NORMAL
9224 	      && DECL_FUNCTION_CODE (expr.value) == BUILT_IN_MEMSET
9225 	      && vec_safe_length (exprlist) == 3)
9226 	    {
9227 	      tree arg0 = (*exprlist)[0];
9228 	      tree arg2 = (*exprlist)[2];
9229 	      warn_for_memset (expr_loc, arg0, arg2, literal_zero_mask);
9230 	    }
9231 
9232 	  start = expr.get_start ();
9233 	  finish = parser->tokens_buf[0].get_finish ();
9234 	  expr.value
9235 	    = c_build_function_call_vec (expr_loc, arg_loc, expr.value,
9236 					 exprlist, origtypes);
9237 	  set_c_expr_source_range (&expr, start, finish);
9238 
9239 	  expr.original_code = ERROR_MARK;
9240 	  if (TREE_CODE (expr.value) == INTEGER_CST
9241 	      && TREE_CODE (orig_expr.value) == FUNCTION_DECL
9242 	      && DECL_BUILT_IN_CLASS (orig_expr.value) == BUILT_IN_NORMAL
9243 	      && DECL_FUNCTION_CODE (orig_expr.value) == BUILT_IN_CONSTANT_P)
9244 	    expr.original_code = C_MAYBE_CONST_EXPR;
9245 	  expr.original_type = NULL;
9246 	  if (exprlist)
9247 	    {
9248 	      release_tree_vector (exprlist);
9249 	      release_tree_vector (origtypes);
9250 	    }
9251 	  arg_loc.release ();
9252 	  break;
9253 	case CPP_DOT:
9254 	  /* Structure element reference.  */
9255 	  c_parser_consume_token (parser);
9256 	  expr = default_function_array_conversion (expr_loc, expr);
9257 	  if (c_parser_next_token_is (parser, CPP_NAME))
9258 	    {
9259 	      c_token *comp_tok = c_parser_peek_token (parser);
9260 	      ident = comp_tok->value;
9261 	      comp_loc = comp_tok->location;
9262 	    }
9263 	  else
9264 	    {
9265 	      c_parser_error (parser, "expected identifier");
9266 	      expr.set_error ();
9267 	      expr.original_code = ERROR_MARK;
9268               expr.original_type = NULL;
9269 	      return expr;
9270 	    }
9271 	  start = expr.get_start ();
9272 	  finish = c_parser_peek_token (parser)->get_finish ();
9273 	  c_parser_consume_token (parser);
9274 	  expr.value = build_component_ref (op_loc, expr.value, ident,
9275 					    comp_loc);
9276 	  set_c_expr_source_range (&expr, start, finish);
9277 	  expr.original_code = ERROR_MARK;
9278 	  if (TREE_CODE (expr.value) != COMPONENT_REF)
9279 	    expr.original_type = NULL;
9280 	  else
9281 	    {
9282 	      /* Remember the original type of a bitfield.  */
9283 	      tree field = TREE_OPERAND (expr.value, 1);
9284 	      if (TREE_CODE (field) != FIELD_DECL)
9285 		expr.original_type = NULL;
9286 	      else
9287 		expr.original_type = DECL_BIT_FIELD_TYPE (field);
9288 	    }
9289 	  break;
9290 	case CPP_DEREF:
9291 	  /* Structure element reference.  */
9292 	  c_parser_consume_token (parser);
9293 	  expr = convert_lvalue_to_rvalue (expr_loc, expr, true, false);
9294 	  if (c_parser_next_token_is (parser, CPP_NAME))
9295 	    {
9296 	      c_token *comp_tok = c_parser_peek_token (parser);
9297 	      ident = comp_tok->value;
9298 	      comp_loc = comp_tok->location;
9299 	    }
9300 	  else
9301 	    {
9302 	      c_parser_error (parser, "expected identifier");
9303 	      expr.set_error ();
9304 	      expr.original_code = ERROR_MARK;
9305 	      expr.original_type = NULL;
9306 	      return expr;
9307 	    }
9308 	  start = expr.get_start ();
9309 	  finish = c_parser_peek_token (parser)->get_finish ();
9310 	  c_parser_consume_token (parser);
9311 	  expr.value = build_component_ref (op_loc,
9312 					    build_indirect_ref (op_loc,
9313 								expr.value,
9314 								RO_ARROW),
9315 					    ident, comp_loc);
9316 	  set_c_expr_source_range (&expr, start, finish);
9317 	  expr.original_code = ERROR_MARK;
9318 	  if (TREE_CODE (expr.value) != COMPONENT_REF)
9319 	    expr.original_type = NULL;
9320 	  else
9321 	    {
9322 	      /* Remember the original type of a bitfield.  */
9323 	      tree field = TREE_OPERAND (expr.value, 1);
9324 	      if (TREE_CODE (field) != FIELD_DECL)
9325 		expr.original_type = NULL;
9326 	      else
9327 		expr.original_type = DECL_BIT_FIELD_TYPE (field);
9328 	    }
9329 	  break;
9330 	case CPP_PLUS_PLUS:
9331 	  /* Postincrement.  */
9332 	  start = expr.get_start ();
9333 	  finish = c_parser_peek_token (parser)->get_finish ();
9334 	  c_parser_consume_token (parser);
9335 	  expr = default_function_array_read_conversion (expr_loc, expr);
9336 	  expr.value = build_unary_op (op_loc, POSTINCREMENT_EXPR,
9337 				       expr.value, false);
9338 	  set_c_expr_source_range (&expr, start, finish);
9339 	  expr.original_code = ERROR_MARK;
9340 	  expr.original_type = NULL;
9341 	  break;
9342 	case CPP_MINUS_MINUS:
9343 	  /* Postdecrement.  */
9344 	  start = expr.get_start ();
9345 	  finish = c_parser_peek_token (parser)->get_finish ();
9346 	  c_parser_consume_token (parser);
9347 	  expr = default_function_array_read_conversion (expr_loc, expr);
9348 	  expr.value = build_unary_op (op_loc, POSTDECREMENT_EXPR,
9349 				       expr.value, false);
9350 	  set_c_expr_source_range (&expr, start, finish);
9351 	  expr.original_code = ERROR_MARK;
9352 	  expr.original_type = NULL;
9353 	  break;
9354 	default:
9355 	  return expr;
9356 	}
9357     }
9358 }
9359 
9360 /* Parse an expression (C90 6.3.17, C99 6.5.17, C11 6.5.17).
9361 
9362    expression:
9363      assignment-expression
9364      expression , assignment-expression
9365 */
9366 
9367 static struct c_expr
c_parser_expression(c_parser * parser)9368 c_parser_expression (c_parser *parser)
9369 {
9370   location_t tloc = c_parser_peek_token (parser)->location;
9371   struct c_expr expr;
9372   expr = c_parser_expr_no_commas (parser, NULL);
9373   if (c_parser_next_token_is (parser, CPP_COMMA))
9374     expr = convert_lvalue_to_rvalue (tloc, expr, true, false);
9375   while (c_parser_next_token_is (parser, CPP_COMMA))
9376     {
9377       struct c_expr next;
9378       tree lhsval;
9379       location_t loc = c_parser_peek_token (parser)->location;
9380       location_t expr_loc;
9381       c_parser_consume_token (parser);
9382       expr_loc = c_parser_peek_token (parser)->location;
9383       lhsval = expr.value;
9384       while (TREE_CODE (lhsval) == COMPOUND_EXPR)
9385 	lhsval = TREE_OPERAND (lhsval, 1);
9386       if (DECL_P (lhsval) || handled_component_p (lhsval))
9387 	mark_exp_read (lhsval);
9388       next = c_parser_expr_no_commas (parser, NULL);
9389       next = convert_lvalue_to_rvalue (expr_loc, next, true, false);
9390       expr.value = build_compound_expr (loc, expr.value, next.value);
9391       expr.original_code = COMPOUND_EXPR;
9392       expr.original_type = next.original_type;
9393     }
9394   return expr;
9395 }
9396 
9397 /* Parse an expression and convert functions or arrays to pointers and
9398    lvalues to rvalues.  */
9399 
9400 static struct c_expr
c_parser_expression_conv(c_parser * parser)9401 c_parser_expression_conv (c_parser *parser)
9402 {
9403   struct c_expr expr;
9404   location_t loc = c_parser_peek_token (parser)->location;
9405   expr = c_parser_expression (parser);
9406   expr = convert_lvalue_to_rvalue (loc, expr, true, false);
9407   return expr;
9408 }
9409 
9410 /* Helper function of c_parser_expr_list.  Check if IDXth (0 based)
9411    argument is a literal zero alone and if so, set it in literal_zero_mask.  */
9412 
9413 static inline void
c_parser_check_literal_zero(c_parser * parser,unsigned * literal_zero_mask,unsigned int idx)9414 c_parser_check_literal_zero (c_parser *parser, unsigned *literal_zero_mask,
9415 			     unsigned int idx)
9416 {
9417   if (idx >= HOST_BITS_PER_INT)
9418     return;
9419 
9420   c_token *tok = c_parser_peek_token (parser);
9421   switch (tok->type)
9422     {
9423     case CPP_NUMBER:
9424     case CPP_CHAR:
9425     case CPP_WCHAR:
9426     case CPP_CHAR16:
9427     case CPP_CHAR32:
9428       /* If a parameter is literal zero alone, remember it
9429 	 for -Wmemset-transposed-args warning.  */
9430       if (integer_zerop (tok->value)
9431 	  && !TREE_OVERFLOW (tok->value)
9432 	  && (c_parser_peek_2nd_token (parser)->type == CPP_COMMA
9433 	      || c_parser_peek_2nd_token (parser)->type == CPP_CLOSE_PAREN))
9434 	*literal_zero_mask |= 1U << idx;
9435     default:
9436       break;
9437     }
9438 }
9439 
9440 /* Parse a non-empty list of expressions.  If CONVERT_P, convert
9441    functions and arrays to pointers and lvalues to rvalues.  If
9442    FOLD_P, fold the expressions.  If LOCATIONS is non-NULL, save the
9443    locations of function arguments into this vector.
9444 
9445    nonempty-expr-list:
9446      assignment-expression
9447      nonempty-expr-list , assignment-expression
9448 */
9449 
9450 static vec<tree, va_gc> *
c_parser_expr_list(c_parser * parser,bool convert_p,bool fold_p,vec<tree,va_gc> ** p_orig_types,location_t * sizeof_arg_loc,tree * sizeof_arg,vec<location_t> * locations,unsigned int * literal_zero_mask)9451 c_parser_expr_list (c_parser *parser, bool convert_p, bool fold_p,
9452 		    vec<tree, va_gc> **p_orig_types,
9453 		    location_t *sizeof_arg_loc, tree *sizeof_arg,
9454 		    vec<location_t> *locations,
9455 		    unsigned int *literal_zero_mask)
9456 {
9457   vec<tree, va_gc> *ret;
9458   vec<tree, va_gc> *orig_types;
9459   struct c_expr expr;
9460   unsigned int idx = 0;
9461 
9462   ret = make_tree_vector ();
9463   if (p_orig_types == NULL)
9464     orig_types = NULL;
9465   else
9466     orig_types = make_tree_vector ();
9467 
9468   if (literal_zero_mask)
9469     c_parser_check_literal_zero (parser, literal_zero_mask, 0);
9470   expr = c_parser_expr_no_commas (parser, NULL);
9471   if (convert_p)
9472     expr = convert_lvalue_to_rvalue (expr.get_location (), expr, true, true);
9473   if (fold_p)
9474     expr.value = c_fully_fold (expr.value, false, NULL);
9475   ret->quick_push (expr.value);
9476   if (orig_types)
9477     orig_types->quick_push (expr.original_type);
9478   if (locations)
9479     locations->safe_push (expr.get_location ());
9480   if (sizeof_arg != NULL
9481       && expr.original_code == SIZEOF_EXPR)
9482     {
9483       sizeof_arg[0] = c_last_sizeof_arg;
9484       sizeof_arg_loc[0] = c_last_sizeof_loc;
9485     }
9486   while (c_parser_next_token_is (parser, CPP_COMMA))
9487     {
9488       c_parser_consume_token (parser);
9489       if (literal_zero_mask)
9490 	c_parser_check_literal_zero (parser, literal_zero_mask, idx + 1);
9491       expr = c_parser_expr_no_commas (parser, NULL);
9492       if (convert_p)
9493 	expr = convert_lvalue_to_rvalue (expr.get_location (), expr, true,
9494 					 true);
9495       if (fold_p)
9496 	expr.value = c_fully_fold (expr.value, false, NULL);
9497       vec_safe_push (ret, expr.value);
9498       if (orig_types)
9499 	vec_safe_push (orig_types, expr.original_type);
9500       if (locations)
9501 	locations->safe_push (expr.get_location ());
9502       if (++idx < 3
9503 	  && sizeof_arg != NULL
9504 	  && expr.original_code == SIZEOF_EXPR)
9505 	{
9506 	  sizeof_arg[idx] = c_last_sizeof_arg;
9507 	  sizeof_arg_loc[idx] = c_last_sizeof_loc;
9508 	}
9509     }
9510   if (orig_types)
9511     *p_orig_types = orig_types;
9512   return ret;
9513 }
9514 
9515 /* Parse Objective-C-specific constructs.  */
9516 
9517 /* Parse an objc-class-definition.
9518 
9519    objc-class-definition:
9520      @interface identifier objc-superclass[opt] objc-protocol-refs[opt]
9521        objc-class-instance-variables[opt] objc-methodprotolist @end
9522      @implementation identifier objc-superclass[opt]
9523        objc-class-instance-variables[opt]
9524      @interface identifier ( identifier ) objc-protocol-refs[opt]
9525        objc-methodprotolist @end
9526      @interface identifier ( ) objc-protocol-refs[opt]
9527        objc-methodprotolist @end
9528      @implementation identifier ( identifier )
9529 
9530    objc-superclass:
9531      : identifier
9532 
9533    "@interface identifier (" must start "@interface identifier (
9534    identifier ) ...": objc-methodprotolist in the first production may
9535    not start with a parenthesized identifier as a declarator of a data
9536    definition with no declaration specifiers if the objc-superclass,
9537    objc-protocol-refs and objc-class-instance-variables are omitted.  */
9538 
9539 static void
c_parser_objc_class_definition(c_parser * parser,tree attributes)9540 c_parser_objc_class_definition (c_parser *parser, tree attributes)
9541 {
9542   bool iface_p;
9543   tree id1;
9544   tree superclass;
9545   if (c_parser_next_token_is_keyword (parser, RID_AT_INTERFACE))
9546     iface_p = true;
9547   else if (c_parser_next_token_is_keyword (parser, RID_AT_IMPLEMENTATION))
9548     iface_p = false;
9549   else
9550     gcc_unreachable ();
9551 
9552   c_parser_consume_token (parser);
9553   if (c_parser_next_token_is_not (parser, CPP_NAME))
9554     {
9555       c_parser_error (parser, "expected identifier");
9556       return;
9557     }
9558   id1 = c_parser_peek_token (parser)->value;
9559   c_parser_consume_token (parser);
9560   if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
9561     {
9562       /* We have a category or class extension.  */
9563       tree id2;
9564       tree proto = NULL_TREE;
9565       matching_parens parens;
9566       parens.consume_open (parser);
9567       if (c_parser_next_token_is_not (parser, CPP_NAME))
9568 	{
9569 	  if (iface_p && c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
9570 	    {
9571 	      /* We have a class extension.  */
9572 	      id2 = NULL_TREE;
9573 	    }
9574 	  else
9575 	    {
9576 	      c_parser_error (parser, "expected identifier or %<)%>");
9577 	      c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
9578 	      return;
9579 	    }
9580 	}
9581       else
9582 	{
9583 	  id2 = c_parser_peek_token (parser)->value;
9584 	  c_parser_consume_token (parser);
9585 	}
9586       parens.skip_until_found_close (parser);
9587       if (!iface_p)
9588 	{
9589 	  objc_start_category_implementation (id1, id2);
9590 	  return;
9591 	}
9592       if (c_parser_next_token_is (parser, CPP_LESS))
9593 	proto = c_parser_objc_protocol_refs (parser);
9594       objc_start_category_interface (id1, id2, proto, attributes);
9595       c_parser_objc_methodprotolist (parser);
9596       c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
9597       objc_finish_interface ();
9598       return;
9599     }
9600   if (c_parser_next_token_is (parser, CPP_COLON))
9601     {
9602       c_parser_consume_token (parser);
9603       if (c_parser_next_token_is_not (parser, CPP_NAME))
9604 	{
9605 	  c_parser_error (parser, "expected identifier");
9606 	  return;
9607 	}
9608       superclass = c_parser_peek_token (parser)->value;
9609       c_parser_consume_token (parser);
9610     }
9611   else
9612     superclass = NULL_TREE;
9613   if (iface_p)
9614     {
9615       tree proto = NULL_TREE;
9616       if (c_parser_next_token_is (parser, CPP_LESS))
9617 	proto = c_parser_objc_protocol_refs (parser);
9618       objc_start_class_interface (id1, superclass, proto, attributes);
9619     }
9620   else
9621     objc_start_class_implementation (id1, superclass);
9622   if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
9623     c_parser_objc_class_instance_variables (parser);
9624   if (iface_p)
9625     {
9626       objc_continue_interface ();
9627       c_parser_objc_methodprotolist (parser);
9628       c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
9629       objc_finish_interface ();
9630     }
9631   else
9632     {
9633       objc_continue_implementation ();
9634       return;
9635     }
9636 }
9637 
9638 /* Parse objc-class-instance-variables.
9639 
9640    objc-class-instance-variables:
9641      { objc-instance-variable-decl-list[opt] }
9642 
9643    objc-instance-variable-decl-list:
9644      objc-visibility-spec
9645      objc-instance-variable-decl ;
9646      ;
9647      objc-instance-variable-decl-list objc-visibility-spec
9648      objc-instance-variable-decl-list objc-instance-variable-decl ;
9649      objc-instance-variable-decl-list ;
9650 
9651    objc-visibility-spec:
9652      @private
9653      @protected
9654      @public
9655 
9656    objc-instance-variable-decl:
9657      struct-declaration
9658 */
9659 
9660 static void
c_parser_objc_class_instance_variables(c_parser * parser)9661 c_parser_objc_class_instance_variables (c_parser *parser)
9662 {
9663   gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE));
9664   c_parser_consume_token (parser);
9665   while (c_parser_next_token_is_not (parser, CPP_EOF))
9666     {
9667       tree decls;
9668       /* Parse any stray semicolon.  */
9669       if (c_parser_next_token_is (parser, CPP_SEMICOLON))
9670 	{
9671 	  pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
9672 		   "extra semicolon");
9673 	  c_parser_consume_token (parser);
9674 	  continue;
9675 	}
9676       /* Stop if at the end of the instance variables.  */
9677       if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
9678 	{
9679 	  c_parser_consume_token (parser);
9680 	  break;
9681 	}
9682       /* Parse any objc-visibility-spec.  */
9683       if (c_parser_next_token_is_keyword (parser, RID_AT_PRIVATE))
9684 	{
9685 	  c_parser_consume_token (parser);
9686 	  objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
9687 	  continue;
9688 	}
9689       else if (c_parser_next_token_is_keyword (parser, RID_AT_PROTECTED))
9690 	{
9691 	  c_parser_consume_token (parser);
9692 	  objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
9693 	  continue;
9694 	}
9695       else if (c_parser_next_token_is_keyword (parser, RID_AT_PUBLIC))
9696 	{
9697 	  c_parser_consume_token (parser);
9698 	  objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
9699 	  continue;
9700 	}
9701       else if (c_parser_next_token_is_keyword (parser, RID_AT_PACKAGE))
9702 	{
9703 	  c_parser_consume_token (parser);
9704 	  objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
9705 	  continue;
9706 	}
9707       else if (c_parser_next_token_is (parser, CPP_PRAGMA))
9708 	{
9709 	  c_parser_pragma (parser, pragma_external, NULL);
9710 	  continue;
9711 	}
9712 
9713       /* Parse some comma-separated declarations.  */
9714       decls = c_parser_struct_declaration (parser);
9715       if (decls == NULL)
9716 	{
9717 	  /* There is a syntax error.  We want to skip the offending
9718 	     tokens up to the next ';' (included) or '}'
9719 	     (excluded).  */
9720 
9721 	  /* First, skip manually a ')' or ']'.  This is because they
9722 	     reduce the nesting level, so c_parser_skip_until_found()
9723 	     wouldn't be able to skip past them.  */
9724 	  c_token *token = c_parser_peek_token (parser);
9725 	  if (token->type == CPP_CLOSE_PAREN || token->type == CPP_CLOSE_SQUARE)
9726 	    c_parser_consume_token (parser);
9727 
9728 	  /* Then, do the standard skipping.  */
9729 	  c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
9730 
9731 	  /* We hopefully recovered.  Start normal parsing again.  */
9732 	  parser->error = false;
9733 	  continue;
9734 	}
9735       else
9736 	{
9737 	  /* Comma-separated instance variables are chained together
9738 	     in reverse order; add them one by one.  */
9739 	  tree ivar = nreverse (decls);
9740 	  for (; ivar; ivar = DECL_CHAIN (ivar))
9741 	    objc_add_instance_variable (copy_node (ivar));
9742 	}
9743       c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9744     }
9745 }
9746 
9747 /* Parse an objc-class-declaration.
9748 
9749    objc-class-declaration:
9750      @class identifier-list ;
9751 */
9752 
9753 static void
c_parser_objc_class_declaration(c_parser * parser)9754 c_parser_objc_class_declaration (c_parser *parser)
9755 {
9756   gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_CLASS));
9757   c_parser_consume_token (parser);
9758   /* Any identifiers, including those declared as type names, are OK
9759      here.  */
9760   while (true)
9761     {
9762       tree id;
9763       if (c_parser_next_token_is_not (parser, CPP_NAME))
9764 	{
9765 	  c_parser_error (parser, "expected identifier");
9766 	  c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
9767 	  parser->error = false;
9768 	  return;
9769 	}
9770       id = c_parser_peek_token (parser)->value;
9771       objc_declare_class (id);
9772       c_parser_consume_token (parser);
9773       if (c_parser_next_token_is (parser, CPP_COMMA))
9774 	c_parser_consume_token (parser);
9775       else
9776 	break;
9777     }
9778   c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9779 }
9780 
9781 /* Parse an objc-alias-declaration.
9782 
9783    objc-alias-declaration:
9784      @compatibility_alias identifier identifier ;
9785 */
9786 
9787 static void
c_parser_objc_alias_declaration(c_parser * parser)9788 c_parser_objc_alias_declaration (c_parser *parser)
9789 {
9790   tree id1, id2;
9791   gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_ALIAS));
9792   c_parser_consume_token (parser);
9793   if (c_parser_next_token_is_not (parser, CPP_NAME))
9794     {
9795       c_parser_error (parser, "expected identifier");
9796       c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
9797       return;
9798     }
9799   id1 = c_parser_peek_token (parser)->value;
9800   c_parser_consume_token (parser);
9801   if (c_parser_next_token_is_not (parser, CPP_NAME))
9802     {
9803       c_parser_error (parser, "expected identifier");
9804       c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
9805       return;
9806     }
9807   id2 = c_parser_peek_token (parser)->value;
9808   c_parser_consume_token (parser);
9809   c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9810   objc_declare_alias (id1, id2);
9811 }
9812 
9813 /* Parse an objc-protocol-definition.
9814 
9815    objc-protocol-definition:
9816      @protocol identifier objc-protocol-refs[opt] objc-methodprotolist @end
9817      @protocol identifier-list ;
9818 
9819    "@protocol identifier ;" should be resolved as "@protocol
9820    identifier-list ;": objc-methodprotolist may not start with a
9821    semicolon in the first alternative if objc-protocol-refs are
9822    omitted.  */
9823 
9824 static void
c_parser_objc_protocol_definition(c_parser * parser,tree attributes)9825 c_parser_objc_protocol_definition (c_parser *parser, tree attributes)
9826 {
9827   gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_PROTOCOL));
9828 
9829   c_parser_consume_token (parser);
9830   if (c_parser_next_token_is_not (parser, CPP_NAME))
9831     {
9832       c_parser_error (parser, "expected identifier");
9833       return;
9834     }
9835   if (c_parser_peek_2nd_token (parser)->type == CPP_COMMA
9836       || c_parser_peek_2nd_token (parser)->type == CPP_SEMICOLON)
9837     {
9838       /* Any identifiers, including those declared as type names, are
9839 	 OK here.  */
9840       while (true)
9841 	{
9842 	  tree id;
9843 	  if (c_parser_next_token_is_not (parser, CPP_NAME))
9844 	    {
9845 	      c_parser_error (parser, "expected identifier");
9846 	      break;
9847 	    }
9848 	  id = c_parser_peek_token (parser)->value;
9849 	  objc_declare_protocol (id, attributes);
9850 	  c_parser_consume_token (parser);
9851 	  if (c_parser_next_token_is (parser, CPP_COMMA))
9852 	    c_parser_consume_token (parser);
9853 	  else
9854 	    break;
9855 	}
9856       c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9857     }
9858   else
9859     {
9860       tree id = c_parser_peek_token (parser)->value;
9861       tree proto = NULL_TREE;
9862       c_parser_consume_token (parser);
9863       if (c_parser_next_token_is (parser, CPP_LESS))
9864 	proto = c_parser_objc_protocol_refs (parser);
9865       parser->objc_pq_context = true;
9866       objc_start_protocol (id, proto, attributes);
9867       c_parser_objc_methodprotolist (parser);
9868       c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
9869       parser->objc_pq_context = false;
9870       objc_finish_interface ();
9871     }
9872 }
9873 
9874 /* Parse an objc-method-type.
9875 
9876    objc-method-type:
9877      +
9878      -
9879 
9880    Return true if it is a class method (+) and false if it is
9881    an instance method (-).
9882 */
9883 static inline bool
c_parser_objc_method_type(c_parser * parser)9884 c_parser_objc_method_type (c_parser *parser)
9885 {
9886   switch (c_parser_peek_token (parser)->type)
9887     {
9888     case CPP_PLUS:
9889       c_parser_consume_token (parser);
9890       return true;
9891     case CPP_MINUS:
9892       c_parser_consume_token (parser);
9893       return false;
9894     default:
9895       gcc_unreachable ();
9896     }
9897 }
9898 
9899 /* Parse an objc-method-definition.
9900 
9901    objc-method-definition:
9902      objc-method-type objc-method-decl ;[opt] compound-statement
9903 */
9904 
9905 static void
c_parser_objc_method_definition(c_parser * parser)9906 c_parser_objc_method_definition (c_parser *parser)
9907 {
9908   bool is_class_method = c_parser_objc_method_type (parser);
9909   tree decl, attributes = NULL_TREE, expr = NULL_TREE;
9910   parser->objc_pq_context = true;
9911   decl = c_parser_objc_method_decl (parser, is_class_method, &attributes,
9912 				    &expr);
9913   if (decl == error_mark_node)
9914     return;  /* Bail here. */
9915 
9916   if (c_parser_next_token_is (parser, CPP_SEMICOLON))
9917     {
9918       c_parser_consume_token (parser);
9919       pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
9920 	       "extra semicolon in method definition specified");
9921     }
9922 
9923   if (!c_parser_next_token_is (parser, CPP_OPEN_BRACE))
9924     {
9925       c_parser_error (parser, "expected %<{%>");
9926       return;
9927     }
9928 
9929   parser->objc_pq_context = false;
9930   if (objc_start_method_definition (is_class_method, decl, attributes, expr))
9931     {
9932       add_stmt (c_parser_compound_statement (parser));
9933       objc_finish_method_definition (current_function_decl);
9934     }
9935   else
9936     {
9937       /* This code is executed when we find a method definition
9938 	 outside of an @implementation context (or invalid for other
9939 	 reasons).  Parse the method (to keep going) but do not emit
9940 	 any code.
9941       */
9942       c_parser_compound_statement (parser);
9943     }
9944 }
9945 
9946 /* Parse an objc-methodprotolist.
9947 
9948    objc-methodprotolist:
9949      empty
9950      objc-methodprotolist objc-methodproto
9951      objc-methodprotolist declaration
9952      objc-methodprotolist ;
9953      @optional
9954      @required
9955 
9956    The declaration is a data definition, which may be missing
9957    declaration specifiers under the same rules and diagnostics as
9958    other data definitions outside functions, and the stray semicolon
9959    is diagnosed the same way as a stray semicolon outside a
9960    function.  */
9961 
9962 static void
c_parser_objc_methodprotolist(c_parser * parser)9963 c_parser_objc_methodprotolist (c_parser *parser)
9964 {
9965   while (true)
9966     {
9967       /* The list is terminated by @end.  */
9968       switch (c_parser_peek_token (parser)->type)
9969 	{
9970 	case CPP_SEMICOLON:
9971 	  pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
9972 		   "ISO C does not allow extra %<;%> outside of a function");
9973 	  c_parser_consume_token (parser);
9974 	  break;
9975 	case CPP_PLUS:
9976 	case CPP_MINUS:
9977 	  c_parser_objc_methodproto (parser);
9978 	  break;
9979 	case CPP_PRAGMA:
9980 	  c_parser_pragma (parser, pragma_external, NULL);
9981 	  break;
9982 	case CPP_EOF:
9983 	  return;
9984 	default:
9985 	  if (c_parser_next_token_is_keyword (parser, RID_AT_END))
9986 	    return;
9987 	  else if (c_parser_next_token_is_keyword (parser, RID_AT_PROPERTY))
9988 	    c_parser_objc_at_property_declaration (parser);
9989 	  else if (c_parser_next_token_is_keyword (parser, RID_AT_OPTIONAL))
9990 	    {
9991 	      objc_set_method_opt (true);
9992 	      c_parser_consume_token (parser);
9993 	    }
9994 	  else if (c_parser_next_token_is_keyword (parser, RID_AT_REQUIRED))
9995 	    {
9996 	      objc_set_method_opt (false);
9997 	      c_parser_consume_token (parser);
9998 	    }
9999 	  else
10000 	    c_parser_declaration_or_fndef (parser, false, false, true,
10001 					   false, true, NULL, vNULL);
10002 	  break;
10003 	}
10004     }
10005 }
10006 
10007 /* Parse an objc-methodproto.
10008 
10009    objc-methodproto:
10010      objc-method-type objc-method-decl ;
10011 */
10012 
10013 static void
c_parser_objc_methodproto(c_parser * parser)10014 c_parser_objc_methodproto (c_parser *parser)
10015 {
10016   bool is_class_method = c_parser_objc_method_type (parser);
10017   tree decl, attributes = NULL_TREE;
10018 
10019   /* Remember protocol qualifiers in prototypes.  */
10020   parser->objc_pq_context = true;
10021   decl = c_parser_objc_method_decl (parser, is_class_method, &attributes,
10022 				    NULL);
10023   /* Forget protocol qualifiers now.  */
10024   parser->objc_pq_context = false;
10025 
10026   /* Do not allow the presence of attributes to hide an erroneous
10027      method implementation in the interface section.  */
10028   if (!c_parser_next_token_is (parser, CPP_SEMICOLON))
10029     {
10030       c_parser_error (parser, "expected %<;%>");
10031       return;
10032     }
10033 
10034   if (decl != error_mark_node)
10035     objc_add_method_declaration (is_class_method, decl, attributes);
10036 
10037   c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
10038 }
10039 
10040 /* If we are at a position that method attributes may be present, check that
10041    there are not any parsed already (a syntax error) and then collect any
10042    specified at the current location.  Finally, if new attributes were present,
10043    check that the next token is legal ( ';' for decls and '{' for defs).  */
10044 
10045 static bool
c_parser_objc_maybe_method_attributes(c_parser * parser,tree * attributes)10046 c_parser_objc_maybe_method_attributes (c_parser* parser, tree* attributes)
10047 {
10048   bool bad = false;
10049   if (*attributes)
10050     {
10051       c_parser_error (parser,
10052 		    "method attributes must be specified at the end only");
10053       *attributes = NULL_TREE;
10054       bad = true;
10055     }
10056 
10057   if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
10058     *attributes = c_parser_attributes (parser);
10059 
10060   /* If there were no attributes here, just report any earlier error.  */
10061   if (*attributes == NULL_TREE || bad)
10062     return bad;
10063 
10064   /* If the attributes are followed by a ; or {, then just report any earlier
10065      error.  */
10066   if (c_parser_next_token_is (parser, CPP_SEMICOLON)
10067       || c_parser_next_token_is (parser, CPP_OPEN_BRACE))
10068     return bad;
10069 
10070   /* We've got attributes, but not at the end.  */
10071   c_parser_error (parser,
10072 		  "expected %<;%> or %<{%> after method attribute definition");
10073   return true;
10074 }
10075 
10076 /* Parse an objc-method-decl.
10077 
10078    objc-method-decl:
10079      ( objc-type-name ) objc-selector
10080      objc-selector
10081      ( objc-type-name ) objc-keyword-selector objc-optparmlist
10082      objc-keyword-selector objc-optparmlist
10083      attributes
10084 
10085    objc-keyword-selector:
10086      objc-keyword-decl
10087      objc-keyword-selector objc-keyword-decl
10088 
10089    objc-keyword-decl:
10090      objc-selector : ( objc-type-name ) identifier
10091      objc-selector : identifier
10092      : ( objc-type-name ) identifier
10093      : identifier
10094 
10095    objc-optparmlist:
10096      objc-optparms objc-optellipsis
10097 
10098    objc-optparms:
10099      empty
10100      objc-opt-parms , parameter-declaration
10101 
10102    objc-optellipsis:
10103      empty
10104      , ...
10105 */
10106 
10107 static tree
c_parser_objc_method_decl(c_parser * parser,bool is_class_method,tree * attributes,tree * expr)10108 c_parser_objc_method_decl (c_parser *parser, bool is_class_method,
10109 			   tree *attributes, tree *expr)
10110 {
10111   tree type = NULL_TREE;
10112   tree sel;
10113   tree parms = NULL_TREE;
10114   bool ellipsis = false;
10115   bool attr_err = false;
10116 
10117   *attributes = NULL_TREE;
10118   if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
10119     {
10120       matching_parens parens;
10121       parens.consume_open (parser);
10122       type = c_parser_objc_type_name (parser);
10123       parens.skip_until_found_close (parser);
10124     }
10125   sel = c_parser_objc_selector (parser);
10126   /* If there is no selector, or a colon follows, we have an
10127      objc-keyword-selector.  If there is a selector, and a colon does
10128      not follow, that selector ends the objc-method-decl.  */
10129   if (!sel || c_parser_next_token_is (parser, CPP_COLON))
10130     {
10131       tree tsel = sel;
10132       tree list = NULL_TREE;
10133       while (true)
10134 	{
10135 	  tree atype = NULL_TREE, id, keyworddecl;
10136 	  tree param_attr = NULL_TREE;
10137 	  if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
10138 	    break;
10139 	  if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
10140 	    {
10141 	      c_parser_consume_token (parser);
10142 	      atype = c_parser_objc_type_name (parser);
10143 	      c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
10144 					 "expected %<)%>");
10145 	    }
10146 	  /* New ObjC allows attributes on method parameters.  */
10147 	  if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
10148 	    param_attr = c_parser_attributes (parser);
10149 	  if (c_parser_next_token_is_not (parser, CPP_NAME))
10150 	    {
10151 	      c_parser_error (parser, "expected identifier");
10152 	      return error_mark_node;
10153 	    }
10154 	  id = c_parser_peek_token (parser)->value;
10155 	  c_parser_consume_token (parser);
10156 	  keyworddecl = objc_build_keyword_decl (tsel, atype, id, param_attr);
10157 	  list = chainon (list, keyworddecl);
10158 	  tsel = c_parser_objc_selector (parser);
10159 	  if (!tsel && c_parser_next_token_is_not (parser, CPP_COLON))
10160 	    break;
10161 	}
10162 
10163       attr_err |= c_parser_objc_maybe_method_attributes (parser, attributes) ;
10164 
10165       /* Parse the optional parameter list.  Optional Objective-C
10166 	 method parameters follow the C syntax, and may include '...'
10167 	 to denote a variable number of arguments.  */
10168       parms = make_node (TREE_LIST);
10169       while (c_parser_next_token_is (parser, CPP_COMMA))
10170 	{
10171 	  struct c_parm *parm;
10172 	  c_parser_consume_token (parser);
10173 	  if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
10174 	    {
10175 	      ellipsis = true;
10176 	      c_parser_consume_token (parser);
10177 	      attr_err |= c_parser_objc_maybe_method_attributes
10178 						(parser, attributes) ;
10179 	      break;
10180 	    }
10181 	  parm = c_parser_parameter_declaration (parser, NULL_TREE);
10182 	  if (parm == NULL)
10183 	    break;
10184 	  parms = chainon (parms,
10185 			   build_tree_list (NULL_TREE, grokparm (parm, expr)));
10186 	}
10187       sel = list;
10188     }
10189   else
10190     attr_err |= c_parser_objc_maybe_method_attributes (parser, attributes) ;
10191 
10192   if (sel == NULL)
10193     {
10194       c_parser_error (parser, "objective-c method declaration is expected");
10195       return error_mark_node;
10196     }
10197 
10198   if (attr_err)
10199     return error_mark_node;
10200 
10201   return objc_build_method_signature (is_class_method, type, sel, parms, ellipsis);
10202 }
10203 
10204 /* Parse an objc-type-name.
10205 
10206    objc-type-name:
10207      objc-type-qualifiers[opt] type-name
10208      objc-type-qualifiers[opt]
10209 
10210    objc-type-qualifiers:
10211      objc-type-qualifier
10212      objc-type-qualifiers objc-type-qualifier
10213 
10214    objc-type-qualifier: one of
10215      in out inout bycopy byref oneway
10216 */
10217 
10218 static tree
c_parser_objc_type_name(c_parser * parser)10219 c_parser_objc_type_name (c_parser *parser)
10220 {
10221   tree quals = NULL_TREE;
10222   struct c_type_name *type_name = NULL;
10223   tree type = NULL_TREE;
10224   while (true)
10225     {
10226       c_token *token = c_parser_peek_token (parser);
10227       if (token->type == CPP_KEYWORD
10228 	  && (token->keyword == RID_IN
10229 	      || token->keyword == RID_OUT
10230 	      || token->keyword == RID_INOUT
10231 	      || token->keyword == RID_BYCOPY
10232 	      || token->keyword == RID_BYREF
10233 	      || token->keyword == RID_ONEWAY))
10234 	{
10235 	  quals = chainon (build_tree_list (NULL_TREE, token->value), quals);
10236 	  c_parser_consume_token (parser);
10237 	}
10238       else
10239 	break;
10240     }
10241   if (c_parser_next_tokens_start_typename (parser, cla_prefer_type))
10242     type_name = c_parser_type_name (parser);
10243   if (type_name)
10244     type = groktypename (type_name, NULL, NULL);
10245 
10246   /* If the type is unknown, and error has already been produced and
10247      we need to recover from the error.  In that case, use NULL_TREE
10248      for the type, as if no type had been specified; this will use the
10249      default type ('id') which is good for error recovery.  */
10250   if (type == error_mark_node)
10251     type = NULL_TREE;
10252 
10253   return build_tree_list (quals, type);
10254 }
10255 
10256 /* Parse objc-protocol-refs.
10257 
10258    objc-protocol-refs:
10259      < identifier-list >
10260 */
10261 
10262 static tree
c_parser_objc_protocol_refs(c_parser * parser)10263 c_parser_objc_protocol_refs (c_parser *parser)
10264 {
10265   tree list = NULL_TREE;
10266   gcc_assert (c_parser_next_token_is (parser, CPP_LESS));
10267   c_parser_consume_token (parser);
10268   /* Any identifiers, including those declared as type names, are OK
10269      here.  */
10270   while (true)
10271     {
10272       tree id;
10273       if (c_parser_next_token_is_not (parser, CPP_NAME))
10274 	{
10275 	  c_parser_error (parser, "expected identifier");
10276 	  break;
10277 	}
10278       id = c_parser_peek_token (parser)->value;
10279       list = chainon (list, build_tree_list (NULL_TREE, id));
10280       c_parser_consume_token (parser);
10281       if (c_parser_next_token_is (parser, CPP_COMMA))
10282 	c_parser_consume_token (parser);
10283       else
10284 	break;
10285     }
10286   c_parser_require (parser, CPP_GREATER, "expected %<>%>");
10287   return list;
10288 }
10289 
10290 /* Parse an objc-try-catch-finally-statement.
10291 
10292    objc-try-catch-finally-statement:
10293      @try compound-statement objc-catch-list[opt]
10294      @try compound-statement objc-catch-list[opt] @finally compound-statement
10295 
10296    objc-catch-list:
10297      @catch ( objc-catch-parameter-declaration ) compound-statement
10298      objc-catch-list @catch ( objc-catch-parameter-declaration ) compound-statement
10299 
10300    objc-catch-parameter-declaration:
10301      parameter-declaration
10302      '...'
10303 
10304    where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
10305 
10306    PS: This function is identical to cp_parser_objc_try_catch_finally_statement
10307    for C++.  Keep them in sync.  */
10308 
10309 static void
c_parser_objc_try_catch_finally_statement(c_parser * parser)10310 c_parser_objc_try_catch_finally_statement (c_parser *parser)
10311 {
10312   location_t location;
10313   tree stmt;
10314 
10315   gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_TRY));
10316   c_parser_consume_token (parser);
10317   location = c_parser_peek_token (parser)->location;
10318   objc_maybe_warn_exceptions (location);
10319   stmt = c_parser_compound_statement (parser);
10320   objc_begin_try_stmt (location, stmt);
10321 
10322   while (c_parser_next_token_is_keyword (parser, RID_AT_CATCH))
10323     {
10324       struct c_parm *parm;
10325       tree parameter_declaration = error_mark_node;
10326       bool seen_open_paren = false;
10327 
10328       c_parser_consume_token (parser);
10329       matching_parens parens;
10330       if (!parens.require_open (parser))
10331 	seen_open_paren = true;
10332       if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
10333 	{
10334 	  /* We have "@catch (...)" (where the '...' are literally
10335 	     what is in the code).  Skip the '...'.
10336 	     parameter_declaration is set to NULL_TREE, and
10337 	     objc_being_catch_clauses() knows that that means
10338 	     '...'.  */
10339 	  c_parser_consume_token (parser);
10340 	  parameter_declaration = NULL_TREE;
10341 	}
10342       else
10343 	{
10344 	  /* We have "@catch (NSException *exception)" or something
10345 	     like that.  Parse the parameter declaration.  */
10346 	  parm = c_parser_parameter_declaration (parser, NULL_TREE);
10347 	  if (parm == NULL)
10348 	    parameter_declaration = error_mark_node;
10349 	  else
10350 	    parameter_declaration = grokparm (parm, NULL);
10351 	}
10352       if (seen_open_paren)
10353 	parens.require_close (parser);
10354       else
10355 	{
10356 	  /* If there was no open parenthesis, we are recovering from
10357 	     an error, and we are trying to figure out what mistake
10358 	     the user has made.  */
10359 
10360 	  /* If there is an immediate closing parenthesis, the user
10361 	     probably forgot the opening one (ie, they typed "@catch
10362 	     NSException *e)".  Parse the closing parenthesis and keep
10363 	     going.  */
10364 	  if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
10365 	    c_parser_consume_token (parser);
10366 
10367 	  /* If these is no immediate closing parenthesis, the user
10368 	     probably doesn't know that parenthesis are required at
10369 	     all (ie, they typed "@catch NSException *e").  So, just
10370 	     forget about the closing parenthesis and keep going.  */
10371 	}
10372       objc_begin_catch_clause (parameter_declaration);
10373       if (c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
10374 	c_parser_compound_statement_nostart (parser);
10375       objc_finish_catch_clause ();
10376     }
10377   if (c_parser_next_token_is_keyword (parser, RID_AT_FINALLY))
10378     {
10379       c_parser_consume_token (parser);
10380       location = c_parser_peek_token (parser)->location;
10381       stmt = c_parser_compound_statement (parser);
10382       objc_build_finally_clause (location, stmt);
10383     }
10384   objc_finish_try_stmt ();
10385 }
10386 
10387 /* Parse an objc-synchronized-statement.
10388 
10389    objc-synchronized-statement:
10390      @synchronized ( expression ) compound-statement
10391 */
10392 
10393 static void
c_parser_objc_synchronized_statement(c_parser * parser)10394 c_parser_objc_synchronized_statement (c_parser *parser)
10395 {
10396   location_t loc;
10397   tree expr, stmt;
10398   gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_SYNCHRONIZED));
10399   c_parser_consume_token (parser);
10400   loc = c_parser_peek_token (parser)->location;
10401   objc_maybe_warn_exceptions (loc);
10402   matching_parens parens;
10403   if (parens.require_open (parser))
10404     {
10405       struct c_expr ce = c_parser_expression (parser);
10406       ce = convert_lvalue_to_rvalue (loc, ce, false, false);
10407       expr = ce.value;
10408       expr = c_fully_fold (expr, false, NULL);
10409       parens.skip_until_found_close (parser);
10410     }
10411   else
10412     expr = error_mark_node;
10413   stmt = c_parser_compound_statement (parser);
10414   objc_build_synchronized (loc, expr, stmt);
10415 }
10416 
10417 /* Parse an objc-selector; return NULL_TREE without an error if the
10418    next token is not an objc-selector.
10419 
10420    objc-selector:
10421      identifier
10422      one of
10423        enum struct union if else while do for switch case default
10424        break continue return goto asm sizeof typeof __alignof
10425        unsigned long const short volatile signed restrict _Complex
10426        in out inout bycopy byref oneway int char float double void _Bool
10427        _Atomic
10428 
10429    ??? Why this selection of keywords but not, for example, storage
10430    class specifiers?  */
10431 
10432 static tree
c_parser_objc_selector(c_parser * parser)10433 c_parser_objc_selector (c_parser *parser)
10434 {
10435   c_token *token = c_parser_peek_token (parser);
10436   tree value = token->value;
10437   if (token->type == CPP_NAME)
10438     {
10439       c_parser_consume_token (parser);
10440       return value;
10441     }
10442   if (token->type != CPP_KEYWORD)
10443     return NULL_TREE;
10444   switch (token->keyword)
10445     {
10446     case RID_ENUM:
10447     case RID_STRUCT:
10448     case RID_UNION:
10449     case RID_IF:
10450     case RID_ELSE:
10451     case RID_WHILE:
10452     case RID_DO:
10453     case RID_FOR:
10454     case RID_SWITCH:
10455     case RID_CASE:
10456     case RID_DEFAULT:
10457     case RID_BREAK:
10458     case RID_CONTINUE:
10459     case RID_RETURN:
10460     case RID_GOTO:
10461     case RID_ASM:
10462     case RID_SIZEOF:
10463     case RID_TYPEOF:
10464     case RID_ALIGNOF:
10465     case RID_UNSIGNED:
10466     case RID_LONG:
10467     case RID_CONST:
10468     case RID_SHORT:
10469     case RID_VOLATILE:
10470     case RID_SIGNED:
10471     case RID_RESTRICT:
10472     case RID_COMPLEX:
10473     case RID_IN:
10474     case RID_OUT:
10475     case RID_INOUT:
10476     case RID_BYCOPY:
10477     case RID_BYREF:
10478     case RID_ONEWAY:
10479     case RID_INT:
10480     case RID_CHAR:
10481     case RID_FLOAT:
10482     case RID_DOUBLE:
10483     CASE_RID_FLOATN_NX:
10484     case RID_VOID:
10485     case RID_BOOL:
10486     case RID_ATOMIC:
10487     case RID_AUTO_TYPE:
10488     case RID_INT_N_0:
10489     case RID_INT_N_1:
10490     case RID_INT_N_2:
10491     case RID_INT_N_3:
10492       c_parser_consume_token (parser);
10493       return value;
10494     default:
10495       return NULL_TREE;
10496     }
10497 }
10498 
10499 /* Parse an objc-selector-arg.
10500 
10501    objc-selector-arg:
10502      objc-selector
10503      objc-keywordname-list
10504 
10505    objc-keywordname-list:
10506      objc-keywordname
10507      objc-keywordname-list objc-keywordname
10508 
10509    objc-keywordname:
10510      objc-selector :
10511      :
10512 */
10513 
10514 static tree
c_parser_objc_selector_arg(c_parser * parser)10515 c_parser_objc_selector_arg (c_parser *parser)
10516 {
10517   tree sel = c_parser_objc_selector (parser);
10518   tree list = NULL_TREE;
10519   if (sel && c_parser_next_token_is_not (parser, CPP_COLON))
10520     return sel;
10521   while (true)
10522     {
10523       if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
10524 	return list;
10525       list = chainon (list, build_tree_list (sel, NULL_TREE));
10526       sel = c_parser_objc_selector (parser);
10527       if (!sel && c_parser_next_token_is_not (parser, CPP_COLON))
10528 	break;
10529     }
10530   return list;
10531 }
10532 
10533 /* Parse an objc-receiver.
10534 
10535    objc-receiver:
10536      expression
10537      class-name
10538      type-name
10539 */
10540 
10541 static tree
c_parser_objc_receiver(c_parser * parser)10542 c_parser_objc_receiver (c_parser *parser)
10543 {
10544   location_t loc = c_parser_peek_token (parser)->location;
10545 
10546   if (c_parser_peek_token (parser)->type == CPP_NAME
10547       && (c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME
10548 	  || c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME))
10549     {
10550       tree id = c_parser_peek_token (parser)->value;
10551       c_parser_consume_token (parser);
10552       return objc_get_class_reference (id);
10553     }
10554   struct c_expr ce = c_parser_expression (parser);
10555   ce = convert_lvalue_to_rvalue (loc, ce, false, false);
10556   return c_fully_fold (ce.value, false, NULL);
10557 }
10558 
10559 /* Parse objc-message-args.
10560 
10561    objc-message-args:
10562      objc-selector
10563      objc-keywordarg-list
10564 
10565    objc-keywordarg-list:
10566      objc-keywordarg
10567      objc-keywordarg-list objc-keywordarg
10568 
10569    objc-keywordarg:
10570      objc-selector : objc-keywordexpr
10571      : objc-keywordexpr
10572 */
10573 
10574 static tree
c_parser_objc_message_args(c_parser * parser)10575 c_parser_objc_message_args (c_parser *parser)
10576 {
10577   tree sel = c_parser_objc_selector (parser);
10578   tree list = NULL_TREE;
10579   if (sel && c_parser_next_token_is_not (parser, CPP_COLON))
10580     return sel;
10581   while (true)
10582     {
10583       tree keywordexpr;
10584       if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
10585 	return error_mark_node;
10586       keywordexpr = c_parser_objc_keywordexpr (parser);
10587       list = chainon (list, build_tree_list (sel, keywordexpr));
10588       sel = c_parser_objc_selector (parser);
10589       if (!sel && c_parser_next_token_is_not (parser, CPP_COLON))
10590 	break;
10591     }
10592   return list;
10593 }
10594 
10595 /* Parse an objc-keywordexpr.
10596 
10597    objc-keywordexpr:
10598      nonempty-expr-list
10599 */
10600 
10601 static tree
c_parser_objc_keywordexpr(c_parser * parser)10602 c_parser_objc_keywordexpr (c_parser *parser)
10603 {
10604   tree ret;
10605   vec<tree, va_gc> *expr_list = c_parser_expr_list (parser, true, true,
10606 						NULL, NULL, NULL, NULL);
10607   if (vec_safe_length (expr_list) == 1)
10608     {
10609       /* Just return the expression, remove a level of
10610 	 indirection.  */
10611       ret = (*expr_list)[0];
10612     }
10613   else
10614     {
10615       /* We have a comma expression, we will collapse later.  */
10616       ret = build_tree_list_vec (expr_list);
10617     }
10618   release_tree_vector (expr_list);
10619   return ret;
10620 }
10621 
10622 /* A check, needed in several places, that ObjC interface, implementation or
10623    method definitions are not prefixed by incorrect items.  */
10624 static bool
c_parser_objc_diagnose_bad_element_prefix(c_parser * parser,struct c_declspecs * specs)10625 c_parser_objc_diagnose_bad_element_prefix (c_parser *parser,
10626 					   struct c_declspecs *specs)
10627 {
10628   if (!specs->declspecs_seen_p || specs->non_sc_seen_p
10629       || specs->typespec_kind != ctsk_none)
10630     {
10631       c_parser_error (parser,
10632       		      "no type or storage class may be specified here,");
10633       c_parser_skip_to_end_of_block_or_statement (parser);
10634       return true;
10635     }
10636   return false;
10637 }
10638 
10639 /* Parse an Objective-C @property declaration.  The syntax is:
10640 
10641    objc-property-declaration:
10642      '@property' objc-property-attributes[opt] struct-declaration ;
10643 
10644    objc-property-attributes:
10645     '(' objc-property-attribute-list ')'
10646 
10647    objc-property-attribute-list:
10648      objc-property-attribute
10649      objc-property-attribute-list, objc-property-attribute
10650 
10651    objc-property-attribute
10652      'getter' = identifier
10653      'setter' = identifier
10654      'readonly'
10655      'readwrite'
10656      'assign'
10657      'retain'
10658      'copy'
10659      'nonatomic'
10660 
10661   For example:
10662     @property NSString *name;
10663     @property (readonly) id object;
10664     @property (retain, nonatomic, getter=getTheName) id name;
10665     @property int a, b, c;
10666 
10667   PS: This function is identical to cp_parser_objc_at_propery_declaration
10668   for C++.  Keep them in sync.  */
10669 static void
c_parser_objc_at_property_declaration(c_parser * parser)10670 c_parser_objc_at_property_declaration (c_parser *parser)
10671 {
10672   /* The following variables hold the attributes of the properties as
10673      parsed.  They are 'false' or 'NULL_TREE' if the attribute was not
10674      seen.  When we see an attribute, we set them to 'true' (if they
10675      are boolean properties) or to the identifier (if they have an
10676      argument, ie, for getter and setter).  Note that here we only
10677      parse the list of attributes, check the syntax and accumulate the
10678      attributes that we find.  objc_add_property_declaration() will
10679      then process the information.  */
10680   bool property_assign = false;
10681   bool property_copy = false;
10682   tree property_getter_ident = NULL_TREE;
10683   bool property_nonatomic = false;
10684   bool property_readonly = false;
10685   bool property_readwrite = false;
10686   bool property_retain = false;
10687   tree property_setter_ident = NULL_TREE;
10688 
10689   /* 'properties' is the list of properties that we read.  Usually a
10690      single one, but maybe more (eg, in "@property int a, b, c;" there
10691      are three).  */
10692   tree properties;
10693   location_t loc;
10694 
10695   loc = c_parser_peek_token (parser)->location;
10696   gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_PROPERTY));
10697 
10698   c_parser_consume_token (parser);  /* Eat '@property'.  */
10699 
10700   /* Parse the optional attribute list...  */
10701   if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
10702     {
10703       matching_parens parens;
10704 
10705       /* Eat the '(' */
10706       parens.consume_open (parser);
10707 
10708       /* Property attribute keywords are valid now.  */
10709       parser->objc_property_attr_context = true;
10710 
10711       while (true)
10712 	{
10713 	  bool syntax_error = false;
10714 	  c_token *token = c_parser_peek_token (parser);
10715 	  enum rid keyword;
10716 
10717 	  if (token->type != CPP_KEYWORD)
10718 	    {
10719 	      if (token->type == CPP_CLOSE_PAREN)
10720 		c_parser_error (parser, "expected identifier");
10721 	      else
10722 		{
10723 		  c_parser_consume_token (parser);
10724 		  c_parser_error (parser, "unknown property attribute");
10725 		}
10726 	      break;
10727 	    }
10728 	  keyword = token->keyword;
10729 	  c_parser_consume_token (parser);
10730 	  switch (keyword)
10731 	    {
10732 	    case RID_ASSIGN:    property_assign = true;    break;
10733 	    case RID_COPY:      property_copy = true;      break;
10734 	    case RID_NONATOMIC: property_nonatomic = true; break;
10735 	    case RID_READONLY:  property_readonly = true;  break;
10736 	    case RID_READWRITE: property_readwrite = true; break;
10737 	    case RID_RETAIN:    property_retain = true;    break;
10738 
10739 	    case RID_GETTER:
10740 	    case RID_SETTER:
10741 	      if (c_parser_next_token_is_not (parser, CPP_EQ))
10742 		{
10743 		  if (keyword == RID_GETTER)
10744 		    c_parser_error (parser,
10745 				    "missing %<=%> (after %<getter%> attribute)");
10746 		  else
10747 		    c_parser_error (parser,
10748 				    "missing %<=%> (after %<setter%> attribute)");
10749 		  syntax_error = true;
10750 		  break;
10751 		}
10752 	      c_parser_consume_token (parser); /* eat the = */
10753 	      if (c_parser_next_token_is_not (parser, CPP_NAME))
10754 		{
10755 		  c_parser_error (parser, "expected identifier");
10756 		  syntax_error = true;
10757 		  break;
10758 		}
10759 	      if (keyword == RID_SETTER)
10760 		{
10761 		  if (property_setter_ident != NULL_TREE)
10762 		    c_parser_error (parser, "the %<setter%> attribute may only be specified once");
10763 		  else
10764 		    property_setter_ident = c_parser_peek_token (parser)->value;
10765 		  c_parser_consume_token (parser);
10766 		  if (c_parser_next_token_is_not (parser, CPP_COLON))
10767 		    c_parser_error (parser, "setter name must terminate with %<:%>");
10768 		  else
10769 		    c_parser_consume_token (parser);
10770 		}
10771 	      else
10772 		{
10773 		  if (property_getter_ident != NULL_TREE)
10774 		    c_parser_error (parser, "the %<getter%> attribute may only be specified once");
10775 		  else
10776 		    property_getter_ident = c_parser_peek_token (parser)->value;
10777 		  c_parser_consume_token (parser);
10778 		}
10779 	      break;
10780 	    default:
10781 	      c_parser_error (parser, "unknown property attribute");
10782 	      syntax_error = true;
10783 	      break;
10784 	    }
10785 
10786 	  if (syntax_error)
10787 	    break;
10788 
10789 	  if (c_parser_next_token_is (parser, CPP_COMMA))
10790 	    c_parser_consume_token (parser);
10791 	  else
10792 	    break;
10793 	}
10794       parser->objc_property_attr_context = false;
10795       parens.skip_until_found_close (parser);
10796     }
10797   /* ... and the property declaration(s).  */
10798   properties = c_parser_struct_declaration (parser);
10799 
10800   if (properties == error_mark_node)
10801     {
10802       c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
10803       parser->error = false;
10804       return;
10805     }
10806 
10807   if (properties == NULL_TREE)
10808     c_parser_error (parser, "expected identifier");
10809   else
10810     {
10811       /* Comma-separated properties are chained together in
10812 	 reverse order; add them one by one.  */
10813       properties = nreverse (properties);
10814 
10815       for (; properties; properties = TREE_CHAIN (properties))
10816 	objc_add_property_declaration (loc, copy_node (properties),
10817 				       property_readonly, property_readwrite,
10818 				       property_assign, property_retain,
10819 				       property_copy, property_nonatomic,
10820 				       property_getter_ident, property_setter_ident);
10821     }
10822 
10823   c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
10824   parser->error = false;
10825 }
10826 
10827 /* Parse an Objective-C @synthesize declaration.  The syntax is:
10828 
10829    objc-synthesize-declaration:
10830      @synthesize objc-synthesize-identifier-list ;
10831 
10832    objc-synthesize-identifier-list:
10833      objc-synthesize-identifier
10834      objc-synthesize-identifier-list, objc-synthesize-identifier
10835 
10836    objc-synthesize-identifier
10837      identifier
10838      identifier = identifier
10839 
10840   For example:
10841     @synthesize MyProperty;
10842     @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
10843 
10844   PS: This function is identical to cp_parser_objc_at_synthesize_declaration
10845   for C++.  Keep them in sync.
10846 */
10847 static void
c_parser_objc_at_synthesize_declaration(c_parser * parser)10848 c_parser_objc_at_synthesize_declaration (c_parser *parser)
10849 {
10850   tree list = NULL_TREE;
10851   location_t loc;
10852   gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_SYNTHESIZE));
10853   loc = c_parser_peek_token (parser)->location;
10854 
10855   c_parser_consume_token (parser);
10856   while (true)
10857     {
10858       tree property, ivar;
10859       if (c_parser_next_token_is_not (parser, CPP_NAME))
10860 	{
10861 	  c_parser_error (parser, "expected identifier");
10862 	  c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
10863 	  /* Once we find the semicolon, we can resume normal parsing.
10864 	     We have to reset parser->error manually because
10865 	     c_parser_skip_until_found() won't reset it for us if the
10866 	     next token is precisely a semicolon.  */
10867 	  parser->error = false;
10868 	  return;
10869 	}
10870       property = c_parser_peek_token (parser)->value;
10871       c_parser_consume_token (parser);
10872       if (c_parser_next_token_is (parser, CPP_EQ))
10873 	{
10874 	  c_parser_consume_token (parser);
10875 	  if (c_parser_next_token_is_not (parser, CPP_NAME))
10876 	    {
10877 	      c_parser_error (parser, "expected identifier");
10878 	      c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
10879 	      parser->error = false;
10880 	      return;
10881 	    }
10882 	  ivar = c_parser_peek_token (parser)->value;
10883 	  c_parser_consume_token (parser);
10884 	}
10885       else
10886 	ivar = NULL_TREE;
10887       list = chainon (list, build_tree_list (ivar, property));
10888       if (c_parser_next_token_is (parser, CPP_COMMA))
10889 	c_parser_consume_token (parser);
10890       else
10891 	break;
10892     }
10893   c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
10894   objc_add_synthesize_declaration (loc, list);
10895 }
10896 
10897 /* Parse an Objective-C @dynamic declaration.  The syntax is:
10898 
10899    objc-dynamic-declaration:
10900      @dynamic identifier-list ;
10901 
10902    For example:
10903      @dynamic MyProperty;
10904      @dynamic MyProperty, AnotherProperty;
10905 
10906   PS: This function is identical to cp_parser_objc_at_dynamic_declaration
10907   for C++.  Keep them in sync.
10908 */
10909 static void
c_parser_objc_at_dynamic_declaration(c_parser * parser)10910 c_parser_objc_at_dynamic_declaration (c_parser *parser)
10911 {
10912   tree list = NULL_TREE;
10913   location_t loc;
10914   gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_DYNAMIC));
10915   loc = c_parser_peek_token (parser)->location;
10916 
10917   c_parser_consume_token (parser);
10918   while (true)
10919     {
10920       tree property;
10921       if (c_parser_next_token_is_not (parser, CPP_NAME))
10922 	{
10923 	  c_parser_error (parser, "expected identifier");
10924 	  c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
10925 	  parser->error = false;
10926 	  return;
10927 	}
10928       property = c_parser_peek_token (parser)->value;
10929       list = chainon (list, build_tree_list (NULL_TREE, property));
10930       c_parser_consume_token (parser);
10931       if (c_parser_next_token_is (parser, CPP_COMMA))
10932 	c_parser_consume_token (parser);
10933       else
10934 	break;
10935     }
10936   c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
10937   objc_add_dynamic_declaration (loc, list);
10938 }
10939 
10940 
10941 /* Parse a pragma GCC ivdep.  */
10942 
10943 static bool
c_parse_pragma_ivdep(c_parser * parser)10944 c_parse_pragma_ivdep (c_parser *parser)
10945 {
10946   c_parser_consume_pragma (parser);
10947   c_parser_skip_to_pragma_eol (parser);
10948   return true;
10949 }
10950 
10951 /* Parse a pragma GCC unroll.  */
10952 
10953 static unsigned short
c_parser_pragma_unroll(c_parser * parser)10954 c_parser_pragma_unroll (c_parser *parser)
10955 {
10956   unsigned short unroll;
10957   c_parser_consume_pragma (parser);
10958   location_t location = c_parser_peek_token (parser)->location;
10959   tree expr = c_parser_expr_no_commas (parser, NULL).value;
10960   mark_exp_read (expr);
10961   expr = c_fully_fold (expr, false, NULL);
10962   HOST_WIDE_INT lunroll = 0;
10963   if (!INTEGRAL_TYPE_P (TREE_TYPE (expr))
10964       || TREE_CODE (expr) != INTEGER_CST
10965       || (lunroll = tree_to_shwi (expr)) < 0
10966       || lunroll >= USHRT_MAX)
10967     {
10968       error_at (location, "%<#pragma GCC unroll%> requires an"
10969 		" assignment-expression that evaluates to a non-negative"
10970 		" integral constant less than %u", USHRT_MAX);
10971       unroll = 0;
10972     }
10973   else
10974     {
10975       unroll = (unsigned short)lunroll;
10976       if (unroll == 0)
10977 	unroll = 1;
10978     }
10979 
10980   c_parser_skip_to_pragma_eol (parser);
10981   return unroll;
10982 }
10983 
10984 /* Handle pragmas.  Some OpenMP pragmas are associated with, and therefore
10985    should be considered, statements.  ALLOW_STMT is true if we're within
10986    the context of a function and such pragmas are to be allowed.  Returns
10987    true if we actually parsed such a pragma.  */
10988 
10989 static bool
c_parser_pragma(c_parser * parser,enum pragma_context context,bool * if_p)10990 c_parser_pragma (c_parser *parser, enum pragma_context context, bool *if_p)
10991 {
10992   unsigned int id;
10993   const char *construct = NULL;
10994 
10995   id = c_parser_peek_token (parser)->pragma_kind;
10996   gcc_assert (id != PRAGMA_NONE);
10997 
10998   switch (id)
10999     {
11000     case PRAGMA_OACC_DECLARE:
11001       c_parser_oacc_declare (parser);
11002       return false;
11003 
11004     case PRAGMA_OACC_ENTER_DATA:
11005       if (context != pragma_compound)
11006 	{
11007 	  construct = "acc enter data";
11008 	in_compound:
11009 	  if (context == pragma_stmt)
11010 	    {
11011 	      error_at (c_parser_peek_token (parser)->location,
11012 			"%<#pragma %s%> may only be used in compound "
11013 			"statements", construct);
11014 	      c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
11015 	      return false;
11016 	    }
11017 	  goto bad_stmt;
11018 	}
11019       c_parser_oacc_enter_exit_data (parser, true);
11020       return false;
11021 
11022     case PRAGMA_OACC_EXIT_DATA:
11023       if (context != pragma_compound)
11024 	{
11025 	  construct = "acc exit data";
11026 	  goto in_compound;
11027 	}
11028       c_parser_oacc_enter_exit_data (parser, false);
11029       return false;
11030 
11031     case PRAGMA_OACC_ROUTINE:
11032       if (context != pragma_external)
11033 	{
11034 	  error_at (c_parser_peek_token (parser)->location,
11035 		    "%<#pragma acc routine%> must be at file scope");
11036 	  c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
11037 	  return false;
11038 	}
11039       c_parser_oacc_routine (parser, context);
11040       return false;
11041 
11042     case PRAGMA_OACC_UPDATE:
11043       if (context != pragma_compound)
11044 	{
11045 	  construct = "acc update";
11046 	  goto in_compound;
11047 	}
11048       c_parser_oacc_update (parser);
11049       return false;
11050 
11051     case PRAGMA_OMP_BARRIER:
11052       if (context != pragma_compound)
11053 	{
11054 	  construct = "omp barrier";
11055 	  goto in_compound;
11056 	}
11057       c_parser_omp_barrier (parser);
11058       return false;
11059 
11060     case PRAGMA_OMP_FLUSH:
11061       if (context != pragma_compound)
11062 	{
11063 	  construct = "omp flush";
11064 	  goto in_compound;
11065 	}
11066       c_parser_omp_flush (parser);
11067       return false;
11068 
11069     case PRAGMA_OMP_TASKWAIT:
11070       if (context != pragma_compound)
11071 	{
11072 	  construct = "omp taskwait";
11073 	  goto in_compound;
11074 	}
11075       c_parser_omp_taskwait (parser);
11076       return false;
11077 
11078     case PRAGMA_OMP_TASKYIELD:
11079       if (context != pragma_compound)
11080 	{
11081 	  construct = "omp taskyield";
11082 	  goto in_compound;
11083 	}
11084       c_parser_omp_taskyield (parser);
11085       return false;
11086 
11087     case PRAGMA_OMP_CANCEL:
11088       if (context != pragma_compound)
11089 	{
11090 	  construct = "omp cancel";
11091 	  goto in_compound;
11092 	}
11093       c_parser_omp_cancel (parser);
11094       return false;
11095 
11096     case PRAGMA_OMP_CANCELLATION_POINT:
11097       c_parser_omp_cancellation_point (parser, context);
11098       return false;
11099 
11100     case PRAGMA_OMP_THREADPRIVATE:
11101       c_parser_omp_threadprivate (parser);
11102       return false;
11103 
11104     case PRAGMA_OMP_TARGET:
11105       return c_parser_omp_target (parser, context, if_p);
11106 
11107     case PRAGMA_OMP_END_DECLARE_TARGET:
11108       c_parser_omp_end_declare_target (parser);
11109       return false;
11110 
11111     case PRAGMA_OMP_SECTION:
11112       error_at (c_parser_peek_token (parser)->location,
11113 		"%<#pragma omp section%> may only be used in "
11114 		"%<#pragma omp sections%> construct");
11115       c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
11116       return false;
11117 
11118     case PRAGMA_OMP_DECLARE:
11119       c_parser_omp_declare (parser, context);
11120       return false;
11121 
11122     case PRAGMA_OMP_ORDERED:
11123       return c_parser_omp_ordered (parser, context, if_p);
11124 
11125     case PRAGMA_IVDEP:
11126       {
11127 	const bool ivdep = c_parse_pragma_ivdep (parser);
11128 	unsigned short unroll;
11129 	if (c_parser_peek_token (parser)->pragma_kind == PRAGMA_UNROLL)
11130 	  unroll = c_parser_pragma_unroll (parser);
11131 	else
11132 	  unroll = 0;
11133 	if (!c_parser_next_token_is_keyword (parser, RID_FOR)
11134 	    && !c_parser_next_token_is_keyword (parser, RID_WHILE)
11135 	    && !c_parser_next_token_is_keyword (parser, RID_DO))
11136 	  {
11137 	    c_parser_error (parser, "for, while or do statement expected");
11138 	    return false;
11139 	  }
11140 	if (c_parser_next_token_is_keyword (parser, RID_FOR))
11141 	  c_parser_for_statement (parser, ivdep, unroll, if_p);
11142 	else if (c_parser_next_token_is_keyword (parser, RID_WHILE))
11143 	  c_parser_while_statement (parser, ivdep, unroll, if_p);
11144 	else
11145 	  c_parser_do_statement (parser, ivdep, unroll);
11146       }
11147       return false;
11148 
11149     case PRAGMA_UNROLL:
11150       {
11151 	unsigned short unroll = c_parser_pragma_unroll (parser);
11152 	bool ivdep;
11153 	if (c_parser_peek_token (parser)->pragma_kind == PRAGMA_IVDEP)
11154 	  ivdep = c_parse_pragma_ivdep (parser);
11155 	else
11156 	  ivdep = false;
11157 	if (!c_parser_next_token_is_keyword (parser, RID_FOR)
11158 	    && !c_parser_next_token_is_keyword (parser, RID_WHILE)
11159 	    && !c_parser_next_token_is_keyword (parser, RID_DO))
11160 	  {
11161 	    c_parser_error (parser, "for, while or do statement expected");
11162 	    return false;
11163 	  }
11164 	if (c_parser_next_token_is_keyword (parser, RID_FOR))
11165 	  c_parser_for_statement (parser, ivdep, unroll, if_p);
11166 	else if (c_parser_next_token_is_keyword (parser, RID_WHILE))
11167 	  c_parser_while_statement (parser, ivdep, unroll, if_p);
11168 	else
11169 	  c_parser_do_statement (parser, ivdep, unroll);
11170       }
11171       return false;
11172 
11173     case PRAGMA_GCC_PCH_PREPROCESS:
11174       c_parser_error (parser, "%<#pragma GCC pch_preprocess%> must be first");
11175       c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
11176       return false;
11177 
11178     case PRAGMA_OACC_WAIT:
11179       if (context != pragma_compound)
11180 	{
11181 	  construct = "acc wait";
11182 	  goto in_compound;
11183 	}
11184 	/* FALL THROUGH.  */
11185 
11186     default:
11187       if (id < PRAGMA_FIRST_EXTERNAL)
11188 	{
11189 	  if (context != pragma_stmt && context != pragma_compound)
11190 	    {
11191 	    bad_stmt:
11192 	      c_parser_error (parser, "expected declaration specifiers");
11193 	      c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
11194 	      return false;
11195 	    }
11196 	  c_parser_omp_construct (parser, if_p);
11197 	  return true;
11198 	}
11199       break;
11200     }
11201 
11202   c_parser_consume_pragma (parser);
11203   c_invoke_pragma_handler (id);
11204 
11205   /* Skip to EOL, but suppress any error message.  Those will have been
11206      generated by the handler routine through calling error, as opposed
11207      to calling c_parser_error.  */
11208   parser->error = true;
11209   c_parser_skip_to_pragma_eol (parser);
11210 
11211   return false;
11212 }
11213 
11214 /* The interface the pragma parsers have to the lexer.  */
11215 
11216 enum cpp_ttype
pragma_lex(tree * value,location_t * loc)11217 pragma_lex (tree *value, location_t *loc)
11218 {
11219   c_token *tok = c_parser_peek_token (the_parser);
11220   enum cpp_ttype ret = tok->type;
11221 
11222   *value = tok->value;
11223   if (loc)
11224     *loc = tok->location;
11225 
11226   if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
11227     ret = CPP_EOF;
11228   else
11229     {
11230       if (ret == CPP_KEYWORD)
11231 	ret = CPP_NAME;
11232       c_parser_consume_token (the_parser);
11233     }
11234 
11235   return ret;
11236 }
11237 
11238 static void
c_parser_pragma_pch_preprocess(c_parser * parser)11239 c_parser_pragma_pch_preprocess (c_parser *parser)
11240 {
11241   tree name = NULL;
11242 
11243   c_parser_consume_pragma (parser);
11244   if (c_parser_next_token_is (parser, CPP_STRING))
11245     {
11246       name = c_parser_peek_token (parser)->value;
11247       c_parser_consume_token (parser);
11248     }
11249   else
11250     c_parser_error (parser, "expected string literal");
11251   c_parser_skip_to_pragma_eol (parser);
11252 
11253   if (name)
11254     c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
11255 }
11256 
11257 /* OpenACC and OpenMP parsing routines.  */
11258 
11259 /* Returns name of the next clause.
11260    If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
11261    the token is not consumed.  Otherwise appropriate pragma_omp_clause is
11262    returned and the token is consumed.  */
11263 
11264 static pragma_omp_clause
c_parser_omp_clause_name(c_parser * parser)11265 c_parser_omp_clause_name (c_parser *parser)
11266 {
11267   pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
11268 
11269   if (c_parser_next_token_is_keyword (parser, RID_AUTO))
11270     result = PRAGMA_OACC_CLAUSE_AUTO;
11271   else if (c_parser_next_token_is_keyword (parser, RID_IF))
11272     result = PRAGMA_OMP_CLAUSE_IF;
11273   else if (c_parser_next_token_is_keyword (parser, RID_DEFAULT))
11274     result = PRAGMA_OMP_CLAUSE_DEFAULT;
11275   else if (c_parser_next_token_is_keyword (parser, RID_FOR))
11276     result = PRAGMA_OMP_CLAUSE_FOR;
11277   else if (c_parser_next_token_is (parser, CPP_NAME))
11278     {
11279       const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
11280 
11281       switch (p[0])
11282 	{
11283 	case 'a':
11284 	  if (!strcmp ("aligned", p))
11285 	    result = PRAGMA_OMP_CLAUSE_ALIGNED;
11286 	  else if (!strcmp ("async", p))
11287 	    result = PRAGMA_OACC_CLAUSE_ASYNC;
11288 	  break;
11289 	case 'c':
11290 	  if (!strcmp ("collapse", p))
11291 	    result = PRAGMA_OMP_CLAUSE_COLLAPSE;
11292 	  else if (!strcmp ("copy", p))
11293 	    result = PRAGMA_OACC_CLAUSE_COPY;
11294 	  else if (!strcmp ("copyin", p))
11295 	    result = PRAGMA_OMP_CLAUSE_COPYIN;
11296 	  else if (!strcmp ("copyout", p))
11297 	    result = PRAGMA_OACC_CLAUSE_COPYOUT;
11298           else if (!strcmp ("copyprivate", p))
11299 	    result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
11300 	  else if (!strcmp ("create", p))
11301 	    result = PRAGMA_OACC_CLAUSE_CREATE;
11302 	  break;
11303 	case 'd':
11304 	  if (!strcmp ("defaultmap", p))
11305 	    result = PRAGMA_OMP_CLAUSE_DEFAULTMAP;
11306 	  else if (!strcmp ("delete", p))
11307 	    result = PRAGMA_OACC_CLAUSE_DELETE;
11308 	  else if (!strcmp ("depend", p))
11309 	    result = PRAGMA_OMP_CLAUSE_DEPEND;
11310 	  else if (!strcmp ("device", p))
11311 	    result = PRAGMA_OMP_CLAUSE_DEVICE;
11312 	  else if (!strcmp ("deviceptr", p))
11313 	    result = PRAGMA_OACC_CLAUSE_DEVICEPTR;
11314 	  else if (!strcmp ("device_resident", p))
11315 	    result = PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT;
11316 	  else if (!strcmp ("dist_schedule", p))
11317 	    result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
11318 	  break;
11319 	case 'f':
11320 	  if (!strcmp ("final", p))
11321 	    result = PRAGMA_OMP_CLAUSE_FINAL;
11322 	  else if (!strcmp ("firstprivate", p))
11323 	    result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
11324 	  else if (!strcmp ("from", p))
11325 	    result = PRAGMA_OMP_CLAUSE_FROM;
11326 	  break;
11327 	case 'g':
11328 	  if (!strcmp ("gang", p))
11329 	    result = PRAGMA_OACC_CLAUSE_GANG;
11330 	  else if (!strcmp ("grainsize", p))
11331 	    result = PRAGMA_OMP_CLAUSE_GRAINSIZE;
11332 	  break;
11333 	case 'h':
11334 	  if (!strcmp ("hint", p))
11335 	    result = PRAGMA_OMP_CLAUSE_HINT;
11336 	  else if (!strcmp ("host", p))
11337 	    result = PRAGMA_OACC_CLAUSE_HOST;
11338 	  break;
11339 	case 'i':
11340 	  if (!strcmp ("inbranch", p))
11341 	    result = PRAGMA_OMP_CLAUSE_INBRANCH;
11342 	  else if (!strcmp ("independent", p))
11343 	    result = PRAGMA_OACC_CLAUSE_INDEPENDENT;
11344 	  else if (!strcmp ("is_device_ptr", p))
11345 	    result = PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR;
11346 	  break;
11347 	case 'l':
11348 	  if (!strcmp ("lastprivate", p))
11349 	    result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
11350 	  else if (!strcmp ("linear", p))
11351 	    result = PRAGMA_OMP_CLAUSE_LINEAR;
11352 	  else if (!strcmp ("link", p))
11353 	    result = PRAGMA_OMP_CLAUSE_LINK;
11354 	  break;
11355 	case 'm':
11356 	  if (!strcmp ("map", p))
11357 	    result = PRAGMA_OMP_CLAUSE_MAP;
11358 	  else if (!strcmp ("mergeable", p))
11359 	    result = PRAGMA_OMP_CLAUSE_MERGEABLE;
11360 	  break;
11361 	case 'n':
11362 	  if (!strcmp ("nogroup", p))
11363 	    result = PRAGMA_OMP_CLAUSE_NOGROUP;
11364 	  else if (!strcmp ("notinbranch", p))
11365 	    result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
11366 	  else if (!strcmp ("nowait", p))
11367 	    result = PRAGMA_OMP_CLAUSE_NOWAIT;
11368 	  else if (!strcmp ("num_gangs", p))
11369 	    result = PRAGMA_OACC_CLAUSE_NUM_GANGS;
11370 	  else if (!strcmp ("num_tasks", p))
11371 	    result = PRAGMA_OMP_CLAUSE_NUM_TASKS;
11372 	  else if (!strcmp ("num_teams", p))
11373 	    result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
11374 	  else if (!strcmp ("num_threads", p))
11375 	    result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
11376 	  else if (!strcmp ("num_workers", p))
11377 	    result = PRAGMA_OACC_CLAUSE_NUM_WORKERS;
11378 	  break;
11379 	case 'o':
11380 	  if (!strcmp ("ordered", p))
11381 	    result = PRAGMA_OMP_CLAUSE_ORDERED;
11382 	  break;
11383 	case 'p':
11384 	  if (!strcmp ("parallel", p))
11385 	    result = PRAGMA_OMP_CLAUSE_PARALLEL;
11386 	  else if (!strcmp ("present", p))
11387 	    result = PRAGMA_OACC_CLAUSE_PRESENT;
11388 	  else if (!strcmp ("present_or_copy", p)
11389 		   || !strcmp ("pcopy", p))
11390 	    result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY;
11391 	  else if (!strcmp ("present_or_copyin", p)
11392 		   || !strcmp ("pcopyin", p))
11393 	    result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN;
11394 	  else if (!strcmp ("present_or_copyout", p)
11395 		   || !strcmp ("pcopyout", p))
11396 	    result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT;
11397 	  else if (!strcmp ("present_or_create", p)
11398 		   || !strcmp ("pcreate", p))
11399 	    result = PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE;
11400 	  else if (!strcmp ("priority", p))
11401 	    result = PRAGMA_OMP_CLAUSE_PRIORITY;
11402 	  else if (!strcmp ("private", p))
11403 	    result = PRAGMA_OMP_CLAUSE_PRIVATE;
11404 	  else if (!strcmp ("proc_bind", p))
11405 	    result = PRAGMA_OMP_CLAUSE_PROC_BIND;
11406 	  break;
11407 	case 'r':
11408 	  if (!strcmp ("reduction", p))
11409 	    result = PRAGMA_OMP_CLAUSE_REDUCTION;
11410 	  break;
11411 	case 's':
11412 	  if (!strcmp ("safelen", p))
11413 	    result = PRAGMA_OMP_CLAUSE_SAFELEN;
11414 	  else if (!strcmp ("schedule", p))
11415 	    result = PRAGMA_OMP_CLAUSE_SCHEDULE;
11416 	  else if (!strcmp ("sections", p))
11417 	    result = PRAGMA_OMP_CLAUSE_SECTIONS;
11418 	  else if (!strcmp ("seq", p))
11419 	    result = PRAGMA_OACC_CLAUSE_SEQ;
11420 	  else if (!strcmp ("shared", p))
11421 	    result = PRAGMA_OMP_CLAUSE_SHARED;
11422 	  else if (!strcmp ("simd", p))
11423 	    result = PRAGMA_OMP_CLAUSE_SIMD;
11424 	  else if (!strcmp ("simdlen", p))
11425 	    result = PRAGMA_OMP_CLAUSE_SIMDLEN;
11426 	  else if (!strcmp ("self", p))
11427 	    result = PRAGMA_OACC_CLAUSE_SELF;
11428 	  break;
11429 	case 't':
11430 	  if (!strcmp ("taskgroup", p))
11431 	    result = PRAGMA_OMP_CLAUSE_TASKGROUP;
11432 	  else if (!strcmp ("thread_limit", p))
11433 	    result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
11434 	  else if (!strcmp ("threads", p))
11435 	    result = PRAGMA_OMP_CLAUSE_THREADS;
11436 	  else if (!strcmp ("tile", p))
11437 	    result = PRAGMA_OACC_CLAUSE_TILE;
11438 	  else if (!strcmp ("to", p))
11439 	    result = PRAGMA_OMP_CLAUSE_TO;
11440 	  break;
11441 	case 'u':
11442 	  if (!strcmp ("uniform", p))
11443 	    result = PRAGMA_OMP_CLAUSE_UNIFORM;
11444 	  else if (!strcmp ("untied", p))
11445 	    result = PRAGMA_OMP_CLAUSE_UNTIED;
11446 	  else if (!strcmp ("use_device", p))
11447 	    result = PRAGMA_OACC_CLAUSE_USE_DEVICE;
11448 	  else if (!strcmp ("use_device_ptr", p))
11449 	    result = PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR;
11450 	  break;
11451 	case 'v':
11452 	  if (!strcmp ("vector", p))
11453 	    result = PRAGMA_OACC_CLAUSE_VECTOR;
11454 	  else if (!strcmp ("vector_length", p))
11455 	    result = PRAGMA_OACC_CLAUSE_VECTOR_LENGTH;
11456 	  break;
11457 	case 'w':
11458 	  if (!strcmp ("wait", p))
11459 	    result = PRAGMA_OACC_CLAUSE_WAIT;
11460 	  else if (!strcmp ("worker", p))
11461 	    result = PRAGMA_OACC_CLAUSE_WORKER;
11462 	  break;
11463 	}
11464     }
11465 
11466   if (result != PRAGMA_OMP_CLAUSE_NONE)
11467     c_parser_consume_token (parser);
11468 
11469   return result;
11470 }
11471 
11472 /* Validate that a clause of the given type does not already exist.  */
11473 
11474 static void
check_no_duplicate_clause(tree clauses,enum omp_clause_code code,const char * name)11475 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
11476 			   const char *name)
11477 {
11478   tree c;
11479 
11480   for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
11481     if (OMP_CLAUSE_CODE (c) == code)
11482       {
11483 	location_t loc = OMP_CLAUSE_LOCATION (c);
11484 	error_at (loc, "too many %qs clauses", name);
11485 	break;
11486       }
11487 }
11488 
11489 /* OpenACC 2.0
11490    Parse wait clause or wait directive parameters.  */
11491 
11492 static tree
c_parser_oacc_wait_list(c_parser * parser,location_t clause_loc,tree list)11493 c_parser_oacc_wait_list (c_parser *parser, location_t clause_loc, tree list)
11494 {
11495   vec<tree, va_gc> *args;
11496   tree t, args_tree;
11497 
11498   matching_parens parens;
11499   if (!parens.require_open (parser))
11500     return list;
11501 
11502   args = c_parser_expr_list (parser, false, true, NULL, NULL, NULL, NULL);
11503 
11504   if (args->length () == 0)
11505     {
11506       c_parser_error (parser, "expected integer expression before ')'");
11507       release_tree_vector (args);
11508       return list;
11509     }
11510 
11511   args_tree = build_tree_list_vec (args);
11512 
11513   for (t = args_tree; t; t = TREE_CHAIN (t))
11514     {
11515       tree targ = TREE_VALUE (t);
11516 
11517       if (targ != error_mark_node)
11518 	{
11519 	  if (!INTEGRAL_TYPE_P (TREE_TYPE (targ)))
11520 	    {
11521 	      c_parser_error (parser, "expression must be integral");
11522 	      targ = error_mark_node;
11523 	    }
11524 	  else
11525 	    {
11526 	      tree c = build_omp_clause (clause_loc, OMP_CLAUSE_WAIT);
11527 
11528 	      OMP_CLAUSE_DECL (c) = targ;
11529 	      OMP_CLAUSE_CHAIN (c) = list;
11530 	      list = c;
11531 	    }
11532 	}
11533     }
11534 
11535   release_tree_vector (args);
11536   parens.require_close (parser);
11537   return list;
11538 }
11539 
11540 /* OpenACC 2.0, OpenMP 2.5:
11541    variable-list:
11542      identifier
11543      variable-list , identifier
11544 
11545    If KIND is nonzero, create the appropriate node and install the
11546    decl in OMP_CLAUSE_DECL and add the node to the head of the list.
11547    If KIND is nonzero, CLAUSE_LOC is the location of the clause.
11548 
11549    If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
11550    return the list created.  */
11551 
11552 static tree
c_parser_omp_variable_list(c_parser * parser,location_t clause_loc,enum omp_clause_code kind,tree list)11553 c_parser_omp_variable_list (c_parser *parser,
11554 			    location_t clause_loc,
11555 			    enum omp_clause_code kind, tree list)
11556 {
11557   if (c_parser_next_token_is_not (parser, CPP_NAME)
11558       || c_parser_peek_token (parser)->id_kind != C_ID_ID)
11559     c_parser_error (parser, "expected identifier");
11560 
11561   while (c_parser_next_token_is (parser, CPP_NAME)
11562 	 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
11563     {
11564       tree t = lookup_name (c_parser_peek_token (parser)->value);
11565 
11566       if (t == NULL_TREE)
11567 	{
11568 	  undeclared_variable (c_parser_peek_token (parser)->location,
11569 			       c_parser_peek_token (parser)->value);
11570 	  t = error_mark_node;
11571 	}
11572 
11573       c_parser_consume_token (parser);
11574 
11575       if (t == error_mark_node)
11576 	;
11577       else if (kind != 0)
11578 	{
11579 	  switch (kind)
11580 	    {
11581 	    case OMP_CLAUSE__CACHE_:
11582 	      /* The OpenACC cache directive explicitly only allows "array
11583 		 elements or subarrays".  */
11584 	      if (c_parser_peek_token (parser)->type != CPP_OPEN_SQUARE)
11585 		{
11586 		  c_parser_error (parser, "expected %<[%>");
11587 		  t = error_mark_node;
11588 		  break;
11589 		}
11590 	      /* FALLTHROUGH  */
11591 	    case OMP_CLAUSE_MAP:
11592 	    case OMP_CLAUSE_FROM:
11593 	    case OMP_CLAUSE_TO:
11594 	      while (c_parser_next_token_is (parser, CPP_DOT))
11595 		{
11596 		  location_t op_loc = c_parser_peek_token (parser)->location;
11597 		  c_parser_consume_token (parser);
11598 		  if (!c_parser_next_token_is (parser, CPP_NAME))
11599 		    {
11600 		      c_parser_error (parser, "expected identifier");
11601 		      t = error_mark_node;
11602 		      break;
11603 		    }
11604 
11605 		  c_token *comp_tok = c_parser_peek_token (parser);
11606 		  tree ident = comp_tok->value;
11607 		  location_t comp_loc = comp_tok->location;
11608 		  c_parser_consume_token (parser);
11609 		  t = build_component_ref (op_loc, t, ident, comp_loc);
11610 		}
11611 	      /* FALLTHROUGH  */
11612 	    case OMP_CLAUSE_DEPEND:
11613 	    case OMP_CLAUSE_REDUCTION:
11614 	      while (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
11615 		{
11616 		  tree low_bound = NULL_TREE, length = NULL_TREE;
11617 
11618 		  c_parser_consume_token (parser);
11619 		  if (!c_parser_next_token_is (parser, CPP_COLON))
11620 		    {
11621 		      location_t expr_loc
11622 			= c_parser_peek_token (parser)->location;
11623 		      c_expr expr = c_parser_expression (parser);
11624 		      expr = convert_lvalue_to_rvalue (expr_loc, expr,
11625 						       false, true);
11626 		      low_bound = expr.value;
11627 		    }
11628 		  if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
11629 		    length = integer_one_node;
11630 		  else
11631 		    {
11632 		      /* Look for `:'.  */
11633 		      if (!c_parser_require (parser, CPP_COLON,
11634 					     "expected %<:%>"))
11635 			{
11636 			  t = error_mark_node;
11637 			  break;
11638 			}
11639 		      if (!c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
11640 			{
11641 			  location_t expr_loc
11642 			    = c_parser_peek_token (parser)->location;
11643 			  c_expr expr = c_parser_expression (parser);
11644 			  expr = convert_lvalue_to_rvalue (expr_loc, expr,
11645 							   false, true);
11646 			  length = expr.value;
11647 			}
11648 		    }
11649 		  /* Look for the closing `]'.  */
11650 		  if (!c_parser_require (parser, CPP_CLOSE_SQUARE,
11651 					 "expected %<]%>"))
11652 		    {
11653 		      t = error_mark_node;
11654 		      break;
11655 		    }
11656 
11657 		  t = tree_cons (low_bound, length, t);
11658 		}
11659 	      break;
11660 	    default:
11661 	      break;
11662 	    }
11663 
11664 	  if (t != error_mark_node)
11665 	    {
11666 	      tree u = build_omp_clause (clause_loc, kind);
11667 	      OMP_CLAUSE_DECL (u) = t;
11668 	      OMP_CLAUSE_CHAIN (u) = list;
11669 	      list = u;
11670 	    }
11671 	}
11672       else
11673 	list = tree_cons (t, NULL_TREE, list);
11674 
11675       if (c_parser_next_token_is_not (parser, CPP_COMMA))
11676 	break;
11677 
11678       c_parser_consume_token (parser);
11679     }
11680 
11681   return list;
11682 }
11683 
11684 /* Similarly, but expect leading and trailing parenthesis.  This is a very
11685    common case for OpenACC and OpenMP clauses.  */
11686 
11687 static tree
c_parser_omp_var_list_parens(c_parser * parser,enum omp_clause_code kind,tree list)11688 c_parser_omp_var_list_parens (c_parser *parser, enum omp_clause_code kind,
11689 			      tree list)
11690 {
11691   /* The clauses location.  */
11692   location_t loc = c_parser_peek_token (parser)->location;
11693 
11694   matching_parens parens;
11695   if (parens.require_open (parser))
11696     {
11697       list = c_parser_omp_variable_list (parser, loc, kind, list);
11698       parens.skip_until_found_close (parser);
11699     }
11700   return list;
11701 }
11702 
11703 /* OpenACC 2.0:
11704    copy ( variable-list )
11705    copyin ( variable-list )
11706    copyout ( variable-list )
11707    create ( variable-list )
11708    delete ( variable-list )
11709    present ( variable-list )
11710    present_or_copy ( variable-list )
11711      pcopy ( variable-list )
11712    present_or_copyin ( variable-list )
11713      pcopyin ( variable-list )
11714    present_or_copyout ( variable-list )
11715      pcopyout ( variable-list )
11716    present_or_create ( variable-list )
11717      pcreate ( variable-list ) */
11718 
11719 static tree
c_parser_oacc_data_clause(c_parser * parser,pragma_omp_clause c_kind,tree list)11720 c_parser_oacc_data_clause (c_parser *parser, pragma_omp_clause c_kind,
11721 			   tree list)
11722 {
11723   enum gomp_map_kind kind;
11724   switch (c_kind)
11725     {
11726     case PRAGMA_OACC_CLAUSE_COPY:
11727       kind = GOMP_MAP_FORCE_TOFROM;
11728       break;
11729     case PRAGMA_OACC_CLAUSE_COPYIN:
11730       kind = GOMP_MAP_FORCE_TO;
11731       break;
11732     case PRAGMA_OACC_CLAUSE_COPYOUT:
11733       kind = GOMP_MAP_FORCE_FROM;
11734       break;
11735     case PRAGMA_OACC_CLAUSE_CREATE:
11736       kind = GOMP_MAP_FORCE_ALLOC;
11737       break;
11738     case PRAGMA_OACC_CLAUSE_DELETE:
11739       kind = GOMP_MAP_DELETE;
11740       break;
11741     case PRAGMA_OACC_CLAUSE_DEVICE:
11742       kind = GOMP_MAP_FORCE_TO;
11743       break;
11744     case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
11745       kind = GOMP_MAP_DEVICE_RESIDENT;
11746       break;
11747     case PRAGMA_OACC_CLAUSE_HOST:
11748     case PRAGMA_OACC_CLAUSE_SELF:
11749       kind = GOMP_MAP_FORCE_FROM;
11750       break;
11751     case PRAGMA_OACC_CLAUSE_LINK:
11752       kind = GOMP_MAP_LINK;
11753       break;
11754     case PRAGMA_OACC_CLAUSE_PRESENT:
11755       kind = GOMP_MAP_FORCE_PRESENT;
11756       break;
11757     case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
11758       kind = GOMP_MAP_TOFROM;
11759       break;
11760     case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
11761       kind = GOMP_MAP_TO;
11762       break;
11763     case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
11764       kind = GOMP_MAP_FROM;
11765       break;
11766     case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
11767       kind = GOMP_MAP_ALLOC;
11768       break;
11769     default:
11770       gcc_unreachable ();
11771     }
11772   tree nl, c;
11773   nl = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_MAP, list);
11774 
11775   for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
11776     OMP_CLAUSE_SET_MAP_KIND (c, kind);
11777 
11778   return nl;
11779 }
11780 
11781 /* OpenACC 2.0:
11782    deviceptr ( variable-list ) */
11783 
11784 static tree
c_parser_oacc_data_clause_deviceptr(c_parser * parser,tree list)11785 c_parser_oacc_data_clause_deviceptr (c_parser *parser, tree list)
11786 {
11787   location_t loc = c_parser_peek_token (parser)->location;
11788   tree vars, t;
11789 
11790   /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
11791      c_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
11792      variable-list must only allow for pointer variables.  */
11793   vars = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
11794   for (t = vars; t && t; t = TREE_CHAIN (t))
11795     {
11796       tree v = TREE_PURPOSE (t);
11797 
11798       /* FIXME diagnostics: Ideally we should keep individual
11799 	 locations for all the variables in the var list to make the
11800 	 following errors more precise.  Perhaps
11801 	 c_parser_omp_var_list_parens() should construct a list of
11802 	 locations to go along with the var list.  */
11803 
11804       if (!VAR_P (v) && TREE_CODE (v) != PARM_DECL)
11805 	error_at (loc, "%qD is not a variable", v);
11806       else if (TREE_TYPE (v) == error_mark_node)
11807 	;
11808       else if (!POINTER_TYPE_P (TREE_TYPE (v)))
11809 	error_at (loc, "%qD is not a pointer variable", v);
11810 
11811       tree u = build_omp_clause (loc, OMP_CLAUSE_MAP);
11812       OMP_CLAUSE_SET_MAP_KIND (u, GOMP_MAP_FORCE_DEVICEPTR);
11813       OMP_CLAUSE_DECL (u) = v;
11814       OMP_CLAUSE_CHAIN (u) = list;
11815       list = u;
11816     }
11817 
11818   return list;
11819 }
11820 
11821 /* OpenACC 2.0, OpenMP 3.0:
11822    collapse ( constant-expression ) */
11823 
11824 static tree
c_parser_omp_clause_collapse(c_parser * parser,tree list)11825 c_parser_omp_clause_collapse (c_parser *parser, tree list)
11826 {
11827   tree c, num = error_mark_node;
11828   HOST_WIDE_INT n;
11829   location_t loc;
11830 
11831   check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse");
11832   check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile");
11833 
11834   loc = c_parser_peek_token (parser)->location;
11835   matching_parens parens;
11836   if (parens.require_open (parser))
11837     {
11838       num = c_parser_expr_no_commas (parser, NULL).value;
11839       parens.skip_until_found_close (parser);
11840     }
11841   if (num == error_mark_node)
11842     return list;
11843   mark_exp_read (num);
11844   num = c_fully_fold (num, false, NULL);
11845   if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
11846       || !tree_fits_shwi_p (num)
11847       || (n = tree_to_shwi (num)) <= 0
11848       || (int) n != n)
11849     {
11850       error_at (loc,
11851 		"collapse argument needs positive constant integer expression");
11852       return list;
11853     }
11854   c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
11855   OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
11856   OMP_CLAUSE_CHAIN (c) = list;
11857   return c;
11858 }
11859 
11860 /* OpenMP 2.5:
11861    copyin ( variable-list ) */
11862 
11863 static tree
c_parser_omp_clause_copyin(c_parser * parser,tree list)11864 c_parser_omp_clause_copyin (c_parser *parser, tree list)
11865 {
11866   return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_COPYIN, list);
11867 }
11868 
11869 /* OpenMP 2.5:
11870    copyprivate ( variable-list ) */
11871 
11872 static tree
c_parser_omp_clause_copyprivate(c_parser * parser,tree list)11873 c_parser_omp_clause_copyprivate (c_parser *parser, tree list)
11874 {
11875   return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_COPYPRIVATE, list);
11876 }
11877 
11878 /* OpenMP 2.5:
11879    default ( none | shared )
11880 
11881    OpenACC:
11882    default ( none | present ) */
11883 
11884 static tree
c_parser_omp_clause_default(c_parser * parser,tree list,bool is_oacc)11885 c_parser_omp_clause_default (c_parser *parser, tree list, bool is_oacc)
11886 {
11887   enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
11888   location_t loc = c_parser_peek_token (parser)->location;
11889   tree c;
11890 
11891   matching_parens parens;
11892   if (!parens.require_open (parser))
11893     return list;
11894   if (c_parser_next_token_is (parser, CPP_NAME))
11895     {
11896       const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
11897 
11898       switch (p[0])
11899 	{
11900 	case 'n':
11901 	  if (strcmp ("none", p) != 0)
11902 	    goto invalid_kind;
11903 	  kind = OMP_CLAUSE_DEFAULT_NONE;
11904 	  break;
11905 
11906 	case 'p':
11907 	  if (strcmp ("present", p) != 0 || !is_oacc)
11908 	    goto invalid_kind;
11909 	  kind = OMP_CLAUSE_DEFAULT_PRESENT;
11910 	  break;
11911 
11912 	case 's':
11913 	  if (strcmp ("shared", p) != 0 || is_oacc)
11914 	    goto invalid_kind;
11915 	  kind = OMP_CLAUSE_DEFAULT_SHARED;
11916 	  break;
11917 
11918 	default:
11919 	  goto invalid_kind;
11920 	}
11921 
11922       c_parser_consume_token (parser);
11923     }
11924   else
11925     {
11926     invalid_kind:
11927       if (is_oacc)
11928 	c_parser_error (parser, "expected %<none%> or %<present%>");
11929       else
11930 	c_parser_error (parser, "expected %<none%> or %<shared%>");
11931     }
11932   parens.skip_until_found_close (parser);
11933 
11934   if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
11935     return list;
11936 
11937   check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default");
11938   c = build_omp_clause (loc, OMP_CLAUSE_DEFAULT);
11939   OMP_CLAUSE_CHAIN (c) = list;
11940   OMP_CLAUSE_DEFAULT_KIND (c) = kind;
11941 
11942   return c;
11943 }
11944 
11945 /* OpenMP 2.5:
11946    firstprivate ( variable-list ) */
11947 
11948 static tree
c_parser_omp_clause_firstprivate(c_parser * parser,tree list)11949 c_parser_omp_clause_firstprivate (c_parser *parser, tree list)
11950 {
11951   return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_FIRSTPRIVATE, list);
11952 }
11953 
11954 /* OpenMP 3.1:
11955    final ( expression ) */
11956 
11957 static tree
c_parser_omp_clause_final(c_parser * parser,tree list)11958 c_parser_omp_clause_final (c_parser *parser, tree list)
11959 {
11960   location_t loc = c_parser_peek_token (parser)->location;
11961   if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
11962     {
11963       tree t = c_parser_paren_condition (parser);
11964       tree c;
11965 
11966       check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final");
11967 
11968       c = build_omp_clause (loc, OMP_CLAUSE_FINAL);
11969       OMP_CLAUSE_FINAL_EXPR (c) = t;
11970       OMP_CLAUSE_CHAIN (c) = list;
11971       list = c;
11972     }
11973   else
11974     c_parser_error (parser, "expected %<(%>");
11975 
11976   return list;
11977 }
11978 
11979 /* OpenACC, OpenMP 2.5:
11980    if ( expression )
11981 
11982    OpenMP 4.5:
11983    if ( directive-name-modifier : expression )
11984 
11985    directive-name-modifier:
11986      parallel | task | taskloop | target data | target | target update
11987      | target enter data | target exit data  */
11988 
11989 static tree
c_parser_omp_clause_if(c_parser * parser,tree list,bool is_omp)11990 c_parser_omp_clause_if (c_parser *parser, tree list, bool is_omp)
11991 {
11992   location_t location = c_parser_peek_token (parser)->location;
11993   enum tree_code if_modifier = ERROR_MARK;
11994 
11995   matching_parens parens;
11996   if (!parens.require_open (parser))
11997     return list;
11998 
11999   if (is_omp && c_parser_next_token_is (parser, CPP_NAME))
12000     {
12001       const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
12002       int n = 2;
12003       if (strcmp (p, "parallel") == 0)
12004 	if_modifier = OMP_PARALLEL;
12005       else if (strcmp (p, "task") == 0)
12006 	if_modifier = OMP_TASK;
12007       else if (strcmp (p, "taskloop") == 0)
12008 	if_modifier = OMP_TASKLOOP;
12009       else if (strcmp (p, "target") == 0)
12010 	{
12011 	  if_modifier = OMP_TARGET;
12012 	  if (c_parser_peek_2nd_token (parser)->type == CPP_NAME)
12013 	    {
12014 	      p = IDENTIFIER_POINTER (c_parser_peek_2nd_token (parser)->value);
12015 	      if (strcmp ("data", p) == 0)
12016 		if_modifier = OMP_TARGET_DATA;
12017 	      else if (strcmp ("update", p) == 0)
12018 		if_modifier = OMP_TARGET_UPDATE;
12019 	      else if (strcmp ("enter", p) == 0)
12020 		if_modifier = OMP_TARGET_ENTER_DATA;
12021 	      else if (strcmp ("exit", p) == 0)
12022 		if_modifier = OMP_TARGET_EXIT_DATA;
12023 	      if (if_modifier != OMP_TARGET)
12024 		{
12025 		  n = 3;
12026 		  c_parser_consume_token (parser);
12027 		}
12028 	      else
12029 		{
12030 		  location_t loc = c_parser_peek_2nd_token (parser)->location;
12031 		  error_at (loc, "expected %<data%>, %<update%>, %<enter%> "
12032 				 "or %<exit%>");
12033 		  if_modifier = ERROR_MARK;
12034 		}
12035 	      if (if_modifier == OMP_TARGET_ENTER_DATA
12036 		  || if_modifier == OMP_TARGET_EXIT_DATA)
12037 		{
12038 		  if (c_parser_peek_2nd_token (parser)->type == CPP_NAME)
12039 		    {
12040 		      p = IDENTIFIER_POINTER
12041 				(c_parser_peek_2nd_token (parser)->value);
12042 		      if (strcmp ("data", p) == 0)
12043 			n = 4;
12044 		    }
12045 		  if (n == 4)
12046 		    c_parser_consume_token (parser);
12047 		  else
12048 		    {
12049 		      location_t loc
12050 			= c_parser_peek_2nd_token (parser)->location;
12051 		      error_at (loc, "expected %<data%>");
12052 		      if_modifier = ERROR_MARK;
12053 		    }
12054 		}
12055 	    }
12056 	}
12057       if (if_modifier != ERROR_MARK)
12058 	{
12059 	  if (c_parser_peek_2nd_token (parser)->type == CPP_COLON)
12060 	    {
12061 	      c_parser_consume_token (parser);
12062 	      c_parser_consume_token (parser);
12063 	    }
12064 	  else
12065 	    {
12066 	      if (n > 2)
12067 		{
12068 		  location_t loc = c_parser_peek_2nd_token (parser)->location;
12069 		  error_at (loc, "expected %<:%>");
12070 		}
12071 	      if_modifier = ERROR_MARK;
12072 	    }
12073 	}
12074     }
12075 
12076   tree t = c_parser_condition (parser), c;
12077   parens.skip_until_found_close (parser);
12078 
12079   for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
12080     if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IF)
12081       {
12082 	if (if_modifier != ERROR_MARK
12083 	    && OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
12084 	  {
12085 	    const char *p = NULL;
12086 	    switch (if_modifier)
12087 	      {
12088 	      case OMP_PARALLEL: p = "parallel"; break;
12089 	      case OMP_TASK: p = "task"; break;
12090 	      case OMP_TASKLOOP: p = "taskloop"; break;
12091 	      case OMP_TARGET_DATA: p = "target data"; break;
12092 	      case OMP_TARGET: p = "target"; break;
12093 	      case OMP_TARGET_UPDATE: p = "target update"; break;
12094 	      case OMP_TARGET_ENTER_DATA: p = "enter data"; break;
12095 	      case OMP_TARGET_EXIT_DATA: p = "exit data"; break;
12096 	      default: gcc_unreachable ();
12097 	      }
12098 	    error_at (location, "too many %<if%> clauses with %qs modifier",
12099 		      p);
12100 	    return list;
12101 	  }
12102 	else if (OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
12103 	  {
12104 	    if (!is_omp)
12105 	      error_at (location, "too many %<if%> clauses");
12106 	    else
12107 	      error_at (location, "too many %<if%> clauses without modifier");
12108 	    return list;
12109 	  }
12110 	else if (if_modifier == ERROR_MARK
12111 		 || OMP_CLAUSE_IF_MODIFIER (c) == ERROR_MARK)
12112 	  {
12113 	    error_at (location, "if any %<if%> clause has modifier, then all "
12114 				"%<if%> clauses have to use modifier");
12115 	    return list;
12116 	  }
12117       }
12118 
12119   c = build_omp_clause (location, OMP_CLAUSE_IF);
12120   OMP_CLAUSE_IF_MODIFIER (c) = if_modifier;
12121   OMP_CLAUSE_IF_EXPR (c) = t;
12122   OMP_CLAUSE_CHAIN (c) = list;
12123   return c;
12124 }
12125 
12126 /* OpenMP 2.5:
12127    lastprivate ( variable-list ) */
12128 
12129 static tree
c_parser_omp_clause_lastprivate(c_parser * parser,tree list)12130 c_parser_omp_clause_lastprivate (c_parser *parser, tree list)
12131 {
12132   return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_LASTPRIVATE, list);
12133 }
12134 
12135 /* OpenMP 3.1:
12136    mergeable */
12137 
12138 static tree
c_parser_omp_clause_mergeable(c_parser * parser ATTRIBUTE_UNUSED,tree list)12139 c_parser_omp_clause_mergeable (c_parser *parser ATTRIBUTE_UNUSED, tree list)
12140 {
12141   tree c;
12142 
12143   /* FIXME: Should we allow duplicates?  */
12144   check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable");
12145 
12146   c = build_omp_clause (c_parser_peek_token (parser)->location,
12147 			OMP_CLAUSE_MERGEABLE);
12148   OMP_CLAUSE_CHAIN (c) = list;
12149 
12150   return c;
12151 }
12152 
12153 /* OpenMP 2.5:
12154    nowait */
12155 
12156 static tree
c_parser_omp_clause_nowait(c_parser * parser ATTRIBUTE_UNUSED,tree list)12157 c_parser_omp_clause_nowait (c_parser *parser ATTRIBUTE_UNUSED, tree list)
12158 {
12159   tree c;
12160   location_t loc = c_parser_peek_token (parser)->location;
12161 
12162   check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait");
12163 
12164   c = build_omp_clause (loc, OMP_CLAUSE_NOWAIT);
12165   OMP_CLAUSE_CHAIN (c) = list;
12166   return c;
12167 }
12168 
12169 /* OpenMP 2.5:
12170    num_threads ( expression ) */
12171 
12172 static tree
c_parser_omp_clause_num_threads(c_parser * parser,tree list)12173 c_parser_omp_clause_num_threads (c_parser *parser, tree list)
12174 {
12175   location_t num_threads_loc = c_parser_peek_token (parser)->location;
12176   matching_parens parens;
12177   if (parens.require_open (parser))
12178     {
12179       location_t expr_loc = c_parser_peek_token (parser)->location;
12180       c_expr expr = c_parser_expression (parser);
12181       expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
12182       tree c, t = expr.value;
12183       t = c_fully_fold (t, false, NULL);
12184 
12185       parens.skip_until_found_close (parser);
12186 
12187       if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
12188 	{
12189 	  c_parser_error (parser, "expected integer expression");
12190 	  return list;
12191 	}
12192 
12193       /* Attempt to statically determine when the number isn't positive.  */
12194       c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
12195 		       build_int_cst (TREE_TYPE (t), 0));
12196       protected_set_expr_location (c, expr_loc);
12197       if (c == boolean_true_node)
12198 	{
12199 	  warning_at (expr_loc, 0,
12200 		      "%<num_threads%> value must be positive");
12201 	  t = integer_one_node;
12202 	}
12203 
12204       check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS, "num_threads");
12205 
12206       c = build_omp_clause (num_threads_loc, OMP_CLAUSE_NUM_THREADS);
12207       OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
12208       OMP_CLAUSE_CHAIN (c) = list;
12209       list = c;
12210     }
12211 
12212   return list;
12213 }
12214 
12215 /* OpenMP 4.5:
12216    num_tasks ( expression ) */
12217 
12218 static tree
c_parser_omp_clause_num_tasks(c_parser * parser,tree list)12219 c_parser_omp_clause_num_tasks (c_parser *parser, tree list)
12220 {
12221   location_t num_tasks_loc = c_parser_peek_token (parser)->location;
12222   matching_parens parens;
12223   if (parens.require_open (parser))
12224     {
12225       location_t expr_loc = c_parser_peek_token (parser)->location;
12226       c_expr expr = c_parser_expression (parser);
12227       expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
12228       tree c, t = expr.value;
12229       t = c_fully_fold (t, false, NULL);
12230 
12231       parens.skip_until_found_close (parser);
12232 
12233       if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
12234 	{
12235 	  c_parser_error (parser, "expected integer expression");
12236 	  return list;
12237 	}
12238 
12239       /* Attempt to statically determine when the number isn't positive.  */
12240       c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
12241 			   build_int_cst (TREE_TYPE (t), 0));
12242       if (CAN_HAVE_LOCATION_P (c))
12243 	SET_EXPR_LOCATION (c, expr_loc);
12244       if (c == boolean_true_node)
12245 	{
12246 	  warning_at (expr_loc, 0, "%<num_tasks%> value must be positive");
12247 	  t = integer_one_node;
12248 	}
12249 
12250       check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TASKS, "num_tasks");
12251 
12252       c = build_omp_clause (num_tasks_loc, OMP_CLAUSE_NUM_TASKS);
12253       OMP_CLAUSE_NUM_TASKS_EXPR (c) = t;
12254       OMP_CLAUSE_CHAIN (c) = list;
12255       list = c;
12256     }
12257 
12258   return list;
12259 }
12260 
12261 /* OpenMP 4.5:
12262    grainsize ( expression ) */
12263 
12264 static tree
c_parser_omp_clause_grainsize(c_parser * parser,tree list)12265 c_parser_omp_clause_grainsize (c_parser *parser, tree list)
12266 {
12267   location_t grainsize_loc = c_parser_peek_token (parser)->location;
12268   matching_parens parens;
12269   if (parens.require_open (parser))
12270     {
12271       location_t expr_loc = c_parser_peek_token (parser)->location;
12272       c_expr expr = c_parser_expression (parser);
12273       expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
12274       tree c, t = expr.value;
12275       t = c_fully_fold (t, false, NULL);
12276 
12277       parens.skip_until_found_close (parser);
12278 
12279       if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
12280 	{
12281 	  c_parser_error (parser, "expected integer expression");
12282 	  return list;
12283 	}
12284 
12285       /* Attempt to statically determine when the number isn't positive.  */
12286       c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
12287 			   build_int_cst (TREE_TYPE (t), 0));
12288       if (CAN_HAVE_LOCATION_P (c))
12289 	SET_EXPR_LOCATION (c, expr_loc);
12290       if (c == boolean_true_node)
12291 	{
12292 	  warning_at (expr_loc, 0, "%<grainsize%> value must be positive");
12293 	  t = integer_one_node;
12294 	}
12295 
12296       check_no_duplicate_clause (list, OMP_CLAUSE_GRAINSIZE, "grainsize");
12297 
12298       c = build_omp_clause (grainsize_loc, OMP_CLAUSE_GRAINSIZE);
12299       OMP_CLAUSE_GRAINSIZE_EXPR (c) = t;
12300       OMP_CLAUSE_CHAIN (c) = list;
12301       list = c;
12302     }
12303 
12304   return list;
12305 }
12306 
12307 /* OpenMP 4.5:
12308    priority ( expression ) */
12309 
12310 static tree
c_parser_omp_clause_priority(c_parser * parser,tree list)12311 c_parser_omp_clause_priority (c_parser *parser, tree list)
12312 {
12313   location_t priority_loc = c_parser_peek_token (parser)->location;
12314   matching_parens parens;
12315   if (parens.require_open (parser))
12316     {
12317       location_t expr_loc = c_parser_peek_token (parser)->location;
12318       c_expr expr = c_parser_expression (parser);
12319       expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
12320       tree c, t = expr.value;
12321       t = c_fully_fold (t, false, NULL);
12322 
12323       parens.skip_until_found_close (parser);
12324 
12325       if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
12326 	{
12327 	  c_parser_error (parser, "expected integer expression");
12328 	  return list;
12329 	}
12330 
12331       /* Attempt to statically determine when the number isn't
12332 	 non-negative.  */
12333       c = fold_build2_loc (expr_loc, LT_EXPR, boolean_type_node, t,
12334 			   build_int_cst (TREE_TYPE (t), 0));
12335       if (CAN_HAVE_LOCATION_P (c))
12336 	SET_EXPR_LOCATION (c, expr_loc);
12337       if (c == boolean_true_node)
12338 	{
12339 	  warning_at (expr_loc, 0, "%<priority%> value must be non-negative");
12340 	  t = integer_one_node;
12341 	}
12342 
12343       check_no_duplicate_clause (list, OMP_CLAUSE_PRIORITY, "priority");
12344 
12345       c = build_omp_clause (priority_loc, OMP_CLAUSE_PRIORITY);
12346       OMP_CLAUSE_PRIORITY_EXPR (c) = t;
12347       OMP_CLAUSE_CHAIN (c) = list;
12348       list = c;
12349     }
12350 
12351   return list;
12352 }
12353 
12354 /* OpenMP 4.5:
12355    hint ( expression ) */
12356 
12357 static tree
c_parser_omp_clause_hint(c_parser * parser,tree list)12358 c_parser_omp_clause_hint (c_parser *parser, tree list)
12359 {
12360   location_t hint_loc = c_parser_peek_token (parser)->location;
12361   matching_parens parens;
12362   if (parens.require_open (parser))
12363     {
12364       location_t expr_loc = c_parser_peek_token (parser)->location;
12365       c_expr expr = c_parser_expression (parser);
12366       expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
12367       tree c, t = expr.value;
12368       t = c_fully_fold (t, false, NULL);
12369 
12370       parens.skip_until_found_close (parser);
12371 
12372       if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
12373 	{
12374 	  c_parser_error (parser, "expected integer expression");
12375 	  return list;
12376 	}
12377 
12378       check_no_duplicate_clause (list, OMP_CLAUSE_HINT, "hint");
12379 
12380       c = build_omp_clause (hint_loc, OMP_CLAUSE_HINT);
12381       OMP_CLAUSE_HINT_EXPR (c) = t;
12382       OMP_CLAUSE_CHAIN (c) = list;
12383       list = c;
12384     }
12385 
12386   return list;
12387 }
12388 
12389 /* OpenMP 4.5:
12390    defaultmap ( tofrom : scalar ) */
12391 
12392 static tree
c_parser_omp_clause_defaultmap(c_parser * parser,tree list)12393 c_parser_omp_clause_defaultmap (c_parser *parser, tree list)
12394 {
12395   location_t loc = c_parser_peek_token (parser)->location;
12396   tree c;
12397   const char *p;
12398 
12399   matching_parens parens;
12400   if (!parens.require_open (parser))
12401     return list;
12402   if (!c_parser_next_token_is (parser, CPP_NAME))
12403     {
12404       c_parser_error (parser, "expected %<tofrom%>");
12405       goto out_err;
12406     }
12407   p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
12408   if (strcmp (p, "tofrom") != 0)
12409     {
12410       c_parser_error (parser, "expected %<tofrom%>");
12411       goto out_err;
12412     }
12413   c_parser_consume_token (parser);
12414   if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
12415     goto out_err;
12416   if (!c_parser_next_token_is (parser, CPP_NAME))
12417     {
12418       c_parser_error (parser, "expected %<scalar%>");
12419       goto out_err;
12420     }
12421   p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
12422   if (strcmp (p, "scalar") != 0)
12423     {
12424       c_parser_error (parser, "expected %<scalar%>");
12425       goto out_err;
12426     }
12427   c_parser_consume_token (parser);
12428   parens.skip_until_found_close (parser);
12429   check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULTMAP, "defaultmap");
12430   c = build_omp_clause (loc, OMP_CLAUSE_DEFAULTMAP);
12431   OMP_CLAUSE_CHAIN (c) = list;
12432   return c;
12433 
12434  out_err:
12435   parens.skip_until_found_close (parser);
12436   return list;
12437 }
12438 
12439 /* OpenACC 2.0:
12440    use_device ( variable-list )
12441 
12442    OpenMP 4.5:
12443    use_device_ptr ( variable-list ) */
12444 
12445 static tree
c_parser_omp_clause_use_device_ptr(c_parser * parser,tree list)12446 c_parser_omp_clause_use_device_ptr (c_parser *parser, tree list)
12447 {
12448   return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_USE_DEVICE_PTR,
12449 				       list);
12450 }
12451 
12452 /* OpenMP 4.5:
12453    is_device_ptr ( variable-list ) */
12454 
12455 static tree
c_parser_omp_clause_is_device_ptr(c_parser * parser,tree list)12456 c_parser_omp_clause_is_device_ptr (c_parser *parser, tree list)
12457 {
12458   return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_IS_DEVICE_PTR, list);
12459 }
12460 
12461 /* OpenACC:
12462    num_gangs ( expression )
12463    num_workers ( expression )
12464    vector_length ( expression )  */
12465 
12466 static tree
c_parser_oacc_single_int_clause(c_parser * parser,omp_clause_code code,tree list)12467 c_parser_oacc_single_int_clause (c_parser *parser, omp_clause_code code,
12468 				 tree list)
12469 {
12470   location_t loc = c_parser_peek_token (parser)->location;
12471 
12472   matching_parens parens;
12473   if (!parens.require_open (parser))
12474     return list;
12475 
12476   location_t expr_loc = c_parser_peek_token (parser)->location;
12477   c_expr expr = c_parser_expression (parser);
12478   expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
12479   tree c, t = expr.value;
12480   t = c_fully_fold (t, false, NULL);
12481 
12482   parens.skip_until_found_close (parser);
12483 
12484   if (t == error_mark_node)
12485     return list;
12486   else if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
12487     {
12488       error_at (expr_loc, "%qs expression must be integral",
12489 		omp_clause_code_name[code]);
12490       return list;
12491     }
12492 
12493   /* Attempt to statically determine when the number isn't positive.  */
12494   c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
12495 		       build_int_cst (TREE_TYPE (t), 0));
12496   protected_set_expr_location (c, expr_loc);
12497   if (c == boolean_true_node)
12498     {
12499       warning_at (expr_loc, 0,
12500 		  "%qs value must be positive",
12501 		  omp_clause_code_name[code]);
12502       t = integer_one_node;
12503     }
12504 
12505   check_no_duplicate_clause (list, code, omp_clause_code_name[code]);
12506 
12507   c = build_omp_clause (loc, code);
12508   OMP_CLAUSE_OPERAND (c, 0) = t;
12509   OMP_CLAUSE_CHAIN (c) = list;
12510   return c;
12511 }
12512 
12513 /* OpenACC:
12514 
12515     gang [( gang-arg-list )]
12516     worker [( [num:] int-expr )]
12517     vector [( [length:] int-expr )]
12518 
12519   where gang-arg is one of:
12520 
12521     [num:] int-expr
12522     static: size-expr
12523 
12524   and size-expr may be:
12525 
12526     *
12527     int-expr
12528 */
12529 
12530 static tree
c_parser_oacc_shape_clause(c_parser * parser,omp_clause_code kind,const char * str,tree list)12531 c_parser_oacc_shape_clause (c_parser *parser, omp_clause_code kind,
12532 			    const char *str, tree list)
12533 {
12534   const char *id = "num";
12535   tree ops[2] = { NULL_TREE, NULL_TREE }, c;
12536   location_t loc = c_parser_peek_token (parser)->location;
12537 
12538   if (kind == OMP_CLAUSE_VECTOR)
12539     id = "length";
12540 
12541   if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
12542     {
12543       c_parser_consume_token (parser);
12544 
12545       do
12546 	{
12547 	  c_token *next = c_parser_peek_token (parser);
12548 	  int idx = 0;
12549 
12550 	  /* Gang static argument.  */
12551 	  if (kind == OMP_CLAUSE_GANG
12552 	      && c_parser_next_token_is_keyword (parser, RID_STATIC))
12553 	    {
12554 	      c_parser_consume_token (parser);
12555 
12556 	      if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
12557 		goto cleanup_error;
12558 
12559 	      idx = 1;
12560 	      if (ops[idx] != NULL_TREE)
12561 		{
12562 		  c_parser_error (parser, "too many %<static%> arguments");
12563 		  goto cleanup_error;
12564 		}
12565 
12566 	      /* Check for the '*' argument.  */
12567 	      if (c_parser_next_token_is (parser, CPP_MULT)
12568 		  && (c_parser_peek_2nd_token (parser)->type == CPP_COMMA
12569 		      || c_parser_peek_2nd_token (parser)->type
12570 		         == CPP_CLOSE_PAREN))
12571 		{
12572 		  c_parser_consume_token (parser);
12573 		  ops[idx] = integer_minus_one_node;
12574 
12575 		  if (c_parser_next_token_is (parser, CPP_COMMA))
12576 		    {
12577 		      c_parser_consume_token (parser);
12578 		      continue;
12579 		    }
12580 		  else
12581 		    break;
12582 		}
12583 	    }
12584 	  /* Worker num: argument and vector length: arguments.  */
12585 	  else if (c_parser_next_token_is (parser, CPP_NAME)
12586 		   && strcmp (id, IDENTIFIER_POINTER (next->value)) == 0
12587 		   && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
12588 	    {
12589 	      c_parser_consume_token (parser);  /* id  */
12590 	      c_parser_consume_token (parser);  /* ':'  */
12591 	    }
12592 
12593 	  /* Now collect the actual argument.  */
12594 	  if (ops[idx] != NULL_TREE)
12595 	    {
12596 	      c_parser_error (parser, "unexpected argument");
12597 	      goto cleanup_error;
12598 	    }
12599 
12600 	  location_t expr_loc = c_parser_peek_token (parser)->location;
12601 	  c_expr cexpr = c_parser_expr_no_commas (parser, NULL);
12602 	  cexpr = convert_lvalue_to_rvalue (expr_loc, cexpr, false, true);
12603 	  tree expr = cexpr.value;
12604 	  if (expr == error_mark_node)
12605 	    goto cleanup_error;
12606 
12607 	  expr = c_fully_fold (expr, false, NULL);
12608 
12609 	  /* Attempt to statically determine when the number isn't a
12610 	     positive integer.  */
12611 
12612 	  if (!INTEGRAL_TYPE_P (TREE_TYPE (expr)))
12613 	    {
12614 	      c_parser_error (parser, "expected integer expression");
12615 	      return list;
12616 	    }
12617 
12618 	  tree c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, expr,
12619 				    build_int_cst (TREE_TYPE (expr), 0));
12620 	  if (c == boolean_true_node)
12621 	    {
12622 	      warning_at (loc, 0,
12623 			  "%qs value must be positive", str);
12624 	      expr = integer_one_node;
12625 	    }
12626 
12627 	  ops[idx] = expr;
12628 
12629 	  if (kind == OMP_CLAUSE_GANG
12630 	      && c_parser_next_token_is (parser, CPP_COMMA))
12631 	    {
12632 	      c_parser_consume_token (parser);
12633 	      continue;
12634 	    }
12635 	  break;
12636 	}
12637       while (1);
12638 
12639       if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
12640 	goto cleanup_error;
12641     }
12642 
12643   check_no_duplicate_clause (list, kind, str);
12644 
12645   c = build_omp_clause (loc, kind);
12646 
12647   if (ops[1])
12648     OMP_CLAUSE_OPERAND (c, 1) = ops[1];
12649 
12650   OMP_CLAUSE_OPERAND (c, 0) = ops[0];
12651   OMP_CLAUSE_CHAIN (c) = list;
12652 
12653   return c;
12654 
12655  cleanup_error:
12656   c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
12657   return list;
12658 }
12659 
12660 /* OpenACC:
12661    auto
12662    independent
12663    nohost
12664    seq */
12665 
12666 static tree
c_parser_oacc_simple_clause(c_parser * parser,enum omp_clause_code code,tree list)12667 c_parser_oacc_simple_clause (c_parser *parser, enum omp_clause_code code,
12668 			     tree list)
12669 {
12670   check_no_duplicate_clause (list, code, omp_clause_code_name[code]);
12671 
12672   tree c = build_omp_clause (c_parser_peek_token (parser)->location, code);
12673   OMP_CLAUSE_CHAIN (c) = list;
12674 
12675   return c;
12676 }
12677 
12678 /* OpenACC:
12679    async [( int-expr )] */
12680 
12681 static tree
c_parser_oacc_clause_async(c_parser * parser,tree list)12682 c_parser_oacc_clause_async (c_parser *parser, tree list)
12683 {
12684   tree c, t;
12685   location_t loc = c_parser_peek_token (parser)->location;
12686 
12687   t = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
12688 
12689   if (c_parser_peek_token (parser)->type == CPP_OPEN_PAREN)
12690     {
12691       c_parser_consume_token (parser);
12692 
12693       t = c_parser_expression (parser).value;
12694       if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
12695 	c_parser_error (parser, "expected integer expression");
12696       else if (t == error_mark_node
12697 	  || !c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
12698 	return list;
12699     }
12700   else
12701     t = c_fully_fold (t, false, NULL);
12702 
12703   check_no_duplicate_clause (list, OMP_CLAUSE_ASYNC, "async");
12704 
12705   c = build_omp_clause (loc, OMP_CLAUSE_ASYNC);
12706   OMP_CLAUSE_ASYNC_EXPR (c) = t;
12707   OMP_CLAUSE_CHAIN (c) = list;
12708   list = c;
12709 
12710   return list;
12711 }
12712 
12713 /* OpenACC 2.0:
12714    tile ( size-expr-list ) */
12715 
12716 static tree
c_parser_oacc_clause_tile(c_parser * parser,tree list)12717 c_parser_oacc_clause_tile (c_parser *parser, tree list)
12718 {
12719   tree c, expr = error_mark_node;
12720   location_t loc;
12721   tree tile = NULL_TREE;
12722 
12723   check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile");
12724   check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse");
12725 
12726   loc = c_parser_peek_token (parser)->location;
12727   if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
12728     return list;
12729 
12730   do
12731     {
12732       if (tile && !c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
12733 	return list;
12734 
12735       if (c_parser_next_token_is (parser, CPP_MULT)
12736 	  && (c_parser_peek_2nd_token (parser)->type == CPP_COMMA
12737 	      || c_parser_peek_2nd_token (parser)->type == CPP_CLOSE_PAREN))
12738 	{
12739 	  c_parser_consume_token (parser);
12740 	  expr = integer_zero_node;
12741 	}
12742       else
12743 	{
12744 	  location_t expr_loc = c_parser_peek_token (parser)->location;
12745 	  c_expr cexpr = c_parser_expr_no_commas (parser, NULL);
12746 	  cexpr = convert_lvalue_to_rvalue (expr_loc, cexpr, false, true);
12747 	  expr = cexpr.value;
12748 
12749 	  if (expr == error_mark_node)
12750 	    {
12751 	      c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
12752 					 "expected %<)%>");
12753 	      return list;
12754 	    }
12755 
12756 	  expr = c_fully_fold (expr, false, NULL);
12757 
12758 	  if (!INTEGRAL_TYPE_P (TREE_TYPE (expr))
12759 	      || !tree_fits_shwi_p (expr)
12760 	      || tree_to_shwi (expr) <= 0)
12761 	    {
12762 	      error_at (expr_loc, "%<tile%> argument needs positive"
12763 			" integral constant");
12764 	      expr = integer_zero_node;
12765 	    }
12766 	}
12767 
12768       tile = tree_cons (NULL_TREE, expr, tile);
12769     }
12770   while (c_parser_next_token_is_not (parser, CPP_CLOSE_PAREN));
12771 
12772   /* Consume the trailing ')'.  */
12773   c_parser_consume_token (parser);
12774 
12775   c = build_omp_clause (loc, OMP_CLAUSE_TILE);
12776   tile = nreverse (tile);
12777   OMP_CLAUSE_TILE_LIST (c) = tile;
12778   OMP_CLAUSE_CHAIN (c) = list;
12779   return c;
12780 }
12781 
12782 /* OpenACC:
12783    wait ( int-expr-list ) */
12784 
12785 static tree
c_parser_oacc_clause_wait(c_parser * parser,tree list)12786 c_parser_oacc_clause_wait (c_parser *parser, tree list)
12787 {
12788   location_t clause_loc = c_parser_peek_token (parser)->location;
12789 
12790   if (c_parser_peek_token (parser)->type == CPP_OPEN_PAREN)
12791     list = c_parser_oacc_wait_list (parser, clause_loc, list);
12792 
12793   return list;
12794 }
12795 
12796 /* OpenMP 2.5:
12797    ordered
12798 
12799    OpenMP 4.5:
12800    ordered ( constant-expression ) */
12801 
12802 static tree
c_parser_omp_clause_ordered(c_parser * parser,tree list)12803 c_parser_omp_clause_ordered (c_parser *parser, tree list)
12804 {
12805   check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED, "ordered");
12806 
12807   tree c, num = NULL_TREE;
12808   HOST_WIDE_INT n;
12809   location_t loc = c_parser_peek_token (parser)->location;
12810   if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
12811     {
12812       matching_parens parens;
12813       parens.consume_open (parser);
12814       num = c_parser_expr_no_commas (parser, NULL).value;
12815       parens.skip_until_found_close (parser);
12816     }
12817   if (num == error_mark_node)
12818     return list;
12819   if (num)
12820     {
12821       mark_exp_read (num);
12822       num = c_fully_fold (num, false, NULL);
12823       if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
12824 	  || !tree_fits_shwi_p (num)
12825 	  || (n = tree_to_shwi (num)) <= 0
12826 	  || (int) n != n)
12827 	{
12828 	  error_at (loc, "ordered argument needs positive "
12829 			 "constant integer expression");
12830 	  return list;
12831 	}
12832     }
12833   c = build_omp_clause (loc, OMP_CLAUSE_ORDERED);
12834   OMP_CLAUSE_ORDERED_EXPR (c) = num;
12835   OMP_CLAUSE_CHAIN (c) = list;
12836   return c;
12837 }
12838 
12839 /* OpenMP 2.5:
12840    private ( variable-list ) */
12841 
12842 static tree
c_parser_omp_clause_private(c_parser * parser,tree list)12843 c_parser_omp_clause_private (c_parser *parser, tree list)
12844 {
12845   return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_PRIVATE, list);
12846 }
12847 
12848 /* OpenMP 2.5:
12849    reduction ( reduction-operator : variable-list )
12850 
12851    reduction-operator:
12852      One of: + * - & ^ | && ||
12853 
12854    OpenMP 3.1:
12855 
12856    reduction-operator:
12857      One of: + * - & ^ | && || max min
12858 
12859    OpenMP 4.0:
12860 
12861    reduction-operator:
12862      One of: + * - & ^ | && ||
12863      identifier  */
12864 
12865 static tree
c_parser_omp_clause_reduction(c_parser * parser,tree list)12866 c_parser_omp_clause_reduction (c_parser *parser, tree list)
12867 {
12868   location_t clause_loc = c_parser_peek_token (parser)->location;
12869   matching_parens parens;
12870   if (parens.require_open (parser))
12871     {
12872       enum tree_code code = ERROR_MARK;
12873       tree reduc_id = NULL_TREE;
12874 
12875       switch (c_parser_peek_token (parser)->type)
12876 	{
12877 	case CPP_PLUS:
12878 	  code = PLUS_EXPR;
12879 	  break;
12880 	case CPP_MULT:
12881 	  code = MULT_EXPR;
12882 	  break;
12883 	case CPP_MINUS:
12884 	  code = MINUS_EXPR;
12885 	  break;
12886 	case CPP_AND:
12887 	  code = BIT_AND_EXPR;
12888 	  break;
12889 	case CPP_XOR:
12890 	  code = BIT_XOR_EXPR;
12891 	  break;
12892 	case CPP_OR:
12893 	  code = BIT_IOR_EXPR;
12894 	  break;
12895 	case CPP_AND_AND:
12896 	  code = TRUTH_ANDIF_EXPR;
12897 	  break;
12898 	case CPP_OR_OR:
12899 	  code = TRUTH_ORIF_EXPR;
12900 	  break;
12901         case CPP_NAME:
12902 	  {
12903 	    const char *p
12904 	      = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
12905 	    if (strcmp (p, "min") == 0)
12906 	      {
12907 		code = MIN_EXPR;
12908 		break;
12909 	      }
12910 	    if (strcmp (p, "max") == 0)
12911 	      {
12912 		code = MAX_EXPR;
12913 		break;
12914 	      }
12915 	    reduc_id = c_parser_peek_token (parser)->value;
12916 	    break;
12917 	  }
12918 	default:
12919 	  c_parser_error (parser,
12920 			  "expected %<+%>, %<*%>, %<-%>, %<&%>, "
12921 			  "%<^%>, %<|%>, %<&&%>, %<||%> or identifier");
12922 	  c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
12923 	  return list;
12924 	}
12925       c_parser_consume_token (parser);
12926       reduc_id = c_omp_reduction_id (code, reduc_id);
12927       if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
12928 	{
12929 	  tree nl, c;
12930 
12931 	  nl = c_parser_omp_variable_list (parser, clause_loc,
12932 					   OMP_CLAUSE_REDUCTION, list);
12933 	  for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
12934 	    {
12935 	      tree d = OMP_CLAUSE_DECL (c), type;
12936 	      if (TREE_CODE (d) != TREE_LIST)
12937 		type = TREE_TYPE (d);
12938 	      else
12939 		{
12940 		  int cnt = 0;
12941 		  tree t;
12942 		  for (t = d; TREE_CODE (t) == TREE_LIST; t = TREE_CHAIN (t))
12943 		    cnt++;
12944 		  type = TREE_TYPE (t);
12945 		  while (cnt > 0)
12946 		    {
12947 		      if (TREE_CODE (type) != POINTER_TYPE
12948 			  && TREE_CODE (type) != ARRAY_TYPE)
12949 			break;
12950 		      type = TREE_TYPE (type);
12951 		      cnt--;
12952 		    }
12953 		}
12954 	      while (TREE_CODE (type) == ARRAY_TYPE)
12955 		type = TREE_TYPE (type);
12956 	      OMP_CLAUSE_REDUCTION_CODE (c) = code;
12957 	      if (code == ERROR_MARK
12958 		  || !(INTEGRAL_TYPE_P (type)
12959 		       || TREE_CODE (type) == REAL_TYPE
12960 		       || TREE_CODE (type) == COMPLEX_TYPE))
12961 		OMP_CLAUSE_REDUCTION_PLACEHOLDER (c)
12962 		  = c_omp_reduction_lookup (reduc_id,
12963 					    TYPE_MAIN_VARIANT (type));
12964 	    }
12965 
12966 	  list = nl;
12967 	}
12968       parens.skip_until_found_close (parser);
12969     }
12970   return list;
12971 }
12972 
12973 /* OpenMP 2.5:
12974    schedule ( schedule-kind )
12975    schedule ( schedule-kind , expression )
12976 
12977    schedule-kind:
12978      static | dynamic | guided | runtime | auto
12979 
12980    OpenMP 4.5:
12981    schedule ( schedule-modifier : schedule-kind )
12982    schedule ( schedule-modifier [ , schedule-modifier ] : schedule-kind , expression )
12983 
12984    schedule-modifier:
12985      simd
12986      monotonic
12987      nonmonotonic  */
12988 
12989 static tree
c_parser_omp_clause_schedule(c_parser * parser,tree list)12990 c_parser_omp_clause_schedule (c_parser *parser, tree list)
12991 {
12992   tree c, t;
12993   location_t loc = c_parser_peek_token (parser)->location;
12994   int modifiers = 0, nmodifiers = 0;
12995 
12996   matching_parens parens;
12997   if (!parens.require_open (parser))
12998     return list;
12999 
13000   c = build_omp_clause (loc, OMP_CLAUSE_SCHEDULE);
13001 
13002   while (c_parser_next_token_is (parser, CPP_NAME))
13003     {
13004       tree kind = c_parser_peek_token (parser)->value;
13005       const char *p = IDENTIFIER_POINTER (kind);
13006       if (strcmp ("simd", p) == 0)
13007 	OMP_CLAUSE_SCHEDULE_SIMD (c) = 1;
13008       else if (strcmp ("monotonic", p) == 0)
13009 	modifiers |= OMP_CLAUSE_SCHEDULE_MONOTONIC;
13010       else if (strcmp ("nonmonotonic", p) == 0)
13011 	modifiers |= OMP_CLAUSE_SCHEDULE_NONMONOTONIC;
13012       else
13013 	break;
13014       c_parser_consume_token (parser);
13015       if (nmodifiers++ == 0
13016 	  && c_parser_next_token_is (parser, CPP_COMMA))
13017 	c_parser_consume_token (parser);
13018       else
13019 	{
13020 	  c_parser_require (parser, CPP_COLON, "expected %<:%>");
13021 	  break;
13022 	}
13023     }
13024 
13025   if ((modifiers & (OMP_CLAUSE_SCHEDULE_MONOTONIC
13026 		    | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
13027       == (OMP_CLAUSE_SCHEDULE_MONOTONIC
13028 	  | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
13029     {
13030       error_at (loc, "both %<monotonic%> and %<nonmonotonic%> modifiers "
13031 		     "specified");
13032       modifiers = 0;
13033     }
13034 
13035   if (c_parser_next_token_is (parser, CPP_NAME))
13036     {
13037       tree kind = c_parser_peek_token (parser)->value;
13038       const char *p = IDENTIFIER_POINTER (kind);
13039 
13040       switch (p[0])
13041 	{
13042 	case 'd':
13043 	  if (strcmp ("dynamic", p) != 0)
13044 	    goto invalid_kind;
13045 	  OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
13046 	  break;
13047 
13048         case 'g':
13049 	  if (strcmp ("guided", p) != 0)
13050 	    goto invalid_kind;
13051 	  OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
13052 	  break;
13053 
13054 	case 'r':
13055 	  if (strcmp ("runtime", p) != 0)
13056 	    goto invalid_kind;
13057 	  OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
13058 	  break;
13059 
13060 	default:
13061 	  goto invalid_kind;
13062 	}
13063     }
13064   else if (c_parser_next_token_is_keyword (parser, RID_STATIC))
13065     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
13066   else if (c_parser_next_token_is_keyword (parser, RID_AUTO))
13067     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
13068   else
13069     goto invalid_kind;
13070 
13071   c_parser_consume_token (parser);
13072   if (c_parser_next_token_is (parser, CPP_COMMA))
13073     {
13074       location_t here;
13075       c_parser_consume_token (parser);
13076 
13077       here = c_parser_peek_token (parser)->location;
13078       c_expr expr = c_parser_expr_no_commas (parser, NULL);
13079       expr = convert_lvalue_to_rvalue (here, expr, false, true);
13080       t = expr.value;
13081       t = c_fully_fold (t, false, NULL);
13082 
13083       if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
13084 	error_at (here, "schedule %<runtime%> does not take "
13085 		  "a %<chunk_size%> parameter");
13086       else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
13087 	error_at (here,
13088 		  "schedule %<auto%> does not take "
13089 		  "a %<chunk_size%> parameter");
13090       else if (TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE)
13091 	{
13092 	  /* Attempt to statically determine when the number isn't
13093 	     positive.  */
13094 	  tree s = fold_build2_loc (loc, LE_EXPR, boolean_type_node, t,
13095 				    build_int_cst (TREE_TYPE (t), 0));
13096 	  protected_set_expr_location (s, loc);
13097 	  if (s == boolean_true_node)
13098 	    {
13099 	      warning_at (loc, 0,
13100 			  "chunk size value must be positive");
13101 	      t = integer_one_node;
13102 	    }
13103 	  OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
13104 	}
13105       else
13106 	c_parser_error (parser, "expected integer expression");
13107 
13108       parens.skip_until_found_close (parser);
13109     }
13110   else
13111     c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
13112 			       "expected %<,%> or %<)%>");
13113 
13114   OMP_CLAUSE_SCHEDULE_KIND (c)
13115     = (enum omp_clause_schedule_kind)
13116       (OMP_CLAUSE_SCHEDULE_KIND (c) | modifiers);
13117 
13118   check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
13119   OMP_CLAUSE_CHAIN (c) = list;
13120   return c;
13121 
13122  invalid_kind:
13123   c_parser_error (parser, "invalid schedule kind");
13124   c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
13125   return list;
13126 }
13127 
13128 /* OpenMP 2.5:
13129    shared ( variable-list ) */
13130 
13131 static tree
c_parser_omp_clause_shared(c_parser * parser,tree list)13132 c_parser_omp_clause_shared (c_parser *parser, tree list)
13133 {
13134   return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_SHARED, list);
13135 }
13136 
13137 /* OpenMP 3.0:
13138    untied */
13139 
13140 static tree
c_parser_omp_clause_untied(c_parser * parser ATTRIBUTE_UNUSED,tree list)13141 c_parser_omp_clause_untied (c_parser *parser ATTRIBUTE_UNUSED, tree list)
13142 {
13143   tree c;
13144 
13145   /* FIXME: Should we allow duplicates?  */
13146   check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied");
13147 
13148   c = build_omp_clause (c_parser_peek_token (parser)->location,
13149 			OMP_CLAUSE_UNTIED);
13150   OMP_CLAUSE_CHAIN (c) = list;
13151 
13152   return c;
13153 }
13154 
13155 /* OpenMP 4.0:
13156    inbranch
13157    notinbranch */
13158 
13159 static tree
c_parser_omp_clause_branch(c_parser * parser ATTRIBUTE_UNUSED,enum omp_clause_code code,tree list)13160 c_parser_omp_clause_branch (c_parser *parser ATTRIBUTE_UNUSED,
13161 			    enum omp_clause_code code, tree list)
13162 {
13163   check_no_duplicate_clause (list, code, omp_clause_code_name[code]);
13164 
13165   tree c = build_omp_clause (c_parser_peek_token (parser)->location, code);
13166   OMP_CLAUSE_CHAIN (c) = list;
13167 
13168   return c;
13169 }
13170 
13171 /* OpenMP 4.0:
13172    parallel
13173    for
13174    sections
13175    taskgroup */
13176 
13177 static tree
c_parser_omp_clause_cancelkind(c_parser * parser ATTRIBUTE_UNUSED,enum omp_clause_code code,tree list)13178 c_parser_omp_clause_cancelkind (c_parser *parser ATTRIBUTE_UNUSED,
13179 				enum omp_clause_code code, tree list)
13180 {
13181   tree c = build_omp_clause (c_parser_peek_token (parser)->location, code);
13182   OMP_CLAUSE_CHAIN (c) = list;
13183 
13184   return c;
13185 }
13186 
13187 /* OpenMP 4.5:
13188    nogroup */
13189 
13190 static tree
c_parser_omp_clause_nogroup(c_parser * parser ATTRIBUTE_UNUSED,tree list)13191 c_parser_omp_clause_nogroup (c_parser *parser ATTRIBUTE_UNUSED, tree list)
13192 {
13193   check_no_duplicate_clause (list, OMP_CLAUSE_NOGROUP, "nogroup");
13194   tree c = build_omp_clause (c_parser_peek_token (parser)->location,
13195 			     OMP_CLAUSE_NOGROUP);
13196   OMP_CLAUSE_CHAIN (c) = list;
13197   return c;
13198 }
13199 
13200 /* OpenMP 4.5:
13201    simd
13202    threads */
13203 
13204 static tree
c_parser_omp_clause_orderedkind(c_parser * parser ATTRIBUTE_UNUSED,enum omp_clause_code code,tree list)13205 c_parser_omp_clause_orderedkind (c_parser *parser ATTRIBUTE_UNUSED,
13206 				 enum omp_clause_code code, tree list)
13207 {
13208   check_no_duplicate_clause (list, code, omp_clause_code_name[code]);
13209   tree c = build_omp_clause (c_parser_peek_token (parser)->location, code);
13210   OMP_CLAUSE_CHAIN (c) = list;
13211   return c;
13212 }
13213 
13214 /* OpenMP 4.0:
13215    num_teams ( expression ) */
13216 
13217 static tree
c_parser_omp_clause_num_teams(c_parser * parser,tree list)13218 c_parser_omp_clause_num_teams (c_parser *parser, tree list)
13219 {
13220   location_t num_teams_loc = c_parser_peek_token (parser)->location;
13221   matching_parens parens;
13222   if (parens.require_open (parser))
13223     {
13224       location_t expr_loc = c_parser_peek_token (parser)->location;
13225       c_expr expr = c_parser_expression (parser);
13226       expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
13227       tree c, t = expr.value;
13228       t = c_fully_fold (t, false, NULL);
13229 
13230       parens.skip_until_found_close (parser);
13231 
13232       if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
13233 	{
13234 	  c_parser_error (parser, "expected integer expression");
13235 	  return list;
13236 	}
13237 
13238       /* Attempt to statically determine when the number isn't positive.  */
13239       c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
13240 			   build_int_cst (TREE_TYPE (t), 0));
13241       protected_set_expr_location (c, expr_loc);
13242       if (c == boolean_true_node)
13243 	{
13244 	  warning_at (expr_loc, 0, "%<num_teams%> value must be positive");
13245 	  t = integer_one_node;
13246 	}
13247 
13248       check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS, "num_teams");
13249 
13250       c = build_omp_clause (num_teams_loc, OMP_CLAUSE_NUM_TEAMS);
13251       OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
13252       OMP_CLAUSE_CHAIN (c) = list;
13253       list = c;
13254     }
13255 
13256   return list;
13257 }
13258 
13259 /* OpenMP 4.0:
13260    thread_limit ( expression ) */
13261 
13262 static tree
c_parser_omp_clause_thread_limit(c_parser * parser,tree list)13263 c_parser_omp_clause_thread_limit (c_parser *parser, tree list)
13264 {
13265   location_t num_thread_limit_loc = c_parser_peek_token (parser)->location;
13266   matching_parens parens;
13267   if (parens.require_open (parser))
13268     {
13269       location_t expr_loc = c_parser_peek_token (parser)->location;
13270       c_expr expr = c_parser_expression (parser);
13271       expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
13272       tree c, t = expr.value;
13273       t = c_fully_fold (t, false, NULL);
13274 
13275       parens.skip_until_found_close (parser);
13276 
13277       if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
13278 	{
13279 	  c_parser_error (parser, "expected integer expression");
13280 	  return list;
13281 	}
13282 
13283       /* Attempt to statically determine when the number isn't positive.  */
13284       c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
13285 			   build_int_cst (TREE_TYPE (t), 0));
13286       protected_set_expr_location (c, expr_loc);
13287       if (c == boolean_true_node)
13288 	{
13289 	  warning_at (expr_loc, 0, "%<thread_limit%> value must be positive");
13290 	  t = integer_one_node;
13291 	}
13292 
13293       check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
13294 				 "thread_limit");
13295 
13296       c = build_omp_clause (num_thread_limit_loc, OMP_CLAUSE_THREAD_LIMIT);
13297       OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
13298       OMP_CLAUSE_CHAIN (c) = list;
13299       list = c;
13300     }
13301 
13302   return list;
13303 }
13304 
13305 /* OpenMP 4.0:
13306    aligned ( variable-list )
13307    aligned ( variable-list : constant-expression ) */
13308 
13309 static tree
c_parser_omp_clause_aligned(c_parser * parser,tree list)13310 c_parser_omp_clause_aligned (c_parser *parser, tree list)
13311 {
13312   location_t clause_loc = c_parser_peek_token (parser)->location;
13313   tree nl, c;
13314 
13315   matching_parens parens;
13316   if (!parens.require_open (parser))
13317     return list;
13318 
13319   nl = c_parser_omp_variable_list (parser, clause_loc,
13320 				   OMP_CLAUSE_ALIGNED, list);
13321 
13322   if (c_parser_next_token_is (parser, CPP_COLON))
13323     {
13324       c_parser_consume_token (parser);
13325       location_t expr_loc = c_parser_peek_token (parser)->location;
13326       c_expr expr = c_parser_expr_no_commas (parser, NULL);
13327       expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
13328       tree alignment = expr.value;
13329       alignment = c_fully_fold (alignment, false, NULL);
13330       if (TREE_CODE (alignment) != INTEGER_CST
13331 	  || !INTEGRAL_TYPE_P (TREE_TYPE (alignment))
13332 	  || tree_int_cst_sgn (alignment) != 1)
13333 	{
13334 	  error_at (clause_loc, "%<aligned%> clause alignment expression must "
13335 				"be positive constant integer expression");
13336 	  alignment = NULL_TREE;
13337 	}
13338 
13339       for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
13340 	OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
13341     }
13342 
13343   parens.skip_until_found_close (parser);
13344   return nl;
13345 }
13346 
13347 /* OpenMP 4.0:
13348    linear ( variable-list )
13349    linear ( variable-list : expression )
13350 
13351    OpenMP 4.5:
13352    linear ( modifier ( variable-list ) )
13353    linear ( modifier ( variable-list ) : expression ) */
13354 
13355 static tree
c_parser_omp_clause_linear(c_parser * parser,tree list)13356 c_parser_omp_clause_linear (c_parser *parser, tree list)
13357 {
13358   location_t clause_loc = c_parser_peek_token (parser)->location;
13359   tree nl, c, step;
13360   enum omp_clause_linear_kind kind = OMP_CLAUSE_LINEAR_DEFAULT;
13361 
13362   matching_parens parens;
13363   if (!parens.require_open (parser))
13364     return list;
13365 
13366   if (c_parser_next_token_is (parser, CPP_NAME))
13367     {
13368       c_token *tok = c_parser_peek_token (parser);
13369       const char *p = IDENTIFIER_POINTER (tok->value);
13370       if (strcmp ("val", p) == 0)
13371 	kind = OMP_CLAUSE_LINEAR_VAL;
13372       if (c_parser_peek_2nd_token (parser)->type != CPP_OPEN_PAREN)
13373 	kind = OMP_CLAUSE_LINEAR_DEFAULT;
13374       if (kind != OMP_CLAUSE_LINEAR_DEFAULT)
13375 	{
13376 	  c_parser_consume_token (parser);
13377 	  c_parser_consume_token (parser);
13378 	}
13379     }
13380 
13381   nl = c_parser_omp_variable_list (parser, clause_loc,
13382 				   OMP_CLAUSE_LINEAR, list);
13383 
13384   if (kind != OMP_CLAUSE_LINEAR_DEFAULT)
13385     parens.skip_until_found_close (parser);
13386 
13387   if (c_parser_next_token_is (parser, CPP_COLON))
13388     {
13389       c_parser_consume_token (parser);
13390       location_t expr_loc = c_parser_peek_token (parser)->location;
13391       c_expr expr = c_parser_expression (parser);
13392       expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
13393       step = expr.value;
13394       step = c_fully_fold (step, false, NULL);
13395       if (!INTEGRAL_TYPE_P (TREE_TYPE (step)))
13396 	{
13397 	  error_at (clause_loc, "%<linear%> clause step expression must "
13398 				"be integral");
13399 	  step = integer_one_node;
13400 	}
13401 
13402     }
13403   else
13404     step = integer_one_node;
13405 
13406   for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
13407     {
13408       OMP_CLAUSE_LINEAR_STEP (c) = step;
13409       OMP_CLAUSE_LINEAR_KIND (c) = kind;
13410     }
13411 
13412   parens.skip_until_found_close (parser);
13413   return nl;
13414 }
13415 
13416 /* OpenMP 4.0:
13417    safelen ( constant-expression ) */
13418 
13419 static tree
c_parser_omp_clause_safelen(c_parser * parser,tree list)13420 c_parser_omp_clause_safelen (c_parser *parser, tree list)
13421 {
13422   location_t clause_loc = c_parser_peek_token (parser)->location;
13423   tree c, t;
13424 
13425   matching_parens parens;
13426   if (!parens.require_open (parser))
13427     return list;
13428 
13429   location_t expr_loc = c_parser_peek_token (parser)->location;
13430   c_expr expr = c_parser_expr_no_commas (parser, NULL);
13431   expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
13432   t = expr.value;
13433   t = c_fully_fold (t, false, NULL);
13434   if (TREE_CODE (t) != INTEGER_CST
13435       || !INTEGRAL_TYPE_P (TREE_TYPE (t))
13436       || tree_int_cst_sgn (t) != 1)
13437     {
13438       error_at (clause_loc, "%<safelen%> clause expression must "
13439 			    "be positive constant integer expression");
13440       t = NULL_TREE;
13441     }
13442 
13443   parens.skip_until_found_close (parser);
13444   if (t == NULL_TREE || t == error_mark_node)
13445     return list;
13446 
13447   check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen");
13448 
13449   c = build_omp_clause (clause_loc, OMP_CLAUSE_SAFELEN);
13450   OMP_CLAUSE_SAFELEN_EXPR (c) = t;
13451   OMP_CLAUSE_CHAIN (c) = list;
13452   return c;
13453 }
13454 
13455 /* OpenMP 4.0:
13456    simdlen ( constant-expression ) */
13457 
13458 static tree
c_parser_omp_clause_simdlen(c_parser * parser,tree list)13459 c_parser_omp_clause_simdlen (c_parser *parser, tree list)
13460 {
13461   location_t clause_loc = c_parser_peek_token (parser)->location;
13462   tree c, t;
13463 
13464   matching_parens parens;
13465   if (!parens.require_open (parser))
13466     return list;
13467 
13468   location_t expr_loc = c_parser_peek_token (parser)->location;
13469   c_expr expr = c_parser_expr_no_commas (parser, NULL);
13470   expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
13471   t = expr.value;
13472   t = c_fully_fold (t, false, NULL);
13473   if (TREE_CODE (t) != INTEGER_CST
13474       || !INTEGRAL_TYPE_P (TREE_TYPE (t))
13475       || tree_int_cst_sgn (t) != 1)
13476     {
13477       error_at (clause_loc, "%<simdlen%> clause expression must "
13478 			    "be positive constant integer expression");
13479       t = NULL_TREE;
13480     }
13481 
13482   parens.skip_until_found_close (parser);
13483   if (t == NULL_TREE || t == error_mark_node)
13484     return list;
13485 
13486   check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen");
13487 
13488   c = build_omp_clause (clause_loc, OMP_CLAUSE_SIMDLEN);
13489   OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
13490   OMP_CLAUSE_CHAIN (c) = list;
13491   return c;
13492 }
13493 
13494 /* OpenMP 4.5:
13495    vec:
13496      identifier [+/- integer]
13497      vec , identifier [+/- integer]
13498 */
13499 
13500 static tree
c_parser_omp_clause_depend_sink(c_parser * parser,location_t clause_loc,tree list)13501 c_parser_omp_clause_depend_sink (c_parser *parser, location_t clause_loc,
13502 				 tree list)
13503 {
13504   tree vec = NULL;
13505   if (c_parser_next_token_is_not (parser, CPP_NAME)
13506       || c_parser_peek_token (parser)->id_kind != C_ID_ID)
13507     {
13508       c_parser_error (parser, "expected identifier");
13509       return list;
13510     }
13511 
13512   while (c_parser_next_token_is (parser, CPP_NAME)
13513 	 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
13514     {
13515       tree t = lookup_name (c_parser_peek_token (parser)->value);
13516       tree addend = NULL;
13517 
13518       if (t == NULL_TREE)
13519 	{
13520 	  undeclared_variable (c_parser_peek_token (parser)->location,
13521 			       c_parser_peek_token (parser)->value);
13522 	  t = error_mark_node;
13523 	}
13524 
13525       c_parser_consume_token (parser);
13526 
13527       bool neg = false;
13528       if (c_parser_next_token_is (parser, CPP_MINUS))
13529 	neg = true;
13530       else if (!c_parser_next_token_is (parser, CPP_PLUS))
13531 	{
13532 	  addend = integer_zero_node;
13533 	  neg = false;
13534 	  goto add_to_vector;
13535 	}
13536       c_parser_consume_token (parser);
13537 
13538       if (c_parser_next_token_is_not (parser, CPP_NUMBER))
13539 	{
13540 	  c_parser_error (parser, "expected integer");
13541 	  return list;
13542 	}
13543 
13544       addend = c_parser_peek_token (parser)->value;
13545       if (TREE_CODE (addend) != INTEGER_CST)
13546 	{
13547 	  c_parser_error (parser, "expected integer");
13548 	  return list;
13549 	}
13550       c_parser_consume_token (parser);
13551 
13552     add_to_vector:
13553       if (t != error_mark_node)
13554 	{
13555 	  vec = tree_cons (addend, t, vec);
13556 	  if (neg)
13557 	    OMP_CLAUSE_DEPEND_SINK_NEGATIVE (vec) = 1;
13558 	}
13559 
13560       if (c_parser_next_token_is_not (parser, CPP_COMMA))
13561 	break;
13562 
13563       c_parser_consume_token (parser);
13564     }
13565 
13566   if (vec == NULL_TREE)
13567     return list;
13568 
13569   tree u = build_omp_clause (clause_loc, OMP_CLAUSE_DEPEND);
13570   OMP_CLAUSE_DEPEND_KIND (u) = OMP_CLAUSE_DEPEND_SINK;
13571   OMP_CLAUSE_DECL (u) = nreverse (vec);
13572   OMP_CLAUSE_CHAIN (u) = list;
13573   return u;
13574 }
13575 
13576 /* OpenMP 4.0:
13577    depend ( depend-kind: variable-list )
13578 
13579    depend-kind:
13580      in | out | inout
13581 
13582    OpenMP 4.5:
13583    depend ( source )
13584 
13585    depend ( sink  : vec )  */
13586 
13587 static tree
c_parser_omp_clause_depend(c_parser * parser,tree list)13588 c_parser_omp_clause_depend (c_parser *parser, tree list)
13589 {
13590   location_t clause_loc = c_parser_peek_token (parser)->location;
13591   enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_INOUT;
13592   tree nl, c;
13593 
13594   matching_parens parens;
13595   if (!parens.require_open (parser))
13596     return list;
13597 
13598   if (c_parser_next_token_is (parser, CPP_NAME))
13599     {
13600       const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
13601       if (strcmp ("in", p) == 0)
13602 	kind = OMP_CLAUSE_DEPEND_IN;
13603       else if (strcmp ("inout", p) == 0)
13604 	kind = OMP_CLAUSE_DEPEND_INOUT;
13605       else if (strcmp ("out", p) == 0)
13606 	kind = OMP_CLAUSE_DEPEND_OUT;
13607       else if (strcmp ("source", p) == 0)
13608 	kind = OMP_CLAUSE_DEPEND_SOURCE;
13609       else if (strcmp ("sink", p) == 0)
13610 	kind = OMP_CLAUSE_DEPEND_SINK;
13611       else
13612 	goto invalid_kind;
13613     }
13614   else
13615     goto invalid_kind;
13616 
13617   c_parser_consume_token (parser);
13618 
13619   if (kind == OMP_CLAUSE_DEPEND_SOURCE)
13620     {
13621       c = build_omp_clause (clause_loc, OMP_CLAUSE_DEPEND);
13622       OMP_CLAUSE_DEPEND_KIND (c) = kind;
13623       OMP_CLAUSE_DECL (c) = NULL_TREE;
13624       OMP_CLAUSE_CHAIN (c) = list;
13625       parens.skip_until_found_close (parser);
13626       return c;
13627     }
13628 
13629   if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
13630     goto resync_fail;
13631 
13632   if (kind == OMP_CLAUSE_DEPEND_SINK)
13633     nl = c_parser_omp_clause_depend_sink (parser, clause_loc, list);
13634   else
13635     {
13636       nl = c_parser_omp_variable_list (parser, clause_loc,
13637 				       OMP_CLAUSE_DEPEND, list);
13638 
13639       for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
13640 	OMP_CLAUSE_DEPEND_KIND (c) = kind;
13641     }
13642 
13643   parens.skip_until_found_close (parser);
13644   return nl;
13645 
13646  invalid_kind:
13647   c_parser_error (parser, "invalid depend kind");
13648  resync_fail:
13649   parens.skip_until_found_close (parser);
13650   return list;
13651 }
13652 
13653 /* OpenMP 4.0:
13654    map ( map-kind: variable-list )
13655    map ( variable-list )
13656 
13657    map-kind:
13658      alloc | to | from | tofrom
13659 
13660    OpenMP 4.5:
13661    map-kind:
13662      alloc | to | from | tofrom | release | delete
13663 
13664    map ( always [,] map-kind: variable-list ) */
13665 
13666 static tree
c_parser_omp_clause_map(c_parser * parser,tree list)13667 c_parser_omp_clause_map (c_parser *parser, tree list)
13668 {
13669   location_t clause_loc = c_parser_peek_token (parser)->location;
13670   enum gomp_map_kind kind = GOMP_MAP_TOFROM;
13671   int always = 0;
13672   enum c_id_kind always_id_kind = C_ID_NONE;
13673   location_t always_loc = UNKNOWN_LOCATION;
13674   tree always_id = NULL_TREE;
13675   tree nl, c;
13676 
13677   matching_parens parens;
13678   if (!parens.require_open (parser))
13679     return list;
13680 
13681   if (c_parser_next_token_is (parser, CPP_NAME))
13682     {
13683       c_token *tok = c_parser_peek_token (parser);
13684       const char *p = IDENTIFIER_POINTER (tok->value);
13685       always_id_kind = tok->id_kind;
13686       always_loc = tok->location;
13687       always_id = tok->value;
13688       if (strcmp ("always", p) == 0)
13689 	{
13690 	  c_token *sectok = c_parser_peek_2nd_token (parser);
13691 	  if (sectok->type == CPP_COMMA)
13692 	    {
13693 	      c_parser_consume_token (parser);
13694 	      c_parser_consume_token (parser);
13695 	      always = 2;
13696 	    }
13697 	  else if (sectok->type == CPP_NAME)
13698 	    {
13699 	      p = IDENTIFIER_POINTER (sectok->value);
13700 	      if (strcmp ("alloc", p) == 0
13701 		  || strcmp ("to", p) == 0
13702 		  || strcmp ("from", p) == 0
13703 		  || strcmp ("tofrom", p) == 0
13704 		  || strcmp ("release", p) == 0
13705 		  || strcmp ("delete", p) == 0)
13706 		{
13707 		  c_parser_consume_token (parser);
13708 		  always = 1;
13709 		}
13710 	    }
13711 	}
13712     }
13713 
13714   if (c_parser_next_token_is (parser, CPP_NAME)
13715       && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
13716     {
13717       const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
13718       if (strcmp ("alloc", p) == 0)
13719 	kind = GOMP_MAP_ALLOC;
13720       else if (strcmp ("to", p) == 0)
13721 	kind = always ? GOMP_MAP_ALWAYS_TO : GOMP_MAP_TO;
13722       else if (strcmp ("from", p) == 0)
13723 	kind = always ? GOMP_MAP_ALWAYS_FROM : GOMP_MAP_FROM;
13724       else if (strcmp ("tofrom", p) == 0)
13725 	kind = always ? GOMP_MAP_ALWAYS_TOFROM : GOMP_MAP_TOFROM;
13726       else if (strcmp ("release", p) == 0)
13727 	kind = GOMP_MAP_RELEASE;
13728       else if (strcmp ("delete", p) == 0)
13729 	kind = GOMP_MAP_DELETE;
13730       else
13731 	{
13732 	  c_parser_error (parser, "invalid map kind");
13733 	  c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
13734 				     "expected %<)%>");
13735 	  return list;
13736 	}
13737       c_parser_consume_token (parser);
13738       c_parser_consume_token (parser);
13739     }
13740   else if (always)
13741     {
13742       if (always_id_kind != C_ID_ID)
13743 	{
13744 	  c_parser_error (parser, "expected identifier");
13745 	  parens.skip_until_found_close (parser);
13746 	  return list;
13747 	}
13748 
13749       tree t = lookup_name (always_id);
13750       if (t == NULL_TREE)
13751 	{
13752 	  undeclared_variable (always_loc, always_id);
13753 	  t = error_mark_node;
13754 	}
13755       if (t != error_mark_node)
13756 	{
13757 	  tree u = build_omp_clause (clause_loc, OMP_CLAUSE_MAP);
13758 	  OMP_CLAUSE_DECL (u) = t;
13759 	  OMP_CLAUSE_CHAIN (u) = list;
13760 	  OMP_CLAUSE_SET_MAP_KIND (u, kind);
13761 	  list = u;
13762 	}
13763       if (always == 1)
13764 	{
13765 	  parens.skip_until_found_close (parser);
13766 	  return list;
13767 	}
13768     }
13769 
13770   nl = c_parser_omp_variable_list (parser, clause_loc, OMP_CLAUSE_MAP, list);
13771 
13772   for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
13773     OMP_CLAUSE_SET_MAP_KIND (c, kind);
13774 
13775   parens.skip_until_found_close (parser);
13776   return nl;
13777 }
13778 
13779 /* OpenMP 4.0:
13780    device ( expression ) */
13781 
13782 static tree
c_parser_omp_clause_device(c_parser * parser,tree list)13783 c_parser_omp_clause_device (c_parser *parser, tree list)
13784 {
13785   location_t clause_loc = c_parser_peek_token (parser)->location;
13786   matching_parens parens;
13787   if (parens.require_open (parser))
13788     {
13789       location_t expr_loc = c_parser_peek_token (parser)->location;
13790       c_expr expr = c_parser_expr_no_commas (parser, NULL);
13791       expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
13792       tree c, t = expr.value;
13793       t = c_fully_fold (t, false, NULL);
13794 
13795       parens.skip_until_found_close (parser);
13796 
13797       if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
13798 	{
13799 	  c_parser_error (parser, "expected integer expression");
13800 	  return list;
13801 	}
13802 
13803       check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE, "device");
13804 
13805       c = build_omp_clause (clause_loc, OMP_CLAUSE_DEVICE);
13806       OMP_CLAUSE_DEVICE_ID (c) = t;
13807       OMP_CLAUSE_CHAIN (c) = list;
13808       list = c;
13809     }
13810 
13811   return list;
13812 }
13813 
13814 /* OpenMP 4.0:
13815    dist_schedule ( static )
13816    dist_schedule ( static , expression ) */
13817 
13818 static tree
c_parser_omp_clause_dist_schedule(c_parser * parser,tree list)13819 c_parser_omp_clause_dist_schedule (c_parser *parser, tree list)
13820 {
13821   tree c, t = NULL_TREE;
13822   location_t loc = c_parser_peek_token (parser)->location;
13823 
13824   matching_parens parens;
13825   if (!parens.require_open (parser))
13826     return list;
13827 
13828   if (!c_parser_next_token_is_keyword (parser, RID_STATIC))
13829     {
13830       c_parser_error (parser, "invalid dist_schedule kind");
13831       c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
13832 				 "expected %<)%>");
13833       return list;
13834     }
13835 
13836   c_parser_consume_token (parser);
13837   if (c_parser_next_token_is (parser, CPP_COMMA))
13838     {
13839       c_parser_consume_token (parser);
13840 
13841       location_t expr_loc = c_parser_peek_token (parser)->location;
13842       c_expr expr = c_parser_expr_no_commas (parser, NULL);
13843       expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
13844       t = expr.value;
13845       t = c_fully_fold (t, false, NULL);
13846       parens.skip_until_found_close (parser);
13847     }
13848   else
13849     c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
13850 			       "expected %<,%> or %<)%>");
13851 
13852   /* check_no_duplicate_clause (list, OMP_CLAUSE_DIST_SCHEDULE,
13853 				"dist_schedule"); */
13854   if (omp_find_clause (list, OMP_CLAUSE_DIST_SCHEDULE))
13855     warning_at (loc, 0, "too many %qs clauses", "dist_schedule");
13856   if (t == error_mark_node)
13857     return list;
13858 
13859   c = build_omp_clause (loc, OMP_CLAUSE_DIST_SCHEDULE);
13860   OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
13861   OMP_CLAUSE_CHAIN (c) = list;
13862   return c;
13863 }
13864 
13865 /* OpenMP 4.0:
13866    proc_bind ( proc-bind-kind )
13867 
13868    proc-bind-kind:
13869      master | close | spread  */
13870 
13871 static tree
c_parser_omp_clause_proc_bind(c_parser * parser,tree list)13872 c_parser_omp_clause_proc_bind (c_parser *parser, tree list)
13873 {
13874   location_t clause_loc = c_parser_peek_token (parser)->location;
13875   enum omp_clause_proc_bind_kind kind;
13876   tree c;
13877 
13878   matching_parens parens;
13879   if (!parens.require_open (parser))
13880     return list;
13881 
13882   if (c_parser_next_token_is (parser, CPP_NAME))
13883     {
13884       const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
13885       if (strcmp ("master", p) == 0)
13886 	kind = OMP_CLAUSE_PROC_BIND_MASTER;
13887       else if (strcmp ("close", p) == 0)
13888 	kind = OMP_CLAUSE_PROC_BIND_CLOSE;
13889       else if (strcmp ("spread", p) == 0)
13890 	kind = OMP_CLAUSE_PROC_BIND_SPREAD;
13891       else
13892 	goto invalid_kind;
13893     }
13894   else
13895     goto invalid_kind;
13896 
13897   c_parser_consume_token (parser);
13898   parens.skip_until_found_close (parser);
13899   c = build_omp_clause (clause_loc, OMP_CLAUSE_PROC_BIND);
13900   OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
13901   OMP_CLAUSE_CHAIN (c) = list;
13902   return c;
13903 
13904  invalid_kind:
13905   c_parser_error (parser, "invalid proc_bind kind");
13906   parens.skip_until_found_close (parser);
13907   return list;
13908 }
13909 
13910 /* OpenMP 4.0:
13911    to ( variable-list ) */
13912 
13913 static tree
c_parser_omp_clause_to(c_parser * parser,tree list)13914 c_parser_omp_clause_to (c_parser *parser, tree list)
13915 {
13916   return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_TO, list);
13917 }
13918 
13919 /* OpenMP 4.0:
13920    from ( variable-list ) */
13921 
13922 static tree
c_parser_omp_clause_from(c_parser * parser,tree list)13923 c_parser_omp_clause_from (c_parser *parser, tree list)
13924 {
13925   return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_FROM, list);
13926 }
13927 
13928 /* OpenMP 4.0:
13929    uniform ( variable-list ) */
13930 
13931 static tree
c_parser_omp_clause_uniform(c_parser * parser,tree list)13932 c_parser_omp_clause_uniform (c_parser *parser, tree list)
13933 {
13934   /* The clauses location.  */
13935   location_t loc = c_parser_peek_token (parser)->location;
13936 
13937   matching_parens parens;
13938   if (parens.require_open (parser))
13939     {
13940       list = c_parser_omp_variable_list (parser, loc, OMP_CLAUSE_UNIFORM,
13941 					 list);
13942       parens.skip_until_found_close (parser);
13943     }
13944   return list;
13945 }
13946 
13947 /* Parse all OpenACC clauses.  The set clauses allowed by the directive
13948    is a bitmask in MASK.  Return the list of clauses found.  */
13949 
13950 static tree
13951 c_parser_oacc_all_clauses (c_parser *parser, omp_clause_mask mask,
13952 			   const char *where, bool finish_p = true)
13953 {
13954   tree clauses = NULL;
13955   bool first = true;
13956 
13957   while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
13958     {
13959       location_t here;
13960       pragma_omp_clause c_kind;
13961       const char *c_name;
13962       tree prev = clauses;
13963 
13964       if (!first && c_parser_next_token_is (parser, CPP_COMMA))
13965 	c_parser_consume_token (parser);
13966 
13967       here = c_parser_peek_token (parser)->location;
13968       c_kind = c_parser_omp_clause_name (parser);
13969 
13970       switch (c_kind)
13971 	{
13972 	case PRAGMA_OACC_CLAUSE_ASYNC:
13973 	  clauses = c_parser_oacc_clause_async (parser, clauses);
13974 	  c_name = "async";
13975 	  break;
13976 	case PRAGMA_OACC_CLAUSE_AUTO:
13977 	  clauses = c_parser_oacc_simple_clause (parser, OMP_CLAUSE_AUTO,
13978 						clauses);
13979 	  c_name = "auto";
13980 	  break;
13981 	case PRAGMA_OACC_CLAUSE_COLLAPSE:
13982 	  clauses = c_parser_omp_clause_collapse (parser, clauses);
13983 	  c_name = "collapse";
13984 	  break;
13985 	case PRAGMA_OACC_CLAUSE_COPY:
13986 	  clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13987 	  c_name = "copy";
13988 	  break;
13989 	case PRAGMA_OACC_CLAUSE_COPYIN:
13990 	  clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13991 	  c_name = "copyin";
13992 	  break;
13993 	case PRAGMA_OACC_CLAUSE_COPYOUT:
13994 	  clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13995 	  c_name = "copyout";
13996 	  break;
13997 	case PRAGMA_OACC_CLAUSE_CREATE:
13998 	  clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13999 	  c_name = "create";
14000 	  break;
14001 	case PRAGMA_OACC_CLAUSE_DELETE:
14002 	  clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
14003 	  c_name = "delete";
14004 	  break;
14005 	case PRAGMA_OMP_CLAUSE_DEFAULT:
14006 	  clauses = c_parser_omp_clause_default (parser, clauses, true);
14007 	  c_name = "default";
14008 	  break;
14009 	case PRAGMA_OACC_CLAUSE_DEVICE:
14010 	  clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
14011 	  c_name = "device";
14012 	  break;
14013 	case PRAGMA_OACC_CLAUSE_DEVICEPTR:
14014 	  clauses = c_parser_oacc_data_clause_deviceptr (parser, clauses);
14015 	  c_name = "deviceptr";
14016 	  break;
14017 	case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
14018 	  clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
14019 	  c_name = "device_resident";
14020 	  break;
14021 	case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE:
14022 	  clauses = c_parser_omp_clause_firstprivate (parser, clauses);
14023 	  c_name = "firstprivate";
14024 	  break;
14025 	case PRAGMA_OACC_CLAUSE_GANG:
14026 	  c_name = "gang";
14027 	  clauses = c_parser_oacc_shape_clause (parser, OMP_CLAUSE_GANG,
14028 						c_name, clauses);
14029 	  break;
14030 	case PRAGMA_OACC_CLAUSE_HOST:
14031 	  clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
14032 	  c_name = "host";
14033 	  break;
14034 	case PRAGMA_OACC_CLAUSE_IF:
14035 	  clauses = c_parser_omp_clause_if (parser, clauses, false);
14036 	  c_name = "if";
14037 	  break;
14038 	case PRAGMA_OACC_CLAUSE_INDEPENDENT:
14039 	  clauses = c_parser_oacc_simple_clause (parser, OMP_CLAUSE_INDEPENDENT,
14040 						clauses);
14041 	  c_name = "independent";
14042 	  break;
14043 	case PRAGMA_OACC_CLAUSE_LINK:
14044 	  clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
14045 	  c_name = "link";
14046 	  break;
14047 	case PRAGMA_OACC_CLAUSE_NUM_GANGS:
14048 	  clauses = c_parser_oacc_single_int_clause (parser,
14049 						     OMP_CLAUSE_NUM_GANGS,
14050 						     clauses);
14051 	  c_name = "num_gangs";
14052 	  break;
14053 	case PRAGMA_OACC_CLAUSE_NUM_WORKERS:
14054 	  clauses = c_parser_oacc_single_int_clause (parser,
14055 						     OMP_CLAUSE_NUM_WORKERS,
14056 						     clauses);
14057 	  c_name = "num_workers";
14058 	  break;
14059 	case PRAGMA_OACC_CLAUSE_PRESENT:
14060 	  clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
14061 	  c_name = "present";
14062 	  break;
14063 	case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
14064 	  clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
14065 	  c_name = "present_or_copy";
14066 	  break;
14067 	case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
14068 	  clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
14069 	  c_name = "present_or_copyin";
14070 	  break;
14071 	case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
14072 	  clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
14073 	  c_name = "present_or_copyout";
14074 	  break;
14075 	case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
14076 	  clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
14077 	  c_name = "present_or_create";
14078 	  break;
14079 	case PRAGMA_OACC_CLAUSE_PRIVATE:
14080 	  clauses = c_parser_omp_clause_private (parser, clauses);
14081 	  c_name = "private";
14082 	  break;
14083 	case PRAGMA_OACC_CLAUSE_REDUCTION:
14084 	  clauses = c_parser_omp_clause_reduction (parser, clauses);
14085 	  c_name = "reduction";
14086 	  break;
14087 	case PRAGMA_OACC_CLAUSE_SELF:
14088 	  clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
14089 	  c_name = "self";
14090 	  break;
14091 	case PRAGMA_OACC_CLAUSE_SEQ:
14092 	  clauses = c_parser_oacc_simple_clause (parser, OMP_CLAUSE_SEQ,
14093 						clauses);
14094 	  c_name = "seq";
14095 	  break;
14096 	case PRAGMA_OACC_CLAUSE_TILE:
14097 	  clauses = c_parser_oacc_clause_tile (parser, clauses);
14098 	  c_name = "tile";
14099 	  break;
14100 	case PRAGMA_OACC_CLAUSE_USE_DEVICE:
14101 	  clauses = c_parser_omp_clause_use_device_ptr (parser, clauses);
14102 	  c_name = "use_device";
14103 	  break;
14104 	case PRAGMA_OACC_CLAUSE_VECTOR:
14105 	  c_name = "vector";
14106 	  clauses = c_parser_oacc_shape_clause (parser, OMP_CLAUSE_VECTOR,
14107 						c_name,	clauses);
14108 	  break;
14109 	case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH:
14110 	  clauses = c_parser_oacc_single_int_clause (parser,
14111 						     OMP_CLAUSE_VECTOR_LENGTH,
14112 						     clauses);
14113 	  c_name = "vector_length";
14114 	  break;
14115 	case PRAGMA_OACC_CLAUSE_WAIT:
14116 	  clauses = c_parser_oacc_clause_wait (parser, clauses);
14117 	  c_name = "wait";
14118 	  break;
14119 	case PRAGMA_OACC_CLAUSE_WORKER:
14120 	  c_name = "worker";
14121 	  clauses = c_parser_oacc_shape_clause (parser, OMP_CLAUSE_WORKER,
14122 						c_name, clauses);
14123 	  break;
14124 	default:
14125 	  c_parser_error (parser, "expected %<#pragma acc%> clause");
14126 	  goto saw_error;
14127 	}
14128 
14129       first = false;
14130 
14131       if (((mask >> c_kind) & 1) == 0)
14132 	{
14133 	  /* Remove the invalid clause(s) from the list to avoid
14134 	     confusing the rest of the compiler.  */
14135 	  clauses = prev;
14136 	  error_at (here, "%qs is not valid for %qs", c_name, where);
14137 	}
14138     }
14139 
14140  saw_error:
14141   c_parser_skip_to_pragma_eol (parser);
14142 
14143   if (finish_p)
14144     return c_finish_omp_clauses (clauses, C_ORT_ACC);
14145 
14146   return clauses;
14147 }
14148 
14149 /* Parse all OpenMP clauses.  The set clauses allowed by the directive
14150    is a bitmask in MASK.  Return the list of clauses found.  */
14151 
14152 static tree
14153 c_parser_omp_all_clauses (c_parser *parser, omp_clause_mask mask,
14154 			  const char *where, bool finish_p = true)
14155 {
14156   tree clauses = NULL;
14157   bool first = true;
14158 
14159   while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
14160     {
14161       location_t here;
14162       pragma_omp_clause c_kind;
14163       const char *c_name;
14164       tree prev = clauses;
14165 
14166       if (!first && c_parser_next_token_is (parser, CPP_COMMA))
14167 	c_parser_consume_token (parser);
14168 
14169       here = c_parser_peek_token (parser)->location;
14170       c_kind = c_parser_omp_clause_name (parser);
14171 
14172       switch (c_kind)
14173 	{
14174 	case PRAGMA_OMP_CLAUSE_COLLAPSE:
14175 	  clauses = c_parser_omp_clause_collapse (parser, clauses);
14176 	  c_name = "collapse";
14177 	  break;
14178 	case PRAGMA_OMP_CLAUSE_COPYIN:
14179 	  clauses = c_parser_omp_clause_copyin (parser, clauses);
14180 	  c_name = "copyin";
14181 	  break;
14182 	case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
14183 	  clauses = c_parser_omp_clause_copyprivate (parser, clauses);
14184 	  c_name = "copyprivate";
14185 	  break;
14186 	case PRAGMA_OMP_CLAUSE_DEFAULT:
14187 	  clauses = c_parser_omp_clause_default (parser, clauses, false);
14188 	  c_name = "default";
14189 	  break;
14190 	case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
14191 	  clauses = c_parser_omp_clause_firstprivate (parser, clauses);
14192 	  c_name = "firstprivate";
14193 	  break;
14194 	case PRAGMA_OMP_CLAUSE_FINAL:
14195 	  clauses = c_parser_omp_clause_final (parser, clauses);
14196 	  c_name = "final";
14197 	  break;
14198 	case PRAGMA_OMP_CLAUSE_GRAINSIZE:
14199 	  clauses = c_parser_omp_clause_grainsize (parser, clauses);
14200 	  c_name = "grainsize";
14201 	  break;
14202 	case PRAGMA_OMP_CLAUSE_HINT:
14203 	  clauses = c_parser_omp_clause_hint (parser, clauses);
14204 	  c_name = "hint";
14205 	  break;
14206 	case PRAGMA_OMP_CLAUSE_DEFAULTMAP:
14207 	  clauses = c_parser_omp_clause_defaultmap (parser, clauses);
14208 	  c_name = "defaultmap";
14209 	  break;
14210 	case PRAGMA_OMP_CLAUSE_IF:
14211 	  clauses = c_parser_omp_clause_if (parser, clauses, true);
14212 	  c_name = "if";
14213 	  break;
14214 	case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
14215 	  clauses = c_parser_omp_clause_lastprivate (parser, clauses);
14216 	  c_name = "lastprivate";
14217 	  break;
14218 	case PRAGMA_OMP_CLAUSE_MERGEABLE:
14219 	  clauses = c_parser_omp_clause_mergeable (parser, clauses);
14220 	  c_name = "mergeable";
14221 	  break;
14222 	case PRAGMA_OMP_CLAUSE_NOWAIT:
14223 	  clauses = c_parser_omp_clause_nowait (parser, clauses);
14224 	  c_name = "nowait";
14225 	  break;
14226 	case PRAGMA_OMP_CLAUSE_NUM_TASKS:
14227 	  clauses = c_parser_omp_clause_num_tasks (parser, clauses);
14228 	  c_name = "num_tasks";
14229 	  break;
14230 	case PRAGMA_OMP_CLAUSE_NUM_THREADS:
14231 	  clauses = c_parser_omp_clause_num_threads (parser, clauses);
14232 	  c_name = "num_threads";
14233 	  break;
14234 	case PRAGMA_OMP_CLAUSE_ORDERED:
14235 	  clauses = c_parser_omp_clause_ordered (parser, clauses);
14236 	  c_name = "ordered";
14237 	  break;
14238 	case PRAGMA_OMP_CLAUSE_PRIORITY:
14239 	  clauses = c_parser_omp_clause_priority (parser, clauses);
14240 	  c_name = "priority";
14241 	  break;
14242 	case PRAGMA_OMP_CLAUSE_PRIVATE:
14243 	  clauses = c_parser_omp_clause_private (parser, clauses);
14244 	  c_name = "private";
14245 	  break;
14246 	case PRAGMA_OMP_CLAUSE_REDUCTION:
14247 	  clauses = c_parser_omp_clause_reduction (parser, clauses);
14248 	  c_name = "reduction";
14249 	  break;
14250 	case PRAGMA_OMP_CLAUSE_SCHEDULE:
14251 	  clauses = c_parser_omp_clause_schedule (parser, clauses);
14252 	  c_name = "schedule";
14253 	  break;
14254 	case PRAGMA_OMP_CLAUSE_SHARED:
14255 	  clauses = c_parser_omp_clause_shared (parser, clauses);
14256 	  c_name = "shared";
14257 	  break;
14258 	case PRAGMA_OMP_CLAUSE_UNTIED:
14259 	  clauses = c_parser_omp_clause_untied (parser, clauses);
14260 	  c_name = "untied";
14261 	  break;
14262 	case PRAGMA_OMP_CLAUSE_INBRANCH:
14263 	  clauses = c_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
14264 						clauses);
14265 	  c_name = "inbranch";
14266 	  break;
14267 	case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
14268 	  clauses = c_parser_omp_clause_branch (parser, OMP_CLAUSE_NOTINBRANCH,
14269 						clauses);
14270 	  c_name = "notinbranch";
14271 	  break;
14272 	case PRAGMA_OMP_CLAUSE_PARALLEL:
14273 	  clauses
14274 	    = c_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
14275 					      clauses);
14276 	  c_name = "parallel";
14277 	  if (!first)
14278 	    {
14279 	     clause_not_first:
14280 	      error_at (here, "%qs must be the first clause of %qs",
14281 			c_name, where);
14282 	      clauses = prev;
14283 	    }
14284 	  break;
14285 	case PRAGMA_OMP_CLAUSE_FOR:
14286 	  clauses
14287 	    = c_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
14288 					      clauses);
14289 	  c_name = "for";
14290 	  if (!first)
14291 	    goto clause_not_first;
14292 	  break;
14293 	case PRAGMA_OMP_CLAUSE_SECTIONS:
14294 	  clauses
14295 	    = c_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
14296 					      clauses);
14297 	  c_name = "sections";
14298 	  if (!first)
14299 	    goto clause_not_first;
14300 	  break;
14301 	case PRAGMA_OMP_CLAUSE_TASKGROUP:
14302 	  clauses
14303 	    = c_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
14304 					      clauses);
14305 	  c_name = "taskgroup";
14306 	  if (!first)
14307 	    goto clause_not_first;
14308 	  break;
14309 	case PRAGMA_OMP_CLAUSE_LINK:
14310 	  clauses
14311 	    = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_LINK, clauses);
14312 	  c_name = "link";
14313 	  break;
14314 	case PRAGMA_OMP_CLAUSE_TO:
14315 	  if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK)) != 0)
14316 	    clauses
14317 	      = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_TO_DECLARE,
14318 					      clauses);
14319 	  else
14320 	    clauses = c_parser_omp_clause_to (parser, clauses);
14321 	  c_name = "to";
14322 	  break;
14323 	case PRAGMA_OMP_CLAUSE_FROM:
14324 	  clauses = c_parser_omp_clause_from (parser, clauses);
14325 	  c_name = "from";
14326 	  break;
14327 	case PRAGMA_OMP_CLAUSE_UNIFORM:
14328 	  clauses = c_parser_omp_clause_uniform (parser, clauses);
14329 	  c_name = "uniform";
14330 	  break;
14331 	case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
14332 	  clauses = c_parser_omp_clause_num_teams (parser, clauses);
14333 	  c_name = "num_teams";
14334 	  break;
14335 	case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
14336 	  clauses = c_parser_omp_clause_thread_limit (parser, clauses);
14337 	  c_name = "thread_limit";
14338 	  break;
14339 	case PRAGMA_OMP_CLAUSE_ALIGNED:
14340 	  clauses = c_parser_omp_clause_aligned (parser, clauses);
14341 	  c_name = "aligned";
14342 	  break;
14343 	case PRAGMA_OMP_CLAUSE_LINEAR:
14344 	  clauses = c_parser_omp_clause_linear (parser, clauses);
14345 	  c_name = "linear";
14346 	  break;
14347 	case PRAGMA_OMP_CLAUSE_DEPEND:
14348 	  clauses = c_parser_omp_clause_depend (parser, clauses);
14349 	  c_name = "depend";
14350 	  break;
14351 	case PRAGMA_OMP_CLAUSE_MAP:
14352 	  clauses = c_parser_omp_clause_map (parser, clauses);
14353 	  c_name = "map";
14354 	  break;
14355 	case PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR:
14356 	  clauses = c_parser_omp_clause_use_device_ptr (parser, clauses);
14357 	  c_name = "use_device_ptr";
14358 	  break;
14359 	case PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR:
14360 	  clauses = c_parser_omp_clause_is_device_ptr (parser, clauses);
14361 	  c_name = "is_device_ptr";
14362 	  break;
14363 	case PRAGMA_OMP_CLAUSE_DEVICE:
14364 	  clauses = c_parser_omp_clause_device (parser, clauses);
14365 	  c_name = "device";
14366 	  break;
14367 	case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
14368 	  clauses = c_parser_omp_clause_dist_schedule (parser, clauses);
14369 	  c_name = "dist_schedule";
14370 	  break;
14371 	case PRAGMA_OMP_CLAUSE_PROC_BIND:
14372 	  clauses = c_parser_omp_clause_proc_bind (parser, clauses);
14373 	  c_name = "proc_bind";
14374 	  break;
14375 	case PRAGMA_OMP_CLAUSE_SAFELEN:
14376 	  clauses = c_parser_omp_clause_safelen (parser, clauses);
14377 	  c_name = "safelen";
14378 	  break;
14379 	case PRAGMA_OMP_CLAUSE_SIMDLEN:
14380 	  clauses = c_parser_omp_clause_simdlen (parser, clauses);
14381 	  c_name = "simdlen";
14382 	  break;
14383 	case PRAGMA_OMP_CLAUSE_NOGROUP:
14384 	  clauses = c_parser_omp_clause_nogroup (parser, clauses);
14385 	  c_name = "nogroup";
14386 	  break;
14387 	case PRAGMA_OMP_CLAUSE_THREADS:
14388 	  clauses
14389 	    = c_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_THREADS,
14390 					       clauses);
14391 	  c_name = "threads";
14392 	  break;
14393 	case PRAGMA_OMP_CLAUSE_SIMD:
14394 	  clauses
14395 	    = c_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_SIMD,
14396 					       clauses);
14397 	  c_name = "simd";
14398 	  break;
14399 	default:
14400 	  c_parser_error (parser, "expected %<#pragma omp%> clause");
14401 	  goto saw_error;
14402 	}
14403 
14404       first = false;
14405 
14406       if (((mask >> c_kind) & 1) == 0)
14407 	{
14408 	  /* Remove the invalid clause(s) from the list to avoid
14409 	     confusing the rest of the compiler.  */
14410 	  clauses = prev;
14411 	  error_at (here, "%qs is not valid for %qs", c_name, where);
14412 	}
14413     }
14414 
14415  saw_error:
14416   c_parser_skip_to_pragma_eol (parser);
14417 
14418   if (finish_p)
14419     {
14420       if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM)) != 0)
14421 	return c_finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
14422       return c_finish_omp_clauses (clauses, C_ORT_OMP);
14423     }
14424 
14425   return clauses;
14426 }
14427 
14428 /* OpenACC 2.0, OpenMP 2.5:
14429    structured-block:
14430      statement
14431 
14432    In practice, we're also interested in adding the statement to an
14433    outer node.  So it is convenient if we work around the fact that
14434    c_parser_statement calls add_stmt.  */
14435 
14436 static tree
c_parser_omp_structured_block(c_parser * parser,bool * if_p)14437 c_parser_omp_structured_block (c_parser *parser, bool *if_p)
14438 {
14439   tree stmt = push_stmt_list ();
14440   c_parser_statement (parser, if_p);
14441   return pop_stmt_list (stmt);
14442 }
14443 
14444 /* OpenACC 2.0:
14445    # pragma acc cache (variable-list) new-line
14446 
14447    LOC is the location of the #pragma token.
14448 */
14449 
14450 static tree
c_parser_oacc_cache(location_t loc,c_parser * parser)14451 c_parser_oacc_cache (location_t loc, c_parser *parser)
14452 {
14453   tree stmt, clauses;
14454 
14455   clauses = c_parser_omp_var_list_parens (parser, OMP_CLAUSE__CACHE_, NULL);
14456   clauses = c_finish_omp_clauses (clauses, C_ORT_ACC);
14457 
14458   c_parser_skip_to_pragma_eol (parser);
14459 
14460   stmt = make_node (OACC_CACHE);
14461   TREE_TYPE (stmt) = void_type_node;
14462   OACC_CACHE_CLAUSES (stmt) = clauses;
14463   SET_EXPR_LOCATION (stmt, loc);
14464   add_stmt (stmt);
14465 
14466   return stmt;
14467 }
14468 
14469 /* OpenACC 2.0:
14470    # pragma acc data oacc-data-clause[optseq] new-line
14471      structured-block
14472 
14473    LOC is the location of the #pragma token.
14474 */
14475 
14476 #define OACC_DATA_CLAUSE_MASK						\
14477 	( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY)		\
14478 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN)		\
14479 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT)		\
14480 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE)		\
14481 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR)		\
14482 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF)			\
14483 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT)		\
14484 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY)	\
14485 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN)	\
14486 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT)	\
14487 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) )
14488 
14489 static tree
c_parser_oacc_data(location_t loc,c_parser * parser,bool * if_p)14490 c_parser_oacc_data (location_t loc, c_parser *parser, bool *if_p)
14491 {
14492   tree stmt, clauses, block;
14493 
14494   clauses = c_parser_oacc_all_clauses (parser, OACC_DATA_CLAUSE_MASK,
14495 				       "#pragma acc data");
14496 
14497   block = c_begin_omp_parallel ();
14498   add_stmt (c_parser_omp_structured_block (parser, if_p));
14499 
14500   stmt = c_finish_oacc_data (loc, clauses, block);
14501 
14502   return stmt;
14503 }
14504 
14505 /* OpenACC 2.0:
14506    # pragma acc declare oacc-data-clause[optseq] new-line
14507 */
14508 
14509 #define OACC_DECLARE_CLAUSE_MASK					\
14510 	( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY)		\
14511 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN)		\
14512 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT)		\
14513 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE)		\
14514 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR)		\
14515 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT)	\
14516 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_LINK)		\
14517 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT)		\
14518 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY)	\
14519 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN)	\
14520 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT)	\
14521 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) )
14522 
14523 static void
c_parser_oacc_declare(c_parser * parser)14524 c_parser_oacc_declare (c_parser *parser)
14525 {
14526   location_t pragma_loc = c_parser_peek_token (parser)->location;
14527   tree clauses, stmt, t, decl;
14528 
14529   bool error = false;
14530 
14531   c_parser_consume_pragma (parser);
14532 
14533   clauses = c_parser_oacc_all_clauses (parser, OACC_DECLARE_CLAUSE_MASK,
14534 				       "#pragma acc declare");
14535   if (!clauses)
14536     {
14537       error_at (pragma_loc,
14538 		"no valid clauses specified in %<#pragma acc declare%>");
14539       return;
14540     }
14541 
14542   for (t = clauses; t; t = OMP_CLAUSE_CHAIN (t))
14543     {
14544       location_t loc = OMP_CLAUSE_LOCATION (t);
14545       decl = OMP_CLAUSE_DECL (t);
14546       if (!DECL_P (decl))
14547 	{
14548 	  error_at (loc, "array section in %<#pragma acc declare%>");
14549 	  error = true;
14550 	  continue;
14551 	}
14552 
14553       switch (OMP_CLAUSE_MAP_KIND (t))
14554 	{
14555 	case GOMP_MAP_FIRSTPRIVATE_POINTER:
14556 	case GOMP_MAP_FORCE_ALLOC:
14557 	case GOMP_MAP_FORCE_TO:
14558 	case GOMP_MAP_FORCE_DEVICEPTR:
14559 	case GOMP_MAP_DEVICE_RESIDENT:
14560 	  break;
14561 
14562 	case GOMP_MAP_LINK:
14563 	  if (!global_bindings_p ()
14564 	      && (TREE_STATIC (decl)
14565 	       || !DECL_EXTERNAL (decl)))
14566 	    {
14567 	      error_at (loc,
14568 			"%qD must be a global variable in "
14569 			"%<#pragma acc declare link%>",
14570 			decl);
14571 	      error = true;
14572 	      continue;
14573 	    }
14574 	  break;
14575 
14576 	default:
14577 	  if (global_bindings_p ())
14578 	    {
14579 	      error_at (loc, "invalid OpenACC clause at file scope");
14580 	      error = true;
14581 	      continue;
14582 	    }
14583 	  if (DECL_EXTERNAL (decl))
14584 	    {
14585 	      error_at (loc,
14586 			"invalid use of %<extern%> variable %qD "
14587 			"in %<#pragma acc declare%>", decl);
14588 	      error = true;
14589 	      continue;
14590 	    }
14591 	  else if (TREE_PUBLIC (decl))
14592 	    {
14593 	      error_at (loc,
14594 			"invalid use of %<global%> variable %qD "
14595 			"in %<#pragma acc declare%>", decl);
14596 	      error = true;
14597 	      continue;
14598 	    }
14599 	  break;
14600 	}
14601 
14602       if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl))
14603 	  || lookup_attribute ("omp declare target link",
14604 			       DECL_ATTRIBUTES (decl)))
14605 	{
14606 	  error_at (loc, "variable %qD used more than once with "
14607 		    "%<#pragma acc declare%>", decl);
14608 	  error = true;
14609 	  continue;
14610 	}
14611 
14612       if (!error)
14613 	{
14614 	  tree id;
14615 
14616 	  if (OMP_CLAUSE_MAP_KIND (t) == GOMP_MAP_LINK)
14617 	    id = get_identifier ("omp declare target link");
14618 	  else
14619 	    id = get_identifier ("omp declare target");
14620 
14621 	  DECL_ATTRIBUTES (decl)
14622 			   = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (decl));
14623 
14624 	  if (global_bindings_p ())
14625 	    {
14626 	      symtab_node *node = symtab_node::get (decl);
14627 	      if (node != NULL)
14628 		{
14629 		  node->offloadable = 1;
14630 		  if (ENABLE_OFFLOADING)
14631 		    {
14632 		      g->have_offload = true;
14633 		      if (is_a <varpool_node *> (node))
14634 			vec_safe_push (offload_vars, decl);
14635 		    }
14636 		}
14637 	    }
14638 	}
14639     }
14640 
14641   if (error || global_bindings_p ())
14642     return;
14643 
14644   stmt = make_node (OACC_DECLARE);
14645   TREE_TYPE (stmt) = void_type_node;
14646   OACC_DECLARE_CLAUSES (stmt) = clauses;
14647   SET_EXPR_LOCATION (stmt, pragma_loc);
14648 
14649   add_stmt (stmt);
14650 
14651   return;
14652 }
14653 
14654 /* OpenACC 2.0:
14655    # pragma acc enter data oacc-enter-data-clause[optseq] new-line
14656 
14657    or
14658 
14659    # pragma acc exit data oacc-exit-data-clause[optseq] new-line
14660 
14661 
14662    LOC is the location of the #pragma token.
14663 */
14664 
14665 #define OACC_ENTER_DATA_CLAUSE_MASK					\
14666 	( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF)			\
14667 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC)		\
14668 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN)		\
14669 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE)		\
14670 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN)	\
14671 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE)	\
14672 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
14673 
14674 #define OACC_EXIT_DATA_CLAUSE_MASK					\
14675 	( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF)			\
14676 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC)		\
14677 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT)		\
14678 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) 		\
14679 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
14680 
14681 static void
c_parser_oacc_enter_exit_data(c_parser * parser,bool enter)14682 c_parser_oacc_enter_exit_data (c_parser *parser, bool enter)
14683 {
14684   location_t loc = c_parser_peek_token (parser)->location;
14685   tree clauses, stmt;
14686   const char *p = "";
14687 
14688   c_parser_consume_pragma (parser);
14689 
14690   if (c_parser_next_token_is (parser, CPP_NAME))
14691     {
14692       p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
14693       c_parser_consume_token (parser);
14694     }
14695 
14696   if (strcmp (p, "data") != 0)
14697     {
14698       error_at (loc, "expected %<data%> after %<#pragma acc %s%>",
14699 		enter ? "enter" : "exit");
14700       parser->error = true;
14701       c_parser_skip_to_pragma_eol (parser);
14702       return;
14703     }
14704 
14705   if (enter)
14706     clauses = c_parser_oacc_all_clauses (parser, OACC_ENTER_DATA_CLAUSE_MASK,
14707 					 "#pragma acc enter data");
14708   else
14709     clauses = c_parser_oacc_all_clauses (parser, OACC_EXIT_DATA_CLAUSE_MASK,
14710 					 "#pragma acc exit data");
14711 
14712   if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
14713     {
14714       error_at (loc, "%<#pragma acc %s data%> has no data movement clause",
14715 		enter ? "enter" : "exit");
14716       return;
14717     }
14718 
14719   stmt = enter ? make_node (OACC_ENTER_DATA) : make_node (OACC_EXIT_DATA);
14720   TREE_TYPE (stmt) = void_type_node;
14721   OMP_STANDALONE_CLAUSES (stmt) = clauses;
14722   SET_EXPR_LOCATION (stmt, loc);
14723   add_stmt (stmt);
14724 }
14725 
14726 
14727 /* OpenACC 2.0:
14728    # pragma acc host_data oacc-data-clause[optseq] new-line
14729      structured-block
14730 */
14731 
14732 #define OACC_HOST_DATA_CLAUSE_MASK					\
14733 	( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_USE_DEVICE) )
14734 
14735 static tree
c_parser_oacc_host_data(location_t loc,c_parser * parser,bool * if_p)14736 c_parser_oacc_host_data (location_t loc, c_parser *parser, bool *if_p)
14737 {
14738   tree stmt, clauses, block;
14739 
14740   clauses = c_parser_oacc_all_clauses (parser, OACC_HOST_DATA_CLAUSE_MASK,
14741 				       "#pragma acc host_data");
14742 
14743   block = c_begin_omp_parallel ();
14744   add_stmt (c_parser_omp_structured_block (parser, if_p));
14745   stmt = c_finish_oacc_host_data (loc, clauses, block);
14746   return stmt;
14747 }
14748 
14749 
14750 /* OpenACC 2.0:
14751 
14752    # pragma acc loop oacc-loop-clause[optseq] new-line
14753      structured-block
14754 
14755    LOC is the location of the #pragma token.
14756 */
14757 
14758 #define OACC_LOOP_CLAUSE_MASK						\
14759 	( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE)		\
14760 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE)		\
14761 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION)		\
14762 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG)		\
14763 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER)		\
14764 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR)		\
14765 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_AUTO)		\
14766 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_INDEPENDENT) 	\
14767 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ)			\
14768 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_TILE) )
14769 static tree
c_parser_oacc_loop(location_t loc,c_parser * parser,char * p_name,omp_clause_mask mask,tree * cclauses,bool * if_p)14770 c_parser_oacc_loop (location_t loc, c_parser *parser, char *p_name,
14771 		    omp_clause_mask mask, tree *cclauses, bool *if_p)
14772 {
14773   bool is_parallel = ((mask >> PRAGMA_OACC_CLAUSE_REDUCTION) & 1) == 1;
14774 
14775   strcat (p_name, " loop");
14776   mask |= OACC_LOOP_CLAUSE_MASK;
14777 
14778   tree clauses = c_parser_oacc_all_clauses (parser, mask, p_name,
14779 					    cclauses == NULL);
14780   if (cclauses)
14781     {
14782       clauses = c_oacc_split_loop_clauses (clauses, cclauses, is_parallel);
14783       if (*cclauses)
14784 	*cclauses = c_finish_omp_clauses (*cclauses, C_ORT_ACC);
14785       if (clauses)
14786 	clauses = c_finish_omp_clauses (clauses, C_ORT_ACC);
14787     }
14788 
14789   tree block = c_begin_compound_stmt (true);
14790   tree stmt = c_parser_omp_for_loop (loc, parser, OACC_LOOP, clauses, NULL,
14791 				     if_p);
14792   block = c_end_compound_stmt (loc, block, true);
14793   add_stmt (block);
14794 
14795   return stmt;
14796 }
14797 
14798 /* OpenACC 2.0:
14799    # pragma acc kernels oacc-kernels-clause[optseq] new-line
14800      structured-block
14801 
14802    or
14803 
14804    # pragma acc parallel oacc-parallel-clause[optseq] new-line
14805      structured-block
14806 
14807    LOC is the location of the #pragma token.
14808 */
14809 
14810 #define OACC_KERNELS_CLAUSE_MASK					\
14811 	( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC)		\
14812 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY)		\
14813 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN)		\
14814 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT)		\
14815 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE)		\
14816 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT)		\
14817 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR)		\
14818 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF)			\
14819 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS)		\
14820 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS)		\
14821 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT)		\
14822 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY)	\
14823 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN)	\
14824 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT)	\
14825 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE)	\
14826 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH)	\
14827 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
14828 
14829 #define OACC_PARALLEL_CLAUSE_MASK					\
14830 	( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC)		\
14831 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY)		\
14832 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN)		\
14833 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT)		\
14834 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE)		\
14835 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT)		\
14836 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR)		\
14837 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF)			\
14838 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE)		\
14839 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE)	\
14840 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS)		\
14841 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS)		\
14842 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT)		\
14843 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY)	\
14844 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN)	\
14845 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT)	\
14846 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE)	\
14847 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION)		\
14848 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH)	\
14849 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
14850 
14851 static tree
c_parser_oacc_kernels_parallel(location_t loc,c_parser * parser,enum pragma_kind p_kind,char * p_name,bool * if_p)14852 c_parser_oacc_kernels_parallel (location_t loc, c_parser *parser,
14853 				enum pragma_kind p_kind, char *p_name,
14854 				bool *if_p)
14855 {
14856   omp_clause_mask mask;
14857   enum tree_code code;
14858   switch (p_kind)
14859     {
14860     case PRAGMA_OACC_KERNELS:
14861       strcat (p_name, " kernels");
14862       mask = OACC_KERNELS_CLAUSE_MASK;
14863       code = OACC_KERNELS;
14864       break;
14865     case PRAGMA_OACC_PARALLEL:
14866       strcat (p_name, " parallel");
14867       mask = OACC_PARALLEL_CLAUSE_MASK;
14868       code = OACC_PARALLEL;
14869       break;
14870     default:
14871       gcc_unreachable ();
14872     }
14873 
14874   if (c_parser_next_token_is (parser, CPP_NAME))
14875     {
14876       const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
14877       if (strcmp (p, "loop") == 0)
14878 	{
14879 	  c_parser_consume_token (parser);
14880 	  tree block = c_begin_omp_parallel ();
14881 	  tree clauses;
14882 	  c_parser_oacc_loop (loc, parser, p_name, mask, &clauses, if_p);
14883 	  return c_finish_omp_construct (loc, code, block, clauses);
14884 	}
14885     }
14886 
14887   tree clauses = c_parser_oacc_all_clauses (parser, mask, p_name);
14888 
14889   tree block = c_begin_omp_parallel ();
14890   add_stmt (c_parser_omp_structured_block (parser, if_p));
14891 
14892   return c_finish_omp_construct (loc, code, block, clauses);
14893 }
14894 
14895 /* OpenACC 2.0:
14896    # pragma acc routine oacc-routine-clause[optseq] new-line
14897      function-definition
14898 
14899    # pragma acc routine ( name ) oacc-routine-clause[optseq] new-line
14900 */
14901 
14902 #define OACC_ROUTINE_CLAUSE_MASK					\
14903 	( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG)		\
14904 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER)		\
14905 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR)		\
14906 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) )
14907 
14908 /* Parse an OpenACC routine directive.  For named directives, we apply
14909    immediately to the named function.  For unnamed ones we then parse
14910    a declaration or definition, which must be for a function.  */
14911 
14912 static void
c_parser_oacc_routine(c_parser * parser,enum pragma_context context)14913 c_parser_oacc_routine (c_parser *parser, enum pragma_context context)
14914 {
14915   gcc_checking_assert (context == pragma_external);
14916 
14917   oacc_routine_data data;
14918   data.error_seen = false;
14919   data.fndecl_seen = false;
14920   data.clauses = NULL_TREE;
14921   data.loc = c_parser_peek_token (parser)->location;
14922 
14923   c_parser_consume_pragma (parser);
14924 
14925   /* Look for optional '( name )'.  */
14926   if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
14927     {
14928       c_parser_consume_token (parser); /* '(' */
14929 
14930       tree decl = NULL_TREE;
14931       c_token *name_token = c_parser_peek_token (parser);
14932       location_t name_loc = name_token->location;
14933       if (name_token->type == CPP_NAME
14934 	  && (name_token->id_kind == C_ID_ID
14935 	      || name_token->id_kind == C_ID_TYPENAME))
14936 	{
14937 	  decl = lookup_name (name_token->value);
14938 	  if (!decl)
14939 	    error_at (name_loc,
14940 		      "%qE has not been declared", name_token->value);
14941 	  c_parser_consume_token (parser);
14942 	}
14943       else
14944 	c_parser_error (parser, "expected function name");
14945 
14946       if (!decl
14947 	  || !c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
14948 	{
14949 	  c_parser_skip_to_pragma_eol (parser, false);
14950 	  return;
14951 	}
14952 
14953       data.clauses
14954 	= c_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
14955 				     "#pragma acc routine");
14956 
14957       if (TREE_CODE (decl) != FUNCTION_DECL)
14958 	{
14959 	  error_at (name_loc, "%qD does not refer to a function", decl);
14960 	  return;
14961 	}
14962 
14963       c_finish_oacc_routine (&data, decl, false);
14964     }
14965   else /* No optional '( name )'.  */
14966     {
14967       data.clauses
14968 	= c_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
14969 				     "#pragma acc routine");
14970 
14971       /* Emit a helpful diagnostic if there's another pragma following this
14972 	 one.  Also don't allow a static assertion declaration, as in the
14973 	 following we'll just parse a *single* "declaration or function
14974 	 definition", and the static assertion counts an one.  */
14975       if (c_parser_next_token_is (parser, CPP_PRAGMA)
14976 	  || c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT))
14977 	{
14978 	  error_at (data.loc,
14979 		    "%<#pragma acc routine%> not immediately followed by"
14980 		    " function declaration or definition");
14981 	  /* ..., and then just keep going.  */
14982 	  return;
14983 	}
14984 
14985       /* We only have to consider the pragma_external case here.  */
14986       if (c_parser_next_token_is (parser, CPP_KEYWORD)
14987 	  && c_parser_peek_token (parser)->keyword == RID_EXTENSION)
14988 	{
14989 	  int ext = disable_extension_diagnostics ();
14990 	  do
14991 	    c_parser_consume_token (parser);
14992 	  while (c_parser_next_token_is (parser, CPP_KEYWORD)
14993 		 && c_parser_peek_token (parser)->keyword == RID_EXTENSION);
14994 	  c_parser_declaration_or_fndef (parser, true, true, true, false, true,
14995 					 NULL, vNULL, &data);
14996 	  restore_extension_diagnostics (ext);
14997 	}
14998       else
14999 	c_parser_declaration_or_fndef (parser, true, true, true, false, true,
15000 				       NULL, vNULL, &data);
15001     }
15002 }
15003 
15004 /* Finalize an OpenACC routine pragma, applying it to FNDECL.
15005    IS_DEFN is true if we're applying it to the definition.  */
15006 
15007 static void
c_finish_oacc_routine(struct oacc_routine_data * data,tree fndecl,bool is_defn)15008 c_finish_oacc_routine (struct oacc_routine_data *data, tree fndecl,
15009 		       bool is_defn)
15010 {
15011   /* Keep going if we're in error reporting mode.  */
15012   if (data->error_seen
15013       || fndecl == error_mark_node)
15014     return;
15015 
15016   if (data->fndecl_seen)
15017     {
15018       error_at (data->loc,
15019 		"%<#pragma acc routine%> not immediately followed by"
15020 		" a single function declaration or definition");
15021       data->error_seen = true;
15022       return;
15023     }
15024   if (fndecl == NULL_TREE || TREE_CODE (fndecl) != FUNCTION_DECL)
15025     {
15026       error_at (data->loc,
15027 		"%<#pragma acc routine%> not immediately followed by"
15028 		" function declaration or definition");
15029       data->error_seen = true;
15030       return;
15031     }
15032 
15033   if (oacc_get_fn_attrib (fndecl))
15034     {
15035       error_at (data->loc,
15036 		"%<#pragma acc routine%> already applied to %qD", fndecl);
15037       data->error_seen = true;
15038       return;
15039     }
15040 
15041   if (TREE_USED (fndecl) || (!is_defn && DECL_SAVED_TREE (fndecl)))
15042     {
15043       error_at (data->loc,
15044 		TREE_USED (fndecl)
15045 		? G_("%<#pragma acc routine%> must be applied before use")
15046 		: G_("%<#pragma acc routine%> must be applied before "
15047 		     "definition"));
15048       data->error_seen = true;
15049       return;
15050     }
15051 
15052   /* Process the routine's dimension clauses.  */
15053   tree dims = oacc_build_routine_dims (data->clauses);
15054   oacc_replace_fn_attrib (fndecl, dims);
15055 
15056   /* Add an "omp declare target" attribute.  */
15057   DECL_ATTRIBUTES (fndecl)
15058     = tree_cons (get_identifier ("omp declare target"),
15059 		 NULL_TREE, DECL_ATTRIBUTES (fndecl));
15060 
15061   /* Remember that we've used this "#pragma acc routine".  */
15062   data->fndecl_seen = true;
15063 }
15064 
15065 /* OpenACC 2.0:
15066    # pragma acc update oacc-update-clause[optseq] new-line
15067 */
15068 
15069 #define OACC_UPDATE_CLAUSE_MASK						\
15070 	( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC)		\
15071 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE)		\
15072 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST)		\
15073 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF)			\
15074 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SELF)		\
15075 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
15076 
15077 static void
c_parser_oacc_update(c_parser * parser)15078 c_parser_oacc_update (c_parser *parser)
15079 {
15080   location_t loc = c_parser_peek_token (parser)->location;
15081 
15082   c_parser_consume_pragma (parser);
15083 
15084   tree clauses = c_parser_oacc_all_clauses (parser, OACC_UPDATE_CLAUSE_MASK,
15085 					    "#pragma acc update");
15086   if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
15087     {
15088       error_at (loc,
15089 		"%<#pragma acc update%> must contain at least one "
15090 		"%<device%> or %<host%> or %<self%> clause");
15091       return;
15092     }
15093 
15094   if (parser->error)
15095     return;
15096 
15097   tree stmt = make_node (OACC_UPDATE);
15098   TREE_TYPE (stmt) = void_type_node;
15099   OACC_UPDATE_CLAUSES (stmt) = clauses;
15100   SET_EXPR_LOCATION (stmt, loc);
15101   add_stmt (stmt);
15102 }
15103 
15104 /* OpenACC 2.0:
15105    # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
15106 
15107    LOC is the location of the #pragma token.
15108 */
15109 
15110 #define OACC_WAIT_CLAUSE_MASK						\
15111 	( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) )
15112 
15113 static tree
c_parser_oacc_wait(location_t loc,c_parser * parser,char * p_name)15114 c_parser_oacc_wait (location_t loc, c_parser *parser, char *p_name)
15115 {
15116   tree clauses, list = NULL_TREE, stmt = NULL_TREE;
15117 
15118   if (c_parser_peek_token (parser)->type == CPP_OPEN_PAREN)
15119     list = c_parser_oacc_wait_list (parser, loc, list);
15120 
15121   strcpy (p_name, " wait");
15122   clauses = c_parser_oacc_all_clauses (parser, OACC_WAIT_CLAUSE_MASK, p_name);
15123   stmt = c_finish_oacc_wait (loc, list, clauses);
15124   add_stmt (stmt);
15125 
15126   return stmt;
15127 }
15128 
15129 /* OpenMP 2.5:
15130    # pragma omp atomic new-line
15131      expression-stmt
15132 
15133    expression-stmt:
15134      x binop= expr | x++ | ++x | x-- | --x
15135    binop:
15136      +, *, -, /, &, ^, |, <<, >>
15137 
15138   where x is an lvalue expression with scalar type.
15139 
15140    OpenMP 3.1:
15141    # pragma omp atomic new-line
15142      update-stmt
15143 
15144    # pragma omp atomic read new-line
15145      read-stmt
15146 
15147    # pragma omp atomic write new-line
15148      write-stmt
15149 
15150    # pragma omp atomic update new-line
15151      update-stmt
15152 
15153    # pragma omp atomic capture new-line
15154      capture-stmt
15155 
15156    # pragma omp atomic capture new-line
15157      capture-block
15158 
15159    read-stmt:
15160      v = x
15161    write-stmt:
15162      x = expr
15163    update-stmt:
15164      expression-stmt | x = x binop expr
15165    capture-stmt:
15166      v = expression-stmt
15167    capture-block:
15168      { v = x; update-stmt; } | { update-stmt; v = x; }
15169 
15170    OpenMP 4.0:
15171    update-stmt:
15172      expression-stmt | x = x binop expr | x = expr binop x
15173    capture-stmt:
15174      v = update-stmt
15175    capture-block:
15176      { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
15177 
15178   where x and v are lvalue expressions with scalar type.
15179 
15180   LOC is the location of the #pragma token.  */
15181 
15182 static void
c_parser_omp_atomic(location_t loc,c_parser * parser)15183 c_parser_omp_atomic (location_t loc, c_parser *parser)
15184 {
15185   tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE;
15186   tree lhs1 = NULL_TREE, rhs1 = NULL_TREE;
15187   tree stmt, orig_lhs, unfolded_lhs = NULL_TREE, unfolded_lhs1 = NULL_TREE;
15188   enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
15189   struct c_expr expr;
15190   location_t eloc;
15191   bool structured_block = false;
15192   bool swapped = false;
15193   bool seq_cst = false;
15194   bool non_lvalue_p;
15195 
15196   if (c_parser_next_token_is (parser, CPP_NAME))
15197     {
15198       const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
15199       if (!strcmp (p, "seq_cst"))
15200 	{
15201 	  seq_cst = true;
15202 	  c_parser_consume_token (parser);
15203 	  if (c_parser_next_token_is (parser, CPP_COMMA)
15204 	      && c_parser_peek_2nd_token (parser)->type == CPP_NAME)
15205 	    c_parser_consume_token (parser);
15206 	}
15207     }
15208   if (c_parser_next_token_is (parser, CPP_NAME))
15209     {
15210       const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
15211 
15212       if (!strcmp (p, "read"))
15213 	code = OMP_ATOMIC_READ;
15214       else if (!strcmp (p, "write"))
15215 	code = NOP_EXPR;
15216       else if (!strcmp (p, "update"))
15217 	code = OMP_ATOMIC;
15218       else if (!strcmp (p, "capture"))
15219 	code = OMP_ATOMIC_CAPTURE_NEW;
15220       else
15221 	p = NULL;
15222       if (p)
15223 	c_parser_consume_token (parser);
15224     }
15225   if (!seq_cst)
15226     {
15227       if (c_parser_next_token_is (parser, CPP_COMMA)
15228 	  && c_parser_peek_2nd_token (parser)->type == CPP_NAME)
15229 	c_parser_consume_token (parser);
15230 
15231       if (c_parser_next_token_is (parser, CPP_NAME))
15232 	{
15233 	  const char *p
15234 	    = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
15235 	  if (!strcmp (p, "seq_cst"))
15236 	    {
15237 	      seq_cst = true;
15238 	      c_parser_consume_token (parser);
15239 	    }
15240 	}
15241     }
15242   c_parser_skip_to_pragma_eol (parser);
15243 
15244   switch (code)
15245     {
15246     case OMP_ATOMIC_READ:
15247     case NOP_EXPR: /* atomic write */
15248       v = c_parser_cast_expression (parser, NULL).value;
15249       non_lvalue_p = !lvalue_p (v);
15250       v = c_fully_fold (v, false, NULL, true);
15251       if (v == error_mark_node)
15252 	goto saw_error;
15253       if (non_lvalue_p)
15254 	v = non_lvalue (v);
15255       loc = c_parser_peek_token (parser)->location;
15256       if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
15257 	goto saw_error;
15258       if (code == NOP_EXPR)
15259 	{
15260 	  lhs = c_parser_expression (parser).value;
15261 	  lhs = c_fully_fold (lhs, false, NULL);
15262 	  if (lhs == error_mark_node)
15263 	    goto saw_error;
15264 	}
15265       else
15266 	{
15267 	  lhs = c_parser_cast_expression (parser, NULL).value;
15268 	  non_lvalue_p = !lvalue_p (lhs);
15269 	  lhs = c_fully_fold (lhs, false, NULL, true);
15270 	  if (lhs == error_mark_node)
15271 	    goto saw_error;
15272 	  if (non_lvalue_p)
15273 	    lhs = non_lvalue (lhs);
15274 	}
15275       if (code == NOP_EXPR)
15276 	{
15277 	  /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
15278 	     opcode.  */
15279 	  code = OMP_ATOMIC;
15280 	  rhs = lhs;
15281 	  lhs = v;
15282 	  v = NULL_TREE;
15283 	}
15284       goto done;
15285     case OMP_ATOMIC_CAPTURE_NEW:
15286       if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
15287 	{
15288 	  c_parser_consume_token (parser);
15289 	  structured_block = true;
15290 	}
15291       else
15292 	{
15293 	  v = c_parser_cast_expression (parser, NULL).value;
15294 	  non_lvalue_p = !lvalue_p (v);
15295 	  v = c_fully_fold (v, false, NULL, true);
15296 	  if (v == error_mark_node)
15297 	    goto saw_error;
15298 	  if (non_lvalue_p)
15299 	    v = non_lvalue (v);
15300 	  if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
15301 	    goto saw_error;
15302 	}
15303       break;
15304     default:
15305       break;
15306     }
15307 
15308   /* For structured_block case we don't know yet whether
15309      old or new x should be captured.  */
15310 restart:
15311   eloc = c_parser_peek_token (parser)->location;
15312   expr = c_parser_cast_expression (parser, NULL);
15313   lhs = expr.value;
15314   expr = default_function_array_conversion (eloc, expr);
15315   unfolded_lhs = expr.value;
15316   lhs = c_fully_fold (lhs, false, NULL, true);
15317   orig_lhs = lhs;
15318   switch (TREE_CODE (lhs))
15319     {
15320     case ERROR_MARK:
15321     saw_error:
15322       c_parser_skip_to_end_of_block_or_statement (parser);
15323       if (structured_block)
15324 	{
15325 	  if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
15326 	    c_parser_consume_token (parser);
15327 	  else if (code == OMP_ATOMIC_CAPTURE_NEW)
15328 	    {
15329 	      c_parser_skip_to_end_of_block_or_statement (parser);
15330 	      if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
15331 		c_parser_consume_token (parser);
15332 	    }
15333 	}
15334       return;
15335 
15336     case POSTINCREMENT_EXPR:
15337       if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
15338 	code = OMP_ATOMIC_CAPTURE_OLD;
15339       /* FALLTHROUGH */
15340     case PREINCREMENT_EXPR:
15341       lhs = TREE_OPERAND (lhs, 0);
15342       unfolded_lhs = NULL_TREE;
15343       opcode = PLUS_EXPR;
15344       rhs = integer_one_node;
15345       break;
15346 
15347     case POSTDECREMENT_EXPR:
15348       if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
15349 	code = OMP_ATOMIC_CAPTURE_OLD;
15350       /* FALLTHROUGH */
15351     case PREDECREMENT_EXPR:
15352       lhs = TREE_OPERAND (lhs, 0);
15353       unfolded_lhs = NULL_TREE;
15354       opcode = MINUS_EXPR;
15355       rhs = integer_one_node;
15356       break;
15357 
15358     case COMPOUND_EXPR:
15359       if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
15360 	  && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
15361 	  && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
15362 	  && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
15363 	  && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
15364 					      (TREE_OPERAND (lhs, 1), 0), 0)))
15365 	     == BOOLEAN_TYPE)
15366 	/* Undo effects of boolean_increment for post {in,de}crement.  */
15367 	lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
15368       /* FALLTHRU */
15369     case MODIFY_EXPR:
15370       if (TREE_CODE (lhs) == MODIFY_EXPR
15371 	  && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
15372 	{
15373 	  /* Undo effects of boolean_increment.  */
15374 	  if (integer_onep (TREE_OPERAND (lhs, 1)))
15375 	    {
15376 	      /* This is pre or post increment.  */
15377 	      rhs = TREE_OPERAND (lhs, 1);
15378 	      lhs = TREE_OPERAND (lhs, 0);
15379 	      unfolded_lhs = NULL_TREE;
15380 	      opcode = NOP_EXPR;
15381 	      if (code == OMP_ATOMIC_CAPTURE_NEW
15382 		  && !structured_block
15383 		  && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
15384 		code = OMP_ATOMIC_CAPTURE_OLD;
15385 	      break;
15386 	    }
15387 	  if (TREE_CODE (TREE_OPERAND (lhs, 1)) == TRUTH_NOT_EXPR
15388 	      && TREE_OPERAND (lhs, 0)
15389 		 == TREE_OPERAND (TREE_OPERAND (lhs, 1), 0))
15390 	    {
15391 	      /* This is pre or post decrement.  */
15392 	      rhs = TREE_OPERAND (lhs, 1);
15393 	      lhs = TREE_OPERAND (lhs, 0);
15394 	      unfolded_lhs = NULL_TREE;
15395 	      opcode = NOP_EXPR;
15396 	      if (code == OMP_ATOMIC_CAPTURE_NEW
15397 		  && !structured_block
15398 		  && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
15399 		code = OMP_ATOMIC_CAPTURE_OLD;
15400 	      break;
15401 	    }
15402 	}
15403       /* FALLTHRU */
15404     default:
15405       if (!lvalue_p (unfolded_lhs))
15406 	lhs = non_lvalue (lhs);
15407       switch (c_parser_peek_token (parser)->type)
15408 	{
15409 	case CPP_MULT_EQ:
15410 	  opcode = MULT_EXPR;
15411 	  break;
15412 	case CPP_DIV_EQ:
15413 	  opcode = TRUNC_DIV_EXPR;
15414 	  break;
15415 	case CPP_PLUS_EQ:
15416 	  opcode = PLUS_EXPR;
15417 	  break;
15418 	case CPP_MINUS_EQ:
15419 	  opcode = MINUS_EXPR;
15420 	  break;
15421 	case CPP_LSHIFT_EQ:
15422 	  opcode = LSHIFT_EXPR;
15423 	  break;
15424 	case CPP_RSHIFT_EQ:
15425 	  opcode = RSHIFT_EXPR;
15426 	  break;
15427 	case CPP_AND_EQ:
15428 	  opcode = BIT_AND_EXPR;
15429 	  break;
15430 	case CPP_OR_EQ:
15431 	  opcode = BIT_IOR_EXPR;
15432 	  break;
15433 	case CPP_XOR_EQ:
15434 	  opcode = BIT_XOR_EXPR;
15435 	  break;
15436 	case CPP_EQ:
15437 	  c_parser_consume_token (parser);
15438 	  eloc = c_parser_peek_token (parser)->location;
15439 	  expr = c_parser_expr_no_commas (parser, NULL, unfolded_lhs);
15440 	  rhs1 = expr.value;
15441 	  switch (TREE_CODE (rhs1))
15442 	    {
15443 	    case MULT_EXPR:
15444 	    case TRUNC_DIV_EXPR:
15445 	    case RDIV_EXPR:
15446 	    case PLUS_EXPR:
15447 	    case MINUS_EXPR:
15448 	    case LSHIFT_EXPR:
15449 	    case RSHIFT_EXPR:
15450 	    case BIT_AND_EXPR:
15451 	    case BIT_IOR_EXPR:
15452 	    case BIT_XOR_EXPR:
15453 	      if (c_tree_equal (TREE_OPERAND (rhs1, 0), unfolded_lhs))
15454 		{
15455 		  opcode = TREE_CODE (rhs1);
15456 		  rhs = c_fully_fold (TREE_OPERAND (rhs1, 1), false, NULL,
15457 				      true);
15458 		  rhs1 = c_fully_fold (TREE_OPERAND (rhs1, 0), false, NULL,
15459 				       true);
15460 		  goto stmt_done;
15461 		}
15462 	      if (c_tree_equal (TREE_OPERAND (rhs1, 1), unfolded_lhs))
15463 		{
15464 		  opcode = TREE_CODE (rhs1);
15465 		  rhs = c_fully_fold (TREE_OPERAND (rhs1, 0), false, NULL,
15466 				      true);
15467 		  rhs1 = c_fully_fold (TREE_OPERAND (rhs1, 1), false, NULL,
15468 				       true);
15469 		  swapped = !commutative_tree_code (opcode);
15470 		  goto stmt_done;
15471 		}
15472 	      break;
15473 	    case ERROR_MARK:
15474 	      goto saw_error;
15475 	    default:
15476 	      break;
15477 	    }
15478 	  if (c_parser_peek_token (parser)->type == CPP_SEMICOLON)
15479 	    {
15480 	      if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
15481 		{
15482 		  code = OMP_ATOMIC_CAPTURE_OLD;
15483 		  v = lhs;
15484 		  lhs = NULL_TREE;
15485 		  expr = default_function_array_read_conversion (eloc, expr);
15486 		  unfolded_lhs1 = expr.value;
15487 		  lhs1 = c_fully_fold (unfolded_lhs1, false, NULL, true);
15488 		  rhs1 = NULL_TREE;
15489 		  c_parser_consume_token (parser);
15490 		  goto restart;
15491 		}
15492 	      if (structured_block)
15493 		{
15494 		  opcode = NOP_EXPR;
15495 		  expr = default_function_array_read_conversion (eloc, expr);
15496 		  rhs = c_fully_fold (expr.value, false, NULL, true);
15497 		  rhs1 = NULL_TREE;
15498 		  goto stmt_done;
15499 		}
15500 	    }
15501 	  c_parser_error (parser, "invalid form of %<#pragma omp atomic%>");
15502 	  goto saw_error;
15503 	default:
15504 	  c_parser_error (parser,
15505 			  "invalid operator for %<#pragma omp atomic%>");
15506 	  goto saw_error;
15507 	}
15508 
15509       /* Arrange to pass the location of the assignment operator to
15510 	 c_finish_omp_atomic.  */
15511       loc = c_parser_peek_token (parser)->location;
15512       c_parser_consume_token (parser);
15513       eloc = c_parser_peek_token (parser)->location;
15514       expr = c_parser_expression (parser);
15515       expr = default_function_array_read_conversion (eloc, expr);
15516       rhs = expr.value;
15517       rhs = c_fully_fold (rhs, false, NULL, true);
15518       break;
15519     }
15520 stmt_done:
15521   if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
15522     {
15523       if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
15524 	goto saw_error;
15525       v = c_parser_cast_expression (parser, NULL).value;
15526       non_lvalue_p = !lvalue_p (v);
15527       v = c_fully_fold (v, false, NULL, true);
15528       if (v == error_mark_node)
15529 	goto saw_error;
15530       if (non_lvalue_p)
15531 	v = non_lvalue (v);
15532       if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
15533 	goto saw_error;
15534       eloc = c_parser_peek_token (parser)->location;
15535       expr = c_parser_cast_expression (parser, NULL);
15536       lhs1 = expr.value;
15537       expr = default_function_array_read_conversion (eloc, expr);
15538       unfolded_lhs1 = expr.value;
15539       lhs1 = c_fully_fold (lhs1, false, NULL, true);
15540       if (lhs1 == error_mark_node)
15541 	goto saw_error;
15542       if (!lvalue_p (unfolded_lhs1))
15543 	lhs1 = non_lvalue (lhs1);
15544     }
15545   if (structured_block)
15546     {
15547       c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
15548       c_parser_require (parser, CPP_CLOSE_BRACE, "expected %<}%>");
15549     }
15550 done:
15551   if (unfolded_lhs && unfolded_lhs1
15552       && !c_tree_equal (unfolded_lhs, unfolded_lhs1))
15553     {
15554       error ("%<#pragma omp atomic capture%> uses two different "
15555 	     "expressions for memory");
15556       stmt = error_mark_node;
15557     }
15558   else
15559     stmt = c_finish_omp_atomic (loc, code, opcode, lhs, rhs, v, lhs1, rhs1,
15560 				swapped, seq_cst);
15561   if (stmt != error_mark_node)
15562     add_stmt (stmt);
15563 
15564   if (!structured_block)
15565     c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
15566 }
15567 
15568 
15569 /* OpenMP 2.5:
15570    # pragma omp barrier new-line
15571 */
15572 
15573 static void
c_parser_omp_barrier(c_parser * parser)15574 c_parser_omp_barrier (c_parser *parser)
15575 {
15576   location_t loc = c_parser_peek_token (parser)->location;
15577   c_parser_consume_pragma (parser);
15578   c_parser_skip_to_pragma_eol (parser);
15579 
15580   c_finish_omp_barrier (loc);
15581 }
15582 
15583 /* OpenMP 2.5:
15584    # pragma omp critical [(name)] new-line
15585      structured-block
15586 
15587    OpenMP 4.5:
15588    # pragma omp critical [(name) [hint(expression)]] new-line
15589 
15590   LOC is the location of the #pragma itself.  */
15591 
15592 #define OMP_CRITICAL_CLAUSE_MASK		\
15593 	( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HINT) )
15594 
15595 static tree
c_parser_omp_critical(location_t loc,c_parser * parser,bool * if_p)15596 c_parser_omp_critical (location_t loc, c_parser *parser, bool *if_p)
15597 {
15598   tree stmt, name = NULL_TREE, clauses = NULL_TREE;
15599 
15600   if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
15601     {
15602       c_parser_consume_token (parser);
15603       if (c_parser_next_token_is (parser, CPP_NAME))
15604 	{
15605 	  name = c_parser_peek_token (parser)->value;
15606 	  c_parser_consume_token (parser);
15607 	  c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
15608 	}
15609       else
15610 	c_parser_error (parser, "expected identifier");
15611 
15612       clauses = c_parser_omp_all_clauses (parser,
15613 					  OMP_CRITICAL_CLAUSE_MASK,
15614 					  "#pragma omp critical");
15615     }
15616   else
15617     {
15618       if (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
15619 	c_parser_error (parser, "expected %<(%> or end of line");
15620       c_parser_skip_to_pragma_eol (parser);
15621     }
15622 
15623   stmt = c_parser_omp_structured_block (parser, if_p);
15624   return c_finish_omp_critical (loc, stmt, name, clauses);
15625 }
15626 
15627 /* OpenMP 2.5:
15628    # pragma omp flush flush-vars[opt] new-line
15629 
15630    flush-vars:
15631      ( variable-list ) */
15632 
15633 static void
c_parser_omp_flush(c_parser * parser)15634 c_parser_omp_flush (c_parser *parser)
15635 {
15636   location_t loc = c_parser_peek_token (parser)->location;
15637   c_parser_consume_pragma (parser);
15638   if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
15639     c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
15640   else if (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
15641     c_parser_error (parser, "expected %<(%> or end of line");
15642   c_parser_skip_to_pragma_eol (parser);
15643 
15644   c_finish_omp_flush (loc);
15645 }
15646 
15647 /* Parse the restricted form of loop statements allowed by OpenACC and OpenMP.
15648    The real trick here is to determine the loop control variable early
15649    so that we can push a new decl if necessary to make it private.
15650    LOC is the location of the "acc" or "omp" in "#pragma acc" or "#pragma omp",
15651    respectively.  */
15652 
15653 static tree
c_parser_omp_for_loop(location_t loc,c_parser * parser,enum tree_code code,tree clauses,tree * cclauses,bool * if_p)15654 c_parser_omp_for_loop (location_t loc, c_parser *parser, enum tree_code code,
15655 		       tree clauses, tree *cclauses, bool *if_p)
15656 {
15657   tree decl, cond, incr, save_break, save_cont, body, init, stmt, cl;
15658   tree declv, condv, incrv, initv, ret = NULL_TREE;
15659   tree pre_body = NULL_TREE, this_pre_body;
15660   tree ordered_cl = NULL_TREE;
15661   bool fail = false, open_brace_parsed = false;
15662   int i, collapse = 1, ordered = 0, count, nbraces = 0;
15663   location_t for_loc;
15664   bool tiling = false;
15665   vec<tree, va_gc> *for_block = make_tree_vector ();
15666 
15667   for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
15668     if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
15669       collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
15670     else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_TILE)
15671       {
15672 	tiling = true;
15673 	collapse = list_length (OMP_CLAUSE_TILE_LIST (cl));
15674       }
15675     else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_ORDERED
15676 	     && OMP_CLAUSE_ORDERED_EXPR (cl))
15677       {
15678 	ordered_cl = cl;
15679 	ordered = tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl));
15680       }
15681 
15682   if (ordered && ordered < collapse)
15683     {
15684       error_at (OMP_CLAUSE_LOCATION (ordered_cl),
15685 		"%<ordered%> clause parameter is less than %<collapse%>");
15686       OMP_CLAUSE_ORDERED_EXPR (ordered_cl)
15687 	= build_int_cst (NULL_TREE, collapse);
15688       ordered = collapse;
15689     }
15690   if (ordered)
15691     {
15692       for (tree *pc = &clauses; *pc; )
15693 	if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LINEAR)
15694 	  {
15695 	    error_at (OMP_CLAUSE_LOCATION (*pc),
15696 		      "%<linear%> clause may not be specified together "
15697 		      "with %<ordered%> clause with a parameter");
15698 	    *pc = OMP_CLAUSE_CHAIN (*pc);
15699 	  }
15700 	else
15701 	  pc = &OMP_CLAUSE_CHAIN (*pc);
15702     }
15703 
15704   gcc_assert (tiling || (collapse >= 1 && ordered >= 0));
15705   count = ordered ? ordered : collapse;
15706 
15707   declv = make_tree_vec (count);
15708   initv = make_tree_vec (count);
15709   condv = make_tree_vec (count);
15710   incrv = make_tree_vec (count);
15711 
15712   if (!c_parser_next_token_is_keyword (parser, RID_FOR))
15713     {
15714       c_parser_error (parser, "for statement expected");
15715       return NULL;
15716     }
15717   for_loc = c_parser_peek_token (parser)->location;
15718   c_parser_consume_token (parser);
15719 
15720   for (i = 0; i < count; i++)
15721     {
15722       int bracecount = 0;
15723 
15724       matching_parens parens;
15725       if (!parens.require_open (parser))
15726 	goto pop_scopes;
15727 
15728       /* Parse the initialization declaration or expression.  */
15729       if (c_parser_next_tokens_start_declaration (parser))
15730 	{
15731 	  if (i > 0)
15732 	    vec_safe_push (for_block, c_begin_compound_stmt (true));
15733 	  this_pre_body = push_stmt_list ();
15734 	  c_parser_declaration_or_fndef (parser, true, true, true, true, true,
15735 					 NULL, vNULL);
15736 	  if (this_pre_body)
15737 	    {
15738 	      this_pre_body = pop_stmt_list (this_pre_body);
15739 	      if (pre_body)
15740 		{
15741 		  tree t = pre_body;
15742 		  pre_body = push_stmt_list ();
15743 		  add_stmt (t);
15744 		  add_stmt (this_pre_body);
15745 		  pre_body = pop_stmt_list (pre_body);
15746 		}
15747 	      else
15748 		pre_body = this_pre_body;
15749 	    }
15750 	  decl = check_for_loop_decls (for_loc, flag_isoc99);
15751 	  if (decl == NULL)
15752 	    goto error_init;
15753 	  if (DECL_INITIAL (decl) == error_mark_node)
15754 	    decl = error_mark_node;
15755 	  init = decl;
15756 	}
15757       else if (c_parser_next_token_is (parser, CPP_NAME)
15758 	       && c_parser_peek_2nd_token (parser)->type == CPP_EQ)
15759 	{
15760 	  struct c_expr decl_exp;
15761 	  struct c_expr init_exp;
15762 	  location_t init_loc;
15763 
15764 	  decl_exp = c_parser_postfix_expression (parser);
15765 	  decl = decl_exp.value;
15766 
15767 	  c_parser_require (parser, CPP_EQ, "expected %<=%>");
15768 
15769 	  init_loc = c_parser_peek_token (parser)->location;
15770 	  init_exp = c_parser_expr_no_commas (parser, NULL);
15771 	  init_exp = default_function_array_read_conversion (init_loc,
15772 							     init_exp);
15773 	  init = build_modify_expr (init_loc, decl, decl_exp.original_type,
15774 				    NOP_EXPR, init_loc, init_exp.value,
15775 				    init_exp.original_type);
15776 	  init = c_process_expr_stmt (init_loc, init);
15777 
15778 	  c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
15779 	}
15780       else
15781 	{
15782 	error_init:
15783 	  c_parser_error (parser,
15784 			  "expected iteration declaration or initialization");
15785 	  c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
15786 				     "expected %<)%>");
15787 	  fail = true;
15788 	  goto parse_next;
15789 	}
15790 
15791       /* Parse the loop condition.  */
15792       cond = NULL_TREE;
15793       if (c_parser_next_token_is_not (parser, CPP_SEMICOLON))
15794 	{
15795 	  location_t cond_loc = c_parser_peek_token (parser)->location;
15796 	  struct c_expr cond_expr
15797 	    = c_parser_binary_expression (parser, NULL, NULL_TREE);
15798 
15799 	  cond = cond_expr.value;
15800 	  cond = c_objc_common_truthvalue_conversion (cond_loc, cond);
15801 	  if (COMPARISON_CLASS_P (cond))
15802 	    {
15803 	      tree op0 = TREE_OPERAND (cond, 0), op1 = TREE_OPERAND (cond, 1);
15804 	      op0 = c_fully_fold (op0, false, NULL);
15805 	      op1 = c_fully_fold (op1, false, NULL);
15806 	      TREE_OPERAND (cond, 0) = op0;
15807 	      TREE_OPERAND (cond, 1) = op1;
15808 	    }
15809 	  switch (cond_expr.original_code)
15810 	    {
15811 	    case GT_EXPR:
15812 	    case GE_EXPR:
15813 	    case LT_EXPR:
15814 	    case LE_EXPR:
15815 	      break;
15816 	    default:
15817 	      /* Can't be cond = error_mark_node, because we want to preserve
15818 		 the location until c_finish_omp_for.  */
15819 	      cond = build1 (NOP_EXPR, boolean_type_node, error_mark_node);
15820 	      break;
15821 	    }
15822 	  protected_set_expr_location (cond, cond_loc);
15823 	}
15824       c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
15825 
15826       /* Parse the increment expression.  */
15827       incr = NULL_TREE;
15828       if (c_parser_next_token_is_not (parser, CPP_CLOSE_PAREN))
15829 	{
15830 	  location_t incr_loc = c_parser_peek_token (parser)->location;
15831 
15832 	  incr = c_process_expr_stmt (incr_loc,
15833 				      c_parser_expression (parser).value);
15834 	}
15835       parens.skip_until_found_close (parser);
15836 
15837       if (decl == NULL || decl == error_mark_node || init == error_mark_node)
15838 	fail = true;
15839       else
15840 	{
15841 	  TREE_VEC_ELT (declv, i) = decl;
15842 	  TREE_VEC_ELT (initv, i) = init;
15843 	  TREE_VEC_ELT (condv, i) = cond;
15844 	  TREE_VEC_ELT (incrv, i) = incr;
15845 	}
15846 
15847     parse_next:
15848       if (i == count - 1)
15849 	break;
15850 
15851       /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
15852 	 in between the collapsed for loops to be still considered perfectly
15853 	 nested.  Hopefully the final version clarifies this.
15854 	 For now handle (multiple) {'s and empty statements.  */
15855       do
15856 	{
15857 	  if (c_parser_next_token_is_keyword (parser, RID_FOR))
15858 	    {
15859 	      c_parser_consume_token (parser);
15860 	      break;
15861 	    }
15862 	  else if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
15863 	    {
15864 	      c_parser_consume_token (parser);
15865 	      bracecount++;
15866 	    }
15867 	  else if (bracecount
15868 		   && c_parser_next_token_is (parser, CPP_SEMICOLON))
15869 	    c_parser_consume_token (parser);
15870 	  else
15871 	    {
15872 	      c_parser_error (parser, "not enough perfectly nested loops");
15873 	      if (bracecount)
15874 		{
15875 		  open_brace_parsed = true;
15876 		  bracecount--;
15877 		}
15878 	      fail = true;
15879 	      count = 0;
15880 	      break;
15881 	    }
15882 	}
15883       while (1);
15884 
15885       nbraces += bracecount;
15886     }
15887 
15888   if (nbraces)
15889     if_p = NULL;
15890 
15891   save_break = c_break_label;
15892   c_break_label = size_one_node;
15893   save_cont = c_cont_label;
15894   c_cont_label = NULL_TREE;
15895   body = push_stmt_list ();
15896 
15897   if (open_brace_parsed)
15898     {
15899       location_t here = c_parser_peek_token (parser)->location;
15900       stmt = c_begin_compound_stmt (true);
15901       c_parser_compound_statement_nostart (parser);
15902       add_stmt (c_end_compound_stmt (here, stmt, true));
15903     }
15904   else
15905     add_stmt (c_parser_c99_block_statement (parser, if_p));
15906   if (c_cont_label)
15907     {
15908       tree t = build1 (LABEL_EXPR, void_type_node, c_cont_label);
15909       SET_EXPR_LOCATION (t, loc);
15910       add_stmt (t);
15911     }
15912 
15913   body = pop_stmt_list (body);
15914   c_break_label = save_break;
15915   c_cont_label = save_cont;
15916 
15917   while (nbraces)
15918     {
15919       if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
15920 	{
15921 	  c_parser_consume_token (parser);
15922 	  nbraces--;
15923 	}
15924       else if (c_parser_next_token_is (parser, CPP_SEMICOLON))
15925 	c_parser_consume_token (parser);
15926       else
15927 	{
15928 	  c_parser_error (parser, "collapsed loops not perfectly nested");
15929 	  while (nbraces)
15930 	    {
15931 	      location_t here = c_parser_peek_token (parser)->location;
15932 	      stmt = c_begin_compound_stmt (true);
15933 	      add_stmt (body);
15934 	      c_parser_compound_statement_nostart (parser);
15935 	      body = c_end_compound_stmt (here, stmt, true);
15936 	      nbraces--;
15937 	    }
15938 	  goto pop_scopes;
15939 	}
15940     }
15941 
15942   /* Only bother calling c_finish_omp_for if we haven't already generated
15943      an error from the initialization parsing.  */
15944   if (!fail)
15945     {
15946       stmt = c_finish_omp_for (loc, code, declv, NULL, initv, condv,
15947 			       incrv, body, pre_body);
15948 
15949       /* Check for iterators appearing in lb, b or incr expressions.  */
15950       if (stmt && !c_omp_check_loop_iv (stmt, declv, NULL))
15951 	stmt = NULL_TREE;
15952 
15953       if (stmt)
15954 	{
15955 	  add_stmt (stmt);
15956 
15957 	  if (cclauses != NULL
15958 	      && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL)
15959 	    {
15960 	      tree *c;
15961 	      for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
15962 		if (OMP_CLAUSE_CODE (*c) != OMP_CLAUSE_FIRSTPRIVATE
15963 		    && OMP_CLAUSE_CODE (*c) != OMP_CLAUSE_LASTPRIVATE)
15964 		  c = &OMP_CLAUSE_CHAIN (*c);
15965 		else
15966 		  {
15967 		    for (i = 0; i < count; i++)
15968 		      if (TREE_VEC_ELT (declv, i) == OMP_CLAUSE_DECL (*c))
15969 			break;
15970 		    if (i == count)
15971 		      c = &OMP_CLAUSE_CHAIN (*c);
15972 		    else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE)
15973 		      {
15974 			error_at (loc,
15975 				  "iteration variable %qD should not be firstprivate",
15976 				  OMP_CLAUSE_DECL (*c));
15977 			*c = OMP_CLAUSE_CHAIN (*c);
15978 		      }
15979 		    else
15980 		      {
15981 			/* Move lastprivate (decl) clause to OMP_FOR_CLAUSES.  */
15982 			tree l = *c;
15983 			*c = OMP_CLAUSE_CHAIN (*c);
15984 			if (code == OMP_SIMD)
15985 			  {
15986 			    OMP_CLAUSE_CHAIN (l)
15987 			      = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
15988 			    cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
15989 			  }
15990 			else
15991 			  {
15992 			    OMP_CLAUSE_CHAIN (l) = clauses;
15993 			    clauses = l;
15994 			  }
15995 		      }
15996 		  }
15997 	    }
15998 	  OMP_FOR_CLAUSES (stmt) = clauses;
15999 	}
16000       ret = stmt;
16001     }
16002 pop_scopes:
16003   while (!for_block->is_empty ())
16004     {
16005       /* FIXME diagnostics: LOC below should be the actual location of
16006 	 this particular for block.  We need to build a list of
16007 	 locations to go along with FOR_BLOCK.  */
16008       stmt = c_end_compound_stmt (loc, for_block->pop (), true);
16009       add_stmt (stmt);
16010     }
16011   release_tree_vector (for_block);
16012   return ret;
16013 }
16014 
16015 /* Helper function for OpenMP parsing, split clauses and call
16016    finish_omp_clauses on each of the set of clauses afterwards.  */
16017 
16018 static void
omp_split_clauses(location_t loc,enum tree_code code,omp_clause_mask mask,tree clauses,tree * cclauses)16019 omp_split_clauses (location_t loc, enum tree_code code,
16020 		   omp_clause_mask mask, tree clauses, tree *cclauses)
16021 {
16022   int i;
16023   c_omp_split_clauses (loc, code, mask, clauses, cclauses);
16024   for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
16025     if (cclauses[i])
16026       cclauses[i] = c_finish_omp_clauses (cclauses[i], C_ORT_OMP);
16027 }
16028 
16029 /* OpenMP 4.0:
16030    #pragma omp simd simd-clause[optseq] new-line
16031      for-loop
16032 
16033    LOC is the location of the #pragma token.
16034 */
16035 
16036 #define OMP_SIMD_CLAUSE_MASK					\
16037 	( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN)	\
16038 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN)	\
16039 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR)	\
16040 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED)	\
16041 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE)	\
16042 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE)	\
16043 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION)	\
16044 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
16045 
16046 static tree
c_parser_omp_simd(location_t loc,c_parser * parser,char * p_name,omp_clause_mask mask,tree * cclauses,bool * if_p)16047 c_parser_omp_simd (location_t loc, c_parser *parser,
16048 		   char *p_name, omp_clause_mask mask, tree *cclauses,
16049 		   bool *if_p)
16050 {
16051   tree block, clauses, ret;
16052 
16053   strcat (p_name, " simd");
16054   mask |= OMP_SIMD_CLAUSE_MASK;
16055 
16056   clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
16057   if (cclauses)
16058     {
16059       omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
16060       clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
16061       tree c = omp_find_clause (cclauses[C_OMP_CLAUSE_SPLIT_FOR],
16062 				OMP_CLAUSE_ORDERED);
16063       if (c && OMP_CLAUSE_ORDERED_EXPR (c))
16064 	{
16065 	  error_at (OMP_CLAUSE_LOCATION (c),
16066 		    "%<ordered%> clause with parameter may not be specified "
16067 		    "on %qs construct", p_name);
16068 	  OMP_CLAUSE_ORDERED_EXPR (c) = NULL_TREE;
16069 	}
16070     }
16071 
16072   block = c_begin_compound_stmt (true);
16073   ret = c_parser_omp_for_loop (loc, parser, OMP_SIMD, clauses, cclauses, if_p);
16074   block = c_end_compound_stmt (loc, block, true);
16075   add_stmt (block);
16076 
16077   return ret;
16078 }
16079 
16080 /* OpenMP 2.5:
16081    #pragma omp for for-clause[optseq] new-line
16082      for-loop
16083 
16084    OpenMP 4.0:
16085    #pragma omp for simd for-simd-clause[optseq] new-line
16086      for-loop
16087 
16088    LOC is the location of the #pragma token.
16089 */
16090 
16091 #define OMP_FOR_CLAUSE_MASK					\
16092 	( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE)	\
16093 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)	\
16094 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE)	\
16095 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR)	\
16096 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION)	\
16097 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED)	\
16098 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE)	\
16099 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE)	\
16100 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
16101 
16102 static tree
c_parser_omp_for(location_t loc,c_parser * parser,char * p_name,omp_clause_mask mask,tree * cclauses,bool * if_p)16103 c_parser_omp_for (location_t loc, c_parser *parser,
16104 		  char *p_name, omp_clause_mask mask, tree *cclauses,
16105 		  bool *if_p)
16106 {
16107   tree block, clauses, ret;
16108 
16109   strcat (p_name, " for");
16110   mask |= OMP_FOR_CLAUSE_MASK;
16111   /* parallel for{, simd} disallows nowait clause, but for
16112      target {teams distribute ,}parallel for{, simd} it should be accepted.  */
16113   if (cclauses && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) == 0)
16114     mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
16115   /* Composite distribute parallel for{, simd} disallows ordered clause.  */
16116   if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
16117     mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
16118 
16119   if (c_parser_next_token_is (parser, CPP_NAME))
16120     {
16121       const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
16122 
16123       if (strcmp (p, "simd") == 0)
16124 	{
16125 	  tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
16126 	  if (cclauses == NULL)
16127 	    cclauses = cclauses_buf;
16128 
16129 	  c_parser_consume_token (parser);
16130 	  if (!flag_openmp)  /* flag_openmp_simd  */
16131 	    return c_parser_omp_simd (loc, parser, p_name, mask, cclauses,
16132 				      if_p);
16133 	  block = c_begin_compound_stmt (true);
16134 	  ret = c_parser_omp_simd (loc, parser, p_name, mask, cclauses, if_p);
16135 	  block = c_end_compound_stmt (loc, block, true);
16136 	  if (ret == NULL_TREE)
16137 	    return ret;
16138 	  ret = make_node (OMP_FOR);
16139 	  TREE_TYPE (ret) = void_type_node;
16140 	  OMP_FOR_BODY (ret) = block;
16141 	  OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
16142 	  SET_EXPR_LOCATION (ret, loc);
16143 	  add_stmt (ret);
16144 	  return ret;
16145 	}
16146     }
16147   if (!flag_openmp)  /* flag_openmp_simd  */
16148     {
16149       c_parser_skip_to_pragma_eol (parser, false);
16150       return NULL_TREE;
16151     }
16152 
16153   /* Composite distribute parallel for disallows linear clause.  */
16154   if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
16155     mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR);
16156 
16157   clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
16158   if (cclauses)
16159     {
16160       omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
16161       clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
16162     }
16163 
16164   block = c_begin_compound_stmt (true);
16165   ret = c_parser_omp_for_loop (loc, parser, OMP_FOR, clauses, cclauses, if_p);
16166   block = c_end_compound_stmt (loc, block, true);
16167   add_stmt (block);
16168 
16169   return ret;
16170 }
16171 
16172 /* OpenMP 2.5:
16173    # pragma omp master new-line
16174      structured-block
16175 
16176    LOC is the location of the #pragma token.
16177 */
16178 
16179 static tree
c_parser_omp_master(location_t loc,c_parser * parser,bool * if_p)16180 c_parser_omp_master (location_t loc, c_parser *parser, bool *if_p)
16181 {
16182   c_parser_skip_to_pragma_eol (parser);
16183   return c_finish_omp_master (loc, c_parser_omp_structured_block (parser,
16184 								  if_p));
16185 }
16186 
16187 /* OpenMP 2.5:
16188    # pragma omp ordered new-line
16189      structured-block
16190 
16191    OpenMP 4.5:
16192    # pragma omp ordered ordered-clauses new-line
16193      structured-block
16194 
16195    # pragma omp ordered depend-clauses new-line  */
16196 
16197 #define OMP_ORDERED_CLAUSE_MASK					\
16198 	( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREADS)	\
16199 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMD))
16200 
16201 #define OMP_ORDERED_DEPEND_CLAUSE_MASK				\
16202 	(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
16203 
16204 static bool
c_parser_omp_ordered(c_parser * parser,enum pragma_context context,bool * if_p)16205 c_parser_omp_ordered (c_parser *parser, enum pragma_context context,
16206 		      bool *if_p)
16207 {
16208   location_t loc = c_parser_peek_token (parser)->location;
16209   c_parser_consume_pragma (parser);
16210 
16211   if (context != pragma_stmt && context != pragma_compound)
16212     {
16213       c_parser_error (parser, "expected declaration specifiers");
16214       c_parser_skip_to_pragma_eol (parser, false);
16215       return false;
16216     }
16217 
16218   if (c_parser_next_token_is (parser, CPP_NAME))
16219     {
16220       const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
16221 
16222       if (!strcmp ("depend", p))
16223 	{
16224 	  if (!flag_openmp)	/* flag_openmp_simd  */
16225 	    {
16226 	      c_parser_skip_to_pragma_eol (parser, false);
16227 	      return false;
16228 	    }
16229 	  if (context == pragma_stmt)
16230 	    {
16231 	      error_at (loc,
16232 			"%<#pragma omp ordered%> with %<depend%> clause may "
16233 			"only be used in compound statements");
16234 	      c_parser_skip_to_pragma_eol (parser, false);
16235 	      return false;
16236 	    }
16237 
16238 	  tree clauses
16239 	    = c_parser_omp_all_clauses (parser,
16240 					OMP_ORDERED_DEPEND_CLAUSE_MASK,
16241 					"#pragma omp ordered");
16242 	  c_finish_omp_ordered (loc, clauses, NULL_TREE);
16243 	  return false;
16244 	}
16245     }
16246 
16247   tree clauses = c_parser_omp_all_clauses (parser, OMP_ORDERED_CLAUSE_MASK,
16248 					   "#pragma omp ordered");
16249 
16250   if (!flag_openmp	/* flag_openmp_simd  */
16251       && omp_find_clause (clauses, OMP_CLAUSE_SIMD) == NULL_TREE)
16252     return false;
16253 
16254   c_finish_omp_ordered (loc, clauses,
16255 			c_parser_omp_structured_block (parser, if_p));
16256   return true;
16257 }
16258 
16259 /* OpenMP 2.5:
16260 
16261    section-scope:
16262      { section-sequence }
16263 
16264    section-sequence:
16265      section-directive[opt] structured-block
16266      section-sequence section-directive structured-block
16267 
16268     SECTIONS_LOC is the location of the #pragma omp sections.  */
16269 
16270 static tree
c_parser_omp_sections_scope(location_t sections_loc,c_parser * parser)16271 c_parser_omp_sections_scope (location_t sections_loc, c_parser *parser)
16272 {
16273   tree stmt, substmt;
16274   bool error_suppress = false;
16275   location_t loc;
16276 
16277   loc = c_parser_peek_token (parser)->location;
16278   if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
16279     {
16280       /* Avoid skipping until the end of the block.  */
16281       parser->error = false;
16282       return NULL_TREE;
16283     }
16284 
16285   stmt = push_stmt_list ();
16286 
16287   if (c_parser_peek_token (parser)->pragma_kind != PRAGMA_OMP_SECTION)
16288     {
16289       substmt = c_parser_omp_structured_block (parser, NULL);
16290       substmt = build1 (OMP_SECTION, void_type_node, substmt);
16291       SET_EXPR_LOCATION (substmt, loc);
16292       add_stmt (substmt);
16293     }
16294 
16295   while (1)
16296     {
16297       if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
16298 	break;
16299       if (c_parser_next_token_is (parser, CPP_EOF))
16300 	break;
16301 
16302       loc = c_parser_peek_token (parser)->location;
16303       if (c_parser_peek_token (parser)->pragma_kind == PRAGMA_OMP_SECTION)
16304 	{
16305 	  c_parser_consume_pragma (parser);
16306 	  c_parser_skip_to_pragma_eol (parser);
16307 	  error_suppress = false;
16308 	}
16309       else if (!error_suppress)
16310 	{
16311 	  error_at (loc, "expected %<#pragma omp section%> or %<}%>");
16312 	  error_suppress = true;
16313 	}
16314 
16315       substmt = c_parser_omp_structured_block (parser, NULL);
16316       substmt = build1 (OMP_SECTION, void_type_node, substmt);
16317       SET_EXPR_LOCATION (substmt, loc);
16318       add_stmt (substmt);
16319     }
16320   c_parser_skip_until_found (parser, CPP_CLOSE_BRACE,
16321 			     "expected %<#pragma omp section%> or %<}%>");
16322 
16323   substmt = pop_stmt_list (stmt);
16324 
16325   stmt = make_node (OMP_SECTIONS);
16326   SET_EXPR_LOCATION (stmt, sections_loc);
16327   TREE_TYPE (stmt) = void_type_node;
16328   OMP_SECTIONS_BODY (stmt) = substmt;
16329 
16330   return add_stmt (stmt);
16331 }
16332 
16333 /* OpenMP 2.5:
16334    # pragma omp sections sections-clause[optseq] newline
16335      sections-scope
16336 
16337    LOC is the location of the #pragma token.
16338 */
16339 
16340 #define OMP_SECTIONS_CLAUSE_MASK				\
16341 	( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE)	\
16342 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)	\
16343 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE)	\
16344 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION)	\
16345 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
16346 
16347 static tree
c_parser_omp_sections(location_t loc,c_parser * parser,char * p_name,omp_clause_mask mask,tree * cclauses)16348 c_parser_omp_sections (location_t loc, c_parser *parser,
16349 		       char *p_name, omp_clause_mask mask, tree *cclauses)
16350 {
16351   tree block, clauses, ret;
16352 
16353   strcat (p_name, " sections");
16354   mask |= OMP_SECTIONS_CLAUSE_MASK;
16355   if (cclauses)
16356     mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
16357 
16358   clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
16359   if (cclauses)
16360     {
16361       omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
16362       clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
16363     }
16364 
16365   block = c_begin_compound_stmt (true);
16366   ret = c_parser_omp_sections_scope (loc, parser);
16367   if (ret)
16368     OMP_SECTIONS_CLAUSES (ret) = clauses;
16369   block = c_end_compound_stmt (loc, block, true);
16370   add_stmt (block);
16371 
16372   return ret;
16373 }
16374 
16375 /* OpenMP 2.5:
16376    # pragma omp parallel parallel-clause[optseq] new-line
16377      structured-block
16378    # pragma omp parallel for parallel-for-clause[optseq] new-line
16379      structured-block
16380    # pragma omp parallel sections parallel-sections-clause[optseq] new-line
16381      structured-block
16382 
16383    OpenMP 4.0:
16384    # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
16385      structured-block
16386 
16387    LOC is the location of the #pragma token.
16388 */
16389 
16390 #define OMP_PARALLEL_CLAUSE_MASK				\
16391 	( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF)		\
16392 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE)	\
16393 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)	\
16394 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT)	\
16395 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED)	\
16396 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN)	\
16397 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION)	\
16398 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS)	\
16399 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
16400 
16401 static tree
c_parser_omp_parallel(location_t loc,c_parser * parser,char * p_name,omp_clause_mask mask,tree * cclauses,bool * if_p)16402 c_parser_omp_parallel (location_t loc, c_parser *parser,
16403 		       char *p_name, omp_clause_mask mask, tree *cclauses,
16404 		       bool *if_p)
16405 {
16406   tree stmt, clauses, block;
16407 
16408   strcat (p_name, " parallel");
16409   mask |= OMP_PARALLEL_CLAUSE_MASK;
16410   /* #pragma omp target parallel{, for, for simd} disallow copyin clause.  */
16411   if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) != 0
16412       && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) == 0)
16413     mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN);
16414 
16415   if (c_parser_next_token_is_keyword (parser, RID_FOR))
16416     {
16417       tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
16418       if (cclauses == NULL)
16419 	cclauses = cclauses_buf;
16420 
16421       c_parser_consume_token (parser);
16422       if (!flag_openmp)  /* flag_openmp_simd  */
16423 	return c_parser_omp_for (loc, parser, p_name, mask, cclauses, if_p);
16424       block = c_begin_omp_parallel ();
16425       tree ret = c_parser_omp_for (loc, parser, p_name, mask, cclauses, if_p);
16426       stmt
16427 	= c_finish_omp_parallel (loc, cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
16428 				 block);
16429       if (ret == NULL_TREE)
16430 	return ret;
16431       OMP_PARALLEL_COMBINED (stmt) = 1;
16432       return stmt;
16433     }
16434   /* When combined with distribute, parallel has to be followed by for.
16435      #pragma omp target parallel is allowed though.  */
16436   else if (cclauses
16437 	   && (mask & (OMP_CLAUSE_MASK_1
16438 		       << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
16439     {
16440       error_at (loc, "expected %<for%> after %qs", p_name);
16441       c_parser_skip_to_pragma_eol (parser);
16442       return NULL_TREE;
16443     }
16444   else if (!flag_openmp)  /* flag_openmp_simd  */
16445     {
16446       c_parser_skip_to_pragma_eol (parser, false);
16447       return NULL_TREE;
16448     }
16449   else if (cclauses == NULL && c_parser_next_token_is (parser, CPP_NAME))
16450     {
16451       const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
16452       if (strcmp (p, "sections") == 0)
16453 	{
16454 	  tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
16455 	  if (cclauses == NULL)
16456 	    cclauses = cclauses_buf;
16457 
16458 	  c_parser_consume_token (parser);
16459 	  block = c_begin_omp_parallel ();
16460 	  c_parser_omp_sections (loc, parser, p_name, mask, cclauses);
16461 	  stmt = c_finish_omp_parallel (loc,
16462 					cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
16463 					block);
16464 	  OMP_PARALLEL_COMBINED (stmt) = 1;
16465 	  return stmt;
16466 	}
16467     }
16468 
16469   clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
16470   if (cclauses)
16471     {
16472       omp_split_clauses (loc, OMP_PARALLEL, mask, clauses, cclauses);
16473       clauses = cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL];
16474     }
16475 
16476   block = c_begin_omp_parallel ();
16477   c_parser_statement (parser, if_p);
16478   stmt = c_finish_omp_parallel (loc, clauses, block);
16479 
16480   return stmt;
16481 }
16482 
16483 /* OpenMP 2.5:
16484    # pragma omp single single-clause[optseq] new-line
16485      structured-block
16486 
16487    LOC is the location of the #pragma.
16488 */
16489 
16490 #define OMP_SINGLE_CLAUSE_MASK					\
16491 	( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE)	\
16492 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)	\
16493 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE)	\
16494 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
16495 
16496 static tree
c_parser_omp_single(location_t loc,c_parser * parser,bool * if_p)16497 c_parser_omp_single (location_t loc, c_parser *parser, bool *if_p)
16498 {
16499   tree stmt = make_node (OMP_SINGLE);
16500   SET_EXPR_LOCATION (stmt, loc);
16501   TREE_TYPE (stmt) = void_type_node;
16502 
16503   OMP_SINGLE_CLAUSES (stmt)
16504     = c_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
16505 				"#pragma omp single");
16506   OMP_SINGLE_BODY (stmt) = c_parser_omp_structured_block (parser, if_p);
16507 
16508   return add_stmt (stmt);
16509 }
16510 
16511 /* OpenMP 3.0:
16512    # pragma omp task task-clause[optseq] new-line
16513 
16514    LOC is the location of the #pragma.
16515 */
16516 
16517 #define OMP_TASK_CLAUSE_MASK					\
16518 	( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF)		\
16519 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED)	\
16520 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT)	\
16521 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE)	\
16522 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)	\
16523 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED)	\
16524 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL)	\
16525 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE)	\
16526 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)	\
16527 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
16528 
16529 static tree
c_parser_omp_task(location_t loc,c_parser * parser,bool * if_p)16530 c_parser_omp_task (location_t loc, c_parser *parser, bool *if_p)
16531 {
16532   tree clauses, block;
16533 
16534   clauses = c_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
16535 				      "#pragma omp task");
16536 
16537   block = c_begin_omp_task ();
16538   c_parser_statement (parser, if_p);
16539   return c_finish_omp_task (loc, clauses, block);
16540 }
16541 
16542 /* OpenMP 3.0:
16543    # pragma omp taskwait new-line
16544 */
16545 
16546 static void
c_parser_omp_taskwait(c_parser * parser)16547 c_parser_omp_taskwait (c_parser *parser)
16548 {
16549   location_t loc = c_parser_peek_token (parser)->location;
16550   c_parser_consume_pragma (parser);
16551   c_parser_skip_to_pragma_eol (parser);
16552 
16553   c_finish_omp_taskwait (loc);
16554 }
16555 
16556 /* OpenMP 3.1:
16557    # pragma omp taskyield new-line
16558 */
16559 
16560 static void
c_parser_omp_taskyield(c_parser * parser)16561 c_parser_omp_taskyield (c_parser *parser)
16562 {
16563   location_t loc = c_parser_peek_token (parser)->location;
16564   c_parser_consume_pragma (parser);
16565   c_parser_skip_to_pragma_eol (parser);
16566 
16567   c_finish_omp_taskyield (loc);
16568 }
16569 
16570 /* OpenMP 4.0:
16571    # pragma omp taskgroup new-line
16572 */
16573 
16574 static tree
c_parser_omp_taskgroup(c_parser * parser,bool * if_p)16575 c_parser_omp_taskgroup (c_parser *parser, bool *if_p)
16576 {
16577   location_t loc = c_parser_peek_token (parser)->location;
16578   c_parser_skip_to_pragma_eol (parser);
16579   return c_finish_omp_taskgroup (loc, c_parser_omp_structured_block (parser,
16580 								     if_p));
16581 }
16582 
16583 /* OpenMP 4.0:
16584    # pragma omp cancel cancel-clause[optseq] new-line
16585 
16586    LOC is the location of the #pragma.
16587 */
16588 
16589 #define OMP_CANCEL_CLAUSE_MASK					\
16590 	( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL)	\
16591 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR)		\
16592 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS)	\
16593 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP)	\
16594 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
16595 
16596 static void
c_parser_omp_cancel(c_parser * parser)16597 c_parser_omp_cancel (c_parser *parser)
16598 {
16599   location_t loc = c_parser_peek_token (parser)->location;
16600 
16601   c_parser_consume_pragma (parser);
16602   tree clauses = c_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
16603 					   "#pragma omp cancel");
16604 
16605   c_finish_omp_cancel (loc, clauses);
16606 }
16607 
16608 /* OpenMP 4.0:
16609    # pragma omp cancellation point cancelpt-clause[optseq] new-line
16610 
16611    LOC is the location of the #pragma.
16612 */
16613 
16614 #define OMP_CANCELLATION_POINT_CLAUSE_MASK			\
16615 	( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL)	\
16616 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR)		\
16617 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS)	\
16618 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
16619 
16620 static void
c_parser_omp_cancellation_point(c_parser * parser,enum pragma_context context)16621 c_parser_omp_cancellation_point (c_parser *parser, enum pragma_context context)
16622 {
16623   location_t loc = c_parser_peek_token (parser)->location;
16624   tree clauses;
16625   bool point_seen = false;
16626 
16627   c_parser_consume_pragma (parser);
16628   if (c_parser_next_token_is (parser, CPP_NAME))
16629     {
16630       const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
16631       if (strcmp (p, "point") == 0)
16632 	{
16633 	  c_parser_consume_token (parser);
16634 	  point_seen = true;
16635 	}
16636     }
16637   if (!point_seen)
16638     {
16639       c_parser_error (parser, "expected %<point%>");
16640       c_parser_skip_to_pragma_eol (parser);
16641       return;
16642     }
16643 
16644   if (context != pragma_compound)
16645     {
16646       if (context == pragma_stmt)
16647 	error_at (loc,
16648 		  "%<#pragma %s%> may only be used in compound statements",
16649 		  "omp cancellation point");
16650       else
16651 	c_parser_error (parser, "expected declaration specifiers");
16652       c_parser_skip_to_pragma_eol (parser, false);
16653       return;
16654     }
16655 
16656   clauses
16657     = c_parser_omp_all_clauses (parser, OMP_CANCELLATION_POINT_CLAUSE_MASK,
16658 				"#pragma omp cancellation point");
16659 
16660   c_finish_omp_cancellation_point (loc, clauses);
16661 }
16662 
16663 /* OpenMP 4.0:
16664    #pragma omp distribute distribute-clause[optseq] new-line
16665      for-loop  */
16666 
16667 #define OMP_DISTRIBUTE_CLAUSE_MASK				\
16668 	( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE)	\
16669 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)	\
16670 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE)	\
16671 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
16672 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
16673 
16674 static tree
c_parser_omp_distribute(location_t loc,c_parser * parser,char * p_name,omp_clause_mask mask,tree * cclauses,bool * if_p)16675 c_parser_omp_distribute (location_t loc, c_parser *parser,
16676 			 char *p_name, omp_clause_mask mask, tree *cclauses,
16677 			 bool *if_p)
16678 {
16679   tree clauses, block, ret;
16680 
16681   strcat (p_name, " distribute");
16682   mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
16683 
16684   if (c_parser_next_token_is (parser, CPP_NAME))
16685     {
16686       const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
16687       bool simd = false;
16688       bool parallel = false;
16689 
16690       if (strcmp (p, "simd") == 0)
16691 	simd = true;
16692       else
16693 	parallel = strcmp (p, "parallel") == 0;
16694       if (parallel || simd)
16695 	{
16696 	  tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
16697 	  if (cclauses == NULL)
16698 	    cclauses = cclauses_buf;
16699 	  c_parser_consume_token (parser);
16700 	  if (!flag_openmp)  /* flag_openmp_simd  */
16701 	    {
16702 	      if (simd)
16703 		return c_parser_omp_simd (loc, parser, p_name, mask, cclauses,
16704 					  if_p);
16705 	      else
16706 		return c_parser_omp_parallel (loc, parser, p_name, mask,
16707 					      cclauses, if_p);
16708 	    }
16709 	  block = c_begin_compound_stmt (true);
16710 	  if (simd)
16711 	    ret = c_parser_omp_simd (loc, parser, p_name, mask, cclauses,
16712 				     if_p);
16713 	  else
16714 	    ret = c_parser_omp_parallel (loc, parser, p_name, mask, cclauses,
16715 					 if_p);
16716 	  block = c_end_compound_stmt (loc, block, true);
16717 	  if (ret == NULL)
16718 	    return ret;
16719 	  ret = make_node (OMP_DISTRIBUTE);
16720 	  TREE_TYPE (ret) = void_type_node;
16721 	  OMP_FOR_BODY (ret) = block;
16722 	  OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
16723 	  SET_EXPR_LOCATION (ret, loc);
16724 	  add_stmt (ret);
16725 	  return ret;
16726 	}
16727     }
16728   if (!flag_openmp)  /* flag_openmp_simd  */
16729     {
16730       c_parser_skip_to_pragma_eol (parser, false);
16731       return NULL_TREE;
16732     }
16733 
16734   clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
16735   if (cclauses)
16736     {
16737       omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
16738       clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
16739     }
16740 
16741   block = c_begin_compound_stmt (true);
16742   ret = c_parser_omp_for_loop (loc, parser, OMP_DISTRIBUTE, clauses, NULL,
16743 			       if_p);
16744   block = c_end_compound_stmt (loc, block, true);
16745   add_stmt (block);
16746 
16747   return ret;
16748 }
16749 
16750 /* OpenMP 4.0:
16751    # pragma omp teams teams-clause[optseq] new-line
16752      structured-block  */
16753 
16754 #define OMP_TEAMS_CLAUSE_MASK					\
16755 	( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE)	\
16756 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)	\
16757 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED)	\
16758 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION)	\
16759 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS)	\
16760 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT)	\
16761 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
16762 
16763 static tree
c_parser_omp_teams(location_t loc,c_parser * parser,char * p_name,omp_clause_mask mask,tree * cclauses,bool * if_p)16764 c_parser_omp_teams (location_t loc, c_parser *parser,
16765 		    char *p_name, omp_clause_mask mask, tree *cclauses,
16766 		    bool *if_p)
16767 {
16768   tree clauses, block, ret;
16769 
16770   strcat (p_name, " teams");
16771   mask |= OMP_TEAMS_CLAUSE_MASK;
16772 
16773   if (c_parser_next_token_is (parser, CPP_NAME))
16774     {
16775       const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
16776       if (strcmp (p, "distribute") == 0)
16777 	{
16778 	  tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
16779 	  if (cclauses == NULL)
16780 	    cclauses = cclauses_buf;
16781 
16782 	  c_parser_consume_token (parser);
16783 	  if (!flag_openmp)  /* flag_openmp_simd  */
16784 	    return c_parser_omp_distribute (loc, parser, p_name, mask,
16785 					    cclauses, if_p);
16786 	  block = c_begin_compound_stmt (true);
16787 	  ret = c_parser_omp_distribute (loc, parser, p_name, mask, cclauses,
16788 					 if_p);
16789 	  block = c_end_compound_stmt (loc, block, true);
16790 	  if (ret == NULL)
16791 	    return ret;
16792 	  clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
16793 	  ret = make_node (OMP_TEAMS);
16794 	  TREE_TYPE (ret) = void_type_node;
16795 	  OMP_TEAMS_CLAUSES (ret) = clauses;
16796 	  OMP_TEAMS_BODY (ret) = block;
16797 	  OMP_TEAMS_COMBINED (ret) = 1;
16798 	  return add_stmt (ret);
16799 	}
16800     }
16801   if (!flag_openmp)  /* flag_openmp_simd  */
16802     {
16803       c_parser_skip_to_pragma_eol (parser, false);
16804       return NULL_TREE;
16805     }
16806 
16807   clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
16808   if (cclauses)
16809     {
16810       omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
16811       clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
16812     }
16813 
16814   tree stmt = make_node (OMP_TEAMS);
16815   TREE_TYPE (stmt) = void_type_node;
16816   OMP_TEAMS_CLAUSES (stmt) = clauses;
16817   OMP_TEAMS_BODY (stmt) = c_parser_omp_structured_block (parser, if_p);
16818 
16819   return add_stmt (stmt);
16820 }
16821 
16822 /* OpenMP 4.0:
16823    # pragma omp target data target-data-clause[optseq] new-line
16824      structured-block  */
16825 
16826 #define OMP_TARGET_DATA_CLAUSE_MASK				\
16827 	( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE)	\
16828 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)		\
16829 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF)		\
16830 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR))
16831 
16832 static tree
c_parser_omp_target_data(location_t loc,c_parser * parser,bool * if_p)16833 c_parser_omp_target_data (location_t loc, c_parser *parser, bool *if_p)
16834 {
16835   tree clauses
16836     = c_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
16837 				"#pragma omp target data");
16838   int map_seen = 0;
16839   for (tree *pc = &clauses; *pc;)
16840     {
16841       if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
16842 	switch (OMP_CLAUSE_MAP_KIND (*pc))
16843 	  {
16844 	  case GOMP_MAP_TO:
16845 	  case GOMP_MAP_ALWAYS_TO:
16846 	  case GOMP_MAP_FROM:
16847 	  case GOMP_MAP_ALWAYS_FROM:
16848 	  case GOMP_MAP_TOFROM:
16849 	  case GOMP_MAP_ALWAYS_TOFROM:
16850 	  case GOMP_MAP_ALLOC:
16851 	    map_seen = 3;
16852 	    break;
16853 	  case GOMP_MAP_FIRSTPRIVATE_POINTER:
16854 	  case GOMP_MAP_ALWAYS_POINTER:
16855 	    break;
16856 	  default:
16857 	    map_seen |= 1;
16858 	    error_at (OMP_CLAUSE_LOCATION (*pc),
16859 		      "%<#pragma omp target data%> with map-type other "
16860 		      "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
16861 		      "on %<map%> clause");
16862 	    *pc = OMP_CLAUSE_CHAIN (*pc);
16863 	    continue;
16864 	  }
16865       pc = &OMP_CLAUSE_CHAIN (*pc);
16866     }
16867 
16868   if (map_seen != 3)
16869     {
16870       if (map_seen == 0)
16871 	error_at (loc,
16872 		  "%<#pragma omp target data%> must contain at least "
16873 		  "one %<map%> clause");
16874       return NULL_TREE;
16875     }
16876 
16877   tree stmt = make_node (OMP_TARGET_DATA);
16878   TREE_TYPE (stmt) = void_type_node;
16879   OMP_TARGET_DATA_CLAUSES (stmt) = clauses;
16880   keep_next_level ();
16881   tree block = c_begin_compound_stmt (true);
16882   add_stmt (c_parser_omp_structured_block (parser, if_p));
16883   OMP_TARGET_DATA_BODY (stmt) = c_end_compound_stmt (loc, block, true);
16884 
16885   SET_EXPR_LOCATION (stmt, loc);
16886   return add_stmt (stmt);
16887 }
16888 
16889 /* OpenMP 4.0:
16890    # pragma omp target update target-update-clause[optseq] new-line */
16891 
16892 #define OMP_TARGET_UPDATE_CLAUSE_MASK				\
16893 	( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM)		\
16894 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO)		\
16895 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE)	\
16896 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF)		\
16897 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)	\
16898 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
16899 
16900 static bool
c_parser_omp_target_update(location_t loc,c_parser * parser,enum pragma_context context)16901 c_parser_omp_target_update (location_t loc, c_parser *parser,
16902 			    enum pragma_context context)
16903 {
16904   if (context == pragma_stmt)
16905     {
16906       error_at (loc, "%<#pragma %s%> may only be used in compound statements",
16907 		"omp target update");
16908       c_parser_skip_to_pragma_eol (parser, false);
16909       return false;
16910     }
16911 
16912   tree clauses
16913     = c_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
16914 				"#pragma omp target update");
16915   if (omp_find_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
16916       && omp_find_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
16917     {
16918       error_at (loc,
16919 		"%<#pragma omp target update%> must contain at least one "
16920 		"%<from%> or %<to%> clauses");
16921       return false;
16922     }
16923 
16924   tree stmt = make_node (OMP_TARGET_UPDATE);
16925   TREE_TYPE (stmt) = void_type_node;
16926   OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
16927   SET_EXPR_LOCATION (stmt, loc);
16928   add_stmt (stmt);
16929   return false;
16930 }
16931 
16932 /* OpenMP 4.5:
16933    # pragma omp target enter data target-data-clause[optseq] new-line  */
16934 
16935 #define OMP_TARGET_ENTER_DATA_CLAUSE_MASK			\
16936 	( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE)	\
16937 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)		\
16938 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF)		\
16939 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)	\
16940 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
16941 
16942 static tree
c_parser_omp_target_enter_data(location_t loc,c_parser * parser,enum pragma_context context)16943 c_parser_omp_target_enter_data (location_t loc, c_parser *parser,
16944 				enum pragma_context context)
16945 {
16946   bool data_seen = false;
16947   if (c_parser_next_token_is (parser, CPP_NAME))
16948     {
16949       const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
16950       if (strcmp (p, "data") == 0)
16951 	{
16952 	  c_parser_consume_token (parser);
16953 	  data_seen = true;
16954 	}
16955     }
16956   if (!data_seen)
16957     {
16958       c_parser_error (parser, "expected %<data%>");
16959       c_parser_skip_to_pragma_eol (parser);
16960       return NULL_TREE;
16961     }
16962 
16963   if (context == pragma_stmt)
16964     {
16965       error_at (loc, "%<#pragma %s%> may only be used in compound statements",
16966 		"omp target enter data");
16967       c_parser_skip_to_pragma_eol (parser, false);
16968       return NULL_TREE;
16969     }
16970 
16971   tree clauses
16972     = c_parser_omp_all_clauses (parser, OMP_TARGET_ENTER_DATA_CLAUSE_MASK,
16973 				"#pragma omp target enter data");
16974   int map_seen = 0;
16975   for (tree *pc = &clauses; *pc;)
16976     {
16977       if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
16978 	switch (OMP_CLAUSE_MAP_KIND (*pc))
16979 	  {
16980 	  case GOMP_MAP_TO:
16981 	  case GOMP_MAP_ALWAYS_TO:
16982 	  case GOMP_MAP_ALLOC:
16983 	    map_seen = 3;
16984 	    break;
16985 	  case GOMP_MAP_FIRSTPRIVATE_POINTER:
16986 	  case GOMP_MAP_ALWAYS_POINTER:
16987 	    break;
16988 	  default:
16989 	    map_seen |= 1;
16990 	    error_at (OMP_CLAUSE_LOCATION (*pc),
16991 		      "%<#pragma omp target enter data%> with map-type other "
16992 		      "than %<to%> or %<alloc%> on %<map%> clause");
16993 	    *pc = OMP_CLAUSE_CHAIN (*pc);
16994 	    continue;
16995 	  }
16996       pc = &OMP_CLAUSE_CHAIN (*pc);
16997     }
16998 
16999   if (map_seen != 3)
17000     {
17001       if (map_seen == 0)
17002 	error_at (loc,
17003 		  "%<#pragma omp target enter data%> must contain at least "
17004 		  "one %<map%> clause");
17005       return NULL_TREE;
17006     }
17007 
17008   tree stmt = make_node (OMP_TARGET_ENTER_DATA);
17009   TREE_TYPE (stmt) = void_type_node;
17010   OMP_TARGET_ENTER_DATA_CLAUSES (stmt) = clauses;
17011   SET_EXPR_LOCATION (stmt, loc);
17012   add_stmt (stmt);
17013   return stmt;
17014 }
17015 
17016 /* OpenMP 4.5:
17017    # pragma omp target exit data target-data-clause[optseq] new-line  */
17018 
17019 #define OMP_TARGET_EXIT_DATA_CLAUSE_MASK			\
17020 	( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE)	\
17021 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)		\
17022 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF)		\
17023 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)	\
17024 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
17025 
17026 static tree
c_parser_omp_target_exit_data(location_t loc,c_parser * parser,enum pragma_context context)17027 c_parser_omp_target_exit_data (location_t loc, c_parser *parser,
17028 			       enum pragma_context context)
17029 {
17030   bool data_seen = false;
17031   if (c_parser_next_token_is (parser, CPP_NAME))
17032     {
17033       const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
17034       if (strcmp (p, "data") == 0)
17035 	{
17036 	  c_parser_consume_token (parser);
17037 	  data_seen = true;
17038 	}
17039     }
17040   if (!data_seen)
17041     {
17042       c_parser_error (parser, "expected %<data%>");
17043       c_parser_skip_to_pragma_eol (parser);
17044       return NULL_TREE;
17045     }
17046 
17047   if (context == pragma_stmt)
17048     {
17049       error_at (loc, "%<#pragma %s%> may only be used in compound statements",
17050 		"omp target exit data");
17051       c_parser_skip_to_pragma_eol (parser, false);
17052       return NULL_TREE;
17053     }
17054 
17055   tree clauses
17056     = c_parser_omp_all_clauses (parser, OMP_TARGET_EXIT_DATA_CLAUSE_MASK,
17057 				"#pragma omp target exit data");
17058 
17059   int map_seen = 0;
17060   for (tree *pc = &clauses; *pc;)
17061     {
17062       if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
17063 	switch (OMP_CLAUSE_MAP_KIND (*pc))
17064 	  {
17065 	  case GOMP_MAP_FROM:
17066 	  case GOMP_MAP_ALWAYS_FROM:
17067 	  case GOMP_MAP_RELEASE:
17068 	  case GOMP_MAP_DELETE:
17069 	    map_seen = 3;
17070 	    break;
17071 	  case GOMP_MAP_FIRSTPRIVATE_POINTER:
17072 	  case GOMP_MAP_ALWAYS_POINTER:
17073 	    break;
17074 	  default:
17075 	    map_seen |= 1;
17076 	    error_at (OMP_CLAUSE_LOCATION (*pc),
17077 		      "%<#pragma omp target exit data%> with map-type other "
17078 		      "than %<from%>, %<release%> or %<delete%> on %<map%>"
17079 		      " clause");
17080 	    *pc = OMP_CLAUSE_CHAIN (*pc);
17081 	    continue;
17082 	  }
17083       pc = &OMP_CLAUSE_CHAIN (*pc);
17084     }
17085 
17086   if (map_seen != 3)
17087     {
17088       if (map_seen == 0)
17089 	error_at (loc,
17090 		  "%<#pragma omp target exit data%> must contain at least one "
17091 		  "%<map%> clause");
17092       return NULL_TREE;
17093     }
17094 
17095   tree stmt = make_node (OMP_TARGET_EXIT_DATA);
17096   TREE_TYPE (stmt) = void_type_node;
17097   OMP_TARGET_EXIT_DATA_CLAUSES (stmt) = clauses;
17098   SET_EXPR_LOCATION (stmt, loc);
17099   add_stmt (stmt);
17100   return stmt;
17101 }
17102 
17103 /* OpenMP 4.0:
17104    # pragma omp target target-clause[optseq] new-line
17105      structured-block  */
17106 
17107 #define OMP_TARGET_CLAUSE_MASK					\
17108 	( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE)	\
17109 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)		\
17110 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF)		\
17111 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)	\
17112 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT)	\
17113 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE)	\
17114 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)	\
17115 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULTMAP)	\
17116 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR))
17117 
17118 static bool
c_parser_omp_target(c_parser * parser,enum pragma_context context,bool * if_p)17119 c_parser_omp_target (c_parser *parser, enum pragma_context context, bool *if_p)
17120 {
17121   location_t loc = c_parser_peek_token (parser)->location;
17122   c_parser_consume_pragma (parser);
17123   tree *pc = NULL, stmt, block;
17124 
17125   if (context != pragma_stmt && context != pragma_compound)
17126     {
17127       c_parser_error (parser, "expected declaration specifiers");
17128       c_parser_skip_to_pragma_eol (parser);
17129       return false;
17130     }
17131 
17132   if (c_parser_next_token_is (parser, CPP_NAME))
17133     {
17134       const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
17135       enum tree_code ccode = ERROR_MARK;
17136 
17137       if (strcmp (p, "teams") == 0)
17138 	ccode = OMP_TEAMS;
17139       else if (strcmp (p, "parallel") == 0)
17140 	ccode = OMP_PARALLEL;
17141       else if (strcmp (p, "simd") == 0)
17142 	ccode = OMP_SIMD;
17143       if (ccode != ERROR_MARK)
17144 	{
17145 	  tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
17146 	  char p_name[sizeof ("#pragma omp target teams distribute "
17147 			      "parallel for simd")];
17148 
17149 	  c_parser_consume_token (parser);
17150 	  strcpy (p_name, "#pragma omp target");
17151 	  if (!flag_openmp)  /* flag_openmp_simd  */
17152 	    {
17153 	      tree stmt;
17154 	      switch (ccode)
17155 		{
17156 		case OMP_TEAMS:
17157 		  stmt = c_parser_omp_teams (loc, parser, p_name,
17158 					     OMP_TARGET_CLAUSE_MASK,
17159 					     cclauses, if_p);
17160 		  break;
17161 		case OMP_PARALLEL:
17162 		  stmt = c_parser_omp_parallel (loc, parser, p_name,
17163 						OMP_TARGET_CLAUSE_MASK,
17164 						cclauses, if_p);
17165 		  break;
17166 		case OMP_SIMD:
17167 		  stmt = c_parser_omp_simd (loc, parser, p_name,
17168 					    OMP_TARGET_CLAUSE_MASK,
17169 					    cclauses, if_p);
17170 		  break;
17171 		default:
17172 		  gcc_unreachable ();
17173 		}
17174 	      return stmt != NULL_TREE;
17175 	    }
17176 	  keep_next_level ();
17177 	  tree block = c_begin_compound_stmt (true), ret;
17178 	  switch (ccode)
17179 	    {
17180 	    case OMP_TEAMS:
17181 	      ret = c_parser_omp_teams (loc, parser, p_name,
17182 					OMP_TARGET_CLAUSE_MASK, cclauses,
17183 					if_p);
17184 	      break;
17185 	    case OMP_PARALLEL:
17186 	      ret = c_parser_omp_parallel (loc, parser, p_name,
17187 					   OMP_TARGET_CLAUSE_MASK, cclauses,
17188 					   if_p);
17189 	      break;
17190 	    case OMP_SIMD:
17191 	      ret = c_parser_omp_simd (loc, parser, p_name,
17192 				       OMP_TARGET_CLAUSE_MASK, cclauses,
17193 				       if_p);
17194 	      break;
17195 	    default:
17196 	      gcc_unreachable ();
17197 	    }
17198 	  block = c_end_compound_stmt (loc, block, true);
17199 	  if (ret == NULL_TREE)
17200 	    return false;
17201 	  if (ccode == OMP_TEAMS)
17202 	    {
17203 	      /* For combined target teams, ensure the num_teams and
17204 		 thread_limit clause expressions are evaluated on the host,
17205 		 before entering the target construct.  */
17206 	      tree c;
17207 	      for (c = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
17208 		   c; c = OMP_CLAUSE_CHAIN (c))
17209 		if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
17210 		     || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
17211 		    && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
17212 		  {
17213 		    tree expr = OMP_CLAUSE_OPERAND (c, 0);
17214 		    tree tmp = create_tmp_var_raw (TREE_TYPE (expr));
17215 		    expr = build4 (TARGET_EXPR, TREE_TYPE (expr), tmp,
17216 				   expr, NULL_TREE, NULL_TREE);
17217 		    add_stmt (expr);
17218 		    OMP_CLAUSE_OPERAND (c, 0) = expr;
17219 		    tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
17220 						OMP_CLAUSE_FIRSTPRIVATE);
17221 		    OMP_CLAUSE_DECL (tc) = tmp;
17222 		    OMP_CLAUSE_CHAIN (tc)
17223 		      = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
17224 		    cclauses[C_OMP_CLAUSE_SPLIT_TARGET] = tc;
17225 		  }
17226 	    }
17227 	  tree stmt = make_node (OMP_TARGET);
17228 	  TREE_TYPE (stmt) = void_type_node;
17229 	  OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
17230 	  OMP_TARGET_BODY (stmt) = block;
17231 	  OMP_TARGET_COMBINED (stmt) = 1;
17232 	  add_stmt (stmt);
17233 	  pc = &OMP_TARGET_CLAUSES (stmt);
17234 	  goto check_clauses;
17235 	}
17236       else if (!flag_openmp)  /* flag_openmp_simd  */
17237 	{
17238 	  c_parser_skip_to_pragma_eol (parser, false);
17239 	  return false;
17240 	}
17241       else if (strcmp (p, "data") == 0)
17242 	{
17243 	  c_parser_consume_token (parser);
17244 	  c_parser_omp_target_data (loc, parser, if_p);
17245 	  return true;
17246 	}
17247       else if (strcmp (p, "enter") == 0)
17248 	{
17249 	  c_parser_consume_token (parser);
17250 	  c_parser_omp_target_enter_data (loc, parser, context);
17251 	  return false;
17252 	}
17253       else if (strcmp (p, "exit") == 0)
17254 	{
17255 	  c_parser_consume_token (parser);
17256 	  c_parser_omp_target_exit_data (loc, parser, context);
17257 	  return false;
17258 	}
17259       else if (strcmp (p, "update") == 0)
17260 	{
17261 	  c_parser_consume_token (parser);
17262 	  return c_parser_omp_target_update (loc, parser, context);
17263 	}
17264     }
17265   if (!flag_openmp) /* flag_openmp_simd  */
17266     {
17267       c_parser_skip_to_pragma_eol (parser, false);
17268       return false;
17269     }
17270 
17271   stmt = make_node (OMP_TARGET);
17272   TREE_TYPE (stmt) = void_type_node;
17273 
17274   OMP_TARGET_CLAUSES (stmt)
17275     = c_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
17276 				"#pragma omp target");
17277   pc = &OMP_TARGET_CLAUSES (stmt);
17278   keep_next_level ();
17279   block = c_begin_compound_stmt (true);
17280   add_stmt (c_parser_omp_structured_block (parser, if_p));
17281   OMP_TARGET_BODY (stmt) = c_end_compound_stmt (loc, block, true);
17282 
17283   SET_EXPR_LOCATION (stmt, loc);
17284   add_stmt (stmt);
17285 
17286 check_clauses:
17287   while (*pc)
17288     {
17289       if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
17290 	switch (OMP_CLAUSE_MAP_KIND (*pc))
17291 	  {
17292 	  case GOMP_MAP_TO:
17293 	  case GOMP_MAP_ALWAYS_TO:
17294 	  case GOMP_MAP_FROM:
17295 	  case GOMP_MAP_ALWAYS_FROM:
17296 	  case GOMP_MAP_TOFROM:
17297 	  case GOMP_MAP_ALWAYS_TOFROM:
17298 	  case GOMP_MAP_ALLOC:
17299 	  case GOMP_MAP_FIRSTPRIVATE_POINTER:
17300 	  case GOMP_MAP_ALWAYS_POINTER:
17301 	    break;
17302 	  default:
17303 	    error_at (OMP_CLAUSE_LOCATION (*pc),
17304 		      "%<#pragma omp target%> with map-type other "
17305 		      "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
17306 		      "on %<map%> clause");
17307 	    *pc = OMP_CLAUSE_CHAIN (*pc);
17308 	    continue;
17309 	  }
17310       pc = &OMP_CLAUSE_CHAIN (*pc);
17311     }
17312   return true;
17313 }
17314 
17315 /* OpenMP 4.0:
17316    # pragma omp declare simd declare-simd-clauses[optseq] new-line  */
17317 
17318 #define OMP_DECLARE_SIMD_CLAUSE_MASK				\
17319 	( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN)	\
17320 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR)	\
17321 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED)	\
17322 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM)	\
17323 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH)	\
17324 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
17325 
17326 static void
c_parser_omp_declare_simd(c_parser * parser,enum pragma_context context)17327 c_parser_omp_declare_simd (c_parser *parser, enum pragma_context context)
17328 {
17329   auto_vec<c_token> clauses;
17330   while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
17331     {
17332       c_token *token = c_parser_peek_token (parser);
17333       if (token->type == CPP_EOF)
17334 	{
17335 	  c_parser_skip_to_pragma_eol (parser);
17336 	  return;
17337 	}
17338       clauses.safe_push (*token);
17339       c_parser_consume_token (parser);
17340     }
17341   clauses.safe_push (*c_parser_peek_token (parser));
17342   c_parser_skip_to_pragma_eol (parser);
17343 
17344   while (c_parser_next_token_is (parser, CPP_PRAGMA))
17345     {
17346       if (c_parser_peek_token (parser)->pragma_kind
17347 	  != PRAGMA_OMP_DECLARE
17348 	  || c_parser_peek_2nd_token (parser)->type != CPP_NAME
17349 	  || strcmp (IDENTIFIER_POINTER
17350 				(c_parser_peek_2nd_token (parser)->value),
17351 		     "simd") != 0)
17352 	{
17353 	  c_parser_error (parser,
17354 			  "%<#pragma omp declare simd%> must be followed by "
17355 			  "function declaration or definition or another "
17356 			  "%<#pragma omp declare simd%>");
17357 	  return;
17358 	}
17359       c_parser_consume_pragma (parser);
17360       while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
17361 	{
17362 	  c_token *token = c_parser_peek_token (parser);
17363 	  if (token->type == CPP_EOF)
17364 	    {
17365 	      c_parser_skip_to_pragma_eol (parser);
17366 	      return;
17367 	    }
17368 	  clauses.safe_push (*token);
17369 	  c_parser_consume_token (parser);
17370 	}
17371       clauses.safe_push (*c_parser_peek_token (parser));
17372       c_parser_skip_to_pragma_eol (parser);
17373     }
17374 
17375   /* Make sure nothing tries to read past the end of the tokens.  */
17376   c_token eof_token;
17377   memset (&eof_token, 0, sizeof (eof_token));
17378   eof_token.type = CPP_EOF;
17379   clauses.safe_push (eof_token);
17380   clauses.safe_push (eof_token);
17381 
17382   switch (context)
17383     {
17384     case pragma_external:
17385       if (c_parser_next_token_is (parser, CPP_KEYWORD)
17386 	  && c_parser_peek_token (parser)->keyword == RID_EXTENSION)
17387 	{
17388 	  int ext = disable_extension_diagnostics ();
17389 	  do
17390 	    c_parser_consume_token (parser);
17391 	  while (c_parser_next_token_is (parser, CPP_KEYWORD)
17392 		 && c_parser_peek_token (parser)->keyword == RID_EXTENSION);
17393 	  c_parser_declaration_or_fndef (parser, true, true, true, false, true,
17394 					 NULL, clauses);
17395 	  restore_extension_diagnostics (ext);
17396 	}
17397       else
17398 	c_parser_declaration_or_fndef (parser, true, true, true, false, true,
17399 				       NULL, clauses);
17400       break;
17401     case pragma_struct:
17402     case pragma_param:
17403     case pragma_stmt:
17404       c_parser_error (parser, "%<#pragma omp declare simd%> must be followed by "
17405 			      "function declaration or definition");
17406       break;
17407     case pragma_compound:
17408       if (c_parser_next_token_is (parser, CPP_KEYWORD)
17409 	  && c_parser_peek_token (parser)->keyword == RID_EXTENSION)
17410 	{
17411 	  int ext = disable_extension_diagnostics ();
17412 	  do
17413 	    c_parser_consume_token (parser);
17414 	  while (c_parser_next_token_is (parser, CPP_KEYWORD)
17415 		 && c_parser_peek_token (parser)->keyword == RID_EXTENSION);
17416 	  if (c_parser_next_tokens_start_declaration (parser))
17417 	    {
17418 	      c_parser_declaration_or_fndef (parser, true, true, true, true,
17419 					     true, NULL, clauses);
17420 	      restore_extension_diagnostics (ext);
17421 	      break;
17422 	    }
17423 	  restore_extension_diagnostics (ext);
17424 	}
17425       else if (c_parser_next_tokens_start_declaration (parser))
17426 	{
17427 	  c_parser_declaration_or_fndef (parser, true, true, true, true, true,
17428 					 NULL, clauses);
17429 	  break;
17430 	}
17431       c_parser_error (parser, "%<#pragma omp declare simd%> must be followed by "
17432 			      "function declaration or definition");
17433       break;
17434     default:
17435       gcc_unreachable ();
17436     }
17437 }
17438 
17439 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
17440    and put that into "omp declare simd" attribute.  */
17441 
17442 static void
c_finish_omp_declare_simd(c_parser * parser,tree fndecl,tree parms,vec<c_token> clauses)17443 c_finish_omp_declare_simd (c_parser *parser, tree fndecl, tree parms,
17444 			   vec<c_token> clauses)
17445 {
17446   /* Normally first token is CPP_NAME "simd".  CPP_EOF there indicates
17447      error has been reported and CPP_PRAGMA that c_finish_omp_declare_simd
17448      has already processed the tokens.  */
17449   if (clauses.exists () && clauses[0].type == CPP_EOF)
17450     return;
17451   if (fndecl == NULL_TREE || TREE_CODE (fndecl) != FUNCTION_DECL)
17452     {
17453       error ("%<#pragma omp declare simd%> not immediately followed by "
17454 	     "a function declaration or definition");
17455       clauses[0].type = CPP_EOF;
17456       return;
17457     }
17458   if (clauses.exists () && clauses[0].type != CPP_NAME)
17459     {
17460       error_at (DECL_SOURCE_LOCATION (fndecl),
17461 		"%<#pragma omp declare simd%> not immediately followed by "
17462 		"a single function declaration or definition");
17463       clauses[0].type = CPP_EOF;
17464       return;
17465     }
17466 
17467   if (parms == NULL_TREE)
17468     parms = DECL_ARGUMENTS (fndecl);
17469 
17470   unsigned int tokens_avail = parser->tokens_avail;
17471   gcc_assert (parser->tokens == &parser->tokens_buf[0]);
17472 
17473 
17474   parser->tokens = clauses.address ();
17475   parser->tokens_avail = clauses.length ();
17476 
17477   /* c_parser_omp_declare_simd pushed 2 extra CPP_EOF tokens at the end.  */
17478   while (parser->tokens_avail > 3)
17479     {
17480       c_token *token = c_parser_peek_token (parser);
17481       gcc_assert (token->type == CPP_NAME
17482 		  && strcmp (IDENTIFIER_POINTER (token->value), "simd") == 0);
17483       c_parser_consume_token (parser);
17484       parser->in_pragma = true;
17485 
17486       tree c = NULL_TREE;
17487       c = c_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
17488 				      "#pragma omp declare simd");
17489       c = c_omp_declare_simd_clauses_to_numbers (parms, c);
17490       if (c != NULL_TREE)
17491 	c = tree_cons (NULL_TREE, c, NULL_TREE);
17492       c = build_tree_list (get_identifier ("omp declare simd"), c);
17493       TREE_CHAIN (c) = DECL_ATTRIBUTES (fndecl);
17494       DECL_ATTRIBUTES (fndecl) = c;
17495     }
17496 
17497   parser->tokens = &parser->tokens_buf[0];
17498   parser->tokens_avail = tokens_avail;
17499   if (clauses.exists ())
17500     clauses[0].type = CPP_PRAGMA;
17501 }
17502 
17503 
17504 /* OpenMP 4.0:
17505    # pragma omp declare target new-line
17506    declarations and definitions
17507    # pragma omp end declare target new-line
17508 
17509    OpenMP 4.5:
17510    # pragma omp declare target ( extended-list ) new-line
17511 
17512    # pragma omp declare target declare-target-clauses[seq] new-line  */
17513 
17514 #define OMP_DECLARE_TARGET_CLAUSE_MASK				\
17515 	( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO)		\
17516 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK))
17517 
17518 static void
c_parser_omp_declare_target(c_parser * parser)17519 c_parser_omp_declare_target (c_parser *parser)
17520 {
17521   location_t loc = c_parser_peek_token (parser)->location;
17522   tree clauses = NULL_TREE;
17523   if (c_parser_next_token_is (parser, CPP_NAME))
17524     clauses = c_parser_omp_all_clauses (parser, OMP_DECLARE_TARGET_CLAUSE_MASK,
17525 					"#pragma omp declare target");
17526   else if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
17527     {
17528       clauses = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_TO_DECLARE,
17529 					      clauses);
17530       clauses = c_finish_omp_clauses (clauses, C_ORT_OMP);
17531       c_parser_skip_to_pragma_eol (parser);
17532     }
17533   else
17534     {
17535       c_parser_skip_to_pragma_eol (parser);
17536       current_omp_declare_target_attribute++;
17537       return;
17538     }
17539   if (current_omp_declare_target_attribute)
17540     error_at (loc, "%<#pragma omp declare target%> with clauses in between "
17541 		   "%<#pragma omp declare target%> without clauses and "
17542 		   "%<#pragma omp end declare target%>");
17543   for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
17544     {
17545       tree t = OMP_CLAUSE_DECL (c), id;
17546       tree at1 = lookup_attribute ("omp declare target", DECL_ATTRIBUTES (t));
17547       tree at2 = lookup_attribute ("omp declare target link",
17548 				   DECL_ATTRIBUTES (t));
17549       if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINK)
17550 	{
17551 	  id = get_identifier ("omp declare target link");
17552 	  std::swap (at1, at2);
17553 	}
17554       else
17555 	id = get_identifier ("omp declare target");
17556       if (at2)
17557 	{
17558 	  error_at (OMP_CLAUSE_LOCATION (c),
17559 		    "%qD specified both in declare target %<link%> and %<to%>"
17560 		    " clauses", t);
17561 	  continue;
17562 	}
17563       if (!at1)
17564 	{
17565 	  DECL_ATTRIBUTES (t) = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
17566 	  if (TREE_CODE (t) != FUNCTION_DECL && !is_global_var (t))
17567 	    continue;
17568 
17569 	  symtab_node *node = symtab_node::get (t);
17570 	  if (node != NULL)
17571 	    {
17572 	      node->offloadable = 1;
17573 	      if (ENABLE_OFFLOADING)
17574 		{
17575 		  g->have_offload = true;
17576 		  if (is_a <varpool_node *> (node))
17577 		    vec_safe_push (offload_vars, t);
17578 		}
17579 	    }
17580 	}
17581     }
17582 }
17583 
17584 static void
c_parser_omp_end_declare_target(c_parser * parser)17585 c_parser_omp_end_declare_target (c_parser *parser)
17586 {
17587   location_t loc = c_parser_peek_token (parser)->location;
17588   c_parser_consume_pragma (parser);
17589   if (c_parser_next_token_is (parser, CPP_NAME)
17590       && strcmp (IDENTIFIER_POINTER (c_parser_peek_token (parser)->value),
17591 		 "declare") == 0)
17592     {
17593       c_parser_consume_token (parser);
17594       if (c_parser_next_token_is (parser, CPP_NAME)
17595 	  && strcmp (IDENTIFIER_POINTER (c_parser_peek_token (parser)->value),
17596 		     "target") == 0)
17597 	c_parser_consume_token (parser);
17598       else
17599 	{
17600 	  c_parser_error (parser, "expected %<target%>");
17601 	  c_parser_skip_to_pragma_eol (parser);
17602 	  return;
17603 	}
17604     }
17605   else
17606     {
17607       c_parser_error (parser, "expected %<declare%>");
17608       c_parser_skip_to_pragma_eol (parser);
17609       return;
17610     }
17611   c_parser_skip_to_pragma_eol (parser);
17612   if (!current_omp_declare_target_attribute)
17613     error_at (loc, "%<#pragma omp end declare target%> without corresponding "
17614 		   "%<#pragma omp declare target%>");
17615   else
17616     current_omp_declare_target_attribute--;
17617 }
17618 
17619 
17620 /* OpenMP 4.0
17621    #pragma omp declare reduction (reduction-id : typename-list : expression) \
17622       initializer-clause[opt] new-line
17623 
17624    initializer-clause:
17625       initializer (omp_priv = initializer)
17626       initializer (function-name (argument-list))  */
17627 
17628 static void
c_parser_omp_declare_reduction(c_parser * parser,enum pragma_context context)17629 c_parser_omp_declare_reduction (c_parser *parser, enum pragma_context context)
17630 {
17631   unsigned int tokens_avail = 0, i;
17632   vec<tree> types = vNULL;
17633   vec<c_token> clauses = vNULL;
17634   enum tree_code reduc_code = ERROR_MARK;
17635   tree reduc_id = NULL_TREE;
17636   tree type;
17637   location_t rloc = c_parser_peek_token (parser)->location;
17638 
17639   if (context == pragma_struct || context == pragma_param)
17640     {
17641       error ("%<#pragma omp declare reduction%> not at file or block scope");
17642       goto fail;
17643     }
17644 
17645   if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
17646     goto fail;
17647 
17648   switch (c_parser_peek_token (parser)->type)
17649     {
17650     case CPP_PLUS:
17651       reduc_code = PLUS_EXPR;
17652       break;
17653     case CPP_MULT:
17654       reduc_code = MULT_EXPR;
17655       break;
17656     case CPP_MINUS:
17657       reduc_code = MINUS_EXPR;
17658       break;
17659     case CPP_AND:
17660       reduc_code = BIT_AND_EXPR;
17661       break;
17662     case CPP_XOR:
17663       reduc_code = BIT_XOR_EXPR;
17664       break;
17665     case CPP_OR:
17666       reduc_code = BIT_IOR_EXPR;
17667       break;
17668     case CPP_AND_AND:
17669       reduc_code = TRUTH_ANDIF_EXPR;
17670       break;
17671     case CPP_OR_OR:
17672       reduc_code = TRUTH_ORIF_EXPR;
17673       break;
17674     case CPP_NAME:
17675       const char *p;
17676       p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
17677       if (strcmp (p, "min") == 0)
17678 	{
17679 	  reduc_code = MIN_EXPR;
17680 	  break;
17681 	}
17682       if (strcmp (p, "max") == 0)
17683 	{
17684 	  reduc_code = MAX_EXPR;
17685 	  break;
17686 	}
17687       reduc_id = c_parser_peek_token (parser)->value;
17688       break;
17689     default:
17690       c_parser_error (parser,
17691 		      "expected %<+%>, %<*%>, %<-%>, %<&%>, "
17692 		      "%<^%>, %<|%>, %<&&%>, %<||%> or identifier");
17693       goto fail;
17694     }
17695 
17696   tree orig_reduc_id, reduc_decl;
17697   orig_reduc_id = reduc_id;
17698   reduc_id = c_omp_reduction_id (reduc_code, reduc_id);
17699   reduc_decl = c_omp_reduction_decl (reduc_id);
17700   c_parser_consume_token (parser);
17701 
17702   if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
17703     goto fail;
17704 
17705   while (true)
17706     {
17707       location_t loc = c_parser_peek_token (parser)->location;
17708       struct c_type_name *ctype = c_parser_type_name (parser);
17709       if (ctype != NULL)
17710 	{
17711 	  type = groktypename (ctype, NULL, NULL);
17712 	  if (type == error_mark_node)
17713 	    ;
17714 	  else if ((INTEGRAL_TYPE_P (type)
17715 		    || TREE_CODE (type) == REAL_TYPE
17716 		    || TREE_CODE (type) == COMPLEX_TYPE)
17717 		   && orig_reduc_id == NULL_TREE)
17718 	    error_at (loc, "predeclared arithmetic type in "
17719 			   "%<#pragma omp declare reduction%>");
17720 	  else if (TREE_CODE (type) == FUNCTION_TYPE
17721 		   || TREE_CODE (type) == ARRAY_TYPE)
17722 	    error_at (loc, "function or array type in "
17723 		      "%<#pragma omp declare reduction%>");
17724 	  else if (TYPE_ATOMIC (type))
17725 	    error_at (loc, "%<_Atomic%> qualified type in "
17726 			   "%<#pragma omp declare reduction%>");
17727 	  else if (TYPE_QUALS_NO_ADDR_SPACE (type))
17728 	    error_at (loc, "const, volatile or restrict qualified type in "
17729 			   "%<#pragma omp declare reduction%>");
17730 	  else
17731 	    {
17732 	      tree t;
17733 	      for (t = DECL_INITIAL (reduc_decl); t; t = TREE_CHAIN (t))
17734 		if (comptypes (TREE_PURPOSE (t), type))
17735 		  {
17736 		    error_at (loc, "redeclaration of %qs "
17737 				   "%<#pragma omp declare reduction%> for "
17738 				   "type %qT",
17739 				   IDENTIFIER_POINTER (reduc_id)
17740 				   + sizeof ("omp declare reduction ") - 1,
17741 				   type);
17742 		    location_t ploc
17743 		      = DECL_SOURCE_LOCATION (TREE_VEC_ELT (TREE_VALUE (t),
17744 							    0));
17745 		    error_at (ploc, "previous %<#pragma omp declare "
17746 				    "reduction%>");
17747 		    break;
17748 		  }
17749 	      if (t == NULL_TREE)
17750 		types.safe_push (type);
17751 	    }
17752 	  if (c_parser_next_token_is (parser, CPP_COMMA))
17753 	    c_parser_consume_token (parser);
17754 	  else
17755 	    break;
17756 	}
17757       else
17758 	break;
17759     }
17760 
17761   if (!c_parser_require (parser, CPP_COLON, "expected %<:%>")
17762       || types.is_empty ())
17763     {
17764      fail:
17765       clauses.release ();
17766       types.release ();
17767       while (true)
17768 	{
17769 	  c_token *token = c_parser_peek_token (parser);
17770 	  if (token->type == CPP_EOF || token->type == CPP_PRAGMA_EOL)
17771 	    break;
17772 	  c_parser_consume_token (parser);
17773 	}
17774       c_parser_skip_to_pragma_eol (parser);
17775       return;
17776     }
17777 
17778   if (types.length () > 1)
17779     {
17780       while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
17781 	{
17782 	  c_token *token = c_parser_peek_token (parser);
17783 	  if (token->type == CPP_EOF)
17784 	    goto fail;
17785 	  clauses.safe_push (*token);
17786 	  c_parser_consume_token (parser);
17787 	}
17788       clauses.safe_push (*c_parser_peek_token (parser));
17789       c_parser_skip_to_pragma_eol (parser);
17790 
17791       /* Make sure nothing tries to read past the end of the tokens.  */
17792       c_token eof_token;
17793       memset (&eof_token, 0, sizeof (eof_token));
17794       eof_token.type = CPP_EOF;
17795       clauses.safe_push (eof_token);
17796       clauses.safe_push (eof_token);
17797     }
17798 
17799   int errs = errorcount;
17800   FOR_EACH_VEC_ELT (types, i, type)
17801     {
17802       tokens_avail = parser->tokens_avail;
17803       gcc_assert (parser->tokens == &parser->tokens_buf[0]);
17804       if (!clauses.is_empty ())
17805 	{
17806 	  parser->tokens = clauses.address ();
17807 	  parser->tokens_avail = clauses.length ();
17808 	  parser->in_pragma = true;
17809 	}
17810 
17811       bool nested = current_function_decl != NULL_TREE;
17812       if (nested)
17813 	c_push_function_context ();
17814       tree fndecl = build_decl (BUILTINS_LOCATION, FUNCTION_DECL,
17815 				reduc_id, default_function_type);
17816       current_function_decl = fndecl;
17817       allocate_struct_function (fndecl, true);
17818       push_scope ();
17819       tree stmt = push_stmt_list ();
17820       /* Intentionally BUILTINS_LOCATION, so that -Wshadow doesn't
17821 	 warn about these.  */
17822       tree omp_out = build_decl (BUILTINS_LOCATION, VAR_DECL,
17823 				 get_identifier ("omp_out"), type);
17824       DECL_ARTIFICIAL (omp_out) = 1;
17825       DECL_CONTEXT (omp_out) = fndecl;
17826       pushdecl (omp_out);
17827       tree omp_in = build_decl (BUILTINS_LOCATION, VAR_DECL,
17828 				get_identifier ("omp_in"), type);
17829       DECL_ARTIFICIAL (omp_in) = 1;
17830       DECL_CONTEXT (omp_in) = fndecl;
17831       pushdecl (omp_in);
17832       struct c_expr combiner = c_parser_expression (parser);
17833       struct c_expr initializer;
17834       tree omp_priv = NULL_TREE, omp_orig = NULL_TREE;
17835       bool bad = false;
17836       initializer.set_error ();
17837       if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
17838 	bad = true;
17839       else if (c_parser_next_token_is (parser, CPP_NAME)
17840 	       && strcmp (IDENTIFIER_POINTER
17841 				(c_parser_peek_token (parser)->value),
17842 			  "initializer") == 0)
17843 	{
17844 	  c_parser_consume_token (parser);
17845 	  pop_scope ();
17846 	  push_scope ();
17847 	  omp_priv = build_decl (BUILTINS_LOCATION, VAR_DECL,
17848 				 get_identifier ("omp_priv"), type);
17849 	  DECL_ARTIFICIAL (omp_priv) = 1;
17850 	  DECL_INITIAL (omp_priv) = error_mark_node;
17851 	  DECL_CONTEXT (omp_priv) = fndecl;
17852 	  pushdecl (omp_priv);
17853 	  omp_orig = build_decl (BUILTINS_LOCATION, VAR_DECL,
17854 				 get_identifier ("omp_orig"), type);
17855 	  DECL_ARTIFICIAL (omp_orig) = 1;
17856 	  DECL_CONTEXT (omp_orig) = fndecl;
17857 	  pushdecl (omp_orig);
17858 	  if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
17859 	    bad = true;
17860 	  else if (!c_parser_next_token_is (parser, CPP_NAME))
17861 	    {
17862 	      c_parser_error (parser, "expected %<omp_priv%> or "
17863 				      "function-name");
17864 	      bad = true;
17865 	    }
17866 	  else if (strcmp (IDENTIFIER_POINTER
17867 				(c_parser_peek_token (parser)->value),
17868 			   "omp_priv") != 0)
17869 	    {
17870 	      if (c_parser_peek_2nd_token (parser)->type != CPP_OPEN_PAREN
17871 		  || c_parser_peek_token (parser)->id_kind != C_ID_ID)
17872 		{
17873 		  c_parser_error (parser, "expected function-name %<(%>");
17874 		  bad = true;
17875 		}
17876 	      else
17877 		initializer = c_parser_postfix_expression (parser);
17878 	      if (initializer.value
17879 		  && TREE_CODE (initializer.value) == CALL_EXPR)
17880 		{
17881 		  int j;
17882 		  tree c = initializer.value;
17883 		  for (j = 0; j < call_expr_nargs (c); j++)
17884 		    {
17885 		      tree a = CALL_EXPR_ARG (c, j);
17886 		      STRIP_NOPS (a);
17887 		      if (TREE_CODE (a) == ADDR_EXPR
17888 			  && TREE_OPERAND (a, 0) == omp_priv)
17889 			break;
17890 		    }
17891 		  if (j == call_expr_nargs (c))
17892 		    error ("one of the initializer call arguments should be "
17893 			   "%<&omp_priv%>");
17894 		}
17895 	    }
17896 	  else
17897 	    {
17898 	      c_parser_consume_token (parser);
17899 	      if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
17900 		bad = true;
17901 	      else
17902 		{
17903 		  tree st = push_stmt_list ();
17904 		  location_t loc = c_parser_peek_token (parser)->location;
17905 		  rich_location richloc (line_table, loc);
17906 		  start_init (omp_priv, NULL_TREE, 0, &richloc);
17907 		  struct c_expr init = c_parser_initializer (parser);
17908 		  finish_init ();
17909 		  finish_decl (omp_priv, loc, init.value,
17910 		      	       init.original_type, NULL_TREE);
17911 		  pop_stmt_list (st);
17912 		}
17913 	    }
17914 	  if (!bad
17915 	      && !c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
17916 	    bad = true;
17917 	}
17918 
17919       if (!bad)
17920 	{
17921 	  c_parser_skip_to_pragma_eol (parser);
17922 
17923 	  tree t = tree_cons (type, make_tree_vec (omp_priv ? 6 : 3),
17924 			      DECL_INITIAL (reduc_decl));
17925 	  DECL_INITIAL (reduc_decl) = t;
17926 	  DECL_SOURCE_LOCATION (omp_out) = rloc;
17927 	  TREE_VEC_ELT (TREE_VALUE (t), 0) = omp_out;
17928 	  TREE_VEC_ELT (TREE_VALUE (t), 1) = omp_in;
17929 	  TREE_VEC_ELT (TREE_VALUE (t), 2) = combiner.value;
17930 	  walk_tree (&combiner.value, c_check_omp_declare_reduction_r,
17931 		     &TREE_VEC_ELT (TREE_VALUE (t), 0), NULL);
17932 	  if (omp_priv)
17933 	    {
17934 	      DECL_SOURCE_LOCATION (omp_priv) = rloc;
17935 	      TREE_VEC_ELT (TREE_VALUE (t), 3) = omp_priv;
17936 	      TREE_VEC_ELT (TREE_VALUE (t), 4) = omp_orig;
17937 	      TREE_VEC_ELT (TREE_VALUE (t), 5) = initializer.value;
17938 	      walk_tree (&initializer.value, c_check_omp_declare_reduction_r,
17939 			 &TREE_VEC_ELT (TREE_VALUE (t), 3), NULL);
17940 	      walk_tree (&DECL_INITIAL (omp_priv),
17941 			 c_check_omp_declare_reduction_r,
17942 			 &TREE_VEC_ELT (TREE_VALUE (t), 3), NULL);
17943 	    }
17944 	}
17945 
17946       pop_stmt_list (stmt);
17947       pop_scope ();
17948       if (cfun->language != NULL)
17949 	{
17950 	  ggc_free (cfun->language);
17951 	  cfun->language = NULL;
17952 	}
17953       set_cfun (NULL);
17954       current_function_decl = NULL_TREE;
17955       if (nested)
17956 	c_pop_function_context ();
17957 
17958       if (!clauses.is_empty ())
17959 	{
17960 	  parser->tokens = &parser->tokens_buf[0];
17961 	  parser->tokens_avail = tokens_avail;
17962 	}
17963       if (bad)
17964 	goto fail;
17965       if (errs != errorcount)
17966 	break;
17967     }
17968 
17969   clauses.release ();
17970   types.release ();
17971 }
17972 
17973 
17974 /* OpenMP 4.0
17975    #pragma omp declare simd declare-simd-clauses[optseq] new-line
17976    #pragma omp declare reduction (reduction-id : typename-list : expression) \
17977       initializer-clause[opt] new-line
17978    #pragma omp declare target new-line  */
17979 
17980 static void
c_parser_omp_declare(c_parser * parser,enum pragma_context context)17981 c_parser_omp_declare (c_parser *parser, enum pragma_context context)
17982 {
17983   c_parser_consume_pragma (parser);
17984   if (c_parser_next_token_is (parser, CPP_NAME))
17985     {
17986       const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
17987       if (strcmp (p, "simd") == 0)
17988 	{
17989 	  /* c_parser_consume_token (parser); done in
17990 	     c_parser_omp_declare_simd.  */
17991 	  c_parser_omp_declare_simd (parser, context);
17992 	  return;
17993 	}
17994       if (strcmp (p, "reduction") == 0)
17995 	{
17996 	  c_parser_consume_token (parser);
17997 	  c_parser_omp_declare_reduction (parser, context);
17998 	  return;
17999 	}
18000       if (!flag_openmp)  /* flag_openmp_simd  */
18001 	{
18002 	  c_parser_skip_to_pragma_eol (parser, false);
18003 	  return;
18004 	}
18005       if (strcmp (p, "target") == 0)
18006 	{
18007 	  c_parser_consume_token (parser);
18008 	  c_parser_omp_declare_target (parser);
18009 	  return;
18010 	}
18011     }
18012 
18013   c_parser_error (parser, "expected %<simd%> or %<reduction%> "
18014 			  "or %<target%>");
18015   c_parser_skip_to_pragma_eol (parser);
18016 }
18017 
18018 /* OpenMP 4.5:
18019    #pragma omp taskloop taskloop-clause[optseq] new-line
18020      for-loop
18021 
18022    #pragma omp taskloop simd taskloop-simd-clause[optseq] new-line
18023      for-loop  */
18024 
18025 #define OMP_TASKLOOP_CLAUSE_MASK				\
18026 	( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED)	\
18027 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE)	\
18028 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)	\
18029 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE)	\
18030 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT)	\
18031 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_GRAINSIZE)	\
18032 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TASKS)	\
18033 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE)	\
18034 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED)	\
18035 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF)		\
18036 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL)	\
18037 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE)	\
18038 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP)	\
18039 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
18040 
18041 static tree
c_parser_omp_taskloop(location_t loc,c_parser * parser,char * p_name,omp_clause_mask mask,tree * cclauses,bool * if_p)18042 c_parser_omp_taskloop (location_t loc, c_parser *parser,
18043 		       char *p_name, omp_clause_mask mask, tree *cclauses,
18044 		       bool *if_p)
18045 {
18046   tree clauses, block, ret;
18047 
18048   strcat (p_name, " taskloop");
18049   mask |= OMP_TASKLOOP_CLAUSE_MASK;
18050 
18051   if (c_parser_next_token_is (parser, CPP_NAME))
18052     {
18053       const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
18054 
18055       if (strcmp (p, "simd") == 0)
18056 	{
18057 	  tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
18058 	  if (cclauses == NULL)
18059 	    cclauses = cclauses_buf;
18060 	  mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION);
18061 	  c_parser_consume_token (parser);
18062 	  if (!flag_openmp)  /* flag_openmp_simd  */
18063 	    return c_parser_omp_simd (loc, parser, p_name, mask, cclauses,
18064 				      if_p);
18065 	  block = c_begin_compound_stmt (true);
18066 	  ret = c_parser_omp_simd (loc, parser, p_name, mask, cclauses, if_p);
18067 	  block = c_end_compound_stmt (loc, block, true);
18068 	  if (ret == NULL)
18069 	    return ret;
18070 	  ret = make_node (OMP_TASKLOOP);
18071 	  TREE_TYPE (ret) = void_type_node;
18072 	  OMP_FOR_BODY (ret) = block;
18073 	  OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
18074 	  SET_EXPR_LOCATION (ret, loc);
18075 	  add_stmt (ret);
18076 	  return ret;
18077 	}
18078     }
18079   if (!flag_openmp)  /* flag_openmp_simd  */
18080     {
18081       c_parser_skip_to_pragma_eol (parser, false);
18082       return NULL_TREE;
18083     }
18084 
18085   clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
18086   if (cclauses)
18087     {
18088       omp_split_clauses (loc, OMP_TASKLOOP, mask, clauses, cclauses);
18089       clauses = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
18090     }
18091 
18092   block = c_begin_compound_stmt (true);
18093   ret = c_parser_omp_for_loop (loc, parser, OMP_TASKLOOP, clauses, NULL, if_p);
18094   block = c_end_compound_stmt (loc, block, true);
18095   add_stmt (block);
18096 
18097   return ret;
18098 }
18099 
18100 /* Main entry point to parsing most OpenMP pragmas.  */
18101 
18102 static void
c_parser_omp_construct(c_parser * parser,bool * if_p)18103 c_parser_omp_construct (c_parser *parser, bool *if_p)
18104 {
18105   enum pragma_kind p_kind;
18106   location_t loc;
18107   tree stmt;
18108   char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
18109   omp_clause_mask mask (0);
18110 
18111   loc = c_parser_peek_token (parser)->location;
18112   p_kind = c_parser_peek_token (parser)->pragma_kind;
18113   c_parser_consume_pragma (parser);
18114 
18115   switch (p_kind)
18116     {
18117     case PRAGMA_OACC_ATOMIC:
18118       c_parser_omp_atomic (loc, parser);
18119       return;
18120     case PRAGMA_OACC_CACHE:
18121       strcpy (p_name, "#pragma acc");
18122       stmt = c_parser_oacc_cache (loc, parser);
18123       break;
18124     case PRAGMA_OACC_DATA:
18125       stmt = c_parser_oacc_data (loc, parser, if_p);
18126       break;
18127     case PRAGMA_OACC_HOST_DATA:
18128       stmt = c_parser_oacc_host_data (loc, parser, if_p);
18129       break;
18130     case PRAGMA_OACC_KERNELS:
18131     case PRAGMA_OACC_PARALLEL:
18132       strcpy (p_name, "#pragma acc");
18133       stmt = c_parser_oacc_kernels_parallel (loc, parser, p_kind, p_name,
18134 					     if_p);
18135       break;
18136     case PRAGMA_OACC_LOOP:
18137       strcpy (p_name, "#pragma acc");
18138       stmt = c_parser_oacc_loop (loc, parser, p_name, mask, NULL, if_p);
18139       break;
18140     case PRAGMA_OACC_WAIT:
18141       strcpy (p_name, "#pragma wait");
18142       stmt = c_parser_oacc_wait (loc, parser, p_name);
18143       break;
18144     case PRAGMA_OMP_ATOMIC:
18145       c_parser_omp_atomic (loc, parser);
18146       return;
18147     case PRAGMA_OMP_CRITICAL:
18148       stmt = c_parser_omp_critical (loc, parser, if_p);
18149       break;
18150     case PRAGMA_OMP_DISTRIBUTE:
18151       strcpy (p_name, "#pragma omp");
18152       stmt = c_parser_omp_distribute (loc, parser, p_name, mask, NULL, if_p);
18153       break;
18154     case PRAGMA_OMP_FOR:
18155       strcpy (p_name, "#pragma omp");
18156       stmt = c_parser_omp_for (loc, parser, p_name, mask, NULL, if_p);
18157       break;
18158     case PRAGMA_OMP_MASTER:
18159       stmt = c_parser_omp_master (loc, parser, if_p);
18160       break;
18161     case PRAGMA_OMP_PARALLEL:
18162       strcpy (p_name, "#pragma omp");
18163       stmt = c_parser_omp_parallel (loc, parser, p_name, mask, NULL, if_p);
18164       break;
18165     case PRAGMA_OMP_SECTIONS:
18166       strcpy (p_name, "#pragma omp");
18167       stmt = c_parser_omp_sections (loc, parser, p_name, mask, NULL);
18168       break;
18169     case PRAGMA_OMP_SIMD:
18170       strcpy (p_name, "#pragma omp");
18171       stmt = c_parser_omp_simd (loc, parser, p_name, mask, NULL, if_p);
18172       break;
18173     case PRAGMA_OMP_SINGLE:
18174       stmt = c_parser_omp_single (loc, parser, if_p);
18175       break;
18176     case PRAGMA_OMP_TASK:
18177       stmt = c_parser_omp_task (loc, parser, if_p);
18178       break;
18179     case PRAGMA_OMP_TASKGROUP:
18180       stmt = c_parser_omp_taskgroup (parser, if_p);
18181       break;
18182     case PRAGMA_OMP_TASKLOOP:
18183       strcpy (p_name, "#pragma omp");
18184       stmt = c_parser_omp_taskloop (loc, parser, p_name, mask, NULL, if_p);
18185       break;
18186     case PRAGMA_OMP_TEAMS:
18187       strcpy (p_name, "#pragma omp");
18188       stmt = c_parser_omp_teams (loc, parser, p_name, mask, NULL, if_p);
18189       break;
18190     default:
18191       gcc_unreachable ();
18192     }
18193 
18194   if (stmt)
18195     gcc_assert (EXPR_LOCATION (stmt) != UNKNOWN_LOCATION);
18196 }
18197 
18198 
18199 /* OpenMP 2.5:
18200    # pragma omp threadprivate (variable-list) */
18201 
18202 static void
c_parser_omp_threadprivate(c_parser * parser)18203 c_parser_omp_threadprivate (c_parser *parser)
18204 {
18205   tree vars, t;
18206   location_t loc;
18207 
18208   c_parser_consume_pragma (parser);
18209   loc = c_parser_peek_token (parser)->location;
18210   vars = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
18211 
18212   /* Mark every variable in VARS to be assigned thread local storage.  */
18213   for (t = vars; t; t = TREE_CHAIN (t))
18214     {
18215       tree v = TREE_PURPOSE (t);
18216 
18217       /* FIXME diagnostics: Ideally we should keep individual
18218 	 locations for all the variables in the var list to make the
18219 	 following errors more precise.  Perhaps
18220 	 c_parser_omp_var_list_parens() should construct a list of
18221 	 locations to go along with the var list.  */
18222 
18223       /* If V had already been marked threadprivate, it doesn't matter
18224 	 whether it had been used prior to this point.  */
18225       if (!VAR_P (v))
18226 	error_at (loc, "%qD is not a variable", v);
18227       else if (TREE_USED (v) && !C_DECL_THREADPRIVATE_P (v))
18228 	error_at (loc, "%qE declared %<threadprivate%> after first use", v);
18229       else if (! is_global_var (v))
18230 	error_at (loc, "automatic variable %qE cannot be %<threadprivate%>", v);
18231       else if (TREE_TYPE (v) == error_mark_node)
18232 	;
18233       else if (! COMPLETE_TYPE_P (TREE_TYPE (v)))
18234 	error_at (loc, "%<threadprivate%> %qE has incomplete type", v);
18235       else
18236 	{
18237 	  if (! DECL_THREAD_LOCAL_P (v))
18238 	    {
18239 	      set_decl_tls_model (v, decl_default_tls_model (v));
18240 	      /* If rtl has been already set for this var, call
18241 		 make_decl_rtl once again, so that encode_section_info
18242 		 has a chance to look at the new decl flags.  */
18243 	      if (DECL_RTL_SET_P (v))
18244 		make_decl_rtl (v);
18245 	    }
18246 	  C_DECL_THREADPRIVATE_P (v) = 1;
18247 	}
18248     }
18249 
18250   c_parser_skip_to_pragma_eol (parser);
18251 }
18252 
18253 /* Parse a transaction attribute (GCC Extension).
18254 
18255    transaction-attribute:
18256      attributes
18257      [ [ any-word ] ]
18258 
18259    The transactional memory language description is written for C++,
18260    and uses the C++0x attribute syntax.  For compatibility, allow the
18261    bracket style for transactions in C as well.  */
18262 
18263 static tree
c_parser_transaction_attributes(c_parser * parser)18264 c_parser_transaction_attributes (c_parser *parser)
18265 {
18266   tree attr_name, attr = NULL;
18267 
18268   if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
18269     return c_parser_attributes (parser);
18270 
18271   if (!c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
18272     return NULL_TREE;
18273   c_parser_consume_token (parser);
18274   if (!c_parser_require (parser, CPP_OPEN_SQUARE, "expected %<[%>"))
18275     goto error1;
18276 
18277   attr_name = c_parser_attribute_any_word (parser);
18278   if (attr_name)
18279     {
18280       c_parser_consume_token (parser);
18281       attr = build_tree_list (attr_name, NULL_TREE);
18282     }
18283   else
18284     c_parser_error (parser, "expected identifier");
18285 
18286   c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, "expected %<]%>");
18287  error1:
18288   c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, "expected %<]%>");
18289   return attr;
18290 }
18291 
18292 /* Parse a __transaction_atomic or __transaction_relaxed statement
18293    (GCC Extension).
18294 
18295    transaction-statement:
18296      __transaction_atomic transaction-attribute[opt] compound-statement
18297      __transaction_relaxed compound-statement
18298 
18299    Note that the only valid attribute is: "outer".
18300 */
18301 
18302 static tree
c_parser_transaction(c_parser * parser,enum rid keyword)18303 c_parser_transaction (c_parser *parser, enum rid keyword)
18304 {
18305   unsigned int old_in = parser->in_transaction;
18306   unsigned int this_in = 1, new_in;
18307   location_t loc = c_parser_peek_token (parser)->location;
18308   tree stmt, attrs;
18309 
18310   gcc_assert ((keyword == RID_TRANSACTION_ATOMIC
18311       || keyword == RID_TRANSACTION_RELAXED)
18312       && c_parser_next_token_is_keyword (parser, keyword));
18313   c_parser_consume_token (parser);
18314 
18315   if (keyword == RID_TRANSACTION_RELAXED)
18316     this_in |= TM_STMT_ATTR_RELAXED;
18317   else
18318     {
18319       attrs = c_parser_transaction_attributes (parser);
18320       if (attrs)
18321 	this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
18322     }
18323 
18324   /* Keep track if we're in the lexical scope of an outer transaction.  */
18325   new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
18326 
18327   parser->in_transaction = new_in;
18328   stmt = c_parser_compound_statement (parser);
18329   parser->in_transaction = old_in;
18330 
18331   if (flag_tm)
18332     stmt = c_finish_transaction (loc, stmt, this_in);
18333   else
18334     error_at (loc, (keyword == RID_TRANSACTION_ATOMIC ?
18335 	"%<__transaction_atomic%> without transactional memory support enabled"
18336 	: "%<__transaction_relaxed %> "
18337 	"without transactional memory support enabled"));
18338 
18339   return stmt;
18340 }
18341 
18342 /* Parse a __transaction_atomic or __transaction_relaxed expression
18343    (GCC Extension).
18344 
18345    transaction-expression:
18346      __transaction_atomic ( expression )
18347      __transaction_relaxed ( expression )
18348 */
18349 
18350 static struct c_expr
c_parser_transaction_expression(c_parser * parser,enum rid keyword)18351 c_parser_transaction_expression (c_parser *parser, enum rid keyword)
18352 {
18353   struct c_expr ret;
18354   unsigned int old_in = parser->in_transaction;
18355   unsigned int this_in = 1;
18356   location_t loc = c_parser_peek_token (parser)->location;
18357   tree attrs;
18358 
18359   gcc_assert ((keyword == RID_TRANSACTION_ATOMIC
18360       || keyword == RID_TRANSACTION_RELAXED)
18361       && c_parser_next_token_is_keyword (parser, keyword));
18362   c_parser_consume_token (parser);
18363 
18364   if (keyword == RID_TRANSACTION_RELAXED)
18365     this_in |= TM_STMT_ATTR_RELAXED;
18366   else
18367     {
18368       attrs = c_parser_transaction_attributes (parser);
18369       if (attrs)
18370 	this_in |= parse_tm_stmt_attr (attrs, 0);
18371     }
18372 
18373   parser->in_transaction = this_in;
18374   matching_parens parens;
18375   if (parens.require_open (parser))
18376     {
18377       tree expr = c_parser_expression (parser).value;
18378       ret.original_type = TREE_TYPE (expr);
18379       ret.value = build1 (TRANSACTION_EXPR, ret.original_type, expr);
18380       if (this_in & TM_STMT_ATTR_RELAXED)
18381 	TRANSACTION_EXPR_RELAXED (ret.value) = 1;
18382       SET_EXPR_LOCATION (ret.value, loc);
18383       ret.original_code = TRANSACTION_EXPR;
18384       if (!parens.require_close (parser))
18385 	{
18386 	  c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
18387 	  goto error;
18388 	}
18389     }
18390   else
18391     {
18392      error:
18393       ret.set_error ();
18394       ret.original_code = ERROR_MARK;
18395       ret.original_type = NULL;
18396     }
18397   parser->in_transaction = old_in;
18398 
18399   if (!flag_tm)
18400     error_at (loc, (keyword == RID_TRANSACTION_ATOMIC ?
18401 	"%<__transaction_atomic%> without transactional memory support enabled"
18402 	: "%<__transaction_relaxed %> "
18403 	"without transactional memory support enabled"));
18404 
18405   set_c_expr_source_range (&ret, loc, loc);
18406 
18407   return ret;
18408 }
18409 
18410 /* Parse a __transaction_cancel statement (GCC Extension).
18411 
18412    transaction-cancel-statement:
18413      __transaction_cancel transaction-attribute[opt] ;
18414 
18415    Note that the only valid attribute is "outer".
18416 */
18417 
18418 static tree
c_parser_transaction_cancel(c_parser * parser)18419 c_parser_transaction_cancel (c_parser *parser)
18420 {
18421   location_t loc = c_parser_peek_token (parser)->location;
18422   tree attrs;
18423   bool is_outer = false;
18424 
18425   gcc_assert (c_parser_next_token_is_keyword (parser, RID_TRANSACTION_CANCEL));
18426   c_parser_consume_token (parser);
18427 
18428   attrs = c_parser_transaction_attributes (parser);
18429   if (attrs)
18430     is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
18431 
18432   if (!flag_tm)
18433     {
18434       error_at (loc, "%<__transaction_cancel%> without "
18435 		"transactional memory support enabled");
18436       goto ret_error;
18437     }
18438   else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
18439     {
18440       error_at (loc, "%<__transaction_cancel%> within a "
18441 		"%<__transaction_relaxed%>");
18442       goto ret_error;
18443     }
18444   else if (is_outer)
18445     {
18446       if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
18447 	  && !is_tm_may_cancel_outer (current_function_decl))
18448 	{
18449 	  error_at (loc, "outer %<__transaction_cancel%> not "
18450 		    "within outer %<__transaction_atomic%>");
18451 	  error_at (loc, "  or a %<transaction_may_cancel_outer%> function");
18452 	  goto ret_error;
18453 	}
18454     }
18455   else if (parser->in_transaction == 0)
18456     {
18457       error_at (loc, "%<__transaction_cancel%> not within "
18458 		"%<__transaction_atomic%>");
18459       goto ret_error;
18460     }
18461 
18462   return add_stmt (build_tm_abort_call (loc, is_outer));
18463 
18464  ret_error:
18465   return build1 (NOP_EXPR, void_type_node, error_mark_node);
18466 }
18467 
18468 /* Parse a single source file.  */
18469 
18470 void
c_parse_file(void)18471 c_parse_file (void)
18472 {
18473   /* Use local storage to begin.  If the first token is a pragma, parse it.
18474      If it is #pragma GCC pch_preprocess, then this will load a PCH file
18475      which will cause garbage collection.  */
18476   c_parser tparser;
18477 
18478   memset (&tparser, 0, sizeof tparser);
18479   tparser.tokens = &tparser.tokens_buf[0];
18480   the_parser = &tparser;
18481 
18482   if (c_parser_peek_token (&tparser)->pragma_kind == PRAGMA_GCC_PCH_PREPROCESS)
18483     c_parser_pragma_pch_preprocess (&tparser);
18484   else
18485     c_common_no_more_pch ();
18486 
18487   the_parser = ggc_alloc<c_parser> ();
18488   *the_parser = tparser;
18489   if (tparser.tokens == &tparser.tokens_buf[0])
18490     the_parser->tokens = &the_parser->tokens_buf[0];
18491 
18492   /* Initialize EH, if we've been told to do so.  */
18493   if (flag_exceptions)
18494     using_eh_for_cleanups ();
18495 
18496   c_parser_translation_unit (the_parser);
18497   the_parser = NULL;
18498 }
18499 
18500 /* Parse the body of a function declaration marked with "__RTL".
18501 
18502    The RTL parser works on the level of characters read from a
18503    FILE *, whereas c_parser works at the level of tokens.
18504    Square this circle by consuming all of the tokens up to and
18505    including the closing brace, recording the start/end of the RTL
18506    fragment, and reopening the file and re-reading the relevant
18507    lines within the RTL parser.
18508 
18509    This requires the opening and closing braces of the C function
18510    to be on separate lines from the RTL they wrap.
18511 
18512    Take ownership of START_WITH_PASS, if non-NULL.  */
18513 
18514 void
c_parser_parse_rtl_body(c_parser * parser,char * start_with_pass)18515 c_parser_parse_rtl_body (c_parser *parser, char *start_with_pass)
18516 {
18517   if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
18518     {
18519       free (start_with_pass);
18520       return;
18521     }
18522 
18523   location_t start_loc = c_parser_peek_token (parser)->location;
18524 
18525   /* Consume all tokens, up to the closing brace, handling
18526      matching pairs of braces in the rtl dump.  */
18527   int num_open_braces = 1;
18528   while (1)
18529     {
18530       switch (c_parser_peek_token (parser)->type)
18531 	{
18532 	case CPP_OPEN_BRACE:
18533 	  num_open_braces++;
18534 	  break;
18535 	case CPP_CLOSE_BRACE:
18536 	  if (--num_open_braces == 0)
18537 	    goto found_closing_brace;
18538 	  break;
18539 	case CPP_EOF:
18540 	  error_at (start_loc, "no closing brace");
18541 	  free (start_with_pass);
18542 	  return;
18543 	default:
18544 	  break;
18545 	}
18546       c_parser_consume_token (parser);
18547     }
18548 
18549  found_closing_brace:
18550   /* At the closing brace; record its location.  */
18551   location_t end_loc = c_parser_peek_token (parser)->location;
18552 
18553   /* Consume the closing brace.  */
18554   c_parser_consume_token (parser);
18555 
18556   /* Invoke the RTL parser.  */
18557   if (!read_rtl_function_body_from_file_range (start_loc, end_loc))
18558     {
18559       free (start_with_pass);
18560       return;
18561     }
18562 
18563  /*  If a pass name was provided for START_WITH_PASS, run the backend
18564      accordingly now, on the cfun created above, transferring
18565      ownership of START_WITH_PASS.  */
18566   if (start_with_pass)
18567     run_rtl_passes (start_with_pass);
18568 }
18569 
18570 #include "gt-c-c-parser.h"
18571