PR modula2/115804 ICE during gimplification with new isfinite optab
[official-gcc.git] / gcc / fortran / check.cc
blob2f50d84b876fc24d00a1aabe97512c00efba0415
1 /* Check functions
2 Copyright (C) 2002-2024 Free Software Foundation, Inc.
3 Contributed by Andy Vaught & Katherine Holcomb
5 This file is part of GCC.
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.
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.
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/>. */
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. */
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"
38 /* Reset a BOZ to a zero value. This is used to prevent run-on errors
39 from resolve.cc(resolve_function). */
41 static void
42 reset_boz (gfc_expr *x)
44 /* Clear boz info. */
45 x->boz.rdx = 0;
46 x->boz.len = 0;
47 free (x->boz.str);
49 x->ts.type = BT_INTEGER;
50 x->ts.kind = gfc_default_integer_kind;
51 mpz_init (x->value.integer);
52 mpz_set_ui (x->value.integer, 0);
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. */
61 bool
62 gfc_invalid_boz (const char *msg, locus *loc)
64 if (flag_allow_invalid_boz)
66 gfc_warning (0, msg, loc);
67 return false;
70 const char *hint = _(" [see %<-fno-allow-invalid-boz%>]");
71 size_t len = strlen (msg) + strlen (hint) + 1;
72 char *msg2 = (char *) alloca (len);
73 strcpy (msg2, msg);
74 strcat (msg2, hint);
75 gfc_error (msg2, loc);
76 return true;
80 /* Issue an error for an illegal BOZ argument. */
82 static bool
83 illegal_boz_arg (gfc_expr *x)
85 if (x->ts.type == BT_BOZ)
87 gfc_error ("BOZ literal constant at %L cannot be an actual argument "
88 "to %qs", &x->where, gfc_current_intrinsic);
89 reset_boz (x);
90 return true;
93 return false;
96 /* Some procedures take two arguments such that both cannot be BOZ. */
98 static bool
99 boz_args_check(gfc_expr *i, gfc_expr *j)
101 if (i->ts.type == BT_BOZ && j->ts.type == BT_BOZ)
103 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 reset_boz (i);
107 reset_boz (j);
108 return false;
112 return true;
116 /* Check that a BOZ is a constant. */
118 static bool
119 is_boz_constant (gfc_expr *a)
121 if (a->expr_type != EXPR_CONSTANT)
123 gfc_error ("Invalid use of BOZ literal constant at %L", &a->where);
124 return false;
127 return true;
131 /* Convert a octal string into a binary string. This is used in the
132 fallback conversion of an octal string to a REAL. */
134 static char *
135 oct2bin(int nbits, char *oct)
137 const char bits[8][5] = {
138 "000", "001", "010", "011", "100", "101", "110", "111"};
140 char *buf, *bufp;
141 int i, j, n;
143 j = nbits + 1;
144 if (nbits == 64) j++;
146 bufp = buf = XCNEWVEC (char, j + 1);
147 memset (bufp, 0, j + 1);
149 n = strlen (oct);
150 for (i = 0; i < n; i++, oct++)
152 j = *oct - 48;
153 strcpy (bufp, &bits[j][0]);
154 bufp += 3;
157 bufp = XCNEWVEC (char, nbits + 1);
158 if (nbits == 64)
159 strcpy (bufp, buf + 2);
160 else
161 strcpy (bufp, buf + 1);
163 free (buf);
165 return bufp;
169 /* Convert a hexidecimal string into a binary string. This is used in the
170 fallback conversion of a hexidecimal string to a REAL. */
172 static char *
173 hex2bin(int nbits, char *hex)
175 const char bits[16][5] = {
176 "0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111",
177 "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
179 char *buf, *bufp;
180 int i, j, n;
182 bufp = buf = XCNEWVEC (char, nbits + 1);
183 memset (bufp, 0, nbits + 1);
185 n = strlen (hex);
186 for (i = 0; i < n; i++, hex++)
188 j = *hex;
189 if (j > 47 && j < 58)
190 j -= 48;
191 else if (j > 64 && j < 71)
192 j -= 55;
193 else if (j > 96 && j < 103)
194 j -= 87;
195 else
196 gcc_unreachable ();
198 strcpy (bufp, &bits[j][0]);
199 bufp += 4;
202 return buf;
206 /* Fallback conversion of a BOZ string to REAL. */
208 static void
209 bin2real (gfc_expr *x, int kind)
211 char buf[114], *sp;
212 int b, i, ie, t, w;
213 bool sgn;
214 mpz_t em;
216 i = gfc_validate_kind (BT_REAL, kind, false);
217 t = gfc_real_kinds[i].digits - 1;
219 /* Number of bits in the exponent. */
220 if (gfc_real_kinds[i].max_exponent == 16384)
221 w = 15;
222 else if (gfc_real_kinds[i].max_exponent == 1024)
223 w = 11;
224 else
225 w = 8;
227 if (x->boz.rdx == 16)
228 sp = hex2bin (gfc_real_kinds[i].mode_precision, x->boz.str);
229 else if (x->boz.rdx == 8)
230 sp = oct2bin (gfc_real_kinds[i].mode_precision, x->boz.str);
231 else
232 sp = x->boz.str;
234 /* Extract sign bit. */
235 sgn = *sp != '0';
237 /* Extract biased exponent. */
238 memset (buf, 0, 114);
239 strncpy (buf, ++sp, w);
240 mpz_init (em);
241 mpz_set_str (em, buf, 2);
242 ie = mpz_get_si (em);
244 mpfr_init2 (x->value.real, t + 1);
245 x->ts.type = BT_REAL;
246 x->ts.kind = kind;
248 sp += w; /* Set to first digit in significand. */
249 b = (1 << w) - 1;
250 if ((i == 0 && ie == b) || (i == 1 && ie == b)
251 || ((i == 2 || i == 3) && ie == b))
253 bool zeros = true;
254 if (i == 2) sp++;
255 for (; *sp; sp++)
257 if (*sp != '0')
259 zeros = false;
260 break;
264 if (zeros)
265 mpfr_set_inf (x->value.real, 1);
266 else
267 mpfr_set_nan (x->value.real);
269 else
271 if (i == 2)
272 strncpy (buf, sp, t + 1);
273 else
275 /* Significand with hidden bit. */
276 buf[0] = '1';
277 strncpy (&buf[1], sp, t);
280 /* Convert to significand to integer. */
281 mpz_set_str (em, buf, 2);
282 ie -= ((1 << (w - 1)) - 1); /* Unbiased exponent. */
283 mpfr_set_z_2exp (x->value.real, em, ie - t, GFC_RND_MODE);
286 if (sgn) mpfr_neg (x->value.real, x->value.real, GFC_RND_MODE);
288 mpz_clear (em);
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. */
296 bool
297 gfc_boz2real (gfc_expr *x, int kind)
299 extern int gfc_max_integer_kind;
300 gfc_typespec ts;
301 int len;
302 char *buf, *str;
304 if (!is_boz_constant (x))
305 return false;
307 /* Determine the length of the required string. */
308 len = 8 * kind;
309 if (x->boz.rdx == 16) len /= 4;
310 if (x->boz.rdx == 8) len = len / 3 + 1;
311 buf = (char *) alloca (len + 1); /* +1 for NULL terminator. */
313 if (x->boz.len >= len) /* Truncate if necessary. */
315 str = x->boz.str + (x->boz.len - len);
316 strcpy(buf, str);
318 else /* Copy and pad. */
320 memset (buf, 48, len);
321 str = buf + (len - x->boz.len);
322 strcpy (str, x->boz.str);
325 /* Need to adjust leading bits in an octal string. */
326 if (x->boz.rdx == 8)
328 /* Clear first bit. */
329 if (kind == 4 || kind == 10 || kind == 16)
331 if (buf[0] == '4')
332 buf[0] = '0';
333 else if (buf[0] == '5')
334 buf[0] = '1';
335 else if (buf[0] == '6')
336 buf[0] = '2';
337 else if (buf[0] == '7')
338 buf[0] = '3';
340 /* Clear first two bits. */
341 else
343 if (buf[0] == '2' || buf[0] == '4' || buf[0] == '6')
344 buf[0] = '0';
345 else if (buf[0] == '3' || buf[0] == '5' || buf[0] == '7')
346 buf[0] = '1';
350 /* Reset BOZ string to the truncated or padded version. */
351 free (x->boz.str);
352 x->boz.len = len;
353 x->boz.str = XCNEWVEC (char, len + 1);
354 strncpy (x->boz.str, buf, len);
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 if (gfc_max_integer_kind < kind)
361 bin2real (x, kind);
363 else
365 /* Convert to widest possible integer. */
366 gfc_boz2int (x, gfc_max_integer_kind);
367 ts.type = BT_REAL;
368 ts.kind = kind;
369 if (!gfc_convert_boz (x, &ts))
371 gfc_error ("Failure in conversion of BOZ to REAL at %L", &x->where);
372 return false;
376 return true;
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. */
386 bool
387 gfc_boz2int (gfc_expr *x, int kind)
389 int i, len;
390 char *buf, *str;
391 mpz_t tmp1;
393 if (!is_boz_constant (x))
394 return false;
396 i = gfc_validate_kind (BT_INTEGER, kind, false);
397 len = gfc_integer_kinds[i].bit_size;
398 if (x->boz.rdx == 16) len /= 4;
399 if (x->boz.rdx == 8) len = len / 3 + 1;
400 buf = (char *) alloca (len + 1); /* +1 for NULL terminator. */
402 if (x->boz.len >= len) /* Truncate if necessary. */
404 str = x->boz.str + (x->boz.len - len);
405 strcpy(buf, str);
407 else /* Copy and pad. */
409 memset (buf, 48, len);
410 str = buf + (len - x->boz.len);
411 strcpy (str, x->boz.str);
414 /* Need to adjust leading bits in an octal string. */
415 if (x->boz.rdx == 8)
417 /* Clear first bit. */
418 if (kind == 1 || kind == 4 || kind == 16)
420 if (buf[0] == '4')
421 buf[0] = '0';
422 else if (buf[0] == '5')
423 buf[0] = '1';
424 else if (buf[0] == '6')
425 buf[0] = '2';
426 else if (buf[0] == '7')
427 buf[0] = '3';
429 /* Clear first two bits. */
430 else
432 if (buf[0] == '2' || buf[0] == '4' || buf[0] == '6')
433 buf[0] = '0';
434 else if (buf[0] == '3' || buf[0] == '5' || buf[0] == '7')
435 buf[0] = '1';
439 /* Convert as-if unsigned integer. */
440 mpz_init (tmp1);
441 mpz_set_str (tmp1, buf, x->boz.rdx);
443 /* Check for wrap-around. */
444 if (mpz_cmp (tmp1, gfc_integer_kinds[i].huge) > 0)
446 mpz_t tmp2;
447 mpz_init (tmp2);
448 mpz_add_ui (tmp2, gfc_integer_kinds[i].huge, 1);
449 mpz_mod (tmp1, tmp1, tmp2);
450 mpz_sub (tmp1, tmp1, tmp2);
451 mpz_clear (tmp2);
454 /* Clear boz info. */
455 x->boz.rdx = 0;
456 x->boz.len = 0;
457 free (x->boz.str);
459 mpz_init (x->value.integer);
460 mpz_set (x->value.integer, tmp1);
461 x->ts.type = BT_INTEGER;
462 x->ts.kind = kind;
463 mpz_clear (tmp1);
465 return true;
469 /* Make sure an expression is a scalar. */
471 static bool
472 scalar_check (gfc_expr *e, int n)
474 if (e->rank == 0)
475 return true;
477 gfc_error ("%qs argument of %qs intrinsic at %L must be a scalar",
478 gfc_current_intrinsic_arg[n]->name, gfc_current_intrinsic,
479 &e->where);
481 return false;
485 /* Check the type of an expression. */
487 static bool
488 type_check (gfc_expr *e, int n, bt type)
490 if (e->ts.type == type)
491 return true;
493 gfc_error ("%qs argument of %qs intrinsic at %L must be %s",
494 gfc_current_intrinsic_arg[n]->name, gfc_current_intrinsic,
495 &e->where, gfc_basic_typename (type));
497 return false;
501 /* Check that the expression is a numeric type. */
503 static bool
504 numeric_check (gfc_expr *e, int n)
506 /* Users sometime use a subroutine designator as an actual argument to
507 an intrinsic subprogram that expects an argument with a numeric type. */
508 if (e->symtree && e->symtree->n.sym->attr.subroutine)
509 goto error;
511 if (gfc_numeric_ts (&e->ts))
512 return true;
514 /* If the expression has not got a type, check if its namespace can
515 offer a default type. */
516 if ((e->expr_type == EXPR_VARIABLE || e->expr_type == EXPR_FUNCTION)
517 && e->symtree->n.sym->ts.type == BT_UNKNOWN
518 && gfc_set_default_type (e->symtree->n.sym, 0, e->symtree->n.sym->ns)
519 && gfc_numeric_ts (&e->symtree->n.sym->ts))
521 e->ts = e->symtree->n.sym->ts;
522 return true;
525 error:
527 gfc_error ("%qs argument of %qs intrinsic at %L must have a numeric type",
528 gfc_current_intrinsic_arg[n]->name, gfc_current_intrinsic,
529 &e->where);
531 return false;
535 /* Check that an expression is integer or real. */
537 static bool
538 int_or_real_check (gfc_expr *e, int n)
540 if (e->ts.type != BT_INTEGER && e->ts.type != BT_REAL)
542 gfc_error ("%qs argument of %qs intrinsic at %L must be INTEGER "
543 "or REAL", gfc_current_intrinsic_arg[n]->name,
544 gfc_current_intrinsic, &e->where);
545 return false;
548 return true;
551 /* Check that an expression is integer or real; allow character for
552 F2003 or later. */
554 static bool
555 int_or_real_or_char_check_f2003 (gfc_expr *e, int n)
557 if (e->ts.type != BT_INTEGER && e->ts.type != BT_REAL)
559 if (e->ts.type == BT_CHARACTER)
560 return gfc_notify_std (GFC_STD_F2003, "Fortran 2003: Character for "
561 "%qs argument of %qs intrinsic at %L",
562 gfc_current_intrinsic_arg[n]->name,
563 gfc_current_intrinsic, &e->where);
564 else
566 if (gfc_option.allow_std & GFC_STD_F2003)
567 gfc_error ("%qs argument of %qs intrinsic at %L must be INTEGER "
568 "or REAL or CHARACTER",
569 gfc_current_intrinsic_arg[n]->name,
570 gfc_current_intrinsic, &e->where);
571 else
572 gfc_error ("%qs argument of %qs intrinsic at %L must be INTEGER "
573 "or REAL", gfc_current_intrinsic_arg[n]->name,
574 gfc_current_intrinsic, &e->where);
576 return false;
579 return true;
582 /* Check that an expression is an intrinsic type. */
583 static bool
584 intrinsic_type_check (gfc_expr *e, int n)
586 if (e->ts.type != BT_INTEGER && e->ts.type != BT_REAL
587 && e->ts.type != BT_COMPLEX && e->ts.type != BT_CHARACTER
588 && e->ts.type != BT_LOGICAL)
590 gfc_error ("%qs argument of %qs intrinsic at %L must be of intrinsic type",
591 gfc_current_intrinsic_arg[n]->name,
592 gfc_current_intrinsic, &e->where);
593 return false;
595 return true;
598 /* Check that an expression is real or complex. */
600 static bool
601 real_or_complex_check (gfc_expr *e, int n)
603 if (e->ts.type != BT_REAL && e->ts.type != BT_COMPLEX)
605 gfc_error ("%qs argument of %qs intrinsic at %L must be REAL "
606 "or COMPLEX", gfc_current_intrinsic_arg[n]->name,
607 gfc_current_intrinsic, &e->where);
608 return false;
611 return true;
615 /* Check that an expression is INTEGER or PROCEDURE. */
617 static bool
618 int_or_proc_check (gfc_expr *e, int n)
620 if (e->ts.type != BT_INTEGER && e->ts.type != BT_PROCEDURE)
622 gfc_error ("%qs argument of %qs intrinsic at %L must be INTEGER "
623 "or PROCEDURE", gfc_current_intrinsic_arg[n]->name,
624 gfc_current_intrinsic, &e->where);
625 return false;
628 return true;
632 /* Check that the expression is an optional constant integer
633 and that it specifies a valid kind for that type. */
635 static bool
636 kind_check (gfc_expr *k, int n, bt type)
638 int kind;
640 if (k == NULL)
641 return true;
643 if (!type_check (k, n, BT_INTEGER))
644 return false;
646 if (!scalar_check (k, n))
647 return false;
649 if (!gfc_check_init_expr (k))
651 gfc_error ("%qs argument of %qs intrinsic at %L must be a constant",
652 gfc_current_intrinsic_arg[n]->name, gfc_current_intrinsic,
653 &k->where);
654 return false;
657 if (gfc_extract_int (k, &kind)
658 || gfc_validate_kind (type, kind, true) < 0)
660 gfc_error ("Invalid kind for %s at %L", gfc_basic_typename (type),
661 &k->where);
662 return false;
665 return true;
669 /* Make sure the expression is a double precision real. */
671 static bool
672 double_check (gfc_expr *d, int n)
674 if (!type_check (d, n, BT_REAL))
675 return false;
677 if (d->ts.kind != gfc_default_double_kind)
679 gfc_error ("%qs argument of %qs intrinsic at %L must be double "
680 "precision", gfc_current_intrinsic_arg[n]->name,
681 gfc_current_intrinsic, &d->where);
682 return false;
685 return true;
689 static bool
690 coarray_check (gfc_expr *e, int n)
692 if (e->ts.type == BT_CLASS && gfc_expr_attr (e).class_ok
693 && CLASS_DATA (e)->attr.codimension
694 && CLASS_DATA (e)->as->corank)
696 gfc_add_class_array_ref (e);
697 return true;
700 if (!gfc_is_coarray (e))
702 gfc_error ("Expected coarray variable as %qs argument to the %s "
703 "intrinsic at %L", gfc_current_intrinsic_arg[n]->name,
704 gfc_current_intrinsic, &e->where);
705 return false;
708 return true;
712 /* Make sure the expression is a logical array. */
714 static bool
715 logical_array_check (gfc_expr *array, int n)
717 if (array->ts.type != BT_LOGICAL || array->rank == 0)
719 gfc_error ("%qs argument of %qs intrinsic at %L must be a logical "
720 "array", gfc_current_intrinsic_arg[n]->name,
721 gfc_current_intrinsic, &array->where);
722 return false;
725 return true;
729 /* Make sure an expression is an array. */
731 static bool
732 array_check (gfc_expr *e, int n)
734 if (e->rank != 0 && e->ts.type == BT_CLASS && gfc_expr_attr (e).class_ok
735 && CLASS_DATA (e)->attr.dimension
736 && CLASS_DATA (e)->as->rank)
738 gfc_add_class_array_ref (e);
741 if (e->rank != 0 && e->ts.type != BT_PROCEDURE)
742 return true;
744 gfc_error ("%qs argument of %qs intrinsic at %L must be an array",
745 gfc_current_intrinsic_arg[n]->name, gfc_current_intrinsic,
746 &e->where);
748 return false;
752 /* If expr is a constant, then check to ensure that it is greater than
753 of equal to zero. */
755 static bool
756 nonnegative_check (const char *arg, gfc_expr *expr)
758 int i;
760 if (expr->expr_type == EXPR_CONSTANT)
762 gfc_extract_int (expr, &i);
763 if (i < 0)
765 gfc_error ("%qs at %L must be nonnegative", arg, &expr->where);
766 return false;
770 return true;
774 /* If expr is a constant, then check to ensure that it is greater than zero. */
776 static bool
777 positive_check (int n, gfc_expr *expr)
779 int i;
781 if (expr->expr_type == EXPR_CONSTANT)
783 gfc_extract_int (expr, &i);
784 if (i <= 0)
786 gfc_error ("%qs argument of %qs intrinsic at %L must be positive",
787 gfc_current_intrinsic_arg[n]->name, gfc_current_intrinsic,
788 &expr->where);
789 return false;
793 return true;
797 /* If expr2 is constant, then check that the value is less than
798 (less than or equal to, if 'or_equal' is true) bit_size(expr1). */
800 static bool
801 less_than_bitsize1 (const char *arg1, gfc_expr *expr1, const char *arg2,
802 gfc_expr *expr2, bool or_equal)
804 int i2, i3;
806 if (expr2->expr_type == EXPR_CONSTANT)
808 gfc_extract_int (expr2, &i2);
809 i3 = gfc_validate_kind (BT_INTEGER, expr1->ts.kind, false);
811 /* For ISHFT[C], check that |shift| <= bit_size(i). */
812 if (arg2 == NULL)
814 if (i2 < 0)
815 i2 = -i2;
817 if (i2 > gfc_integer_kinds[i3].bit_size)
819 gfc_error ("The absolute value of SHIFT at %L must be less "
820 "than or equal to BIT_SIZE(%qs)",
821 &expr2->where, arg1);
822 return false;
826 if (or_equal)
828 if (i2 > gfc_integer_kinds[i3].bit_size)
830 gfc_error ("%qs at %L must be less than "
831 "or equal to BIT_SIZE(%qs)",
832 arg2, &expr2->where, arg1);
833 return false;
836 else
838 if (i2 >= gfc_integer_kinds[i3].bit_size)
840 gfc_error ("%qs at %L must be less than BIT_SIZE(%qs)",
841 arg2, &expr2->where, arg1);
842 return false;
847 return true;
851 /* If expr is constant, then check that the value is less than or equal
852 to the bit_size of the kind k. */
854 static bool
855 less_than_bitsizekind (const char *arg, gfc_expr *expr, int k)
857 int i, val;
859 if (expr->expr_type != EXPR_CONSTANT)
860 return true;
862 i = gfc_validate_kind (BT_INTEGER, k, false);
863 gfc_extract_int (expr, &val);
865 if (val > gfc_integer_kinds[i].bit_size)
867 gfc_error ("%qs at %L must be less than or equal to the BIT_SIZE of "
868 "INTEGER(KIND=%d)", arg, &expr->where, k);
869 return false;
872 return true;
876 /* If expr2 and expr3 are constants, then check that the value is less than
877 or equal to bit_size(expr1). */
879 static bool
880 less_than_bitsize2 (const char *arg1, gfc_expr *expr1, const char *arg2,
881 gfc_expr *expr2, const char *arg3, gfc_expr *expr3)
883 int i2, i3;
885 if (expr2->expr_type == EXPR_CONSTANT && expr3->expr_type == EXPR_CONSTANT)
887 gfc_extract_int (expr2, &i2);
888 gfc_extract_int (expr3, &i3);
889 i2 += i3;
890 i3 = gfc_validate_kind (BT_INTEGER, expr1->ts.kind, false);
891 if (i2 > gfc_integer_kinds[i3].bit_size)
893 gfc_error ("%<%s + %s%> at %L must be less than or equal "
894 "to BIT_SIZE(%qs)",
895 arg2, arg3, &expr2->where, arg1);
896 return false;
900 return true;
903 /* Make sure two expressions have the same type. */
905 static bool
906 same_type_check (gfc_expr *e, int n, gfc_expr *f, int m, bool assoc = false)
908 gfc_typespec *ets = &e->ts;
909 gfc_typespec *fts = &f->ts;
911 if (assoc)
913 /* Procedure pointer component expressions have the type of the interface
914 procedure. If they are being tested for association with a procedure
915 pointer (ie. not a component), the type of the procedure must be
916 determined. */
917 if (e->ts.type == BT_PROCEDURE && e->symtree->n.sym)
918 ets = &e->symtree->n.sym->ts;
919 if (f->ts.type == BT_PROCEDURE && f->symtree->n.sym)
920 fts = &f->symtree->n.sym->ts;
923 if (gfc_compare_types (ets, fts))
924 return true;
926 gfc_error ("%qs argument of %qs intrinsic at %L must be the same type "
927 "and kind as %qs", gfc_current_intrinsic_arg[m]->name,
928 gfc_current_intrinsic, &f->where,
929 gfc_current_intrinsic_arg[n]->name);
931 return false;
935 /* Make sure that an expression has a certain (nonzero) rank. */
937 static bool
938 rank_check (gfc_expr *e, int n, int rank)
940 if (e->rank == rank)
941 return true;
943 gfc_error ("%qs argument of %qs intrinsic at %L must be of rank %d",
944 gfc_current_intrinsic_arg[n]->name, gfc_current_intrinsic,
945 &e->where, rank);
947 return false;
951 /* Make sure a variable expression is not an optional dummy argument. */
953 static bool
954 nonoptional_check (gfc_expr *e, int n)
956 if (e->expr_type == EXPR_VARIABLE && e->symtree->n.sym->attr.optional)
958 gfc_error ("%qs argument of %qs intrinsic at %L must not be OPTIONAL",
959 gfc_current_intrinsic_arg[n]->name, gfc_current_intrinsic,
960 &e->where);
963 /* TODO: Recursive check on nonoptional variables? */
965 return true;
969 /* Check for ALLOCATABLE attribute. */
971 static bool
972 allocatable_check (gfc_expr *e, int n)
974 symbol_attribute attr;
976 attr = gfc_variable_attr (e, NULL);
977 if (!attr.allocatable
978 || (attr.associate_var && !attr.select_rank_temporary))
980 gfc_error ("%qs argument of %qs intrinsic at %L must be ALLOCATABLE",
981 gfc_current_intrinsic_arg[n]->name, gfc_current_intrinsic,
982 &e->where);
983 return false;
986 return true;
990 /* Check that an expression has a particular kind. */
992 static bool
993 kind_value_check (gfc_expr *e, int n, int k)
995 if (e->ts.kind == k)
996 return true;
998 gfc_error ("%qs argument of %qs intrinsic at %L must be of kind %d",
999 gfc_current_intrinsic_arg[n]->name, gfc_current_intrinsic,
1000 &e->where, k);
1002 return false;
1006 /* Make sure an expression is a variable. */
1008 static bool
1009 variable_check (gfc_expr *e, int n, bool allow_proc)
1011 if (e->expr_type == EXPR_VARIABLE
1012 && e->symtree->n.sym->attr.intent == INTENT_IN
1013 && (gfc_current_intrinsic_arg[n]->intent == INTENT_OUT
1014 || gfc_current_intrinsic_arg[n]->intent == INTENT_INOUT)
1015 && !gfc_check_vardef_context (e, false, true, false, NULL))
1017 gfc_error ("%qs argument of %qs intrinsic at %L cannot be INTENT(IN)",
1018 gfc_current_intrinsic_arg[n]->name,
1019 gfc_current_intrinsic, &e->where);
1020 return false;
1023 if (e->expr_type == EXPR_VARIABLE
1024 && e->symtree->n.sym->attr.flavor != FL_PARAMETER
1025 && (allow_proc || !e->symtree->n.sym->attr.function))
1026 return true;
1028 if (e->expr_type == EXPR_VARIABLE && e->symtree->n.sym->attr.function
1029 && e->symtree->n.sym == e->symtree->n.sym->result)
1031 gfc_namespace *ns;
1032 for (ns = gfc_current_ns; ns; ns = ns->parent)
1033 if (ns->proc_name == e->symtree->n.sym)
1034 return true;
1037 /* F2018:R902: function reference having a data pointer result. */
1038 if (e->expr_type == EXPR_FUNCTION
1039 && e->symtree->n.sym->attr.flavor == FL_PROCEDURE
1040 && e->symtree->n.sym->attr.function
1041 && e->symtree->n.sym->attr.pointer)
1042 return true;
1044 gfc_error ("%qs argument of %qs intrinsic at %L must be a variable",
1045 gfc_current_intrinsic_arg[n]->name, gfc_current_intrinsic, &e->where);
1047 return false;
1051 /* Check the common DIM parameter for correctness. */
1053 static bool
1054 dim_check (gfc_expr *dim, int n, bool optional)
1056 if (dim == NULL)
1057 return true;
1059 if (!type_check (dim, n, BT_INTEGER))
1060 return false;
1062 if (!scalar_check (dim, n))
1063 return false;
1065 if (!optional && !nonoptional_check (dim, n))
1066 return false;
1068 return true;
1072 /* If a coarray DIM parameter is a constant, make sure that it is greater than
1073 zero and less than or equal to the corank of the given array. */
1075 static bool
1076 dim_corank_check (gfc_expr *dim, gfc_expr *array)
1078 int corank;
1080 gcc_assert (array->expr_type == EXPR_VARIABLE);
1082 if (dim->expr_type != EXPR_CONSTANT)
1083 return true;
1085 if (array->ts.type == BT_CLASS)
1086 return true;
1088 corank = gfc_get_corank (array);
1090 if (mpz_cmp_ui (dim->value.integer, 1) < 0
1091 || mpz_cmp_ui (dim->value.integer, corank) > 0)
1093 gfc_error ("%<dim%> argument of %qs intrinsic at %L is not a valid "
1094 "codimension index", gfc_current_intrinsic, &dim->where);
1096 return false;
1099 return true;
1103 /* If a DIM parameter is a constant, make sure that it is greater than
1104 zero and less than or equal to the rank of the given array. If
1105 allow_assumed is zero then dim must be less than the rank of the array
1106 for assumed size arrays. */
1108 static bool
1109 dim_rank_check (gfc_expr *dim, gfc_expr *array, int allow_assumed)
1111 gfc_array_ref *ar;
1112 int rank;
1114 if (dim == NULL)
1115 return true;
1117 if (dim->expr_type != EXPR_CONSTANT)
1118 return true;
1120 if (array->expr_type == EXPR_FUNCTION && array->value.function.isym
1121 && array->value.function.isym->id == GFC_ISYM_SPREAD)
1122 rank = array->rank + 1;
1123 else
1124 rank = array->rank;
1126 /* Assumed-rank array. */
1127 if (rank == -1)
1128 rank = GFC_MAX_DIMENSIONS;
1130 if (array->expr_type == EXPR_VARIABLE)
1132 ar = gfc_find_array_ref (array, true);
1133 if (!ar)
1134 return false;
1135 if (ar->as->type == AS_ASSUMED_SIZE
1136 && !allow_assumed
1137 && ar->type != AR_ELEMENT
1138 && ar->type != AR_SECTION)
1139 rank--;
1142 if (mpz_cmp_ui (dim->value.integer, 1) < 0
1143 || mpz_cmp_ui (dim->value.integer, rank) > 0)
1145 gfc_error ("%<dim%> argument of %qs intrinsic at %L is not a valid "
1146 "dimension index", gfc_current_intrinsic, &dim->where);
1148 return false;
1151 return true;
1155 /* Compare the size of a along dimension ai with the size of b along
1156 dimension bi, returning 0 if they are known not to be identical,
1157 and 1 if they are identical, or if this cannot be determined. */
1159 static bool
1160 identical_dimen_shape (gfc_expr *a, int ai, gfc_expr *b, int bi)
1162 mpz_t a_size, b_size;
1163 bool ret;
1165 gcc_assert (a->rank > ai);
1166 gcc_assert (b->rank > bi);
1168 ret = true;
1170 if (gfc_array_dimen_size (a, ai, &a_size))
1172 if (gfc_array_dimen_size (b, bi, &b_size))
1174 if (mpz_cmp (a_size, b_size) != 0)
1175 ret = false;
1177 mpz_clear (b_size);
1179 mpz_clear (a_size);
1181 return ret;
1184 /* Calculate the length of a character variable, including substrings.
1185 Strip away parentheses if necessary. Return -1 if no length could
1186 be determined. */
1188 static long
1189 gfc_var_strlen (const gfc_expr *a)
1191 gfc_ref *ra;
1193 while (a->expr_type == EXPR_OP && a->value.op.op == INTRINSIC_PARENTHESES)
1194 a = a->value.op.op1;
1196 for (ra = a->ref; ra != NULL && ra->type != REF_SUBSTRING; ra = ra->next)
1199 if (ra)
1201 long start_a, end_a;
1203 if (!ra->u.ss.end)
1204 return -1;
1206 if ((!ra->u.ss.start || ra->u.ss.start->expr_type == EXPR_CONSTANT)
1207 && ra->u.ss.end->expr_type == EXPR_CONSTANT)
1209 start_a = ra->u.ss.start ? mpz_get_si (ra->u.ss.start->value.integer)
1210 : 1;
1211 end_a = mpz_get_si (ra->u.ss.end->value.integer);
1212 return (end_a < start_a) ? 0 : end_a - start_a + 1;
1214 else if (ra->u.ss.start
1215 && gfc_dep_compare_expr (ra->u.ss.start, ra->u.ss.end) == 0)
1216 return 1;
1217 else
1218 return -1;
1221 if (a->ts.u.cl && a->ts.u.cl->length
1222 && a->ts.u.cl->length->expr_type == EXPR_CONSTANT)
1223 return mpz_get_si (a->ts.u.cl->length->value.integer);
1224 else if (a->expr_type == EXPR_CONSTANT
1225 && (a->ts.u.cl == NULL || a->ts.u.cl->length == NULL))
1226 return a->value.character.length;
1227 else
1228 return -1;
1232 /* Check whether two character expressions have the same length;
1233 returns true if they have or if the length cannot be determined,
1234 otherwise return false and raise a gfc_error. */
1236 bool
1237 gfc_check_same_strlen (const gfc_expr *a, const gfc_expr *b, const char *name)
1239 long len_a, len_b;
1241 len_a = gfc_var_strlen(a);
1242 len_b = gfc_var_strlen(b);
1244 if (len_a == -1 || len_b == -1 || len_a == len_b)
1245 return true;
1246 else
1248 gfc_error ("Unequal character lengths (%ld/%ld) in %s at %L",
1249 len_a, len_b, name, &a->where);
1250 return false;
1254 /* Check size of an array argument against a required size.
1255 Returns true if the requirement is satisfied or if the size cannot be
1256 determined, otherwise return false and raise a gfc_error */
1258 static bool
1259 array_size_check (gfc_expr *a, int n, long size_min)
1261 bool ok = true;
1262 mpz_t size;
1264 if (gfc_array_size (a, &size))
1266 HOST_WIDE_INT sz = gfc_mpz_get_hwi (size);
1267 if (size_min >= 0 && sz < size_min)
1269 gfc_error ("Size of %qs argument of %qs intrinsic at %L "
1270 "too small (%wd/%ld)",
1271 gfc_current_intrinsic_arg[n]->name,
1272 gfc_current_intrinsic, &a->where, sz, size_min);
1273 ok = false;
1275 mpz_clear (size);
1278 return ok;
1282 /***** Check functions *****/
1284 /* Check subroutine suitable for intrinsics taking a real argument and
1285 a kind argument for the result. */
1287 static bool
1288 check_a_kind (gfc_expr *a, gfc_expr *kind, bt type)
1290 if (!type_check (a, 0, BT_REAL))
1291 return false;
1292 if (!kind_check (kind, 1, type))
1293 return false;
1295 return true;
1299 /* Check subroutine suitable for ceiling, floor and nint. */
1301 bool
1302 gfc_check_a_ikind (gfc_expr *a, gfc_expr *kind)
1304 return check_a_kind (a, kind, BT_INTEGER);
1308 /* Check subroutine suitable for aint, anint. */
1310 bool
1311 gfc_check_a_xkind (gfc_expr *a, gfc_expr *kind)
1313 return check_a_kind (a, kind, BT_REAL);
1317 bool
1318 gfc_check_abs (gfc_expr *a)
1320 if (!numeric_check (a, 0))
1321 return false;
1323 return true;
1327 bool
1328 gfc_check_achar (gfc_expr *a, gfc_expr *kind)
1330 if (a->ts.type == BT_BOZ)
1332 if (gfc_invalid_boz (G_("BOZ literal constant at %L cannot appear in "
1333 "ACHAR intrinsic subprogram"), &a->where))
1334 return false;
1336 if (!gfc_boz2int (a, gfc_default_integer_kind))
1337 return false;
1340 if (!type_check (a, 0, BT_INTEGER))
1341 return false;
1343 if (!kind_check (kind, 1, BT_CHARACTER))
1344 return false;
1346 return true;
1350 bool
1351 gfc_check_access_func (gfc_expr *name, gfc_expr *mode)
1353 if (!type_check (name, 0, BT_CHARACTER)
1354 || !scalar_check (name, 0))
1355 return false;
1356 if (!kind_value_check (name, 0, gfc_default_character_kind))
1357 return false;
1359 if (!type_check (mode, 1, BT_CHARACTER)
1360 || !scalar_check (mode, 1))
1361 return false;
1362 if (!kind_value_check (mode, 1, gfc_default_character_kind))
1363 return false;
1365 return true;
1369 bool
1370 gfc_check_all_any (gfc_expr *mask, gfc_expr *dim)
1372 if (!logical_array_check (mask, 0))
1373 return false;
1375 if (!dim_check (dim, 1, false))
1376 return false;
1378 if (!dim_rank_check (dim, mask, 0))
1379 return false;
1381 return true;
1385 /* Limited checking for ALLOCATED intrinsic. Additional checking
1386 is performed in intrinsic.cc(sort_actual), because ALLOCATED
1387 has two mutually exclusive non-optional arguments. */
1389 bool
1390 gfc_check_allocated (gfc_expr *array)
1392 /* Tests on allocated components of coarrays need to detour the check to
1393 argument of the _caf_get. */
1394 if (flag_coarray == GFC_FCOARRAY_LIB && array->expr_type == EXPR_FUNCTION
1395 && array->value.function.isym
1396 && array->value.function.isym->id == GFC_ISYM_CAF_GET)
1398 array = array->value.function.actual->expr;
1399 if (!array->ref)
1400 return false;
1403 if (!variable_check (array, 0, false))
1404 return false;
1405 if (!allocatable_check (array, 0))
1406 return false;
1408 return true;
1412 /* Common check function where the first argument must be real or
1413 integer and the second argument must be the same as the first. */
1415 bool
1416 gfc_check_a_p (gfc_expr *a, gfc_expr *p)
1418 if (!int_or_real_check (a, 0))
1419 return false;
1421 if (a->ts.type != p->ts.type)
1423 gfc_error ("%qs and %qs arguments of %qs intrinsic at %L must "
1424 "have the same type", gfc_current_intrinsic_arg[0]->name,
1425 gfc_current_intrinsic_arg[1]->name, gfc_current_intrinsic,
1426 &p->where);
1427 return false;
1430 if (a->ts.kind != p->ts.kind)
1432 if (!gfc_notify_std (GFC_STD_GNU, "Different type kinds at %L",
1433 &p->where))
1434 return false;
1437 return true;
1441 bool
1442 gfc_check_x_yd (gfc_expr *x, gfc_expr *y)
1444 if (!double_check (x, 0) || !double_check (y, 1))
1445 return false;
1447 return true;
1450 bool
1451 gfc_invalid_null_arg (gfc_expr *x)
1453 if (x->expr_type == EXPR_NULL)
1455 gfc_error ("NULL at %L is not permitted as actual argument "
1456 "to %qs intrinsic function", &x->where,
1457 gfc_current_intrinsic);
1458 return true;
1460 return false;
1463 bool
1464 gfc_check_associated (gfc_expr *pointer, gfc_expr *target)
1466 symbol_attribute attr1, attr2;
1467 int i;
1468 bool t;
1470 if (gfc_invalid_null_arg (pointer))
1471 return false;
1473 attr1 = gfc_expr_attr (pointer);
1475 if (!attr1.pointer && !attr1.proc_pointer)
1477 gfc_error ("%qs argument of %qs intrinsic at %L must be a POINTER",
1478 gfc_current_intrinsic_arg[0]->name, gfc_current_intrinsic,
1479 &pointer->where);
1480 return false;
1483 /* F2008, C1242. */
1484 if (attr1.pointer && gfc_is_coindexed (pointer))
1486 gfc_error ("%qs argument of %qs intrinsic at %L shall not be "
1487 "coindexed", gfc_current_intrinsic_arg[0]->name,
1488 gfc_current_intrinsic, &pointer->where);
1489 return false;
1492 /* Target argument is optional. */
1493 if (target == NULL)
1494 return true;
1496 if (gfc_invalid_null_arg (target))
1497 return false;
1499 if (target->expr_type == EXPR_VARIABLE || target->expr_type == EXPR_FUNCTION)
1500 attr2 = gfc_expr_attr (target);
1501 else
1503 gfc_error ("%qs argument of %qs intrinsic at %L must be a pointer "
1504 "or target VARIABLE or FUNCTION",
1505 gfc_current_intrinsic_arg[1]->name, gfc_current_intrinsic,
1506 &target->where);
1507 return false;
1510 if (attr1.pointer && !attr2.pointer && !attr2.target)
1512 gfc_error ("%qs argument of %qs intrinsic at %L must be a POINTER "
1513 "or a TARGET", gfc_current_intrinsic_arg[1]->name,
1514 gfc_current_intrinsic, &target->where);
1515 return false;
1518 /* F2008, C1242. */
1519 if (attr1.pointer && gfc_is_coindexed (target))
1521 gfc_error ("%qs argument of %qs intrinsic at %L shall not be "
1522 "coindexed", gfc_current_intrinsic_arg[1]->name,
1523 gfc_current_intrinsic, &target->where);
1524 return false;
1527 t = true;
1528 if (!same_type_check (pointer, 0, target, 1, true))
1529 t = false;
1530 /* F2018 C838 explicitly allows an assumed-rank variable as the first
1531 argument of intrinsic inquiry functions. */
1532 if (pointer->rank != -1 && !rank_check (target, 0, pointer->rank))
1533 t = false;
1534 if (target->rank > 0 && target->ref)
1536 for (i = 0; i < target->rank; i++)
1537 if (target->ref->u.ar.dimen_type[i] == DIMEN_VECTOR)
1539 gfc_error ("Array section with a vector subscript at %L shall not "
1540 "be the target of a pointer",
1541 &target->where);
1542 t = false;
1543 break;
1546 return t;
1550 bool
1551 gfc_check_atan_2 (gfc_expr *y, gfc_expr *x)
1553 /* gfc_notify_std would be a waste of time as the return value
1554 is seemingly used only for the generic resolution. The error
1555 will be: Too many arguments. */
1556 if ((gfc_option.allow_std & GFC_STD_F2008) == 0)
1557 return false;
1559 return gfc_check_atan2 (y, x);
1563 bool
1564 gfc_check_atan2 (gfc_expr *y, gfc_expr *x)
1566 if (!type_check (y, 0, BT_REAL))
1567 return false;
1568 if (!same_type_check (y, 0, x, 1))
1569 return false;
1571 return true;
1575 static bool
1576 gfc_check_atomic (gfc_expr *atom, int atom_no, gfc_expr *value, int val_no,
1577 gfc_expr *stat, int stat_no)
1579 if (!scalar_check (atom, atom_no) || !scalar_check (value, val_no))
1580 return false;
1582 if (!(atom->ts.type == BT_INTEGER && atom->ts.kind == gfc_atomic_int_kind)
1583 && !(atom->ts.type == BT_LOGICAL
1584 && atom->ts.kind == gfc_atomic_logical_kind))
1586 gfc_error ("ATOM argument at %L to intrinsic function %s shall be an "
1587 "integer of ATOMIC_INT_KIND or a logical of "
1588 "ATOMIC_LOGICAL_KIND", &atom->where, gfc_current_intrinsic);
1589 return false;
1592 if (!gfc_is_coarray (atom) && !gfc_is_coindexed (atom))
1594 gfc_error ("ATOM argument at %L of the %s intrinsic function shall be a "
1595 "coarray or coindexed", &atom->where, gfc_current_intrinsic);
1596 return false;
1599 if (atom->ts.type != value->ts.type)
1601 gfc_error ("%qs argument of %qs intrinsic at %L shall have the same "
1602 "type as %qs at %L", gfc_current_intrinsic_arg[val_no]->name,
1603 gfc_current_intrinsic, &value->where,
1604 gfc_current_intrinsic_arg[atom_no]->name, &atom->where);
1605 return false;
1608 if (stat != NULL)
1610 if (!type_check (stat, stat_no, BT_INTEGER))
1611 return false;
1612 if (!scalar_check (stat, stat_no))
1613 return false;
1614 if (!variable_check (stat, stat_no, false))
1615 return false;
1616 if (!kind_value_check (stat, stat_no, gfc_default_integer_kind))
1617 return false;
1619 if (!gfc_notify_std (GFC_STD_F2018, "STAT= argument to %s at %L",
1620 gfc_current_intrinsic, &stat->where))
1621 return false;
1624 return true;
1628 bool
1629 gfc_check_atomic_def (gfc_expr *atom, gfc_expr *value, gfc_expr *stat)
1631 if (atom->expr_type == EXPR_FUNCTION
1632 && atom->value.function.isym
1633 && atom->value.function.isym->id == GFC_ISYM_CAF_GET)
1634 atom = atom->value.function.actual->expr;
1636 if (!gfc_check_vardef_context (atom, false, false, false, NULL))
1638 gfc_error ("ATOM argument of the %s intrinsic function at %L shall be "
1639 "definable", gfc_current_intrinsic, &atom->where);
1640 return false;
1643 return gfc_check_atomic (atom, 0, value, 1, stat, 2);
1647 bool
1648 gfc_check_atomic_op (gfc_expr *atom, gfc_expr *value, gfc_expr *stat)
1650 if (atom->ts.type != BT_INTEGER || atom->ts.kind != gfc_atomic_int_kind)
1652 gfc_error ("ATOM argument at %L to intrinsic function %s shall be an "
1653 "integer of ATOMIC_INT_KIND", &atom->where,
1654 gfc_current_intrinsic);
1655 return false;
1658 return gfc_check_atomic_def (atom, value, stat);
1662 bool
1663 gfc_check_atomic_ref (gfc_expr *value, gfc_expr *atom, gfc_expr *stat)
1665 if (atom->expr_type == EXPR_FUNCTION
1666 && atom->value.function.isym
1667 && atom->value.function.isym->id == GFC_ISYM_CAF_GET)
1668 atom = atom->value.function.actual->expr;
1670 if (!gfc_check_vardef_context (value, false, false, false, NULL))
1672 gfc_error ("VALUE argument of the %s intrinsic function at %L shall be "
1673 "definable", gfc_current_intrinsic, &value->where);
1674 return false;
1677 return gfc_check_atomic (atom, 1, value, 0, stat, 2);
1681 bool
1682 gfc_check_image_status (gfc_expr *image, gfc_expr *team)
1684 /* IMAGE has to be a positive, scalar integer. */
1685 if (!type_check (image, 0, BT_INTEGER) || !scalar_check (image, 0)
1686 || !positive_check (0, image))
1687 return false;
1689 if (team)
1691 gfc_error ("%qs argument of %qs intrinsic at %L not yet supported",
1692 gfc_current_intrinsic_arg[1]->name, gfc_current_intrinsic,
1693 &team->where);
1694 return false;
1696 return true;
1700 bool
1701 gfc_check_failed_or_stopped_images (gfc_expr *team, gfc_expr *kind)
1703 if (team)
1705 gfc_error ("%qs argument of %qs intrinsic at %L not yet supported",
1706 gfc_current_intrinsic_arg[0]->name, gfc_current_intrinsic,
1707 &team->where);
1708 return false;
1711 if (kind)
1713 int k;
1715 if (!type_check (kind, 1, BT_INTEGER) || !scalar_check (kind, 1)
1716 || !positive_check (1, kind))
1717 return false;
1719 /* Get the kind, reporting error on non-constant or overflow. */
1720 gfc_current_locus = kind->where;
1721 if (gfc_extract_int (kind, &k, 1))
1722 return false;
1723 if (gfc_validate_kind (BT_INTEGER, k, true) == -1)
1725 gfc_error ("%qs argument of %qs intrinsic at %L shall specify a "
1726 "valid integer kind", gfc_current_intrinsic_arg[1]->name,
1727 gfc_current_intrinsic, &kind->where);
1728 return false;
1731 return true;
1735 bool
1736 gfc_check_get_team (gfc_expr *level)
1738 if (level)
1740 gfc_error ("%qs argument of %qs intrinsic at %L not yet supported",
1741 gfc_current_intrinsic_arg[0]->name, gfc_current_intrinsic,
1742 &level->where);
1743 return false;
1745 return true;
1749 bool
1750 gfc_check_atomic_cas (gfc_expr *atom, gfc_expr *old, gfc_expr *compare,
1751 gfc_expr *new_val, gfc_expr *stat)
1753 if (atom->expr_type == EXPR_FUNCTION
1754 && atom->value.function.isym
1755 && atom->value.function.isym->id == GFC_ISYM_CAF_GET)
1756 atom = atom->value.function.actual->expr;
1758 if (!gfc_check_atomic (atom, 0, new_val, 3, stat, 4))
1759 return false;
1761 if (!scalar_check (old, 1) || !scalar_check (compare, 2))
1762 return false;
1764 if (!same_type_check (atom, 0, old, 1))
1765 return false;
1767 if (!same_type_check (atom, 0, compare, 2))
1768 return false;
1770 if (!gfc_check_vardef_context (atom, false, false, false, NULL))
1772 gfc_error ("ATOM argument of the %s intrinsic function at %L shall be "
1773 "definable", gfc_current_intrinsic, &atom->where);
1774 return false;
1777 if (!gfc_check_vardef_context (old, false, false, false, NULL))
1779 gfc_error ("OLD argument of the %s intrinsic function at %L shall be "
1780 "definable", gfc_current_intrinsic, &old->where);
1781 return false;
1784 return true;
1787 bool
1788 gfc_check_event_query (gfc_expr *event, gfc_expr *count, gfc_expr *stat)
1790 if (event->ts.type != BT_DERIVED
1791 || event->ts.u.derived->from_intmod != INTMOD_ISO_FORTRAN_ENV
1792 || event->ts.u.derived->intmod_sym_id != ISOFORTRAN_EVENT_TYPE)
1794 gfc_error ("EVENT argument at %L to the intrinsic EVENT_QUERY "
1795 "shall be of type EVENT_TYPE", &event->where);
1796 return false;
1799 if (!scalar_check (event, 0))
1800 return false;
1802 if (!gfc_check_vardef_context (count, false, false, false, NULL))
1804 gfc_error ("COUNT argument of the EVENT_QUERY intrinsic function at %L "
1805 "shall be definable", &count->where);
1806 return false;
1809 if (!type_check (count, 1, BT_INTEGER))
1810 return false;
1812 int i = gfc_validate_kind (BT_INTEGER, count->ts.kind, false);
1813 int j = gfc_validate_kind (BT_INTEGER, gfc_default_integer_kind, false);
1815 if (gfc_integer_kinds[i].range < gfc_integer_kinds[j].range)
1817 gfc_error ("COUNT argument of the EVENT_QUERY intrinsic function at %L "
1818 "shall have at least the range of the default integer",
1819 &count->where);
1820 return false;
1823 if (stat != NULL)
1825 if (!type_check (stat, 2, BT_INTEGER))
1826 return false;
1827 if (!scalar_check (stat, 2))
1828 return false;
1829 if (!variable_check (stat, 2, false))
1830 return false;
1832 if (!gfc_notify_std (GFC_STD_F2018, "STAT= argument to %s at %L",
1833 gfc_current_intrinsic, &stat->where))
1834 return false;
1837 return true;
1841 bool
1842 gfc_check_atomic_fetch_op (gfc_expr *atom, gfc_expr *value, gfc_expr *old,
1843 gfc_expr *stat)
1845 if (atom->expr_type == EXPR_FUNCTION
1846 && atom->value.function.isym
1847 && atom->value.function.isym->id == GFC_ISYM_CAF_GET)
1848 atom = atom->value.function.actual->expr;
1850 if (atom->ts.type != BT_INTEGER || atom->ts.kind != gfc_atomic_int_kind)
1852 gfc_error ("ATOM argument at %L to intrinsic function %s shall be an "
1853 "integer of ATOMIC_INT_KIND", &atom->where,
1854 gfc_current_intrinsic);
1855 return false;
1858 if (!gfc_check_atomic (atom, 0, value, 1, stat, 3))
1859 return false;
1861 if (!scalar_check (old, 2))
1862 return false;
1864 if (!same_type_check (atom, 0, old, 2))
1865 return false;
1867 if (!gfc_check_vardef_context (atom, false, false, false, NULL))
1869 gfc_error ("ATOM argument of the %s intrinsic function at %L shall be "
1870 "definable", gfc_current_intrinsic, &atom->where);
1871 return false;
1874 if (!gfc_check_vardef_context (old, false, false, false, NULL))
1876 gfc_error ("OLD argument of the %s intrinsic function at %L shall be "
1877 "definable", gfc_current_intrinsic, &old->where);
1878 return false;
1881 return true;
1885 /* BESJN and BESYN functions. */
1887 bool
1888 gfc_check_besn (gfc_expr *n, gfc_expr *x)
1890 if (!type_check (n, 0, BT_INTEGER))
1891 return false;
1892 if (n->expr_type == EXPR_CONSTANT)
1894 int i;
1895 gfc_extract_int (n, &i);
1896 if (i < 0 && !gfc_notify_std (GFC_STD_GNU, "Negative argument "
1897 "N at %L", &n->where))
1898 return false;
1901 if (!type_check (x, 1, BT_REAL))
1902 return false;
1904 return true;
1908 /* Transformational version of the Bessel JN and YN functions. */
1910 bool
1911 gfc_check_bessel_n2 (gfc_expr *n1, gfc_expr *n2, gfc_expr *x)
1913 if (!type_check (n1, 0, BT_INTEGER))
1914 return false;
1915 if (!scalar_check (n1, 0))
1916 return false;
1917 if (!nonnegative_check ("N1", n1))
1918 return false;
1920 if (!type_check (n2, 1, BT_INTEGER))
1921 return false;
1922 if (!scalar_check (n2, 1))
1923 return false;
1924 if (!nonnegative_check ("N2", n2))
1925 return false;
1927 if (!type_check (x, 2, BT_REAL))
1928 return false;
1929 if (!scalar_check (x, 2))
1930 return false;
1932 return true;
1936 bool
1937 gfc_check_bge_bgt_ble_blt (gfc_expr *i, gfc_expr *j)
1939 extern int gfc_max_integer_kind;
1941 /* If i and j are both BOZ, convert to widest INTEGER. */
1942 if (i->ts.type == BT_BOZ && j->ts.type == BT_BOZ)
1944 if (!gfc_boz2int (i, gfc_max_integer_kind))
1945 return false;
1946 if (!gfc_boz2int (j, gfc_max_integer_kind))
1947 return false;
1950 /* If i is BOZ and j is integer, convert i to type of j. */
1951 if (i->ts.type == BT_BOZ && j->ts.type == BT_INTEGER
1952 && !gfc_boz2int (i, j->ts.kind))
1953 return false;
1955 /* If j is BOZ and i is integer, convert j to type of i. */
1956 if (j->ts.type == BT_BOZ && i->ts.type == BT_INTEGER
1957 && !gfc_boz2int (j, i->ts.kind))
1958 return false;
1960 if (!type_check (i, 0, BT_INTEGER))
1961 return false;
1963 if (!type_check (j, 1, BT_INTEGER))
1964 return false;
1966 return true;
1970 bool
1971 gfc_check_bitfcn (gfc_expr *i, gfc_expr *pos)
1973 if (!type_check (i, 0, BT_INTEGER))
1974 return false;
1976 if (!type_check (pos, 1, BT_INTEGER))
1977 return false;
1979 if (!nonnegative_check ("pos", pos))
1980 return false;
1982 if (!less_than_bitsize1 ("i", i, "pos", pos, false))
1983 return false;
1985 return true;
1989 bool
1990 gfc_check_char (gfc_expr *i, gfc_expr *kind)
1992 if (i->ts.type == BT_BOZ)
1994 if (gfc_invalid_boz (G_("BOZ literal constant at %L cannot appear in "
1995 "CHAR intrinsic subprogram"), &i->where))
1996 return false;
1998 if (!gfc_boz2int (i, gfc_default_integer_kind))
1999 return false;
2002 if (!type_check (i, 0, BT_INTEGER))
2003 return false;
2005 if (!kind_check (kind, 1, BT_CHARACTER))
2006 return false;
2008 return true;
2012 bool
2013 gfc_check_chdir (gfc_expr *dir)
2015 if (!type_check (dir, 0, BT_CHARACTER))
2016 return false;
2017 if (!kind_value_check (dir, 0, gfc_default_character_kind))
2018 return false;
2020 return true;
2024 bool
2025 gfc_check_chdir_sub (gfc_expr *dir, gfc_expr *status)
2027 if (!type_check (dir, 0, BT_CHARACTER))
2028 return false;
2029 if (!kind_value_check (dir, 0, gfc_default_character_kind))
2030 return false;
2032 if (status == NULL)
2033 return true;
2035 if (!type_check (status, 1, BT_INTEGER))
2036 return false;
2037 if (!scalar_check (status, 1))
2038 return false;
2040 return true;
2044 bool
2045 gfc_check_chmod (gfc_expr *name, gfc_expr *mode)
2047 if (!type_check (name, 0, BT_CHARACTER))
2048 return false;
2049 if (!kind_value_check (name, 0, gfc_default_character_kind))
2050 return false;
2052 if (!type_check (mode, 1, BT_CHARACTER))
2053 return false;
2054 if (!kind_value_check (mode, 1, gfc_default_character_kind))
2055 return false;
2057 return true;
2061 bool
2062 gfc_check_chmod_sub (gfc_expr *name, gfc_expr *mode, gfc_expr *status)
2064 if (!type_check (name, 0, BT_CHARACTER))
2065 return false;
2066 if (!kind_value_check (name, 0, gfc_default_character_kind))
2067 return false;
2069 if (!type_check (mode, 1, BT_CHARACTER))
2070 return false;
2071 if (!kind_value_check (mode, 1, gfc_default_character_kind))
2072 return false;
2074 if (status == NULL)
2075 return true;
2077 if (!type_check (status, 2, BT_INTEGER))
2078 return false;
2080 if (!scalar_check (status, 2))
2081 return false;
2083 return true;
2087 bool
2088 gfc_check_cmplx (gfc_expr *x, gfc_expr *y, gfc_expr *kind)
2090 int k;
2092 /* Check kind first, because it may be needed in conversion of a BOZ. */
2093 if (kind)
2095 if (!kind_check (kind, 2, BT_COMPLEX))
2096 return false;
2097 gfc_extract_int (kind, &k);
2099 else
2100 k = gfc_default_complex_kind;
2102 if (x->ts.type == BT_BOZ && !gfc_boz2real (x, k))
2103 return false;
2105 if (!numeric_check (x, 0))
2106 return false;
2108 if (y != NULL)
2110 if (y->ts.type == BT_BOZ && !gfc_boz2real (y, k))
2111 return false;
2113 if (!numeric_check (y, 1))
2114 return false;
2116 if (x->ts.type == BT_COMPLEX)
2118 gfc_error ("%qs argument of %qs intrinsic at %L must not be "
2119 "present if %<x%> is COMPLEX",
2120 gfc_current_intrinsic_arg[1]->name, gfc_current_intrinsic,
2121 &y->where);
2122 return false;
2125 if (y->ts.type == BT_COMPLEX)
2127 gfc_error ("%qs argument of %qs intrinsic at %L must have a type "
2128 "of either REAL or INTEGER",
2129 gfc_current_intrinsic_arg[1]->name, gfc_current_intrinsic,
2130 &y->where);
2131 return false;
2135 if (!kind && warn_conversion
2136 && x->ts.type == BT_REAL && x->ts.kind > gfc_default_real_kind)
2137 gfc_warning_now (OPT_Wconversion, "Conversion from %s to default-kind "
2138 "COMPLEX(%d) at %L might lose precision, consider using "
2139 "the KIND argument", gfc_typename (&x->ts),
2140 gfc_default_real_kind, &x->where);
2141 else if (y && !kind && warn_conversion
2142 && y->ts.type == BT_REAL && y->ts.kind > gfc_default_real_kind)
2143 gfc_warning_now (OPT_Wconversion, "Conversion from %s to default-kind "
2144 "COMPLEX(%d) at %L might lose precision, consider using "
2145 "the KIND argument", gfc_typename (&y->ts),
2146 gfc_default_real_kind, &y->where);
2147 return true;
2151 static bool
2152 check_co_collective (gfc_expr *a, gfc_expr *image_idx, gfc_expr *stat,
2153 gfc_expr *errmsg, bool co_reduce)
2155 if (!variable_check (a, 0, false))
2156 return false;
2158 if (!gfc_check_vardef_context (a, false, false, false, "argument 'A' with "
2159 "INTENT(INOUT)"))
2160 return false;
2162 /* Fortran 2008, 12.5.2.4, paragraph 18. */
2163 if (gfc_has_vector_subscript (a))
2165 gfc_error ("Argument %<A%> with INTENT(INOUT) at %L of the intrinsic "
2166 "subroutine %s shall not have a vector subscript",
2167 &a->where, gfc_current_intrinsic);
2168 return false;
2171 if (gfc_is_coindexed (a))
2173 gfc_error ("The A argument at %L to the intrinsic %s shall not be "
2174 "coindexed", &a->where, gfc_current_intrinsic);
2175 return false;
2178 if (image_idx != NULL)
2180 if (!type_check (image_idx, co_reduce ? 2 : 1, BT_INTEGER))
2181 return false;
2182 if (!scalar_check (image_idx, co_reduce ? 2 : 1))
2183 return false;
2186 if (stat != NULL)
2188 if (!type_check (stat, co_reduce ? 3 : 2, BT_INTEGER))
2189 return false;
2190 if (!scalar_check (stat, co_reduce ? 3 : 2))
2191 return false;
2192 if (!variable_check (stat, co_reduce ? 3 : 2, false))
2193 return false;
2194 if (stat->ts.kind != 4)
2196 gfc_error ("The stat= argument at %L must be a kind=4 integer "
2197 "variable", &stat->where);
2198 return false;
2202 if (errmsg != NULL)
2204 if (!type_check (errmsg, co_reduce ? 4 : 3, BT_CHARACTER))
2205 return false;
2206 if (!scalar_check (errmsg, co_reduce ? 4 : 3))
2207 return false;
2208 if (!variable_check (errmsg, co_reduce ? 4 : 3, false))
2209 return false;
2210 if (errmsg->ts.kind != 1)
2212 gfc_error ("The errmsg= argument at %L must be a default-kind "
2213 "character variable", &errmsg->where);
2214 return false;
2218 if (flag_coarray == GFC_FCOARRAY_NONE)
2220 gfc_fatal_error ("Coarrays disabled at %L, use %<-fcoarray=%> to enable",
2221 &a->where);
2222 return false;
2225 return true;
2229 bool
2230 gfc_check_co_broadcast (gfc_expr *a, gfc_expr *source_image, gfc_expr *stat,
2231 gfc_expr *errmsg)
2233 if (a->ts.type == BT_CLASS || gfc_expr_attr (a).alloc_comp)
2235 gfc_error ("Support for the A argument at %L which is polymorphic A "
2236 "argument or has allocatable components is not yet "
2237 "implemented", &a->where);
2238 return false;
2240 return check_co_collective (a, source_image, stat, errmsg, false);
2244 bool
2245 gfc_check_co_reduce (gfc_expr *a, gfc_expr *op, gfc_expr *result_image,
2246 gfc_expr *stat, gfc_expr *errmsg)
2248 symbol_attribute attr;
2249 gfc_formal_arglist *formal;
2250 gfc_symbol *sym;
2252 if (a->ts.type == BT_CLASS)
2254 gfc_error ("The A argument at %L of CO_REDUCE shall not be polymorphic",
2255 &a->where);
2256 return false;
2259 if (gfc_expr_attr (a).alloc_comp)
2261 gfc_error ("Support for the A argument at %L with allocatable components"
2262 " is not yet implemented", &a->where);
2263 return false;
2266 if (!check_co_collective (a, result_image, stat, errmsg, true))
2267 return false;
2269 if (!gfc_resolve_expr (op))
2270 return false;
2272 attr = gfc_expr_attr (op);
2273 if (!attr.pure || !attr.function)
2275 gfc_error ("OPERATION argument at %L must be a PURE function",
2276 &op->where);
2277 return false;
2280 if (attr.intrinsic)
2282 /* None of the intrinsics fulfills the criteria of taking two arguments,
2283 returning the same type and kind as the arguments and being permitted
2284 as actual argument. */
2285 gfc_error ("Intrinsic function %s at %L is not permitted for CO_REDUCE",
2286 op->symtree->n.sym->name, &op->where);
2287 return false;
2290 if (gfc_is_proc_ptr_comp (op))
2292 gfc_component *comp = gfc_get_proc_ptr_comp (op);
2293 sym = comp->ts.interface;
2295 else
2296 sym = op->symtree->n.sym;
2298 formal = sym->formal;
2300 if (!formal || !formal->next || formal->next->next)
2302 gfc_error ("The function passed as OPERATION at %L shall have two "
2303 "arguments", &op->where);
2304 return false;
2307 if (sym->result->ts.type == BT_UNKNOWN)
2308 gfc_set_default_type (sym->result, 0, NULL);
2310 if (!gfc_compare_types (&a->ts, &sym->result->ts))
2312 gfc_error ("The A argument at %L has type %s but the function passed as "
2313 "OPERATION at %L returns %s",
2314 &a->where, gfc_typename (a), &op->where,
2315 gfc_typename (&sym->result->ts));
2316 return false;
2318 if (!gfc_compare_types (&a->ts, &formal->sym->ts)
2319 || !gfc_compare_types (&a->ts, &formal->next->sym->ts))
2321 gfc_error ("The function passed as OPERATION at %L has arguments of type "
2322 "%s and %s but shall have type %s", &op->where,
2323 gfc_typename (&formal->sym->ts),
2324 gfc_typename (&formal->next->sym->ts), gfc_typename (a));
2325 return false;
2327 if (op->rank || attr.allocatable || attr.pointer || formal->sym->as
2328 || formal->next->sym->as || formal->sym->attr.allocatable
2329 || formal->next->sym->attr.allocatable || formal->sym->attr.pointer
2330 || formal->next->sym->attr.pointer)
2332 gfc_error ("The function passed as OPERATION at %L shall have scalar "
2333 "nonallocatable nonpointer arguments and return a "
2334 "nonallocatable nonpointer scalar", &op->where);
2335 return false;
2338 if (formal->sym->attr.value != formal->next->sym->attr.value)
2340 gfc_error ("The function passed as OPERATION at %L shall have the VALUE "
2341 "attribute either for none or both arguments", &op->where);
2342 return false;
2345 if (formal->sym->attr.target != formal->next->sym->attr.target)
2347 gfc_error ("The function passed as OPERATION at %L shall have the TARGET "
2348 "attribute either for none or both arguments", &op->where);
2349 return false;
2352 if (formal->sym->attr.asynchronous != formal->next->sym->attr.asynchronous)
2354 gfc_error ("The function passed as OPERATION at %L shall have the "
2355 "ASYNCHRONOUS attribute either for none or both arguments",
2356 &op->where);
2357 return false;
2360 if (formal->sym->attr.optional || formal->next->sym->attr.optional)
2362 gfc_error ("The function passed as OPERATION at %L shall not have the "
2363 "OPTIONAL attribute for either of the arguments", &op->where);
2364 return false;
2367 if (a->ts.type == BT_CHARACTER)
2369 gfc_charlen *cl;
2370 unsigned long actual_size, formal_size1, formal_size2, result_size;
2372 cl = a->ts.u.cl;
2373 actual_size = cl && cl->length && cl->length->expr_type == EXPR_CONSTANT
2374 ? mpz_get_ui (cl->length->value.integer) : 0;
2376 cl = formal->sym->ts.u.cl;
2377 formal_size1 = cl && cl->length && cl->length->expr_type == EXPR_CONSTANT
2378 ? mpz_get_ui (cl->length->value.integer) : 0;
2380 cl = formal->next->sym->ts.u.cl;
2381 formal_size2 = cl && cl->length && cl->length->expr_type == EXPR_CONSTANT
2382 ? mpz_get_ui (cl->length->value.integer) : 0;
2384 cl = sym->ts.u.cl;
2385 result_size = cl && cl->length && cl->length->expr_type == EXPR_CONSTANT
2386 ? mpz_get_ui (cl->length->value.integer) : 0;
2388 if (actual_size
2389 && ((formal_size1 && actual_size != formal_size1)
2390 || (formal_size2 && actual_size != formal_size2)))
2392 gfc_error ("The character length of the A argument at %L and of the "
2393 "arguments of the OPERATION at %L shall be the same",
2394 &a->where, &op->where);
2395 return false;
2397 if (actual_size && result_size && actual_size != result_size)
2399 gfc_error ("The character length of the A argument at %L and of the "
2400 "function result of the OPERATION at %L shall be the same",
2401 &a->where, &op->where);
2402 return false;
2406 return true;
2410 bool
2411 gfc_check_co_minmax (gfc_expr *a, gfc_expr *result_image, gfc_expr *stat,
2412 gfc_expr *errmsg)
2414 if (a->ts.type != BT_INTEGER && a->ts.type != BT_REAL
2415 && a->ts.type != BT_CHARACTER)
2417 gfc_error ("%qs argument of %qs intrinsic at %L shall be of type "
2418 "integer, real or character",
2419 gfc_current_intrinsic_arg[0]->name, gfc_current_intrinsic,
2420 &a->where);
2421 return false;
2423 return check_co_collective (a, result_image, stat, errmsg, false);
2427 bool
2428 gfc_check_co_sum (gfc_expr *a, gfc_expr *result_image, gfc_expr *stat,
2429 gfc_expr *errmsg)
2431 if (!numeric_check (a, 0))
2432 return false;
2433 return check_co_collective (a, result_image, stat, errmsg, false);
2437 bool
2438 gfc_check_complex (gfc_expr *x, gfc_expr *y)
2440 if (!boz_args_check (x, y))
2441 return false;
2443 if (x->ts.type == BT_BOZ)
2445 if (gfc_invalid_boz (G_("BOZ constant at %L cannot appear in the COMPLEX"
2446 " intrinsic subprogram"), &x->where))
2448 reset_boz (x);
2449 return false;
2451 if (y->ts.type == BT_INTEGER && !gfc_boz2int (x, y->ts.kind))
2452 return false;
2453 if (y->ts.type == BT_REAL && !gfc_boz2real (x, y->ts.kind))
2454 return false;
2457 if (y->ts.type == BT_BOZ)
2459 if (gfc_invalid_boz (G_("BOZ constant at %L cannot appear in the COMPLEX"
2460 " intrinsic subprogram"), &y->where))
2462 reset_boz (y);
2463 return false;
2465 if (x->ts.type == BT_INTEGER && !gfc_boz2int (y, x->ts.kind))
2466 return false;
2467 if (x->ts.type == BT_REAL && !gfc_boz2real (y, x->ts.kind))
2468 return false;
2471 if (!int_or_real_check (x, 0))
2472 return false;
2473 if (!scalar_check (x, 0))
2474 return false;
2476 if (!int_or_real_check (y, 1))
2477 return false;
2478 if (!scalar_check (y, 1))
2479 return false;
2481 return true;
2485 bool
2486 gfc_check_count (gfc_expr *mask, gfc_expr *dim, gfc_expr *kind)
2488 if (!logical_array_check (mask, 0))
2489 return false;
2490 if (!dim_check (dim, 1, false))
2491 return false;
2492 if (!dim_rank_check (dim, mask, 0))
2493 return false;
2494 if (!kind_check (kind, 2, BT_INTEGER))
2495 return false;
2496 if (kind && !gfc_notify_std (GFC_STD_F2003, "%qs intrinsic "
2497 "with KIND argument at %L",
2498 gfc_current_intrinsic, &kind->where))
2499 return false;
2501 return true;
2505 bool
2506 gfc_check_cshift (gfc_expr *array, gfc_expr *shift, gfc_expr *dim)
2508 if (!array_check (array, 0))
2509 return false;
2511 if (!type_check (shift, 1, BT_INTEGER))
2512 return false;
2514 if (!dim_check (dim, 2, true))
2515 return false;
2517 if (!dim_rank_check (dim, array, false))
2518 return false;
2520 if (array->rank == 1 || shift->rank == 0)
2522 if (!scalar_check (shift, 1))
2523 return false;
2525 else if (shift->rank == array->rank - 1)
2527 int d;
2528 if (!dim)
2529 d = 1;
2530 else if (dim->expr_type == EXPR_CONSTANT)
2531 gfc_extract_int (dim, &d);
2532 else
2533 d = -1;
2535 if (d > 0)
2537 int i, j;
2538 for (i = 0, j = 0; i < array->rank; i++)
2539 if (i != d - 1)
2541 if (!identical_dimen_shape (array, i, shift, j))
2543 gfc_error ("%qs argument of %qs intrinsic at %L has "
2544 "invalid shape in dimension %d (%ld/%ld)",
2545 gfc_current_intrinsic_arg[1]->name,
2546 gfc_current_intrinsic, &shift->where, i + 1,
2547 mpz_get_si (array->shape[i]),
2548 mpz_get_si (shift->shape[j]));
2549 return false;
2552 j += 1;
2556 else
2558 gfc_error ("%qs argument of intrinsic %qs at %L of must have rank "
2559 "%d or be a scalar", gfc_current_intrinsic_arg[1]->name,
2560 gfc_current_intrinsic, &shift->where, array->rank - 1);
2561 return false;
2564 return true;
2568 bool
2569 gfc_check_ctime (gfc_expr *time)
2571 if (!scalar_check (time, 0))
2572 return false;
2574 if (!type_check (time, 0, BT_INTEGER))
2575 return false;
2577 return true;
2581 bool gfc_check_datan2 (gfc_expr *y, gfc_expr *x)
2583 if (!double_check (y, 0) || !double_check (x, 1))
2584 return false;
2586 return true;
2589 bool
2590 gfc_check_dcmplx (gfc_expr *x, gfc_expr *y)
2592 if (x->ts.type == BT_BOZ && !gfc_boz2real (x, gfc_default_double_kind))
2593 return false;
2595 if (!numeric_check (x, 0))
2596 return false;
2598 if (y != NULL)
2600 if (y->ts.type == BT_BOZ && !gfc_boz2real (y, gfc_default_double_kind))
2601 return false;
2603 if (!numeric_check (y, 1))
2604 return false;
2606 if (x->ts.type == BT_COMPLEX)
2608 gfc_error ("%qs argument of %qs intrinsic at %L must not be "
2609 "present if %<x%> is COMPLEX",
2610 gfc_current_intrinsic_arg[1]->name, gfc_current_intrinsic,
2611 &y->where);
2612 return false;
2615 if (y->ts.type == BT_COMPLEX)
2617 gfc_error ("%qs argument of %qs intrinsic at %L must have a type "
2618 "of either REAL or INTEGER",
2619 gfc_current_intrinsic_arg[1]->name, gfc_current_intrinsic,
2620 &y->where);
2621 return false;
2625 return true;
2629 bool
2630 gfc_check_dble (gfc_expr *x)
2632 if (x->ts.type == BT_BOZ && !gfc_boz2real (x, gfc_default_double_kind))
2633 return false;
2635 if (!numeric_check (x, 0))
2636 return false;
2638 return true;
2642 bool
2643 gfc_check_digits (gfc_expr *x)
2645 if (!int_or_real_check (x, 0))
2646 return false;
2648 return true;
2652 bool
2653 gfc_check_dot_product (gfc_expr *vector_a, gfc_expr *vector_b)
2655 switch (vector_a->ts.type)
2657 case BT_LOGICAL:
2658 if (!type_check (vector_b, 1, BT_LOGICAL))
2659 return false;
2660 break;
2662 case BT_INTEGER:
2663 case BT_REAL:
2664 case BT_COMPLEX:
2665 if (!numeric_check (vector_b, 1))
2666 return false;
2667 break;
2669 default:
2670 gfc_error ("%qs argument of %qs intrinsic at %L must be numeric "
2671 "or LOGICAL", gfc_current_intrinsic_arg[0]->name,
2672 gfc_current_intrinsic, &vector_a->where);
2673 return false;
2676 if (!rank_check (vector_a, 0, 1))
2677 return false;
2679 if (!rank_check (vector_b, 1, 1))
2680 return false;
2682 if (! identical_dimen_shape (vector_a, 0, vector_b, 0))
2684 gfc_error ("Different shape for arguments %qs and %qs at %L for "
2685 "intrinsic %<dot_product%>",
2686 gfc_current_intrinsic_arg[0]->name,
2687 gfc_current_intrinsic_arg[1]->name, &vector_a->where);
2688 return false;
2691 return true;
2695 bool
2696 gfc_check_dprod (gfc_expr *x, gfc_expr *y)
2698 if (!type_check (x, 0, BT_REAL)
2699 || !type_check (y, 1, BT_REAL))
2700 return false;
2702 if (x->ts.kind != gfc_default_real_kind)
2704 gfc_error ("%qs argument of %qs intrinsic at %L must be default "
2705 "real", gfc_current_intrinsic_arg[0]->name,
2706 gfc_current_intrinsic, &x->where);
2707 return false;
2710 if (y->ts.kind != gfc_default_real_kind)
2712 gfc_error ("%qs argument of %qs intrinsic at %L must be default "
2713 "real", gfc_current_intrinsic_arg[1]->name,
2714 gfc_current_intrinsic, &y->where);
2715 return false;
2718 return true;
2721 bool
2722 gfc_check_dshift (gfc_expr *i, gfc_expr *j, gfc_expr *shift)
2724 /* i and j cannot both be BOZ literal constants. */
2725 if (!boz_args_check (i, j))
2726 return false;
2728 /* If i is BOZ and j is integer, convert i to type of j. If j is not
2729 an integer, clear the BOZ; otherwise, check that i is an integer. */
2730 if (i->ts.type == BT_BOZ)
2732 if (j->ts.type != BT_INTEGER)
2733 reset_boz (i);
2734 else if (!gfc_boz2int (i, j->ts.kind))
2735 return false;
2737 else if (!type_check (i, 0, BT_INTEGER))
2739 if (j->ts.type == BT_BOZ)
2740 reset_boz (j);
2741 return false;
2744 /* If j is BOZ and i is integer, convert j to type of i. If i is not
2745 an integer, clear the BOZ; otherwise, check that i is an integer. */
2746 if (j->ts.type == BT_BOZ)
2748 if (i->ts.type != BT_INTEGER)
2749 reset_boz (j);
2750 else if (!gfc_boz2int (j, i->ts.kind))
2751 return false;
2753 else if (!type_check (j, 1, BT_INTEGER))
2754 return false;
2756 if (!same_type_check (i, 0, j, 1))
2757 return false;
2759 if (!type_check (shift, 2, BT_INTEGER))
2760 return false;
2762 if (!nonnegative_check ("SHIFT", shift))
2763 return false;
2765 if (!less_than_bitsize1 ("I", i, "SHIFT", shift, true))
2766 return false;
2768 return true;
2772 bool
2773 gfc_check_eoshift (gfc_expr *array, gfc_expr *shift, gfc_expr *boundary,
2774 gfc_expr *dim)
2776 int d;
2778 if (!array_check (array, 0))
2779 return false;
2781 if (!type_check (shift, 1, BT_INTEGER))
2782 return false;
2784 if (!dim_check (dim, 3, true))
2785 return false;
2787 if (!dim_rank_check (dim, array, false))
2788 return false;
2790 if (!dim)
2791 d = 1;
2792 else if (dim->expr_type == EXPR_CONSTANT)
2793 gfc_extract_int (dim, &d);
2794 else
2795 d = -1;
2797 if (array->rank == 1 || shift->rank == 0)
2799 if (!scalar_check (shift, 1))
2800 return false;
2802 else if (shift->rank == array->rank - 1)
2804 if (d > 0)
2806 int i, j;
2807 for (i = 0, j = 0; i < array->rank; i++)
2808 if (i != d - 1)
2810 if (!identical_dimen_shape (array, i, shift, j))
2812 gfc_error ("%qs argument of %qs intrinsic at %L has "
2813 "invalid shape in dimension %d (%ld/%ld)",
2814 gfc_current_intrinsic_arg[1]->name,
2815 gfc_current_intrinsic, &shift->where, i + 1,
2816 mpz_get_si (array->shape[i]),
2817 mpz_get_si (shift->shape[j]));
2818 return false;
2821 j += 1;
2825 else
2827 gfc_error ("%qs argument of intrinsic %qs at %L of must have rank "
2828 "%d or be a scalar", gfc_current_intrinsic_arg[1]->name,
2829 gfc_current_intrinsic, &shift->where, array->rank - 1);
2830 return false;
2833 if (boundary != NULL)
2835 if (!same_type_check (array, 0, boundary, 2))
2836 return false;
2838 /* Reject unequal string lengths and emit a better error message than
2839 gfc_check_same_strlen would. */
2840 if (array->ts.type == BT_CHARACTER)
2842 ssize_t len_a, len_b;
2844 len_a = gfc_var_strlen (array);
2845 len_b = gfc_var_strlen (boundary);
2846 if (len_a != -1 && len_b != -1 && len_a != len_b)
2848 gfc_error ("%qs must be of same type and kind as %qs at %L in %qs",
2849 gfc_current_intrinsic_arg[2]->name,
2850 gfc_current_intrinsic_arg[0]->name,
2851 &boundary->where, gfc_current_intrinsic);
2852 return false;
2856 if (array->rank == 1 || boundary->rank == 0)
2858 if (!scalar_check (boundary, 2))
2859 return false;
2861 else if (boundary->rank == array->rank - 1)
2863 if (d > 0)
2865 int i,j;
2866 for (i = 0, j = 0; i < array->rank; i++)
2868 if (i != d - 1)
2870 if (!identical_dimen_shape (array, i, boundary, j))
2872 gfc_error ("%qs argument of %qs intrinsic at %L has "
2873 "invalid shape in dimension %d (%ld/%ld)",
2874 gfc_current_intrinsic_arg[2]->name,
2875 gfc_current_intrinsic, &shift->where, i+1,
2876 mpz_get_si (array->shape[i]),
2877 mpz_get_si (boundary->shape[j]));
2878 return false;
2880 j += 1;
2885 else
2887 gfc_error ("%qs argument of intrinsic %qs at %L of must have "
2888 "rank %d or be a scalar",
2889 gfc_current_intrinsic_arg[1]->name, gfc_current_intrinsic,
2890 &shift->where, array->rank - 1);
2891 return false;
2894 else
2896 switch (array->ts.type)
2898 case BT_INTEGER:
2899 case BT_LOGICAL:
2900 case BT_REAL:
2901 case BT_COMPLEX:
2902 case BT_CHARACTER:
2903 break;
2905 default:
2906 gfc_error ("Missing %qs argument to %qs intrinsic at %L for %qs "
2907 "of type %qs", gfc_current_intrinsic_arg[2]->name,
2908 gfc_current_intrinsic, &array->where,
2909 gfc_current_intrinsic_arg[0]->name,
2910 gfc_typename (array));
2911 return false;
2915 return true;
2919 bool
2920 gfc_check_float (gfc_expr *a)
2922 if (a->ts.type == BT_BOZ)
2924 if (gfc_invalid_boz (G_("BOZ literal constant at %L cannot appear in the"
2925 " FLOAT intrinsic subprogram"), &a->where))
2927 reset_boz (a);
2928 return false;
2930 if (!gfc_boz2int (a, gfc_default_integer_kind))
2931 return false;
2934 if (!type_check (a, 0, BT_INTEGER))
2935 return false;
2937 if ((a->ts.kind != gfc_default_integer_kind)
2938 && !gfc_notify_std (GFC_STD_GNU, "non-default INTEGER "
2939 "kind argument to %s intrinsic at %L",
2940 gfc_current_intrinsic, &a->where))
2941 return false;
2943 return true;
2946 /* A single complex argument. */
2948 bool
2949 gfc_check_fn_c (gfc_expr *a)
2951 if (!type_check (a, 0, BT_COMPLEX))
2952 return false;
2954 return true;
2958 /* A single real argument. */
2960 bool
2961 gfc_check_fn_r (gfc_expr *a)
2963 if (!type_check (a, 0, BT_REAL))
2964 return false;
2966 return true;
2969 /* A single double argument. */
2971 bool
2972 gfc_check_fn_d (gfc_expr *a)
2974 if (!double_check (a, 0))
2975 return false;
2977 return true;
2980 /* A single real or complex argument. */
2982 bool
2983 gfc_check_fn_rc (gfc_expr *a)
2985 if (!real_or_complex_check (a, 0))
2986 return false;
2988 return true;
2992 bool
2993 gfc_check_fn_rc2008 (gfc_expr *a)
2995 if (!real_or_complex_check (a, 0))
2996 return false;
2998 if (a->ts.type == BT_COMPLEX
2999 && !gfc_notify_std (GFC_STD_F2008, "COMPLEX argument %qs "
3000 "of %qs intrinsic at %L",
3001 gfc_current_intrinsic_arg[0]->name,
3002 gfc_current_intrinsic, &a->where))
3003 return false;
3005 return true;
3009 bool
3010 gfc_check_fnum (gfc_expr *unit)
3012 if (!type_check (unit, 0, BT_INTEGER))
3013 return false;
3015 if (!scalar_check (unit, 0))
3016 return false;
3018 return true;
3022 bool
3023 gfc_check_huge (gfc_expr *x)
3025 if (!int_or_real_check (x, 0))
3026 return false;
3028 return true;
3032 bool
3033 gfc_check_hypot (gfc_expr *x, gfc_expr *y)
3035 if (!type_check (x, 0, BT_REAL))
3036 return false;
3037 if (!same_type_check (x, 0, y, 1))
3038 return false;
3040 return true;
3044 /* Check that the single argument is an integer. */
3046 bool
3047 gfc_check_i (gfc_expr *i)
3049 if (!type_check (i, 0, BT_INTEGER))
3050 return false;
3052 return true;
3056 bool
3057 gfc_check_iand_ieor_ior (gfc_expr *i, gfc_expr *j)
3059 /* i and j cannot both be BOZ literal constants. */
3060 if (!boz_args_check (i, j))
3061 return false;
3063 /* If i is BOZ and j is integer, convert i to type of j. */
3064 if (i->ts.type == BT_BOZ && j->ts.type == BT_INTEGER
3065 && !gfc_boz2int (i, j->ts.kind))
3066 return false;
3068 /* If j is BOZ and i is integer, convert j to type of i. */
3069 if (j->ts.type == BT_BOZ && i->ts.type == BT_INTEGER
3070 && !gfc_boz2int (j, i->ts.kind))
3071 return false;
3073 if (!type_check (i, 0, BT_INTEGER))
3074 return false;
3076 if (!type_check (j, 1, BT_INTEGER))
3077 return false;
3079 if (i->ts.kind != j->ts.kind)
3081 gfc_error ("Arguments of %qs have different kind type parameters "
3082 "at %L", gfc_current_intrinsic, &i->where);
3083 return false;
3086 return true;
3090 bool
3091 gfc_check_ibits (gfc_expr *i, gfc_expr *pos, gfc_expr *len)
3093 if (!type_check (i, 0, BT_INTEGER))
3094 return false;
3096 if (!type_check (pos, 1, BT_INTEGER))
3097 return false;
3099 if (!type_check (len, 2, BT_INTEGER))
3100 return false;
3102 if (!nonnegative_check ("pos", pos))
3103 return false;
3105 if (!nonnegative_check ("len", len))
3106 return false;
3108 if (!less_than_bitsize2 ("i", i, "pos", pos, "len", len))
3109 return false;
3111 return true;
3115 bool
3116 gfc_check_ichar_iachar (gfc_expr *c, gfc_expr *kind)
3118 int i;
3120 if (!type_check (c, 0, BT_CHARACTER))
3121 return false;
3123 if (!kind_check (kind, 1, BT_INTEGER))
3124 return false;
3126 if (kind && !gfc_notify_std (GFC_STD_F2003, "%qs intrinsic "
3127 "with KIND argument at %L",
3128 gfc_current_intrinsic, &kind->where))
3129 return false;
3131 if (c->expr_type == EXPR_VARIABLE || c->expr_type == EXPR_SUBSTRING)
3133 gfc_expr *start;
3134 gfc_expr *end;
3135 gfc_ref *ref;
3137 /* Substring references don't have the charlength set. */
3138 ref = c->ref;
3139 while (ref && ref->type != REF_SUBSTRING)
3140 ref = ref->next;
3142 gcc_assert (ref == NULL || ref->type == REF_SUBSTRING);
3144 if (!ref)
3146 /* Check that the argument is length one. Non-constant lengths
3147 can't be checked here, so assume they are ok. */
3148 if (c->ts.u.cl && c->ts.u.cl->length)
3150 /* If we already have a length for this expression then use it. */
3151 if (c->ts.u.cl->length->expr_type != EXPR_CONSTANT)
3152 return true;
3153 i = mpz_get_si (c->ts.u.cl->length->value.integer);
3155 else
3156 return true;
3158 else
3160 start = ref->u.ss.start;
3161 end = ref->u.ss.end;
3163 gcc_assert (start);
3164 if (end == NULL || end->expr_type != EXPR_CONSTANT
3165 || start->expr_type != EXPR_CONSTANT)
3166 return true;
3168 i = mpz_get_si (end->value.integer) + 1
3169 - mpz_get_si (start->value.integer);
3172 else
3173 return true;
3175 if (i != 1)
3177 gfc_error ("Argument of %s at %L must be of length one",
3178 gfc_current_intrinsic, &c->where);
3179 return false;
3182 return true;
3186 bool
3187 gfc_check_idnint (gfc_expr *a)
3189 if (!double_check (a, 0))
3190 return false;
3192 return true;
3196 bool
3197 gfc_check_index (gfc_expr *string, gfc_expr *substring, gfc_expr *back,
3198 gfc_expr *kind)
3200 if (!type_check (string, 0, BT_CHARACTER)
3201 || !type_check (substring, 1, BT_CHARACTER))
3202 return false;
3204 if (back != NULL && !type_check (back, 2, BT_LOGICAL))
3205 return false;
3207 if (!kind_check (kind, 3, BT_INTEGER))
3208 return false;
3209 if (kind && !gfc_notify_std (GFC_STD_F2003, "%qs intrinsic "
3210 "with KIND argument at %L",
3211 gfc_current_intrinsic, &kind->where))
3212 return false;
3214 if (string->ts.kind != substring->ts.kind)
3216 gfc_error ("%qs argument of %qs intrinsic at %L must be the same "
3217 "kind as %qs", gfc_current_intrinsic_arg[1]->name,
3218 gfc_current_intrinsic, &substring->where,
3219 gfc_current_intrinsic_arg[0]->name);
3220 return false;
3223 return true;
3227 bool
3228 gfc_check_int (gfc_expr *x, gfc_expr *kind)
3230 /* BOZ is dealt within simplify_int*. */
3231 if (x->ts.type == BT_BOZ)
3232 return true;
3234 if (!numeric_check (x, 0))
3235 return false;
3237 if (!kind_check (kind, 1, BT_INTEGER))
3238 return false;
3240 return true;
3244 bool
3245 gfc_check_intconv (gfc_expr *x)
3247 if (strcmp (gfc_current_intrinsic, "short") == 0
3248 || strcmp (gfc_current_intrinsic, "long") == 0)
3250 gfc_error ("%qs intrinsic subprogram at %L has been removed. "
3251 "Use INT intrinsic subprogram.", gfc_current_intrinsic,
3252 &x->where);
3253 return false;
3256 /* BOZ is dealt within simplify_int*. */
3257 if (x->ts.type == BT_BOZ)
3258 return true;
3260 if (!numeric_check (x, 0))
3261 return false;
3263 return true;
3266 bool
3267 gfc_check_ishft (gfc_expr *i, gfc_expr *shift)
3269 if (!type_check (i, 0, BT_INTEGER)
3270 || !type_check (shift, 1, BT_INTEGER))
3271 return false;
3273 if (!less_than_bitsize1 ("I", i, NULL, shift, true))
3274 return false;
3276 return true;
3280 bool
3281 gfc_check_ishftc (gfc_expr *i, gfc_expr *shift, gfc_expr *size)
3283 if (!type_check (i, 0, BT_INTEGER)
3284 || !type_check (shift, 1, BT_INTEGER))
3285 return false;
3287 if (size != NULL)
3289 int i2, i3;
3291 if (!type_check (size, 2, BT_INTEGER))
3292 return false;
3294 if (!less_than_bitsize1 ("I", i, "SIZE", size, true))
3295 return false;
3297 if (size->expr_type == EXPR_CONSTANT)
3299 gfc_extract_int (size, &i3);
3300 if (i3 <= 0)
3302 gfc_error ("SIZE at %L must be positive", &size->where);
3303 return false;
3306 if (shift->expr_type == EXPR_CONSTANT)
3308 gfc_extract_int (shift, &i2);
3309 if (i2 < 0)
3310 i2 = -i2;
3312 if (i2 > i3)
3314 gfc_error ("The absolute value of SHIFT at %L must be less "
3315 "than or equal to SIZE at %L", &shift->where,
3316 &size->where);
3317 return false;
3322 else if (!less_than_bitsize1 ("I", i, NULL, shift, true))
3323 return false;
3325 return true;
3329 bool
3330 gfc_check_kill (gfc_expr *pid, gfc_expr *sig)
3332 if (!type_check (pid, 0, BT_INTEGER))
3333 return false;
3335 if (!scalar_check (pid, 0))
3336 return false;
3338 if (!type_check (sig, 1, BT_INTEGER))
3339 return false;
3341 if (!scalar_check (sig, 1))
3342 return false;
3344 return true;
3348 bool
3349 gfc_check_kill_sub (gfc_expr *pid, gfc_expr *sig, gfc_expr *status)
3351 if (!type_check (pid, 0, BT_INTEGER))
3352 return false;
3354 if (!scalar_check (pid, 0))
3355 return false;
3357 if (!type_check (sig, 1, BT_INTEGER))
3358 return false;
3360 if (!scalar_check (sig, 1))
3361 return false;
3363 if (status)
3365 if (!type_check (status, 2, BT_INTEGER))
3366 return false;
3368 if (!scalar_check (status, 2))
3369 return false;
3371 if (status->expr_type != EXPR_VARIABLE)
3373 gfc_error ("STATUS at %L shall be an INTENT(OUT) variable",
3374 &status->where);
3375 return false;
3378 if (status->expr_type == EXPR_VARIABLE
3379 && status->symtree && status->symtree->n.sym
3380 && status->symtree->n.sym->attr.intent == INTENT_IN)
3382 gfc_error ("%qs at %L shall be an INTENT(OUT) variable",
3383 status->symtree->name, &status->where);
3384 return false;
3388 return true;
3392 bool
3393 gfc_check_kind (gfc_expr *x)
3395 if (gfc_invalid_null_arg (x))
3396 return false;
3398 if (gfc_bt_struct (x->ts.type) || x->ts.type == BT_CLASS)
3400 gfc_error ("%qs argument of %qs intrinsic at %L must be of "
3401 "intrinsic type", gfc_current_intrinsic_arg[0]->name,
3402 gfc_current_intrinsic, &x->where);
3403 return false;
3405 if (x->ts.type == BT_PROCEDURE)
3407 gfc_error ("%qs argument of %qs intrinsic at %L must be a data entity",
3408 gfc_current_intrinsic_arg[0]->name, gfc_current_intrinsic,
3409 &x->where);
3410 return false;
3413 return true;
3417 bool
3418 gfc_check_lbound (gfc_expr *array, gfc_expr *dim, gfc_expr *kind)
3420 if (!array_check (array, 0))
3421 return false;
3423 if (!dim_check (dim, 1, false))
3424 return false;
3426 if (!dim_rank_check (dim, array, 1))
3427 return false;
3429 if (!kind_check (kind, 2, BT_INTEGER))
3430 return false;
3431 if (kind && !gfc_notify_std (GFC_STD_F2003, "%qs intrinsic "
3432 "with KIND argument at %L",
3433 gfc_current_intrinsic, &kind->where))
3434 return false;
3436 return true;
3440 bool
3441 gfc_check_lcobound (gfc_expr *coarray, gfc_expr *dim, gfc_expr *kind)
3443 if (flag_coarray == GFC_FCOARRAY_NONE)
3445 gfc_fatal_error ("Coarrays disabled at %C, use %<-fcoarray=%> to enable");
3446 return false;
3449 if (!coarray_check (coarray, 0))
3450 return false;
3452 if (dim != NULL)
3454 if (!dim_check (dim, 1, false))
3455 return false;
3457 if (!dim_corank_check (dim, coarray))
3458 return false;
3461 if (!kind_check (kind, 2, BT_INTEGER))
3462 return false;
3464 return true;
3468 bool
3469 gfc_check_len_lentrim (gfc_expr *s, gfc_expr *kind)
3471 if (!type_check (s, 0, BT_CHARACTER))
3472 return false;
3474 if (gfc_invalid_null_arg (s))
3475 return false;
3477 if (!kind_check (kind, 1, BT_INTEGER))
3478 return false;
3479 if (kind && !gfc_notify_std (GFC_STD_F2003, "%qs intrinsic "
3480 "with KIND argument at %L",
3481 gfc_current_intrinsic, &kind->where))
3482 return false;
3484 return true;
3488 bool
3489 gfc_check_lge_lgt_lle_llt (gfc_expr *a, gfc_expr *b)
3491 if (!type_check (a, 0, BT_CHARACTER))
3492 return false;
3493 if (!kind_value_check (a, 0, gfc_default_character_kind))
3494 return false;
3496 if (!type_check (b, 1, BT_CHARACTER))
3497 return false;
3498 if (!kind_value_check (b, 1, gfc_default_character_kind))
3499 return false;
3501 return true;
3505 bool
3506 gfc_check_link (gfc_expr *path1, gfc_expr *path2)
3508 if (!type_check (path1, 0, BT_CHARACTER))
3509 return false;
3510 if (!kind_value_check (path1, 0, gfc_default_character_kind))
3511 return false;
3513 if (!type_check (path2, 1, BT_CHARACTER))
3514 return false;
3515 if (!kind_value_check (path2, 1, gfc_default_character_kind))
3516 return false;
3518 return true;
3522 bool
3523 gfc_check_link_sub (gfc_expr *path1, gfc_expr *path2, gfc_expr *status)
3525 if (!type_check (path1, 0, BT_CHARACTER))
3526 return false;
3527 if (!kind_value_check (path1, 0, gfc_default_character_kind))
3528 return false;
3530 if (!type_check (path2, 1, BT_CHARACTER))
3531 return false;
3532 if (!kind_value_check (path2, 0, gfc_default_character_kind))
3533 return false;
3535 if (status == NULL)
3536 return true;
3538 if (!type_check (status, 2, BT_INTEGER))
3539 return false;
3541 if (!scalar_check (status, 2))
3542 return false;
3544 return true;
3548 bool
3549 gfc_check_loc (gfc_expr *expr)
3551 return variable_check (expr, 0, true);
3555 bool
3556 gfc_check_symlnk (gfc_expr *path1, gfc_expr *path2)
3558 if (!type_check (path1, 0, BT_CHARACTER))
3559 return false;
3560 if (!kind_value_check (path1, 0, gfc_default_character_kind))
3561 return false;
3563 if (!type_check (path2, 1, BT_CHARACTER))
3564 return false;
3565 if (!kind_value_check (path2, 1, gfc_default_character_kind))
3566 return false;
3568 return true;
3572 bool
3573 gfc_check_symlnk_sub (gfc_expr *path1, gfc_expr *path2, gfc_expr *status)
3575 if (!type_check (path1, 0, BT_CHARACTER))
3576 return false;
3577 if (!kind_value_check (path1, 0, gfc_default_character_kind))
3578 return false;
3580 if (!type_check (path2, 1, BT_CHARACTER))
3581 return false;
3582 if (!kind_value_check (path2, 1, gfc_default_character_kind))
3583 return false;
3585 if (status == NULL)
3586 return true;
3588 if (!type_check (status, 2, BT_INTEGER))
3589 return false;
3591 if (!scalar_check (status, 2))
3592 return false;
3594 return true;
3598 bool
3599 gfc_check_logical (gfc_expr *a, gfc_expr *kind)
3601 if (!type_check (a, 0, BT_LOGICAL))
3602 return false;
3603 if (!kind_check (kind, 1, BT_LOGICAL))
3604 return false;
3606 return true;
3610 /* Min/max family. */
3612 static bool
3613 min_max_args (gfc_actual_arglist *args)
3615 gfc_actual_arglist *arg;
3616 int i, j, nargs, *nlabels, nlabelless;
3617 bool a1 = false, a2 = false;
3619 if (args == NULL || args->next == NULL)
3621 gfc_error ("Intrinsic %qs at %L must have at least two arguments",
3622 gfc_current_intrinsic, gfc_current_intrinsic_where);
3623 return false;
3626 if (!args->name)
3627 a1 = true;
3629 if (!args->next->name)
3630 a2 = true;
3632 nargs = 0;
3633 for (arg = args; arg; arg = arg->next)
3634 if (arg->name)
3635 nargs++;
3637 if (nargs == 0)
3638 return true;
3640 /* Note: Having a keywordless argument after an "arg=" is checked before. */
3641 nlabelless = 0;
3642 nlabels = XALLOCAVEC (int, nargs);
3643 for (arg = args, i = 0; arg; arg = arg->next, i++)
3644 if (arg->name)
3646 int n;
3647 char *endp;
3649 if (arg->name[0] != 'a' || arg->name[1] < '1' || arg->name[1] > '9')
3650 goto unknown;
3651 n = strtol (&arg->name[1], &endp, 10);
3652 if (endp[0] != '\0')
3653 goto unknown;
3654 if (n <= 0)
3655 goto unknown;
3656 if (n <= nlabelless)
3657 goto duplicate;
3658 nlabels[i] = n;
3659 if (n == 1)
3660 a1 = true;
3661 if (n == 2)
3662 a2 = true;
3664 else
3665 nlabelless++;
3667 if (!a1 || !a2)
3669 gfc_error ("Missing %qs argument to the %s intrinsic at %L",
3670 !a1 ? "a1" : "a2", gfc_current_intrinsic,
3671 gfc_current_intrinsic_where);
3672 return false;
3675 /* Check for duplicates. */
3676 for (i = 0; i < nargs; i++)
3677 for (j = i + 1; j < nargs; j++)
3678 if (nlabels[i] == nlabels[j])
3679 goto duplicate;
3681 return true;
3683 duplicate:
3684 gfc_error ("Duplicate argument %qs at %L to intrinsic %s", arg->name,
3685 &arg->expr->where, gfc_current_intrinsic);
3686 return false;
3688 unknown:
3689 gfc_error ("Unknown argument %qs at %L to intrinsic %s", arg->name,
3690 &arg->expr->where, gfc_current_intrinsic);
3691 return false;
3695 static bool
3696 check_rest (bt type, int kind, gfc_actual_arglist *arglist)
3698 gfc_actual_arglist *arg, *tmp;
3699 gfc_expr *x;
3700 int m, n;
3702 if (!min_max_args (arglist))
3703 return false;
3705 for (arg = arglist, n=1; arg; arg = arg->next, n++)
3707 x = arg->expr;
3708 if (x->ts.type != type || x->ts.kind != kind)
3710 if (x->ts.type == type)
3712 if (x->ts.type == BT_CHARACTER)
3714 gfc_error ("Different character kinds at %L", &x->where);
3715 return false;
3717 if (!gfc_notify_std (GFC_STD_GNU, "Different type "
3718 "kinds at %L", &x->where))
3719 return false;
3721 else
3723 gfc_error ("%<a%d%> argument of %qs intrinsic at %L must be "
3724 "%s(%d)", n, gfc_current_intrinsic, &x->where,
3725 gfc_basic_typename (type), kind);
3726 return false;
3730 for (tmp = arglist, m=1; tmp != arg; tmp = tmp->next, m++)
3731 if (!gfc_check_conformance (tmp->expr, x,
3732 _("arguments 'a%d' and 'a%d' for "
3733 "intrinsic '%s'"), m, n,
3734 gfc_current_intrinsic))
3735 return false;
3738 return true;
3742 bool
3743 gfc_check_min_max (gfc_actual_arglist *arg)
3745 gfc_expr *x;
3747 if (!min_max_args (arg))
3748 return false;
3750 x = arg->expr;
3752 if (x->ts.type == BT_CHARACTER)
3754 if (!gfc_notify_std (GFC_STD_F2003, "%qs intrinsic "
3755 "with CHARACTER argument at %L",
3756 gfc_current_intrinsic, &x->where))
3757 return false;
3759 else if (x->ts.type != BT_INTEGER && x->ts.type != BT_REAL)
3761 gfc_error ("%<a1%> argument of %qs intrinsic at %L must be INTEGER, "
3762 "REAL or CHARACTER", gfc_current_intrinsic, &x->where);
3763 return false;
3766 return check_rest (x->ts.type, x->ts.kind, arg);
3770 bool
3771 gfc_check_min_max_integer (gfc_actual_arglist *arg)
3773 return check_rest (BT_INTEGER, gfc_default_integer_kind, arg);
3777 bool
3778 gfc_check_min_max_real (gfc_actual_arglist *arg)
3780 return check_rest (BT_REAL, gfc_default_real_kind, arg);
3784 bool
3785 gfc_check_min_max_double (gfc_actual_arglist *arg)
3787 return check_rest (BT_REAL, gfc_default_double_kind, arg);
3791 /* End of min/max family. */
3793 bool
3794 gfc_check_malloc (gfc_expr *size)
3796 if (!type_check (size, 0, BT_INTEGER))
3797 return false;
3799 if (!scalar_check (size, 0))
3800 return false;
3802 return true;
3806 bool
3807 gfc_check_matmul (gfc_expr *matrix_a, gfc_expr *matrix_b)
3809 if ((matrix_a->ts.type != BT_LOGICAL) && !gfc_numeric_ts (&matrix_a->ts))
3811 gfc_error ("%qs argument of %qs intrinsic at %L must be numeric "
3812 "or LOGICAL", gfc_current_intrinsic_arg[0]->name,
3813 gfc_current_intrinsic, &matrix_a->where);
3814 return false;
3817 if ((matrix_b->ts.type != BT_LOGICAL) && !gfc_numeric_ts (&matrix_b->ts))
3819 gfc_error ("%qs argument of %qs intrinsic at %L must be numeric "
3820 "or LOGICAL", gfc_current_intrinsic_arg[1]->name,
3821 gfc_current_intrinsic, &matrix_b->where);
3822 return false;
3825 if ((matrix_a->ts.type == BT_LOGICAL && gfc_numeric_ts (&matrix_b->ts))
3826 || (gfc_numeric_ts (&matrix_a->ts) && matrix_b->ts.type == BT_LOGICAL))
3828 gfc_error ("Argument types of %qs intrinsic at %L must match (%s/%s)",
3829 gfc_current_intrinsic, &matrix_a->where,
3830 gfc_typename(&matrix_a->ts), gfc_typename(&matrix_b->ts));
3831 return false;
3834 switch (matrix_a->rank)
3836 case 1:
3837 if (!rank_check (matrix_b, 1, 2))
3838 return false;
3839 /* Check for case matrix_a has shape(m), matrix_b has shape (m, k). */
3840 if (!identical_dimen_shape (matrix_a, 0, matrix_b, 0))
3842 gfc_error ("Different shape on dimension 1 for arguments %qs "
3843 "and %qs at %L for intrinsic matmul",
3844 gfc_current_intrinsic_arg[0]->name,
3845 gfc_current_intrinsic_arg[1]->name, &matrix_a->where);
3846 return false;
3848 break;
3850 case 2:
3851 if (matrix_b->rank != 2)
3853 if (!rank_check (matrix_b, 1, 1))
3854 return false;
3856 /* matrix_b has rank 1 or 2 here. Common check for the cases
3857 - matrix_a has shape (n,m) and matrix_b has shape (m, k)
3858 - matrix_a has shape (n,m) and matrix_b has shape (m). */
3859 if (!identical_dimen_shape (matrix_a, 1, matrix_b, 0))
3861 gfc_error ("Different shape on dimension 2 for argument %qs and "
3862 "dimension 1 for argument %qs at %L for intrinsic "
3863 "matmul", gfc_current_intrinsic_arg[0]->name,
3864 gfc_current_intrinsic_arg[1]->name, &matrix_a->where);
3865 return false;
3867 break;
3869 default:
3870 gfc_error ("%qs argument of %qs intrinsic at %L must be of rank "
3871 "1 or 2", gfc_current_intrinsic_arg[0]->name,
3872 gfc_current_intrinsic, &matrix_a->where);
3873 return false;
3876 return true;
3880 /* Whoever came up with this interface was probably on something.
3881 The possibilities for the occupation of the second and third
3882 parameters are:
3884 Arg #2 Arg #3
3885 NULL NULL
3886 DIM NULL
3887 MASK NULL
3888 NULL MASK minloc(array, mask=m)
3889 DIM MASK
3891 I.e. in the case of minloc(array,mask), mask will be in the second
3892 position of the argument list and we'll have to fix that up. Also,
3893 add the BACK argument if that isn't present. */
3895 bool
3896 gfc_check_minloc_maxloc (gfc_actual_arglist *ap)
3898 gfc_expr *a, *m, *d, *k, *b;
3900 a = ap->expr;
3901 if (!int_or_real_or_char_check_f2003 (a, 0) || !array_check (a, 0))
3902 return false;
3904 d = ap->next->expr;
3905 m = ap->next->next->expr;
3906 k = ap->next->next->next->expr;
3907 b = ap->next->next->next->next->expr;
3909 if (b)
3911 if (!type_check (b, 4, BT_LOGICAL) || !scalar_check (b,4))
3912 return false;
3914 else
3916 b = gfc_get_logical_expr (gfc_logical_4_kind, NULL, 0);
3917 ap->next->next->next->next->expr = b;
3918 ap->next->next->next->next->name = gfc_get_string ("back");
3921 if (m == NULL && d != NULL && d->ts.type == BT_LOGICAL
3922 && ap->next->name == NULL)
3924 m = d;
3925 d = NULL;
3926 ap->next->expr = NULL;
3927 ap->next->next->expr = m;
3930 if (!dim_check (d, 1, false))
3931 return false;
3933 if (!dim_rank_check (d, a, 0))
3934 return false;
3936 if (m != NULL && !type_check (m, 2, BT_LOGICAL))
3937 return false;
3939 if (m != NULL
3940 && !gfc_check_conformance (a, m,
3941 _("arguments '%s' and '%s' for intrinsic %s"),
3942 gfc_current_intrinsic_arg[0]->name,
3943 gfc_current_intrinsic_arg[2]->name,
3944 gfc_current_intrinsic))
3945 return false;
3947 if (!kind_check (k, 1, BT_INTEGER))
3948 return false;
3950 return true;
3953 /* Check function for findloc. Mostly like gfc_check_minloc_maxloc
3954 above, with the additional "value" argument. */
3956 bool
3957 gfc_check_findloc (gfc_actual_arglist *ap)
3959 gfc_expr *a, *v, *m, *d, *k, *b;
3960 bool a1, v1;
3962 a = ap->expr;
3963 if (!intrinsic_type_check (a, 0) || !array_check (a, 0))
3964 return false;
3966 v = ap->next->expr;
3967 if (!intrinsic_type_check (v, 1) || !scalar_check (v,1))
3968 return false;
3970 /* Check if the type are both logical. */
3971 a1 = a->ts.type == BT_LOGICAL;
3972 v1 = v->ts.type == BT_LOGICAL;
3973 if ((a1 && !v1) || (!a1 && v1))
3974 goto incompat;
3976 /* Check if the type are both character. */
3977 a1 = a->ts.type == BT_CHARACTER;
3978 v1 = v->ts.type == BT_CHARACTER;
3979 if ((a1 && !v1) || (!a1 && v1))
3980 goto incompat;
3982 /* Check the kind of the characters argument match. */
3983 if (a1 && v1 && a->ts.kind != v->ts.kind)
3984 goto incompat;
3986 d = ap->next->next->expr;
3987 m = ap->next->next->next->expr;
3988 k = ap->next->next->next->next->expr;
3989 b = ap->next->next->next->next->next->expr;
3991 if (b)
3993 if (!type_check (b, 5, BT_LOGICAL) || !scalar_check (b,4))
3994 return false;
3996 else
3998 b = gfc_get_logical_expr (gfc_logical_4_kind, NULL, 0);
3999 ap->next->next->next->next->next->expr = b;
4000 ap->next->next->next->next->next->name = gfc_get_string ("back");
4003 if (m == NULL && d != NULL && d->ts.type == BT_LOGICAL
4004 && ap->next->name == NULL)
4006 m = d;
4007 d = NULL;
4008 ap->next->next->expr = NULL;
4009 ap->next->next->next->expr = m;
4012 if (!dim_check (d, 2, false))
4013 return false;
4015 if (!dim_rank_check (d, a, 0))
4016 return false;
4018 if (m != NULL && !type_check (m, 3, BT_LOGICAL))
4019 return false;
4021 if (m != NULL
4022 && !gfc_check_conformance (a, m,
4023 _("arguments '%s' and '%s' for intrinsic %s"),
4024 gfc_current_intrinsic_arg[0]->name,
4025 gfc_current_intrinsic_arg[3]->name,
4026 gfc_current_intrinsic))
4027 return false;
4029 if (!kind_check (k, 1, BT_INTEGER))
4030 return false;
4032 return true;
4034 incompat:
4035 gfc_error ("Argument %qs of %qs intrinsic at %L must be in type "
4036 "conformance to argument %qs at %L",
4037 gfc_current_intrinsic_arg[0]->name,
4038 gfc_current_intrinsic, &a->where,
4039 gfc_current_intrinsic_arg[1]->name, &v->where);
4040 return false;
4044 /* Similar to minloc/maxloc, the argument list might need to be
4045 reordered for the MINVAL, MAXVAL, PRODUCT, and SUM intrinsics. The
4046 difference is that MINLOC/MAXLOC take an additional KIND argument.
4047 The possibilities are:
4049 Arg #2 Arg #3
4050 NULL NULL
4051 DIM NULL
4052 MASK NULL
4053 NULL MASK minval(array, mask=m)
4054 DIM MASK
4056 I.e. in the case of minval(array,mask), mask will be in the second
4057 position of the argument list and we'll have to fix that up. */
4059 static bool
4060 check_reduction (gfc_actual_arglist *ap)
4062 gfc_expr *a, *m, *d;
4064 a = ap->expr;
4065 d = ap->next->expr;
4066 m = ap->next->next->expr;
4068 if (m == NULL && d != NULL && d->ts.type == BT_LOGICAL
4069 && ap->next->name == NULL)
4071 m = d;
4072 d = NULL;
4073 ap->next->expr = NULL;
4074 ap->next->next->expr = m;
4077 if (!dim_check (d, 1, false))
4078 return false;
4080 if (!dim_rank_check (d, a, 0))
4081 return false;
4083 if (m != NULL && !type_check (m, 2, BT_LOGICAL))
4084 return false;
4086 if (m != NULL
4087 && !gfc_check_conformance (a, m,
4088 _("arguments '%s' and '%s' for intrinsic %s"),
4089 gfc_current_intrinsic_arg[0]->name,
4090 gfc_current_intrinsic_arg[2]->name,
4091 gfc_current_intrinsic))
4092 return false;
4094 return true;
4098 bool
4099 gfc_check_minval_maxval (gfc_actual_arglist *ap)
4101 if (!int_or_real_or_char_check_f2003 (ap->expr, 0)
4102 || !array_check (ap->expr, 0))
4103 return false;
4105 return check_reduction (ap);
4109 bool
4110 gfc_check_product_sum (gfc_actual_arglist *ap)
4112 if (!numeric_check (ap->expr, 0)
4113 || !array_check (ap->expr, 0))
4114 return false;
4116 return check_reduction (ap);
4120 /* For IANY, IALL and IPARITY. */
4122 bool
4123 gfc_check_mask (gfc_expr *i, gfc_expr *kind)
4125 int k;
4127 if (!type_check (i, 0, BT_INTEGER))
4128 return false;
4130 if (!nonnegative_check ("I", i))
4131 return false;
4133 if (!kind_check (kind, 1, BT_INTEGER))
4134 return false;
4136 if (kind)
4137 gfc_extract_int (kind, &k);
4138 else
4139 k = gfc_default_integer_kind;
4141 if (!less_than_bitsizekind ("I", i, k))
4142 return false;
4144 return true;
4148 bool
4149 gfc_check_transf_bit_intrins (gfc_actual_arglist *ap)
4151 if (ap->expr->ts.type != BT_INTEGER)
4153 gfc_error ("%qs argument of %qs intrinsic at %L must be INTEGER",
4154 gfc_current_intrinsic_arg[0]->name,
4155 gfc_current_intrinsic, &ap->expr->where);
4156 return false;
4159 if (!array_check (ap->expr, 0))
4160 return false;
4162 return check_reduction (ap);
4166 bool
4167 gfc_check_merge (gfc_expr *tsource, gfc_expr *fsource, gfc_expr *mask)
4169 if (gfc_invalid_null_arg (tsource))
4170 return false;
4172 if (gfc_invalid_null_arg (fsource))
4173 return false;
4175 if (!same_type_check (tsource, 0, fsource, 1))
4176 return false;
4178 if (!type_check (mask, 2, BT_LOGICAL))
4179 return false;
4181 if (tsource->ts.type == BT_CHARACTER)
4182 return gfc_check_same_strlen (tsource, fsource, "MERGE intrinsic");
4184 return true;
4188 bool
4189 gfc_check_merge_bits (gfc_expr *i, gfc_expr *j, gfc_expr *mask)
4191 /* i and j cannot both be BOZ literal constants. */
4192 if (!boz_args_check (i, j))
4193 return false;
4195 /* If i is BOZ and j is integer, convert i to type of j. */
4196 if (i->ts.type == BT_BOZ && j->ts.type == BT_INTEGER
4197 && !gfc_boz2int (i, j->ts.kind))
4198 return false;
4200 /* If j is BOZ and i is integer, convert j to type of i. */
4201 if (j->ts.type == BT_BOZ && i->ts.type == BT_INTEGER
4202 && !gfc_boz2int (j, i->ts.kind))
4203 return false;
4205 if (!type_check (i, 0, BT_INTEGER))
4206 return false;
4208 if (!type_check (j, 1, BT_INTEGER))
4209 return false;
4211 if (!same_type_check (i, 0, j, 1))
4212 return false;
4214 if (mask->ts.type == BT_BOZ && !gfc_boz2int(mask, i->ts.kind))
4215 return false;
4217 if (!type_check (mask, 2, BT_INTEGER))
4218 return false;
4220 if (!same_type_check (i, 0, mask, 2))
4221 return false;
4223 return true;
4227 bool
4228 gfc_check_move_alloc (gfc_expr *from, gfc_expr *to)
4230 if (!variable_check (from, 0, false))
4231 return false;
4232 if (!allocatable_check (from, 0))
4233 return false;
4234 if (gfc_is_coindexed (from))
4236 gfc_error ("The FROM argument to MOVE_ALLOC at %L shall not be "
4237 "coindexed", &from->where);
4238 return false;
4241 if (!variable_check (to, 1, false))
4242 return false;
4243 if (!allocatable_check (to, 1))
4244 return false;
4245 if (gfc_is_coindexed (to))
4247 gfc_error ("The TO argument to MOVE_ALLOC at %L shall not be "
4248 "coindexed", &to->where);
4249 return false;
4252 if (from->ts.type == BT_CLASS && to->ts.type == BT_DERIVED)
4254 gfc_error ("The TO arguments in MOVE_ALLOC at %L must be "
4255 "polymorphic if FROM is polymorphic",
4256 &to->where);
4257 return false;
4260 if (!same_type_check (to, 1, from, 0))
4261 return false;
4263 if (to->rank != from->rank)
4265 gfc_error ("The FROM and TO arguments of the MOVE_ALLOC intrinsic at %L "
4266 "must have the same rank %d/%d", &to->where, from->rank,
4267 to->rank);
4268 return false;
4271 /* IR F08/0040; cf. 12-006A. */
4272 if (gfc_get_corank (to) != gfc_get_corank (from))
4274 gfc_error ("The FROM and TO arguments of the MOVE_ALLOC intrinsic at %L "
4275 "must have the same corank %d/%d", &to->where,
4276 gfc_get_corank (from), gfc_get_corank (to));
4277 return false;
4280 /* This is based losely on F2003 12.4.1.7. It is intended to prevent
4281 the likes of to = sym->cmp1->cmp2 and from = sym->cmp1, where cmp1
4282 and cmp2 are allocatable. After the allocation is transferred,
4283 the 'to' chain is broken by the nullification of the 'from'. A bit
4284 of reflection reveals that this can only occur for derived types
4285 with recursive allocatable components. */
4286 if (to->expr_type == EXPR_VARIABLE && from->expr_type == EXPR_VARIABLE
4287 && !strcmp (to->symtree->n.sym->name, from->symtree->n.sym->name))
4289 gfc_ref *to_ref, *from_ref;
4290 to_ref = to->ref;
4291 from_ref = from->ref;
4292 bool aliasing = true;
4294 for (; from_ref && to_ref;
4295 from_ref = from_ref->next, to_ref = to_ref->next)
4297 if (to_ref->type != from->ref->type)
4298 aliasing = false;
4299 else if (to_ref->type == REF_ARRAY
4300 && to_ref->u.ar.type != AR_FULL
4301 && from_ref->u.ar.type != AR_FULL)
4302 /* Play safe; assume sections and elements are different. */
4303 aliasing = false;
4304 else if (to_ref->type == REF_COMPONENT
4305 && to_ref->u.c.component != from_ref->u.c.component)
4306 aliasing = false;
4308 if (!aliasing)
4309 break;
4312 if (aliasing)
4314 gfc_error ("The FROM and TO arguments at %L violate aliasing "
4315 "restrictions (F2003 12.4.1.7)", &to->where);
4316 return false;
4320 /* CLASS arguments: Make sure the vtab of from is present. */
4321 if (to->ts.type == BT_CLASS && !UNLIMITED_POLY (from))
4322 gfc_find_vtab (&from->ts);
4324 return true;
4328 bool
4329 gfc_check_nearest (gfc_expr *x, gfc_expr *s)
4331 if (!type_check (x, 0, BT_REAL))
4332 return false;
4334 if (!type_check (s, 1, BT_REAL))
4335 return false;
4337 if (s->expr_type == EXPR_CONSTANT)
4339 if (mpfr_sgn (s->value.real) == 0)
4341 gfc_error ("Argument %<S%> of NEAREST at %L shall not be zero",
4342 &s->where);
4343 return false;
4347 return true;
4351 bool
4352 gfc_check_new_line (gfc_expr *a)
4354 if (!type_check (a, 0, BT_CHARACTER))
4355 return false;
4357 return true;
4361 bool
4362 gfc_check_norm2 (gfc_expr *array, gfc_expr *dim)
4364 if (!type_check (array, 0, BT_REAL))
4365 return false;
4367 if (!array_check (array, 0))
4368 return false;
4370 if (!dim_check (dim, 1, false))
4371 return false;
4373 if (!dim_rank_check (dim, array, false))
4374 return false;
4376 return true;
4379 bool
4380 gfc_check_null (gfc_expr *mold)
4382 symbol_attribute attr;
4384 if (mold == NULL)
4385 return true;
4387 if (mold->expr_type == EXPR_NULL)
4388 return true;
4390 if (!variable_check (mold, 0, true))
4391 return false;
4393 attr = gfc_variable_attr (mold, NULL);
4395 if (!attr.pointer && !attr.proc_pointer && !attr.allocatable)
4397 gfc_error ("%qs argument of %qs intrinsic at %L must be a POINTER, "
4398 "ALLOCATABLE or procedure pointer",
4399 gfc_current_intrinsic_arg[0]->name,
4400 gfc_current_intrinsic, &mold->where);
4401 return false;
4404 if (attr.allocatable
4405 && !gfc_notify_std (GFC_STD_F2003, "NULL intrinsic with "
4406 "allocatable MOLD at %L", &mold->where))
4407 return false;
4409 /* F2008, C1242. */
4410 if (gfc_is_coindexed (mold))
4412 gfc_error ("%qs argument of %qs intrinsic at %L shall not be "
4413 "coindexed", gfc_current_intrinsic_arg[0]->name,
4414 gfc_current_intrinsic, &mold->where);
4415 return false;
4418 return true;
4422 bool
4423 gfc_check_pack (gfc_expr *array, gfc_expr *mask, gfc_expr *vector)
4425 if (!array_check (array, 0))
4426 return false;
4428 if (!type_check (mask, 1, BT_LOGICAL))
4429 return false;
4431 if (!gfc_check_conformance (array, mask,
4432 _("arguments '%s' and '%s' for intrinsic '%s'"),
4433 gfc_current_intrinsic_arg[0]->name,
4434 gfc_current_intrinsic_arg[1]->name,
4435 gfc_current_intrinsic))
4436 return false;
4438 if (vector != NULL)
4440 mpz_t array_size, vector_size;
4441 bool have_array_size, have_vector_size;
4443 if (!same_type_check (array, 0, vector, 2))
4444 return false;
4446 if (!rank_check (vector, 2, 1))
4447 return false;
4449 /* VECTOR requires at least as many elements as MASK
4450 has .TRUE. values. */
4451 have_array_size = gfc_array_size(array, &array_size);
4452 have_vector_size = gfc_array_size(vector, &vector_size);
4454 if (have_vector_size
4455 && (mask->expr_type == EXPR_ARRAY
4456 || (mask->expr_type == EXPR_CONSTANT
4457 && have_array_size)))
4459 int mask_true_values = 0;
4461 if (mask->expr_type == EXPR_ARRAY)
4463 gfc_constructor *mask_ctor;
4464 mask_ctor = gfc_constructor_first (mask->value.constructor);
4465 while (mask_ctor)
4467 if (mask_ctor->expr->expr_type != EXPR_CONSTANT)
4469 mask_true_values = 0;
4470 break;
4473 if (mask_ctor->expr->value.logical)
4474 mask_true_values++;
4476 mask_ctor = gfc_constructor_next (mask_ctor);
4479 else if (mask->expr_type == EXPR_CONSTANT && mask->value.logical)
4480 mask_true_values = mpz_get_si (array_size);
4482 if (mpz_get_si (vector_size) < mask_true_values)
4484 gfc_error ("%qs argument of %qs intrinsic at %L must "
4485 "provide at least as many elements as there "
4486 "are .TRUE. values in %qs (%ld/%d)",
4487 gfc_current_intrinsic_arg[2]->name,
4488 gfc_current_intrinsic, &vector->where,
4489 gfc_current_intrinsic_arg[1]->name,
4490 mpz_get_si (vector_size), mask_true_values);
4491 return false;
4495 if (have_array_size)
4496 mpz_clear (array_size);
4497 if (have_vector_size)
4498 mpz_clear (vector_size);
4501 return true;
4505 bool
4506 gfc_check_parity (gfc_expr *mask, gfc_expr *dim)
4508 if (!type_check (mask, 0, BT_LOGICAL))
4509 return false;
4511 if (!array_check (mask, 0))
4512 return false;
4514 if (!dim_check (dim, 1, false))
4515 return false;
4517 if (!dim_rank_check (dim, mask, false))
4518 return false;
4520 return true;
4524 bool
4525 gfc_check_precision (gfc_expr *x)
4527 if (!real_or_complex_check (x, 0))
4528 return false;
4530 return true;
4534 bool
4535 gfc_check_present (gfc_expr *a)
4537 gfc_symbol *sym;
4539 if (!variable_check (a, 0, true))
4540 return false;
4542 sym = a->symtree->n.sym;
4543 if (!sym->attr.dummy)
4545 gfc_error ("%qs argument of %qs intrinsic at %L must be of a "
4546 "dummy variable", gfc_current_intrinsic_arg[0]->name,
4547 gfc_current_intrinsic, &a->where);
4548 return false;
4551 /* For CLASS, the optional attribute might be set at either location. */
4552 if ((sym->ts.type != BT_CLASS || !CLASS_DATA (sym)->attr.optional)
4553 && !sym->attr.optional)
4555 gfc_error ("%qs argument of %qs intrinsic at %L must be of "
4556 "an OPTIONAL dummy variable",
4557 gfc_current_intrinsic_arg[0]->name, gfc_current_intrinsic,
4558 &a->where);
4559 return false;
4562 /* 13.14.82 PRESENT(A)
4563 ......
4564 Argument. A shall be the name of an optional dummy argument that is
4565 accessible in the subprogram in which the PRESENT function reference
4566 appears... */
4568 if (a->ref != NULL
4569 && !(a->ref->next == NULL && a->ref->type == REF_ARRAY
4570 && (a->ref->u.ar.type == AR_FULL
4571 || (a->ref->u.ar.type == AR_ELEMENT
4572 && a->ref->u.ar.as->rank == 0))))
4574 gfc_error ("%qs argument of %qs intrinsic at %L must not be a "
4575 "subobject of %qs", gfc_current_intrinsic_arg[0]->name,
4576 gfc_current_intrinsic, &a->where, sym->name);
4577 return false;
4580 return true;
4584 bool
4585 gfc_check_radix (gfc_expr *x)
4587 if (!int_or_real_check (x, 0))
4588 return false;
4590 return true;
4594 bool
4595 gfc_check_range (gfc_expr *x)
4597 if (!numeric_check (x, 0))
4598 return false;
4600 return true;
4604 bool
4605 gfc_check_rank (gfc_expr *a)
4607 /* Any data object is allowed; a "data object" is a "constant (4.1.3),
4608 variable (6), or subobject of a constant (2.4.3.2.3)" (F2008, 1.3.45). */
4610 bool is_variable = true;
4612 /* Functions returning pointers are regarded as variable, cf. F2008, R602. */
4613 if (a->expr_type == EXPR_FUNCTION)
4614 is_variable = a->value.function.esym
4615 ? a->value.function.esym->result->attr.pointer
4616 : a->symtree->n.sym->result->attr.pointer;
4618 if (a->expr_type == EXPR_OP
4619 || a->expr_type == EXPR_NULL
4620 || a->expr_type == EXPR_COMPCALL
4621 || a->expr_type == EXPR_PPC
4622 || a->ts.type == BT_PROCEDURE
4623 || !is_variable)
4625 gfc_error ("The argument of the RANK intrinsic at %L must be a data "
4626 "object", &a->where);
4627 return false;
4630 return true;
4634 bool
4635 gfc_check_real (gfc_expr *a, gfc_expr *kind)
4637 if (!kind_check (kind, 1, BT_REAL))
4638 return false;
4640 /* BOZ is dealt with in gfc_simplify_real. */
4641 if (a->ts.type == BT_BOZ)
4642 return true;
4644 if (!numeric_check (a, 0))
4645 return false;
4647 return true;
4651 bool
4652 gfc_check_rename (gfc_expr *path1, gfc_expr *path2)
4654 if (!type_check (path1, 0, BT_CHARACTER))
4655 return false;
4656 if (!kind_value_check (path1, 0, gfc_default_character_kind))
4657 return false;
4659 if (!type_check (path2, 1, BT_CHARACTER))
4660 return false;
4661 if (!kind_value_check (path2, 1, gfc_default_character_kind))
4662 return false;
4664 return true;
4668 bool
4669 gfc_check_rename_sub (gfc_expr *path1, gfc_expr *path2, gfc_expr *status)
4671 if (!type_check (path1, 0, BT_CHARACTER))
4672 return false;
4673 if (!kind_value_check (path1, 0, gfc_default_character_kind))
4674 return false;
4676 if (!type_check (path2, 1, BT_CHARACTER))
4677 return false;
4678 if (!kind_value_check (path2, 1, gfc_default_character_kind))
4679 return false;
4681 if (status == NULL)
4682 return true;
4684 if (!type_check (status, 2, BT_INTEGER))
4685 return false;
4687 if (!scalar_check (status, 2))
4688 return false;
4690 return true;
4694 bool
4695 gfc_check_repeat (gfc_expr *x, gfc_expr *y)
4697 if (!type_check (x, 0, BT_CHARACTER))
4698 return false;
4700 if (!scalar_check (x, 0))
4701 return false;
4703 if (!type_check (y, 0, BT_INTEGER))
4704 return false;
4706 if (!scalar_check (y, 1))
4707 return false;
4709 return true;
4713 bool
4714 gfc_check_reshape (gfc_expr *source, gfc_expr *shape,
4715 gfc_expr *pad, gfc_expr *order)
4717 mpz_t size;
4718 mpz_t nelems;
4719 int shape_size;
4720 bool shape_is_const;
4722 if (!array_check (source, 0))
4723 return false;
4725 if (!rank_check (shape, 1, 1))
4726 return false;
4728 if (!type_check (shape, 1, BT_INTEGER))
4729 return false;
4731 if (!gfc_array_size (shape, &size))
4733 gfc_error ("%<shape%> argument of %<reshape%> intrinsic at %L must be an "
4734 "array of constant size", &shape->where);
4735 return false;
4738 shape_size = mpz_get_ui (size);
4739 mpz_clear (size);
4741 if (shape_size <= 0)
4743 gfc_error ("%qs argument of %qs intrinsic at %L is empty",
4744 gfc_current_intrinsic_arg[1]->name, gfc_current_intrinsic,
4745 &shape->where);
4746 return false;
4748 else if (shape_size > GFC_MAX_DIMENSIONS)
4750 gfc_error ("%<shape%> argument of %<reshape%> intrinsic at %L has more "
4751 "than %d elements", &shape->where, GFC_MAX_DIMENSIONS);
4752 return false;
4755 gfc_simplify_expr (shape, 0);
4756 shape_is_const = gfc_is_constant_array_expr (shape);
4758 if (shape->expr_type == EXPR_ARRAY && shape_is_const)
4760 gfc_expr *e;
4761 int i, extent;
4762 for (i = 0; i < shape_size; ++i)
4764 e = gfc_constructor_lookup_expr (shape->value.constructor, i);
4765 if (e == NULL)
4766 break;
4767 if (e->expr_type != EXPR_CONSTANT)
4768 continue;
4770 gfc_extract_int (e, &extent);
4771 if (extent < 0)
4773 gfc_error ("%qs argument of %qs intrinsic at %L has "
4774 "negative element (%d)",
4775 gfc_current_intrinsic_arg[1]->name,
4776 gfc_current_intrinsic, &shape->where, extent);
4777 return false;
4782 if (pad != NULL)
4784 if (!same_type_check (source, 0, pad, 2))
4785 return false;
4787 if (!array_check (pad, 2))
4788 return false;
4791 if (order != NULL)
4793 if (!array_check (order, 3))
4794 return false;
4796 if (!type_check (order, 3, BT_INTEGER))
4797 return false;
4799 if (order->expr_type == EXPR_ARRAY && gfc_is_constant_array_expr (order))
4801 int i, order_size, dim, perm[GFC_MAX_DIMENSIONS];
4802 gfc_expr *e;
4804 for (i = 0; i < GFC_MAX_DIMENSIONS; ++i)
4805 perm[i] = 0;
4807 gfc_array_size (order, &size);
4808 order_size = mpz_get_ui (size);
4809 mpz_clear (size);
4811 if (order_size != shape_size)
4813 gfc_error ("%qs argument of %qs intrinsic at %L "
4814 "has wrong number of elements (%d/%d)",
4815 gfc_current_intrinsic_arg[3]->name,
4816 gfc_current_intrinsic, &order->where,
4817 order_size, shape_size);
4818 return false;
4821 for (i = 1; i <= order_size; ++i)
4823 e = gfc_constructor_lookup_expr (order->value.constructor, i-1);
4824 if (e->expr_type != EXPR_CONSTANT)
4825 continue;
4827 gfc_extract_int (e, &dim);
4829 if (dim < 1 || dim > order_size)
4831 gfc_error ("%qs argument of %qs intrinsic at %L "
4832 "has out-of-range dimension (%d)",
4833 gfc_current_intrinsic_arg[3]->name,
4834 gfc_current_intrinsic, &e->where, dim);
4835 return false;
4838 if (perm[dim-1] != 0)
4840 gfc_error ("%qs argument of %qs intrinsic at %L has "
4841 "invalid permutation of dimensions (dimension "
4842 "%qd duplicated)",
4843 gfc_current_intrinsic_arg[3]->name,
4844 gfc_current_intrinsic, &e->where, dim);
4845 return false;
4848 perm[dim-1] = 1;
4853 if (pad == NULL && shape->expr_type == EXPR_ARRAY && shape_is_const
4854 && !(source->expr_type == EXPR_VARIABLE && source->symtree->n.sym->as
4855 && source->symtree->n.sym->as->type == AS_ASSUMED_SIZE))
4857 /* Check the match in size between source and destination. */
4858 if (gfc_array_size (source, &nelems))
4860 gfc_constructor *c;
4861 bool test;
4864 mpz_init_set_ui (size, 1);
4865 for (c = gfc_constructor_first (shape->value.constructor);
4866 c; c = gfc_constructor_next (c))
4867 mpz_mul (size, size, c->expr->value.integer);
4869 test = mpz_cmp (nelems, size) < 0 && mpz_cmp_ui (size, 0) > 0;
4870 mpz_clear (nelems);
4871 mpz_clear (size);
4873 if (test)
4875 gfc_error ("Without padding, there are not enough elements "
4876 "in the intrinsic RESHAPE source at %L to match "
4877 "the shape", &source->where);
4878 return false;
4883 return true;
4887 bool
4888 gfc_check_same_type_as (gfc_expr *a, gfc_expr *b)
4890 if (a->ts.type != BT_DERIVED && a->ts.type != BT_CLASS)
4892 gfc_error ("%qs argument of %qs intrinsic at %L "
4893 "cannot be of type %s",
4894 gfc_current_intrinsic_arg[0]->name,
4895 gfc_current_intrinsic,
4896 &a->where, gfc_typename (a));
4897 return false;
4900 if (!(gfc_type_is_extensible (a->ts.u.derived) || UNLIMITED_POLY (a)))
4902 gfc_error ("%qs argument of %qs intrinsic at %L "
4903 "must be of an extensible type",
4904 gfc_current_intrinsic_arg[0]->name, gfc_current_intrinsic,
4905 &a->where);
4906 return false;
4909 if (b->ts.type != BT_DERIVED && b->ts.type != BT_CLASS)
4911 gfc_error ("%qs argument of %qs intrinsic at %L "
4912 "cannot be of type %s",
4913 gfc_current_intrinsic_arg[0]->name,
4914 gfc_current_intrinsic,
4915 &b->where, gfc_typename (b));
4916 return false;
4919 if (!(gfc_type_is_extensible (b->ts.u.derived) || UNLIMITED_POLY (b)))
4921 gfc_error ("%qs argument of %qs intrinsic at %L "
4922 "must be of an extensible type",
4923 gfc_current_intrinsic_arg[1]->name, gfc_current_intrinsic,
4924 &b->where);
4925 return false;
4928 return true;
4932 bool
4933 gfc_check_scale (gfc_expr *x, gfc_expr *i)
4935 if (!type_check (x, 0, BT_REAL))
4936 return false;
4938 if (!type_check (i, 1, BT_INTEGER))
4939 return false;
4941 return true;
4945 bool
4946 gfc_check_scan (gfc_expr *x, gfc_expr *y, gfc_expr *z, gfc_expr *kind)
4948 if (!type_check (x, 0, BT_CHARACTER))
4949 return false;
4951 if (!type_check (y, 1, BT_CHARACTER))
4952 return false;
4954 if (z != NULL && !type_check (z, 2, BT_LOGICAL))
4955 return false;
4957 if (!kind_check (kind, 3, BT_INTEGER))
4958 return false;
4959 if (kind && !gfc_notify_std (GFC_STD_F2003, "%qs intrinsic "
4960 "with KIND argument at %L",
4961 gfc_current_intrinsic, &kind->where))
4962 return false;
4964 if (!same_type_check (x, 0, y, 1))
4965 return false;
4967 return true;
4971 bool
4972 gfc_check_secnds (gfc_expr *r)
4974 if (!type_check (r, 0, BT_REAL))
4975 return false;
4977 if (!kind_value_check (r, 0, 4))
4978 return false;
4980 if (!scalar_check (r, 0))
4981 return false;
4983 return true;
4987 bool
4988 gfc_check_selected_char_kind (gfc_expr *name)
4990 if (!type_check (name, 0, BT_CHARACTER))
4991 return false;
4993 if (!kind_value_check (name, 0, gfc_default_character_kind))
4994 return false;
4996 if (!scalar_check (name, 0))
4997 return false;
4999 return true;
5003 bool
5004 gfc_check_selected_int_kind (gfc_expr *r)
5006 if (!type_check (r, 0, BT_INTEGER))
5007 return false;
5009 if (!scalar_check (r, 0))
5010 return false;
5012 return true;
5016 bool
5017 gfc_check_selected_real_kind (gfc_expr *p, gfc_expr *r, gfc_expr *radix)
5019 if (p == NULL && r == NULL
5020 && !gfc_notify_std (GFC_STD_F2008, "SELECTED_REAL_KIND with"
5021 " neither %<P%> nor %<R%> argument at %L",
5022 gfc_current_intrinsic_where))
5023 return false;
5025 if (p)
5027 if (!type_check (p, 0, BT_INTEGER))
5028 return false;
5030 if (!scalar_check (p, 0))
5031 return false;
5034 if (r)
5036 if (!type_check (r, 1, BT_INTEGER))
5037 return false;
5039 if (!scalar_check (r, 1))
5040 return false;
5043 if (radix)
5045 if (!type_check (radix, 1, BT_INTEGER))
5046 return false;
5048 if (!scalar_check (radix, 1))
5049 return false;
5051 if (!gfc_notify_std (GFC_STD_F2008, "%qs intrinsic with "
5052 "RADIX argument at %L", gfc_current_intrinsic,
5053 &radix->where))
5054 return false;
5057 return true;
5061 bool
5062 gfc_check_set_exponent (gfc_expr *x, gfc_expr *i)
5064 if (!type_check (x, 0, BT_REAL))
5065 return false;
5067 if (!type_check (i, 1, BT_INTEGER))
5068 return false;
5070 return true;
5074 bool
5075 gfc_check_shape (gfc_expr *source, gfc_expr *kind)
5077 gfc_array_ref *ar;
5079 if (gfc_invalid_null_arg (source))
5080 return false;
5082 if (!kind_check (kind, 1, BT_INTEGER))
5083 return false;
5084 if (kind && !gfc_notify_std (GFC_STD_F2003, "%qs intrinsic "
5085 "with KIND argument at %L",
5086 gfc_current_intrinsic, &kind->where))
5087 return false;
5089 if (source->rank == 0 || source->expr_type != EXPR_VARIABLE)
5090 return true;
5092 if (source->ref == NULL)
5093 return false;
5095 ar = gfc_find_array_ref (source);
5097 if (ar->as && ar->as->type == AS_ASSUMED_SIZE && ar->type == AR_FULL)
5099 gfc_error ("%<source%> argument of %<shape%> intrinsic at %L must not be "
5100 "an assumed size array", &source->where);
5101 return false;
5104 return true;
5108 bool
5109 gfc_check_shift (gfc_expr *i, gfc_expr *shift)
5111 if (!type_check (i, 0, BT_INTEGER))
5112 return false;
5114 if (!type_check (shift, 0, BT_INTEGER))
5115 return false;
5117 if (!nonnegative_check ("SHIFT", shift))
5118 return false;
5120 if (!less_than_bitsize1 ("I", i, "SHIFT", shift, true))
5121 return false;
5123 return true;
5127 bool
5128 gfc_check_sign (gfc_expr *a, gfc_expr *b)
5130 if (!int_or_real_check (a, 0))
5131 return false;
5133 if (!same_type_check (a, 0, b, 1))
5134 return false;
5136 return true;
5140 bool
5141 gfc_check_size (gfc_expr *array, gfc_expr *dim, gfc_expr *kind)
5143 if (!array_check (array, 0))
5144 return false;
5146 if (!dim_check (dim, 1, true))
5147 return false;
5149 if (!dim_rank_check (dim, array, 0))
5150 return false;
5152 if (!kind_check (kind, 2, BT_INTEGER))
5153 return false;
5154 if (kind && !gfc_notify_std (GFC_STD_F2003, "%qs intrinsic "
5155 "with KIND argument at %L",
5156 gfc_current_intrinsic, &kind->where))
5157 return false;
5160 return true;
5164 bool
5165 gfc_check_sizeof (gfc_expr *arg)
5167 if (gfc_invalid_null_arg (arg))
5168 return false;
5170 if (arg->ts.type == BT_PROCEDURE)
5172 gfc_error ("%qs argument of %qs intrinsic at %L shall not be a procedure",
5173 gfc_current_intrinsic_arg[0]->name, gfc_current_intrinsic,
5174 &arg->where);
5175 return false;
5178 if (illegal_boz_arg (arg))
5179 return false;
5181 /* TYPE(*) is acceptable if and only if it uses an array descriptor. */
5182 if (arg->ts.type == BT_ASSUMED
5183 && (arg->symtree->n.sym->as == NULL
5184 || (arg->symtree->n.sym->as->type != AS_ASSUMED_SHAPE
5185 && arg->symtree->n.sym->as->type != AS_DEFERRED
5186 && arg->symtree->n.sym->as->type != AS_ASSUMED_RANK)))
5188 gfc_error ("%qs argument of %qs intrinsic at %L shall not be TYPE(*)",
5189 gfc_current_intrinsic_arg[0]->name, gfc_current_intrinsic,
5190 &arg->where);
5191 return false;
5194 if (arg->rank && arg->expr_type == EXPR_VARIABLE
5195 && arg->symtree->n.sym->as != NULL
5196 && arg->symtree->n.sym->as->type == AS_ASSUMED_SIZE && arg->ref
5197 && arg->ref->type == REF_ARRAY && arg->ref->u.ar.type == AR_FULL)
5199 gfc_error ("%qs argument of %qs intrinsic at %L shall not be an "
5200 "assumed-size array", gfc_current_intrinsic_arg[0]->name,
5201 gfc_current_intrinsic, &arg->where);
5202 return false;
5205 return true;
5209 /* Check whether an expression is interoperable. When returning false,
5210 msg is set to a string telling why the expression is not interoperable,
5211 otherwise, it is set to NULL. The msg string can be used in diagnostics.
5212 If c_loc is true, character with len > 1 are allowed (cf. Fortran
5213 2003corr5); additionally, assumed-shape/assumed-rank/deferred-shape
5214 arrays are permitted. And if c_f_ptr is true, deferred-shape arrays
5215 are permitted. */
5217 static bool
5218 is_c_interoperable (gfc_expr *expr, const char **msg, bool c_loc, bool c_f_ptr)
5220 *msg = NULL;
5222 if (expr->expr_type == EXPR_NULL && expr->ts.type == BT_UNKNOWN)
5224 *msg = "NULL() is not interoperable";
5225 return false;
5228 if (expr->ts.type == BT_BOZ)
5230 *msg = "BOZ literal constant";
5231 return false;
5234 if (expr->ts.type == BT_CLASS)
5236 *msg = "Expression is polymorphic";
5237 return false;
5240 if (expr->ts.type == BT_DERIVED && !expr->ts.u.derived->attr.is_bind_c
5241 && !expr->ts.u.derived->ts.is_iso_c)
5243 *msg = "Expression is a noninteroperable derived type";
5244 return false;
5247 if (expr->ts.type == BT_PROCEDURE)
5249 *msg = "Procedure unexpected as argument";
5250 return false;
5253 if (gfc_notification_std (GFC_STD_GNU) && expr->ts.type == BT_LOGICAL)
5255 int i;
5256 for (i = 0; gfc_logical_kinds[i].kind; i++)
5257 if (gfc_logical_kinds[i].kind == expr->ts.kind)
5258 return true;
5259 *msg = "Extension to use a non-C_Bool-kind LOGICAL";
5260 return false;
5263 if (gfc_notification_std (GFC_STD_GNU) && expr->ts.type == BT_CHARACTER
5264 && expr->ts.kind != 1)
5266 *msg = "Extension to use a non-C_CHAR-kind CHARACTER";
5267 return false;
5270 if (expr->ts.type == BT_CHARACTER) {
5271 if (expr->ts.deferred)
5273 /* TS 29113 allows deferred-length strings as dummy arguments,
5274 but it is not an interoperable type. */
5275 *msg = "Expression shall not be a deferred-length string";
5276 return false;
5279 if (expr->ts.u.cl && expr->ts.u.cl->length
5280 && !gfc_simplify_expr (expr->ts.u.cl->length, 0))
5281 gfc_internal_error ("is_c_interoperable(): gfc_simplify_expr failed");
5283 if (!c_loc
5284 && expr->ts.u.cl
5285 && !gfc_length_one_character_type_p (&expr->ts))
5287 *msg = "Type shall have a character length of 1";
5288 return false;
5292 /* Note: The following checks are about interoperatable variables, Fortran
5293 15.3.5/15.3.6. In intrinsics like C_LOC or in procedure interface, more
5294 is allowed, e.g. assumed-shape arrays with TS 29113. */
5296 if (gfc_is_coarray (expr))
5298 *msg = "Coarrays are not interoperable";
5299 return false;
5302 /* Checks for C_SIZEOF need to take into account edits to 18-007r1, see
5303 https://j3-fortran.org/doc/year/22/22-101r1.txt . */
5304 if (!c_loc && !c_f_ptr && expr->rank > 0 && expr->expr_type == EXPR_VARIABLE)
5306 gfc_array_ref *ar = gfc_find_array_ref (expr);
5307 if (ar->type == AR_FULL && ar->as->type == AS_ASSUMED_SIZE)
5309 *msg = "Assumed-size arrays are not interoperable";
5310 return false;
5314 return true;
5318 bool
5319 gfc_check_c_sizeof (gfc_expr *arg)
5321 const char *msg;
5323 if (!is_c_interoperable (arg, &msg, false, false))
5325 gfc_error ("%qs argument of %qs intrinsic at %L must be an "
5326 "interoperable data entity: %s",
5327 gfc_current_intrinsic_arg[0]->name, gfc_current_intrinsic,
5328 &arg->where, msg);
5329 return false;
5332 if (arg->ts.type == BT_ASSUMED)
5334 gfc_error ("%qs argument of %qs intrinsic at %L shall not be "
5335 "TYPE(*)",
5336 gfc_current_intrinsic_arg[0]->name, gfc_current_intrinsic,
5337 &arg->where);
5338 return false;
5341 if (arg->rank && arg->expr_type == EXPR_VARIABLE
5342 && arg->symtree->n.sym->as != NULL
5343 && arg->symtree->n.sym->as->type == AS_ASSUMED_SIZE && arg->ref
5344 && arg->ref->type == REF_ARRAY && arg->ref->u.ar.type == AR_FULL)
5346 gfc_error ("%qs argument of %qs intrinsic at %L shall not be an "
5347 "assumed-size array", gfc_current_intrinsic_arg[0]->name,
5348 gfc_current_intrinsic, &arg->where);
5349 return false;
5352 return true;
5356 bool
5357 gfc_check_c_associated (gfc_expr *c_ptr_1, gfc_expr *c_ptr_2)
5359 if (c_ptr_1->ts.type != BT_DERIVED
5360 || c_ptr_1->ts.u.derived->from_intmod != INTMOD_ISO_C_BINDING
5361 || (c_ptr_1->ts.u.derived->intmod_sym_id != ISOCBINDING_PTR
5362 && c_ptr_1->ts.u.derived->intmod_sym_id != ISOCBINDING_FUNPTR))
5364 gfc_error ("Argument C_PTR_1 at %L to C_ASSOCIATED shall have the "
5365 "type TYPE(C_PTR) or TYPE(C_FUNPTR)", &c_ptr_1->where);
5366 return false;
5369 if (!scalar_check (c_ptr_1, 0))
5370 return false;
5372 if (c_ptr_2
5373 && (c_ptr_2->ts.type != BT_DERIVED
5374 || c_ptr_2->ts.u.derived->from_intmod != INTMOD_ISO_C_BINDING
5375 || (c_ptr_1->ts.u.derived->intmod_sym_id
5376 != c_ptr_2->ts.u.derived->intmod_sym_id)))
5378 gfc_error ("Argument C_PTR_2 at %L to C_ASSOCIATED shall have the "
5379 "same type as C_PTR_1: %s instead of %s", &c_ptr_1->where,
5380 gfc_typename (&c_ptr_1->ts),
5381 gfc_typename (&c_ptr_2->ts));
5382 return false;
5385 if (c_ptr_2 && !scalar_check (c_ptr_2, 1))
5386 return false;
5388 return true;
5392 bool
5393 gfc_check_c_f_pointer (gfc_expr *cptr, gfc_expr *fptr, gfc_expr *shape)
5395 symbol_attribute attr;
5396 const char *msg;
5398 if (cptr->ts.type != BT_DERIVED
5399 || cptr->ts.u.derived->from_intmod != INTMOD_ISO_C_BINDING
5400 || cptr->ts.u.derived->intmod_sym_id != ISOCBINDING_PTR)
5402 gfc_error ("Argument CPTR at %L to C_F_POINTER shall have the "
5403 "type TYPE(C_PTR)", &cptr->where);
5404 return false;
5407 if (!scalar_check (cptr, 0))
5408 return false;
5410 attr = gfc_expr_attr (fptr);
5412 if (!attr.pointer)
5414 gfc_error ("Argument FPTR at %L to C_F_POINTER must be a pointer",
5415 &fptr->where);
5416 return false;
5419 if (fptr->ts.type == BT_CLASS)
5421 gfc_error ("FPTR argument at %L to C_F_POINTER shall not be polymorphic",
5422 &fptr->where);
5423 return false;
5426 if (gfc_is_coindexed (fptr))
5428 gfc_error ("Argument FPTR at %L to C_F_POINTER shall not be "
5429 "coindexed", &fptr->where);
5430 return false;
5433 if (fptr->rank == 0 && shape)
5435 gfc_error ("Unexpected SHAPE argument at %L to C_F_POINTER with scalar "
5436 "FPTR", &fptr->where);
5437 return false;
5439 else if (fptr->rank && !shape)
5441 gfc_error ("Expected SHAPE argument to C_F_POINTER with array "
5442 "FPTR at %L", &fptr->where);
5443 return false;
5446 if (shape && !rank_check (shape, 2, 1))
5447 return false;
5449 if (shape && !type_check (shape, 2, BT_INTEGER))
5450 return false;
5452 if (shape)
5454 mpz_t size;
5455 if (gfc_array_size (shape, &size))
5457 if (mpz_cmp_ui (size, fptr->rank) != 0)
5459 mpz_clear (size);
5460 gfc_error ("SHAPE argument at %L to C_F_POINTER must have the same "
5461 "size as the RANK of FPTR", &shape->where);
5462 return false;
5464 mpz_clear (size);
5468 if (fptr->ts.type == BT_CLASS)
5470 gfc_error ("Polymorphic FPTR at %L to C_F_POINTER", &fptr->where);
5471 return false;
5474 if (fptr->ts.type == BT_PROCEDURE && attr.function)
5476 gfc_error ("FPTR argument to C_F_POINTER at %L is a function "
5477 "returning a pointer", &fptr->where);
5478 return false;
5481 if (fptr->rank > 0 && !is_c_interoperable (fptr, &msg, false, true))
5482 return gfc_notify_std (GFC_STD_F2018,
5483 "Noninteroperable array FPTR argument to "
5484 "C_F_POINTER at %L: %s", &fptr->where, msg);
5486 return true;
5490 bool
5491 gfc_check_c_f_procpointer (gfc_expr *cptr, gfc_expr *fptr)
5493 symbol_attribute attr;
5495 if (cptr->ts.type != BT_DERIVED
5496 || cptr->ts.u.derived->from_intmod != INTMOD_ISO_C_BINDING
5497 || cptr->ts.u.derived->intmod_sym_id != ISOCBINDING_FUNPTR)
5499 gfc_error ("Argument CPTR at %L to C_F_PROCPOINTER shall have the "
5500 "type TYPE(C_FUNPTR)", &cptr->where);
5501 return false;
5504 if (!scalar_check (cptr, 0))
5505 return false;
5507 attr = gfc_expr_attr (fptr);
5509 if (!attr.proc_pointer)
5511 gfc_error ("Argument FPTR at %L to C_F_PROCPOINTER shall be a procedure "
5512 "pointer", &fptr->where);
5513 return false;
5516 if (gfc_is_coindexed (fptr))
5518 gfc_error ("Argument FPTR at %L to C_F_PROCPOINTER shall not be "
5519 "coindexed", &fptr->where);
5520 return false;
5523 if (!attr.is_bind_c)
5524 return gfc_notify_std (GFC_STD_F2018, "Noninteroperable procedure "
5525 "pointer at %L to C_F_PROCPOINTER", &fptr->where);
5527 return true;
5531 bool
5532 gfc_check_c_funloc (gfc_expr *x)
5534 symbol_attribute attr;
5536 if (gfc_is_coindexed (x))
5538 gfc_error ("Argument X at %L to C_FUNLOC shall not be "
5539 "coindexed", &x->where);
5540 return false;
5543 attr = gfc_expr_attr (x);
5545 if (attr.function && !attr.proc_pointer && x->expr_type == EXPR_VARIABLE
5546 && x->symtree->n.sym == x->symtree->n.sym->result)
5547 for (gfc_namespace *ns = gfc_current_ns; ns; ns = ns->parent)
5548 if (x->symtree->n.sym == ns->proc_name)
5550 gfc_error ("Function result %qs at %L is invalid as X argument "
5551 "to C_FUNLOC", x->symtree->n.sym->name, &x->where);
5552 return false;
5555 if (attr.flavor != FL_PROCEDURE)
5557 gfc_error ("Argument X at %L to C_FUNLOC shall be a procedure "
5558 "or a procedure pointer", &x->where);
5559 return false;
5562 if (!attr.is_bind_c)
5563 return gfc_notify_std (GFC_STD_F2018, "Noninteroperable procedure "
5564 "at %L to C_FUNLOC", &x->where);
5565 return true;
5569 bool
5570 gfc_check_c_loc (gfc_expr *x)
5572 symbol_attribute attr;
5573 const char *msg;
5575 if (gfc_is_coindexed (x))
5577 gfc_error ("Argument X at %L to C_LOC shall not be coindexed", &x->where);
5578 return false;
5581 if (x->ts.type == BT_CLASS)
5583 gfc_error ("X argument at %L to C_LOC shall not be polymorphic",
5584 &x->where);
5585 return false;
5588 attr = gfc_expr_attr (x);
5590 if (!attr.pointer
5591 && (x->expr_type != EXPR_VARIABLE || !attr.target
5592 || attr.flavor == FL_PARAMETER))
5594 gfc_error ("Argument X at %L to C_LOC shall have either "
5595 "the POINTER or the TARGET attribute", &x->where);
5596 return false;
5599 if (x->ts.type == BT_CHARACTER
5600 && gfc_var_strlen (x) == 0)
5602 gfc_error ("Argument X at %L to C_LOC shall be not be a zero-sized "
5603 "string", &x->where);
5604 return false;
5607 if (!is_c_interoperable (x, &msg, true, false))
5609 if (x->ts.type == BT_CLASS)
5611 gfc_error ("Argument at %L to C_LOC shall not be polymorphic",
5612 &x->where);
5613 return false;
5616 if (x->rank
5617 && !gfc_notify_std (GFC_STD_F2018,
5618 "Noninteroperable array at %L as"
5619 " argument to C_LOC: %s", &x->where, msg))
5620 return false;
5622 else if (x->rank > 0 && gfc_notification_std (GFC_STD_F2008))
5624 gfc_array_ref *ar = gfc_find_array_ref (x);
5626 if (ar->as->type != AS_EXPLICIT && ar->as->type != AS_ASSUMED_SIZE
5627 && !attr.allocatable
5628 && !gfc_notify_std (GFC_STD_F2008,
5629 "Array of interoperable type at %L "
5630 "to C_LOC which is nonallocatable and neither "
5631 "assumed size nor explicit size", &x->where))
5632 return false;
5633 else if (ar->type != AR_FULL
5634 && !gfc_notify_std (GFC_STD_F2008, "Array section at %L "
5635 "to C_LOC", &x->where))
5636 return false;
5639 return true;
5643 bool
5644 gfc_check_sleep_sub (gfc_expr *seconds)
5646 if (!type_check (seconds, 0, BT_INTEGER))
5647 return false;
5649 if (!scalar_check (seconds, 0))
5650 return false;
5652 return true;
5655 bool
5656 gfc_check_sngl (gfc_expr *a)
5658 if (!type_check (a, 0, BT_REAL))
5659 return false;
5661 if ((a->ts.kind != gfc_default_double_kind)
5662 && !gfc_notify_std (GFC_STD_GNU, "non double precision "
5663 "REAL argument to %s intrinsic at %L",
5664 gfc_current_intrinsic, &a->where))
5665 return false;
5667 return true;
5670 bool
5671 gfc_check_spread (gfc_expr *source, gfc_expr *dim, gfc_expr *ncopies)
5673 if (gfc_invalid_null_arg (source))
5674 return false;
5676 if (source->rank >= GFC_MAX_DIMENSIONS)
5678 gfc_error ("%qs argument of %qs intrinsic at %L must be less "
5679 "than rank %d", gfc_current_intrinsic_arg[0]->name,
5680 gfc_current_intrinsic, &source->where, GFC_MAX_DIMENSIONS);
5682 return false;
5685 if (dim == NULL)
5686 return false;
5688 if (!dim_check (dim, 1, false))
5689 return false;
5691 /* dim_rank_check() does not apply here. */
5692 if (dim
5693 && dim->expr_type == EXPR_CONSTANT
5694 && (mpz_cmp_ui (dim->value.integer, 1) < 0
5695 || mpz_cmp_ui (dim->value.integer, source->rank + 1) > 0))
5697 gfc_error ("%qs argument of %qs intrinsic at %L is not a valid "
5698 "dimension index", gfc_current_intrinsic_arg[1]->name,
5699 gfc_current_intrinsic, &dim->where);
5700 return false;
5703 if (!type_check (ncopies, 2, BT_INTEGER))
5704 return false;
5706 if (!scalar_check (ncopies, 2))
5707 return false;
5709 return true;
5713 /* Functions for checking FGETC, FPUTC, FGET and FPUT (subroutines and
5714 functions). */
5716 bool
5717 arg_strlen_is_zero (gfc_expr *c, int n)
5719 if (gfc_var_strlen (c) == 0)
5721 gfc_error ("%qs argument of %qs intrinsic at %L must have "
5722 "length at least 1", gfc_current_intrinsic_arg[n]->name,
5723 gfc_current_intrinsic, &c->where);
5724 return true;
5726 return false;
5729 bool
5730 gfc_check_fgetputc_sub (gfc_expr *unit, gfc_expr *c, gfc_expr *status)
5732 if (!type_check (unit, 0, BT_INTEGER))
5733 return false;
5735 if (!scalar_check (unit, 0))
5736 return false;
5738 if (!type_check (c, 1, BT_CHARACTER))
5739 return false;
5740 if (!kind_value_check (c, 1, gfc_default_character_kind))
5741 return false;
5742 if (strcmp (gfc_current_intrinsic, "fgetc") == 0
5743 && !variable_check (c, 1, false))
5744 return false;
5745 if (arg_strlen_is_zero (c, 1))
5746 return false;
5748 if (status == NULL)
5749 return true;
5751 if (!type_check (status, 2, BT_INTEGER)
5752 || !kind_value_check (status, 2, gfc_default_integer_kind)
5753 || !scalar_check (status, 2)
5754 || !variable_check (status, 2, false))
5755 return false;
5757 return true;
5761 bool
5762 gfc_check_fgetputc (gfc_expr *unit, gfc_expr *c)
5764 return gfc_check_fgetputc_sub (unit, c, NULL);
5768 bool
5769 gfc_check_fgetput_sub (gfc_expr *c, gfc_expr *status)
5771 if (!type_check (c, 0, BT_CHARACTER))
5772 return false;
5773 if (!kind_value_check (c, 0, gfc_default_character_kind))
5774 return false;
5775 if (strcmp (gfc_current_intrinsic, "fget") == 0
5776 && !variable_check (c, 0, false))
5777 return false;
5778 if (arg_strlen_is_zero (c, 0))
5779 return false;
5781 if (status == NULL)
5782 return true;
5784 if (!type_check (status, 1, BT_INTEGER)
5785 || !kind_value_check (status, 1, gfc_default_integer_kind)
5786 || !scalar_check (status, 1)
5787 || !variable_check (status, 1, false))
5788 return false;
5790 return true;
5794 bool
5795 gfc_check_fgetput (gfc_expr *c)
5797 return gfc_check_fgetput_sub (c, NULL);
5801 bool
5802 gfc_check_fseek_sub (gfc_expr *unit, gfc_expr *offset, gfc_expr *whence, gfc_expr *status)
5804 if (!type_check (unit, 0, BT_INTEGER))
5805 return false;
5807 if (!scalar_check (unit, 0))
5808 return false;
5810 if (!type_check (offset, 1, BT_INTEGER))
5811 return false;
5813 if (!scalar_check (offset, 1))
5814 return false;
5816 if (!type_check (whence, 2, BT_INTEGER))
5817 return false;
5819 if (!scalar_check (whence, 2))
5820 return false;
5822 if (status == NULL)
5823 return true;
5825 if (!type_check (status, 3, BT_INTEGER))
5826 return false;
5828 if (!kind_value_check (status, 3, 4))
5829 return false;
5831 if (!scalar_check (status, 3))
5832 return false;
5834 return true;
5839 bool
5840 gfc_check_fstat (gfc_expr *unit, gfc_expr *array)
5842 if (!type_check (unit, 0, BT_INTEGER))
5843 return false;
5845 if (!scalar_check (unit, 0))
5846 return false;
5848 if (!type_check (array, 1, BT_INTEGER)
5849 || !kind_value_check (unit, 0, gfc_default_integer_kind))
5850 return false;
5852 if (!array_check (array, 1))
5853 return false;
5855 return true;
5859 bool
5860 gfc_check_fstat_sub (gfc_expr *unit, gfc_expr *array, gfc_expr *status)
5862 if (!type_check (unit, 0, BT_INTEGER))
5863 return false;
5865 if (!scalar_check (unit, 0))
5866 return false;
5868 if (!type_check (array, 1, BT_INTEGER)
5869 || !kind_value_check (array, 1, gfc_default_integer_kind))
5870 return false;
5872 if (!array_check (array, 1))
5873 return false;
5875 if (status == NULL)
5876 return true;
5878 if (!type_check (status, 2, BT_INTEGER)
5879 || !kind_value_check (status, 2, gfc_default_integer_kind))
5880 return false;
5882 if (!scalar_check (status, 2))
5883 return false;
5885 return true;
5889 bool
5890 gfc_check_ftell (gfc_expr *unit)
5892 if (!type_check (unit, 0, BT_INTEGER))
5893 return false;
5895 if (!scalar_check (unit, 0))
5896 return false;
5898 return true;
5902 bool
5903 gfc_check_ftell_sub (gfc_expr *unit, gfc_expr *offset)
5905 if (!type_check (unit, 0, BT_INTEGER))
5906 return false;
5908 if (!scalar_check (unit, 0))
5909 return false;
5911 if (!type_check (offset, 1, BT_INTEGER))
5912 return false;
5914 if (!scalar_check (offset, 1))
5915 return false;
5917 return true;
5921 bool
5922 gfc_check_stat (gfc_expr *name, gfc_expr *array)
5924 if (!type_check (name, 0, BT_CHARACTER))
5925 return false;
5926 if (!kind_value_check (name, 0, gfc_default_character_kind))
5927 return false;
5929 if (!type_check (array, 1, BT_INTEGER)
5930 || !kind_value_check (array, 1, gfc_default_integer_kind))
5931 return false;
5933 if (!array_check (array, 1))
5934 return false;
5936 return true;
5940 bool
5941 gfc_check_stat_sub (gfc_expr *name, gfc_expr *array, gfc_expr *status)
5943 if (!type_check (name, 0, BT_CHARACTER))
5944 return false;
5945 if (!kind_value_check (name, 0, gfc_default_character_kind))
5946 return false;
5948 if (!type_check (array, 1, BT_INTEGER)
5949 || !kind_value_check (array, 1, gfc_default_integer_kind))
5950 return false;
5952 if (!array_check (array, 1))
5953 return false;
5955 if (status == NULL)
5956 return true;
5958 if (!type_check (status, 2, BT_INTEGER)
5959 || !kind_value_check (array, 1, gfc_default_integer_kind))
5960 return false;
5962 if (!scalar_check (status, 2))
5963 return false;
5965 return true;
5969 bool
5970 gfc_check_image_index (gfc_expr *coarray, gfc_expr *sub)
5972 mpz_t nelems;
5974 if (flag_coarray == GFC_FCOARRAY_NONE)
5976 gfc_fatal_error ("Coarrays disabled at %C, use %<-fcoarray=%> to enable");
5977 return false;
5980 if (!coarray_check (coarray, 0))
5981 return false;
5983 if (sub->rank != 1)
5985 gfc_error ("%s argument to IMAGE_INDEX must be a rank one array at %L",
5986 gfc_current_intrinsic_arg[1]->name, &sub->where);
5987 return false;
5990 if (sub->ts.type != BT_INTEGER)
5992 gfc_error ("Type of %s argument of IMAGE_INDEX at %L shall be INTEGER",
5993 gfc_current_intrinsic_arg[1]->name, &sub->where);
5994 return false;
5997 if (gfc_array_size (sub, &nelems))
5999 int corank = gfc_get_corank (coarray);
6001 if (mpz_cmp_ui (nelems, corank) != 0)
6003 gfc_error ("The number of array elements of the SUB argument to "
6004 "IMAGE_INDEX at %L shall be %d (corank) not %d",
6005 &sub->where, corank, (int) mpz_get_si (nelems));
6006 mpz_clear (nelems);
6007 return false;
6009 mpz_clear (nelems);
6012 return true;
6016 bool
6017 gfc_check_num_images (gfc_expr *distance, gfc_expr *failed)
6019 if (flag_coarray == GFC_FCOARRAY_NONE)
6021 gfc_fatal_error ("Coarrays disabled at %C, use %<-fcoarray=%> to enable");
6022 return false;
6025 if (distance)
6027 if (!type_check (distance, 0, BT_INTEGER))
6028 return false;
6030 if (!nonnegative_check ("DISTANCE", distance))
6031 return false;
6033 if (!scalar_check (distance, 0))
6034 return false;
6036 if (!gfc_notify_std (GFC_STD_F2018, "DISTANCE= argument to "
6037 "NUM_IMAGES at %L", &distance->where))
6038 return false;
6041 if (failed)
6043 if (!type_check (failed, 1, BT_LOGICAL))
6044 return false;
6046 if (!scalar_check (failed, 1))
6047 return false;
6049 if (!gfc_notify_std (GFC_STD_F2018, "FAILED= argument to "
6050 "NUM_IMAGES at %L", &failed->where))
6051 return false;
6054 return true;
6058 bool
6059 gfc_check_team_number (gfc_expr *team)
6061 if (flag_coarray == GFC_FCOARRAY_NONE)
6063 gfc_fatal_error ("Coarrays disabled at %C, use %<-fcoarray=%> to enable");
6064 return false;
6067 if (team)
6069 if (team->ts.type != BT_DERIVED
6070 || team->ts.u.derived->from_intmod != INTMOD_ISO_FORTRAN_ENV
6071 || team->ts.u.derived->intmod_sym_id != ISOFORTRAN_TEAM_TYPE)
6073 gfc_error ("TEAM argument at %L to the intrinsic TEAM_NUMBER "
6074 "shall be of type TEAM_TYPE", &team->where);
6075 return false;
6078 else
6079 return true;
6081 return true;
6085 bool
6086 gfc_check_this_image (gfc_expr *coarray, gfc_expr *dim, gfc_expr *distance)
6088 if (flag_coarray == GFC_FCOARRAY_NONE)
6090 gfc_fatal_error ("Coarrays disabled at %C, use %<-fcoarray=%> to enable");
6091 return false;
6094 if (coarray == NULL && dim == NULL && distance == NULL)
6095 return true;
6097 if (dim != NULL && coarray == NULL)
6099 gfc_error ("DIM argument without COARRAY argument not allowed for "
6100 "THIS_IMAGE intrinsic at %L", &dim->where);
6101 return false;
6104 if (distance && (coarray || dim))
6106 gfc_error ("The DISTANCE argument may not be specified together with the "
6107 "COARRAY or DIM argument in intrinsic at %L",
6108 &distance->where);
6109 return false;
6112 /* Assume that we have "this_image (distance)". */
6113 if (coarray && !gfc_is_coarray (coarray) && coarray->ts.type == BT_INTEGER)
6115 if (dim)
6117 gfc_error ("Unexpected DIM argument with noncoarray argument at %L",
6118 &coarray->where);
6119 return false;
6121 distance = coarray;
6124 if (distance)
6126 if (!type_check (distance, 2, BT_INTEGER))
6127 return false;
6129 if (!nonnegative_check ("DISTANCE", distance))
6130 return false;
6132 if (!scalar_check (distance, 2))
6133 return false;
6135 if (!gfc_notify_std (GFC_STD_F2018, "DISTANCE= argument to "
6136 "THIS_IMAGE at %L", &distance->where))
6137 return false;
6139 return true;
6142 if (!coarray_check (coarray, 0))
6143 return false;
6145 if (dim != NULL)
6147 if (!dim_check (dim, 1, false))
6148 return false;
6150 if (!dim_corank_check (dim, coarray))
6151 return false;
6154 return true;
6157 /* Calculate the sizes for transfer, used by gfc_check_transfer and also
6158 by gfc_simplify_transfer. Return false if we cannot do so. */
6160 bool
6161 gfc_calculate_transfer_sizes (gfc_expr *source, gfc_expr *mold, gfc_expr *size,
6162 size_t *source_size, size_t *result_size,
6163 size_t *result_length_p)
6165 size_t result_elt_size;
6167 if (source->expr_type == EXPR_FUNCTION)
6168 return false;
6170 if (size && size->expr_type != EXPR_CONSTANT)
6171 return false;
6173 /* Calculate the size of the source. */
6174 if (!gfc_target_expr_size (source, source_size))
6175 return false;
6177 /* Determine the size of the element. */
6178 if (!gfc_element_size (mold, &result_elt_size))
6179 return false;
6181 /* If the storage size of SOURCE is greater than zero and MOLD is an array,
6182 * a scalar with the type and type parameters of MOLD shall not have a
6183 * storage size equal to zero.
6184 * If MOLD is a scalar and SIZE is absent, the result is a scalar.
6185 * If MOLD is an array and SIZE is absent, the result is an array and of
6186 * rank one. Its size is as small as possible such that its physical
6187 * representation is not shorter than that of SOURCE.
6188 * If SIZE is present, the result is an array of rank one and size SIZE.
6190 if (result_elt_size == 0 && *source_size > 0
6191 && (mold->expr_type == EXPR_ARRAY || mold->rank))
6193 gfc_error ("%<MOLD%> argument of %<TRANSFER%> intrinsic at %L is an "
6194 "array and shall not have storage size 0 when %<SOURCE%> "
6195 "argument has size greater than 0", &mold->where);
6196 return false;
6199 if (result_elt_size == 0 && *source_size == 0 && !size)
6201 *result_size = 0;
6202 if (result_length_p)
6203 *result_length_p = 0;
6204 return true;
6207 if ((result_elt_size > 0 && (mold->expr_type == EXPR_ARRAY || mold->rank))
6208 || size)
6210 int result_length;
6212 if (size)
6213 result_length = (size_t)mpz_get_ui (size->value.integer);
6214 else
6216 result_length = *source_size / result_elt_size;
6217 if (result_length * result_elt_size < *source_size)
6218 result_length += 1;
6221 *result_size = result_length * result_elt_size;
6222 if (result_length_p)
6223 *result_length_p = result_length;
6225 else
6226 *result_size = result_elt_size;
6228 return true;
6232 bool
6233 gfc_check_transfer (gfc_expr *source, gfc_expr *mold, gfc_expr *size)
6235 size_t source_size;
6236 size_t result_size;
6238 if (gfc_invalid_null_arg (source))
6239 return false;
6241 /* SOURCE shall be a scalar or array of any type. */
6242 if (source->ts.type == BT_PROCEDURE
6243 && source->symtree->n.sym->attr.subroutine == 1)
6245 gfc_error ("%<SOURCE%> argument of %<TRANSFER%> intrinsic at %L "
6246 "must not be a %s", &source->where,
6247 gfc_basic_typename (source->ts.type));
6248 return false;
6251 if (source->ts.type == BT_BOZ && illegal_boz_arg (source))
6252 return false;
6254 if (mold->ts.type == BT_BOZ && illegal_boz_arg (mold))
6255 return false;
6257 if (gfc_invalid_null_arg (mold))
6258 return false;
6260 /* MOLD shall be a scalar or array of any type. */
6261 if (mold->ts.type == BT_PROCEDURE
6262 && mold->symtree->n.sym->attr.subroutine == 1)
6264 gfc_error ("%<MOLD%> argument of %<TRANSFER%> intrinsic at %L "
6265 "must not be a %s", &mold->where,
6266 gfc_basic_typename (mold->ts.type));
6267 return false;
6270 if (mold->ts.type == BT_HOLLERITH)
6272 gfc_error ("%<MOLD%> argument of %<TRANSFER%> intrinsic at %L must not be"
6273 " %s", &mold->where, gfc_basic_typename (BT_HOLLERITH));
6274 return false;
6277 /* SIZE (optional) shall be an integer scalar. The corresponding actual
6278 argument shall not be an optional dummy argument. */
6279 if (size != NULL)
6281 if (!type_check (size, 2, BT_INTEGER))
6283 if (size->ts.type == BT_BOZ)
6284 reset_boz (size);
6285 return false;
6288 if (!scalar_check (size, 2))
6289 return false;
6291 if (!nonoptional_check (size, 2))
6292 return false;
6295 if (!warn_surprising)
6296 return true;
6298 /* If we can't calculate the sizes, we cannot check any more.
6299 Return true for that case. */
6301 if (!gfc_calculate_transfer_sizes (source, mold, size, &source_size,
6302 &result_size, NULL))
6303 return true;
6305 if (source_size < result_size)
6306 gfc_warning (OPT_Wsurprising,
6307 "Intrinsic TRANSFER at %L has partly undefined result: "
6308 "source size %zd < result size %zd", &source->where,
6309 source_size, result_size);
6311 return true;
6315 bool
6316 gfc_check_transpose (gfc_expr *matrix)
6318 if (!rank_check (matrix, 0, 2))
6319 return false;
6321 return true;
6325 bool
6326 gfc_check_ubound (gfc_expr *array, gfc_expr *dim, gfc_expr *kind)
6328 if (!array_check (array, 0))
6329 return false;
6331 if (!dim_check (dim, 1, false))
6332 return false;
6334 if (!dim_rank_check (dim, array, 0))
6335 return false;
6337 if (!kind_check (kind, 2, BT_INTEGER))
6338 return false;
6339 if (kind && !gfc_notify_std (GFC_STD_F2003, "%qs intrinsic "
6340 "with KIND argument at %L",
6341 gfc_current_intrinsic, &kind->where))
6342 return false;
6344 return true;
6348 bool
6349 gfc_check_ucobound (gfc_expr *coarray, gfc_expr *dim, gfc_expr *kind)
6351 if (flag_coarray == GFC_FCOARRAY_NONE)
6353 gfc_fatal_error ("Coarrays disabled at %C, use %<-fcoarray=%> to enable");
6354 return false;
6357 if (!coarray_check (coarray, 0))
6358 return false;
6360 if (dim != NULL)
6362 if (!dim_check (dim, 1, false))
6363 return false;
6365 if (!dim_corank_check (dim, coarray))
6366 return false;
6369 if (!kind_check (kind, 2, BT_INTEGER))
6370 return false;
6372 return true;
6376 bool
6377 gfc_check_unpack (gfc_expr *vector, gfc_expr *mask, gfc_expr *field)
6379 mpz_t vector_size;
6381 if (!rank_check (vector, 0, 1))
6382 return false;
6384 if (!array_check (mask, 1))
6385 return false;
6387 if (!type_check (mask, 1, BT_LOGICAL))
6388 return false;
6390 if (!same_type_check (vector, 0, field, 2))
6391 return false;
6393 gfc_simplify_expr (mask, 0);
6395 if (mask->expr_type == EXPR_ARRAY
6396 && gfc_array_size (vector, &vector_size))
6398 int mask_true_count = 0;
6399 gfc_constructor *mask_ctor;
6400 mask_ctor = gfc_constructor_first (mask->value.constructor);
6401 while (mask_ctor)
6403 if (mask_ctor->expr->expr_type != EXPR_CONSTANT)
6405 mask_true_count = 0;
6406 break;
6409 if (mask_ctor->expr->value.logical)
6410 mask_true_count++;
6412 mask_ctor = gfc_constructor_next (mask_ctor);
6415 if (mpz_get_si (vector_size) < mask_true_count)
6417 gfc_error ("%qs argument of %qs intrinsic at %L must "
6418 "provide at least as many elements as there "
6419 "are .TRUE. values in %qs (%ld/%d)",
6420 gfc_current_intrinsic_arg[0]->name, gfc_current_intrinsic,
6421 &vector->where, gfc_current_intrinsic_arg[1]->name,
6422 mpz_get_si (vector_size), mask_true_count);
6423 return false;
6426 mpz_clear (vector_size);
6429 if (mask->rank != field->rank && field->rank != 0)
6431 gfc_error ("%qs argument of %qs intrinsic at %L must have "
6432 "the same rank as %qs or be a scalar",
6433 gfc_current_intrinsic_arg[2]->name, gfc_current_intrinsic,
6434 &field->where, gfc_current_intrinsic_arg[1]->name);
6435 return false;
6438 if (mask->rank == field->rank)
6440 int i;
6441 for (i = 0; i < field->rank; i++)
6442 if (! identical_dimen_shape (mask, i, field, i))
6444 gfc_error ("%qs and %qs arguments of %qs intrinsic at %L "
6445 "must have identical shape.",
6446 gfc_current_intrinsic_arg[2]->name,
6447 gfc_current_intrinsic_arg[1]->name, gfc_current_intrinsic,
6448 &field->where);
6452 return true;
6456 bool
6457 gfc_check_verify (gfc_expr *x, gfc_expr *y, gfc_expr *z, gfc_expr *kind)
6459 if (!type_check (x, 0, BT_CHARACTER))
6460 return false;
6462 if (!same_type_check (x, 0, y, 1))
6463 return false;
6465 if (z != NULL && !type_check (z, 2, BT_LOGICAL))
6466 return false;
6468 if (!kind_check (kind, 3, BT_INTEGER))
6469 return false;
6470 if (kind && !gfc_notify_std (GFC_STD_F2003, "%qs intrinsic "
6471 "with KIND argument at %L",
6472 gfc_current_intrinsic, &kind->where))
6473 return false;
6475 return true;
6479 bool
6480 gfc_check_trim (gfc_expr *x)
6482 if (!type_check (x, 0, BT_CHARACTER))
6483 return false;
6485 if (gfc_invalid_null_arg (x))
6486 return false;
6488 if (!scalar_check (x, 0))
6489 return false;
6491 return true;
6495 bool
6496 gfc_check_ttynam (gfc_expr *unit)
6498 if (!scalar_check (unit, 0))
6499 return false;
6501 if (!type_check (unit, 0, BT_INTEGER))
6502 return false;
6504 return true;
6508 /************* Check functions for intrinsic subroutines *************/
6510 bool
6511 gfc_check_cpu_time (gfc_expr *time)
6513 if (!scalar_check (time, 0))
6514 return false;
6516 if (!type_check (time, 0, BT_REAL))
6517 return false;
6519 if (!variable_check (time, 0, false))
6520 return false;
6522 return true;
6526 bool
6527 gfc_check_date_and_time (gfc_expr *date, gfc_expr *time,
6528 gfc_expr *zone, gfc_expr *values)
6530 if (date != NULL)
6532 if (!type_check (date, 0, BT_CHARACTER))
6533 return false;
6534 if (!kind_value_check (date, 0, gfc_default_character_kind))
6535 return false;
6536 if (!scalar_check (date, 0))
6537 return false;
6538 if (!variable_check (date, 0, false))
6539 return false;
6542 if (time != NULL)
6544 if (!type_check (time, 1, BT_CHARACTER))
6545 return false;
6546 if (!kind_value_check (time, 1, gfc_default_character_kind))
6547 return false;
6548 if (!scalar_check (time, 1))
6549 return false;
6550 if (!variable_check (time, 1, false))
6551 return false;
6554 if (zone != NULL)
6556 if (!type_check (zone, 2, BT_CHARACTER))
6557 return false;
6558 if (!kind_value_check (zone, 2, gfc_default_character_kind))
6559 return false;
6560 if (!scalar_check (zone, 2))
6561 return false;
6562 if (!variable_check (zone, 2, false))
6563 return false;
6566 if (values != NULL)
6568 if (!type_check (values, 3, BT_INTEGER))
6569 return false;
6570 if (!array_check (values, 3))
6571 return false;
6572 if (!rank_check (values, 3, 1))
6573 return false;
6574 if (!variable_check (values, 3, false))
6575 return false;
6576 if (!array_size_check (values, 3, 8))
6577 return false;
6579 if (values->ts.kind != gfc_default_integer_kind
6580 && !gfc_notify_std (GFC_STD_F2018, "VALUES argument of "
6581 "DATE_AND_TIME at %L has non-default kind",
6582 &values->where))
6583 return false;
6585 /* F2018:16.9.59 DATE_AND_TIME
6586 "VALUES shall be a rank-one array of type integer
6587 with a decimal exponent range of at least four."
6588 This is a hard limit also required by the implementation in
6589 libgfortran. */
6590 if (values->ts.kind < 2)
6592 gfc_error ("VALUES argument of DATE_AND_TIME at %L must have "
6593 "a decimal exponent range of at least four",
6594 &values->where);
6595 return false;
6599 return true;
6603 bool
6604 gfc_check_mvbits (gfc_expr *from, gfc_expr *frompos, gfc_expr *len,
6605 gfc_expr *to, gfc_expr *topos)
6607 if (!type_check (from, 0, BT_INTEGER))
6608 return false;
6610 if (!type_check (frompos, 1, BT_INTEGER))
6611 return false;
6613 if (!type_check (len, 2, BT_INTEGER))
6614 return false;
6616 if (!same_type_check (from, 0, to, 3))
6617 return false;
6619 if (!variable_check (to, 3, false))
6620 return false;
6622 if (!type_check (topos, 4, BT_INTEGER))
6623 return false;
6625 if (!nonnegative_check ("frompos", frompos))
6626 return false;
6628 if (!nonnegative_check ("topos", topos))
6629 return false;
6631 if (!nonnegative_check ("len", len))
6632 return false;
6634 if (!less_than_bitsize2 ("from", from, "frompos", frompos, "len", len))
6635 return false;
6637 if (!less_than_bitsize2 ("to", to, "topos", topos, "len", len))
6638 return false;
6640 return true;
6644 /* Check the arguments for RANDOM_INIT. */
6646 bool
6647 gfc_check_random_init (gfc_expr *repeatable, gfc_expr *image_distinct)
6649 if (!type_check (repeatable, 0, BT_LOGICAL))
6650 return false;
6652 if (!scalar_check (repeatable, 0))
6653 return false;
6655 if (!type_check (image_distinct, 1, BT_LOGICAL))
6656 return false;
6658 if (!scalar_check (image_distinct, 1))
6659 return false;
6661 return true;
6665 bool
6666 gfc_check_random_number (gfc_expr *harvest)
6668 if (!type_check (harvest, 0, BT_REAL))
6669 return false;
6671 if (!variable_check (harvest, 0, false))
6672 return false;
6674 return true;
6678 bool
6679 gfc_check_random_seed (gfc_expr *size, gfc_expr *put, gfc_expr *get)
6681 unsigned int nargs = 0, seed_size;
6682 locus *where = NULL;
6683 mpz_t put_size, get_size;
6685 /* Keep the number of bytes in sync with master_state in
6686 libgfortran/intrinsics/random.c. */
6687 seed_size = 32 / gfc_default_integer_kind;
6689 if (size != NULL)
6691 if (size->expr_type != EXPR_VARIABLE
6692 || !size->symtree->n.sym->attr.optional)
6693 nargs++;
6695 if (!scalar_check (size, 0))
6696 return false;
6698 if (!type_check (size, 0, BT_INTEGER))
6699 return false;
6701 if (!variable_check (size, 0, false))
6702 return false;
6704 if (!kind_value_check (size, 0, gfc_default_integer_kind))
6705 return false;
6708 if (put != NULL)
6710 if (put->expr_type != EXPR_VARIABLE
6711 || !put->symtree->n.sym->attr.optional)
6713 nargs++;
6714 where = &put->where;
6717 if (!array_check (put, 1))
6718 return false;
6720 if (!rank_check (put, 1, 1))
6721 return false;
6723 if (!type_check (put, 1, BT_INTEGER))
6724 return false;
6726 if (!kind_value_check (put, 1, gfc_default_integer_kind))
6727 return false;
6729 if (gfc_array_size (put, &put_size)
6730 && mpz_get_ui (put_size) < seed_size)
6731 gfc_error ("Size of %qs argument of %qs intrinsic at %L "
6732 "too small (%i/%i)",
6733 gfc_current_intrinsic_arg[1]->name, gfc_current_intrinsic,
6734 &put->where, (int) mpz_get_ui (put_size), seed_size);
6737 if (get != NULL)
6739 if (get->expr_type != EXPR_VARIABLE
6740 || !get->symtree->n.sym->attr.optional)
6742 nargs++;
6743 where = &get->where;
6746 if (!array_check (get, 2))
6747 return false;
6749 if (!rank_check (get, 2, 1))
6750 return false;
6752 if (!type_check (get, 2, BT_INTEGER))
6753 return false;
6755 if (!variable_check (get, 2, false))
6756 return false;
6758 if (!kind_value_check (get, 2, gfc_default_integer_kind))
6759 return false;
6761 if (gfc_array_size (get, &get_size)
6762 && mpz_get_ui (get_size) < seed_size)
6763 gfc_error ("Size of %qs argument of %qs intrinsic at %L "
6764 "too small (%i/%i)",
6765 gfc_current_intrinsic_arg[2]->name, gfc_current_intrinsic,
6766 &get->where, (int) mpz_get_ui (get_size), seed_size);
6769 /* RANDOM_SEED may not have more than one non-optional argument. */
6770 if (nargs > 1)
6771 gfc_error ("Too many arguments to %s at %L", gfc_current_intrinsic, where);
6773 return true;
6776 bool
6777 gfc_check_fe_runtime_error (gfc_actual_arglist *a)
6779 gfc_expr *e;
6780 size_t len, i;
6781 int num_percent, nargs;
6783 e = a->expr;
6784 if (e->expr_type != EXPR_CONSTANT)
6785 return true;
6787 len = e->value.character.length;
6788 if (e->value.character.string[len-1] != '\0')
6789 gfc_internal_error ("fe_runtime_error string must be null terminated");
6791 num_percent = 0;
6792 for (i=0; i<len-1; i++)
6793 if (e->value.character.string[i] == '%')
6794 num_percent ++;
6796 nargs = 0;
6797 for (; a; a = a->next)
6798 nargs ++;
6800 if (nargs -1 != num_percent)
6801 gfc_internal_error ("fe_runtime_error: Wrong number of arguments (%d instead of %d)",
6802 nargs, num_percent++);
6804 return true;
6807 bool
6808 gfc_check_second_sub (gfc_expr *time)
6810 if (!scalar_check (time, 0))
6811 return false;
6813 if (!type_check (time, 0, BT_REAL))
6814 return false;
6816 if (!kind_value_check (time, 0, 4))
6817 return false;
6819 return true;
6823 /* COUNT and COUNT_MAX of SYSTEM_CLOCK are scalar, default-kind integer
6824 variables in Fortran 95. In Fortran 2003 and later, they can be of any
6825 kind, and COUNT_RATE can be of type real. Note, count, count_rate, and
6826 count_max are all optional arguments */
6828 bool
6829 gfc_check_system_clock (gfc_expr *count, gfc_expr *count_rate,
6830 gfc_expr *count_max)
6832 int first_int_kind = -1;
6834 if (count != NULL)
6836 if (!scalar_check (count, 0))
6837 return false;
6839 if (!type_check (count, 0, BT_INTEGER))
6840 return false;
6842 if (count->ts.kind != gfc_default_integer_kind
6843 && !gfc_notify_std (GFC_STD_F2003, "COUNT argument to "
6844 "SYSTEM_CLOCK at %L has non-default kind",
6845 &count->where))
6846 return false;
6848 if (count->ts.kind < gfc_default_integer_kind
6849 && !gfc_notify_std (GFC_STD_F2023_DEL,
6850 "COUNT argument to SYSTEM_CLOCK at %L "
6851 "with kind smaller than default integer",
6852 &count->where))
6853 return false;
6855 if (!variable_check (count, 0, false))
6856 return false;
6858 first_int_kind = count->ts.kind;
6861 if (count_rate != NULL)
6863 if (!scalar_check (count_rate, 1))
6864 return false;
6866 if (!variable_check (count_rate, 1, false))
6867 return false;
6869 if (count_rate->ts.type == BT_REAL)
6871 if (!gfc_notify_std (GFC_STD_F2003, "Real COUNT_RATE argument to "
6872 "SYSTEM_CLOCK at %L", &count_rate->where))
6873 return false;
6875 else
6877 if (!type_check (count_rate, 1, BT_INTEGER))
6878 return false;
6880 if (count_rate->ts.kind != gfc_default_integer_kind
6881 && !gfc_notify_std (GFC_STD_F2003, "COUNT_RATE argument to "
6882 "SYSTEM_CLOCK at %L has non-default kind",
6883 &count_rate->where))
6884 return false;
6886 if (count_rate->ts.kind < gfc_default_integer_kind
6887 && !gfc_notify_std (GFC_STD_F2023_DEL,
6888 "COUNT_RATE argument to SYSTEM_CLOCK at %L "
6889 "with kind smaller than default integer",
6890 &count_rate->where))
6891 return false;
6893 if (first_int_kind < 0)
6894 first_int_kind = count_rate->ts.kind;
6899 if (count_max != NULL)
6901 if (!scalar_check (count_max, 2))
6902 return false;
6904 if (!type_check (count_max, 2, BT_INTEGER))
6905 return false;
6907 if (count_max->ts.kind != gfc_default_integer_kind
6908 && !gfc_notify_std (GFC_STD_F2003, "COUNT_MAX argument to "
6909 "SYSTEM_CLOCK at %L has non-default kind",
6910 &count_max->where))
6911 return false;
6913 if (!variable_check (count_max, 2, false))
6914 return false;
6916 if (count_max->ts.kind < gfc_default_integer_kind
6917 && !gfc_notify_std (GFC_STD_F2023_DEL,
6918 "COUNT_MAX argument to SYSTEM_CLOCK at %L "
6919 "with kind smaller than default integer",
6920 &count_max->where))
6921 return false;
6923 if (first_int_kind < 0)
6924 first_int_kind = count_max->ts.kind;
6927 if (first_int_kind > 0)
6929 if (count_rate
6930 && count_rate->ts.type == BT_INTEGER
6931 && count_rate->ts.kind != first_int_kind
6932 && !gfc_notify_std (GFC_STD_F2023_DEL,
6933 "integer arguments to SYSTEM_CLOCK at %L "
6934 "with different kind parameters",
6935 &count_rate->where))
6936 return false;
6938 if (count_max && count_max->ts.kind != first_int_kind
6939 && !gfc_notify_std (GFC_STD_F2023_DEL,
6940 "integer arguments to SYSTEM_CLOCK at %L "
6941 "with different kind parameters",
6942 &count_max->where))
6943 return false;
6946 return true;
6950 bool
6951 gfc_check_irand (gfc_expr *x)
6953 if (x == NULL)
6954 return true;
6956 if (!scalar_check (x, 0))
6957 return false;
6959 if (!type_check (x, 0, BT_INTEGER))
6960 return false;
6962 if (!kind_value_check (x, 0, 4))
6963 return false;
6965 return true;
6969 bool
6970 gfc_check_alarm_sub (gfc_expr *seconds, gfc_expr *handler, gfc_expr *status)
6972 if (!scalar_check (seconds, 0))
6973 return false;
6974 if (!type_check (seconds, 0, BT_INTEGER))
6975 return false;
6977 if (!int_or_proc_check (handler, 1))
6978 return false;
6979 if (handler->ts.type == BT_INTEGER && !scalar_check (handler, 1))
6980 return false;
6982 if (status == NULL)
6983 return true;
6985 if (!scalar_check (status, 2))
6986 return false;
6987 if (!type_check (status, 2, BT_INTEGER))
6988 return false;
6989 if (!kind_value_check (status, 2, gfc_default_integer_kind))
6990 return false;
6992 return true;
6996 bool
6997 gfc_check_rand (gfc_expr *x)
6999 if (x == NULL)
7000 return true;
7002 if (!scalar_check (x, 0))
7003 return false;
7005 if (!type_check (x, 0, BT_INTEGER))
7006 return false;
7008 if (!kind_value_check (x, 0, 4))
7009 return false;
7011 return true;
7015 bool
7016 gfc_check_srand (gfc_expr *x)
7018 if (!scalar_check (x, 0))
7019 return false;
7021 if (!type_check (x, 0, BT_INTEGER))
7022 return false;
7024 if (!kind_value_check (x, 0, 4))
7025 return false;
7027 return true;
7031 bool
7032 gfc_check_ctime_sub (gfc_expr *time, gfc_expr *result)
7034 if (!scalar_check (time, 0))
7035 return false;
7036 if (!type_check (time, 0, BT_INTEGER))
7037 return false;
7039 if (!type_check (result, 1, BT_CHARACTER))
7040 return false;
7041 if (!kind_value_check (result, 1, gfc_default_character_kind))
7042 return false;
7044 return true;
7048 bool
7049 gfc_check_dtime_etime (gfc_expr *x)
7051 if (!array_check (x, 0))
7052 return false;
7054 if (!rank_check (x, 0, 1))
7055 return false;
7057 if (!variable_check (x, 0, false))
7058 return false;
7060 if (!type_check (x, 0, BT_REAL))
7061 return false;
7063 if (!kind_value_check (x, 0, 4))
7064 return false;
7066 return true;
7070 bool
7071 gfc_check_dtime_etime_sub (gfc_expr *values, gfc_expr *time)
7073 if (!array_check (values, 0))
7074 return false;
7076 if (!rank_check (values, 0, 1))
7077 return false;
7079 if (!variable_check (values, 0, false))
7080 return false;
7082 if (!type_check (values, 0, BT_REAL))
7083 return false;
7085 if (!kind_value_check (values, 0, 4))
7086 return false;
7088 if (!scalar_check (time, 1))
7089 return false;
7091 if (!type_check (time, 1, BT_REAL))
7092 return false;
7094 if (!kind_value_check (time, 1, 4))
7095 return false;
7097 return true;
7101 bool
7102 gfc_check_fdate_sub (gfc_expr *date)
7104 if (!type_check (date, 0, BT_CHARACTER))
7105 return false;
7106 if (!kind_value_check (date, 0, gfc_default_character_kind))
7107 return false;
7109 return true;
7113 bool
7114 gfc_check_gerror (gfc_expr *msg)
7116 if (!type_check (msg, 0, BT_CHARACTER))
7117 return false;
7118 if (!kind_value_check (msg, 0, gfc_default_character_kind))
7119 return false;
7121 return true;
7125 bool
7126 gfc_check_getcwd_sub (gfc_expr *cwd, gfc_expr *status)
7128 if (!type_check (cwd, 0, BT_CHARACTER))
7129 return false;
7130 if (!kind_value_check (cwd, 0, gfc_default_character_kind))
7131 return false;
7133 if (status == NULL)
7134 return true;
7136 if (!scalar_check (status, 1))
7137 return false;
7139 if (!type_check (status, 1, BT_INTEGER))
7140 return false;
7142 return true;
7146 bool
7147 gfc_check_getarg (gfc_expr *pos, gfc_expr *value)
7149 if (!type_check (pos, 0, BT_INTEGER))
7150 return false;
7152 if (pos->ts.kind > gfc_default_integer_kind)
7154 gfc_error ("%qs argument of %qs intrinsic at %L must be of a kind "
7155 "not wider than the default kind (%d)",
7156 gfc_current_intrinsic_arg[0]->name, gfc_current_intrinsic,
7157 &pos->where, gfc_default_integer_kind);
7158 return false;
7161 if (!type_check (value, 1, BT_CHARACTER))
7162 return false;
7163 if (!kind_value_check (value, 1, gfc_default_character_kind))
7164 return false;
7166 return true;
7170 bool
7171 gfc_check_getlog (gfc_expr *msg)
7173 if (!type_check (msg, 0, BT_CHARACTER))
7174 return false;
7175 if (!kind_value_check (msg, 0, gfc_default_character_kind))
7176 return false;
7178 return true;
7182 bool
7183 gfc_check_exit (gfc_expr *status)
7185 if (status == NULL)
7186 return true;
7188 if (!type_check (status, 0, BT_INTEGER))
7189 return false;
7191 if (!scalar_check (status, 0))
7192 return false;
7194 return true;
7198 bool
7199 gfc_check_flush (gfc_expr *unit)
7201 if (unit == NULL)
7202 return true;
7204 if (!type_check (unit, 0, BT_INTEGER))
7205 return false;
7207 if (!scalar_check (unit, 0))
7208 return false;
7210 return true;
7214 bool
7215 gfc_check_free (gfc_expr *i)
7217 if (!type_check (i, 0, BT_INTEGER))
7218 return false;
7220 if (!scalar_check (i, 0))
7221 return false;
7223 return true;
7227 bool
7228 gfc_check_hostnm (gfc_expr *name)
7230 if (!type_check (name, 0, BT_CHARACTER))
7231 return false;
7232 if (!kind_value_check (name, 0, gfc_default_character_kind))
7233 return false;
7235 return true;
7239 bool
7240 gfc_check_hostnm_sub (gfc_expr *name, gfc_expr *status)
7242 if (!type_check (name, 0, BT_CHARACTER))
7243 return false;
7244 if (!kind_value_check (name, 0, gfc_default_character_kind))
7245 return false;
7247 if (status == NULL)
7248 return true;
7250 if (!scalar_check (status, 1))
7251 return false;
7253 if (!type_check (status, 1, BT_INTEGER))
7254 return false;
7256 return true;
7260 bool
7261 gfc_check_itime_idate (gfc_expr *values)
7263 if (!array_check (values, 0))
7264 return false;
7266 if (!rank_check (values, 0, 1))
7267 return false;
7269 if (!variable_check (values, 0, false))
7270 return false;
7272 if (!type_check (values, 0, BT_INTEGER))
7273 return false;
7275 if (!kind_value_check (values, 0, gfc_default_integer_kind))
7276 return false;
7278 return true;
7282 bool
7283 gfc_check_ltime_gmtime (gfc_expr *time, gfc_expr *values)
7285 if (!type_check (time, 0, BT_INTEGER))
7286 return false;
7288 if (!kind_value_check (time, 0, gfc_default_integer_kind))
7289 return false;
7291 if (!scalar_check (time, 0))
7292 return false;
7294 if (!array_check (values, 1))
7295 return false;
7297 if (!rank_check (values, 1, 1))
7298 return false;
7300 if (!variable_check (values, 1, false))
7301 return false;
7303 if (!type_check (values, 1, BT_INTEGER))
7304 return false;
7306 if (!kind_value_check (values, 1, gfc_default_integer_kind))
7307 return false;
7309 return true;
7313 bool
7314 gfc_check_ttynam_sub (gfc_expr *unit, gfc_expr *name)
7316 if (!scalar_check (unit, 0))
7317 return false;
7319 if (!type_check (unit, 0, BT_INTEGER))
7320 return false;
7322 if (!type_check (name, 1, BT_CHARACTER))
7323 return false;
7324 if (!kind_value_check (name, 1, gfc_default_character_kind))
7325 return false;
7327 return true;
7331 bool
7332 gfc_check_is_contiguous (gfc_expr *array)
7334 if (array->expr_type == EXPR_NULL)
7336 gfc_error ("Actual argument at %L of %qs intrinsic shall be an "
7337 "associated pointer", &array->where, gfc_current_intrinsic);
7338 return false;
7341 if (!array_check (array, 0))
7342 return false;
7344 return true;
7348 bool
7349 gfc_check_isatty (gfc_expr *unit)
7351 if (unit == NULL)
7352 return false;
7354 if (!type_check (unit, 0, BT_INTEGER))
7355 return false;
7357 if (!scalar_check (unit, 0))
7358 return false;
7360 return true;
7364 bool
7365 gfc_check_isnan (gfc_expr *x)
7367 if (!type_check (x, 0, BT_REAL))
7368 return false;
7370 return true;
7374 bool
7375 gfc_check_perror (gfc_expr *string)
7377 if (!type_check (string, 0, BT_CHARACTER))
7378 return false;
7379 if (!kind_value_check (string, 0, gfc_default_character_kind))
7380 return false;
7382 return true;
7386 bool
7387 gfc_check_umask (gfc_expr *mask)
7389 if (!type_check (mask, 0, BT_INTEGER))
7390 return false;
7392 if (!scalar_check (mask, 0))
7393 return false;
7395 return true;
7399 bool
7400 gfc_check_umask_sub (gfc_expr *mask, gfc_expr *old)
7402 if (!type_check (mask, 0, BT_INTEGER))
7403 return false;
7405 if (!scalar_check (mask, 0))
7406 return false;
7408 if (old == NULL)
7409 return true;
7411 if (!scalar_check (old, 1))
7412 return false;
7414 if (!type_check (old, 1, BT_INTEGER))
7415 return false;
7417 return true;
7421 bool
7422 gfc_check_unlink (gfc_expr *name)
7424 if (!type_check (name, 0, BT_CHARACTER))
7425 return false;
7426 if (!kind_value_check (name, 0, gfc_default_character_kind))
7427 return false;
7429 return true;
7433 bool
7434 gfc_check_unlink_sub (gfc_expr *name, gfc_expr *status)
7436 if (!type_check (name, 0, BT_CHARACTER))
7437 return false;
7438 if (!kind_value_check (name, 0, gfc_default_character_kind))
7439 return false;
7441 if (status == NULL)
7442 return true;
7444 if (!scalar_check (status, 1))
7445 return false;
7447 if (!type_check (status, 1, BT_INTEGER))
7448 return false;
7450 return true;
7454 bool
7455 gfc_check_signal (gfc_expr *number, gfc_expr *handler)
7457 if (!scalar_check (number, 0))
7458 return false;
7459 if (!type_check (number, 0, BT_INTEGER))
7460 return false;
7462 if (!int_or_proc_check (handler, 1))
7463 return false;
7464 if (handler->ts.type == BT_INTEGER && !scalar_check (handler, 1))
7465 return false;
7467 return true;
7471 bool
7472 gfc_check_signal_sub (gfc_expr *number, gfc_expr *handler, gfc_expr *status)
7474 if (!scalar_check (number, 0))
7475 return false;
7476 if (!type_check (number, 0, BT_INTEGER))
7477 return false;
7479 if (!int_or_proc_check (handler, 1))
7480 return false;
7481 if (handler->ts.type == BT_INTEGER && !scalar_check (handler, 1))
7482 return false;
7484 if (status == NULL)
7485 return true;
7487 if (!type_check (status, 2, BT_INTEGER))
7488 return false;
7489 if (!scalar_check (status, 2))
7490 return false;
7492 return true;
7496 bool
7497 gfc_check_system_sub (gfc_expr *cmd, gfc_expr *status)
7499 if (!type_check (cmd, 0, BT_CHARACTER))
7500 return false;
7501 if (!kind_value_check (cmd, 0, gfc_default_character_kind))
7502 return false;
7504 if (!scalar_check (status, 1))
7505 return false;
7507 if (!type_check (status, 1, BT_INTEGER))
7508 return false;
7510 if (!kind_value_check (status, 1, gfc_default_integer_kind))
7511 return false;
7513 return true;
7517 /* This is used for the GNU intrinsics AND, OR and XOR. */
7518 bool
7519 gfc_check_and (gfc_expr *i, gfc_expr *j)
7521 if (i->ts.type != BT_INTEGER
7522 && i->ts.type != BT_LOGICAL
7523 && i->ts.type != BT_BOZ)
7525 gfc_error ("%qs argument of %qs intrinsic at %L must be INTEGER, "
7526 "LOGICAL, or a BOZ literal constant",
7527 gfc_current_intrinsic_arg[0]->name,
7528 gfc_current_intrinsic, &i->where);
7529 return false;
7532 if (j->ts.type != BT_INTEGER
7533 && j->ts.type != BT_LOGICAL
7534 && j->ts.type != BT_BOZ)
7536 gfc_error ("%qs argument of %qs intrinsic at %L must be INTEGER, "
7537 "LOGICAL, or a BOZ literal constant",
7538 gfc_current_intrinsic_arg[1]->name,
7539 gfc_current_intrinsic, &j->where);
7540 return false;
7543 /* i and j cannot both be BOZ literal constants. */
7544 if (!boz_args_check (i, j))
7545 return false;
7547 /* If i is BOZ and j is integer, convert i to type of j. */
7548 if (i->ts.type == BT_BOZ)
7550 if (j->ts.type != BT_INTEGER)
7552 gfc_error ("%qs argument of %qs intrinsic at %L must be INTEGER",
7553 gfc_current_intrinsic_arg[1]->name,
7554 gfc_current_intrinsic, &j->where);
7555 reset_boz (i);
7556 return false;
7558 if (!gfc_boz2int (i, j->ts.kind))
7559 return false;
7562 /* If j is BOZ and i is integer, convert j to type of i. */
7563 if (j->ts.type == BT_BOZ)
7565 if (i->ts.type != BT_INTEGER)
7567 gfc_error ("%qs argument of %qs intrinsic at %L must be INTEGER",
7568 gfc_current_intrinsic_arg[0]->name,
7569 gfc_current_intrinsic, &j->where);
7570 reset_boz (j);
7571 return false;
7573 if (!gfc_boz2int (j, i->ts.kind))
7574 return false;
7577 if (!same_type_check (i, 0, j, 1, false))
7578 return false;
7580 if (!scalar_check (i, 0))
7581 return false;
7583 if (!scalar_check (j, 1))
7584 return false;
7586 return true;
7590 bool
7591 gfc_check_storage_size (gfc_expr *a, gfc_expr *kind)
7594 if (a->expr_type == EXPR_NULL)
7596 gfc_error ("Intrinsic function NULL at %L cannot be an actual "
7597 "argument to STORAGE_SIZE, because it returns a "
7598 "disassociated pointer", &a->where);
7599 return false;
7602 if (a->ts.type == BT_ASSUMED)
7604 gfc_error ("%qs argument of %qs intrinsic at %L shall not be TYPE(*)",
7605 gfc_current_intrinsic_arg[0]->name, gfc_current_intrinsic,
7606 &a->where);
7607 return false;
7610 if (a->ts.type == BT_PROCEDURE)
7612 gfc_error ("%qs argument of %qs intrinsic at %L shall not be a "
7613 "procedure", gfc_current_intrinsic_arg[0]->name,
7614 gfc_current_intrinsic, &a->where);
7615 return false;
7618 if (a->ts.type == BT_BOZ && illegal_boz_arg (a))
7619 return false;
7621 if (kind == NULL)
7622 return true;
7624 if (!type_check (kind, 1, BT_INTEGER))
7625 return false;
7627 if (!scalar_check (kind, 1))
7628 return false;
7630 if (kind->expr_type != EXPR_CONSTANT)
7632 gfc_error ("%qs argument of %qs intrinsic at %L must be a constant",
7633 gfc_current_intrinsic_arg[1]->name, gfc_current_intrinsic,
7634 &kind->where);
7635 return false;
7638 return true;