Line data Source code
1 : /* Check functions
2 : Copyright (C) 2002-2026 Free Software Foundation, Inc.
3 : Contributed by Andy Vaught & Katherine Holcomb
4 :
5 : This file is part of GCC.
6 :
7 : GCC is free software; you can redistribute it and/or modify it under
8 : the terms of the GNU General Public License as published by the Free
9 : Software Foundation; either version 3, or (at your option) any later
10 : version.
11 :
12 : GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 : WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 : FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 : for more details.
16 :
17 : You should have received a copy of the GNU General Public License
18 : along with GCC; see the file COPYING3. If not see
19 : <http://www.gnu.org/licenses/>. */
20 :
21 :
22 : /* These functions check to see if an argument list is compatible with
23 : a particular intrinsic function or subroutine. Presence of
24 : required arguments has already been established, the argument list
25 : has been sorted into the right order and has NULL arguments in the
26 : correct places for missing optional arguments. */
27 :
28 : #include "config.h"
29 : #include "system.h"
30 : #include "coretypes.h"
31 : #include "options.h"
32 : #include "gfortran.h"
33 : #include "intrinsic.h"
34 : #include "constructor.h"
35 : #include "target-memory.h"
36 :
37 :
38 : /* Reset a BOZ to a zero value. This is used to prevent run-on errors
39 : from resolve.cc(resolve_function). */
40 :
41 : static void
42 39 : reset_boz (gfc_expr *x)
43 : {
44 : /* Clear boz info. */
45 39 : x->boz.rdx = 0;
46 39 : x->boz.len = 0;
47 39 : free (x->boz.str);
48 :
49 39 : x->ts.type = BT_INTEGER;
50 39 : x->ts.kind = gfc_default_integer_kind;
51 39 : mpz_init (x->value.integer);
52 39 : mpz_set_ui (x->value.integer, 0);
53 39 : }
54 :
55 : /* A BOZ literal constant can appear in a limited number of contexts.
56 : gfc_invalid_boz() is a helper function to simplify error/warning
57 : generation. gfortran accepts the nonstandard 'X' for 'Z', and gfortran
58 : allows the BOZ indicator to appear as a suffix. If -fallow-invalid-boz
59 : is used, then issue a warning; otherwise issue an error. */
60 :
61 : bool
62 217 : gfc_invalid_boz (const char *msg, locus *loc)
63 : {
64 217 : if (flag_allow_invalid_boz)
65 : {
66 204 : gfc_warning (0, msg, loc);
67 204 : return false;
68 : }
69 :
70 13 : const char *hint = _(" [see %<-fallow-invalid-boz%>]");
71 13 : size_t len = strlen (msg) + strlen (hint) + 1;
72 13 : char *msg2 = (char *) alloca (len);
73 13 : strcpy (msg2, msg);
74 13 : strcat (msg2, hint);
75 13 : gfc_error (msg2, loc);
76 13 : return true;
77 : }
78 :
79 :
80 : /* Issue an error for an illegal BOZ argument. */
81 :
82 : static bool
83 1878 : illegal_boz_arg (gfc_expr *x)
84 : {
85 1878 : if (x->ts.type == BT_BOZ)
86 : {
87 4 : gfc_error ("BOZ literal constant at %L cannot be an actual argument "
88 : "to %qs", &x->where, gfc_current_intrinsic);
89 4 : reset_boz (x);
90 4 : return true;
91 : }
92 :
93 : return false;
94 : }
95 :
96 : /* Some procedures take two arguments such that both cannot be BOZ. */
97 :
98 : static bool
99 7016 : boz_args_check(gfc_expr *i, gfc_expr *j)
100 : {
101 7016 : if (i->ts.type == BT_BOZ && j->ts.type == BT_BOZ)
102 : {
103 14 : gfc_error ("Arguments of %qs at %L and %L cannot both be BOZ "
104 : "literal constants", gfc_current_intrinsic, &i->where,
105 : &j->where);
106 14 : reset_boz (i);
107 14 : reset_boz (j);
108 14 : return false;
109 :
110 : }
111 :
112 : return true;
113 : }
114 :
115 :
116 : /* Check that a BOZ is a constant. */
117 :
118 : static bool
119 2686 : is_boz_constant (gfc_expr *a)
120 : {
121 0 : if (a->expr_type != EXPR_CONSTANT)
122 : {
123 0 : gfc_error ("Invalid use of BOZ literal constant at %L", &a->where);
124 0 : return false;
125 : }
126 :
127 : return true;
128 : }
129 :
130 :
131 : /* Convert a octal string into a binary string. This is used in the
132 : fallback conversion of an octal string to a REAL. */
133 :
134 : static char *
135 0 : oct2bin(int nbits, char *oct)
136 : {
137 0 : const char bits[8][5] = {
138 : "000", "001", "010", "011", "100", "101", "110", "111"};
139 :
140 0 : char *buf, *bufp;
141 0 : int i, j, n;
142 :
143 0 : j = nbits + 1;
144 0 : if (nbits == 64) j++;
145 :
146 0 : bufp = buf = XCNEWVEC (char, j + 1);
147 0 : memset (bufp, 0, j + 1);
148 :
149 0 : n = strlen (oct);
150 0 : for (i = 0; i < n; i++, oct++)
151 : {
152 0 : j = *oct - 48;
153 0 : strcpy (bufp, &bits[j][0]);
154 0 : bufp += 3;
155 : }
156 :
157 0 : bufp = XCNEWVEC (char, nbits + 1);
158 0 : if (nbits == 64)
159 0 : strcpy (bufp, buf + 2);
160 : else
161 0 : strcpy (bufp, buf + 1);
162 :
163 0 : free (buf);
164 :
165 0 : return bufp;
166 : }
167 :
168 :
169 : /* Convert a hexadecimal string into a binary string. This is used in the
170 : fallback conversion of a hexadecimal string to a REAL. */
171 :
172 : static char *
173 0 : hex2bin(int nbits, char *hex)
174 : {
175 0 : const char bits[16][5] = {
176 : "0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111",
177 : "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
178 :
179 0 : char *buf, *bufp;
180 0 : int i, j, n;
181 :
182 0 : bufp = buf = XCNEWVEC (char, nbits + 1);
183 0 : memset (bufp, 0, nbits + 1);
184 :
185 0 : n = strlen (hex);
186 0 : for (i = 0; i < n; i++, hex++)
187 : {
188 0 : j = *hex;
189 0 : if (j > 47 && j < 58)
190 0 : j -= 48;
191 0 : else if (j > 64 && j < 71)
192 0 : j -= 55;
193 0 : else if (j > 96 && j < 103)
194 0 : j -= 87;
195 : else
196 0 : gcc_unreachable ();
197 :
198 0 : strcpy (bufp, &bits[j][0]);
199 0 : bufp += 4;
200 : }
201 :
202 0 : return buf;
203 : }
204 :
205 :
206 : /* Fallback conversion of a BOZ string to REAL. */
207 :
208 : static void
209 0 : bin2real (gfc_expr *x, int kind)
210 : {
211 0 : char buf[114], *sp;
212 0 : int b, i, ie, t, w;
213 0 : bool sgn;
214 0 : mpz_t em;
215 :
216 0 : i = gfc_validate_kind (BT_REAL, kind, false);
217 0 : t = gfc_real_kinds[i].digits - 1;
218 :
219 : /* Number of bits in the exponent. */
220 0 : if (gfc_real_kinds[i].max_exponent == 16384)
221 : w = 15;
222 0 : else if (gfc_real_kinds[i].max_exponent == 1024)
223 : w = 11;
224 : else
225 0 : w = 8;
226 :
227 0 : if (x->boz.rdx == 16)
228 0 : sp = hex2bin (gfc_real_kinds[i].mode_precision, x->boz.str);
229 0 : else if (x->boz.rdx == 8)
230 0 : sp = oct2bin (gfc_real_kinds[i].mode_precision, x->boz.str);
231 : else
232 0 : sp = x->boz.str;
233 :
234 : /* Extract sign bit. */
235 0 : sgn = *sp != '0';
236 :
237 : /* Extract biased exponent. */
238 0 : memset (buf, 0, 114);
239 0 : strncpy (buf, ++sp, w);
240 0 : mpz_init (em);
241 0 : mpz_set_str (em, buf, 2);
242 0 : ie = mpz_get_si (em);
243 :
244 0 : mpfr_init2 (x->value.real, t + 1);
245 0 : x->ts.type = BT_REAL;
246 0 : x->ts.kind = kind;
247 :
248 0 : sp += w; /* Set to first digit in significand. */
249 0 : b = (1 << w) - 1;
250 0 : if ((i == 0 && ie == b) || (i == 1 && ie == b)
251 0 : || ((i == 2 || i == 3) && ie == b))
252 : {
253 0 : bool zeros = true;
254 0 : if (i == 2) sp++;
255 0 : for (; *sp; sp++)
256 : {
257 0 : if (*sp != '0')
258 : {
259 : zeros = false;
260 : break;
261 : }
262 : }
263 :
264 0 : if (zeros)
265 0 : mpfr_set_inf (x->value.real, 1);
266 : else
267 0 : mpfr_set_nan (x->value.real);
268 : }
269 : else
270 : {
271 0 : if (i == 2)
272 0 : strncpy (buf, sp, t + 1);
273 : else
274 : {
275 : /* Significand with hidden bit. */
276 0 : buf[0] = '1';
277 0 : strncpy (&buf[1], sp, t);
278 : }
279 :
280 : /* Convert to significand to integer. */
281 0 : mpz_set_str (em, buf, 2);
282 0 : ie -= ((1 << (w - 1)) - 1); /* Unbiased exponent. */
283 0 : mpfr_set_z_2exp (x->value.real, em, ie - t, GFC_RND_MODE);
284 : }
285 :
286 0 : if (sgn) mpfr_neg (x->value.real, x->value.real, GFC_RND_MODE);
287 :
288 0 : mpz_clear (em);
289 0 : }
290 :
291 :
292 : /* Fortran 2018 treats a BOZ as simply a string of bits. gfc_boz2real ()
293 : converts the string into a REAL of the appropriate kind. The treatment
294 : of the sign bit is processor dependent. */
295 :
296 : bool
297 254 : gfc_boz2real (gfc_expr *x, int kind)
298 : {
299 254 : extern int gfc_max_integer_kind;
300 254 : gfc_typespec ts;
301 254 : int len;
302 254 : char *buf, *str;
303 :
304 254 : if (!is_boz_constant (x))
305 0 : return false;
306 :
307 : /* Determine the length of the required string. */
308 254 : len = 8 * kind;
309 254 : if (x->boz.rdx == 16) len /= 4;
310 254 : if (x->boz.rdx == 8) len = len / 3 + 1;
311 254 : buf = (char *) alloca (len + 1); /* +1 for NULL terminator. */
312 :
313 254 : if (x->boz.len >= len) /* Truncate if necessary. */
314 : {
315 172 : str = x->boz.str + (x->boz.len - len);
316 172 : strcpy(buf, str);
317 : }
318 : else /* Copy and pad. */
319 : {
320 82 : memset (buf, 48, len);
321 82 : str = buf + (len - x->boz.len);
322 82 : strcpy (str, x->boz.str);
323 : }
324 :
325 : /* Need to adjust leading bits in an octal string. */
326 254 : if (x->boz.rdx == 8)
327 : {
328 : /* Clear first bit. */
329 54 : if (kind == 4 || kind == 10 || kind == 16)
330 : {
331 36 : if (buf[0] == '4')
332 0 : buf[0] = '0';
333 36 : else if (buf[0] == '5')
334 0 : buf[0] = '1';
335 36 : else if (buf[0] == '6')
336 0 : buf[0] = '2';
337 36 : else if (buf[0] == '7')
338 0 : buf[0] = '3';
339 : }
340 : /* Clear first two bits. */
341 : else
342 : {
343 18 : if (buf[0] == '2' || buf[0] == '4' || buf[0] == '6')
344 0 : buf[0] = '0';
345 : else if (buf[0] == '3' || buf[0] == '5' || buf[0] == '7')
346 0 : buf[0] = '1';
347 : }
348 : }
349 :
350 : /* Reset BOZ string to the truncated or padded version. */
351 254 : free (x->boz.str);
352 254 : x->boz.len = len;
353 254 : x->boz.str = XCNEWVEC (char, len + 1);
354 254 : strncpy (x->boz.str, buf, len);
355 :
356 : /* For some targets, the largest INTEGER in terms of bits is smaller than
357 : the bits needed to hold the REAL. Fortunately, the kind type parameter
358 : indicates the number of bytes required to an INTEGER and a REAL. */
359 254 : if (gfc_max_integer_kind < kind)
360 : {
361 0 : bin2real (x, kind);
362 : }
363 : else
364 : {
365 : /* Convert to widest possible integer. */
366 254 : gfc_boz2int (x, gfc_max_integer_kind);
367 254 : ts.type = BT_REAL;
368 254 : ts.kind = kind;
369 254 : if (!gfc_convert_boz (x, &ts))
370 : {
371 0 : gfc_error ("Failure in conversion of BOZ to REAL at %L", &x->where);
372 0 : return false;
373 : }
374 : }
375 :
376 : return true;
377 : }
378 :
379 :
380 : /* Fortran 2018 treats a BOZ as simply a string of bits. gfc_boz2int ()
381 : converts the string into an INTEGER of the appropriate kind. The
382 : treatment of the sign bit is processor dependent. If the converted
383 : value exceeds the range of the type, then wrap-around semantics are
384 : applied. */
385 :
386 : bool
387 2426 : gfc_boz2int (gfc_expr *x, int kind)
388 : {
389 2426 : int i, len;
390 2426 : char *buf, *str;
391 2426 : mpz_t tmp1;
392 :
393 2426 : if (!is_boz_constant (x))
394 0 : return false;
395 :
396 2426 : i = gfc_validate_kind (BT_INTEGER, kind, false);
397 2426 : len = gfc_integer_kinds[i].bit_size;
398 2426 : if (x->boz.rdx == 16) len /= 4;
399 2426 : if (x->boz.rdx == 8) len = len / 3 + 1;
400 2426 : buf = (char *) alloca (len + 1); /* +1 for NULL terminator. */
401 :
402 2426 : if (x->boz.len >= len) /* Truncate if necessary. */
403 : {
404 814 : str = x->boz.str + (x->boz.len - len);
405 814 : strcpy(buf, str);
406 : }
407 : else /* Copy and pad. */
408 : {
409 1612 : memset (buf, 48, len);
410 1612 : str = buf + (len - x->boz.len);
411 1612 : strcpy (str, x->boz.str);
412 : }
413 :
414 : /* Need to adjust leading bits in an octal string. */
415 2426 : if (x->boz.rdx == 8)
416 : {
417 : /* Clear first bit. */
418 358 : if (kind == 1 || kind == 4 || kind == 16)
419 : {
420 192 : if (buf[0] == '4')
421 0 : buf[0] = '0';
422 192 : else if (buf[0] == '5')
423 0 : buf[0] = '1';
424 192 : else if (buf[0] == '6')
425 1 : buf[0] = '2';
426 191 : else if (buf[0] == '7')
427 0 : buf[0] = '3';
428 : }
429 : /* Clear first two bits. */
430 : else
431 : {
432 166 : if (buf[0] == '2' || buf[0] == '4' || buf[0] == '6')
433 66 : buf[0] = '0';
434 : else if (buf[0] == '3' || buf[0] == '5' || buf[0] == '7')
435 37 : buf[0] = '1';
436 : }
437 : }
438 :
439 : /* Convert as-if unsigned integer. */
440 2426 : mpz_init (tmp1);
441 2426 : mpz_set_str (tmp1, buf, x->boz.rdx);
442 :
443 : /* Check for wrap-around. */
444 2426 : if (mpz_cmp (tmp1, gfc_integer_kinds[i].huge) > 0)
445 : {
446 103 : mpz_t tmp2;
447 103 : mpz_init (tmp2);
448 103 : mpz_add_ui (tmp2, gfc_integer_kinds[i].huge, 1);
449 103 : mpz_mod (tmp1, tmp1, tmp2);
450 103 : mpz_sub (tmp1, tmp1, tmp2);
451 103 : mpz_clear (tmp2);
452 : }
453 :
454 : /* Clear boz info. */
455 2426 : x->boz.rdx = 0;
456 2426 : x->boz.len = 0;
457 2426 : free (x->boz.str);
458 :
459 2426 : mpz_init (x->value.integer);
460 2426 : mpz_set (x->value.integer, tmp1);
461 2426 : x->ts.type = BT_INTEGER;
462 2426 : x->ts.kind = kind;
463 2426 : mpz_clear (tmp1);
464 :
465 2426 : return true;
466 : }
467 :
468 : /* Same as above for UNSIGNED, but much simpler because
469 : of wraparound. */
470 : bool
471 6 : gfc_boz2uint (gfc_expr *x, int kind)
472 : {
473 6 : int k;
474 6 : if (!is_boz_constant (x))
475 0 : return false;
476 :
477 6 : mpz_init (x->value.integer);
478 6 : mpz_set_str (x->value.integer, x->boz.str, x->boz.rdx);
479 6 : k = gfc_validate_kind (BT_UNSIGNED, kind, false);
480 6 : if (mpz_cmp (x->value.integer, gfc_unsigned_kinds[k].huge) > 0)
481 : {
482 0 : gfc_warning (0, _("BOZ constant truncated at %L"), &x->where);
483 0 : mpz_and (x->value.integer, x->value.integer, gfc_unsigned_kinds[k].huge);
484 : }
485 :
486 6 : x->ts.type = BT_UNSIGNED;
487 6 : x->ts.kind = kind;
488 :
489 : /* Clear boz info. */
490 6 : x->boz.rdx = 0;
491 6 : x->boz.len = 0;
492 6 : free (x->boz.str);
493 :
494 6 : return true;
495 : }
496 : /* Make sure an expression is a scalar. */
497 :
498 : static bool
499 65950 : scalar_check (gfc_expr *e, int n)
500 : {
501 65950 : if (e->rank == 0)
502 : return true;
503 :
504 40 : gfc_error ("%qs argument of %qs intrinsic at %L must be a scalar",
505 40 : gfc_current_intrinsic_arg[n]->name, gfc_current_intrinsic,
506 : &e->where);
507 :
508 40 : return false;
509 : }
510 :
511 :
512 : /* Check the type of an expression. */
513 :
514 : static bool
515 196562 : type_check (gfc_expr *e, int n, bt type)
516 : {
517 196562 : if (e->ts.type == type)
518 : return true;
519 :
520 3660 : gfc_error ("%qs argument of %qs intrinsic at %L must be %s",
521 3660 : gfc_current_intrinsic_arg[n]->name, gfc_current_intrinsic,
522 : &e->where, gfc_basic_typename (type));
523 :
524 3660 : return false;
525 : }
526 :
527 : /* Check the type of an expression which can be one of two. */
528 :
529 : static bool
530 2826 : type_check2 (gfc_expr *e, int n, bt type1, bt type2)
531 : {
532 2826 : if (e->ts.type == type1 || e->ts.type == type2)
533 : return true;
534 :
535 0 : gfc_error ("%qs argument of %qs intrinsic at %L must be %s or %s",
536 0 : gfc_current_intrinsic_arg[n]->name, gfc_current_intrinsic,
537 : &e->where, gfc_basic_typename (type1), gfc_basic_typename (type2));
538 :
539 0 : return false;
540 : }
541 :
542 : /* Check that the expression is a numeric type. */
543 :
544 : static bool
545 19522 : numeric_check (gfc_expr *e, int n)
546 : {
547 : /* Users sometime use a subroutine designator as an actual argument to
548 : an intrinsic subprogram that expects an argument with a numeric type. */
549 19522 : if (e->symtree && e->symtree->n.sym->attr.subroutine)
550 1 : goto error;
551 :
552 19521 : if (gfc_numeric_ts (&e->ts))
553 : return true;
554 :
555 : /* If the expression has not got a type, check if its namespace can
556 : offer a default type. */
557 1 : if ((e->expr_type == EXPR_VARIABLE || e->expr_type == EXPR_FUNCTION)
558 2 : && e->symtree->n.sym->ts.type == BT_UNKNOWN
559 0 : && gfc_set_default_type (e->symtree->n.sym, 0, e->symtree->n.sym->ns)
560 3 : && gfc_numeric_ts (&e->symtree->n.sym->ts))
561 : {
562 0 : e->ts = e->symtree->n.sym->ts;
563 0 : return true;
564 : }
565 :
566 4 : error:
567 :
568 4 : gfc_error ("%qs argument of %qs intrinsic at %L must have a numeric type",
569 4 : gfc_current_intrinsic_arg[n]->name, gfc_current_intrinsic,
570 : &e->where);
571 :
572 4 : return false;
573 : }
574 :
575 :
576 : /* Check that an expression is integer or real. */
577 :
578 : static bool
579 8496 : int_or_real_check (gfc_expr *e, int n)
580 : {
581 8496 : if (e->ts.type != BT_INTEGER && e->ts.type != BT_REAL)
582 : {
583 2 : gfc_error ("%qs argument of %qs intrinsic at %L must be INTEGER "
584 2 : "or REAL", gfc_current_intrinsic_arg[n]->name,
585 : gfc_current_intrinsic, &e->where);
586 2 : return false;
587 : }
588 :
589 : return true;
590 : }
591 :
592 : /* Check that an expression is integer or real... or unsigned. */
593 :
594 : static bool
595 1562 : int_or_real_or_unsigned_check (gfc_expr *e, int n)
596 : {
597 1562 : if (e->ts.type != BT_INTEGER && e->ts.type != BT_REAL
598 : && e->ts.type != BT_UNSIGNED)
599 : {
600 0 : gfc_error ("%qs argument of %qs intrinsic at %L must be INTEGER, "
601 0 : "REAL or UNSIGNED", gfc_current_intrinsic_arg[n]->name,
602 : gfc_current_intrinsic, &e->where);
603 0 : return false;
604 : }
605 :
606 : return true;
607 : }
608 :
609 : /* Check that an expression is integer or real; allow character for
610 : F2003 or later. */
611 :
612 : static bool
613 18193 : int_or_real_or_char_check_f2003 (gfc_expr *e, int n)
614 : {
615 18193 : if (e->ts.type != BT_INTEGER && e->ts.type != BT_REAL)
616 : {
617 1549 : if (e->ts.type == BT_CHARACTER)
618 1549 : return gfc_notify_std (GFC_STD_F2003, "Fortran 2003: Character for "
619 : "%qs argument of %qs intrinsic at %L",
620 1549 : gfc_current_intrinsic_arg[n]->name,
621 1549 : gfc_current_intrinsic, &e->where);
622 : else
623 : {
624 0 : if (gfc_option.allow_std & GFC_STD_F2003)
625 0 : gfc_error ("%qs argument of %qs intrinsic at %L must be INTEGER "
626 : "or REAL or CHARACTER",
627 0 : gfc_current_intrinsic_arg[n]->name,
628 : gfc_current_intrinsic, &e->where);
629 : else
630 0 : gfc_error ("%qs argument of %qs intrinsic at %L must be INTEGER "
631 0 : "or REAL", gfc_current_intrinsic_arg[n]->name,
632 : gfc_current_intrinsic, &e->where);
633 : }
634 : return false;
635 : }
636 :
637 : return true;
638 : }
639 :
640 : /* Check that an expression is integer or real or unsigned; allow character for
641 : F2003 or later. */
642 :
643 : static bool
644 234 : int_or_real_or_char_or_unsigned_check_f2003 (gfc_expr *e, int n)
645 : {
646 234 : if (e->ts.type != BT_INTEGER && e->ts.type != BT_REAL
647 186 : && e->ts.type != BT_UNSIGNED)
648 : {
649 0 : if (e->ts.type == BT_CHARACTER)
650 0 : return gfc_notify_std (GFC_STD_F2003, "Fortran 2003: Character for "
651 : "%qs argument of %qs intrinsic at %L",
652 0 : gfc_current_intrinsic_arg[n]->name,
653 0 : gfc_current_intrinsic, &e->where);
654 : else
655 : {
656 0 : if (gfc_option.allow_std & GFC_STD_F2003)
657 0 : gfc_error ("%qs argument of %qs intrinsic at %L must be INTEGER "
658 : "or REAL or CHARACTER or UNSIGNED",
659 0 : gfc_current_intrinsic_arg[n]->name,
660 : gfc_current_intrinsic, &e->where);
661 : else
662 0 : gfc_error ("%qs argument of %qs intrinsic at %L must be INTEGER "
663 : "or REAL or UNSIGNED",
664 0 : gfc_current_intrinsic_arg[n]->name,
665 : gfc_current_intrinsic, &e->where);
666 : }
667 : return false;
668 : }
669 :
670 : return true;
671 : }
672 :
673 : /* Check that an expression is an intrinsic type. */
674 : static bool
675 1814 : intrinsic_type_check (gfc_expr *e, int n)
676 : {
677 1814 : if (e->ts.type != BT_INTEGER && e->ts.type != BT_REAL
678 : && e->ts.type != BT_COMPLEX && e->ts.type != BT_CHARACTER
679 : && e->ts.type != BT_LOGICAL && e->ts.type != BT_UNSIGNED)
680 : {
681 1 : gfc_error ("%qs argument of %qs intrinsic at %L must be of intrinsic type",
682 1 : gfc_current_intrinsic_arg[n]->name,
683 : gfc_current_intrinsic, &e->where);
684 1 : return false;
685 : }
686 : return true;
687 : }
688 :
689 : /* Check that an expression is real or complex. */
690 :
691 : static bool
692 3032 : real_or_complex_check (gfc_expr *e, int n)
693 : {
694 3032 : if (e->ts.type != BT_REAL && e->ts.type != BT_COMPLEX)
695 : {
696 0 : gfc_error ("%qs argument of %qs intrinsic at %L must be REAL "
697 0 : "or COMPLEX", gfc_current_intrinsic_arg[n]->name,
698 : gfc_current_intrinsic, &e->where);
699 0 : return false;
700 : }
701 :
702 : return true;
703 : }
704 :
705 :
706 : /* Check that an expression is INTEGER or PROCEDURE. */
707 :
708 : static bool
709 1 : int_or_proc_check (gfc_expr *e, int n)
710 : {
711 1 : if (e->ts.type != BT_INTEGER && e->ts.type != BT_PROCEDURE)
712 : {
713 0 : gfc_error ("%qs argument of %qs intrinsic at %L must be INTEGER "
714 0 : "or PROCEDURE", gfc_current_intrinsic_arg[n]->name,
715 : gfc_current_intrinsic, &e->where);
716 0 : return false;
717 : }
718 :
719 : return true;
720 : }
721 :
722 :
723 : /* Check that the expression is an optional constant integer
724 : and that it specifies a valid kind for that type. */
725 :
726 : static bool
727 90021 : kind_check (gfc_expr *k, int n, bt type)
728 : {
729 90021 : int kind;
730 :
731 90021 : if (k == NULL)
732 : return true;
733 :
734 9801 : if (!type_check (k, n, BT_INTEGER))
735 : return false;
736 :
737 9801 : if (!scalar_check (k, n))
738 : return false;
739 :
740 9799 : if (!gfc_check_init_expr (k))
741 : {
742 0 : gfc_error ("%qs argument of %qs intrinsic at %L must be a constant",
743 0 : gfc_current_intrinsic_arg[n]->name, gfc_current_intrinsic,
744 : &k->where);
745 0 : return false;
746 : }
747 :
748 9799 : if (gfc_extract_int (k, &kind)
749 9799 : || gfc_validate_kind (type, kind, true) < 0)
750 : {
751 1 : gfc_error ("Invalid kind for %s at %L", gfc_basic_typename (type),
752 : &k->where);
753 1 : return false;
754 : }
755 :
756 : return true;
757 : }
758 :
759 :
760 : /* Make sure the expression is a double precision real. */
761 :
762 : static bool
763 15886 : double_check (gfc_expr *d, int n)
764 : {
765 15886 : if (!type_check (d, n, BT_REAL))
766 : return false;
767 :
768 12332 : if (d->ts.kind != gfc_default_double_kind)
769 : {
770 7238 : gfc_error ("%qs argument of %qs intrinsic at %L must be double "
771 7238 : "precision", gfc_current_intrinsic_arg[n]->name,
772 : gfc_current_intrinsic, &d->where);
773 7238 : return false;
774 : }
775 :
776 : return true;
777 : }
778 :
779 :
780 : static bool
781 1834 : coarray_check (gfc_expr *e, int n)
782 : {
783 162 : if (e->ts.type == BT_CLASS && gfc_expr_attr (e).class_ok
784 159 : && CLASS_DATA (e)->attr.codimension
785 1993 : && CLASS_DATA (e)->as->corank)
786 : {
787 159 : gfc_add_class_array_ref (e);
788 159 : return true;
789 : }
790 :
791 1675 : if (!gfc_is_coarray (e))
792 : {
793 24 : gfc_error ("Expected coarray variable as %qs argument to the %s "
794 24 : "intrinsic at %L", gfc_current_intrinsic_arg[n]->name,
795 : gfc_current_intrinsic, &e->where);
796 24 : return false;
797 : }
798 :
799 : return true;
800 : }
801 :
802 :
803 : /* Make sure the expression is a logical array. */
804 :
805 : static bool
806 42738 : logical_array_check (gfc_expr *array, int n)
807 : {
808 42738 : if (array->ts.type != BT_LOGICAL || array->rank == 0)
809 : {
810 0 : gfc_error ("%qs argument of %qs intrinsic at %L must be a logical "
811 0 : "array", gfc_current_intrinsic_arg[n]->name,
812 : gfc_current_intrinsic, &array->where);
813 0 : return false;
814 : }
815 :
816 : return true;
817 : }
818 :
819 :
820 : /* Make sure an expression is an array. */
821 :
822 : static bool
823 64705 : array_check (gfc_expr *e, int n)
824 : {
825 64705 : if (e->rank != 0 && e->ts.type == BT_CLASS && gfc_expr_attr (e).class_ok
826 1241 : && CLASS_DATA (e)->attr.dimension
827 65946 : && CLASS_DATA (e)->as->rank)
828 : {
829 1241 : gfc_add_class_array_ref (e);
830 : }
831 :
832 64705 : if (e->rank != 0 && e->ts.type != BT_PROCEDURE)
833 : return true;
834 :
835 13 : if (gfc_is_class_array_function (e))
836 : return true;
837 :
838 12 : gfc_error ("%qs argument of %qs intrinsic at %L must be an array",
839 12 : gfc_current_intrinsic_arg[n]->name, gfc_current_intrinsic,
840 : &e->where);
841 :
842 12 : return false;
843 : }
844 :
845 :
846 : /* If expr is a constant, then check to ensure that it is greater than
847 : of equal to zero. */
848 :
849 : static bool
850 11260 : nonnegative_check (const char *arg, gfc_expr *expr)
851 : {
852 11260 : int i;
853 :
854 11260 : if (expr->expr_type == EXPR_CONSTANT)
855 : {
856 10286 : gfc_extract_int (expr, &i);
857 10286 : if (i < 0)
858 : {
859 33 : gfc_error ("%qs at %L must be nonnegative", arg, &expr->where);
860 33 : return false;
861 : }
862 : }
863 :
864 : return true;
865 : }
866 :
867 :
868 : /* If expr is a constant, then check to ensure that it is greater than zero. */
869 :
870 : static bool
871 145 : positive_check (int n, gfc_expr *expr)
872 : {
873 145 : int i;
874 :
875 145 : if (expr->expr_type == EXPR_CONSTANT)
876 : {
877 122 : gfc_extract_int (expr, &i);
878 122 : if (i <= 0)
879 : {
880 12 : gfc_error ("%qs argument of %qs intrinsic at %L must be positive",
881 12 : gfc_current_intrinsic_arg[n]->name, gfc_current_intrinsic,
882 : &expr->where);
883 12 : return false;
884 : }
885 : }
886 :
887 : return true;
888 : }
889 :
890 :
891 : /* If expr2 is constant, then check that the value is less than
892 : (less than or equal to, if 'or_equal' is true) bit_size(expr1). */
893 :
894 : static bool
895 11298 : less_than_bitsize1 (const char *arg1, gfc_expr *expr1, const char *arg2,
896 : gfc_expr *expr2, bool or_equal)
897 : {
898 11298 : int i2, i3;
899 :
900 11298 : if (expr2->expr_type == EXPR_CONSTANT)
901 : {
902 9818 : gfc_extract_int (expr2, &i2);
903 9818 : i3 = gfc_validate_kind (BT_INTEGER, expr1->ts.kind, false);
904 :
905 : /* For ISHFT[C], check that |shift| <= bit_size(i). */
906 9818 : if (arg2 == NULL)
907 : {
908 1091 : if (i2 < 0)
909 296 : i2 = -i2;
910 :
911 1091 : if (i2 > gfc_integer_kinds[i3].bit_size)
912 : {
913 4 : gfc_error ("The absolute value of SHIFT at %L must be less "
914 : "than or equal to BIT_SIZE(%qs)",
915 : &expr2->where, arg1);
916 4 : return false;
917 : }
918 : }
919 :
920 9814 : if (or_equal)
921 : {
922 9442 : if (i2 > gfc_integer_kinds[i3].bit_size)
923 : {
924 7 : gfc_error ("%qs at %L must be less than "
925 : "or equal to BIT_SIZE(%qs)",
926 : arg2, &expr2->where, arg1);
927 7 : return false;
928 : }
929 : }
930 : else
931 : {
932 372 : if (i2 >= gfc_integer_kinds[i3].bit_size)
933 : {
934 15 : gfc_error ("%qs at %L must be less than BIT_SIZE(%qs)",
935 : arg2, &expr2->where, arg1);
936 15 : return false;
937 : }
938 : }
939 : }
940 :
941 : return true;
942 : }
943 :
944 :
945 : /* If expr is constant, then check that the value is less than or equal
946 : to the bit_size of the kind k. */
947 :
948 : static bool
949 1018 : less_than_bitsizekind (const char *arg, gfc_expr *expr, int k)
950 : {
951 1018 : int i, val;
952 1018 : int bit_size;
953 :
954 1018 : if (expr->expr_type != EXPR_CONSTANT)
955 : return true;
956 :
957 944 : i = gfc_validate_kind (expr->ts.type, k, false);
958 944 : gfc_extract_int (expr, &val);
959 :
960 944 : if (expr->ts.type == BT_INTEGER)
961 944 : bit_size = gfc_integer_kinds[i].bit_size;
962 : else
963 0 : bit_size = gfc_unsigned_kinds[i].bit_size;
964 :
965 944 : if (val > bit_size)
966 : {
967 4 : gfc_error ("%qs at %L must be less than or equal to the BIT_SIZE of "
968 : "INTEGER(KIND=%d)", arg, &expr->where, k);
969 4 : return false;
970 : }
971 :
972 : return true;
973 : }
974 :
975 :
976 : /* If expr2 and expr3 are constants, then check that the value is less than
977 : or equal to bit_size(expr1). */
978 :
979 : static bool
980 466 : less_than_bitsize2 (const char *arg1, gfc_expr *expr1, const char *arg2,
981 : gfc_expr *expr2, const char *arg3, gfc_expr *expr3)
982 : {
983 466 : int i2, i3;
984 466 : int k, bit_size;
985 :
986 466 : if (expr2->expr_type == EXPR_CONSTANT && expr3->expr_type == EXPR_CONSTANT)
987 : {
988 310 : gfc_extract_int (expr2, &i2);
989 310 : gfc_extract_int (expr3, &i3);
990 310 : i2 += i3;
991 310 : k = gfc_validate_kind (expr1->ts.type, expr1->ts.kind, false);
992 :
993 310 : if (expr1->ts.type == BT_INTEGER)
994 298 : bit_size = gfc_integer_kinds[k].bit_size;
995 : else
996 12 : bit_size = gfc_unsigned_kinds[k].bit_size;
997 :
998 310 : if (i2 > bit_size)
999 : {
1000 7 : gfc_error ("%<%s + %s%> at %L must be less than or equal "
1001 : "to BIT_SIZE(%qs)",
1002 : arg2, arg3, &expr2->where, arg1);
1003 7 : return false;
1004 : }
1005 : }
1006 :
1007 : return true;
1008 : }
1009 :
1010 : /* Make sure two expressions have the same type. */
1011 :
1012 : static bool
1013 10316 : same_type_check (gfc_expr *e, int n, gfc_expr *f, int m, bool assoc = false)
1014 : {
1015 10316 : gfc_typespec *ets = &e->ts;
1016 10316 : gfc_typespec *fts = &f->ts;
1017 :
1018 10316 : if (assoc)
1019 : {
1020 : /* Procedure pointer component expressions have the type of the interface
1021 : procedure. If they are being tested for association with a procedure
1022 : pointer (ie. not a component), the type of the procedure must be
1023 : determined. */
1024 2446 : if (e->ts.type == BT_PROCEDURE && e->symtree->n.sym)
1025 128 : ets = &e->symtree->n.sym->ts;
1026 2446 : if (f->ts.type == BT_PROCEDURE && f->symtree->n.sym)
1027 127 : fts = &f->symtree->n.sym->ts;
1028 : }
1029 :
1030 10316 : if (gfc_compare_types (ets, fts))
1031 : return true;
1032 :
1033 24 : gfc_error ("%qs argument of %qs intrinsic at %L must be the same type "
1034 24 : "and kind as %qs", gfc_current_intrinsic_arg[m]->name,
1035 : gfc_current_intrinsic, &f->where,
1036 24 : gfc_current_intrinsic_arg[n]->name);
1037 :
1038 24 : return false;
1039 : }
1040 :
1041 :
1042 : /* Make sure that an expression has a certain (nonzero) rank. */
1043 :
1044 : static bool
1045 15471 : rank_check (gfc_expr *e, int n, int rank)
1046 : {
1047 15471 : if (e->rank == rank)
1048 : return true;
1049 :
1050 4 : gfc_error ("%qs argument of %qs intrinsic at %L must be of rank %d",
1051 4 : gfc_current_intrinsic_arg[n]->name, gfc_current_intrinsic,
1052 : &e->where, rank);
1053 :
1054 4 : return false;
1055 : }
1056 :
1057 :
1058 : /* Make sure a variable expression is not an optional dummy argument. */
1059 :
1060 : static bool
1061 25801 : nonoptional_check (gfc_expr *e, int n)
1062 : {
1063 25801 : if (e->expr_type == EXPR_VARIABLE && e->symtree->n.sym->attr.optional)
1064 : {
1065 2 : gfc_error ("%qs argument of %qs intrinsic at %L must not be OPTIONAL",
1066 2 : gfc_current_intrinsic_arg[n]->name, gfc_current_intrinsic,
1067 : &e->where);
1068 : }
1069 :
1070 : /* TODO: Recursive check on nonoptional variables? */
1071 :
1072 25801 : return true;
1073 : }
1074 :
1075 :
1076 : /* Check for ALLOCATABLE attribute. */
1077 :
1078 : static bool
1079 8122 : allocatable_check (gfc_expr *e, int n)
1080 : {
1081 8122 : symbol_attribute attr;
1082 :
1083 8122 : attr = gfc_variable_attr (e);
1084 8122 : if (!attr.allocatable
1085 8110 : || (attr.associate_var && !attr.select_rank_temporary))
1086 : {
1087 12 : gfc_error ("%qs argument of %qs intrinsic at %L must be ALLOCATABLE",
1088 12 : gfc_current_intrinsic_arg[n]->name, gfc_current_intrinsic,
1089 : &e->where);
1090 12 : return false;
1091 : }
1092 :
1093 : return true;
1094 : }
1095 :
1096 :
1097 : /* Check that an expression has a particular kind. */
1098 :
1099 : static bool
1100 2835 : kind_value_check (gfc_expr *e, int n, int k)
1101 : {
1102 2835 : if (e->ts.kind == k)
1103 : return true;
1104 :
1105 141 : gfc_error ("%qs argument of %qs intrinsic at %L must be of kind %d",
1106 141 : gfc_current_intrinsic_arg[n]->name, gfc_current_intrinsic,
1107 : &e->where, k);
1108 :
1109 141 : return false;
1110 : }
1111 :
1112 :
1113 : /* Error message for an actual argument with an unsupported kind value. */
1114 :
1115 : static void
1116 2 : error_unsupported_kind (gfc_expr *e, int n)
1117 : {
1118 2 : gfc_error ("Not supported: %qs argument of %qs intrinsic at %L with kind %d",
1119 2 : gfc_current_intrinsic_arg[n]->name,
1120 : gfc_current_intrinsic, &e->where, e->ts.kind);
1121 2 : return;
1122 : }
1123 :
1124 :
1125 : /* Check if the decimal exponent range of an integer variable is at least four
1126 : so that it is large enough to e.g. hold errno values and the values of
1127 : LIBERROR_* from libgfortran.h. */
1128 :
1129 : static bool
1130 69 : check_minrange4 (gfc_expr *e, int n)
1131 : {
1132 69 : if (e->ts.kind >= 2)
1133 : return true;
1134 :
1135 3 : gfc_error ("%qs argument of %qs intrinsic at %L must have "
1136 : "a decimal exponent range of at least four",
1137 3 : gfc_current_intrinsic_arg[n]->name,
1138 : gfc_current_intrinsic, &e->where);
1139 3 : return false;
1140 : }
1141 :
1142 :
1143 : /* Check if an argument is not a coindexed variable. */
1144 :
1145 : static bool
1146 17 : check_not_coindexed (gfc_expr *e, int n)
1147 : {
1148 17 : if (!gfc_is_coindexed (e))
1149 : return true;
1150 :
1151 2 : gfc_error ("%qs argument of %qs intrinsic at %L shall not be coindexed",
1152 2 : gfc_current_intrinsic_arg[n]->name,
1153 : gfc_current_intrinsic, &e->where);
1154 2 : return false;
1155 : }
1156 :
1157 :
1158 : /* Make sure an expression is a variable. */
1159 :
1160 : static bool
1161 20812 : variable_check (gfc_expr *e, int n, bool allow_proc)
1162 : {
1163 20812 : if (e->expr_type == EXPR_VARIABLE
1164 20784 : && e->symtree->n.sym->attr.intent == INTENT_IN
1165 1443 : && (gfc_current_intrinsic_arg[n]->intent == INTENT_OUT
1166 1432 : || gfc_current_intrinsic_arg[n]->intent == INTENT_INOUT)
1167 20842 : && !gfc_check_vardef_context (e, false, true, false, NULL))
1168 : {
1169 6 : gfc_error ("%qs argument of %qs intrinsic at %L cannot be INTENT(IN)",
1170 6 : gfc_current_intrinsic_arg[n]->name,
1171 : gfc_current_intrinsic, &e->where);
1172 6 : return false;
1173 : }
1174 :
1175 20806 : if (e->expr_type == EXPR_VARIABLE
1176 20778 : && e->symtree->n.sym->attr.flavor != FL_PARAMETER
1177 20778 : && (allow_proc || !e->symtree->n.sym->attr.function))
1178 : return true;
1179 :
1180 84 : if (e->expr_type == EXPR_VARIABLE && e->symtree->n.sym->attr.function
1181 56 : && e->symtree->n.sym == e->symtree->n.sym->result)
1182 : {
1183 56 : gfc_namespace *ns;
1184 67 : for (ns = gfc_current_ns; ns; ns = ns->parent)
1185 66 : if (ns->proc_name == e->symtree->n.sym)
1186 : return true;
1187 : }
1188 :
1189 : /* F2018:R902: function reference having a data pointer result. */
1190 29 : if (e->expr_type == EXPR_FUNCTION
1191 1 : && e->symtree->n.sym->attr.flavor == FL_PROCEDURE
1192 1 : && e->symtree->n.sym->attr.function
1193 1 : && e->symtree->n.sym->attr.pointer)
1194 : return true;
1195 :
1196 28 : gfc_error ("%qs argument of %qs intrinsic at %L must be a variable",
1197 28 : gfc_current_intrinsic_arg[n]->name, gfc_current_intrinsic, &e->where);
1198 :
1199 28 : return false;
1200 : }
1201 :
1202 :
1203 : /* Check the common DIM parameter for correctness. */
1204 :
1205 : static bool
1206 96538 : dim_check (gfc_expr *dim, int n, bool optional)
1207 : {
1208 96538 : if (dim == NULL)
1209 : return true;
1210 :
1211 31680 : if (!type_check (dim, n, BT_INTEGER))
1212 : return false;
1213 :
1214 31664 : if (!scalar_check (dim, n))
1215 : return false;
1216 :
1217 31660 : if (!optional && !nonoptional_check (dim, n))
1218 : return false;
1219 :
1220 : return true;
1221 : }
1222 :
1223 :
1224 : /* If a coarray DIM parameter is a constant, make sure that it is greater than
1225 : zero and less than or equal to the corank of the given array. */
1226 :
1227 : static bool
1228 919 : dim_corank_check (gfc_expr *dim, gfc_expr *array)
1229 : {
1230 919 : gcc_assert (array->expr_type == EXPR_VARIABLE);
1231 :
1232 919 : if (dim->expr_type != EXPR_CONSTANT)
1233 : return true;
1234 :
1235 709 : if (array->ts.type == BT_CLASS)
1236 : return true;
1237 :
1238 664 : if (mpz_cmp_ui (dim->value.integer, 1) < 0
1239 664 : || mpz_cmp_ui (dim->value.integer, array->corank) > 0)
1240 : {
1241 1 : gfc_error ("%<dim%> argument of %qs intrinsic at %L is not a valid "
1242 : "codimension index", gfc_current_intrinsic, &dim->where);
1243 :
1244 1 : return false;
1245 : }
1246 :
1247 : return true;
1248 : }
1249 :
1250 :
1251 : /* If a DIM parameter is a constant, make sure that it is greater than
1252 : zero and less than or equal to the rank of the given array. If
1253 : allow_assumed is zero then dim must be less than the rank of the array
1254 : for assumed size arrays. */
1255 :
1256 : static bool
1257 94850 : dim_rank_check (gfc_expr *dim, gfc_expr *array, int allow_assumed)
1258 : {
1259 94850 : gfc_array_ref *ar;
1260 94850 : int rank;
1261 :
1262 94850 : if (dim == NULL)
1263 : return true;
1264 :
1265 29992 : if (dim->expr_type != EXPR_CONSTANT)
1266 : return true;
1267 :
1268 28551 : if (array->expr_type == EXPR_FUNCTION && array->value.function.isym
1269 641 : && array->value.function.isym->id == GFC_ISYM_SPREAD)
1270 60 : rank = array->rank + 1;
1271 : else
1272 28491 : rank = array->rank;
1273 :
1274 : /* Assumed-rank array. */
1275 28551 : if (rank == -1)
1276 1236 : rank = GFC_MAX_DIMENSIONS;
1277 :
1278 28551 : if (array->expr_type == EXPR_VARIABLE)
1279 : {
1280 27338 : ar = gfc_find_array_ref (array, true);
1281 27338 : if (!ar)
1282 : return false;
1283 27337 : if (ar->as->type == AS_ASSUMED_SIZE
1284 430 : && !allow_assumed
1285 190 : && ar->type != AR_ELEMENT
1286 190 : && ar->type != AR_SECTION)
1287 184 : rank--;
1288 : }
1289 :
1290 28550 : if (mpz_cmp_ui (dim->value.integer, 1) < 0
1291 28548 : || mpz_cmp_ui (dim->value.integer, rank) > 0)
1292 : {
1293 11 : gfc_error ("%<dim%> argument of %qs intrinsic at %L is not a valid "
1294 : "dimension index", gfc_current_intrinsic, &dim->where);
1295 :
1296 11 : return false;
1297 : }
1298 :
1299 : return true;
1300 : }
1301 :
1302 :
1303 : /* Compare the size of a along dimension ai with the size of b along
1304 : dimension bi, returning 0 if they are known not to be identical,
1305 : and 1 if they are identical, or if this cannot be determined. */
1306 :
1307 : static bool
1308 2557 : identical_dimen_shape (gfc_expr *a, int ai, gfc_expr *b, int bi)
1309 : {
1310 2557 : mpz_t a_size, b_size;
1311 2557 : bool ret;
1312 :
1313 2557 : gcc_assert (a->rank > ai);
1314 2557 : gcc_assert (b->rank > bi);
1315 :
1316 2557 : ret = true;
1317 :
1318 2557 : if (gfc_array_dimen_size (a, ai, &a_size))
1319 : {
1320 2077 : if (gfc_array_dimen_size (b, bi, &b_size))
1321 : {
1322 1980 : if (mpz_cmp (a_size, b_size) != 0)
1323 10 : ret = false;
1324 :
1325 1980 : mpz_clear (b_size);
1326 : }
1327 2077 : mpz_clear (a_size);
1328 : }
1329 2557 : return ret;
1330 : }
1331 :
1332 : /* Calculate the length of a character variable, including substrings.
1333 : Strip away parentheses if necessary. Return -1 if no length could
1334 : be determined. */
1335 :
1336 : static long
1337 5091 : gfc_var_strlen (const gfc_expr *a)
1338 : {
1339 5091 : gfc_ref *ra;
1340 :
1341 5091 : while (a->expr_type == EXPR_OP && a->value.op.op == INTRINSIC_PARENTHESES)
1342 0 : a = a->value.op.op1;
1343 :
1344 7082 : for (ra = a->ref; ra != NULL && ra->type != REF_SUBSTRING; ra = ra->next)
1345 : ;
1346 :
1347 5091 : if (ra)
1348 : {
1349 309 : long start_a, end_a;
1350 :
1351 309 : if (!ra->u.ss.end)
1352 : return -1;
1353 :
1354 308 : if ((!ra->u.ss.start || ra->u.ss.start->expr_type == EXPR_CONSTANT)
1355 299 : && ra->u.ss.end->expr_type == EXPR_CONSTANT)
1356 : {
1357 293 : start_a = ra->u.ss.start ? mpz_get_si (ra->u.ss.start->value.integer)
1358 : : 1;
1359 293 : end_a = mpz_get_si (ra->u.ss.end->value.integer);
1360 293 : return (end_a < start_a) ? 0 : end_a - start_a + 1;
1361 : }
1362 15 : else if (ra->u.ss.start
1363 15 : && gfc_dep_compare_expr (ra->u.ss.start, ra->u.ss.end) == 0)
1364 : return 1;
1365 : else
1366 13 : return -1;
1367 : }
1368 :
1369 4782 : if (a->ts.u.cl && a->ts.u.cl->length
1370 2674 : && a->ts.u.cl->length->expr_type == EXPR_CONSTANT)
1371 2587 : return mpz_get_si (a->ts.u.cl->length->value.integer);
1372 2195 : else if (a->expr_type == EXPR_CONSTANT
1373 401 : && (a->ts.u.cl == NULL || a->ts.u.cl->length == NULL))
1374 401 : return a->value.character.length;
1375 : else
1376 : return -1;
1377 :
1378 : }
1379 :
1380 : /* Check whether two character expressions have the same length;
1381 : returns true if they have or if the length cannot be determined,
1382 : otherwise return false and raise a gfc_error. */
1383 :
1384 : bool
1385 2144 : gfc_check_same_strlen (const gfc_expr *a, const gfc_expr *b, const char *name)
1386 : {
1387 2144 : long len_a, len_b;
1388 :
1389 2144 : len_a = gfc_var_strlen(a);
1390 2144 : len_b = gfc_var_strlen(b);
1391 :
1392 2144 : if (len_a == -1 || len_b == -1 || len_a == len_b)
1393 : return true;
1394 : else
1395 : {
1396 23 : gfc_error ("Unequal character lengths (%ld/%ld) in %s at %L",
1397 : len_a, len_b, name, &a->where);
1398 23 : return false;
1399 : }
1400 : }
1401 :
1402 : /* Check size of an array argument against a required size.
1403 : Returns true if the requirement is satisfied or if the size cannot be
1404 : determined, otherwise return false and raise a gfc_error */
1405 :
1406 : static bool
1407 215 : array_size_check (gfc_expr *a, int n, long size_min)
1408 : {
1409 215 : bool ok = true;
1410 215 : mpz_t size;
1411 :
1412 215 : if (gfc_array_size (a, &size))
1413 : {
1414 203 : HOST_WIDE_INT sz = gfc_mpz_get_hwi (size);
1415 203 : if (size_min >= 0 && sz < size_min)
1416 : {
1417 7 : gfc_error ("Size of %qs argument of %qs intrinsic at %L "
1418 : "too small (%wd/%ld)",
1419 7 : gfc_current_intrinsic_arg[n]->name,
1420 : gfc_current_intrinsic, &a->where, sz, size_min);
1421 7 : ok = false;
1422 : }
1423 203 : mpz_clear (size);
1424 : }
1425 :
1426 215 : return ok;
1427 : }
1428 :
1429 :
1430 : /***** Check functions *****/
1431 :
1432 : /* Check subroutine suitable for intrinsics taking a real argument and
1433 : a kind argument for the result. */
1434 :
1435 : static bool
1436 651 : check_a_kind (gfc_expr *a, gfc_expr *kind, bt type)
1437 : {
1438 651 : if (!type_check (a, 0, BT_REAL))
1439 : return false;
1440 651 : if (!kind_check (kind, 1, type))
1441 : return false;
1442 :
1443 : return true;
1444 : }
1445 :
1446 :
1447 : /* Check subroutine suitable for ceiling, floor and nint. */
1448 :
1449 : bool
1450 389 : gfc_check_a_ikind (gfc_expr *a, gfc_expr *kind)
1451 : {
1452 389 : return check_a_kind (a, kind, BT_INTEGER);
1453 : }
1454 :
1455 :
1456 : /* Check subroutine suitable for aint, anint. */
1457 :
1458 : bool
1459 262 : gfc_check_a_xkind (gfc_expr *a, gfc_expr *kind)
1460 : {
1461 262 : return check_a_kind (a, kind, BT_REAL);
1462 : }
1463 :
1464 :
1465 : bool
1466 4722 : gfc_check_abs (gfc_expr *a)
1467 : {
1468 4722 : if (!numeric_check (a, 0))
1469 : return false;
1470 :
1471 : return true;
1472 : }
1473 :
1474 :
1475 : bool
1476 6916 : gfc_check_achar (gfc_expr *a, gfc_expr *kind)
1477 : {
1478 6916 : if (a->ts.type == BT_BOZ)
1479 : {
1480 0 : if (gfc_invalid_boz (G_("BOZ literal constant at %L cannot appear in "
1481 : "ACHAR intrinsic subprogram"), &a->where))
1482 : return false;
1483 :
1484 0 : if (!gfc_boz2int (a, gfc_default_integer_kind))
1485 : return false;
1486 : }
1487 :
1488 6916 : if (!type_check (a, 0, BT_INTEGER))
1489 : return false;
1490 :
1491 6916 : if (!kind_check (kind, 1, BT_CHARACTER))
1492 : return false;
1493 :
1494 : return true;
1495 : }
1496 :
1497 :
1498 : bool
1499 292 : gfc_check_access_func (gfc_expr *name, gfc_expr *mode)
1500 : {
1501 292 : if (!type_check (name, 0, BT_CHARACTER)
1502 292 : || !scalar_check (name, 0))
1503 : return false;
1504 292 : if (!kind_value_check (name, 0, gfc_default_character_kind))
1505 : return false;
1506 :
1507 290 : if (!type_check (mode, 1, BT_CHARACTER)
1508 290 : || !scalar_check (mode, 1))
1509 : return false;
1510 290 : if (!kind_value_check (mode, 1, gfc_default_character_kind))
1511 : return false;
1512 :
1513 : return true;
1514 : }
1515 :
1516 :
1517 : bool
1518 42377 : gfc_check_all_any (gfc_expr *mask, gfc_expr *dim)
1519 : {
1520 42377 : if (!logical_array_check (mask, 0))
1521 : return false;
1522 :
1523 42377 : if (!dim_check (dim, 1, false))
1524 : return false;
1525 :
1526 42377 : if (!dim_rank_check (dim, mask, 0))
1527 : return false;
1528 :
1529 : return true;
1530 : }
1531 :
1532 :
1533 : /* Limited checking for ALLOCATED intrinsic. Additional checking
1534 : is performed in intrinsic.cc(sort_actual), because ALLOCATED
1535 : has two mutually exclusive non-optional arguments. */
1536 :
1537 : bool
1538 7509 : gfc_check_allocated (gfc_expr *array)
1539 : {
1540 : /* Tests on allocated components of coarrays need to detour the check to
1541 : argument of the _caf_get. */
1542 7509 : if (flag_coarray == GFC_FCOARRAY_LIB && array->expr_type == EXPR_FUNCTION
1543 0 : && array->value.function.isym
1544 0 : && array->value.function.isym->id == GFC_ISYM_CAF_GET)
1545 : {
1546 0 : array = array->value.function.actual->expr;
1547 0 : if (!array->ref)
1548 : return false;
1549 : }
1550 :
1551 7509 : if (!variable_check (array, 0, false))
1552 : return false;
1553 7508 : if (!allocatable_check (array, 0))
1554 : return false;
1555 :
1556 : return true;
1557 : }
1558 :
1559 : /* Common check function where the first argument must be real or
1560 : integer and the second argument must be the same as the first. */
1561 :
1562 : bool
1563 73 : gfc_check_a_p (gfc_expr *a, gfc_expr *p)
1564 : {
1565 73 : if (!int_or_real_check (a, 0))
1566 : return false;
1567 :
1568 73 : if (a->ts.type != p->ts.type)
1569 : {
1570 0 : gfc_error ("%qs and %qs arguments of %qs intrinsic at %L must "
1571 0 : "have the same type", gfc_current_intrinsic_arg[0]->name,
1572 0 : gfc_current_intrinsic_arg[1]->name, gfc_current_intrinsic,
1573 : &p->where);
1574 0 : return false;
1575 : }
1576 :
1577 73 : if (a->ts.kind != p->ts.kind)
1578 : {
1579 0 : if (!gfc_notify_std (GFC_STD_GNU, "Different type kinds at %L",
1580 : &p->where))
1581 : return false;
1582 : }
1583 :
1584 : return true;
1585 : }
1586 :
1587 : /* Check function where the first argument must be real or integer (or
1588 : unsigned) and the second argument must be the same as the first. */
1589 :
1590 : bool
1591 1876 : gfc_check_mod (gfc_expr *a, gfc_expr *p)
1592 : {
1593 1876 : if (flag_unsigned)
1594 : {
1595 78 : if (!int_or_real_or_unsigned_check (a,0))
1596 : return false;
1597 : }
1598 1798 : else if (!int_or_real_check (a, 0))
1599 : return false;
1600 :
1601 1876 : if (a->ts.type != p->ts.type)
1602 : {
1603 0 : gfc_error ("%qs and %qs arguments of %qs intrinsic at %L must "
1604 0 : "have the same type", gfc_current_intrinsic_arg[0]->name,
1605 0 : gfc_current_intrinsic_arg[1]->name, gfc_current_intrinsic,
1606 : &p->where);
1607 0 : return false;
1608 : }
1609 :
1610 1876 : if (a->ts.kind != p->ts.kind)
1611 : {
1612 146 : if (!gfc_notify_std (GFC_STD_GNU, "Different type kinds at %L",
1613 : &p->where))
1614 : return false;
1615 : }
1616 :
1617 : return true;
1618 : }
1619 :
1620 :
1621 : bool
1622 1778 : gfc_check_x_yd (gfc_expr *x, gfc_expr *y)
1623 : {
1624 1778 : if (!double_check (x, 0) || !double_check (y, 1))
1625 1624 : return false;
1626 :
1627 : return true;
1628 : }
1629 :
1630 : bool
1631 46969 : gfc_invalid_null_arg (gfc_expr *x)
1632 : {
1633 46969 : if (x->expr_type == EXPR_NULL)
1634 : {
1635 23 : gfc_error ("NULL at %L is not permitted as actual argument "
1636 : "to %qs intrinsic function", &x->where,
1637 : gfc_current_intrinsic);
1638 23 : return true;
1639 : }
1640 : return false;
1641 : }
1642 :
1643 : bool
1644 7269 : gfc_check_associated (gfc_expr *pointer, gfc_expr *target)
1645 : {
1646 7269 : symbol_attribute attr1, attr2;
1647 7269 : int i;
1648 7269 : bool t;
1649 :
1650 7269 : if (gfc_invalid_null_arg (pointer))
1651 : return false;
1652 :
1653 7268 : attr1 = gfc_expr_attr (pointer);
1654 :
1655 7268 : if (!attr1.pointer && !attr1.proc_pointer)
1656 : {
1657 2 : gfc_error ("%qs argument of %qs intrinsic at %L must be a POINTER",
1658 2 : gfc_current_intrinsic_arg[0]->name, gfc_current_intrinsic,
1659 : &pointer->where);
1660 2 : return false;
1661 : }
1662 :
1663 : /* F2008, C1242. */
1664 7266 : if (attr1.pointer && gfc_is_coindexed (pointer))
1665 : {
1666 1 : gfc_error ("%qs argument of %qs intrinsic at %L shall not be "
1667 1 : "coindexed", gfc_current_intrinsic_arg[0]->name,
1668 : gfc_current_intrinsic, &pointer->where);
1669 1 : return false;
1670 : }
1671 :
1672 : /* Target argument is optional. */
1673 7265 : if (target == NULL)
1674 : return true;
1675 :
1676 2449 : if (gfc_invalid_null_arg (target))
1677 : return false;
1678 :
1679 2448 : if (target->expr_type == EXPR_VARIABLE || target->expr_type == EXPR_FUNCTION)
1680 2447 : attr2 = gfc_expr_attr (target);
1681 : else
1682 : {
1683 1 : gfc_error ("%qs argument of %qs intrinsic at %L must be a pointer "
1684 : "or target VARIABLE or FUNCTION",
1685 1 : gfc_current_intrinsic_arg[1]->name, gfc_current_intrinsic,
1686 : &target->where);
1687 1 : return false;
1688 : }
1689 :
1690 2447 : if (attr1.pointer && !attr2.pointer && !attr2.target)
1691 : {
1692 0 : gfc_error ("%qs argument of %qs intrinsic at %L must be a POINTER "
1693 0 : "or a TARGET", gfc_current_intrinsic_arg[1]->name,
1694 : gfc_current_intrinsic, &target->where);
1695 0 : return false;
1696 : }
1697 :
1698 : /* F2008, C1242. */
1699 2447 : if (attr1.pointer && gfc_is_coindexed (target))
1700 : {
1701 1 : gfc_error ("%qs argument of %qs intrinsic at %L shall not be "
1702 1 : "coindexed", gfc_current_intrinsic_arg[1]->name,
1703 : gfc_current_intrinsic, &target->where);
1704 1 : return false;
1705 : }
1706 :
1707 2446 : t = true;
1708 2446 : if (!same_type_check (pointer, 0, target, 1, true))
1709 : t = false;
1710 : /* F2018 C838 explicitly allows an assumed-rank variable as the first
1711 : argument of intrinsic inquiry functions. */
1712 2446 : if (pointer->rank != -1 && !rank_check (target, 0, pointer->rank))
1713 : t = false;
1714 2446 : if (target->rank > 0 && target->ref)
1715 : {
1716 3261 : for (i = 0; i < target->rank; i++)
1717 1770 : if (target->ref->u.ar.dimen_type[i] == DIMEN_VECTOR)
1718 : {
1719 0 : gfc_error ("Array section with a vector subscript at %L shall not "
1720 : "be the target of a pointer",
1721 : &target->where);
1722 0 : t = false;
1723 0 : break;
1724 : }
1725 : }
1726 : return t;
1727 : }
1728 :
1729 :
1730 : bool
1731 74 : gfc_check_atan_2 (gfc_expr *y, gfc_expr *x)
1732 : {
1733 : /* gfc_notify_std would be a waste of time as the return value
1734 : is seemingly used only for the generic resolution. The error
1735 : will be: Too many arguments. */
1736 74 : if ((gfc_option.allow_std & GFC_STD_F2008) == 0)
1737 : return false;
1738 :
1739 72 : return gfc_check_atan2 (y, x);
1740 : }
1741 :
1742 :
1743 : bool
1744 565 : gfc_check_atan2 (gfc_expr *y, gfc_expr *x)
1745 : {
1746 565 : if (!type_check (y, 0, BT_REAL))
1747 : return false;
1748 561 : if (!same_type_check (y, 0, x, 1))
1749 : return false;
1750 :
1751 : return true;
1752 : }
1753 :
1754 :
1755 : static bool
1756 375 : gfc_check_atomic (gfc_expr *atom, int atom_no, gfc_expr *value, int val_no,
1757 : gfc_expr *stat, int stat_no)
1758 : {
1759 375 : if (!scalar_check (atom, atom_no) || !scalar_check (value, val_no))
1760 : return false;
1761 :
1762 374 : if (!(atom->ts.type == BT_INTEGER && atom->ts.kind == gfc_atomic_int_kind)
1763 63 : && !(atom->ts.type == BT_LOGICAL
1764 60 : && atom->ts.kind == gfc_atomic_logical_kind))
1765 : {
1766 7 : gfc_error ("ATOM argument at %L to intrinsic function %s shall be an "
1767 : "integer of ATOMIC_INT_KIND or a logical of "
1768 : "ATOMIC_LOGICAL_KIND", &atom->where, gfc_current_intrinsic);
1769 7 : return false;
1770 : }
1771 :
1772 367 : if (!gfc_is_coarray (atom) && !gfc_is_coindexed (atom))
1773 : {
1774 14 : gfc_error ("ATOM argument at %L of the %s intrinsic function shall be a "
1775 : "coarray or coindexed", &atom->where, gfc_current_intrinsic);
1776 14 : return false;
1777 : }
1778 :
1779 353 : if (atom->ts.type != value->ts.type)
1780 : {
1781 13 : gfc_error ("%qs argument of %qs intrinsic at %L shall have the same "
1782 13 : "type as %qs at %L", gfc_current_intrinsic_arg[val_no]->name,
1783 : gfc_current_intrinsic, &value->where,
1784 13 : gfc_current_intrinsic_arg[atom_no]->name, &atom->where);
1785 13 : return false;
1786 : }
1787 :
1788 340 : if (stat != NULL)
1789 : {
1790 293 : if (!type_check (stat, stat_no, BT_INTEGER))
1791 : return false;
1792 293 : if (!scalar_check (stat, stat_no))
1793 : return false;
1794 293 : if (!variable_check (stat, stat_no, false))
1795 : return false;
1796 293 : if (!kind_value_check (stat, stat_no, gfc_default_integer_kind))
1797 : return false;
1798 :
1799 282 : if (!gfc_notify_std (GFC_STD_F2018, "STAT= argument to %s at %L",
1800 : gfc_current_intrinsic, &stat->where))
1801 : return false;
1802 : }
1803 :
1804 : return true;
1805 : }
1806 :
1807 :
1808 : bool
1809 95 : gfc_check_atomic_def (gfc_expr *atom, gfc_expr *value, gfc_expr *stat)
1810 : {
1811 95 : if (atom->expr_type == EXPR_FUNCTION
1812 0 : && atom->value.function.isym
1813 0 : && atom->value.function.isym->id == GFC_ISYM_CAF_GET)
1814 0 : atom = atom->value.function.actual->expr;
1815 :
1816 95 : if (!gfc_check_vardef_context (atom, false, false, false, NULL))
1817 : {
1818 0 : gfc_error ("ATOM argument of the %s intrinsic function at %L shall be "
1819 : "definable", gfc_current_intrinsic, &atom->where);
1820 0 : return false;
1821 : }
1822 :
1823 95 : return gfc_check_atomic (atom, 0, value, 1, stat, 2);
1824 : }
1825 :
1826 :
1827 : bool
1828 62 : gfc_check_atomic_op (gfc_expr *atom, gfc_expr *value, gfc_expr *stat)
1829 : {
1830 62 : if (atom->ts.type != BT_INTEGER || atom->ts.kind != gfc_atomic_int_kind)
1831 : {
1832 4 : gfc_error ("ATOM argument at %L to intrinsic function %s shall be an "
1833 : "integer of ATOMIC_INT_KIND", &atom->where,
1834 : gfc_current_intrinsic);
1835 4 : return false;
1836 : }
1837 :
1838 58 : return gfc_check_atomic_def (atom, value, stat);
1839 : }
1840 :
1841 :
1842 : bool
1843 193 : gfc_check_atomic_ref (gfc_expr *value, gfc_expr *atom, gfc_expr *stat)
1844 : {
1845 193 : if (atom->expr_type == EXPR_FUNCTION
1846 0 : && atom->value.function.isym
1847 0 : && atom->value.function.isym->id == GFC_ISYM_CAF_GET)
1848 0 : atom = atom->value.function.actual->expr;
1849 :
1850 193 : if (!gfc_check_vardef_context (value, false, false, false, NULL))
1851 : {
1852 1 : gfc_error ("VALUE argument of the %s intrinsic function at %L shall be "
1853 : "definable", gfc_current_intrinsic, &value->where);
1854 1 : return false;
1855 : }
1856 :
1857 192 : return gfc_check_atomic (atom, 1, value, 0, stat, 2);
1858 : }
1859 :
1860 : bool
1861 138 : team_type_check (gfc_expr *e, int n)
1862 : {
1863 138 : if (e->ts.type != BT_DERIVED || !e->ts.u.derived
1864 121 : || e->ts.u.derived->from_intmod != INTMOD_ISO_FORTRAN_ENV
1865 121 : || e->ts.u.derived->intmod_sym_id != ISOFORTRAN_TEAM_TYPE)
1866 : {
1867 17 : gfc_error ("%qs argument of %qs intrinsic at %L shall be of type "
1868 : "%<team_type%> from the intrinsic module "
1869 : "%<ISO_FORTRAN_ENV%>",
1870 17 : gfc_current_intrinsic_arg[n]->name, gfc_current_intrinsic,
1871 : &e->where);
1872 17 : return false;
1873 : }
1874 :
1875 : return true;
1876 : }
1877 :
1878 : bool
1879 79 : gfc_check_image_status (gfc_expr *image, gfc_expr *team)
1880 : {
1881 : /* IMAGE has to be a positive, scalar integer. */
1882 155 : if (!type_check (image, 0, BT_INTEGER) || !scalar_check (image, 0)
1883 152 : || !positive_check (0, image))
1884 : return false;
1885 :
1886 67 : return !team || (scalar_check (team, 1) && team_type_check (team, 1));
1887 : }
1888 :
1889 :
1890 : /* Check the arguments for f_c_string. */
1891 :
1892 : bool
1893 76 : gfc_check_f_c_string (gfc_expr *string, gfc_expr *asis)
1894 : {
1895 :
1896 76 : if (gfc_invalid_null_arg (string))
1897 : return false;
1898 :
1899 76 : if (!scalar_check (string, 0))
1900 : return false;
1901 :
1902 76 : if (string->ts.type != BT_CHARACTER
1903 76 : || (string->ts.type == BT_CHARACTER
1904 76 : && (string->ts.kind != gfc_default_character_kind)))
1905 : {
1906 0 : gfc_error ("%qs argument of %qs intrinsic at %L shall have "
1907 : "a type of CHARACTER(KIND=C_CHAR)",
1908 0 : gfc_current_intrinsic_arg[0]->name, gfc_current_intrinsic,
1909 : &string->where);
1910 0 : return false;
1911 : }
1912 :
1913 76 : if (asis)
1914 : {
1915 54 : if (!type_check (asis, 1, BT_LOGICAL))
1916 : return false;
1917 :
1918 54 : if (!scalar_check (asis, 1))
1919 : return false;
1920 : }
1921 :
1922 : return true;
1923 : }
1924 :
1925 :
1926 : bool
1927 132 : gfc_check_failed_or_stopped_images (gfc_expr *team, gfc_expr *kind)
1928 : {
1929 132 : if (team && (!scalar_check (team, 0) || !team_type_check (team, 0)))
1930 : return false;
1931 :
1932 126 : if (kind)
1933 : {
1934 78 : int k;
1935 :
1936 150 : if (!type_check (kind, 1, BT_INTEGER) || !scalar_check (kind, 1)
1937 150 : || !positive_check (1, kind))
1938 24 : return false;
1939 :
1940 : /* Get the kind, reporting error on non-constant or overflow. */
1941 66 : gfc_current_locus = kind->where;
1942 66 : if (gfc_extract_int (kind, &k, 1))
1943 : return false;
1944 60 : if (gfc_validate_kind (BT_INTEGER, k, true) == -1)
1945 : {
1946 6 : gfc_error ("%qs argument of %qs intrinsic at %L shall specify a "
1947 6 : "valid integer kind", gfc_current_intrinsic_arg[1]->name,
1948 : gfc_current_intrinsic, &kind->where);
1949 6 : return false;
1950 : }
1951 : }
1952 : return true;
1953 : }
1954 :
1955 :
1956 : bool
1957 84 : gfc_check_get_team (gfc_expr *level)
1958 : {
1959 84 : if (level)
1960 : {
1961 39 : int l;
1962 :
1963 39 : if (!type_check (level, 0, BT_INTEGER) || !scalar_check (level, 0))
1964 19 : return false;
1965 :
1966 : /* When level is a constant, try to extract it. If not, the runtime has
1967 : to check. */
1968 30 : if (gfc_extract_int (level, &l, 0))
1969 : return true;
1970 :
1971 23 : if (l < GFC_CAF_INITIAL_TEAM || l > GFC_CAF_CURRENT_TEAM)
1972 : {
1973 3 : gfc_error ("%qs argument of %qs intrinsic at %L shall specify one of "
1974 : "the INITIAL_TEAM, PARENT_TEAM or CURRENT_TEAM constants "
1975 : "from the intrinsic module ISO_FORTRAN_ENV",
1976 3 : gfc_current_intrinsic_arg[0]->name, gfc_current_intrinsic,
1977 : &level->where);
1978 3 : return false;
1979 : }
1980 : }
1981 : return true;
1982 : }
1983 :
1984 :
1985 : bool
1986 27 : gfc_check_atomic_cas (gfc_expr *atom, gfc_expr *old, gfc_expr *compare,
1987 : gfc_expr *new_val, gfc_expr *stat)
1988 : {
1989 27 : if (atom->expr_type == EXPR_FUNCTION
1990 0 : && atom->value.function.isym
1991 0 : && atom->value.function.isym->id == GFC_ISYM_CAF_GET)
1992 0 : atom = atom->value.function.actual->expr;
1993 :
1994 27 : if (!gfc_check_atomic (atom, 0, new_val, 3, stat, 4))
1995 : return false;
1996 :
1997 20 : if (!scalar_check (old, 1) || !scalar_check (compare, 2))
1998 : return false;
1999 :
2000 20 : if (!same_type_check (atom, 0, old, 1))
2001 : return false;
2002 :
2003 18 : if (!same_type_check (atom, 0, compare, 2))
2004 : return false;
2005 :
2006 16 : if (!gfc_check_vardef_context (atom, false, false, false, NULL))
2007 : {
2008 0 : gfc_error ("ATOM argument of the %s intrinsic function at %L shall be "
2009 : "definable", gfc_current_intrinsic, &atom->where);
2010 0 : return false;
2011 : }
2012 :
2013 16 : if (!gfc_check_vardef_context (old, false, false, false, NULL))
2014 : {
2015 0 : gfc_error ("OLD argument of the %s intrinsic function at %L shall be "
2016 : "definable", gfc_current_intrinsic, &old->where);
2017 0 : return false;
2018 : }
2019 :
2020 : return true;
2021 : }
2022 :
2023 : bool
2024 105 : gfc_check_event_query (gfc_expr *event, gfc_expr *count, gfc_expr *stat)
2025 : {
2026 105 : if (event->ts.type != BT_DERIVED
2027 105 : || event->ts.u.derived->from_intmod != INTMOD_ISO_FORTRAN_ENV
2028 105 : || event->ts.u.derived->intmod_sym_id != ISOFORTRAN_EVENT_TYPE)
2029 : {
2030 0 : gfc_error ("EVENT argument at %L to the intrinsic EVENT_QUERY "
2031 : "shall be of type EVENT_TYPE", &event->where);
2032 0 : return false;
2033 : }
2034 :
2035 105 : if (!scalar_check (event, 0))
2036 : return false;
2037 :
2038 105 : if (!gfc_check_vardef_context (count, false, false, false, NULL))
2039 : {
2040 0 : gfc_error ("COUNT argument of the EVENT_QUERY intrinsic function at %L "
2041 : "shall be definable", &count->where);
2042 0 : return false;
2043 : }
2044 :
2045 105 : if (!type_check (count, 1, BT_INTEGER))
2046 : return false;
2047 :
2048 105 : int i = gfc_validate_kind (BT_INTEGER, count->ts.kind, false);
2049 105 : int j = gfc_validate_kind (BT_INTEGER, gfc_default_integer_kind, false);
2050 :
2051 105 : if (gfc_integer_kinds[i].range < gfc_integer_kinds[j].range)
2052 : {
2053 0 : gfc_error ("COUNT argument of the EVENT_QUERY intrinsic function at %L "
2054 : "shall have at least the range of the default integer",
2055 : &count->where);
2056 0 : return false;
2057 : }
2058 :
2059 105 : if (stat != NULL)
2060 : {
2061 18 : if (!type_check (stat, 2, BT_INTEGER))
2062 : return false;
2063 18 : if (!scalar_check (stat, 2))
2064 : return false;
2065 18 : if (!variable_check (stat, 2, false))
2066 : return false;
2067 :
2068 18 : if (!gfc_notify_std (GFC_STD_F2018, "STAT= argument to %s at %L",
2069 : gfc_current_intrinsic, &stat->where))
2070 : return false;
2071 : }
2072 :
2073 : return true;
2074 : }
2075 :
2076 :
2077 : bool
2078 65 : gfc_check_atomic_fetch_op (gfc_expr *atom, gfc_expr *value, gfc_expr *old,
2079 : gfc_expr *stat)
2080 : {
2081 65 : if (atom->expr_type == EXPR_FUNCTION
2082 0 : && atom->value.function.isym
2083 0 : && atom->value.function.isym->id == GFC_ISYM_CAF_GET)
2084 0 : atom = atom->value.function.actual->expr;
2085 :
2086 65 : if (atom->ts.type != BT_INTEGER || atom->ts.kind != gfc_atomic_int_kind)
2087 : {
2088 4 : gfc_error ("ATOM argument at %L to intrinsic function %s shall be an "
2089 : "integer of ATOMIC_INT_KIND", &atom->where,
2090 : gfc_current_intrinsic);
2091 4 : return false;
2092 : }
2093 :
2094 61 : if (!gfc_check_atomic (atom, 0, value, 1, stat, 3))
2095 : return false;
2096 :
2097 49 : if (!scalar_check (old, 2))
2098 : return false;
2099 :
2100 49 : if (!same_type_check (atom, 0, old, 2))
2101 : return false;
2102 :
2103 45 : if (!gfc_check_vardef_context (atom, false, false, false, NULL))
2104 : {
2105 0 : gfc_error ("ATOM argument of the %s intrinsic function at %L shall be "
2106 : "definable", gfc_current_intrinsic, &atom->where);
2107 0 : return false;
2108 : }
2109 :
2110 45 : if (!gfc_check_vardef_context (old, false, false, false, NULL))
2111 : {
2112 0 : gfc_error ("OLD argument of the %s intrinsic function at %L shall be "
2113 : "definable", gfc_current_intrinsic, &old->where);
2114 0 : return false;
2115 : }
2116 :
2117 : return true;
2118 : }
2119 :
2120 :
2121 : /* BESJN and BESYN functions. */
2122 :
2123 : bool
2124 239 : gfc_check_besn (gfc_expr *n, gfc_expr *x)
2125 : {
2126 239 : if (!type_check (n, 0, BT_INTEGER))
2127 : return false;
2128 239 : if (n->expr_type == EXPR_CONSTANT)
2129 : {
2130 59 : int i;
2131 59 : gfc_extract_int (n, &i);
2132 59 : if (i < 0 && !gfc_notify_std (GFC_STD_GNU, "Negative argument "
2133 : "N at %L", &n->where))
2134 2 : return false;
2135 : }
2136 :
2137 237 : if (!type_check (x, 1, BT_REAL))
2138 : return false;
2139 :
2140 : return true;
2141 : }
2142 :
2143 :
2144 : /* Transformational version of the Bessel JN and YN functions. */
2145 :
2146 : bool
2147 71 : gfc_check_bessel_n2 (gfc_expr *n1, gfc_expr *n2, gfc_expr *x)
2148 : {
2149 71 : if (!type_check (n1, 0, BT_INTEGER))
2150 : return false;
2151 71 : if (!scalar_check (n1, 0))
2152 : return false;
2153 71 : if (!nonnegative_check ("N1", n1))
2154 : return false;
2155 :
2156 70 : if (!type_check (n2, 1, BT_INTEGER))
2157 : return false;
2158 70 : if (!scalar_check (n2, 1))
2159 : return false;
2160 70 : if (!nonnegative_check ("N2", n2))
2161 : return false;
2162 :
2163 70 : if (!type_check (x, 2, BT_REAL))
2164 : return false;
2165 70 : if (!scalar_check (x, 2))
2166 : return false;
2167 :
2168 : return true;
2169 : }
2170 :
2171 :
2172 : bool
2173 1662 : gfc_check_bge_bgt_ble_blt (gfc_expr *i, gfc_expr *j)
2174 : {
2175 1662 : extern int gfc_max_integer_kind;
2176 :
2177 : /* If i and j are both BOZ, convert to widest INTEGER. */
2178 1662 : if (i->ts.type == BT_BOZ && j->ts.type == BT_BOZ)
2179 : {
2180 24 : if (!gfc_boz2int (i, gfc_max_integer_kind))
2181 : return false;
2182 24 : if (!gfc_boz2int (j, gfc_max_integer_kind))
2183 : return false;
2184 : }
2185 :
2186 : /* If i is BOZ and j is integer, convert i to type of j. */
2187 24 : if (i->ts.type == BT_BOZ && j->ts.type == BT_INTEGER
2188 1686 : && !gfc_boz2int (i, j->ts.kind))
2189 : return false;
2190 :
2191 : /* If j is BOZ and i is integer, convert j to type of i. */
2192 24 : if (j->ts.type == BT_BOZ && i->ts.type == BT_INTEGER
2193 1686 : && !gfc_boz2int (j, i->ts.kind))
2194 : return false;
2195 :
2196 1662 : if (flag_unsigned)
2197 : {
2198 : /* If i is BOZ and j is UNSIGNED, convert i to type of j. */
2199 0 : if (i->ts.type == BT_BOZ && j->ts.type == BT_UNSIGNED
2200 480 : && !gfc_boz2uint (i, j->ts.kind))
2201 : return false;
2202 :
2203 : /* If j is BOZ and i is UNSIGNED, convert j to type of i. */
2204 0 : if (j->ts.type == BT_BOZ && i->ts.type == BT_UNSIGNED
2205 480 : && !gfc_boz2uint (j, i->ts.kind))
2206 : return false;
2207 :
2208 480 : if (gfc_invalid_unsigned_ops (i,j))
2209 : return false;
2210 :
2211 480 : if (!type_check2 (i, 0, BT_INTEGER, BT_UNSIGNED))
2212 : return false;
2213 :
2214 480 : if (!type_check2 (j, 1, BT_INTEGER, BT_UNSIGNED))
2215 : return false;
2216 :
2217 : }
2218 : else
2219 : {
2220 1182 : if (!type_check (i, 0, BT_INTEGER))
2221 : return false;
2222 :
2223 1182 : if (!type_check (j, 1, BT_INTEGER))
2224 : return false;
2225 : }
2226 :
2227 : return true;
2228 : }
2229 :
2230 :
2231 : bool
2232 777 : gfc_check_bitfcn (gfc_expr *i, gfc_expr *pos)
2233 : {
2234 777 : if (flag_unsigned)
2235 : {
2236 102 : if (!type_check2 (i, 0, BT_INTEGER, BT_UNSIGNED))
2237 : return false;
2238 : }
2239 : else
2240 : {
2241 675 : if (!type_check (i, 0, BT_INTEGER))
2242 : return false;
2243 : }
2244 :
2245 777 : if (!type_check (pos, 1, BT_INTEGER))
2246 : return false;
2247 :
2248 777 : if (!nonnegative_check ("pos", pos))
2249 : return false;
2250 :
2251 762 : if (!less_than_bitsize1 ("i", i, "pos", pos, false))
2252 : return false;
2253 :
2254 : return true;
2255 : }
2256 :
2257 :
2258 : bool
2259 1073 : gfc_check_char (gfc_expr *i, gfc_expr *kind)
2260 : {
2261 1073 : if (i->ts.type == BT_BOZ)
2262 : {
2263 0 : if (gfc_invalid_boz (G_("BOZ literal constant at %L cannot appear in "
2264 : "CHAR intrinsic subprogram"), &i->where))
2265 : return false;
2266 :
2267 0 : if (!gfc_boz2int (i, gfc_default_integer_kind))
2268 : return false;
2269 : }
2270 :
2271 1073 : if (!type_check (i, 0, BT_INTEGER))
2272 : return false;
2273 :
2274 1073 : if (!kind_check (kind, 1, BT_CHARACTER))
2275 : return false;
2276 :
2277 : return true;
2278 : }
2279 :
2280 :
2281 : bool
2282 5 : gfc_check_chdir (gfc_expr *dir)
2283 : {
2284 5 : if (!type_check (dir, 0, BT_CHARACTER))
2285 : return false;
2286 5 : if (!kind_value_check (dir, 0, gfc_default_character_kind))
2287 : return false;
2288 :
2289 : return true;
2290 : }
2291 :
2292 :
2293 : bool
2294 11 : gfc_check_chdir_sub (gfc_expr *dir, gfc_expr *status)
2295 : {
2296 11 : if (!type_check (dir, 0, BT_CHARACTER))
2297 : return false;
2298 11 : if (!kind_value_check (dir, 0, gfc_default_character_kind))
2299 : return false;
2300 :
2301 9 : if (status == NULL)
2302 : return true;
2303 :
2304 7 : if (!type_check (status, 1, BT_INTEGER))
2305 : return false;
2306 7 : if (!scalar_check (status, 1))
2307 : return false;
2308 :
2309 : return true;
2310 : }
2311 :
2312 :
2313 : bool
2314 40 : gfc_check_chmod (gfc_expr *name, gfc_expr *mode)
2315 : {
2316 40 : if (!type_check (name, 0, BT_CHARACTER))
2317 : return false;
2318 40 : if (!kind_value_check (name, 0, gfc_default_character_kind))
2319 : return false;
2320 :
2321 38 : if (!type_check (mode, 1, BT_CHARACTER))
2322 : return false;
2323 38 : if (!kind_value_check (mode, 1, gfc_default_character_kind))
2324 : return false;
2325 :
2326 : return true;
2327 : }
2328 :
2329 :
2330 : bool
2331 20 : gfc_check_chmod_sub (gfc_expr *name, gfc_expr *mode, gfc_expr *status)
2332 : {
2333 20 : if (!type_check (name, 0, BT_CHARACTER))
2334 : return false;
2335 20 : if (!kind_value_check (name, 0, gfc_default_character_kind))
2336 : return false;
2337 :
2338 16 : if (!type_check (mode, 1, BT_CHARACTER))
2339 : return false;
2340 16 : if (!kind_value_check (mode, 1, gfc_default_character_kind))
2341 : return false;
2342 :
2343 14 : if (status == NULL)
2344 : return true;
2345 :
2346 13 : if (!type_check (status, 2, BT_INTEGER))
2347 : return false;
2348 :
2349 13 : if (!scalar_check (status, 2))
2350 : return false;
2351 :
2352 : return true;
2353 : }
2354 :
2355 :
2356 : bool
2357 2230 : gfc_check_cmplx (gfc_expr *x, gfc_expr *y, gfc_expr *kind)
2358 : {
2359 2230 : int k;
2360 :
2361 : /* Check kind first, because it may be needed in conversion of a BOZ. */
2362 2230 : if (kind)
2363 : {
2364 1251 : if (!kind_check (kind, 2, BT_COMPLEX))
2365 : return false;
2366 1251 : gfc_extract_int (kind, &k);
2367 : }
2368 : else
2369 979 : k = gfc_default_complex_kind;
2370 :
2371 2230 : if (x->ts.type == BT_BOZ && !gfc_boz2real (x, k))
2372 : return false;
2373 :
2374 2230 : if (!numeric_check (x, 0))
2375 : return false;
2376 :
2377 2230 : if (y != NULL)
2378 : {
2379 2077 : if (y->ts.type == BT_BOZ && !gfc_boz2real (y, k))
2380 : return false;
2381 :
2382 2077 : if (!numeric_check (y, 1))
2383 : return false;
2384 :
2385 2077 : if (x->ts.type == BT_COMPLEX)
2386 : {
2387 0 : gfc_error ("%qs argument of %qs intrinsic at %L must not be "
2388 : "present if %<x%> is COMPLEX",
2389 0 : gfc_current_intrinsic_arg[1]->name, gfc_current_intrinsic,
2390 : &y->where);
2391 0 : return false;
2392 : }
2393 :
2394 2077 : if (y->ts.type == BT_COMPLEX)
2395 : {
2396 1 : gfc_error ("%qs argument of %qs intrinsic at %L must have a type "
2397 : "of either REAL or INTEGER",
2398 1 : gfc_current_intrinsic_arg[1]->name, gfc_current_intrinsic,
2399 : &y->where);
2400 1 : return false;
2401 : }
2402 : }
2403 :
2404 2229 : if (!kind && warn_conversion
2405 6 : && x->ts.type == BT_REAL && x->ts.kind > gfc_default_real_kind)
2406 2 : gfc_warning_now (OPT_Wconversion, "Conversion from %s to default-kind "
2407 : "COMPLEX(%d) at %L might lose precision, consider using "
2408 : "the KIND argument", gfc_typename (&x->ts),
2409 : gfc_default_real_kind, &x->where);
2410 2227 : else if (y && !kind && warn_conversion
2411 4 : && y->ts.type == BT_REAL && y->ts.kind > gfc_default_real_kind)
2412 1 : gfc_warning_now (OPT_Wconversion, "Conversion from %s to default-kind "
2413 : "COMPLEX(%d) at %L might lose precision, consider using "
2414 : "the KIND argument", gfc_typename (&y->ts),
2415 : gfc_default_real_kind, &y->where);
2416 : return true;
2417 : }
2418 :
2419 :
2420 : static bool
2421 205 : check_co_collective (gfc_expr *a, gfc_expr *image_idx, gfc_expr *stat,
2422 : gfc_expr *errmsg, bool co_reduce)
2423 : {
2424 205 : if (!variable_check (a, 0, false))
2425 : return false;
2426 :
2427 200 : if (!gfc_check_vardef_context (a, false, false, false, "argument 'A' with "
2428 : "INTENT(INOUT)"))
2429 : return false;
2430 :
2431 : /* Fortran 2008, 12.5.2.4, paragraph 18. */
2432 199 : if (gfc_has_vector_subscript (a))
2433 : {
2434 4 : gfc_error ("Argument %<A%> with INTENT(INOUT) at %L of the intrinsic "
2435 : "subroutine %s shall not have a vector subscript",
2436 : &a->where, gfc_current_intrinsic);
2437 4 : return false;
2438 : }
2439 :
2440 195 : if (gfc_is_coindexed (a))
2441 : {
2442 5 : gfc_error ("The A argument at %L to the intrinsic %s shall not be "
2443 : "coindexed", &a->where, gfc_current_intrinsic);
2444 5 : return false;
2445 : }
2446 :
2447 190 : if (image_idx != NULL)
2448 : {
2449 174 : if (!type_check (image_idx, co_reduce ? 2 : 1, BT_INTEGER))
2450 : return false;
2451 89 : if (!scalar_check (image_idx, co_reduce ? 2 : 1))
2452 : return false;
2453 : }
2454 :
2455 184 : if (stat != NULL)
2456 : {
2457 181 : if (!type_check (stat, co_reduce ? 3 : 2, BT_INTEGER))
2458 : return false;
2459 95 : if (!scalar_check (stat, co_reduce ? 3 : 2))
2460 : return false;
2461 92 : if (!variable_check (stat, co_reduce ? 3 : 2, false))
2462 : return false;
2463 89 : if (stat->ts.kind != 4)
2464 : {
2465 3 : gfc_error ("The stat= argument at %L must be a kind=4 integer "
2466 : "variable", &stat->where);
2467 3 : return false;
2468 : }
2469 : }
2470 :
2471 172 : if (errmsg != NULL)
2472 : {
2473 124 : if (!type_check (errmsg, co_reduce ? 4 : 3, BT_CHARACTER))
2474 : return false;
2475 63 : if (!scalar_check (errmsg, co_reduce ? 4 : 3))
2476 : return false;
2477 60 : if (!variable_check (errmsg, co_reduce ? 4 : 3, false))
2478 : return false;
2479 57 : if (errmsg->ts.kind != 1)
2480 : {
2481 3 : gfc_error ("The errmsg= argument at %L must be a default-kind "
2482 : "character variable", &errmsg->where);
2483 3 : return false;
2484 : }
2485 : }
2486 :
2487 160 : if (flag_coarray == GFC_FCOARRAY_NONE)
2488 : {
2489 1 : gfc_fatal_error ("Coarrays disabled at %L, use %<-fcoarray=%> to enable",
2490 : &a->where);
2491 : return false;
2492 : }
2493 :
2494 : return true;
2495 : }
2496 :
2497 :
2498 : bool
2499 62 : gfc_check_co_broadcast (gfc_expr *a, gfc_expr *source_image, gfc_expr *stat,
2500 : gfc_expr *errmsg)
2501 : {
2502 62 : if (a->ts.type == BT_CLASS || gfc_expr_attr (a).alloc_comp)
2503 : {
2504 0 : gfc_error ("Support for the A argument at %L which is polymorphic A "
2505 : "argument or has allocatable components is not yet "
2506 : "implemented", &a->where);
2507 0 : return false;
2508 : }
2509 62 : return check_co_collective (a, source_image, stat, errmsg, false);
2510 : }
2511 :
2512 :
2513 : /* Helper function for character arguments in gfc_check_[co_]reduce. */
2514 :
2515 : static unsigned long
2516 179 : get_ul_from_cst_cl (const gfc_charlen *cl)
2517 : {
2518 179 : return cl && cl->length && cl->length->expr_type == EXPR_CONSTANT
2519 355 : ? mpz_get_ui (cl->length->value.integer) : 0;
2520 : }
2521 :
2522 :
2523 : /* Checks shared between co_reduce and reduce. */
2524 : static bool
2525 316 : check_operation (gfc_expr *op, gfc_expr *a, bool is_co_reduce)
2526 : {
2527 316 : symbol_attribute attr;
2528 316 : gfc_formal_arglist *formal;
2529 316 : gfc_symbol *sym;
2530 :
2531 316 : if (!gfc_resolve_expr (op))
2532 : return false;
2533 :
2534 316 : attr = gfc_expr_attr (op);
2535 316 : if (!attr.pure || !attr.function)
2536 : {
2537 9 : gfc_error ("OPERATION argument at %L must be a PURE function",
2538 : &op->where);
2539 9 : return false;
2540 : }
2541 :
2542 307 : if (attr.intrinsic)
2543 : {
2544 : /* None of the intrinsics fulfills the criteria of taking two arguments,
2545 : returning the same type and kind as the arguments and being permitted
2546 : as actual argument. */
2547 1 : gfc_error ("Intrinsic function %s at %L is not permitted for %s",
2548 1 : op->symtree->n.sym->name, &op->where,
2549 : is_co_reduce ? "CO_REDUCE" : "REDUCE");
2550 1 : return false;
2551 : }
2552 :
2553 306 : if (gfc_is_proc_ptr_comp (op))
2554 : {
2555 16 : gfc_component *comp = gfc_get_proc_ptr_comp (op);
2556 16 : sym = comp->ts.interface;
2557 : }
2558 : else
2559 290 : sym = op->symtree->n.sym;
2560 :
2561 306 : formal = sym->formal;
2562 :
2563 306 : if (!formal || !formal->next || formal->next->next)
2564 : {
2565 6 : gfc_error ("The function passed as OPERATION at %L shall have two "
2566 : "arguments", &op->where);
2567 6 : return false;
2568 : }
2569 :
2570 300 : if (sym->result->ts.type == BT_UNKNOWN)
2571 0 : gfc_set_default_type (sym->result, 0, NULL);
2572 :
2573 300 : if (!gfc_compare_types (&a->ts, &sym->result->ts))
2574 : {
2575 5 : gfc_error ("The %s argument at %L has type %s but the function passed "
2576 : "as OPERATION at %L returns %s",
2577 : is_co_reduce ? "A" : "ARRAY",
2578 : &a->where, gfc_typename (a), &op->where,
2579 5 : gfc_typename (&sym->result->ts));
2580 5 : return false;
2581 : }
2582 :
2583 295 : if (!gfc_compare_types (&a->ts, &formal->sym->ts)
2584 295 : || !gfc_compare_types (&a->ts, &formal->next->sym->ts))
2585 : {
2586 0 : gfc_error ("The function passed as OPERATION at %L has arguments of type "
2587 : "%s and %s but shall have type %s", &op->where,
2588 0 : gfc_typename (&formal->sym->ts),
2589 0 : gfc_typename (&formal->next->sym->ts), gfc_typename (a));
2590 0 : return false;
2591 : }
2592 295 : if (op->rank || attr.allocatable || attr.pointer || formal->sym->as
2593 290 : || formal->next->sym->as || formal->sym->attr.allocatable
2594 288 : || formal->next->sym->attr.allocatable || formal->sym->attr.pointer
2595 286 : || formal->next->sym->attr.pointer)
2596 : {
2597 9 : gfc_error ("The function passed as OPERATION at %L shall have scalar "
2598 : "nonallocatable nonpointer arguments and return a "
2599 : "nonallocatable nonpointer scalar", &op->where);
2600 9 : return false;
2601 : }
2602 :
2603 286 : if (formal->sym->attr.value != formal->next->sym->attr.value)
2604 : {
2605 3 : gfc_error ("The function passed as OPERATION at %L shall have the VALUE "
2606 : "attribute either for none or both arguments", &op->where);
2607 3 : return false;
2608 : }
2609 :
2610 283 : if (formal->sym->attr.target != formal->next->sym->attr.target)
2611 : {
2612 2 : gfc_error ("The function passed as OPERATION at %L shall have the TARGET "
2613 : "attribute either for none or both arguments", &op->where);
2614 2 : return false;
2615 : }
2616 :
2617 281 : if (formal->sym->attr.asynchronous != formal->next->sym->attr.asynchronous)
2618 : {
2619 2 : gfc_error ("The function passed as OPERATION at %L shall have the "
2620 : "ASYNCHRONOUS attribute either for none or both arguments",
2621 : &op->where);
2622 2 : return false;
2623 : }
2624 :
2625 279 : if (formal->sym->attr.optional || formal->next->sym->attr.optional)
2626 : {
2627 3 : gfc_error ("The function passed as OPERATION at %L shall not have the "
2628 : "OPTIONAL attribute for either of the arguments", &op->where);
2629 3 : return false;
2630 : }
2631 :
2632 276 : if (a->ts.type == BT_CHARACTER)
2633 : {
2634 32 : unsigned long actual_size, formal_size1, formal_size2, result_size;
2635 :
2636 32 : actual_size = get_ul_from_cst_cl (a->ts.u.cl);
2637 32 : formal_size1 = get_ul_from_cst_cl (formal->sym->ts.u.cl);
2638 32 : formal_size2 = get_ul_from_cst_cl (formal->next->sym->ts.u.cl);
2639 32 : result_size = get_ul_from_cst_cl (sym->ts.u.cl);
2640 :
2641 32 : if (actual_size
2642 29 : && ((formal_size1 && actual_size != formal_size1)
2643 24 : || (formal_size2 && actual_size != formal_size2)))
2644 : {
2645 5 : gfc_error ("The character length of the %s argument at %L and of "
2646 : "the arguments of the OPERATION at %L shall be the same",
2647 : is_co_reduce ? "A" : "ARRAY", &a->where, &op->where);
2648 5 : return false;
2649 : }
2650 :
2651 27 : if (actual_size && result_size && actual_size != result_size)
2652 : {
2653 3 : gfc_error ("The character length of the %s argument at %L and of "
2654 : "the function result of the OPERATION at %L shall be the "
2655 : "same", is_co_reduce ? "A" : "ARRAY",
2656 : &a->where, &op->where);
2657 3 : return false;
2658 : }
2659 : }
2660 : return true;
2661 : }
2662 :
2663 :
2664 : bool
2665 73 : gfc_check_co_reduce (gfc_expr *a, gfc_expr *op, gfc_expr *result_image,
2666 : gfc_expr *stat, gfc_expr *errmsg)
2667 : {
2668 73 : if (a->ts.type == BT_CLASS)
2669 : {
2670 0 : gfc_error ("The A argument at %L of CO_REDUCE shall not be polymorphic",
2671 : &a->where);
2672 0 : return false;
2673 : }
2674 :
2675 73 : if (gfc_expr_attr (a).alloc_comp)
2676 : {
2677 0 : gfc_error ("Support for the A argument at %L with allocatable components"
2678 : " is not yet implemented", &a->where);
2679 0 : return false;
2680 : }
2681 :
2682 73 : if (!check_co_collective (a, result_image, stat, errmsg, true))
2683 : return false;
2684 :
2685 60 : if (!check_operation (op, a, true))
2686 : return false;
2687 :
2688 : return true;
2689 : }
2690 :
2691 :
2692 : bool
2693 40 : gfc_check_co_minmax (gfc_expr *a, gfc_expr *result_image, gfc_expr *stat,
2694 : gfc_expr *errmsg)
2695 : {
2696 40 : if (a->ts.type != BT_INTEGER && a->ts.type != BT_REAL
2697 : && a->ts.type != BT_CHARACTER)
2698 : {
2699 2 : gfc_error ("%qs argument of %qs intrinsic at %L shall be of type "
2700 : "integer, real or character",
2701 2 : gfc_current_intrinsic_arg[0]->name, gfc_current_intrinsic,
2702 : &a->where);
2703 2 : return false;
2704 : }
2705 38 : return check_co_collective (a, result_image, stat, errmsg, false);
2706 : }
2707 :
2708 :
2709 : bool
2710 33 : gfc_check_co_sum (gfc_expr *a, gfc_expr *result_image, gfc_expr *stat,
2711 : gfc_expr *errmsg)
2712 : {
2713 33 : if (!numeric_check (a, 0))
2714 : return false;
2715 32 : return check_co_collective (a, result_image, stat, errmsg, false);
2716 : }
2717 :
2718 :
2719 : bool
2720 56 : gfc_check_complex (gfc_expr *x, gfc_expr *y)
2721 : {
2722 56 : if (!boz_args_check (x, y))
2723 : return false;
2724 :
2725 : /* COMPLEX is an extension, we do not want UNSIGNED there. */
2726 55 : if (x->ts.type == BT_UNSIGNED)
2727 : {
2728 1 : gfc_error ("%qs argument of %qs intrinsic at %L shall not be "
2729 1 : "UNSIGNED", gfc_current_intrinsic_arg[0]->name,
2730 : gfc_current_intrinsic, &x->where);
2731 1 : return false;
2732 : }
2733 :
2734 54 : if (y->ts.type == BT_UNSIGNED)
2735 : {
2736 1 : gfc_error ("%qs argument of %qs intrinsic at %L shall not be "
2737 1 : "UNSIGNED", gfc_current_intrinsic_arg[1]->name,
2738 : gfc_current_intrinsic, &y->where);
2739 1 : return false;
2740 : }
2741 :
2742 53 : if (x->ts.type == BT_BOZ)
2743 : {
2744 16 : if (gfc_invalid_boz (G_("BOZ constant at %L cannot appear in the COMPLEX"
2745 : " intrinsic subprogram"), &x->where))
2746 : {
2747 2 : reset_boz (x);
2748 2 : return false;
2749 : }
2750 14 : if (y->ts.type == BT_INTEGER && !gfc_boz2int (x, y->ts.kind))
2751 : return false;
2752 14 : if (y->ts.type == BT_REAL && !gfc_boz2real (x, y->ts.kind))
2753 : return false;
2754 : }
2755 :
2756 51 : if (y->ts.type == BT_BOZ)
2757 : {
2758 0 : if (gfc_invalid_boz (G_("BOZ constant at %L cannot appear in the COMPLEX"
2759 : " intrinsic subprogram"), &y->where))
2760 : {
2761 0 : reset_boz (y);
2762 0 : return false;
2763 : }
2764 0 : if (x->ts.type == BT_INTEGER && !gfc_boz2int (y, x->ts.kind))
2765 : return false;
2766 0 : if (x->ts.type == BT_REAL && !gfc_boz2real (y, x->ts.kind))
2767 : return false;
2768 : }
2769 :
2770 51 : if (!int_or_real_check (x, 0))
2771 : return false;
2772 50 : if (!scalar_check (x, 0))
2773 : return false;
2774 :
2775 50 : if (!int_or_real_check (y, 1))
2776 : return false;
2777 49 : if (!scalar_check (y, 1))
2778 : return false;
2779 :
2780 : return true;
2781 : }
2782 :
2783 :
2784 : bool
2785 18 : gfc_check_coshape (gfc_expr *coarray, gfc_expr *kind)
2786 : {
2787 18 : if (flag_coarray == GFC_FCOARRAY_NONE)
2788 : {
2789 0 : gfc_fatal_error ("Coarrays disabled at %L, use %<-fcoarray=%> to enable",
2790 : gfc_current_intrinsic_where);
2791 : return false;
2792 : }
2793 :
2794 18 : if (!coarray_check (coarray, 0))
2795 : return false;
2796 :
2797 18 : if (!kind_check (kind, 2, BT_INTEGER))
2798 : return false;
2799 :
2800 : return true;
2801 : }
2802 :
2803 :
2804 : bool
2805 361 : gfc_check_count (gfc_expr *mask, gfc_expr *dim, gfc_expr *kind)
2806 : {
2807 361 : if (!logical_array_check (mask, 0))
2808 : return false;
2809 361 : if (!dim_check (dim, 1, false))
2810 : return false;
2811 361 : if (!dim_rank_check (dim, mask, 0))
2812 : return false;
2813 361 : if (!kind_check (kind, 2, BT_INTEGER))
2814 : return false;
2815 361 : if (kind && !gfc_notify_std (GFC_STD_F2003, "%qs intrinsic "
2816 : "with KIND argument at %L",
2817 : gfc_current_intrinsic, &kind->where))
2818 : return false;
2819 :
2820 : return true;
2821 : }
2822 :
2823 :
2824 : bool
2825 714 : gfc_check_cshift (gfc_expr *array, gfc_expr *shift, gfc_expr *dim)
2826 : {
2827 714 : if (!array_check (array, 0))
2828 : return false;
2829 :
2830 714 : if (!type_check (shift, 1, BT_INTEGER))
2831 : return false;
2832 :
2833 713 : if (!dim_check (dim, 2, true))
2834 : return false;
2835 :
2836 713 : if (!dim_rank_check (dim, array, false))
2837 : return false;
2838 :
2839 713 : if (array->rank == 1 || shift->rank == 0)
2840 : {
2841 515 : if (!scalar_check (shift, 1))
2842 : return false;
2843 : }
2844 198 : else if (shift->rank == array->rank - 1)
2845 : {
2846 197 : int d;
2847 197 : if (!dim)
2848 48 : d = 1;
2849 149 : else if (dim->expr_type == EXPR_CONSTANT)
2850 118 : gfc_extract_int (dim, &d);
2851 : else
2852 31 : d = -1;
2853 :
2854 197 : if (d > 0)
2855 : {
2856 : int i, j;
2857 553 : for (i = 0, j = 0; i < array->rank; i++)
2858 387 : if (i != d - 1)
2859 : {
2860 221 : if (!identical_dimen_shape (array, i, shift, j))
2861 : {
2862 0 : gfc_error ("%qs argument of %qs intrinsic at %L has "
2863 : "invalid shape in dimension %d (%ld/%ld)",
2864 0 : gfc_current_intrinsic_arg[1]->name,
2865 : gfc_current_intrinsic, &shift->where, i + 1,
2866 0 : mpz_get_si (array->shape[i]),
2867 0 : mpz_get_si (shift->shape[j]));
2868 0 : return false;
2869 : }
2870 :
2871 221 : j += 1;
2872 : }
2873 : }
2874 : }
2875 : else
2876 : {
2877 1 : gfc_error ("%qs argument of intrinsic %qs at %L of must have rank "
2878 1 : "%d or be a scalar", gfc_current_intrinsic_arg[1]->name,
2879 : gfc_current_intrinsic, &shift->where, array->rank - 1);
2880 1 : return false;
2881 : }
2882 :
2883 : return true;
2884 : }
2885 :
2886 :
2887 : bool
2888 0 : gfc_check_ctime (gfc_expr *time)
2889 : {
2890 0 : if (!scalar_check (time, 0))
2891 : return false;
2892 :
2893 0 : if (!type_check (time, 0, BT_INTEGER))
2894 : return false;
2895 :
2896 : return true;
2897 : }
2898 :
2899 :
2900 471 : bool gfc_check_datan2 (gfc_expr *y, gfc_expr *x)
2901 : {
2902 471 : if (!double_check (y, 0) || !double_check (x, 1))
2903 283 : return false;
2904 :
2905 : return true;
2906 : }
2907 :
2908 : bool
2909 163 : gfc_check_dcmplx (gfc_expr *x, gfc_expr *y)
2910 : {
2911 163 : if (x->ts.type == BT_BOZ && !gfc_boz2real (x, gfc_default_double_kind))
2912 : return false;
2913 :
2914 163 : if (!numeric_check (x, 0))
2915 : return false;
2916 :
2917 163 : if (y != NULL)
2918 : {
2919 162 : if (y->ts.type == BT_BOZ && !gfc_boz2real (y, gfc_default_double_kind))
2920 : return false;
2921 :
2922 162 : if (!numeric_check (y, 1))
2923 : return false;
2924 :
2925 162 : if (x->ts.type == BT_COMPLEX)
2926 : {
2927 0 : gfc_error ("%qs argument of %qs intrinsic at %L must not be "
2928 : "present if %<x%> is COMPLEX",
2929 0 : gfc_current_intrinsic_arg[1]->name, gfc_current_intrinsic,
2930 : &y->where);
2931 0 : return false;
2932 : }
2933 :
2934 162 : if (y->ts.type == BT_COMPLEX)
2935 : {
2936 1 : gfc_error ("%qs argument of %qs intrinsic at %L must have a type "
2937 : "of either REAL or INTEGER",
2938 1 : gfc_current_intrinsic_arg[1]->name, gfc_current_intrinsic,
2939 : &y->where);
2940 1 : return false;
2941 : }
2942 : }
2943 :
2944 : return true;
2945 : }
2946 :
2947 :
2948 : bool
2949 229 : gfc_check_dble (gfc_expr *x)
2950 : {
2951 229 : if (x->ts.type == BT_BOZ && !gfc_boz2real (x, gfc_default_double_kind))
2952 : return false;
2953 :
2954 229 : if (!numeric_check (x, 0))
2955 : return false;
2956 :
2957 : return true;
2958 : }
2959 :
2960 :
2961 : bool
2962 40 : gfc_check_digits (gfc_expr *x)
2963 : {
2964 :
2965 40 : if (flag_unsigned)
2966 : {
2967 6 : if (!int_or_real_or_unsigned_check (x, 0))
2968 : return false;
2969 : }
2970 34 : else if (!int_or_real_check (x, 0))
2971 : return false;
2972 :
2973 : return true;
2974 : }
2975 :
2976 :
2977 : bool
2978 185 : gfc_check_dot_product (gfc_expr *vector_a, gfc_expr *vector_b)
2979 : {
2980 185 : switch (vector_a->ts.type)
2981 : {
2982 36 : case BT_LOGICAL:
2983 36 : if (!type_check (vector_b, 1, BT_LOGICAL))
2984 : return false;
2985 : break;
2986 :
2987 137 : case BT_INTEGER:
2988 137 : case BT_REAL:
2989 137 : case BT_COMPLEX:
2990 137 : if (!numeric_check (vector_b, 1))
2991 : return false;
2992 : break;
2993 :
2994 : case BT_UNSIGNED:
2995 : /* Check comes later. */
2996 : break;
2997 :
2998 0 : default:
2999 0 : gfc_error ("%qs argument of %qs intrinsic at %L must be numeric "
3000 0 : "or LOGICAL", gfc_current_intrinsic_arg[0]->name,
3001 : gfc_current_intrinsic, &vector_a->where);
3002 0 : return false;
3003 : }
3004 :
3005 185 : if (gfc_invalid_unsigned_ops (vector_a, vector_b))
3006 : {
3007 0 : gfc_error ("Argument types of %qs intrinsic at %L must match (%s/%s)",
3008 : gfc_current_intrinsic, &vector_a->where,
3009 : gfc_typename(&vector_a->ts), gfc_typename(&vector_b->ts));
3010 0 : return false;
3011 : }
3012 :
3013 185 : if (!rank_check (vector_a, 0, 1))
3014 : return false;
3015 :
3016 185 : if (!rank_check (vector_b, 1, 1))
3017 : return false;
3018 :
3019 185 : if (! identical_dimen_shape (vector_a, 0, vector_b, 0))
3020 : {
3021 1 : gfc_error ("Different shape for arguments %qs and %qs at %L for "
3022 : "intrinsic %<dot_product%>",
3023 1 : gfc_current_intrinsic_arg[0]->name,
3024 1 : gfc_current_intrinsic_arg[1]->name, &vector_a->where);
3025 1 : return false;
3026 : }
3027 :
3028 : return true;
3029 : }
3030 :
3031 :
3032 : bool
3033 22 : gfc_check_dprod (gfc_expr *x, gfc_expr *y)
3034 : {
3035 22 : if (!type_check (x, 0, BT_REAL)
3036 22 : || !type_check (y, 1, BT_REAL))
3037 : return false;
3038 :
3039 22 : if (x->ts.kind != gfc_default_real_kind)
3040 : {
3041 1 : gfc_error ("%qs argument of %qs intrinsic at %L must be default "
3042 1 : "real", gfc_current_intrinsic_arg[0]->name,
3043 : gfc_current_intrinsic, &x->where);
3044 1 : return false;
3045 : }
3046 :
3047 21 : if (y->ts.kind != gfc_default_real_kind)
3048 : {
3049 1 : gfc_error ("%qs argument of %qs intrinsic at %L must be default "
3050 1 : "real", gfc_current_intrinsic_arg[1]->name,
3051 : gfc_current_intrinsic, &y->where);
3052 1 : return false;
3053 : }
3054 :
3055 : return true;
3056 : }
3057 :
3058 : bool
3059 1644 : gfc_check_dshift (gfc_expr *i, gfc_expr *j, gfc_expr *shift)
3060 : {
3061 : /* i and j cannot both be BOZ literal constants. */
3062 1644 : if (!boz_args_check (i, j))
3063 : return false;
3064 :
3065 1640 : if (i->ts.type == BT_BOZ)
3066 : {
3067 17 : if (j->ts.type == BT_INTEGER)
3068 : {
3069 16 : if (!gfc_boz2int (i, j->ts.kind))
3070 : return false;
3071 : }
3072 1 : else if (flag_unsigned && j->ts.type == BT_UNSIGNED)
3073 : {
3074 0 : if (!gfc_boz2uint (i, j->ts.kind))
3075 : return false;
3076 : }
3077 : else
3078 1 : reset_boz (i);
3079 : }
3080 :
3081 1640 : if (j->ts.type == BT_BOZ)
3082 : {
3083 15 : if (i->ts.type == BT_INTEGER)
3084 : {
3085 14 : if (!gfc_boz2int (j, i->ts.kind))
3086 : return false;
3087 : }
3088 1 : else if (flag_unsigned && i->ts.type == BT_UNSIGNED)
3089 : {
3090 0 : if (!gfc_boz2uint (j, i->ts.kind))
3091 : return false;
3092 : }
3093 : else
3094 1 : reset_boz (j);
3095 : }
3096 :
3097 1640 : if (flag_unsigned)
3098 : {
3099 96 : if (!type_check2 (i, 0, BT_INTEGER, BT_UNSIGNED))
3100 : return false;
3101 :
3102 96 : if (!type_check2 (j, 1, BT_INTEGER, BT_UNSIGNED))
3103 : return false;
3104 : }
3105 : else
3106 : {
3107 1544 : if (!type_check (i, 0, BT_INTEGER))
3108 : return false;
3109 :
3110 1543 : if (!type_check (j, 1, BT_INTEGER))
3111 : return false;
3112 : }
3113 :
3114 1638 : if (!same_type_check (i, 0, j, 1))
3115 : return false;
3116 :
3117 1634 : if (!type_check (shift, 2, BT_INTEGER))
3118 : return false;
3119 :
3120 1634 : if (!nonnegative_check ("SHIFT", shift))
3121 : return false;
3122 :
3123 1632 : if (!less_than_bitsize1 ("I", i, "SHIFT", shift, true))
3124 : return false;
3125 :
3126 : return true;
3127 : }
3128 :
3129 :
3130 : bool
3131 1164 : gfc_check_eoshift (gfc_expr *array, gfc_expr *shift, gfc_expr *boundary,
3132 : gfc_expr *dim)
3133 : {
3134 1164 : int d;
3135 :
3136 1164 : if (!array_check (array, 0))
3137 : return false;
3138 :
3139 1164 : if (!type_check (shift, 1, BT_INTEGER))
3140 : return false;
3141 :
3142 1162 : if (!dim_check (dim, 3, true))
3143 : return false;
3144 :
3145 1162 : if (!dim_rank_check (dim, array, false))
3146 : return false;
3147 :
3148 1162 : if (!dim)
3149 464 : d = 1;
3150 698 : else if (dim->expr_type == EXPR_CONSTANT)
3151 622 : gfc_extract_int (dim, &d);
3152 : else
3153 76 : d = -1;
3154 :
3155 1162 : if (array->rank == 1 || shift->rank == 0)
3156 : {
3157 771 : if (!scalar_check (shift, 1))
3158 : return false;
3159 : }
3160 391 : else if (shift->rank == array->rank - 1)
3161 : {
3162 390 : if (d > 0)
3163 : {
3164 : int i, j;
3165 1139 : for (i = 0, j = 0; i < array->rank; i++)
3166 793 : if (i != d - 1)
3167 : {
3168 446 : if (!identical_dimen_shape (array, i, shift, j))
3169 : {
3170 1 : gfc_error ("%qs argument of %qs intrinsic at %L has "
3171 : "invalid shape in dimension %d (%ld/%ld)",
3172 1 : gfc_current_intrinsic_arg[1]->name,
3173 : gfc_current_intrinsic, &shift->where, i + 1,
3174 1 : mpz_get_si (array->shape[i]),
3175 1 : mpz_get_si (shift->shape[j]));
3176 1 : return false;
3177 : }
3178 :
3179 445 : j += 1;
3180 : }
3181 : }
3182 : }
3183 : else
3184 : {
3185 1 : gfc_error ("%qs argument of intrinsic %qs at %L of must have rank "
3186 1 : "%d or be a scalar", gfc_current_intrinsic_arg[1]->name,
3187 : gfc_current_intrinsic, &shift->where, array->rank - 1);
3188 1 : return false;
3189 : }
3190 :
3191 1158 : if (boundary != NULL)
3192 : {
3193 657 : if (!same_type_check (array, 0, boundary, 2))
3194 : return false;
3195 :
3196 : /* Reject unequal string lengths and emit a better error message than
3197 : gfc_check_same_strlen would. */
3198 657 : if (array->ts.type == BT_CHARACTER)
3199 : {
3200 250 : ssize_t len_a, len_b;
3201 :
3202 250 : len_a = gfc_var_strlen (array);
3203 250 : len_b = gfc_var_strlen (boundary);
3204 250 : if (len_a != -1 && len_b != -1 && len_a != len_b)
3205 : {
3206 1 : gfc_error ("%qs must be of same type and kind as %qs at %L in %qs",
3207 1 : gfc_current_intrinsic_arg[2]->name,
3208 1 : gfc_current_intrinsic_arg[0]->name,
3209 : &boundary->where, gfc_current_intrinsic);
3210 1 : return false;
3211 : }
3212 : }
3213 :
3214 656 : if (array->rank == 1 || boundary->rank == 0)
3215 : {
3216 402 : if (!scalar_check (boundary, 2))
3217 : return false;
3218 : }
3219 254 : else if (boundary->rank == array->rank - 1)
3220 : {
3221 252 : if (d > 0)
3222 : {
3223 : int i,j;
3224 786 : for (i = 0, j = 0; i < array->rank; i++)
3225 : {
3226 542 : if (i != d - 1)
3227 : {
3228 297 : if (!identical_dimen_shape (array, i, boundary, j))
3229 : {
3230 1 : gfc_error ("%qs argument of %qs intrinsic at %L has "
3231 : "invalid shape in dimension %d (%ld/%ld)",
3232 1 : gfc_current_intrinsic_arg[2]->name,
3233 : gfc_current_intrinsic, &shift->where, i+1,
3234 1 : mpz_get_si (array->shape[i]),
3235 1 : mpz_get_si (boundary->shape[j]));
3236 1 : return false;
3237 : }
3238 296 : j += 1;
3239 : }
3240 : }
3241 : }
3242 : }
3243 : else
3244 : {
3245 2 : gfc_error ("%qs argument of intrinsic %qs at %L of must have "
3246 : "rank %d or be a scalar",
3247 2 : gfc_current_intrinsic_arg[1]->name, gfc_current_intrinsic,
3248 : &shift->where, array->rank - 1);
3249 2 : return false;
3250 : }
3251 : }
3252 : else
3253 : {
3254 501 : switch (array->ts.type)
3255 : {
3256 : case BT_INTEGER:
3257 : case BT_LOGICAL:
3258 : case BT_REAL:
3259 : case BT_COMPLEX:
3260 : case BT_CHARACTER:
3261 : break;
3262 :
3263 12 : case BT_UNSIGNED:
3264 12 : if (flag_unsigned)
3265 : break;
3266 :
3267 1 : gcc_fallthrough();
3268 :
3269 1 : default:
3270 1 : gfc_error ("Missing %qs argument to %qs intrinsic at %L for %qs "
3271 1 : "of type %qs", gfc_current_intrinsic_arg[2]->name,
3272 : gfc_current_intrinsic, &array->where,
3273 1 : gfc_current_intrinsic_arg[0]->name,
3274 : gfc_typename (array));
3275 1 : return false;
3276 : }
3277 : }
3278 :
3279 : return true;
3280 : }
3281 :
3282 :
3283 : bool
3284 152 : gfc_check_float (gfc_expr *a)
3285 : {
3286 152 : if (a->ts.type == BT_BOZ)
3287 : {
3288 8 : if (gfc_invalid_boz (G_("BOZ literal constant at %L cannot appear in the"
3289 : " FLOAT intrinsic subprogram"), &a->where))
3290 : {
3291 1 : reset_boz (a);
3292 1 : return false;
3293 : }
3294 7 : if (!gfc_boz2int (a, gfc_default_integer_kind))
3295 : return false;
3296 : }
3297 :
3298 151 : if (!type_check (a, 0, BT_INTEGER))
3299 : return false;
3300 :
3301 151 : if ((a->ts.kind != gfc_default_integer_kind)
3302 151 : && !gfc_notify_std (GFC_STD_GNU, "non-default INTEGER "
3303 : "kind argument to %s intrinsic at %L",
3304 : gfc_current_intrinsic, &a->where))
3305 : return false;
3306 :
3307 : return true;
3308 : }
3309 :
3310 : /* A single complex argument. */
3311 :
3312 : bool
3313 749 : gfc_check_fn_c (gfc_expr *a)
3314 : {
3315 749 : if (!type_check (a, 0, BT_COMPLEX))
3316 : return false;
3317 :
3318 : return true;
3319 : }
3320 :
3321 :
3322 : /* A single real argument. */
3323 :
3324 : bool
3325 6780 : gfc_check_fn_r (gfc_expr *a)
3326 : {
3327 6780 : if (!type_check (a, 0, BT_REAL))
3328 : return false;
3329 :
3330 : return true;
3331 : }
3332 :
3333 : /* A single double argument. */
3334 :
3335 : bool
3336 13037 : gfc_check_fn_d (gfc_expr *a)
3337 : {
3338 13037 : if (!double_check (a, 0))
3339 : return false;
3340 :
3341 : return true;
3342 : }
3343 :
3344 : /* A single real or complex argument. */
3345 :
3346 : bool
3347 1000 : gfc_check_fn_rc (gfc_expr *a)
3348 : {
3349 1000 : if (!real_or_complex_check (a, 0))
3350 : return false;
3351 :
3352 : return true;
3353 : }
3354 :
3355 :
3356 : bool
3357 1572 : gfc_check_fn_rc2008 (gfc_expr *a)
3358 : {
3359 1572 : if (!real_or_complex_check (a, 0))
3360 : return false;
3361 :
3362 1572 : if (a->ts.type == BT_COMPLEX
3363 1572 : && !gfc_notify_std (GFC_STD_F2008, "COMPLEX argument %qs "
3364 : "of %qs intrinsic at %L",
3365 650 : gfc_current_intrinsic_arg[0]->name,
3366 : gfc_current_intrinsic, &a->where))
3367 : return false;
3368 :
3369 : return true;
3370 : }
3371 :
3372 :
3373 : bool
3374 0 : gfc_check_fnum (gfc_expr *unit)
3375 : {
3376 0 : if (!type_check (unit, 0, BT_INTEGER))
3377 : return false;
3378 :
3379 0 : if (!scalar_check (unit, 0))
3380 : return false;
3381 :
3382 : return true;
3383 : }
3384 :
3385 :
3386 : bool
3387 6284 : gfc_check_huge (gfc_expr *x)
3388 : {
3389 6284 : if (flag_unsigned)
3390 : {
3391 182 : if (!int_or_real_or_unsigned_check (x, 0))
3392 : return false;
3393 : }
3394 6102 : else if (!int_or_real_check (x, 0))
3395 : return false;
3396 :
3397 : return true;
3398 : }
3399 :
3400 :
3401 : bool
3402 24 : gfc_check_hypot (gfc_expr *x, gfc_expr *y)
3403 : {
3404 24 : if (!type_check (x, 0, BT_REAL))
3405 : return false;
3406 24 : if (!same_type_check (x, 0, y, 1))
3407 : return false;
3408 :
3409 : return true;
3410 : }
3411 :
3412 :
3413 : /* Check that the single argument is an integer. */
3414 :
3415 : bool
3416 68 : gfc_check_i (gfc_expr *i)
3417 : {
3418 68 : if (!type_check (i, 0, BT_INTEGER))
3419 : return false;
3420 :
3421 : return true;
3422 : }
3423 :
3424 : /* Check that the single argument is an integer or an UNSIGNED. */
3425 :
3426 : bool
3427 6667 : gfc_check_iu (gfc_expr *i)
3428 : {
3429 6667 : if (flag_unsigned)
3430 : {
3431 912 : if (!type_check2 (i, 0, BT_INTEGER, BT_UNSIGNED))
3432 : return false;
3433 : }
3434 5755 : else if (!type_check (i, 0, BT_INTEGER))
3435 : return false;
3436 :
3437 : return true;
3438 : }
3439 :
3440 : bool
3441 4821 : gfc_check_iand_ieor_ior (gfc_expr *i, gfc_expr *j)
3442 : {
3443 : /* i and j cannot both be BOZ literal constants. */
3444 4821 : if (!boz_args_check (i, j))
3445 : return false;
3446 :
3447 : /* If i is BOZ and j is integer, convert i to type of j. */
3448 25 : if (i->ts.type == BT_BOZ && j->ts.type == BT_INTEGER
3449 4842 : && !gfc_boz2int (i, j->ts.kind))
3450 : return false;
3451 :
3452 : /* If j is BOZ and i is integer, convert j to type of i. */
3453 37 : if (j->ts.type == BT_BOZ && i->ts.type == BT_INTEGER
3454 4854 : && !gfc_boz2int (j, i->ts.kind))
3455 : return false;
3456 :
3457 4817 : if (flag_unsigned)
3458 : {
3459 : /* If i is BOZ and j is UNSIGNED, convert i to type of j. */
3460 0 : if (i->ts.type == BT_BOZ && j->ts.type == BT_UNSIGNED
3461 42 : && !gfc_boz2uint (i, j->ts.kind))
3462 : return false;
3463 :
3464 : /* If j is BOZ and i is UNSIGNED, convert j to type of i. */
3465 0 : if (j->ts.type == BT_BOZ && i->ts.type == BT_UNSIGNED
3466 42 : && !gfc_boz2uint (j, i->ts.kind))
3467 : return false;
3468 :
3469 42 : if (gfc_invalid_unsigned_ops (i,j))
3470 : return false;
3471 :
3472 42 : if (!type_check2 (i, 0, BT_INTEGER, BT_UNSIGNED))
3473 : return false;
3474 :
3475 42 : if (!type_check2 (j, 1, BT_INTEGER, BT_UNSIGNED))
3476 : return false;
3477 : }
3478 : else
3479 : {
3480 4775 : if (!type_check (i, 0, BT_INTEGER))
3481 : return false;
3482 :
3483 4775 : if (!type_check (j, 1, BT_INTEGER))
3484 : return false;
3485 : }
3486 :
3487 4817 : if (i->ts.kind != j->ts.kind)
3488 : {
3489 1 : gfc_error ("Arguments of %qs have different kind type parameters "
3490 : "at %L", gfc_current_intrinsic, &i->where);
3491 1 : return false;
3492 : }
3493 :
3494 : return true;
3495 : }
3496 :
3497 :
3498 : bool
3499 77 : gfc_check_ibits (gfc_expr *i, gfc_expr *pos, gfc_expr *len)
3500 : {
3501 77 : if (flag_unsigned)
3502 : {
3503 24 : if (!type_check2 (i, 0, BT_INTEGER, BT_UNSIGNED))
3504 : return false;
3505 : }
3506 : else
3507 : {
3508 53 : if (!type_check (i, 0, BT_INTEGER))
3509 : return false;
3510 : }
3511 :
3512 77 : if (!type_check (pos, 1, BT_INTEGER))
3513 : return false;
3514 :
3515 77 : if (!type_check (len, 2, BT_INTEGER))
3516 : return false;
3517 :
3518 77 : if (!nonnegative_check ("pos", pos))
3519 : return false;
3520 :
3521 72 : if (!nonnegative_check ("len", len))
3522 : return false;
3523 :
3524 67 : if (!less_than_bitsize2 ("i", i, "pos", pos, "len", len))
3525 : return false;
3526 :
3527 : return true;
3528 : }
3529 :
3530 :
3531 : bool
3532 8981 : gfc_check_ichar_iachar (gfc_expr *c, gfc_expr *kind)
3533 : {
3534 8981 : int i;
3535 :
3536 8981 : if (!type_check (c, 0, BT_CHARACTER))
3537 : return false;
3538 :
3539 8981 : if (!kind_check (kind, 1, BT_INTEGER))
3540 : return false;
3541 :
3542 8981 : if (kind && !gfc_notify_std (GFC_STD_F2003, "%qs intrinsic "
3543 : "with KIND argument at %L",
3544 : gfc_current_intrinsic, &kind->where))
3545 : return false;
3546 :
3547 8981 : if (c->expr_type == EXPR_VARIABLE || c->expr_type == EXPR_SUBSTRING)
3548 : {
3549 1959 : gfc_expr *start;
3550 1959 : gfc_expr *end;
3551 1959 : gfc_ref *ref;
3552 :
3553 : /* Substring references don't have the charlength set. */
3554 1959 : ref = c->ref;
3555 2104 : while (ref && ref->type != REF_SUBSTRING)
3556 145 : ref = ref->next;
3557 :
3558 1959 : gcc_assert (ref == NULL || ref->type == REF_SUBSTRING);
3559 :
3560 1959 : if (!ref)
3561 : {
3562 : /* Check that the argument is length one. Non-constant lengths
3563 : can't be checked here, so assume they are ok. */
3564 1691 : if (c->ts.u.cl && c->ts.u.cl->length)
3565 : {
3566 : /* If we already have a length for this expression then use it. */
3567 1684 : if (c->ts.u.cl->length->expr_type != EXPR_CONSTANT)
3568 : return true;
3569 1684 : i = mpz_get_si (c->ts.u.cl->length->value.integer);
3570 : }
3571 : else
3572 : return true;
3573 : }
3574 : else
3575 : {
3576 268 : start = ref->u.ss.start;
3577 268 : end = ref->u.ss.end;
3578 :
3579 268 : gcc_assert (start);
3580 268 : if (end == NULL || end->expr_type != EXPR_CONSTANT
3581 186 : || start->expr_type != EXPR_CONSTANT)
3582 : return true;
3583 :
3584 186 : i = mpz_get_si (end->value.integer) + 1
3585 186 : - mpz_get_si (start->value.integer);
3586 : }
3587 : }
3588 : else
3589 : return true;
3590 :
3591 1870 : if (i != 1)
3592 : {
3593 8 : gfc_error ("Argument of %s at %L must be of length one",
3594 : gfc_current_intrinsic, &c->where);
3595 8 : return false;
3596 : }
3597 :
3598 : return true;
3599 : }
3600 :
3601 :
3602 : bool
3603 252 : gfc_check_idnint (gfc_expr *a)
3604 : {
3605 252 : if (!double_check (a, 0))
3606 : return false;
3607 :
3608 : return true;
3609 : }
3610 :
3611 :
3612 : bool
3613 614 : gfc_check_index (gfc_expr *string, gfc_expr *substring, gfc_expr *back,
3614 : gfc_expr *kind)
3615 : {
3616 614 : if (!type_check (string, 0, BT_CHARACTER)
3617 614 : || !type_check (substring, 1, BT_CHARACTER))
3618 : return false;
3619 :
3620 614 : if (back != NULL && !type_check (back, 2, BT_LOGICAL))
3621 : return false;
3622 :
3623 614 : if (!kind_check (kind, 3, BT_INTEGER))
3624 : return false;
3625 614 : if (kind && !gfc_notify_std (GFC_STD_F2003, "%qs intrinsic "
3626 : "with KIND argument at %L",
3627 : gfc_current_intrinsic, &kind->where))
3628 : return false;
3629 :
3630 614 : if (string->ts.kind != substring->ts.kind)
3631 : {
3632 0 : gfc_error ("%qs argument of %qs intrinsic at %L must be the same "
3633 0 : "kind as %qs", gfc_current_intrinsic_arg[1]->name,
3634 : gfc_current_intrinsic, &substring->where,
3635 0 : gfc_current_intrinsic_arg[0]->name);
3636 0 : return false;
3637 : }
3638 :
3639 : return true;
3640 : }
3641 :
3642 :
3643 : bool
3644 4271 : gfc_check_int (gfc_expr *x, gfc_expr *kind)
3645 : {
3646 : /* BOZ is dealt within simplify_int*. */
3647 4271 : if (x->ts.type == BT_BOZ)
3648 : return true;
3649 :
3650 2640 : if (!numeric_check (x, 0))
3651 : return false;
3652 :
3653 2640 : if (!kind_check (kind, 1, BT_INTEGER))
3654 : return false;
3655 :
3656 : return true;
3657 : }
3658 :
3659 : bool
3660 189 : gfc_check_uint (gfc_expr *x, gfc_expr *kind)
3661 : {
3662 :
3663 189 : if (!flag_unsigned)
3664 : {
3665 0 : gfc_error ("UINT intrinsic only valid with %<-funsigned%> at %L",
3666 : &x->where);
3667 0 : return false;
3668 : }
3669 :
3670 : /* BOZ is dealt within simplify_uint*. */
3671 189 : if (x->ts.type == BT_BOZ)
3672 : return true;
3673 :
3674 183 : if (!numeric_check (x, 0))
3675 : return false;
3676 :
3677 183 : if (!kind_check (kind, 1, BT_INTEGER))
3678 : return false;
3679 :
3680 : return true;
3681 : }
3682 :
3683 : bool
3684 97 : gfc_check_intconv (gfc_expr *x)
3685 : {
3686 97 : if (strcmp (gfc_current_intrinsic, "short") == 0
3687 62 : || strcmp (gfc_current_intrinsic, "long") == 0)
3688 : {
3689 36 : gfc_error ("%qs intrinsic subprogram at %L has been removed. "
3690 : "Use INT intrinsic subprogram.", gfc_current_intrinsic,
3691 : &x->where);
3692 36 : return false;
3693 : }
3694 :
3695 : /* BOZ is dealt within simplify_int*. */
3696 61 : if (x->ts.type == BT_BOZ)
3697 : return true;
3698 :
3699 61 : if (!numeric_check (x, 0))
3700 : return false;
3701 :
3702 : return true;
3703 : }
3704 :
3705 : bool
3706 1079 : gfc_check_ishft (gfc_expr *i, gfc_expr *shift)
3707 : {
3708 1079 : if (flag_unsigned)
3709 : {
3710 78 : if (!type_check2 (i, 0, BT_INTEGER, BT_UNSIGNED))
3711 : return false;
3712 : }
3713 : else
3714 : {
3715 1001 : if (!type_check (i, 0, BT_INTEGER))
3716 : return false;
3717 : }
3718 :
3719 1079 : if (!type_check (shift, 1, BT_INTEGER))
3720 : return false;
3721 :
3722 1079 : if (!less_than_bitsize1 ("I", i, NULL, shift, true))
3723 : return false;
3724 :
3725 : return true;
3726 : }
3727 :
3728 :
3729 : bool
3730 904 : gfc_check_ishftc (gfc_expr *i, gfc_expr *shift, gfc_expr *size)
3731 : {
3732 904 : if (flag_unsigned)
3733 : {
3734 48 : if (!type_check2 (i, 0, BT_INTEGER, BT_UNSIGNED))
3735 : return false;
3736 : }
3737 : else
3738 : {
3739 856 : if (!type_check (i, 0, BT_INTEGER))
3740 : return false;
3741 : }
3742 :
3743 904 : if (size != NULL)
3744 : {
3745 605 : int i2, i3;
3746 :
3747 605 : if (!type_check (size, 2, BT_INTEGER))
3748 11 : return false;
3749 :
3750 605 : if (!less_than_bitsize1 ("I", i, "SIZE", size, true))
3751 : return false;
3752 :
3753 602 : if (size->expr_type == EXPR_CONSTANT)
3754 : {
3755 155 : gfc_extract_int (size, &i3);
3756 155 : if (i3 <= 0)
3757 : {
3758 4 : gfc_error ("SIZE at %L must be positive", &size->where);
3759 4 : return false;
3760 : }
3761 :
3762 151 : if (shift->expr_type == EXPR_CONSTANT)
3763 : {
3764 126 : gfc_extract_int (shift, &i2);
3765 126 : if (i2 < 0)
3766 28 : i2 = -i2;
3767 :
3768 126 : if (i2 > i3)
3769 : {
3770 4 : gfc_error ("The absolute value of SHIFT at %L must be less "
3771 : "than or equal to SIZE at %L", &shift->where,
3772 : &size->where);
3773 4 : return false;
3774 : }
3775 : }
3776 : }
3777 : }
3778 299 : else if (!less_than_bitsize1 ("I", i, NULL, shift, true))
3779 : return false;
3780 :
3781 : return true;
3782 : }
3783 :
3784 :
3785 : bool
3786 8 : gfc_check_kill (gfc_expr *pid, gfc_expr *sig)
3787 : {
3788 8 : if (!type_check (pid, 0, BT_INTEGER))
3789 : return false;
3790 :
3791 8 : if (!scalar_check (pid, 0))
3792 : return false;
3793 :
3794 8 : if (!type_check (sig, 1, BT_INTEGER))
3795 : return false;
3796 :
3797 8 : if (!scalar_check (sig, 1))
3798 : return false;
3799 :
3800 : return true;
3801 : }
3802 :
3803 :
3804 : bool
3805 18 : gfc_check_kill_sub (gfc_expr *pid, gfc_expr *sig, gfc_expr *status)
3806 : {
3807 18 : if (!type_check (pid, 0, BT_INTEGER))
3808 : return false;
3809 :
3810 18 : if (!scalar_check (pid, 0))
3811 : return false;
3812 :
3813 18 : if (!type_check (sig, 1, BT_INTEGER))
3814 : return false;
3815 :
3816 18 : if (!scalar_check (sig, 1))
3817 : return false;
3818 :
3819 18 : if (status)
3820 : {
3821 13 : if (!type_check (status, 2, BT_INTEGER))
3822 : return false;
3823 :
3824 13 : if (!scalar_check (status, 2))
3825 : return false;
3826 :
3827 13 : if (status->expr_type != EXPR_VARIABLE)
3828 : {
3829 1 : gfc_error ("STATUS at %L shall be an INTENT(OUT) variable",
3830 : &status->where);
3831 1 : return false;
3832 : }
3833 :
3834 12 : if (status->expr_type == EXPR_VARIABLE
3835 12 : && status->symtree && status->symtree->n.sym
3836 12 : && status->symtree->n.sym->attr.intent == INTENT_IN)
3837 : {
3838 1 : gfc_error ("%qs at %L shall be an INTENT(OUT) variable",
3839 : status->symtree->name, &status->where);
3840 1 : return false;
3841 : }
3842 : }
3843 :
3844 : return true;
3845 : }
3846 :
3847 :
3848 : bool
3849 5529 : gfc_check_kind (gfc_expr *x)
3850 : {
3851 5529 : if (gfc_invalid_null_arg (x))
3852 : return false;
3853 :
3854 5528 : if (gfc_bt_struct (x->ts.type) || x->ts.type == BT_CLASS)
3855 : {
3856 2 : gfc_error ("%qs argument of %qs intrinsic at %L must be of "
3857 2 : "intrinsic type", gfc_current_intrinsic_arg[0]->name,
3858 : gfc_current_intrinsic, &x->where);
3859 2 : return false;
3860 : }
3861 5526 : if (x->ts.type == BT_PROCEDURE)
3862 : {
3863 2 : gfc_error ("%qs argument of %qs intrinsic at %L must be a data entity",
3864 2 : gfc_current_intrinsic_arg[0]->name, gfc_current_intrinsic,
3865 : &x->where);
3866 2 : return false;
3867 : }
3868 :
3869 : return true;
3870 : }
3871 :
3872 :
3873 : bool
3874 6576 : gfc_check_lbound (gfc_expr *array, gfc_expr *dim, gfc_expr *kind)
3875 : {
3876 6576 : if (!array_check (array, 0))
3877 : return false;
3878 :
3879 6575 : if (!dim_check (dim, 1, false))
3880 : return false;
3881 :
3882 6575 : if (!dim_rank_check (dim, array, 1))
3883 : return false;
3884 :
3885 6575 : if (!kind_check (kind, 2, BT_INTEGER))
3886 : return false;
3887 6575 : if (kind && !gfc_notify_std (GFC_STD_F2003, "%qs intrinsic "
3888 : "with KIND argument at %L",
3889 : gfc_current_intrinsic, &kind->where))
3890 : return false;
3891 :
3892 : return true;
3893 : }
3894 :
3895 :
3896 : bool
3897 426 : gfc_check_lcobound (gfc_expr *coarray, gfc_expr *dim, gfc_expr *kind)
3898 : {
3899 426 : if (flag_coarray == GFC_FCOARRAY_NONE)
3900 : {
3901 0 : gfc_fatal_error ("Coarrays disabled at %L, use %<-fcoarray=%> to enable",
3902 : gfc_current_intrinsic_where);
3903 : return false;
3904 : }
3905 :
3906 426 : if (!coarray_check (coarray, 0))
3907 : return false;
3908 :
3909 422 : if (dim != NULL)
3910 : {
3911 215 : if (!dim_check (dim, 1, false))
3912 : return false;
3913 :
3914 215 : if (!dim_corank_check (dim, coarray))
3915 : return false;
3916 : }
3917 :
3918 422 : if (!kind_check (kind, 2, BT_INTEGER))
3919 : return false;
3920 :
3921 : return true;
3922 : }
3923 :
3924 :
3925 : bool
3926 11585 : gfc_check_len_lentrim (gfc_expr *s, gfc_expr *kind)
3927 : {
3928 11585 : if (!type_check (s, 0, BT_CHARACTER))
3929 : return false;
3930 :
3931 11563 : if (gfc_invalid_null_arg (s))
3932 : return false;
3933 :
3934 11557 : if (!kind_check (kind, 1, BT_INTEGER))
3935 : return false;
3936 11557 : if (kind && !gfc_notify_std (GFC_STD_F2003, "%qs intrinsic "
3937 : "with KIND argument at %L",
3938 : gfc_current_intrinsic, &kind->where))
3939 : return false;
3940 :
3941 : return true;
3942 : }
3943 :
3944 :
3945 : bool
3946 167 : gfc_check_lge_lgt_lle_llt (gfc_expr *a, gfc_expr *b)
3947 : {
3948 167 : if (!type_check (a, 0, BT_CHARACTER))
3949 : return false;
3950 167 : if (!kind_value_check (a, 0, gfc_default_character_kind))
3951 : return false;
3952 :
3953 135 : if (!type_check (b, 1, BT_CHARACTER))
3954 : return false;
3955 135 : if (!kind_value_check (b, 1, gfc_default_character_kind))
3956 : return false;
3957 :
3958 : return true;
3959 : }
3960 :
3961 :
3962 : bool
3963 7 : gfc_check_link (gfc_expr *path1, gfc_expr *path2)
3964 : {
3965 7 : if (!type_check (path1, 0, BT_CHARACTER))
3966 : return false;
3967 7 : if (!kind_value_check (path1, 0, gfc_default_character_kind))
3968 : return false;
3969 :
3970 5 : if (!type_check (path2, 1, BT_CHARACTER))
3971 : return false;
3972 5 : if (!kind_value_check (path2, 1, gfc_default_character_kind))
3973 : return false;
3974 :
3975 : return true;
3976 : }
3977 :
3978 :
3979 : bool
3980 15 : gfc_check_link_sub (gfc_expr *path1, gfc_expr *path2, gfc_expr *status)
3981 : {
3982 15 : if (!type_check (path1, 0, BT_CHARACTER))
3983 : return false;
3984 15 : if (!kind_value_check (path1, 0, gfc_default_character_kind))
3985 : return false;
3986 :
3987 11 : if (!type_check (path2, 1, BT_CHARACTER))
3988 : return false;
3989 11 : if (!kind_value_check (path2, 0, gfc_default_character_kind))
3990 : return false;
3991 :
3992 9 : if (status == NULL)
3993 : return true;
3994 :
3995 7 : if (!type_check (status, 2, BT_INTEGER))
3996 : return false;
3997 :
3998 7 : if (!scalar_check (status, 2))
3999 : return false;
4000 :
4001 : return true;
4002 : }
4003 :
4004 :
4005 : bool
4006 3982 : gfc_check_loc (gfc_expr *expr)
4007 : {
4008 3982 : return variable_check (expr, 0, true);
4009 : }
4010 :
4011 :
4012 : bool
4013 7 : gfc_check_symlnk (gfc_expr *path1, gfc_expr *path2)
4014 : {
4015 7 : if (!type_check (path1, 0, BT_CHARACTER))
4016 : return false;
4017 7 : if (!kind_value_check (path1, 0, gfc_default_character_kind))
4018 : return false;
4019 :
4020 5 : if (!type_check (path2, 1, BT_CHARACTER))
4021 : return false;
4022 5 : if (!kind_value_check (path2, 1, gfc_default_character_kind))
4023 : return false;
4024 :
4025 : return true;
4026 : }
4027 :
4028 :
4029 : bool
4030 15 : gfc_check_symlnk_sub (gfc_expr *path1, gfc_expr *path2, gfc_expr *status)
4031 : {
4032 15 : if (!type_check (path1, 0, BT_CHARACTER))
4033 : return false;
4034 15 : if (!kind_value_check (path1, 0, gfc_default_character_kind))
4035 : return false;
4036 :
4037 11 : if (!type_check (path2, 1, BT_CHARACTER))
4038 : return false;
4039 11 : if (!kind_value_check (path2, 1, gfc_default_character_kind))
4040 : return false;
4041 :
4042 9 : if (status == NULL)
4043 : return true;
4044 :
4045 7 : if (!type_check (status, 2, BT_INTEGER))
4046 : return false;
4047 :
4048 7 : if (!scalar_check (status, 2))
4049 : return false;
4050 :
4051 : return true;
4052 : }
4053 :
4054 :
4055 : bool
4056 28 : gfc_check_logical (gfc_expr *a, gfc_expr *kind)
4057 : {
4058 28 : if (!type_check (a, 0, BT_LOGICAL))
4059 : return false;
4060 28 : if (!kind_check (kind, 1, BT_LOGICAL))
4061 : return false;
4062 :
4063 : return true;
4064 : }
4065 :
4066 :
4067 : /* Min/max family. */
4068 :
4069 : static bool
4070 5000 : min_max_args (gfc_actual_arglist *args)
4071 : {
4072 5000 : gfc_actual_arglist *arg;
4073 5000 : int i, j, nargs, *nlabels, nlabelless;
4074 5000 : bool a1 = false, a2 = false;
4075 :
4076 5000 : if (args == NULL || args->next == NULL)
4077 : {
4078 0 : gfc_error ("Intrinsic %qs at %L must have at least two arguments",
4079 : gfc_current_intrinsic, gfc_current_intrinsic_where);
4080 0 : return false;
4081 : }
4082 :
4083 5000 : if (!args->name)
4084 4988 : a1 = true;
4085 :
4086 5000 : if (!args->next->name)
4087 4987 : a2 = true;
4088 :
4089 5000 : nargs = 0;
4090 16478 : for (arg = args; arg; arg = arg->next)
4091 11478 : if (arg->name)
4092 38 : nargs++;
4093 :
4094 5000 : if (nargs == 0)
4095 : return true;
4096 :
4097 : /* Note: Having a keywordless argument after an "arg=" is checked before. */
4098 13 : nlabelless = 0;
4099 13 : nlabels = XALLOCAVEC (int, nargs);
4100 40 : for (arg = args, i = 0; arg; arg = arg->next)
4101 34 : if (arg->name)
4102 : {
4103 33 : int n;
4104 33 : char *endp;
4105 :
4106 33 : if (arg->name[0] != 'a' || arg->name[1] < '1' || arg->name[1] > '9')
4107 2 : goto unknown;
4108 31 : n = strtol (&arg->name[1], &endp, 10);
4109 31 : if (endp[0] != '\0')
4110 4 : goto unknown;
4111 27 : if (n <= 0)
4112 0 : goto unknown;
4113 27 : if (n <= nlabelless)
4114 1 : goto duplicate;
4115 26 : nlabels[i] = n;
4116 26 : i++;
4117 26 : if (n == 1)
4118 : a1 = true;
4119 15 : if (n == 2)
4120 5 : a2 = true;
4121 : }
4122 : else
4123 1 : nlabelless++;
4124 :
4125 6 : if (!a1 || !a2)
4126 : {
4127 4 : gfc_error ("Missing %qs argument to the %s intrinsic at %L",
4128 : !a1 ? "a1" : "a2", gfc_current_intrinsic,
4129 : gfc_current_intrinsic_where);
4130 4 : return false;
4131 : }
4132 :
4133 : /* Check for duplicates. */
4134 8 : for (i = 0; i < nargs; i++)
4135 12 : for (j = i + 1; j < nargs; j++)
4136 6 : if (nlabels[i] == nlabels[j])
4137 0 : goto duplicate;
4138 :
4139 : return true;
4140 :
4141 1 : duplicate:
4142 1 : gfc_error ("Duplicate argument %qs at %L to intrinsic %s", arg->name,
4143 1 : &arg->expr->where, gfc_current_intrinsic);
4144 1 : return false;
4145 :
4146 6 : unknown:
4147 6 : gfc_error ("Unknown argument %qs at %L to intrinsic %s", arg->name,
4148 6 : &arg->expr->where, gfc_current_intrinsic);
4149 6 : return false;
4150 : }
4151 :
4152 :
4153 : static bool
4154 2544 : check_rest (bt type, int kind, gfc_actual_arglist *arglist)
4155 : {
4156 2544 : gfc_actual_arglist *arg, *tmp;
4157 2544 : gfc_expr *x;
4158 2544 : int m, n;
4159 :
4160 2544 : if (!min_max_args (arglist))
4161 : return false;
4162 :
4163 8303 : for (arg = arglist, n=1; arg; arg = arg->next, n++)
4164 : {
4165 5806 : x = arg->expr;
4166 5806 : if (x->ts.type != type || x->ts.kind != kind)
4167 : {
4168 138 : if (x->ts.type == type)
4169 : {
4170 138 : if (x->ts.type == BT_CHARACTER)
4171 : {
4172 2 : gfc_error ("Different character kinds at %L", &x->where);
4173 2 : return false;
4174 : }
4175 136 : if (!gfc_notify_std (GFC_STD_GNU, "Different type "
4176 : "kinds at %L", &x->where))
4177 : return false;
4178 : }
4179 : else
4180 : {
4181 0 : gfc_error ("%<a%d%> argument of %qs intrinsic at %L must be "
4182 : "%s(%d)", n, gfc_current_intrinsic, &x->where,
4183 : gfc_basic_typename (type), kind);
4184 0 : return false;
4185 : }
4186 : }
4187 :
4188 10055 : for (tmp = arglist, m=1; tmp != arg; tmp = tmp->next, m++)
4189 4287 : if (!gfc_check_conformance (tmp->expr, x,
4190 4287 : _("arguments 'a%d' and 'a%d' for "
4191 : "intrinsic '%s'"), m, n,
4192 : gfc_current_intrinsic))
4193 : return false;
4194 : }
4195 :
4196 : return true;
4197 : }
4198 :
4199 :
4200 : bool
4201 2456 : gfc_check_min_max (gfc_actual_arglist *arg)
4202 : {
4203 2456 : gfc_expr *x;
4204 :
4205 2456 : if (!min_max_args (arg))
4206 : return false;
4207 :
4208 2454 : x = arg->expr;
4209 :
4210 2454 : if (x->ts.type == BT_CHARACTER)
4211 : {
4212 521 : if (!gfc_notify_std (GFC_STD_F2003, "%qs intrinsic "
4213 : "with CHARACTER argument at %L",
4214 : gfc_current_intrinsic, &x->where))
4215 : return false;
4216 : }
4217 : else
4218 : {
4219 1933 : if (flag_unsigned)
4220 : {
4221 78 : if (x->ts.type != BT_INTEGER && x->ts.type != BT_REAL
4222 : && x->ts.type != BT_UNSIGNED)
4223 : {
4224 0 : gfc_error ("%<a1%> argument of %qs intrinsic at %L must be "
4225 : "INTEGER, REAL, CHARACTER or UNSIGNED",
4226 : gfc_current_intrinsic, &x->where);
4227 0 : return false;
4228 : }
4229 : }
4230 : else
4231 : {
4232 1855 : if (x->ts.type != BT_INTEGER && x->ts.type != BT_REAL)
4233 : {
4234 0 : gfc_error ("%<a1%> argument of %qs intrinsic at %L must be "
4235 : "INTEGER, REAL or CHARACTER",
4236 : gfc_current_intrinsic, &x->where);
4237 0 : return false;
4238 : }
4239 : }
4240 : }
4241 :
4242 2453 : return check_rest (x->ts.type, x->ts.kind, arg);
4243 : }
4244 :
4245 :
4246 : bool
4247 43 : gfc_check_min_max_integer (gfc_actual_arglist *arg)
4248 : {
4249 43 : return check_rest (BT_INTEGER, gfc_default_integer_kind, arg);
4250 : }
4251 :
4252 :
4253 : bool
4254 38 : gfc_check_min_max_real (gfc_actual_arglist *arg)
4255 : {
4256 38 : return check_rest (BT_REAL, gfc_default_real_kind, arg);
4257 : }
4258 :
4259 :
4260 : bool
4261 10 : gfc_check_min_max_double (gfc_actual_arglist *arg)
4262 : {
4263 10 : return check_rest (BT_REAL, gfc_default_double_kind, arg);
4264 : }
4265 :
4266 :
4267 : /* End of min/max family. */
4268 :
4269 : bool
4270 16 : gfc_check_malloc (gfc_expr *size)
4271 : {
4272 16 : if (!type_check (size, 0, BT_INTEGER))
4273 : return false;
4274 :
4275 16 : if (!scalar_check (size, 0))
4276 : return false;
4277 :
4278 : return true;
4279 : }
4280 :
4281 :
4282 : bool
4283 963 : gfc_check_matmul (gfc_expr *matrix_a, gfc_expr *matrix_b)
4284 : {
4285 963 : if ((matrix_a->ts.type != BT_LOGICAL) && !gfc_numeric_ts (&matrix_a->ts))
4286 : {
4287 3 : gfc_error ("%qs argument of %qs intrinsic at %L must be numeric "
4288 3 : "or LOGICAL", gfc_current_intrinsic_arg[0]->name,
4289 : gfc_current_intrinsic, &matrix_a->where);
4290 3 : return false;
4291 : }
4292 :
4293 960 : if ((matrix_b->ts.type != BT_LOGICAL) && !gfc_numeric_ts (&matrix_b->ts))
4294 : {
4295 2 : gfc_error ("%qs argument of %qs intrinsic at %L must be numeric "
4296 2 : "or LOGICAL", gfc_current_intrinsic_arg[1]->name,
4297 : gfc_current_intrinsic, &matrix_b->where);
4298 2 : return false;
4299 : }
4300 :
4301 20 : if ((matrix_a->ts.type == BT_LOGICAL && gfc_numeric_ts (&matrix_b->ts))
4302 957 : || (gfc_numeric_ts (&matrix_a->ts) && matrix_b->ts.type == BT_LOGICAL)
4303 1914 : || gfc_invalid_unsigned_ops (matrix_a, matrix_b))
4304 : {
4305 2 : gfc_error ("Argument types of %qs intrinsic at %L must match (%s/%s)",
4306 : gfc_current_intrinsic, &matrix_a->where,
4307 : gfc_typename(&matrix_a->ts), gfc_typename(&matrix_b->ts));
4308 2 : return false;
4309 : }
4310 :
4311 956 : switch (matrix_a->rank)
4312 : {
4313 145 : case 1:
4314 145 : if (!rank_check (matrix_b, 1, 2))
4315 : return false;
4316 : /* Check for case matrix_a has shape(m), matrix_b has shape (m, k). */
4317 145 : if (!identical_dimen_shape (matrix_a, 0, matrix_b, 0))
4318 : {
4319 2 : gfc_error ("Different shape on dimension 1 for arguments %qs "
4320 : "and %qs at %L for intrinsic matmul",
4321 2 : gfc_current_intrinsic_arg[0]->name,
4322 2 : gfc_current_intrinsic_arg[1]->name, &matrix_a->where);
4323 2 : return false;
4324 : }
4325 : break;
4326 :
4327 811 : case 2:
4328 811 : if (matrix_b->rank != 2)
4329 : {
4330 171 : if (!rank_check (matrix_b, 1, 1))
4331 : return false;
4332 : }
4333 : /* matrix_b has rank 1 or 2 here. Common check for the cases
4334 : - matrix_a has shape (n,m) and matrix_b has shape (m, k)
4335 : - matrix_a has shape (n,m) and matrix_b has shape (m). */
4336 811 : if (!identical_dimen_shape (matrix_a, 1, matrix_b, 0))
4337 : {
4338 0 : gfc_error ("Different shape on dimension 2 for argument %qs and "
4339 : "dimension 1 for argument %qs at %L for intrinsic "
4340 0 : "matmul", gfc_current_intrinsic_arg[0]->name,
4341 0 : gfc_current_intrinsic_arg[1]->name, &matrix_a->where);
4342 0 : return false;
4343 : }
4344 : break;
4345 :
4346 0 : default:
4347 0 : gfc_error ("%qs argument of %qs intrinsic at %L must be of rank "
4348 0 : "1 or 2", gfc_current_intrinsic_arg[0]->name,
4349 : gfc_current_intrinsic, &matrix_a->where);
4350 0 : return false;
4351 : }
4352 :
4353 : return true;
4354 : }
4355 :
4356 :
4357 : /* Whoever came up with this interface was probably on something.
4358 : The possibilities for the occupation of the second and third
4359 : parameters are:
4360 :
4361 : Arg #2 Arg #3
4362 : NULL NULL
4363 : DIM NULL
4364 : MASK NULL
4365 : NULL MASK minloc(array, mask=m)
4366 : DIM MASK
4367 :
4368 : I.e. in the case of minloc(array,mask), mask will be in the second
4369 : position of the argument list and we'll have to fix that up. Also,
4370 : add the BACK argument if that isn't present. */
4371 :
4372 : bool
4373 14365 : gfc_check_minloc_maxloc (gfc_actual_arglist *ap)
4374 : {
4375 14365 : gfc_expr *a, *m, *d, *k, *b;
4376 :
4377 14365 : a = ap->expr;
4378 :
4379 14365 : if (flag_unsigned)
4380 : {
4381 126 : if (!int_or_real_or_char_or_unsigned_check_f2003 (a, 0))
4382 : return false;
4383 : }
4384 : else
4385 14239 : if (!int_or_real_or_char_check_f2003 (a, 0))
4386 : return false;
4387 :
4388 14365 : if (!array_check (a, 0))
4389 : return false;
4390 :
4391 14365 : d = ap->next->expr;
4392 14365 : m = ap->next->next->expr;
4393 14365 : k = ap->next->next->next->expr;
4394 14365 : b = ap->next->next->next->next->expr;
4395 :
4396 14365 : if (b)
4397 : {
4398 3898 : if (!type_check (b, 4, BT_LOGICAL) || !scalar_check (b,4))
4399 : return false;
4400 : }
4401 : else
4402 : {
4403 10467 : b = gfc_get_logical_expr (gfc_logical_4_kind, NULL, 0);
4404 10467 : ap->next->next->next->next->expr = b;
4405 10467 : ap->next->next->next->next->name = gfc_get_string ("back");
4406 : }
4407 :
4408 14361 : if (m == NULL && d != NULL && d->ts.type == BT_LOGICAL
4409 62 : && ap->next->name == NULL)
4410 : {
4411 62 : m = d;
4412 62 : d = NULL;
4413 62 : ap->next->expr = NULL;
4414 62 : ap->next->next->expr = m;
4415 : }
4416 :
4417 14361 : if (!dim_check (d, 1, false))
4418 : return false;
4419 :
4420 14361 : if (!dim_rank_check (d, a, 0))
4421 : return false;
4422 :
4423 14360 : if (m != NULL && !type_check (m, 2, BT_LOGICAL))
4424 : return false;
4425 :
4426 14356 : if (m != NULL
4427 23652 : && !gfc_check_conformance (a, m,
4428 9296 : _("arguments '%s' and '%s' for intrinsic %s"),
4429 9296 : gfc_current_intrinsic_arg[0]->name,
4430 9296 : gfc_current_intrinsic_arg[2]->name,
4431 : gfc_current_intrinsic))
4432 : return false;
4433 :
4434 14348 : if (!kind_check (k, 1, BT_INTEGER))
4435 : return false;
4436 :
4437 : return true;
4438 : }
4439 :
4440 : /* Check function for findloc. Mostly like gfc_check_minloc_maxloc
4441 : above, with the additional "value" argument. */
4442 :
4443 : bool
4444 907 : gfc_check_findloc (gfc_actual_arglist *ap)
4445 : {
4446 907 : gfc_expr *a, *v, *m, *d, *k, *b;
4447 907 : bool a1, v1;
4448 :
4449 907 : a = ap->expr;
4450 907 : if (!intrinsic_type_check (a, 0) || !array_check (a, 0))
4451 : return false;
4452 :
4453 907 : v = ap->next->expr;
4454 907 : if (!intrinsic_type_check (v, 1) || !scalar_check (v,1))
4455 : return false;
4456 :
4457 : /* Check if the type are both logical. */
4458 906 : a1 = a->ts.type == BT_LOGICAL;
4459 906 : v1 = v->ts.type == BT_LOGICAL;
4460 906 : if ((a1 && !v1) || (!a1 && v1))
4461 1 : goto incompat;
4462 :
4463 : /* Check if the type are both character. */
4464 905 : a1 = a->ts.type == BT_CHARACTER;
4465 905 : v1 = v->ts.type == BT_CHARACTER;
4466 905 : if ((a1 && !v1) || (!a1 && v1))
4467 2 : goto incompat;
4468 :
4469 903 : if (flag_unsigned && gfc_invalid_unsigned_ops (a,v))
4470 0 : goto incompat;
4471 :
4472 : /* Check the kind of the characters argument match. */
4473 903 : if (a1 && v1 && a->ts.kind != v->ts.kind)
4474 4 : goto incompat;
4475 :
4476 899 : d = ap->next->next->expr;
4477 899 : m = ap->next->next->next->expr;
4478 899 : k = ap->next->next->next->next->expr;
4479 899 : b = ap->next->next->next->next->next->expr;
4480 :
4481 899 : if (b)
4482 : {
4483 248 : if (!type_check (b, 5, BT_LOGICAL) || !scalar_check (b,4))
4484 : return false;
4485 : }
4486 : else
4487 : {
4488 651 : b = gfc_get_logical_expr (gfc_logical_4_kind, NULL, 0);
4489 651 : ap->next->next->next->next->next->expr = b;
4490 651 : ap->next->next->next->next->next->name = gfc_get_string ("back");
4491 : }
4492 :
4493 899 : if (m == NULL && d != NULL && d->ts.type == BT_LOGICAL
4494 13 : && ap->next->name == NULL)
4495 : {
4496 13 : m = d;
4497 13 : d = NULL;
4498 13 : ap->next->next->expr = NULL;
4499 13 : ap->next->next->next->expr = m;
4500 : }
4501 :
4502 899 : if (!dim_check (d, 2, false))
4503 : return false;
4504 :
4505 898 : if (!dim_rank_check (d, a, 0))
4506 : return false;
4507 :
4508 897 : if (m != NULL && !type_check (m, 3, BT_LOGICAL))
4509 : return false;
4510 :
4511 895 : if (m != NULL
4512 1319 : && !gfc_check_conformance (a, m,
4513 424 : _("arguments '%s' and '%s' for intrinsic %s"),
4514 424 : gfc_current_intrinsic_arg[0]->name,
4515 424 : gfc_current_intrinsic_arg[3]->name,
4516 : gfc_current_intrinsic))
4517 : return false;
4518 :
4519 894 : if (!kind_check (k, 1, BT_INTEGER))
4520 : return false;
4521 :
4522 : return true;
4523 :
4524 7 : incompat:
4525 7 : gfc_error ("Argument %qs of %qs intrinsic at %L must be in type "
4526 : "conformance to argument %qs at %L",
4527 7 : gfc_current_intrinsic_arg[0]->name,
4528 : gfc_current_intrinsic, &a->where,
4529 7 : gfc_current_intrinsic_arg[1]->name, &v->where);
4530 7 : return false;
4531 : }
4532 :
4533 :
4534 : /* Similar to minloc/maxloc, the argument list might need to be
4535 : reordered for the MINVAL, MAXVAL, PRODUCT, and SUM intrinsics. The
4536 : difference is that MINLOC/MAXLOC take an additional KIND argument.
4537 : The possibilities are:
4538 :
4539 : Arg #2 Arg #3
4540 : NULL NULL
4541 : DIM NULL
4542 : MASK NULL
4543 : NULL MASK minval(array, mask=m)
4544 : DIM MASK
4545 :
4546 : I.e. in the case of minval(array,mask), mask will be in the second
4547 : position of the argument list and we'll have to fix that up. */
4548 :
4549 : static bool
4550 7812 : check_reduction (gfc_actual_arglist *ap)
4551 : {
4552 7812 : gfc_expr *a, *m, *d;
4553 :
4554 7812 : a = ap->expr;
4555 7812 : d = ap->next->expr;
4556 7812 : m = ap->next->next->expr;
4557 :
4558 7812 : if (m == NULL && d != NULL && d->ts.type == BT_LOGICAL
4559 290 : && ap->next->name == NULL)
4560 : {
4561 290 : m = d;
4562 290 : d = NULL;
4563 290 : ap->next->expr = NULL;
4564 290 : ap->next->next->expr = m;
4565 : }
4566 :
4567 7812 : if (!dim_check (d, 1, false))
4568 : return false;
4569 :
4570 7812 : if (!dim_rank_check (d, a, 0))
4571 : return false;
4572 :
4573 7809 : if (m != NULL && !type_check (m, 2, BT_LOGICAL))
4574 : return false;
4575 :
4576 7809 : if (m != NULL
4577 7809 : && !gfc_check_conformance (a, m,
4578 3395 : _("arguments '%s' and '%s' for intrinsic %s"),
4579 3395 : gfc_current_intrinsic_arg[0]->name,
4580 3395 : gfc_current_intrinsic_arg[2]->name,
4581 : gfc_current_intrinsic))
4582 : return false;
4583 :
4584 : return true;
4585 : }
4586 :
4587 :
4588 : bool
4589 4062 : gfc_check_minval_maxval (gfc_actual_arglist *ap)
4590 : {
4591 4062 : if (flag_unsigned)
4592 : {
4593 108 : if (!int_or_real_or_char_or_unsigned_check_f2003 (ap->expr, 0))
4594 : return false;
4595 : }
4596 3954 : else if (!int_or_real_or_char_check_f2003 (ap->expr, 0))
4597 : return false;
4598 :
4599 4062 : if (!array_check (ap->expr, 0))
4600 : return false;
4601 :
4602 4062 : return check_reduction (ap);
4603 : }
4604 :
4605 :
4606 : bool
4607 3220 : gfc_check_product_sum (gfc_actual_arglist *ap)
4608 : {
4609 3220 : if (!numeric_check (ap->expr, 0)
4610 3220 : || !array_check (ap->expr, 0))
4611 : return false;
4612 :
4613 3219 : return check_reduction (ap);
4614 : }
4615 :
4616 :
4617 : /* For IANY, IALL and IPARITY. */
4618 :
4619 : bool
4620 1020 : gfc_check_mask (gfc_expr *i, gfc_expr *kind)
4621 : {
4622 1020 : int k;
4623 :
4624 1020 : if (flag_unsigned)
4625 : {
4626 96 : if (!type_check2 (i, 0, BT_INTEGER, BT_UNSIGNED))
4627 : return false;
4628 : }
4629 924 : else if (!type_check (i, 0, BT_INTEGER))
4630 : return false;
4631 :
4632 1020 : if (!nonnegative_check ("I", i))
4633 : return false;
4634 :
4635 1018 : if (!kind_check (kind, 1, BT_INTEGER))
4636 : return false;
4637 :
4638 1018 : if (kind)
4639 960 : gfc_extract_int (kind, &k);
4640 : else
4641 58 : k = i->ts.type == BT_UNSIGNED ? gfc_default_unsigned_kind : gfc_default_integer_kind;
4642 :
4643 1018 : if (!less_than_bitsizekind ("I", i, k))
4644 : return false;
4645 :
4646 : return true;
4647 : }
4648 :
4649 :
4650 : bool
4651 531 : gfc_check_transf_bit_intrins (gfc_actual_arglist *ap)
4652 : {
4653 531 : bt type = ap->expr->ts.type;
4654 :
4655 531 : if (flag_unsigned)
4656 : {
4657 108 : if (type != BT_INTEGER && type != BT_UNSIGNED)
4658 : {
4659 0 : gfc_error ("%qs argument of %qs intrinsic at %L must be INTEGER "
4660 0 : "or UNSIGNED", gfc_current_intrinsic_arg[0]->name,
4661 : gfc_current_intrinsic, &ap->expr->where);
4662 0 : return false;
4663 : }
4664 : }
4665 423 : else if (ap->expr->ts.type != BT_INTEGER)
4666 : {
4667 0 : gfc_error ("%qs argument of %qs intrinsic at %L must be INTEGER",
4668 0 : gfc_current_intrinsic_arg[0]->name,
4669 : gfc_current_intrinsic, &ap->expr->where);
4670 0 : return false;
4671 : }
4672 :
4673 531 : if (!array_check (ap->expr, 0))
4674 : return false;
4675 :
4676 531 : return check_reduction (ap);
4677 : }
4678 :
4679 :
4680 : bool
4681 1509 : gfc_check_merge (gfc_expr *tsource, gfc_expr *fsource, gfc_expr *mask)
4682 : {
4683 1509 : if (gfc_invalid_null_arg (tsource))
4684 : return false;
4685 :
4686 1507 : if (gfc_invalid_null_arg (fsource))
4687 : return false;
4688 :
4689 1506 : if (!same_type_check (tsource, 0, fsource, 1))
4690 : return false;
4691 :
4692 1506 : if (!type_check (mask, 2, BT_LOGICAL))
4693 : return false;
4694 :
4695 1506 : if (tsource->ts.type == BT_CHARACTER)
4696 585 : return gfc_check_same_strlen (tsource, fsource, "MERGE intrinsic");
4697 :
4698 : return true;
4699 : }
4700 :
4701 :
4702 : bool
4703 337 : gfc_check_merge_bits (gfc_expr *i, gfc_expr *j, gfc_expr *mask)
4704 : {
4705 : /* i and j cannot both be BOZ literal constants. */
4706 337 : if (!boz_args_check (i, j))
4707 : return false;
4708 :
4709 : /* If i is BOZ and j is integer, convert i to type of j. */
4710 12 : if (i->ts.type == BT_BOZ && j->ts.type == BT_INTEGER
4711 348 : && !gfc_boz2int (i, j->ts.kind))
4712 : return false;
4713 :
4714 : /* If j is BOZ and i is integer, convert j to type of i. */
4715 24 : if (j->ts.type == BT_BOZ && i->ts.type == BT_INTEGER
4716 360 : && !gfc_boz2int (j, i->ts.kind))
4717 : return false;
4718 :
4719 336 : if (flag_unsigned)
4720 : {
4721 : /* If i is BOZ and j is unsigned, convert i to type of j. */
4722 0 : if (i->ts.type == BT_BOZ && j->ts.type == BT_UNSIGNED
4723 24 : && !gfc_boz2uint (i, j->ts.kind))
4724 : return false;
4725 :
4726 : /* If j is BOZ and i is unsigned, convert j to type of i. */
4727 0 : if (j->ts.type == BT_BOZ && i->ts.type == BT_UNSIGNED
4728 24 : && !gfc_boz2int (j, i->ts.kind))
4729 : return false;
4730 :
4731 24 : if (!type_check2 (i, 0, BT_INTEGER, BT_UNSIGNED))
4732 : return false;
4733 :
4734 24 : if (!type_check2 (j, 1, BT_INTEGER, BT_UNSIGNED))
4735 : return false;
4736 : }
4737 : else
4738 : {
4739 312 : if (!type_check (i, 0, BT_INTEGER))
4740 : return false;
4741 :
4742 312 : if (!type_check (j, 1, BT_INTEGER))
4743 : return false;
4744 : }
4745 :
4746 336 : if (!same_type_check (i, 0, j, 1))
4747 : return false;
4748 :
4749 336 : if (mask->ts.type == BT_BOZ)
4750 : {
4751 24 : if (i->ts.type == BT_INTEGER && !gfc_boz2int (mask, i->ts.kind))
4752 : return false;
4753 24 : if (i->ts.type == BT_UNSIGNED && !gfc_boz2uint (mask, i->ts.kind))
4754 : return false;
4755 : }
4756 :
4757 336 : if (flag_unsigned)
4758 : {
4759 24 : if (!type_check2 (mask, 2, BT_INTEGER, BT_UNSIGNED))
4760 : return false;
4761 : }
4762 : else
4763 : {
4764 312 : if (!type_check (mask, 2, BT_INTEGER))
4765 : return false;
4766 : }
4767 :
4768 336 : if (!same_type_check (i, 0, mask, 2))
4769 : return false;
4770 :
4771 : return true;
4772 : }
4773 :
4774 :
4775 : bool
4776 329 : gfc_check_move_alloc (gfc_expr *from, gfc_expr *to, gfc_expr *stat,
4777 : gfc_expr *errmsg)
4778 : {
4779 329 : struct sync_stat sync_stat = {stat, errmsg};
4780 :
4781 329 : if ((stat || errmsg)
4782 329 : && !gfc_notify_std (GFC_STD_F2018, "STAT= or ERRMSG= argument at %L",
4783 : stat ? &stat->where : &errmsg->where))
4784 : return false;
4785 :
4786 327 : if (stat)
4787 : {
4788 14 : if (!type_check (stat, 2, BT_INTEGER) || !scalar_check (stat, 2))
4789 : return false;
4790 11 : if (!check_minrange4 (stat, 2))
4791 : return false;
4792 10 : if (!variable_check (stat, 2, false))
4793 : return false;
4794 9 : if (!check_not_coindexed (stat, 2))
4795 : return false;
4796 8 : if (!gfc_check_vardef_context (stat, false, false, false,
4797 : "argument 'STAT' with INTENT(OUT)"))
4798 : return false;
4799 : }
4800 :
4801 321 : if (errmsg)
4802 : {
4803 12 : if (!type_check (errmsg, 3, BT_CHARACTER) || !scalar_check (errmsg, 3))
4804 : return false;
4805 10 : if (!kind_value_check (errmsg, 3, gfc_default_character_kind))
4806 : return false;
4807 9 : if (!variable_check (errmsg, 3, false))
4808 : return false;
4809 8 : if (!check_not_coindexed (errmsg, 3))
4810 : return false;
4811 7 : if (!gfc_check_vardef_context (errmsg, false, false, false,
4812 : "argument 'ERRMSG' with INTENT(INOUT)"))
4813 : return false;
4814 : }
4815 :
4816 316 : if (!gfc_resolve_sync_stat (&sync_stat))
4817 : return false;
4818 :
4819 316 : if (!variable_check (from, 0, false))
4820 : return false;
4821 311 : if (!allocatable_check (from, 0))
4822 : return false;
4823 305 : if (gfc_is_coindexed (from))
4824 : {
4825 2 : gfc_error ("The FROM argument to MOVE_ALLOC at %L shall not be "
4826 : "coindexed", &from->where);
4827 2 : return false;
4828 : }
4829 :
4830 303 : if (!variable_check (to, 1, false))
4831 : return false;
4832 303 : if (!allocatable_check (to, 1))
4833 : return false;
4834 302 : if (gfc_is_coindexed (to))
4835 : {
4836 2 : gfc_error ("The TO argument to MOVE_ALLOC at %L shall not be "
4837 : "coindexed", &to->where);
4838 2 : return false;
4839 : }
4840 :
4841 300 : if (from->ts.type == BT_CLASS && to->ts.type == BT_DERIVED)
4842 : {
4843 1 : gfc_error ("The TO arguments in MOVE_ALLOC at %L must be "
4844 : "polymorphic if FROM is polymorphic",
4845 : &to->where);
4846 1 : return false;
4847 : }
4848 :
4849 299 : if (!same_type_check (to, 1, from, 0))
4850 : return false;
4851 :
4852 299 : if (to->rank != from->rank)
4853 : {
4854 0 : gfc_error ("The FROM and TO arguments of the MOVE_ALLOC intrinsic at %L "
4855 : "must have the same rank %d/%d", &to->where, from->rank,
4856 : to->rank);
4857 0 : return false;
4858 : }
4859 :
4860 : /* IR F08/0040; cf. 12-006A. */
4861 299 : if (to->corank != from->corank)
4862 : {
4863 4 : gfc_error ("The FROM and TO arguments of the MOVE_ALLOC intrinsic at %L "
4864 : "must have the same corank %d/%d",
4865 : &to->where, from->corank, to->corank);
4866 4 : return false;
4867 : }
4868 :
4869 : /* This is based loosely on F2003 12.4.1.7. It is intended to prevent
4870 : the likes of to = sym->cmp1->cmp2 and from = sym->cmp1, where cmp1
4871 : and cmp2 are allocatable. After the allocation is transferred,
4872 : the 'to' chain is broken by the nullification of the 'from'. A bit
4873 : of reflection reveals that this can only occur for derived types
4874 : with recursive allocatable components. */
4875 295 : if (to->expr_type == EXPR_VARIABLE && from->expr_type == EXPR_VARIABLE
4876 295 : && !strcmp (to->symtree->n.sym->name, from->symtree->n.sym->name))
4877 : {
4878 2 : gfc_ref *to_ref, *from_ref;
4879 2 : to_ref = to->ref;
4880 2 : from_ref = from->ref;
4881 2 : bool aliasing = true;
4882 :
4883 3 : for (; from_ref && to_ref;
4884 1 : from_ref = from_ref->next, to_ref = to_ref->next)
4885 : {
4886 2 : if (to_ref->type != from->ref->type)
4887 : aliasing = false;
4888 2 : else if (to_ref->type == REF_ARRAY
4889 1 : && to_ref->u.ar.type != AR_FULL
4890 1 : && from_ref->u.ar.type != AR_FULL)
4891 : /* Play safe; assume sections and elements are different. */
4892 : aliasing = false;
4893 1 : else if (to_ref->type == REF_COMPONENT
4894 1 : && to_ref->u.c.component != from_ref->u.c.component)
4895 : aliasing = false;
4896 :
4897 1 : if (!aliasing)
4898 : break;
4899 : }
4900 :
4901 2 : if (aliasing)
4902 : {
4903 1 : gfc_error ("The FROM and TO arguments at %L violate aliasing "
4904 : "restrictions (F2003 12.4.1.7)", &to->where);
4905 1 : return false;
4906 : }
4907 : }
4908 :
4909 : /* CLASS arguments: Make sure the vtab of from is present. */
4910 294 : if (to->ts.type == BT_CLASS && !UNLIMITED_POLY (from))
4911 100 : gfc_find_vtab (&from->ts);
4912 :
4913 : return true;
4914 : }
4915 :
4916 :
4917 : bool
4918 2490 : gfc_check_nearest (gfc_expr *x, gfc_expr *s)
4919 : {
4920 2490 : if (!type_check (x, 0, BT_REAL))
4921 : return false;
4922 :
4923 2490 : if (!type_check (s, 1, BT_REAL))
4924 : return false;
4925 :
4926 2490 : if (s->expr_type == EXPR_CONSTANT)
4927 : {
4928 2394 : if (mpfr_sgn (s->value.real) == 0)
4929 : {
4930 4 : gfc_error ("Argument %<S%> of NEAREST at %L shall not be zero",
4931 : &s->where);
4932 4 : return false;
4933 : }
4934 : }
4935 :
4936 : return true;
4937 : }
4938 :
4939 :
4940 : bool
4941 331 : gfc_check_new_line (gfc_expr *a)
4942 : {
4943 331 : if (!type_check (a, 0, BT_CHARACTER))
4944 : return false;
4945 :
4946 : return true;
4947 : }
4948 :
4949 :
4950 : bool
4951 172 : gfc_check_norm2 (gfc_expr *array, gfc_expr *dim)
4952 : {
4953 172 : if (!type_check (array, 0, BT_REAL))
4954 : return false;
4955 :
4956 170 : if (!array_check (array, 0))
4957 : return false;
4958 :
4959 169 : if (!dim_check (dim, 1, false))
4960 : return false;
4961 :
4962 168 : if (!dim_rank_check (dim, array, false))
4963 : return false;
4964 :
4965 : return true;
4966 : }
4967 :
4968 : bool
4969 2009 : gfc_check_null (gfc_expr *mold)
4970 : {
4971 2009 : symbol_attribute attr;
4972 :
4973 2009 : if (mold == NULL)
4974 : return true;
4975 :
4976 567 : if (mold->expr_type == EXPR_NULL)
4977 : return true;
4978 :
4979 564 : if (!variable_check (mold, 0, true))
4980 : return false;
4981 :
4982 564 : attr = gfc_variable_attr (mold);
4983 :
4984 564 : if (!attr.pointer && !attr.proc_pointer && !attr.allocatable)
4985 : {
4986 0 : gfc_error ("%qs argument of %qs intrinsic at %L must be a POINTER, "
4987 : "ALLOCATABLE or procedure pointer",
4988 0 : gfc_current_intrinsic_arg[0]->name,
4989 : gfc_current_intrinsic, &mold->where);
4990 0 : return false;
4991 : }
4992 :
4993 564 : if (attr.allocatable
4994 564 : && !gfc_notify_std (GFC_STD_F2003, "NULL intrinsic with "
4995 : "allocatable MOLD at %L", &mold->where))
4996 : return false;
4997 :
4998 : /* F2008, C1242. */
4999 563 : if (gfc_is_coindexed (mold))
5000 : {
5001 1 : gfc_error ("%qs argument of %qs intrinsic at %L shall not be "
5002 1 : "coindexed", gfc_current_intrinsic_arg[0]->name,
5003 : gfc_current_intrinsic, &mold->where);
5004 1 : return false;
5005 : }
5006 :
5007 : return true;
5008 : }
5009 :
5010 :
5011 : bool
5012 648 : gfc_check_out_of_range (gfc_expr *x, gfc_expr *mold, gfc_expr *round)
5013 : {
5014 648 : if (!int_or_real_or_unsigned_check (x, 0))
5015 : return false;
5016 :
5017 648 : if (mold == NULL)
5018 : return false;
5019 :
5020 648 : if (!int_or_real_or_unsigned_check (mold, 1))
5021 : return false;
5022 :
5023 648 : if (!scalar_check (mold, 1))
5024 : return false;
5025 :
5026 648 : if (round)
5027 : {
5028 282 : if (!type_check (round, 2, BT_LOGICAL))
5029 : return false;
5030 :
5031 282 : if (!scalar_check (round, 2))
5032 : return false;
5033 :
5034 282 : if (x->ts.type != BT_REAL
5035 282 : || (mold->ts.type != BT_INTEGER && mold->ts.type != BT_UNSIGNED))
5036 : {
5037 0 : gfc_error ("%qs argument of %qs intrinsic at %L shall appear "
5038 : "only if %qs is of type REAL and %qs is of type "
5039 : "INTEGER or UNSIGNED",
5040 0 : gfc_current_intrinsic_arg[2]->name,
5041 : gfc_current_intrinsic, &round->where,
5042 0 : gfc_current_intrinsic_arg[0]->name,
5043 0 : gfc_current_intrinsic_arg[1]->name);
5044 :
5045 0 : return false;
5046 : }
5047 : }
5048 :
5049 : return true;
5050 : }
5051 :
5052 :
5053 : bool
5054 659 : gfc_check_pack (gfc_expr *array, gfc_expr *mask, gfc_expr *vector)
5055 : {
5056 659 : if (!array_check (array, 0))
5057 : return false;
5058 :
5059 659 : if (!type_check (mask, 1, BT_LOGICAL))
5060 : return false;
5061 :
5062 659 : if (!gfc_check_conformance (array, mask,
5063 659 : _("arguments '%s' and '%s' for intrinsic '%s'"),
5064 659 : gfc_current_intrinsic_arg[0]->name,
5065 659 : gfc_current_intrinsic_arg[1]->name,
5066 : gfc_current_intrinsic))
5067 : return false;
5068 :
5069 658 : if (vector != NULL)
5070 : {
5071 213 : mpz_t array_size, vector_size;
5072 213 : bool have_array_size, have_vector_size;
5073 :
5074 213 : if (!same_type_check (array, 0, vector, 2))
5075 2 : return false;
5076 :
5077 213 : if (!rank_check (vector, 2, 1))
5078 : return false;
5079 :
5080 : /* VECTOR requires at least as many elements as MASK
5081 : has .TRUE. values. */
5082 213 : have_array_size = gfc_array_size(array, &array_size);
5083 213 : have_vector_size = gfc_array_size(vector, &vector_size);
5084 :
5085 213 : if (have_vector_size
5086 177 : && (mask->expr_type == EXPR_ARRAY
5087 174 : || (mask->expr_type == EXPR_CONSTANT
5088 42 : && have_array_size)))
5089 : {
5090 33 : int mask_true_values = 0;
5091 :
5092 33 : if (mask->expr_type == EXPR_ARRAY)
5093 : {
5094 3 : gfc_constructor *mask_ctor;
5095 3 : mask_ctor = gfc_constructor_first (mask->value.constructor);
5096 42 : while (mask_ctor)
5097 : {
5098 36 : if (mask_ctor->expr->expr_type != EXPR_CONSTANT)
5099 : {
5100 : mask_true_values = 0;
5101 : break;
5102 : }
5103 :
5104 36 : if (mask_ctor->expr->value.logical)
5105 6 : mask_true_values++;
5106 :
5107 36 : mask_ctor = gfc_constructor_next (mask_ctor);
5108 : }
5109 : }
5110 30 : else if (mask->expr_type == EXPR_CONSTANT && mask->value.logical)
5111 12 : mask_true_values = mpz_get_si (array_size);
5112 :
5113 33 : if (mpz_get_si (vector_size) < mask_true_values)
5114 : {
5115 2 : gfc_error ("%qs argument of %qs intrinsic at %L must "
5116 : "provide at least as many elements as there "
5117 : "are .TRUE. values in %qs (%ld/%d)",
5118 2 : gfc_current_intrinsic_arg[2]->name,
5119 : gfc_current_intrinsic, &vector->where,
5120 2 : gfc_current_intrinsic_arg[1]->name,
5121 : mpz_get_si (vector_size), mask_true_values);
5122 2 : return false;
5123 : }
5124 : }
5125 :
5126 199 : if (have_array_size)
5127 151 : mpz_clear (array_size);
5128 211 : if (have_vector_size)
5129 175 : mpz_clear (vector_size);
5130 : }
5131 :
5132 : return true;
5133 : }
5134 :
5135 :
5136 : bool
5137 103 : gfc_check_parity (gfc_expr *mask, gfc_expr *dim)
5138 : {
5139 103 : if (!type_check (mask, 0, BT_LOGICAL))
5140 : return false;
5141 :
5142 101 : if (!array_check (mask, 0))
5143 : return false;
5144 :
5145 100 : if (!dim_check (dim, 1, false))
5146 : return false;
5147 :
5148 99 : if (!dim_rank_check (dim, mask, false))
5149 : return false;
5150 :
5151 : return true;
5152 : }
5153 :
5154 :
5155 : bool
5156 460 : gfc_check_precision (gfc_expr *x)
5157 : {
5158 460 : if (!real_or_complex_check (x, 0))
5159 : return false;
5160 :
5161 : return true;
5162 : }
5163 :
5164 :
5165 : bool
5166 5169 : gfc_check_present (gfc_expr *a)
5167 : {
5168 5169 : gfc_symbol *sym;
5169 :
5170 5169 : if (!variable_check (a, 0, true))
5171 : return false;
5172 :
5173 5169 : sym = a->symtree->n.sym;
5174 5169 : if (!sym->attr.dummy)
5175 : {
5176 0 : gfc_error ("%qs argument of %qs intrinsic at %L must be of a "
5177 0 : "dummy variable", gfc_current_intrinsic_arg[0]->name,
5178 : gfc_current_intrinsic, &a->where);
5179 0 : return false;
5180 : }
5181 :
5182 : /* For CLASS, the optional attribute might be set at either location. */
5183 5169 : if ((sym->ts.type != BT_CLASS || !CLASS_DATA (sym)->attr.optional)
5184 5169 : && !sym->attr.optional)
5185 : {
5186 0 : gfc_error ("%qs argument of %qs intrinsic at %L must be of "
5187 : "an OPTIONAL dummy variable",
5188 0 : gfc_current_intrinsic_arg[0]->name, gfc_current_intrinsic,
5189 : &a->where);
5190 0 : return false;
5191 : }
5192 :
5193 : /* 13.14.82 PRESENT(A)
5194 : ......
5195 : Argument. A shall be the name of an optional dummy argument that is
5196 : accessible in the subprogram in which the PRESENT function reference
5197 : appears... */
5198 :
5199 5169 : if (a->ref != NULL
5200 2404 : && !(a->ref->next == NULL && a->ref->type == REF_ARRAY
5201 2403 : && (a->ref->u.ar.type == AR_FULL
5202 21 : || (a->ref->u.ar.type == AR_ELEMENT
5203 21 : && a->ref->u.ar.as->rank == 0))))
5204 : {
5205 2 : gfc_error ("%qs argument of %qs intrinsic at %L must not be a "
5206 2 : "subobject of %qs", gfc_current_intrinsic_arg[0]->name,
5207 : gfc_current_intrinsic, &a->where, sym->name);
5208 2 : return false;
5209 : }
5210 :
5211 : return true;
5212 : }
5213 :
5214 :
5215 : bool
5216 61 : gfc_check_radix (gfc_expr *x)
5217 : {
5218 61 : if (!int_or_real_check (x, 0))
5219 : return false;
5220 :
5221 : return true;
5222 : }
5223 :
5224 :
5225 : bool
5226 180 : gfc_check_range (gfc_expr *x)
5227 : {
5228 180 : if (!numeric_check (x, 0))
5229 : return false;
5230 :
5231 : return true;
5232 : }
5233 :
5234 :
5235 : bool
5236 1412 : gfc_check_rank (gfc_expr *a)
5237 : {
5238 : /* Any data object is allowed; a "data object" is a "constant (4.1.3),
5239 : variable (6), or subobject of a constant (2.4.3.2.3)" (F2008, 1.3.45). */
5240 :
5241 1412 : bool is_variable = true;
5242 :
5243 : /* Functions returning pointers are regarded as variable, cf. F2008, R602. */
5244 1412 : if (a->expr_type == EXPR_FUNCTION)
5245 0 : is_variable = a->value.function.esym
5246 0 : ? a->value.function.esym->result->attr.pointer
5247 0 : : a->symtree->n.sym->result->attr.pointer;
5248 :
5249 1412 : if (a->expr_type == EXPR_OP
5250 1412 : || a->expr_type == EXPR_NULL
5251 1412 : || a->expr_type == EXPR_COMPCALL
5252 1412 : || a->expr_type == EXPR_PPC
5253 1412 : || a->ts.type == BT_PROCEDURE
5254 1412 : || !is_variable)
5255 : {
5256 0 : gfc_error ("The argument of the RANK intrinsic at %L must be a data "
5257 : "object", &a->where);
5258 0 : return false;
5259 : }
5260 :
5261 : return true;
5262 : }
5263 :
5264 :
5265 : bool
5266 3570 : gfc_check_real (gfc_expr *a, gfc_expr *kind)
5267 : {
5268 3570 : if (!kind_check (kind, 1, BT_REAL))
5269 : return false;
5270 :
5271 : /* BOZ is dealt with in gfc_simplify_real. */
5272 3570 : if (a->ts.type == BT_BOZ)
5273 : return true;
5274 :
5275 3485 : if (!numeric_check (a, 0))
5276 : return false;
5277 :
5278 : return true;
5279 : }
5280 :
5281 :
5282 : bool
5283 257 : gfc_check_reduce (gfc_expr *array, gfc_expr *operation, gfc_expr *dim,
5284 : gfc_expr *mask, gfc_expr *identity, gfc_expr *ordered)
5285 : {
5286 257 : if (array->ts.type == BT_CLASS)
5287 : {
5288 1 : gfc_error ("The ARRAY argument at %L of REDUCE shall not be polymorphic",
5289 : &array->where);
5290 1 : return false;
5291 : }
5292 :
5293 256 : if (!check_operation (operation, array, false))
5294 : return false;
5295 :
5296 242 : if (dim && (dim->rank || dim->ts.type != BT_INTEGER))
5297 : {
5298 2 : gfc_error ("The DIM argument at %L, if present, must be an integer "
5299 : "scalar", &dim->where);
5300 2 : return false;
5301 : }
5302 :
5303 240 : if (mask && (array->rank != mask->rank || mask->ts.type != BT_LOGICAL))
5304 : {
5305 2 : gfc_error ("The MASK argument at %L, if present, must be a logical "
5306 : "array with the same rank as ARRAY", &mask->where);
5307 2 : return false;
5308 : }
5309 :
5310 82 : if (mask
5311 82 : && !gfc_check_conformance (array, mask,
5312 82 : _("arguments '%s' and '%s' for intrinsic %s"),
5313 : "ARRAY", "MASK", "REDUCE"))
5314 : return false;
5315 :
5316 237 : if (mask && !identity)
5317 1 : gfc_warning (0, "MASK present at %L without IDENTITY", &mask->where);
5318 :
5319 237 : if (ordered && (ordered->rank || ordered->ts.type != BT_LOGICAL))
5320 : {
5321 0 : gfc_error ("The ORDERED argument at %L, if present, must be a logical "
5322 : "scalar", &ordered->where);
5323 0 : return false;
5324 : }
5325 :
5326 237 : if (identity && (identity->rank
5327 79 : || !gfc_compare_types (&array->ts, &identity->ts)))
5328 : {
5329 2 : gfc_error ("The IDENTITY argument at %L, if present, must be a scalar "
5330 : "with the same type as ARRAY", &identity->where);
5331 2 : return false;
5332 : }
5333 :
5334 : return true;
5335 : }
5336 :
5337 :
5338 : bool
5339 7 : gfc_check_rename (gfc_expr *path1, gfc_expr *path2)
5340 : {
5341 7 : if (!type_check (path1, 0, BT_CHARACTER))
5342 : return false;
5343 7 : if (!kind_value_check (path1, 0, gfc_default_character_kind))
5344 : return false;
5345 :
5346 5 : if (!type_check (path2, 1, BT_CHARACTER))
5347 : return false;
5348 5 : if (!kind_value_check (path2, 1, gfc_default_character_kind))
5349 : return false;
5350 :
5351 : return true;
5352 : }
5353 :
5354 :
5355 : bool
5356 15 : gfc_check_rename_sub (gfc_expr *path1, gfc_expr *path2, gfc_expr *status)
5357 : {
5358 15 : if (!type_check (path1, 0, BT_CHARACTER))
5359 : return false;
5360 15 : if (!kind_value_check (path1, 0, gfc_default_character_kind))
5361 : return false;
5362 :
5363 11 : if (!type_check (path2, 1, BT_CHARACTER))
5364 : return false;
5365 11 : if (!kind_value_check (path2, 1, gfc_default_character_kind))
5366 : return false;
5367 :
5368 9 : if (status == NULL)
5369 : return true;
5370 :
5371 7 : if (!type_check (status, 2, BT_INTEGER))
5372 : return false;
5373 :
5374 7 : if (!scalar_check (status, 2))
5375 : return false;
5376 :
5377 : return true;
5378 : }
5379 :
5380 :
5381 : bool
5382 1503 : gfc_check_repeat (gfc_expr *x, gfc_expr *y)
5383 : {
5384 1503 : if (!type_check (x, 0, BT_CHARACTER))
5385 : return false;
5386 :
5387 1503 : if (!scalar_check (x, 0))
5388 : return false;
5389 :
5390 1503 : if (!type_check (y, 0, BT_INTEGER))
5391 : return false;
5392 :
5393 1503 : if (!scalar_check (y, 1))
5394 : return false;
5395 :
5396 : return true;
5397 : }
5398 :
5399 :
5400 : bool
5401 9570 : gfc_check_reshape (gfc_expr *source, gfc_expr *shape,
5402 : gfc_expr *pad, gfc_expr *order)
5403 : {
5404 9570 : mpz_t size;
5405 9570 : mpz_t nelems;
5406 9570 : int shape_size;
5407 9570 : bool shape_is_const;
5408 :
5409 9570 : if (!array_check (source, 0))
5410 : return false;
5411 :
5412 9569 : if (!rank_check (shape, 1, 1))
5413 : return false;
5414 :
5415 9569 : if (!type_check (shape, 1, BT_INTEGER))
5416 : return false;
5417 :
5418 9569 : if (!gfc_array_size (shape, &size))
5419 : {
5420 0 : gfc_error ("%<shape%> argument of %<reshape%> intrinsic at %L must be an "
5421 : "array of constant size", &shape->where);
5422 0 : return false;
5423 : }
5424 :
5425 9569 : shape_size = mpz_get_ui (size);
5426 9569 : mpz_clear (size);
5427 :
5428 9569 : if (shape_size <= 0)
5429 : {
5430 1 : gfc_error ("%qs argument of %qs intrinsic at %L is empty",
5431 1 : gfc_current_intrinsic_arg[1]->name, gfc_current_intrinsic,
5432 : &shape->where);
5433 1 : return false;
5434 : }
5435 9568 : else if (shape_size > GFC_MAX_DIMENSIONS)
5436 : {
5437 1 : gfc_error ("%<shape%> argument of %<reshape%> intrinsic at %L has more "
5438 : "than %d elements", &shape->where, GFC_MAX_DIMENSIONS);
5439 1 : return false;
5440 : }
5441 :
5442 9567 : gfc_simplify_expr (shape, 0);
5443 9567 : shape_is_const = gfc_is_constant_array_expr (shape);
5444 :
5445 9567 : if (shape->expr_type == EXPR_ARRAY && shape_is_const)
5446 : {
5447 : gfc_expr *e;
5448 : int i, extent;
5449 26196 : for (i = 0; i < shape_size; ++i)
5450 : {
5451 18235 : e = gfc_constructor_lookup_expr (shape->value.constructor, i);
5452 18235 : if (e == NULL)
5453 : break;
5454 18235 : if (e->expr_type != EXPR_CONSTANT)
5455 0 : continue;
5456 :
5457 18235 : gfc_extract_int (e, &extent);
5458 18235 : if (extent < 0)
5459 : {
5460 4 : gfc_error ("%qs argument of %qs intrinsic at %L has "
5461 : "negative element (%d)",
5462 4 : gfc_current_intrinsic_arg[1]->name,
5463 : gfc_current_intrinsic, &shape->where, extent);
5464 4 : return false;
5465 : }
5466 : }
5467 : }
5468 :
5469 9563 : if (pad != NULL)
5470 : {
5471 367 : if (!same_type_check (source, 0, pad, 2))
5472 : return false;
5473 :
5474 367 : if (!array_check (pad, 2))
5475 : return false;
5476 : }
5477 :
5478 9563 : if (order != NULL)
5479 : {
5480 143 : if (!array_check (order, 3))
5481 : return false;
5482 :
5483 143 : if (!type_check (order, 3, BT_INTEGER))
5484 : return false;
5485 :
5486 142 : if (order->expr_type == EXPR_ARRAY && gfc_is_constant_array_expr (order))
5487 : {
5488 : int i, order_size, dim, perm[GFC_MAX_DIMENSIONS];
5489 : gfc_expr *e;
5490 :
5491 1344 : for (i = 0; i < GFC_MAX_DIMENSIONS; ++i)
5492 1260 : perm[i] = 0;
5493 :
5494 84 : gfc_array_size (order, &size);
5495 84 : order_size = mpz_get_ui (size);
5496 84 : mpz_clear (size);
5497 :
5498 84 : if (order_size != shape_size)
5499 : {
5500 1 : gfc_error ("%qs argument of %qs intrinsic at %L "
5501 : "has wrong number of elements (%d/%d)",
5502 1 : gfc_current_intrinsic_arg[3]->name,
5503 : gfc_current_intrinsic, &order->where,
5504 : order_size, shape_size);
5505 3 : return false;
5506 : }
5507 :
5508 253 : for (i = 1; i <= order_size; ++i)
5509 : {
5510 172 : e = gfc_constructor_lookup_expr (order->value.constructor, i-1);
5511 172 : if (e->expr_type != EXPR_CONSTANT)
5512 0 : continue;
5513 :
5514 172 : gfc_extract_int (e, &dim);
5515 :
5516 172 : if (dim < 1 || dim > order_size)
5517 : {
5518 1 : gfc_error ("%qs argument of %qs intrinsic at %L "
5519 : "has out-of-range dimension (%d)",
5520 1 : gfc_current_intrinsic_arg[3]->name,
5521 : gfc_current_intrinsic, &e->where, dim);
5522 1 : return false;
5523 : }
5524 :
5525 171 : if (perm[dim-1] != 0)
5526 : {
5527 1 : gfc_error ("%qs argument of %qs intrinsic at %L has "
5528 : "invalid permutation of dimensions (dimension "
5529 : "%qd duplicated)",
5530 1 : gfc_current_intrinsic_arg[3]->name,
5531 : gfc_current_intrinsic, &e->where, dim);
5532 1 : return false;
5533 : }
5534 :
5535 170 : perm[dim-1] = 1;
5536 : }
5537 : }
5538 : }
5539 :
5540 9559 : if (pad == NULL && shape->expr_type == EXPR_ARRAY && shape_is_const
5541 7641 : && !(source->expr_type == EXPR_VARIABLE && source->symtree->n.sym->as
5542 1938 : && source->symtree->n.sym->as->type == AS_ASSUMED_SIZE))
5543 : {
5544 : /* Check the match in size between source and destination. */
5545 7640 : if (gfc_array_size (source, &nelems))
5546 : {
5547 7385 : gfc_constructor *c;
5548 7385 : bool test;
5549 :
5550 :
5551 7385 : mpz_init_set_ui (size, 1);
5552 7385 : for (c = gfc_constructor_first (shape->value.constructor);
5553 24173 : c; c = gfc_constructor_next (c))
5554 16788 : mpz_mul (size, size, c->expr->value.integer);
5555 :
5556 7385 : test = mpz_cmp (nelems, size) < 0 && mpz_cmp_ui (size, 0) > 0;
5557 7385 : mpz_clear (nelems);
5558 7385 : mpz_clear (size);
5559 :
5560 7385 : if (test)
5561 : {
5562 11 : gfc_error ("Without padding, there are not enough elements "
5563 : "in the intrinsic RESHAPE source at %L to match "
5564 : "the shape", &source->where);
5565 11 : return false;
5566 : }
5567 : }
5568 : }
5569 :
5570 : return true;
5571 : }
5572 :
5573 :
5574 : bool
5575 773 : gfc_check_same_type_as (gfc_expr *a, gfc_expr *b)
5576 : {
5577 773 : if (a->ts.type != BT_DERIVED && a->ts.type != BT_CLASS)
5578 : {
5579 4 : gfc_error ("%qs argument of %qs intrinsic at %L "
5580 : "cannot be of type %s",
5581 4 : gfc_current_intrinsic_arg[0]->name,
5582 : gfc_current_intrinsic,
5583 : &a->where, gfc_typename (a));
5584 4 : return false;
5585 : }
5586 :
5587 769 : if (!(gfc_type_is_extensible (a->ts.u.derived) || UNLIMITED_POLY (a)))
5588 : {
5589 0 : gfc_error ("%qs argument of %qs intrinsic at %L "
5590 : "must be of an extensible type",
5591 0 : gfc_current_intrinsic_arg[0]->name, gfc_current_intrinsic,
5592 : &a->where);
5593 0 : return false;
5594 : }
5595 :
5596 769 : if (b->ts.type != BT_DERIVED && b->ts.type != BT_CLASS)
5597 : {
5598 0 : gfc_error ("%qs argument of %qs intrinsic at %L "
5599 : "cannot be of type %s",
5600 0 : gfc_current_intrinsic_arg[0]->name,
5601 : gfc_current_intrinsic,
5602 : &b->where, gfc_typename (b));
5603 0 : return false;
5604 : }
5605 :
5606 769 : if (!(gfc_type_is_extensible (b->ts.u.derived) || UNLIMITED_POLY (b)))
5607 : {
5608 2 : gfc_error ("%qs argument of %qs intrinsic at %L "
5609 : "must be of an extensible type",
5610 2 : gfc_current_intrinsic_arg[1]->name, gfc_current_intrinsic,
5611 : &b->where);
5612 2 : return false;
5613 : }
5614 :
5615 : return true;
5616 : }
5617 :
5618 :
5619 : bool
5620 84 : gfc_check_scale (gfc_expr *x, gfc_expr *i)
5621 : {
5622 84 : if (!type_check (x, 0, BT_REAL))
5623 : return false;
5624 :
5625 84 : if (!type_check (i, 1, BT_INTEGER))
5626 : return false;
5627 :
5628 : return true;
5629 : }
5630 :
5631 :
5632 : bool
5633 418 : gfc_check_scan (gfc_expr *x, gfc_expr *y, gfc_expr *z, gfc_expr *kind)
5634 : {
5635 418 : if (!type_check (x, 0, BT_CHARACTER))
5636 : return false;
5637 :
5638 418 : if (!type_check (y, 1, BT_CHARACTER))
5639 : return false;
5640 :
5641 418 : if (z != NULL && !type_check (z, 2, BT_LOGICAL))
5642 : return false;
5643 :
5644 418 : if (!kind_check (kind, 3, BT_INTEGER))
5645 : return false;
5646 418 : if (kind && !gfc_notify_std (GFC_STD_F2003, "%qs intrinsic "
5647 : "with KIND argument at %L",
5648 : gfc_current_intrinsic, &kind->where))
5649 : return false;
5650 :
5651 418 : if (!same_type_check (x, 0, y, 1))
5652 : return false;
5653 :
5654 : return true;
5655 : }
5656 :
5657 : bool
5658 102 : gfc_check_split (gfc_expr *string, gfc_expr *set, gfc_expr *pos, gfc_expr *back)
5659 : {
5660 102 : if (!type_check (string, 0, BT_CHARACTER))
5661 : return false;
5662 :
5663 102 : if (!type_check (set, 1, BT_CHARACTER))
5664 : return false;
5665 :
5666 102 : if (!type_check (pos, 2, BT_INTEGER) || !scalar_check (pos, 2))
5667 : return false;
5668 :
5669 102 : if (back != NULL
5670 102 : && (!type_check (back, 3, BT_LOGICAL) || !scalar_check (back, 3)))
5671 : return false;
5672 :
5673 102 : if (!same_type_check (string, 0, set, 1))
5674 : return false;
5675 :
5676 : return true;
5677 : }
5678 :
5679 : bool
5680 32 : gfc_check_secnds (gfc_expr *r)
5681 : {
5682 32 : if (!type_check (r, 0, BT_REAL))
5683 : return false;
5684 :
5685 32 : if (!kind_value_check (r, 0, 4))
5686 : return false;
5687 :
5688 32 : if (!scalar_check (r, 0))
5689 : return false;
5690 :
5691 : return true;
5692 : }
5693 :
5694 :
5695 : bool
5696 251 : gfc_check_selected_char_kind (gfc_expr *name)
5697 : {
5698 251 : if (!type_check (name, 0, BT_CHARACTER))
5699 : return false;
5700 :
5701 250 : if (!kind_value_check (name, 0, gfc_default_character_kind))
5702 : return false;
5703 :
5704 248 : if (!scalar_check (name, 0))
5705 : return false;
5706 :
5707 : return true;
5708 : }
5709 :
5710 :
5711 : bool
5712 347 : gfc_check_selected_int_kind (gfc_expr *r)
5713 : {
5714 347 : if (!type_check (r, 0, BT_INTEGER))
5715 : return false;
5716 :
5717 347 : if (!scalar_check (r, 0))
5718 : return false;
5719 :
5720 : return true;
5721 : }
5722 :
5723 : bool
5724 725 : gfc_check_selected_real_kind (gfc_expr *p, gfc_expr *r, gfc_expr *radix)
5725 : {
5726 725 : if (p == NULL && r == NULL
5727 725 : && !gfc_notify_std (GFC_STD_F2008, "SELECTED_REAL_KIND with"
5728 : " neither %<P%> nor %<R%> argument at %L",
5729 : gfc_current_intrinsic_where))
5730 : return false;
5731 :
5732 724 : if (p)
5733 : {
5734 682 : if (!type_check (p, 0, BT_INTEGER))
5735 : return false;
5736 :
5737 682 : if (!scalar_check (p, 0))
5738 : return false;
5739 : }
5740 :
5741 723 : if (r)
5742 : {
5743 245 : if (!type_check (r, 1, BT_INTEGER))
5744 : return false;
5745 :
5746 245 : if (!scalar_check (r, 1))
5747 : return false;
5748 : }
5749 :
5750 722 : if (radix)
5751 : {
5752 53 : if (!type_check (radix, 1, BT_INTEGER))
5753 : return false;
5754 :
5755 53 : if (!scalar_check (radix, 1))
5756 : return false;
5757 :
5758 53 : if (!gfc_notify_std (GFC_STD_F2008, "%qs intrinsic with "
5759 : "RADIX argument at %L", gfc_current_intrinsic,
5760 : &radix->where))
5761 : return false;
5762 : }
5763 :
5764 : return true;
5765 : }
5766 :
5767 :
5768 : bool
5769 412 : gfc_check_set_exponent (gfc_expr *x, gfc_expr *i)
5770 : {
5771 412 : if (!type_check (x, 0, BT_REAL))
5772 : return false;
5773 :
5774 412 : if (!type_check (i, 1, BT_INTEGER))
5775 : return false;
5776 :
5777 : return true;
5778 : }
5779 :
5780 :
5781 : bool
5782 7562 : gfc_check_shape (gfc_expr *source, gfc_expr *kind)
5783 : {
5784 7562 : gfc_array_ref *ar;
5785 :
5786 7562 : if (gfc_invalid_null_arg (source))
5787 : return false;
5788 :
5789 7561 : if (!kind_check (kind, 1, BT_INTEGER))
5790 : return false;
5791 7560 : if (kind && !gfc_notify_std (GFC_STD_F2003, "%qs intrinsic "
5792 : "with KIND argument at %L",
5793 : gfc_current_intrinsic, &kind->where))
5794 : return false;
5795 :
5796 7560 : if (source->rank == 0 || source->expr_type != EXPR_VARIABLE)
5797 : return true;
5798 :
5799 7367 : if (source->ref == NULL)
5800 : return false;
5801 :
5802 7367 : ar = gfc_find_array_ref (source);
5803 :
5804 7367 : if (ar->as && ar->as->type == AS_ASSUMED_SIZE && ar->type == AR_FULL)
5805 : {
5806 1 : gfc_error ("%<source%> argument of %<shape%> intrinsic at %L must not be "
5807 : "an assumed size array", &source->where);
5808 1 : return false;
5809 : }
5810 :
5811 : return true;
5812 : }
5813 :
5814 :
5815 : bool
5816 6921 : gfc_check_shift (gfc_expr *i, gfc_expr *shift)
5817 : {
5818 6921 : if (flag_unsigned)
5819 : {
5820 156 : if (!type_check2 (i, 0, BT_INTEGER, BT_UNSIGNED))
5821 : return false;
5822 : }
5823 : else
5824 : {
5825 6765 : if (!type_check (i, 0, BT_INTEGER))
5826 : return false;
5827 : }
5828 :
5829 6921 : if (!type_check (shift, 0, BT_INTEGER))
5830 : return false;
5831 :
5832 6921 : if (!nonnegative_check ("SHIFT", shift))
5833 : return false;
5834 :
5835 6921 : if (!less_than_bitsize1 ("I", i, "SHIFT", shift, true))
5836 : return false;
5837 :
5838 : return true;
5839 : }
5840 :
5841 :
5842 : bool
5843 327 : gfc_check_sign (gfc_expr *a, gfc_expr *b)
5844 : {
5845 327 : if (!int_or_real_check (a, 0))
5846 : return false;
5847 :
5848 327 : if (!same_type_check (a, 0, b, 1))
5849 : return false;
5850 :
5851 : return true;
5852 : }
5853 :
5854 :
5855 : bool
5856 13039 : gfc_check_size (gfc_expr *array, gfc_expr *dim, gfc_expr *kind)
5857 : {
5858 13039 : if (!array_check (array, 0))
5859 : return false;
5860 :
5861 13033 : if (!dim_check (dim, 1, true))
5862 : return false;
5863 :
5864 13032 : if (!dim_rank_check (dim, array, 0))
5865 : return false;
5866 :
5867 13028 : if (!kind_check (kind, 2, BT_INTEGER))
5868 : return false;
5869 13027 : if (kind && !gfc_notify_std (GFC_STD_F2003, "%qs intrinsic "
5870 : "with KIND argument at %L",
5871 : gfc_current_intrinsic, &kind->where))
5872 : return false;
5873 :
5874 :
5875 : return true;
5876 : }
5877 :
5878 :
5879 : bool
5880 1881 : gfc_check_sizeof (gfc_expr *arg)
5881 : {
5882 1881 : if (gfc_invalid_null_arg (arg))
5883 : return false;
5884 :
5885 1880 : if (arg->ts.type == BT_PROCEDURE)
5886 : {
5887 5 : gfc_error ("%qs argument of %qs intrinsic at %L shall not be a procedure",
5888 5 : gfc_current_intrinsic_arg[0]->name, gfc_current_intrinsic,
5889 : &arg->where);
5890 5 : return false;
5891 : }
5892 :
5893 1875 : if (illegal_boz_arg (arg))
5894 : return false;
5895 :
5896 : /* TYPE(*) is acceptable if and only if it uses an array descriptor. */
5897 1874 : if (arg->ts.type == BT_ASSUMED
5898 197 : && (arg->symtree->n.sym->as == NULL
5899 196 : || (arg->symtree->n.sym->as->type != AS_ASSUMED_SHAPE
5900 196 : && arg->symtree->n.sym->as->type != AS_DEFERRED
5901 130 : && arg->symtree->n.sym->as->type != AS_ASSUMED_RANK)))
5902 : {
5903 1 : gfc_error ("%qs argument of %qs intrinsic at %L shall not be TYPE(*)",
5904 1 : gfc_current_intrinsic_arg[0]->name, gfc_current_intrinsic,
5905 : &arg->where);
5906 1 : return false;
5907 : }
5908 :
5909 1873 : if (arg->rank && arg->expr_type == EXPR_VARIABLE
5910 1123 : && arg->symtree->n.sym->as != NULL
5911 699 : && arg->symtree->n.sym->as->type == AS_ASSUMED_SIZE && arg->ref
5912 1 : && arg->ref->type == REF_ARRAY && arg->ref->u.ar.type == AR_FULL)
5913 : {
5914 1 : gfc_error ("%qs argument of %qs intrinsic at %L shall not be an "
5915 1 : "assumed-size array", gfc_current_intrinsic_arg[0]->name,
5916 : gfc_current_intrinsic, &arg->where);
5917 1 : return false;
5918 : }
5919 :
5920 : return true;
5921 : }
5922 :
5923 :
5924 : /* Check whether an expression is interoperable. When returning false,
5925 : msg is set to a string telling why the expression is not interoperable,
5926 : otherwise, it is set to NULL. The msg string can be used in diagnostics.
5927 : If c_loc is true, character with len > 1 are allowed (cf. Fortran
5928 : 2003corr5); additionally, assumed-shape/assumed-rank/deferred-shape
5929 : arrays are permitted. And if c_f_ptr is true, deferred-shape arrays
5930 : are permitted. */
5931 :
5932 : static bool
5933 4785 : is_c_interoperable (gfc_expr *expr, const char **msg, bool c_loc, bool c_f_ptr)
5934 : {
5935 4785 : *msg = NULL;
5936 :
5937 4785 : if (expr->expr_type == EXPR_NULL && expr->ts.type == BT_UNKNOWN)
5938 : {
5939 1 : *msg = _("NULL() is not interoperable");
5940 1 : return false;
5941 : }
5942 :
5943 4784 : if (expr->ts.type == BT_BOZ)
5944 : {
5945 1 : *msg = _("BOZ literal constant");
5946 1 : return false;
5947 : }
5948 :
5949 4783 : if (expr->ts.type == BT_CLASS)
5950 : {
5951 0 : *msg = _("Expression is polymorphic");
5952 0 : return false;
5953 : }
5954 :
5955 4783 : if (expr->ts.type == BT_DERIVED && !expr->ts.u.derived->attr.is_bind_c
5956 41 : && !expr->ts.u.derived->ts.is_iso_c)
5957 : {
5958 41 : *msg = _("Expression is a noninteroperable derived type");
5959 41 : return false;
5960 : }
5961 :
5962 4742 : if (expr->ts.type == BT_PROCEDURE)
5963 : {
5964 4 : *msg = _("Procedure unexpected as argument");
5965 4 : return false;
5966 : }
5967 :
5968 4738 : if (gfc_notification_std (GFC_STD_GNU) && expr->ts.type == BT_LOGICAL)
5969 : {
5970 : int i;
5971 24 : for (i = 0; gfc_logical_kinds[i].kind; i++)
5972 24 : if (gfc_logical_kinds[i].kind == expr->ts.kind)
5973 : return true;
5974 0 : *msg = _("Extension to use a non-C_Bool-kind LOGICAL");
5975 0 : return false;
5976 : }
5977 :
5978 5332 : if (gfc_notification_std (GFC_STD_GNU) && expr->ts.type == BT_CHARACTER
5979 4893 : && expr->ts.kind != 1)
5980 : {
5981 48 : *msg = _("Extension to use a non-C_CHAR-kind CHARACTER");
5982 48 : return false;
5983 : }
5984 :
5985 4678 : if (expr->ts.type == BT_CHARACTER) {
5986 127 : if (expr->ts.deferred)
5987 : {
5988 : /* TS 29113 allows deferred-length strings as dummy arguments,
5989 : but it is not an interoperable type. */
5990 1 : *msg = "Expression shall not be a deferred-length string";
5991 1 : return false;
5992 : }
5993 :
5994 126 : if (expr->ts.u.cl && expr->ts.u.cl->length
5995 203 : && !gfc_simplify_expr (expr->ts.u.cl->length, 0))
5996 0 : gfc_internal_error ("is_c_interoperable(): gfc_simplify_expr failed");
5997 :
5998 126 : if (!c_loc
5999 29 : && expr->ts.u.cl
6000 155 : && !gfc_length_one_character_type_p (&expr->ts))
6001 : {
6002 0 : *msg = _("Type shall have a character length of 1");
6003 0 : return false;
6004 : }
6005 : }
6006 :
6007 : /* Note: The following checks are about interoperatable variables, Fortran
6008 : 15.3.5/15.3.6. In intrinsics like C_LOC or in procedure interface, more
6009 : is allowed, e.g. assumed-shape arrays with TS 29113. */
6010 :
6011 4677 : if (gfc_is_coarray (expr))
6012 : {
6013 0 : *msg = _("Coarrays are not interoperable");
6014 0 : return false;
6015 : }
6016 :
6017 : /* Checks for C_SIZEOF need to take into account edits to 18-007r1, see
6018 : https://j3-fortran.org/doc/year/22/22-101r1.txt . */
6019 4677 : if (!c_loc && !c_f_ptr && expr->rank > 0 && expr->expr_type == EXPR_VARIABLE)
6020 : {
6021 101 : gfc_array_ref *ar = gfc_find_array_ref (expr);
6022 101 : if (ar->type == AR_FULL && ar->as->type == AS_ASSUMED_SIZE)
6023 : {
6024 2 : *msg = _("Assumed-size arrays are not interoperable");
6025 2 : return false;
6026 : }
6027 : }
6028 :
6029 : return true;
6030 : }
6031 :
6032 :
6033 : bool
6034 436 : gfc_check_c_sizeof (gfc_expr *arg)
6035 : {
6036 436 : const char *msg;
6037 :
6038 436 : if (!is_c_interoperable (arg, &msg, false, false))
6039 : {
6040 9 : gfc_error ("%qs argument of %qs intrinsic at %L must be an "
6041 : "interoperable data entity: %s",
6042 9 : gfc_current_intrinsic_arg[0]->name, gfc_current_intrinsic,
6043 : &arg->where, msg);
6044 9 : return false;
6045 : }
6046 :
6047 427 : if (arg->ts.type == BT_ASSUMED)
6048 : {
6049 0 : gfc_error ("%qs argument of %qs intrinsic at %L shall not be "
6050 : "TYPE(*)",
6051 0 : gfc_current_intrinsic_arg[0]->name, gfc_current_intrinsic,
6052 : &arg->where);
6053 0 : return false;
6054 : }
6055 :
6056 427 : if (arg->rank && arg->expr_type == EXPR_VARIABLE
6057 101 : && arg->symtree->n.sym->as != NULL
6058 99 : && arg->symtree->n.sym->as->type == AS_ASSUMED_SIZE && arg->ref
6059 1 : && arg->ref->type == REF_ARRAY && arg->ref->u.ar.type == AR_FULL)
6060 : {
6061 0 : gfc_error ("%qs argument of %qs intrinsic at %L shall not be an "
6062 0 : "assumed-size array", gfc_current_intrinsic_arg[0]->name,
6063 : gfc_current_intrinsic, &arg->where);
6064 0 : return false;
6065 : }
6066 :
6067 : return true;
6068 : }
6069 :
6070 :
6071 : /* Helper functions check_c_ptr_1 and check_c_ptr_2
6072 : used in gfc_check_c_associated. */
6073 :
6074 : static inline
6075 2094 : bool check_c_ptr_1 (gfc_expr *c_ptr_1)
6076 : {
6077 2094 : if ((c_ptr_1->ts.type == BT_VOID)
6078 3 : && (c_ptr_1->expr_type == EXPR_FUNCTION))
6079 : return true;
6080 :
6081 2091 : if (c_ptr_1->ts.type != BT_DERIVED
6082 2082 : || c_ptr_1->ts.u.derived->from_intmod != INTMOD_ISO_C_BINDING
6083 2081 : || (c_ptr_1->ts.u.derived->intmod_sym_id != ISOCBINDING_PTR
6084 159 : && c_ptr_1->ts.u.derived->intmod_sym_id != ISOCBINDING_FUNPTR))
6085 10 : goto check_1_error;
6086 :
6087 2081 : if ((c_ptr_1->ts.type == BT_DERIVED)
6088 : && (c_ptr_1->expr_type == EXPR_STRUCTURE)
6089 : && (c_ptr_1->ts.u.derived->intmod_sym_id
6090 : == ISOCBINDING_NULL_FUNPTR))
6091 : goto check_1_error;
6092 :
6093 2081 : if (scalar_check (c_ptr_1, 0))
6094 : return true;
6095 : else
6096 : /* Return since the check_1_error message may not apply here. */
6097 : return false;
6098 :
6099 10 : check_1_error:
6100 :
6101 10 : gfc_error ("Argument C_PTR_1 at %L to C_ASSOCIATED shall have the "
6102 : "type TYPE(C_PTR) or TYPE(C_FUNPTR)", &c_ptr_1->where);
6103 10 : return false;
6104 : }
6105 :
6106 : static inline
6107 398 : bool check_c_ptr_2 (gfc_expr *c_ptr_1, gfc_expr *c_ptr_2)
6108 : {
6109 398 : switch (c_ptr_2->ts.type)
6110 : {
6111 4 : case BT_VOID:
6112 4 : if (c_ptr_2->expr_type == EXPR_FUNCTION)
6113 : {
6114 4 : if ((c_ptr_1->ts.type == BT_DERIVED)
6115 4 : && c_ptr_1->expr_type == EXPR_STRUCTURE
6116 2 : && (c_ptr_1->ts.u.derived->intmod_sym_id
6117 : == ISOCBINDING_FUNPTR))
6118 1 : goto check_2_error;
6119 : }
6120 : break;
6121 :
6122 387 : case BT_DERIVED:
6123 387 : if ((c_ptr_2->expr_type == EXPR_STRUCTURE)
6124 3 : && (c_ptr_2->ts.u.derived->intmod_sym_id == ISOCBINDING_PTR)
6125 1 : && (c_ptr_1->ts.type == BT_VOID)
6126 1 : && (c_ptr_1->expr_type == EXPR_FUNCTION))
6127 1 : return scalar_check (c_ptr_2, 1);
6128 :
6129 386 : if ((c_ptr_2->expr_type == EXPR_STRUCTURE)
6130 2 : && (c_ptr_1->ts.type == BT_VOID)
6131 1 : && (c_ptr_1->expr_type == EXPR_FUNCTION))
6132 1 : goto check_2_error;
6133 :
6134 385 : if (c_ptr_2->ts.u.derived->from_intmod != INTMOD_ISO_C_BINDING)
6135 1 : goto check_2_error;
6136 :
6137 384 : if (c_ptr_1->ts.type == BT_DERIVED
6138 382 : && (c_ptr_1->ts.u.derived->intmod_sym_id
6139 382 : != c_ptr_2->ts.u.derived->intmod_sym_id))
6140 2 : goto check_2_error;
6141 : break;
6142 :
6143 7 : default:
6144 7 : goto check_2_error;
6145 : }
6146 :
6147 385 : if (scalar_check (c_ptr_2, 1))
6148 : return true;
6149 : else
6150 : /* Return since the check_2_error message may not apply here. */
6151 : return false;
6152 :
6153 12 : check_2_error:
6154 :
6155 12 : gfc_error ("Argument C_PTR_2 at %L to C_ASSOCIATED shall have the "
6156 : "same type as C_PTR_1, found %s instead of %s", &c_ptr_2->where,
6157 : gfc_typename (&c_ptr_2->ts), gfc_typename (&c_ptr_1->ts));
6158 :
6159 12 : return false;
6160 : }
6161 :
6162 :
6163 : bool
6164 2106 : gfc_check_c_associated (gfc_expr *c_ptr_1, gfc_expr *c_ptr_2)
6165 : {
6166 2106 : if (c_ptr_2)
6167 : {
6168 398 : if (check_c_ptr_2 (c_ptr_1, c_ptr_2))
6169 386 : return check_c_ptr_1 (c_ptr_1);
6170 : else
6171 : return false;
6172 : }
6173 : else
6174 1708 : return check_c_ptr_1 (c_ptr_1);
6175 : }
6176 :
6177 :
6178 : bool
6179 648 : gfc_check_c_f_pointer (gfc_expr *cptr, gfc_expr *fptr, gfc_expr *shape,
6180 : gfc_expr *lower)
6181 : {
6182 648 : symbol_attribute attr;
6183 648 : const char *msg;
6184 :
6185 648 : if (cptr->ts.type != BT_DERIVED
6186 648 : || cptr->ts.u.derived->from_intmod != INTMOD_ISO_C_BINDING
6187 648 : || cptr->ts.u.derived->intmod_sym_id != ISOCBINDING_PTR)
6188 : {
6189 2 : gfc_error ("Argument CPTR at %L to C_F_POINTER shall have the "
6190 : "type TYPE(C_PTR)", &cptr->where);
6191 2 : return false;
6192 : }
6193 :
6194 646 : if (!scalar_check (cptr, 0))
6195 : return false;
6196 :
6197 646 : attr = gfc_expr_attr (fptr);
6198 :
6199 646 : if (!attr.pointer)
6200 : {
6201 1 : gfc_error ("Argument FPTR at %L to C_F_POINTER must be a pointer",
6202 : &fptr->where);
6203 1 : return false;
6204 : }
6205 :
6206 645 : if (fptr->ts.type == BT_CLASS)
6207 : {
6208 1 : gfc_error ("FPTR argument at %L to C_F_POINTER shall not be polymorphic",
6209 : &fptr->where);
6210 1 : return false;
6211 : }
6212 :
6213 644 : if (gfc_is_coindexed (fptr))
6214 : {
6215 0 : gfc_error ("Argument FPTR at %L to C_F_POINTER shall not be "
6216 : "coindexed", &fptr->where);
6217 0 : return false;
6218 : }
6219 :
6220 644 : if (fptr->rank == 0 && shape)
6221 : {
6222 1 : gfc_error ("Unexpected SHAPE argument at %L to C_F_POINTER with scalar "
6223 : "FPTR", &fptr->where);
6224 1 : return false;
6225 : }
6226 643 : else if (fptr->rank && !shape)
6227 : {
6228 1 : gfc_error ("Expected SHAPE argument to C_F_POINTER with array "
6229 : "FPTR at %L", &fptr->where);
6230 1 : return false;
6231 : }
6232 :
6233 642 : if (shape && !rank_check (shape, 2, 1))
6234 : return false;
6235 :
6236 641 : if (shape && !type_check (shape, 2, BT_INTEGER))
6237 : return false;
6238 :
6239 640 : if (shape)
6240 : {
6241 492 : mpz_t size;
6242 492 : if (gfc_array_size (shape, &size))
6243 : {
6244 491 : if (mpz_cmp_ui (size, fptr->rank) != 0)
6245 : {
6246 0 : mpz_clear (size);
6247 0 : gfc_error ("SHAPE argument at %L to C_F_POINTER must have the same "
6248 : "size as the RANK of FPTR", &shape->where);
6249 0 : return false;
6250 : }
6251 491 : mpz_clear (size);
6252 : }
6253 : }
6254 :
6255 640 : if (lower
6256 640 : && !gfc_notify_std (GFC_STD_F2023, "LOWER argument at %L to C_F_POINTER",
6257 : &lower->where))
6258 : return false;
6259 :
6260 639 : if (!shape && lower)
6261 : {
6262 0 : gfc_error ("Unexpected LOWER argument at %L to C_F_POINTER "
6263 : "with scalar FPTR",
6264 : &lower->where);
6265 0 : return false;
6266 : }
6267 :
6268 639 : if (lower && !rank_check (lower, 3, 1))
6269 : return false;
6270 :
6271 638 : if (lower && !type_check (lower, 3, BT_INTEGER))
6272 : return false;
6273 :
6274 637 : if (lower)
6275 : {
6276 12 : mpz_t size;
6277 12 : if (gfc_array_size (lower, &size))
6278 : {
6279 12 : if (mpz_cmp_ui (size, fptr->rank) != 0)
6280 : {
6281 0 : mpz_clear (size);
6282 0 : gfc_error (
6283 : "LOWER argument at %L to C_F_POINTER must have the same "
6284 : "size as the RANK of FPTR",
6285 : &lower->where);
6286 0 : return false;
6287 : }
6288 12 : mpz_clear (size);
6289 : }
6290 : }
6291 :
6292 637 : if (fptr->ts.type == BT_CLASS)
6293 : {
6294 0 : gfc_error ("Polymorphic FPTR at %L to C_F_POINTER", &fptr->where);
6295 0 : return false;
6296 : }
6297 :
6298 637 : if (fptr->ts.type == BT_PROCEDURE && attr.function)
6299 : {
6300 2 : gfc_error ("FPTR argument to C_F_POINTER at %L is a function "
6301 : "returning a pointer", &fptr->where);
6302 2 : return false;
6303 : }
6304 :
6305 635 : if (fptr->rank > 0 && !is_c_interoperable (fptr, &msg, false, true))
6306 13 : return gfc_notify_std (GFC_STD_F2018,
6307 : "Noninteroperable array FPTR argument to "
6308 13 : "C_F_POINTER at %L: %s", &fptr->where, msg);
6309 :
6310 : return true;
6311 : }
6312 :
6313 :
6314 : bool
6315 86 : gfc_check_c_f_procpointer (gfc_expr *cptr, gfc_expr *fptr)
6316 : {
6317 86 : symbol_attribute attr;
6318 :
6319 86 : if (cptr->ts.type != BT_DERIVED
6320 86 : || cptr->ts.u.derived->from_intmod != INTMOD_ISO_C_BINDING
6321 86 : || cptr->ts.u.derived->intmod_sym_id != ISOCBINDING_FUNPTR)
6322 : {
6323 3 : gfc_error ("Argument CPTR at %L to C_F_PROCPOINTER shall have the "
6324 : "type TYPE(C_FUNPTR)", &cptr->where);
6325 3 : return false;
6326 : }
6327 :
6328 83 : if (!scalar_check (cptr, 0))
6329 : return false;
6330 :
6331 83 : attr = gfc_expr_attr (fptr);
6332 :
6333 83 : if (!attr.proc_pointer)
6334 : {
6335 0 : gfc_error ("Argument FPTR at %L to C_F_PROCPOINTER shall be a procedure "
6336 : "pointer", &fptr->where);
6337 0 : return false;
6338 : }
6339 :
6340 83 : if (gfc_is_coindexed (fptr))
6341 : {
6342 0 : gfc_error ("Argument FPTR at %L to C_F_PROCPOINTER shall not be "
6343 : "coindexed", &fptr->where);
6344 0 : return false;
6345 : }
6346 :
6347 83 : if (!attr.is_bind_c)
6348 47 : return gfc_notify_std (GFC_STD_F2018, "Noninteroperable procedure "
6349 47 : "pointer at %L to C_F_PROCPOINTER", &fptr->where);
6350 :
6351 : return true;
6352 : }
6353 :
6354 :
6355 : /* Handle both forms of this intrinsic, differentiated by whether
6356 : the first argument is a scalar or array. */
6357 :
6358 : bool
6359 74 : gfc_check_c_f_strpointer (gfc_expr *arg0, gfc_expr *fstrptr,
6360 : gfc_expr *nchars)
6361 : {
6362 74 : bool arg0_is_scalar = false;
6363 74 : const char *arg0name = "cstrarray";
6364 :
6365 74 : if (arg0->rank == 0)
6366 : {
6367 22 : arg0_is_scalar = true;
6368 22 : arg0name = "cstrptr";
6369 :
6370 : /* cstrptr is a scalar of type c_ptr. It is an intent in argument
6371 : holding the C address of a contiguous array s of nchars characters.
6372 : Its value must not be the C address of a Fortran variable without
6373 : the target attribute. */
6374 22 : if (arg0->ts.type != BT_DERIVED
6375 22 : || arg0->ts.u.derived->from_intmod != INTMOD_ISO_C_BINDING
6376 22 : || arg0->ts.u.derived->intmod_sym_id != ISOCBINDING_PTR)
6377 : {
6378 0 : gfc_error ("%qs argument of %qs intrinsic at %L shall be "
6379 : "a scalar of type C_PTR",
6380 : arg0name, gfc_current_intrinsic, &arg0->where);
6381 0 : return false;
6382 : }
6383 :
6384 22 : if (!nchars)
6385 : {
6386 1 : gfc_error ("%qs argument of %qs intrinsic shall be present "
6387 : "when the %qs argument at %L is a C_PTR",
6388 1 : gfc_current_intrinsic_arg[2]->name,
6389 : gfc_current_intrinsic, arg0name, &arg0->where);
6390 1 : return false;
6391 : }
6392 : }
6393 : else
6394 : {
6395 : /* arg0 is a rank-one character array of kind c_char and character
6396 : length one. It is an intent in argument. Its actual argument
6397 : must be simply contiguous and have the target attribute. */
6398 52 : if (arg0->rank != 1
6399 51 : || arg0->ts.type != BT_CHARACTER
6400 51 : || arg0->ts.kind != gfc_default_character_kind
6401 103 : || get_ul_from_cst_cl (arg0->ts.u.cl) != 1)
6402 : {
6403 2 : gfc_error ("%qs argument of %qs intrinsic at %L shall be "
6404 : "a rank-one character array of kind C_CHAR and "
6405 : "character length one",
6406 : arg0name, gfc_current_intrinsic, &arg0->where);
6407 2 : return false;
6408 : }
6409 50 : if (!gfc_is_simply_contiguous (arg0, true, false))
6410 : {
6411 1 : gfc_error ("%qs argument of %qs intrinsic at %L shall be "
6412 : "simply contiguous",
6413 : arg0name, gfc_current_intrinsic, &arg0->where);
6414 1 : return false;
6415 : }
6416 49 : if (!gfc_expr_attr (arg0).target)
6417 : {
6418 1 : gfc_error ("%qs argument of %qs intrinsic at %L shall have "
6419 : "the TARGET attribute",
6420 : arg0name, gfc_current_intrinsic, &arg0->where);
6421 1 : return false;
6422 : }
6423 :
6424 : /* If cstrarray is assumed-size, nchars must be present. */
6425 48 : if (!nchars)
6426 : {
6427 36 : gfc_array_ref *ar = gfc_find_array_ref (arg0);
6428 36 : if (ar->as && ar->as->type == AS_ASSUMED_SIZE
6429 4 : && (ar->type == AR_FULL || ar->end[0] == nullptr))
6430 : {
6431 1 : gfc_error ("%qs argument of %qs intrinsic shall be present "
6432 : "when the %qs argument at %L is assumed-size",
6433 1 : gfc_current_intrinsic_arg[2]->name,
6434 : gfc_current_intrinsic, arg0name, &arg0->where);
6435 1 : return false;
6436 : }
6437 : }
6438 : }
6439 :
6440 : /* fstrptr is a scalar deferred-length character pointer of kind c_char.
6441 : It is an intent out argument [...] */
6442 68 : if (fstrptr->rank != 0
6443 68 : || fstrptr->ts.type != BT_CHARACTER
6444 68 : || fstrptr->ts.kind != gfc_default_character_kind
6445 68 : || !fstrptr->ts.deferred
6446 136 : || !gfc_expr_attr (fstrptr).pointer)
6447 : {
6448 2 : gfc_error ("%qs argument of %qs intrinsic at %L shall be "
6449 : "a scalar deferred-length character pointer of kind C_CHAR",
6450 2 : gfc_current_intrinsic_arg[1]->name, gfc_current_intrinsic,
6451 : &fstrptr->where);
6452 2 : return false;
6453 : }
6454 66 : if (gfc_expr_attr (fstrptr).intent == INTENT_IN)
6455 : {
6456 1 : gfc_error ("%qs argument of %qs intrinsic at %L cannot be INTENT(IN)",
6457 1 : gfc_current_intrinsic_arg[1]->name, gfc_current_intrinsic,
6458 : &fstrptr->where);
6459 1 : return false;
6460 : }
6461 :
6462 : /* For the array form: nchars is an optional integer scalar with intent in.
6463 : If nchars is present, its value must be nonnegative and not greater
6464 : than the size of cstrarray.
6465 : For the scalar form: nchars is an integer scalar with intent in. Its
6466 : value must be nonnegative. */
6467 65 : if (!nchars)
6468 : return true;
6469 30 : if (nchars->rank != 0 || nchars->ts.type != BT_INTEGER)
6470 : {
6471 0 : gfc_error ("%qs argument of %qs intrinsic at %L shall be "
6472 : "a scalar integer",
6473 0 : gfc_current_intrinsic_arg[2]->name, gfc_current_intrinsic,
6474 : &nchars->where);
6475 0 : return false;
6476 : }
6477 30 : if (nchars->expr_type != EXPR_CONSTANT)
6478 : return true;
6479 12 : if (!nonnegative_check (gfc_current_intrinsic_arg[2]->name, nchars))
6480 : return false;
6481 12 : if (!arg0_is_scalar)
6482 : {
6483 0 : mpz_t asize;
6484 0 : if (gfc_array_size (arg0, &asize)
6485 0 : && mpz_cmp (nchars->value.integer, asize) > 0)
6486 : {
6487 0 : gfc_error ("%qs at %L must not be greater than the size of %qs",
6488 0 : gfc_current_intrinsic_arg[2]->name, &nchars->where,
6489 : arg0name);
6490 0 : return false;
6491 : }
6492 : }
6493 :
6494 : return true;
6495 : }
6496 :
6497 : bool
6498 269 : gfc_check_c_funloc (gfc_expr *x)
6499 : {
6500 269 : symbol_attribute attr;
6501 :
6502 269 : if (gfc_is_coindexed (x))
6503 : {
6504 0 : gfc_error ("Argument X at %L to C_FUNLOC shall not be "
6505 : "coindexed", &x->where);
6506 0 : return false;
6507 : }
6508 :
6509 269 : attr = gfc_expr_attr (x);
6510 :
6511 269 : if (!attr.proc_pointer && x->expr_type == EXPR_VARIABLE
6512 240 : && x->symtree->n.sym == x->symtree->n.sym->result)
6513 74 : for (gfc_namespace *ns = gfc_current_ns; ns; ns = ns->parent)
6514 46 : if (x->symtree->n.sym == ns->proc_name)
6515 : {
6516 3 : gfc_error ("Function result %qs at %L is invalid as X argument "
6517 : "to C_FUNLOC", x->symtree->n.sym->name, &x->where);
6518 3 : return false;
6519 : }
6520 :
6521 266 : if (attr.flavor != FL_PROCEDURE)
6522 : {
6523 1 : gfc_error ("Argument X at %L to C_FUNLOC shall be a procedure "
6524 : "or a procedure pointer", &x->where);
6525 1 : return false;
6526 : }
6527 :
6528 265 : if (!attr.is_bind_c)
6529 100 : return gfc_notify_std (GFC_STD_F2018, "Noninteroperable procedure "
6530 100 : "at %L to C_FUNLOC", &x->where);
6531 : return true;
6532 : }
6533 :
6534 :
6535 : bool
6536 3867 : gfc_check_c_loc (gfc_expr *x)
6537 : {
6538 3867 : symbol_attribute attr;
6539 3867 : const char *msg;
6540 :
6541 3867 : if (gfc_is_coindexed (x))
6542 : {
6543 1 : gfc_error ("Argument X at %L to C_LOC shall not be coindexed", &x->where);
6544 1 : return false;
6545 : }
6546 :
6547 3866 : if (x->ts.type == BT_CLASS)
6548 : {
6549 1 : gfc_error ("X argument at %L to C_LOC shall not be polymorphic",
6550 : &x->where);
6551 1 : return false;
6552 : }
6553 :
6554 3865 : attr = gfc_expr_attr (x);
6555 :
6556 3865 : if (!attr.pointer
6557 2469 : && (x->expr_type != EXPR_VARIABLE || !attr.target
6558 2465 : || attr.flavor == FL_PARAMETER))
6559 : {
6560 4 : gfc_error ("Argument X at %L to C_LOC shall have either "
6561 : "the POINTER or the TARGET attribute", &x->where);
6562 4 : return false;
6563 : }
6564 :
6565 3861 : if (x->ts.type == BT_CHARACTER
6566 3861 : && gfc_var_strlen (x) == 0)
6567 : {
6568 0 : gfc_error ("Argument X at %L to C_LOC shall be not be a zero-sized "
6569 : "string", &x->where);
6570 0 : return false;
6571 : }
6572 :
6573 3861 : if (!is_c_interoperable (x, &msg, true, false))
6574 : {
6575 76 : if (x->ts.type == BT_CLASS)
6576 : {
6577 0 : gfc_error ("Argument at %L to C_LOC shall not be polymorphic",
6578 : &x->where);
6579 0 : return false;
6580 : }
6581 :
6582 76 : if (x->rank
6583 76 : && !gfc_notify_std (GFC_STD_F2018,
6584 : "Noninteroperable array at %L as"
6585 : " argument to C_LOC: %s", &x->where, msg))
6586 : return false;
6587 : }
6588 3785 : else if (x->rank > 0 && gfc_notification_std (GFC_STD_F2008))
6589 : {
6590 7 : gfc_array_ref *ar = gfc_find_array_ref (x);
6591 :
6592 6 : if (ar->as->type != AS_EXPLICIT && ar->as->type != AS_ASSUMED_SIZE
6593 5 : && !attr.allocatable
6594 11 : && !gfc_notify_std (GFC_STD_F2008,
6595 : "Array of interoperable type at %L "
6596 : "to C_LOC which is nonallocatable and neither "
6597 : "assumed size nor explicit size", &x->where))
6598 : return false;
6599 3 : else if (ar->type != AR_FULL
6600 3 : && !gfc_notify_std (GFC_STD_F2008, "Array section at %L "
6601 : "to C_LOC", &x->where))
6602 : return false;
6603 : }
6604 :
6605 : return true;
6606 : }
6607 :
6608 :
6609 : bool
6610 29 : gfc_check_sleep_sub (gfc_expr *seconds)
6611 : {
6612 29 : if (!type_check (seconds, 0, BT_INTEGER))
6613 : return false;
6614 :
6615 29 : if (!scalar_check (seconds, 0))
6616 : return false;
6617 :
6618 : return true;
6619 : }
6620 :
6621 : bool
6622 3 : gfc_check_sngl (gfc_expr *a)
6623 : {
6624 3 : if (!type_check (a, 0, BT_REAL))
6625 : return false;
6626 :
6627 3 : if ((a->ts.kind != gfc_default_double_kind)
6628 3 : && !gfc_notify_std (GFC_STD_GNU, "non double precision "
6629 : "REAL argument to %s intrinsic at %L",
6630 : gfc_current_intrinsic, &a->where))
6631 : return false;
6632 :
6633 : return true;
6634 : }
6635 :
6636 : bool
6637 747 : gfc_check_spread (gfc_expr *source, gfc_expr *dim, gfc_expr *ncopies)
6638 : {
6639 747 : if (gfc_invalid_null_arg (source))
6640 : return false;
6641 :
6642 746 : if (source->rank >= GFC_MAX_DIMENSIONS)
6643 : {
6644 0 : gfc_error ("%qs argument of %qs intrinsic at %L must be less "
6645 0 : "than rank %d", gfc_current_intrinsic_arg[0]->name,
6646 : gfc_current_intrinsic, &source->where, GFC_MAX_DIMENSIONS);
6647 :
6648 0 : return false;
6649 : }
6650 :
6651 746 : if (dim == NULL)
6652 : return false;
6653 :
6654 746 : if (!dim_check (dim, 1, false))
6655 : return false;
6656 :
6657 : /* dim_rank_check() does not apply here. */
6658 746 : if (dim
6659 746 : && dim->expr_type == EXPR_CONSTANT
6660 698 : && (mpz_cmp_ui (dim->value.integer, 1) < 0
6661 697 : || mpz_cmp_ui (dim->value.integer, source->rank + 1) > 0))
6662 : {
6663 2 : gfc_error ("%qs argument of %qs intrinsic at %L is not a valid "
6664 2 : "dimension index", gfc_current_intrinsic_arg[1]->name,
6665 : gfc_current_intrinsic, &dim->where);
6666 2 : return false;
6667 : }
6668 :
6669 744 : if (!type_check (ncopies, 2, BT_INTEGER))
6670 : return false;
6671 :
6672 744 : if (!scalar_check (ncopies, 2))
6673 : return false;
6674 :
6675 : return true;
6676 : }
6677 :
6678 :
6679 : /* Functions for checking FGETC, FPUTC, FGET and FPUT (subroutines and
6680 : functions). */
6681 :
6682 : bool
6683 157 : arg_strlen_is_zero (gfc_expr *c, int n)
6684 : {
6685 157 : if (gfc_var_strlen (c) == 0)
6686 : {
6687 2 : gfc_error ("%qs argument of %qs intrinsic at %L must have "
6688 2 : "length at least 1", gfc_current_intrinsic_arg[n]->name,
6689 : gfc_current_intrinsic, &c->where);
6690 2 : return true;
6691 : }
6692 : return false;
6693 : }
6694 :
6695 : bool
6696 155 : gfc_check_fgetputc_sub (gfc_expr *unit, gfc_expr *c, gfc_expr *status)
6697 : {
6698 155 : if (!type_check (unit, 0, BT_INTEGER))
6699 : return false;
6700 :
6701 155 : if (!scalar_check (unit, 0))
6702 : return false;
6703 :
6704 155 : if (!type_check (c, 1, BT_CHARACTER))
6705 : return false;
6706 155 : if (!kind_value_check (c, 1, gfc_default_character_kind))
6707 : return false;
6708 149 : if (strcmp (gfc_current_intrinsic, "fgetc") == 0
6709 149 : && !variable_check (c, 1, false))
6710 : return false;
6711 148 : if (arg_strlen_is_zero (c, 1))
6712 : return false;
6713 :
6714 147 : if (status == NULL)
6715 : return true;
6716 :
6717 58 : if (!type_check (status, 2, BT_INTEGER)
6718 58 : || !kind_value_check (status, 2, gfc_default_integer_kind)
6719 58 : || !scalar_check (status, 2)
6720 116 : || !variable_check (status, 2, false))
6721 2 : return false;
6722 :
6723 : return true;
6724 : }
6725 :
6726 :
6727 : bool
6728 71 : gfc_check_fgetputc (gfc_expr *unit, gfc_expr *c)
6729 : {
6730 71 : return gfc_check_fgetputc_sub (unit, c, NULL);
6731 : }
6732 :
6733 :
6734 : bool
6735 17 : gfc_check_fgetput_sub (gfc_expr *c, gfc_expr *status)
6736 : {
6737 17 : if (!type_check (c, 0, BT_CHARACTER))
6738 : return false;
6739 17 : if (!kind_value_check (c, 0, gfc_default_character_kind))
6740 : return false;
6741 11 : if (strcmp (gfc_current_intrinsic, "fget") == 0
6742 11 : && !variable_check (c, 0, false))
6743 : return false;
6744 9 : if (arg_strlen_is_zero (c, 0))
6745 : return false;
6746 :
6747 8 : if (status == NULL)
6748 : return true;
6749 :
6750 2 : if (!type_check (status, 1, BT_INTEGER)
6751 2 : || !kind_value_check (status, 1, gfc_default_integer_kind)
6752 2 : || !scalar_check (status, 1)
6753 4 : || !variable_check (status, 1, false))
6754 0 : return false;
6755 :
6756 : return true;
6757 : }
6758 :
6759 :
6760 : bool
6761 8 : gfc_check_fgetput (gfc_expr *c)
6762 : {
6763 8 : return gfc_check_fgetput_sub (c, NULL);
6764 : }
6765 :
6766 :
6767 : bool
6768 60 : gfc_check_fseek_sub (gfc_expr *unit, gfc_expr *offset, gfc_expr *whence, gfc_expr *status)
6769 : {
6770 60 : if (!type_check (unit, 0, BT_INTEGER))
6771 : return false;
6772 :
6773 60 : if (!scalar_check (unit, 0))
6774 : return false;
6775 :
6776 60 : if (!type_check (offset, 1, BT_INTEGER))
6777 : return false;
6778 :
6779 60 : if (!scalar_check (offset, 1))
6780 : return false;
6781 :
6782 60 : if (!type_check (whence, 2, BT_INTEGER))
6783 : return false;
6784 :
6785 60 : if (!scalar_check (whence, 2))
6786 : return false;
6787 :
6788 60 : if (status == NULL)
6789 : return true;
6790 :
6791 54 : if (!type_check (status, 3, BT_INTEGER))
6792 : return false;
6793 :
6794 54 : if (!kind_value_check (status, 3, 4))
6795 : return false;
6796 :
6797 54 : if (!scalar_check (status, 3))
6798 : return false;
6799 :
6800 : return true;
6801 : }
6802 :
6803 :
6804 :
6805 : bool
6806 43 : gfc_check_fstat (gfc_expr *unit, gfc_expr *values)
6807 : {
6808 43 : if (!type_check (unit, 0, BT_INTEGER))
6809 : return false;
6810 :
6811 43 : if (!scalar_check (unit, 0))
6812 : return false;
6813 :
6814 43 : if (!type_check (values, 1, BT_INTEGER))
6815 : return false;
6816 :
6817 43 : if (values->ts.kind != 4 && values->ts.kind != 8)
6818 : {
6819 1 : error_unsupported_kind (values, 1);
6820 1 : return false;
6821 : }
6822 :
6823 42 : if (!array_check (values, 1))
6824 : return false;
6825 :
6826 42 : if (!variable_check (values, 1, false))
6827 : return false;
6828 :
6829 40 : if (!array_size_check (values, 1, 13))
6830 : return false;
6831 :
6832 : return true;
6833 : }
6834 :
6835 :
6836 : bool
6837 28 : gfc_check_fstat_sub (gfc_expr *unit, gfc_expr *values, gfc_expr *status)
6838 : {
6839 28 : if (!gfc_check_fstat (unit, values))
6840 : return false;
6841 :
6842 25 : if (status == NULL)
6843 : return true;
6844 :
6845 19 : if (!type_check (status, 2, BT_INTEGER)
6846 19 : || !check_minrange4 (status, 2))
6847 : return false;
6848 :
6849 18 : if (!scalar_check (status, 2))
6850 : return false;
6851 :
6852 18 : if (!variable_check (status, 2, false))
6853 : return false;
6854 :
6855 : return true;
6856 : }
6857 :
6858 :
6859 : bool
6860 102 : gfc_check_ftell (gfc_expr *unit)
6861 : {
6862 102 : if (!type_check (unit, 0, BT_INTEGER))
6863 : return false;
6864 :
6865 102 : if (!scalar_check (unit, 0))
6866 : return false;
6867 :
6868 : return true;
6869 : }
6870 :
6871 :
6872 : bool
6873 36 : gfc_check_ftell_sub (gfc_expr *unit, gfc_expr *offset)
6874 : {
6875 36 : if (!type_check (unit, 0, BT_INTEGER))
6876 : return false;
6877 :
6878 36 : if (!scalar_check (unit, 0))
6879 : return false;
6880 :
6881 36 : if (!type_check (offset, 1, BT_INTEGER))
6882 : return false;
6883 :
6884 36 : if (!scalar_check (offset, 1))
6885 : return false;
6886 :
6887 : return true;
6888 : }
6889 :
6890 :
6891 : bool
6892 86 : gfc_check_stat (gfc_expr *name, gfc_expr *values)
6893 : {
6894 86 : if (!type_check (name, 0, BT_CHARACTER))
6895 : return false;
6896 86 : if (!kind_value_check (name, 0, gfc_default_character_kind))
6897 : return false;
6898 :
6899 80 : if (!type_check (values, 1, BT_INTEGER))
6900 : return false;
6901 :
6902 80 : if (values->ts.kind != 4 && values->ts.kind != 8)
6903 : {
6904 1 : error_unsupported_kind (values, 1);
6905 1 : return false;
6906 : }
6907 :
6908 79 : if (!array_check (values, 1))
6909 : return false;
6910 :
6911 79 : if (!variable_check (values, 1, false))
6912 : return false;
6913 :
6914 75 : if (!array_size_check (values, 1, 13))
6915 : return false;
6916 :
6917 : return true;
6918 : }
6919 :
6920 :
6921 : bool
6922 53 : gfc_check_stat_sub (gfc_expr *name, gfc_expr *values, gfc_expr *status)
6923 : {
6924 53 : if (!gfc_check_stat (name, values))
6925 : return false;
6926 :
6927 45 : if (status == NULL)
6928 : return true;
6929 :
6930 39 : if (!type_check (status, 2, BT_INTEGER)
6931 39 : || !check_minrange4 (status, 2))
6932 : return false;
6933 :
6934 38 : if (!scalar_check (status, 2))
6935 : return false;
6936 :
6937 38 : if (!variable_check (status, 2, false))
6938 : return false;
6939 :
6940 : return true;
6941 : }
6942 :
6943 :
6944 : /* Check the TEAM and TEAM_NUMBER arguments of IMAGE_INDEX and NUM_IMAGES,
6945 : moving a positional INTEGER argument to TEAM_NUMBER. N is the formal
6946 : index of TEAM. */
6947 :
6948 : static bool
6949 1776 : check_team_or_team_number (gfc_actual_arglist *team_arg,
6950 : gfc_actual_arglist *team_number_arg, int n)
6951 : {
6952 1776 : gfc_expr *team = team_arg->expr, *team_number = team_number_arg->expr;
6953 :
6954 1776 : if (team && team_number)
6955 : {
6956 4 : gfc_error ("%qs and %qs arguments of %qs intrinsic at %L are mutually "
6957 4 : "exclusive", gfc_current_intrinsic_arg[n]->name,
6958 4 : gfc_current_intrinsic_arg[n + 1]->name, gfc_current_intrinsic,
6959 : &team->where);
6960 4 : return false;
6961 : }
6962 :
6963 1772 : if (team && !team_arg->name && team->ts.type == BT_INTEGER)
6964 : {
6965 38 : team_number_arg->expr = team;
6966 38 : team_arg->expr = NULL;
6967 38 : team_number = team;
6968 38 : team = NULL;
6969 : }
6970 :
6971 1772 : if (!team && !team_number)
6972 : return true;
6973 :
6974 123 : if (!gfc_notify_std (GFC_STD_F2018,
6975 : "%<team%> or %<team_number%> argument to %qs at %L",
6976 : gfc_current_intrinsic,
6977 : team ? &team->where : &team_number->where))
6978 : return false;
6979 :
6980 121 : if (team)
6981 58 : return scalar_check (team, n) && team_type_check (team, n);
6982 :
6983 63 : return type_check (team_number, n + 1, BT_INTEGER)
6984 63 : && scalar_check (team_number, n + 1);
6985 : }
6986 :
6987 :
6988 : bool
6989 359 : gfc_check_image_index (gfc_actual_arglist *args)
6990 : {
6991 359 : gfc_expr *coarray = args->expr, *sub = args->next->expr;
6992 359 : mpz_t nelems;
6993 :
6994 359 : if (flag_coarray == GFC_FCOARRAY_NONE)
6995 : {
6996 0 : gfc_fatal_error ("Coarrays disabled at %L, use %<-fcoarray=%> to enable",
6997 : gfc_current_intrinsic_where);
6998 : return false;
6999 : }
7000 :
7001 359 : if (!coarray_check (coarray, 0))
7002 : return false;
7003 :
7004 358 : if (sub->rank != 1)
7005 : {
7006 1 : gfc_error ("%s argument to IMAGE_INDEX must be a rank one array at %L",
7007 1 : gfc_current_intrinsic_arg[1]->name, &sub->where);
7008 1 : return false;
7009 : }
7010 :
7011 357 : if (!type_check (sub, 1, BT_INTEGER))
7012 : return false;
7013 :
7014 356 : if (gfc_array_size (sub, &nelems))
7015 : {
7016 356 : if (mpz_cmp_ui (nelems, coarray->corank) != 0)
7017 : {
7018 3 : gfc_error ("The number of array elements of the SUB argument to "
7019 : "IMAGE_INDEX at %L shall be %d (corank) not %d",
7020 3 : &sub->where, coarray->corank, (int) mpz_get_si (nelems));
7021 3 : mpz_clear (nelems);
7022 3 : return false;
7023 : }
7024 353 : mpz_clear (nelems);
7025 : }
7026 :
7027 353 : return check_team_or_team_number (args->next->next,
7028 353 : args->next->next->next, 2);
7029 : }
7030 :
7031 : bool
7032 1423 : gfc_check_num_images (gfc_actual_arglist *args)
7033 : {
7034 1423 : if (flag_coarray == GFC_FCOARRAY_NONE)
7035 : {
7036 0 : gfc_fatal_error ("Coarrays disabled at %L, use %<-fcoarray=%> to enable",
7037 : gfc_current_intrinsic_where);
7038 : return false;
7039 : }
7040 :
7041 1423 : return check_team_or_team_number (args, args->next, 0);
7042 : }
7043 :
7044 :
7045 : bool
7046 68 : gfc_check_team_number (gfc_expr *team)
7047 : {
7048 68 : if (flag_coarray == GFC_FCOARRAY_NONE)
7049 : {
7050 0 : gfc_fatal_error ("Coarrays disabled at %L, use %<-fcoarray=%> to enable",
7051 : gfc_current_intrinsic_where);
7052 : return false;
7053 : }
7054 :
7055 68 : return !team || (scalar_check (team, 0) && team_type_check (team, 0));
7056 : }
7057 :
7058 :
7059 : bool
7060 2358 : gfc_check_this_image (gfc_actual_arglist *args)
7061 : {
7062 2358 : gfc_expr *coarray, *dim, *team, *cur;
7063 :
7064 2358 : coarray = dim = team = NULL;
7065 :
7066 2358 : if (flag_coarray == GFC_FCOARRAY_NONE)
7067 : {
7068 0 : gfc_fatal_error ("Coarrays disabled at %L, use %<-fcoarray=%> to enable",
7069 : gfc_current_intrinsic_where);
7070 : return false;
7071 : }
7072 :
7073 : /* Shortcut when no arguments are given. */
7074 2358 : if (!args->expr && !args->next->expr && !args->next->next->expr)
7075 : return true;
7076 :
7077 654 : cur = args->expr;
7078 :
7079 654 : if (cur)
7080 : {
7081 653 : gfc_push_suppress_errors ();
7082 653 : if (coarray_check (cur, 0))
7083 : coarray = cur;
7084 15 : else if (scalar_check (cur, 2) && team_type_check (cur, 2))
7085 : team = cur;
7086 : else
7087 : {
7088 1 : gfc_pop_suppress_errors ();
7089 1 : gfc_error ("First argument of %<this_image%> intrinsic at %L must be "
7090 : "a coarray "
7091 : "variable or an object of type %<team_type%> from the "
7092 : "intrinsic module "
7093 : "%<ISO_FORTRAN_ENV%>",
7094 : &cur->where);
7095 1 : return false;
7096 : }
7097 652 : gfc_pop_suppress_errors ();
7098 : }
7099 :
7100 653 : cur = args->next->expr;
7101 653 : if (cur)
7102 : {
7103 490 : gfc_push_suppress_errors ();
7104 490 : if (dim_check (cur, 1, true) && cur->corank == 0)
7105 : dim = cur;
7106 18 : else if (scalar_check (cur, 2) && team_type_check (cur, 2))
7107 : {
7108 14 : if (team)
7109 : {
7110 0 : gfc_pop_suppress_errors ();
7111 0 : goto team_type_error;
7112 : }
7113 : team = cur;
7114 : }
7115 : else
7116 : {
7117 4 : gfc_pop_suppress_errors ();
7118 4 : gfc_error ("Second argument of %<this_image%> intrinsic at %L must "
7119 : "be an %<INTEGER%> "
7120 : "typed scalar or an object of type %<team_type%> from the "
7121 : "intrinsic "
7122 : "module %<ISO_FORTRAN_ENV%>",
7123 : &cur->where);
7124 4 : return false;
7125 : }
7126 486 : gfc_pop_suppress_errors ();
7127 : }
7128 :
7129 649 : cur = args->next->next->expr;
7130 649 : if (cur)
7131 : {
7132 15 : if (team_type_check (cur, 2) && scalar_check (cur, 2))
7133 : {
7134 14 : if (team)
7135 0 : goto team_type_error;
7136 : team = cur;
7137 : }
7138 : else
7139 : return false;
7140 : }
7141 :
7142 648 : if (dim != NULL && coarray == NULL)
7143 : {
7144 1 : gfc_error ("%<dim%> argument without %<coarray%> argument not allowed "
7145 : "for %<this_image%> intrinsic at %L",
7146 : &dim->where);
7147 1 : return false;
7148 : }
7149 :
7150 647 : if (dim && !dim_corank_check (dim, coarray))
7151 : return false;
7152 :
7153 646 : if (team
7154 646 : && !gfc_notify_std (GFC_STD_F2018,
7155 : "%<team%> argument to %<this_image%> at %L",
7156 : &team->where))
7157 : return false;
7158 :
7159 646 : args->expr = coarray;
7160 646 : args->next->expr = dim;
7161 646 : args->next->next->expr = team;
7162 646 : return true;
7163 :
7164 0 : team_type_error:
7165 0 : gfc_error (
7166 : "At most one argument of type %<team_type%> from the intrinsic module "
7167 : "%<ISO_FORTRAN_ENV%> to %<this_image%> at %L allowed",
7168 : &cur->where);
7169 0 : return false;
7170 : }
7171 :
7172 : /* Calculate the sizes for transfer, used by gfc_check_transfer and also
7173 : by gfc_simplify_transfer. Return false if we cannot do so. */
7174 :
7175 : bool
7176 1015 : gfc_calculate_transfer_sizes (gfc_expr *source, gfc_expr *mold, gfc_expr *size,
7177 : size_t *source_size, size_t *result_size,
7178 : size_t *result_length_p)
7179 : {
7180 1015 : size_t result_elt_size;
7181 :
7182 1015 : if (source->expr_type == EXPR_FUNCTION)
7183 : return false;
7184 :
7185 1014 : if (size && size->expr_type != EXPR_CONSTANT)
7186 : return false;
7187 :
7188 : /* Calculate the size of the source. */
7189 1013 : if (!gfc_target_expr_size (source, source_size))
7190 : return false;
7191 :
7192 : /* Determine the size of the element. */
7193 1012 : if (!gfc_element_size (mold, &result_elt_size))
7194 : return false;
7195 :
7196 : /* If the storage size of SOURCE is greater than zero and MOLD is an array,
7197 : * a scalar with the type and type parameters of MOLD shall not have a
7198 : * storage size equal to zero.
7199 : * If MOLD is a scalar and SIZE is absent, the result is a scalar.
7200 : * If MOLD is an array and SIZE is absent, the result is an array and of
7201 : * rank one. Its size is as small as possible such that its physical
7202 : * representation is not shorter than that of SOURCE.
7203 : * If SIZE is present, the result is an array of rank one and size SIZE.
7204 : */
7205 999 : if (result_elt_size == 0 && *source_size > 0
7206 14 : && (mold->expr_type == EXPR_ARRAY || mold->rank))
7207 : {
7208 8 : gfc_error ("%<MOLD%> argument of %<TRANSFER%> intrinsic at %L is an "
7209 : "array and shall not have storage size 0 when %<SOURCE%> "
7210 : "argument has size greater than 0", &mold->where);
7211 8 : return false;
7212 : }
7213 :
7214 991 : if (result_elt_size == 0 && *source_size == 0 && !size)
7215 : {
7216 41 : *result_size = 0;
7217 41 : if (result_length_p)
7218 40 : *result_length_p = 0;
7219 : return true;
7220 : }
7221 :
7222 950 : if ((result_elt_size > 0 && (mold->expr_type == EXPR_ARRAY || mold->rank))
7223 752 : || size)
7224 : {
7225 198 : int result_length;
7226 :
7227 198 : if (size)
7228 195 : result_length = (size_t)mpz_get_ui (size->value.integer);
7229 : else
7230 : {
7231 135 : result_length = *source_size / result_elt_size;
7232 135 : if (result_length * result_elt_size < *source_size)
7233 3 : result_length += 1;
7234 : }
7235 :
7236 310 : *result_size = result_length * result_elt_size;
7237 310 : if (result_length_p)
7238 302 : *result_length_p = result_length;
7239 : }
7240 : else
7241 640 : *result_size = result_elt_size;
7242 :
7243 : return true;
7244 : }
7245 :
7246 :
7247 : bool
7248 2381 : gfc_check_transfer (gfc_expr *source, gfc_expr *mold, gfc_expr *size)
7249 : {
7250 2381 : size_t source_size;
7251 2381 : size_t result_size;
7252 :
7253 2381 : if (gfc_invalid_null_arg (source))
7254 : return false;
7255 :
7256 : /* SOURCE shall be a scalar or array of any type. */
7257 2378 : if (source->ts.type == BT_PROCEDURE
7258 3 : && source->symtree->n.sym->attr.subroutine == 1)
7259 : {
7260 1 : gfc_error ("%<SOURCE%> argument of %<TRANSFER%> intrinsic at %L "
7261 : "must not be a %s", &source->where,
7262 : gfc_basic_typename (source->ts.type));
7263 1 : return false;
7264 : }
7265 :
7266 2377 : if (source->ts.type == BT_BOZ && illegal_boz_arg (source))
7267 : return false;
7268 :
7269 2376 : if (mold->ts.type == BT_BOZ && illegal_boz_arg (mold))
7270 : return false;
7271 :
7272 2375 : if (gfc_invalid_null_arg (mold))
7273 : return false;
7274 :
7275 : /* MOLD shall be a scalar or array of any type. */
7276 2373 : if (mold->ts.type == BT_PROCEDURE
7277 2 : && mold->symtree->n.sym->attr.subroutine == 1)
7278 : {
7279 1 : gfc_error ("%<MOLD%> argument of %<TRANSFER%> intrinsic at %L "
7280 : "must not be a %s", &mold->where,
7281 : gfc_basic_typename (mold->ts.type));
7282 1 : return false;
7283 : }
7284 :
7285 2372 : if (mold->ts.type == BT_HOLLERITH)
7286 : {
7287 1 : gfc_error ("%<MOLD%> argument of %<TRANSFER%> intrinsic at %L must not be"
7288 : " %s", &mold->where, gfc_basic_typename (BT_HOLLERITH));
7289 1 : return false;
7290 : }
7291 :
7292 : /* SIZE (optional) shall be an integer scalar. The corresponding actual
7293 : argument shall not be an optional dummy argument. */
7294 2371 : if (size != NULL)
7295 : {
7296 438 : if (!type_check (size, 2, BT_INTEGER))
7297 : {
7298 1 : if (size->ts.type == BT_BOZ)
7299 1 : reset_boz (size);
7300 : return false;
7301 : }
7302 :
7303 437 : if (!scalar_check (size, 2))
7304 : return false;
7305 :
7306 437 : if (!nonoptional_check (size, 2))
7307 : return false;
7308 : }
7309 :
7310 2370 : if (!warn_surprising)
7311 : return true;
7312 :
7313 : /* If we can't calculate the sizes, we cannot check any more.
7314 : Return true for that case. */
7315 :
7316 52 : if (!gfc_calculate_transfer_sizes (source, mold, size, &source_size,
7317 : &result_size, NULL))
7318 : return true;
7319 :
7320 50 : if (source_size < result_size)
7321 6 : gfc_warning (OPT_Wsurprising,
7322 : "Intrinsic TRANSFER at %L has partly undefined result: "
7323 : "source size %zd < result size %zd", &source->where,
7324 : source_size, result_size);
7325 :
7326 : return true;
7327 : }
7328 :
7329 :
7330 : bool
7331 1343 : gfc_check_transpose (gfc_expr *matrix)
7332 : {
7333 1343 : if (!rank_check (matrix, 0, 2))
7334 : return false;
7335 :
7336 : return true;
7337 : }
7338 :
7339 :
7340 : bool
7341 7293 : gfc_check_ubound (gfc_expr *array, gfc_expr *dim, gfc_expr *kind)
7342 : {
7343 7293 : if (!array_check (array, 0))
7344 : return false;
7345 :
7346 7292 : if (!dim_check (dim, 1, false))
7347 : return false;
7348 :
7349 7292 : if (!dim_rank_check (dim, array, 0))
7350 : return false;
7351 :
7352 7290 : if (!kind_check (kind, 2, BT_INTEGER))
7353 : return false;
7354 7290 : if (kind && !gfc_notify_std (GFC_STD_F2003, "%qs intrinsic "
7355 : "with KIND argument at %L",
7356 : gfc_current_intrinsic, &kind->where))
7357 : return false;
7358 :
7359 : return true;
7360 : }
7361 :
7362 :
7363 : bool
7364 378 : gfc_check_ucobound (gfc_expr *coarray, gfc_expr *dim, gfc_expr *kind)
7365 : {
7366 378 : if (flag_coarray == GFC_FCOARRAY_NONE)
7367 : {
7368 0 : gfc_fatal_error ("Coarrays disabled at %L, use %<-fcoarray=%> to enable",
7369 : gfc_current_intrinsic_where);
7370 : return false;
7371 : }
7372 :
7373 378 : if (!coarray_check (coarray, 0))
7374 : return false;
7375 :
7376 374 : if (dim != NULL)
7377 : {
7378 233 : if (!dim_check (dim, 1, false))
7379 : return false;
7380 :
7381 233 : if (!dim_corank_check (dim, coarray))
7382 : return false;
7383 : }
7384 :
7385 374 : if (!kind_check (kind, 2, BT_INTEGER))
7386 : return false;
7387 :
7388 : return true;
7389 : }
7390 :
7391 :
7392 : bool
7393 393 : gfc_check_unpack (gfc_expr *vector, gfc_expr *mask, gfc_expr *field)
7394 : {
7395 393 : mpz_t vector_size;
7396 :
7397 393 : if (!rank_check (vector, 0, 1))
7398 : return false;
7399 :
7400 393 : if (!array_check (mask, 1))
7401 : return false;
7402 :
7403 393 : if (!type_check (mask, 1, BT_LOGICAL))
7404 : return false;
7405 :
7406 393 : if (!same_type_check (vector, 0, field, 2))
7407 : return false;
7408 :
7409 393 : gfc_simplify_expr (mask, 0);
7410 :
7411 393 : if (mask->expr_type == EXPR_ARRAY
7412 393 : && gfc_array_size (vector, &vector_size))
7413 : {
7414 40 : int mask_true_count = 0;
7415 40 : gfc_constructor *mask_ctor;
7416 40 : mask_ctor = gfc_constructor_first (mask->value.constructor);
7417 263 : while (mask_ctor)
7418 : {
7419 183 : if (mask_ctor->expr->expr_type != EXPR_CONSTANT)
7420 : {
7421 : mask_true_count = 0;
7422 : break;
7423 : }
7424 :
7425 183 : if (mask_ctor->expr->value.logical)
7426 78 : mask_true_count++;
7427 :
7428 183 : mask_ctor = gfc_constructor_next (mask_ctor);
7429 : }
7430 :
7431 40 : if (mpz_get_si (vector_size) < mask_true_count)
7432 : {
7433 1 : gfc_error ("%qs argument of %qs intrinsic at %L must "
7434 : "provide at least as many elements as there "
7435 : "are .TRUE. values in %qs (%ld/%d)",
7436 1 : gfc_current_intrinsic_arg[0]->name, gfc_current_intrinsic,
7437 1 : &vector->where, gfc_current_intrinsic_arg[1]->name,
7438 : mpz_get_si (vector_size), mask_true_count);
7439 1 : return false;
7440 : }
7441 :
7442 39 : mpz_clear (vector_size);
7443 : }
7444 :
7445 392 : if (mask->rank != field->rank && field->rank != 0)
7446 : {
7447 0 : gfc_error ("%qs argument of %qs intrinsic at %L must have "
7448 : "the same rank as %qs or be a scalar",
7449 0 : gfc_current_intrinsic_arg[2]->name, gfc_current_intrinsic,
7450 0 : &field->where, gfc_current_intrinsic_arg[1]->name);
7451 0 : return false;
7452 : }
7453 :
7454 392 : if (mask->rank == field->rank)
7455 : {
7456 : int i;
7457 712 : for (i = 0; i < field->rank; i++)
7458 452 : if (! identical_dimen_shape (mask, i, field, i))
7459 : {
7460 5 : gfc_error ("%qs and %qs arguments of %qs intrinsic at %L "
7461 : "must have identical shape.",
7462 5 : gfc_current_intrinsic_arg[2]->name,
7463 5 : gfc_current_intrinsic_arg[1]->name, gfc_current_intrinsic,
7464 : &field->where);
7465 : }
7466 : }
7467 :
7468 : return true;
7469 : }
7470 :
7471 :
7472 : bool
7473 250 : gfc_check_verify (gfc_expr *x, gfc_expr *y, gfc_expr *z, gfc_expr *kind)
7474 : {
7475 250 : if (!type_check (x, 0, BT_CHARACTER))
7476 : return false;
7477 :
7478 250 : if (!same_type_check (x, 0, y, 1))
7479 : return false;
7480 :
7481 250 : if (z != NULL && !type_check (z, 2, BT_LOGICAL))
7482 : return false;
7483 :
7484 250 : if (!kind_check (kind, 3, BT_INTEGER))
7485 : return false;
7486 250 : if (kind && !gfc_notify_std (GFC_STD_F2003, "%qs intrinsic "
7487 : "with KIND argument at %L",
7488 : gfc_current_intrinsic, &kind->where))
7489 : return false;
7490 :
7491 : return true;
7492 : }
7493 :
7494 :
7495 : bool
7496 2119 : gfc_check_trim (gfc_expr *x)
7497 : {
7498 2119 : if (!type_check (x, 0, BT_CHARACTER))
7499 : return false;
7500 :
7501 2119 : if (gfc_invalid_null_arg (x))
7502 : return false;
7503 :
7504 2118 : if (!scalar_check (x, 0))
7505 : return false;
7506 :
7507 : return true;
7508 : }
7509 :
7510 :
7511 : bool
7512 0 : gfc_check_ttynam (gfc_expr *unit)
7513 : {
7514 0 : if (!scalar_check (unit, 0))
7515 : return false;
7516 :
7517 0 : if (!type_check (unit, 0, BT_INTEGER))
7518 : return false;
7519 :
7520 : return true;
7521 : }
7522 :
7523 :
7524 : /************* Check functions for intrinsic subroutines *************/
7525 :
7526 : bool
7527 21 : gfc_check_cpu_time (gfc_expr *time)
7528 : {
7529 21 : if (!scalar_check (time, 0))
7530 : return false;
7531 :
7532 21 : if (!type_check (time, 0, BT_REAL))
7533 : return false;
7534 :
7535 21 : if (!variable_check (time, 0, false))
7536 : return false;
7537 :
7538 : return true;
7539 : }
7540 :
7541 :
7542 : bool
7543 183 : gfc_check_date_and_time (gfc_expr *date, gfc_expr *time,
7544 : gfc_expr *zone, gfc_expr *values)
7545 : {
7546 183 : if (date != NULL)
7547 : {
7548 71 : if (!type_check (date, 0, BT_CHARACTER))
7549 : return false;
7550 71 : if (!kind_value_check (date, 0, gfc_default_character_kind))
7551 : return false;
7552 69 : if (!scalar_check (date, 0))
7553 : return false;
7554 69 : if (!variable_check (date, 0, false))
7555 : return false;
7556 : }
7557 :
7558 181 : if (time != NULL)
7559 : {
7560 79 : if (!type_check (time, 1, BT_CHARACTER))
7561 : return false;
7562 79 : if (!kind_value_check (time, 1, gfc_default_character_kind))
7563 : return false;
7564 78 : if (!scalar_check (time, 1))
7565 : return false;
7566 78 : if (!variable_check (time, 1, false))
7567 : return false;
7568 : }
7569 :
7570 180 : if (zone != NULL)
7571 : {
7572 70 : if (!type_check (zone, 2, BT_CHARACTER))
7573 : return false;
7574 70 : if (!kind_value_check (zone, 2, gfc_default_character_kind))
7575 : return false;
7576 69 : if (!scalar_check (zone, 2))
7577 : return false;
7578 69 : if (!variable_check (zone, 2, false))
7579 : return false;
7580 : }
7581 :
7582 179 : if (values != NULL)
7583 : {
7584 100 : if (!type_check (values, 3, BT_INTEGER))
7585 : return false;
7586 100 : if (!array_check (values, 3))
7587 : return false;
7588 100 : if (!rank_check (values, 3, 1))
7589 : return false;
7590 100 : if (!variable_check (values, 3, false))
7591 : return false;
7592 100 : if (!array_size_check (values, 3, 8))
7593 : return false;
7594 :
7595 99 : if (values->ts.kind != gfc_default_integer_kind
7596 99 : && !gfc_notify_std (GFC_STD_F2018, "VALUES argument of "
7597 : "DATE_AND_TIME at %L has non-default kind",
7598 : &values->where))
7599 : return false;
7600 :
7601 : /* F2018:16.9.59 DATE_AND_TIME
7602 : "VALUES shall be a rank-one array of type integer
7603 : with a decimal exponent range of at least four."
7604 : This is a hard limit also required by the implementation in
7605 : libgfortran. */
7606 99 : if (values->ts.kind < 2)
7607 : {
7608 1 : gfc_error ("VALUES argument of DATE_AND_TIME at %L must have "
7609 : "a decimal exponent range of at least four",
7610 : &values->where);
7611 1 : return false;
7612 : }
7613 : }
7614 :
7615 : return true;
7616 : }
7617 :
7618 :
7619 : bool
7620 203 : gfc_check_mvbits (gfc_expr *from, gfc_expr *frompos, gfc_expr *len,
7621 : gfc_expr *to, gfc_expr *topos)
7622 : {
7623 :
7624 203 : if (flag_unsigned)
7625 : {
7626 24 : if (!type_check2 (from, 0, BT_INTEGER, BT_UNSIGNED))
7627 : return false;
7628 : }
7629 : else
7630 : {
7631 179 : if (!type_check (from, 0, BT_INTEGER))
7632 : return false;
7633 : }
7634 :
7635 203 : if (!type_check (frompos, 1, BT_INTEGER))
7636 : return false;
7637 :
7638 203 : if (!type_check (len, 2, BT_INTEGER))
7639 : return false;
7640 :
7641 203 : if (!same_type_check (from, 0, to, 3))
7642 : return false;
7643 :
7644 203 : if (!variable_check (to, 3, false))
7645 : return false;
7646 :
7647 203 : if (!type_check (topos, 4, BT_INTEGER))
7648 : return false;
7649 :
7650 203 : if (!nonnegative_check ("frompos", frompos))
7651 : return false;
7652 :
7653 202 : if (!nonnegative_check ("topos", topos))
7654 : return false;
7655 :
7656 201 : if (!nonnegative_check ("len", len))
7657 : return false;
7658 :
7659 200 : if (!less_than_bitsize2 ("from", from, "frompos", frompos, "len", len))
7660 : return false;
7661 :
7662 199 : if (!less_than_bitsize2 ("to", to, "topos", topos, "len", len))
7663 : return false;
7664 :
7665 : return true;
7666 : }
7667 :
7668 :
7669 : /* Check the arguments for RANDOM_INIT. */
7670 :
7671 : bool
7672 94 : gfc_check_random_init (gfc_expr *repeatable, gfc_expr *image_distinct)
7673 : {
7674 94 : if (!type_check (repeatable, 0, BT_LOGICAL))
7675 : return false;
7676 :
7677 93 : if (!scalar_check (repeatable, 0))
7678 : return false;
7679 :
7680 92 : if (!type_check (image_distinct, 1, BT_LOGICAL))
7681 : return false;
7682 :
7683 91 : if (!scalar_check (image_distinct, 1))
7684 : return false;
7685 :
7686 : return true;
7687 : }
7688 :
7689 :
7690 : bool
7691 542 : gfc_check_random_number (gfc_expr *harvest)
7692 : {
7693 542 : if (flag_unsigned)
7694 : {
7695 78 : if (!type_check2 (harvest, 0, BT_REAL, BT_UNSIGNED))
7696 : return false;
7697 : }
7698 : else
7699 464 : if (!type_check (harvest, 0, BT_REAL))
7700 : return false;
7701 :
7702 542 : if (!variable_check (harvest, 0, false))
7703 : return false;
7704 :
7705 : return true;
7706 : }
7707 :
7708 :
7709 : bool
7710 304 : gfc_check_random_seed (gfc_expr *size, gfc_expr *put, gfc_expr *get)
7711 : {
7712 304 : unsigned int nargs = 0, seed_size;
7713 304 : locus *where = NULL;
7714 304 : mpz_t put_size, get_size;
7715 :
7716 : /* Keep the number of bytes in sync with master_state in
7717 : libgfortran/intrinsics/random.c. */
7718 304 : seed_size = 32 / gfc_default_integer_kind;
7719 :
7720 304 : if (size != NULL)
7721 : {
7722 90 : if (size->expr_type != EXPR_VARIABLE
7723 90 : || !size->symtree->n.sym->attr.optional)
7724 68 : nargs++;
7725 :
7726 90 : if (!scalar_check (size, 0))
7727 : return false;
7728 :
7729 90 : if (!type_check (size, 0, BT_INTEGER))
7730 : return false;
7731 :
7732 90 : if (!variable_check (size, 0, false))
7733 : return false;
7734 :
7735 89 : if (!kind_value_check (size, 0, gfc_default_integer_kind))
7736 : return false;
7737 : }
7738 :
7739 303 : if (put != NULL)
7740 : {
7741 117 : if (put->expr_type != EXPR_VARIABLE
7742 117 : || !put->symtree->n.sym->attr.optional)
7743 : {
7744 96 : nargs++;
7745 96 : where = &put->where;
7746 : }
7747 :
7748 117 : if (!array_check (put, 1))
7749 : return false;
7750 :
7751 117 : if (!rank_check (put, 1, 1))
7752 : return false;
7753 :
7754 117 : if (!type_check (put, 1, BT_INTEGER))
7755 : return false;
7756 :
7757 117 : if (!kind_value_check (put, 1, gfc_default_integer_kind))
7758 : return false;
7759 :
7760 117 : if (gfc_array_size (put, &put_size))
7761 : {
7762 5 : if (mpz_get_ui (put_size) < seed_size)
7763 3 : gfc_error ("Size of %qs argument of %qs intrinsic at %L "
7764 : "too small (%i/%i)",
7765 3 : gfc_current_intrinsic_arg[1]->name,
7766 : gfc_current_intrinsic,
7767 3 : &put->where, (int) mpz_get_ui (put_size), seed_size);
7768 5 : mpz_clear (put_size);
7769 : }
7770 : }
7771 :
7772 303 : if (get != NULL)
7773 : {
7774 136 : if (get->expr_type != EXPR_VARIABLE
7775 136 : || !get->symtree->n.sym->attr.optional)
7776 : {
7777 115 : nargs++;
7778 115 : where = &get->where;
7779 : }
7780 :
7781 136 : if (!array_check (get, 2))
7782 : return false;
7783 :
7784 136 : if (!rank_check (get, 2, 1))
7785 : return false;
7786 :
7787 136 : if (!type_check (get, 2, BT_INTEGER))
7788 : return false;
7789 :
7790 136 : if (!variable_check (get, 2, false))
7791 : return false;
7792 :
7793 136 : if (!kind_value_check (get, 2, gfc_default_integer_kind))
7794 : return false;
7795 :
7796 136 : if (gfc_array_size (get, &get_size))
7797 : {
7798 5 : if (mpz_get_ui (get_size) < seed_size)
7799 3 : gfc_error ("Size of %qs argument of %qs intrinsic at %L "
7800 : "too small (%i/%i)",
7801 3 : gfc_current_intrinsic_arg[2]->name,
7802 : gfc_current_intrinsic,
7803 3 : &get->where, (int) mpz_get_ui (get_size), seed_size);
7804 5 : mpz_clear (get_size);
7805 : }
7806 : }
7807 :
7808 : /* RANDOM_SEED may not have more than one non-optional argument. */
7809 303 : if (nargs > 1)
7810 1 : gfc_error ("Too many arguments to %s at %L", gfc_current_intrinsic, where);
7811 :
7812 : return true;
7813 : }
7814 :
7815 : bool
7816 393 : gfc_check_fe_runtime_error (gfc_actual_arglist *a)
7817 : {
7818 393 : gfc_expr *e;
7819 393 : size_t len, i;
7820 393 : int num_percent, nargs;
7821 :
7822 393 : e = a->expr;
7823 393 : if (e->expr_type != EXPR_CONSTANT)
7824 : return true;
7825 :
7826 393 : len = e->value.character.length;
7827 393 : if (e->value.character.string[len-1] != '\0')
7828 0 : gfc_internal_error ("fe_runtime_error string must be null terminated");
7829 :
7830 : num_percent = 0;
7831 27189 : for (i=0; i<len-1; i++)
7832 26796 : if (e->value.character.string[i] == '%')
7833 786 : num_percent ++;
7834 :
7835 : nargs = 0;
7836 1572 : for (; a; a = a->next)
7837 1179 : nargs ++;
7838 :
7839 393 : if (nargs -1 != num_percent)
7840 0 : gfc_internal_error ("fe_runtime_error: Wrong number of arguments (%d instead of %d)",
7841 : nargs, num_percent++);
7842 :
7843 : return true;
7844 : }
7845 :
7846 : bool
7847 0 : gfc_check_second_sub (gfc_expr *time)
7848 : {
7849 0 : if (!scalar_check (time, 0))
7850 : return false;
7851 :
7852 0 : if (!type_check (time, 0, BT_REAL))
7853 : return false;
7854 :
7855 0 : if (!kind_value_check (time, 0, 4))
7856 : return false;
7857 :
7858 : return true;
7859 : }
7860 :
7861 :
7862 : /* COUNT and COUNT_MAX of SYSTEM_CLOCK are scalar, default-kind integer
7863 : variables in Fortran 95. In Fortran 2003 and later, they can be of any
7864 : kind, and COUNT_RATE can be of type real. Note, count, count_rate, and
7865 : count_max are all optional arguments */
7866 :
7867 : bool
7868 214 : gfc_check_system_clock (gfc_expr *count, gfc_expr *count_rate,
7869 : gfc_expr *count_max)
7870 : {
7871 214 : int first_int_kind = -1;
7872 :
7873 214 : if (count != NULL)
7874 : {
7875 209 : if (!scalar_check (count, 0))
7876 : return false;
7877 :
7878 209 : if (!type_check (count, 0, BT_INTEGER))
7879 : return false;
7880 :
7881 209 : if (count->ts.kind != gfc_default_integer_kind
7882 209 : && !gfc_notify_std (GFC_STD_F2003, "COUNT argument to "
7883 : "SYSTEM_CLOCK at %L has non-default kind",
7884 : &count->where))
7885 : return false;
7886 :
7887 208 : if (count->ts.kind < gfc_default_integer_kind
7888 208 : && !gfc_notify_std (GFC_STD_F2023_DEL,
7889 : "COUNT argument to SYSTEM_CLOCK at %L "
7890 : "with kind smaller than default integer",
7891 : &count->where))
7892 : return false;
7893 :
7894 207 : if (!variable_check (count, 0, false))
7895 : return false;
7896 :
7897 207 : first_int_kind = count->ts.kind;
7898 : }
7899 :
7900 212 : if (count_rate != NULL)
7901 : {
7902 194 : if (!scalar_check (count_rate, 1))
7903 : return false;
7904 :
7905 194 : if (!variable_check (count_rate, 1, false))
7906 : return false;
7907 :
7908 194 : if (count_rate->ts.type == BT_REAL)
7909 : {
7910 120 : if (!gfc_notify_std (GFC_STD_F2003, "Real COUNT_RATE argument to "
7911 : "SYSTEM_CLOCK at %L", &count_rate->where))
7912 : return false;
7913 : }
7914 : else
7915 : {
7916 74 : if (!type_check (count_rate, 1, BT_INTEGER))
7917 : return false;
7918 :
7919 74 : if (count_rate->ts.kind != gfc_default_integer_kind
7920 74 : && !gfc_notify_std (GFC_STD_F2003, "COUNT_RATE argument to "
7921 : "SYSTEM_CLOCK at %L has non-default kind",
7922 : &count_rate->where))
7923 : return false;
7924 :
7925 73 : if (count_rate->ts.kind < gfc_default_integer_kind
7926 73 : && !gfc_notify_std (GFC_STD_F2023_DEL,
7927 : "COUNT_RATE argument to SYSTEM_CLOCK at %L "
7928 : "with kind smaller than default integer",
7929 : &count_rate->where))
7930 : return false;
7931 :
7932 72 : if (first_int_kind < 0)
7933 2 : first_int_kind = count_rate->ts.kind;
7934 : }
7935 :
7936 : }
7937 :
7938 208 : if (count_max != NULL)
7939 : {
7940 189 : if (!scalar_check (count_max, 2))
7941 : return false;
7942 :
7943 189 : if (!type_check (count_max, 2, BT_INTEGER))
7944 : return false;
7945 :
7946 189 : if (count_max->ts.kind != gfc_default_integer_kind
7947 189 : && !gfc_notify_std (GFC_STD_F2003, "COUNT_MAX argument to "
7948 : "SYSTEM_CLOCK at %L has non-default kind",
7949 : &count_max->where))
7950 : return false;
7951 :
7952 188 : if (!variable_check (count_max, 2, false))
7953 : return false;
7954 :
7955 188 : if (count_max->ts.kind < gfc_default_integer_kind
7956 188 : && !gfc_notify_std (GFC_STD_F2023_DEL,
7957 : "COUNT_MAX argument to SYSTEM_CLOCK at %L "
7958 : "with kind smaller than default integer",
7959 : &count_max->where))
7960 : return false;
7961 :
7962 187 : if (first_int_kind < 0)
7963 0 : first_int_kind = count_max->ts.kind;
7964 : }
7965 :
7966 206 : if (first_int_kind > 0)
7967 : {
7968 205 : if (count_rate
7969 188 : && count_rate->ts.type == BT_INTEGER
7970 71 : && count_rate->ts.kind != first_int_kind
7971 237 : && !gfc_notify_std (GFC_STD_F2023_DEL,
7972 : "integer arguments to SYSTEM_CLOCK at %L "
7973 : "with different kind parameters",
7974 : &count_rate->where))
7975 : return false;
7976 :
7977 187 : if (count_max && count_max->ts.kind != first_int_kind
7978 286 : && !gfc_notify_std (GFC_STD_F2023_DEL,
7979 : "integer arguments to SYSTEM_CLOCK at %L "
7980 : "with different kind parameters",
7981 : &count_max->where))
7982 : return false;
7983 : }
7984 :
7985 : return true;
7986 : }
7987 :
7988 :
7989 : bool
7990 2 : gfc_check_irand (gfc_expr *x)
7991 : {
7992 2 : if (x == NULL)
7993 : return true;
7994 :
7995 0 : if (!scalar_check (x, 0))
7996 : return false;
7997 :
7998 0 : if (!type_check (x, 0, BT_INTEGER))
7999 : return false;
8000 :
8001 0 : if (!kind_value_check (x, 0, 4))
8002 : return false;
8003 :
8004 : return true;
8005 : }
8006 :
8007 :
8008 : bool
8009 0 : gfc_check_alarm_sub (gfc_expr *seconds, gfc_expr *handler, gfc_expr *status)
8010 : {
8011 0 : if (!scalar_check (seconds, 0))
8012 : return false;
8013 0 : if (!type_check (seconds, 0, BT_INTEGER))
8014 : return false;
8015 :
8016 0 : if (!int_or_proc_check (handler, 1))
8017 : return false;
8018 0 : if (handler->ts.type == BT_INTEGER && !scalar_check (handler, 1))
8019 : return false;
8020 :
8021 0 : if (status == NULL)
8022 : return true;
8023 :
8024 0 : if (!scalar_check (status, 2))
8025 : return false;
8026 0 : if (!type_check (status, 2, BT_INTEGER))
8027 : return false;
8028 0 : if (!kind_value_check (status, 2, gfc_default_integer_kind))
8029 : return false;
8030 :
8031 : return true;
8032 : }
8033 :
8034 :
8035 : bool
8036 34 : gfc_check_rand (gfc_expr *x)
8037 : {
8038 34 : if (x == NULL)
8039 : return true;
8040 :
8041 1 : if (!scalar_check (x, 0))
8042 : return false;
8043 :
8044 1 : if (!type_check (x, 0, BT_INTEGER))
8045 : return false;
8046 :
8047 1 : if (!kind_value_check (x, 0, 4))
8048 : return false;
8049 :
8050 : return true;
8051 : }
8052 :
8053 :
8054 : bool
8055 0 : gfc_check_srand (gfc_expr *x)
8056 : {
8057 0 : if (!scalar_check (x, 0))
8058 : return false;
8059 :
8060 0 : if (!type_check (x, 0, BT_INTEGER))
8061 : return false;
8062 :
8063 0 : if (!kind_value_check (x, 0, 4))
8064 : return false;
8065 :
8066 : return true;
8067 : }
8068 :
8069 :
8070 : bool
8071 2 : gfc_check_ctime_sub (gfc_expr *time, gfc_expr *result)
8072 : {
8073 2 : if (!scalar_check (time, 0))
8074 : return false;
8075 2 : if (!type_check (time, 0, BT_INTEGER))
8076 : return false;
8077 :
8078 2 : if (!type_check (result, 1, BT_CHARACTER))
8079 : return false;
8080 2 : if (!kind_value_check (result, 1, gfc_default_character_kind))
8081 : return false;
8082 :
8083 : return true;
8084 : }
8085 :
8086 :
8087 : bool
8088 1 : gfc_check_dtime_etime (gfc_expr *x)
8089 : {
8090 1 : if (!array_check (x, 0))
8091 : return false;
8092 :
8093 1 : if (!rank_check (x, 0, 1))
8094 : return false;
8095 :
8096 1 : if (!variable_check (x, 0, false))
8097 : return false;
8098 :
8099 1 : if (!type_check (x, 0, BT_REAL))
8100 : return false;
8101 :
8102 1 : if (!kind_value_check (x, 0, 4))
8103 : return false;
8104 :
8105 : return true;
8106 : }
8107 :
8108 :
8109 : bool
8110 1 : gfc_check_dtime_etime_sub (gfc_expr *values, gfc_expr *time)
8111 : {
8112 1 : if (!array_check (values, 0))
8113 : return false;
8114 :
8115 1 : if (!rank_check (values, 0, 1))
8116 : return false;
8117 :
8118 1 : if (!variable_check (values, 0, false))
8119 : return false;
8120 :
8121 1 : if (!type_check (values, 0, BT_REAL))
8122 : return false;
8123 :
8124 1 : if (!kind_value_check (values, 0, 4))
8125 : return false;
8126 :
8127 1 : if (!scalar_check (time, 1))
8128 : return false;
8129 :
8130 1 : if (!type_check (time, 1, BT_REAL))
8131 : return false;
8132 :
8133 1 : if (!kind_value_check (time, 1, 4))
8134 : return false;
8135 :
8136 : return true;
8137 : }
8138 :
8139 :
8140 : bool
8141 2 : gfc_check_fdate_sub (gfc_expr *date)
8142 : {
8143 2 : if (!type_check (date, 0, BT_CHARACTER))
8144 : return false;
8145 2 : if (!kind_value_check (date, 0, gfc_default_character_kind))
8146 : return false;
8147 :
8148 : return true;
8149 : }
8150 :
8151 :
8152 : bool
8153 3 : gfc_check_gerror (gfc_expr *msg)
8154 : {
8155 3 : if (!type_check (msg, 0, BT_CHARACTER))
8156 : return false;
8157 3 : if (!kind_value_check (msg, 0, gfc_default_character_kind))
8158 : return false;
8159 :
8160 : return true;
8161 : }
8162 :
8163 :
8164 : bool
8165 10 : gfc_check_getcwd_sub (gfc_expr *cwd, gfc_expr *status)
8166 : {
8167 10 : if (!type_check (cwd, 0, BT_CHARACTER))
8168 : return false;
8169 10 : if (!kind_value_check (cwd, 0, gfc_default_character_kind))
8170 : return false;
8171 :
8172 8 : if (status == NULL)
8173 : return true;
8174 :
8175 1 : if (!scalar_check (status, 1))
8176 : return false;
8177 :
8178 1 : if (!type_check (status, 1, BT_INTEGER))
8179 : return false;
8180 :
8181 : return true;
8182 : }
8183 :
8184 :
8185 : bool
8186 57 : gfc_check_getarg (gfc_expr *pos, gfc_expr *value)
8187 : {
8188 57 : if (!type_check (pos, 0, BT_INTEGER))
8189 : return false;
8190 :
8191 57 : if (pos->ts.kind > gfc_default_integer_kind)
8192 : {
8193 0 : gfc_error ("%qs argument of %qs intrinsic at %L must be of a kind "
8194 : "not wider than the default kind (%d)",
8195 0 : gfc_current_intrinsic_arg[0]->name, gfc_current_intrinsic,
8196 : &pos->where, gfc_default_integer_kind);
8197 0 : return false;
8198 : }
8199 :
8200 57 : if (!type_check (value, 1, BT_CHARACTER))
8201 : return false;
8202 57 : if (!kind_value_check (value, 1, gfc_default_character_kind))
8203 : return false;
8204 :
8205 : return true;
8206 : }
8207 :
8208 :
8209 : bool
8210 3 : gfc_check_getlog (gfc_expr *msg)
8211 : {
8212 3 : if (!type_check (msg, 0, BT_CHARACTER))
8213 : return false;
8214 3 : if (!kind_value_check (msg, 0, gfc_default_character_kind))
8215 : return false;
8216 :
8217 : return true;
8218 : }
8219 :
8220 :
8221 : bool
8222 3 : gfc_check_exit (gfc_expr *status)
8223 : {
8224 3 : if (status == NULL)
8225 : return true;
8226 :
8227 2 : if (!type_check (status, 0, BT_INTEGER))
8228 : return false;
8229 :
8230 2 : if (!scalar_check (status, 0))
8231 : return false;
8232 :
8233 : return true;
8234 : }
8235 :
8236 :
8237 : bool
8238 25 : gfc_check_flush (gfc_expr *unit)
8239 : {
8240 25 : if (unit == NULL)
8241 : return true;
8242 :
8243 12 : if (!type_check (unit, 0, BT_INTEGER))
8244 : return false;
8245 :
8246 12 : if (!scalar_check (unit, 0))
8247 : return false;
8248 :
8249 : return true;
8250 : }
8251 :
8252 :
8253 : bool
8254 10 : gfc_check_free (gfc_expr *i)
8255 : {
8256 10 : if (!type_check (i, 0, BT_INTEGER))
8257 : return false;
8258 :
8259 10 : if (!scalar_check (i, 0))
8260 : return false;
8261 :
8262 : return true;
8263 : }
8264 :
8265 :
8266 : bool
8267 5 : gfc_check_hostnm (gfc_expr *name)
8268 : {
8269 5 : if (!type_check (name, 0, BT_CHARACTER))
8270 : return false;
8271 5 : if (!kind_value_check (name, 0, gfc_default_character_kind))
8272 : return false;
8273 :
8274 : return true;
8275 : }
8276 :
8277 :
8278 : bool
8279 11 : gfc_check_hostnm_sub (gfc_expr *name, gfc_expr *status)
8280 : {
8281 11 : if (!type_check (name, 0, BT_CHARACTER))
8282 : return false;
8283 11 : if (!kind_value_check (name, 0, gfc_default_character_kind))
8284 : return false;
8285 :
8286 9 : if (status == NULL)
8287 : return true;
8288 :
8289 7 : if (!scalar_check (status, 1))
8290 : return false;
8291 :
8292 7 : if (!type_check (status, 1, BT_INTEGER))
8293 : return false;
8294 :
8295 : return true;
8296 : }
8297 :
8298 :
8299 : bool
8300 24 : gfc_check_itime_idate (gfc_expr *values)
8301 : {
8302 24 : if (!array_check (values, 0))
8303 : return false;
8304 :
8305 24 : if (!rank_check (values, 0, 1))
8306 : return false;
8307 :
8308 24 : if (!variable_check (values, 0, false))
8309 : return false;
8310 :
8311 24 : if (!type_check (values, 0, BT_INTEGER))
8312 : return false;
8313 :
8314 24 : if (!kind_value_check (values, 0, gfc_default_integer_kind))
8315 : return false;
8316 :
8317 : return true;
8318 : }
8319 :
8320 :
8321 : bool
8322 24 : gfc_check_ltime_gmtime (gfc_expr *time, gfc_expr *values)
8323 : {
8324 24 : if (!type_check (time, 0, BT_INTEGER))
8325 : return false;
8326 :
8327 24 : if (!kind_value_check (time, 0, gfc_default_integer_kind))
8328 : return false;
8329 :
8330 24 : if (!scalar_check (time, 0))
8331 : return false;
8332 :
8333 24 : if (!array_check (values, 1))
8334 : return false;
8335 :
8336 24 : if (!rank_check (values, 1, 1))
8337 : return false;
8338 :
8339 24 : if (!variable_check (values, 1, false))
8340 : return false;
8341 :
8342 24 : if (!type_check (values, 1, BT_INTEGER))
8343 : return false;
8344 :
8345 24 : if (!kind_value_check (values, 1, gfc_default_integer_kind))
8346 : return false;
8347 :
8348 : return true;
8349 : }
8350 :
8351 :
8352 : bool
8353 2 : gfc_check_ttynam_sub (gfc_expr *unit, gfc_expr *name)
8354 : {
8355 2 : if (!scalar_check (unit, 0))
8356 : return false;
8357 :
8358 2 : if (!type_check (unit, 0, BT_INTEGER))
8359 : return false;
8360 :
8361 2 : if (!type_check (name, 1, BT_CHARACTER))
8362 : return false;
8363 2 : if (!kind_value_check (name, 1, gfc_default_character_kind))
8364 : return false;
8365 :
8366 : return true;
8367 : }
8368 :
8369 :
8370 : bool
8371 909 : gfc_check_is_contiguous (gfc_expr *array)
8372 : {
8373 909 : if (array->expr_type == EXPR_NULL)
8374 : {
8375 2 : gfc_error ("Actual argument at %L of %qs intrinsic shall be an "
8376 : "associated pointer", &array->where, gfc_current_intrinsic);
8377 2 : return false;
8378 : }
8379 :
8380 907 : if (!array_check (array, 0))
8381 : return false;
8382 :
8383 : return true;
8384 : }
8385 :
8386 :
8387 : bool
8388 0 : gfc_check_isatty (gfc_expr *unit)
8389 : {
8390 0 : if (unit == NULL)
8391 : return false;
8392 :
8393 0 : if (!type_check (unit, 0, BT_INTEGER))
8394 : return false;
8395 :
8396 0 : if (!scalar_check (unit, 0))
8397 : return false;
8398 :
8399 : return true;
8400 : }
8401 :
8402 :
8403 : bool
8404 626 : gfc_check_isnan (gfc_expr *x)
8405 : {
8406 626 : if (!type_check (x, 0, BT_REAL))
8407 : return false;
8408 :
8409 : return true;
8410 : }
8411 :
8412 :
8413 : bool
8414 3 : gfc_check_perror (gfc_expr *string)
8415 : {
8416 3 : if (!type_check (string, 0, BT_CHARACTER))
8417 : return false;
8418 3 : if (!kind_value_check (string, 0, gfc_default_character_kind))
8419 : return false;
8420 :
8421 : return true;
8422 : }
8423 :
8424 :
8425 : bool
8426 0 : gfc_check_umask (gfc_expr *mask)
8427 : {
8428 0 : if (!type_check (mask, 0, BT_INTEGER))
8429 : return false;
8430 :
8431 0 : if (!scalar_check (mask, 0))
8432 : return false;
8433 :
8434 : return true;
8435 : }
8436 :
8437 :
8438 : bool
8439 0 : gfc_check_umask_sub (gfc_expr *mask, gfc_expr *old)
8440 : {
8441 0 : if (!type_check (mask, 0, BT_INTEGER))
8442 : return false;
8443 :
8444 0 : if (!scalar_check (mask, 0))
8445 : return false;
8446 :
8447 0 : if (old == NULL)
8448 : return true;
8449 :
8450 0 : if (!scalar_check (old, 1))
8451 : return false;
8452 :
8453 0 : if (!type_check (old, 1, BT_INTEGER))
8454 : return false;
8455 :
8456 : return true;
8457 : }
8458 :
8459 :
8460 : bool
8461 2 : gfc_check_unlink (gfc_expr *name)
8462 : {
8463 2 : if (!type_check (name, 0, BT_CHARACTER))
8464 : return false;
8465 2 : if (!kind_value_check (name, 0, gfc_default_character_kind))
8466 : return false;
8467 :
8468 : return true;
8469 : }
8470 :
8471 :
8472 : bool
8473 12 : gfc_check_unlink_sub (gfc_expr *name, gfc_expr *status)
8474 : {
8475 12 : if (!type_check (name, 0, BT_CHARACTER))
8476 : return false;
8477 12 : if (!kind_value_check (name, 0, gfc_default_character_kind))
8478 : return false;
8479 :
8480 10 : if (status == NULL)
8481 : return true;
8482 :
8483 1 : if (!scalar_check (status, 1))
8484 : return false;
8485 :
8486 1 : if (!type_check (status, 1, BT_INTEGER))
8487 : return false;
8488 :
8489 : return true;
8490 : }
8491 :
8492 :
8493 : bool
8494 1 : gfc_check_signal (gfc_expr *number, gfc_expr *handler)
8495 : {
8496 1 : if (!scalar_check (number, 0))
8497 : return false;
8498 1 : if (!type_check (number, 0, BT_INTEGER))
8499 : return false;
8500 :
8501 1 : if (!int_or_proc_check (handler, 1))
8502 : return false;
8503 1 : if (handler->ts.type == BT_INTEGER && !scalar_check (handler, 1))
8504 : return false;
8505 :
8506 : return true;
8507 : }
8508 :
8509 :
8510 : bool
8511 0 : gfc_check_signal_sub (gfc_expr *number, gfc_expr *handler, gfc_expr *status)
8512 : {
8513 0 : if (!scalar_check (number, 0))
8514 : return false;
8515 0 : if (!type_check (number, 0, BT_INTEGER))
8516 : return false;
8517 :
8518 0 : if (!int_or_proc_check (handler, 1))
8519 : return false;
8520 0 : if (handler->ts.type == BT_INTEGER && !scalar_check (handler, 1))
8521 : return false;
8522 :
8523 0 : if (status == NULL)
8524 : return true;
8525 :
8526 0 : if (!type_check (status, 2, BT_INTEGER))
8527 : return false;
8528 0 : if (!scalar_check (status, 2))
8529 : return false;
8530 :
8531 : return true;
8532 : }
8533 :
8534 :
8535 : bool
8536 0 : gfc_check_system_sub (gfc_expr *cmd, gfc_expr *status)
8537 : {
8538 0 : if (!type_check (cmd, 0, BT_CHARACTER))
8539 : return false;
8540 0 : if (!kind_value_check (cmd, 0, gfc_default_character_kind))
8541 : return false;
8542 :
8543 0 : if (!scalar_check (status, 1))
8544 : return false;
8545 :
8546 0 : if (!type_check (status, 1, BT_INTEGER))
8547 : return false;
8548 :
8549 0 : if (!kind_value_check (status, 1, gfc_default_integer_kind))
8550 : return false;
8551 :
8552 : return true;
8553 : }
8554 :
8555 :
8556 : /* This is used for the GNU intrinsics AND, OR and XOR. */
8557 : bool
8558 164 : gfc_check_and (gfc_expr *i, gfc_expr *j)
8559 : {
8560 164 : if (i->ts.type != BT_INTEGER
8561 164 : && i->ts.type != BT_LOGICAL
8562 25 : && i->ts.type != BT_BOZ)
8563 : {
8564 3 : gfc_error ("%qs argument of %qs intrinsic at %L must be INTEGER, "
8565 : "LOGICAL, or a BOZ literal constant",
8566 3 : gfc_current_intrinsic_arg[0]->name,
8567 : gfc_current_intrinsic, &i->where);
8568 3 : return false;
8569 : }
8570 :
8571 161 : if (j->ts.type != BT_INTEGER
8572 161 : && j->ts.type != BT_LOGICAL
8573 28 : && j->ts.type != BT_BOZ)
8574 : {
8575 3 : gfc_error ("%qs argument of %qs intrinsic at %L must be INTEGER, "
8576 : "LOGICAL, or a BOZ literal constant",
8577 3 : gfc_current_intrinsic_arg[1]->name,
8578 : gfc_current_intrinsic, &j->where);
8579 3 : return false;
8580 : }
8581 :
8582 : /* i and j cannot both be BOZ literal constants. */
8583 158 : if (!boz_args_check (i, j))
8584 : return false;
8585 :
8586 : /* If i is BOZ and j is integer, convert i to type of j. */
8587 154 : if (i->ts.type == BT_BOZ)
8588 : {
8589 18 : if (j->ts.type != BT_INTEGER)
8590 : {
8591 0 : gfc_error ("%qs argument of %qs intrinsic at %L must be INTEGER",
8592 0 : gfc_current_intrinsic_arg[1]->name,
8593 : gfc_current_intrinsic, &j->where);
8594 0 : reset_boz (i);
8595 0 : return false;
8596 : }
8597 18 : if (!gfc_boz2int (i, j->ts.kind))
8598 : return false;
8599 : }
8600 :
8601 : /* If j is BOZ and i is integer, convert j to type of i. */
8602 154 : if (j->ts.type == BT_BOZ)
8603 : {
8604 21 : if (i->ts.type != BT_INTEGER)
8605 : {
8606 1 : gfc_error ("%qs argument of %qs intrinsic at %L must be INTEGER",
8607 1 : gfc_current_intrinsic_arg[0]->name,
8608 : gfc_current_intrinsic, &j->where);
8609 1 : reset_boz (j);
8610 1 : return false;
8611 : }
8612 20 : if (!gfc_boz2int (j, i->ts.kind))
8613 : return false;
8614 : }
8615 :
8616 153 : if (!same_type_check (i, 0, j, 1, false))
8617 : return false;
8618 :
8619 146 : if (!scalar_check (i, 0))
8620 : return false;
8621 :
8622 146 : if (!scalar_check (j, 1))
8623 : return false;
8624 :
8625 : return true;
8626 : }
8627 :
8628 :
8629 : bool
8630 1067 : gfc_check_storage_size (gfc_expr *a, gfc_expr *kind)
8631 : {
8632 :
8633 1067 : if (a->expr_type == EXPR_NULL)
8634 : {
8635 1 : gfc_error ("Intrinsic function NULL at %L cannot be an actual "
8636 : "argument to STORAGE_SIZE, because it returns a "
8637 : "disassociated pointer", &a->where);
8638 1 : return false;
8639 : }
8640 :
8641 1066 : if (a->ts.type == BT_ASSUMED)
8642 : {
8643 0 : gfc_error ("%qs argument of %qs intrinsic at %L shall not be TYPE(*)",
8644 0 : gfc_current_intrinsic_arg[0]->name, gfc_current_intrinsic,
8645 : &a->where);
8646 0 : return false;
8647 : }
8648 :
8649 1066 : if (a->ts.type == BT_PROCEDURE)
8650 : {
8651 1 : gfc_error ("%qs argument of %qs intrinsic at %L shall not be a "
8652 1 : "procedure", gfc_current_intrinsic_arg[0]->name,
8653 : gfc_current_intrinsic, &a->where);
8654 1 : return false;
8655 : }
8656 :
8657 1065 : if (a->ts.type == BT_BOZ && illegal_boz_arg (a))
8658 : return false;
8659 :
8660 1064 : if (kind == NULL)
8661 : return true;
8662 :
8663 303 : if (!type_check (kind, 1, BT_INTEGER))
8664 : return false;
8665 :
8666 302 : if (!scalar_check (kind, 1))
8667 : return false;
8668 :
8669 301 : if (kind->expr_type != EXPR_CONSTANT)
8670 : {
8671 1 : gfc_error ("%qs argument of %qs intrinsic at %L must be a constant",
8672 1 : gfc_current_intrinsic_arg[1]->name, gfc_current_intrinsic,
8673 : &kind->where);
8674 1 : return false;
8675 : }
8676 :
8677 : return true;
8678 : }
8679 :
8680 : /* Check two operands that either both or none of them can
8681 : be UNSIGNED. */
8682 :
8683 : bool
8684 431852 : gfc_invalid_unsigned_ops (gfc_expr * op1, gfc_expr * op2)
8685 : {
8686 431852 : return (op1->ts.type == BT_UNSIGNED) ^ (op2->ts.type == BT_UNSIGNED);
8687 : }
|