Don't warn when alignment of global common data exceeds maximum alignment.
[official-gcc.git] / gcc / fortran / check.c
blob851af1b30dc18cad6f8074130ff13395a73a04a9
1 /* Check functions
2 Copyright (C) 2002-2021 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.c(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 precedures 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))
1016 gfc_ref *ref;
1017 bool pointer = e->symtree->n.sym->ts.type == BT_CLASS
1018 && CLASS_DATA (e->symtree->n.sym)
1019 ? CLASS_DATA (e->symtree->n.sym)->attr.class_pointer
1020 : e->symtree->n.sym->attr.pointer;
1022 for (ref = e->ref; ref; ref = ref->next)
1024 if (pointer && ref->type == REF_COMPONENT)
1025 break;
1026 if (ref->type == REF_COMPONENT
1027 && ((ref->u.c.component->ts.type == BT_CLASS
1028 && CLASS_DATA (ref->u.c.component)->attr.class_pointer)
1029 || (ref->u.c.component->ts.type != BT_CLASS
1030 && ref->u.c.component->attr.pointer)))
1031 break;
1034 if (!ref)
1036 gfc_error ("%qs argument of %qs intrinsic at %L cannot be "
1037 "INTENT(IN)", gfc_current_intrinsic_arg[n]->name,
1038 gfc_current_intrinsic, &e->where);
1039 return false;
1043 if (e->expr_type == EXPR_VARIABLE
1044 && e->symtree->n.sym->attr.flavor != FL_PARAMETER
1045 && (allow_proc || !e->symtree->n.sym->attr.function))
1046 return true;
1048 if (e->expr_type == EXPR_VARIABLE && e->symtree->n.sym->attr.function
1049 && e->symtree->n.sym == e->symtree->n.sym->result)
1051 gfc_namespace *ns;
1052 for (ns = gfc_current_ns; ns; ns = ns->parent)
1053 if (ns->proc_name == e->symtree->n.sym)
1054 return true;
1057 /* F2018:R902: function reference having a data pointer result. */
1058 if (e->expr_type == EXPR_FUNCTION
1059 && e->symtree->n.sym->attr.flavor == FL_PROCEDURE
1060 && e->symtree->n.sym->attr.function
1061 && e->symtree->n.sym->attr.pointer)
1062 return true;
1064 gfc_error ("%qs argument of %qs intrinsic at %L must be a variable",
1065 gfc_current_intrinsic_arg[n]->name, gfc_current_intrinsic, &e->where);
1067 return false;
1071 /* Check the common DIM parameter for correctness. */
1073 static bool
1074 dim_check (gfc_expr *dim, int n, bool optional)
1076 if (dim == NULL)
1077 return true;
1079 if (!type_check (dim, n, BT_INTEGER))
1080 return false;
1082 if (!scalar_check (dim, n))
1083 return false;
1085 if (!optional && !nonoptional_check (dim, n))
1086 return false;
1088 return true;
1092 /* If a coarray DIM parameter is a constant, make sure that it is greater than
1093 zero and less than or equal to the corank of the given array. */
1095 static bool
1096 dim_corank_check (gfc_expr *dim, gfc_expr *array)
1098 int corank;
1100 gcc_assert (array->expr_type == EXPR_VARIABLE);
1102 if (dim->expr_type != EXPR_CONSTANT)
1103 return true;
1105 if (array->ts.type == BT_CLASS)
1106 return true;
1108 corank = gfc_get_corank (array);
1110 if (mpz_cmp_ui (dim->value.integer, 1) < 0
1111 || mpz_cmp_ui (dim->value.integer, corank) > 0)
1113 gfc_error ("%<dim%> argument of %qs intrinsic at %L is not a valid "
1114 "codimension index", gfc_current_intrinsic, &dim->where);
1116 return false;
1119 return true;
1123 /* If a DIM parameter is a constant, make sure that it is greater than
1124 zero and less than or equal to the rank of the given array. If
1125 allow_assumed is zero then dim must be less than the rank of the array
1126 for assumed size arrays. */
1128 static bool
1129 dim_rank_check (gfc_expr *dim, gfc_expr *array, int allow_assumed)
1131 gfc_array_ref *ar;
1132 int rank;
1134 if (dim == NULL)
1135 return true;
1137 if (dim->expr_type != EXPR_CONSTANT)
1138 return true;
1140 if (array->expr_type == EXPR_FUNCTION && array->value.function.isym
1141 && array->value.function.isym->id == GFC_ISYM_SPREAD)
1142 rank = array->rank + 1;
1143 else
1144 rank = array->rank;
1146 /* Assumed-rank array. */
1147 if (rank == -1)
1148 rank = GFC_MAX_DIMENSIONS;
1150 if (array->expr_type == EXPR_VARIABLE)
1152 ar = gfc_find_array_ref (array, true);
1153 if (!ar)
1154 return false;
1155 if (ar->as->type == AS_ASSUMED_SIZE
1156 && !allow_assumed
1157 && ar->type != AR_ELEMENT
1158 && ar->type != AR_SECTION)
1159 rank--;
1162 if (mpz_cmp_ui (dim->value.integer, 1) < 0
1163 || mpz_cmp_ui (dim->value.integer, rank) > 0)
1165 gfc_error ("%<dim%> argument of %qs intrinsic at %L is not a valid "
1166 "dimension index", gfc_current_intrinsic, &dim->where);
1168 return false;
1171 return true;
1175 /* Compare the size of a along dimension ai with the size of b along
1176 dimension bi, returning 0 if they are known not to be identical,
1177 and 1 if they are identical, or if this cannot be determined. */
1179 static int
1180 identical_dimen_shape (gfc_expr *a, int ai, gfc_expr *b, int bi)
1182 mpz_t a_size, b_size;
1183 int ret;
1185 gcc_assert (a->rank > ai);
1186 gcc_assert (b->rank > bi);
1188 ret = 1;
1190 if (gfc_array_dimen_size (a, ai, &a_size))
1192 if (gfc_array_dimen_size (b, bi, &b_size))
1194 if (mpz_cmp (a_size, b_size) != 0)
1195 ret = 0;
1197 mpz_clear (b_size);
1199 mpz_clear (a_size);
1201 return ret;
1204 /* Calculate the length of a character variable, including substrings.
1205 Strip away parentheses if necessary. Return -1 if no length could
1206 be determined. */
1208 static long
1209 gfc_var_strlen (const gfc_expr *a)
1211 gfc_ref *ra;
1213 while (a->expr_type == EXPR_OP && a->value.op.op == INTRINSIC_PARENTHESES)
1214 a = a->value.op.op1;
1216 for (ra = a->ref; ra != NULL && ra->type != REF_SUBSTRING; ra = ra->next)
1219 if (ra)
1221 long start_a, end_a;
1223 if (!ra->u.ss.end)
1224 return -1;
1226 if ((!ra->u.ss.start || ra->u.ss.start->expr_type == EXPR_CONSTANT)
1227 && ra->u.ss.end->expr_type == EXPR_CONSTANT)
1229 start_a = ra->u.ss.start ? mpz_get_si (ra->u.ss.start->value.integer)
1230 : 1;
1231 end_a = mpz_get_si (ra->u.ss.end->value.integer);
1232 return (end_a < start_a) ? 0 : end_a - start_a + 1;
1234 else if (ra->u.ss.start
1235 && gfc_dep_compare_expr (ra->u.ss.start, ra->u.ss.end) == 0)
1236 return 1;
1237 else
1238 return -1;
1241 if (a->ts.u.cl && a->ts.u.cl->length
1242 && a->ts.u.cl->length->expr_type == EXPR_CONSTANT)
1243 return mpz_get_si (a->ts.u.cl->length->value.integer);
1244 else if (a->expr_type == EXPR_CONSTANT
1245 && (a->ts.u.cl == NULL || a->ts.u.cl->length == NULL))
1246 return a->value.character.length;
1247 else
1248 return -1;
1252 /* Check whether two character expressions have the same length;
1253 returns true if they have or if the length cannot be determined,
1254 otherwise return false and raise a gfc_error. */
1256 bool
1257 gfc_check_same_strlen (const gfc_expr *a, const gfc_expr *b, const char *name)
1259 long len_a, len_b;
1261 len_a = gfc_var_strlen(a);
1262 len_b = gfc_var_strlen(b);
1264 if (len_a == -1 || len_b == -1 || len_a == len_b)
1265 return true;
1266 else
1268 gfc_error ("Unequal character lengths (%ld/%ld) in %s at %L",
1269 len_a, len_b, name, &a->where);
1270 return false;
1275 /***** Check functions *****/
1277 /* Check subroutine suitable for intrinsics taking a real argument and
1278 a kind argument for the result. */
1280 static bool
1281 check_a_kind (gfc_expr *a, gfc_expr *kind, bt type)
1283 if (!type_check (a, 0, BT_REAL))
1284 return false;
1285 if (!kind_check (kind, 1, type))
1286 return false;
1288 return true;
1292 /* Check subroutine suitable for ceiling, floor and nint. */
1294 bool
1295 gfc_check_a_ikind (gfc_expr *a, gfc_expr *kind)
1297 return check_a_kind (a, kind, BT_INTEGER);
1301 /* Check subroutine suitable for aint, anint. */
1303 bool
1304 gfc_check_a_xkind (gfc_expr *a, gfc_expr *kind)
1306 return check_a_kind (a, kind, BT_REAL);
1310 bool
1311 gfc_check_abs (gfc_expr *a)
1313 if (!numeric_check (a, 0))
1314 return false;
1316 return true;
1320 bool
1321 gfc_check_achar (gfc_expr *a, gfc_expr *kind)
1323 if (a->ts.type == BT_BOZ)
1325 if (gfc_invalid_boz (G_("BOZ literal constant at %L cannot appear in "
1326 "ACHAR intrinsic subprogram"), &a->where))
1327 return false;
1329 if (!gfc_boz2int (a, gfc_default_integer_kind))
1330 return false;
1333 if (!type_check (a, 0, BT_INTEGER))
1334 return false;
1336 if (!kind_check (kind, 1, BT_CHARACTER))
1337 return false;
1339 return true;
1343 bool
1344 gfc_check_access_func (gfc_expr *name, gfc_expr *mode)
1346 if (!type_check (name, 0, BT_CHARACTER)
1347 || !scalar_check (name, 0))
1348 return false;
1349 if (!kind_value_check (name, 0, gfc_default_character_kind))
1350 return false;
1352 if (!type_check (mode, 1, BT_CHARACTER)
1353 || !scalar_check (mode, 1))
1354 return false;
1355 if (!kind_value_check (mode, 1, gfc_default_character_kind))
1356 return false;
1358 return true;
1362 bool
1363 gfc_check_all_any (gfc_expr *mask, gfc_expr *dim)
1365 if (!logical_array_check (mask, 0))
1366 return false;
1368 if (!dim_check (dim, 1, false))
1369 return false;
1371 if (!dim_rank_check (dim, mask, 0))
1372 return false;
1374 return true;
1378 /* Limited checking for ALLOCATED intrinsic. Additional checking
1379 is performed in intrinsic.c(sort_actual), because ALLOCATED
1380 has two mutually exclusive non-optional arguments. */
1382 bool
1383 gfc_check_allocated (gfc_expr *array)
1385 /* Tests on allocated components of coarrays need to detour the check to
1386 argument of the _caf_get. */
1387 if (flag_coarray == GFC_FCOARRAY_LIB && array->expr_type == EXPR_FUNCTION
1388 && array->value.function.isym
1389 && array->value.function.isym->id == GFC_ISYM_CAF_GET)
1391 array = array->value.function.actual->expr;
1392 if (!array->ref)
1393 return false;
1396 if (!variable_check (array, 0, false))
1397 return false;
1398 if (!allocatable_check (array, 0))
1399 return false;
1401 return true;
1405 /* Common check function where the first argument must be real or
1406 integer and the second argument must be the same as the first. */
1408 bool
1409 gfc_check_a_p (gfc_expr *a, gfc_expr *p)
1411 if (!int_or_real_check (a, 0))
1412 return false;
1414 if (a->ts.type != p->ts.type)
1416 gfc_error ("%qs and %qs arguments of %qs intrinsic at %L must "
1417 "have the same type", gfc_current_intrinsic_arg[0]->name,
1418 gfc_current_intrinsic_arg[1]->name, gfc_current_intrinsic,
1419 &p->where);
1420 return false;
1423 if (a->ts.kind != p->ts.kind)
1425 if (!gfc_notify_std (GFC_STD_GNU, "Different type kinds at %L",
1426 &p->where))
1427 return false;
1430 return true;
1434 bool
1435 gfc_check_x_yd (gfc_expr *x, gfc_expr *y)
1437 if (!double_check (x, 0) || !double_check (y, 1))
1438 return false;
1440 return true;
1443 bool
1444 gfc_invalid_null_arg (gfc_expr *x)
1446 if (x->expr_type == EXPR_NULL)
1448 gfc_error ("NULL at %L is not permitted as actual argument "
1449 "to %qs intrinsic function", &x->where,
1450 gfc_current_intrinsic);
1451 return true;
1453 return false;
1456 bool
1457 gfc_check_associated (gfc_expr *pointer, gfc_expr *target)
1459 symbol_attribute attr1, attr2;
1460 int i;
1461 bool t;
1463 if (gfc_invalid_null_arg (pointer))
1464 return false;
1466 attr1 = gfc_expr_attr (pointer);
1468 if (!attr1.pointer && !attr1.proc_pointer)
1470 gfc_error ("%qs argument of %qs intrinsic at %L must be a POINTER",
1471 gfc_current_intrinsic_arg[0]->name, gfc_current_intrinsic,
1472 &pointer->where);
1473 return false;
1476 /* F2008, C1242. */
1477 if (attr1.pointer && gfc_is_coindexed (pointer))
1479 gfc_error ("%qs argument of %qs intrinsic at %L shall not be "
1480 "coindexed", gfc_current_intrinsic_arg[0]->name,
1481 gfc_current_intrinsic, &pointer->where);
1482 return false;
1485 /* Target argument is optional. */
1486 if (target == NULL)
1487 return true;
1489 if (gfc_invalid_null_arg (target))
1490 return false;
1492 if (target->expr_type == EXPR_VARIABLE || target->expr_type == EXPR_FUNCTION)
1493 attr2 = gfc_expr_attr (target);
1494 else
1496 gfc_error ("%qs argument of %qs intrinsic at %L must be a pointer "
1497 "or target VARIABLE or FUNCTION",
1498 gfc_current_intrinsic_arg[1]->name, gfc_current_intrinsic,
1499 &target->where);
1500 return false;
1503 if (attr1.pointer && !attr2.pointer && !attr2.target)
1505 gfc_error ("%qs argument of %qs intrinsic at %L must be a POINTER "
1506 "or a TARGET", gfc_current_intrinsic_arg[1]->name,
1507 gfc_current_intrinsic, &target->where);
1508 return false;
1511 /* F2008, C1242. */
1512 if (attr1.pointer && gfc_is_coindexed (target))
1514 gfc_error ("%qs argument of %qs intrinsic at %L shall not be "
1515 "coindexed", gfc_current_intrinsic_arg[1]->name,
1516 gfc_current_intrinsic, &target->where);
1517 return false;
1520 t = true;
1521 if (!same_type_check (pointer, 0, target, 1, true))
1522 t = false;
1523 if (!rank_check (target, 0, pointer->rank))
1524 t = false;
1525 if (target->rank > 0)
1527 for (i = 0; i < target->rank; i++)
1528 if (target->ref->u.ar.dimen_type[i] == DIMEN_VECTOR)
1530 gfc_error ("Array section with a vector subscript at %L shall not "
1531 "be the target of a pointer",
1532 &target->where);
1533 t = false;
1534 break;
1537 return t;
1541 bool
1542 gfc_check_atan_2 (gfc_expr *y, gfc_expr *x)
1544 /* gfc_notify_std would be a waste of time as the return value
1545 is seemingly used only for the generic resolution. The error
1546 will be: Too many arguments. */
1547 if ((gfc_option.allow_std & GFC_STD_F2008) == 0)
1548 return false;
1550 return gfc_check_atan2 (y, x);
1554 bool
1555 gfc_check_atan2 (gfc_expr *y, gfc_expr *x)
1557 if (!type_check (y, 0, BT_REAL))
1558 return false;
1559 if (!same_type_check (y, 0, x, 1))
1560 return false;
1562 return true;
1566 static bool
1567 gfc_check_atomic (gfc_expr *atom, int atom_no, gfc_expr *value, int val_no,
1568 gfc_expr *stat, int stat_no)
1570 if (!scalar_check (atom, atom_no) || !scalar_check (value, val_no))
1571 return false;
1573 if (!(atom->ts.type == BT_INTEGER && atom->ts.kind == gfc_atomic_int_kind)
1574 && !(atom->ts.type == BT_LOGICAL
1575 && atom->ts.kind == gfc_atomic_logical_kind))
1577 gfc_error ("ATOM argument at %L to intrinsic function %s shall be an "
1578 "integer of ATOMIC_INT_KIND or a logical of "
1579 "ATOMIC_LOGICAL_KIND", &atom->where, gfc_current_intrinsic);
1580 return false;
1583 if (!gfc_is_coarray (atom) && !gfc_is_coindexed (atom))
1585 gfc_error ("ATOM argument at %L of the %s intrinsic function shall be a "
1586 "coarray or coindexed", &atom->where, gfc_current_intrinsic);
1587 return false;
1590 if (atom->ts.type != value->ts.type)
1592 gfc_error ("%qs argument of %qs intrinsic at %L shall have the same "
1593 "type as %qs at %L", gfc_current_intrinsic_arg[val_no]->name,
1594 gfc_current_intrinsic, &value->where,
1595 gfc_current_intrinsic_arg[atom_no]->name, &atom->where);
1596 return false;
1599 if (stat != NULL)
1601 if (!type_check (stat, stat_no, BT_INTEGER))
1602 return false;
1603 if (!scalar_check (stat, stat_no))
1604 return false;
1605 if (!variable_check (stat, stat_no, false))
1606 return false;
1607 if (!kind_value_check (stat, stat_no, gfc_default_integer_kind))
1608 return false;
1610 if (!gfc_notify_std (GFC_STD_F2018, "STAT= argument to %s at %L",
1611 gfc_current_intrinsic, &stat->where))
1612 return false;
1615 return true;
1619 bool
1620 gfc_check_atomic_def (gfc_expr *atom, gfc_expr *value, gfc_expr *stat)
1622 if (atom->expr_type == EXPR_FUNCTION
1623 && atom->value.function.isym
1624 && atom->value.function.isym->id == GFC_ISYM_CAF_GET)
1625 atom = atom->value.function.actual->expr;
1627 if (!gfc_check_vardef_context (atom, false, false, false, NULL))
1629 gfc_error ("ATOM argument of the %s intrinsic function at %L shall be "
1630 "definable", gfc_current_intrinsic, &atom->where);
1631 return false;
1634 return gfc_check_atomic (atom, 0, value, 1, stat, 2);
1638 bool
1639 gfc_check_atomic_op (gfc_expr *atom, gfc_expr *value, gfc_expr *stat)
1641 if (atom->ts.type != BT_INTEGER || atom->ts.kind != gfc_atomic_int_kind)
1643 gfc_error ("ATOM argument at %L to intrinsic function %s shall be an "
1644 "integer of ATOMIC_INT_KIND", &atom->where,
1645 gfc_current_intrinsic);
1646 return false;
1649 return gfc_check_atomic_def (atom, value, stat);
1653 bool
1654 gfc_check_atomic_ref (gfc_expr *value, gfc_expr *atom, gfc_expr *stat)
1656 if (atom->expr_type == EXPR_FUNCTION
1657 && atom->value.function.isym
1658 && atom->value.function.isym->id == GFC_ISYM_CAF_GET)
1659 atom = atom->value.function.actual->expr;
1661 if (!gfc_check_vardef_context (value, false, false, false, NULL))
1663 gfc_error ("VALUE argument of the %s intrinsic function at %L shall be "
1664 "definable", gfc_current_intrinsic, &value->where);
1665 return false;
1668 return gfc_check_atomic (atom, 1, value, 0, stat, 2);
1672 bool
1673 gfc_check_image_status (gfc_expr *image, gfc_expr *team)
1675 /* IMAGE has to be a positive, scalar integer. */
1676 if (!type_check (image, 0, BT_INTEGER) || !scalar_check (image, 0)
1677 || !positive_check (0, image))
1678 return false;
1680 if (team)
1682 gfc_error ("%qs argument of %qs intrinsic at %L not yet supported",
1683 gfc_current_intrinsic_arg[1]->name, gfc_current_intrinsic,
1684 &team->where);
1685 return false;
1687 return true;
1691 bool
1692 gfc_check_failed_or_stopped_images (gfc_expr *team, gfc_expr *kind)
1694 if (team)
1696 gfc_error ("%qs argument of %qs intrinsic at %L not yet supported",
1697 gfc_current_intrinsic_arg[0]->name, gfc_current_intrinsic,
1698 &team->where);
1699 return false;
1702 if (kind)
1704 int k;
1706 if (!type_check (kind, 1, BT_INTEGER) || !scalar_check (kind, 1)
1707 || !positive_check (1, kind))
1708 return false;
1710 /* Get the kind, reporting error on non-constant or overflow. */
1711 gfc_current_locus = kind->where;
1712 if (gfc_extract_int (kind, &k, 1))
1713 return false;
1714 if (gfc_validate_kind (BT_INTEGER, k, true) == -1)
1716 gfc_error ("%qs argument of %qs intrinsic at %L shall specify a "
1717 "valid integer kind", gfc_current_intrinsic_arg[1]->name,
1718 gfc_current_intrinsic, &kind->where);
1719 return false;
1722 return true;
1726 bool
1727 gfc_check_get_team (gfc_expr *level)
1729 if (level)
1731 gfc_error ("%qs argument of %qs intrinsic at %L not yet supported",
1732 gfc_current_intrinsic_arg[0]->name, gfc_current_intrinsic,
1733 &level->where);
1734 return false;
1736 return true;
1740 bool
1741 gfc_check_atomic_cas (gfc_expr *atom, gfc_expr *old, gfc_expr *compare,
1742 gfc_expr *new_val, gfc_expr *stat)
1744 if (atom->expr_type == EXPR_FUNCTION
1745 && atom->value.function.isym
1746 && atom->value.function.isym->id == GFC_ISYM_CAF_GET)
1747 atom = atom->value.function.actual->expr;
1749 if (!gfc_check_atomic (atom, 0, new_val, 3, stat, 4))
1750 return false;
1752 if (!scalar_check (old, 1) || !scalar_check (compare, 2))
1753 return false;
1755 if (!same_type_check (atom, 0, old, 1))
1756 return false;
1758 if (!same_type_check (atom, 0, compare, 2))
1759 return false;
1761 if (!gfc_check_vardef_context (atom, false, false, false, NULL))
1763 gfc_error ("ATOM argument of the %s intrinsic function at %L shall be "
1764 "definable", gfc_current_intrinsic, &atom->where);
1765 return false;
1768 if (!gfc_check_vardef_context (old, false, false, false, NULL))
1770 gfc_error ("OLD argument of the %s intrinsic function at %L shall be "
1771 "definable", gfc_current_intrinsic, &old->where);
1772 return false;
1775 return true;
1778 bool
1779 gfc_check_event_query (gfc_expr *event, gfc_expr *count, gfc_expr *stat)
1781 if (event->ts.type != BT_DERIVED
1782 || event->ts.u.derived->from_intmod != INTMOD_ISO_FORTRAN_ENV
1783 || event->ts.u.derived->intmod_sym_id != ISOFORTRAN_EVENT_TYPE)
1785 gfc_error ("EVENT argument at %L to the intrinsic EVENT_QUERY "
1786 "shall be of type EVENT_TYPE", &event->where);
1787 return false;
1790 if (!scalar_check (event, 0))
1791 return false;
1793 if (!gfc_check_vardef_context (count, false, false, false, NULL))
1795 gfc_error ("COUNT argument of the EVENT_QUERY intrinsic function at %L "
1796 "shall be definable", &count->where);
1797 return false;
1800 if (!type_check (count, 1, BT_INTEGER))
1801 return false;
1803 int i = gfc_validate_kind (BT_INTEGER, count->ts.kind, false);
1804 int j = gfc_validate_kind (BT_INTEGER, gfc_default_integer_kind, false);
1806 if (gfc_integer_kinds[i].range < gfc_integer_kinds[j].range)
1808 gfc_error ("COUNT argument of the EVENT_QUERY intrinsic function at %L "
1809 "shall have at least the range of the default integer",
1810 &count->where);
1811 return false;
1814 if (stat != NULL)
1816 if (!type_check (stat, 2, BT_INTEGER))
1817 return false;
1818 if (!scalar_check (stat, 2))
1819 return false;
1820 if (!variable_check (stat, 2, false))
1821 return false;
1823 if (!gfc_notify_std (GFC_STD_F2018, "STAT= argument to %s at %L",
1824 gfc_current_intrinsic, &stat->where))
1825 return false;
1828 return true;
1832 bool
1833 gfc_check_atomic_fetch_op (gfc_expr *atom, gfc_expr *value, gfc_expr *old,
1834 gfc_expr *stat)
1836 if (atom->expr_type == EXPR_FUNCTION
1837 && atom->value.function.isym
1838 && atom->value.function.isym->id == GFC_ISYM_CAF_GET)
1839 atom = atom->value.function.actual->expr;
1841 if (atom->ts.type != BT_INTEGER || atom->ts.kind != gfc_atomic_int_kind)
1843 gfc_error ("ATOM argument at %L to intrinsic function %s shall be an "
1844 "integer of ATOMIC_INT_KIND", &atom->where,
1845 gfc_current_intrinsic);
1846 return false;
1849 if (!gfc_check_atomic (atom, 0, value, 1, stat, 3))
1850 return false;
1852 if (!scalar_check (old, 2))
1853 return false;
1855 if (!same_type_check (atom, 0, old, 2))
1856 return false;
1858 if (!gfc_check_vardef_context (atom, false, false, false, NULL))
1860 gfc_error ("ATOM argument of the %s intrinsic function at %L shall be "
1861 "definable", gfc_current_intrinsic, &atom->where);
1862 return false;
1865 if (!gfc_check_vardef_context (old, false, false, false, NULL))
1867 gfc_error ("OLD argument of the %s intrinsic function at %L shall be "
1868 "definable", gfc_current_intrinsic, &old->where);
1869 return false;
1872 return true;
1876 /* BESJN and BESYN functions. */
1878 bool
1879 gfc_check_besn (gfc_expr *n, gfc_expr *x)
1881 if (!type_check (n, 0, BT_INTEGER))
1882 return false;
1883 if (n->expr_type == EXPR_CONSTANT)
1885 int i;
1886 gfc_extract_int (n, &i);
1887 if (i < 0 && !gfc_notify_std (GFC_STD_GNU, "Negative argument "
1888 "N at %L", &n->where))
1889 return false;
1892 if (!type_check (x, 1, BT_REAL))
1893 return false;
1895 return true;
1899 /* Transformational version of the Bessel JN and YN functions. */
1901 bool
1902 gfc_check_bessel_n2 (gfc_expr *n1, gfc_expr *n2, gfc_expr *x)
1904 if (!type_check (n1, 0, BT_INTEGER))
1905 return false;
1906 if (!scalar_check (n1, 0))
1907 return false;
1908 if (!nonnegative_check ("N1", n1))
1909 return false;
1911 if (!type_check (n2, 1, BT_INTEGER))
1912 return false;
1913 if (!scalar_check (n2, 1))
1914 return false;
1915 if (!nonnegative_check ("N2", n2))
1916 return false;
1918 if (!type_check (x, 2, BT_REAL))
1919 return false;
1920 if (!scalar_check (x, 2))
1921 return false;
1923 return true;
1927 bool
1928 gfc_check_bge_bgt_ble_blt (gfc_expr *i, gfc_expr *j)
1930 extern int gfc_max_integer_kind;
1932 /* If i and j are both BOZ, convert to widest INTEGER. */
1933 if (i->ts.type == BT_BOZ && j->ts.type == BT_BOZ)
1935 if (!gfc_boz2int (i, gfc_max_integer_kind))
1936 return false;
1937 if (!gfc_boz2int (j, gfc_max_integer_kind))
1938 return false;
1941 /* If i is BOZ and j is integer, convert i to type of j. */
1942 if (i->ts.type == BT_BOZ && j->ts.type == BT_INTEGER
1943 && !gfc_boz2int (i, j->ts.kind))
1944 return false;
1946 /* If j is BOZ and i is integer, convert j to type of i. */
1947 if (j->ts.type == BT_BOZ && i->ts.type == BT_INTEGER
1948 && !gfc_boz2int (j, i->ts.kind))
1949 return false;
1951 if (!type_check (i, 0, BT_INTEGER))
1952 return false;
1954 if (!type_check (j, 1, BT_INTEGER))
1955 return false;
1957 return true;
1961 bool
1962 gfc_check_bitfcn (gfc_expr *i, gfc_expr *pos)
1964 if (!type_check (i, 0, BT_INTEGER))
1965 return false;
1967 if (!type_check (pos, 1, BT_INTEGER))
1968 return false;
1970 if (!nonnegative_check ("pos", pos))
1971 return false;
1973 if (!less_than_bitsize1 ("i", i, "pos", pos, false))
1974 return false;
1976 return true;
1980 bool
1981 gfc_check_char (gfc_expr *i, gfc_expr *kind)
1983 if (i->ts.type == BT_BOZ)
1985 if (gfc_invalid_boz (G_("BOZ literal constant at %L cannot appear in "
1986 "CHAR intrinsic subprogram"), &i->where))
1987 return false;
1989 if (!gfc_boz2int (i, gfc_default_integer_kind))
1990 return false;
1993 if (!type_check (i, 0, BT_INTEGER))
1994 return false;
1996 if (!kind_check (kind, 1, BT_CHARACTER))
1997 return false;
1999 return true;
2003 bool
2004 gfc_check_chdir (gfc_expr *dir)
2006 if (!type_check (dir, 0, BT_CHARACTER))
2007 return false;
2008 if (!kind_value_check (dir, 0, gfc_default_character_kind))
2009 return false;
2011 return true;
2015 bool
2016 gfc_check_chdir_sub (gfc_expr *dir, gfc_expr *status)
2018 if (!type_check (dir, 0, BT_CHARACTER))
2019 return false;
2020 if (!kind_value_check (dir, 0, gfc_default_character_kind))
2021 return false;
2023 if (status == NULL)
2024 return true;
2026 if (!type_check (status, 1, BT_INTEGER))
2027 return false;
2028 if (!scalar_check (status, 1))
2029 return false;
2031 return true;
2035 bool
2036 gfc_check_chmod (gfc_expr *name, gfc_expr *mode)
2038 if (!type_check (name, 0, BT_CHARACTER))
2039 return false;
2040 if (!kind_value_check (name, 0, gfc_default_character_kind))
2041 return false;
2043 if (!type_check (mode, 1, BT_CHARACTER))
2044 return false;
2045 if (!kind_value_check (mode, 1, gfc_default_character_kind))
2046 return false;
2048 return true;
2052 bool
2053 gfc_check_chmod_sub (gfc_expr *name, gfc_expr *mode, gfc_expr *status)
2055 if (!type_check (name, 0, BT_CHARACTER))
2056 return false;
2057 if (!kind_value_check (name, 0, gfc_default_character_kind))
2058 return false;
2060 if (!type_check (mode, 1, BT_CHARACTER))
2061 return false;
2062 if (!kind_value_check (mode, 1, gfc_default_character_kind))
2063 return false;
2065 if (status == NULL)
2066 return true;
2068 if (!type_check (status, 2, BT_INTEGER))
2069 return false;
2071 if (!scalar_check (status, 2))
2072 return false;
2074 return true;
2078 bool
2079 gfc_check_cmplx (gfc_expr *x, gfc_expr *y, gfc_expr *kind)
2081 int k;
2083 /* Check kind first, because it may be needed in conversion of a BOZ. */
2084 if (kind)
2086 if (!kind_check (kind, 2, BT_COMPLEX))
2087 return false;
2088 gfc_extract_int (kind, &k);
2090 else
2091 k = gfc_default_complex_kind;
2093 if (x->ts.type == BT_BOZ && !gfc_boz2real (x, k))
2094 return false;
2096 if (!numeric_check (x, 0))
2097 return false;
2099 if (y != NULL)
2101 if (y->ts.type == BT_BOZ && !gfc_boz2real (y, k))
2102 return false;
2104 if (!numeric_check (y, 1))
2105 return false;
2107 if (x->ts.type == BT_COMPLEX)
2109 gfc_error ("%qs argument of %qs intrinsic at %L must not be "
2110 "present if %<x%> is COMPLEX",
2111 gfc_current_intrinsic_arg[1]->name, gfc_current_intrinsic,
2112 &y->where);
2113 return false;
2116 if (y->ts.type == BT_COMPLEX)
2118 gfc_error ("%qs argument of %qs intrinsic at %L must have a type "
2119 "of either REAL or INTEGER",
2120 gfc_current_intrinsic_arg[1]->name, gfc_current_intrinsic,
2121 &y->where);
2122 return false;
2126 if (!kind && warn_conversion
2127 && x->ts.type == BT_REAL && x->ts.kind > gfc_default_real_kind)
2128 gfc_warning_now (OPT_Wconversion, "Conversion from %s to default-kind "
2129 "COMPLEX(%d) at %L might lose precision, consider using "
2130 "the KIND argument", gfc_typename (&x->ts),
2131 gfc_default_real_kind, &x->where);
2132 else if (y && !kind && warn_conversion
2133 && y->ts.type == BT_REAL && y->ts.kind > gfc_default_real_kind)
2134 gfc_warning_now (OPT_Wconversion, "Conversion from %s to default-kind "
2135 "COMPLEX(%d) at %L might lose precision, consider using "
2136 "the KIND argument", gfc_typename (&y->ts),
2137 gfc_default_real_kind, &y->where);
2138 return true;
2142 static bool
2143 check_co_collective (gfc_expr *a, gfc_expr *image_idx, gfc_expr *stat,
2144 gfc_expr *errmsg, bool co_reduce)
2146 if (!variable_check (a, 0, false))
2147 return false;
2149 if (!gfc_check_vardef_context (a, false, false, false, "argument 'A' with "
2150 "INTENT(INOUT)"))
2151 return false;
2153 /* Fortran 2008, 12.5.2.4, paragraph 18. */
2154 if (gfc_has_vector_subscript (a))
2156 gfc_error ("Argument %<A%> with INTENT(INOUT) at %L of the intrinsic "
2157 "subroutine %s shall not have a vector subscript",
2158 &a->where, gfc_current_intrinsic);
2159 return false;
2162 if (gfc_is_coindexed (a))
2164 gfc_error ("The A argument at %L to the intrinsic %s shall not be "
2165 "coindexed", &a->where, gfc_current_intrinsic);
2166 return false;
2169 if (image_idx != NULL)
2171 if (!type_check (image_idx, co_reduce ? 2 : 1, BT_INTEGER))
2172 return false;
2173 if (!scalar_check (image_idx, co_reduce ? 2 : 1))
2174 return false;
2177 if (stat != NULL)
2179 if (!type_check (stat, co_reduce ? 3 : 2, BT_INTEGER))
2180 return false;
2181 if (!scalar_check (stat, co_reduce ? 3 : 2))
2182 return false;
2183 if (!variable_check (stat, co_reduce ? 3 : 2, false))
2184 return false;
2185 if (stat->ts.kind != 4)
2187 gfc_error ("The stat= argument at %L must be a kind=4 integer "
2188 "variable", &stat->where);
2189 return false;
2193 if (errmsg != NULL)
2195 if (!type_check (errmsg, co_reduce ? 4 : 3, BT_CHARACTER))
2196 return false;
2197 if (!scalar_check (errmsg, co_reduce ? 4 : 3))
2198 return false;
2199 if (!variable_check (errmsg, co_reduce ? 4 : 3, false))
2200 return false;
2201 if (errmsg->ts.kind != 1)
2203 gfc_error ("The errmsg= argument at %L must be a default-kind "
2204 "character variable", &errmsg->where);
2205 return false;
2209 if (flag_coarray == GFC_FCOARRAY_NONE)
2211 gfc_fatal_error ("Coarrays disabled at %L, use %<-fcoarray=%> to enable",
2212 &a->where);
2213 return false;
2216 return true;
2220 bool
2221 gfc_check_co_broadcast (gfc_expr *a, gfc_expr *source_image, gfc_expr *stat,
2222 gfc_expr *errmsg)
2224 if (a->ts.type == BT_CLASS || gfc_expr_attr (a).alloc_comp)
2226 gfc_error ("Support for the A argument at %L which is polymorphic A "
2227 "argument or has allocatable components is not yet "
2228 "implemented", &a->where);
2229 return false;
2231 return check_co_collective (a, source_image, stat, errmsg, false);
2235 bool
2236 gfc_check_co_reduce (gfc_expr *a, gfc_expr *op, gfc_expr *result_image,
2237 gfc_expr *stat, gfc_expr *errmsg)
2239 symbol_attribute attr;
2240 gfc_formal_arglist *formal;
2241 gfc_symbol *sym;
2243 if (a->ts.type == BT_CLASS)
2245 gfc_error ("The A argument at %L of CO_REDUCE shall not be polymorphic",
2246 &a->where);
2247 return false;
2250 if (gfc_expr_attr (a).alloc_comp)
2252 gfc_error ("Support for the A argument at %L with allocatable components"
2253 " is not yet implemented", &a->where);
2254 return false;
2257 if (!check_co_collective (a, result_image, stat, errmsg, true))
2258 return false;
2260 if (!gfc_resolve_expr (op))
2261 return false;
2263 attr = gfc_expr_attr (op);
2264 if (!attr.pure || !attr.function)
2266 gfc_error ("OPERATOR argument at %L must be a PURE function",
2267 &op->where);
2268 return false;
2271 if (attr.intrinsic)
2273 /* None of the intrinsics fulfills the criteria of taking two arguments,
2274 returning the same type and kind as the arguments and being permitted
2275 as actual argument. */
2276 gfc_error ("Intrinsic function %s at %L is not permitted for CO_REDUCE",
2277 op->symtree->n.sym->name, &op->where);
2278 return false;
2281 if (gfc_is_proc_ptr_comp (op))
2283 gfc_component *comp = gfc_get_proc_ptr_comp (op);
2284 sym = comp->ts.interface;
2286 else
2287 sym = op->symtree->n.sym;
2289 formal = sym->formal;
2291 if (!formal || !formal->next || formal->next->next)
2293 gfc_error ("The function passed as OPERATOR at %L shall have two "
2294 "arguments", &op->where);
2295 return false;
2298 if (sym->result->ts.type == BT_UNKNOWN)
2299 gfc_set_default_type (sym->result, 0, NULL);
2301 if (!gfc_compare_types (&a->ts, &sym->result->ts))
2303 gfc_error ("The A argument at %L has type %s but the function passed as "
2304 "OPERATOR at %L returns %s",
2305 &a->where, gfc_typename (a), &op->where,
2306 gfc_typename (&sym->result->ts));
2307 return false;
2309 if (!gfc_compare_types (&a->ts, &formal->sym->ts)
2310 || !gfc_compare_types (&a->ts, &formal->next->sym->ts))
2312 gfc_error ("The function passed as OPERATOR at %L has arguments of type "
2313 "%s and %s but shall have type %s", &op->where,
2314 gfc_typename (&formal->sym->ts),
2315 gfc_typename (&formal->next->sym->ts), gfc_typename (a));
2316 return false;
2318 if (op->rank || attr.allocatable || attr.pointer || formal->sym->as
2319 || formal->next->sym->as || formal->sym->attr.allocatable
2320 || formal->next->sym->attr.allocatable || formal->sym->attr.pointer
2321 || formal->next->sym->attr.pointer)
2323 gfc_error ("The function passed as OPERATOR at %L shall have scalar "
2324 "nonallocatable nonpointer arguments and return a "
2325 "nonallocatable nonpointer scalar", &op->where);
2326 return false;
2329 if (formal->sym->attr.value != formal->next->sym->attr.value)
2331 gfc_error ("The function passed as OPERATOR at %L shall have the VALUE "
2332 "attribute either for none or both arguments", &op->where);
2333 return false;
2336 if (formal->sym->attr.target != formal->next->sym->attr.target)
2338 gfc_error ("The function passed as OPERATOR at %L shall have the TARGET "
2339 "attribute either for none or both arguments", &op->where);
2340 return false;
2343 if (formal->sym->attr.asynchronous != formal->next->sym->attr.asynchronous)
2345 gfc_error ("The function passed as OPERATOR at %L shall have the "
2346 "ASYNCHRONOUS attribute either for none or both arguments",
2347 &op->where);
2348 return false;
2351 if (formal->sym->attr.optional || formal->next->sym->attr.optional)
2353 gfc_error ("The function passed as OPERATOR at %L shall not have the "
2354 "OPTIONAL attribute for either of the arguments", &op->where);
2355 return false;
2358 if (a->ts.type == BT_CHARACTER)
2360 gfc_charlen *cl;
2361 unsigned long actual_size, formal_size1, formal_size2, result_size;
2363 cl = a->ts.u.cl;
2364 actual_size = cl && cl->length && cl->length->expr_type == EXPR_CONSTANT
2365 ? mpz_get_ui (cl->length->value.integer) : 0;
2367 cl = formal->sym->ts.u.cl;
2368 formal_size1 = cl && cl->length && cl->length->expr_type == EXPR_CONSTANT
2369 ? mpz_get_ui (cl->length->value.integer) : 0;
2371 cl = formal->next->sym->ts.u.cl;
2372 formal_size2 = cl && cl->length && cl->length->expr_type == EXPR_CONSTANT
2373 ? mpz_get_ui (cl->length->value.integer) : 0;
2375 cl = sym->ts.u.cl;
2376 result_size = cl && cl->length && cl->length->expr_type == EXPR_CONSTANT
2377 ? mpz_get_ui (cl->length->value.integer) : 0;
2379 if (actual_size
2380 && ((formal_size1 && actual_size != formal_size1)
2381 || (formal_size2 && actual_size != formal_size2)))
2383 gfc_error ("The character length of the A argument at %L and of the "
2384 "arguments of the OPERATOR at %L shall be the same",
2385 &a->where, &op->where);
2386 return false;
2388 if (actual_size && result_size && actual_size != result_size)
2390 gfc_error ("The character length of the A argument at %L and of the "
2391 "function result of the OPERATOR at %L shall be the same",
2392 &a->where, &op->where);
2393 return false;
2397 return true;
2401 bool
2402 gfc_check_co_minmax (gfc_expr *a, gfc_expr *result_image, gfc_expr *stat,
2403 gfc_expr *errmsg)
2405 if (a->ts.type != BT_INTEGER && a->ts.type != BT_REAL
2406 && a->ts.type != BT_CHARACTER)
2408 gfc_error ("%qs argument of %qs intrinsic at %L shall be of type "
2409 "integer, real or character",
2410 gfc_current_intrinsic_arg[0]->name, gfc_current_intrinsic,
2411 &a->where);
2412 return false;
2414 return check_co_collective (a, result_image, stat, errmsg, false);
2418 bool
2419 gfc_check_co_sum (gfc_expr *a, gfc_expr *result_image, gfc_expr *stat,
2420 gfc_expr *errmsg)
2422 if (!numeric_check (a, 0))
2423 return false;
2424 return check_co_collective (a, result_image, stat, errmsg, false);
2428 bool
2429 gfc_check_complex (gfc_expr *x, gfc_expr *y)
2431 if (!boz_args_check (x, y))
2432 return false;
2434 if (x->ts.type == BT_BOZ)
2436 if (gfc_invalid_boz (G_("BOZ constant at %L cannot appear in the COMPLEX"
2437 " intrinsic subprogram"), &x->where))
2439 reset_boz (x);
2440 return false;
2442 if (y->ts.type == BT_INTEGER && !gfc_boz2int (x, y->ts.kind))
2443 return false;
2444 if (y->ts.type == BT_REAL && !gfc_boz2real (x, y->ts.kind))
2445 return false;
2448 if (y->ts.type == BT_BOZ)
2450 if (gfc_invalid_boz (G_("BOZ constant at %L cannot appear in the COMPLEX"
2451 " intrinsic subprogram"), &y->where))
2453 reset_boz (y);
2454 return false;
2456 if (x->ts.type == BT_INTEGER && !gfc_boz2int (y, x->ts.kind))
2457 return false;
2458 if (x->ts.type == BT_REAL && !gfc_boz2real (y, x->ts.kind))
2459 return false;
2462 if (!int_or_real_check (x, 0))
2463 return false;
2464 if (!scalar_check (x, 0))
2465 return false;
2467 if (!int_or_real_check (y, 1))
2468 return false;
2469 if (!scalar_check (y, 1))
2470 return false;
2472 return true;
2476 bool
2477 gfc_check_count (gfc_expr *mask, gfc_expr *dim, gfc_expr *kind)
2479 if (!logical_array_check (mask, 0))
2480 return false;
2481 if (!dim_check (dim, 1, false))
2482 return false;
2483 if (!dim_rank_check (dim, mask, 0))
2484 return false;
2485 if (!kind_check (kind, 2, BT_INTEGER))
2486 return false;
2487 if (kind && !gfc_notify_std (GFC_STD_F2003, "%qs intrinsic "
2488 "with KIND argument at %L",
2489 gfc_current_intrinsic, &kind->where))
2490 return false;
2492 return true;
2496 bool
2497 gfc_check_cshift (gfc_expr *array, gfc_expr *shift, gfc_expr *dim)
2499 if (!array_check (array, 0))
2500 return false;
2502 if (!type_check (shift, 1, BT_INTEGER))
2503 return false;
2505 if (!dim_check (dim, 2, true))
2506 return false;
2508 if (!dim_rank_check (dim, array, false))
2509 return false;
2511 if (array->rank == 1 || shift->rank == 0)
2513 if (!scalar_check (shift, 1))
2514 return false;
2516 else if (shift->rank == array->rank - 1)
2518 int d;
2519 if (!dim)
2520 d = 1;
2521 else if (dim->expr_type == EXPR_CONSTANT)
2522 gfc_extract_int (dim, &d);
2523 else
2524 d = -1;
2526 if (d > 0)
2528 int i, j;
2529 for (i = 0, j = 0; i < array->rank; i++)
2530 if (i != d - 1)
2532 if (!identical_dimen_shape (array, i, shift, j))
2534 gfc_error ("%qs argument of %qs intrinsic at %L has "
2535 "invalid shape in dimension %d (%ld/%ld)",
2536 gfc_current_intrinsic_arg[1]->name,
2537 gfc_current_intrinsic, &shift->where, i + 1,
2538 mpz_get_si (array->shape[i]),
2539 mpz_get_si (shift->shape[j]));
2540 return false;
2543 j += 1;
2547 else
2549 gfc_error ("%qs argument of intrinsic %qs at %L of must have rank "
2550 "%d or be a scalar", gfc_current_intrinsic_arg[1]->name,
2551 gfc_current_intrinsic, &shift->where, array->rank - 1);
2552 return false;
2555 return true;
2559 bool
2560 gfc_check_ctime (gfc_expr *time)
2562 if (!scalar_check (time, 0))
2563 return false;
2565 if (!type_check (time, 0, BT_INTEGER))
2566 return false;
2568 return true;
2572 bool gfc_check_datan2 (gfc_expr *y, gfc_expr *x)
2574 if (!double_check (y, 0) || !double_check (x, 1))
2575 return false;
2577 return true;
2580 bool
2581 gfc_check_dcmplx (gfc_expr *x, gfc_expr *y)
2583 if (x->ts.type == BT_BOZ && !gfc_boz2real (x, gfc_default_double_kind))
2584 return false;
2586 if (!numeric_check (x, 0))
2587 return false;
2589 if (y != NULL)
2591 if (y->ts.type == BT_BOZ && !gfc_boz2real (y, gfc_default_double_kind))
2592 return false;
2594 if (!numeric_check (y, 1))
2595 return false;
2597 if (x->ts.type == BT_COMPLEX)
2599 gfc_error ("%qs argument of %qs intrinsic at %L must not be "
2600 "present if %<x%> is COMPLEX",
2601 gfc_current_intrinsic_arg[1]->name, gfc_current_intrinsic,
2602 &y->where);
2603 return false;
2606 if (y->ts.type == BT_COMPLEX)
2608 gfc_error ("%qs argument of %qs intrinsic at %L must have a type "
2609 "of either REAL or INTEGER",
2610 gfc_current_intrinsic_arg[1]->name, gfc_current_intrinsic,
2611 &y->where);
2612 return false;
2616 return true;
2620 bool
2621 gfc_check_dble (gfc_expr *x)
2623 if (x->ts.type == BT_BOZ && !gfc_boz2real (x, gfc_default_double_kind))
2624 return false;
2626 if (!numeric_check (x, 0))
2627 return false;
2629 return true;
2633 bool
2634 gfc_check_digits (gfc_expr *x)
2636 if (!int_or_real_check (x, 0))
2637 return false;
2639 return true;
2643 bool
2644 gfc_check_dot_product (gfc_expr *vector_a, gfc_expr *vector_b)
2646 switch (vector_a->ts.type)
2648 case BT_LOGICAL:
2649 if (!type_check (vector_b, 1, BT_LOGICAL))
2650 return false;
2651 break;
2653 case BT_INTEGER:
2654 case BT_REAL:
2655 case BT_COMPLEX:
2656 if (!numeric_check (vector_b, 1))
2657 return false;
2658 break;
2660 default:
2661 gfc_error ("%qs argument of %qs intrinsic at %L must be numeric "
2662 "or LOGICAL", gfc_current_intrinsic_arg[0]->name,
2663 gfc_current_intrinsic, &vector_a->where);
2664 return false;
2667 if (!rank_check (vector_a, 0, 1))
2668 return false;
2670 if (!rank_check (vector_b, 1, 1))
2671 return false;
2673 if (! identical_dimen_shape (vector_a, 0, vector_b, 0))
2675 gfc_error ("Different shape for arguments %qs and %qs at %L for "
2676 "intrinsic %<dot_product%>",
2677 gfc_current_intrinsic_arg[0]->name,
2678 gfc_current_intrinsic_arg[1]->name, &vector_a->where);
2679 return false;
2682 return true;
2686 bool
2687 gfc_check_dprod (gfc_expr *x, gfc_expr *y)
2689 if (!type_check (x, 0, BT_REAL)
2690 || !type_check (y, 1, BT_REAL))
2691 return false;
2693 if (x->ts.kind != gfc_default_real_kind)
2695 gfc_error ("%qs argument of %qs intrinsic at %L must be default "
2696 "real", gfc_current_intrinsic_arg[0]->name,
2697 gfc_current_intrinsic, &x->where);
2698 return false;
2701 if (y->ts.kind != gfc_default_real_kind)
2703 gfc_error ("%qs argument of %qs intrinsic at %L must be default "
2704 "real", gfc_current_intrinsic_arg[1]->name,
2705 gfc_current_intrinsic, &y->where);
2706 return false;
2709 return true;
2712 bool
2713 gfc_check_dshift (gfc_expr *i, gfc_expr *j, gfc_expr *shift)
2715 /* i and j cannot both be BOZ literal constants. */
2716 if (!boz_args_check (i, j))
2717 return false;
2719 /* If i is BOZ and j is integer, convert i to type of j. If j is not
2720 an integer, clear the BOZ; otherwise, check that i is an integer. */
2721 if (i->ts.type == BT_BOZ)
2723 if (j->ts.type != BT_INTEGER)
2724 reset_boz (i);
2725 else if (!gfc_boz2int (i, j->ts.kind))
2726 return false;
2728 else if (!type_check (i, 0, BT_INTEGER))
2730 if (j->ts.type == BT_BOZ)
2731 reset_boz (j);
2732 return false;
2735 /* If j is BOZ and i is integer, convert j to type of i. If i is not
2736 an integer, clear the BOZ; otherwise, check that i is an integer. */
2737 if (j->ts.type == BT_BOZ)
2739 if (i->ts.type != BT_INTEGER)
2740 reset_boz (j);
2741 else if (!gfc_boz2int (j, i->ts.kind))
2742 return false;
2744 else if (!type_check (j, 1, BT_INTEGER))
2745 return false;
2747 if (!same_type_check (i, 0, j, 1))
2748 return false;
2750 if (!type_check (shift, 2, BT_INTEGER))
2751 return false;
2753 if (!nonnegative_check ("SHIFT", shift))
2754 return false;
2756 if (!less_than_bitsize1 ("I", i, "SHIFT", shift, true))
2757 return false;
2759 return true;
2763 bool
2764 gfc_check_eoshift (gfc_expr *array, gfc_expr *shift, gfc_expr *boundary,
2765 gfc_expr *dim)
2767 int d;
2769 if (!array_check (array, 0))
2770 return false;
2772 if (!type_check (shift, 1, BT_INTEGER))
2773 return false;
2775 if (!dim_check (dim, 3, true))
2776 return false;
2778 if (!dim_rank_check (dim, array, false))
2779 return false;
2781 if (!dim)
2782 d = 1;
2783 else if (dim->expr_type == EXPR_CONSTANT)
2784 gfc_extract_int (dim, &d);
2785 else
2786 d = -1;
2788 if (array->rank == 1 || shift->rank == 0)
2790 if (!scalar_check (shift, 1))
2791 return false;
2793 else if (shift->rank == array->rank - 1)
2795 if (d > 0)
2797 int i, j;
2798 for (i = 0, j = 0; i < array->rank; i++)
2799 if (i != d - 1)
2801 if (!identical_dimen_shape (array, i, shift, j))
2803 gfc_error ("%qs argument of %qs intrinsic at %L has "
2804 "invalid shape in dimension %d (%ld/%ld)",
2805 gfc_current_intrinsic_arg[1]->name,
2806 gfc_current_intrinsic, &shift->where, i + 1,
2807 mpz_get_si (array->shape[i]),
2808 mpz_get_si (shift->shape[j]));
2809 return false;
2812 j += 1;
2816 else
2818 gfc_error ("%qs argument of intrinsic %qs at %L of must have rank "
2819 "%d or be a scalar", gfc_current_intrinsic_arg[1]->name,
2820 gfc_current_intrinsic, &shift->where, array->rank - 1);
2821 return false;
2824 if (boundary != NULL)
2826 if (!same_type_check (array, 0, boundary, 2))
2827 return false;
2829 /* Reject unequal string lengths and emit a better error message than
2830 gfc_check_same_strlen would. */
2831 if (array->ts.type == BT_CHARACTER)
2833 ssize_t len_a, len_b;
2835 len_a = gfc_var_strlen (array);
2836 len_b = gfc_var_strlen (boundary);
2837 if (len_a != -1 && len_b != -1 && len_a != len_b)
2839 gfc_error ("%qs must be of same type and kind as %qs at %L in %qs",
2840 gfc_current_intrinsic_arg[2]->name,
2841 gfc_current_intrinsic_arg[0]->name,
2842 &boundary->where, gfc_current_intrinsic);
2843 return false;
2847 if (array->rank == 1 || boundary->rank == 0)
2849 if (!scalar_check (boundary, 2))
2850 return false;
2852 else if (boundary->rank == array->rank - 1)
2854 if (d > 0)
2856 int i,j;
2857 for (i = 0, j = 0; i < array->rank; i++)
2859 if (i != d - 1)
2861 if (!identical_dimen_shape (array, i, boundary, j))
2863 gfc_error ("%qs argument of %qs intrinsic at %L has "
2864 "invalid shape in dimension %d (%ld/%ld)",
2865 gfc_current_intrinsic_arg[2]->name,
2866 gfc_current_intrinsic, &shift->where, i+1,
2867 mpz_get_si (array->shape[i]),
2868 mpz_get_si (boundary->shape[j]));
2869 return false;
2871 j += 1;
2876 else
2878 gfc_error ("%qs argument of intrinsic %qs at %L of must have "
2879 "rank %d or be a scalar",
2880 gfc_current_intrinsic_arg[1]->name, gfc_current_intrinsic,
2881 &shift->where, array->rank - 1);
2882 return false;
2885 else
2887 switch (array->ts.type)
2889 case BT_INTEGER:
2890 case BT_LOGICAL:
2891 case BT_REAL:
2892 case BT_COMPLEX:
2893 case BT_CHARACTER:
2894 break;
2896 default:
2897 gfc_error ("Missing %qs argument to %qs intrinsic at %L for %qs "
2898 "of type %qs", gfc_current_intrinsic_arg[2]->name,
2899 gfc_current_intrinsic, &array->where,
2900 gfc_current_intrinsic_arg[0]->name,
2901 gfc_typename (array));
2902 return false;
2906 return true;
2910 bool
2911 gfc_check_float (gfc_expr *a)
2913 if (a->ts.type == BT_BOZ)
2915 if (gfc_invalid_boz (G_("BOZ literal constant at %L cannot appear in the"
2916 " FLOAT intrinsic subprogram"), &a->where))
2918 reset_boz (a);
2919 return false;
2921 if (!gfc_boz2int (a, gfc_default_integer_kind))
2922 return false;
2925 if (!type_check (a, 0, BT_INTEGER))
2926 return false;
2928 if ((a->ts.kind != gfc_default_integer_kind)
2929 && !gfc_notify_std (GFC_STD_GNU, "non-default INTEGER "
2930 "kind argument to %s intrinsic at %L",
2931 gfc_current_intrinsic, &a->where))
2932 return false;
2934 return true;
2937 /* A single complex argument. */
2939 bool
2940 gfc_check_fn_c (gfc_expr *a)
2942 if (!type_check (a, 0, BT_COMPLEX))
2943 return false;
2945 return true;
2949 /* A single real argument. */
2951 bool
2952 gfc_check_fn_r (gfc_expr *a)
2954 if (!type_check (a, 0, BT_REAL))
2955 return false;
2957 return true;
2960 /* A single double argument. */
2962 bool
2963 gfc_check_fn_d (gfc_expr *a)
2965 if (!double_check (a, 0))
2966 return false;
2968 return true;
2971 /* A single real or complex argument. */
2973 bool
2974 gfc_check_fn_rc (gfc_expr *a)
2976 if (!real_or_complex_check (a, 0))
2977 return false;
2979 return true;
2983 bool
2984 gfc_check_fn_rc2008 (gfc_expr *a)
2986 if (!real_or_complex_check (a, 0))
2987 return false;
2989 if (a->ts.type == BT_COMPLEX
2990 && !gfc_notify_std (GFC_STD_F2008, "COMPLEX argument %qs "
2991 "of %qs intrinsic at %L",
2992 gfc_current_intrinsic_arg[0]->name,
2993 gfc_current_intrinsic, &a->where))
2994 return false;
2996 return true;
3000 bool
3001 gfc_check_fnum (gfc_expr *unit)
3003 if (!type_check (unit, 0, BT_INTEGER))
3004 return false;
3006 if (!scalar_check (unit, 0))
3007 return false;
3009 return true;
3013 bool
3014 gfc_check_huge (gfc_expr *x)
3016 if (!int_or_real_check (x, 0))
3017 return false;
3019 return true;
3023 bool
3024 gfc_check_hypot (gfc_expr *x, gfc_expr *y)
3026 if (!type_check (x, 0, BT_REAL))
3027 return false;
3028 if (!same_type_check (x, 0, y, 1))
3029 return false;
3031 return true;
3035 /* Check that the single argument is an integer. */
3037 bool
3038 gfc_check_i (gfc_expr *i)
3040 if (!type_check (i, 0, BT_INTEGER))
3041 return false;
3043 return true;
3047 bool
3048 gfc_check_iand_ieor_ior (gfc_expr *i, gfc_expr *j)
3050 /* i and j cannot both be BOZ literal constants. */
3051 if (!boz_args_check (i, j))
3052 return false;
3054 /* If i is BOZ and j is integer, convert i to type of j. */
3055 if (i->ts.type == BT_BOZ && j->ts.type == BT_INTEGER
3056 && !gfc_boz2int (i, j->ts.kind))
3057 return false;
3059 /* If j is BOZ and i is integer, convert j to type of i. */
3060 if (j->ts.type == BT_BOZ && i->ts.type == BT_INTEGER
3061 && !gfc_boz2int (j, i->ts.kind))
3062 return false;
3064 if (!type_check (i, 0, BT_INTEGER))
3065 return false;
3067 if (!type_check (j, 1, BT_INTEGER))
3068 return false;
3070 if (i->ts.kind != j->ts.kind)
3072 gfc_error ("Arguments of %qs have different kind type parameters "
3073 "at %L", gfc_current_intrinsic, &i->where);
3074 return false;
3077 return true;
3081 bool
3082 gfc_check_ibits (gfc_expr *i, gfc_expr *pos, gfc_expr *len)
3084 if (!type_check (i, 0, BT_INTEGER))
3085 return false;
3087 if (!type_check (pos, 1, BT_INTEGER))
3088 return false;
3090 if (!type_check (len, 2, BT_INTEGER))
3091 return false;
3093 if (!nonnegative_check ("pos", pos))
3094 return false;
3096 if (!nonnegative_check ("len", len))
3097 return false;
3099 if (!less_than_bitsize2 ("i", i, "pos", pos, "len", len))
3100 return false;
3102 return true;
3106 bool
3107 gfc_check_ichar_iachar (gfc_expr *c, gfc_expr *kind)
3109 int i;
3111 if (!type_check (c, 0, BT_CHARACTER))
3112 return false;
3114 if (!kind_check (kind, 1, BT_INTEGER))
3115 return false;
3117 if (kind && !gfc_notify_std (GFC_STD_F2003, "%qs intrinsic "
3118 "with KIND argument at %L",
3119 gfc_current_intrinsic, &kind->where))
3120 return false;
3122 if (c->expr_type == EXPR_VARIABLE || c->expr_type == EXPR_SUBSTRING)
3124 gfc_expr *start;
3125 gfc_expr *end;
3126 gfc_ref *ref;
3128 /* Substring references don't have the charlength set. */
3129 ref = c->ref;
3130 while (ref && ref->type != REF_SUBSTRING)
3131 ref = ref->next;
3133 gcc_assert (ref == NULL || ref->type == REF_SUBSTRING);
3135 if (!ref)
3137 /* Check that the argument is length one. Non-constant lengths
3138 can't be checked here, so assume they are ok. */
3139 if (c->ts.u.cl && c->ts.u.cl->length)
3141 /* If we already have a length for this expression then use it. */
3142 if (c->ts.u.cl->length->expr_type != EXPR_CONSTANT)
3143 return true;
3144 i = mpz_get_si (c->ts.u.cl->length->value.integer);
3146 else
3147 return true;
3149 else
3151 start = ref->u.ss.start;
3152 end = ref->u.ss.end;
3154 gcc_assert (start);
3155 if (end == NULL || end->expr_type != EXPR_CONSTANT
3156 || start->expr_type != EXPR_CONSTANT)
3157 return true;
3159 i = mpz_get_si (end->value.integer) + 1
3160 - mpz_get_si (start->value.integer);
3163 else
3164 return true;
3166 if (i != 1)
3168 gfc_error ("Argument of %s at %L must be of length one",
3169 gfc_current_intrinsic, &c->where);
3170 return false;
3173 return true;
3177 bool
3178 gfc_check_idnint (gfc_expr *a)
3180 if (!double_check (a, 0))
3181 return false;
3183 return true;
3187 bool
3188 gfc_check_index (gfc_expr *string, gfc_expr *substring, gfc_expr *back,
3189 gfc_expr *kind)
3191 if (!type_check (string, 0, BT_CHARACTER)
3192 || !type_check (substring, 1, BT_CHARACTER))
3193 return false;
3195 if (back != NULL && !type_check (back, 2, BT_LOGICAL))
3196 return false;
3198 if (!kind_check (kind, 3, BT_INTEGER))
3199 return false;
3200 if (kind && !gfc_notify_std (GFC_STD_F2003, "%qs intrinsic "
3201 "with KIND argument at %L",
3202 gfc_current_intrinsic, &kind->where))
3203 return false;
3205 if (string->ts.kind != substring->ts.kind)
3207 gfc_error ("%qs argument of %qs intrinsic at %L must be the same "
3208 "kind as %qs", gfc_current_intrinsic_arg[1]->name,
3209 gfc_current_intrinsic, &substring->where,
3210 gfc_current_intrinsic_arg[0]->name);
3211 return false;
3214 return true;
3218 bool
3219 gfc_check_int (gfc_expr *x, gfc_expr *kind)
3221 /* BOZ is dealt within simplify_int*. */
3222 if (x->ts.type == BT_BOZ)
3223 return true;
3225 if (!numeric_check (x, 0))
3226 return false;
3228 if (!kind_check (kind, 1, BT_INTEGER))
3229 return false;
3231 return true;
3235 bool
3236 gfc_check_intconv (gfc_expr *x)
3238 if (strcmp (gfc_current_intrinsic, "short") == 0
3239 || strcmp (gfc_current_intrinsic, "long") == 0)
3241 gfc_error ("%qs intrinsic subprogram at %L has been deprecated. "
3242 "Use INT intrinsic subprogram.", gfc_current_intrinsic,
3243 &x->where);
3244 return false;
3247 /* BOZ is dealt within simplify_int*. */
3248 if (x->ts.type == BT_BOZ)
3249 return true;
3251 if (!numeric_check (x, 0))
3252 return false;
3254 return true;
3257 bool
3258 gfc_check_ishft (gfc_expr *i, gfc_expr *shift)
3260 if (!type_check (i, 0, BT_INTEGER)
3261 || !type_check (shift, 1, BT_INTEGER))
3262 return false;
3264 if (!less_than_bitsize1 ("I", i, NULL, shift, true))
3265 return false;
3267 return true;
3271 bool
3272 gfc_check_ishftc (gfc_expr *i, gfc_expr *shift, gfc_expr *size)
3274 if (!type_check (i, 0, BT_INTEGER)
3275 || !type_check (shift, 1, BT_INTEGER))
3276 return false;
3278 if (size != NULL)
3280 int i2, i3;
3282 if (!type_check (size, 2, BT_INTEGER))
3283 return false;
3285 if (!less_than_bitsize1 ("I", i, "SIZE", size, true))
3286 return false;
3288 if (size->expr_type == EXPR_CONSTANT)
3290 gfc_extract_int (size, &i3);
3291 if (i3 <= 0)
3293 gfc_error ("SIZE at %L must be positive", &size->where);
3294 return false;
3297 if (shift->expr_type == EXPR_CONSTANT)
3299 gfc_extract_int (shift, &i2);
3300 if (i2 < 0)
3301 i2 = -i2;
3303 if (i2 > i3)
3305 gfc_error ("The absolute value of SHIFT at %L must be less "
3306 "than or equal to SIZE at %L", &shift->where,
3307 &size->where);
3308 return false;
3313 else if (!less_than_bitsize1 ("I", i, NULL, shift, true))
3314 return false;
3316 return true;
3320 bool
3321 gfc_check_kill (gfc_expr *pid, gfc_expr *sig)
3323 if (!type_check (pid, 0, BT_INTEGER))
3324 return false;
3326 if (!scalar_check (pid, 0))
3327 return false;
3329 if (!type_check (sig, 1, BT_INTEGER))
3330 return false;
3332 if (!scalar_check (sig, 1))
3333 return false;
3335 return true;
3339 bool
3340 gfc_check_kill_sub (gfc_expr *pid, gfc_expr *sig, gfc_expr *status)
3342 if (!type_check (pid, 0, BT_INTEGER))
3343 return false;
3345 if (!scalar_check (pid, 0))
3346 return false;
3348 if (!type_check (sig, 1, BT_INTEGER))
3349 return false;
3351 if (!scalar_check (sig, 1))
3352 return false;
3354 if (status)
3356 if (!type_check (status, 2, BT_INTEGER))
3357 return false;
3359 if (!scalar_check (status, 2))
3360 return false;
3362 if (status->expr_type != EXPR_VARIABLE)
3364 gfc_error ("STATUS at %L shall be an INTENT(OUT) variable",
3365 &status->where);
3366 return false;
3369 if (status->expr_type == EXPR_VARIABLE
3370 && status->symtree && status->symtree->n.sym
3371 && status->symtree->n.sym->attr.intent == INTENT_IN)
3373 gfc_error ("%qs at %L shall be an INTENT(OUT) variable",
3374 status->symtree->name, &status->where);
3375 return false;
3379 return true;
3383 bool
3384 gfc_check_kind (gfc_expr *x)
3386 if (gfc_invalid_null_arg (x))
3387 return false;
3389 if (gfc_bt_struct (x->ts.type) || x->ts.type == BT_CLASS)
3391 gfc_error ("%qs argument of %qs intrinsic at %L must be of "
3392 "intrinsic type", gfc_current_intrinsic_arg[0]->name,
3393 gfc_current_intrinsic, &x->where);
3394 return false;
3396 if (x->ts.type == BT_PROCEDURE)
3398 gfc_error ("%qs argument of %qs intrinsic at %L must be a data entity",
3399 gfc_current_intrinsic_arg[0]->name, gfc_current_intrinsic,
3400 &x->where);
3401 return false;
3404 return true;
3408 bool
3409 gfc_check_lbound (gfc_expr *array, gfc_expr *dim, gfc_expr *kind)
3411 if (!array_check (array, 0))
3412 return false;
3414 if (!dim_check (dim, 1, false))
3415 return false;
3417 if (!dim_rank_check (dim, array, 1))
3418 return false;
3420 if (!kind_check (kind, 2, BT_INTEGER))
3421 return false;
3422 if (kind && !gfc_notify_std (GFC_STD_F2003, "%qs intrinsic "
3423 "with KIND argument at %L",
3424 gfc_current_intrinsic, &kind->where))
3425 return false;
3427 return true;
3431 bool
3432 gfc_check_lcobound (gfc_expr *coarray, gfc_expr *dim, gfc_expr *kind)
3434 if (flag_coarray == GFC_FCOARRAY_NONE)
3436 gfc_fatal_error ("Coarrays disabled at %C, use %<-fcoarray=%> to enable");
3437 return false;
3440 if (!coarray_check (coarray, 0))
3441 return false;
3443 if (dim != NULL)
3445 if (!dim_check (dim, 1, false))
3446 return false;
3448 if (!dim_corank_check (dim, coarray))
3449 return false;
3452 if (!kind_check (kind, 2, BT_INTEGER))
3453 return false;
3455 return true;
3459 bool
3460 gfc_check_len_lentrim (gfc_expr *s, gfc_expr *kind)
3462 if (!type_check (s, 0, BT_CHARACTER))
3463 return false;
3465 if (gfc_invalid_null_arg (s))
3466 return false;
3468 if (!kind_check (kind, 1, BT_INTEGER))
3469 return false;
3470 if (kind && !gfc_notify_std (GFC_STD_F2003, "%qs intrinsic "
3471 "with KIND argument at %L",
3472 gfc_current_intrinsic, &kind->where))
3473 return false;
3475 return true;
3479 bool
3480 gfc_check_lge_lgt_lle_llt (gfc_expr *a, gfc_expr *b)
3482 if (!type_check (a, 0, BT_CHARACTER))
3483 return false;
3484 if (!kind_value_check (a, 0, gfc_default_character_kind))
3485 return false;
3487 if (!type_check (b, 1, BT_CHARACTER))
3488 return false;
3489 if (!kind_value_check (b, 1, gfc_default_character_kind))
3490 return false;
3492 return true;
3496 bool
3497 gfc_check_link (gfc_expr *path1, gfc_expr *path2)
3499 if (!type_check (path1, 0, BT_CHARACTER))
3500 return false;
3501 if (!kind_value_check (path1, 0, gfc_default_character_kind))
3502 return false;
3504 if (!type_check (path2, 1, BT_CHARACTER))
3505 return false;
3506 if (!kind_value_check (path2, 1, gfc_default_character_kind))
3507 return false;
3509 return true;
3513 bool
3514 gfc_check_link_sub (gfc_expr *path1, gfc_expr *path2, gfc_expr *status)
3516 if (!type_check (path1, 0, BT_CHARACTER))
3517 return false;
3518 if (!kind_value_check (path1, 0, gfc_default_character_kind))
3519 return false;
3521 if (!type_check (path2, 1, BT_CHARACTER))
3522 return false;
3523 if (!kind_value_check (path2, 0, gfc_default_character_kind))
3524 return false;
3526 if (status == NULL)
3527 return true;
3529 if (!type_check (status, 2, BT_INTEGER))
3530 return false;
3532 if (!scalar_check (status, 2))
3533 return false;
3535 return true;
3539 bool
3540 gfc_check_loc (gfc_expr *expr)
3542 return variable_check (expr, 0, true);
3546 bool
3547 gfc_check_symlnk (gfc_expr *path1, gfc_expr *path2)
3549 if (!type_check (path1, 0, BT_CHARACTER))
3550 return false;
3551 if (!kind_value_check (path1, 0, gfc_default_character_kind))
3552 return false;
3554 if (!type_check (path2, 1, BT_CHARACTER))
3555 return false;
3556 if (!kind_value_check (path2, 1, gfc_default_character_kind))
3557 return false;
3559 return true;
3563 bool
3564 gfc_check_symlnk_sub (gfc_expr *path1, gfc_expr *path2, gfc_expr *status)
3566 if (!type_check (path1, 0, BT_CHARACTER))
3567 return false;
3568 if (!kind_value_check (path1, 0, gfc_default_character_kind))
3569 return false;
3571 if (!type_check (path2, 1, BT_CHARACTER))
3572 return false;
3573 if (!kind_value_check (path2, 1, gfc_default_character_kind))
3574 return false;
3576 if (status == NULL)
3577 return true;
3579 if (!type_check (status, 2, BT_INTEGER))
3580 return false;
3582 if (!scalar_check (status, 2))
3583 return false;
3585 return true;
3589 bool
3590 gfc_check_logical (gfc_expr *a, gfc_expr *kind)
3592 if (!type_check (a, 0, BT_LOGICAL))
3593 return false;
3594 if (!kind_check (kind, 1, BT_LOGICAL))
3595 return false;
3597 return true;
3601 /* Min/max family. */
3603 static bool
3604 min_max_args (gfc_actual_arglist *args)
3606 gfc_actual_arglist *arg;
3607 int i, j, nargs, *nlabels, nlabelless;
3608 bool a1 = false, a2 = false;
3610 if (args == NULL || args->next == NULL)
3612 gfc_error ("Intrinsic %qs at %L must have at least two arguments",
3613 gfc_current_intrinsic, gfc_current_intrinsic_where);
3614 return false;
3617 if (!args->name)
3618 a1 = true;
3620 if (!args->next->name)
3621 a2 = true;
3623 nargs = 0;
3624 for (arg = args; arg; arg = arg->next)
3625 if (arg->name)
3626 nargs++;
3628 if (nargs == 0)
3629 return true;
3631 /* Note: Having a keywordless argument after an "arg=" is checked before. */
3632 nlabelless = 0;
3633 nlabels = XALLOCAVEC (int, nargs);
3634 for (arg = args, i = 0; arg; arg = arg->next, i++)
3635 if (arg->name)
3637 int n;
3638 char *endp;
3640 if (arg->name[0] != 'a' || arg->name[1] < '1' || arg->name[1] > '9')
3641 goto unknown;
3642 n = strtol (&arg->name[1], &endp, 10);
3643 if (endp[0] != '\0')
3644 goto unknown;
3645 if (n <= 0)
3646 goto unknown;
3647 if (n <= nlabelless)
3648 goto duplicate;
3649 nlabels[i] = n;
3650 if (n == 1)
3651 a1 = true;
3652 if (n == 2)
3653 a2 = true;
3655 else
3656 nlabelless++;
3658 if (!a1 || !a2)
3660 gfc_error ("Missing %qs argument to the %s intrinsic at %L",
3661 !a1 ? "a1" : "a2", gfc_current_intrinsic,
3662 gfc_current_intrinsic_where);
3663 return false;
3666 /* Check for duplicates. */
3667 for (i = 0; i < nargs; i++)
3668 for (j = i + 1; j < nargs; j++)
3669 if (nlabels[i] == nlabels[j])
3670 goto duplicate;
3672 return true;
3674 duplicate:
3675 gfc_error ("Duplicate argument %qs at %L to intrinsic %s", arg->name,
3676 &arg->expr->where, gfc_current_intrinsic);
3677 return false;
3679 unknown:
3680 gfc_error ("Unknown argument %qs at %L to intrinsic %s", arg->name,
3681 &arg->expr->where, gfc_current_intrinsic);
3682 return false;
3686 static bool
3687 check_rest (bt type, int kind, gfc_actual_arglist *arglist)
3689 gfc_actual_arglist *arg, *tmp;
3690 gfc_expr *x;
3691 int m, n;
3693 if (!min_max_args (arglist))
3694 return false;
3696 for (arg = arglist, n=1; arg; arg = arg->next, n++)
3698 x = arg->expr;
3699 if (x->ts.type != type || x->ts.kind != kind)
3701 if (x->ts.type == type)
3703 if (x->ts.type == BT_CHARACTER)
3705 gfc_error ("Different character kinds at %L", &x->where);
3706 return false;
3708 if (!gfc_notify_std (GFC_STD_GNU, "Different type "
3709 "kinds at %L", &x->where))
3710 return false;
3712 else
3714 gfc_error ("%<a%d%> argument of %qs intrinsic at %L must be "
3715 "%s(%d)", n, gfc_current_intrinsic, &x->where,
3716 gfc_basic_typename (type), kind);
3717 return false;
3721 for (tmp = arglist, m=1; tmp != arg; tmp = tmp->next, m++)
3722 if (!gfc_check_conformance (tmp->expr, x,
3723 _("arguments 'a%d' and 'a%d' for "
3724 "intrinsic '%s'"), m, n,
3725 gfc_current_intrinsic))
3726 return false;
3729 return true;
3733 bool
3734 gfc_check_min_max (gfc_actual_arglist *arg)
3736 gfc_expr *x;
3738 if (!min_max_args (arg))
3739 return false;
3741 x = arg->expr;
3743 if (x->ts.type == BT_CHARACTER)
3745 if (!gfc_notify_std (GFC_STD_F2003, "%qs intrinsic "
3746 "with CHARACTER argument at %L",
3747 gfc_current_intrinsic, &x->where))
3748 return false;
3750 else if (x->ts.type != BT_INTEGER && x->ts.type != BT_REAL)
3752 gfc_error ("%<a1%> argument of %qs intrinsic at %L must be INTEGER, "
3753 "REAL or CHARACTER", gfc_current_intrinsic, &x->where);
3754 return false;
3757 return check_rest (x->ts.type, x->ts.kind, arg);
3761 bool
3762 gfc_check_min_max_integer (gfc_actual_arglist *arg)
3764 return check_rest (BT_INTEGER, gfc_default_integer_kind, arg);
3768 bool
3769 gfc_check_min_max_real (gfc_actual_arglist *arg)
3771 return check_rest (BT_REAL, gfc_default_real_kind, arg);
3775 bool
3776 gfc_check_min_max_double (gfc_actual_arglist *arg)
3778 return check_rest (BT_REAL, gfc_default_double_kind, arg);
3782 /* End of min/max family. */
3784 bool
3785 gfc_check_malloc (gfc_expr *size)
3787 if (!type_check (size, 0, BT_INTEGER))
3788 return false;
3790 if (!scalar_check (size, 0))
3791 return false;
3793 return true;
3797 bool
3798 gfc_check_matmul (gfc_expr *matrix_a, gfc_expr *matrix_b)
3800 if ((matrix_a->ts.type != BT_LOGICAL) && !gfc_numeric_ts (&matrix_a->ts))
3802 gfc_error ("%qs argument of %qs intrinsic at %L must be numeric "
3803 "or LOGICAL", gfc_current_intrinsic_arg[0]->name,
3804 gfc_current_intrinsic, &matrix_a->where);
3805 return false;
3808 if ((matrix_b->ts.type != BT_LOGICAL) && !gfc_numeric_ts (&matrix_b->ts))
3810 gfc_error ("%qs argument of %qs intrinsic at %L must be numeric "
3811 "or LOGICAL", gfc_current_intrinsic_arg[1]->name,
3812 gfc_current_intrinsic, &matrix_b->where);
3813 return false;
3816 if ((matrix_a->ts.type == BT_LOGICAL && gfc_numeric_ts (&matrix_b->ts))
3817 || (gfc_numeric_ts (&matrix_a->ts) && matrix_b->ts.type == BT_LOGICAL))
3819 gfc_error ("Argument types of %qs intrinsic at %L must match (%s/%s)",
3820 gfc_current_intrinsic, &matrix_a->where,
3821 gfc_typename(&matrix_a->ts), gfc_typename(&matrix_b->ts));
3822 return false;
3825 switch (matrix_a->rank)
3827 case 1:
3828 if (!rank_check (matrix_b, 1, 2))
3829 return false;
3830 /* Check for case matrix_a has shape(m), matrix_b has shape (m, k). */
3831 if (!identical_dimen_shape (matrix_a, 0, matrix_b, 0))
3833 gfc_error ("Different shape on dimension 1 for arguments %qs "
3834 "and %qs at %L for intrinsic matmul",
3835 gfc_current_intrinsic_arg[0]->name,
3836 gfc_current_intrinsic_arg[1]->name, &matrix_a->where);
3837 return false;
3839 break;
3841 case 2:
3842 if (matrix_b->rank != 2)
3844 if (!rank_check (matrix_b, 1, 1))
3845 return false;
3847 /* matrix_b has rank 1 or 2 here. Common check for the cases
3848 - matrix_a has shape (n,m) and matrix_b has shape (m, k)
3849 - matrix_a has shape (n,m) and matrix_b has shape (m). */
3850 if (!identical_dimen_shape (matrix_a, 1, matrix_b, 0))
3852 gfc_error ("Different shape on dimension 2 for argument %qs and "
3853 "dimension 1 for argument %qs at %L for intrinsic "
3854 "matmul", gfc_current_intrinsic_arg[0]->name,
3855 gfc_current_intrinsic_arg[1]->name, &matrix_a->where);
3856 return false;
3858 break;
3860 default:
3861 gfc_error ("%qs argument of %qs intrinsic at %L must be of rank "
3862 "1 or 2", gfc_current_intrinsic_arg[0]->name,
3863 gfc_current_intrinsic, &matrix_a->where);
3864 return false;
3867 return true;
3871 /* Whoever came up with this interface was probably on something.
3872 The possibilities for the occupation of the second and third
3873 parameters are:
3875 Arg #2 Arg #3
3876 NULL NULL
3877 DIM NULL
3878 MASK NULL
3879 NULL MASK minloc(array, mask=m)
3880 DIM MASK
3882 I.e. in the case of minloc(array,mask), mask will be in the second
3883 position of the argument list and we'll have to fix that up. Also,
3884 add the BACK argument if that isn't present. */
3886 bool
3887 gfc_check_minloc_maxloc (gfc_actual_arglist *ap)
3889 gfc_expr *a, *m, *d, *k, *b;
3891 a = ap->expr;
3892 if (!int_or_real_or_char_check_f2003 (a, 0) || !array_check (a, 0))
3893 return false;
3895 d = ap->next->expr;
3896 m = ap->next->next->expr;
3897 k = ap->next->next->next->expr;
3898 b = ap->next->next->next->next->expr;
3900 if (b)
3902 if (!type_check (b, 4, BT_LOGICAL) || !scalar_check (b,4))
3903 return false;
3905 else
3907 b = gfc_get_logical_expr (gfc_logical_4_kind, NULL, 0);
3908 ap->next->next->next->next->expr = b;
3911 if (m == NULL && d != NULL && d->ts.type == BT_LOGICAL
3912 && ap->next->name == NULL)
3914 m = d;
3915 d = NULL;
3916 ap->next->expr = NULL;
3917 ap->next->next->expr = m;
3920 if (!dim_check (d, 1, false))
3921 return false;
3923 if (!dim_rank_check (d, a, 0))
3924 return false;
3926 if (m != NULL && !type_check (m, 2, BT_LOGICAL))
3927 return false;
3929 if (m != NULL
3930 && !gfc_check_conformance (a, m,
3931 _("arguments '%s' and '%s' for intrinsic %s"),
3932 gfc_current_intrinsic_arg[0]->name,
3933 gfc_current_intrinsic_arg[2]->name,
3934 gfc_current_intrinsic))
3935 return false;
3937 if (!kind_check (k, 1, BT_INTEGER))
3938 return false;
3940 return true;
3943 /* Check function for findloc. Mostly like gfc_check_minloc_maxloc
3944 above, with the additional "value" argument. */
3946 bool
3947 gfc_check_findloc (gfc_actual_arglist *ap)
3949 gfc_expr *a, *v, *m, *d, *k, *b;
3950 bool a1, v1;
3952 a = ap->expr;
3953 if (!intrinsic_type_check (a, 0) || !array_check (a, 0))
3954 return false;
3956 v = ap->next->expr;
3957 if (!intrinsic_type_check (v, 1) || !scalar_check (v,1))
3958 return false;
3960 /* Check if the type are both logical. */
3961 a1 = a->ts.type == BT_LOGICAL;
3962 v1 = v->ts.type == BT_LOGICAL;
3963 if ((a1 && !v1) || (!a1 && v1))
3964 goto incompat;
3966 /* Check if the type are both character. */
3967 a1 = a->ts.type == BT_CHARACTER;
3968 v1 = v->ts.type == BT_CHARACTER;
3969 if ((a1 && !v1) || (!a1 && v1))
3970 goto incompat;
3972 /* Check the kind of the characters argument match. */
3973 if (a1 && v1 && a->ts.kind != v->ts.kind)
3974 goto incompat;
3976 d = ap->next->next->expr;
3977 m = ap->next->next->next->expr;
3978 k = ap->next->next->next->next->expr;
3979 b = ap->next->next->next->next->next->expr;
3981 if (b)
3983 if (!type_check (b, 5, BT_LOGICAL) || !scalar_check (b,4))
3984 return false;
3986 else
3988 b = gfc_get_logical_expr (gfc_logical_4_kind, NULL, 0);
3989 ap->next->next->next->next->next->expr = b;
3992 if (m == NULL && d != NULL && d->ts.type == BT_LOGICAL
3993 && ap->next->name == NULL)
3995 m = d;
3996 d = NULL;
3997 ap->next->next->expr = NULL;
3998 ap->next->next->next->expr = m;
4001 if (!dim_check (d, 2, false))
4002 return false;
4004 if (!dim_rank_check (d, a, 0))
4005 return false;
4007 if (m != NULL && !type_check (m, 3, BT_LOGICAL))
4008 return false;
4010 if (m != NULL
4011 && !gfc_check_conformance (a, m,
4012 _("arguments '%s' and '%s' for intrinsic %s"),
4013 gfc_current_intrinsic_arg[0]->name,
4014 gfc_current_intrinsic_arg[3]->name,
4015 gfc_current_intrinsic))
4016 return false;
4018 if (!kind_check (k, 1, BT_INTEGER))
4019 return false;
4021 return true;
4023 incompat:
4024 gfc_error ("Argument %qs of %qs intrinsic at %L must be in type "
4025 "conformance to argument %qs at %L",
4026 gfc_current_intrinsic_arg[0]->name,
4027 gfc_current_intrinsic, &a->where,
4028 gfc_current_intrinsic_arg[1]->name, &v->where);
4029 return false;
4033 /* Similar to minloc/maxloc, the argument list might need to be
4034 reordered for the MINVAL, MAXVAL, PRODUCT, and SUM intrinsics. The
4035 difference is that MINLOC/MAXLOC take an additional KIND argument.
4036 The possibilities are:
4038 Arg #2 Arg #3
4039 NULL NULL
4040 DIM NULL
4041 MASK NULL
4042 NULL MASK minval(array, mask=m)
4043 DIM MASK
4045 I.e. in the case of minval(array,mask), mask will be in the second
4046 position of the argument list and we'll have to fix that up. */
4048 static bool
4049 check_reduction (gfc_actual_arglist *ap)
4051 gfc_expr *a, *m, *d;
4053 a = ap->expr;
4054 d = ap->next->expr;
4055 m = ap->next->next->expr;
4057 if (m == NULL && d != NULL && d->ts.type == BT_LOGICAL
4058 && ap->next->name == NULL)
4060 m = d;
4061 d = NULL;
4062 ap->next->expr = NULL;
4063 ap->next->next->expr = m;
4066 if (!dim_check (d, 1, false))
4067 return false;
4069 if (!dim_rank_check (d, a, 0))
4070 return false;
4072 if (m != NULL && !type_check (m, 2, BT_LOGICAL))
4073 return false;
4075 if (m != NULL
4076 && !gfc_check_conformance (a, m,
4077 _("arguments '%s' and '%s' for intrinsic %s"),
4078 gfc_current_intrinsic_arg[0]->name,
4079 gfc_current_intrinsic_arg[2]->name,
4080 gfc_current_intrinsic))
4081 return false;
4083 return true;
4087 bool
4088 gfc_check_minval_maxval (gfc_actual_arglist *ap)
4090 if (!int_or_real_or_char_check_f2003 (ap->expr, 0)
4091 || !array_check (ap->expr, 0))
4092 return false;
4094 return check_reduction (ap);
4098 bool
4099 gfc_check_product_sum (gfc_actual_arglist *ap)
4101 if (!numeric_check (ap->expr, 0)
4102 || !array_check (ap->expr, 0))
4103 return false;
4105 return check_reduction (ap);
4109 /* For IANY, IALL and IPARITY. */
4111 bool
4112 gfc_check_mask (gfc_expr *i, gfc_expr *kind)
4114 int k;
4116 if (!type_check (i, 0, BT_INTEGER))
4117 return false;
4119 if (!nonnegative_check ("I", i))
4120 return false;
4122 if (!kind_check (kind, 1, BT_INTEGER))
4123 return false;
4125 if (kind)
4126 gfc_extract_int (kind, &k);
4127 else
4128 k = gfc_default_integer_kind;
4130 if (!less_than_bitsizekind ("I", i, k))
4131 return false;
4133 return true;
4137 bool
4138 gfc_check_transf_bit_intrins (gfc_actual_arglist *ap)
4140 if (ap->expr->ts.type != BT_INTEGER)
4142 gfc_error ("%qs argument of %qs intrinsic at %L must be INTEGER",
4143 gfc_current_intrinsic_arg[0]->name,
4144 gfc_current_intrinsic, &ap->expr->where);
4145 return false;
4148 if (!array_check (ap->expr, 0))
4149 return false;
4151 return check_reduction (ap);
4155 bool
4156 gfc_check_merge (gfc_expr *tsource, gfc_expr *fsource, gfc_expr *mask)
4158 if (gfc_invalid_null_arg (tsource))
4159 return false;
4161 if (gfc_invalid_null_arg (fsource))
4162 return false;
4164 if (!same_type_check (tsource, 0, fsource, 1))
4165 return false;
4167 if (!type_check (mask, 2, BT_LOGICAL))
4168 return false;
4170 if (tsource->ts.type == BT_CHARACTER)
4171 return gfc_check_same_strlen (tsource, fsource, "MERGE intrinsic");
4173 return true;
4177 bool
4178 gfc_check_merge_bits (gfc_expr *i, gfc_expr *j, gfc_expr *mask)
4180 /* i and j cannot both be BOZ literal constants. */
4181 if (!boz_args_check (i, j))
4182 return false;
4184 /* If i is BOZ and j is integer, convert i to type of j. */
4185 if (i->ts.type == BT_BOZ && j->ts.type == BT_INTEGER
4186 && !gfc_boz2int (i, j->ts.kind))
4187 return false;
4189 /* If j is BOZ and i is integer, convert j to type of i. */
4190 if (j->ts.type == BT_BOZ && i->ts.type == BT_INTEGER
4191 && !gfc_boz2int (j, i->ts.kind))
4192 return false;
4194 if (!type_check (i, 0, BT_INTEGER))
4195 return false;
4197 if (!type_check (j, 1, BT_INTEGER))
4198 return false;
4200 if (!same_type_check (i, 0, j, 1))
4201 return false;
4203 if (mask->ts.type == BT_BOZ && !gfc_boz2int(mask, i->ts.kind))
4204 return false;
4206 if (!type_check (mask, 2, BT_INTEGER))
4207 return false;
4209 if (!same_type_check (i, 0, mask, 2))
4210 return false;
4212 return true;
4216 bool
4217 gfc_check_move_alloc (gfc_expr *from, gfc_expr *to)
4219 if (!variable_check (from, 0, false))
4220 return false;
4221 if (!allocatable_check (from, 0))
4222 return false;
4223 if (gfc_is_coindexed (from))
4225 gfc_error ("The FROM argument to MOVE_ALLOC at %L shall not be "
4226 "coindexed", &from->where);
4227 return false;
4230 if (!variable_check (to, 1, false))
4231 return false;
4232 if (!allocatable_check (to, 1))
4233 return false;
4234 if (gfc_is_coindexed (to))
4236 gfc_error ("The TO argument to MOVE_ALLOC at %L shall not be "
4237 "coindexed", &to->where);
4238 return false;
4241 if (from->ts.type == BT_CLASS && to->ts.type == BT_DERIVED)
4243 gfc_error ("The TO arguments in MOVE_ALLOC at %L must be "
4244 "polymorphic if FROM is polymorphic",
4245 &to->where);
4246 return false;
4249 if (!same_type_check (to, 1, from, 0))
4250 return false;
4252 if (to->rank != from->rank)
4254 gfc_error ("The FROM and TO arguments of the MOVE_ALLOC intrinsic at %L "
4255 "must have the same rank %d/%d", &to->where, from->rank,
4256 to->rank);
4257 return false;
4260 /* IR F08/0040; cf. 12-006A. */
4261 if (gfc_get_corank (to) != gfc_get_corank (from))
4263 gfc_error ("The FROM and TO arguments of the MOVE_ALLOC intrinsic at %L "
4264 "must have the same corank %d/%d", &to->where,
4265 gfc_get_corank (from), gfc_get_corank (to));
4266 return false;
4269 /* This is based losely on F2003 12.4.1.7. It is intended to prevent
4270 the likes of to = sym->cmp1->cmp2 and from = sym->cmp1, where cmp1
4271 and cmp2 are allocatable. After the allocation is transferred,
4272 the 'to' chain is broken by the nullification of the 'from'. A bit
4273 of reflection reveals that this can only occur for derived types
4274 with recursive allocatable components. */
4275 if (to->expr_type == EXPR_VARIABLE && from->expr_type == EXPR_VARIABLE
4276 && !strcmp (to->symtree->n.sym->name, from->symtree->n.sym->name))
4278 gfc_ref *to_ref, *from_ref;
4279 to_ref = to->ref;
4280 from_ref = from->ref;
4281 bool aliasing = true;
4283 for (; from_ref && to_ref;
4284 from_ref = from_ref->next, to_ref = to_ref->next)
4286 if (to_ref->type != from->ref->type)
4287 aliasing = false;
4288 else if (to_ref->type == REF_ARRAY
4289 && to_ref->u.ar.type != AR_FULL
4290 && from_ref->u.ar.type != AR_FULL)
4291 /* Play safe; assume sections and elements are different. */
4292 aliasing = false;
4293 else if (to_ref->type == REF_COMPONENT
4294 && to_ref->u.c.component != from_ref->u.c.component)
4295 aliasing = false;
4297 if (!aliasing)
4298 break;
4301 if (aliasing)
4303 gfc_error ("The FROM and TO arguments at %L violate aliasing "
4304 "restrictions (F2003 12.4.1.7)", &to->where);
4305 return false;
4309 /* CLASS arguments: Make sure the vtab of from is present. */
4310 if (to->ts.type == BT_CLASS && !UNLIMITED_POLY (from))
4311 gfc_find_vtab (&from->ts);
4313 return true;
4317 bool
4318 gfc_check_nearest (gfc_expr *x, gfc_expr *s)
4320 if (!type_check (x, 0, BT_REAL))
4321 return false;
4323 if (!type_check (s, 1, BT_REAL))
4324 return false;
4326 if (s->expr_type == EXPR_CONSTANT)
4328 if (mpfr_sgn (s->value.real) == 0)
4330 gfc_error ("Argument %<S%> of NEAREST at %L shall not be zero",
4331 &s->where);
4332 return false;
4336 return true;
4340 bool
4341 gfc_check_new_line (gfc_expr *a)
4343 if (!type_check (a, 0, BT_CHARACTER))
4344 return false;
4346 return true;
4350 bool
4351 gfc_check_norm2 (gfc_expr *array, gfc_expr *dim)
4353 if (!type_check (array, 0, BT_REAL))
4354 return false;
4356 if (!array_check (array, 0))
4357 return false;
4359 if (!dim_rank_check (dim, array, false))
4360 return false;
4362 return true;
4365 bool
4366 gfc_check_null (gfc_expr *mold)
4368 symbol_attribute attr;
4370 if (mold == NULL)
4371 return true;
4373 if (!variable_check (mold, 0, true))
4374 return false;
4376 attr = gfc_variable_attr (mold, NULL);
4378 if (!attr.pointer && !attr.proc_pointer && !attr.allocatable)
4380 gfc_error ("%qs argument of %qs intrinsic at %L must be a POINTER, "
4381 "ALLOCATABLE or procedure pointer",
4382 gfc_current_intrinsic_arg[0]->name,
4383 gfc_current_intrinsic, &mold->where);
4384 return false;
4387 if (attr.allocatable
4388 && !gfc_notify_std (GFC_STD_F2003, "NULL intrinsic with "
4389 "allocatable MOLD at %L", &mold->where))
4390 return false;
4392 /* F2008, C1242. */
4393 if (gfc_is_coindexed (mold))
4395 gfc_error ("%qs argument of %qs intrinsic at %L shall not be "
4396 "coindexed", gfc_current_intrinsic_arg[0]->name,
4397 gfc_current_intrinsic, &mold->where);
4398 return false;
4401 return true;
4405 bool
4406 gfc_check_pack (gfc_expr *array, gfc_expr *mask, gfc_expr *vector)
4408 if (!array_check (array, 0))
4409 return false;
4411 if (!type_check (mask, 1, BT_LOGICAL))
4412 return false;
4414 if (!gfc_check_conformance (array, mask,
4415 _("arguments '%s' and '%s' for intrinsic '%s'"),
4416 gfc_current_intrinsic_arg[0]->name,
4417 gfc_current_intrinsic_arg[1]->name,
4418 gfc_current_intrinsic))
4419 return false;
4421 if (vector != NULL)
4423 mpz_t array_size, vector_size;
4424 bool have_array_size, have_vector_size;
4426 if (!same_type_check (array, 0, vector, 2))
4427 return false;
4429 if (!rank_check (vector, 2, 1))
4430 return false;
4432 /* VECTOR requires at least as many elements as MASK
4433 has .TRUE. values. */
4434 have_array_size = gfc_array_size(array, &array_size);
4435 have_vector_size = gfc_array_size(vector, &vector_size);
4437 if (have_vector_size
4438 && (mask->expr_type == EXPR_ARRAY
4439 || (mask->expr_type == EXPR_CONSTANT
4440 && have_array_size)))
4442 int mask_true_values = 0;
4444 if (mask->expr_type == EXPR_ARRAY)
4446 gfc_constructor *mask_ctor;
4447 mask_ctor = gfc_constructor_first (mask->value.constructor);
4448 while (mask_ctor)
4450 if (mask_ctor->expr->expr_type != EXPR_CONSTANT)
4452 mask_true_values = 0;
4453 break;
4456 if (mask_ctor->expr->value.logical)
4457 mask_true_values++;
4459 mask_ctor = gfc_constructor_next (mask_ctor);
4462 else if (mask->expr_type == EXPR_CONSTANT && mask->value.logical)
4463 mask_true_values = mpz_get_si (array_size);
4465 if (mpz_get_si (vector_size) < mask_true_values)
4467 gfc_error ("%qs argument of %qs intrinsic at %L must "
4468 "provide at least as many elements as there "
4469 "are .TRUE. values in %qs (%ld/%d)",
4470 gfc_current_intrinsic_arg[2]->name,
4471 gfc_current_intrinsic, &vector->where,
4472 gfc_current_intrinsic_arg[1]->name,
4473 mpz_get_si (vector_size), mask_true_values);
4474 return false;
4478 if (have_array_size)
4479 mpz_clear (array_size);
4480 if (have_vector_size)
4481 mpz_clear (vector_size);
4484 return true;
4488 bool
4489 gfc_check_parity (gfc_expr *mask, gfc_expr *dim)
4491 if (!type_check (mask, 0, BT_LOGICAL))
4492 return false;
4494 if (!array_check (mask, 0))
4495 return false;
4497 if (!dim_rank_check (dim, mask, false))
4498 return false;
4500 return true;
4504 bool
4505 gfc_check_precision (gfc_expr *x)
4507 if (!real_or_complex_check (x, 0))
4508 return false;
4510 return true;
4514 bool
4515 gfc_check_present (gfc_expr *a)
4517 gfc_symbol *sym;
4519 if (!variable_check (a, 0, true))
4520 return false;
4522 sym = a->symtree->n.sym;
4523 if (!sym->attr.dummy)
4525 gfc_error ("%qs argument of %qs intrinsic at %L must be of a "
4526 "dummy variable", gfc_current_intrinsic_arg[0]->name,
4527 gfc_current_intrinsic, &a->where);
4528 return false;
4531 if (!sym->attr.optional)
4533 gfc_error ("%qs argument of %qs intrinsic at %L must be of "
4534 "an OPTIONAL dummy variable",
4535 gfc_current_intrinsic_arg[0]->name, gfc_current_intrinsic,
4536 &a->where);
4537 return false;
4540 /* 13.14.82 PRESENT(A)
4541 ......
4542 Argument. A shall be the name of an optional dummy argument that is
4543 accessible in the subprogram in which the PRESENT function reference
4544 appears... */
4546 if (a->ref != NULL
4547 && !(a->ref->next == NULL && a->ref->type == REF_ARRAY
4548 && (a->ref->u.ar.type == AR_FULL
4549 || (a->ref->u.ar.type == AR_ELEMENT
4550 && a->ref->u.ar.as->rank == 0))))
4552 gfc_error ("%qs argument of %qs intrinsic at %L must not be a "
4553 "subobject of %qs", gfc_current_intrinsic_arg[0]->name,
4554 gfc_current_intrinsic, &a->where, sym->name);
4555 return false;
4558 return true;
4562 bool
4563 gfc_check_radix (gfc_expr *x)
4565 if (!int_or_real_check (x, 0))
4566 return false;
4568 return true;
4572 bool
4573 gfc_check_range (gfc_expr *x)
4575 if (!numeric_check (x, 0))
4576 return false;
4578 return true;
4582 bool
4583 gfc_check_rank (gfc_expr *a)
4585 /* Any data object is allowed; a "data object" is a "constant (4.1.3),
4586 variable (6), or subobject of a constant (2.4.3.2.3)" (F2008, 1.3.45). */
4588 bool is_variable = true;
4590 /* Functions returning pointers are regarded as variable, cf. F2008, R602. */
4591 if (a->expr_type == EXPR_FUNCTION)
4592 is_variable = a->value.function.esym
4593 ? a->value.function.esym->result->attr.pointer
4594 : a->symtree->n.sym->result->attr.pointer;
4596 if (a->expr_type == EXPR_OP
4597 || a->expr_type == EXPR_NULL
4598 || a->expr_type == EXPR_COMPCALL
4599 || a->expr_type == EXPR_PPC
4600 || a->ts.type == BT_PROCEDURE
4601 || !is_variable)
4603 gfc_error ("The argument of the RANK intrinsic at %L must be a data "
4604 "object", &a->where);
4605 return false;
4608 return true;
4612 bool
4613 gfc_check_real (gfc_expr *a, gfc_expr *kind)
4615 if (!kind_check (kind, 1, BT_REAL))
4616 return false;
4618 /* BOZ is dealt with in gfc_simplify_real. */
4619 if (a->ts.type == BT_BOZ)
4620 return true;
4622 if (!numeric_check (a, 0))
4623 return false;
4625 return true;
4629 bool
4630 gfc_check_rename (gfc_expr *path1, gfc_expr *path2)
4632 if (!type_check (path1, 0, BT_CHARACTER))
4633 return false;
4634 if (!kind_value_check (path1, 0, gfc_default_character_kind))
4635 return false;
4637 if (!type_check (path2, 1, BT_CHARACTER))
4638 return false;
4639 if (!kind_value_check (path2, 1, gfc_default_character_kind))
4640 return false;
4642 return true;
4646 bool
4647 gfc_check_rename_sub (gfc_expr *path1, gfc_expr *path2, gfc_expr *status)
4649 if (!type_check (path1, 0, BT_CHARACTER))
4650 return false;
4651 if (!kind_value_check (path1, 0, gfc_default_character_kind))
4652 return false;
4654 if (!type_check (path2, 1, BT_CHARACTER))
4655 return false;
4656 if (!kind_value_check (path2, 1, gfc_default_character_kind))
4657 return false;
4659 if (status == NULL)
4660 return true;
4662 if (!type_check (status, 2, BT_INTEGER))
4663 return false;
4665 if (!scalar_check (status, 2))
4666 return false;
4668 return true;
4672 bool
4673 gfc_check_repeat (gfc_expr *x, gfc_expr *y)
4675 if (!type_check (x, 0, BT_CHARACTER))
4676 return false;
4678 if (!scalar_check (x, 0))
4679 return false;
4681 if (!type_check (y, 0, BT_INTEGER))
4682 return false;
4684 if (!scalar_check (y, 1))
4685 return false;
4687 return true;
4691 bool
4692 gfc_check_reshape (gfc_expr *source, gfc_expr *shape,
4693 gfc_expr *pad, gfc_expr *order)
4695 mpz_t size;
4696 mpz_t nelems;
4697 int shape_size;
4699 if (!array_check (source, 0))
4700 return false;
4702 if (!rank_check (shape, 1, 1))
4703 return false;
4705 if (!type_check (shape, 1, BT_INTEGER))
4706 return false;
4708 if (!gfc_array_size (shape, &size))
4710 gfc_error ("%<shape%> argument of %<reshape%> intrinsic at %L must be an "
4711 "array of constant size", &shape->where);
4712 return false;
4715 shape_size = mpz_get_ui (size);
4716 mpz_clear (size);
4718 if (shape_size <= 0)
4720 gfc_error ("%qs argument of %qs intrinsic at %L is empty",
4721 gfc_current_intrinsic_arg[1]->name, gfc_current_intrinsic,
4722 &shape->where);
4723 return false;
4725 else if (shape_size > GFC_MAX_DIMENSIONS)
4727 gfc_error ("%<shape%> argument of %<reshape%> intrinsic at %L has more "
4728 "than %d elements", &shape->where, GFC_MAX_DIMENSIONS);
4729 return false;
4731 else if (shape->expr_type == EXPR_ARRAY && gfc_is_constant_expr (shape))
4733 gfc_expr *e;
4734 int i, extent;
4735 for (i = 0; i < shape_size; ++i)
4737 e = gfc_constructor_lookup_expr (shape->value.constructor, i);
4738 if (e->expr_type != EXPR_CONSTANT)
4739 continue;
4741 gfc_extract_int (e, &extent);
4742 if (extent < 0)
4744 gfc_error ("%qs argument of %qs intrinsic at %L has "
4745 "negative element (%d)",
4746 gfc_current_intrinsic_arg[1]->name,
4747 gfc_current_intrinsic, &e->where, extent);
4748 return false;
4752 else if (shape->expr_type == EXPR_VARIABLE && shape->ref
4753 && shape->ref->u.ar.type == AR_FULL && shape->ref->u.ar.dimen == 1
4754 && shape->ref->u.ar.as
4755 && shape->ref->u.ar.as->lower[0]->expr_type == EXPR_CONSTANT
4756 && shape->ref->u.ar.as->lower[0]->ts.type == BT_INTEGER
4757 && shape->ref->u.ar.as->upper[0]->expr_type == EXPR_CONSTANT
4758 && shape->ref->u.ar.as->upper[0]->ts.type == BT_INTEGER
4759 && shape->symtree->n.sym->attr.flavor == FL_PARAMETER
4760 && shape->symtree->n.sym->value)
4762 int i, extent;
4763 gfc_expr *e, *v;
4765 v = shape->symtree->n.sym->value;
4767 for (i = 0; i < shape_size; i++)
4769 e = gfc_constructor_lookup_expr (v->value.constructor, i);
4770 if (e == NULL)
4771 break;
4773 gfc_extract_int (e, &extent);
4775 if (extent < 0)
4777 gfc_error ("Element %d of actual argument of RESHAPE at %L "
4778 "cannot be negative", i + 1, &shape->where);
4779 return false;
4784 if (pad != NULL)
4786 if (!same_type_check (source, 0, pad, 2))
4787 return false;
4789 if (!array_check (pad, 2))
4790 return false;
4793 if (order != NULL)
4795 if (!array_check (order, 3))
4796 return false;
4798 if (!type_check (order, 3, BT_INTEGER))
4799 return false;
4801 if (order->expr_type == EXPR_ARRAY && gfc_is_constant_expr (order))
4803 int i, order_size, dim, perm[GFC_MAX_DIMENSIONS];
4804 gfc_expr *e;
4806 for (i = 0; i < GFC_MAX_DIMENSIONS; ++i)
4807 perm[i] = 0;
4809 gfc_array_size (order, &size);
4810 order_size = mpz_get_ui (size);
4811 mpz_clear (size);
4813 if (order_size != shape_size)
4815 gfc_error ("%qs argument of %qs intrinsic at %L "
4816 "has wrong number of elements (%d/%d)",
4817 gfc_current_intrinsic_arg[3]->name,
4818 gfc_current_intrinsic, &order->where,
4819 order_size, shape_size);
4820 return false;
4823 for (i = 1; i <= order_size; ++i)
4825 e = gfc_constructor_lookup_expr (order->value.constructor, i-1);
4826 if (e->expr_type != EXPR_CONSTANT)
4827 continue;
4829 gfc_extract_int (e, &dim);
4831 if (dim < 1 || dim > order_size)
4833 gfc_error ("%qs argument of %qs intrinsic at %L "
4834 "has out-of-range dimension (%d)",
4835 gfc_current_intrinsic_arg[3]->name,
4836 gfc_current_intrinsic, &e->where, dim);
4837 return false;
4840 if (perm[dim-1] != 0)
4842 gfc_error ("%qs argument of %qs intrinsic at %L has "
4843 "invalid permutation of dimensions (dimension "
4844 "%qd duplicated)",
4845 gfc_current_intrinsic_arg[3]->name,
4846 gfc_current_intrinsic, &e->where, dim);
4847 return false;
4850 perm[dim-1] = 1;
4855 if (pad == NULL && shape->expr_type == EXPR_ARRAY
4856 && gfc_is_constant_expr (shape)
4857 && !(source->expr_type == EXPR_VARIABLE && source->symtree->n.sym->as
4858 && source->symtree->n.sym->as->type == AS_ASSUMED_SIZE))
4860 /* Check the match in size between source and destination. */
4861 if (gfc_array_size (source, &nelems))
4863 gfc_constructor *c;
4864 bool test;
4867 mpz_init_set_ui (size, 1);
4868 for (c = gfc_constructor_first (shape->value.constructor);
4869 c; c = gfc_constructor_next (c))
4870 mpz_mul (size, size, c->expr->value.integer);
4872 test = mpz_cmp (nelems, size) < 0 && mpz_cmp_ui (size, 0) > 0;
4873 mpz_clear (nelems);
4874 mpz_clear (size);
4876 if (test)
4878 gfc_error ("Without padding, there are not enough elements "
4879 "in the intrinsic RESHAPE source at %L to match "
4880 "the shape", &source->where);
4881 return false;
4886 return true;
4890 bool
4891 gfc_check_same_type_as (gfc_expr *a, gfc_expr *b)
4893 if (a->ts.type != BT_DERIVED && a->ts.type != BT_CLASS)
4895 gfc_error ("%qs argument of %qs intrinsic at %L "
4896 "cannot be of type %s",
4897 gfc_current_intrinsic_arg[0]->name,
4898 gfc_current_intrinsic,
4899 &a->where, gfc_typename (a));
4900 return false;
4903 if (!(gfc_type_is_extensible (a->ts.u.derived) || UNLIMITED_POLY (a)))
4905 gfc_error ("%qs argument of %qs intrinsic at %L "
4906 "must be of an extensible type",
4907 gfc_current_intrinsic_arg[0]->name, gfc_current_intrinsic,
4908 &a->where);
4909 return false;
4912 if (b->ts.type != BT_DERIVED && b->ts.type != BT_CLASS)
4914 gfc_error ("%qs argument of %qs intrinsic at %L "
4915 "cannot be of type %s",
4916 gfc_current_intrinsic_arg[0]->name,
4917 gfc_current_intrinsic,
4918 &b->where, gfc_typename (b));
4919 return false;
4922 if (!(gfc_type_is_extensible (b->ts.u.derived) || UNLIMITED_POLY (b)))
4924 gfc_error ("%qs argument of %qs intrinsic at %L "
4925 "must be of an extensible type",
4926 gfc_current_intrinsic_arg[1]->name, gfc_current_intrinsic,
4927 &b->where);
4928 return false;
4931 return true;
4935 bool
4936 gfc_check_scale (gfc_expr *x, gfc_expr *i)
4938 if (!type_check (x, 0, BT_REAL))
4939 return false;
4941 if (!type_check (i, 1, BT_INTEGER))
4942 return false;
4944 return true;
4948 bool
4949 gfc_check_scan (gfc_expr *x, gfc_expr *y, gfc_expr *z, gfc_expr *kind)
4951 if (!type_check (x, 0, BT_CHARACTER))
4952 return false;
4954 if (!type_check (y, 1, BT_CHARACTER))
4955 return false;
4957 if (z != NULL && !type_check (z, 2, BT_LOGICAL))
4958 return false;
4960 if (!kind_check (kind, 3, BT_INTEGER))
4961 return false;
4962 if (kind && !gfc_notify_std (GFC_STD_F2003, "%qs intrinsic "
4963 "with KIND argument at %L",
4964 gfc_current_intrinsic, &kind->where))
4965 return false;
4967 if (!same_type_check (x, 0, y, 1))
4968 return false;
4970 return true;
4974 bool
4975 gfc_check_secnds (gfc_expr *r)
4977 if (!type_check (r, 0, BT_REAL))
4978 return false;
4980 if (!kind_value_check (r, 0, 4))
4981 return false;
4983 if (!scalar_check (r, 0))
4984 return false;
4986 return true;
4990 bool
4991 gfc_check_selected_char_kind (gfc_expr *name)
4993 if (!type_check (name, 0, BT_CHARACTER))
4994 return false;
4996 if (!kind_value_check (name, 0, gfc_default_character_kind))
4997 return false;
4999 if (!scalar_check (name, 0))
5000 return false;
5002 return true;
5006 bool
5007 gfc_check_selected_int_kind (gfc_expr *r)
5009 if (!type_check (r, 0, BT_INTEGER))
5010 return false;
5012 if (!scalar_check (r, 0))
5013 return false;
5015 return true;
5019 bool
5020 gfc_check_selected_real_kind (gfc_expr *p, gfc_expr *r, gfc_expr *radix)
5022 if (p == NULL && r == NULL
5023 && !gfc_notify_std (GFC_STD_F2008, "SELECTED_REAL_KIND with"
5024 " neither %<P%> nor %<R%> argument at %L",
5025 gfc_current_intrinsic_where))
5026 return false;
5028 if (p)
5030 if (!type_check (p, 0, BT_INTEGER))
5031 return false;
5033 if (!scalar_check (p, 0))
5034 return false;
5037 if (r)
5039 if (!type_check (r, 1, BT_INTEGER))
5040 return false;
5042 if (!scalar_check (r, 1))
5043 return false;
5046 if (radix)
5048 if (!type_check (radix, 1, BT_INTEGER))
5049 return false;
5051 if (!scalar_check (radix, 1))
5052 return false;
5054 if (!gfc_notify_std (GFC_STD_F2008, "%qs intrinsic with "
5055 "RADIX argument at %L", gfc_current_intrinsic,
5056 &radix->where))
5057 return false;
5060 return true;
5064 bool
5065 gfc_check_set_exponent (gfc_expr *x, gfc_expr *i)
5067 if (!type_check (x, 0, BT_REAL))
5068 return false;
5070 if (!type_check (i, 1, BT_INTEGER))
5071 return false;
5073 return true;
5077 bool
5078 gfc_check_shape (gfc_expr *source, gfc_expr *kind)
5080 gfc_array_ref *ar;
5082 if (gfc_invalid_null_arg (source))
5083 return false;
5085 if (source->rank == 0 || source->expr_type != EXPR_VARIABLE)
5086 return true;
5088 ar = gfc_find_array_ref (source);
5090 if (ar->as && ar->as->type == AS_ASSUMED_SIZE && ar->type == AR_FULL)
5092 gfc_error ("%<source%> argument of %<shape%> intrinsic at %L must not be "
5093 "an assumed size array", &source->where);
5094 return false;
5097 if (!kind_check (kind, 1, BT_INTEGER))
5098 return false;
5099 if (kind && !gfc_notify_std (GFC_STD_F2003, "%qs intrinsic "
5100 "with KIND argument at %L",
5101 gfc_current_intrinsic, &kind->where))
5102 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 /* TYPE(*) is acceptable if and only if it uses an array descriptor. */
5179 if (arg->ts.type == BT_ASSUMED
5180 && (arg->symtree->n.sym->as == NULL
5181 || (arg->symtree->n.sym->as->type != AS_ASSUMED_SHAPE
5182 && arg->symtree->n.sym->as->type != AS_DEFERRED
5183 && arg->symtree->n.sym->as->type != AS_ASSUMED_RANK)))
5185 gfc_error ("%qs argument of %qs intrinsic at %L shall not be TYPE(*)",
5186 gfc_current_intrinsic_arg[0]->name, gfc_current_intrinsic,
5187 &arg->where);
5188 return false;
5191 if (arg->rank && arg->expr_type == EXPR_VARIABLE
5192 && arg->symtree->n.sym->as != NULL
5193 && arg->symtree->n.sym->as->type == AS_ASSUMED_SIZE && arg->ref
5194 && arg->ref->type == REF_ARRAY && arg->ref->u.ar.type == AR_FULL)
5196 gfc_error ("%qs argument of %qs intrinsic at %L shall not be an "
5197 "assumed-size array", gfc_current_intrinsic_arg[0]->name,
5198 gfc_current_intrinsic, &arg->where);
5199 return false;
5202 return true;
5206 /* Check whether an expression is interoperable. When returning false,
5207 msg is set to a string telling why the expression is not interoperable,
5208 otherwise, it is set to NULL. The msg string can be used in diagnostics.
5209 If c_loc is true, character with len > 1 are allowed (cf. Fortran
5210 2003corr5); additionally, assumed-shape/assumed-rank/deferred-shape
5211 arrays are permitted. And if c_f_ptr is true, deferred-shape arrays
5212 are permitted. */
5214 static bool
5215 is_c_interoperable (gfc_expr *expr, const char **msg, bool c_loc, bool c_f_ptr)
5217 *msg = NULL;
5219 if (expr->ts.type == BT_CLASS)
5221 *msg = "Expression is polymorphic";
5222 return false;
5225 if (expr->ts.type == BT_DERIVED && !expr->ts.u.derived->attr.is_bind_c
5226 && !expr->ts.u.derived->ts.is_iso_c)
5228 *msg = "Expression is a noninteroperable derived type";
5229 return false;
5232 if (expr->ts.type == BT_PROCEDURE)
5234 *msg = "Procedure unexpected as argument";
5235 return false;
5238 if (gfc_notification_std (GFC_STD_GNU) && expr->ts.type == BT_LOGICAL)
5240 int i;
5241 for (i = 0; gfc_logical_kinds[i].kind; i++)
5242 if (gfc_logical_kinds[i].kind == expr->ts.kind)
5243 return true;
5244 *msg = "Extension to use a non-C_Bool-kind LOGICAL";
5245 return false;
5248 if (gfc_notification_std (GFC_STD_GNU) && expr->ts.type == BT_CHARACTER
5249 && expr->ts.kind != 1)
5251 *msg = "Extension to use a non-C_CHAR-kind CHARACTER";
5252 return false;
5255 if (expr->ts.type == BT_CHARACTER) {
5256 if (expr->ts.deferred)
5258 /* TS 29113 allows deferred-length strings as dummy arguments,
5259 but it is not an interoperable type. */
5260 *msg = "Expression shall not be a deferred-length string";
5261 return false;
5264 if (expr->ts.u.cl && expr->ts.u.cl->length
5265 && !gfc_simplify_expr (expr->ts.u.cl->length, 0))
5266 gfc_internal_error ("is_c_interoperable(): gfc_simplify_expr failed");
5268 if (!c_loc && expr->ts.u.cl
5269 && (!expr->ts.u.cl->length
5270 || expr->ts.u.cl->length->expr_type != EXPR_CONSTANT
5271 || mpz_cmp_si (expr->ts.u.cl->length->value.integer, 1) != 0))
5273 *msg = "Type shall have a character length of 1";
5274 return false;
5278 /* Note: The following checks are about interoperatable variables, Fortran
5279 15.3.5/15.3.6. In intrinsics like C_LOC or in procedure interface, more
5280 is allowed, e.g. assumed-shape arrays with TS 29113. */
5282 if (gfc_is_coarray (expr))
5284 *msg = "Coarrays are not interoperable";
5285 return false;
5288 if (!c_loc && expr->rank > 0 && expr->expr_type != EXPR_ARRAY)
5290 gfc_array_ref *ar = gfc_find_array_ref (expr);
5291 if (ar->type != AR_FULL)
5293 *msg = "Only whole-arrays are interoperable";
5294 return false;
5296 if (!c_f_ptr && ar->as->type != AS_EXPLICIT
5297 && ar->as->type != AS_ASSUMED_SIZE)
5299 *msg = "Only explicit-size and assumed-size arrays are interoperable";
5300 return false;
5304 return true;
5308 bool
5309 gfc_check_c_sizeof (gfc_expr *arg)
5311 const char *msg;
5313 if (!is_c_interoperable (arg, &msg, false, false))
5315 gfc_error ("%qs argument of %qs intrinsic at %L must be an "
5316 "interoperable data entity: %s",
5317 gfc_current_intrinsic_arg[0]->name, gfc_current_intrinsic,
5318 &arg->where, msg);
5319 return false;
5322 if (arg->ts.type == BT_ASSUMED)
5324 gfc_error ("%qs argument of %qs intrinsic at %L shall not be "
5325 "TYPE(*)",
5326 gfc_current_intrinsic_arg[0]->name, gfc_current_intrinsic,
5327 &arg->where);
5328 return false;
5331 if (arg->rank && arg->expr_type == EXPR_VARIABLE
5332 && arg->symtree->n.sym->as != NULL
5333 && arg->symtree->n.sym->as->type == AS_ASSUMED_SIZE && arg->ref
5334 && arg->ref->type == REF_ARRAY && arg->ref->u.ar.type == AR_FULL)
5336 gfc_error ("%qs argument of %qs intrinsic at %L shall not be an "
5337 "assumed-size array", gfc_current_intrinsic_arg[0]->name,
5338 gfc_current_intrinsic, &arg->where);
5339 return false;
5342 return true;
5346 bool
5347 gfc_check_c_associated (gfc_expr *c_ptr_1, gfc_expr *c_ptr_2)
5349 if (c_ptr_1->ts.type != BT_DERIVED
5350 || c_ptr_1->ts.u.derived->from_intmod != INTMOD_ISO_C_BINDING
5351 || (c_ptr_1->ts.u.derived->intmod_sym_id != ISOCBINDING_PTR
5352 && c_ptr_1->ts.u.derived->intmod_sym_id != ISOCBINDING_FUNPTR))
5354 gfc_error ("Argument C_PTR_1 at %L to C_ASSOCIATED shall have the "
5355 "type TYPE(C_PTR) or TYPE(C_FUNPTR)", &c_ptr_1->where);
5356 return false;
5359 if (!scalar_check (c_ptr_1, 0))
5360 return false;
5362 if (c_ptr_2
5363 && (c_ptr_2->ts.type != BT_DERIVED
5364 || c_ptr_2->ts.u.derived->from_intmod != INTMOD_ISO_C_BINDING
5365 || (c_ptr_1->ts.u.derived->intmod_sym_id
5366 != c_ptr_2->ts.u.derived->intmod_sym_id)))
5368 gfc_error ("Argument C_PTR_2 at %L to C_ASSOCIATED shall have the "
5369 "same type as C_PTR_1: %s instead of %s", &c_ptr_1->where,
5370 gfc_typename (&c_ptr_1->ts),
5371 gfc_typename (&c_ptr_2->ts));
5372 return false;
5375 if (c_ptr_2 && !scalar_check (c_ptr_2, 1))
5376 return false;
5378 return true;
5382 bool
5383 gfc_check_c_f_pointer (gfc_expr *cptr, gfc_expr *fptr, gfc_expr *shape)
5385 symbol_attribute attr;
5386 const char *msg;
5388 if (cptr->ts.type != BT_DERIVED
5389 || cptr->ts.u.derived->from_intmod != INTMOD_ISO_C_BINDING
5390 || cptr->ts.u.derived->intmod_sym_id != ISOCBINDING_PTR)
5392 gfc_error ("Argument CPTR at %L to C_F_POINTER shall have the "
5393 "type TYPE(C_PTR)", &cptr->where);
5394 return false;
5397 if (!scalar_check (cptr, 0))
5398 return false;
5400 attr = gfc_expr_attr (fptr);
5402 if (!attr.pointer)
5404 gfc_error ("Argument FPTR at %L to C_F_POINTER must be a pointer",
5405 &fptr->where);
5406 return false;
5409 if (fptr->ts.type == BT_CLASS)
5411 gfc_error ("FPTR argument at %L to C_F_POINTER shall not be polymorphic",
5412 &fptr->where);
5413 return false;
5416 if (gfc_is_coindexed (fptr))
5418 gfc_error ("Argument FPTR at %L to C_F_POINTER shall not be "
5419 "coindexed", &fptr->where);
5420 return false;
5423 if (fptr->rank == 0 && shape)
5425 gfc_error ("Unexpected SHAPE argument at %L to C_F_POINTER with scalar "
5426 "FPTR", &fptr->where);
5427 return false;
5429 else if (fptr->rank && !shape)
5431 gfc_error ("Expected SHAPE argument to C_F_POINTER with array "
5432 "FPTR at %L", &fptr->where);
5433 return false;
5436 if (shape && !rank_check (shape, 2, 1))
5437 return false;
5439 if (shape && !type_check (shape, 2, BT_INTEGER))
5440 return false;
5442 if (shape)
5444 mpz_t size;
5445 if (gfc_array_size (shape, &size))
5447 if (mpz_cmp_ui (size, fptr->rank) != 0)
5449 mpz_clear (size);
5450 gfc_error ("SHAPE argument at %L to C_F_POINTER must have the same "
5451 "size as the RANK of FPTR", &shape->where);
5452 return false;
5454 mpz_clear (size);
5458 if (fptr->ts.type == BT_CLASS)
5460 gfc_error ("Polymorphic FPTR at %L to C_F_POINTER", &fptr->where);
5461 return false;
5464 if (fptr->rank > 0 && !is_c_interoperable (fptr, &msg, false, true))
5465 return gfc_notify_std (GFC_STD_F2018, "Noninteroperable array FPTR "
5466 "at %L to C_F_POINTER: %s", &fptr->where, msg);
5468 return true;
5472 bool
5473 gfc_check_c_f_procpointer (gfc_expr *cptr, gfc_expr *fptr)
5475 symbol_attribute attr;
5477 if (cptr->ts.type != BT_DERIVED
5478 || cptr->ts.u.derived->from_intmod != INTMOD_ISO_C_BINDING
5479 || cptr->ts.u.derived->intmod_sym_id != ISOCBINDING_FUNPTR)
5481 gfc_error ("Argument CPTR at %L to C_F_PROCPOINTER shall have the "
5482 "type TYPE(C_FUNPTR)", &cptr->where);
5483 return false;
5486 if (!scalar_check (cptr, 0))
5487 return false;
5489 attr = gfc_expr_attr (fptr);
5491 if (!attr.proc_pointer)
5493 gfc_error ("Argument FPTR at %L to C_F_PROCPOINTER shall be a procedure "
5494 "pointer", &fptr->where);
5495 return false;
5498 if (gfc_is_coindexed (fptr))
5500 gfc_error ("Argument FPTR at %L to C_F_PROCPOINTER shall not be "
5501 "coindexed", &fptr->where);
5502 return false;
5505 if (!attr.is_bind_c)
5506 return gfc_notify_std (GFC_STD_F2018, "Noninteroperable procedure "
5507 "pointer at %L to C_F_PROCPOINTER", &fptr->where);
5509 return true;
5513 bool
5514 gfc_check_c_funloc (gfc_expr *x)
5516 symbol_attribute attr;
5518 if (gfc_is_coindexed (x))
5520 gfc_error ("Argument X at %L to C_FUNLOC shall not be "
5521 "coindexed", &x->where);
5522 return false;
5525 attr = gfc_expr_attr (x);
5527 if (attr.function && !attr.proc_pointer && x->expr_type == EXPR_VARIABLE
5528 && x->symtree->n.sym == x->symtree->n.sym->result)
5529 for (gfc_namespace *ns = gfc_current_ns; ns; ns = ns->parent)
5530 if (x->symtree->n.sym == ns->proc_name)
5532 gfc_error ("Function result %qs at %L is invalid as X argument "
5533 "to C_FUNLOC", x->symtree->n.sym->name, &x->where);
5534 return false;
5537 if (attr.flavor != FL_PROCEDURE)
5539 gfc_error ("Argument X at %L to C_FUNLOC shall be a procedure "
5540 "or a procedure pointer", &x->where);
5541 return false;
5544 if (!attr.is_bind_c)
5545 return gfc_notify_std (GFC_STD_F2018, "Noninteroperable procedure "
5546 "at %L to C_FUNLOC", &x->where);
5547 return true;
5551 bool
5552 gfc_check_c_loc (gfc_expr *x)
5554 symbol_attribute attr;
5555 const char *msg;
5557 if (gfc_is_coindexed (x))
5559 gfc_error ("Argument X at %L to C_LOC shall not be coindexed", &x->where);
5560 return false;
5563 if (x->ts.type == BT_CLASS)
5565 gfc_error ("X argument at %L to C_LOC shall not be polymorphic",
5566 &x->where);
5567 return false;
5570 attr = gfc_expr_attr (x);
5572 if (!attr.pointer
5573 && (x->expr_type != EXPR_VARIABLE || !attr.target
5574 || attr.flavor == FL_PARAMETER))
5576 gfc_error ("Argument X at %L to C_LOC shall have either "
5577 "the POINTER or the TARGET attribute", &x->where);
5578 return false;
5581 if (x->ts.type == BT_CHARACTER
5582 && gfc_var_strlen (x) == 0)
5584 gfc_error ("Argument X at %L to C_LOC shall be not be a zero-sized "
5585 "string", &x->where);
5586 return false;
5589 if (!is_c_interoperable (x, &msg, true, false))
5591 if (x->ts.type == BT_CLASS)
5593 gfc_error ("Argument at %L to C_LOC shall not be polymorphic",
5594 &x->where);
5595 return false;
5598 if (x->rank
5599 && !gfc_notify_std (GFC_STD_F2018,
5600 "Noninteroperable array at %L as"
5601 " argument to C_LOC: %s", &x->where, msg))
5602 return false;
5604 else if (x->rank > 0 && gfc_notification_std (GFC_STD_F2008))
5606 gfc_array_ref *ar = gfc_find_array_ref (x);
5608 if (ar->as->type != AS_EXPLICIT && ar->as->type != AS_ASSUMED_SIZE
5609 && !attr.allocatable
5610 && !gfc_notify_std (GFC_STD_F2008,
5611 "Array of interoperable type at %L "
5612 "to C_LOC which is nonallocatable and neither "
5613 "assumed size nor explicit size", &x->where))
5614 return false;
5615 else if (ar->type != AR_FULL
5616 && !gfc_notify_std (GFC_STD_F2008, "Array section at %L "
5617 "to C_LOC", &x->where))
5618 return false;
5621 return true;
5625 bool
5626 gfc_check_sleep_sub (gfc_expr *seconds)
5628 if (!type_check (seconds, 0, BT_INTEGER))
5629 return false;
5631 if (!scalar_check (seconds, 0))
5632 return false;
5634 return true;
5637 bool
5638 gfc_check_sngl (gfc_expr *a)
5640 if (!type_check (a, 0, BT_REAL))
5641 return false;
5643 if ((a->ts.kind != gfc_default_double_kind)
5644 && !gfc_notify_std (GFC_STD_GNU, "non double precision "
5645 "REAL argument to %s intrinsic at %L",
5646 gfc_current_intrinsic, &a->where))
5647 return false;
5649 return true;
5652 bool
5653 gfc_check_spread (gfc_expr *source, gfc_expr *dim, gfc_expr *ncopies)
5655 if (gfc_invalid_null_arg (source))
5656 return false;
5658 if (source->rank >= GFC_MAX_DIMENSIONS)
5660 gfc_error ("%qs argument of %qs intrinsic at %L must be less "
5661 "than rank %d", gfc_current_intrinsic_arg[0]->name,
5662 gfc_current_intrinsic, &source->where, GFC_MAX_DIMENSIONS);
5664 return false;
5667 if (dim == NULL)
5668 return false;
5670 if (!dim_check (dim, 1, false))
5671 return false;
5673 /* dim_rank_check() does not apply here. */
5674 if (dim
5675 && dim->expr_type == EXPR_CONSTANT
5676 && (mpz_cmp_ui (dim->value.integer, 1) < 0
5677 || mpz_cmp_ui (dim->value.integer, source->rank + 1) > 0))
5679 gfc_error ("%qs argument of %qs intrinsic at %L is not a valid "
5680 "dimension index", gfc_current_intrinsic_arg[1]->name,
5681 gfc_current_intrinsic, &dim->where);
5682 return false;
5685 if (!type_check (ncopies, 2, BT_INTEGER))
5686 return false;
5688 if (!scalar_check (ncopies, 2))
5689 return false;
5691 return true;
5695 /* Functions for checking FGETC, FPUTC, FGET and FPUT (subroutines and
5696 functions). */
5698 bool
5699 arg_strlen_is_zero (gfc_expr *c, int n)
5701 if (gfc_var_strlen (c) == 0)
5703 gfc_error ("%qs argument of %qs intrinsic at %L must have "
5704 "length at least 1", gfc_current_intrinsic_arg[n]->name,
5705 gfc_current_intrinsic, &c->where);
5706 return true;
5708 return false;
5711 bool
5712 gfc_check_fgetputc_sub (gfc_expr *unit, gfc_expr *c, gfc_expr *status)
5714 if (!type_check (unit, 0, BT_INTEGER))
5715 return false;
5717 if (!scalar_check (unit, 0))
5718 return false;
5720 if (!type_check (c, 1, BT_CHARACTER))
5721 return false;
5722 if (!kind_value_check (c, 1, gfc_default_character_kind))
5723 return false;
5724 if (strcmp (gfc_current_intrinsic, "fgetc") == 0
5725 && !variable_check (c, 1, false))
5726 return false;
5727 if (arg_strlen_is_zero (c, 1))
5728 return false;
5730 if (status == NULL)
5731 return true;
5733 if (!type_check (status, 2, BT_INTEGER)
5734 || !kind_value_check (status, 2, gfc_default_integer_kind)
5735 || !scalar_check (status, 2)
5736 || !variable_check (status, 2, false))
5737 return false;
5739 return true;
5743 bool
5744 gfc_check_fgetputc (gfc_expr *unit, gfc_expr *c)
5746 return gfc_check_fgetputc_sub (unit, c, NULL);
5750 bool
5751 gfc_check_fgetput_sub (gfc_expr *c, gfc_expr *status)
5753 if (!type_check (c, 0, BT_CHARACTER))
5754 return false;
5755 if (!kind_value_check (c, 0, gfc_default_character_kind))
5756 return false;
5757 if (strcmp (gfc_current_intrinsic, "fget") == 0
5758 && !variable_check (c, 0, false))
5759 return false;
5760 if (arg_strlen_is_zero (c, 0))
5761 return false;
5763 if (status == NULL)
5764 return true;
5766 if (!type_check (status, 1, BT_INTEGER)
5767 || !kind_value_check (status, 1, gfc_default_integer_kind)
5768 || !scalar_check (status, 1)
5769 || !variable_check (status, 1, false))
5770 return false;
5772 return true;
5776 bool
5777 gfc_check_fgetput (gfc_expr *c)
5779 return gfc_check_fgetput_sub (c, NULL);
5783 bool
5784 gfc_check_fseek_sub (gfc_expr *unit, gfc_expr *offset, gfc_expr *whence, gfc_expr *status)
5786 if (!type_check (unit, 0, BT_INTEGER))
5787 return false;
5789 if (!scalar_check (unit, 0))
5790 return false;
5792 if (!type_check (offset, 1, BT_INTEGER))
5793 return false;
5795 if (!scalar_check (offset, 1))
5796 return false;
5798 if (!type_check (whence, 2, BT_INTEGER))
5799 return false;
5801 if (!scalar_check (whence, 2))
5802 return false;
5804 if (status == NULL)
5805 return true;
5807 if (!type_check (status, 3, BT_INTEGER))
5808 return false;
5810 if (!kind_value_check (status, 3, 4))
5811 return false;
5813 if (!scalar_check (status, 3))
5814 return false;
5816 return true;
5821 bool
5822 gfc_check_fstat (gfc_expr *unit, gfc_expr *array)
5824 if (!type_check (unit, 0, BT_INTEGER))
5825 return false;
5827 if (!scalar_check (unit, 0))
5828 return false;
5830 if (!type_check (array, 1, BT_INTEGER)
5831 || !kind_value_check (unit, 0, gfc_default_integer_kind))
5832 return false;
5834 if (!array_check (array, 1))
5835 return false;
5837 return true;
5841 bool
5842 gfc_check_fstat_sub (gfc_expr *unit, gfc_expr *array, gfc_expr *status)
5844 if (!type_check (unit, 0, BT_INTEGER))
5845 return false;
5847 if (!scalar_check (unit, 0))
5848 return false;
5850 if (!type_check (array, 1, BT_INTEGER)
5851 || !kind_value_check (array, 1, gfc_default_integer_kind))
5852 return false;
5854 if (!array_check (array, 1))
5855 return false;
5857 if (status == NULL)
5858 return true;
5860 if (!type_check (status, 2, BT_INTEGER)
5861 || !kind_value_check (status, 2, gfc_default_integer_kind))
5862 return false;
5864 if (!scalar_check (status, 2))
5865 return false;
5867 return true;
5871 bool
5872 gfc_check_ftell (gfc_expr *unit)
5874 if (!type_check (unit, 0, BT_INTEGER))
5875 return false;
5877 if (!scalar_check (unit, 0))
5878 return false;
5880 return true;
5884 bool
5885 gfc_check_ftell_sub (gfc_expr *unit, gfc_expr *offset)
5887 if (!type_check (unit, 0, BT_INTEGER))
5888 return false;
5890 if (!scalar_check (unit, 0))
5891 return false;
5893 if (!type_check (offset, 1, BT_INTEGER))
5894 return false;
5896 if (!scalar_check (offset, 1))
5897 return false;
5899 return true;
5903 bool
5904 gfc_check_stat (gfc_expr *name, gfc_expr *array)
5906 if (!type_check (name, 0, BT_CHARACTER))
5907 return false;
5908 if (!kind_value_check (name, 0, gfc_default_character_kind))
5909 return false;
5911 if (!type_check (array, 1, BT_INTEGER)
5912 || !kind_value_check (array, 1, gfc_default_integer_kind))
5913 return false;
5915 if (!array_check (array, 1))
5916 return false;
5918 return true;
5922 bool
5923 gfc_check_stat_sub (gfc_expr *name, gfc_expr *array, gfc_expr *status)
5925 if (!type_check (name, 0, BT_CHARACTER))
5926 return false;
5927 if (!kind_value_check (name, 0, gfc_default_character_kind))
5928 return false;
5930 if (!type_check (array, 1, BT_INTEGER)
5931 || !kind_value_check (array, 1, gfc_default_integer_kind))
5932 return false;
5934 if (!array_check (array, 1))
5935 return false;
5937 if (status == NULL)
5938 return true;
5940 if (!type_check (status, 2, BT_INTEGER)
5941 || !kind_value_check (array, 1, gfc_default_integer_kind))
5942 return false;
5944 if (!scalar_check (status, 2))
5945 return false;
5947 return true;
5951 bool
5952 gfc_check_image_index (gfc_expr *coarray, gfc_expr *sub)
5954 mpz_t nelems;
5956 if (flag_coarray == GFC_FCOARRAY_NONE)
5958 gfc_fatal_error ("Coarrays disabled at %C, use %<-fcoarray=%> to enable");
5959 return false;
5962 if (!coarray_check (coarray, 0))
5963 return false;
5965 if (sub->rank != 1)
5967 gfc_error ("%s argument to IMAGE_INDEX must be a rank one array at %L",
5968 gfc_current_intrinsic_arg[1]->name, &sub->where);
5969 return false;
5972 if (gfc_array_size (sub, &nelems))
5974 int corank = gfc_get_corank (coarray);
5976 if (mpz_cmp_ui (nelems, corank) != 0)
5978 gfc_error ("The number of array elements of the SUB argument to "
5979 "IMAGE_INDEX at %L shall be %d (corank) not %d",
5980 &sub->where, corank, (int) mpz_get_si (nelems));
5981 mpz_clear (nelems);
5982 return false;
5984 mpz_clear (nelems);
5987 return true;
5991 bool
5992 gfc_check_num_images (gfc_expr *distance, gfc_expr *failed)
5994 if (flag_coarray == GFC_FCOARRAY_NONE)
5996 gfc_fatal_error ("Coarrays disabled at %C, use %<-fcoarray=%> to enable");
5997 return false;
6000 if (distance)
6002 if (!type_check (distance, 0, BT_INTEGER))
6003 return false;
6005 if (!nonnegative_check ("DISTANCE", distance))
6006 return false;
6008 if (!scalar_check (distance, 0))
6009 return false;
6011 if (!gfc_notify_std (GFC_STD_F2018, "DISTANCE= argument to "
6012 "NUM_IMAGES at %L", &distance->where))
6013 return false;
6016 if (failed)
6018 if (!type_check (failed, 1, BT_LOGICAL))
6019 return false;
6021 if (!scalar_check (failed, 1))
6022 return false;
6024 if (!gfc_notify_std (GFC_STD_F2018, "FAILED= argument to "
6025 "NUM_IMAGES at %L", &failed->where))
6026 return false;
6029 return true;
6033 bool
6034 gfc_check_team_number (gfc_expr *team)
6036 if (flag_coarray == GFC_FCOARRAY_NONE)
6038 gfc_fatal_error ("Coarrays disabled at %C, use %<-fcoarray=%> to enable");
6039 return false;
6042 if (team)
6044 if (team->ts.type != BT_DERIVED
6045 || team->ts.u.derived->from_intmod != INTMOD_ISO_FORTRAN_ENV
6046 || team->ts.u.derived->intmod_sym_id != ISOFORTRAN_TEAM_TYPE)
6048 gfc_error ("TEAM argument at %L to the intrinsic TEAM_NUMBER "
6049 "shall be of type TEAM_TYPE", &team->where);
6050 return false;
6053 else
6054 return true;
6056 return true;
6060 bool
6061 gfc_check_this_image (gfc_expr *coarray, gfc_expr *dim, gfc_expr *distance)
6063 if (flag_coarray == GFC_FCOARRAY_NONE)
6065 gfc_fatal_error ("Coarrays disabled at %C, use %<-fcoarray=%> to enable");
6066 return false;
6069 if (coarray == NULL && dim == NULL && distance == NULL)
6070 return true;
6072 if (dim != NULL && coarray == NULL)
6074 gfc_error ("DIM argument without COARRAY argument not allowed for "
6075 "THIS_IMAGE intrinsic at %L", &dim->where);
6076 return false;
6079 if (distance && (coarray || dim))
6081 gfc_error ("The DISTANCE argument may not be specified together with the "
6082 "COARRAY or DIM argument in intrinsic at %L",
6083 &distance->where);
6084 return false;
6087 /* Assume that we have "this_image (distance)". */
6088 if (coarray && !gfc_is_coarray (coarray) && coarray->ts.type == BT_INTEGER)
6090 if (dim)
6092 gfc_error ("Unexpected DIM argument with noncoarray argument at %L",
6093 &coarray->where);
6094 return false;
6096 distance = coarray;
6099 if (distance)
6101 if (!type_check (distance, 2, BT_INTEGER))
6102 return false;
6104 if (!nonnegative_check ("DISTANCE", distance))
6105 return false;
6107 if (!scalar_check (distance, 2))
6108 return false;
6110 if (!gfc_notify_std (GFC_STD_F2018, "DISTANCE= argument to "
6111 "THIS_IMAGE at %L", &distance->where))
6112 return false;
6114 return true;
6117 if (!coarray_check (coarray, 0))
6118 return false;
6120 if (dim != NULL)
6122 if (!dim_check (dim, 1, false))
6123 return false;
6125 if (!dim_corank_check (dim, coarray))
6126 return false;
6129 return true;
6132 /* Calculate the sizes for transfer, used by gfc_check_transfer and also
6133 by gfc_simplify_transfer. Return false if we cannot do so. */
6135 bool
6136 gfc_calculate_transfer_sizes (gfc_expr *source, gfc_expr *mold, gfc_expr *size,
6137 size_t *source_size, size_t *result_size,
6138 size_t *result_length_p)
6140 size_t result_elt_size;
6142 if (source->expr_type == EXPR_FUNCTION)
6143 return false;
6145 if (size && size->expr_type != EXPR_CONSTANT)
6146 return false;
6148 /* Calculate the size of the source. */
6149 if (!gfc_target_expr_size (source, source_size))
6150 return false;
6152 /* Determine the size of the element. */
6153 if (!gfc_element_size (mold, &result_elt_size))
6154 return false;
6156 /* If the storage size of SOURCE is greater than zero and MOLD is an array,
6157 * a scalar with the type and type parameters of MOLD shall not have a
6158 * storage size equal to zero.
6159 * If MOLD is a scalar and SIZE is absent, the result is a scalar.
6160 * If MOLD is an array and SIZE is absent, the result is an array and of
6161 * rank one. Its size is as small as possible such that its physical
6162 * representation is not shorter than that of SOURCE.
6163 * If SIZE is present, the result is an array of rank one and size SIZE.
6165 if (result_elt_size == 0 && *source_size > 0 && !size
6166 && mold->expr_type == EXPR_ARRAY)
6168 gfc_error ("%<MOLD%> argument of %<TRANSFER%> intrinsic at %L is an "
6169 "array and shall not have storage size 0 when %<SOURCE%> "
6170 "argument has size greater than 0", &mold->where);
6171 return false;
6174 if (result_elt_size == 0 && *source_size == 0 && !size)
6176 *result_size = 0;
6177 if (result_length_p)
6178 *result_length_p = 0;
6179 return true;
6182 if ((result_elt_size > 0 && (mold->expr_type == EXPR_ARRAY || mold->rank))
6183 || size)
6185 int result_length;
6187 if (size)
6188 result_length = (size_t)mpz_get_ui (size->value.integer);
6189 else
6191 result_length = *source_size / result_elt_size;
6192 if (result_length * result_elt_size < *source_size)
6193 result_length += 1;
6196 *result_size = result_length * result_elt_size;
6197 if (result_length_p)
6198 *result_length_p = result_length;
6200 else
6201 *result_size = result_elt_size;
6203 return true;
6207 bool
6208 gfc_check_transfer (gfc_expr *source, gfc_expr *mold, gfc_expr *size)
6210 size_t source_size;
6211 size_t result_size;
6213 if (gfc_invalid_null_arg (source))
6214 return false;
6216 /* SOURCE shall be a scalar or array of any type. */
6217 if (source->ts.type == BT_PROCEDURE
6218 && source->symtree->n.sym->attr.subroutine == 1)
6220 gfc_error ("%<SOURCE%> argument of %<TRANSFER%> intrinsic at %L "
6221 "must not be a %s", &source->where,
6222 gfc_basic_typename (source->ts.type));
6223 return false;
6226 if (source->ts.type == BT_BOZ && illegal_boz_arg (source))
6227 return false;
6229 if (mold->ts.type == BT_BOZ && illegal_boz_arg (mold))
6230 return false;
6232 if (gfc_invalid_null_arg (mold))
6233 return false;
6235 /* MOLD shall be a scalar or array of any type. */
6236 if (mold->ts.type == BT_PROCEDURE
6237 && mold->symtree->n.sym->attr.subroutine == 1)
6239 gfc_error ("%<MOLD%> argument of %<TRANSFER%> intrinsic at %L "
6240 "must not be a %s", &mold->where,
6241 gfc_basic_typename (mold->ts.type));
6242 return false;
6245 if (mold->ts.type == BT_HOLLERITH)
6247 gfc_error ("%<MOLD%> argument of %<TRANSFER%> intrinsic at %L must not be"
6248 " %s", &mold->where, gfc_basic_typename (BT_HOLLERITH));
6249 return false;
6252 /* SIZE (optional) shall be an integer scalar. The corresponding actual
6253 argument shall not be an optional dummy argument. */
6254 if (size != NULL)
6256 if (!type_check (size, 2, BT_INTEGER))
6258 if (size->ts.type == BT_BOZ)
6259 reset_boz (size);
6260 return false;
6263 if (!scalar_check (size, 2))
6264 return false;
6266 if (!nonoptional_check (size, 2))
6267 return false;
6270 if (!warn_surprising)
6271 return true;
6273 /* If we can't calculate the sizes, we cannot check any more.
6274 Return true for that case. */
6276 if (!gfc_calculate_transfer_sizes (source, mold, size, &source_size,
6277 &result_size, NULL))
6278 return true;
6280 if (source_size < result_size)
6281 gfc_warning (OPT_Wsurprising,
6282 "Intrinsic TRANSFER at %L has partly undefined result: "
6283 "source size %ld < result size %ld", &source->where,
6284 (long) source_size, (long) result_size);
6286 return true;
6290 bool
6291 gfc_check_transpose (gfc_expr *matrix)
6293 if (!rank_check (matrix, 0, 2))
6294 return false;
6296 return true;
6300 bool
6301 gfc_check_ubound (gfc_expr *array, gfc_expr *dim, gfc_expr *kind)
6303 if (!array_check (array, 0))
6304 return false;
6306 if (!dim_check (dim, 1, false))
6307 return false;
6309 if (!dim_rank_check (dim, array, 0))
6310 return false;
6312 if (!kind_check (kind, 2, BT_INTEGER))
6313 return false;
6314 if (kind && !gfc_notify_std (GFC_STD_F2003, "%qs intrinsic "
6315 "with KIND argument at %L",
6316 gfc_current_intrinsic, &kind->where))
6317 return false;
6319 return true;
6323 bool
6324 gfc_check_ucobound (gfc_expr *coarray, gfc_expr *dim, gfc_expr *kind)
6326 if (flag_coarray == GFC_FCOARRAY_NONE)
6328 gfc_fatal_error ("Coarrays disabled at %C, use %<-fcoarray=%> to enable");
6329 return false;
6332 if (!coarray_check (coarray, 0))
6333 return false;
6335 if (dim != NULL)
6337 if (!dim_check (dim, 1, false))
6338 return false;
6340 if (!dim_corank_check (dim, coarray))
6341 return false;
6344 if (!kind_check (kind, 2, BT_INTEGER))
6345 return false;
6347 return true;
6351 bool
6352 gfc_check_unpack (gfc_expr *vector, gfc_expr *mask, gfc_expr *field)
6354 mpz_t vector_size;
6356 if (!rank_check (vector, 0, 1))
6357 return false;
6359 if (!array_check (mask, 1))
6360 return false;
6362 if (!type_check (mask, 1, BT_LOGICAL))
6363 return false;
6365 if (!same_type_check (vector, 0, field, 2))
6366 return false;
6368 if (mask->expr_type == EXPR_ARRAY
6369 && gfc_array_size (vector, &vector_size))
6371 int mask_true_count = 0;
6372 gfc_constructor *mask_ctor;
6373 mask_ctor = gfc_constructor_first (mask->value.constructor);
6374 while (mask_ctor)
6376 if (mask_ctor->expr->expr_type != EXPR_CONSTANT)
6378 mask_true_count = 0;
6379 break;
6382 if (mask_ctor->expr->value.logical)
6383 mask_true_count++;
6385 mask_ctor = gfc_constructor_next (mask_ctor);
6388 if (mpz_get_si (vector_size) < mask_true_count)
6390 gfc_error ("%qs argument of %qs intrinsic at %L must "
6391 "provide at least as many elements as there "
6392 "are .TRUE. values in %qs (%ld/%d)",
6393 gfc_current_intrinsic_arg[0]->name, gfc_current_intrinsic,
6394 &vector->where, gfc_current_intrinsic_arg[1]->name,
6395 mpz_get_si (vector_size), mask_true_count);
6396 return false;
6399 mpz_clear (vector_size);
6402 if (mask->rank != field->rank && field->rank != 0)
6404 gfc_error ("%qs argument of %qs intrinsic at %L must have "
6405 "the same rank as %qs or be a scalar",
6406 gfc_current_intrinsic_arg[2]->name, gfc_current_intrinsic,
6407 &field->where, gfc_current_intrinsic_arg[1]->name);
6408 return false;
6411 if (mask->rank == field->rank)
6413 int i;
6414 for (i = 0; i < field->rank; i++)
6415 if (! identical_dimen_shape (mask, i, field, i))
6417 gfc_error ("%qs and %qs arguments of %qs intrinsic at %L "
6418 "must have identical shape.",
6419 gfc_current_intrinsic_arg[2]->name,
6420 gfc_current_intrinsic_arg[1]->name, gfc_current_intrinsic,
6421 &field->where);
6425 return true;
6429 bool
6430 gfc_check_verify (gfc_expr *x, gfc_expr *y, gfc_expr *z, gfc_expr *kind)
6432 if (!type_check (x, 0, BT_CHARACTER))
6433 return false;
6435 if (!same_type_check (x, 0, y, 1))
6436 return false;
6438 if (z != NULL && !type_check (z, 2, BT_LOGICAL))
6439 return false;
6441 if (!kind_check (kind, 3, BT_INTEGER))
6442 return false;
6443 if (kind && !gfc_notify_std (GFC_STD_F2003, "%qs intrinsic "
6444 "with KIND argument at %L",
6445 gfc_current_intrinsic, &kind->where))
6446 return false;
6448 return true;
6452 bool
6453 gfc_check_trim (gfc_expr *x)
6455 if (!type_check (x, 0, BT_CHARACTER))
6456 return false;
6458 if (gfc_invalid_null_arg (x))
6459 return false;
6461 if (!scalar_check (x, 0))
6462 return false;
6464 return true;
6468 bool
6469 gfc_check_ttynam (gfc_expr *unit)
6471 if (!scalar_check (unit, 0))
6472 return false;
6474 if (!type_check (unit, 0, BT_INTEGER))
6475 return false;
6477 return true;
6481 /************* Check functions for intrinsic subroutines *************/
6483 bool
6484 gfc_check_cpu_time (gfc_expr *time)
6486 if (!scalar_check (time, 0))
6487 return false;
6489 if (!type_check (time, 0, BT_REAL))
6490 return false;
6492 if (!variable_check (time, 0, false))
6493 return false;
6495 return true;
6499 bool
6500 gfc_check_date_and_time (gfc_expr *date, gfc_expr *time,
6501 gfc_expr *zone, gfc_expr *values)
6503 if (date != NULL)
6505 if (!type_check (date, 0, BT_CHARACTER))
6506 return false;
6507 if (!kind_value_check (date, 0, gfc_default_character_kind))
6508 return false;
6509 if (!scalar_check (date, 0))
6510 return false;
6511 if (!variable_check (date, 0, false))
6512 return false;
6515 if (time != NULL)
6517 if (!type_check (time, 1, BT_CHARACTER))
6518 return false;
6519 if (!kind_value_check (time, 1, gfc_default_character_kind))
6520 return false;
6521 if (!scalar_check (time, 1))
6522 return false;
6523 if (!variable_check (time, 1, false))
6524 return false;
6527 if (zone != NULL)
6529 if (!type_check (zone, 2, BT_CHARACTER))
6530 return false;
6531 if (!kind_value_check (zone, 2, gfc_default_character_kind))
6532 return false;
6533 if (!scalar_check (zone, 2))
6534 return false;
6535 if (!variable_check (zone, 2, false))
6536 return false;
6539 if (values != NULL)
6541 if (!type_check (values, 3, BT_INTEGER))
6542 return false;
6543 if (!array_check (values, 3))
6544 return false;
6545 if (!rank_check (values, 3, 1))
6546 return false;
6547 if (!variable_check (values, 3, false))
6548 return false;
6551 return true;
6555 bool
6556 gfc_check_mvbits (gfc_expr *from, gfc_expr *frompos, gfc_expr *len,
6557 gfc_expr *to, gfc_expr *topos)
6559 if (!type_check (from, 0, BT_INTEGER))
6560 return false;
6562 if (!type_check (frompos, 1, BT_INTEGER))
6563 return false;
6565 if (!type_check (len, 2, BT_INTEGER))
6566 return false;
6568 if (!same_type_check (from, 0, to, 3))
6569 return false;
6571 if (!variable_check (to, 3, false))
6572 return false;
6574 if (!type_check (topos, 4, BT_INTEGER))
6575 return false;
6577 if (!nonnegative_check ("frompos", frompos))
6578 return false;
6580 if (!nonnegative_check ("topos", topos))
6581 return false;
6583 if (!nonnegative_check ("len", len))
6584 return false;
6586 if (!less_than_bitsize2 ("from", from, "frompos", frompos, "len", len))
6587 return false;
6589 if (!less_than_bitsize2 ("to", to, "topos", topos, "len", len))
6590 return false;
6592 return true;
6596 /* Check the arguments for RANDOM_INIT. */
6598 bool
6599 gfc_check_random_init (gfc_expr *repeatable, gfc_expr *image_distinct)
6601 if (!type_check (repeatable, 0, BT_LOGICAL))
6602 return false;
6604 if (!scalar_check (repeatable, 0))
6605 return false;
6607 if (!type_check (image_distinct, 1, BT_LOGICAL))
6608 return false;
6610 if (!scalar_check (image_distinct, 1))
6611 return false;
6613 return true;
6617 bool
6618 gfc_check_random_number (gfc_expr *harvest)
6620 if (!type_check (harvest, 0, BT_REAL))
6621 return false;
6623 if (!variable_check (harvest, 0, false))
6624 return false;
6626 return true;
6630 bool
6631 gfc_check_random_seed (gfc_expr *size, gfc_expr *put, gfc_expr *get)
6633 unsigned int nargs = 0, seed_size;
6634 locus *where = NULL;
6635 mpz_t put_size, get_size;
6637 /* Keep the number of bytes in sync with master_state in
6638 libgfortran/intrinsics/random.c. */
6639 seed_size = 32 / gfc_default_integer_kind;
6641 if (size != NULL)
6643 if (size->expr_type != EXPR_VARIABLE
6644 || !size->symtree->n.sym->attr.optional)
6645 nargs++;
6647 if (!scalar_check (size, 0))
6648 return false;
6650 if (!type_check (size, 0, BT_INTEGER))
6651 return false;
6653 if (!variable_check (size, 0, false))
6654 return false;
6656 if (!kind_value_check (size, 0, gfc_default_integer_kind))
6657 return false;
6660 if (put != NULL)
6662 if (put->expr_type != EXPR_VARIABLE
6663 || !put->symtree->n.sym->attr.optional)
6665 nargs++;
6666 where = &put->where;
6669 if (!array_check (put, 1))
6670 return false;
6672 if (!rank_check (put, 1, 1))
6673 return false;
6675 if (!type_check (put, 1, BT_INTEGER))
6676 return false;
6678 if (!kind_value_check (put, 1, gfc_default_integer_kind))
6679 return false;
6681 if (gfc_array_size (put, &put_size)
6682 && mpz_get_ui (put_size) < seed_size)
6683 gfc_error ("Size of %qs argument of %qs intrinsic at %L "
6684 "too small (%i/%i)",
6685 gfc_current_intrinsic_arg[1]->name, gfc_current_intrinsic,
6686 &put->where, (int) mpz_get_ui (put_size), seed_size);
6689 if (get != NULL)
6691 if (get->expr_type != EXPR_VARIABLE
6692 || !get->symtree->n.sym->attr.optional)
6694 nargs++;
6695 where = &get->where;
6698 if (!array_check (get, 2))
6699 return false;
6701 if (!rank_check (get, 2, 1))
6702 return false;
6704 if (!type_check (get, 2, BT_INTEGER))
6705 return false;
6707 if (!variable_check (get, 2, false))
6708 return false;
6710 if (!kind_value_check (get, 2, gfc_default_integer_kind))
6711 return false;
6713 if (gfc_array_size (get, &get_size)
6714 && mpz_get_ui (get_size) < seed_size)
6715 gfc_error ("Size of %qs argument of %qs intrinsic at %L "
6716 "too small (%i/%i)",
6717 gfc_current_intrinsic_arg[2]->name, gfc_current_intrinsic,
6718 &get->where, (int) mpz_get_ui (get_size), seed_size);
6721 /* RANDOM_SEED may not have more than one non-optional argument. */
6722 if (nargs > 1)
6723 gfc_error ("Too many arguments to %s at %L", gfc_current_intrinsic, where);
6725 return true;
6728 bool
6729 gfc_check_fe_runtime_error (gfc_actual_arglist *a)
6731 gfc_expr *e;
6732 size_t len, i;
6733 int num_percent, nargs;
6735 e = a->expr;
6736 if (e->expr_type != EXPR_CONSTANT)
6737 return true;
6739 len = e->value.character.length;
6740 if (e->value.character.string[len-1] != '\0')
6741 gfc_internal_error ("fe_runtime_error string must be null terminated");
6743 num_percent = 0;
6744 for (i=0; i<len-1; i++)
6745 if (e->value.character.string[i] == '%')
6746 num_percent ++;
6748 nargs = 0;
6749 for (; a; a = a->next)
6750 nargs ++;
6752 if (nargs -1 != num_percent)
6753 gfc_internal_error ("fe_runtime_error: Wrong number of arguments (%d instead of %d)",
6754 nargs, num_percent++);
6756 return true;
6759 bool
6760 gfc_check_second_sub (gfc_expr *time)
6762 if (!scalar_check (time, 0))
6763 return false;
6765 if (!type_check (time, 0, BT_REAL))
6766 return false;
6768 if (!kind_value_check (time, 0, 4))
6769 return false;
6771 return true;
6775 /* COUNT and COUNT_MAX of SYSTEM_CLOCK are scalar, default-kind integer
6776 variables in Fortran 95. In Fortran 2003 and later, they can be of any
6777 kind, and COUNT_RATE can be of type real. Note, count, count_rate, and
6778 count_max are all optional arguments */
6780 bool
6781 gfc_check_system_clock (gfc_expr *count, gfc_expr *count_rate,
6782 gfc_expr *count_max)
6784 if (count != NULL)
6786 if (!scalar_check (count, 0))
6787 return false;
6789 if (!type_check (count, 0, BT_INTEGER))
6790 return false;
6792 if (count->ts.kind != gfc_default_integer_kind
6793 && !gfc_notify_std (GFC_STD_F2003, "COUNT argument to "
6794 "SYSTEM_CLOCK at %L has non-default kind",
6795 &count->where))
6796 return false;
6798 if (!variable_check (count, 0, false))
6799 return false;
6802 if (count_rate != NULL)
6804 if (!scalar_check (count_rate, 1))
6805 return false;
6807 if (!variable_check (count_rate, 1, false))
6808 return false;
6810 if (count_rate->ts.type == BT_REAL)
6812 if (!gfc_notify_std (GFC_STD_F2003, "Real COUNT_RATE argument to "
6813 "SYSTEM_CLOCK at %L", &count_rate->where))
6814 return false;
6816 else
6818 if (!type_check (count_rate, 1, BT_INTEGER))
6819 return false;
6821 if (count_rate->ts.kind != gfc_default_integer_kind
6822 && !gfc_notify_std (GFC_STD_F2003, "COUNT_RATE argument to "
6823 "SYSTEM_CLOCK at %L has non-default kind",
6824 &count_rate->where))
6825 return false;
6830 if (count_max != NULL)
6832 if (!scalar_check (count_max, 2))
6833 return false;
6835 if (!type_check (count_max, 2, BT_INTEGER))
6836 return false;
6838 if (count_max->ts.kind != gfc_default_integer_kind
6839 && !gfc_notify_std (GFC_STD_F2003, "COUNT_MAX argument to "
6840 "SYSTEM_CLOCK at %L has non-default kind",
6841 &count_max->where))
6842 return false;
6844 if (!variable_check (count_max, 2, false))
6845 return false;
6848 return true;
6852 bool
6853 gfc_check_irand (gfc_expr *x)
6855 if (x == NULL)
6856 return true;
6858 if (!scalar_check (x, 0))
6859 return false;
6861 if (!type_check (x, 0, BT_INTEGER))
6862 return false;
6864 if (!kind_value_check (x, 0, 4))
6865 return false;
6867 return true;
6871 bool
6872 gfc_check_alarm_sub (gfc_expr *seconds, gfc_expr *handler, gfc_expr *status)
6874 if (!scalar_check (seconds, 0))
6875 return false;
6876 if (!type_check (seconds, 0, BT_INTEGER))
6877 return false;
6879 if (!int_or_proc_check (handler, 1))
6880 return false;
6881 if (handler->ts.type == BT_INTEGER && !scalar_check (handler, 1))
6882 return false;
6884 if (status == NULL)
6885 return true;
6887 if (!scalar_check (status, 2))
6888 return false;
6889 if (!type_check (status, 2, BT_INTEGER))
6890 return false;
6891 if (!kind_value_check (status, 2, gfc_default_integer_kind))
6892 return false;
6894 return true;
6898 bool
6899 gfc_check_rand (gfc_expr *x)
6901 if (x == NULL)
6902 return true;
6904 if (!scalar_check (x, 0))
6905 return false;
6907 if (!type_check (x, 0, BT_INTEGER))
6908 return false;
6910 if (!kind_value_check (x, 0, 4))
6911 return false;
6913 return true;
6917 bool
6918 gfc_check_srand (gfc_expr *x)
6920 if (!scalar_check (x, 0))
6921 return false;
6923 if (!type_check (x, 0, BT_INTEGER))
6924 return false;
6926 if (!kind_value_check (x, 0, 4))
6927 return false;
6929 return true;
6933 bool
6934 gfc_check_ctime_sub (gfc_expr *time, gfc_expr *result)
6936 if (!scalar_check (time, 0))
6937 return false;
6938 if (!type_check (time, 0, BT_INTEGER))
6939 return false;
6941 if (!type_check (result, 1, BT_CHARACTER))
6942 return false;
6943 if (!kind_value_check (result, 1, gfc_default_character_kind))
6944 return false;
6946 return true;
6950 bool
6951 gfc_check_dtime_etime (gfc_expr *x)
6953 if (!array_check (x, 0))
6954 return false;
6956 if (!rank_check (x, 0, 1))
6957 return false;
6959 if (!variable_check (x, 0, false))
6960 return false;
6962 if (!type_check (x, 0, BT_REAL))
6963 return false;
6965 if (!kind_value_check (x, 0, 4))
6966 return false;
6968 return true;
6972 bool
6973 gfc_check_dtime_etime_sub (gfc_expr *values, gfc_expr *time)
6975 if (!array_check (values, 0))
6976 return false;
6978 if (!rank_check (values, 0, 1))
6979 return false;
6981 if (!variable_check (values, 0, false))
6982 return false;
6984 if (!type_check (values, 0, BT_REAL))
6985 return false;
6987 if (!kind_value_check (values, 0, 4))
6988 return false;
6990 if (!scalar_check (time, 1))
6991 return false;
6993 if (!type_check (time, 1, BT_REAL))
6994 return false;
6996 if (!kind_value_check (time, 1, 4))
6997 return false;
6999 return true;
7003 bool
7004 gfc_check_fdate_sub (gfc_expr *date)
7006 if (!type_check (date, 0, BT_CHARACTER))
7007 return false;
7008 if (!kind_value_check (date, 0, gfc_default_character_kind))
7009 return false;
7011 return true;
7015 bool
7016 gfc_check_gerror (gfc_expr *msg)
7018 if (!type_check (msg, 0, BT_CHARACTER))
7019 return false;
7020 if (!kind_value_check (msg, 0, gfc_default_character_kind))
7021 return false;
7023 return true;
7027 bool
7028 gfc_check_getcwd_sub (gfc_expr *cwd, gfc_expr *status)
7030 if (!type_check (cwd, 0, BT_CHARACTER))
7031 return false;
7032 if (!kind_value_check (cwd, 0, gfc_default_character_kind))
7033 return false;
7035 if (status == NULL)
7036 return true;
7038 if (!scalar_check (status, 1))
7039 return false;
7041 if (!type_check (status, 1, BT_INTEGER))
7042 return false;
7044 return true;
7048 bool
7049 gfc_check_getarg (gfc_expr *pos, gfc_expr *value)
7051 if (!type_check (pos, 0, BT_INTEGER))
7052 return false;
7054 if (pos->ts.kind > gfc_default_integer_kind)
7056 gfc_error ("%qs argument of %qs intrinsic at %L must be of a kind "
7057 "not wider than the default kind (%d)",
7058 gfc_current_intrinsic_arg[0]->name, gfc_current_intrinsic,
7059 &pos->where, gfc_default_integer_kind);
7060 return false;
7063 if (!type_check (value, 1, BT_CHARACTER))
7064 return false;
7065 if (!kind_value_check (value, 1, gfc_default_character_kind))
7066 return false;
7068 return true;
7072 bool
7073 gfc_check_getlog (gfc_expr *msg)
7075 if (!type_check (msg, 0, BT_CHARACTER))
7076 return false;
7077 if (!kind_value_check (msg, 0, gfc_default_character_kind))
7078 return false;
7080 return true;
7084 bool
7085 gfc_check_exit (gfc_expr *status)
7087 if (status == NULL)
7088 return true;
7090 if (!type_check (status, 0, BT_INTEGER))
7091 return false;
7093 if (!scalar_check (status, 0))
7094 return false;
7096 return true;
7100 bool
7101 gfc_check_flush (gfc_expr *unit)
7103 if (unit == NULL)
7104 return true;
7106 if (!type_check (unit, 0, BT_INTEGER))
7107 return false;
7109 if (!scalar_check (unit, 0))
7110 return false;
7112 return true;
7116 bool
7117 gfc_check_free (gfc_expr *i)
7119 if (!type_check (i, 0, BT_INTEGER))
7120 return false;
7122 if (!scalar_check (i, 0))
7123 return false;
7125 return true;
7129 bool
7130 gfc_check_hostnm (gfc_expr *name)
7132 if (!type_check (name, 0, BT_CHARACTER))
7133 return false;
7134 if (!kind_value_check (name, 0, gfc_default_character_kind))
7135 return false;
7137 return true;
7141 bool
7142 gfc_check_hostnm_sub (gfc_expr *name, gfc_expr *status)
7144 if (!type_check (name, 0, BT_CHARACTER))
7145 return false;
7146 if (!kind_value_check (name, 0, gfc_default_character_kind))
7147 return false;
7149 if (status == NULL)
7150 return true;
7152 if (!scalar_check (status, 1))
7153 return false;
7155 if (!type_check (status, 1, BT_INTEGER))
7156 return false;
7158 return true;
7162 bool
7163 gfc_check_itime_idate (gfc_expr *values)
7165 if (!array_check (values, 0))
7166 return false;
7168 if (!rank_check (values, 0, 1))
7169 return false;
7171 if (!variable_check (values, 0, false))
7172 return false;
7174 if (!type_check (values, 0, BT_INTEGER))
7175 return false;
7177 if (!kind_value_check (values, 0, gfc_default_integer_kind))
7178 return false;
7180 return true;
7184 bool
7185 gfc_check_ltime_gmtime (gfc_expr *time, gfc_expr *values)
7187 if (!type_check (time, 0, BT_INTEGER))
7188 return false;
7190 if (!kind_value_check (time, 0, gfc_default_integer_kind))
7191 return false;
7193 if (!scalar_check (time, 0))
7194 return false;
7196 if (!array_check (values, 1))
7197 return false;
7199 if (!rank_check (values, 1, 1))
7200 return false;
7202 if (!variable_check (values, 1, false))
7203 return false;
7205 if (!type_check (values, 1, BT_INTEGER))
7206 return false;
7208 if (!kind_value_check (values, 1, gfc_default_integer_kind))
7209 return false;
7211 return true;
7215 bool
7216 gfc_check_ttynam_sub (gfc_expr *unit, gfc_expr *name)
7218 if (!scalar_check (unit, 0))
7219 return false;
7221 if (!type_check (unit, 0, BT_INTEGER))
7222 return false;
7224 if (!type_check (name, 1, BT_CHARACTER))
7225 return false;
7226 if (!kind_value_check (name, 1, gfc_default_character_kind))
7227 return false;
7229 return true;
7233 bool
7234 gfc_check_is_contiguous (gfc_expr *array)
7236 if (array->expr_type == EXPR_NULL)
7238 gfc_error ("Actual argument at %L of %qs intrinsic shall be an "
7239 "associated pointer", &array->where, gfc_current_intrinsic);
7240 return false;
7243 if (!array_check (array, 0))
7244 return false;
7246 return true;
7250 bool
7251 gfc_check_isatty (gfc_expr *unit)
7253 if (unit == NULL)
7254 return false;
7256 if (!type_check (unit, 0, BT_INTEGER))
7257 return false;
7259 if (!scalar_check (unit, 0))
7260 return false;
7262 return true;
7266 bool
7267 gfc_check_isnan (gfc_expr *x)
7269 if (!type_check (x, 0, BT_REAL))
7270 return false;
7272 return true;
7276 bool
7277 gfc_check_perror (gfc_expr *string)
7279 if (!type_check (string, 0, BT_CHARACTER))
7280 return false;
7281 if (!kind_value_check (string, 0, gfc_default_character_kind))
7282 return false;
7284 return true;
7288 bool
7289 gfc_check_umask (gfc_expr *mask)
7291 if (!type_check (mask, 0, BT_INTEGER))
7292 return false;
7294 if (!scalar_check (mask, 0))
7295 return false;
7297 return true;
7301 bool
7302 gfc_check_umask_sub (gfc_expr *mask, gfc_expr *old)
7304 if (!type_check (mask, 0, BT_INTEGER))
7305 return false;
7307 if (!scalar_check (mask, 0))
7308 return false;
7310 if (old == NULL)
7311 return true;
7313 if (!scalar_check (old, 1))
7314 return false;
7316 if (!type_check (old, 1, BT_INTEGER))
7317 return false;
7319 return true;
7323 bool
7324 gfc_check_unlink (gfc_expr *name)
7326 if (!type_check (name, 0, BT_CHARACTER))
7327 return false;
7328 if (!kind_value_check (name, 0, gfc_default_character_kind))
7329 return false;
7331 return true;
7335 bool
7336 gfc_check_unlink_sub (gfc_expr *name, gfc_expr *status)
7338 if (!type_check (name, 0, BT_CHARACTER))
7339 return false;
7340 if (!kind_value_check (name, 0, gfc_default_character_kind))
7341 return false;
7343 if (status == NULL)
7344 return true;
7346 if (!scalar_check (status, 1))
7347 return false;
7349 if (!type_check (status, 1, BT_INTEGER))
7350 return false;
7352 return true;
7356 bool
7357 gfc_check_signal (gfc_expr *number, gfc_expr *handler)
7359 if (!scalar_check (number, 0))
7360 return false;
7361 if (!type_check (number, 0, BT_INTEGER))
7362 return false;
7364 if (!int_or_proc_check (handler, 1))
7365 return false;
7366 if (handler->ts.type == BT_INTEGER && !scalar_check (handler, 1))
7367 return false;
7369 return true;
7373 bool
7374 gfc_check_signal_sub (gfc_expr *number, gfc_expr *handler, gfc_expr *status)
7376 if (!scalar_check (number, 0))
7377 return false;
7378 if (!type_check (number, 0, BT_INTEGER))
7379 return false;
7381 if (!int_or_proc_check (handler, 1))
7382 return false;
7383 if (handler->ts.type == BT_INTEGER && !scalar_check (handler, 1))
7384 return false;
7386 if (status == NULL)
7387 return true;
7389 if (!type_check (status, 2, BT_INTEGER))
7390 return false;
7391 if (!scalar_check (status, 2))
7392 return false;
7394 return true;
7398 bool
7399 gfc_check_system_sub (gfc_expr *cmd, gfc_expr *status)
7401 if (!type_check (cmd, 0, BT_CHARACTER))
7402 return false;
7403 if (!kind_value_check (cmd, 0, gfc_default_character_kind))
7404 return false;
7406 if (!scalar_check (status, 1))
7407 return false;
7409 if (!type_check (status, 1, BT_INTEGER))
7410 return false;
7412 if (!kind_value_check (status, 1, gfc_default_integer_kind))
7413 return false;
7415 return true;
7419 /* This is used for the GNU intrinsics AND, OR and XOR. */
7420 bool
7421 gfc_check_and (gfc_expr *i, gfc_expr *j)
7423 if (i->ts.type != BT_INTEGER
7424 && i->ts.type != BT_LOGICAL
7425 && i->ts.type != BT_BOZ)
7427 gfc_error ("%qs argument of %qs intrinsic at %L must be INTEGER, "
7428 "LOGICAL, or a BOZ literal constant",
7429 gfc_current_intrinsic_arg[0]->name,
7430 gfc_current_intrinsic, &i->where);
7431 return false;
7434 if (j->ts.type != BT_INTEGER
7435 && j->ts.type != BT_LOGICAL
7436 && j->ts.type != BT_BOZ)
7438 gfc_error ("%qs argument of %qs intrinsic at %L must be INTEGER, "
7439 "LOGICAL, or a BOZ literal constant",
7440 gfc_current_intrinsic_arg[1]->name,
7441 gfc_current_intrinsic, &j->where);
7442 return false;
7445 /* i and j cannot both be BOZ literal constants. */
7446 if (!boz_args_check (i, j))
7447 return false;
7449 /* If i is BOZ and j is integer, convert i to type of j. */
7450 if (i->ts.type == BT_BOZ)
7452 if (j->ts.type != BT_INTEGER)
7454 gfc_error ("%qs argument of %qs intrinsic at %L must be INTEGER",
7455 gfc_current_intrinsic_arg[1]->name,
7456 gfc_current_intrinsic, &j->where);
7457 reset_boz (i);
7458 return false;
7460 if (!gfc_boz2int (i, j->ts.kind))
7461 return false;
7464 /* If j is BOZ and i is integer, convert j to type of i. */
7465 if (j->ts.type == BT_BOZ)
7467 if (i->ts.type != BT_INTEGER)
7469 gfc_error ("%qs argument of %qs intrinsic at %L must be INTEGER",
7470 gfc_current_intrinsic_arg[0]->name,
7471 gfc_current_intrinsic, &j->where);
7472 reset_boz (j);
7473 return false;
7475 if (!gfc_boz2int (j, i->ts.kind))
7476 return false;
7479 if (!same_type_check (i, 0, j, 1, false))
7480 return false;
7482 if (!scalar_check (i, 0))
7483 return false;
7485 if (!scalar_check (j, 1))
7486 return false;
7488 return true;
7492 bool
7493 gfc_check_storage_size (gfc_expr *a, gfc_expr *kind)
7496 if (a->expr_type == EXPR_NULL)
7498 gfc_error ("Intrinsic function NULL at %L cannot be an actual "
7499 "argument to STORAGE_SIZE, because it returns a "
7500 "disassociated pointer", &a->where);
7501 return false;
7504 if (a->ts.type == BT_ASSUMED)
7506 gfc_error ("%qs argument of %qs intrinsic at %L shall not be TYPE(*)",
7507 gfc_current_intrinsic_arg[0]->name, gfc_current_intrinsic,
7508 &a->where);
7509 return false;
7512 if (a->ts.type == BT_PROCEDURE)
7514 gfc_error ("%qs argument of %qs intrinsic at %L shall not be a "
7515 "procedure", gfc_current_intrinsic_arg[0]->name,
7516 gfc_current_intrinsic, &a->where);
7517 return false;
7520 if (a->ts.type == BT_BOZ && illegal_boz_arg (a))
7521 return false;
7523 if (kind == NULL)
7524 return true;
7526 if (!type_check (kind, 1, BT_INTEGER))
7527 return false;
7529 if (!scalar_check (kind, 1))
7530 return false;
7532 if (kind->expr_type != EXPR_CONSTANT)
7534 gfc_error ("%qs argument of %qs intrinsic at %L must be a constant",
7535 gfc_current_intrinsic_arg[1]->name, gfc_current_intrinsic,
7536 &kind->where);
7537 return false;
7540 return true;