Merge from trunk:
[official-gcc.git] / main / gcc / fortran / check.c
blob95d28693f2737b020f9e2d10d11e09cf43b99b6a
1 /* Check functions
2 Copyright (C) 2002-2014 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->expr_type == EXPR_FUNCTION && array->value.function.isym
612 && array->value.function.isym->id == GFC_ISYM_SPREAD)
613 rank = array->rank + 1;
614 else
615 rank = array->rank;
617 /* Assumed-rank array. */
618 if (rank == -1)
619 rank = GFC_MAX_DIMENSIONS;
621 if (array->expr_type == EXPR_VARIABLE)
623 ar = gfc_find_array_ref (array);
624 if (ar->as->type == AS_ASSUMED_SIZE
625 && !allow_assumed
626 && ar->type != AR_ELEMENT
627 && ar->type != AR_SECTION)
628 rank--;
631 if (mpz_cmp_ui (dim->value.integer, 1) < 0
632 || mpz_cmp_ui (dim->value.integer, rank) > 0)
634 gfc_error ("'dim' argument of '%s' intrinsic at %L is not a valid "
635 "dimension index", gfc_current_intrinsic, &dim->where);
637 return false;
640 return true;
644 /* Compare the size of a along dimension ai with the size of b along
645 dimension bi, returning 0 if they are known not to be identical,
646 and 1 if they are identical, or if this cannot be determined. */
648 static int
649 identical_dimen_shape (gfc_expr *a, int ai, gfc_expr *b, int bi)
651 mpz_t a_size, b_size;
652 int ret;
654 gcc_assert (a->rank > ai);
655 gcc_assert (b->rank > bi);
657 ret = 1;
659 if (gfc_array_dimen_size (a, ai, &a_size))
661 if (gfc_array_dimen_size (b, bi, &b_size))
663 if (mpz_cmp (a_size, b_size) != 0)
664 ret = 0;
666 mpz_clear (b_size);
668 mpz_clear (a_size);
670 return ret;
673 /* Calculate the length of a character variable, including substrings.
674 Strip away parentheses if necessary. Return -1 if no length could
675 be determined. */
677 static long
678 gfc_var_strlen (const gfc_expr *a)
680 gfc_ref *ra;
682 while (a->expr_type == EXPR_OP && a->value.op.op == INTRINSIC_PARENTHESES)
683 a = a->value.op.op1;
685 for (ra = a->ref; ra != NULL && ra->type != REF_SUBSTRING; ra = ra->next)
688 if (ra)
690 long start_a, end_a;
692 if (!ra->u.ss.end)
693 return -1;
695 if ((!ra->u.ss.start || ra->u.ss.start->expr_type == EXPR_CONSTANT)
696 && ra->u.ss.end->expr_type == EXPR_CONSTANT)
698 start_a = ra->u.ss.start ? mpz_get_si (ra->u.ss.start->value.integer)
699 : 1;
700 end_a = mpz_get_si (ra->u.ss.end->value.integer);
701 return (end_a < start_a) ? 0 : end_a - start_a + 1;
703 else if (ra->u.ss.start
704 && gfc_dep_compare_expr (ra->u.ss.start, ra->u.ss.end) == 0)
705 return 1;
706 else
707 return -1;
710 if (a->ts.u.cl && a->ts.u.cl->length
711 && a->ts.u.cl->length->expr_type == EXPR_CONSTANT)
712 return mpz_get_si (a->ts.u.cl->length->value.integer);
713 else if (a->expr_type == EXPR_CONSTANT
714 && (a->ts.u.cl == NULL || a->ts.u.cl->length == NULL))
715 return a->value.character.length;
716 else
717 return -1;
721 /* Check whether two character expressions have the same length;
722 returns true if they have or if the length cannot be determined,
723 otherwise return false and raise a gfc_error. */
725 bool
726 gfc_check_same_strlen (const gfc_expr *a, const gfc_expr *b, const char *name)
728 long len_a, len_b;
730 len_a = gfc_var_strlen(a);
731 len_b = gfc_var_strlen(b);
733 if (len_a == -1 || len_b == -1 || len_a == len_b)
734 return true;
735 else
737 gfc_error ("Unequal character lengths (%ld/%ld) in %s at %L",
738 len_a, len_b, name, &a->where);
739 return false;
744 /***** Check functions *****/
746 /* Check subroutine suitable for intrinsics taking a real argument and
747 a kind argument for the result. */
749 static bool
750 check_a_kind (gfc_expr *a, gfc_expr *kind, bt type)
752 if (!type_check (a, 0, BT_REAL))
753 return false;
754 if (!kind_check (kind, 1, type))
755 return false;
757 return true;
761 /* Check subroutine suitable for ceiling, floor and nint. */
763 bool
764 gfc_check_a_ikind (gfc_expr *a, gfc_expr *kind)
766 return check_a_kind (a, kind, BT_INTEGER);
770 /* Check subroutine suitable for aint, anint. */
772 bool
773 gfc_check_a_xkind (gfc_expr *a, gfc_expr *kind)
775 return check_a_kind (a, kind, BT_REAL);
779 bool
780 gfc_check_abs (gfc_expr *a)
782 if (!numeric_check (a, 0))
783 return false;
785 return true;
789 bool
790 gfc_check_achar (gfc_expr *a, gfc_expr *kind)
792 if (!type_check (a, 0, BT_INTEGER))
793 return false;
794 if (!kind_check (kind, 1, BT_CHARACTER))
795 return false;
797 return true;
801 bool
802 gfc_check_access_func (gfc_expr *name, gfc_expr *mode)
804 if (!type_check (name, 0, BT_CHARACTER)
805 || !scalar_check (name, 0))
806 return false;
807 if (!kind_value_check (name, 0, gfc_default_character_kind))
808 return false;
810 if (!type_check (mode, 1, BT_CHARACTER)
811 || !scalar_check (mode, 1))
812 return false;
813 if (!kind_value_check (mode, 1, gfc_default_character_kind))
814 return false;
816 return true;
820 bool
821 gfc_check_all_any (gfc_expr *mask, gfc_expr *dim)
823 if (!logical_array_check (mask, 0))
824 return false;
826 if (!dim_check (dim, 1, false))
827 return false;
829 if (!dim_rank_check (dim, mask, 0))
830 return false;
832 return true;
836 bool
837 gfc_check_allocated (gfc_expr *array)
839 if (!variable_check (array, 0, false))
840 return false;
841 if (!allocatable_check (array, 0))
842 return false;
844 return true;
848 /* Common check function where the first argument must be real or
849 integer and the second argument must be the same as the first. */
851 bool
852 gfc_check_a_p (gfc_expr *a, gfc_expr *p)
854 if (!int_or_real_check (a, 0))
855 return false;
857 if (a->ts.type != p->ts.type)
859 gfc_error ("'%s' and '%s' arguments of '%s' intrinsic at %L must "
860 "have the same type", gfc_current_intrinsic_arg[0]->name,
861 gfc_current_intrinsic_arg[1]->name, gfc_current_intrinsic,
862 &p->where);
863 return false;
866 if (a->ts.kind != p->ts.kind)
868 if (!gfc_notify_std (GFC_STD_GNU, "Different type kinds at %L",
869 &p->where))
870 return false;
873 return true;
877 bool
878 gfc_check_x_yd (gfc_expr *x, gfc_expr *y)
880 if (!double_check (x, 0) || !double_check (y, 1))
881 return false;
883 return true;
887 bool
888 gfc_check_associated (gfc_expr *pointer, gfc_expr *target)
890 symbol_attribute attr1, attr2;
891 int i;
892 bool t;
893 locus *where;
895 where = &pointer->where;
897 if (pointer->expr_type == EXPR_NULL)
898 goto null_arg;
900 attr1 = gfc_expr_attr (pointer);
902 if (!attr1.pointer && !attr1.proc_pointer)
904 gfc_error ("'%s' argument of '%s' intrinsic at %L must be a POINTER",
905 gfc_current_intrinsic_arg[0]->name, gfc_current_intrinsic,
906 &pointer->where);
907 return false;
910 /* F2008, C1242. */
911 if (attr1.pointer && gfc_is_coindexed (pointer))
913 gfc_error ("'%s' argument of '%s' intrinsic at %L shall not be "
914 "coindexed", gfc_current_intrinsic_arg[0]->name,
915 gfc_current_intrinsic, &pointer->where);
916 return false;
919 /* Target argument is optional. */
920 if (target == NULL)
921 return true;
923 where = &target->where;
924 if (target->expr_type == EXPR_NULL)
925 goto null_arg;
927 if (target->expr_type == EXPR_VARIABLE || target->expr_type == EXPR_FUNCTION)
928 attr2 = gfc_expr_attr (target);
929 else
931 gfc_error ("'%s' argument of '%s' intrinsic at %L must be a pointer "
932 "or target VARIABLE or FUNCTION",
933 gfc_current_intrinsic_arg[1]->name, gfc_current_intrinsic,
934 &target->where);
935 return false;
938 if (attr1.pointer && !attr2.pointer && !attr2.target)
940 gfc_error ("'%s' argument of '%s' intrinsic at %L must be a POINTER "
941 "or a TARGET", gfc_current_intrinsic_arg[1]->name,
942 gfc_current_intrinsic, &target->where);
943 return false;
946 /* F2008, C1242. */
947 if (attr1.pointer && gfc_is_coindexed (target))
949 gfc_error ("'%s' argument of '%s' intrinsic at %L shall not be "
950 "coindexed", gfc_current_intrinsic_arg[1]->name,
951 gfc_current_intrinsic, &target->where);
952 return false;
955 t = true;
956 if (!same_type_check (pointer, 0, target, 1))
957 t = false;
958 if (!rank_check (target, 0, pointer->rank))
959 t = false;
960 if (target->rank > 0)
962 for (i = 0; i < target->rank; i++)
963 if (target->ref->u.ar.dimen_type[i] == DIMEN_VECTOR)
965 gfc_error ("Array section with a vector subscript at %L shall not "
966 "be the target of a pointer",
967 &target->where);
968 t = false;
969 break;
972 return t;
974 null_arg:
976 gfc_error ("NULL pointer at %L is not permitted as actual argument "
977 "of '%s' intrinsic function", where, gfc_current_intrinsic);
978 return false;
983 bool
984 gfc_check_atan_2 (gfc_expr *y, gfc_expr *x)
986 /* gfc_notify_std would be a waste of time as the return value
987 is seemingly used only for the generic resolution. The error
988 will be: Too many arguments. */
989 if ((gfc_option.allow_std & GFC_STD_F2008) == 0)
990 return false;
992 return gfc_check_atan2 (y, x);
996 bool
997 gfc_check_atan2 (gfc_expr *y, gfc_expr *x)
999 if (!type_check (y, 0, BT_REAL))
1000 return false;
1001 if (!same_type_check (y, 0, x, 1))
1002 return false;
1004 return true;
1008 static bool
1009 gfc_check_atomic (gfc_expr *atom, int atom_no, gfc_expr *value, int val_no,
1010 gfc_expr *stat, int stat_no)
1012 if (!scalar_check (atom, atom_no) || !scalar_check (value, val_no))
1013 return false;
1015 if (!(atom->ts.type == BT_INTEGER && atom->ts.kind == gfc_atomic_int_kind)
1016 && !(atom->ts.type == BT_LOGICAL
1017 && atom->ts.kind == gfc_atomic_logical_kind))
1019 gfc_error ("ATOM argument at %L to intrinsic function %s shall be an "
1020 "integer of ATOMIC_INT_KIND or a logical of "
1021 "ATOMIC_LOGICAL_KIND", &atom->where, gfc_current_intrinsic);
1022 return false;
1025 if (!gfc_expr_attr (atom).codimension)
1027 gfc_error ("ATOM argument at %L of the %s intrinsic function shall be a "
1028 "coarray or coindexed", &atom->where, gfc_current_intrinsic);
1029 return false;
1032 if (atom->ts.type != value->ts.type)
1034 gfc_error ("'%s' argument of '%s' intrinsic at %L shall have the same "
1035 "type as '%s' at %L", gfc_current_intrinsic_arg[val_no]->name,
1036 gfc_current_intrinsic, &value->where,
1037 gfc_current_intrinsic_arg[atom_no]->name, &atom->where);
1038 return false;
1041 if (stat != NULL)
1043 if (!type_check (stat, stat_no, BT_INTEGER))
1044 return false;
1045 if (!scalar_check (stat, stat_no))
1046 return false;
1047 if (!variable_check (stat, stat_no, false))
1048 return false;
1049 if (!kind_value_check (stat, stat_no, gfc_default_integer_kind))
1050 return false;
1052 if (!gfc_notify_std (GFC_STD_F2008_TS, "STAT= argument to %s at %L",
1053 gfc_current_intrinsic, &stat->where))
1054 return false;
1057 return true;
1061 bool
1062 gfc_check_atomic_def (gfc_expr *atom, gfc_expr *value, gfc_expr *stat)
1064 if (atom->expr_type == EXPR_FUNCTION
1065 && atom->value.function.isym
1066 && atom->value.function.isym->id == GFC_ISYM_CAF_GET)
1067 atom = atom->value.function.actual->expr;
1069 if (!gfc_check_vardef_context (atom, false, false, false, NULL))
1071 gfc_error ("ATOM argument of the %s intrinsic function at %L shall be "
1072 "definable", gfc_current_intrinsic, &atom->where);
1073 return false;
1076 return gfc_check_atomic (atom, 0, value, 1, stat, 2);
1080 bool
1081 gfc_check_atomic_op (gfc_expr *atom, gfc_expr *value, gfc_expr *stat)
1083 if (atom->ts.type != BT_INTEGER || atom->ts.kind != gfc_atomic_int_kind)
1085 gfc_error ("ATOM argument at %L to intrinsic function %s shall be an "
1086 "integer of ATOMIC_INT_KIND", &atom->where,
1087 gfc_current_intrinsic);
1088 return false;
1091 return gfc_check_atomic_def (atom, value, stat);
1095 bool
1096 gfc_check_atomic_ref (gfc_expr *value, gfc_expr *atom, gfc_expr *stat)
1098 if (atom->expr_type == EXPR_FUNCTION
1099 && atom->value.function.isym
1100 && atom->value.function.isym->id == GFC_ISYM_CAF_GET)
1101 atom = atom->value.function.actual->expr;
1103 if (!gfc_check_vardef_context (value, false, false, false, NULL))
1105 gfc_error ("VALUE argument of the %s intrinsic function at %L shall be "
1106 "definable", gfc_current_intrinsic, &value->where);
1107 return false;
1110 return gfc_check_atomic (atom, 1, value, 0, stat, 2);
1114 bool
1115 gfc_check_atomic_cas (gfc_expr *atom, gfc_expr *old, gfc_expr *compare,
1116 gfc_expr *new_val, gfc_expr *stat)
1118 if (atom->expr_type == EXPR_FUNCTION
1119 && atom->value.function.isym
1120 && atom->value.function.isym->id == GFC_ISYM_CAF_GET)
1121 atom = atom->value.function.actual->expr;
1123 if (!gfc_check_atomic (atom, 0, new_val, 3, stat, 4))
1124 return false;
1126 if (!scalar_check (old, 1) || !scalar_check (compare, 2))
1127 return false;
1129 if (!same_type_check (atom, 0, old, 1))
1130 return false;
1132 if (!same_type_check (atom, 0, compare, 2))
1133 return false;
1135 if (!gfc_check_vardef_context (atom, false, false, false, NULL))
1137 gfc_error ("ATOM argument of the %s intrinsic function at %L shall be "
1138 "definable", gfc_current_intrinsic, &atom->where);
1139 return false;
1142 if (!gfc_check_vardef_context (old, false, false, false, NULL))
1144 gfc_error ("OLD argument of the %s intrinsic function at %L shall be "
1145 "definable", gfc_current_intrinsic, &old->where);
1146 return false;
1149 return true;
1153 bool
1154 gfc_check_atomic_fetch_op (gfc_expr *atom, gfc_expr *value, gfc_expr *old,
1155 gfc_expr *stat)
1157 if (atom->expr_type == EXPR_FUNCTION
1158 && atom->value.function.isym
1159 && atom->value.function.isym->id == GFC_ISYM_CAF_GET)
1160 atom = atom->value.function.actual->expr;
1162 if (atom->ts.type != BT_INTEGER || atom->ts.kind != gfc_atomic_int_kind)
1164 gfc_error ("ATOM argument at %L to intrinsic function %s shall be an "
1165 "integer of ATOMIC_INT_KIND", &atom->where,
1166 gfc_current_intrinsic);
1167 return false;
1170 if (!gfc_check_atomic (atom, 0, value, 1, stat, 3))
1171 return false;
1173 if (!scalar_check (old, 2))
1174 return false;
1176 if (!same_type_check (atom, 0, old, 2))
1177 return false;
1179 if (!gfc_check_vardef_context (atom, false, false, false, NULL))
1181 gfc_error ("ATOM argument of the %s intrinsic function at %L shall be "
1182 "definable", gfc_current_intrinsic, &atom->where);
1183 return false;
1186 if (!gfc_check_vardef_context (old, false, false, false, NULL))
1188 gfc_error ("OLD argument of the %s intrinsic function at %L shall be "
1189 "definable", gfc_current_intrinsic, &old->where);
1190 return false;
1193 return true;
1197 /* BESJN and BESYN functions. */
1199 bool
1200 gfc_check_besn (gfc_expr *n, gfc_expr *x)
1202 if (!type_check (n, 0, BT_INTEGER))
1203 return false;
1204 if (n->expr_type == EXPR_CONSTANT)
1206 int i;
1207 gfc_extract_int (n, &i);
1208 if (i < 0 && !gfc_notify_std (GFC_STD_GNU, "Negative argument "
1209 "N at %L", &n->where))
1210 return false;
1213 if (!type_check (x, 1, BT_REAL))
1214 return false;
1216 return true;
1220 /* Transformational version of the Bessel JN and YN functions. */
1222 bool
1223 gfc_check_bessel_n2 (gfc_expr *n1, gfc_expr *n2, gfc_expr *x)
1225 if (!type_check (n1, 0, BT_INTEGER))
1226 return false;
1227 if (!scalar_check (n1, 0))
1228 return false;
1229 if (!nonnegative_check ("N1", n1))
1230 return false;
1232 if (!type_check (n2, 1, BT_INTEGER))
1233 return false;
1234 if (!scalar_check (n2, 1))
1235 return false;
1236 if (!nonnegative_check ("N2", n2))
1237 return false;
1239 if (!type_check (x, 2, BT_REAL))
1240 return false;
1241 if (!scalar_check (x, 2))
1242 return false;
1244 return true;
1248 bool
1249 gfc_check_bge_bgt_ble_blt (gfc_expr *i, gfc_expr *j)
1251 if (!type_check (i, 0, BT_INTEGER))
1252 return false;
1254 if (!type_check (j, 1, BT_INTEGER))
1255 return false;
1257 return true;
1261 bool
1262 gfc_check_bitfcn (gfc_expr *i, gfc_expr *pos)
1264 if (!type_check (i, 0, BT_INTEGER))
1265 return false;
1267 if (!type_check (pos, 1, BT_INTEGER))
1268 return false;
1270 if (!nonnegative_check ("pos", pos))
1271 return false;
1273 if (!less_than_bitsize1 ("i", i, "pos", pos, false))
1274 return false;
1276 return true;
1280 bool
1281 gfc_check_char (gfc_expr *i, gfc_expr *kind)
1283 if (!type_check (i, 0, BT_INTEGER))
1284 return false;
1285 if (!kind_check (kind, 1, BT_CHARACTER))
1286 return false;
1288 return true;
1292 bool
1293 gfc_check_chdir (gfc_expr *dir)
1295 if (!type_check (dir, 0, BT_CHARACTER))
1296 return false;
1297 if (!kind_value_check (dir, 0, gfc_default_character_kind))
1298 return false;
1300 return true;
1304 bool
1305 gfc_check_chdir_sub (gfc_expr *dir, gfc_expr *status)
1307 if (!type_check (dir, 0, BT_CHARACTER))
1308 return false;
1309 if (!kind_value_check (dir, 0, gfc_default_character_kind))
1310 return false;
1312 if (status == NULL)
1313 return true;
1315 if (!type_check (status, 1, BT_INTEGER))
1316 return false;
1317 if (!scalar_check (status, 1))
1318 return false;
1320 return true;
1324 bool
1325 gfc_check_chmod (gfc_expr *name, gfc_expr *mode)
1327 if (!type_check (name, 0, BT_CHARACTER))
1328 return false;
1329 if (!kind_value_check (name, 0, gfc_default_character_kind))
1330 return false;
1332 if (!type_check (mode, 1, BT_CHARACTER))
1333 return false;
1334 if (!kind_value_check (mode, 1, gfc_default_character_kind))
1335 return false;
1337 return true;
1341 bool
1342 gfc_check_chmod_sub (gfc_expr *name, gfc_expr *mode, gfc_expr *status)
1344 if (!type_check (name, 0, BT_CHARACTER))
1345 return false;
1346 if (!kind_value_check (name, 0, gfc_default_character_kind))
1347 return false;
1349 if (!type_check (mode, 1, BT_CHARACTER))
1350 return false;
1351 if (!kind_value_check (mode, 1, gfc_default_character_kind))
1352 return false;
1354 if (status == NULL)
1355 return true;
1357 if (!type_check (status, 2, BT_INTEGER))
1358 return false;
1360 if (!scalar_check (status, 2))
1361 return false;
1363 return true;
1367 bool
1368 gfc_check_cmplx (gfc_expr *x, gfc_expr *y, gfc_expr *kind)
1370 if (!numeric_check (x, 0))
1371 return false;
1373 if (y != NULL)
1375 if (!numeric_check (y, 1))
1376 return false;
1378 if (x->ts.type == BT_COMPLEX)
1380 gfc_error ("'%s' argument of '%s' intrinsic at %L must not be "
1381 "present if 'x' is COMPLEX",
1382 gfc_current_intrinsic_arg[1]->name, gfc_current_intrinsic,
1383 &y->where);
1384 return false;
1387 if (y->ts.type == BT_COMPLEX)
1389 gfc_error ("'%s' argument of '%s' intrinsic at %L must have a type "
1390 "of either REAL or INTEGER",
1391 gfc_current_intrinsic_arg[1]->name, gfc_current_intrinsic,
1392 &y->where);
1393 return false;
1398 if (!kind_check (kind, 2, BT_COMPLEX))
1399 return false;
1401 if (!kind && gfc_option.gfc_warn_conversion
1402 && x->ts.type == BT_REAL && x->ts.kind > gfc_default_real_kind)
1403 gfc_warning_now ("Conversion from %s to default-kind COMPLEX(%d) at %L "
1404 "might lose precision, consider using the KIND argument",
1405 gfc_typename (&x->ts), gfc_default_real_kind, &x->where);
1406 else if (y && !kind && gfc_option.gfc_warn_conversion
1407 && y->ts.type == BT_REAL && y->ts.kind > gfc_default_real_kind)
1408 gfc_warning_now ("Conversion from %s to default-kind COMPLEX(%d) at %L "
1409 "might lose precision, consider using the KIND argument",
1410 gfc_typename (&y->ts), gfc_default_real_kind, &y->where);
1412 return true;
1416 static bool
1417 check_co_minmaxsum (gfc_expr *a, gfc_expr *result_image, gfc_expr *stat,
1418 gfc_expr *errmsg)
1420 if (!variable_check (a, 0, false))
1421 return false;
1423 if (!gfc_check_vardef_context (a, false, false, false, "argument 'A' with "
1424 "INTENT(INOUT)"))
1425 return false;
1427 if (gfc_has_vector_subscript (a))
1429 gfc_error ("Argument 'A' with INTENT(INOUT) at %L of the intrinsic "
1430 "subroutine %s shall not have a vector subscript",
1431 &a->where, gfc_current_intrinsic);
1432 return false;
1435 if (result_image != NULL)
1437 if (!type_check (result_image, 1, BT_INTEGER))
1438 return false;
1439 if (!scalar_check (result_image, 1))
1440 return false;
1443 if (stat != NULL)
1445 if (!type_check (stat, 2, BT_INTEGER))
1446 return false;
1447 if (!scalar_check (stat, 2))
1448 return false;
1449 if (!variable_check (stat, 2, false))
1450 return false;
1451 if (stat->ts.kind != 4)
1453 gfc_error ("The stat= argument at %L must be a kind=4 integer "
1454 "variable", &stat->where);
1455 return false;
1459 if (errmsg != NULL)
1461 if (!type_check (errmsg, 3, BT_CHARACTER))
1462 return false;
1463 if (!scalar_check (errmsg, 3))
1464 return false;
1465 if (!variable_check (errmsg, 3, false))
1466 return false;
1467 if (errmsg->ts.kind != 1)
1469 gfc_error ("The errmsg= argument at %L must be a default-kind "
1470 "character variable", &errmsg->where);
1471 return false;
1475 if (gfc_option.coarray == GFC_FCOARRAY_NONE)
1477 gfc_fatal_error ("Coarrays disabled at %L, use -fcoarray= to enable",
1478 &a->where);
1479 return false;
1482 return true;
1486 bool
1487 gfc_check_co_minmax (gfc_expr *a, gfc_expr *result_image, gfc_expr *stat,
1488 gfc_expr *errmsg)
1490 if (a->ts.type != BT_INTEGER && a->ts.type != BT_REAL
1491 && a->ts.type != BT_CHARACTER)
1493 gfc_error ("'%s' argument of '%s' intrinsic at %L shall be of type "
1494 "integer, real or character",
1495 gfc_current_intrinsic_arg[0]->name, gfc_current_intrinsic,
1496 &a->where);
1497 return false;
1499 return check_co_minmaxsum (a, result_image, stat, errmsg);
1503 bool
1504 gfc_check_co_sum (gfc_expr *a, gfc_expr *result_image, gfc_expr *stat,
1505 gfc_expr *errmsg)
1507 if (!numeric_check (a, 0))
1508 return false;
1509 return check_co_minmaxsum (a, result_image, stat, errmsg);
1513 bool
1514 gfc_check_complex (gfc_expr *x, gfc_expr *y)
1516 if (!int_or_real_check (x, 0))
1517 return false;
1518 if (!scalar_check (x, 0))
1519 return false;
1521 if (!int_or_real_check (y, 1))
1522 return false;
1523 if (!scalar_check (y, 1))
1524 return false;
1526 return true;
1530 bool
1531 gfc_check_count (gfc_expr *mask, gfc_expr *dim, gfc_expr *kind)
1533 if (!logical_array_check (mask, 0))
1534 return false;
1535 if (!dim_check (dim, 1, false))
1536 return false;
1537 if (!dim_rank_check (dim, mask, 0))
1538 return false;
1539 if (!kind_check (kind, 2, BT_INTEGER))
1540 return false;
1541 if (kind && !gfc_notify_std (GFC_STD_F2003, "'%s' intrinsic "
1542 "with KIND argument at %L",
1543 gfc_current_intrinsic, &kind->where))
1544 return false;
1546 return true;
1550 bool
1551 gfc_check_cshift (gfc_expr *array, gfc_expr *shift, gfc_expr *dim)
1553 if (!array_check (array, 0))
1554 return false;
1556 if (!type_check (shift, 1, BT_INTEGER))
1557 return false;
1559 if (!dim_check (dim, 2, true))
1560 return false;
1562 if (!dim_rank_check (dim, array, false))
1563 return false;
1565 if (array->rank == 1 || shift->rank == 0)
1567 if (!scalar_check (shift, 1))
1568 return false;
1570 else if (shift->rank == array->rank - 1)
1572 int d;
1573 if (!dim)
1574 d = 1;
1575 else if (dim->expr_type == EXPR_CONSTANT)
1576 gfc_extract_int (dim, &d);
1577 else
1578 d = -1;
1580 if (d > 0)
1582 int i, j;
1583 for (i = 0, j = 0; i < array->rank; i++)
1584 if (i != d - 1)
1586 if (!identical_dimen_shape (array, i, shift, j))
1588 gfc_error ("'%s' argument of '%s' intrinsic at %L has "
1589 "invalid shape in dimension %d (%ld/%ld)",
1590 gfc_current_intrinsic_arg[1]->name,
1591 gfc_current_intrinsic, &shift->where, i + 1,
1592 mpz_get_si (array->shape[i]),
1593 mpz_get_si (shift->shape[j]));
1594 return false;
1597 j += 1;
1601 else
1603 gfc_error ("'%s' argument of intrinsic '%s' at %L of must have rank "
1604 "%d or be a scalar", gfc_current_intrinsic_arg[1]->name,
1605 gfc_current_intrinsic, &shift->where, array->rank - 1);
1606 return false;
1609 return true;
1613 bool
1614 gfc_check_ctime (gfc_expr *time)
1616 if (!scalar_check (time, 0))
1617 return false;
1619 if (!type_check (time, 0, BT_INTEGER))
1620 return false;
1622 return true;
1626 bool gfc_check_datan2 (gfc_expr *y, gfc_expr *x)
1628 if (!double_check (y, 0) || !double_check (x, 1))
1629 return false;
1631 return true;
1634 bool
1635 gfc_check_dcmplx (gfc_expr *x, gfc_expr *y)
1637 if (!numeric_check (x, 0))
1638 return false;
1640 if (y != NULL)
1642 if (!numeric_check (y, 1))
1643 return false;
1645 if (x->ts.type == BT_COMPLEX)
1647 gfc_error ("'%s' argument of '%s' intrinsic at %L must not be "
1648 "present if 'x' is COMPLEX",
1649 gfc_current_intrinsic_arg[1]->name, gfc_current_intrinsic,
1650 &y->where);
1651 return false;
1654 if (y->ts.type == BT_COMPLEX)
1656 gfc_error ("'%s' argument of '%s' intrinsic at %L must have a type "
1657 "of either REAL or INTEGER",
1658 gfc_current_intrinsic_arg[1]->name, gfc_current_intrinsic,
1659 &y->where);
1660 return false;
1664 return true;
1668 bool
1669 gfc_check_dble (gfc_expr *x)
1671 if (!numeric_check (x, 0))
1672 return false;
1674 return true;
1678 bool
1679 gfc_check_digits (gfc_expr *x)
1681 if (!int_or_real_check (x, 0))
1682 return false;
1684 return true;
1688 bool
1689 gfc_check_dot_product (gfc_expr *vector_a, gfc_expr *vector_b)
1691 switch (vector_a->ts.type)
1693 case BT_LOGICAL:
1694 if (!type_check (vector_b, 1, BT_LOGICAL))
1695 return false;
1696 break;
1698 case BT_INTEGER:
1699 case BT_REAL:
1700 case BT_COMPLEX:
1701 if (!numeric_check (vector_b, 1))
1702 return false;
1703 break;
1705 default:
1706 gfc_error ("'%s' argument of '%s' intrinsic at %L must be numeric "
1707 "or LOGICAL", gfc_current_intrinsic_arg[0]->name,
1708 gfc_current_intrinsic, &vector_a->where);
1709 return false;
1712 if (!rank_check (vector_a, 0, 1))
1713 return false;
1715 if (!rank_check (vector_b, 1, 1))
1716 return false;
1718 if (! identical_dimen_shape (vector_a, 0, vector_b, 0))
1720 gfc_error ("Different shape for arguments '%s' and '%s' at %L for "
1721 "intrinsic 'dot_product'", gfc_current_intrinsic_arg[0]->name,
1722 gfc_current_intrinsic_arg[1]->name, &vector_a->where);
1723 return false;
1726 return true;
1730 bool
1731 gfc_check_dprod (gfc_expr *x, gfc_expr *y)
1733 if (!type_check (x, 0, BT_REAL)
1734 || !type_check (y, 1, BT_REAL))
1735 return false;
1737 if (x->ts.kind != gfc_default_real_kind)
1739 gfc_error ("'%s' argument of '%s' intrinsic at %L must be default "
1740 "real", gfc_current_intrinsic_arg[0]->name,
1741 gfc_current_intrinsic, &x->where);
1742 return false;
1745 if (y->ts.kind != gfc_default_real_kind)
1747 gfc_error ("'%s' argument of '%s' intrinsic at %L must be default "
1748 "real", gfc_current_intrinsic_arg[1]->name,
1749 gfc_current_intrinsic, &y->where);
1750 return false;
1753 return true;
1757 bool
1758 gfc_check_dshift (gfc_expr *i, gfc_expr *j, gfc_expr *shift)
1760 if (!type_check (i, 0, BT_INTEGER))
1761 return false;
1763 if (!type_check (j, 1, BT_INTEGER))
1764 return false;
1766 if (i->is_boz && j->is_boz)
1768 gfc_error ("'I' at %L and 'J' at %L cannot both be BOZ literal "
1769 "constants", &i->where, &j->where);
1770 return false;
1773 if (!i->is_boz && !j->is_boz && !same_type_check (i, 0, j, 1))
1774 return false;
1776 if (!type_check (shift, 2, BT_INTEGER))
1777 return false;
1779 if (!nonnegative_check ("SHIFT", shift))
1780 return false;
1782 if (i->is_boz)
1784 if (!less_than_bitsize1 ("J", j, "SHIFT", shift, true))
1785 return false;
1786 i->ts.kind = j->ts.kind;
1788 else
1790 if (!less_than_bitsize1 ("I", i, "SHIFT", shift, true))
1791 return false;
1792 j->ts.kind = i->ts.kind;
1795 return true;
1799 bool
1800 gfc_check_eoshift (gfc_expr *array, gfc_expr *shift, gfc_expr *boundary,
1801 gfc_expr *dim)
1803 if (!array_check (array, 0))
1804 return false;
1806 if (!type_check (shift, 1, BT_INTEGER))
1807 return false;
1809 if (!dim_check (dim, 3, true))
1810 return false;
1812 if (!dim_rank_check (dim, array, false))
1813 return false;
1815 if (array->rank == 1 || shift->rank == 0)
1817 if (!scalar_check (shift, 1))
1818 return false;
1820 else if (shift->rank == array->rank - 1)
1822 int d;
1823 if (!dim)
1824 d = 1;
1825 else if (dim->expr_type == EXPR_CONSTANT)
1826 gfc_extract_int (dim, &d);
1827 else
1828 d = -1;
1830 if (d > 0)
1832 int i, j;
1833 for (i = 0, j = 0; i < array->rank; i++)
1834 if (i != d - 1)
1836 if (!identical_dimen_shape (array, i, shift, j))
1838 gfc_error ("'%s' argument of '%s' intrinsic at %L has "
1839 "invalid shape in dimension %d (%ld/%ld)",
1840 gfc_current_intrinsic_arg[1]->name,
1841 gfc_current_intrinsic, &shift->where, i + 1,
1842 mpz_get_si (array->shape[i]),
1843 mpz_get_si (shift->shape[j]));
1844 return false;
1847 j += 1;
1851 else
1853 gfc_error ("'%s' argument of intrinsic '%s' at %L of must have rank "
1854 "%d or be a scalar", gfc_current_intrinsic_arg[1]->name,
1855 gfc_current_intrinsic, &shift->where, array->rank - 1);
1856 return false;
1859 if (boundary != NULL)
1861 if (!same_type_check (array, 0, boundary, 2))
1862 return false;
1864 if (array->rank == 1 || boundary->rank == 0)
1866 if (!scalar_check (boundary, 2))
1867 return false;
1869 else if (boundary->rank == array->rank - 1)
1871 if (!gfc_check_conformance (shift, boundary,
1872 "arguments '%s' and '%s' for "
1873 "intrinsic %s",
1874 gfc_current_intrinsic_arg[1]->name,
1875 gfc_current_intrinsic_arg[2]->name,
1876 gfc_current_intrinsic))
1877 return false;
1879 else
1881 gfc_error ("'%s' argument of intrinsic '%s' at %L of must have "
1882 "rank %d or be a scalar",
1883 gfc_current_intrinsic_arg[1]->name, gfc_current_intrinsic,
1884 &shift->where, array->rank - 1);
1885 return false;
1889 return true;
1892 bool
1893 gfc_check_float (gfc_expr *a)
1895 if (!type_check (a, 0, BT_INTEGER))
1896 return false;
1898 if ((a->ts.kind != gfc_default_integer_kind)
1899 && !gfc_notify_std (GFC_STD_GNU, "non-default INTEGER "
1900 "kind argument to %s intrinsic at %L",
1901 gfc_current_intrinsic, &a->where))
1902 return false;
1904 return true;
1907 /* A single complex argument. */
1909 bool
1910 gfc_check_fn_c (gfc_expr *a)
1912 if (!type_check (a, 0, BT_COMPLEX))
1913 return false;
1915 return true;
1918 /* A single real argument. */
1920 bool
1921 gfc_check_fn_r (gfc_expr *a)
1923 if (!type_check (a, 0, BT_REAL))
1924 return false;
1926 return true;
1929 /* A single double argument. */
1931 bool
1932 gfc_check_fn_d (gfc_expr *a)
1934 if (!double_check (a, 0))
1935 return false;
1937 return true;
1940 /* A single real or complex argument. */
1942 bool
1943 gfc_check_fn_rc (gfc_expr *a)
1945 if (!real_or_complex_check (a, 0))
1946 return false;
1948 return true;
1952 bool
1953 gfc_check_fn_rc2008 (gfc_expr *a)
1955 if (!real_or_complex_check (a, 0))
1956 return false;
1958 if (a->ts.type == BT_COMPLEX
1959 && !gfc_notify_std (GFC_STD_F2008, "COMPLEX argument '%s' "
1960 "of '%s' intrinsic at %L",
1961 gfc_current_intrinsic_arg[0]->name,
1962 gfc_current_intrinsic, &a->where))
1963 return false;
1965 return true;
1969 bool
1970 gfc_check_fnum (gfc_expr *unit)
1972 if (!type_check (unit, 0, BT_INTEGER))
1973 return false;
1975 if (!scalar_check (unit, 0))
1976 return false;
1978 return true;
1982 bool
1983 gfc_check_huge (gfc_expr *x)
1985 if (!int_or_real_check (x, 0))
1986 return false;
1988 return true;
1992 bool
1993 gfc_check_hypot (gfc_expr *x, gfc_expr *y)
1995 if (!type_check (x, 0, BT_REAL))
1996 return false;
1997 if (!same_type_check (x, 0, y, 1))
1998 return false;
2000 return true;
2004 /* Check that the single argument is an integer. */
2006 bool
2007 gfc_check_i (gfc_expr *i)
2009 if (!type_check (i, 0, BT_INTEGER))
2010 return false;
2012 return true;
2016 bool
2017 gfc_check_iand (gfc_expr *i, gfc_expr *j)
2019 if (!type_check (i, 0, BT_INTEGER))
2020 return false;
2022 if (!type_check (j, 1, BT_INTEGER))
2023 return false;
2025 if (i->ts.kind != j->ts.kind)
2027 if (!gfc_notify_std (GFC_STD_GNU, "Different type kinds at %L",
2028 &i->where))
2029 return false;
2032 return true;
2036 bool
2037 gfc_check_ibits (gfc_expr *i, gfc_expr *pos, gfc_expr *len)
2039 if (!type_check (i, 0, BT_INTEGER))
2040 return false;
2042 if (!type_check (pos, 1, BT_INTEGER))
2043 return false;
2045 if (!type_check (len, 2, BT_INTEGER))
2046 return false;
2048 if (!nonnegative_check ("pos", pos))
2049 return false;
2051 if (!nonnegative_check ("len", len))
2052 return false;
2054 if (!less_than_bitsize2 ("i", i, "pos", pos, "len", len))
2055 return false;
2057 return true;
2061 bool
2062 gfc_check_ichar_iachar (gfc_expr *c, gfc_expr *kind)
2064 int i;
2066 if (!type_check (c, 0, BT_CHARACTER))
2067 return false;
2069 if (!kind_check (kind, 1, BT_INTEGER))
2070 return false;
2072 if (kind && !gfc_notify_std (GFC_STD_F2003, "'%s' intrinsic "
2073 "with KIND argument at %L",
2074 gfc_current_intrinsic, &kind->where))
2075 return false;
2077 if (c->expr_type == EXPR_VARIABLE || c->expr_type == EXPR_SUBSTRING)
2079 gfc_expr *start;
2080 gfc_expr *end;
2081 gfc_ref *ref;
2083 /* Substring references don't have the charlength set. */
2084 ref = c->ref;
2085 while (ref && ref->type != REF_SUBSTRING)
2086 ref = ref->next;
2088 gcc_assert (ref == NULL || ref->type == REF_SUBSTRING);
2090 if (!ref)
2092 /* Check that the argument is length one. Non-constant lengths
2093 can't be checked here, so assume they are ok. */
2094 if (c->ts.u.cl && c->ts.u.cl->length)
2096 /* If we already have a length for this expression then use it. */
2097 if (c->ts.u.cl->length->expr_type != EXPR_CONSTANT)
2098 return true;
2099 i = mpz_get_si (c->ts.u.cl->length->value.integer);
2101 else
2102 return true;
2104 else
2106 start = ref->u.ss.start;
2107 end = ref->u.ss.end;
2109 gcc_assert (start);
2110 if (end == NULL || end->expr_type != EXPR_CONSTANT
2111 || start->expr_type != EXPR_CONSTANT)
2112 return true;
2114 i = mpz_get_si (end->value.integer) + 1
2115 - mpz_get_si (start->value.integer);
2118 else
2119 return true;
2121 if (i != 1)
2123 gfc_error ("Argument of %s at %L must be of length one",
2124 gfc_current_intrinsic, &c->where);
2125 return false;
2128 return true;
2132 bool
2133 gfc_check_idnint (gfc_expr *a)
2135 if (!double_check (a, 0))
2136 return false;
2138 return true;
2142 bool
2143 gfc_check_ieor (gfc_expr *i, gfc_expr *j)
2145 if (!type_check (i, 0, BT_INTEGER))
2146 return false;
2148 if (!type_check (j, 1, BT_INTEGER))
2149 return false;
2151 if (i->ts.kind != j->ts.kind)
2153 if (!gfc_notify_std (GFC_STD_GNU, "Different type kinds at %L",
2154 &i->where))
2155 return false;
2158 return true;
2162 bool
2163 gfc_check_index (gfc_expr *string, gfc_expr *substring, gfc_expr *back,
2164 gfc_expr *kind)
2166 if (!type_check (string, 0, BT_CHARACTER)
2167 || !type_check (substring, 1, BT_CHARACTER))
2168 return false;
2170 if (back != NULL && !type_check (back, 2, BT_LOGICAL))
2171 return false;
2173 if (!kind_check (kind, 3, BT_INTEGER))
2174 return false;
2175 if (kind && !gfc_notify_std (GFC_STD_F2003, "'%s' intrinsic "
2176 "with KIND argument at %L",
2177 gfc_current_intrinsic, &kind->where))
2178 return false;
2180 if (string->ts.kind != substring->ts.kind)
2182 gfc_error ("'%s' argument of '%s' intrinsic at %L must be the same "
2183 "kind as '%s'", gfc_current_intrinsic_arg[1]->name,
2184 gfc_current_intrinsic, &substring->where,
2185 gfc_current_intrinsic_arg[0]->name);
2186 return false;
2189 return true;
2193 bool
2194 gfc_check_int (gfc_expr *x, gfc_expr *kind)
2196 if (!numeric_check (x, 0))
2197 return false;
2199 if (!kind_check (kind, 1, BT_INTEGER))
2200 return false;
2202 return true;
2206 bool
2207 gfc_check_intconv (gfc_expr *x)
2209 if (!numeric_check (x, 0))
2210 return false;
2212 return true;
2216 bool
2217 gfc_check_ior (gfc_expr *i, gfc_expr *j)
2219 if (!type_check (i, 0, BT_INTEGER))
2220 return false;
2222 if (!type_check (j, 1, BT_INTEGER))
2223 return false;
2225 if (i->ts.kind != j->ts.kind)
2227 if (!gfc_notify_std (GFC_STD_GNU, "Different type kinds at %L",
2228 &i->where))
2229 return false;
2232 return true;
2236 bool
2237 gfc_check_ishft (gfc_expr *i, gfc_expr *shift)
2239 if (!type_check (i, 0, BT_INTEGER)
2240 || !type_check (shift, 1, BT_INTEGER))
2241 return false;
2243 if (!less_than_bitsize1 ("I", i, NULL, shift, true))
2244 return false;
2246 return true;
2250 bool
2251 gfc_check_ishftc (gfc_expr *i, gfc_expr *shift, gfc_expr *size)
2253 if (!type_check (i, 0, BT_INTEGER)
2254 || !type_check (shift, 1, BT_INTEGER))
2255 return false;
2257 if (size != NULL)
2259 int i2, i3;
2261 if (!type_check (size, 2, BT_INTEGER))
2262 return false;
2264 if (!less_than_bitsize1 ("I", i, "SIZE", size, true))
2265 return false;
2267 if (size->expr_type == EXPR_CONSTANT)
2269 gfc_extract_int (size, &i3);
2270 if (i3 <= 0)
2272 gfc_error ("SIZE at %L must be positive", &size->where);
2273 return false;
2276 if (shift->expr_type == EXPR_CONSTANT)
2278 gfc_extract_int (shift, &i2);
2279 if (i2 < 0)
2280 i2 = -i2;
2282 if (i2 > i3)
2284 gfc_error ("The absolute value of SHIFT at %L must be less "
2285 "than or equal to SIZE at %L", &shift->where,
2286 &size->where);
2287 return false;
2292 else if (!less_than_bitsize1 ("I", i, NULL, shift, true))
2293 return false;
2295 return true;
2299 bool
2300 gfc_check_kill (gfc_expr *pid, gfc_expr *sig)
2302 if (!type_check (pid, 0, BT_INTEGER))
2303 return false;
2305 if (!type_check (sig, 1, BT_INTEGER))
2306 return false;
2308 return true;
2312 bool
2313 gfc_check_kill_sub (gfc_expr *pid, gfc_expr *sig, gfc_expr *status)
2315 if (!type_check (pid, 0, BT_INTEGER))
2316 return false;
2318 if (!scalar_check (pid, 0))
2319 return false;
2321 if (!type_check (sig, 1, BT_INTEGER))
2322 return false;
2324 if (!scalar_check (sig, 1))
2325 return false;
2327 if (status == NULL)
2328 return true;
2330 if (!type_check (status, 2, BT_INTEGER))
2331 return false;
2333 if (!scalar_check (status, 2))
2334 return false;
2336 return true;
2340 bool
2341 gfc_check_kind (gfc_expr *x)
2343 if (x->ts.type == BT_DERIVED)
2345 gfc_error ("'%s' argument of '%s' intrinsic at %L must be a "
2346 "non-derived type", gfc_current_intrinsic_arg[0]->name,
2347 gfc_current_intrinsic, &x->where);
2348 return false;
2351 return true;
2355 bool
2356 gfc_check_lbound (gfc_expr *array, gfc_expr *dim, gfc_expr *kind)
2358 if (!array_check (array, 0))
2359 return false;
2361 if (!dim_check (dim, 1, false))
2362 return false;
2364 if (!dim_rank_check (dim, array, 1))
2365 return false;
2367 if (!kind_check (kind, 2, BT_INTEGER))
2368 return false;
2369 if (kind && !gfc_notify_std (GFC_STD_F2003, "'%s' intrinsic "
2370 "with KIND argument at %L",
2371 gfc_current_intrinsic, &kind->where))
2372 return false;
2374 return true;
2378 bool
2379 gfc_check_lcobound (gfc_expr *coarray, gfc_expr *dim, gfc_expr *kind)
2381 if (gfc_option.coarray == GFC_FCOARRAY_NONE)
2383 gfc_fatal_error ("Coarrays disabled at %C, use -fcoarray= to enable");
2384 return false;
2387 if (!coarray_check (coarray, 0))
2388 return false;
2390 if (dim != NULL)
2392 if (!dim_check (dim, 1, false))
2393 return false;
2395 if (!dim_corank_check (dim, coarray))
2396 return false;
2399 if (!kind_check (kind, 2, BT_INTEGER))
2400 return false;
2402 return true;
2406 bool
2407 gfc_check_len_lentrim (gfc_expr *s, gfc_expr *kind)
2409 if (!type_check (s, 0, BT_CHARACTER))
2410 return false;
2412 if (!kind_check (kind, 1, BT_INTEGER))
2413 return false;
2414 if (kind && !gfc_notify_std (GFC_STD_F2003, "'%s' intrinsic "
2415 "with KIND argument at %L",
2416 gfc_current_intrinsic, &kind->where))
2417 return false;
2419 return true;
2423 bool
2424 gfc_check_lge_lgt_lle_llt (gfc_expr *a, gfc_expr *b)
2426 if (!type_check (a, 0, BT_CHARACTER))
2427 return false;
2428 if (!kind_value_check (a, 0, gfc_default_character_kind))
2429 return false;
2431 if (!type_check (b, 1, BT_CHARACTER))
2432 return false;
2433 if (!kind_value_check (b, 1, gfc_default_character_kind))
2434 return false;
2436 return true;
2440 bool
2441 gfc_check_link (gfc_expr *path1, gfc_expr *path2)
2443 if (!type_check (path1, 0, BT_CHARACTER))
2444 return false;
2445 if (!kind_value_check (path1, 0, gfc_default_character_kind))
2446 return false;
2448 if (!type_check (path2, 1, BT_CHARACTER))
2449 return false;
2450 if (!kind_value_check (path2, 1, gfc_default_character_kind))
2451 return false;
2453 return true;
2457 bool
2458 gfc_check_link_sub (gfc_expr *path1, gfc_expr *path2, gfc_expr *status)
2460 if (!type_check (path1, 0, BT_CHARACTER))
2461 return false;
2462 if (!kind_value_check (path1, 0, gfc_default_character_kind))
2463 return false;
2465 if (!type_check (path2, 1, BT_CHARACTER))
2466 return false;
2467 if (!kind_value_check (path2, 0, gfc_default_character_kind))
2468 return false;
2470 if (status == NULL)
2471 return true;
2473 if (!type_check (status, 2, BT_INTEGER))
2474 return false;
2476 if (!scalar_check (status, 2))
2477 return false;
2479 return true;
2483 bool
2484 gfc_check_loc (gfc_expr *expr)
2486 return variable_check (expr, 0, true);
2490 bool
2491 gfc_check_symlnk (gfc_expr *path1, gfc_expr *path2)
2493 if (!type_check (path1, 0, BT_CHARACTER))
2494 return false;
2495 if (!kind_value_check (path1, 0, gfc_default_character_kind))
2496 return false;
2498 if (!type_check (path2, 1, BT_CHARACTER))
2499 return false;
2500 if (!kind_value_check (path2, 1, gfc_default_character_kind))
2501 return false;
2503 return true;
2507 bool
2508 gfc_check_symlnk_sub (gfc_expr *path1, gfc_expr *path2, gfc_expr *status)
2510 if (!type_check (path1, 0, BT_CHARACTER))
2511 return false;
2512 if (!kind_value_check (path1, 0, gfc_default_character_kind))
2513 return false;
2515 if (!type_check (path2, 1, BT_CHARACTER))
2516 return false;
2517 if (!kind_value_check (path2, 1, gfc_default_character_kind))
2518 return false;
2520 if (status == NULL)
2521 return true;
2523 if (!type_check (status, 2, BT_INTEGER))
2524 return false;
2526 if (!scalar_check (status, 2))
2527 return false;
2529 return true;
2533 bool
2534 gfc_check_logical (gfc_expr *a, gfc_expr *kind)
2536 if (!type_check (a, 0, BT_LOGICAL))
2537 return false;
2538 if (!kind_check (kind, 1, BT_LOGICAL))
2539 return false;
2541 return true;
2545 /* Min/max family. */
2547 static bool
2548 min_max_args (gfc_actual_arglist *args)
2550 gfc_actual_arglist *arg;
2551 int i, j, nargs, *nlabels, nlabelless;
2552 bool a1 = false, a2 = false;
2554 if (args == NULL || args->next == NULL)
2556 gfc_error ("Intrinsic '%s' at %L must have at least two arguments",
2557 gfc_current_intrinsic, gfc_current_intrinsic_where);
2558 return false;
2561 if (!args->name)
2562 a1 = true;
2564 if (!args->next->name)
2565 a2 = true;
2567 nargs = 0;
2568 for (arg = args; arg; arg = arg->next)
2569 if (arg->name)
2570 nargs++;
2572 if (nargs == 0)
2573 return true;
2575 /* Note: Having a keywordless argument after an "arg=" is checked before. */
2576 nlabelless = 0;
2577 nlabels = XALLOCAVEC (int, nargs);
2578 for (arg = args, i = 0; arg; arg = arg->next, i++)
2579 if (arg->name)
2581 int n;
2582 char *endp;
2584 if (arg->name[0] != 'a' || arg->name[1] < '1' || arg->name[1] > '9')
2585 goto unknown;
2586 n = strtol (&arg->name[1], &endp, 10);
2587 if (endp[0] != '\0')
2588 goto unknown;
2589 if (n <= 0)
2590 goto unknown;
2591 if (n <= nlabelless)
2592 goto duplicate;
2593 nlabels[i] = n;
2594 if (n == 1)
2595 a1 = true;
2596 if (n == 2)
2597 a2 = true;
2599 else
2600 nlabelless++;
2602 if (!a1 || !a2)
2604 gfc_error ("Missing '%s' argument to the %s intrinsic at %L",
2605 !a1 ? "a1" : "a2", gfc_current_intrinsic,
2606 gfc_current_intrinsic_where);
2607 return false;
2610 /* Check for duplicates. */
2611 for (i = 0; i < nargs; i++)
2612 for (j = i + 1; j < nargs; j++)
2613 if (nlabels[i] == nlabels[j])
2614 goto duplicate;
2616 return true;
2618 duplicate:
2619 gfc_error ("Duplicate argument '%s' at %L to intrinsic %s", arg->name,
2620 &arg->expr->where, gfc_current_intrinsic);
2621 return false;
2623 unknown:
2624 gfc_error ("Unknown argument '%s' at %L to intrinsic %s", arg->name,
2625 &arg->expr->where, gfc_current_intrinsic);
2626 return false;
2630 static bool
2631 check_rest (bt type, int kind, gfc_actual_arglist *arglist)
2633 gfc_actual_arglist *arg, *tmp;
2634 gfc_expr *x;
2635 int m, n;
2637 if (!min_max_args (arglist))
2638 return false;
2640 for (arg = arglist, n=1; arg; arg = arg->next, n++)
2642 x = arg->expr;
2643 if (x->ts.type != type || x->ts.kind != kind)
2645 if (x->ts.type == type)
2647 if (!gfc_notify_std (GFC_STD_GNU, "Different type "
2648 "kinds at %L", &x->where))
2649 return false;
2651 else
2653 gfc_error ("'a%d' argument of '%s' intrinsic at %L must be "
2654 "%s(%d)", n, gfc_current_intrinsic, &x->where,
2655 gfc_basic_typename (type), kind);
2656 return false;
2660 for (tmp = arglist, m=1; tmp != arg; tmp = tmp->next, m++)
2661 if (!gfc_check_conformance (tmp->expr, x,
2662 "arguments 'a%d' and 'a%d' for "
2663 "intrinsic '%s'", m, n,
2664 gfc_current_intrinsic))
2665 return false;
2668 return true;
2672 bool
2673 gfc_check_min_max (gfc_actual_arglist *arg)
2675 gfc_expr *x;
2677 if (!min_max_args (arg))
2678 return false;
2680 x = arg->expr;
2682 if (x->ts.type == BT_CHARACTER)
2684 if (!gfc_notify_std (GFC_STD_F2003, "'%s' intrinsic "
2685 "with CHARACTER argument at %L",
2686 gfc_current_intrinsic, &x->where))
2687 return false;
2689 else if (x->ts.type != BT_INTEGER && x->ts.type != BT_REAL)
2691 gfc_error ("'a1' argument of '%s' intrinsic at %L must be INTEGER, "
2692 "REAL or CHARACTER", gfc_current_intrinsic, &x->where);
2693 return false;
2696 return check_rest (x->ts.type, x->ts.kind, arg);
2700 bool
2701 gfc_check_min_max_integer (gfc_actual_arglist *arg)
2703 return check_rest (BT_INTEGER, gfc_default_integer_kind, arg);
2707 bool
2708 gfc_check_min_max_real (gfc_actual_arglist *arg)
2710 return check_rest (BT_REAL, gfc_default_real_kind, arg);
2714 bool
2715 gfc_check_min_max_double (gfc_actual_arglist *arg)
2717 return check_rest (BT_REAL, gfc_default_double_kind, arg);
2721 /* End of min/max family. */
2723 bool
2724 gfc_check_malloc (gfc_expr *size)
2726 if (!type_check (size, 0, BT_INTEGER))
2727 return false;
2729 if (!scalar_check (size, 0))
2730 return false;
2732 return true;
2736 bool
2737 gfc_check_matmul (gfc_expr *matrix_a, gfc_expr *matrix_b)
2739 if ((matrix_a->ts.type != BT_LOGICAL) && !gfc_numeric_ts (&matrix_a->ts))
2741 gfc_error ("'%s' argument of '%s' intrinsic at %L must be numeric "
2742 "or LOGICAL", gfc_current_intrinsic_arg[0]->name,
2743 gfc_current_intrinsic, &matrix_a->where);
2744 return false;
2747 if ((matrix_b->ts.type != BT_LOGICAL) && !gfc_numeric_ts (&matrix_b->ts))
2749 gfc_error ("'%s' argument of '%s' intrinsic at %L must be numeric "
2750 "or LOGICAL", gfc_current_intrinsic_arg[1]->name,
2751 gfc_current_intrinsic, &matrix_b->where);
2752 return false;
2755 if ((matrix_a->ts.type == BT_LOGICAL && gfc_numeric_ts (&matrix_b->ts))
2756 || (gfc_numeric_ts (&matrix_a->ts) && matrix_b->ts.type == BT_LOGICAL))
2758 gfc_error ("Argument types of '%s' intrinsic at %L must match (%s/%s)",
2759 gfc_current_intrinsic, &matrix_a->where,
2760 gfc_typename(&matrix_a->ts), gfc_typename(&matrix_b->ts));
2761 return false;
2764 switch (matrix_a->rank)
2766 case 1:
2767 if (!rank_check (matrix_b, 1, 2))
2768 return false;
2769 /* Check for case matrix_a has shape(m), matrix_b has shape (m, k). */
2770 if (!identical_dimen_shape (matrix_a, 0, matrix_b, 0))
2772 gfc_error ("Different shape on dimension 1 for arguments '%s' "
2773 "and '%s' at %L for intrinsic matmul",
2774 gfc_current_intrinsic_arg[0]->name,
2775 gfc_current_intrinsic_arg[1]->name, &matrix_a->where);
2776 return false;
2778 break;
2780 case 2:
2781 if (matrix_b->rank != 2)
2783 if (!rank_check (matrix_b, 1, 1))
2784 return false;
2786 /* matrix_b has rank 1 or 2 here. Common check for the cases
2787 - matrix_a has shape (n,m) and matrix_b has shape (m, k)
2788 - matrix_a has shape (n,m) and matrix_b has shape (m). */
2789 if (!identical_dimen_shape (matrix_a, 1, matrix_b, 0))
2791 gfc_error ("Different shape on dimension 2 for argument '%s' and "
2792 "dimension 1 for argument '%s' at %L for intrinsic "
2793 "matmul", gfc_current_intrinsic_arg[0]->name,
2794 gfc_current_intrinsic_arg[1]->name, &matrix_a->where);
2795 return false;
2797 break;
2799 default:
2800 gfc_error ("'%s' argument of '%s' intrinsic at %L must be of rank "
2801 "1 or 2", gfc_current_intrinsic_arg[0]->name,
2802 gfc_current_intrinsic, &matrix_a->where);
2803 return false;
2806 return true;
2810 /* Whoever came up with this interface was probably on something.
2811 The possibilities for the occupation of the second and third
2812 parameters are:
2814 Arg #2 Arg #3
2815 NULL NULL
2816 DIM NULL
2817 MASK NULL
2818 NULL MASK minloc(array, mask=m)
2819 DIM MASK
2821 I.e. in the case of minloc(array,mask), mask will be in the second
2822 position of the argument list and we'll have to fix that up. */
2824 bool
2825 gfc_check_minloc_maxloc (gfc_actual_arglist *ap)
2827 gfc_expr *a, *m, *d;
2829 a = ap->expr;
2830 if (!int_or_real_check (a, 0) || !array_check (a, 0))
2831 return false;
2833 d = ap->next->expr;
2834 m = ap->next->next->expr;
2836 if (m == NULL && d != NULL && d->ts.type == BT_LOGICAL
2837 && ap->next->name == NULL)
2839 m = d;
2840 d = NULL;
2841 ap->next->expr = NULL;
2842 ap->next->next->expr = m;
2845 if (!dim_check (d, 1, false))
2846 return false;
2848 if (!dim_rank_check (d, a, 0))
2849 return false;
2851 if (m != NULL && !type_check (m, 2, BT_LOGICAL))
2852 return false;
2854 if (m != NULL
2855 && !gfc_check_conformance (a, m,
2856 "arguments '%s' and '%s' for intrinsic %s",
2857 gfc_current_intrinsic_arg[0]->name,
2858 gfc_current_intrinsic_arg[2]->name,
2859 gfc_current_intrinsic))
2860 return false;
2862 return true;
2866 /* Similar to minloc/maxloc, the argument list might need to be
2867 reordered for the MINVAL, MAXVAL, PRODUCT, and SUM intrinsics. The
2868 difference is that MINLOC/MAXLOC take an additional KIND argument.
2869 The possibilities are:
2871 Arg #2 Arg #3
2872 NULL NULL
2873 DIM NULL
2874 MASK NULL
2875 NULL MASK minval(array, mask=m)
2876 DIM MASK
2878 I.e. in the case of minval(array,mask), mask will be in the second
2879 position of the argument list and we'll have to fix that up. */
2881 static bool
2882 check_reduction (gfc_actual_arglist *ap)
2884 gfc_expr *a, *m, *d;
2886 a = ap->expr;
2887 d = ap->next->expr;
2888 m = ap->next->next->expr;
2890 if (m == NULL && d != NULL && d->ts.type == BT_LOGICAL
2891 && ap->next->name == NULL)
2893 m = d;
2894 d = NULL;
2895 ap->next->expr = NULL;
2896 ap->next->next->expr = m;
2899 if (!dim_check (d, 1, false))
2900 return false;
2902 if (!dim_rank_check (d, a, 0))
2903 return false;
2905 if (m != NULL && !type_check (m, 2, BT_LOGICAL))
2906 return false;
2908 if (m != NULL
2909 && !gfc_check_conformance (a, m,
2910 "arguments '%s' and '%s' for intrinsic %s",
2911 gfc_current_intrinsic_arg[0]->name,
2912 gfc_current_intrinsic_arg[2]->name,
2913 gfc_current_intrinsic))
2914 return false;
2916 return true;
2920 bool
2921 gfc_check_minval_maxval (gfc_actual_arglist *ap)
2923 if (!int_or_real_check (ap->expr, 0)
2924 || !array_check (ap->expr, 0))
2925 return false;
2927 return check_reduction (ap);
2931 bool
2932 gfc_check_product_sum (gfc_actual_arglist *ap)
2934 if (!numeric_check (ap->expr, 0)
2935 || !array_check (ap->expr, 0))
2936 return false;
2938 return check_reduction (ap);
2942 /* For IANY, IALL and IPARITY. */
2944 bool
2945 gfc_check_mask (gfc_expr *i, gfc_expr *kind)
2947 int k;
2949 if (!type_check (i, 0, BT_INTEGER))
2950 return false;
2952 if (!nonnegative_check ("I", i))
2953 return false;
2955 if (!kind_check (kind, 1, BT_INTEGER))
2956 return false;
2958 if (kind)
2959 gfc_extract_int (kind, &k);
2960 else
2961 k = gfc_default_integer_kind;
2963 if (!less_than_bitsizekind ("I", i, k))
2964 return false;
2966 return true;
2970 bool
2971 gfc_check_transf_bit_intrins (gfc_actual_arglist *ap)
2973 if (ap->expr->ts.type != BT_INTEGER)
2975 gfc_error ("'%s' argument of '%s' intrinsic at %L must be INTEGER",
2976 gfc_current_intrinsic_arg[0]->name,
2977 gfc_current_intrinsic, &ap->expr->where);
2978 return false;
2981 if (!array_check (ap->expr, 0))
2982 return false;
2984 return check_reduction (ap);
2988 bool
2989 gfc_check_merge (gfc_expr *tsource, gfc_expr *fsource, gfc_expr *mask)
2991 if (!same_type_check (tsource, 0, fsource, 1))
2992 return false;
2994 if (!type_check (mask, 2, BT_LOGICAL))
2995 return false;
2997 if (tsource->ts.type == BT_CHARACTER)
2998 return gfc_check_same_strlen (tsource, fsource, "MERGE intrinsic");
3000 return true;
3004 bool
3005 gfc_check_merge_bits (gfc_expr *i, gfc_expr *j, gfc_expr *mask)
3007 if (!type_check (i, 0, BT_INTEGER))
3008 return false;
3010 if (!type_check (j, 1, BT_INTEGER))
3011 return false;
3013 if (!type_check (mask, 2, BT_INTEGER))
3014 return false;
3016 if (!same_type_check (i, 0, j, 1))
3017 return false;
3019 if (!same_type_check (i, 0, mask, 2))
3020 return false;
3022 return true;
3026 bool
3027 gfc_check_move_alloc (gfc_expr *from, gfc_expr *to)
3029 if (!variable_check (from, 0, false))
3030 return false;
3031 if (!allocatable_check (from, 0))
3032 return false;
3033 if (gfc_is_coindexed (from))
3035 gfc_error ("The FROM argument to MOVE_ALLOC at %L shall not be "
3036 "coindexed", &from->where);
3037 return false;
3040 if (!variable_check (to, 1, false))
3041 return false;
3042 if (!allocatable_check (to, 1))
3043 return false;
3044 if (gfc_is_coindexed (to))
3046 gfc_error ("The TO argument to MOVE_ALLOC at %L shall not be "
3047 "coindexed", &to->where);
3048 return false;
3051 if (from->ts.type == BT_CLASS && to->ts.type == BT_DERIVED)
3053 gfc_error ("The TO arguments in MOVE_ALLOC at %L must be "
3054 "polymorphic if FROM is polymorphic",
3055 &to->where);
3056 return false;
3059 if (!same_type_check (to, 1, from, 0))
3060 return false;
3062 if (to->rank != from->rank)
3064 gfc_error ("The FROM and TO arguments of the MOVE_ALLOC intrinsic at %L "
3065 "must have the same rank %d/%d", &to->where, from->rank,
3066 to->rank);
3067 return false;
3070 /* IR F08/0040; cf. 12-006A. */
3071 if (gfc_get_corank (to) != gfc_get_corank (from))
3073 gfc_error ("The FROM and TO arguments of the MOVE_ALLOC intrinsic at %L "
3074 "must have the same corank %d/%d", &to->where,
3075 gfc_get_corank (from), gfc_get_corank (to));
3076 return false;
3079 /* CLASS arguments: Make sure the vtab of from is present. */
3080 if (to->ts.type == BT_CLASS && !UNLIMITED_POLY (from))
3081 gfc_find_vtab (&from->ts);
3083 return true;
3087 bool
3088 gfc_check_nearest (gfc_expr *x, gfc_expr *s)
3090 if (!type_check (x, 0, BT_REAL))
3091 return false;
3093 if (!type_check (s, 1, BT_REAL))
3094 return false;
3096 if (s->expr_type == EXPR_CONSTANT)
3098 if (mpfr_sgn (s->value.real) == 0)
3100 gfc_error ("Argument 'S' of NEAREST at %L shall not be zero",
3101 &s->where);
3102 return false;
3106 return true;
3110 bool
3111 gfc_check_new_line (gfc_expr *a)
3113 if (!type_check (a, 0, BT_CHARACTER))
3114 return false;
3116 return true;
3120 bool
3121 gfc_check_norm2 (gfc_expr *array, gfc_expr *dim)
3123 if (!type_check (array, 0, BT_REAL))
3124 return false;
3126 if (!array_check (array, 0))
3127 return false;
3129 if (!dim_rank_check (dim, array, false))
3130 return false;
3132 return true;
3135 bool
3136 gfc_check_null (gfc_expr *mold)
3138 symbol_attribute attr;
3140 if (mold == NULL)
3141 return true;
3143 if (!variable_check (mold, 0, true))
3144 return false;
3146 attr = gfc_variable_attr (mold, NULL);
3148 if (!attr.pointer && !attr.proc_pointer && !attr.allocatable)
3150 gfc_error ("'%s' argument of '%s' intrinsic at %L must be a POINTER, "
3151 "ALLOCATABLE or procedure pointer",
3152 gfc_current_intrinsic_arg[0]->name,
3153 gfc_current_intrinsic, &mold->where);
3154 return false;
3157 if (attr.allocatable
3158 && !gfc_notify_std (GFC_STD_F2003, "NULL intrinsic with "
3159 "allocatable MOLD at %L", &mold->where))
3160 return false;
3162 /* F2008, C1242. */
3163 if (gfc_is_coindexed (mold))
3165 gfc_error ("'%s' argument of '%s' intrinsic at %L shall not be "
3166 "coindexed", gfc_current_intrinsic_arg[0]->name,
3167 gfc_current_intrinsic, &mold->where);
3168 return false;
3171 return true;
3175 bool
3176 gfc_check_pack (gfc_expr *array, gfc_expr *mask, gfc_expr *vector)
3178 if (!array_check (array, 0))
3179 return false;
3181 if (!type_check (mask, 1, BT_LOGICAL))
3182 return false;
3184 if (!gfc_check_conformance (array, mask,
3185 "arguments '%s' and '%s' for intrinsic '%s'",
3186 gfc_current_intrinsic_arg[0]->name,
3187 gfc_current_intrinsic_arg[1]->name,
3188 gfc_current_intrinsic))
3189 return false;
3191 if (vector != NULL)
3193 mpz_t array_size, vector_size;
3194 bool have_array_size, have_vector_size;
3196 if (!same_type_check (array, 0, vector, 2))
3197 return false;
3199 if (!rank_check (vector, 2, 1))
3200 return false;
3202 /* VECTOR requires at least as many elements as MASK
3203 has .TRUE. values. */
3204 have_array_size = gfc_array_size(array, &array_size);
3205 have_vector_size = gfc_array_size(vector, &vector_size);
3207 if (have_vector_size
3208 && (mask->expr_type == EXPR_ARRAY
3209 || (mask->expr_type == EXPR_CONSTANT
3210 && have_array_size)))
3212 int mask_true_values = 0;
3214 if (mask->expr_type == EXPR_ARRAY)
3216 gfc_constructor *mask_ctor;
3217 mask_ctor = gfc_constructor_first (mask->value.constructor);
3218 while (mask_ctor)
3220 if (mask_ctor->expr->expr_type != EXPR_CONSTANT)
3222 mask_true_values = 0;
3223 break;
3226 if (mask_ctor->expr->value.logical)
3227 mask_true_values++;
3229 mask_ctor = gfc_constructor_next (mask_ctor);
3232 else if (mask->expr_type == EXPR_CONSTANT && mask->value.logical)
3233 mask_true_values = mpz_get_si (array_size);
3235 if (mpz_get_si (vector_size) < mask_true_values)
3237 gfc_error ("'%s' argument of '%s' intrinsic at %L must "
3238 "provide at least as many elements as there "
3239 "are .TRUE. values in '%s' (%ld/%d)",
3240 gfc_current_intrinsic_arg[2]->name,
3241 gfc_current_intrinsic, &vector->where,
3242 gfc_current_intrinsic_arg[1]->name,
3243 mpz_get_si (vector_size), mask_true_values);
3244 return false;
3248 if (have_array_size)
3249 mpz_clear (array_size);
3250 if (have_vector_size)
3251 mpz_clear (vector_size);
3254 return true;
3258 bool
3259 gfc_check_parity (gfc_expr *mask, gfc_expr *dim)
3261 if (!type_check (mask, 0, BT_LOGICAL))
3262 return false;
3264 if (!array_check (mask, 0))
3265 return false;
3267 if (!dim_rank_check (dim, mask, false))
3268 return false;
3270 return true;
3274 bool
3275 gfc_check_precision (gfc_expr *x)
3277 if (!real_or_complex_check (x, 0))
3278 return false;
3280 return true;
3284 bool
3285 gfc_check_present (gfc_expr *a)
3287 gfc_symbol *sym;
3289 if (!variable_check (a, 0, true))
3290 return false;
3292 sym = a->symtree->n.sym;
3293 if (!sym->attr.dummy)
3295 gfc_error ("'%s' argument of '%s' intrinsic at %L must be of a "
3296 "dummy variable", gfc_current_intrinsic_arg[0]->name,
3297 gfc_current_intrinsic, &a->where);
3298 return false;
3301 if (!sym->attr.optional)
3303 gfc_error ("'%s' argument of '%s' intrinsic at %L must be of "
3304 "an OPTIONAL dummy variable",
3305 gfc_current_intrinsic_arg[0]->name, gfc_current_intrinsic,
3306 &a->where);
3307 return false;
3310 /* 13.14.82 PRESENT(A)
3311 ......
3312 Argument. A shall be the name of an optional dummy argument that is
3313 accessible in the subprogram in which the PRESENT function reference
3314 appears... */
3316 if (a->ref != NULL
3317 && !(a->ref->next == NULL && a->ref->type == REF_ARRAY
3318 && (a->ref->u.ar.type == AR_FULL
3319 || (a->ref->u.ar.type == AR_ELEMENT
3320 && a->ref->u.ar.as->rank == 0))))
3322 gfc_error ("'%s' argument of '%s' intrinsic at %L must not be a "
3323 "subobject of '%s'", gfc_current_intrinsic_arg[0]->name,
3324 gfc_current_intrinsic, &a->where, sym->name);
3325 return false;
3328 return true;
3332 bool
3333 gfc_check_radix (gfc_expr *x)
3335 if (!int_or_real_check (x, 0))
3336 return false;
3338 return true;
3342 bool
3343 gfc_check_range (gfc_expr *x)
3345 if (!numeric_check (x, 0))
3346 return false;
3348 return true;
3352 bool
3353 gfc_check_rank (gfc_expr *a ATTRIBUTE_UNUSED)
3355 /* Any data object is allowed; a "data object" is a "constant (4.1.3),
3356 variable (6), or subobject of a constant (2.4.3.2.3)" (F2008, 1.3.45). */
3358 bool is_variable = true;
3360 /* Functions returning pointers are regarded as variable, cf. F2008, R602. */
3361 if (a->expr_type == EXPR_FUNCTION)
3362 is_variable = a->value.function.esym
3363 ? a->value.function.esym->result->attr.pointer
3364 : a->symtree->n.sym->result->attr.pointer;
3366 if (a->expr_type == EXPR_OP || a->expr_type == EXPR_NULL
3367 || a->expr_type == EXPR_COMPCALL|| a->expr_type == EXPR_PPC
3368 || !is_variable)
3370 gfc_error ("The argument of the RANK intrinsic at %L must be a data "
3371 "object", &a->where);
3372 return false;
3375 return true;
3379 /* real, float, sngl. */
3380 bool
3381 gfc_check_real (gfc_expr *a, gfc_expr *kind)
3383 if (!numeric_check (a, 0))
3384 return false;
3386 if (!kind_check (kind, 1, BT_REAL))
3387 return false;
3389 return true;
3393 bool
3394 gfc_check_rename (gfc_expr *path1, gfc_expr *path2)
3396 if (!type_check (path1, 0, BT_CHARACTER))
3397 return false;
3398 if (!kind_value_check (path1, 0, gfc_default_character_kind))
3399 return false;
3401 if (!type_check (path2, 1, BT_CHARACTER))
3402 return false;
3403 if (!kind_value_check (path2, 1, gfc_default_character_kind))
3404 return false;
3406 return true;
3410 bool
3411 gfc_check_rename_sub (gfc_expr *path1, gfc_expr *path2, gfc_expr *status)
3413 if (!type_check (path1, 0, BT_CHARACTER))
3414 return false;
3415 if (!kind_value_check (path1, 0, gfc_default_character_kind))
3416 return false;
3418 if (!type_check (path2, 1, BT_CHARACTER))
3419 return false;
3420 if (!kind_value_check (path2, 1, gfc_default_character_kind))
3421 return false;
3423 if (status == NULL)
3424 return true;
3426 if (!type_check (status, 2, BT_INTEGER))
3427 return false;
3429 if (!scalar_check (status, 2))
3430 return false;
3432 return true;
3436 bool
3437 gfc_check_repeat (gfc_expr *x, gfc_expr *y)
3439 if (!type_check (x, 0, BT_CHARACTER))
3440 return false;
3442 if (!scalar_check (x, 0))
3443 return false;
3445 if (!type_check (y, 0, BT_INTEGER))
3446 return false;
3448 if (!scalar_check (y, 1))
3449 return false;
3451 return true;
3455 bool
3456 gfc_check_reshape (gfc_expr *source, gfc_expr *shape,
3457 gfc_expr *pad, gfc_expr *order)
3459 mpz_t size;
3460 mpz_t nelems;
3461 int shape_size;
3463 if (!array_check (source, 0))
3464 return false;
3466 if (!rank_check (shape, 1, 1))
3467 return false;
3469 if (!type_check (shape, 1, BT_INTEGER))
3470 return false;
3472 if (!gfc_array_size (shape, &size))
3474 gfc_error ("'shape' argument of 'reshape' intrinsic at %L must be an "
3475 "array of constant size", &shape->where);
3476 return false;
3479 shape_size = mpz_get_ui (size);
3480 mpz_clear (size);
3482 if (shape_size <= 0)
3484 gfc_error ("'%s' argument of '%s' intrinsic at %L is empty",
3485 gfc_current_intrinsic_arg[1]->name, gfc_current_intrinsic,
3486 &shape->where);
3487 return false;
3489 else if (shape_size > GFC_MAX_DIMENSIONS)
3491 gfc_error ("'shape' argument of 'reshape' intrinsic at %L has more "
3492 "than %d elements", &shape->where, GFC_MAX_DIMENSIONS);
3493 return false;
3495 else if (shape->expr_type == EXPR_ARRAY && gfc_is_constant_expr (shape))
3497 gfc_expr *e;
3498 int i, extent;
3499 for (i = 0; i < shape_size; ++i)
3501 e = gfc_constructor_lookup_expr (shape->value.constructor, i);
3502 if (e->expr_type != EXPR_CONSTANT)
3503 continue;
3505 gfc_extract_int (e, &extent);
3506 if (extent < 0)
3508 gfc_error ("'%s' argument of '%s' intrinsic at %L has "
3509 "negative element (%d)",
3510 gfc_current_intrinsic_arg[1]->name,
3511 gfc_current_intrinsic, &e->where, extent);
3512 return false;
3517 if (pad != NULL)
3519 if (!same_type_check (source, 0, pad, 2))
3520 return false;
3522 if (!array_check (pad, 2))
3523 return false;
3526 if (order != NULL)
3528 if (!array_check (order, 3))
3529 return false;
3531 if (!type_check (order, 3, BT_INTEGER))
3532 return false;
3534 if (order->expr_type == EXPR_ARRAY)
3536 int i, order_size, dim, perm[GFC_MAX_DIMENSIONS];
3537 gfc_expr *e;
3539 for (i = 0; i < GFC_MAX_DIMENSIONS; ++i)
3540 perm[i] = 0;
3542 gfc_array_size (order, &size);
3543 order_size = mpz_get_ui (size);
3544 mpz_clear (size);
3546 if (order_size != shape_size)
3548 gfc_error ("'%s' argument of '%s' intrinsic at %L "
3549 "has wrong number of elements (%d/%d)",
3550 gfc_current_intrinsic_arg[3]->name,
3551 gfc_current_intrinsic, &order->where,
3552 order_size, shape_size);
3553 return false;
3556 for (i = 1; i <= order_size; ++i)
3558 e = gfc_constructor_lookup_expr (order->value.constructor, i-1);
3559 if (e->expr_type != EXPR_CONSTANT)
3560 continue;
3562 gfc_extract_int (e, &dim);
3564 if (dim < 1 || dim > order_size)
3566 gfc_error ("'%s' argument of '%s' intrinsic at %L "
3567 "has out-of-range dimension (%d)",
3568 gfc_current_intrinsic_arg[3]->name,
3569 gfc_current_intrinsic, &e->where, dim);
3570 return false;
3573 if (perm[dim-1] != 0)
3575 gfc_error ("'%s' argument of '%s' intrinsic at %L has "
3576 "invalid permutation of dimensions (dimension "
3577 "'%d' duplicated)",
3578 gfc_current_intrinsic_arg[3]->name,
3579 gfc_current_intrinsic, &e->where, dim);
3580 return false;
3583 perm[dim-1] = 1;
3588 if (pad == NULL && shape->expr_type == EXPR_ARRAY
3589 && gfc_is_constant_expr (shape)
3590 && !(source->expr_type == EXPR_VARIABLE && source->symtree->n.sym->as
3591 && source->symtree->n.sym->as->type == AS_ASSUMED_SIZE))
3593 /* Check the match in size between source and destination. */
3594 if (gfc_array_size (source, &nelems))
3596 gfc_constructor *c;
3597 bool test;
3600 mpz_init_set_ui (size, 1);
3601 for (c = gfc_constructor_first (shape->value.constructor);
3602 c; c = gfc_constructor_next (c))
3603 mpz_mul (size, size, c->expr->value.integer);
3605 test = mpz_cmp (nelems, size) < 0 && mpz_cmp_ui (size, 0) > 0;
3606 mpz_clear (nelems);
3607 mpz_clear (size);
3609 if (test)
3611 gfc_error ("Without padding, there are not enough elements "
3612 "in the intrinsic RESHAPE source at %L to match "
3613 "the shape", &source->where);
3614 return false;
3619 return true;
3623 bool
3624 gfc_check_same_type_as (gfc_expr *a, gfc_expr *b)
3626 if (a->ts.type != BT_DERIVED && a->ts.type != BT_CLASS)
3628 gfc_error ("'%s' argument of '%s' intrinsic at %L "
3629 "cannot be of type %s",
3630 gfc_current_intrinsic_arg[0]->name,
3631 gfc_current_intrinsic,
3632 &a->where, gfc_typename (&a->ts));
3633 return false;
3636 if (!(gfc_type_is_extensible (a->ts.u.derived) || UNLIMITED_POLY (a)))
3638 gfc_error ("'%s' argument of '%s' intrinsic at %L "
3639 "must be of an extensible type",
3640 gfc_current_intrinsic_arg[0]->name, gfc_current_intrinsic,
3641 &a->where);
3642 return false;
3645 if (b->ts.type != BT_DERIVED && b->ts.type != BT_CLASS)
3647 gfc_error ("'%s' argument of '%s' intrinsic at %L "
3648 "cannot be of type %s",
3649 gfc_current_intrinsic_arg[0]->name,
3650 gfc_current_intrinsic,
3651 &b->where, gfc_typename (&b->ts));
3652 return false;
3655 if (!(gfc_type_is_extensible (b->ts.u.derived) || UNLIMITED_POLY (b)))
3657 gfc_error ("'%s' argument of '%s' intrinsic at %L "
3658 "must be of an extensible type",
3659 gfc_current_intrinsic_arg[1]->name, gfc_current_intrinsic,
3660 &b->where);
3661 return false;
3664 return true;
3668 bool
3669 gfc_check_scale (gfc_expr *x, gfc_expr *i)
3671 if (!type_check (x, 0, BT_REAL))
3672 return false;
3674 if (!type_check (i, 1, BT_INTEGER))
3675 return false;
3677 return true;
3681 bool
3682 gfc_check_scan (gfc_expr *x, gfc_expr *y, gfc_expr *z, gfc_expr *kind)
3684 if (!type_check (x, 0, BT_CHARACTER))
3685 return false;
3687 if (!type_check (y, 1, BT_CHARACTER))
3688 return false;
3690 if (z != NULL && !type_check (z, 2, BT_LOGICAL))
3691 return false;
3693 if (!kind_check (kind, 3, BT_INTEGER))
3694 return false;
3695 if (kind && !gfc_notify_std (GFC_STD_F2003, "'%s' intrinsic "
3696 "with KIND argument at %L",
3697 gfc_current_intrinsic, &kind->where))
3698 return false;
3700 if (!same_type_check (x, 0, y, 1))
3701 return false;
3703 return true;
3707 bool
3708 gfc_check_secnds (gfc_expr *r)
3710 if (!type_check (r, 0, BT_REAL))
3711 return false;
3713 if (!kind_value_check (r, 0, 4))
3714 return false;
3716 if (!scalar_check (r, 0))
3717 return false;
3719 return true;
3723 bool
3724 gfc_check_selected_char_kind (gfc_expr *name)
3726 if (!type_check (name, 0, BT_CHARACTER))
3727 return false;
3729 if (!kind_value_check (name, 0, gfc_default_character_kind))
3730 return false;
3732 if (!scalar_check (name, 0))
3733 return false;
3735 return true;
3739 bool
3740 gfc_check_selected_int_kind (gfc_expr *r)
3742 if (!type_check (r, 0, BT_INTEGER))
3743 return false;
3745 if (!scalar_check (r, 0))
3746 return false;
3748 return true;
3752 bool
3753 gfc_check_selected_real_kind (gfc_expr *p, gfc_expr *r, gfc_expr *radix)
3755 if (p == NULL && r == NULL
3756 && !gfc_notify_std (GFC_STD_F2008, "SELECTED_REAL_KIND with"
3757 " neither 'P' nor 'R' argument at %L",
3758 gfc_current_intrinsic_where))
3759 return false;
3761 if (p)
3763 if (!type_check (p, 0, BT_INTEGER))
3764 return false;
3766 if (!scalar_check (p, 0))
3767 return false;
3770 if (r)
3772 if (!type_check (r, 1, BT_INTEGER))
3773 return false;
3775 if (!scalar_check (r, 1))
3776 return false;
3779 if (radix)
3781 if (!type_check (radix, 1, BT_INTEGER))
3782 return false;
3784 if (!scalar_check (radix, 1))
3785 return false;
3787 if (!gfc_notify_std (GFC_STD_F2008, "'%s' intrinsic with "
3788 "RADIX argument at %L", gfc_current_intrinsic,
3789 &radix->where))
3790 return false;
3793 return true;
3797 bool
3798 gfc_check_set_exponent (gfc_expr *x, gfc_expr *i)
3800 if (!type_check (x, 0, BT_REAL))
3801 return false;
3803 if (!type_check (i, 1, BT_INTEGER))
3804 return false;
3806 return true;
3810 bool
3811 gfc_check_shape (gfc_expr *source, gfc_expr *kind)
3813 gfc_array_ref *ar;
3815 if (source->rank == 0 || source->expr_type != EXPR_VARIABLE)
3816 return true;
3818 ar = gfc_find_array_ref (source);
3820 if (ar->as && ar->as->type == AS_ASSUMED_SIZE && ar->type == AR_FULL)
3822 gfc_error ("'source' argument of 'shape' intrinsic at %L must not be "
3823 "an assumed size array", &source->where);
3824 return false;
3827 if (!kind_check (kind, 1, BT_INTEGER))
3828 return false;
3829 if (kind && !gfc_notify_std (GFC_STD_F2003, "'%s' intrinsic "
3830 "with KIND argument at %L",
3831 gfc_current_intrinsic, &kind->where))
3832 return false;
3834 return true;
3838 bool
3839 gfc_check_shift (gfc_expr *i, gfc_expr *shift)
3841 if (!type_check (i, 0, BT_INTEGER))
3842 return false;
3844 if (!type_check (shift, 0, BT_INTEGER))
3845 return false;
3847 if (!nonnegative_check ("SHIFT", shift))
3848 return false;
3850 if (!less_than_bitsize1 ("I", i, "SHIFT", shift, true))
3851 return false;
3853 return true;
3857 bool
3858 gfc_check_sign (gfc_expr *a, gfc_expr *b)
3860 if (!int_or_real_check (a, 0))
3861 return false;
3863 if (!same_type_check (a, 0, b, 1))
3864 return false;
3866 return true;
3870 bool
3871 gfc_check_size (gfc_expr *array, gfc_expr *dim, gfc_expr *kind)
3873 if (!array_check (array, 0))
3874 return false;
3876 if (!dim_check (dim, 1, true))
3877 return false;
3879 if (!dim_rank_check (dim, array, 0))
3880 return false;
3882 if (!kind_check (kind, 2, BT_INTEGER))
3883 return false;
3884 if (kind && !gfc_notify_std (GFC_STD_F2003, "'%s' intrinsic "
3885 "with KIND argument at %L",
3886 gfc_current_intrinsic, &kind->where))
3887 return false;
3890 return true;
3894 bool
3895 gfc_check_sizeof (gfc_expr *arg)
3897 if (arg->ts.type == BT_PROCEDURE)
3899 gfc_error ("'%s' argument of '%s' intrinsic at %L shall not be a procedure",
3900 gfc_current_intrinsic_arg[0]->name, gfc_current_intrinsic,
3901 &arg->where);
3902 return false;
3905 /* TYPE(*) is acceptable if and only if it uses an array descriptor. */
3906 if (arg->ts.type == BT_ASSUMED
3907 && (arg->symtree->n.sym->as == NULL
3908 || (arg->symtree->n.sym->as->type != AS_ASSUMED_SHAPE
3909 && arg->symtree->n.sym->as->type != AS_DEFERRED
3910 && arg->symtree->n.sym->as->type != AS_ASSUMED_RANK)))
3912 gfc_error ("'%s' argument of '%s' intrinsic at %L shall not be TYPE(*)",
3913 gfc_current_intrinsic_arg[0]->name, gfc_current_intrinsic,
3914 &arg->where);
3915 return false;
3918 if (arg->rank && arg->expr_type == EXPR_VARIABLE
3919 && arg->symtree->n.sym->as != NULL
3920 && arg->symtree->n.sym->as->type == AS_ASSUMED_SIZE && arg->ref
3921 && arg->ref->type == REF_ARRAY && arg->ref->u.ar.type == AR_FULL)
3923 gfc_error ("'%s' argument of '%s' intrinsic at %L shall not be an "
3924 "assumed-size array", gfc_current_intrinsic_arg[0]->name,
3925 gfc_current_intrinsic, &arg->where);
3926 return false;
3929 return true;
3933 /* Check whether an expression is interoperable. When returning false,
3934 msg is set to a string telling why the expression is not interoperable,
3935 otherwise, it is set to NULL. The msg string can be used in diagnostics.
3936 If c_loc is true, character with len > 1 are allowed (cf. Fortran
3937 2003corr5); additionally, assumed-shape/assumed-rank/deferred-shape
3938 arrays are permitted. And if c_f_ptr is true, deferred-shape arrays
3939 are permitted. */
3941 static bool
3942 is_c_interoperable (gfc_expr *expr, const char **msg, bool c_loc, bool c_f_ptr)
3944 *msg = NULL;
3946 if (expr->ts.type == BT_CLASS)
3948 *msg = "Expression is polymorphic";
3949 return false;
3952 if (expr->ts.type == BT_DERIVED && !expr->ts.u.derived->attr.is_bind_c
3953 && !expr->ts.u.derived->ts.is_iso_c)
3955 *msg = "Expression is a noninteroperable derived type";
3956 return false;
3959 if (expr->ts.type == BT_PROCEDURE)
3961 *msg = "Procedure unexpected as argument";
3962 return false;
3965 if (gfc_notification_std (GFC_STD_GNU) && expr->ts.type == BT_LOGICAL)
3967 int i;
3968 for (i = 0; gfc_logical_kinds[i].kind; i++)
3969 if (gfc_logical_kinds[i].kind == expr->ts.kind)
3970 return true;
3971 *msg = "Extension to use a non-C_Bool-kind LOGICAL";
3972 return false;
3975 if (gfc_notification_std (GFC_STD_GNU) && expr->ts.type == BT_CHARACTER
3976 && expr->ts.kind != 1)
3978 *msg = "Extension to use a non-C_CHAR-kind CHARACTER";
3979 return false;
3982 if (expr->ts.type == BT_CHARACTER) {
3983 if (expr->ts.deferred)
3985 /* TS 29113 allows deferred-length strings as dummy arguments,
3986 but it is not an interoperable type. */
3987 *msg = "Expression shall not be a deferred-length string";
3988 return false;
3991 if (expr->ts.u.cl && expr->ts.u.cl->length
3992 && !gfc_simplify_expr (expr, 0))
3993 gfc_internal_error ("is_c_interoperable(): gfc_simplify_expr failed");
3995 if (!c_loc && expr->ts.u.cl
3996 && (!expr->ts.u.cl->length
3997 || expr->ts.u.cl->length->expr_type != EXPR_CONSTANT
3998 || mpz_cmp_si (expr->ts.u.cl->length->value.integer, 1) != 0))
4000 *msg = "Type shall have a character length of 1";
4001 return false;
4005 /* Note: The following checks are about interoperatable variables, Fortran
4006 15.3.5/15.3.6. In intrinsics like C_LOC or in procedure interface, more
4007 is allowed, e.g. assumed-shape arrays with TS 29113. */
4009 if (gfc_is_coarray (expr))
4011 *msg = "Coarrays are not interoperable";
4012 return false;
4015 if (!c_loc && expr->rank > 0 && expr->expr_type != EXPR_ARRAY)
4017 gfc_array_ref *ar = gfc_find_array_ref (expr);
4018 if (ar->type != AR_FULL)
4020 *msg = "Only whole-arrays are interoperable";
4021 return false;
4023 if (!c_f_ptr && ar->as->type != AS_EXPLICIT
4024 && ar->as->type != AS_ASSUMED_SIZE)
4026 *msg = "Only explicit-size and assumed-size arrays are interoperable";
4027 return false;
4031 return true;
4035 bool
4036 gfc_check_c_sizeof (gfc_expr *arg)
4038 const char *msg;
4040 if (!is_c_interoperable (arg, &msg, false, false))
4042 gfc_error ("'%s' argument of '%s' intrinsic at %L must be an "
4043 "interoperable data entity: %s",
4044 gfc_current_intrinsic_arg[0]->name, gfc_current_intrinsic,
4045 &arg->where, msg);
4046 return false;
4049 if (arg->ts.type == BT_ASSUMED)
4051 gfc_error ("'%s' argument of '%s' intrinsic at %L shall not be "
4052 "TYPE(*)",
4053 gfc_current_intrinsic_arg[0]->name, gfc_current_intrinsic,
4054 &arg->where);
4055 return false;
4058 if (arg->rank && arg->expr_type == EXPR_VARIABLE
4059 && arg->symtree->n.sym->as != NULL
4060 && arg->symtree->n.sym->as->type == AS_ASSUMED_SIZE && arg->ref
4061 && arg->ref->type == REF_ARRAY && arg->ref->u.ar.type == AR_FULL)
4063 gfc_error ("'%s' argument of '%s' intrinsic at %L shall not be an "
4064 "assumed-size array", gfc_current_intrinsic_arg[0]->name,
4065 gfc_current_intrinsic, &arg->where);
4066 return false;
4069 return true;
4073 bool
4074 gfc_check_c_associated (gfc_expr *c_ptr_1, gfc_expr *c_ptr_2)
4076 if (c_ptr_1->ts.type != BT_DERIVED
4077 || c_ptr_1->ts.u.derived->from_intmod != INTMOD_ISO_C_BINDING
4078 || (c_ptr_1->ts.u.derived->intmod_sym_id != ISOCBINDING_PTR
4079 && c_ptr_1->ts.u.derived->intmod_sym_id != ISOCBINDING_FUNPTR))
4081 gfc_error ("Argument C_PTR_1 at %L to C_ASSOCIATED shall have the "
4082 "type TYPE(C_PTR) or TYPE(C_FUNPTR)", &c_ptr_1->where);
4083 return false;
4086 if (!scalar_check (c_ptr_1, 0))
4087 return false;
4089 if (c_ptr_2
4090 && (c_ptr_2->ts.type != BT_DERIVED
4091 || c_ptr_2->ts.u.derived->from_intmod != INTMOD_ISO_C_BINDING
4092 || (c_ptr_1->ts.u.derived->intmod_sym_id
4093 != c_ptr_2->ts.u.derived->intmod_sym_id)))
4095 gfc_error ("Argument C_PTR_2 at %L to C_ASSOCIATED shall have the "
4096 "same type as C_PTR_1: %s instead of %s", &c_ptr_1->where,
4097 gfc_typename (&c_ptr_1->ts),
4098 gfc_typename (&c_ptr_2->ts));
4099 return false;
4102 if (c_ptr_2 && !scalar_check (c_ptr_2, 1))
4103 return false;
4105 return true;
4109 bool
4110 gfc_check_c_f_pointer (gfc_expr *cptr, gfc_expr *fptr, gfc_expr *shape)
4112 symbol_attribute attr;
4113 const char *msg;
4115 if (cptr->ts.type != BT_DERIVED
4116 || cptr->ts.u.derived->from_intmod != INTMOD_ISO_C_BINDING
4117 || cptr->ts.u.derived->intmod_sym_id != ISOCBINDING_PTR)
4119 gfc_error ("Argument CPTR at %L to C_F_POINTER shall have the "
4120 "type TYPE(C_PTR)", &cptr->where);
4121 return false;
4124 if (!scalar_check (cptr, 0))
4125 return false;
4127 attr = gfc_expr_attr (fptr);
4129 if (!attr.pointer)
4131 gfc_error ("Argument FPTR at %L to C_F_POINTER must be a pointer",
4132 &fptr->where);
4133 return false;
4136 if (fptr->ts.type == BT_CLASS)
4138 gfc_error ("FPTR argument at %L to C_F_POINTER shall not be polymorphic",
4139 &fptr->where);
4140 return false;
4143 if (gfc_is_coindexed (fptr))
4145 gfc_error ("Argument FPTR at %L to C_F_POINTER shall not be "
4146 "coindexed", &fptr->where);
4147 return false;
4150 if (fptr->rank == 0 && shape)
4152 gfc_error ("Unexpected SHAPE argument at %L to C_F_POINTER with scalar "
4153 "FPTR", &fptr->where);
4154 return false;
4156 else if (fptr->rank && !shape)
4158 gfc_error ("Expected SHAPE argument to C_F_POINTER with array "
4159 "FPTR at %L", &fptr->where);
4160 return false;
4163 if (shape && !rank_check (shape, 2, 1))
4164 return false;
4166 if (shape && !type_check (shape, 2, BT_INTEGER))
4167 return false;
4169 if (shape)
4171 mpz_t size;
4172 if (gfc_array_size (shape, &size))
4174 if (mpz_cmp_ui (size, fptr->rank) != 0)
4176 mpz_clear (size);
4177 gfc_error ("SHAPE argument at %L to C_F_POINTER must have the same "
4178 "size as the RANK of FPTR", &shape->where);
4179 return false;
4181 mpz_clear (size);
4185 if (fptr->ts.type == BT_CLASS)
4187 gfc_error ("Polymorphic FPTR at %L to C_F_POINTER", &fptr->where);
4188 return false;
4191 if (!is_c_interoperable (fptr, &msg, false, true))
4192 return gfc_notify_std (GFC_STD_F2008_TS, "Noninteroperable array FPTR "
4193 "at %L to C_F_POINTER: %s", &fptr->where, msg);
4195 return true;
4199 bool
4200 gfc_check_c_f_procpointer (gfc_expr *cptr, gfc_expr *fptr)
4202 symbol_attribute attr;
4204 if (cptr->ts.type != BT_DERIVED
4205 || cptr->ts.u.derived->from_intmod != INTMOD_ISO_C_BINDING
4206 || cptr->ts.u.derived->intmod_sym_id != ISOCBINDING_FUNPTR)
4208 gfc_error ("Argument CPTR at %L to C_F_PROCPOINTER shall have the "
4209 "type TYPE(C_FUNPTR)", &cptr->where);
4210 return false;
4213 if (!scalar_check (cptr, 0))
4214 return false;
4216 attr = gfc_expr_attr (fptr);
4218 if (!attr.proc_pointer)
4220 gfc_error ("Argument FPTR at %L to C_F_PROCPOINTER shall be a procedure "
4221 "pointer", &fptr->where);
4222 return false;
4225 if (gfc_is_coindexed (fptr))
4227 gfc_error ("Argument FPTR at %L to C_F_PROCPOINTER shall not be "
4228 "coindexed", &fptr->where);
4229 return false;
4232 if (!attr.is_bind_c)
4233 return gfc_notify_std (GFC_STD_F2008_TS, "Noninteroperable procedure "
4234 "pointer at %L to C_F_PROCPOINTER", &fptr->where);
4236 return true;
4240 bool
4241 gfc_check_c_funloc (gfc_expr *x)
4243 symbol_attribute attr;
4245 if (gfc_is_coindexed (x))
4247 gfc_error ("Argument X at %L to C_FUNLOC shall not be "
4248 "coindexed", &x->where);
4249 return false;
4252 attr = gfc_expr_attr (x);
4254 if (attr.function && !attr.proc_pointer && x->expr_type == EXPR_VARIABLE
4255 && x->symtree->n.sym == x->symtree->n.sym->result)
4257 gfc_namespace *ns = gfc_current_ns;
4259 for (ns = gfc_current_ns; ns; ns = ns->parent)
4260 if (x->symtree->n.sym == ns->proc_name)
4262 gfc_error ("Function result '%s' at %L is invalid as X argument "
4263 "to C_FUNLOC", x->symtree->n.sym->name, &x->where);
4264 return false;
4268 if (attr.flavor != FL_PROCEDURE)
4270 gfc_error ("Argument X at %L to C_FUNLOC shall be a procedure "
4271 "or a procedure pointer", &x->where);
4272 return false;
4275 if (!attr.is_bind_c)
4276 return gfc_notify_std (GFC_STD_F2008_TS, "Noninteroperable procedure "
4277 "at %L to C_FUNLOC", &x->where);
4278 return true;
4282 bool
4283 gfc_check_c_loc (gfc_expr *x)
4285 symbol_attribute attr;
4286 const char *msg;
4288 if (gfc_is_coindexed (x))
4290 gfc_error ("Argument X at %L to C_LOC shall not be coindexed", &x->where);
4291 return false;
4294 if (x->ts.type == BT_CLASS)
4296 gfc_error ("X argument at %L to C_LOC shall not be polymorphic",
4297 &x->where);
4298 return false;
4301 attr = gfc_expr_attr (x);
4303 if (!attr.pointer
4304 && (x->expr_type != EXPR_VARIABLE || !attr.target
4305 || attr.flavor == FL_PARAMETER))
4307 gfc_error ("Argument X at %L to C_LOC shall have either "
4308 "the POINTER or the TARGET attribute", &x->where);
4309 return false;
4312 if (x->ts.type == BT_CHARACTER
4313 && gfc_var_strlen (x) == 0)
4315 gfc_error ("Argument X at %L to C_LOC shall be not be a zero-sized "
4316 "string", &x->where);
4317 return false;
4320 if (!is_c_interoperable (x, &msg, true, false))
4322 if (x->ts.type == BT_CLASS)
4324 gfc_error ("Argument at %L to C_LOC shall not be polymorphic",
4325 &x->where);
4326 return false;
4329 if (x->rank
4330 && !gfc_notify_std (GFC_STD_F2008_TS,
4331 "Noninteroperable array at %L as"
4332 " argument to C_LOC: %s", &x->where, msg))
4333 return false;
4335 else if (x->rank > 0 && gfc_notification_std (GFC_STD_F2008))
4337 gfc_array_ref *ar = gfc_find_array_ref (x);
4339 if (ar->as->type != AS_EXPLICIT && ar->as->type != AS_ASSUMED_SIZE
4340 && !attr.allocatable
4341 && !gfc_notify_std (GFC_STD_F2008,
4342 "Array of interoperable type at %L "
4343 "to C_LOC which is nonallocatable and neither "
4344 "assumed size nor explicit size", &x->where))
4345 return false;
4346 else if (ar->type != AR_FULL
4347 && !gfc_notify_std (GFC_STD_F2008, "Array section at %L "
4348 "to C_LOC", &x->where))
4349 return false;
4352 return true;
4356 bool
4357 gfc_check_sleep_sub (gfc_expr *seconds)
4359 if (!type_check (seconds, 0, BT_INTEGER))
4360 return false;
4362 if (!scalar_check (seconds, 0))
4363 return false;
4365 return true;
4368 bool
4369 gfc_check_sngl (gfc_expr *a)
4371 if (!type_check (a, 0, BT_REAL))
4372 return false;
4374 if ((a->ts.kind != gfc_default_double_kind)
4375 && !gfc_notify_std (GFC_STD_GNU, "non double precision "
4376 "REAL argument to %s intrinsic at %L",
4377 gfc_current_intrinsic, &a->where))
4378 return false;
4380 return true;
4383 bool
4384 gfc_check_spread (gfc_expr *source, gfc_expr *dim, gfc_expr *ncopies)
4386 if (source->rank >= GFC_MAX_DIMENSIONS)
4388 gfc_error ("'%s' argument of '%s' intrinsic at %L must be less "
4389 "than rank %d", gfc_current_intrinsic_arg[0]->name,
4390 gfc_current_intrinsic, &source->where, GFC_MAX_DIMENSIONS);
4392 return false;
4395 if (dim == NULL)
4396 return false;
4398 if (!dim_check (dim, 1, false))
4399 return false;
4401 /* dim_rank_check() does not apply here. */
4402 if (dim
4403 && dim->expr_type == EXPR_CONSTANT
4404 && (mpz_cmp_ui (dim->value.integer, 1) < 0
4405 || mpz_cmp_ui (dim->value.integer, source->rank + 1) > 0))
4407 gfc_error ("'%s' argument of '%s' intrinsic at %L is not a valid "
4408 "dimension index", gfc_current_intrinsic_arg[1]->name,
4409 gfc_current_intrinsic, &dim->where);
4410 return false;
4413 if (!type_check (ncopies, 2, BT_INTEGER))
4414 return false;
4416 if (!scalar_check (ncopies, 2))
4417 return false;
4419 return true;
4423 /* Functions for checking FGETC, FPUTC, FGET and FPUT (subroutines and
4424 functions). */
4426 bool
4427 gfc_check_fgetputc_sub (gfc_expr *unit, gfc_expr *c, gfc_expr *status)
4429 if (!type_check (unit, 0, BT_INTEGER))
4430 return false;
4432 if (!scalar_check (unit, 0))
4433 return false;
4435 if (!type_check (c, 1, BT_CHARACTER))
4436 return false;
4437 if (!kind_value_check (c, 1, gfc_default_character_kind))
4438 return false;
4440 if (status == NULL)
4441 return true;
4443 if (!type_check (status, 2, BT_INTEGER)
4444 || !kind_value_check (status, 2, gfc_default_integer_kind)
4445 || !scalar_check (status, 2))
4446 return false;
4448 return true;
4452 bool
4453 gfc_check_fgetputc (gfc_expr *unit, gfc_expr *c)
4455 return gfc_check_fgetputc_sub (unit, c, NULL);
4459 bool
4460 gfc_check_fgetput_sub (gfc_expr *c, gfc_expr *status)
4462 if (!type_check (c, 0, BT_CHARACTER))
4463 return false;
4464 if (!kind_value_check (c, 0, gfc_default_character_kind))
4465 return false;
4467 if (status == NULL)
4468 return true;
4470 if (!type_check (status, 1, BT_INTEGER)
4471 || !kind_value_check (status, 1, gfc_default_integer_kind)
4472 || !scalar_check (status, 1))
4473 return false;
4475 return true;
4479 bool
4480 gfc_check_fgetput (gfc_expr *c)
4482 return gfc_check_fgetput_sub (c, NULL);
4486 bool
4487 gfc_check_fseek_sub (gfc_expr *unit, gfc_expr *offset, gfc_expr *whence, gfc_expr *status)
4489 if (!type_check (unit, 0, BT_INTEGER))
4490 return false;
4492 if (!scalar_check (unit, 0))
4493 return false;
4495 if (!type_check (offset, 1, BT_INTEGER))
4496 return false;
4498 if (!scalar_check (offset, 1))
4499 return false;
4501 if (!type_check (whence, 2, BT_INTEGER))
4502 return false;
4504 if (!scalar_check (whence, 2))
4505 return false;
4507 if (status == NULL)
4508 return true;
4510 if (!type_check (status, 3, BT_INTEGER))
4511 return false;
4513 if (!kind_value_check (status, 3, 4))
4514 return false;
4516 if (!scalar_check (status, 3))
4517 return false;
4519 return true;
4524 bool
4525 gfc_check_fstat (gfc_expr *unit, gfc_expr *array)
4527 if (!type_check (unit, 0, BT_INTEGER))
4528 return false;
4530 if (!scalar_check (unit, 0))
4531 return false;
4533 if (!type_check (array, 1, BT_INTEGER)
4534 || !kind_value_check (unit, 0, gfc_default_integer_kind))
4535 return false;
4537 if (!array_check (array, 1))
4538 return false;
4540 return true;
4544 bool
4545 gfc_check_fstat_sub (gfc_expr *unit, gfc_expr *array, gfc_expr *status)
4547 if (!type_check (unit, 0, BT_INTEGER))
4548 return false;
4550 if (!scalar_check (unit, 0))
4551 return false;
4553 if (!type_check (array, 1, BT_INTEGER)
4554 || !kind_value_check (array, 1, gfc_default_integer_kind))
4555 return false;
4557 if (!array_check (array, 1))
4558 return false;
4560 if (status == NULL)
4561 return true;
4563 if (!type_check (status, 2, BT_INTEGER)
4564 || !kind_value_check (status, 2, gfc_default_integer_kind))
4565 return false;
4567 if (!scalar_check (status, 2))
4568 return false;
4570 return true;
4574 bool
4575 gfc_check_ftell (gfc_expr *unit)
4577 if (!type_check (unit, 0, BT_INTEGER))
4578 return false;
4580 if (!scalar_check (unit, 0))
4581 return false;
4583 return true;
4587 bool
4588 gfc_check_ftell_sub (gfc_expr *unit, gfc_expr *offset)
4590 if (!type_check (unit, 0, BT_INTEGER))
4591 return false;
4593 if (!scalar_check (unit, 0))
4594 return false;
4596 if (!type_check (offset, 1, BT_INTEGER))
4597 return false;
4599 if (!scalar_check (offset, 1))
4600 return false;
4602 return true;
4606 bool
4607 gfc_check_stat (gfc_expr *name, gfc_expr *array)
4609 if (!type_check (name, 0, BT_CHARACTER))
4610 return false;
4611 if (!kind_value_check (name, 0, gfc_default_character_kind))
4612 return false;
4614 if (!type_check (array, 1, BT_INTEGER)
4615 || !kind_value_check (array, 1, gfc_default_integer_kind))
4616 return false;
4618 if (!array_check (array, 1))
4619 return false;
4621 return true;
4625 bool
4626 gfc_check_stat_sub (gfc_expr *name, gfc_expr *array, gfc_expr *status)
4628 if (!type_check (name, 0, BT_CHARACTER))
4629 return false;
4630 if (!kind_value_check (name, 0, gfc_default_character_kind))
4631 return false;
4633 if (!type_check (array, 1, BT_INTEGER)
4634 || !kind_value_check (array, 1, gfc_default_integer_kind))
4635 return false;
4637 if (!array_check (array, 1))
4638 return false;
4640 if (status == NULL)
4641 return true;
4643 if (!type_check (status, 2, BT_INTEGER)
4644 || !kind_value_check (array, 1, gfc_default_integer_kind))
4645 return false;
4647 if (!scalar_check (status, 2))
4648 return false;
4650 return true;
4654 bool
4655 gfc_check_image_index (gfc_expr *coarray, gfc_expr *sub)
4657 mpz_t nelems;
4659 if (gfc_option.coarray == GFC_FCOARRAY_NONE)
4661 gfc_fatal_error ("Coarrays disabled at %C, use -fcoarray= to enable");
4662 return false;
4665 if (!coarray_check (coarray, 0))
4666 return false;
4668 if (sub->rank != 1)
4670 gfc_error ("%s argument to IMAGE_INDEX must be a rank one array at %L",
4671 gfc_current_intrinsic_arg[1]->name, &sub->where);
4672 return false;
4675 if (gfc_array_size (sub, &nelems))
4677 int corank = gfc_get_corank (coarray);
4679 if (mpz_cmp_ui (nelems, corank) != 0)
4681 gfc_error ("The number of array elements of the SUB argument to "
4682 "IMAGE_INDEX at %L shall be %d (corank) not %d",
4683 &sub->where, corank, (int) mpz_get_si (nelems));
4684 mpz_clear (nelems);
4685 return false;
4687 mpz_clear (nelems);
4690 return true;
4694 bool
4695 gfc_check_num_images (gfc_expr *distance, gfc_expr *failed)
4697 if (gfc_option.coarray == GFC_FCOARRAY_NONE)
4699 gfc_fatal_error ("Coarrays disabled at %C, use -fcoarray= to enable");
4700 return false;
4703 if (distance)
4705 if (!type_check (distance, 0, BT_INTEGER))
4706 return false;
4708 if (!nonnegative_check ("DISTANCE", distance))
4709 return false;
4711 if (!scalar_check (distance, 0))
4712 return false;
4714 if (!gfc_notify_std (GFC_STD_F2008_TS, "DISTANCE= argument to "
4715 "NUM_IMAGES at %L", &distance->where))
4716 return false;
4719 if (failed)
4721 if (!type_check (failed, 1, BT_LOGICAL))
4722 return false;
4724 if (!scalar_check (failed, 1))
4725 return false;
4727 if (!gfc_notify_std (GFC_STD_F2008_TS, "FAILED= argument to "
4728 "NUM_IMAGES at %L", &distance->where))
4729 return false;
4732 return true;
4736 bool
4737 gfc_check_this_image (gfc_expr *coarray, gfc_expr *dim, gfc_expr *distance)
4739 if (gfc_option.coarray == GFC_FCOARRAY_NONE)
4741 gfc_fatal_error ("Coarrays disabled at %C, use -fcoarray= to enable");
4742 return false;
4745 if (coarray == NULL && dim == NULL && distance == NULL)
4746 return true;
4748 if (dim != NULL && coarray == NULL)
4750 gfc_error ("DIM argument without COARRAY argument not allowed for "
4751 "THIS_IMAGE intrinsic at %L", &dim->where);
4752 return false;
4755 if (distance && (coarray || dim))
4757 gfc_error ("The DISTANCE argument may not be specified together with the "
4758 "COARRAY or DIM argument in intrinsic at %L",
4759 &distance->where);
4760 return false;
4763 /* Assume that we have "this_image (distance)". */
4764 if (coarray && !gfc_is_coarray (coarray) && coarray->ts.type == BT_INTEGER)
4766 if (dim)
4768 gfc_error ("Unexpected DIM argument with noncoarray argument at %L",
4769 &coarray->where);
4770 return false;
4772 distance = coarray;
4775 if (distance)
4777 if (!type_check (distance, 2, BT_INTEGER))
4778 return false;
4780 if (!nonnegative_check ("DISTANCE", distance))
4781 return false;
4783 if (!scalar_check (distance, 2))
4784 return false;
4786 if (!gfc_notify_std (GFC_STD_F2008_TS, "DISTANCE= argument to "
4787 "THIS_IMAGE at %L", &distance->where))
4788 return false;
4790 return true;
4793 if (!coarray_check (coarray, 0))
4794 return false;
4796 if (dim != NULL)
4798 if (!dim_check (dim, 1, false))
4799 return false;
4801 if (!dim_corank_check (dim, coarray))
4802 return false;
4805 return true;
4808 /* Calculate the sizes for transfer, used by gfc_check_transfer and also
4809 by gfc_simplify_transfer. Return false if we cannot do so. */
4811 bool
4812 gfc_calculate_transfer_sizes (gfc_expr *source, gfc_expr *mold, gfc_expr *size,
4813 size_t *source_size, size_t *result_size,
4814 size_t *result_length_p)
4816 size_t result_elt_size;
4818 if (source->expr_type == EXPR_FUNCTION)
4819 return false;
4821 if (size && size->expr_type != EXPR_CONSTANT)
4822 return false;
4824 /* Calculate the size of the source. */
4825 *source_size = gfc_target_expr_size (source);
4826 if (*source_size == 0)
4827 return false;
4829 /* Determine the size of the element. */
4830 result_elt_size = gfc_element_size (mold);
4831 if (result_elt_size == 0)
4832 return false;
4834 if (mold->expr_type == EXPR_ARRAY || mold->rank || size)
4836 int result_length;
4838 if (size)
4839 result_length = (size_t)mpz_get_ui (size->value.integer);
4840 else
4842 result_length = *source_size / result_elt_size;
4843 if (result_length * result_elt_size < *source_size)
4844 result_length += 1;
4847 *result_size = result_length * result_elt_size;
4848 if (result_length_p)
4849 *result_length_p = result_length;
4851 else
4852 *result_size = result_elt_size;
4854 return true;
4858 bool
4859 gfc_check_transfer (gfc_expr *source, gfc_expr *mold, gfc_expr *size)
4861 size_t source_size;
4862 size_t result_size;
4864 if (mold->ts.type == BT_HOLLERITH)
4866 gfc_error ("'MOLD' argument of 'TRANSFER' intrinsic at %L must not be %s",
4867 &mold->where, gfc_basic_typename (BT_HOLLERITH));
4868 return false;
4871 if (size != NULL)
4873 if (!type_check (size, 2, BT_INTEGER))
4874 return false;
4876 if (!scalar_check (size, 2))
4877 return false;
4879 if (!nonoptional_check (size, 2))
4880 return false;
4883 if (!gfc_option.warn_surprising)
4884 return true;
4886 /* If we can't calculate the sizes, we cannot check any more.
4887 Return true for that case. */
4889 if (!gfc_calculate_transfer_sizes (source, mold, size, &source_size,
4890 &result_size, NULL))
4891 return true;
4893 if (source_size < result_size)
4894 gfc_warning("Intrinsic TRANSFER at %L has partly undefined result: "
4895 "source size %ld < result size %ld", &source->where,
4896 (long) source_size, (long) result_size);
4898 return true;
4902 bool
4903 gfc_check_transpose (gfc_expr *matrix)
4905 if (!rank_check (matrix, 0, 2))
4906 return false;
4908 return true;
4912 bool
4913 gfc_check_ubound (gfc_expr *array, gfc_expr *dim, gfc_expr *kind)
4915 if (!array_check (array, 0))
4916 return false;
4918 if (!dim_check (dim, 1, false))
4919 return false;
4921 if (!dim_rank_check (dim, array, 0))
4922 return false;
4924 if (!kind_check (kind, 2, BT_INTEGER))
4925 return false;
4926 if (kind && !gfc_notify_std (GFC_STD_F2003, "'%s' intrinsic "
4927 "with KIND argument at %L",
4928 gfc_current_intrinsic, &kind->where))
4929 return false;
4931 return true;
4935 bool
4936 gfc_check_ucobound (gfc_expr *coarray, gfc_expr *dim, gfc_expr *kind)
4938 if (gfc_option.coarray == GFC_FCOARRAY_NONE)
4940 gfc_fatal_error ("Coarrays disabled at %C, use -fcoarray= to enable");
4941 return false;
4944 if (!coarray_check (coarray, 0))
4945 return false;
4947 if (dim != NULL)
4949 if (!dim_check (dim, 1, false))
4950 return false;
4952 if (!dim_corank_check (dim, coarray))
4953 return false;
4956 if (!kind_check (kind, 2, BT_INTEGER))
4957 return false;
4959 return true;
4963 bool
4964 gfc_check_unpack (gfc_expr *vector, gfc_expr *mask, gfc_expr *field)
4966 mpz_t vector_size;
4968 if (!rank_check (vector, 0, 1))
4969 return false;
4971 if (!array_check (mask, 1))
4972 return false;
4974 if (!type_check (mask, 1, BT_LOGICAL))
4975 return false;
4977 if (!same_type_check (vector, 0, field, 2))
4978 return false;
4980 if (mask->expr_type == EXPR_ARRAY
4981 && gfc_array_size (vector, &vector_size))
4983 int mask_true_count = 0;
4984 gfc_constructor *mask_ctor;
4985 mask_ctor = gfc_constructor_first (mask->value.constructor);
4986 while (mask_ctor)
4988 if (mask_ctor->expr->expr_type != EXPR_CONSTANT)
4990 mask_true_count = 0;
4991 break;
4994 if (mask_ctor->expr->value.logical)
4995 mask_true_count++;
4997 mask_ctor = gfc_constructor_next (mask_ctor);
5000 if (mpz_get_si (vector_size) < mask_true_count)
5002 gfc_error ("'%s' argument of '%s' intrinsic at %L must "
5003 "provide at least as many elements as there "
5004 "are .TRUE. values in '%s' (%ld/%d)",
5005 gfc_current_intrinsic_arg[0]->name, gfc_current_intrinsic,
5006 &vector->where, gfc_current_intrinsic_arg[1]->name,
5007 mpz_get_si (vector_size), mask_true_count);
5008 return false;
5011 mpz_clear (vector_size);
5014 if (mask->rank != field->rank && field->rank != 0)
5016 gfc_error ("'%s' argument of '%s' intrinsic at %L must have "
5017 "the same rank as '%s' or be a scalar",
5018 gfc_current_intrinsic_arg[2]->name, gfc_current_intrinsic,
5019 &field->where, gfc_current_intrinsic_arg[1]->name);
5020 return false;
5023 if (mask->rank == field->rank)
5025 int i;
5026 for (i = 0; i < field->rank; i++)
5027 if (! identical_dimen_shape (mask, i, field, i))
5029 gfc_error ("'%s' and '%s' arguments of '%s' intrinsic at %L "
5030 "must have identical shape.",
5031 gfc_current_intrinsic_arg[2]->name,
5032 gfc_current_intrinsic_arg[1]->name, gfc_current_intrinsic,
5033 &field->where);
5037 return true;
5041 bool
5042 gfc_check_verify (gfc_expr *x, gfc_expr *y, gfc_expr *z, gfc_expr *kind)
5044 if (!type_check (x, 0, BT_CHARACTER))
5045 return false;
5047 if (!same_type_check (x, 0, y, 1))
5048 return false;
5050 if (z != NULL && !type_check (z, 2, BT_LOGICAL))
5051 return false;
5053 if (!kind_check (kind, 3, BT_INTEGER))
5054 return false;
5055 if (kind && !gfc_notify_std (GFC_STD_F2003, "'%s' intrinsic "
5056 "with KIND argument at %L",
5057 gfc_current_intrinsic, &kind->where))
5058 return false;
5060 return true;
5064 bool
5065 gfc_check_trim (gfc_expr *x)
5067 if (!type_check (x, 0, BT_CHARACTER))
5068 return false;
5070 if (!scalar_check (x, 0))
5071 return false;
5073 return true;
5077 bool
5078 gfc_check_ttynam (gfc_expr *unit)
5080 if (!scalar_check (unit, 0))
5081 return false;
5083 if (!type_check (unit, 0, BT_INTEGER))
5084 return false;
5086 return true;
5090 /* Common check function for the half a dozen intrinsics that have a
5091 single real argument. */
5093 bool
5094 gfc_check_x (gfc_expr *x)
5096 if (!type_check (x, 0, BT_REAL))
5097 return false;
5099 return true;
5103 /************* Check functions for intrinsic subroutines *************/
5105 bool
5106 gfc_check_cpu_time (gfc_expr *time)
5108 if (!scalar_check (time, 0))
5109 return false;
5111 if (!type_check (time, 0, BT_REAL))
5112 return false;
5114 if (!variable_check (time, 0, false))
5115 return false;
5117 return true;
5121 bool
5122 gfc_check_date_and_time (gfc_expr *date, gfc_expr *time,
5123 gfc_expr *zone, gfc_expr *values)
5125 if (date != NULL)
5127 if (!type_check (date, 0, BT_CHARACTER))
5128 return false;
5129 if (!kind_value_check (date, 0, gfc_default_character_kind))
5130 return false;
5131 if (!scalar_check (date, 0))
5132 return false;
5133 if (!variable_check (date, 0, false))
5134 return false;
5137 if (time != NULL)
5139 if (!type_check (time, 1, BT_CHARACTER))
5140 return false;
5141 if (!kind_value_check (time, 1, gfc_default_character_kind))
5142 return false;
5143 if (!scalar_check (time, 1))
5144 return false;
5145 if (!variable_check (time, 1, false))
5146 return false;
5149 if (zone != NULL)
5151 if (!type_check (zone, 2, BT_CHARACTER))
5152 return false;
5153 if (!kind_value_check (zone, 2, gfc_default_character_kind))
5154 return false;
5155 if (!scalar_check (zone, 2))
5156 return false;
5157 if (!variable_check (zone, 2, false))
5158 return false;
5161 if (values != NULL)
5163 if (!type_check (values, 3, BT_INTEGER))
5164 return false;
5165 if (!array_check (values, 3))
5166 return false;
5167 if (!rank_check (values, 3, 1))
5168 return false;
5169 if (!variable_check (values, 3, false))
5170 return false;
5173 return true;
5177 bool
5178 gfc_check_mvbits (gfc_expr *from, gfc_expr *frompos, gfc_expr *len,
5179 gfc_expr *to, gfc_expr *topos)
5181 if (!type_check (from, 0, BT_INTEGER))
5182 return false;
5184 if (!type_check (frompos, 1, BT_INTEGER))
5185 return false;
5187 if (!type_check (len, 2, BT_INTEGER))
5188 return false;
5190 if (!same_type_check (from, 0, to, 3))
5191 return false;
5193 if (!variable_check (to, 3, false))
5194 return false;
5196 if (!type_check (topos, 4, BT_INTEGER))
5197 return false;
5199 if (!nonnegative_check ("frompos", frompos))
5200 return false;
5202 if (!nonnegative_check ("topos", topos))
5203 return false;
5205 if (!nonnegative_check ("len", len))
5206 return false;
5208 if (!less_than_bitsize2 ("from", from, "frompos", frompos, "len", len))
5209 return false;
5211 if (!less_than_bitsize2 ("to", to, "topos", topos, "len", len))
5212 return false;
5214 return true;
5218 bool
5219 gfc_check_random_number (gfc_expr *harvest)
5221 if (!type_check (harvest, 0, BT_REAL))
5222 return false;
5224 if (!variable_check (harvest, 0, false))
5225 return false;
5227 return true;
5231 bool
5232 gfc_check_random_seed (gfc_expr *size, gfc_expr *put, gfc_expr *get)
5234 unsigned int nargs = 0, kiss_size;
5235 locus *where = NULL;
5236 mpz_t put_size, get_size;
5237 bool have_gfc_real_16; /* Try and mimic HAVE_GFC_REAL_16 in libgfortran. */
5239 have_gfc_real_16 = gfc_validate_kind (BT_REAL, 16, true) != -1;
5241 /* Keep the number of bytes in sync with kiss_size in
5242 libgfortran/intrinsics/random.c. */
5243 kiss_size = (have_gfc_real_16 ? 48 : 32) / gfc_default_integer_kind;
5245 if (size != NULL)
5247 if (size->expr_type != EXPR_VARIABLE
5248 || !size->symtree->n.sym->attr.optional)
5249 nargs++;
5251 if (!scalar_check (size, 0))
5252 return false;
5254 if (!type_check (size, 0, BT_INTEGER))
5255 return false;
5257 if (!variable_check (size, 0, false))
5258 return false;
5260 if (!kind_value_check (size, 0, gfc_default_integer_kind))
5261 return false;
5264 if (put != NULL)
5266 if (put->expr_type != EXPR_VARIABLE
5267 || !put->symtree->n.sym->attr.optional)
5269 nargs++;
5270 where = &put->where;
5273 if (!array_check (put, 1))
5274 return false;
5276 if (!rank_check (put, 1, 1))
5277 return false;
5279 if (!type_check (put, 1, BT_INTEGER))
5280 return false;
5282 if (!kind_value_check (put, 1, gfc_default_integer_kind))
5283 return false;
5285 if (gfc_array_size (put, &put_size)
5286 && mpz_get_ui (put_size) < kiss_size)
5287 gfc_error ("Size of '%s' argument of '%s' intrinsic at %L "
5288 "too small (%i/%i)",
5289 gfc_current_intrinsic_arg[1]->name, gfc_current_intrinsic,
5290 where, (int) mpz_get_ui (put_size), kiss_size);
5293 if (get != NULL)
5295 if (get->expr_type != EXPR_VARIABLE
5296 || !get->symtree->n.sym->attr.optional)
5298 nargs++;
5299 where = &get->where;
5302 if (!array_check (get, 2))
5303 return false;
5305 if (!rank_check (get, 2, 1))
5306 return false;
5308 if (!type_check (get, 2, BT_INTEGER))
5309 return false;
5311 if (!variable_check (get, 2, false))
5312 return false;
5314 if (!kind_value_check (get, 2, gfc_default_integer_kind))
5315 return false;
5317 if (gfc_array_size (get, &get_size)
5318 && mpz_get_ui (get_size) < kiss_size)
5319 gfc_error ("Size of '%s' argument of '%s' intrinsic at %L "
5320 "too small (%i/%i)",
5321 gfc_current_intrinsic_arg[2]->name, gfc_current_intrinsic,
5322 where, (int) mpz_get_ui (get_size), kiss_size);
5325 /* RANDOM_SEED may not have more than one non-optional argument. */
5326 if (nargs > 1)
5327 gfc_error ("Too many arguments to %s at %L", gfc_current_intrinsic, where);
5329 return true;
5333 bool
5334 gfc_check_second_sub (gfc_expr *time)
5336 if (!scalar_check (time, 0))
5337 return false;
5339 if (!type_check (time, 0, BT_REAL))
5340 return false;
5342 if (!kind_value_check (time, 0, 4))
5343 return false;
5345 return true;
5349 /* COUNT and COUNT_MAX of SYSTEM_CLOCK are scalar, default-kind integer
5350 variables in Fortran 95. In Fortran 2003 and later, they can be of any
5351 kind, and COUNT_RATE can be of type real. Note, count, count_rate, and
5352 count_max are all optional arguments */
5354 bool
5355 gfc_check_system_clock (gfc_expr *count, gfc_expr *count_rate,
5356 gfc_expr *count_max)
5358 if (count != NULL)
5360 if (!scalar_check (count, 0))
5361 return false;
5363 if (!type_check (count, 0, BT_INTEGER))
5364 return false;
5366 if (count->ts.kind != gfc_default_integer_kind
5367 && !gfc_notify_std (GFC_STD_F2003, "COUNT argument to "
5368 "SYSTEM_CLOCK at %L has non-default kind",
5369 &count->where))
5370 return false;
5372 if (!variable_check (count, 0, false))
5373 return false;
5376 if (count_rate != NULL)
5378 if (!scalar_check (count_rate, 1))
5379 return false;
5381 if (!variable_check (count_rate, 1, false))
5382 return false;
5384 if (count_rate->ts.type == BT_REAL)
5386 if (!gfc_notify_std (GFC_STD_F2003, "Real COUNT_RATE argument to "
5387 "SYSTEM_CLOCK at %L", &count_rate->where))
5388 return false;
5390 else
5392 if (!type_check (count_rate, 1, BT_INTEGER))
5393 return false;
5395 if (count_rate->ts.kind != gfc_default_integer_kind
5396 && !gfc_notify_std (GFC_STD_F2003, "COUNT_RATE argument to "
5397 "SYSTEM_CLOCK at %L has non-default kind",
5398 &count_rate->where))
5399 return false;
5404 if (count_max != NULL)
5406 if (!scalar_check (count_max, 2))
5407 return false;
5409 if (!type_check (count_max, 2, BT_INTEGER))
5410 return false;
5412 if (count_max->ts.kind != gfc_default_integer_kind
5413 && !gfc_notify_std (GFC_STD_F2003, "COUNT_MAX argument to "
5414 "SYSTEM_CLOCK at %L has non-default kind",
5415 &count_max->where))
5416 return false;
5418 if (!variable_check (count_max, 2, false))
5419 return false;
5422 return true;
5426 bool
5427 gfc_check_irand (gfc_expr *x)
5429 if (x == NULL)
5430 return true;
5432 if (!scalar_check (x, 0))
5433 return false;
5435 if (!type_check (x, 0, BT_INTEGER))
5436 return false;
5438 if (!kind_value_check (x, 0, 4))
5439 return false;
5441 return true;
5445 bool
5446 gfc_check_alarm_sub (gfc_expr *seconds, gfc_expr *handler, gfc_expr *status)
5448 if (!scalar_check (seconds, 0))
5449 return false;
5450 if (!type_check (seconds, 0, BT_INTEGER))
5451 return false;
5453 if (!int_or_proc_check (handler, 1))
5454 return false;
5455 if (handler->ts.type == BT_INTEGER && !scalar_check (handler, 1))
5456 return false;
5458 if (status == NULL)
5459 return true;
5461 if (!scalar_check (status, 2))
5462 return false;
5463 if (!type_check (status, 2, BT_INTEGER))
5464 return false;
5465 if (!kind_value_check (status, 2, gfc_default_integer_kind))
5466 return false;
5468 return true;
5472 bool
5473 gfc_check_rand (gfc_expr *x)
5475 if (x == NULL)
5476 return true;
5478 if (!scalar_check (x, 0))
5479 return false;
5481 if (!type_check (x, 0, BT_INTEGER))
5482 return false;
5484 if (!kind_value_check (x, 0, 4))
5485 return false;
5487 return true;
5491 bool
5492 gfc_check_srand (gfc_expr *x)
5494 if (!scalar_check (x, 0))
5495 return false;
5497 if (!type_check (x, 0, BT_INTEGER))
5498 return false;
5500 if (!kind_value_check (x, 0, 4))
5501 return false;
5503 return true;
5507 bool
5508 gfc_check_ctime_sub (gfc_expr *time, gfc_expr *result)
5510 if (!scalar_check (time, 0))
5511 return false;
5512 if (!type_check (time, 0, BT_INTEGER))
5513 return false;
5515 if (!type_check (result, 1, BT_CHARACTER))
5516 return false;
5517 if (!kind_value_check (result, 1, gfc_default_character_kind))
5518 return false;
5520 return true;
5524 bool
5525 gfc_check_dtime_etime (gfc_expr *x)
5527 if (!array_check (x, 0))
5528 return false;
5530 if (!rank_check (x, 0, 1))
5531 return false;
5533 if (!variable_check (x, 0, false))
5534 return false;
5536 if (!type_check (x, 0, BT_REAL))
5537 return false;
5539 if (!kind_value_check (x, 0, 4))
5540 return false;
5542 return true;
5546 bool
5547 gfc_check_dtime_etime_sub (gfc_expr *values, gfc_expr *time)
5549 if (!array_check (values, 0))
5550 return false;
5552 if (!rank_check (values, 0, 1))
5553 return false;
5555 if (!variable_check (values, 0, false))
5556 return false;
5558 if (!type_check (values, 0, BT_REAL))
5559 return false;
5561 if (!kind_value_check (values, 0, 4))
5562 return false;
5564 if (!scalar_check (time, 1))
5565 return false;
5567 if (!type_check (time, 1, BT_REAL))
5568 return false;
5570 if (!kind_value_check (time, 1, 4))
5571 return false;
5573 return true;
5577 bool
5578 gfc_check_fdate_sub (gfc_expr *date)
5580 if (!type_check (date, 0, BT_CHARACTER))
5581 return false;
5582 if (!kind_value_check (date, 0, gfc_default_character_kind))
5583 return false;
5585 return true;
5589 bool
5590 gfc_check_gerror (gfc_expr *msg)
5592 if (!type_check (msg, 0, BT_CHARACTER))
5593 return false;
5594 if (!kind_value_check (msg, 0, gfc_default_character_kind))
5595 return false;
5597 return true;
5601 bool
5602 gfc_check_getcwd_sub (gfc_expr *cwd, gfc_expr *status)
5604 if (!type_check (cwd, 0, BT_CHARACTER))
5605 return false;
5606 if (!kind_value_check (cwd, 0, gfc_default_character_kind))
5607 return false;
5609 if (status == NULL)
5610 return true;
5612 if (!scalar_check (status, 1))
5613 return false;
5615 if (!type_check (status, 1, BT_INTEGER))
5616 return false;
5618 return true;
5622 bool
5623 gfc_check_getarg (gfc_expr *pos, gfc_expr *value)
5625 if (!type_check (pos, 0, BT_INTEGER))
5626 return false;
5628 if (pos->ts.kind > gfc_default_integer_kind)
5630 gfc_error ("'%s' argument of '%s' intrinsic at %L must be of a kind "
5631 "not wider than the default kind (%d)",
5632 gfc_current_intrinsic_arg[0]->name, gfc_current_intrinsic,
5633 &pos->where, gfc_default_integer_kind);
5634 return false;
5637 if (!type_check (value, 1, BT_CHARACTER))
5638 return false;
5639 if (!kind_value_check (value, 1, gfc_default_character_kind))
5640 return false;
5642 return true;
5646 bool
5647 gfc_check_getlog (gfc_expr *msg)
5649 if (!type_check (msg, 0, BT_CHARACTER))
5650 return false;
5651 if (!kind_value_check (msg, 0, gfc_default_character_kind))
5652 return false;
5654 return true;
5658 bool
5659 gfc_check_exit (gfc_expr *status)
5661 if (status == NULL)
5662 return true;
5664 if (!type_check (status, 0, BT_INTEGER))
5665 return false;
5667 if (!scalar_check (status, 0))
5668 return false;
5670 return true;
5674 bool
5675 gfc_check_flush (gfc_expr *unit)
5677 if (unit == NULL)
5678 return true;
5680 if (!type_check (unit, 0, BT_INTEGER))
5681 return false;
5683 if (!scalar_check (unit, 0))
5684 return false;
5686 return true;
5690 bool
5691 gfc_check_free (gfc_expr *i)
5693 if (!type_check (i, 0, BT_INTEGER))
5694 return false;
5696 if (!scalar_check (i, 0))
5697 return false;
5699 return true;
5703 bool
5704 gfc_check_hostnm (gfc_expr *name)
5706 if (!type_check (name, 0, BT_CHARACTER))
5707 return false;
5708 if (!kind_value_check (name, 0, gfc_default_character_kind))
5709 return false;
5711 return true;
5715 bool
5716 gfc_check_hostnm_sub (gfc_expr *name, gfc_expr *status)
5718 if (!type_check (name, 0, BT_CHARACTER))
5719 return false;
5720 if (!kind_value_check (name, 0, gfc_default_character_kind))
5721 return false;
5723 if (status == NULL)
5724 return true;
5726 if (!scalar_check (status, 1))
5727 return false;
5729 if (!type_check (status, 1, BT_INTEGER))
5730 return false;
5732 return true;
5736 bool
5737 gfc_check_itime_idate (gfc_expr *values)
5739 if (!array_check (values, 0))
5740 return false;
5742 if (!rank_check (values, 0, 1))
5743 return false;
5745 if (!variable_check (values, 0, false))
5746 return false;
5748 if (!type_check (values, 0, BT_INTEGER))
5749 return false;
5751 if (!kind_value_check (values, 0, gfc_default_integer_kind))
5752 return false;
5754 return true;
5758 bool
5759 gfc_check_ltime_gmtime (gfc_expr *time, gfc_expr *values)
5761 if (!type_check (time, 0, BT_INTEGER))
5762 return false;
5764 if (!kind_value_check (time, 0, gfc_default_integer_kind))
5765 return false;
5767 if (!scalar_check (time, 0))
5768 return false;
5770 if (!array_check (values, 1))
5771 return false;
5773 if (!rank_check (values, 1, 1))
5774 return false;
5776 if (!variable_check (values, 1, false))
5777 return false;
5779 if (!type_check (values, 1, BT_INTEGER))
5780 return false;
5782 if (!kind_value_check (values, 1, gfc_default_integer_kind))
5783 return false;
5785 return true;
5789 bool
5790 gfc_check_ttynam_sub (gfc_expr *unit, gfc_expr *name)
5792 if (!scalar_check (unit, 0))
5793 return false;
5795 if (!type_check (unit, 0, BT_INTEGER))
5796 return false;
5798 if (!type_check (name, 1, BT_CHARACTER))
5799 return false;
5800 if (!kind_value_check (name, 1, gfc_default_character_kind))
5801 return false;
5803 return true;
5807 bool
5808 gfc_check_isatty (gfc_expr *unit)
5810 if (unit == NULL)
5811 return false;
5813 if (!type_check (unit, 0, BT_INTEGER))
5814 return false;
5816 if (!scalar_check (unit, 0))
5817 return false;
5819 return true;
5823 bool
5824 gfc_check_isnan (gfc_expr *x)
5826 if (!type_check (x, 0, BT_REAL))
5827 return false;
5829 return true;
5833 bool
5834 gfc_check_perror (gfc_expr *string)
5836 if (!type_check (string, 0, BT_CHARACTER))
5837 return false;
5838 if (!kind_value_check (string, 0, gfc_default_character_kind))
5839 return false;
5841 return true;
5845 bool
5846 gfc_check_umask (gfc_expr *mask)
5848 if (!type_check (mask, 0, BT_INTEGER))
5849 return false;
5851 if (!scalar_check (mask, 0))
5852 return false;
5854 return true;
5858 bool
5859 gfc_check_umask_sub (gfc_expr *mask, gfc_expr *old)
5861 if (!type_check (mask, 0, BT_INTEGER))
5862 return false;
5864 if (!scalar_check (mask, 0))
5865 return false;
5867 if (old == NULL)
5868 return true;
5870 if (!scalar_check (old, 1))
5871 return false;
5873 if (!type_check (old, 1, BT_INTEGER))
5874 return false;
5876 return true;
5880 bool
5881 gfc_check_unlink (gfc_expr *name)
5883 if (!type_check (name, 0, BT_CHARACTER))
5884 return false;
5885 if (!kind_value_check (name, 0, gfc_default_character_kind))
5886 return false;
5888 return true;
5892 bool
5893 gfc_check_unlink_sub (gfc_expr *name, gfc_expr *status)
5895 if (!type_check (name, 0, BT_CHARACTER))
5896 return false;
5897 if (!kind_value_check (name, 0, gfc_default_character_kind))
5898 return false;
5900 if (status == NULL)
5901 return true;
5903 if (!scalar_check (status, 1))
5904 return false;
5906 if (!type_check (status, 1, BT_INTEGER))
5907 return false;
5909 return true;
5913 bool
5914 gfc_check_signal (gfc_expr *number, gfc_expr *handler)
5916 if (!scalar_check (number, 0))
5917 return false;
5918 if (!type_check (number, 0, BT_INTEGER))
5919 return false;
5921 if (!int_or_proc_check (handler, 1))
5922 return false;
5923 if (handler->ts.type == BT_INTEGER && !scalar_check (handler, 1))
5924 return false;
5926 return true;
5930 bool
5931 gfc_check_signal_sub (gfc_expr *number, gfc_expr *handler, gfc_expr *status)
5933 if (!scalar_check (number, 0))
5934 return false;
5935 if (!type_check (number, 0, BT_INTEGER))
5936 return false;
5938 if (!int_or_proc_check (handler, 1))
5939 return false;
5940 if (handler->ts.type == BT_INTEGER && !scalar_check (handler, 1))
5941 return false;
5943 if (status == NULL)
5944 return true;
5946 if (!type_check (status, 2, BT_INTEGER))
5947 return false;
5948 if (!scalar_check (status, 2))
5949 return false;
5951 return true;
5955 bool
5956 gfc_check_system_sub (gfc_expr *cmd, gfc_expr *status)
5958 if (!type_check (cmd, 0, BT_CHARACTER))
5959 return false;
5960 if (!kind_value_check (cmd, 0, gfc_default_character_kind))
5961 return false;
5963 if (!scalar_check (status, 1))
5964 return false;
5966 if (!type_check (status, 1, BT_INTEGER))
5967 return false;
5969 if (!kind_value_check (status, 1, gfc_default_integer_kind))
5970 return false;
5972 return true;
5976 /* This is used for the GNU intrinsics AND, OR and XOR. */
5977 bool
5978 gfc_check_and (gfc_expr *i, gfc_expr *j)
5980 if (i->ts.type != BT_INTEGER && i->ts.type != BT_LOGICAL)
5982 gfc_error ("'%s' argument of '%s' intrinsic at %L must be INTEGER "
5983 "or LOGICAL", gfc_current_intrinsic_arg[0]->name,
5984 gfc_current_intrinsic, &i->where);
5985 return false;
5988 if (j->ts.type != BT_INTEGER && j->ts.type != BT_LOGICAL)
5990 gfc_error ("'%s' argument of '%s' intrinsic at %L must be INTEGER "
5991 "or LOGICAL", gfc_current_intrinsic_arg[1]->name,
5992 gfc_current_intrinsic, &j->where);
5993 return false;
5996 if (i->ts.type != j->ts.type)
5998 gfc_error ("'%s' and '%s' arguments of '%s' intrinsic at %L must "
5999 "have the same type", gfc_current_intrinsic_arg[0]->name,
6000 gfc_current_intrinsic_arg[1]->name, gfc_current_intrinsic,
6001 &j->where);
6002 return false;
6005 if (!scalar_check (i, 0))
6006 return false;
6008 if (!scalar_check (j, 1))
6009 return false;
6011 return true;
6015 bool
6016 gfc_check_storage_size (gfc_expr *a, gfc_expr *kind)
6018 if (a->ts.type == BT_ASSUMED)
6020 gfc_error ("'%s' argument of '%s' intrinsic at %L shall not be TYPE(*)",
6021 gfc_current_intrinsic_arg[0]->name, gfc_current_intrinsic,
6022 &a->where);
6023 return false;
6026 if (a->ts.type == BT_PROCEDURE)
6028 gfc_error ("'%s' argument of '%s' intrinsic at %L shall not be a "
6029 "procedure", gfc_current_intrinsic_arg[0]->name,
6030 gfc_current_intrinsic, &a->where);
6031 return false;
6034 if (kind == NULL)
6035 return true;
6037 if (!type_check (kind, 1, BT_INTEGER))
6038 return false;
6040 if (!scalar_check (kind, 1))
6041 return false;
6043 if (kind->expr_type != EXPR_CONSTANT)
6045 gfc_error ("'%s' argument of '%s' intrinsic at %L must be a constant",
6046 gfc_current_intrinsic_arg[1]->name, gfc_current_intrinsic,
6047 &kind->where);
6048 return false;
6051 return true;