rs6000.c (output_toc): Correct little-endian float constant output.
[official-gcc.git] / gcc / fortran / check.c
blobe531deb147a75e48718a11b81a8669cb020fd11f
1 /* Check functions
2 Copyright (C) 2002-2013 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 "flags.h"
32 #include "gfortran.h"
33 #include "intrinsic.h"
34 #include "constructor.h"
35 #include "target-memory.h"
38 /* Make sure an expression is a scalar. */
40 static bool
41 scalar_check (gfc_expr *e, int n)
43 if (e->rank == 0)
44 return true;
46 gfc_error ("'%s' argument of '%s' intrinsic at %L must be a scalar",
47 gfc_current_intrinsic_arg[n]->name, gfc_current_intrinsic,
48 &e->where);
50 return false;
54 /* Check the type of an expression. */
56 static bool
57 type_check (gfc_expr *e, int n, bt type)
59 if (e->ts.type == type)
60 return true;
62 gfc_error ("'%s' argument of '%s' intrinsic at %L must be %s",
63 gfc_current_intrinsic_arg[n]->name, gfc_current_intrinsic,
64 &e->where, gfc_basic_typename (type));
66 return false;
70 /* Check that the expression is a numeric type. */
72 static bool
73 numeric_check (gfc_expr *e, int n)
75 if (gfc_numeric_ts (&e->ts))
76 return true;
78 /* If the expression has not got a type, check if its namespace can
79 offer a default type. */
80 if ((e->expr_type == EXPR_VARIABLE || e->expr_type == EXPR_FUNCTION)
81 && e->symtree->n.sym->ts.type == BT_UNKNOWN
82 && gfc_set_default_type (e->symtree->n.sym, 0, e->symtree->n.sym->ns)
83 && gfc_numeric_ts (&e->symtree->n.sym->ts))
85 e->ts = e->symtree->n.sym->ts;
86 return true;
89 gfc_error ("'%s' argument of '%s' intrinsic at %L must be a numeric type",
90 gfc_current_intrinsic_arg[n]->name, gfc_current_intrinsic,
91 &e->where);
93 return false;
97 /* Check that an expression is integer or real. */
99 static bool
100 int_or_real_check (gfc_expr *e, int n)
102 if (e->ts.type != BT_INTEGER && e->ts.type != BT_REAL)
104 gfc_error ("'%s' argument of '%s' intrinsic at %L must be INTEGER "
105 "or REAL", gfc_current_intrinsic_arg[n]->name,
106 gfc_current_intrinsic, &e->where);
107 return false;
110 return true;
114 /* Check that an expression is real or complex. */
116 static bool
117 real_or_complex_check (gfc_expr *e, int n)
119 if (e->ts.type != BT_REAL && e->ts.type != BT_COMPLEX)
121 gfc_error ("'%s' argument of '%s' intrinsic at %L must be REAL "
122 "or COMPLEX", gfc_current_intrinsic_arg[n]->name,
123 gfc_current_intrinsic, &e->where);
124 return false;
127 return true;
131 /* Check that an expression is INTEGER or PROCEDURE. */
133 static bool
134 int_or_proc_check (gfc_expr *e, int n)
136 if (e->ts.type != BT_INTEGER && e->ts.type != BT_PROCEDURE)
138 gfc_error ("'%s' argument of '%s' intrinsic at %L must be INTEGER "
139 "or PROCEDURE", gfc_current_intrinsic_arg[n]->name,
140 gfc_current_intrinsic, &e->where);
141 return false;
144 return true;
148 /* Check that the expression is an optional constant integer
149 and that it specifies a valid kind for that type. */
151 static bool
152 kind_check (gfc_expr *k, int n, bt type)
154 int kind;
156 if (k == NULL)
157 return true;
159 if (!type_check (k, n, BT_INTEGER))
160 return false;
162 if (!scalar_check (k, n))
163 return false;
165 if (!gfc_check_init_expr (k))
167 gfc_error ("'%s' argument of '%s' intrinsic at %L must be a constant",
168 gfc_current_intrinsic_arg[n]->name, gfc_current_intrinsic,
169 &k->where);
170 return false;
173 if (gfc_extract_int (k, &kind) != NULL
174 || gfc_validate_kind (type, kind, true) < 0)
176 gfc_error ("Invalid kind for %s at %L", gfc_basic_typename (type),
177 &k->where);
178 return false;
181 return true;
185 /* Make sure the expression is a double precision real. */
187 static bool
188 double_check (gfc_expr *d, int n)
190 if (!type_check (d, n, BT_REAL))
191 return false;
193 if (d->ts.kind != gfc_default_double_kind)
195 gfc_error ("'%s' argument of '%s' intrinsic at %L must be double "
196 "precision", gfc_current_intrinsic_arg[n]->name,
197 gfc_current_intrinsic, &d->where);
198 return false;
201 return true;
205 static bool
206 coarray_check (gfc_expr *e, int n)
208 if (e->ts.type == BT_CLASS && gfc_expr_attr (e).class_ok
209 && CLASS_DATA (e)->attr.codimension
210 && CLASS_DATA (e)->as->corank)
212 gfc_add_class_array_ref (e);
213 return true;
216 if (!gfc_is_coarray (e))
218 gfc_error ("Expected coarray variable as '%s' argument to the %s "
219 "intrinsic at %L", gfc_current_intrinsic_arg[n]->name,
220 gfc_current_intrinsic, &e->where);
221 return false;
224 return true;
228 /* Make sure the expression is a logical array. */
230 static bool
231 logical_array_check (gfc_expr *array, int n)
233 if (array->ts.type != BT_LOGICAL || array->rank == 0)
235 gfc_error ("'%s' argument of '%s' intrinsic at %L must be a logical "
236 "array", gfc_current_intrinsic_arg[n]->name,
237 gfc_current_intrinsic, &array->where);
238 return false;
241 return true;
245 /* Make sure an expression is an array. */
247 static bool
248 array_check (gfc_expr *e, int n)
250 if (e->ts.type == BT_CLASS && gfc_expr_attr (e).class_ok
251 && CLASS_DATA (e)->attr.dimension
252 && CLASS_DATA (e)->as->rank)
254 gfc_add_class_array_ref (e);
255 return true;
258 if (e->rank != 0 && e->ts.type != BT_PROCEDURE)
259 return true;
261 gfc_error ("'%s' argument of '%s' intrinsic at %L must be an array",
262 gfc_current_intrinsic_arg[n]->name, gfc_current_intrinsic,
263 &e->where);
265 return false;
269 /* If expr is a constant, then check to ensure that it is greater than
270 of equal to zero. */
272 static bool
273 nonnegative_check (const char *arg, gfc_expr *expr)
275 int i;
277 if (expr->expr_type == EXPR_CONSTANT)
279 gfc_extract_int (expr, &i);
280 if (i < 0)
282 gfc_error ("'%s' at %L must be nonnegative", arg, &expr->where);
283 return false;
287 return true;
291 /* If expr2 is constant, then check that the value is less than
292 (less than or equal to, if 'or_equal' is true) bit_size(expr1). */
294 static bool
295 less_than_bitsize1 (const char *arg1, gfc_expr *expr1, const char *arg2,
296 gfc_expr *expr2, bool or_equal)
298 int i2, i3;
300 if (expr2->expr_type == EXPR_CONSTANT)
302 gfc_extract_int (expr2, &i2);
303 i3 = gfc_validate_kind (BT_INTEGER, expr1->ts.kind, false);
305 /* For ISHFT[C], check that |shift| <= bit_size(i). */
306 if (arg2 == NULL)
308 if (i2 < 0)
309 i2 = -i2;
311 if (i2 > gfc_integer_kinds[i3].bit_size)
313 gfc_error ("The absolute value of SHIFT at %L must be less "
314 "than or equal to BIT_SIZE('%s')",
315 &expr2->where, arg1);
316 return false;
320 if (or_equal)
322 if (i2 > gfc_integer_kinds[i3].bit_size)
324 gfc_error ("'%s' at %L must be less than "
325 "or equal to BIT_SIZE('%s')",
326 arg2, &expr2->where, arg1);
327 return false;
330 else
332 if (i2 >= gfc_integer_kinds[i3].bit_size)
334 gfc_error ("'%s' at %L must be less than BIT_SIZE('%s')",
335 arg2, &expr2->where, arg1);
336 return false;
341 return true;
345 /* If expr is constant, then check that the value is less than or equal
346 to the bit_size of the kind k. */
348 static bool
349 less_than_bitsizekind (const char *arg, gfc_expr *expr, int k)
351 int i, val;
353 if (expr->expr_type != EXPR_CONSTANT)
354 return true;
356 i = gfc_validate_kind (BT_INTEGER, k, false);
357 gfc_extract_int (expr, &val);
359 if (val > gfc_integer_kinds[i].bit_size)
361 gfc_error ("'%s' at %L must be less than or equal to the BIT_SIZE of "
362 "INTEGER(KIND=%d)", arg, &expr->where, k);
363 return false;
366 return true;
370 /* If expr2 and expr3 are constants, then check that the value is less than
371 or equal to bit_size(expr1). */
373 static bool
374 less_than_bitsize2 (const char *arg1, gfc_expr *expr1, const char *arg2,
375 gfc_expr *expr2, const char *arg3, gfc_expr *expr3)
377 int i2, i3;
379 if (expr2->expr_type == EXPR_CONSTANT && expr3->expr_type == EXPR_CONSTANT)
381 gfc_extract_int (expr2, &i2);
382 gfc_extract_int (expr3, &i3);
383 i2 += i3;
384 i3 = gfc_validate_kind (BT_INTEGER, expr1->ts.kind, false);
385 if (i2 > gfc_integer_kinds[i3].bit_size)
387 gfc_error ("'%s + %s' at %L must be less than or equal "
388 "to BIT_SIZE('%s')",
389 arg2, arg3, &expr2->where, arg1);
390 return false;
394 return true;
397 /* Make sure two expressions have the same type. */
399 static bool
400 same_type_check (gfc_expr *e, int n, gfc_expr *f, int m)
402 if (gfc_compare_types (&e->ts, &f->ts))
403 return true;
405 gfc_error ("'%s' argument of '%s' intrinsic at %L must be the same type "
406 "and kind as '%s'", gfc_current_intrinsic_arg[m]->name,
407 gfc_current_intrinsic, &f->where,
408 gfc_current_intrinsic_arg[n]->name);
410 return false;
414 /* Make sure that an expression has a certain (nonzero) rank. */
416 static bool
417 rank_check (gfc_expr *e, int n, int rank)
419 if (e->rank == rank)
420 return true;
422 gfc_error ("'%s' argument of '%s' intrinsic at %L must be of rank %d",
423 gfc_current_intrinsic_arg[n]->name, gfc_current_intrinsic,
424 &e->where, rank);
426 return false;
430 /* Make sure a variable expression is not an optional dummy argument. */
432 static bool
433 nonoptional_check (gfc_expr *e, int n)
435 if (e->expr_type == EXPR_VARIABLE && e->symtree->n.sym->attr.optional)
437 gfc_error ("'%s' argument of '%s' intrinsic at %L must not be OPTIONAL",
438 gfc_current_intrinsic_arg[n]->name, gfc_current_intrinsic,
439 &e->where);
442 /* TODO: Recursive check on nonoptional variables? */
444 return true;
448 /* Check for ALLOCATABLE attribute. */
450 static bool
451 allocatable_check (gfc_expr *e, int n)
453 symbol_attribute attr;
455 attr = gfc_variable_attr (e, NULL);
456 if (!attr.allocatable || attr.associate_var)
458 gfc_error ("'%s' argument of '%s' intrinsic at %L must be ALLOCATABLE",
459 gfc_current_intrinsic_arg[n]->name, gfc_current_intrinsic,
460 &e->where);
461 return false;
464 return true;
468 /* Check that an expression has a particular kind. */
470 static bool
471 kind_value_check (gfc_expr *e, int n, int k)
473 if (e->ts.kind == k)
474 return true;
476 gfc_error ("'%s' argument of '%s' intrinsic at %L must be of kind %d",
477 gfc_current_intrinsic_arg[n]->name, gfc_current_intrinsic,
478 &e->where, k);
480 return false;
484 /* Make sure an expression is a variable. */
486 static bool
487 variable_check (gfc_expr *e, int n, bool allow_proc)
489 if (e->expr_type == EXPR_VARIABLE
490 && e->symtree->n.sym->attr.intent == INTENT_IN
491 && (gfc_current_intrinsic_arg[n]->intent == INTENT_OUT
492 || gfc_current_intrinsic_arg[n]->intent == INTENT_INOUT))
494 gfc_ref *ref;
495 bool pointer = e->symtree->n.sym->ts.type == BT_CLASS
496 && CLASS_DATA (e->symtree->n.sym)
497 ? CLASS_DATA (e->symtree->n.sym)->attr.class_pointer
498 : e->symtree->n.sym->attr.pointer;
500 for (ref = e->ref; ref; ref = ref->next)
502 if (pointer && ref->type == REF_COMPONENT)
503 break;
504 if (ref->type == REF_COMPONENT
505 && ((ref->u.c.component->ts.type == BT_CLASS
506 && CLASS_DATA (ref->u.c.component)->attr.class_pointer)
507 || (ref->u.c.component->ts.type != BT_CLASS
508 && ref->u.c.component->attr.pointer)))
509 break;
512 if (!ref)
514 gfc_error ("'%s' argument of '%s' intrinsic at %L cannot be "
515 "INTENT(IN)", gfc_current_intrinsic_arg[n]->name,
516 gfc_current_intrinsic, &e->where);
517 return false;
521 if (e->expr_type == EXPR_VARIABLE
522 && e->symtree->n.sym->attr.flavor != FL_PARAMETER
523 && (allow_proc || !e->symtree->n.sym->attr.function))
524 return true;
526 if (e->expr_type == EXPR_VARIABLE && e->symtree->n.sym->attr.function
527 && e->symtree->n.sym == e->symtree->n.sym->result)
529 gfc_namespace *ns;
530 for (ns = gfc_current_ns; ns; ns = ns->parent)
531 if (ns->proc_name == e->symtree->n.sym)
532 return true;
535 gfc_error ("'%s' argument of '%s' intrinsic at %L must be a variable",
536 gfc_current_intrinsic_arg[n]->name, gfc_current_intrinsic, &e->where);
538 return false;
542 /* Check the common DIM parameter for correctness. */
544 static bool
545 dim_check (gfc_expr *dim, int n, bool optional)
547 if (dim == NULL)
548 return true;
550 if (!type_check (dim, n, BT_INTEGER))
551 return false;
553 if (!scalar_check (dim, n))
554 return false;
556 if (!optional && !nonoptional_check (dim, n))
557 return false;
559 return true;
563 /* If a coarray DIM parameter is a constant, make sure that it is greater than
564 zero and less than or equal to the corank of the given array. */
566 static bool
567 dim_corank_check (gfc_expr *dim, gfc_expr *array)
569 int corank;
571 gcc_assert (array->expr_type == EXPR_VARIABLE);
573 if (dim->expr_type != EXPR_CONSTANT)
574 return true;
576 if (array->ts.type == BT_CLASS)
577 return true;
579 corank = gfc_get_corank (array);
581 if (mpz_cmp_ui (dim->value.integer, 1) < 0
582 || mpz_cmp_ui (dim->value.integer, corank) > 0)
584 gfc_error ("'dim' argument of '%s' intrinsic at %L is not a valid "
585 "codimension index", gfc_current_intrinsic, &dim->where);
587 return false;
590 return true;
594 /* If a DIM parameter is a constant, make sure that it is greater than
595 zero and less than or equal to the rank of the given array. If
596 allow_assumed is zero then dim must be less than the rank of the array
597 for assumed size arrays. */
599 static bool
600 dim_rank_check (gfc_expr *dim, gfc_expr *array, int allow_assumed)
602 gfc_array_ref *ar;
603 int rank;
605 if (dim == NULL)
606 return true;
608 if (dim->expr_type != EXPR_CONSTANT)
609 return true;
611 if (array->ts.type == BT_CLASS)
612 return true;
614 if (array->expr_type == EXPR_FUNCTION && array->value.function.isym
615 && array->value.function.isym->id == GFC_ISYM_SPREAD)
616 rank = array->rank + 1;
617 else
618 rank = array->rank;
620 /* Assumed-rank array. */
621 if (rank == -1)
622 rank = GFC_MAX_DIMENSIONS;
624 if (array->expr_type == EXPR_VARIABLE)
626 ar = gfc_find_array_ref (array);
627 if (ar->as->type == AS_ASSUMED_SIZE
628 && !allow_assumed
629 && ar->type != AR_ELEMENT
630 && ar->type != AR_SECTION)
631 rank--;
634 if (mpz_cmp_ui (dim->value.integer, 1) < 0
635 || mpz_cmp_ui (dim->value.integer, rank) > 0)
637 gfc_error ("'dim' argument of '%s' intrinsic at %L is not a valid "
638 "dimension index", gfc_current_intrinsic, &dim->where);
640 return false;
643 return true;
647 /* Compare the size of a along dimension ai with the size of b along
648 dimension bi, returning 0 if they are known not to be identical,
649 and 1 if they are identical, or if this cannot be determined. */
651 static int
652 identical_dimen_shape (gfc_expr *a, int ai, gfc_expr *b, int bi)
654 mpz_t a_size, b_size;
655 int ret;
657 gcc_assert (a->rank > ai);
658 gcc_assert (b->rank > bi);
660 ret = 1;
662 if (gfc_array_dimen_size (a, ai, &a_size))
664 if (gfc_array_dimen_size (b, bi, &b_size))
666 if (mpz_cmp (a_size, b_size) != 0)
667 ret = 0;
669 mpz_clear (b_size);
671 mpz_clear (a_size);
673 return ret;
676 /* Calculate the length of a character variable, including substrings.
677 Strip away parentheses if necessary. Return -1 if no length could
678 be determined. */
680 static long
681 gfc_var_strlen (const gfc_expr *a)
683 gfc_ref *ra;
685 while (a->expr_type == EXPR_OP && a->value.op.op == INTRINSIC_PARENTHESES)
686 a = a->value.op.op1;
688 for (ra = a->ref; ra != NULL && ra->type != REF_SUBSTRING; ra = ra->next)
691 if (ra)
693 long start_a, end_a;
695 if (!ra->u.ss.end)
696 return -1;
698 if ((!ra->u.ss.start || ra->u.ss.start->expr_type == EXPR_CONSTANT)
699 && ra->u.ss.end->expr_type == EXPR_CONSTANT)
701 start_a = ra->u.ss.start ? mpz_get_si (ra->u.ss.start->value.integer)
702 : 1;
703 end_a = mpz_get_si (ra->u.ss.end->value.integer);
704 return (end_a < start_a) ? 0 : end_a - start_a + 1;
706 else if (ra->u.ss.start
707 && gfc_dep_compare_expr (ra->u.ss.start, ra->u.ss.end) == 0)
708 return 1;
709 else
710 return -1;
713 if (a->ts.u.cl && a->ts.u.cl->length
714 && a->ts.u.cl->length->expr_type == EXPR_CONSTANT)
715 return mpz_get_si (a->ts.u.cl->length->value.integer);
716 else if (a->expr_type == EXPR_CONSTANT
717 && (a->ts.u.cl == NULL || a->ts.u.cl->length == NULL))
718 return a->value.character.length;
719 else
720 return -1;
724 /* Check whether two character expressions have the same length;
725 returns true if they have or if the length cannot be determined,
726 otherwise return false and raise a gfc_error. */
728 bool
729 gfc_check_same_strlen (const gfc_expr *a, const gfc_expr *b, const char *name)
731 long len_a, len_b;
733 len_a = gfc_var_strlen(a);
734 len_b = gfc_var_strlen(b);
736 if (len_a == -1 || len_b == -1 || len_a == len_b)
737 return true;
738 else
740 gfc_error ("Unequal character lengths (%ld/%ld) in %s at %L",
741 len_a, len_b, name, &a->where);
742 return false;
747 /***** Check functions *****/
749 /* Check subroutine suitable for intrinsics taking a real argument and
750 a kind argument for the result. */
752 static bool
753 check_a_kind (gfc_expr *a, gfc_expr *kind, bt type)
755 if (!type_check (a, 0, BT_REAL))
756 return false;
757 if (!kind_check (kind, 1, type))
758 return false;
760 return true;
764 /* Check subroutine suitable for ceiling, floor and nint. */
766 bool
767 gfc_check_a_ikind (gfc_expr *a, gfc_expr *kind)
769 return check_a_kind (a, kind, BT_INTEGER);
773 /* Check subroutine suitable for aint, anint. */
775 bool
776 gfc_check_a_xkind (gfc_expr *a, gfc_expr *kind)
778 return check_a_kind (a, kind, BT_REAL);
782 bool
783 gfc_check_abs (gfc_expr *a)
785 if (!numeric_check (a, 0))
786 return false;
788 return true;
792 bool
793 gfc_check_achar (gfc_expr *a, gfc_expr *kind)
795 if (!type_check (a, 0, BT_INTEGER))
796 return false;
797 if (!kind_check (kind, 1, BT_CHARACTER))
798 return false;
800 return true;
804 bool
805 gfc_check_access_func (gfc_expr *name, gfc_expr *mode)
807 if (!type_check (name, 0, BT_CHARACTER)
808 || !scalar_check (name, 0))
809 return false;
810 if (!kind_value_check (name, 0, gfc_default_character_kind))
811 return false;
813 if (!type_check (mode, 1, BT_CHARACTER)
814 || !scalar_check (mode, 1))
815 return false;
816 if (!kind_value_check (mode, 1, gfc_default_character_kind))
817 return false;
819 return true;
823 bool
824 gfc_check_all_any (gfc_expr *mask, gfc_expr *dim)
826 if (!logical_array_check (mask, 0))
827 return false;
829 if (!dim_check (dim, 1, false))
830 return false;
832 if (!dim_rank_check (dim, mask, 0))
833 return false;
835 return true;
839 bool
840 gfc_check_allocated (gfc_expr *array)
842 if (!variable_check (array, 0, false))
843 return false;
844 if (!allocatable_check (array, 0))
845 return false;
847 return true;
851 /* Common check function where the first argument must be real or
852 integer and the second argument must be the same as the first. */
854 bool
855 gfc_check_a_p (gfc_expr *a, gfc_expr *p)
857 if (!int_or_real_check (a, 0))
858 return false;
860 if (a->ts.type != p->ts.type)
862 gfc_error ("'%s' and '%s' arguments of '%s' intrinsic at %L must "
863 "have the same type", gfc_current_intrinsic_arg[0]->name,
864 gfc_current_intrinsic_arg[1]->name, gfc_current_intrinsic,
865 &p->where);
866 return false;
869 if (a->ts.kind != p->ts.kind)
871 if (!gfc_notify_std (GFC_STD_GNU, "Different type kinds at %L",
872 &p->where))
873 return false;
876 return true;
880 bool
881 gfc_check_x_yd (gfc_expr *x, gfc_expr *y)
883 if (!double_check (x, 0) || !double_check (y, 1))
884 return false;
886 return true;
890 bool
891 gfc_check_associated (gfc_expr *pointer, gfc_expr *target)
893 symbol_attribute attr1, attr2;
894 int i;
895 bool t;
896 locus *where;
898 where = &pointer->where;
900 if (pointer->expr_type == EXPR_NULL)
901 goto null_arg;
903 attr1 = gfc_expr_attr (pointer);
905 if (!attr1.pointer && !attr1.proc_pointer)
907 gfc_error ("'%s' argument of '%s' intrinsic at %L must be a POINTER",
908 gfc_current_intrinsic_arg[0]->name, gfc_current_intrinsic,
909 &pointer->where);
910 return false;
913 /* F2008, C1242. */
914 if (attr1.pointer && gfc_is_coindexed (pointer))
916 gfc_error ("'%s' argument of '%s' intrinsic at %L shall not be "
917 "coindexed", gfc_current_intrinsic_arg[0]->name,
918 gfc_current_intrinsic, &pointer->where);
919 return false;
922 /* Target argument is optional. */
923 if (target == NULL)
924 return true;
926 where = &target->where;
927 if (target->expr_type == EXPR_NULL)
928 goto null_arg;
930 if (target->expr_type == EXPR_VARIABLE || target->expr_type == EXPR_FUNCTION)
931 attr2 = gfc_expr_attr (target);
932 else
934 gfc_error ("'%s' argument of '%s' intrinsic at %L must be a pointer "
935 "or target VARIABLE or FUNCTION",
936 gfc_current_intrinsic_arg[1]->name, gfc_current_intrinsic,
937 &target->where);
938 return false;
941 if (attr1.pointer && !attr2.pointer && !attr2.target)
943 gfc_error ("'%s' argument of '%s' intrinsic at %L must be a POINTER "
944 "or a TARGET", gfc_current_intrinsic_arg[1]->name,
945 gfc_current_intrinsic, &target->where);
946 return false;
949 /* F2008, C1242. */
950 if (attr1.pointer && gfc_is_coindexed (target))
952 gfc_error ("'%s' argument of '%s' intrinsic at %L shall not be "
953 "coindexed", gfc_current_intrinsic_arg[1]->name,
954 gfc_current_intrinsic, &target->where);
955 return false;
958 t = true;
959 if (!same_type_check (pointer, 0, target, 1))
960 t = false;
961 if (!rank_check (target, 0, pointer->rank))
962 t = false;
963 if (target->rank > 0)
965 for (i = 0; i < target->rank; i++)
966 if (target->ref->u.ar.dimen_type[i] == DIMEN_VECTOR)
968 gfc_error ("Array section with a vector subscript at %L shall not "
969 "be the target of a pointer",
970 &target->where);
971 t = false;
972 break;
975 return t;
977 null_arg:
979 gfc_error ("NULL pointer at %L is not permitted as actual argument "
980 "of '%s' intrinsic function", where, gfc_current_intrinsic);
981 return false;
986 bool
987 gfc_check_atan_2 (gfc_expr *y, gfc_expr *x)
989 /* gfc_notify_std would be a waste of time as the return value
990 is seemingly used only for the generic resolution. The error
991 will be: Too many arguments. */
992 if ((gfc_option.allow_std & GFC_STD_F2008) == 0)
993 return false;
995 return gfc_check_atan2 (y, x);
999 bool
1000 gfc_check_atan2 (gfc_expr *y, gfc_expr *x)
1002 if (!type_check (y, 0, BT_REAL))
1003 return false;
1004 if (!same_type_check (y, 0, x, 1))
1005 return false;
1007 return true;
1011 static bool
1012 gfc_check_atomic (gfc_expr *atom, gfc_expr *value)
1014 if (!(atom->ts.type == BT_INTEGER && atom->ts.kind == gfc_atomic_int_kind)
1015 && !(atom->ts.type == BT_LOGICAL
1016 && atom->ts.kind == gfc_atomic_logical_kind))
1018 gfc_error ("ATOM argument at %L to intrinsic function %s shall be an "
1019 "integer of ATOMIC_INT_KIND or a logical of "
1020 "ATOMIC_LOGICAL_KIND", &atom->where, gfc_current_intrinsic);
1021 return false;
1024 if (!gfc_expr_attr (atom).codimension)
1026 gfc_error ("ATOM argument at %L of the %s intrinsic function shall be a "
1027 "coarray or coindexed", &atom->where, gfc_current_intrinsic);
1028 return false;
1031 if (atom->ts.type != value->ts.type)
1033 gfc_error ("ATOM and VALUE argument of the %s intrinsic function shall "
1034 "have the same type at %L", gfc_current_intrinsic,
1035 &value->where);
1036 return false;
1039 return true;
1043 bool
1044 gfc_check_atomic_def (gfc_expr *atom, gfc_expr *value)
1046 if (!scalar_check (atom, 0) || !scalar_check (value, 1))
1047 return false;
1049 if (!gfc_check_vardef_context (atom, false, false, false, NULL))
1051 gfc_error ("ATOM argument of the %s intrinsic function at %L shall be "
1052 "definable", gfc_current_intrinsic, &atom->where);
1053 return false;
1056 return gfc_check_atomic (atom, value);
1060 bool
1061 gfc_check_atomic_ref (gfc_expr *value, gfc_expr *atom)
1063 if (!scalar_check (value, 0) || !scalar_check (atom, 1))
1064 return false;
1066 if (!gfc_check_vardef_context (value, false, false, false, NULL))
1068 gfc_error ("VALUE argument of the %s intrinsic function at %L shall be "
1069 "definable", gfc_current_intrinsic, &value->where);
1070 return false;
1073 return gfc_check_atomic (atom, value);
1077 /* BESJN and BESYN functions. */
1079 bool
1080 gfc_check_besn (gfc_expr *n, gfc_expr *x)
1082 if (!type_check (n, 0, BT_INTEGER))
1083 return false;
1084 if (n->expr_type == EXPR_CONSTANT)
1086 int i;
1087 gfc_extract_int (n, &i);
1088 if (i < 0 && !gfc_notify_std (GFC_STD_GNU, "Negative argument "
1089 "N at %L", &n->where))
1090 return false;
1093 if (!type_check (x, 1, BT_REAL))
1094 return false;
1096 return true;
1100 /* Transformational version of the Bessel JN and YN functions. */
1102 bool
1103 gfc_check_bessel_n2 (gfc_expr *n1, gfc_expr *n2, gfc_expr *x)
1105 if (!type_check (n1, 0, BT_INTEGER))
1106 return false;
1107 if (!scalar_check (n1, 0))
1108 return false;
1109 if (!nonnegative_check ("N1", n1))
1110 return false;
1112 if (!type_check (n2, 1, BT_INTEGER))
1113 return false;
1114 if (!scalar_check (n2, 1))
1115 return false;
1116 if (!nonnegative_check ("N2", n2))
1117 return false;
1119 if (!type_check (x, 2, BT_REAL))
1120 return false;
1121 if (!scalar_check (x, 2))
1122 return false;
1124 return true;
1128 bool
1129 gfc_check_bge_bgt_ble_blt (gfc_expr *i, gfc_expr *j)
1131 if (!type_check (i, 0, BT_INTEGER))
1132 return false;
1134 if (!type_check (j, 1, BT_INTEGER))
1135 return false;
1137 return true;
1141 bool
1142 gfc_check_bitfcn (gfc_expr *i, gfc_expr *pos)
1144 if (!type_check (i, 0, BT_INTEGER))
1145 return false;
1147 if (!type_check (pos, 1, BT_INTEGER))
1148 return false;
1150 if (!nonnegative_check ("pos", pos))
1151 return false;
1153 if (!less_than_bitsize1 ("i", i, "pos", pos, false))
1154 return false;
1156 return true;
1160 bool
1161 gfc_check_char (gfc_expr *i, gfc_expr *kind)
1163 if (!type_check (i, 0, BT_INTEGER))
1164 return false;
1165 if (!kind_check (kind, 1, BT_CHARACTER))
1166 return false;
1168 return true;
1172 bool
1173 gfc_check_chdir (gfc_expr *dir)
1175 if (!type_check (dir, 0, BT_CHARACTER))
1176 return false;
1177 if (!kind_value_check (dir, 0, gfc_default_character_kind))
1178 return false;
1180 return true;
1184 bool
1185 gfc_check_chdir_sub (gfc_expr *dir, gfc_expr *status)
1187 if (!type_check (dir, 0, BT_CHARACTER))
1188 return false;
1189 if (!kind_value_check (dir, 0, gfc_default_character_kind))
1190 return false;
1192 if (status == NULL)
1193 return true;
1195 if (!type_check (status, 1, BT_INTEGER))
1196 return false;
1197 if (!scalar_check (status, 1))
1198 return false;
1200 return true;
1204 bool
1205 gfc_check_chmod (gfc_expr *name, gfc_expr *mode)
1207 if (!type_check (name, 0, BT_CHARACTER))
1208 return false;
1209 if (!kind_value_check (name, 0, gfc_default_character_kind))
1210 return false;
1212 if (!type_check (mode, 1, BT_CHARACTER))
1213 return false;
1214 if (!kind_value_check (mode, 1, gfc_default_character_kind))
1215 return false;
1217 return true;
1221 bool
1222 gfc_check_chmod_sub (gfc_expr *name, gfc_expr *mode, gfc_expr *status)
1224 if (!type_check (name, 0, BT_CHARACTER))
1225 return false;
1226 if (!kind_value_check (name, 0, gfc_default_character_kind))
1227 return false;
1229 if (!type_check (mode, 1, BT_CHARACTER))
1230 return false;
1231 if (!kind_value_check (mode, 1, gfc_default_character_kind))
1232 return false;
1234 if (status == NULL)
1235 return true;
1237 if (!type_check (status, 2, BT_INTEGER))
1238 return false;
1240 if (!scalar_check (status, 2))
1241 return false;
1243 return true;
1247 bool
1248 gfc_check_cmplx (gfc_expr *x, gfc_expr *y, gfc_expr *kind)
1250 if (!numeric_check (x, 0))
1251 return false;
1253 if (y != NULL)
1255 if (!numeric_check (y, 1))
1256 return false;
1258 if (x->ts.type == BT_COMPLEX)
1260 gfc_error ("'%s' argument of '%s' intrinsic at %L must not be "
1261 "present if 'x' is COMPLEX",
1262 gfc_current_intrinsic_arg[1]->name, gfc_current_intrinsic,
1263 &y->where);
1264 return false;
1267 if (y->ts.type == BT_COMPLEX)
1269 gfc_error ("'%s' argument of '%s' intrinsic at %L must have a type "
1270 "of either REAL or INTEGER",
1271 gfc_current_intrinsic_arg[1]->name, gfc_current_intrinsic,
1272 &y->where);
1273 return false;
1278 if (!kind_check (kind, 2, BT_COMPLEX))
1279 return false;
1281 if (!kind && gfc_option.gfc_warn_conversion
1282 && x->ts.type == BT_REAL && x->ts.kind > gfc_default_real_kind)
1283 gfc_warning_now ("Conversion from %s to default-kind COMPLEX(%d) at %L "
1284 "might loose precision, consider using the KIND argument",
1285 gfc_typename (&x->ts), gfc_default_real_kind, &x->where);
1286 else if (y && !kind && gfc_option.gfc_warn_conversion
1287 && y->ts.type == BT_REAL && y->ts.kind > gfc_default_real_kind)
1288 gfc_warning_now ("Conversion from %s to default-kind COMPLEX(%d) at %L "
1289 "might loose precision, consider using the KIND argument",
1290 gfc_typename (&y->ts), gfc_default_real_kind, &y->where);
1292 return true;
1296 bool
1297 gfc_check_complex (gfc_expr *x, gfc_expr *y)
1299 if (!int_or_real_check (x, 0))
1300 return false;
1301 if (!scalar_check (x, 0))
1302 return false;
1304 if (!int_or_real_check (y, 1))
1305 return false;
1306 if (!scalar_check (y, 1))
1307 return false;
1309 return true;
1313 bool
1314 gfc_check_count (gfc_expr *mask, gfc_expr *dim, gfc_expr *kind)
1316 if (!logical_array_check (mask, 0))
1317 return false;
1318 if (!dim_check (dim, 1, false))
1319 return false;
1320 if (!dim_rank_check (dim, mask, 0))
1321 return false;
1322 if (!kind_check (kind, 2, BT_INTEGER))
1323 return false;
1324 if (kind && !gfc_notify_std (GFC_STD_F2003, "'%s' intrinsic "
1325 "with KIND argument at %L",
1326 gfc_current_intrinsic, &kind->where))
1327 return false;
1329 return true;
1333 bool
1334 gfc_check_cshift (gfc_expr *array, gfc_expr *shift, gfc_expr *dim)
1336 if (!array_check (array, 0))
1337 return false;
1339 if (!type_check (shift, 1, BT_INTEGER))
1340 return false;
1342 if (!dim_check (dim, 2, true))
1343 return false;
1345 if (!dim_rank_check (dim, array, false))
1346 return false;
1348 if (array->rank == 1 || shift->rank == 0)
1350 if (!scalar_check (shift, 1))
1351 return false;
1353 else if (shift->rank == array->rank - 1)
1355 int d;
1356 if (!dim)
1357 d = 1;
1358 else if (dim->expr_type == EXPR_CONSTANT)
1359 gfc_extract_int (dim, &d);
1360 else
1361 d = -1;
1363 if (d > 0)
1365 int i, j;
1366 for (i = 0, j = 0; i < array->rank; i++)
1367 if (i != d - 1)
1369 if (!identical_dimen_shape (array, i, shift, j))
1371 gfc_error ("'%s' argument of '%s' intrinsic at %L has "
1372 "invalid shape in dimension %d (%ld/%ld)",
1373 gfc_current_intrinsic_arg[1]->name,
1374 gfc_current_intrinsic, &shift->where, i + 1,
1375 mpz_get_si (array->shape[i]),
1376 mpz_get_si (shift->shape[j]));
1377 return false;
1380 j += 1;
1384 else
1386 gfc_error ("'%s' argument of intrinsic '%s' at %L of must have rank "
1387 "%d or be a scalar", gfc_current_intrinsic_arg[1]->name,
1388 gfc_current_intrinsic, &shift->where, array->rank - 1);
1389 return false;
1392 return true;
1396 bool
1397 gfc_check_ctime (gfc_expr *time)
1399 if (!scalar_check (time, 0))
1400 return false;
1402 if (!type_check (time, 0, BT_INTEGER))
1403 return false;
1405 return true;
1409 bool gfc_check_datan2 (gfc_expr *y, gfc_expr *x)
1411 if (!double_check (y, 0) || !double_check (x, 1))
1412 return false;
1414 return true;
1417 bool
1418 gfc_check_dcmplx (gfc_expr *x, gfc_expr *y)
1420 if (!numeric_check (x, 0))
1421 return false;
1423 if (y != NULL)
1425 if (!numeric_check (y, 1))
1426 return false;
1428 if (x->ts.type == BT_COMPLEX)
1430 gfc_error ("'%s' argument of '%s' intrinsic at %L must not be "
1431 "present if 'x' is COMPLEX",
1432 gfc_current_intrinsic_arg[1]->name, gfc_current_intrinsic,
1433 &y->where);
1434 return false;
1437 if (y->ts.type == BT_COMPLEX)
1439 gfc_error ("'%s' argument of '%s' intrinsic at %L must have a type "
1440 "of either REAL or INTEGER",
1441 gfc_current_intrinsic_arg[1]->name, gfc_current_intrinsic,
1442 &y->where);
1443 return false;
1447 return true;
1451 bool
1452 gfc_check_dble (gfc_expr *x)
1454 if (!numeric_check (x, 0))
1455 return false;
1457 return true;
1461 bool
1462 gfc_check_digits (gfc_expr *x)
1464 if (!int_or_real_check (x, 0))
1465 return false;
1467 return true;
1471 bool
1472 gfc_check_dot_product (gfc_expr *vector_a, gfc_expr *vector_b)
1474 switch (vector_a->ts.type)
1476 case BT_LOGICAL:
1477 if (!type_check (vector_b, 1, BT_LOGICAL))
1478 return false;
1479 break;
1481 case BT_INTEGER:
1482 case BT_REAL:
1483 case BT_COMPLEX:
1484 if (!numeric_check (vector_b, 1))
1485 return false;
1486 break;
1488 default:
1489 gfc_error ("'%s' argument of '%s' intrinsic at %L must be numeric "
1490 "or LOGICAL", gfc_current_intrinsic_arg[0]->name,
1491 gfc_current_intrinsic, &vector_a->where);
1492 return false;
1495 if (!rank_check (vector_a, 0, 1))
1496 return false;
1498 if (!rank_check (vector_b, 1, 1))
1499 return false;
1501 if (! identical_dimen_shape (vector_a, 0, vector_b, 0))
1503 gfc_error ("Different shape for arguments '%s' and '%s' at %L for "
1504 "intrinsic 'dot_product'", gfc_current_intrinsic_arg[0]->name,
1505 gfc_current_intrinsic_arg[1]->name, &vector_a->where);
1506 return false;
1509 return true;
1513 bool
1514 gfc_check_dprod (gfc_expr *x, gfc_expr *y)
1516 if (!type_check (x, 0, BT_REAL)
1517 || !type_check (y, 1, BT_REAL))
1518 return false;
1520 if (x->ts.kind != gfc_default_real_kind)
1522 gfc_error ("'%s' argument of '%s' intrinsic at %L must be default "
1523 "real", gfc_current_intrinsic_arg[0]->name,
1524 gfc_current_intrinsic, &x->where);
1525 return false;
1528 if (y->ts.kind != gfc_default_real_kind)
1530 gfc_error ("'%s' argument of '%s' intrinsic at %L must be default "
1531 "real", gfc_current_intrinsic_arg[1]->name,
1532 gfc_current_intrinsic, &y->where);
1533 return false;
1536 return true;
1540 bool
1541 gfc_check_dshift (gfc_expr *i, gfc_expr *j, gfc_expr *shift)
1543 if (!type_check (i, 0, BT_INTEGER))
1544 return false;
1546 if (!type_check (j, 1, BT_INTEGER))
1547 return false;
1549 if (i->is_boz && j->is_boz)
1551 gfc_error ("'I' at %L and 'J' at %L cannot both be BOZ literal "
1552 "constants", &i->where, &j->where);
1553 return false;
1556 if (!i->is_boz && !j->is_boz && !same_type_check (i, 0, j, 1))
1557 return false;
1559 if (!type_check (shift, 2, BT_INTEGER))
1560 return false;
1562 if (!nonnegative_check ("SHIFT", shift))
1563 return false;
1565 if (i->is_boz)
1567 if (!less_than_bitsize1 ("J", j, "SHIFT", shift, true))
1568 return false;
1569 i->ts.kind = j->ts.kind;
1571 else
1573 if (!less_than_bitsize1 ("I", i, "SHIFT", shift, true))
1574 return false;
1575 j->ts.kind = i->ts.kind;
1578 return true;
1582 bool
1583 gfc_check_eoshift (gfc_expr *array, gfc_expr *shift, gfc_expr *boundary,
1584 gfc_expr *dim)
1586 if (!array_check (array, 0))
1587 return false;
1589 if (!type_check (shift, 1, BT_INTEGER))
1590 return false;
1592 if (!dim_check (dim, 3, true))
1593 return false;
1595 if (!dim_rank_check (dim, array, false))
1596 return false;
1598 if (array->rank == 1 || shift->rank == 0)
1600 if (!scalar_check (shift, 1))
1601 return false;
1603 else if (shift->rank == array->rank - 1)
1605 int d;
1606 if (!dim)
1607 d = 1;
1608 else if (dim->expr_type == EXPR_CONSTANT)
1609 gfc_extract_int (dim, &d);
1610 else
1611 d = -1;
1613 if (d > 0)
1615 int i, j;
1616 for (i = 0, j = 0; i < array->rank; i++)
1617 if (i != d - 1)
1619 if (!identical_dimen_shape (array, i, shift, j))
1621 gfc_error ("'%s' argument of '%s' intrinsic at %L has "
1622 "invalid shape in dimension %d (%ld/%ld)",
1623 gfc_current_intrinsic_arg[1]->name,
1624 gfc_current_intrinsic, &shift->where, i + 1,
1625 mpz_get_si (array->shape[i]),
1626 mpz_get_si (shift->shape[j]));
1627 return false;
1630 j += 1;
1634 else
1636 gfc_error ("'%s' argument of intrinsic '%s' at %L of must have rank "
1637 "%d or be a scalar", gfc_current_intrinsic_arg[1]->name,
1638 gfc_current_intrinsic, &shift->where, array->rank - 1);
1639 return false;
1642 if (boundary != NULL)
1644 if (!same_type_check (array, 0, boundary, 2))
1645 return false;
1647 if (array->rank == 1 || boundary->rank == 0)
1649 if (!scalar_check (boundary, 2))
1650 return false;
1652 else if (boundary->rank == array->rank - 1)
1654 if (!gfc_check_conformance (shift, boundary,
1655 "arguments '%s' and '%s' for "
1656 "intrinsic %s",
1657 gfc_current_intrinsic_arg[1]->name,
1658 gfc_current_intrinsic_arg[2]->name,
1659 gfc_current_intrinsic))
1660 return false;
1662 else
1664 gfc_error ("'%s' argument of intrinsic '%s' at %L of must have "
1665 "rank %d or be a scalar",
1666 gfc_current_intrinsic_arg[1]->name, gfc_current_intrinsic,
1667 &shift->where, array->rank - 1);
1668 return false;
1672 return true;
1675 bool
1676 gfc_check_float (gfc_expr *a)
1678 if (!type_check (a, 0, BT_INTEGER))
1679 return false;
1681 if ((a->ts.kind != gfc_default_integer_kind)
1682 && !gfc_notify_std (GFC_STD_GNU, "non-default INTEGER "
1683 "kind argument to %s intrinsic at %L",
1684 gfc_current_intrinsic, &a->where))
1685 return false;
1687 return true;
1690 /* A single complex argument. */
1692 bool
1693 gfc_check_fn_c (gfc_expr *a)
1695 if (!type_check (a, 0, BT_COMPLEX))
1696 return false;
1698 return true;
1701 /* A single real argument. */
1703 bool
1704 gfc_check_fn_r (gfc_expr *a)
1706 if (!type_check (a, 0, BT_REAL))
1707 return false;
1709 return true;
1712 /* A single double argument. */
1714 bool
1715 gfc_check_fn_d (gfc_expr *a)
1717 if (!double_check (a, 0))
1718 return false;
1720 return true;
1723 /* A single real or complex argument. */
1725 bool
1726 gfc_check_fn_rc (gfc_expr *a)
1728 if (!real_or_complex_check (a, 0))
1729 return false;
1731 return true;
1735 bool
1736 gfc_check_fn_rc2008 (gfc_expr *a)
1738 if (!real_or_complex_check (a, 0))
1739 return false;
1741 if (a->ts.type == BT_COMPLEX
1742 && !gfc_notify_std (GFC_STD_F2008, "COMPLEX argument '%s' "
1743 "argument of '%s' intrinsic at %L",
1744 gfc_current_intrinsic_arg[0]->name,
1745 gfc_current_intrinsic, &a->where))
1746 return false;
1748 return true;
1752 bool
1753 gfc_check_fnum (gfc_expr *unit)
1755 if (!type_check (unit, 0, BT_INTEGER))
1756 return false;
1758 if (!scalar_check (unit, 0))
1759 return false;
1761 return true;
1765 bool
1766 gfc_check_huge (gfc_expr *x)
1768 if (!int_or_real_check (x, 0))
1769 return false;
1771 return true;
1775 bool
1776 gfc_check_hypot (gfc_expr *x, gfc_expr *y)
1778 if (!type_check (x, 0, BT_REAL))
1779 return false;
1780 if (!same_type_check (x, 0, y, 1))
1781 return false;
1783 return true;
1787 /* Check that the single argument is an integer. */
1789 bool
1790 gfc_check_i (gfc_expr *i)
1792 if (!type_check (i, 0, BT_INTEGER))
1793 return false;
1795 return true;
1799 bool
1800 gfc_check_iand (gfc_expr *i, gfc_expr *j)
1802 if (!type_check (i, 0, BT_INTEGER))
1803 return false;
1805 if (!type_check (j, 1, BT_INTEGER))
1806 return false;
1808 if (i->ts.kind != j->ts.kind)
1810 if (!gfc_notify_std (GFC_STD_GNU, "Different type kinds at %L",
1811 &i->where))
1812 return false;
1815 return true;
1819 bool
1820 gfc_check_ibits (gfc_expr *i, gfc_expr *pos, gfc_expr *len)
1822 if (!type_check (i, 0, BT_INTEGER))
1823 return false;
1825 if (!type_check (pos, 1, BT_INTEGER))
1826 return false;
1828 if (!type_check (len, 2, BT_INTEGER))
1829 return false;
1831 if (!nonnegative_check ("pos", pos))
1832 return false;
1834 if (!nonnegative_check ("len", len))
1835 return false;
1837 if (!less_than_bitsize2 ("i", i, "pos", pos, "len", len))
1838 return false;
1840 return true;
1844 bool
1845 gfc_check_ichar_iachar (gfc_expr *c, gfc_expr *kind)
1847 int i;
1849 if (!type_check (c, 0, BT_CHARACTER))
1850 return false;
1852 if (!kind_check (kind, 1, BT_INTEGER))
1853 return false;
1855 if (kind && !gfc_notify_std (GFC_STD_F2003, "'%s' intrinsic "
1856 "with KIND argument at %L",
1857 gfc_current_intrinsic, &kind->where))
1858 return false;
1860 if (c->expr_type == EXPR_VARIABLE || c->expr_type == EXPR_SUBSTRING)
1862 gfc_expr *start;
1863 gfc_expr *end;
1864 gfc_ref *ref;
1866 /* Substring references don't have the charlength set. */
1867 ref = c->ref;
1868 while (ref && ref->type != REF_SUBSTRING)
1869 ref = ref->next;
1871 gcc_assert (ref == NULL || ref->type == REF_SUBSTRING);
1873 if (!ref)
1875 /* Check that the argument is length one. Non-constant lengths
1876 can't be checked here, so assume they are ok. */
1877 if (c->ts.u.cl && c->ts.u.cl->length)
1879 /* If we already have a length for this expression then use it. */
1880 if (c->ts.u.cl->length->expr_type != EXPR_CONSTANT)
1881 return true;
1882 i = mpz_get_si (c->ts.u.cl->length->value.integer);
1884 else
1885 return true;
1887 else
1889 start = ref->u.ss.start;
1890 end = ref->u.ss.end;
1892 gcc_assert (start);
1893 if (end == NULL || end->expr_type != EXPR_CONSTANT
1894 || start->expr_type != EXPR_CONSTANT)
1895 return true;
1897 i = mpz_get_si (end->value.integer) + 1
1898 - mpz_get_si (start->value.integer);
1901 else
1902 return true;
1904 if (i != 1)
1906 gfc_error ("Argument of %s at %L must be of length one",
1907 gfc_current_intrinsic, &c->where);
1908 return false;
1911 return true;
1915 bool
1916 gfc_check_idnint (gfc_expr *a)
1918 if (!double_check (a, 0))
1919 return false;
1921 return true;
1925 bool
1926 gfc_check_ieor (gfc_expr *i, gfc_expr *j)
1928 if (!type_check (i, 0, BT_INTEGER))
1929 return false;
1931 if (!type_check (j, 1, BT_INTEGER))
1932 return false;
1934 if (i->ts.kind != j->ts.kind)
1936 if (!gfc_notify_std (GFC_STD_GNU, "Different type kinds at %L",
1937 &i->where))
1938 return false;
1941 return true;
1945 bool
1946 gfc_check_index (gfc_expr *string, gfc_expr *substring, gfc_expr *back,
1947 gfc_expr *kind)
1949 if (!type_check (string, 0, BT_CHARACTER)
1950 || !type_check (substring, 1, BT_CHARACTER))
1951 return false;
1953 if (back != NULL && !type_check (back, 2, BT_LOGICAL))
1954 return false;
1956 if (!kind_check (kind, 3, BT_INTEGER))
1957 return false;
1958 if (kind && !gfc_notify_std (GFC_STD_F2003, "'%s' intrinsic "
1959 "with KIND argument at %L",
1960 gfc_current_intrinsic, &kind->where))
1961 return false;
1963 if (string->ts.kind != substring->ts.kind)
1965 gfc_error ("'%s' argument of '%s' intrinsic at %L must be the same "
1966 "kind as '%s'", gfc_current_intrinsic_arg[1]->name,
1967 gfc_current_intrinsic, &substring->where,
1968 gfc_current_intrinsic_arg[0]->name);
1969 return false;
1972 return true;
1976 bool
1977 gfc_check_int (gfc_expr *x, gfc_expr *kind)
1979 if (!numeric_check (x, 0))
1980 return false;
1982 if (!kind_check (kind, 1, BT_INTEGER))
1983 return false;
1985 return true;
1989 bool
1990 gfc_check_intconv (gfc_expr *x)
1992 if (!numeric_check (x, 0))
1993 return false;
1995 return true;
1999 bool
2000 gfc_check_ior (gfc_expr *i, gfc_expr *j)
2002 if (!type_check (i, 0, BT_INTEGER))
2003 return false;
2005 if (!type_check (j, 1, BT_INTEGER))
2006 return false;
2008 if (i->ts.kind != j->ts.kind)
2010 if (!gfc_notify_std (GFC_STD_GNU, "Different type kinds at %L",
2011 &i->where))
2012 return false;
2015 return true;
2019 bool
2020 gfc_check_ishft (gfc_expr *i, gfc_expr *shift)
2022 if (!type_check (i, 0, BT_INTEGER)
2023 || !type_check (shift, 1, BT_INTEGER))
2024 return false;
2026 if (!less_than_bitsize1 ("I", i, NULL, shift, true))
2027 return false;
2029 return true;
2033 bool
2034 gfc_check_ishftc (gfc_expr *i, gfc_expr *shift, gfc_expr *size)
2036 if (!type_check (i, 0, BT_INTEGER)
2037 || !type_check (shift, 1, BT_INTEGER))
2038 return false;
2040 if (size != NULL)
2042 int i2, i3;
2044 if (!type_check (size, 2, BT_INTEGER))
2045 return false;
2047 if (!less_than_bitsize1 ("I", i, "SIZE", size, true))
2048 return false;
2050 if (size->expr_type == EXPR_CONSTANT)
2052 gfc_extract_int (size, &i3);
2053 if (i3 <= 0)
2055 gfc_error ("SIZE at %L must be positive", &size->where);
2056 return false;
2059 if (shift->expr_type == EXPR_CONSTANT)
2061 gfc_extract_int (shift, &i2);
2062 if (i2 < 0)
2063 i2 = -i2;
2065 if (i2 > i3)
2067 gfc_error ("The absolute value of SHIFT at %L must be less "
2068 "than or equal to SIZE at %L", &shift->where,
2069 &size->where);
2070 return false;
2075 else if (!less_than_bitsize1 ("I", i, NULL, shift, true))
2076 return false;
2078 return true;
2082 bool
2083 gfc_check_kill (gfc_expr *pid, gfc_expr *sig)
2085 if (!type_check (pid, 0, BT_INTEGER))
2086 return false;
2088 if (!type_check (sig, 1, BT_INTEGER))
2089 return false;
2091 return true;
2095 bool
2096 gfc_check_kill_sub (gfc_expr *pid, gfc_expr *sig, gfc_expr *status)
2098 if (!type_check (pid, 0, BT_INTEGER))
2099 return false;
2101 if (!scalar_check (pid, 0))
2102 return false;
2104 if (!type_check (sig, 1, BT_INTEGER))
2105 return false;
2107 if (!scalar_check (sig, 1))
2108 return false;
2110 if (status == NULL)
2111 return true;
2113 if (!type_check (status, 2, BT_INTEGER))
2114 return false;
2116 if (!scalar_check (status, 2))
2117 return false;
2119 return true;
2123 bool
2124 gfc_check_kind (gfc_expr *x)
2126 if (x->ts.type == BT_DERIVED)
2128 gfc_error ("'%s' argument of '%s' intrinsic at %L must be a "
2129 "non-derived type", gfc_current_intrinsic_arg[0]->name,
2130 gfc_current_intrinsic, &x->where);
2131 return false;
2134 return true;
2138 bool
2139 gfc_check_lbound (gfc_expr *array, gfc_expr *dim, gfc_expr *kind)
2141 if (!array_check (array, 0))
2142 return false;
2144 if (!dim_check (dim, 1, false))
2145 return false;
2147 if (!dim_rank_check (dim, array, 1))
2148 return false;
2150 if (!kind_check (kind, 2, BT_INTEGER))
2151 return false;
2152 if (kind && !gfc_notify_std (GFC_STD_F2003, "'%s' intrinsic "
2153 "with KIND argument at %L",
2154 gfc_current_intrinsic, &kind->where))
2155 return false;
2157 return true;
2161 bool
2162 gfc_check_lcobound (gfc_expr *coarray, gfc_expr *dim, gfc_expr *kind)
2164 if (gfc_option.coarray == GFC_FCOARRAY_NONE)
2166 gfc_fatal_error ("Coarrays disabled at %C, use -fcoarray= to enable");
2167 return false;
2170 if (!coarray_check (coarray, 0))
2171 return false;
2173 if (dim != NULL)
2175 if (!dim_check (dim, 1, false))
2176 return false;
2178 if (!dim_corank_check (dim, coarray))
2179 return false;
2182 if (!kind_check (kind, 2, BT_INTEGER))
2183 return false;
2185 return true;
2189 bool
2190 gfc_check_len_lentrim (gfc_expr *s, gfc_expr *kind)
2192 if (!type_check (s, 0, BT_CHARACTER))
2193 return false;
2195 if (!kind_check (kind, 1, BT_INTEGER))
2196 return false;
2197 if (kind && !gfc_notify_std (GFC_STD_F2003, "'%s' intrinsic "
2198 "with KIND argument at %L",
2199 gfc_current_intrinsic, &kind->where))
2200 return false;
2202 return true;
2206 bool
2207 gfc_check_lge_lgt_lle_llt (gfc_expr *a, gfc_expr *b)
2209 if (!type_check (a, 0, BT_CHARACTER))
2210 return false;
2211 if (!kind_value_check (a, 0, gfc_default_character_kind))
2212 return false;
2214 if (!type_check (b, 1, BT_CHARACTER))
2215 return false;
2216 if (!kind_value_check (b, 1, gfc_default_character_kind))
2217 return false;
2219 return true;
2223 bool
2224 gfc_check_link (gfc_expr *path1, gfc_expr *path2)
2226 if (!type_check (path1, 0, BT_CHARACTER))
2227 return false;
2228 if (!kind_value_check (path1, 0, gfc_default_character_kind))
2229 return false;
2231 if (!type_check (path2, 1, BT_CHARACTER))
2232 return false;
2233 if (!kind_value_check (path2, 1, gfc_default_character_kind))
2234 return false;
2236 return true;
2240 bool
2241 gfc_check_link_sub (gfc_expr *path1, gfc_expr *path2, gfc_expr *status)
2243 if (!type_check (path1, 0, BT_CHARACTER))
2244 return false;
2245 if (!kind_value_check (path1, 0, gfc_default_character_kind))
2246 return false;
2248 if (!type_check (path2, 1, BT_CHARACTER))
2249 return false;
2250 if (!kind_value_check (path2, 0, gfc_default_character_kind))
2251 return false;
2253 if (status == NULL)
2254 return true;
2256 if (!type_check (status, 2, BT_INTEGER))
2257 return false;
2259 if (!scalar_check (status, 2))
2260 return false;
2262 return true;
2266 bool
2267 gfc_check_loc (gfc_expr *expr)
2269 return variable_check (expr, 0, true);
2273 bool
2274 gfc_check_symlnk (gfc_expr *path1, gfc_expr *path2)
2276 if (!type_check (path1, 0, BT_CHARACTER))
2277 return false;
2278 if (!kind_value_check (path1, 0, gfc_default_character_kind))
2279 return false;
2281 if (!type_check (path2, 1, BT_CHARACTER))
2282 return false;
2283 if (!kind_value_check (path2, 1, gfc_default_character_kind))
2284 return false;
2286 return true;
2290 bool
2291 gfc_check_symlnk_sub (gfc_expr *path1, gfc_expr *path2, gfc_expr *status)
2293 if (!type_check (path1, 0, BT_CHARACTER))
2294 return false;
2295 if (!kind_value_check (path1, 0, gfc_default_character_kind))
2296 return false;
2298 if (!type_check (path2, 1, BT_CHARACTER))
2299 return false;
2300 if (!kind_value_check (path2, 1, gfc_default_character_kind))
2301 return false;
2303 if (status == NULL)
2304 return true;
2306 if (!type_check (status, 2, BT_INTEGER))
2307 return false;
2309 if (!scalar_check (status, 2))
2310 return false;
2312 return true;
2316 bool
2317 gfc_check_logical (gfc_expr *a, gfc_expr *kind)
2319 if (!type_check (a, 0, BT_LOGICAL))
2320 return false;
2321 if (!kind_check (kind, 1, BT_LOGICAL))
2322 return false;
2324 return true;
2328 /* Min/max family. */
2330 static bool
2331 min_max_args (gfc_actual_arglist *arg)
2333 if (arg == NULL || arg->next == NULL)
2335 gfc_error ("Intrinsic '%s' at %L must have at least two arguments",
2336 gfc_current_intrinsic, gfc_current_intrinsic_where);
2337 return false;
2340 return true;
2344 static bool
2345 check_rest (bt type, int kind, gfc_actual_arglist *arglist)
2347 gfc_actual_arglist *arg, *tmp;
2349 gfc_expr *x;
2350 int m, n;
2352 if (!min_max_args (arglist))
2353 return false;
2355 for (arg = arglist, n=1; arg; arg = arg->next, n++)
2357 x = arg->expr;
2358 if (x->ts.type != type || x->ts.kind != kind)
2360 if (x->ts.type == type)
2362 if (!gfc_notify_std (GFC_STD_GNU, "Different type "
2363 "kinds at %L", &x->where))
2364 return false;
2366 else
2368 gfc_error ("'a%d' argument of '%s' intrinsic at %L must be "
2369 "%s(%d)", n, gfc_current_intrinsic, &x->where,
2370 gfc_basic_typename (type), kind);
2371 return false;
2375 for (tmp = arglist, m=1; tmp != arg; tmp = tmp->next, m++)
2376 if (!gfc_check_conformance (tmp->expr, x,
2377 "arguments 'a%d' and 'a%d' for "
2378 "intrinsic '%s'", m, n,
2379 gfc_current_intrinsic))
2380 return false;
2383 return true;
2387 bool
2388 gfc_check_min_max (gfc_actual_arglist *arg)
2390 gfc_expr *x;
2392 if (!min_max_args (arg))
2393 return false;
2395 x = arg->expr;
2397 if (x->ts.type == BT_CHARACTER)
2399 if (!gfc_notify_std (GFC_STD_F2003, "'%s' intrinsic "
2400 "with CHARACTER argument at %L",
2401 gfc_current_intrinsic, &x->where))
2402 return false;
2404 else if (x->ts.type != BT_INTEGER && x->ts.type != BT_REAL)
2406 gfc_error ("'a1' argument of '%s' intrinsic at %L must be INTEGER, "
2407 "REAL or CHARACTER", gfc_current_intrinsic, &x->where);
2408 return false;
2411 return check_rest (x->ts.type, x->ts.kind, arg);
2415 bool
2416 gfc_check_min_max_integer (gfc_actual_arglist *arg)
2418 return check_rest (BT_INTEGER, gfc_default_integer_kind, arg);
2422 bool
2423 gfc_check_min_max_real (gfc_actual_arglist *arg)
2425 return check_rest (BT_REAL, gfc_default_real_kind, arg);
2429 bool
2430 gfc_check_min_max_double (gfc_actual_arglist *arg)
2432 return check_rest (BT_REAL, gfc_default_double_kind, arg);
2436 /* End of min/max family. */
2438 bool
2439 gfc_check_malloc (gfc_expr *size)
2441 if (!type_check (size, 0, BT_INTEGER))
2442 return false;
2444 if (!scalar_check (size, 0))
2445 return false;
2447 return true;
2451 bool
2452 gfc_check_matmul (gfc_expr *matrix_a, gfc_expr *matrix_b)
2454 if ((matrix_a->ts.type != BT_LOGICAL) && !gfc_numeric_ts (&matrix_a->ts))
2456 gfc_error ("'%s' argument of '%s' intrinsic at %L must be numeric "
2457 "or LOGICAL", gfc_current_intrinsic_arg[0]->name,
2458 gfc_current_intrinsic, &matrix_a->where);
2459 return false;
2462 if ((matrix_b->ts.type != BT_LOGICAL) && !gfc_numeric_ts (&matrix_b->ts))
2464 gfc_error ("'%s' argument of '%s' intrinsic at %L must be numeric "
2465 "or LOGICAL", gfc_current_intrinsic_arg[1]->name,
2466 gfc_current_intrinsic, &matrix_b->where);
2467 return false;
2470 if ((matrix_a->ts.type == BT_LOGICAL && gfc_numeric_ts (&matrix_b->ts))
2471 || (gfc_numeric_ts (&matrix_a->ts) && matrix_b->ts.type == BT_LOGICAL))
2473 gfc_error ("Argument types of '%s' intrinsic at %L must match (%s/%s)",
2474 gfc_current_intrinsic, &matrix_a->where,
2475 gfc_typename(&matrix_a->ts), gfc_typename(&matrix_b->ts));
2476 return false;
2479 switch (matrix_a->rank)
2481 case 1:
2482 if (!rank_check (matrix_b, 1, 2))
2483 return false;
2484 /* Check for case matrix_a has shape(m), matrix_b has shape (m, k). */
2485 if (!identical_dimen_shape (matrix_a, 0, matrix_b, 0))
2487 gfc_error ("Different shape on dimension 1 for arguments '%s' "
2488 "and '%s' at %L for intrinsic matmul",
2489 gfc_current_intrinsic_arg[0]->name,
2490 gfc_current_intrinsic_arg[1]->name, &matrix_a->where);
2491 return false;
2493 break;
2495 case 2:
2496 if (matrix_b->rank != 2)
2498 if (!rank_check (matrix_b, 1, 1))
2499 return false;
2501 /* matrix_b has rank 1 or 2 here. Common check for the cases
2502 - matrix_a has shape (n,m) and matrix_b has shape (m, k)
2503 - matrix_a has shape (n,m) and matrix_b has shape (m). */
2504 if (!identical_dimen_shape (matrix_a, 1, matrix_b, 0))
2506 gfc_error ("Different shape on dimension 2 for argument '%s' and "
2507 "dimension 1 for argument '%s' at %L for intrinsic "
2508 "matmul", gfc_current_intrinsic_arg[0]->name,
2509 gfc_current_intrinsic_arg[1]->name, &matrix_a->where);
2510 return false;
2512 break;
2514 default:
2515 gfc_error ("'%s' argument of '%s' intrinsic at %L must be of rank "
2516 "1 or 2", gfc_current_intrinsic_arg[0]->name,
2517 gfc_current_intrinsic, &matrix_a->where);
2518 return false;
2521 return true;
2525 /* Whoever came up with this interface was probably on something.
2526 The possibilities for the occupation of the second and third
2527 parameters are:
2529 Arg #2 Arg #3
2530 NULL NULL
2531 DIM NULL
2532 MASK NULL
2533 NULL MASK minloc(array, mask=m)
2534 DIM MASK
2536 I.e. in the case of minloc(array,mask), mask will be in the second
2537 position of the argument list and we'll have to fix that up. */
2539 bool
2540 gfc_check_minloc_maxloc (gfc_actual_arglist *ap)
2542 gfc_expr *a, *m, *d;
2544 a = ap->expr;
2545 if (!int_or_real_check (a, 0) || !array_check (a, 0))
2546 return false;
2548 d = ap->next->expr;
2549 m = ap->next->next->expr;
2551 if (m == NULL && d != NULL && d->ts.type == BT_LOGICAL
2552 && ap->next->name == NULL)
2554 m = d;
2555 d = NULL;
2556 ap->next->expr = NULL;
2557 ap->next->next->expr = m;
2560 if (!dim_check (d, 1, false))
2561 return false;
2563 if (!dim_rank_check (d, a, 0))
2564 return false;
2566 if (m != NULL && !type_check (m, 2, BT_LOGICAL))
2567 return false;
2569 if (m != NULL
2570 && !gfc_check_conformance (a, m,
2571 "arguments '%s' and '%s' for intrinsic %s",
2572 gfc_current_intrinsic_arg[0]->name,
2573 gfc_current_intrinsic_arg[2]->name,
2574 gfc_current_intrinsic))
2575 return false;
2577 return true;
2581 /* Similar to minloc/maxloc, the argument list might need to be
2582 reordered for the MINVAL, MAXVAL, PRODUCT, and SUM intrinsics. The
2583 difference is that MINLOC/MAXLOC take an additional KIND argument.
2584 The possibilities are:
2586 Arg #2 Arg #3
2587 NULL NULL
2588 DIM NULL
2589 MASK NULL
2590 NULL MASK minval(array, mask=m)
2591 DIM MASK
2593 I.e. in the case of minval(array,mask), mask will be in the second
2594 position of the argument list and we'll have to fix that up. */
2596 static bool
2597 check_reduction (gfc_actual_arglist *ap)
2599 gfc_expr *a, *m, *d;
2601 a = ap->expr;
2602 d = ap->next->expr;
2603 m = ap->next->next->expr;
2605 if (m == NULL && d != NULL && d->ts.type == BT_LOGICAL
2606 && ap->next->name == NULL)
2608 m = d;
2609 d = NULL;
2610 ap->next->expr = NULL;
2611 ap->next->next->expr = m;
2614 if (!dim_check (d, 1, false))
2615 return false;
2617 if (!dim_rank_check (d, a, 0))
2618 return false;
2620 if (m != NULL && !type_check (m, 2, BT_LOGICAL))
2621 return false;
2623 if (m != NULL
2624 && !gfc_check_conformance (a, m,
2625 "arguments '%s' and '%s' for intrinsic %s",
2626 gfc_current_intrinsic_arg[0]->name,
2627 gfc_current_intrinsic_arg[2]->name,
2628 gfc_current_intrinsic))
2629 return false;
2631 return true;
2635 bool
2636 gfc_check_minval_maxval (gfc_actual_arglist *ap)
2638 if (!int_or_real_check (ap->expr, 0)
2639 || !array_check (ap->expr, 0))
2640 return false;
2642 return check_reduction (ap);
2646 bool
2647 gfc_check_product_sum (gfc_actual_arglist *ap)
2649 if (!numeric_check (ap->expr, 0)
2650 || !array_check (ap->expr, 0))
2651 return false;
2653 return check_reduction (ap);
2657 /* For IANY, IALL and IPARITY. */
2659 bool
2660 gfc_check_mask (gfc_expr *i, gfc_expr *kind)
2662 int k;
2664 if (!type_check (i, 0, BT_INTEGER))
2665 return false;
2667 if (!nonnegative_check ("I", i))
2668 return false;
2670 if (!kind_check (kind, 1, BT_INTEGER))
2671 return false;
2673 if (kind)
2674 gfc_extract_int (kind, &k);
2675 else
2676 k = gfc_default_integer_kind;
2678 if (!less_than_bitsizekind ("I", i, k))
2679 return false;
2681 return true;
2685 bool
2686 gfc_check_transf_bit_intrins (gfc_actual_arglist *ap)
2688 if (ap->expr->ts.type != BT_INTEGER)
2690 gfc_error ("'%s' argument of '%s' intrinsic at %L must be INTEGER",
2691 gfc_current_intrinsic_arg[0]->name,
2692 gfc_current_intrinsic, &ap->expr->where);
2693 return false;
2696 if (!array_check (ap->expr, 0))
2697 return false;
2699 return check_reduction (ap);
2703 bool
2704 gfc_check_merge (gfc_expr *tsource, gfc_expr *fsource, gfc_expr *mask)
2706 if (!same_type_check (tsource, 0, fsource, 1))
2707 return false;
2709 if (!type_check (mask, 2, BT_LOGICAL))
2710 return false;
2712 if (tsource->ts.type == BT_CHARACTER)
2713 return gfc_check_same_strlen (tsource, fsource, "MERGE intrinsic");
2715 return true;
2719 bool
2720 gfc_check_merge_bits (gfc_expr *i, gfc_expr *j, gfc_expr *mask)
2722 if (!type_check (i, 0, BT_INTEGER))
2723 return false;
2725 if (!type_check (j, 1, BT_INTEGER))
2726 return false;
2728 if (!type_check (mask, 2, BT_INTEGER))
2729 return false;
2731 if (!same_type_check (i, 0, j, 1))
2732 return false;
2734 if (!same_type_check (i, 0, mask, 2))
2735 return false;
2737 return true;
2741 bool
2742 gfc_check_move_alloc (gfc_expr *from, gfc_expr *to)
2744 if (!variable_check (from, 0, false))
2745 return false;
2746 if (!allocatable_check (from, 0))
2747 return false;
2748 if (gfc_is_coindexed (from))
2750 gfc_error ("The FROM argument to MOVE_ALLOC at %L shall not be "
2751 "coindexed", &from->where);
2752 return false;
2755 if (!variable_check (to, 1, false))
2756 return false;
2757 if (!allocatable_check (to, 1))
2758 return false;
2759 if (gfc_is_coindexed (to))
2761 gfc_error ("The TO argument to MOVE_ALLOC at %L shall not be "
2762 "coindexed", &to->where);
2763 return false;
2766 if (from->ts.type == BT_CLASS && to->ts.type == BT_DERIVED)
2768 gfc_error ("The TO arguments in MOVE_ALLOC at %L must be "
2769 "polymorphic if FROM is polymorphic",
2770 &to->where);
2771 return false;
2774 if (!same_type_check (to, 1, from, 0))
2775 return false;
2777 if (to->rank != from->rank)
2779 gfc_error ("The FROM and TO arguments of the MOVE_ALLOC intrinsic at %L "
2780 "must have the same rank %d/%d", &to->where, from->rank,
2781 to->rank);
2782 return false;
2785 /* IR F08/0040; cf. 12-006A. */
2786 if (gfc_get_corank (to) != gfc_get_corank (from))
2788 gfc_error ("The FROM and TO arguments of the MOVE_ALLOC intrinsic at %L "
2789 "must have the same corank %d/%d", &to->where,
2790 gfc_get_corank (from), gfc_get_corank (to));
2791 return false;
2794 /* CLASS arguments: Make sure the vtab of from is present. */
2795 if (to->ts.type == BT_CLASS && !UNLIMITED_POLY (from))
2797 if (from->ts.type == BT_CLASS || from->ts.type == BT_DERIVED)
2798 gfc_find_derived_vtab (from->ts.u.derived);
2799 else
2800 gfc_find_intrinsic_vtab (&from->ts);
2803 return true;
2807 bool
2808 gfc_check_nearest (gfc_expr *x, gfc_expr *s)
2810 if (!type_check (x, 0, BT_REAL))
2811 return false;
2813 if (!type_check (s, 1, BT_REAL))
2814 return false;
2816 if (s->expr_type == EXPR_CONSTANT)
2818 if (mpfr_sgn (s->value.real) == 0)
2820 gfc_error ("Argument 'S' of NEAREST at %L shall not be zero",
2821 &s->where);
2822 return false;
2826 return true;
2830 bool
2831 gfc_check_new_line (gfc_expr *a)
2833 if (!type_check (a, 0, BT_CHARACTER))
2834 return false;
2836 return true;
2840 bool
2841 gfc_check_norm2 (gfc_expr *array, gfc_expr *dim)
2843 if (!type_check (array, 0, BT_REAL))
2844 return false;
2846 if (!array_check (array, 0))
2847 return false;
2849 if (!dim_rank_check (dim, array, false))
2850 return false;
2852 return true;
2855 bool
2856 gfc_check_null (gfc_expr *mold)
2858 symbol_attribute attr;
2860 if (mold == NULL)
2861 return true;
2863 if (!variable_check (mold, 0, true))
2864 return false;
2866 attr = gfc_variable_attr (mold, NULL);
2868 if (!attr.pointer && !attr.proc_pointer && !attr.allocatable)
2870 gfc_error ("'%s' argument of '%s' intrinsic at %L must be a POINTER, "
2871 "ALLOCATABLE or procedure pointer",
2872 gfc_current_intrinsic_arg[0]->name,
2873 gfc_current_intrinsic, &mold->where);
2874 return false;
2877 if (attr.allocatable
2878 && !gfc_notify_std (GFC_STD_F2003, "NULL intrinsic with "
2879 "allocatable MOLD at %L", &mold->where))
2880 return false;
2882 /* F2008, C1242. */
2883 if (gfc_is_coindexed (mold))
2885 gfc_error ("'%s' argument of '%s' intrinsic at %L shall not be "
2886 "coindexed", gfc_current_intrinsic_arg[0]->name,
2887 gfc_current_intrinsic, &mold->where);
2888 return false;
2891 return true;
2895 bool
2896 gfc_check_pack (gfc_expr *array, gfc_expr *mask, gfc_expr *vector)
2898 if (!array_check (array, 0))
2899 return false;
2901 if (!type_check (mask, 1, BT_LOGICAL))
2902 return false;
2904 if (!gfc_check_conformance (array, mask,
2905 "arguments '%s' and '%s' for intrinsic '%s'",
2906 gfc_current_intrinsic_arg[0]->name,
2907 gfc_current_intrinsic_arg[1]->name,
2908 gfc_current_intrinsic))
2909 return false;
2911 if (vector != NULL)
2913 mpz_t array_size, vector_size;
2914 bool have_array_size, have_vector_size;
2916 if (!same_type_check (array, 0, vector, 2))
2917 return false;
2919 if (!rank_check (vector, 2, 1))
2920 return false;
2922 /* VECTOR requires at least as many elements as MASK
2923 has .TRUE. values. */
2924 have_array_size = gfc_array_size(array, &array_size);
2925 have_vector_size = gfc_array_size(vector, &vector_size);
2927 if (have_vector_size
2928 && (mask->expr_type == EXPR_ARRAY
2929 || (mask->expr_type == EXPR_CONSTANT
2930 && have_array_size)))
2932 int mask_true_values = 0;
2934 if (mask->expr_type == EXPR_ARRAY)
2936 gfc_constructor *mask_ctor;
2937 mask_ctor = gfc_constructor_first (mask->value.constructor);
2938 while (mask_ctor)
2940 if (mask_ctor->expr->expr_type != EXPR_CONSTANT)
2942 mask_true_values = 0;
2943 break;
2946 if (mask_ctor->expr->value.logical)
2947 mask_true_values++;
2949 mask_ctor = gfc_constructor_next (mask_ctor);
2952 else if (mask->expr_type == EXPR_CONSTANT && mask->value.logical)
2953 mask_true_values = mpz_get_si (array_size);
2955 if (mpz_get_si (vector_size) < mask_true_values)
2957 gfc_error ("'%s' argument of '%s' intrinsic at %L must "
2958 "provide at least as many elements as there "
2959 "are .TRUE. values in '%s' (%ld/%d)",
2960 gfc_current_intrinsic_arg[2]->name,
2961 gfc_current_intrinsic, &vector->where,
2962 gfc_current_intrinsic_arg[1]->name,
2963 mpz_get_si (vector_size), mask_true_values);
2964 return false;
2968 if (have_array_size)
2969 mpz_clear (array_size);
2970 if (have_vector_size)
2971 mpz_clear (vector_size);
2974 return true;
2978 bool
2979 gfc_check_parity (gfc_expr *mask, gfc_expr *dim)
2981 if (!type_check (mask, 0, BT_LOGICAL))
2982 return false;
2984 if (!array_check (mask, 0))
2985 return false;
2987 if (!dim_rank_check (dim, mask, false))
2988 return false;
2990 return true;
2994 bool
2995 gfc_check_precision (gfc_expr *x)
2997 if (!real_or_complex_check (x, 0))
2998 return false;
3000 return true;
3004 bool
3005 gfc_check_present (gfc_expr *a)
3007 gfc_symbol *sym;
3009 if (!variable_check (a, 0, true))
3010 return false;
3012 sym = a->symtree->n.sym;
3013 if (!sym->attr.dummy)
3015 gfc_error ("'%s' argument of '%s' intrinsic at %L must be of a "
3016 "dummy variable", gfc_current_intrinsic_arg[0]->name,
3017 gfc_current_intrinsic, &a->where);
3018 return false;
3021 if (!sym->attr.optional)
3023 gfc_error ("'%s' argument of '%s' intrinsic at %L must be of "
3024 "an OPTIONAL dummy variable",
3025 gfc_current_intrinsic_arg[0]->name, gfc_current_intrinsic,
3026 &a->where);
3027 return false;
3030 /* 13.14.82 PRESENT(A)
3031 ......
3032 Argument. A shall be the name of an optional dummy argument that is
3033 accessible in the subprogram in which the PRESENT function reference
3034 appears... */
3036 if (a->ref != NULL
3037 && !(a->ref->next == NULL && a->ref->type == REF_ARRAY
3038 && (a->ref->u.ar.type == AR_FULL
3039 || (a->ref->u.ar.type == AR_ELEMENT
3040 && a->ref->u.ar.as->rank == 0))))
3042 gfc_error ("'%s' argument of '%s' intrinsic at %L must not be a "
3043 "subobject of '%s'", gfc_current_intrinsic_arg[0]->name,
3044 gfc_current_intrinsic, &a->where, sym->name);
3045 return false;
3048 return true;
3052 bool
3053 gfc_check_radix (gfc_expr *x)
3055 if (!int_or_real_check (x, 0))
3056 return false;
3058 return true;
3062 bool
3063 gfc_check_range (gfc_expr *x)
3065 if (!numeric_check (x, 0))
3066 return false;
3068 return true;
3072 bool
3073 gfc_check_rank (gfc_expr *a ATTRIBUTE_UNUSED)
3075 /* Any data object is allowed; a "data object" is a "constant (4.1.3),
3076 variable (6), or subobject of a constant (2.4.3.2.3)" (F2008, 1.3.45). */
3078 bool is_variable = true;
3080 /* Functions returning pointers are regarded as variable, cf. F2008, R602. */
3081 if (a->expr_type == EXPR_FUNCTION)
3082 is_variable = a->value.function.esym
3083 ? a->value.function.esym->result->attr.pointer
3084 : a->symtree->n.sym->result->attr.pointer;
3086 if (a->expr_type == EXPR_OP || a->expr_type == EXPR_NULL
3087 || a->expr_type == EXPR_COMPCALL|| a->expr_type == EXPR_PPC
3088 || !is_variable)
3090 gfc_error ("The argument of the RANK intrinsic at %L must be a data "
3091 "object", &a->where);
3092 return false;
3095 return true;
3099 /* real, float, sngl. */
3100 bool
3101 gfc_check_real (gfc_expr *a, gfc_expr *kind)
3103 if (!numeric_check (a, 0))
3104 return false;
3106 if (!kind_check (kind, 1, BT_REAL))
3107 return false;
3109 return true;
3113 bool
3114 gfc_check_rename (gfc_expr *path1, gfc_expr *path2)
3116 if (!type_check (path1, 0, BT_CHARACTER))
3117 return false;
3118 if (!kind_value_check (path1, 0, gfc_default_character_kind))
3119 return false;
3121 if (!type_check (path2, 1, BT_CHARACTER))
3122 return false;
3123 if (!kind_value_check (path2, 1, gfc_default_character_kind))
3124 return false;
3126 return true;
3130 bool
3131 gfc_check_rename_sub (gfc_expr *path1, gfc_expr *path2, gfc_expr *status)
3133 if (!type_check (path1, 0, BT_CHARACTER))
3134 return false;
3135 if (!kind_value_check (path1, 0, gfc_default_character_kind))
3136 return false;
3138 if (!type_check (path2, 1, BT_CHARACTER))
3139 return false;
3140 if (!kind_value_check (path2, 1, gfc_default_character_kind))
3141 return false;
3143 if (status == NULL)
3144 return true;
3146 if (!type_check (status, 2, BT_INTEGER))
3147 return false;
3149 if (!scalar_check (status, 2))
3150 return false;
3152 return true;
3156 bool
3157 gfc_check_repeat (gfc_expr *x, gfc_expr *y)
3159 if (!type_check (x, 0, BT_CHARACTER))
3160 return false;
3162 if (!scalar_check (x, 0))
3163 return false;
3165 if (!type_check (y, 0, BT_INTEGER))
3166 return false;
3168 if (!scalar_check (y, 1))
3169 return false;
3171 return true;
3175 bool
3176 gfc_check_reshape (gfc_expr *source, gfc_expr *shape,
3177 gfc_expr *pad, gfc_expr *order)
3179 mpz_t size;
3180 mpz_t nelems;
3181 int shape_size;
3183 if (!array_check (source, 0))
3184 return false;
3186 if (!rank_check (shape, 1, 1))
3187 return false;
3189 if (!type_check (shape, 1, BT_INTEGER))
3190 return false;
3192 if (!gfc_array_size (shape, &size))
3194 gfc_error ("'shape' argument of 'reshape' intrinsic at %L must be an "
3195 "array of constant size", &shape->where);
3196 return false;
3199 shape_size = mpz_get_ui (size);
3200 mpz_clear (size);
3202 if (shape_size <= 0)
3204 gfc_error ("'%s' argument of '%s' intrinsic at %L is empty",
3205 gfc_current_intrinsic_arg[1]->name, gfc_current_intrinsic,
3206 &shape->where);
3207 return false;
3209 else if (shape_size > GFC_MAX_DIMENSIONS)
3211 gfc_error ("'shape' argument of 'reshape' intrinsic at %L has more "
3212 "than %d elements", &shape->where, GFC_MAX_DIMENSIONS);
3213 return false;
3215 else if (shape->expr_type == EXPR_ARRAY)
3217 gfc_expr *e;
3218 int i, extent;
3219 for (i = 0; i < shape_size; ++i)
3221 e = gfc_constructor_lookup_expr (shape->value.constructor, i);
3222 if (e->expr_type != EXPR_CONSTANT)
3223 continue;
3225 gfc_extract_int (e, &extent);
3226 if (extent < 0)
3228 gfc_error ("'%s' argument of '%s' intrinsic at %L has "
3229 "negative element (%d)",
3230 gfc_current_intrinsic_arg[1]->name,
3231 gfc_current_intrinsic, &e->where, extent);
3232 return false;
3237 if (pad != NULL)
3239 if (!same_type_check (source, 0, pad, 2))
3240 return false;
3242 if (!array_check (pad, 2))
3243 return false;
3246 if (order != NULL)
3248 if (!array_check (order, 3))
3249 return false;
3251 if (!type_check (order, 3, BT_INTEGER))
3252 return false;
3254 if (order->expr_type == EXPR_ARRAY)
3256 int i, order_size, dim, perm[GFC_MAX_DIMENSIONS];
3257 gfc_expr *e;
3259 for (i = 0; i < GFC_MAX_DIMENSIONS; ++i)
3260 perm[i] = 0;
3262 gfc_array_size (order, &size);
3263 order_size = mpz_get_ui (size);
3264 mpz_clear (size);
3266 if (order_size != shape_size)
3268 gfc_error ("'%s' argument of '%s' intrinsic at %L "
3269 "has wrong number of elements (%d/%d)",
3270 gfc_current_intrinsic_arg[3]->name,
3271 gfc_current_intrinsic, &order->where,
3272 order_size, shape_size);
3273 return false;
3276 for (i = 1; i <= order_size; ++i)
3278 e = gfc_constructor_lookup_expr (order->value.constructor, i-1);
3279 if (e->expr_type != EXPR_CONSTANT)
3280 continue;
3282 gfc_extract_int (e, &dim);
3284 if (dim < 1 || dim > order_size)
3286 gfc_error ("'%s' argument of '%s' intrinsic at %L "
3287 "has out-of-range dimension (%d)",
3288 gfc_current_intrinsic_arg[3]->name,
3289 gfc_current_intrinsic, &e->where, dim);
3290 return false;
3293 if (perm[dim-1] != 0)
3295 gfc_error ("'%s' argument of '%s' intrinsic at %L has "
3296 "invalid permutation of dimensions (dimension "
3297 "'%d' duplicated)",
3298 gfc_current_intrinsic_arg[3]->name,
3299 gfc_current_intrinsic, &e->where, dim);
3300 return false;
3303 perm[dim-1] = 1;
3308 if (pad == NULL && shape->expr_type == EXPR_ARRAY
3309 && gfc_is_constant_expr (shape)
3310 && !(source->expr_type == EXPR_VARIABLE && source->symtree->n.sym->as
3311 && source->symtree->n.sym->as->type == AS_ASSUMED_SIZE))
3313 /* Check the match in size between source and destination. */
3314 if (gfc_array_size (source, &nelems))
3316 gfc_constructor *c;
3317 bool test;
3320 mpz_init_set_ui (size, 1);
3321 for (c = gfc_constructor_first (shape->value.constructor);
3322 c; c = gfc_constructor_next (c))
3323 mpz_mul (size, size, c->expr->value.integer);
3325 test = mpz_cmp (nelems, size) < 0 && mpz_cmp_ui (size, 0) > 0;
3326 mpz_clear (nelems);
3327 mpz_clear (size);
3329 if (test)
3331 gfc_error ("Without padding, there are not enough elements "
3332 "in the intrinsic RESHAPE source at %L to match "
3333 "the shape", &source->where);
3334 return false;
3339 return true;
3343 bool
3344 gfc_check_same_type_as (gfc_expr *a, gfc_expr *b)
3346 if (a->ts.type != BT_DERIVED && a->ts.type != BT_CLASS)
3348 gfc_error ("'%s' argument of '%s' intrinsic at %L "
3349 "cannot be of type %s",
3350 gfc_current_intrinsic_arg[0]->name,
3351 gfc_current_intrinsic,
3352 &a->where, gfc_typename (&a->ts));
3353 return false;
3356 if (!(gfc_type_is_extensible (a->ts.u.derived) || UNLIMITED_POLY (a)))
3358 gfc_error ("'%s' argument of '%s' intrinsic at %L "
3359 "must be of an extensible type",
3360 gfc_current_intrinsic_arg[0]->name, gfc_current_intrinsic,
3361 &a->where);
3362 return false;
3365 if (b->ts.type != BT_DERIVED && b->ts.type != BT_CLASS)
3367 gfc_error ("'%s' argument of '%s' intrinsic at %L "
3368 "cannot be of type %s",
3369 gfc_current_intrinsic_arg[0]->name,
3370 gfc_current_intrinsic,
3371 &b->where, gfc_typename (&b->ts));
3372 return false;
3375 if (!(gfc_type_is_extensible (b->ts.u.derived) || UNLIMITED_POLY (b)))
3377 gfc_error ("'%s' argument of '%s' intrinsic at %L "
3378 "must be of an extensible type",
3379 gfc_current_intrinsic_arg[1]->name, gfc_current_intrinsic,
3380 &b->where);
3381 return false;
3384 return true;
3388 bool
3389 gfc_check_scale (gfc_expr *x, gfc_expr *i)
3391 if (!type_check (x, 0, BT_REAL))
3392 return false;
3394 if (!type_check (i, 1, BT_INTEGER))
3395 return false;
3397 return true;
3401 bool
3402 gfc_check_scan (gfc_expr *x, gfc_expr *y, gfc_expr *z, gfc_expr *kind)
3404 if (!type_check (x, 0, BT_CHARACTER))
3405 return false;
3407 if (!type_check (y, 1, BT_CHARACTER))
3408 return false;
3410 if (z != NULL && !type_check (z, 2, BT_LOGICAL))
3411 return false;
3413 if (!kind_check (kind, 3, BT_INTEGER))
3414 return false;
3415 if (kind && !gfc_notify_std (GFC_STD_F2003, "'%s' intrinsic "
3416 "with KIND argument at %L",
3417 gfc_current_intrinsic, &kind->where))
3418 return false;
3420 if (!same_type_check (x, 0, y, 1))
3421 return false;
3423 return true;
3427 bool
3428 gfc_check_secnds (gfc_expr *r)
3430 if (!type_check (r, 0, BT_REAL))
3431 return false;
3433 if (!kind_value_check (r, 0, 4))
3434 return false;
3436 if (!scalar_check (r, 0))
3437 return false;
3439 return true;
3443 bool
3444 gfc_check_selected_char_kind (gfc_expr *name)
3446 if (!type_check (name, 0, BT_CHARACTER))
3447 return false;
3449 if (!kind_value_check (name, 0, gfc_default_character_kind))
3450 return false;
3452 if (!scalar_check (name, 0))
3453 return false;
3455 return true;
3459 bool
3460 gfc_check_selected_int_kind (gfc_expr *r)
3462 if (!type_check (r, 0, BT_INTEGER))
3463 return false;
3465 if (!scalar_check (r, 0))
3466 return false;
3468 return true;
3472 bool
3473 gfc_check_selected_real_kind (gfc_expr *p, gfc_expr *r, gfc_expr *radix)
3475 if (p == NULL && r == NULL
3476 && !gfc_notify_std (GFC_STD_F2008, "SELECTED_REAL_KIND with"
3477 " neither 'P' nor 'R' argument at %L",
3478 gfc_current_intrinsic_where))
3479 return false;
3481 if (p)
3483 if (!type_check (p, 0, BT_INTEGER))
3484 return false;
3486 if (!scalar_check (p, 0))
3487 return false;
3490 if (r)
3492 if (!type_check (r, 1, BT_INTEGER))
3493 return false;
3495 if (!scalar_check (r, 1))
3496 return false;
3499 if (radix)
3501 if (!type_check (radix, 1, BT_INTEGER))
3502 return false;
3504 if (!scalar_check (radix, 1))
3505 return false;
3507 if (!gfc_notify_std (GFC_STD_F2008, "'%s' intrinsic with "
3508 "RADIX argument at %L", gfc_current_intrinsic,
3509 &radix->where))
3510 return false;
3513 return true;
3517 bool
3518 gfc_check_set_exponent (gfc_expr *x, gfc_expr *i)
3520 if (!type_check (x, 0, BT_REAL))
3521 return false;
3523 if (!type_check (i, 1, BT_INTEGER))
3524 return false;
3526 return true;
3530 bool
3531 gfc_check_shape (gfc_expr *source, gfc_expr *kind)
3533 gfc_array_ref *ar;
3535 if (source->rank == 0 || source->expr_type != EXPR_VARIABLE)
3536 return true;
3538 ar = gfc_find_array_ref (source);
3540 if (ar->as && ar->as->type == AS_ASSUMED_SIZE && ar->type == AR_FULL)
3542 gfc_error ("'source' argument of 'shape' intrinsic at %L must not be "
3543 "an assumed size array", &source->where);
3544 return false;
3547 if (!kind_check (kind, 1, BT_INTEGER))
3548 return false;
3549 if (kind && !gfc_notify_std (GFC_STD_F2003, "'%s' intrinsic "
3550 "with KIND argument at %L",
3551 gfc_current_intrinsic, &kind->where))
3552 return false;
3554 return true;
3558 bool
3559 gfc_check_shift (gfc_expr *i, gfc_expr *shift)
3561 if (!type_check (i, 0, BT_INTEGER))
3562 return false;
3564 if (!type_check (shift, 0, BT_INTEGER))
3565 return false;
3567 if (!nonnegative_check ("SHIFT", shift))
3568 return false;
3570 if (!less_than_bitsize1 ("I", i, "SHIFT", shift, true))
3571 return false;
3573 return true;
3577 bool
3578 gfc_check_sign (gfc_expr *a, gfc_expr *b)
3580 if (!int_or_real_check (a, 0))
3581 return false;
3583 if (!same_type_check (a, 0, b, 1))
3584 return false;
3586 return true;
3590 bool
3591 gfc_check_size (gfc_expr *array, gfc_expr *dim, gfc_expr *kind)
3593 if (!array_check (array, 0))
3594 return false;
3596 if (!dim_check (dim, 1, true))
3597 return false;
3599 if (!dim_rank_check (dim, array, 0))
3600 return false;
3602 if (!kind_check (kind, 2, BT_INTEGER))
3603 return false;
3604 if (kind && !gfc_notify_std (GFC_STD_F2003, "'%s' intrinsic "
3605 "with KIND argument at %L",
3606 gfc_current_intrinsic, &kind->where))
3607 return false;
3610 return true;
3614 bool
3615 gfc_check_sizeof (gfc_expr *arg)
3617 if (arg->ts.type == BT_PROCEDURE)
3619 gfc_error ("'%s' argument of '%s' intrinsic at %L shall not be a procedure",
3620 gfc_current_intrinsic_arg[0]->name, gfc_current_intrinsic,
3621 &arg->where);
3622 return false;
3625 if (arg->ts.type == BT_ASSUMED)
3627 gfc_error ("'%s' argument of '%s' intrinsic at %L shall not be TYPE(*)",
3628 gfc_current_intrinsic_arg[0]->name, gfc_current_intrinsic,
3629 &arg->where);
3630 return false;
3633 if (arg->rank && arg->expr_type == EXPR_VARIABLE
3634 && arg->symtree->n.sym->as != NULL
3635 && arg->symtree->n.sym->as->type == AS_ASSUMED_SIZE && arg->ref
3636 && arg->ref->type == REF_ARRAY && arg->ref->u.ar.type == AR_FULL)
3638 gfc_error ("'%s' argument of '%s' intrinsic at %L shall not be an "
3639 "assumed-size array", gfc_current_intrinsic_arg[0]->name,
3640 gfc_current_intrinsic, &arg->where);
3641 return false;
3644 return true;
3648 /* Check whether an expression is interoperable. When returning false,
3649 msg is set to a string telling why the expression is not interoperable,
3650 otherwise, it is set to NULL. The msg string can be used in diagnostics.
3651 If c_loc is true, character with len > 1 are allowed (cf. Fortran
3652 2003corr5); additionally, assumed-shape/assumed-rank/deferred-shape
3653 arrays are permitted. */
3655 static bool
3656 is_c_interoperable (gfc_expr *expr, const char **msg, bool c_loc)
3658 *msg = NULL;
3660 if (expr->ts.type == BT_CLASS)
3662 *msg = "Expression is polymorphic";
3663 return false;
3666 if (expr->ts.type == BT_DERIVED && !expr->ts.u.derived->attr.is_bind_c
3667 && !expr->ts.u.derived->ts.is_iso_c)
3669 *msg = "Expression is a noninteroperable derived type";
3670 return false;
3673 if (expr->ts.type == BT_PROCEDURE)
3675 *msg = "Procedure unexpected as argument";
3676 return false;
3679 if (gfc_notification_std (GFC_STD_GNU) && expr->ts.type == BT_LOGICAL)
3681 int i;
3682 for (i = 0; gfc_logical_kinds[i].kind; i++)
3683 if (gfc_logical_kinds[i].kind == expr->ts.kind)
3684 return true;
3685 *msg = "Extension to use a non-C_Bool-kind LOGICAL";
3686 return false;
3689 if (gfc_notification_std (GFC_STD_GNU) && expr->ts.type == BT_CHARACTER
3690 && expr->ts.kind != 1)
3692 *msg = "Extension to use a non-C_CHAR-kind CHARACTER";
3693 return false;
3696 if (expr->ts.type == BT_CHARACTER) {
3697 if (expr->ts.deferred)
3699 /* TS 29113 allows deferred-length strings as dummy arguments,
3700 but it is not an interoperable type. */
3701 *msg = "Expression shall not be a deferred-length string";
3702 return false;
3705 if (expr->ts.u.cl && expr->ts.u.cl->length
3706 && !gfc_simplify_expr (expr, 0))
3707 gfc_internal_error ("is_c_interoperable(): gfc_simplify_expr failed");
3709 if (!c_loc && expr->ts.u.cl
3710 && (!expr->ts.u.cl->length
3711 || expr->ts.u.cl->length->expr_type != EXPR_CONSTANT
3712 || mpz_cmp_si (expr->ts.u.cl->length->value.integer, 1) != 0))
3714 *msg = "Type shall have a character length of 1";
3715 return false;
3719 /* Note: The following checks are about interoperatable variables, Fortran
3720 15.3.5/15.3.6. In intrinsics like C_LOC or in procedure interface, more
3721 is allowed, e.g. assumed-shape arrays with TS 29113. */
3723 if (gfc_is_coarray (expr))
3725 *msg = "Coarrays are not interoperable";
3726 return false;
3729 if (!c_loc && expr->rank > 0 && expr->expr_type != EXPR_ARRAY)
3731 gfc_array_ref *ar = gfc_find_array_ref (expr);
3732 if (ar->type != AR_FULL)
3734 *msg = "Only whole-arrays are interoperable";
3735 return false;
3737 if (ar->as->type != AS_EXPLICIT && ar->as->type != AS_ASSUMED_SIZE)
3739 *msg = "Only explicit-size and assumed-size arrays are interoperable";
3740 return false;
3744 return true;
3748 bool
3749 gfc_check_c_sizeof (gfc_expr *arg)
3751 const char *msg;
3753 if (!is_c_interoperable (arg, &msg, false))
3755 gfc_error ("'%s' argument of '%s' intrinsic at %L must be an "
3756 "interoperable data entity: %s",
3757 gfc_current_intrinsic_arg[0]->name, gfc_current_intrinsic,
3758 &arg->where, msg);
3759 return false;
3762 if (arg->ts.type == BT_ASSUMED)
3764 gfc_error ("'%s' argument of '%s' intrinsic at %L shall not be "
3765 "TYPE(*)",
3766 gfc_current_intrinsic_arg[0]->name, gfc_current_intrinsic,
3767 &arg->where);
3768 return false;
3771 if (arg->rank && arg->expr_type == EXPR_VARIABLE
3772 && arg->symtree->n.sym->as != NULL
3773 && arg->symtree->n.sym->as->type == AS_ASSUMED_SIZE && arg->ref
3774 && arg->ref->type == REF_ARRAY && arg->ref->u.ar.type == AR_FULL)
3776 gfc_error ("'%s' argument of '%s' intrinsic at %L shall not be an "
3777 "assumed-size array", gfc_current_intrinsic_arg[0]->name,
3778 gfc_current_intrinsic, &arg->where);
3779 return false;
3782 return true;
3786 bool
3787 gfc_check_c_associated (gfc_expr *c_ptr_1, gfc_expr *c_ptr_2)
3789 if (c_ptr_1->ts.type != BT_DERIVED
3790 || c_ptr_1->ts.u.derived->from_intmod != INTMOD_ISO_C_BINDING
3791 || (c_ptr_1->ts.u.derived->intmod_sym_id != ISOCBINDING_PTR
3792 && c_ptr_1->ts.u.derived->intmod_sym_id != ISOCBINDING_FUNPTR))
3794 gfc_error ("Argument C_PTR_1 at %L to C_ASSOCIATED shall have the "
3795 "type TYPE(C_PTR) or TYPE(C_FUNPTR)", &c_ptr_1->where);
3796 return false;
3799 if (!scalar_check (c_ptr_1, 0))
3800 return false;
3802 if (c_ptr_2
3803 && (c_ptr_2->ts.type != BT_DERIVED
3804 || c_ptr_2->ts.u.derived->from_intmod != INTMOD_ISO_C_BINDING
3805 || (c_ptr_1->ts.u.derived->intmod_sym_id
3806 != c_ptr_2->ts.u.derived->intmod_sym_id)))
3808 gfc_error ("Argument C_PTR_2 at %L to C_ASSOCIATED shall have the "
3809 "same type as C_PTR_1: %s instead of %s", &c_ptr_1->where,
3810 gfc_typename (&c_ptr_1->ts),
3811 gfc_typename (&c_ptr_2->ts));
3812 return false;
3815 if (c_ptr_2 && !scalar_check (c_ptr_2, 1))
3816 return false;
3818 return true;
3822 bool
3823 gfc_check_c_f_pointer (gfc_expr *cptr, gfc_expr *fptr, gfc_expr *shape)
3825 symbol_attribute attr;
3826 const char *msg;
3828 if (cptr->ts.type != BT_DERIVED
3829 || cptr->ts.u.derived->from_intmod != INTMOD_ISO_C_BINDING
3830 || cptr->ts.u.derived->intmod_sym_id != ISOCBINDING_PTR)
3832 gfc_error ("Argument CPTR at %L to C_F_POINTER shall have the "
3833 "type TYPE(C_PTR)", &cptr->where);
3834 return false;
3837 if (!scalar_check (cptr, 0))
3838 return false;
3840 attr = gfc_expr_attr (fptr);
3842 if (!attr.pointer)
3844 gfc_error ("Argument FPTR at %L to C_F_POINTER must be a pointer",
3845 &fptr->where);
3846 return false;
3849 if (fptr->ts.type == BT_CLASS)
3851 gfc_error ("FPTR argument at %L to C_F_POINTER shall not be polymorphic",
3852 &fptr->where);
3853 return false;
3856 if (gfc_is_coindexed (fptr))
3858 gfc_error ("Argument FPTR at %L to C_F_POINTER shall not be "
3859 "coindexed", &fptr->where);
3860 return false;
3863 if (fptr->rank == 0 && shape)
3865 gfc_error ("Unexpected SHAPE argument at %L to C_F_POINTER with scalar "
3866 "FPTR", &fptr->where);
3867 return false;
3869 else if (fptr->rank && !shape)
3871 gfc_error ("Expected SHAPE argument to C_F_POINTER with array "
3872 "FPTR at %L", &fptr->where);
3873 return false;
3876 if (shape && !rank_check (shape, 2, 1))
3877 return false;
3879 if (shape && !type_check (shape, 2, BT_INTEGER))
3880 return false;
3882 if (shape)
3884 mpz_t size;
3886 if (gfc_array_size (shape, &size)
3887 && mpz_cmp_ui (size, fptr->rank) != 0)
3889 mpz_clear (size);
3890 gfc_error ("SHAPE argument at %L to C_F_POINTER must have the same "
3891 "size as the RANK of FPTR", &shape->where);
3892 return false;
3894 mpz_clear (size);
3897 if (fptr->ts.type == BT_CLASS)
3899 gfc_error ("Polymorphic FPTR at %L to C_F_POINTER", &fptr->where);
3900 return false;
3903 if (!is_c_interoperable (fptr, &msg, false) && fptr->rank)
3904 return gfc_notify_std (GFC_STD_F2008_TS, "Noninteroperable array FPTR "
3905 "at %L to C_F_POINTER: %s", &fptr->where, msg);
3907 return true;
3911 bool
3912 gfc_check_c_f_procpointer (gfc_expr *cptr, gfc_expr *fptr)
3914 symbol_attribute attr;
3916 if (cptr->ts.type != BT_DERIVED
3917 || cptr->ts.u.derived->from_intmod != INTMOD_ISO_C_BINDING
3918 || cptr->ts.u.derived->intmod_sym_id != ISOCBINDING_FUNPTR)
3920 gfc_error ("Argument CPTR at %L to C_F_PROCPOINTER shall have the "
3921 "type TYPE(C_FUNPTR)", &cptr->where);
3922 return false;
3925 if (!scalar_check (cptr, 0))
3926 return false;
3928 attr = gfc_expr_attr (fptr);
3930 if (!attr.proc_pointer)
3932 gfc_error ("Argument FPTR at %L to C_F_PROCPOINTER shall be a procedure "
3933 "pointer", &fptr->where);
3934 return false;
3937 if (gfc_is_coindexed (fptr))
3939 gfc_error ("Argument FPTR at %L to C_F_PROCPOINTER shall not be "
3940 "coindexed", &fptr->where);
3941 return false;
3944 if (!attr.is_bind_c)
3945 return gfc_notify_std (GFC_STD_F2008_TS, "Noninteroperable procedure "
3946 "pointer at %L to C_F_PROCPOINTER", &fptr->where);
3948 return true;
3952 bool
3953 gfc_check_c_funloc (gfc_expr *x)
3955 symbol_attribute attr;
3957 if (gfc_is_coindexed (x))
3959 gfc_error ("Argument X at %L to C_FUNLOC shall not be "
3960 "coindexed", &x->where);
3961 return false;
3964 attr = gfc_expr_attr (x);
3966 if (attr.function && !attr.proc_pointer && x->expr_type == EXPR_VARIABLE
3967 && x->symtree->n.sym == x->symtree->n.sym->result)
3969 gfc_namespace *ns = gfc_current_ns;
3971 for (ns = gfc_current_ns; ns; ns = ns->parent)
3972 if (x->symtree->n.sym == ns->proc_name)
3974 gfc_error ("Function result '%s' at %L is invalid as X argument "
3975 "to C_FUNLOC", x->symtree->n.sym->name, &x->where);
3976 return false;
3980 if (attr.flavor != FL_PROCEDURE)
3982 gfc_error ("Argument X at %L to C_FUNLOC shall be a procedure "
3983 "or a procedure pointer", &x->where);
3984 return false;
3987 if (!attr.is_bind_c)
3988 return gfc_notify_std (GFC_STD_F2008_TS, "Noninteroperable procedure "
3989 "at %L to C_FUNLOC", &x->where);
3990 return true;
3994 bool
3995 gfc_check_c_loc (gfc_expr *x)
3997 symbol_attribute attr;
3998 const char *msg;
4000 if (gfc_is_coindexed (x))
4002 gfc_error ("Argument X at %L to C_LOC shall not be coindexed", &x->where);
4003 return false;
4006 if (x->ts.type == BT_CLASS)
4008 gfc_error ("X argument at %L to C_LOC shall not be polymorphic",
4009 &x->where);
4010 return false;
4013 attr = gfc_expr_attr (x);
4015 if (!attr.pointer
4016 && (x->expr_type != EXPR_VARIABLE || !attr.target
4017 || attr.flavor == FL_PARAMETER))
4019 gfc_error ("Argument X at %L to C_LOC shall have either "
4020 "the POINTER or the TARGET attribute", &x->where);
4021 return false;
4024 if (x->ts.type == BT_CHARACTER
4025 && gfc_var_strlen (x) == 0)
4027 gfc_error ("Argument X at %L to C_LOC shall be not be a zero-sized "
4028 "string", &x->where);
4029 return false;
4032 if (!is_c_interoperable (x, &msg, true))
4034 if (x->ts.type == BT_CLASS)
4036 gfc_error ("Argument at %L to C_LOC shall not be polymorphic",
4037 &x->where);
4038 return false;
4041 if (x->rank
4042 && !gfc_notify_std (GFC_STD_F2008_TS,
4043 "Noninteroperable array at %L as"
4044 " argument to C_LOC: %s", &x->where, msg))
4045 return false;
4047 else if (x->rank > 0 && gfc_notification_std (GFC_STD_F2008))
4049 gfc_array_ref *ar = gfc_find_array_ref (x);
4051 if (ar->as->type != AS_EXPLICIT && ar->as->type != AS_ASSUMED_SIZE
4052 && !attr.allocatable
4053 && !gfc_notify_std (GFC_STD_F2008,
4054 "Array of interoperable type at %L "
4055 "to C_LOC which is nonallocatable and neither "
4056 "assumed size nor explicit size", &x->where))
4057 return false;
4058 else if (ar->type != AR_FULL
4059 && !gfc_notify_std (GFC_STD_F2008, "Array section at %L "
4060 "to C_LOC", &x->where))
4061 return false;
4064 return true;
4068 bool
4069 gfc_check_sleep_sub (gfc_expr *seconds)
4071 if (!type_check (seconds, 0, BT_INTEGER))
4072 return false;
4074 if (!scalar_check (seconds, 0))
4075 return false;
4077 return true;
4080 bool
4081 gfc_check_sngl (gfc_expr *a)
4083 if (!type_check (a, 0, BT_REAL))
4084 return false;
4086 if ((a->ts.kind != gfc_default_double_kind)
4087 && !gfc_notify_std (GFC_STD_GNU, "non double precision "
4088 "REAL argument to %s intrinsic at %L",
4089 gfc_current_intrinsic, &a->where))
4090 return false;
4092 return true;
4095 bool
4096 gfc_check_spread (gfc_expr *source, gfc_expr *dim, gfc_expr *ncopies)
4098 if (source->rank >= GFC_MAX_DIMENSIONS)
4100 gfc_error ("'%s' argument of '%s' intrinsic at %L must be less "
4101 "than rank %d", gfc_current_intrinsic_arg[0]->name,
4102 gfc_current_intrinsic, &source->where, GFC_MAX_DIMENSIONS);
4104 return false;
4107 if (dim == NULL)
4108 return false;
4110 if (!dim_check (dim, 1, false))
4111 return false;
4113 /* dim_rank_check() does not apply here. */
4114 if (dim
4115 && dim->expr_type == EXPR_CONSTANT
4116 && (mpz_cmp_ui (dim->value.integer, 1) < 0
4117 || mpz_cmp_ui (dim->value.integer, source->rank + 1) > 0))
4119 gfc_error ("'%s' argument of '%s' intrinsic at %L is not a valid "
4120 "dimension index", gfc_current_intrinsic_arg[1]->name,
4121 gfc_current_intrinsic, &dim->where);
4122 return false;
4125 if (!type_check (ncopies, 2, BT_INTEGER))
4126 return false;
4128 if (!scalar_check (ncopies, 2))
4129 return false;
4131 return true;
4135 /* Functions for checking FGETC, FPUTC, FGET and FPUT (subroutines and
4136 functions). */
4138 bool
4139 gfc_check_fgetputc_sub (gfc_expr *unit, gfc_expr *c, gfc_expr *status)
4141 if (!type_check (unit, 0, BT_INTEGER))
4142 return false;
4144 if (!scalar_check (unit, 0))
4145 return false;
4147 if (!type_check (c, 1, BT_CHARACTER))
4148 return false;
4149 if (!kind_value_check (c, 1, gfc_default_character_kind))
4150 return false;
4152 if (status == NULL)
4153 return true;
4155 if (!type_check (status, 2, BT_INTEGER)
4156 || !kind_value_check (status, 2, gfc_default_integer_kind)
4157 || !scalar_check (status, 2))
4158 return false;
4160 return true;
4164 bool
4165 gfc_check_fgetputc (gfc_expr *unit, gfc_expr *c)
4167 return gfc_check_fgetputc_sub (unit, c, NULL);
4171 bool
4172 gfc_check_fgetput_sub (gfc_expr *c, gfc_expr *status)
4174 if (!type_check (c, 0, BT_CHARACTER))
4175 return false;
4176 if (!kind_value_check (c, 0, gfc_default_character_kind))
4177 return false;
4179 if (status == NULL)
4180 return true;
4182 if (!type_check (status, 1, BT_INTEGER)
4183 || !kind_value_check (status, 1, gfc_default_integer_kind)
4184 || !scalar_check (status, 1))
4185 return false;
4187 return true;
4191 bool
4192 gfc_check_fgetput (gfc_expr *c)
4194 return gfc_check_fgetput_sub (c, NULL);
4198 bool
4199 gfc_check_fseek_sub (gfc_expr *unit, gfc_expr *offset, gfc_expr *whence, gfc_expr *status)
4201 if (!type_check (unit, 0, BT_INTEGER))
4202 return false;
4204 if (!scalar_check (unit, 0))
4205 return false;
4207 if (!type_check (offset, 1, BT_INTEGER))
4208 return false;
4210 if (!scalar_check (offset, 1))
4211 return false;
4213 if (!type_check (whence, 2, BT_INTEGER))
4214 return false;
4216 if (!scalar_check (whence, 2))
4217 return false;
4219 if (status == NULL)
4220 return true;
4222 if (!type_check (status, 3, BT_INTEGER))
4223 return false;
4225 if (!kind_value_check (status, 3, 4))
4226 return false;
4228 if (!scalar_check (status, 3))
4229 return false;
4231 return true;
4236 bool
4237 gfc_check_fstat (gfc_expr *unit, gfc_expr *array)
4239 if (!type_check (unit, 0, BT_INTEGER))
4240 return false;
4242 if (!scalar_check (unit, 0))
4243 return false;
4245 if (!type_check (array, 1, BT_INTEGER)
4246 || !kind_value_check (unit, 0, gfc_default_integer_kind))
4247 return false;
4249 if (!array_check (array, 1))
4250 return false;
4252 return true;
4256 bool
4257 gfc_check_fstat_sub (gfc_expr *unit, gfc_expr *array, gfc_expr *status)
4259 if (!type_check (unit, 0, BT_INTEGER))
4260 return false;
4262 if (!scalar_check (unit, 0))
4263 return false;
4265 if (!type_check (array, 1, BT_INTEGER)
4266 || !kind_value_check (array, 1, gfc_default_integer_kind))
4267 return false;
4269 if (!array_check (array, 1))
4270 return false;
4272 if (status == NULL)
4273 return true;
4275 if (!type_check (status, 2, BT_INTEGER)
4276 || !kind_value_check (status, 2, gfc_default_integer_kind))
4277 return false;
4279 if (!scalar_check (status, 2))
4280 return false;
4282 return true;
4286 bool
4287 gfc_check_ftell (gfc_expr *unit)
4289 if (!type_check (unit, 0, BT_INTEGER))
4290 return false;
4292 if (!scalar_check (unit, 0))
4293 return false;
4295 return true;
4299 bool
4300 gfc_check_ftell_sub (gfc_expr *unit, gfc_expr *offset)
4302 if (!type_check (unit, 0, BT_INTEGER))
4303 return false;
4305 if (!scalar_check (unit, 0))
4306 return false;
4308 if (!type_check (offset, 1, BT_INTEGER))
4309 return false;
4311 if (!scalar_check (offset, 1))
4312 return false;
4314 return true;
4318 bool
4319 gfc_check_stat (gfc_expr *name, gfc_expr *array)
4321 if (!type_check (name, 0, BT_CHARACTER))
4322 return false;
4323 if (!kind_value_check (name, 0, gfc_default_character_kind))
4324 return false;
4326 if (!type_check (array, 1, BT_INTEGER)
4327 || !kind_value_check (array, 1, gfc_default_integer_kind))
4328 return false;
4330 if (!array_check (array, 1))
4331 return false;
4333 return true;
4337 bool
4338 gfc_check_stat_sub (gfc_expr *name, gfc_expr *array, gfc_expr *status)
4340 if (!type_check (name, 0, BT_CHARACTER))
4341 return false;
4342 if (!kind_value_check (name, 0, gfc_default_character_kind))
4343 return false;
4345 if (!type_check (array, 1, BT_INTEGER)
4346 || !kind_value_check (array, 1, gfc_default_integer_kind))
4347 return false;
4349 if (!array_check (array, 1))
4350 return false;
4352 if (status == NULL)
4353 return true;
4355 if (!type_check (status, 2, BT_INTEGER)
4356 || !kind_value_check (array, 1, gfc_default_integer_kind))
4357 return false;
4359 if (!scalar_check (status, 2))
4360 return false;
4362 return true;
4366 bool
4367 gfc_check_image_index (gfc_expr *coarray, gfc_expr *sub)
4369 mpz_t nelems;
4371 if (gfc_option.coarray == GFC_FCOARRAY_NONE)
4373 gfc_fatal_error ("Coarrays disabled at %C, use -fcoarray= to enable");
4374 return false;
4377 if (!coarray_check (coarray, 0))
4378 return false;
4380 if (sub->rank != 1)
4382 gfc_error ("%s argument to IMAGE_INDEX must be a rank one array at %L",
4383 gfc_current_intrinsic_arg[1]->name, &sub->where);
4384 return false;
4387 if (gfc_array_size (sub, &nelems))
4389 int corank = gfc_get_corank (coarray);
4391 if (mpz_cmp_ui (nelems, corank) != 0)
4393 gfc_error ("The number of array elements of the SUB argument to "
4394 "IMAGE_INDEX at %L shall be %d (corank) not %d",
4395 &sub->where, corank, (int) mpz_get_si (nelems));
4396 mpz_clear (nelems);
4397 return false;
4399 mpz_clear (nelems);
4402 return true;
4406 bool
4407 gfc_check_this_image (gfc_expr *coarray, gfc_expr *dim)
4409 if (gfc_option.coarray == GFC_FCOARRAY_NONE)
4411 gfc_fatal_error ("Coarrays disabled at %C, use -fcoarray= to enable");
4412 return false;
4415 if (dim != NULL && coarray == NULL)
4417 gfc_error ("DIM argument without ARRAY argument not allowed for THIS_IMAGE "
4418 "intrinsic at %L", &dim->where);
4419 return false;
4422 if (coarray == NULL)
4423 return true;
4425 if (!coarray_check (coarray, 0))
4426 return false;
4428 if (dim != NULL)
4430 if (!dim_check (dim, 1, false))
4431 return false;
4433 if (!dim_corank_check (dim, coarray))
4434 return false;
4437 return true;
4440 /* Calculate the sizes for transfer, used by gfc_check_transfer and also
4441 by gfc_simplify_transfer. Return false if we cannot do so. */
4443 bool
4444 gfc_calculate_transfer_sizes (gfc_expr *source, gfc_expr *mold, gfc_expr *size,
4445 size_t *source_size, size_t *result_size,
4446 size_t *result_length_p)
4448 size_t result_elt_size;
4450 if (source->expr_type == EXPR_FUNCTION)
4451 return false;
4453 if (size && size->expr_type != EXPR_CONSTANT)
4454 return false;
4456 /* Calculate the size of the source. */
4457 *source_size = gfc_target_expr_size (source);
4458 if (*source_size == 0)
4459 return false;
4461 /* Determine the size of the element. */
4462 result_elt_size = gfc_element_size (mold);
4463 if (result_elt_size == 0)
4464 return false;
4466 if (mold->expr_type == EXPR_ARRAY || mold->rank || size)
4468 int result_length;
4470 if (size)
4471 result_length = (size_t)mpz_get_ui (size->value.integer);
4472 else
4474 result_length = *source_size / result_elt_size;
4475 if (result_length * result_elt_size < *source_size)
4476 result_length += 1;
4479 *result_size = result_length * result_elt_size;
4480 if (result_length_p)
4481 *result_length_p = result_length;
4483 else
4484 *result_size = result_elt_size;
4486 return true;
4490 bool
4491 gfc_check_transfer (gfc_expr *source, gfc_expr *mold, gfc_expr *size)
4493 size_t source_size;
4494 size_t result_size;
4496 if (mold->ts.type == BT_HOLLERITH)
4498 gfc_error ("'MOLD' argument of 'TRANSFER' intrinsic at %L must not be %s",
4499 &mold->where, gfc_basic_typename (BT_HOLLERITH));
4500 return false;
4503 if (size != NULL)
4505 if (!type_check (size, 2, BT_INTEGER))
4506 return false;
4508 if (!scalar_check (size, 2))
4509 return false;
4511 if (!nonoptional_check (size, 2))
4512 return false;
4515 if (!gfc_option.warn_surprising)
4516 return true;
4518 /* If we can't calculate the sizes, we cannot check any more.
4519 Return true for that case. */
4521 if (!gfc_calculate_transfer_sizes (source, mold, size, &source_size,
4522 &result_size, NULL))
4523 return true;
4525 if (source_size < result_size)
4526 gfc_warning("Intrinsic TRANSFER at %L has partly undefined result: "
4527 "source size %ld < result size %ld", &source->where,
4528 (long) source_size, (long) result_size);
4530 return true;
4534 bool
4535 gfc_check_transpose (gfc_expr *matrix)
4537 if (!rank_check (matrix, 0, 2))
4538 return false;
4540 return true;
4544 bool
4545 gfc_check_ubound (gfc_expr *array, gfc_expr *dim, gfc_expr *kind)
4547 if (!array_check (array, 0))
4548 return false;
4550 if (!dim_check (dim, 1, false))
4551 return false;
4553 if (!dim_rank_check (dim, array, 0))
4554 return false;
4556 if (!kind_check (kind, 2, BT_INTEGER))
4557 return false;
4558 if (kind && !gfc_notify_std (GFC_STD_F2003, "'%s' intrinsic "
4559 "with KIND argument at %L",
4560 gfc_current_intrinsic, &kind->where))
4561 return false;
4563 return true;
4567 bool
4568 gfc_check_ucobound (gfc_expr *coarray, gfc_expr *dim, gfc_expr *kind)
4570 if (gfc_option.coarray == GFC_FCOARRAY_NONE)
4572 gfc_fatal_error ("Coarrays disabled at %C, use -fcoarray= to enable");
4573 return false;
4576 if (!coarray_check (coarray, 0))
4577 return false;
4579 if (dim != NULL)
4581 if (!dim_check (dim, 1, false))
4582 return false;
4584 if (!dim_corank_check (dim, coarray))
4585 return false;
4588 if (!kind_check (kind, 2, BT_INTEGER))
4589 return false;
4591 return true;
4595 bool
4596 gfc_check_unpack (gfc_expr *vector, gfc_expr *mask, gfc_expr *field)
4598 mpz_t vector_size;
4600 if (!rank_check (vector, 0, 1))
4601 return false;
4603 if (!array_check (mask, 1))
4604 return false;
4606 if (!type_check (mask, 1, BT_LOGICAL))
4607 return false;
4609 if (!same_type_check (vector, 0, field, 2))
4610 return false;
4612 if (mask->expr_type == EXPR_ARRAY
4613 && gfc_array_size (vector, &vector_size))
4615 int mask_true_count = 0;
4616 gfc_constructor *mask_ctor;
4617 mask_ctor = gfc_constructor_first (mask->value.constructor);
4618 while (mask_ctor)
4620 if (mask_ctor->expr->expr_type != EXPR_CONSTANT)
4622 mask_true_count = 0;
4623 break;
4626 if (mask_ctor->expr->value.logical)
4627 mask_true_count++;
4629 mask_ctor = gfc_constructor_next (mask_ctor);
4632 if (mpz_get_si (vector_size) < mask_true_count)
4634 gfc_error ("'%s' argument of '%s' intrinsic at %L must "
4635 "provide at least as many elements as there "
4636 "are .TRUE. values in '%s' (%ld/%d)",
4637 gfc_current_intrinsic_arg[0]->name, gfc_current_intrinsic,
4638 &vector->where, gfc_current_intrinsic_arg[1]->name,
4639 mpz_get_si (vector_size), mask_true_count);
4640 return false;
4643 mpz_clear (vector_size);
4646 if (mask->rank != field->rank && field->rank != 0)
4648 gfc_error ("'%s' argument of '%s' intrinsic at %L must have "
4649 "the same rank as '%s' or be a scalar",
4650 gfc_current_intrinsic_arg[2]->name, gfc_current_intrinsic,
4651 &field->where, gfc_current_intrinsic_arg[1]->name);
4652 return false;
4655 if (mask->rank == field->rank)
4657 int i;
4658 for (i = 0; i < field->rank; i++)
4659 if (! identical_dimen_shape (mask, i, field, i))
4661 gfc_error ("'%s' and '%s' arguments of '%s' intrinsic at %L "
4662 "must have identical shape.",
4663 gfc_current_intrinsic_arg[2]->name,
4664 gfc_current_intrinsic_arg[1]->name, gfc_current_intrinsic,
4665 &field->where);
4669 return true;
4673 bool
4674 gfc_check_verify (gfc_expr *x, gfc_expr *y, gfc_expr *z, gfc_expr *kind)
4676 if (!type_check (x, 0, BT_CHARACTER))
4677 return false;
4679 if (!same_type_check (x, 0, y, 1))
4680 return false;
4682 if (z != NULL && !type_check (z, 2, BT_LOGICAL))
4683 return false;
4685 if (!kind_check (kind, 3, BT_INTEGER))
4686 return false;
4687 if (kind && !gfc_notify_std (GFC_STD_F2003, "'%s' intrinsic "
4688 "with KIND argument at %L",
4689 gfc_current_intrinsic, &kind->where))
4690 return false;
4692 return true;
4696 bool
4697 gfc_check_trim (gfc_expr *x)
4699 if (!type_check (x, 0, BT_CHARACTER))
4700 return false;
4702 if (!scalar_check (x, 0))
4703 return false;
4705 return true;
4709 bool
4710 gfc_check_ttynam (gfc_expr *unit)
4712 if (!scalar_check (unit, 0))
4713 return false;
4715 if (!type_check (unit, 0, BT_INTEGER))
4716 return false;
4718 return true;
4722 /* Common check function for the half a dozen intrinsics that have a
4723 single real argument. */
4725 bool
4726 gfc_check_x (gfc_expr *x)
4728 if (!type_check (x, 0, BT_REAL))
4729 return false;
4731 return true;
4735 /************* Check functions for intrinsic subroutines *************/
4737 bool
4738 gfc_check_cpu_time (gfc_expr *time)
4740 if (!scalar_check (time, 0))
4741 return false;
4743 if (!type_check (time, 0, BT_REAL))
4744 return false;
4746 if (!variable_check (time, 0, false))
4747 return false;
4749 return true;
4753 bool
4754 gfc_check_date_and_time (gfc_expr *date, gfc_expr *time,
4755 gfc_expr *zone, gfc_expr *values)
4757 if (date != NULL)
4759 if (!type_check (date, 0, BT_CHARACTER))
4760 return false;
4761 if (!kind_value_check (date, 0, gfc_default_character_kind))
4762 return false;
4763 if (!scalar_check (date, 0))
4764 return false;
4765 if (!variable_check (date, 0, false))
4766 return false;
4769 if (time != NULL)
4771 if (!type_check (time, 1, BT_CHARACTER))
4772 return false;
4773 if (!kind_value_check (time, 1, gfc_default_character_kind))
4774 return false;
4775 if (!scalar_check (time, 1))
4776 return false;
4777 if (!variable_check (time, 1, false))
4778 return false;
4781 if (zone != NULL)
4783 if (!type_check (zone, 2, BT_CHARACTER))
4784 return false;
4785 if (!kind_value_check (zone, 2, gfc_default_character_kind))
4786 return false;
4787 if (!scalar_check (zone, 2))
4788 return false;
4789 if (!variable_check (zone, 2, false))
4790 return false;
4793 if (values != NULL)
4795 if (!type_check (values, 3, BT_INTEGER))
4796 return false;
4797 if (!array_check (values, 3))
4798 return false;
4799 if (!rank_check (values, 3, 1))
4800 return false;
4801 if (!variable_check (values, 3, false))
4802 return false;
4805 return true;
4809 bool
4810 gfc_check_mvbits (gfc_expr *from, gfc_expr *frompos, gfc_expr *len,
4811 gfc_expr *to, gfc_expr *topos)
4813 if (!type_check (from, 0, BT_INTEGER))
4814 return false;
4816 if (!type_check (frompos, 1, BT_INTEGER))
4817 return false;
4819 if (!type_check (len, 2, BT_INTEGER))
4820 return false;
4822 if (!same_type_check (from, 0, to, 3))
4823 return false;
4825 if (!variable_check (to, 3, false))
4826 return false;
4828 if (!type_check (topos, 4, BT_INTEGER))
4829 return false;
4831 if (!nonnegative_check ("frompos", frompos))
4832 return false;
4834 if (!nonnegative_check ("topos", topos))
4835 return false;
4837 if (!nonnegative_check ("len", len))
4838 return false;
4840 if (!less_than_bitsize2 ("from", from, "frompos", frompos, "len", len))
4841 return false;
4843 if (!less_than_bitsize2 ("to", to, "topos", topos, "len", len))
4844 return false;
4846 return true;
4850 bool
4851 gfc_check_random_number (gfc_expr *harvest)
4853 if (!type_check (harvest, 0, BT_REAL))
4854 return false;
4856 if (!variable_check (harvest, 0, false))
4857 return false;
4859 return true;
4863 bool
4864 gfc_check_random_seed (gfc_expr *size, gfc_expr *put, gfc_expr *get)
4866 unsigned int nargs = 0, kiss_size;
4867 locus *where = NULL;
4868 mpz_t put_size, get_size;
4869 bool have_gfc_real_16; /* Try and mimic HAVE_GFC_REAL_16 in libgfortran. */
4871 have_gfc_real_16 = gfc_validate_kind (BT_REAL, 16, true) != -1;
4873 /* Keep the number of bytes in sync with kiss_size in
4874 libgfortran/intrinsics/random.c. */
4875 kiss_size = (have_gfc_real_16 ? 48 : 32) / gfc_default_integer_kind;
4877 if (size != NULL)
4879 if (size->expr_type != EXPR_VARIABLE
4880 || !size->symtree->n.sym->attr.optional)
4881 nargs++;
4883 if (!scalar_check (size, 0))
4884 return false;
4886 if (!type_check (size, 0, BT_INTEGER))
4887 return false;
4889 if (!variable_check (size, 0, false))
4890 return false;
4892 if (!kind_value_check (size, 0, gfc_default_integer_kind))
4893 return false;
4896 if (put != NULL)
4898 if (put->expr_type != EXPR_VARIABLE
4899 || !put->symtree->n.sym->attr.optional)
4901 nargs++;
4902 where = &put->where;
4905 if (!array_check (put, 1))
4906 return false;
4908 if (!rank_check (put, 1, 1))
4909 return false;
4911 if (!type_check (put, 1, BT_INTEGER))
4912 return false;
4914 if (!kind_value_check (put, 1, gfc_default_integer_kind))
4915 return false;
4917 if (gfc_array_size (put, &put_size)
4918 && mpz_get_ui (put_size) < kiss_size)
4919 gfc_error ("Size of '%s' argument of '%s' intrinsic at %L "
4920 "too small (%i/%i)",
4921 gfc_current_intrinsic_arg[1]->name, gfc_current_intrinsic,
4922 where, (int) mpz_get_ui (put_size), kiss_size);
4925 if (get != NULL)
4927 if (get->expr_type != EXPR_VARIABLE
4928 || !get->symtree->n.sym->attr.optional)
4930 nargs++;
4931 where = &get->where;
4934 if (!array_check (get, 2))
4935 return false;
4937 if (!rank_check (get, 2, 1))
4938 return false;
4940 if (!type_check (get, 2, BT_INTEGER))
4941 return false;
4943 if (!variable_check (get, 2, false))
4944 return false;
4946 if (!kind_value_check (get, 2, gfc_default_integer_kind))
4947 return false;
4949 if (gfc_array_size (get, &get_size)
4950 && mpz_get_ui (get_size) < kiss_size)
4951 gfc_error ("Size of '%s' argument of '%s' intrinsic at %L "
4952 "too small (%i/%i)",
4953 gfc_current_intrinsic_arg[2]->name, gfc_current_intrinsic,
4954 where, (int) mpz_get_ui (get_size), kiss_size);
4957 /* RANDOM_SEED may not have more than one non-optional argument. */
4958 if (nargs > 1)
4959 gfc_error ("Too many arguments to %s at %L", gfc_current_intrinsic, where);
4961 return true;
4965 bool
4966 gfc_check_second_sub (gfc_expr *time)
4968 if (!scalar_check (time, 0))
4969 return false;
4971 if (!type_check (time, 0, BT_REAL))
4972 return false;
4974 if (!kind_value_check (time, 0, 4))
4975 return false;
4977 return true;
4981 /* The arguments of SYSTEM_CLOCK are scalar, integer variables. Note,
4982 count, count_rate, and count_max are all optional arguments */
4984 bool
4985 gfc_check_system_clock (gfc_expr *count, gfc_expr *count_rate,
4986 gfc_expr *count_max)
4988 if (count != NULL)
4990 if (!scalar_check (count, 0))
4991 return false;
4993 if (!type_check (count, 0, BT_INTEGER))
4994 return false;
4996 if (!variable_check (count, 0, false))
4997 return false;
5000 if (count_rate != NULL)
5002 if (!scalar_check (count_rate, 1))
5003 return false;
5005 if (!type_check (count_rate, 1, BT_INTEGER))
5006 return false;
5008 if (!variable_check (count_rate, 1, false))
5009 return false;
5011 if (count != NULL
5012 && !same_type_check (count, 0, count_rate, 1))
5013 return false;
5017 if (count_max != NULL)
5019 if (!scalar_check (count_max, 2))
5020 return false;
5022 if (!type_check (count_max, 2, BT_INTEGER))
5023 return false;
5025 if (!variable_check (count_max, 2, false))
5026 return false;
5028 if (count != NULL
5029 && !same_type_check (count, 0, count_max, 2))
5030 return false;
5032 if (count_rate != NULL
5033 && !same_type_check (count_rate, 1, count_max, 2))
5034 return false;
5037 return true;
5041 bool
5042 gfc_check_irand (gfc_expr *x)
5044 if (x == NULL)
5045 return true;
5047 if (!scalar_check (x, 0))
5048 return false;
5050 if (!type_check (x, 0, BT_INTEGER))
5051 return false;
5053 if (!kind_value_check (x, 0, 4))
5054 return false;
5056 return true;
5060 bool
5061 gfc_check_alarm_sub (gfc_expr *seconds, gfc_expr *handler, gfc_expr *status)
5063 if (!scalar_check (seconds, 0))
5064 return false;
5065 if (!type_check (seconds, 0, BT_INTEGER))
5066 return false;
5068 if (!int_or_proc_check (handler, 1))
5069 return false;
5070 if (handler->ts.type == BT_INTEGER && !scalar_check (handler, 1))
5071 return false;
5073 if (status == NULL)
5074 return true;
5076 if (!scalar_check (status, 2))
5077 return false;
5078 if (!type_check (status, 2, BT_INTEGER))
5079 return false;
5080 if (!kind_value_check (status, 2, gfc_default_integer_kind))
5081 return false;
5083 return true;
5087 bool
5088 gfc_check_rand (gfc_expr *x)
5090 if (x == NULL)
5091 return true;
5093 if (!scalar_check (x, 0))
5094 return false;
5096 if (!type_check (x, 0, BT_INTEGER))
5097 return false;
5099 if (!kind_value_check (x, 0, 4))
5100 return false;
5102 return true;
5106 bool
5107 gfc_check_srand (gfc_expr *x)
5109 if (!scalar_check (x, 0))
5110 return false;
5112 if (!type_check (x, 0, BT_INTEGER))
5113 return false;
5115 if (!kind_value_check (x, 0, 4))
5116 return false;
5118 return true;
5122 bool
5123 gfc_check_ctime_sub (gfc_expr *time, gfc_expr *result)
5125 if (!scalar_check (time, 0))
5126 return false;
5127 if (!type_check (time, 0, BT_INTEGER))
5128 return false;
5130 if (!type_check (result, 1, BT_CHARACTER))
5131 return false;
5132 if (!kind_value_check (result, 1, gfc_default_character_kind))
5133 return false;
5135 return true;
5139 bool
5140 gfc_check_dtime_etime (gfc_expr *x)
5142 if (!array_check (x, 0))
5143 return false;
5145 if (!rank_check (x, 0, 1))
5146 return false;
5148 if (!variable_check (x, 0, false))
5149 return false;
5151 if (!type_check (x, 0, BT_REAL))
5152 return false;
5154 if (!kind_value_check (x, 0, 4))
5155 return false;
5157 return true;
5161 bool
5162 gfc_check_dtime_etime_sub (gfc_expr *values, gfc_expr *time)
5164 if (!array_check (values, 0))
5165 return false;
5167 if (!rank_check (values, 0, 1))
5168 return false;
5170 if (!variable_check (values, 0, false))
5171 return false;
5173 if (!type_check (values, 0, BT_REAL))
5174 return false;
5176 if (!kind_value_check (values, 0, 4))
5177 return false;
5179 if (!scalar_check (time, 1))
5180 return false;
5182 if (!type_check (time, 1, BT_REAL))
5183 return false;
5185 if (!kind_value_check (time, 1, 4))
5186 return false;
5188 return true;
5192 bool
5193 gfc_check_fdate_sub (gfc_expr *date)
5195 if (!type_check (date, 0, BT_CHARACTER))
5196 return false;
5197 if (!kind_value_check (date, 0, gfc_default_character_kind))
5198 return false;
5200 return true;
5204 bool
5205 gfc_check_gerror (gfc_expr *msg)
5207 if (!type_check (msg, 0, BT_CHARACTER))
5208 return false;
5209 if (!kind_value_check (msg, 0, gfc_default_character_kind))
5210 return false;
5212 return true;
5216 bool
5217 gfc_check_getcwd_sub (gfc_expr *cwd, gfc_expr *status)
5219 if (!type_check (cwd, 0, BT_CHARACTER))
5220 return false;
5221 if (!kind_value_check (cwd, 0, gfc_default_character_kind))
5222 return false;
5224 if (status == NULL)
5225 return true;
5227 if (!scalar_check (status, 1))
5228 return false;
5230 if (!type_check (status, 1, BT_INTEGER))
5231 return false;
5233 return true;
5237 bool
5238 gfc_check_getarg (gfc_expr *pos, gfc_expr *value)
5240 if (!type_check (pos, 0, BT_INTEGER))
5241 return false;
5243 if (pos->ts.kind > gfc_default_integer_kind)
5245 gfc_error ("'%s' argument of '%s' intrinsic at %L must be of a kind "
5246 "not wider than the default kind (%d)",
5247 gfc_current_intrinsic_arg[0]->name, gfc_current_intrinsic,
5248 &pos->where, gfc_default_integer_kind);
5249 return false;
5252 if (!type_check (value, 1, BT_CHARACTER))
5253 return false;
5254 if (!kind_value_check (value, 1, gfc_default_character_kind))
5255 return false;
5257 return true;
5261 bool
5262 gfc_check_getlog (gfc_expr *msg)
5264 if (!type_check (msg, 0, BT_CHARACTER))
5265 return false;
5266 if (!kind_value_check (msg, 0, gfc_default_character_kind))
5267 return false;
5269 return true;
5273 bool
5274 gfc_check_exit (gfc_expr *status)
5276 if (status == NULL)
5277 return true;
5279 if (!type_check (status, 0, BT_INTEGER))
5280 return false;
5282 if (!scalar_check (status, 0))
5283 return false;
5285 return true;
5289 bool
5290 gfc_check_flush (gfc_expr *unit)
5292 if (unit == NULL)
5293 return true;
5295 if (!type_check (unit, 0, BT_INTEGER))
5296 return false;
5298 if (!scalar_check (unit, 0))
5299 return false;
5301 return true;
5305 bool
5306 gfc_check_free (gfc_expr *i)
5308 if (!type_check (i, 0, BT_INTEGER))
5309 return false;
5311 if (!scalar_check (i, 0))
5312 return false;
5314 return true;
5318 bool
5319 gfc_check_hostnm (gfc_expr *name)
5321 if (!type_check (name, 0, BT_CHARACTER))
5322 return false;
5323 if (!kind_value_check (name, 0, gfc_default_character_kind))
5324 return false;
5326 return true;
5330 bool
5331 gfc_check_hostnm_sub (gfc_expr *name, gfc_expr *status)
5333 if (!type_check (name, 0, BT_CHARACTER))
5334 return false;
5335 if (!kind_value_check (name, 0, gfc_default_character_kind))
5336 return false;
5338 if (status == NULL)
5339 return true;
5341 if (!scalar_check (status, 1))
5342 return false;
5344 if (!type_check (status, 1, BT_INTEGER))
5345 return false;
5347 return true;
5351 bool
5352 gfc_check_itime_idate (gfc_expr *values)
5354 if (!array_check (values, 0))
5355 return false;
5357 if (!rank_check (values, 0, 1))
5358 return false;
5360 if (!variable_check (values, 0, false))
5361 return false;
5363 if (!type_check (values, 0, BT_INTEGER))
5364 return false;
5366 if (!kind_value_check (values, 0, gfc_default_integer_kind))
5367 return false;
5369 return true;
5373 bool
5374 gfc_check_ltime_gmtime (gfc_expr *time, gfc_expr *values)
5376 if (!type_check (time, 0, BT_INTEGER))
5377 return false;
5379 if (!kind_value_check (time, 0, gfc_default_integer_kind))
5380 return false;
5382 if (!scalar_check (time, 0))
5383 return false;
5385 if (!array_check (values, 1))
5386 return false;
5388 if (!rank_check (values, 1, 1))
5389 return false;
5391 if (!variable_check (values, 1, false))
5392 return false;
5394 if (!type_check (values, 1, BT_INTEGER))
5395 return false;
5397 if (!kind_value_check (values, 1, gfc_default_integer_kind))
5398 return false;
5400 return true;
5404 bool
5405 gfc_check_ttynam_sub (gfc_expr *unit, gfc_expr *name)
5407 if (!scalar_check (unit, 0))
5408 return false;
5410 if (!type_check (unit, 0, BT_INTEGER))
5411 return false;
5413 if (!type_check (name, 1, BT_CHARACTER))
5414 return false;
5415 if (!kind_value_check (name, 1, gfc_default_character_kind))
5416 return false;
5418 return true;
5422 bool
5423 gfc_check_isatty (gfc_expr *unit)
5425 if (unit == NULL)
5426 return false;
5428 if (!type_check (unit, 0, BT_INTEGER))
5429 return false;
5431 if (!scalar_check (unit, 0))
5432 return false;
5434 return true;
5438 bool
5439 gfc_check_isnan (gfc_expr *x)
5441 if (!type_check (x, 0, BT_REAL))
5442 return false;
5444 return true;
5448 bool
5449 gfc_check_perror (gfc_expr *string)
5451 if (!type_check (string, 0, BT_CHARACTER))
5452 return false;
5453 if (!kind_value_check (string, 0, gfc_default_character_kind))
5454 return false;
5456 return true;
5460 bool
5461 gfc_check_umask (gfc_expr *mask)
5463 if (!type_check (mask, 0, BT_INTEGER))
5464 return false;
5466 if (!scalar_check (mask, 0))
5467 return false;
5469 return true;
5473 bool
5474 gfc_check_umask_sub (gfc_expr *mask, gfc_expr *old)
5476 if (!type_check (mask, 0, BT_INTEGER))
5477 return false;
5479 if (!scalar_check (mask, 0))
5480 return false;
5482 if (old == NULL)
5483 return true;
5485 if (!scalar_check (old, 1))
5486 return false;
5488 if (!type_check (old, 1, BT_INTEGER))
5489 return false;
5491 return true;
5495 bool
5496 gfc_check_unlink (gfc_expr *name)
5498 if (!type_check (name, 0, BT_CHARACTER))
5499 return false;
5500 if (!kind_value_check (name, 0, gfc_default_character_kind))
5501 return false;
5503 return true;
5507 bool
5508 gfc_check_unlink_sub (gfc_expr *name, gfc_expr *status)
5510 if (!type_check (name, 0, BT_CHARACTER))
5511 return false;
5512 if (!kind_value_check (name, 0, gfc_default_character_kind))
5513 return false;
5515 if (status == NULL)
5516 return true;
5518 if (!scalar_check (status, 1))
5519 return false;
5521 if (!type_check (status, 1, BT_INTEGER))
5522 return false;
5524 return true;
5528 bool
5529 gfc_check_signal (gfc_expr *number, gfc_expr *handler)
5531 if (!scalar_check (number, 0))
5532 return false;
5533 if (!type_check (number, 0, BT_INTEGER))
5534 return false;
5536 if (!int_or_proc_check (handler, 1))
5537 return false;
5538 if (handler->ts.type == BT_INTEGER && !scalar_check (handler, 1))
5539 return false;
5541 return true;
5545 bool
5546 gfc_check_signal_sub (gfc_expr *number, gfc_expr *handler, gfc_expr *status)
5548 if (!scalar_check (number, 0))
5549 return false;
5550 if (!type_check (number, 0, BT_INTEGER))
5551 return false;
5553 if (!int_or_proc_check (handler, 1))
5554 return false;
5555 if (handler->ts.type == BT_INTEGER && !scalar_check (handler, 1))
5556 return false;
5558 if (status == NULL)
5559 return true;
5561 if (!type_check (status, 2, BT_INTEGER))
5562 return false;
5563 if (!scalar_check (status, 2))
5564 return false;
5566 return true;
5570 bool
5571 gfc_check_system_sub (gfc_expr *cmd, gfc_expr *status)
5573 if (!type_check (cmd, 0, BT_CHARACTER))
5574 return false;
5575 if (!kind_value_check (cmd, 0, gfc_default_character_kind))
5576 return false;
5578 if (!scalar_check (status, 1))
5579 return false;
5581 if (!type_check (status, 1, BT_INTEGER))
5582 return false;
5584 if (!kind_value_check (status, 1, gfc_default_integer_kind))
5585 return false;
5587 return true;
5591 /* This is used for the GNU intrinsics AND, OR and XOR. */
5592 bool
5593 gfc_check_and (gfc_expr *i, gfc_expr *j)
5595 if (i->ts.type != BT_INTEGER && i->ts.type != BT_LOGICAL)
5597 gfc_error ("'%s' argument of '%s' intrinsic at %L must be INTEGER "
5598 "or LOGICAL", gfc_current_intrinsic_arg[0]->name,
5599 gfc_current_intrinsic, &i->where);
5600 return false;
5603 if (j->ts.type != BT_INTEGER && j->ts.type != BT_LOGICAL)
5605 gfc_error ("'%s' argument of '%s' intrinsic at %L must be INTEGER "
5606 "or LOGICAL", gfc_current_intrinsic_arg[1]->name,
5607 gfc_current_intrinsic, &j->where);
5608 return false;
5611 if (i->ts.type != j->ts.type)
5613 gfc_error ("'%s' and '%s' arguments of '%s' intrinsic at %L must "
5614 "have the same type", gfc_current_intrinsic_arg[0]->name,
5615 gfc_current_intrinsic_arg[1]->name, gfc_current_intrinsic,
5616 &j->where);
5617 return false;
5620 if (!scalar_check (i, 0))
5621 return false;
5623 if (!scalar_check (j, 1))
5624 return false;
5626 return true;
5630 bool
5631 gfc_check_storage_size (gfc_expr *a, gfc_expr *kind)
5633 if (a->ts.type == BT_ASSUMED)
5635 gfc_error ("'%s' argument of '%s' intrinsic at %L shall not be TYPE(*)",
5636 gfc_current_intrinsic_arg[0]->name, gfc_current_intrinsic,
5637 &a->where);
5638 return false;
5641 if (a->ts.type == BT_PROCEDURE)
5643 gfc_error ("'%s' argument of '%s' intrinsic at %L shall not be a "
5644 "procedure", gfc_current_intrinsic_arg[0]->name,
5645 gfc_current_intrinsic, &a->where);
5646 return false;
5649 if (kind == NULL)
5650 return true;
5652 if (!type_check (kind, 1, BT_INTEGER))
5653 return false;
5655 if (!scalar_check (kind, 1))
5656 return false;
5658 if (kind->expr_type != EXPR_CONSTANT)
5660 gfc_error ("'%s' argument of '%s' intrinsic at %L must be a constant",
5661 gfc_current_intrinsic_arg[1]->name, gfc_current_intrinsic,
5662 &kind->where);
5663 return false;
5666 return true;