2016-11-09 Paul Thomas <pault@gcc.gnu.org>
[official-gcc.git] / gcc / fortran / check.c
blob265fe22594f4fe56b7184b74a908c5d0b3fe1eaf
1 /* Check functions
2 Copyright (C) 2002-2016 Free Software Foundation, Inc.
3 Contributed by Andy Vaught & Katherine Holcomb
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
22 /* These functions check to see if an argument list is compatible with
23 a particular intrinsic function or subroutine. Presence of
24 required arguments has already been established, the argument list
25 has been sorted into the right order and has NULL arguments in the
26 correct places for missing optional arguments. */
28 #include "config.h"
29 #include "system.h"
30 #include "coretypes.h"
31 #include "options.h"
32 #include "gfortran.h"
33 #include "intrinsic.h"
34 #include "constructor.h"
35 #include "target-memory.h"
38 /* 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 ("%qs argument of %qs 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 ("%qs argument of %qs 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 /* Users sometime use a subroutine designator as an actual argument to
76 an intrinsic subprogram that expects an argument with a numeric type. */
77 if (e->symtree && e->symtree->n.sym->attr.subroutine)
78 goto error;
80 if (gfc_numeric_ts (&e->ts))
81 return true;
83 /* If the expression has not got a type, check if its namespace can
84 offer a default type. */
85 if ((e->expr_type == EXPR_VARIABLE || e->expr_type == EXPR_FUNCTION)
86 && e->symtree->n.sym->ts.type == BT_UNKNOWN
87 && gfc_set_default_type (e->symtree->n.sym, 0, e->symtree->n.sym->ns)
88 && gfc_numeric_ts (&e->symtree->n.sym->ts))
90 e->ts = e->symtree->n.sym->ts;
91 return true;
94 error:
96 gfc_error ("%qs argument of %qs intrinsic at %L must have a numeric type",
97 gfc_current_intrinsic_arg[n]->name, gfc_current_intrinsic,
98 &e->where);
100 return false;
104 /* Check that an expression is integer or real. */
106 static bool
107 int_or_real_check (gfc_expr *e, int n)
109 if (e->ts.type != BT_INTEGER && e->ts.type != BT_REAL)
111 gfc_error ("%qs argument of %qs intrinsic at %L must be INTEGER "
112 "or REAL", gfc_current_intrinsic_arg[n]->name,
113 gfc_current_intrinsic, &e->where);
114 return false;
117 return true;
121 /* Check that an expression is real or complex. */
123 static bool
124 real_or_complex_check (gfc_expr *e, int n)
126 if (e->ts.type != BT_REAL && e->ts.type != BT_COMPLEX)
128 gfc_error ("%qs argument of %qs intrinsic at %L must be REAL "
129 "or COMPLEX", gfc_current_intrinsic_arg[n]->name,
130 gfc_current_intrinsic, &e->where);
131 return false;
134 return true;
138 /* Check that an expression is INTEGER or PROCEDURE. */
140 static bool
141 int_or_proc_check (gfc_expr *e, int n)
143 if (e->ts.type != BT_INTEGER && e->ts.type != BT_PROCEDURE)
145 gfc_error ("%qs argument of %qs intrinsic at %L must be INTEGER "
146 "or PROCEDURE", gfc_current_intrinsic_arg[n]->name,
147 gfc_current_intrinsic, &e->where);
148 return false;
151 return true;
155 /* Check that the expression is an optional constant integer
156 and that it specifies a valid kind for that type. */
158 static bool
159 kind_check (gfc_expr *k, int n, bt type)
161 int kind;
163 if (k == NULL)
164 return true;
166 if (!type_check (k, n, BT_INTEGER))
167 return false;
169 if (!scalar_check (k, n))
170 return false;
172 if (!gfc_check_init_expr (k))
174 gfc_error ("%qs argument of %qs intrinsic at %L must be a constant",
175 gfc_current_intrinsic_arg[n]->name, gfc_current_intrinsic,
176 &k->where);
177 return false;
180 if (gfc_extract_int (k, &kind) != NULL
181 || gfc_validate_kind (type, kind, true) < 0)
183 gfc_error ("Invalid kind for %s at %L", gfc_basic_typename (type),
184 &k->where);
185 return false;
188 return true;
192 /* Make sure the expression is a double precision real. */
194 static bool
195 double_check (gfc_expr *d, int n)
197 if (!type_check (d, n, BT_REAL))
198 return false;
200 if (d->ts.kind != gfc_default_double_kind)
202 gfc_error ("%qs argument of %qs intrinsic at %L must be double "
203 "precision", gfc_current_intrinsic_arg[n]->name,
204 gfc_current_intrinsic, &d->where);
205 return false;
208 return true;
212 static bool
213 coarray_check (gfc_expr *e, int n)
215 if (e->ts.type == BT_CLASS && gfc_expr_attr (e).class_ok
216 && CLASS_DATA (e)->attr.codimension
217 && CLASS_DATA (e)->as->corank)
219 gfc_add_class_array_ref (e);
220 return true;
223 if (!gfc_is_coarray (e))
225 gfc_error ("Expected coarray variable as %qs argument to the %s "
226 "intrinsic at %L", gfc_current_intrinsic_arg[n]->name,
227 gfc_current_intrinsic, &e->where);
228 return false;
231 return true;
235 /* Make sure the expression is a logical array. */
237 static bool
238 logical_array_check (gfc_expr *array, int n)
240 if (array->ts.type != BT_LOGICAL || array->rank == 0)
242 gfc_error ("%qs argument of %qs intrinsic at %L must be a logical "
243 "array", gfc_current_intrinsic_arg[n]->name,
244 gfc_current_intrinsic, &array->where);
245 return false;
248 return true;
252 /* Make sure an expression is an array. */
254 static bool
255 array_check (gfc_expr *e, int n)
257 if (e->ts.type == BT_CLASS && gfc_expr_attr (e).class_ok
258 && CLASS_DATA (e)->attr.dimension
259 && CLASS_DATA (e)->as->rank)
261 gfc_add_class_array_ref (e);
262 return true;
265 if (e->rank != 0 && e->ts.type != BT_PROCEDURE)
266 return true;
268 gfc_error ("%qs argument of %qs intrinsic at %L must be an array",
269 gfc_current_intrinsic_arg[n]->name, gfc_current_intrinsic,
270 &e->where);
272 return false;
276 /* If expr is a constant, then check to ensure that it is greater than
277 of equal to zero. */
279 static bool
280 nonnegative_check (const char *arg, gfc_expr *expr)
282 int i;
284 if (expr->expr_type == EXPR_CONSTANT)
286 gfc_extract_int (expr, &i);
287 if (i < 0)
289 gfc_error ("%qs at %L must be nonnegative", arg, &expr->where);
290 return false;
294 return true;
298 /* If expr2 is constant, then check that the value is less than
299 (less than or equal to, if 'or_equal' is true) bit_size(expr1). */
301 static bool
302 less_than_bitsize1 (const char *arg1, gfc_expr *expr1, const char *arg2,
303 gfc_expr *expr2, bool or_equal)
305 int i2, i3;
307 if (expr2->expr_type == EXPR_CONSTANT)
309 gfc_extract_int (expr2, &i2);
310 i3 = gfc_validate_kind (BT_INTEGER, expr1->ts.kind, false);
312 /* For ISHFT[C], check that |shift| <= bit_size(i). */
313 if (arg2 == NULL)
315 if (i2 < 0)
316 i2 = -i2;
318 if (i2 > gfc_integer_kinds[i3].bit_size)
320 gfc_error ("The absolute value of SHIFT at %L must be less "
321 "than or equal to BIT_SIZE(%qs)",
322 &expr2->where, arg1);
323 return false;
327 if (or_equal)
329 if (i2 > gfc_integer_kinds[i3].bit_size)
331 gfc_error ("%qs at %L must be less than "
332 "or equal to BIT_SIZE(%qs)",
333 arg2, &expr2->where, arg1);
334 return false;
337 else
339 if (i2 >= gfc_integer_kinds[i3].bit_size)
341 gfc_error ("%qs at %L must be less than BIT_SIZE(%qs)",
342 arg2, &expr2->where, arg1);
343 return false;
348 return true;
352 /* If expr is constant, then check that the value is less than or equal
353 to the bit_size of the kind k. */
355 static bool
356 less_than_bitsizekind (const char *arg, gfc_expr *expr, int k)
358 int i, val;
360 if (expr->expr_type != EXPR_CONSTANT)
361 return true;
363 i = gfc_validate_kind (BT_INTEGER, k, false);
364 gfc_extract_int (expr, &val);
366 if (val > gfc_integer_kinds[i].bit_size)
368 gfc_error ("%qs at %L must be less than or equal to the BIT_SIZE of "
369 "INTEGER(KIND=%d)", arg, &expr->where, k);
370 return false;
373 return true;
377 /* If expr2 and expr3 are constants, then check that the value is less than
378 or equal to bit_size(expr1). */
380 static bool
381 less_than_bitsize2 (const char *arg1, gfc_expr *expr1, const char *arg2,
382 gfc_expr *expr2, const char *arg3, gfc_expr *expr3)
384 int i2, i3;
386 if (expr2->expr_type == EXPR_CONSTANT && expr3->expr_type == EXPR_CONSTANT)
388 gfc_extract_int (expr2, &i2);
389 gfc_extract_int (expr3, &i3);
390 i2 += i3;
391 i3 = gfc_validate_kind (BT_INTEGER, expr1->ts.kind, false);
392 if (i2 > gfc_integer_kinds[i3].bit_size)
394 gfc_error ("%<%s + %s%> at %L must be less than or equal "
395 "to BIT_SIZE(%qs)",
396 arg2, arg3, &expr2->where, arg1);
397 return false;
401 return true;
404 /* Make sure two expressions have the same type. */
406 static bool
407 same_type_check (gfc_expr *e, int n, gfc_expr *f, int m)
409 gfc_typespec *ets = &e->ts;
410 gfc_typespec *fts = &f->ts;
412 if (e->ts.type == BT_PROCEDURE && e->symtree->n.sym)
413 ets = &e->symtree->n.sym->ts;
414 if (f->ts.type == BT_PROCEDURE && f->symtree->n.sym)
415 fts = &f->symtree->n.sym->ts;
417 if (gfc_compare_types (ets, fts))
418 return true;
420 gfc_error ("%qs argument of %qs intrinsic at %L must be the same type "
421 "and kind as %qs", gfc_current_intrinsic_arg[m]->name,
422 gfc_current_intrinsic, &f->where,
423 gfc_current_intrinsic_arg[n]->name);
425 return false;
429 /* Make sure that an expression has a certain (nonzero) rank. */
431 static bool
432 rank_check (gfc_expr *e, int n, int rank)
434 if (e->rank == rank)
435 return true;
437 gfc_error ("%qs argument of %qs intrinsic at %L must be of rank %d",
438 gfc_current_intrinsic_arg[n]->name, gfc_current_intrinsic,
439 &e->where, rank);
441 return false;
445 /* Make sure a variable expression is not an optional dummy argument. */
447 static bool
448 nonoptional_check (gfc_expr *e, int n)
450 if (e->expr_type == EXPR_VARIABLE && e->symtree->n.sym->attr.optional)
452 gfc_error ("%qs argument of %qs intrinsic at %L must not be OPTIONAL",
453 gfc_current_intrinsic_arg[n]->name, gfc_current_intrinsic,
454 &e->where);
457 /* TODO: Recursive check on nonoptional variables? */
459 return true;
463 /* Check for ALLOCATABLE attribute. */
465 static bool
466 allocatable_check (gfc_expr *e, int n)
468 symbol_attribute attr;
470 attr = gfc_variable_attr (e, NULL);
471 if (!attr.allocatable || attr.associate_var)
473 gfc_error ("%qs argument of %qs intrinsic at %L must be ALLOCATABLE",
474 gfc_current_intrinsic_arg[n]->name, gfc_current_intrinsic,
475 &e->where);
476 return false;
479 return true;
483 /* Check that an expression has a particular kind. */
485 static bool
486 kind_value_check (gfc_expr *e, int n, int k)
488 if (e->ts.kind == k)
489 return true;
491 gfc_error ("%qs argument of %qs intrinsic at %L must be of kind %d",
492 gfc_current_intrinsic_arg[n]->name, gfc_current_intrinsic,
493 &e->where, k);
495 return false;
499 /* Make sure an expression is a variable. */
501 static bool
502 variable_check (gfc_expr *e, int n, bool allow_proc)
504 if (e->expr_type == EXPR_VARIABLE
505 && e->symtree->n.sym->attr.intent == INTENT_IN
506 && (gfc_current_intrinsic_arg[n]->intent == INTENT_OUT
507 || gfc_current_intrinsic_arg[n]->intent == INTENT_INOUT))
509 gfc_ref *ref;
510 bool pointer = e->symtree->n.sym->ts.type == BT_CLASS
511 && CLASS_DATA (e->symtree->n.sym)
512 ? CLASS_DATA (e->symtree->n.sym)->attr.class_pointer
513 : e->symtree->n.sym->attr.pointer;
515 for (ref = e->ref; ref; ref = ref->next)
517 if (pointer && ref->type == REF_COMPONENT)
518 break;
519 if (ref->type == REF_COMPONENT
520 && ((ref->u.c.component->ts.type == BT_CLASS
521 && CLASS_DATA (ref->u.c.component)->attr.class_pointer)
522 || (ref->u.c.component->ts.type != BT_CLASS
523 && ref->u.c.component->attr.pointer)))
524 break;
527 if (!ref)
529 gfc_error ("%qs argument of %qs intrinsic at %L cannot be "
530 "INTENT(IN)", gfc_current_intrinsic_arg[n]->name,
531 gfc_current_intrinsic, &e->where);
532 return false;
536 if (e->expr_type == EXPR_VARIABLE
537 && e->symtree->n.sym->attr.flavor != FL_PARAMETER
538 && (allow_proc || !e->symtree->n.sym->attr.function))
539 return true;
541 if (e->expr_type == EXPR_VARIABLE && e->symtree->n.sym->attr.function
542 && e->symtree->n.sym == e->symtree->n.sym->result)
544 gfc_namespace *ns;
545 for (ns = gfc_current_ns; ns; ns = ns->parent)
546 if (ns->proc_name == e->symtree->n.sym)
547 return true;
550 gfc_error ("%qs argument of %qs intrinsic at %L must be a variable",
551 gfc_current_intrinsic_arg[n]->name, gfc_current_intrinsic, &e->where);
553 return false;
557 /* Check the common DIM parameter for correctness. */
559 static bool
560 dim_check (gfc_expr *dim, int n, bool optional)
562 if (dim == NULL)
563 return true;
565 if (!type_check (dim, n, BT_INTEGER))
566 return false;
568 if (!scalar_check (dim, n))
569 return false;
571 if (!optional && !nonoptional_check (dim, n))
572 return false;
574 return true;
578 /* If a coarray DIM parameter is a constant, make sure that it is greater than
579 zero and less than or equal to the corank of the given array. */
581 static bool
582 dim_corank_check (gfc_expr *dim, gfc_expr *array)
584 int corank;
586 gcc_assert (array->expr_type == EXPR_VARIABLE);
588 if (dim->expr_type != EXPR_CONSTANT)
589 return true;
591 if (array->ts.type == BT_CLASS)
592 return true;
594 corank = gfc_get_corank (array);
596 if (mpz_cmp_ui (dim->value.integer, 1) < 0
597 || mpz_cmp_ui (dim->value.integer, corank) > 0)
599 gfc_error ("%<dim%> argument of %qs intrinsic at %L is not a valid "
600 "codimension index", gfc_current_intrinsic, &dim->where);
602 return false;
605 return true;
609 /* If a DIM parameter is a constant, make sure that it is greater than
610 zero and less than or equal to the rank of the given array. If
611 allow_assumed is zero then dim must be less than the rank of the array
612 for assumed size arrays. */
614 static bool
615 dim_rank_check (gfc_expr *dim, gfc_expr *array, int allow_assumed)
617 gfc_array_ref *ar;
618 int rank;
620 if (dim == NULL)
621 return true;
623 if (dim->expr_type != EXPR_CONSTANT)
624 return true;
626 if (array->expr_type == EXPR_FUNCTION && array->value.function.isym
627 && array->value.function.isym->id == GFC_ISYM_SPREAD)
628 rank = array->rank + 1;
629 else
630 rank = array->rank;
632 /* Assumed-rank array. */
633 if (rank == -1)
634 rank = GFC_MAX_DIMENSIONS;
636 if (array->expr_type == EXPR_VARIABLE)
638 ar = gfc_find_array_ref (array);
639 if (ar->as->type == AS_ASSUMED_SIZE
640 && !allow_assumed
641 && ar->type != AR_ELEMENT
642 && ar->type != AR_SECTION)
643 rank--;
646 if (mpz_cmp_ui (dim->value.integer, 1) < 0
647 || mpz_cmp_ui (dim->value.integer, rank) > 0)
649 gfc_error ("%<dim%> argument of %qs intrinsic at %L is not a valid "
650 "dimension index", gfc_current_intrinsic, &dim->where);
652 return false;
655 return true;
659 /* Compare the size of a along dimension ai with the size of b along
660 dimension bi, returning 0 if they are known not to be identical,
661 and 1 if they are identical, or if this cannot be determined. */
663 static int
664 identical_dimen_shape (gfc_expr *a, int ai, gfc_expr *b, int bi)
666 mpz_t a_size, b_size;
667 int ret;
669 gcc_assert (a->rank > ai);
670 gcc_assert (b->rank > bi);
672 ret = 1;
674 if (gfc_array_dimen_size (a, ai, &a_size))
676 if (gfc_array_dimen_size (b, bi, &b_size))
678 if (mpz_cmp (a_size, b_size) != 0)
679 ret = 0;
681 mpz_clear (b_size);
683 mpz_clear (a_size);
685 return ret;
688 /* Calculate the length of a character variable, including substrings.
689 Strip away parentheses if necessary. Return -1 if no length could
690 be determined. */
692 static long
693 gfc_var_strlen (const gfc_expr *a)
695 gfc_ref *ra;
697 while (a->expr_type == EXPR_OP && a->value.op.op == INTRINSIC_PARENTHESES)
698 a = a->value.op.op1;
700 for (ra = a->ref; ra != NULL && ra->type != REF_SUBSTRING; ra = ra->next)
703 if (ra)
705 long start_a, end_a;
707 if (!ra->u.ss.end)
708 return -1;
710 if ((!ra->u.ss.start || ra->u.ss.start->expr_type == EXPR_CONSTANT)
711 && ra->u.ss.end->expr_type == EXPR_CONSTANT)
713 start_a = ra->u.ss.start ? mpz_get_si (ra->u.ss.start->value.integer)
714 : 1;
715 end_a = mpz_get_si (ra->u.ss.end->value.integer);
716 return (end_a < start_a) ? 0 : end_a - start_a + 1;
718 else if (ra->u.ss.start
719 && gfc_dep_compare_expr (ra->u.ss.start, ra->u.ss.end) == 0)
720 return 1;
721 else
722 return -1;
725 if (a->ts.u.cl && a->ts.u.cl->length
726 && a->ts.u.cl->length->expr_type == EXPR_CONSTANT)
727 return mpz_get_si (a->ts.u.cl->length->value.integer);
728 else if (a->expr_type == EXPR_CONSTANT
729 && (a->ts.u.cl == NULL || a->ts.u.cl->length == NULL))
730 return a->value.character.length;
731 else
732 return -1;
736 /* Check whether two character expressions have the same length;
737 returns true if they have or if the length cannot be determined,
738 otherwise return false and raise a gfc_error. */
740 bool
741 gfc_check_same_strlen (const gfc_expr *a, const gfc_expr *b, const char *name)
743 long len_a, len_b;
745 len_a = gfc_var_strlen(a);
746 len_b = gfc_var_strlen(b);
748 if (len_a == -1 || len_b == -1 || len_a == len_b)
749 return true;
750 else
752 gfc_error ("Unequal character lengths (%ld/%ld) in %s at %L",
753 len_a, len_b, name, &a->where);
754 return false;
759 /***** Check functions *****/
761 /* Check subroutine suitable for intrinsics taking a real argument and
762 a kind argument for the result. */
764 static bool
765 check_a_kind (gfc_expr *a, gfc_expr *kind, bt type)
767 if (!type_check (a, 0, BT_REAL))
768 return false;
769 if (!kind_check (kind, 1, type))
770 return false;
772 return true;
776 /* Check subroutine suitable for ceiling, floor and nint. */
778 bool
779 gfc_check_a_ikind (gfc_expr *a, gfc_expr *kind)
781 return check_a_kind (a, kind, BT_INTEGER);
785 /* Check subroutine suitable for aint, anint. */
787 bool
788 gfc_check_a_xkind (gfc_expr *a, gfc_expr *kind)
790 return check_a_kind (a, kind, BT_REAL);
794 bool
795 gfc_check_abs (gfc_expr *a)
797 if (!numeric_check (a, 0))
798 return false;
800 return true;
804 bool
805 gfc_check_achar (gfc_expr *a, gfc_expr *kind)
807 if (!type_check (a, 0, BT_INTEGER))
808 return false;
809 if (!kind_check (kind, 1, BT_CHARACTER))
810 return false;
812 return true;
816 bool
817 gfc_check_access_func (gfc_expr *name, gfc_expr *mode)
819 if (!type_check (name, 0, BT_CHARACTER)
820 || !scalar_check (name, 0))
821 return false;
822 if (!kind_value_check (name, 0, gfc_default_character_kind))
823 return false;
825 if (!type_check (mode, 1, BT_CHARACTER)
826 || !scalar_check (mode, 1))
827 return false;
828 if (!kind_value_check (mode, 1, gfc_default_character_kind))
829 return false;
831 return true;
835 bool
836 gfc_check_all_any (gfc_expr *mask, gfc_expr *dim)
838 if (!logical_array_check (mask, 0))
839 return false;
841 if (!dim_check (dim, 1, false))
842 return false;
844 if (!dim_rank_check (dim, mask, 0))
845 return false;
847 return true;
851 bool
852 gfc_check_allocated (gfc_expr *array)
854 if (!variable_check (array, 0, false))
855 return false;
856 if (!allocatable_check (array, 0))
857 return false;
859 return true;
863 /* Common check function where the first argument must be real or
864 integer and the second argument must be the same as the first. */
866 bool
867 gfc_check_a_p (gfc_expr *a, gfc_expr *p)
869 if (!int_or_real_check (a, 0))
870 return false;
872 if (a->ts.type != p->ts.type)
874 gfc_error ("%qs and %qs arguments of %qs intrinsic at %L must "
875 "have the same type", gfc_current_intrinsic_arg[0]->name,
876 gfc_current_intrinsic_arg[1]->name, gfc_current_intrinsic,
877 &p->where);
878 return false;
881 if (a->ts.kind != p->ts.kind)
883 if (!gfc_notify_std (GFC_STD_GNU, "Different type kinds at %L",
884 &p->where))
885 return false;
888 return true;
892 bool
893 gfc_check_x_yd (gfc_expr *x, gfc_expr *y)
895 if (!double_check (x, 0) || !double_check (y, 1))
896 return false;
898 return true;
902 bool
903 gfc_check_associated (gfc_expr *pointer, gfc_expr *target)
905 symbol_attribute attr1, attr2;
906 int i;
907 bool t;
908 locus *where;
910 where = &pointer->where;
912 if (pointer->expr_type == EXPR_NULL)
913 goto null_arg;
915 attr1 = gfc_expr_attr (pointer);
917 if (!attr1.pointer && !attr1.proc_pointer)
919 gfc_error ("%qs argument of %qs intrinsic at %L must be a POINTER",
920 gfc_current_intrinsic_arg[0]->name, gfc_current_intrinsic,
921 &pointer->where);
922 return false;
925 /* F2008, C1242. */
926 if (attr1.pointer && gfc_is_coindexed (pointer))
928 gfc_error ("%qs argument of %qs intrinsic at %L shall not be "
929 "coindexed", gfc_current_intrinsic_arg[0]->name,
930 gfc_current_intrinsic, &pointer->where);
931 return false;
934 /* Target argument is optional. */
935 if (target == NULL)
936 return true;
938 where = &target->where;
939 if (target->expr_type == EXPR_NULL)
940 goto null_arg;
942 if (target->expr_type == EXPR_VARIABLE || target->expr_type == EXPR_FUNCTION)
943 attr2 = gfc_expr_attr (target);
944 else
946 gfc_error ("%qs argument of %qs intrinsic at %L must be a pointer "
947 "or target VARIABLE or FUNCTION",
948 gfc_current_intrinsic_arg[1]->name, gfc_current_intrinsic,
949 &target->where);
950 return false;
953 if (attr1.pointer && !attr2.pointer && !attr2.target)
955 gfc_error ("%qs argument of %qs intrinsic at %L must be a POINTER "
956 "or a TARGET", gfc_current_intrinsic_arg[1]->name,
957 gfc_current_intrinsic, &target->where);
958 return false;
961 /* F2008, C1242. */
962 if (attr1.pointer && gfc_is_coindexed (target))
964 gfc_error ("%qs argument of %qs intrinsic at %L shall not be "
965 "coindexed", gfc_current_intrinsic_arg[1]->name,
966 gfc_current_intrinsic, &target->where);
967 return false;
970 t = true;
971 if (!same_type_check (pointer, 0, target, 1))
972 t = false;
973 if (!rank_check (target, 0, pointer->rank))
974 t = false;
975 if (target->rank > 0)
977 for (i = 0; i < target->rank; i++)
978 if (target->ref->u.ar.dimen_type[i] == DIMEN_VECTOR)
980 gfc_error ("Array section with a vector subscript at %L shall not "
981 "be the target of a pointer",
982 &target->where);
983 t = false;
984 break;
987 return t;
989 null_arg:
991 gfc_error ("NULL pointer at %L is not permitted as actual argument "
992 "of %qs intrinsic function", where, gfc_current_intrinsic);
993 return false;
998 bool
999 gfc_check_atan_2 (gfc_expr *y, gfc_expr *x)
1001 /* gfc_notify_std would be a waste of time as the return value
1002 is seemingly used only for the generic resolution. The error
1003 will be: Too many arguments. */
1004 if ((gfc_option.allow_std & GFC_STD_F2008) == 0)
1005 return false;
1007 return gfc_check_atan2 (y, x);
1011 bool
1012 gfc_check_atan2 (gfc_expr *y, gfc_expr *x)
1014 if (!type_check (y, 0, BT_REAL))
1015 return false;
1016 if (!same_type_check (y, 0, x, 1))
1017 return false;
1019 return true;
1023 static bool
1024 gfc_check_atomic (gfc_expr *atom, int atom_no, gfc_expr *value, int val_no,
1025 gfc_expr *stat, int stat_no)
1027 if (!scalar_check (atom, atom_no) || !scalar_check (value, val_no))
1028 return false;
1030 if (!(atom->ts.type == BT_INTEGER && atom->ts.kind == gfc_atomic_int_kind)
1031 && !(atom->ts.type == BT_LOGICAL
1032 && atom->ts.kind == gfc_atomic_logical_kind))
1034 gfc_error ("ATOM argument at %L to intrinsic function %s shall be an "
1035 "integer of ATOMIC_INT_KIND or a logical of "
1036 "ATOMIC_LOGICAL_KIND", &atom->where, gfc_current_intrinsic);
1037 return false;
1040 if (!gfc_is_coarray (atom) && !gfc_is_coindexed (atom))
1042 gfc_error ("ATOM argument at %L of the %s intrinsic function shall be a "
1043 "coarray or coindexed", &atom->where, gfc_current_intrinsic);
1044 return false;
1047 if (atom->ts.type != value->ts.type)
1049 gfc_error ("%qs argument of %qs intrinsic at %L shall have the same "
1050 "type as %qs at %L", gfc_current_intrinsic_arg[val_no]->name,
1051 gfc_current_intrinsic, &value->where,
1052 gfc_current_intrinsic_arg[atom_no]->name, &atom->where);
1053 return false;
1056 if (stat != NULL)
1058 if (!type_check (stat, stat_no, BT_INTEGER))
1059 return false;
1060 if (!scalar_check (stat, stat_no))
1061 return false;
1062 if (!variable_check (stat, stat_no, false))
1063 return false;
1064 if (!kind_value_check (stat, stat_no, gfc_default_integer_kind))
1065 return false;
1067 if (!gfc_notify_std (GFC_STD_F2008_TS, "STAT= argument to %s at %L",
1068 gfc_current_intrinsic, &stat->where))
1069 return false;
1072 return true;
1076 bool
1077 gfc_check_atomic_def (gfc_expr *atom, gfc_expr *value, gfc_expr *stat)
1079 if (atom->expr_type == EXPR_FUNCTION
1080 && atom->value.function.isym
1081 && atom->value.function.isym->id == GFC_ISYM_CAF_GET)
1082 atom = atom->value.function.actual->expr;
1084 if (!gfc_check_vardef_context (atom, false, false, false, NULL))
1086 gfc_error ("ATOM argument of the %s intrinsic function at %L shall be "
1087 "definable", gfc_current_intrinsic, &atom->where);
1088 return false;
1091 return gfc_check_atomic (atom, 0, value, 1, stat, 2);
1095 bool
1096 gfc_check_atomic_op (gfc_expr *atom, gfc_expr *value, gfc_expr *stat)
1098 if (atom->ts.type != BT_INTEGER || atom->ts.kind != gfc_atomic_int_kind)
1100 gfc_error ("ATOM argument at %L to intrinsic function %s shall be an "
1101 "integer of ATOMIC_INT_KIND", &atom->where,
1102 gfc_current_intrinsic);
1103 return false;
1106 return gfc_check_atomic_def (atom, value, stat);
1110 bool
1111 gfc_check_atomic_ref (gfc_expr *value, gfc_expr *atom, gfc_expr *stat)
1113 if (atom->expr_type == EXPR_FUNCTION
1114 && atom->value.function.isym
1115 && atom->value.function.isym->id == GFC_ISYM_CAF_GET)
1116 atom = atom->value.function.actual->expr;
1118 if (!gfc_check_vardef_context (value, false, false, false, NULL))
1120 gfc_error ("VALUE argument of the %s intrinsic function at %L shall be "
1121 "definable", gfc_current_intrinsic, &value->where);
1122 return false;
1125 return gfc_check_atomic (atom, 1, value, 0, stat, 2);
1129 bool
1130 gfc_check_atomic_cas (gfc_expr *atom, gfc_expr *old, gfc_expr *compare,
1131 gfc_expr *new_val, gfc_expr *stat)
1133 if (atom->expr_type == EXPR_FUNCTION
1134 && atom->value.function.isym
1135 && atom->value.function.isym->id == GFC_ISYM_CAF_GET)
1136 atom = atom->value.function.actual->expr;
1138 if (!gfc_check_atomic (atom, 0, new_val, 3, stat, 4))
1139 return false;
1141 if (!scalar_check (old, 1) || !scalar_check (compare, 2))
1142 return false;
1144 if (!same_type_check (atom, 0, old, 1))
1145 return false;
1147 if (!same_type_check (atom, 0, compare, 2))
1148 return false;
1150 if (!gfc_check_vardef_context (atom, false, false, false, NULL))
1152 gfc_error ("ATOM argument of the %s intrinsic function at %L shall be "
1153 "definable", gfc_current_intrinsic, &atom->where);
1154 return false;
1157 if (!gfc_check_vardef_context (old, false, false, false, NULL))
1159 gfc_error ("OLD argument of the %s intrinsic function at %L shall be "
1160 "definable", gfc_current_intrinsic, &old->where);
1161 return false;
1164 return true;
1167 bool
1168 gfc_check_event_query (gfc_expr *event, gfc_expr *count, gfc_expr *stat)
1170 if (event->ts.type != BT_DERIVED
1171 || event->ts.u.derived->from_intmod != INTMOD_ISO_FORTRAN_ENV
1172 || event->ts.u.derived->intmod_sym_id != ISOFORTRAN_EVENT_TYPE)
1174 gfc_error ("EVENT argument at %L to the intrinsic EVENT_QUERY "
1175 "shall be of type EVENT_TYPE", &event->where);
1176 return false;
1179 if (!scalar_check (event, 0))
1180 return false;
1182 if (!gfc_check_vardef_context (count, false, false, false, NULL))
1184 gfc_error ("COUNT argument of the EVENT_QUERY intrinsic function at %L "
1185 "shall be definable", &count->where);
1186 return false;
1189 if (!type_check (count, 1, BT_INTEGER))
1190 return false;
1192 int i = gfc_validate_kind (BT_INTEGER, count->ts.kind, false);
1193 int j = gfc_validate_kind (BT_INTEGER, gfc_default_integer_kind, false);
1195 if (gfc_integer_kinds[i].range < gfc_integer_kinds[j].range)
1197 gfc_error ("COUNT argument of the EVENT_QUERY intrinsic function at %L "
1198 "shall have at least the range of the default integer",
1199 &count->where);
1200 return false;
1203 if (stat != NULL)
1205 if (!type_check (stat, 2, BT_INTEGER))
1206 return false;
1207 if (!scalar_check (stat, 2))
1208 return false;
1209 if (!variable_check (stat, 2, false))
1210 return false;
1212 if (!gfc_notify_std (GFC_STD_F2008_TS, "STAT= argument to %s at %L",
1213 gfc_current_intrinsic, &stat->where))
1214 return false;
1217 return true;
1221 bool
1222 gfc_check_atomic_fetch_op (gfc_expr *atom, gfc_expr *value, gfc_expr *old,
1223 gfc_expr *stat)
1225 if (atom->expr_type == EXPR_FUNCTION
1226 && atom->value.function.isym
1227 && atom->value.function.isym->id == GFC_ISYM_CAF_GET)
1228 atom = atom->value.function.actual->expr;
1230 if (atom->ts.type != BT_INTEGER || atom->ts.kind != gfc_atomic_int_kind)
1232 gfc_error ("ATOM argument at %L to intrinsic function %s shall be an "
1233 "integer of ATOMIC_INT_KIND", &atom->where,
1234 gfc_current_intrinsic);
1235 return false;
1238 if (!gfc_check_atomic (atom, 0, value, 1, stat, 3))
1239 return false;
1241 if (!scalar_check (old, 2))
1242 return false;
1244 if (!same_type_check (atom, 0, old, 2))
1245 return false;
1247 if (!gfc_check_vardef_context (atom, false, false, false, NULL))
1249 gfc_error ("ATOM argument of the %s intrinsic function at %L shall be "
1250 "definable", gfc_current_intrinsic, &atom->where);
1251 return false;
1254 if (!gfc_check_vardef_context (old, false, false, false, NULL))
1256 gfc_error ("OLD argument of the %s intrinsic function at %L shall be "
1257 "definable", gfc_current_intrinsic, &old->where);
1258 return false;
1261 return true;
1265 /* BESJN and BESYN functions. */
1267 bool
1268 gfc_check_besn (gfc_expr *n, gfc_expr *x)
1270 if (!type_check (n, 0, BT_INTEGER))
1271 return false;
1272 if (n->expr_type == EXPR_CONSTANT)
1274 int i;
1275 gfc_extract_int (n, &i);
1276 if (i < 0 && !gfc_notify_std (GFC_STD_GNU, "Negative argument "
1277 "N at %L", &n->where))
1278 return false;
1281 if (!type_check (x, 1, BT_REAL))
1282 return false;
1284 return true;
1288 /* Transformational version of the Bessel JN and YN functions. */
1290 bool
1291 gfc_check_bessel_n2 (gfc_expr *n1, gfc_expr *n2, gfc_expr *x)
1293 if (!type_check (n1, 0, BT_INTEGER))
1294 return false;
1295 if (!scalar_check (n1, 0))
1296 return false;
1297 if (!nonnegative_check ("N1", n1))
1298 return false;
1300 if (!type_check (n2, 1, BT_INTEGER))
1301 return false;
1302 if (!scalar_check (n2, 1))
1303 return false;
1304 if (!nonnegative_check ("N2", n2))
1305 return false;
1307 if (!type_check (x, 2, BT_REAL))
1308 return false;
1309 if (!scalar_check (x, 2))
1310 return false;
1312 return true;
1316 bool
1317 gfc_check_bge_bgt_ble_blt (gfc_expr *i, gfc_expr *j)
1319 if (!type_check (i, 0, BT_INTEGER))
1320 return false;
1322 if (!type_check (j, 1, BT_INTEGER))
1323 return false;
1325 return true;
1329 bool
1330 gfc_check_bitfcn (gfc_expr *i, gfc_expr *pos)
1332 if (!type_check (i, 0, BT_INTEGER))
1333 return false;
1335 if (!type_check (pos, 1, BT_INTEGER))
1336 return false;
1338 if (!nonnegative_check ("pos", pos))
1339 return false;
1341 if (!less_than_bitsize1 ("i", i, "pos", pos, false))
1342 return false;
1344 return true;
1348 bool
1349 gfc_check_char (gfc_expr *i, gfc_expr *kind)
1351 if (!type_check (i, 0, BT_INTEGER))
1352 return false;
1353 if (!kind_check (kind, 1, BT_CHARACTER))
1354 return false;
1356 return true;
1360 bool
1361 gfc_check_chdir (gfc_expr *dir)
1363 if (!type_check (dir, 0, BT_CHARACTER))
1364 return false;
1365 if (!kind_value_check (dir, 0, gfc_default_character_kind))
1366 return false;
1368 return true;
1372 bool
1373 gfc_check_chdir_sub (gfc_expr *dir, gfc_expr *status)
1375 if (!type_check (dir, 0, BT_CHARACTER))
1376 return false;
1377 if (!kind_value_check (dir, 0, gfc_default_character_kind))
1378 return false;
1380 if (status == NULL)
1381 return true;
1383 if (!type_check (status, 1, BT_INTEGER))
1384 return false;
1385 if (!scalar_check (status, 1))
1386 return false;
1388 return true;
1392 bool
1393 gfc_check_chmod (gfc_expr *name, gfc_expr *mode)
1395 if (!type_check (name, 0, BT_CHARACTER))
1396 return false;
1397 if (!kind_value_check (name, 0, gfc_default_character_kind))
1398 return false;
1400 if (!type_check (mode, 1, BT_CHARACTER))
1401 return false;
1402 if (!kind_value_check (mode, 1, gfc_default_character_kind))
1403 return false;
1405 return true;
1409 bool
1410 gfc_check_chmod_sub (gfc_expr *name, gfc_expr *mode, gfc_expr *status)
1412 if (!type_check (name, 0, BT_CHARACTER))
1413 return false;
1414 if (!kind_value_check (name, 0, gfc_default_character_kind))
1415 return false;
1417 if (!type_check (mode, 1, BT_CHARACTER))
1418 return false;
1419 if (!kind_value_check (mode, 1, gfc_default_character_kind))
1420 return false;
1422 if (status == NULL)
1423 return true;
1425 if (!type_check (status, 2, BT_INTEGER))
1426 return false;
1428 if (!scalar_check (status, 2))
1429 return false;
1431 return true;
1435 bool
1436 gfc_check_cmplx (gfc_expr *x, gfc_expr *y, gfc_expr *kind)
1438 if (!numeric_check (x, 0))
1439 return false;
1441 if (y != NULL)
1443 if (!numeric_check (y, 1))
1444 return false;
1446 if (x->ts.type == BT_COMPLEX)
1448 gfc_error ("%qs argument of %qs intrinsic at %L must not be "
1449 "present if %<x%> is COMPLEX",
1450 gfc_current_intrinsic_arg[1]->name, gfc_current_intrinsic,
1451 &y->where);
1452 return false;
1455 if (y->ts.type == BT_COMPLEX)
1457 gfc_error ("%qs argument of %qs intrinsic at %L must have a type "
1458 "of either REAL or INTEGER",
1459 gfc_current_intrinsic_arg[1]->name, gfc_current_intrinsic,
1460 &y->where);
1461 return false;
1466 if (!kind_check (kind, 2, BT_COMPLEX))
1467 return false;
1469 if (!kind && warn_conversion
1470 && x->ts.type == BT_REAL && x->ts.kind > gfc_default_real_kind)
1471 gfc_warning_now (OPT_Wconversion, "Conversion from %s to default-kind "
1472 "COMPLEX(%d) at %L might lose precision, consider using "
1473 "the KIND argument", gfc_typename (&x->ts),
1474 gfc_default_real_kind, &x->where);
1475 else if (y && !kind && warn_conversion
1476 && y->ts.type == BT_REAL && y->ts.kind > gfc_default_real_kind)
1477 gfc_warning_now (OPT_Wconversion, "Conversion from %s to default-kind "
1478 "COMPLEX(%d) at %L might lose precision, consider using "
1479 "the KIND argument", gfc_typename (&y->ts),
1480 gfc_default_real_kind, &y->where);
1481 return true;
1485 static bool
1486 check_co_collective (gfc_expr *a, gfc_expr *image_idx, gfc_expr *stat,
1487 gfc_expr *errmsg, bool co_reduce)
1489 if (!variable_check (a, 0, false))
1490 return false;
1492 if (!gfc_check_vardef_context (a, false, false, false, "argument 'A' with "
1493 "INTENT(INOUT)"))
1494 return false;
1496 /* Fortran 2008, 12.5.2.4, paragraph 18. */
1497 if (gfc_has_vector_subscript (a))
1499 gfc_error ("Argument %<A%> with INTENT(INOUT) at %L of the intrinsic "
1500 "subroutine %s shall not have a vector subscript",
1501 &a->where, gfc_current_intrinsic);
1502 return false;
1505 if (gfc_is_coindexed (a))
1507 gfc_error ("The A argument at %L to the intrinsic %s shall not be "
1508 "coindexed", &a->where, gfc_current_intrinsic);
1509 return false;
1512 if (image_idx != NULL)
1514 if (!type_check (image_idx, co_reduce ? 2 : 1, BT_INTEGER))
1515 return false;
1516 if (!scalar_check (image_idx, co_reduce ? 2 : 1))
1517 return false;
1520 if (stat != NULL)
1522 if (!type_check (stat, co_reduce ? 3 : 2, BT_INTEGER))
1523 return false;
1524 if (!scalar_check (stat, co_reduce ? 3 : 2))
1525 return false;
1526 if (!variable_check (stat, co_reduce ? 3 : 2, false))
1527 return false;
1528 if (stat->ts.kind != 4)
1530 gfc_error ("The stat= argument at %L must be a kind=4 integer "
1531 "variable", &stat->where);
1532 return false;
1536 if (errmsg != NULL)
1538 if (!type_check (errmsg, co_reduce ? 4 : 3, BT_CHARACTER))
1539 return false;
1540 if (!scalar_check (errmsg, co_reduce ? 4 : 3))
1541 return false;
1542 if (!variable_check (errmsg, co_reduce ? 4 : 3, false))
1543 return false;
1544 if (errmsg->ts.kind != 1)
1546 gfc_error ("The errmsg= argument at %L must be a default-kind "
1547 "character variable", &errmsg->where);
1548 return false;
1552 if (flag_coarray == GFC_FCOARRAY_NONE)
1554 gfc_fatal_error ("Coarrays disabled at %L, use %<-fcoarray=%> to enable",
1555 &a->where);
1556 return false;
1559 return true;
1563 bool
1564 gfc_check_co_broadcast (gfc_expr *a, gfc_expr *source_image, gfc_expr *stat,
1565 gfc_expr *errmsg)
1567 if (a->ts.type == BT_CLASS || gfc_expr_attr (a).alloc_comp)
1569 gfc_error ("Support for the A argument at %L which is polymorphic A "
1570 "argument or has allocatable components is not yet "
1571 "implemented", &a->where);
1572 return false;
1574 return check_co_collective (a, source_image, stat, errmsg, false);
1578 bool
1579 gfc_check_co_reduce (gfc_expr *a, gfc_expr *op, gfc_expr *result_image,
1580 gfc_expr *stat, gfc_expr *errmsg)
1582 symbol_attribute attr;
1583 gfc_formal_arglist *formal;
1584 gfc_symbol *sym;
1586 if (a->ts.type == BT_CLASS)
1588 gfc_error ("The A argument at %L of CO_REDUCE shall not be polymorphic",
1589 &a->where);
1590 return false;
1593 if (gfc_expr_attr (a).alloc_comp)
1595 gfc_error ("Support for the A argument at %L with allocatable components"
1596 " is not yet implemented", &a->where);
1597 return false;
1600 if (!check_co_collective (a, result_image, stat, errmsg, true))
1601 return false;
1603 if (!gfc_resolve_expr (op))
1604 return false;
1606 attr = gfc_expr_attr (op);
1607 if (!attr.pure || !attr.function)
1609 gfc_error ("OPERATOR argument at %L must be a PURE function",
1610 &op->where);
1611 return false;
1614 if (attr.intrinsic)
1616 /* None of the intrinsics fulfills the criteria of taking two arguments,
1617 returning the same type and kind as the arguments and being permitted
1618 as actual argument. */
1619 gfc_error ("Intrinsic function %s at %L is not permitted for CO_REDUCE",
1620 op->symtree->n.sym->name, &op->where);
1621 return false;
1624 if (gfc_is_proc_ptr_comp (op))
1626 gfc_component *comp = gfc_get_proc_ptr_comp (op);
1627 sym = comp->ts.interface;
1629 else
1630 sym = op->symtree->n.sym;
1632 formal = sym->formal;
1634 if (!formal || !formal->next || formal->next->next)
1636 gfc_error ("The function passed as OPERATOR at %L shall have two "
1637 "arguments", &op->where);
1638 return false;
1641 if (sym->result->ts.type == BT_UNKNOWN)
1642 gfc_set_default_type (sym->result, 0, NULL);
1644 if (!gfc_compare_types (&a->ts, &sym->result->ts))
1646 gfc_error ("A argument at %L has type %s but the function passed as "
1647 "OPERATOR at %L returns %s",
1648 &a->where, gfc_typename (&a->ts), &op->where,
1649 gfc_typename (&sym->result->ts));
1650 return false;
1652 if (!gfc_compare_types (&a->ts, &formal->sym->ts)
1653 || !gfc_compare_types (&a->ts, &formal->next->sym->ts))
1655 gfc_error ("The function passed as OPERATOR at %L has arguments of type "
1656 "%s and %s but shall have type %s", &op->where,
1657 gfc_typename (&formal->sym->ts),
1658 gfc_typename (&formal->next->sym->ts), gfc_typename (&a->ts));
1659 return false;
1661 if (op->rank || attr.allocatable || attr.pointer || formal->sym->as
1662 || formal->next->sym->as || formal->sym->attr.allocatable
1663 || formal->next->sym->attr.allocatable || formal->sym->attr.pointer
1664 || formal->next->sym->attr.pointer)
1666 gfc_error ("The function passed as OPERATOR at %L shall have scalar "
1667 "nonallocatable nonpointer arguments and return a "
1668 "nonallocatable nonpointer scalar", &op->where);
1669 return false;
1672 if (formal->sym->attr.value != formal->next->sym->attr.value)
1674 gfc_error ("The function passed as OPERATOR at %L shall have the VALUE "
1675 "attribute either for none or both arguments", &op->where);
1676 return false;
1679 if (formal->sym->attr.target != formal->next->sym->attr.target)
1681 gfc_error ("The function passed as OPERATOR at %L shall have the TARGET "
1682 "attribute either for none or both arguments", &op->where);
1683 return false;
1686 if (formal->sym->attr.asynchronous != formal->next->sym->attr.asynchronous)
1688 gfc_error ("The function passed as OPERATOR at %L shall have the "
1689 "ASYNCHRONOUS attribute either for none or both arguments",
1690 &op->where);
1691 return false;
1694 if (formal->sym->attr.optional || formal->next->sym->attr.optional)
1696 gfc_error ("The function passed as OPERATOR at %L shall not have the "
1697 "OPTIONAL attribute for either of the arguments", &op->where);
1698 return false;
1701 if (a->ts.type == BT_CHARACTER)
1703 gfc_charlen *cl;
1704 unsigned long actual_size, formal_size1, formal_size2, result_size;
1706 cl = a->ts.u.cl;
1707 actual_size = cl && cl->length && cl->length->expr_type == EXPR_CONSTANT
1708 ? mpz_get_ui (cl->length->value.integer) : 0;
1710 cl = formal->sym->ts.u.cl;
1711 formal_size1 = cl && cl->length && cl->length->expr_type == EXPR_CONSTANT
1712 ? mpz_get_ui (cl->length->value.integer) : 0;
1714 cl = formal->next->sym->ts.u.cl;
1715 formal_size2 = cl && cl->length && cl->length->expr_type == EXPR_CONSTANT
1716 ? mpz_get_ui (cl->length->value.integer) : 0;
1718 cl = sym->ts.u.cl;
1719 result_size = cl && cl->length && cl->length->expr_type == EXPR_CONSTANT
1720 ? mpz_get_ui (cl->length->value.integer) : 0;
1722 if (actual_size
1723 && ((formal_size1 && actual_size != formal_size1)
1724 || (formal_size2 && actual_size != formal_size2)))
1726 gfc_error ("The character length of the A argument at %L and of the "
1727 "arguments of the OPERATOR at %L shall be the same",
1728 &a->where, &op->where);
1729 return false;
1731 if (actual_size && result_size && actual_size != result_size)
1733 gfc_error ("The character length of the A argument at %L and of the "
1734 "function result of the OPERATOR at %L shall be the same",
1735 &a->where, &op->where);
1736 return false;
1740 return true;
1744 bool
1745 gfc_check_co_minmax (gfc_expr *a, gfc_expr *result_image, gfc_expr *stat,
1746 gfc_expr *errmsg)
1748 if (a->ts.type != BT_INTEGER && a->ts.type != BT_REAL
1749 && a->ts.type != BT_CHARACTER)
1751 gfc_error ("%qs argument of %qs intrinsic at %L shall be of type "
1752 "integer, real or character",
1753 gfc_current_intrinsic_arg[0]->name, gfc_current_intrinsic,
1754 &a->where);
1755 return false;
1757 return check_co_collective (a, result_image, stat, errmsg, false);
1761 bool
1762 gfc_check_co_sum (gfc_expr *a, gfc_expr *result_image, gfc_expr *stat,
1763 gfc_expr *errmsg)
1765 if (!numeric_check (a, 0))
1766 return false;
1767 return check_co_collective (a, result_image, stat, errmsg, false);
1771 bool
1772 gfc_check_complex (gfc_expr *x, gfc_expr *y)
1774 if (!int_or_real_check (x, 0))
1775 return false;
1776 if (!scalar_check (x, 0))
1777 return false;
1779 if (!int_or_real_check (y, 1))
1780 return false;
1781 if (!scalar_check (y, 1))
1782 return false;
1784 return true;
1788 bool
1789 gfc_check_count (gfc_expr *mask, gfc_expr *dim, gfc_expr *kind)
1791 if (!logical_array_check (mask, 0))
1792 return false;
1793 if (!dim_check (dim, 1, false))
1794 return false;
1795 if (!dim_rank_check (dim, mask, 0))
1796 return false;
1797 if (!kind_check (kind, 2, BT_INTEGER))
1798 return false;
1799 if (kind && !gfc_notify_std (GFC_STD_F2003, "%qs intrinsic "
1800 "with KIND argument at %L",
1801 gfc_current_intrinsic, &kind->where))
1802 return false;
1804 return true;
1808 bool
1809 gfc_check_cshift (gfc_expr *array, gfc_expr *shift, gfc_expr *dim)
1811 if (!array_check (array, 0))
1812 return false;
1814 if (!type_check (shift, 1, BT_INTEGER))
1815 return false;
1817 if (!dim_check (dim, 2, true))
1818 return false;
1820 if (!dim_rank_check (dim, array, false))
1821 return false;
1823 if (array->rank == 1 || shift->rank == 0)
1825 if (!scalar_check (shift, 1))
1826 return false;
1828 else if (shift->rank == array->rank - 1)
1830 int d;
1831 if (!dim)
1832 d = 1;
1833 else if (dim->expr_type == EXPR_CONSTANT)
1834 gfc_extract_int (dim, &d);
1835 else
1836 d = -1;
1838 if (d > 0)
1840 int i, j;
1841 for (i = 0, j = 0; i < array->rank; i++)
1842 if (i != d - 1)
1844 if (!identical_dimen_shape (array, i, shift, j))
1846 gfc_error ("%qs argument of %qs intrinsic at %L has "
1847 "invalid shape in dimension %d (%ld/%ld)",
1848 gfc_current_intrinsic_arg[1]->name,
1849 gfc_current_intrinsic, &shift->where, i + 1,
1850 mpz_get_si (array->shape[i]),
1851 mpz_get_si (shift->shape[j]));
1852 return false;
1855 j += 1;
1859 else
1861 gfc_error ("%qs argument of intrinsic %qs at %L of must have rank "
1862 "%d or be a scalar", gfc_current_intrinsic_arg[1]->name,
1863 gfc_current_intrinsic, &shift->where, array->rank - 1);
1864 return false;
1867 return true;
1871 bool
1872 gfc_check_ctime (gfc_expr *time)
1874 if (!scalar_check (time, 0))
1875 return false;
1877 if (!type_check (time, 0, BT_INTEGER))
1878 return false;
1880 return true;
1884 bool gfc_check_datan2 (gfc_expr *y, gfc_expr *x)
1886 if (!double_check (y, 0) || !double_check (x, 1))
1887 return false;
1889 return true;
1892 bool
1893 gfc_check_dcmplx (gfc_expr *x, gfc_expr *y)
1895 if (!numeric_check (x, 0))
1896 return false;
1898 if (y != NULL)
1900 if (!numeric_check (y, 1))
1901 return false;
1903 if (x->ts.type == BT_COMPLEX)
1905 gfc_error ("%qs argument of %qs intrinsic at %L must not be "
1906 "present if %<x%> is COMPLEX",
1907 gfc_current_intrinsic_arg[1]->name, gfc_current_intrinsic,
1908 &y->where);
1909 return false;
1912 if (y->ts.type == BT_COMPLEX)
1914 gfc_error ("%qs argument of %qs intrinsic at %L must have a type "
1915 "of either REAL or INTEGER",
1916 gfc_current_intrinsic_arg[1]->name, gfc_current_intrinsic,
1917 &y->where);
1918 return false;
1922 return true;
1926 bool
1927 gfc_check_dble (gfc_expr *x)
1929 if (!numeric_check (x, 0))
1930 return false;
1932 return true;
1936 bool
1937 gfc_check_digits (gfc_expr *x)
1939 if (!int_or_real_check (x, 0))
1940 return false;
1942 return true;
1946 bool
1947 gfc_check_dot_product (gfc_expr *vector_a, gfc_expr *vector_b)
1949 switch (vector_a->ts.type)
1951 case BT_LOGICAL:
1952 if (!type_check (vector_b, 1, BT_LOGICAL))
1953 return false;
1954 break;
1956 case BT_INTEGER:
1957 case BT_REAL:
1958 case BT_COMPLEX:
1959 if (!numeric_check (vector_b, 1))
1960 return false;
1961 break;
1963 default:
1964 gfc_error ("%qs argument of %qs intrinsic at %L must be numeric "
1965 "or LOGICAL", gfc_current_intrinsic_arg[0]->name,
1966 gfc_current_intrinsic, &vector_a->where);
1967 return false;
1970 if (!rank_check (vector_a, 0, 1))
1971 return false;
1973 if (!rank_check (vector_b, 1, 1))
1974 return false;
1976 if (! identical_dimen_shape (vector_a, 0, vector_b, 0))
1978 gfc_error ("Different shape for arguments %qs and %qs at %L for "
1979 "intrinsic %<dot_product%>",
1980 gfc_current_intrinsic_arg[0]->name,
1981 gfc_current_intrinsic_arg[1]->name, &vector_a->where);
1982 return false;
1985 return true;
1989 bool
1990 gfc_check_dprod (gfc_expr *x, gfc_expr *y)
1992 if (!type_check (x, 0, BT_REAL)
1993 || !type_check (y, 1, BT_REAL))
1994 return false;
1996 if (x->ts.kind != gfc_default_real_kind)
1998 gfc_error ("%qs argument of %qs intrinsic at %L must be default "
1999 "real", gfc_current_intrinsic_arg[0]->name,
2000 gfc_current_intrinsic, &x->where);
2001 return false;
2004 if (y->ts.kind != gfc_default_real_kind)
2006 gfc_error ("%qs argument of %qs intrinsic at %L must be default "
2007 "real", gfc_current_intrinsic_arg[1]->name,
2008 gfc_current_intrinsic, &y->where);
2009 return false;
2012 return true;
2016 bool
2017 gfc_check_dshift (gfc_expr *i, gfc_expr *j, gfc_expr *shift)
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->is_boz && j->is_boz)
2027 gfc_error ("%<I%> at %L and %<J%>' at %L cannot both be BOZ literal "
2028 "constants", &i->where, &j->where);
2029 return false;
2032 if (!i->is_boz && !j->is_boz && !same_type_check (i, 0, j, 1))
2033 return false;
2035 if (!type_check (shift, 2, BT_INTEGER))
2036 return false;
2038 if (!nonnegative_check ("SHIFT", shift))
2039 return false;
2041 if (i->is_boz)
2043 if (!less_than_bitsize1 ("J", j, "SHIFT", shift, true))
2044 return false;
2045 i->ts.kind = j->ts.kind;
2047 else
2049 if (!less_than_bitsize1 ("I", i, "SHIFT", shift, true))
2050 return false;
2051 j->ts.kind = i->ts.kind;
2054 return true;
2058 bool
2059 gfc_check_eoshift (gfc_expr *array, gfc_expr *shift, gfc_expr *boundary,
2060 gfc_expr *dim)
2062 if (!array_check (array, 0))
2063 return false;
2065 if (!type_check (shift, 1, BT_INTEGER))
2066 return false;
2068 if (!dim_check (dim, 3, true))
2069 return false;
2071 if (!dim_rank_check (dim, array, false))
2072 return false;
2074 if (array->rank == 1 || shift->rank == 0)
2076 if (!scalar_check (shift, 1))
2077 return false;
2079 else if (shift->rank == array->rank - 1)
2081 int d;
2082 if (!dim)
2083 d = 1;
2084 else if (dim->expr_type == EXPR_CONSTANT)
2085 gfc_extract_int (dim, &d);
2086 else
2087 d = -1;
2089 if (d > 0)
2091 int i, j;
2092 for (i = 0, j = 0; i < array->rank; i++)
2093 if (i != d - 1)
2095 if (!identical_dimen_shape (array, i, shift, j))
2097 gfc_error ("%qs argument of %qs intrinsic at %L has "
2098 "invalid shape in dimension %d (%ld/%ld)",
2099 gfc_current_intrinsic_arg[1]->name,
2100 gfc_current_intrinsic, &shift->where, i + 1,
2101 mpz_get_si (array->shape[i]),
2102 mpz_get_si (shift->shape[j]));
2103 return false;
2106 j += 1;
2110 else
2112 gfc_error ("%qs argument of intrinsic %qs at %L of must have rank "
2113 "%d or be a scalar", gfc_current_intrinsic_arg[1]->name,
2114 gfc_current_intrinsic, &shift->where, array->rank - 1);
2115 return false;
2118 if (boundary != NULL)
2120 if (!same_type_check (array, 0, boundary, 2))
2121 return false;
2123 if (array->rank == 1 || boundary->rank == 0)
2125 if (!scalar_check (boundary, 2))
2126 return false;
2128 else if (boundary->rank == array->rank - 1)
2130 if (!gfc_check_conformance (shift, boundary,
2131 "arguments '%s' and '%s' for "
2132 "intrinsic %s",
2133 gfc_current_intrinsic_arg[1]->name,
2134 gfc_current_intrinsic_arg[2]->name,
2135 gfc_current_intrinsic))
2136 return false;
2138 else
2140 gfc_error ("%qs argument of intrinsic %qs at %L of must have "
2141 "rank %d or be a scalar",
2142 gfc_current_intrinsic_arg[1]->name, gfc_current_intrinsic,
2143 &shift->where, array->rank - 1);
2144 return false;
2148 return true;
2151 bool
2152 gfc_check_float (gfc_expr *a)
2154 if (!type_check (a, 0, BT_INTEGER))
2155 return false;
2157 if ((a->ts.kind != gfc_default_integer_kind)
2158 && !gfc_notify_std (GFC_STD_GNU, "non-default INTEGER "
2159 "kind argument to %s intrinsic at %L",
2160 gfc_current_intrinsic, &a->where))
2161 return false;
2163 return true;
2166 /* A single complex argument. */
2168 bool
2169 gfc_check_fn_c (gfc_expr *a)
2171 if (!type_check (a, 0, BT_COMPLEX))
2172 return false;
2174 return true;
2177 /* A single real argument. */
2179 bool
2180 gfc_check_fn_r (gfc_expr *a)
2182 if (!type_check (a, 0, BT_REAL))
2183 return false;
2185 return true;
2188 /* A single double argument. */
2190 bool
2191 gfc_check_fn_d (gfc_expr *a)
2193 if (!double_check (a, 0))
2194 return false;
2196 return true;
2199 /* A single real or complex argument. */
2201 bool
2202 gfc_check_fn_rc (gfc_expr *a)
2204 if (!real_or_complex_check (a, 0))
2205 return false;
2207 return true;
2211 bool
2212 gfc_check_fn_rc2008 (gfc_expr *a)
2214 if (!real_or_complex_check (a, 0))
2215 return false;
2217 if (a->ts.type == BT_COMPLEX
2218 && !gfc_notify_std (GFC_STD_F2008, "COMPLEX argument %qs "
2219 "of %qs intrinsic at %L",
2220 gfc_current_intrinsic_arg[0]->name,
2221 gfc_current_intrinsic, &a->where))
2222 return false;
2224 return true;
2228 bool
2229 gfc_check_fnum (gfc_expr *unit)
2231 if (!type_check (unit, 0, BT_INTEGER))
2232 return false;
2234 if (!scalar_check (unit, 0))
2235 return false;
2237 return true;
2241 bool
2242 gfc_check_huge (gfc_expr *x)
2244 if (!int_or_real_check (x, 0))
2245 return false;
2247 return true;
2251 bool
2252 gfc_check_hypot (gfc_expr *x, gfc_expr *y)
2254 if (!type_check (x, 0, BT_REAL))
2255 return false;
2256 if (!same_type_check (x, 0, y, 1))
2257 return false;
2259 return true;
2263 /* Check that the single argument is an integer. */
2265 bool
2266 gfc_check_i (gfc_expr *i)
2268 if (!type_check (i, 0, BT_INTEGER))
2269 return false;
2271 return true;
2275 bool
2276 gfc_check_iand (gfc_expr *i, gfc_expr *j)
2278 if (!type_check (i, 0, BT_INTEGER))
2279 return false;
2281 if (!type_check (j, 1, BT_INTEGER))
2282 return false;
2284 if (i->ts.kind != j->ts.kind)
2286 if (!gfc_notify_std (GFC_STD_GNU, "Different type kinds at %L",
2287 &i->where))
2288 return false;
2291 return true;
2295 bool
2296 gfc_check_ibits (gfc_expr *i, gfc_expr *pos, gfc_expr *len)
2298 if (!type_check (i, 0, BT_INTEGER))
2299 return false;
2301 if (!type_check (pos, 1, BT_INTEGER))
2302 return false;
2304 if (!type_check (len, 2, BT_INTEGER))
2305 return false;
2307 if (!nonnegative_check ("pos", pos))
2308 return false;
2310 if (!nonnegative_check ("len", len))
2311 return false;
2313 if (!less_than_bitsize2 ("i", i, "pos", pos, "len", len))
2314 return false;
2316 return true;
2320 bool
2321 gfc_check_ichar_iachar (gfc_expr *c, gfc_expr *kind)
2323 int i;
2325 if (!type_check (c, 0, BT_CHARACTER))
2326 return false;
2328 if (!kind_check (kind, 1, BT_INTEGER))
2329 return false;
2331 if (kind && !gfc_notify_std (GFC_STD_F2003, "%qs intrinsic "
2332 "with KIND argument at %L",
2333 gfc_current_intrinsic, &kind->where))
2334 return false;
2336 if (c->expr_type == EXPR_VARIABLE || c->expr_type == EXPR_SUBSTRING)
2338 gfc_expr *start;
2339 gfc_expr *end;
2340 gfc_ref *ref;
2342 /* Substring references don't have the charlength set. */
2343 ref = c->ref;
2344 while (ref && ref->type != REF_SUBSTRING)
2345 ref = ref->next;
2347 gcc_assert (ref == NULL || ref->type == REF_SUBSTRING);
2349 if (!ref)
2351 /* Check that the argument is length one. Non-constant lengths
2352 can't be checked here, so assume they are ok. */
2353 if (c->ts.u.cl && c->ts.u.cl->length)
2355 /* If we already have a length for this expression then use it. */
2356 if (c->ts.u.cl->length->expr_type != EXPR_CONSTANT)
2357 return true;
2358 i = mpz_get_si (c->ts.u.cl->length->value.integer);
2360 else
2361 return true;
2363 else
2365 start = ref->u.ss.start;
2366 end = ref->u.ss.end;
2368 gcc_assert (start);
2369 if (end == NULL || end->expr_type != EXPR_CONSTANT
2370 || start->expr_type != EXPR_CONSTANT)
2371 return true;
2373 i = mpz_get_si (end->value.integer) + 1
2374 - mpz_get_si (start->value.integer);
2377 else
2378 return true;
2380 if (i != 1)
2382 gfc_error ("Argument of %s at %L must be of length one",
2383 gfc_current_intrinsic, &c->where);
2384 return false;
2387 return true;
2391 bool
2392 gfc_check_idnint (gfc_expr *a)
2394 if (!double_check (a, 0))
2395 return false;
2397 return true;
2401 bool
2402 gfc_check_ieor (gfc_expr *i, gfc_expr *j)
2404 if (!type_check (i, 0, BT_INTEGER))
2405 return false;
2407 if (!type_check (j, 1, BT_INTEGER))
2408 return false;
2410 if (i->ts.kind != j->ts.kind)
2412 if (!gfc_notify_std (GFC_STD_GNU, "Different type kinds at %L",
2413 &i->where))
2414 return false;
2417 return true;
2421 bool
2422 gfc_check_index (gfc_expr *string, gfc_expr *substring, gfc_expr *back,
2423 gfc_expr *kind)
2425 if (!type_check (string, 0, BT_CHARACTER)
2426 || !type_check (substring, 1, BT_CHARACTER))
2427 return false;
2429 if (back != NULL && !type_check (back, 2, BT_LOGICAL))
2430 return false;
2432 if (!kind_check (kind, 3, BT_INTEGER))
2433 return false;
2434 if (kind && !gfc_notify_std (GFC_STD_F2003, "%qs intrinsic "
2435 "with KIND argument at %L",
2436 gfc_current_intrinsic, &kind->where))
2437 return false;
2439 if (string->ts.kind != substring->ts.kind)
2441 gfc_error ("%qs argument of %qs intrinsic at %L must be the same "
2442 "kind as %qs", gfc_current_intrinsic_arg[1]->name,
2443 gfc_current_intrinsic, &substring->where,
2444 gfc_current_intrinsic_arg[0]->name);
2445 return false;
2448 return true;
2452 bool
2453 gfc_check_int (gfc_expr *x, gfc_expr *kind)
2455 if (!numeric_check (x, 0))
2456 return false;
2458 if (!kind_check (kind, 1, BT_INTEGER))
2459 return false;
2461 return true;
2465 bool
2466 gfc_check_intconv (gfc_expr *x)
2468 if (!numeric_check (x, 0))
2469 return false;
2471 return true;
2475 bool
2476 gfc_check_ior (gfc_expr *i, gfc_expr *j)
2478 if (!type_check (i, 0, BT_INTEGER))
2479 return false;
2481 if (!type_check (j, 1, BT_INTEGER))
2482 return false;
2484 if (i->ts.kind != j->ts.kind)
2486 if (!gfc_notify_std (GFC_STD_GNU, "Different type kinds at %L",
2487 &i->where))
2488 return false;
2491 return true;
2495 bool
2496 gfc_check_ishft (gfc_expr *i, gfc_expr *shift)
2498 if (!type_check (i, 0, BT_INTEGER)
2499 || !type_check (shift, 1, BT_INTEGER))
2500 return false;
2502 if (!less_than_bitsize1 ("I", i, NULL, shift, true))
2503 return false;
2505 return true;
2509 bool
2510 gfc_check_ishftc (gfc_expr *i, gfc_expr *shift, gfc_expr *size)
2512 if (!type_check (i, 0, BT_INTEGER)
2513 || !type_check (shift, 1, BT_INTEGER))
2514 return false;
2516 if (size != NULL)
2518 int i2, i3;
2520 if (!type_check (size, 2, BT_INTEGER))
2521 return false;
2523 if (!less_than_bitsize1 ("I", i, "SIZE", size, true))
2524 return false;
2526 if (size->expr_type == EXPR_CONSTANT)
2528 gfc_extract_int (size, &i3);
2529 if (i3 <= 0)
2531 gfc_error ("SIZE at %L must be positive", &size->where);
2532 return false;
2535 if (shift->expr_type == EXPR_CONSTANT)
2537 gfc_extract_int (shift, &i2);
2538 if (i2 < 0)
2539 i2 = -i2;
2541 if (i2 > i3)
2543 gfc_error ("The absolute value of SHIFT at %L must be less "
2544 "than or equal to SIZE at %L", &shift->where,
2545 &size->where);
2546 return false;
2551 else if (!less_than_bitsize1 ("I", i, NULL, shift, true))
2552 return false;
2554 return true;
2558 bool
2559 gfc_check_kill (gfc_expr *pid, gfc_expr *sig)
2561 if (!type_check (pid, 0, BT_INTEGER))
2562 return false;
2564 if (!type_check (sig, 1, BT_INTEGER))
2565 return false;
2567 return true;
2571 bool
2572 gfc_check_kill_sub (gfc_expr *pid, gfc_expr *sig, gfc_expr *status)
2574 if (!type_check (pid, 0, BT_INTEGER))
2575 return false;
2577 if (!scalar_check (pid, 0))
2578 return false;
2580 if (!type_check (sig, 1, BT_INTEGER))
2581 return false;
2583 if (!scalar_check (sig, 1))
2584 return false;
2586 if (status == NULL)
2587 return true;
2589 if (!type_check (status, 2, BT_INTEGER))
2590 return false;
2592 if (!scalar_check (status, 2))
2593 return false;
2595 return true;
2599 bool
2600 gfc_check_kind (gfc_expr *x)
2602 if (gfc_bt_struct (x->ts.type) || x->ts.type == BT_CLASS)
2604 gfc_error ("%qs argument of %qs intrinsic at %L must be of "
2605 "intrinsic type", gfc_current_intrinsic_arg[0]->name,
2606 gfc_current_intrinsic, &x->where);
2607 return false;
2609 if (x->ts.type == BT_PROCEDURE)
2611 gfc_error ("%qs argument of %qs intrinsic at %L must be a data entity",
2612 gfc_current_intrinsic_arg[0]->name, gfc_current_intrinsic,
2613 &x->where);
2614 return false;
2617 return true;
2621 bool
2622 gfc_check_lbound (gfc_expr *array, gfc_expr *dim, gfc_expr *kind)
2624 if (!array_check (array, 0))
2625 return false;
2627 if (!dim_check (dim, 1, false))
2628 return false;
2630 if (!dim_rank_check (dim, array, 1))
2631 return false;
2633 if (!kind_check (kind, 2, BT_INTEGER))
2634 return false;
2635 if (kind && !gfc_notify_std (GFC_STD_F2003, "%qs intrinsic "
2636 "with KIND argument at %L",
2637 gfc_current_intrinsic, &kind->where))
2638 return false;
2640 return true;
2644 bool
2645 gfc_check_lcobound (gfc_expr *coarray, gfc_expr *dim, gfc_expr *kind)
2647 if (flag_coarray == GFC_FCOARRAY_NONE)
2649 gfc_fatal_error ("Coarrays disabled at %C, use %<-fcoarray=%> to enable");
2650 return false;
2653 if (!coarray_check (coarray, 0))
2654 return false;
2656 if (dim != NULL)
2658 if (!dim_check (dim, 1, false))
2659 return false;
2661 if (!dim_corank_check (dim, coarray))
2662 return false;
2665 if (!kind_check (kind, 2, BT_INTEGER))
2666 return false;
2668 return true;
2672 bool
2673 gfc_check_len_lentrim (gfc_expr *s, gfc_expr *kind)
2675 if (!type_check (s, 0, BT_CHARACTER))
2676 return false;
2678 if (!kind_check (kind, 1, BT_INTEGER))
2679 return false;
2680 if (kind && !gfc_notify_std (GFC_STD_F2003, "%qs intrinsic "
2681 "with KIND argument at %L",
2682 gfc_current_intrinsic, &kind->where))
2683 return false;
2685 return true;
2689 bool
2690 gfc_check_lge_lgt_lle_llt (gfc_expr *a, gfc_expr *b)
2692 if (!type_check (a, 0, BT_CHARACTER))
2693 return false;
2694 if (!kind_value_check (a, 0, gfc_default_character_kind))
2695 return false;
2697 if (!type_check (b, 1, BT_CHARACTER))
2698 return false;
2699 if (!kind_value_check (b, 1, gfc_default_character_kind))
2700 return false;
2702 return true;
2706 bool
2707 gfc_check_link (gfc_expr *path1, gfc_expr *path2)
2709 if (!type_check (path1, 0, BT_CHARACTER))
2710 return false;
2711 if (!kind_value_check (path1, 0, gfc_default_character_kind))
2712 return false;
2714 if (!type_check (path2, 1, BT_CHARACTER))
2715 return false;
2716 if (!kind_value_check (path2, 1, gfc_default_character_kind))
2717 return false;
2719 return true;
2723 bool
2724 gfc_check_link_sub (gfc_expr *path1, gfc_expr *path2, gfc_expr *status)
2726 if (!type_check (path1, 0, BT_CHARACTER))
2727 return false;
2728 if (!kind_value_check (path1, 0, gfc_default_character_kind))
2729 return false;
2731 if (!type_check (path2, 1, BT_CHARACTER))
2732 return false;
2733 if (!kind_value_check (path2, 0, gfc_default_character_kind))
2734 return false;
2736 if (status == NULL)
2737 return true;
2739 if (!type_check (status, 2, BT_INTEGER))
2740 return false;
2742 if (!scalar_check (status, 2))
2743 return false;
2745 return true;
2749 bool
2750 gfc_check_loc (gfc_expr *expr)
2752 return variable_check (expr, 0, true);
2756 bool
2757 gfc_check_symlnk (gfc_expr *path1, gfc_expr *path2)
2759 if (!type_check (path1, 0, BT_CHARACTER))
2760 return false;
2761 if (!kind_value_check (path1, 0, gfc_default_character_kind))
2762 return false;
2764 if (!type_check (path2, 1, BT_CHARACTER))
2765 return false;
2766 if (!kind_value_check (path2, 1, gfc_default_character_kind))
2767 return false;
2769 return true;
2773 bool
2774 gfc_check_symlnk_sub (gfc_expr *path1, gfc_expr *path2, gfc_expr *status)
2776 if (!type_check (path1, 0, BT_CHARACTER))
2777 return false;
2778 if (!kind_value_check (path1, 0, gfc_default_character_kind))
2779 return false;
2781 if (!type_check (path2, 1, BT_CHARACTER))
2782 return false;
2783 if (!kind_value_check (path2, 1, gfc_default_character_kind))
2784 return false;
2786 if (status == NULL)
2787 return true;
2789 if (!type_check (status, 2, BT_INTEGER))
2790 return false;
2792 if (!scalar_check (status, 2))
2793 return false;
2795 return true;
2799 bool
2800 gfc_check_logical (gfc_expr *a, gfc_expr *kind)
2802 if (!type_check (a, 0, BT_LOGICAL))
2803 return false;
2804 if (!kind_check (kind, 1, BT_LOGICAL))
2805 return false;
2807 return true;
2811 /* Min/max family. */
2813 static bool
2814 min_max_args (gfc_actual_arglist *args)
2816 gfc_actual_arglist *arg;
2817 int i, j, nargs, *nlabels, nlabelless;
2818 bool a1 = false, a2 = false;
2820 if (args == NULL || args->next == NULL)
2822 gfc_error ("Intrinsic %qs at %L must have at least two arguments",
2823 gfc_current_intrinsic, gfc_current_intrinsic_where);
2824 return false;
2827 if (!args->name)
2828 a1 = true;
2830 if (!args->next->name)
2831 a2 = true;
2833 nargs = 0;
2834 for (arg = args; arg; arg = arg->next)
2835 if (arg->name)
2836 nargs++;
2838 if (nargs == 0)
2839 return true;
2841 /* Note: Having a keywordless argument after an "arg=" is checked before. */
2842 nlabelless = 0;
2843 nlabels = XALLOCAVEC (int, nargs);
2844 for (arg = args, i = 0; arg; arg = arg->next, i++)
2845 if (arg->name)
2847 int n;
2848 char *endp;
2850 if (arg->name[0] != 'a' || arg->name[1] < '1' || arg->name[1] > '9')
2851 goto unknown;
2852 n = strtol (&arg->name[1], &endp, 10);
2853 if (endp[0] != '\0')
2854 goto unknown;
2855 if (n <= 0)
2856 goto unknown;
2857 if (n <= nlabelless)
2858 goto duplicate;
2859 nlabels[i] = n;
2860 if (n == 1)
2861 a1 = true;
2862 if (n == 2)
2863 a2 = true;
2865 else
2866 nlabelless++;
2868 if (!a1 || !a2)
2870 gfc_error ("Missing %qs argument to the %s intrinsic at %L",
2871 !a1 ? "a1" : "a2", gfc_current_intrinsic,
2872 gfc_current_intrinsic_where);
2873 return false;
2876 /* Check for duplicates. */
2877 for (i = 0; i < nargs; i++)
2878 for (j = i + 1; j < nargs; j++)
2879 if (nlabels[i] == nlabels[j])
2880 goto duplicate;
2882 return true;
2884 duplicate:
2885 gfc_error ("Duplicate argument %qs at %L to intrinsic %s", arg->name,
2886 &arg->expr->where, gfc_current_intrinsic);
2887 return false;
2889 unknown:
2890 gfc_error ("Unknown argument %qs at %L to intrinsic %s", arg->name,
2891 &arg->expr->where, gfc_current_intrinsic);
2892 return false;
2896 static bool
2897 check_rest (bt type, int kind, gfc_actual_arglist *arglist)
2899 gfc_actual_arglist *arg, *tmp;
2900 gfc_expr *x;
2901 int m, n;
2903 if (!min_max_args (arglist))
2904 return false;
2906 for (arg = arglist, n=1; arg; arg = arg->next, n++)
2908 x = arg->expr;
2909 if (x->ts.type != type || x->ts.kind != kind)
2911 if (x->ts.type == type)
2913 if (!gfc_notify_std (GFC_STD_GNU, "Different type "
2914 "kinds at %L", &x->where))
2915 return false;
2917 else
2919 gfc_error ("%<a%d%> argument of %qs intrinsic at %L must be "
2920 "%s(%d)", n, gfc_current_intrinsic, &x->where,
2921 gfc_basic_typename (type), kind);
2922 return false;
2926 for (tmp = arglist, m=1; tmp != arg; tmp = tmp->next, m++)
2927 if (!gfc_check_conformance (tmp->expr, x,
2928 "arguments 'a%d' and 'a%d' for "
2929 "intrinsic '%s'", m, n,
2930 gfc_current_intrinsic))
2931 return false;
2934 return true;
2938 bool
2939 gfc_check_min_max (gfc_actual_arglist *arg)
2941 gfc_expr *x;
2943 if (!min_max_args (arg))
2944 return false;
2946 x = arg->expr;
2948 if (x->ts.type == BT_CHARACTER)
2950 if (!gfc_notify_std (GFC_STD_F2003, "%qs intrinsic "
2951 "with CHARACTER argument at %L",
2952 gfc_current_intrinsic, &x->where))
2953 return false;
2955 else if (x->ts.type != BT_INTEGER && x->ts.type != BT_REAL)
2957 gfc_error ("%<a1%> argument of %qs intrinsic at %L must be INTEGER, "
2958 "REAL or CHARACTER", gfc_current_intrinsic, &x->where);
2959 return false;
2962 return check_rest (x->ts.type, x->ts.kind, arg);
2966 bool
2967 gfc_check_min_max_integer (gfc_actual_arglist *arg)
2969 return check_rest (BT_INTEGER, gfc_default_integer_kind, arg);
2973 bool
2974 gfc_check_min_max_real (gfc_actual_arglist *arg)
2976 return check_rest (BT_REAL, gfc_default_real_kind, arg);
2980 bool
2981 gfc_check_min_max_double (gfc_actual_arglist *arg)
2983 return check_rest (BT_REAL, gfc_default_double_kind, arg);
2987 /* End of min/max family. */
2989 bool
2990 gfc_check_malloc (gfc_expr *size)
2992 if (!type_check (size, 0, BT_INTEGER))
2993 return false;
2995 if (!scalar_check (size, 0))
2996 return false;
2998 return true;
3002 bool
3003 gfc_check_matmul (gfc_expr *matrix_a, gfc_expr *matrix_b)
3005 if ((matrix_a->ts.type != BT_LOGICAL) && !gfc_numeric_ts (&matrix_a->ts))
3007 gfc_error ("%qs argument of %qs intrinsic at %L must be numeric "
3008 "or LOGICAL", gfc_current_intrinsic_arg[0]->name,
3009 gfc_current_intrinsic, &matrix_a->where);
3010 return false;
3013 if ((matrix_b->ts.type != BT_LOGICAL) && !gfc_numeric_ts (&matrix_b->ts))
3015 gfc_error ("%qs argument of %qs intrinsic at %L must be numeric "
3016 "or LOGICAL", gfc_current_intrinsic_arg[1]->name,
3017 gfc_current_intrinsic, &matrix_b->where);
3018 return false;
3021 if ((matrix_a->ts.type == BT_LOGICAL && gfc_numeric_ts (&matrix_b->ts))
3022 || (gfc_numeric_ts (&matrix_a->ts) && matrix_b->ts.type == BT_LOGICAL))
3024 gfc_error ("Argument types of %qs intrinsic at %L must match (%s/%s)",
3025 gfc_current_intrinsic, &matrix_a->where,
3026 gfc_typename(&matrix_a->ts), gfc_typename(&matrix_b->ts));
3027 return false;
3030 switch (matrix_a->rank)
3032 case 1:
3033 if (!rank_check (matrix_b, 1, 2))
3034 return false;
3035 /* Check for case matrix_a has shape(m), matrix_b has shape (m, k). */
3036 if (!identical_dimen_shape (matrix_a, 0, matrix_b, 0))
3038 gfc_error ("Different shape on dimension 1 for arguments %qs "
3039 "and %qs at %L for intrinsic matmul",
3040 gfc_current_intrinsic_arg[0]->name,
3041 gfc_current_intrinsic_arg[1]->name, &matrix_a->where);
3042 return false;
3044 break;
3046 case 2:
3047 if (matrix_b->rank != 2)
3049 if (!rank_check (matrix_b, 1, 1))
3050 return false;
3052 /* matrix_b has rank 1 or 2 here. Common check for the cases
3053 - matrix_a has shape (n,m) and matrix_b has shape (m, k)
3054 - matrix_a has shape (n,m) and matrix_b has shape (m). */
3055 if (!identical_dimen_shape (matrix_a, 1, matrix_b, 0))
3057 gfc_error ("Different shape on dimension 2 for argument %qs and "
3058 "dimension 1 for argument %qs at %L for intrinsic "
3059 "matmul", gfc_current_intrinsic_arg[0]->name,
3060 gfc_current_intrinsic_arg[1]->name, &matrix_a->where);
3061 return false;
3063 break;
3065 default:
3066 gfc_error ("%qs argument of %qs intrinsic at %L must be of rank "
3067 "1 or 2", gfc_current_intrinsic_arg[0]->name,
3068 gfc_current_intrinsic, &matrix_a->where);
3069 return false;
3072 return true;
3076 /* Whoever came up with this interface was probably on something.
3077 The possibilities for the occupation of the second and third
3078 parameters are:
3080 Arg #2 Arg #3
3081 NULL NULL
3082 DIM NULL
3083 MASK NULL
3084 NULL MASK minloc(array, mask=m)
3085 DIM MASK
3087 I.e. in the case of minloc(array,mask), mask will be in the second
3088 position of the argument list and we'll have to fix that up. */
3090 bool
3091 gfc_check_minloc_maxloc (gfc_actual_arglist *ap)
3093 gfc_expr *a, *m, *d;
3095 a = ap->expr;
3096 if (!int_or_real_check (a, 0) || !array_check (a, 0))
3097 return false;
3099 d = ap->next->expr;
3100 m = ap->next->next->expr;
3102 if (m == NULL && d != NULL && d->ts.type == BT_LOGICAL
3103 && ap->next->name == NULL)
3105 m = d;
3106 d = NULL;
3107 ap->next->expr = NULL;
3108 ap->next->next->expr = m;
3111 if (!dim_check (d, 1, false))
3112 return false;
3114 if (!dim_rank_check (d, a, 0))
3115 return false;
3117 if (m != NULL && !type_check (m, 2, BT_LOGICAL))
3118 return false;
3120 if (m != NULL
3121 && !gfc_check_conformance (a, m,
3122 "arguments '%s' and '%s' for intrinsic %s",
3123 gfc_current_intrinsic_arg[0]->name,
3124 gfc_current_intrinsic_arg[2]->name,
3125 gfc_current_intrinsic))
3126 return false;
3128 return true;
3132 /* Similar to minloc/maxloc, the argument list might need to be
3133 reordered for the MINVAL, MAXVAL, PRODUCT, and SUM intrinsics. The
3134 difference is that MINLOC/MAXLOC take an additional KIND argument.
3135 The possibilities are:
3137 Arg #2 Arg #3
3138 NULL NULL
3139 DIM NULL
3140 MASK NULL
3141 NULL MASK minval(array, mask=m)
3142 DIM MASK
3144 I.e. in the case of minval(array,mask), mask will be in the second
3145 position of the argument list and we'll have to fix that up. */
3147 static bool
3148 check_reduction (gfc_actual_arglist *ap)
3150 gfc_expr *a, *m, *d;
3152 a = ap->expr;
3153 d = ap->next->expr;
3154 m = ap->next->next->expr;
3156 if (m == NULL && d != NULL && d->ts.type == BT_LOGICAL
3157 && ap->next->name == NULL)
3159 m = d;
3160 d = NULL;
3161 ap->next->expr = NULL;
3162 ap->next->next->expr = m;
3165 if (!dim_check (d, 1, false))
3166 return false;
3168 if (!dim_rank_check (d, a, 0))
3169 return false;
3171 if (m != NULL && !type_check (m, 2, BT_LOGICAL))
3172 return false;
3174 if (m != NULL
3175 && !gfc_check_conformance (a, m,
3176 "arguments '%s' and '%s' for intrinsic %s",
3177 gfc_current_intrinsic_arg[0]->name,
3178 gfc_current_intrinsic_arg[2]->name,
3179 gfc_current_intrinsic))
3180 return false;
3182 return true;
3186 bool
3187 gfc_check_minval_maxval (gfc_actual_arglist *ap)
3189 if (!int_or_real_check (ap->expr, 0)
3190 || !array_check (ap->expr, 0))
3191 return false;
3193 return check_reduction (ap);
3197 bool
3198 gfc_check_product_sum (gfc_actual_arglist *ap)
3200 if (!numeric_check (ap->expr, 0)
3201 || !array_check (ap->expr, 0))
3202 return false;
3204 return check_reduction (ap);
3208 /* For IANY, IALL and IPARITY. */
3210 bool
3211 gfc_check_mask (gfc_expr *i, gfc_expr *kind)
3213 int k;
3215 if (!type_check (i, 0, BT_INTEGER))
3216 return false;
3218 if (!nonnegative_check ("I", i))
3219 return false;
3221 if (!kind_check (kind, 1, BT_INTEGER))
3222 return false;
3224 if (kind)
3225 gfc_extract_int (kind, &k);
3226 else
3227 k = gfc_default_integer_kind;
3229 if (!less_than_bitsizekind ("I", i, k))
3230 return false;
3232 return true;
3236 bool
3237 gfc_check_transf_bit_intrins (gfc_actual_arglist *ap)
3239 if (ap->expr->ts.type != BT_INTEGER)
3241 gfc_error ("%qs argument of %qs intrinsic at %L must be INTEGER",
3242 gfc_current_intrinsic_arg[0]->name,
3243 gfc_current_intrinsic, &ap->expr->where);
3244 return false;
3247 if (!array_check (ap->expr, 0))
3248 return false;
3250 return check_reduction (ap);
3254 bool
3255 gfc_check_merge (gfc_expr *tsource, gfc_expr *fsource, gfc_expr *mask)
3257 if (!same_type_check (tsource, 0, fsource, 1))
3258 return false;
3260 if (!type_check (mask, 2, BT_LOGICAL))
3261 return false;
3263 if (tsource->ts.type == BT_CHARACTER)
3264 return gfc_check_same_strlen (tsource, fsource, "MERGE intrinsic");
3266 return true;
3270 bool
3271 gfc_check_merge_bits (gfc_expr *i, gfc_expr *j, gfc_expr *mask)
3273 if (!type_check (i, 0, BT_INTEGER))
3274 return false;
3276 if (!type_check (j, 1, BT_INTEGER))
3277 return false;
3279 if (!type_check (mask, 2, BT_INTEGER))
3280 return false;
3282 if (!same_type_check (i, 0, j, 1))
3283 return false;
3285 if (!same_type_check (i, 0, mask, 2))
3286 return false;
3288 return true;
3292 bool
3293 gfc_check_move_alloc (gfc_expr *from, gfc_expr *to)
3295 if (!variable_check (from, 0, false))
3296 return false;
3297 if (!allocatable_check (from, 0))
3298 return false;
3299 if (gfc_is_coindexed (from))
3301 gfc_error ("The FROM argument to MOVE_ALLOC at %L shall not be "
3302 "coindexed", &from->where);
3303 return false;
3306 if (!variable_check (to, 1, false))
3307 return false;
3308 if (!allocatable_check (to, 1))
3309 return false;
3310 if (gfc_is_coindexed (to))
3312 gfc_error ("The TO argument to MOVE_ALLOC at %L shall not be "
3313 "coindexed", &to->where);
3314 return false;
3317 if (from->ts.type == BT_CLASS && to->ts.type == BT_DERIVED)
3319 gfc_error ("The TO arguments in MOVE_ALLOC at %L must be "
3320 "polymorphic if FROM is polymorphic",
3321 &to->where);
3322 return false;
3325 if (!same_type_check (to, 1, from, 0))
3326 return false;
3328 if (to->rank != from->rank)
3330 gfc_error ("The FROM and TO arguments of the MOVE_ALLOC intrinsic at %L "
3331 "must have the same rank %d/%d", &to->where, from->rank,
3332 to->rank);
3333 return false;
3336 /* IR F08/0040; cf. 12-006A. */
3337 if (gfc_get_corank (to) != gfc_get_corank (from))
3339 gfc_error ("The FROM and TO arguments of the MOVE_ALLOC intrinsic at %L "
3340 "must have the same corank %d/%d", &to->where,
3341 gfc_get_corank (from), gfc_get_corank (to));
3342 return false;
3345 /* This is based losely on F2003 12.4.1.7. It is intended to prevent
3346 the likes of to = sym->cmp1->cmp2 and from = sym->cmp1, where cmp1
3347 and cmp2 are allocatable. After the allocation is transferred,
3348 the 'to' chain is broken by the nullification of the 'from'. A bit
3349 of reflection reveals that this can only occur for derived types
3350 with recursive allocatable components. */
3351 if (to->expr_type == EXPR_VARIABLE && from->expr_type == EXPR_VARIABLE
3352 && !strcmp (to->symtree->n.sym->name, from->symtree->n.sym->name))
3354 gfc_ref *to_ref, *from_ref;
3355 to_ref = to->ref;
3356 from_ref = from->ref;
3357 bool aliasing = true;
3359 for (; from_ref && to_ref;
3360 from_ref = from_ref->next, to_ref = to_ref->next)
3362 if (to_ref->type != from->ref->type)
3363 aliasing = false;
3364 else if (to_ref->type == REF_ARRAY
3365 && to_ref->u.ar.type != AR_FULL
3366 && from_ref->u.ar.type != AR_FULL)
3367 /* Play safe; assume sections and elements are different. */
3368 aliasing = false;
3369 else if (to_ref->type == REF_COMPONENT
3370 && to_ref->u.c.component != from_ref->u.c.component)
3371 aliasing = false;
3373 if (!aliasing)
3374 break;
3377 if (aliasing)
3379 gfc_error ("The FROM and TO arguments at %L violate aliasing "
3380 "restrictions (F2003 12.4.1.7)", &to->where);
3381 return false;
3385 /* CLASS arguments: Make sure the vtab of from is present. */
3386 if (to->ts.type == BT_CLASS && !UNLIMITED_POLY (from))
3387 gfc_find_vtab (&from->ts);
3389 return true;
3393 bool
3394 gfc_check_nearest (gfc_expr *x, gfc_expr *s)
3396 if (!type_check (x, 0, BT_REAL))
3397 return false;
3399 if (!type_check (s, 1, BT_REAL))
3400 return false;
3402 if (s->expr_type == EXPR_CONSTANT)
3404 if (mpfr_sgn (s->value.real) == 0)
3406 gfc_error ("Argument %<S%> of NEAREST at %L shall not be zero",
3407 &s->where);
3408 return false;
3412 return true;
3416 bool
3417 gfc_check_new_line (gfc_expr *a)
3419 if (!type_check (a, 0, BT_CHARACTER))
3420 return false;
3422 return true;
3426 bool
3427 gfc_check_norm2 (gfc_expr *array, gfc_expr *dim)
3429 if (!type_check (array, 0, BT_REAL))
3430 return false;
3432 if (!array_check (array, 0))
3433 return false;
3435 if (!dim_rank_check (dim, array, false))
3436 return false;
3438 return true;
3441 bool
3442 gfc_check_null (gfc_expr *mold)
3444 symbol_attribute attr;
3446 if (mold == NULL)
3447 return true;
3449 if (!variable_check (mold, 0, true))
3450 return false;
3452 attr = gfc_variable_attr (mold, NULL);
3454 if (!attr.pointer && !attr.proc_pointer && !attr.allocatable)
3456 gfc_error ("%qs argument of %qs intrinsic at %L must be a POINTER, "
3457 "ALLOCATABLE or procedure pointer",
3458 gfc_current_intrinsic_arg[0]->name,
3459 gfc_current_intrinsic, &mold->where);
3460 return false;
3463 if (attr.allocatable
3464 && !gfc_notify_std (GFC_STD_F2003, "NULL intrinsic with "
3465 "allocatable MOLD at %L", &mold->where))
3466 return false;
3468 /* F2008, C1242. */
3469 if (gfc_is_coindexed (mold))
3471 gfc_error ("%qs argument of %qs intrinsic at %L shall not be "
3472 "coindexed", gfc_current_intrinsic_arg[0]->name,
3473 gfc_current_intrinsic, &mold->where);
3474 return false;
3477 return true;
3481 bool
3482 gfc_check_pack (gfc_expr *array, gfc_expr *mask, gfc_expr *vector)
3484 if (!array_check (array, 0))
3485 return false;
3487 if (!type_check (mask, 1, BT_LOGICAL))
3488 return false;
3490 if (!gfc_check_conformance (array, mask,
3491 "arguments '%s' and '%s' for intrinsic '%s'",
3492 gfc_current_intrinsic_arg[0]->name,
3493 gfc_current_intrinsic_arg[1]->name,
3494 gfc_current_intrinsic))
3495 return false;
3497 if (vector != NULL)
3499 mpz_t array_size, vector_size;
3500 bool have_array_size, have_vector_size;
3502 if (!same_type_check (array, 0, vector, 2))
3503 return false;
3505 if (!rank_check (vector, 2, 1))
3506 return false;
3508 /* VECTOR requires at least as many elements as MASK
3509 has .TRUE. values. */
3510 have_array_size = gfc_array_size(array, &array_size);
3511 have_vector_size = gfc_array_size(vector, &vector_size);
3513 if (have_vector_size
3514 && (mask->expr_type == EXPR_ARRAY
3515 || (mask->expr_type == EXPR_CONSTANT
3516 && have_array_size)))
3518 int mask_true_values = 0;
3520 if (mask->expr_type == EXPR_ARRAY)
3522 gfc_constructor *mask_ctor;
3523 mask_ctor = gfc_constructor_first (mask->value.constructor);
3524 while (mask_ctor)
3526 if (mask_ctor->expr->expr_type != EXPR_CONSTANT)
3528 mask_true_values = 0;
3529 break;
3532 if (mask_ctor->expr->value.logical)
3533 mask_true_values++;
3535 mask_ctor = gfc_constructor_next (mask_ctor);
3538 else if (mask->expr_type == EXPR_CONSTANT && mask->value.logical)
3539 mask_true_values = mpz_get_si (array_size);
3541 if (mpz_get_si (vector_size) < mask_true_values)
3543 gfc_error ("%qs argument of %qs intrinsic at %L must "
3544 "provide at least as many elements as there "
3545 "are .TRUE. values in %qs (%ld/%d)",
3546 gfc_current_intrinsic_arg[2]->name,
3547 gfc_current_intrinsic, &vector->where,
3548 gfc_current_intrinsic_arg[1]->name,
3549 mpz_get_si (vector_size), mask_true_values);
3550 return false;
3554 if (have_array_size)
3555 mpz_clear (array_size);
3556 if (have_vector_size)
3557 mpz_clear (vector_size);
3560 return true;
3564 bool
3565 gfc_check_parity (gfc_expr *mask, gfc_expr *dim)
3567 if (!type_check (mask, 0, BT_LOGICAL))
3568 return false;
3570 if (!array_check (mask, 0))
3571 return false;
3573 if (!dim_rank_check (dim, mask, false))
3574 return false;
3576 return true;
3580 bool
3581 gfc_check_precision (gfc_expr *x)
3583 if (!real_or_complex_check (x, 0))
3584 return false;
3586 return true;
3590 bool
3591 gfc_check_present (gfc_expr *a)
3593 gfc_symbol *sym;
3595 if (!variable_check (a, 0, true))
3596 return false;
3598 sym = a->symtree->n.sym;
3599 if (!sym->attr.dummy)
3601 gfc_error ("%qs argument of %qs intrinsic at %L must be of a "
3602 "dummy variable", gfc_current_intrinsic_arg[0]->name,
3603 gfc_current_intrinsic, &a->where);
3604 return false;
3607 if (!sym->attr.optional)
3609 gfc_error ("%qs argument of %qs intrinsic at %L must be of "
3610 "an OPTIONAL dummy variable",
3611 gfc_current_intrinsic_arg[0]->name, gfc_current_intrinsic,
3612 &a->where);
3613 return false;
3616 /* 13.14.82 PRESENT(A)
3617 ......
3618 Argument. A shall be the name of an optional dummy argument that is
3619 accessible in the subprogram in which the PRESENT function reference
3620 appears... */
3622 if (a->ref != NULL
3623 && !(a->ref->next == NULL && a->ref->type == REF_ARRAY
3624 && (a->ref->u.ar.type == AR_FULL
3625 || (a->ref->u.ar.type == AR_ELEMENT
3626 && a->ref->u.ar.as->rank == 0))))
3628 gfc_error ("%qs argument of %qs intrinsic at %L must not be a "
3629 "subobject of %qs", gfc_current_intrinsic_arg[0]->name,
3630 gfc_current_intrinsic, &a->where, sym->name);
3631 return false;
3634 return true;
3638 bool
3639 gfc_check_radix (gfc_expr *x)
3641 if (!int_or_real_check (x, 0))
3642 return false;
3644 return true;
3648 bool
3649 gfc_check_range (gfc_expr *x)
3651 if (!numeric_check (x, 0))
3652 return false;
3654 return true;
3658 bool
3659 gfc_check_rank (gfc_expr *a ATTRIBUTE_UNUSED)
3661 /* Any data object is allowed; a "data object" is a "constant (4.1.3),
3662 variable (6), or subobject of a constant (2.4.3.2.3)" (F2008, 1.3.45). */
3664 bool is_variable = true;
3666 /* Functions returning pointers are regarded as variable, cf. F2008, R602. */
3667 if (a->expr_type == EXPR_FUNCTION)
3668 is_variable = a->value.function.esym
3669 ? a->value.function.esym->result->attr.pointer
3670 : a->symtree->n.sym->result->attr.pointer;
3672 if (a->expr_type == EXPR_OP || a->expr_type == EXPR_NULL
3673 || a->expr_type == EXPR_COMPCALL|| a->expr_type == EXPR_PPC
3674 || !is_variable)
3676 gfc_error ("The argument of the RANK intrinsic at %L must be a data "
3677 "object", &a->where);
3678 return false;
3681 return true;
3685 /* real, float, sngl. */
3686 bool
3687 gfc_check_real (gfc_expr *a, gfc_expr *kind)
3689 if (!numeric_check (a, 0))
3690 return false;
3692 if (!kind_check (kind, 1, BT_REAL))
3693 return false;
3695 return true;
3699 bool
3700 gfc_check_rename (gfc_expr *path1, gfc_expr *path2)
3702 if (!type_check (path1, 0, BT_CHARACTER))
3703 return false;
3704 if (!kind_value_check (path1, 0, gfc_default_character_kind))
3705 return false;
3707 if (!type_check (path2, 1, BT_CHARACTER))
3708 return false;
3709 if (!kind_value_check (path2, 1, gfc_default_character_kind))
3710 return false;
3712 return true;
3716 bool
3717 gfc_check_rename_sub (gfc_expr *path1, gfc_expr *path2, gfc_expr *status)
3719 if (!type_check (path1, 0, BT_CHARACTER))
3720 return false;
3721 if (!kind_value_check (path1, 0, gfc_default_character_kind))
3722 return false;
3724 if (!type_check (path2, 1, BT_CHARACTER))
3725 return false;
3726 if (!kind_value_check (path2, 1, gfc_default_character_kind))
3727 return false;
3729 if (status == NULL)
3730 return true;
3732 if (!type_check (status, 2, BT_INTEGER))
3733 return false;
3735 if (!scalar_check (status, 2))
3736 return false;
3738 return true;
3742 bool
3743 gfc_check_repeat (gfc_expr *x, gfc_expr *y)
3745 if (!type_check (x, 0, BT_CHARACTER))
3746 return false;
3748 if (!scalar_check (x, 0))
3749 return false;
3751 if (!type_check (y, 0, BT_INTEGER))
3752 return false;
3754 if (!scalar_check (y, 1))
3755 return false;
3757 return true;
3761 bool
3762 gfc_check_reshape (gfc_expr *source, gfc_expr *shape,
3763 gfc_expr *pad, gfc_expr *order)
3765 mpz_t size;
3766 mpz_t nelems;
3767 int shape_size;
3769 if (!array_check (source, 0))
3770 return false;
3772 if (!rank_check (shape, 1, 1))
3773 return false;
3775 if (!type_check (shape, 1, BT_INTEGER))
3776 return false;
3778 if (!gfc_array_size (shape, &size))
3780 gfc_error ("%<shape%> argument of %<reshape%> intrinsic at %L must be an "
3781 "array of constant size", &shape->where);
3782 return false;
3785 shape_size = mpz_get_ui (size);
3786 mpz_clear (size);
3788 if (shape_size <= 0)
3790 gfc_error ("%qs argument of %qs intrinsic at %L is empty",
3791 gfc_current_intrinsic_arg[1]->name, gfc_current_intrinsic,
3792 &shape->where);
3793 return false;
3795 else if (shape_size > GFC_MAX_DIMENSIONS)
3797 gfc_error ("%<shape%> argument of %<reshape%> intrinsic at %L has more "
3798 "than %d elements", &shape->where, GFC_MAX_DIMENSIONS);
3799 return false;
3801 else if (shape->expr_type == EXPR_ARRAY && gfc_is_constant_expr (shape))
3803 gfc_expr *e;
3804 int i, extent;
3805 for (i = 0; i < shape_size; ++i)
3807 e = gfc_constructor_lookup_expr (shape->value.constructor, i);
3808 if (e->expr_type != EXPR_CONSTANT)
3809 continue;
3811 gfc_extract_int (e, &extent);
3812 if (extent < 0)
3814 gfc_error ("%qs argument of %qs intrinsic at %L has "
3815 "negative element (%d)",
3816 gfc_current_intrinsic_arg[1]->name,
3817 gfc_current_intrinsic, &e->where, extent);
3818 return false;
3822 else if (shape->expr_type == EXPR_VARIABLE && shape->ref
3823 && shape->ref->u.ar.type == AR_FULL && shape->ref->u.ar.dimen == 1
3824 && shape->ref->u.ar.as
3825 && shape->ref->u.ar.as->lower[0]->expr_type == EXPR_CONSTANT
3826 && shape->ref->u.ar.as->lower[0]->ts.type == BT_INTEGER
3827 && shape->ref->u.ar.as->upper[0]->expr_type == EXPR_CONSTANT
3828 && shape->ref->u.ar.as->upper[0]->ts.type == BT_INTEGER
3829 && shape->symtree->n.sym->attr.flavor == FL_PARAMETER)
3831 int i, extent;
3832 gfc_expr *e, *v;
3834 v = shape->symtree->n.sym->value;
3836 for (i = 0; i < shape_size; i++)
3838 e = gfc_constructor_lookup_expr (v->value.constructor, i);
3839 if (e == NULL)
3840 break;
3842 gfc_extract_int (e, &extent);
3844 if (extent < 0)
3846 gfc_error ("Element %d of actual argument of RESHAPE at %L "
3847 "cannot be negative", i + 1, &shape->where);
3848 return false;
3853 if (pad != NULL)
3855 if (!same_type_check (source, 0, pad, 2))
3856 return false;
3858 if (!array_check (pad, 2))
3859 return false;
3862 if (order != NULL)
3864 if (!array_check (order, 3))
3865 return false;
3867 if (!type_check (order, 3, BT_INTEGER))
3868 return false;
3870 if (order->expr_type == EXPR_ARRAY && gfc_is_constant_expr (order))
3872 int i, order_size, dim, perm[GFC_MAX_DIMENSIONS];
3873 gfc_expr *e;
3875 for (i = 0; i < GFC_MAX_DIMENSIONS; ++i)
3876 perm[i] = 0;
3878 gfc_array_size (order, &size);
3879 order_size = mpz_get_ui (size);
3880 mpz_clear (size);
3882 if (order_size != shape_size)
3884 gfc_error ("%qs argument of %qs intrinsic at %L "
3885 "has wrong number of elements (%d/%d)",
3886 gfc_current_intrinsic_arg[3]->name,
3887 gfc_current_intrinsic, &order->where,
3888 order_size, shape_size);
3889 return false;
3892 for (i = 1; i <= order_size; ++i)
3894 e = gfc_constructor_lookup_expr (order->value.constructor, i-1);
3895 if (e->expr_type != EXPR_CONSTANT)
3896 continue;
3898 gfc_extract_int (e, &dim);
3900 if (dim < 1 || dim > order_size)
3902 gfc_error ("%qs argument of %qs intrinsic at %L "
3903 "has out-of-range dimension (%d)",
3904 gfc_current_intrinsic_arg[3]->name,
3905 gfc_current_intrinsic, &e->where, dim);
3906 return false;
3909 if (perm[dim-1] != 0)
3911 gfc_error ("%qs argument of %qs intrinsic at %L has "
3912 "invalid permutation of dimensions (dimension "
3913 "%qd duplicated)",
3914 gfc_current_intrinsic_arg[3]->name,
3915 gfc_current_intrinsic, &e->where, dim);
3916 return false;
3919 perm[dim-1] = 1;
3924 if (pad == NULL && shape->expr_type == EXPR_ARRAY
3925 && gfc_is_constant_expr (shape)
3926 && !(source->expr_type == EXPR_VARIABLE && source->symtree->n.sym->as
3927 && source->symtree->n.sym->as->type == AS_ASSUMED_SIZE))
3929 /* Check the match in size between source and destination. */
3930 if (gfc_array_size (source, &nelems))
3932 gfc_constructor *c;
3933 bool test;
3936 mpz_init_set_ui (size, 1);
3937 for (c = gfc_constructor_first (shape->value.constructor);
3938 c; c = gfc_constructor_next (c))
3939 mpz_mul (size, size, c->expr->value.integer);
3941 test = mpz_cmp (nelems, size) < 0 && mpz_cmp_ui (size, 0) > 0;
3942 mpz_clear (nelems);
3943 mpz_clear (size);
3945 if (test)
3947 gfc_error ("Without padding, there are not enough elements "
3948 "in the intrinsic RESHAPE source at %L to match "
3949 "the shape", &source->where);
3950 return false;
3955 return true;
3959 bool
3960 gfc_check_same_type_as (gfc_expr *a, gfc_expr *b)
3962 if (a->ts.type != BT_DERIVED && a->ts.type != BT_CLASS)
3964 gfc_error ("%qs argument of %qs intrinsic at %L "
3965 "cannot be of type %s",
3966 gfc_current_intrinsic_arg[0]->name,
3967 gfc_current_intrinsic,
3968 &a->where, gfc_typename (&a->ts));
3969 return false;
3972 if (!(gfc_type_is_extensible (a->ts.u.derived) || UNLIMITED_POLY (a)))
3974 gfc_error ("%qs argument of %qs intrinsic at %L "
3975 "must be of an extensible type",
3976 gfc_current_intrinsic_arg[0]->name, gfc_current_intrinsic,
3977 &a->where);
3978 return false;
3981 if (b->ts.type != BT_DERIVED && b->ts.type != BT_CLASS)
3983 gfc_error ("%qs argument of %qs intrinsic at %L "
3984 "cannot be of type %s",
3985 gfc_current_intrinsic_arg[0]->name,
3986 gfc_current_intrinsic,
3987 &b->where, gfc_typename (&b->ts));
3988 return false;
3991 if (!(gfc_type_is_extensible (b->ts.u.derived) || UNLIMITED_POLY (b)))
3993 gfc_error ("%qs argument of %qs intrinsic at %L "
3994 "must be of an extensible type",
3995 gfc_current_intrinsic_arg[1]->name, gfc_current_intrinsic,
3996 &b->where);
3997 return false;
4000 return true;
4004 bool
4005 gfc_check_scale (gfc_expr *x, gfc_expr *i)
4007 if (!type_check (x, 0, BT_REAL))
4008 return false;
4010 if (!type_check (i, 1, BT_INTEGER))
4011 return false;
4013 return true;
4017 bool
4018 gfc_check_scan (gfc_expr *x, gfc_expr *y, gfc_expr *z, gfc_expr *kind)
4020 if (!type_check (x, 0, BT_CHARACTER))
4021 return false;
4023 if (!type_check (y, 1, BT_CHARACTER))
4024 return false;
4026 if (z != NULL && !type_check (z, 2, BT_LOGICAL))
4027 return false;
4029 if (!kind_check (kind, 3, BT_INTEGER))
4030 return false;
4031 if (kind && !gfc_notify_std (GFC_STD_F2003, "%qs intrinsic "
4032 "with KIND argument at %L",
4033 gfc_current_intrinsic, &kind->where))
4034 return false;
4036 if (!same_type_check (x, 0, y, 1))
4037 return false;
4039 return true;
4043 bool
4044 gfc_check_secnds (gfc_expr *r)
4046 if (!type_check (r, 0, BT_REAL))
4047 return false;
4049 if (!kind_value_check (r, 0, 4))
4050 return false;
4052 if (!scalar_check (r, 0))
4053 return false;
4055 return true;
4059 bool
4060 gfc_check_selected_char_kind (gfc_expr *name)
4062 if (!type_check (name, 0, BT_CHARACTER))
4063 return false;
4065 if (!kind_value_check (name, 0, gfc_default_character_kind))
4066 return false;
4068 if (!scalar_check (name, 0))
4069 return false;
4071 return true;
4075 bool
4076 gfc_check_selected_int_kind (gfc_expr *r)
4078 if (!type_check (r, 0, BT_INTEGER))
4079 return false;
4081 if (!scalar_check (r, 0))
4082 return false;
4084 return true;
4088 bool
4089 gfc_check_selected_real_kind (gfc_expr *p, gfc_expr *r, gfc_expr *radix)
4091 if (p == NULL && r == NULL
4092 && !gfc_notify_std (GFC_STD_F2008, "SELECTED_REAL_KIND with"
4093 " neither %<P%> nor %<R%> argument at %L",
4094 gfc_current_intrinsic_where))
4095 return false;
4097 if (p)
4099 if (!type_check (p, 0, BT_INTEGER))
4100 return false;
4102 if (!scalar_check (p, 0))
4103 return false;
4106 if (r)
4108 if (!type_check (r, 1, BT_INTEGER))
4109 return false;
4111 if (!scalar_check (r, 1))
4112 return false;
4115 if (radix)
4117 if (!type_check (radix, 1, BT_INTEGER))
4118 return false;
4120 if (!scalar_check (radix, 1))
4121 return false;
4123 if (!gfc_notify_std (GFC_STD_F2008, "%qs intrinsic with "
4124 "RADIX argument at %L", gfc_current_intrinsic,
4125 &radix->where))
4126 return false;
4129 return true;
4133 bool
4134 gfc_check_set_exponent (gfc_expr *x, gfc_expr *i)
4136 if (!type_check (x, 0, BT_REAL))
4137 return false;
4139 if (!type_check (i, 1, BT_INTEGER))
4140 return false;
4142 return true;
4146 bool
4147 gfc_check_shape (gfc_expr *source, gfc_expr *kind)
4149 gfc_array_ref *ar;
4151 if (source->rank == 0 || source->expr_type != EXPR_VARIABLE)
4152 return true;
4154 ar = gfc_find_array_ref (source);
4156 if (ar->as && ar->as->type == AS_ASSUMED_SIZE && ar->type == AR_FULL)
4158 gfc_error ("%<source%> argument of %<shape%> intrinsic at %L must not be "
4159 "an assumed size array", &source->where);
4160 return false;
4163 if (!kind_check (kind, 1, BT_INTEGER))
4164 return false;
4165 if (kind && !gfc_notify_std (GFC_STD_F2003, "%qs intrinsic "
4166 "with KIND argument at %L",
4167 gfc_current_intrinsic, &kind->where))
4168 return false;
4170 return true;
4174 bool
4175 gfc_check_shift (gfc_expr *i, gfc_expr *shift)
4177 if (!type_check (i, 0, BT_INTEGER))
4178 return false;
4180 if (!type_check (shift, 0, BT_INTEGER))
4181 return false;
4183 if (!nonnegative_check ("SHIFT", shift))
4184 return false;
4186 if (!less_than_bitsize1 ("I", i, "SHIFT", shift, true))
4187 return false;
4189 return true;
4193 bool
4194 gfc_check_sign (gfc_expr *a, gfc_expr *b)
4196 if (!int_or_real_check (a, 0))
4197 return false;
4199 if (!same_type_check (a, 0, b, 1))
4200 return false;
4202 return true;
4206 bool
4207 gfc_check_size (gfc_expr *array, gfc_expr *dim, gfc_expr *kind)
4209 if (!array_check (array, 0))
4210 return false;
4212 if (!dim_check (dim, 1, true))
4213 return false;
4215 if (!dim_rank_check (dim, array, 0))
4216 return false;
4218 if (!kind_check (kind, 2, BT_INTEGER))
4219 return false;
4220 if (kind && !gfc_notify_std (GFC_STD_F2003, "%qs intrinsic "
4221 "with KIND argument at %L",
4222 gfc_current_intrinsic, &kind->where))
4223 return false;
4226 return true;
4230 bool
4231 gfc_check_sizeof (gfc_expr *arg)
4233 if (arg->ts.type == BT_PROCEDURE)
4235 gfc_error ("%qs argument of %qs intrinsic at %L shall not be a procedure",
4236 gfc_current_intrinsic_arg[0]->name, gfc_current_intrinsic,
4237 &arg->where);
4238 return false;
4241 /* TYPE(*) is acceptable if and only if it uses an array descriptor. */
4242 if (arg->ts.type == BT_ASSUMED
4243 && (arg->symtree->n.sym->as == NULL
4244 || (arg->symtree->n.sym->as->type != AS_ASSUMED_SHAPE
4245 && arg->symtree->n.sym->as->type != AS_DEFERRED
4246 && arg->symtree->n.sym->as->type != AS_ASSUMED_RANK)))
4248 gfc_error ("%qs argument of %qs intrinsic at %L shall not be TYPE(*)",
4249 gfc_current_intrinsic_arg[0]->name, gfc_current_intrinsic,
4250 &arg->where);
4251 return false;
4254 if (arg->rank && arg->expr_type == EXPR_VARIABLE
4255 && arg->symtree->n.sym->as != NULL
4256 && arg->symtree->n.sym->as->type == AS_ASSUMED_SIZE && arg->ref
4257 && arg->ref->type == REF_ARRAY && arg->ref->u.ar.type == AR_FULL)
4259 gfc_error ("%qs argument of %qs intrinsic at %L shall not be an "
4260 "assumed-size array", gfc_current_intrinsic_arg[0]->name,
4261 gfc_current_intrinsic, &arg->where);
4262 return false;
4265 return true;
4269 /* Check whether an expression is interoperable. When returning false,
4270 msg is set to a string telling why the expression is not interoperable,
4271 otherwise, it is set to NULL. The msg string can be used in diagnostics.
4272 If c_loc is true, character with len > 1 are allowed (cf. Fortran
4273 2003corr5); additionally, assumed-shape/assumed-rank/deferred-shape
4274 arrays are permitted. And if c_f_ptr is true, deferred-shape arrays
4275 are permitted. */
4277 static bool
4278 is_c_interoperable (gfc_expr *expr, const char **msg, bool c_loc, bool c_f_ptr)
4280 *msg = NULL;
4282 if (expr->ts.type == BT_CLASS)
4284 *msg = "Expression is polymorphic";
4285 return false;
4288 if (expr->ts.type == BT_DERIVED && !expr->ts.u.derived->attr.is_bind_c
4289 && !expr->ts.u.derived->ts.is_iso_c)
4291 *msg = "Expression is a noninteroperable derived type";
4292 return false;
4295 if (expr->ts.type == BT_PROCEDURE)
4297 *msg = "Procedure unexpected as argument";
4298 return false;
4301 if (gfc_notification_std (GFC_STD_GNU) && expr->ts.type == BT_LOGICAL)
4303 int i;
4304 for (i = 0; gfc_logical_kinds[i].kind; i++)
4305 if (gfc_logical_kinds[i].kind == expr->ts.kind)
4306 return true;
4307 *msg = "Extension to use a non-C_Bool-kind LOGICAL";
4308 return false;
4311 if (gfc_notification_std (GFC_STD_GNU) && expr->ts.type == BT_CHARACTER
4312 && expr->ts.kind != 1)
4314 *msg = "Extension to use a non-C_CHAR-kind CHARACTER";
4315 return false;
4318 if (expr->ts.type == BT_CHARACTER) {
4319 if (expr->ts.deferred)
4321 /* TS 29113 allows deferred-length strings as dummy arguments,
4322 but it is not an interoperable type. */
4323 *msg = "Expression shall not be a deferred-length string";
4324 return false;
4327 if (expr->ts.u.cl && expr->ts.u.cl->length
4328 && !gfc_simplify_expr (expr->ts.u.cl->length, 0))
4329 gfc_internal_error ("is_c_interoperable(): gfc_simplify_expr failed");
4331 if (!c_loc && expr->ts.u.cl
4332 && (!expr->ts.u.cl->length
4333 || expr->ts.u.cl->length->expr_type != EXPR_CONSTANT
4334 || mpz_cmp_si (expr->ts.u.cl->length->value.integer, 1) != 0))
4336 *msg = "Type shall have a character length of 1";
4337 return false;
4341 /* Note: The following checks are about interoperatable variables, Fortran
4342 15.3.5/15.3.6. In intrinsics like C_LOC or in procedure interface, more
4343 is allowed, e.g. assumed-shape arrays with TS 29113. */
4345 if (gfc_is_coarray (expr))
4347 *msg = "Coarrays are not interoperable";
4348 return false;
4351 if (!c_loc && expr->rank > 0 && expr->expr_type != EXPR_ARRAY)
4353 gfc_array_ref *ar = gfc_find_array_ref (expr);
4354 if (ar->type != AR_FULL)
4356 *msg = "Only whole-arrays are interoperable";
4357 return false;
4359 if (!c_f_ptr && ar->as->type != AS_EXPLICIT
4360 && ar->as->type != AS_ASSUMED_SIZE)
4362 *msg = "Only explicit-size and assumed-size arrays are interoperable";
4363 return false;
4367 return true;
4371 bool
4372 gfc_check_c_sizeof (gfc_expr *arg)
4374 const char *msg;
4376 if (!is_c_interoperable (arg, &msg, false, false))
4378 gfc_error ("%qs argument of %qs intrinsic at %L must be an "
4379 "interoperable data entity: %s",
4380 gfc_current_intrinsic_arg[0]->name, gfc_current_intrinsic,
4381 &arg->where, msg);
4382 return false;
4385 if (arg->ts.type == BT_ASSUMED)
4387 gfc_error ("%qs argument of %qs intrinsic at %L shall not be "
4388 "TYPE(*)",
4389 gfc_current_intrinsic_arg[0]->name, gfc_current_intrinsic,
4390 &arg->where);
4391 return false;
4394 if (arg->rank && arg->expr_type == EXPR_VARIABLE
4395 && arg->symtree->n.sym->as != NULL
4396 && arg->symtree->n.sym->as->type == AS_ASSUMED_SIZE && arg->ref
4397 && arg->ref->type == REF_ARRAY && arg->ref->u.ar.type == AR_FULL)
4399 gfc_error ("%qs argument of %qs intrinsic at %L shall not be an "
4400 "assumed-size array", gfc_current_intrinsic_arg[0]->name,
4401 gfc_current_intrinsic, &arg->where);
4402 return false;
4405 return true;
4409 bool
4410 gfc_check_c_associated (gfc_expr *c_ptr_1, gfc_expr *c_ptr_2)
4412 if (c_ptr_1->ts.type != BT_DERIVED
4413 || c_ptr_1->ts.u.derived->from_intmod != INTMOD_ISO_C_BINDING
4414 || (c_ptr_1->ts.u.derived->intmod_sym_id != ISOCBINDING_PTR
4415 && c_ptr_1->ts.u.derived->intmod_sym_id != ISOCBINDING_FUNPTR))
4417 gfc_error ("Argument C_PTR_1 at %L to C_ASSOCIATED shall have the "
4418 "type TYPE(C_PTR) or TYPE(C_FUNPTR)", &c_ptr_1->where);
4419 return false;
4422 if (!scalar_check (c_ptr_1, 0))
4423 return false;
4425 if (c_ptr_2
4426 && (c_ptr_2->ts.type != BT_DERIVED
4427 || c_ptr_2->ts.u.derived->from_intmod != INTMOD_ISO_C_BINDING
4428 || (c_ptr_1->ts.u.derived->intmod_sym_id
4429 != c_ptr_2->ts.u.derived->intmod_sym_id)))
4431 gfc_error ("Argument C_PTR_2 at %L to C_ASSOCIATED shall have the "
4432 "same type as C_PTR_1: %s instead of %s", &c_ptr_1->where,
4433 gfc_typename (&c_ptr_1->ts),
4434 gfc_typename (&c_ptr_2->ts));
4435 return false;
4438 if (c_ptr_2 && !scalar_check (c_ptr_2, 1))
4439 return false;
4441 return true;
4445 bool
4446 gfc_check_c_f_pointer (gfc_expr *cptr, gfc_expr *fptr, gfc_expr *shape)
4448 symbol_attribute attr;
4449 const char *msg;
4451 if (cptr->ts.type != BT_DERIVED
4452 || cptr->ts.u.derived->from_intmod != INTMOD_ISO_C_BINDING
4453 || cptr->ts.u.derived->intmod_sym_id != ISOCBINDING_PTR)
4455 gfc_error ("Argument CPTR at %L to C_F_POINTER shall have the "
4456 "type TYPE(C_PTR)", &cptr->where);
4457 return false;
4460 if (!scalar_check (cptr, 0))
4461 return false;
4463 attr = gfc_expr_attr (fptr);
4465 if (!attr.pointer)
4467 gfc_error ("Argument FPTR at %L to C_F_POINTER must be a pointer",
4468 &fptr->where);
4469 return false;
4472 if (fptr->ts.type == BT_CLASS)
4474 gfc_error ("FPTR argument at %L to C_F_POINTER shall not be polymorphic",
4475 &fptr->where);
4476 return false;
4479 if (gfc_is_coindexed (fptr))
4481 gfc_error ("Argument FPTR at %L to C_F_POINTER shall not be "
4482 "coindexed", &fptr->where);
4483 return false;
4486 if (fptr->rank == 0 && shape)
4488 gfc_error ("Unexpected SHAPE argument at %L to C_F_POINTER with scalar "
4489 "FPTR", &fptr->where);
4490 return false;
4492 else if (fptr->rank && !shape)
4494 gfc_error ("Expected SHAPE argument to C_F_POINTER with array "
4495 "FPTR at %L", &fptr->where);
4496 return false;
4499 if (shape && !rank_check (shape, 2, 1))
4500 return false;
4502 if (shape && !type_check (shape, 2, BT_INTEGER))
4503 return false;
4505 if (shape)
4507 mpz_t size;
4508 if (gfc_array_size (shape, &size))
4510 if (mpz_cmp_ui (size, fptr->rank) != 0)
4512 mpz_clear (size);
4513 gfc_error ("SHAPE argument at %L to C_F_POINTER must have the same "
4514 "size as the RANK of FPTR", &shape->where);
4515 return false;
4517 mpz_clear (size);
4521 if (fptr->ts.type == BT_CLASS)
4523 gfc_error ("Polymorphic FPTR at %L to C_F_POINTER", &fptr->where);
4524 return false;
4527 if (!is_c_interoperable (fptr, &msg, false, true))
4528 return gfc_notify_std (GFC_STD_F2008_TS, "Noninteroperable array FPTR "
4529 "at %L to C_F_POINTER: %s", &fptr->where, msg);
4531 return true;
4535 bool
4536 gfc_check_c_f_procpointer (gfc_expr *cptr, gfc_expr *fptr)
4538 symbol_attribute attr;
4540 if (cptr->ts.type != BT_DERIVED
4541 || cptr->ts.u.derived->from_intmod != INTMOD_ISO_C_BINDING
4542 || cptr->ts.u.derived->intmod_sym_id != ISOCBINDING_FUNPTR)
4544 gfc_error ("Argument CPTR at %L to C_F_PROCPOINTER shall have the "
4545 "type TYPE(C_FUNPTR)", &cptr->where);
4546 return false;
4549 if (!scalar_check (cptr, 0))
4550 return false;
4552 attr = gfc_expr_attr (fptr);
4554 if (!attr.proc_pointer)
4556 gfc_error ("Argument FPTR at %L to C_F_PROCPOINTER shall be a procedure "
4557 "pointer", &fptr->where);
4558 return false;
4561 if (gfc_is_coindexed (fptr))
4563 gfc_error ("Argument FPTR at %L to C_F_PROCPOINTER shall not be "
4564 "coindexed", &fptr->where);
4565 return false;
4568 if (!attr.is_bind_c)
4569 return gfc_notify_std (GFC_STD_F2008_TS, "Noninteroperable procedure "
4570 "pointer at %L to C_F_PROCPOINTER", &fptr->where);
4572 return true;
4576 bool
4577 gfc_check_c_funloc (gfc_expr *x)
4579 symbol_attribute attr;
4581 if (gfc_is_coindexed (x))
4583 gfc_error ("Argument X at %L to C_FUNLOC shall not be "
4584 "coindexed", &x->where);
4585 return false;
4588 attr = gfc_expr_attr (x);
4590 if (attr.function && !attr.proc_pointer && x->expr_type == EXPR_VARIABLE
4591 && x->symtree->n.sym == x->symtree->n.sym->result)
4593 gfc_namespace *ns = gfc_current_ns;
4595 for (ns = gfc_current_ns; ns; ns = ns->parent)
4596 if (x->symtree->n.sym == ns->proc_name)
4598 gfc_error ("Function result %qs at %L is invalid as X argument "
4599 "to C_FUNLOC", x->symtree->n.sym->name, &x->where);
4600 return false;
4604 if (attr.flavor != FL_PROCEDURE)
4606 gfc_error ("Argument X at %L to C_FUNLOC shall be a procedure "
4607 "or a procedure pointer", &x->where);
4608 return false;
4611 if (!attr.is_bind_c)
4612 return gfc_notify_std (GFC_STD_F2008_TS, "Noninteroperable procedure "
4613 "at %L to C_FUNLOC", &x->where);
4614 return true;
4618 bool
4619 gfc_check_c_loc (gfc_expr *x)
4621 symbol_attribute attr;
4622 const char *msg;
4624 if (gfc_is_coindexed (x))
4626 gfc_error ("Argument X at %L to C_LOC shall not be coindexed", &x->where);
4627 return false;
4630 if (x->ts.type == BT_CLASS)
4632 gfc_error ("X argument at %L to C_LOC shall not be polymorphic",
4633 &x->where);
4634 return false;
4637 attr = gfc_expr_attr (x);
4639 if (!attr.pointer
4640 && (x->expr_type != EXPR_VARIABLE || !attr.target
4641 || attr.flavor == FL_PARAMETER))
4643 gfc_error ("Argument X at %L to C_LOC shall have either "
4644 "the POINTER or the TARGET attribute", &x->where);
4645 return false;
4648 if (x->ts.type == BT_CHARACTER
4649 && gfc_var_strlen (x) == 0)
4651 gfc_error ("Argument X at %L to C_LOC shall be not be a zero-sized "
4652 "string", &x->where);
4653 return false;
4656 if (!is_c_interoperable (x, &msg, true, false))
4658 if (x->ts.type == BT_CLASS)
4660 gfc_error ("Argument at %L to C_LOC shall not be polymorphic",
4661 &x->where);
4662 return false;
4665 if (x->rank
4666 && !gfc_notify_std (GFC_STD_F2008_TS,
4667 "Noninteroperable array at %L as"
4668 " argument to C_LOC: %s", &x->where, msg))
4669 return false;
4671 else if (x->rank > 0 && gfc_notification_std (GFC_STD_F2008))
4673 gfc_array_ref *ar = gfc_find_array_ref (x);
4675 if (ar->as->type != AS_EXPLICIT && ar->as->type != AS_ASSUMED_SIZE
4676 && !attr.allocatable
4677 && !gfc_notify_std (GFC_STD_F2008,
4678 "Array of interoperable type at %L "
4679 "to C_LOC which is nonallocatable and neither "
4680 "assumed size nor explicit size", &x->where))
4681 return false;
4682 else if (ar->type != AR_FULL
4683 && !gfc_notify_std (GFC_STD_F2008, "Array section at %L "
4684 "to C_LOC", &x->where))
4685 return false;
4688 return true;
4692 bool
4693 gfc_check_sleep_sub (gfc_expr *seconds)
4695 if (!type_check (seconds, 0, BT_INTEGER))
4696 return false;
4698 if (!scalar_check (seconds, 0))
4699 return false;
4701 return true;
4704 bool
4705 gfc_check_sngl (gfc_expr *a)
4707 if (!type_check (a, 0, BT_REAL))
4708 return false;
4710 if ((a->ts.kind != gfc_default_double_kind)
4711 && !gfc_notify_std (GFC_STD_GNU, "non double precision "
4712 "REAL argument to %s intrinsic at %L",
4713 gfc_current_intrinsic, &a->where))
4714 return false;
4716 return true;
4719 bool
4720 gfc_check_spread (gfc_expr *source, gfc_expr *dim, gfc_expr *ncopies)
4722 if (source->rank >= GFC_MAX_DIMENSIONS)
4724 gfc_error ("%qs argument of %qs intrinsic at %L must be less "
4725 "than rank %d", gfc_current_intrinsic_arg[0]->name,
4726 gfc_current_intrinsic, &source->where, GFC_MAX_DIMENSIONS);
4728 return false;
4731 if (dim == NULL)
4732 return false;
4734 if (!dim_check (dim, 1, false))
4735 return false;
4737 /* dim_rank_check() does not apply here. */
4738 if (dim
4739 && dim->expr_type == EXPR_CONSTANT
4740 && (mpz_cmp_ui (dim->value.integer, 1) < 0
4741 || mpz_cmp_ui (dim->value.integer, source->rank + 1) > 0))
4743 gfc_error ("%qs argument of %qs intrinsic at %L is not a valid "
4744 "dimension index", gfc_current_intrinsic_arg[1]->name,
4745 gfc_current_intrinsic, &dim->where);
4746 return false;
4749 if (!type_check (ncopies, 2, BT_INTEGER))
4750 return false;
4752 if (!scalar_check (ncopies, 2))
4753 return false;
4755 return true;
4759 /* Functions for checking FGETC, FPUTC, FGET and FPUT (subroutines and
4760 functions). */
4762 bool
4763 gfc_check_fgetputc_sub (gfc_expr *unit, gfc_expr *c, gfc_expr *status)
4765 if (!type_check (unit, 0, BT_INTEGER))
4766 return false;
4768 if (!scalar_check (unit, 0))
4769 return false;
4771 if (!type_check (c, 1, BT_CHARACTER))
4772 return false;
4773 if (!kind_value_check (c, 1, gfc_default_character_kind))
4774 return false;
4776 if (status == NULL)
4777 return true;
4779 if (!type_check (status, 2, BT_INTEGER)
4780 || !kind_value_check (status, 2, gfc_default_integer_kind)
4781 || !scalar_check (status, 2))
4782 return false;
4784 return true;
4788 bool
4789 gfc_check_fgetputc (gfc_expr *unit, gfc_expr *c)
4791 return gfc_check_fgetputc_sub (unit, c, NULL);
4795 bool
4796 gfc_check_fgetput_sub (gfc_expr *c, gfc_expr *status)
4798 if (!type_check (c, 0, BT_CHARACTER))
4799 return false;
4800 if (!kind_value_check (c, 0, gfc_default_character_kind))
4801 return false;
4803 if (status == NULL)
4804 return true;
4806 if (!type_check (status, 1, BT_INTEGER)
4807 || !kind_value_check (status, 1, gfc_default_integer_kind)
4808 || !scalar_check (status, 1))
4809 return false;
4811 return true;
4815 bool
4816 gfc_check_fgetput (gfc_expr *c)
4818 return gfc_check_fgetput_sub (c, NULL);
4822 bool
4823 gfc_check_fseek_sub (gfc_expr *unit, gfc_expr *offset, gfc_expr *whence, gfc_expr *status)
4825 if (!type_check (unit, 0, BT_INTEGER))
4826 return false;
4828 if (!scalar_check (unit, 0))
4829 return false;
4831 if (!type_check (offset, 1, BT_INTEGER))
4832 return false;
4834 if (!scalar_check (offset, 1))
4835 return false;
4837 if (!type_check (whence, 2, BT_INTEGER))
4838 return false;
4840 if (!scalar_check (whence, 2))
4841 return false;
4843 if (status == NULL)
4844 return true;
4846 if (!type_check (status, 3, BT_INTEGER))
4847 return false;
4849 if (!kind_value_check (status, 3, 4))
4850 return false;
4852 if (!scalar_check (status, 3))
4853 return false;
4855 return true;
4860 bool
4861 gfc_check_fstat (gfc_expr *unit, gfc_expr *array)
4863 if (!type_check (unit, 0, BT_INTEGER))
4864 return false;
4866 if (!scalar_check (unit, 0))
4867 return false;
4869 if (!type_check (array, 1, BT_INTEGER)
4870 || !kind_value_check (unit, 0, gfc_default_integer_kind))
4871 return false;
4873 if (!array_check (array, 1))
4874 return false;
4876 return true;
4880 bool
4881 gfc_check_fstat_sub (gfc_expr *unit, gfc_expr *array, gfc_expr *status)
4883 if (!type_check (unit, 0, BT_INTEGER))
4884 return false;
4886 if (!scalar_check (unit, 0))
4887 return false;
4889 if (!type_check (array, 1, BT_INTEGER)
4890 || !kind_value_check (array, 1, gfc_default_integer_kind))
4891 return false;
4893 if (!array_check (array, 1))
4894 return false;
4896 if (status == NULL)
4897 return true;
4899 if (!type_check (status, 2, BT_INTEGER)
4900 || !kind_value_check (status, 2, gfc_default_integer_kind))
4901 return false;
4903 if (!scalar_check (status, 2))
4904 return false;
4906 return true;
4910 bool
4911 gfc_check_ftell (gfc_expr *unit)
4913 if (!type_check (unit, 0, BT_INTEGER))
4914 return false;
4916 if (!scalar_check (unit, 0))
4917 return false;
4919 return true;
4923 bool
4924 gfc_check_ftell_sub (gfc_expr *unit, gfc_expr *offset)
4926 if (!type_check (unit, 0, BT_INTEGER))
4927 return false;
4929 if (!scalar_check (unit, 0))
4930 return false;
4932 if (!type_check (offset, 1, BT_INTEGER))
4933 return false;
4935 if (!scalar_check (offset, 1))
4936 return false;
4938 return true;
4942 bool
4943 gfc_check_stat (gfc_expr *name, gfc_expr *array)
4945 if (!type_check (name, 0, BT_CHARACTER))
4946 return false;
4947 if (!kind_value_check (name, 0, gfc_default_character_kind))
4948 return false;
4950 if (!type_check (array, 1, BT_INTEGER)
4951 || !kind_value_check (array, 1, gfc_default_integer_kind))
4952 return false;
4954 if (!array_check (array, 1))
4955 return false;
4957 return true;
4961 bool
4962 gfc_check_stat_sub (gfc_expr *name, gfc_expr *array, gfc_expr *status)
4964 if (!type_check (name, 0, BT_CHARACTER))
4965 return false;
4966 if (!kind_value_check (name, 0, gfc_default_character_kind))
4967 return false;
4969 if (!type_check (array, 1, BT_INTEGER)
4970 || !kind_value_check (array, 1, gfc_default_integer_kind))
4971 return false;
4973 if (!array_check (array, 1))
4974 return false;
4976 if (status == NULL)
4977 return true;
4979 if (!type_check (status, 2, BT_INTEGER)
4980 || !kind_value_check (array, 1, gfc_default_integer_kind))
4981 return false;
4983 if (!scalar_check (status, 2))
4984 return false;
4986 return true;
4990 bool
4991 gfc_check_image_index (gfc_expr *coarray, gfc_expr *sub)
4993 mpz_t nelems;
4995 if (flag_coarray == GFC_FCOARRAY_NONE)
4997 gfc_fatal_error ("Coarrays disabled at %C, use %<-fcoarray=%> to enable");
4998 return false;
5001 if (!coarray_check (coarray, 0))
5002 return false;
5004 if (sub->rank != 1)
5006 gfc_error ("%s argument to IMAGE_INDEX must be a rank one array at %L",
5007 gfc_current_intrinsic_arg[1]->name, &sub->where);
5008 return false;
5011 if (gfc_array_size (sub, &nelems))
5013 int corank = gfc_get_corank (coarray);
5015 if (mpz_cmp_ui (nelems, corank) != 0)
5017 gfc_error ("The number of array elements of the SUB argument to "
5018 "IMAGE_INDEX at %L shall be %d (corank) not %d",
5019 &sub->where, corank, (int) mpz_get_si (nelems));
5020 mpz_clear (nelems);
5021 return false;
5023 mpz_clear (nelems);
5026 return true;
5030 bool
5031 gfc_check_num_images (gfc_expr *distance, gfc_expr *failed)
5033 if (flag_coarray == GFC_FCOARRAY_NONE)
5035 gfc_fatal_error ("Coarrays disabled at %C, use %<-fcoarray=%> to enable");
5036 return false;
5039 if (distance)
5041 if (!type_check (distance, 0, BT_INTEGER))
5042 return false;
5044 if (!nonnegative_check ("DISTANCE", distance))
5045 return false;
5047 if (!scalar_check (distance, 0))
5048 return false;
5050 if (!gfc_notify_std (GFC_STD_F2008_TS, "DISTANCE= argument to "
5051 "NUM_IMAGES at %L", &distance->where))
5052 return false;
5055 if (failed)
5057 if (!type_check (failed, 1, BT_LOGICAL))
5058 return false;
5060 if (!scalar_check (failed, 1))
5061 return false;
5063 if (!gfc_notify_std (GFC_STD_F2008_TS, "FAILED= argument to "
5064 "NUM_IMAGES at %L", &distance->where))
5065 return false;
5068 return true;
5072 bool
5073 gfc_check_this_image (gfc_expr *coarray, gfc_expr *dim, gfc_expr *distance)
5075 if (flag_coarray == GFC_FCOARRAY_NONE)
5077 gfc_fatal_error ("Coarrays disabled at %C, use %<-fcoarray=%> to enable");
5078 return false;
5081 if (coarray == NULL && dim == NULL && distance == NULL)
5082 return true;
5084 if (dim != NULL && coarray == NULL)
5086 gfc_error ("DIM argument without COARRAY argument not allowed for "
5087 "THIS_IMAGE intrinsic at %L", &dim->where);
5088 return false;
5091 if (distance && (coarray || dim))
5093 gfc_error ("The DISTANCE argument may not be specified together with the "
5094 "COARRAY or DIM argument in intrinsic at %L",
5095 &distance->where);
5096 return false;
5099 /* Assume that we have "this_image (distance)". */
5100 if (coarray && !gfc_is_coarray (coarray) && coarray->ts.type == BT_INTEGER)
5102 if (dim)
5104 gfc_error ("Unexpected DIM argument with noncoarray argument at %L",
5105 &coarray->where);
5106 return false;
5108 distance = coarray;
5111 if (distance)
5113 if (!type_check (distance, 2, BT_INTEGER))
5114 return false;
5116 if (!nonnegative_check ("DISTANCE", distance))
5117 return false;
5119 if (!scalar_check (distance, 2))
5120 return false;
5122 if (!gfc_notify_std (GFC_STD_F2008_TS, "DISTANCE= argument to "
5123 "THIS_IMAGE at %L", &distance->where))
5124 return false;
5126 return true;
5129 if (!coarray_check (coarray, 0))
5130 return false;
5132 if (dim != NULL)
5134 if (!dim_check (dim, 1, false))
5135 return false;
5137 if (!dim_corank_check (dim, coarray))
5138 return false;
5141 return true;
5144 /* Calculate the sizes for transfer, used by gfc_check_transfer and also
5145 by gfc_simplify_transfer. Return false if we cannot do so. */
5147 bool
5148 gfc_calculate_transfer_sizes (gfc_expr *source, gfc_expr *mold, gfc_expr *size,
5149 size_t *source_size, size_t *result_size,
5150 size_t *result_length_p)
5152 size_t result_elt_size;
5154 if (source->expr_type == EXPR_FUNCTION)
5155 return false;
5157 if (size && size->expr_type != EXPR_CONSTANT)
5158 return false;
5160 /* Calculate the size of the source. */
5161 *source_size = gfc_target_expr_size (source);
5162 if (*source_size == 0)
5163 return false;
5165 /* Determine the size of the element. */
5166 result_elt_size = gfc_element_size (mold);
5167 if (result_elt_size == 0)
5168 return false;
5170 if (mold->expr_type == EXPR_ARRAY || mold->rank || size)
5172 int result_length;
5174 if (size)
5175 result_length = (size_t)mpz_get_ui (size->value.integer);
5176 else
5178 result_length = *source_size / result_elt_size;
5179 if (result_length * result_elt_size < *source_size)
5180 result_length += 1;
5183 *result_size = result_length * result_elt_size;
5184 if (result_length_p)
5185 *result_length_p = result_length;
5187 else
5188 *result_size = result_elt_size;
5190 return true;
5194 bool
5195 gfc_check_transfer (gfc_expr *source, gfc_expr *mold, gfc_expr *size)
5197 size_t source_size;
5198 size_t result_size;
5200 if (mold->ts.type == BT_HOLLERITH)
5202 gfc_error ("%<MOLD%> argument of %<TRANSFER%> intrinsic at %L must not be"
5203 " %s", &mold->where, gfc_basic_typename (BT_HOLLERITH));
5204 return false;
5207 if (size != NULL)
5209 if (!type_check (size, 2, BT_INTEGER))
5210 return false;
5212 if (!scalar_check (size, 2))
5213 return false;
5215 if (!nonoptional_check (size, 2))
5216 return false;
5219 if (!warn_surprising)
5220 return true;
5222 /* If we can't calculate the sizes, we cannot check any more.
5223 Return true for that case. */
5225 if (!gfc_calculate_transfer_sizes (source, mold, size, &source_size,
5226 &result_size, NULL))
5227 return true;
5229 if (source_size < result_size)
5230 gfc_warning (OPT_Wsurprising,
5231 "Intrinsic TRANSFER at %L has partly undefined result: "
5232 "source size %ld < result size %ld", &source->where,
5233 (long) source_size, (long) result_size);
5235 return true;
5239 bool
5240 gfc_check_transpose (gfc_expr *matrix)
5242 if (!rank_check (matrix, 0, 2))
5243 return false;
5245 return true;
5249 bool
5250 gfc_check_ubound (gfc_expr *array, gfc_expr *dim, gfc_expr *kind)
5252 if (!array_check (array, 0))
5253 return false;
5255 if (!dim_check (dim, 1, false))
5256 return false;
5258 if (!dim_rank_check (dim, array, 0))
5259 return false;
5261 if (!kind_check (kind, 2, BT_INTEGER))
5262 return false;
5263 if (kind && !gfc_notify_std (GFC_STD_F2003, "%qs intrinsic "
5264 "with KIND argument at %L",
5265 gfc_current_intrinsic, &kind->where))
5266 return false;
5268 return true;
5272 bool
5273 gfc_check_ucobound (gfc_expr *coarray, gfc_expr *dim, gfc_expr *kind)
5275 if (flag_coarray == GFC_FCOARRAY_NONE)
5277 gfc_fatal_error ("Coarrays disabled at %C, use %<-fcoarray=%> to enable");
5278 return false;
5281 if (!coarray_check (coarray, 0))
5282 return false;
5284 if (dim != NULL)
5286 if (!dim_check (dim, 1, false))
5287 return false;
5289 if (!dim_corank_check (dim, coarray))
5290 return false;
5293 if (!kind_check (kind, 2, BT_INTEGER))
5294 return false;
5296 return true;
5300 bool
5301 gfc_check_unpack (gfc_expr *vector, gfc_expr *mask, gfc_expr *field)
5303 mpz_t vector_size;
5305 if (!rank_check (vector, 0, 1))
5306 return false;
5308 if (!array_check (mask, 1))
5309 return false;
5311 if (!type_check (mask, 1, BT_LOGICAL))
5312 return false;
5314 if (!same_type_check (vector, 0, field, 2))
5315 return false;
5317 if (mask->expr_type == EXPR_ARRAY
5318 && gfc_array_size (vector, &vector_size))
5320 int mask_true_count = 0;
5321 gfc_constructor *mask_ctor;
5322 mask_ctor = gfc_constructor_first (mask->value.constructor);
5323 while (mask_ctor)
5325 if (mask_ctor->expr->expr_type != EXPR_CONSTANT)
5327 mask_true_count = 0;
5328 break;
5331 if (mask_ctor->expr->value.logical)
5332 mask_true_count++;
5334 mask_ctor = gfc_constructor_next (mask_ctor);
5337 if (mpz_get_si (vector_size) < mask_true_count)
5339 gfc_error ("%qs argument of %qs intrinsic at %L must "
5340 "provide at least as many elements as there "
5341 "are .TRUE. values in %qs (%ld/%d)",
5342 gfc_current_intrinsic_arg[0]->name, gfc_current_intrinsic,
5343 &vector->where, gfc_current_intrinsic_arg[1]->name,
5344 mpz_get_si (vector_size), mask_true_count);
5345 return false;
5348 mpz_clear (vector_size);
5351 if (mask->rank != field->rank && field->rank != 0)
5353 gfc_error ("%qs argument of %qs intrinsic at %L must have "
5354 "the same rank as %qs or be a scalar",
5355 gfc_current_intrinsic_arg[2]->name, gfc_current_intrinsic,
5356 &field->where, gfc_current_intrinsic_arg[1]->name);
5357 return false;
5360 if (mask->rank == field->rank)
5362 int i;
5363 for (i = 0; i < field->rank; i++)
5364 if (! identical_dimen_shape (mask, i, field, i))
5366 gfc_error ("%qs and %qs arguments of %qs intrinsic at %L "
5367 "must have identical shape.",
5368 gfc_current_intrinsic_arg[2]->name,
5369 gfc_current_intrinsic_arg[1]->name, gfc_current_intrinsic,
5370 &field->where);
5374 return true;
5378 bool
5379 gfc_check_verify (gfc_expr *x, gfc_expr *y, gfc_expr *z, gfc_expr *kind)
5381 if (!type_check (x, 0, BT_CHARACTER))
5382 return false;
5384 if (!same_type_check (x, 0, y, 1))
5385 return false;
5387 if (z != NULL && !type_check (z, 2, BT_LOGICAL))
5388 return false;
5390 if (!kind_check (kind, 3, BT_INTEGER))
5391 return false;
5392 if (kind && !gfc_notify_std (GFC_STD_F2003, "%qs intrinsic "
5393 "with KIND argument at %L",
5394 gfc_current_intrinsic, &kind->where))
5395 return false;
5397 return true;
5401 bool
5402 gfc_check_trim (gfc_expr *x)
5404 if (!type_check (x, 0, BT_CHARACTER))
5405 return false;
5407 if (!scalar_check (x, 0))
5408 return false;
5410 return true;
5414 bool
5415 gfc_check_ttynam (gfc_expr *unit)
5417 if (!scalar_check (unit, 0))
5418 return false;
5420 if (!type_check (unit, 0, BT_INTEGER))
5421 return false;
5423 return true;
5427 /* Common check function for the half a dozen intrinsics that have a
5428 single real argument. */
5430 bool
5431 gfc_check_x (gfc_expr *x)
5433 if (!type_check (x, 0, BT_REAL))
5434 return false;
5436 return true;
5440 /************* Check functions for intrinsic subroutines *************/
5442 bool
5443 gfc_check_cpu_time (gfc_expr *time)
5445 if (!scalar_check (time, 0))
5446 return false;
5448 if (!type_check (time, 0, BT_REAL))
5449 return false;
5451 if (!variable_check (time, 0, false))
5452 return false;
5454 return true;
5458 bool
5459 gfc_check_date_and_time (gfc_expr *date, gfc_expr *time,
5460 gfc_expr *zone, gfc_expr *values)
5462 if (date != NULL)
5464 if (!type_check (date, 0, BT_CHARACTER))
5465 return false;
5466 if (!kind_value_check (date, 0, gfc_default_character_kind))
5467 return false;
5468 if (!scalar_check (date, 0))
5469 return false;
5470 if (!variable_check (date, 0, false))
5471 return false;
5474 if (time != NULL)
5476 if (!type_check (time, 1, BT_CHARACTER))
5477 return false;
5478 if (!kind_value_check (time, 1, gfc_default_character_kind))
5479 return false;
5480 if (!scalar_check (time, 1))
5481 return false;
5482 if (!variable_check (time, 1, false))
5483 return false;
5486 if (zone != NULL)
5488 if (!type_check (zone, 2, BT_CHARACTER))
5489 return false;
5490 if (!kind_value_check (zone, 2, gfc_default_character_kind))
5491 return false;
5492 if (!scalar_check (zone, 2))
5493 return false;
5494 if (!variable_check (zone, 2, false))
5495 return false;
5498 if (values != NULL)
5500 if (!type_check (values, 3, BT_INTEGER))
5501 return false;
5502 if (!array_check (values, 3))
5503 return false;
5504 if (!rank_check (values, 3, 1))
5505 return false;
5506 if (!variable_check (values, 3, false))
5507 return false;
5510 return true;
5514 bool
5515 gfc_check_mvbits (gfc_expr *from, gfc_expr *frompos, gfc_expr *len,
5516 gfc_expr *to, gfc_expr *topos)
5518 if (!type_check (from, 0, BT_INTEGER))
5519 return false;
5521 if (!type_check (frompos, 1, BT_INTEGER))
5522 return false;
5524 if (!type_check (len, 2, BT_INTEGER))
5525 return false;
5527 if (!same_type_check (from, 0, to, 3))
5528 return false;
5530 if (!variable_check (to, 3, false))
5531 return false;
5533 if (!type_check (topos, 4, BT_INTEGER))
5534 return false;
5536 if (!nonnegative_check ("frompos", frompos))
5537 return false;
5539 if (!nonnegative_check ("topos", topos))
5540 return false;
5542 if (!nonnegative_check ("len", len))
5543 return false;
5545 if (!less_than_bitsize2 ("from", from, "frompos", frompos, "len", len))
5546 return false;
5548 if (!less_than_bitsize2 ("to", to, "topos", topos, "len", len))
5549 return false;
5551 return true;
5555 bool
5556 gfc_check_random_number (gfc_expr *harvest)
5558 if (!type_check (harvest, 0, BT_REAL))
5559 return false;
5561 if (!variable_check (harvest, 0, false))
5562 return false;
5564 return true;
5568 bool
5569 gfc_check_random_seed (gfc_expr *size, gfc_expr *put, gfc_expr *get)
5571 unsigned int nargs = 0, seed_size;
5572 locus *where = NULL;
5573 mpz_t put_size, get_size;
5575 /* Keep the number of bytes in sync with master_state in
5576 libgfortran/intrinsics/random.c. +1 due to the integer p which is
5577 part of the state too. */
5578 seed_size = 128 / gfc_default_integer_kind + 1;
5580 if (size != NULL)
5582 if (size->expr_type != EXPR_VARIABLE
5583 || !size->symtree->n.sym->attr.optional)
5584 nargs++;
5586 if (!scalar_check (size, 0))
5587 return false;
5589 if (!type_check (size, 0, BT_INTEGER))
5590 return false;
5592 if (!variable_check (size, 0, false))
5593 return false;
5595 if (!kind_value_check (size, 0, gfc_default_integer_kind))
5596 return false;
5599 if (put != NULL)
5601 if (put->expr_type != EXPR_VARIABLE
5602 || !put->symtree->n.sym->attr.optional)
5604 nargs++;
5605 where = &put->where;
5608 if (!array_check (put, 1))
5609 return false;
5611 if (!rank_check (put, 1, 1))
5612 return false;
5614 if (!type_check (put, 1, BT_INTEGER))
5615 return false;
5617 if (!kind_value_check (put, 1, gfc_default_integer_kind))
5618 return false;
5620 if (gfc_array_size (put, &put_size)
5621 && mpz_get_ui (put_size) < seed_size)
5622 gfc_error ("Size of %qs argument of %qs intrinsic at %L "
5623 "too small (%i/%i)",
5624 gfc_current_intrinsic_arg[1]->name, gfc_current_intrinsic,
5625 where, (int) mpz_get_ui (put_size), seed_size);
5628 if (get != NULL)
5630 if (get->expr_type != EXPR_VARIABLE
5631 || !get->symtree->n.sym->attr.optional)
5633 nargs++;
5634 where = &get->where;
5637 if (!array_check (get, 2))
5638 return false;
5640 if (!rank_check (get, 2, 1))
5641 return false;
5643 if (!type_check (get, 2, BT_INTEGER))
5644 return false;
5646 if (!variable_check (get, 2, false))
5647 return false;
5649 if (!kind_value_check (get, 2, gfc_default_integer_kind))
5650 return false;
5652 if (gfc_array_size (get, &get_size)
5653 && mpz_get_ui (get_size) < seed_size)
5654 gfc_error ("Size of %qs argument of %qs intrinsic at %L "
5655 "too small (%i/%i)",
5656 gfc_current_intrinsic_arg[2]->name, gfc_current_intrinsic,
5657 where, (int) mpz_get_ui (get_size), seed_size);
5660 /* RANDOM_SEED may not have more than one non-optional argument. */
5661 if (nargs > 1)
5662 gfc_error ("Too many arguments to %s at %L", gfc_current_intrinsic, where);
5664 return true;
5667 bool
5668 gfc_check_fe_runtime_error (gfc_actual_arglist *a)
5670 gfc_expr *e;
5671 int len, i;
5672 int num_percent, nargs;
5674 e = a->expr;
5675 if (e->expr_type != EXPR_CONSTANT)
5676 return true;
5678 len = e->value.character.length;
5679 if (e->value.character.string[len-1] != '\0')
5680 gfc_internal_error ("fe_runtime_error string must be null terminated");
5682 num_percent = 0;
5683 for (i=0; i<len-1; i++)
5684 if (e->value.character.string[i] == '%')
5685 num_percent ++;
5687 nargs = 0;
5688 for (; a; a = a->next)
5689 nargs ++;
5691 if (nargs -1 != num_percent)
5692 gfc_internal_error ("fe_runtime_error: Wrong number of arguments (%d instead of %d)",
5693 nargs, num_percent++);
5695 return true;
5698 bool
5699 gfc_check_second_sub (gfc_expr *time)
5701 if (!scalar_check (time, 0))
5702 return false;
5704 if (!type_check (time, 0, BT_REAL))
5705 return false;
5707 if (!kind_value_check (time, 0, 4))
5708 return false;
5710 return true;
5714 /* COUNT and COUNT_MAX of SYSTEM_CLOCK are scalar, default-kind integer
5715 variables in Fortran 95. In Fortran 2003 and later, they can be of any
5716 kind, and COUNT_RATE can be of type real. Note, count, count_rate, and
5717 count_max are all optional arguments */
5719 bool
5720 gfc_check_system_clock (gfc_expr *count, gfc_expr *count_rate,
5721 gfc_expr *count_max)
5723 if (count != NULL)
5725 if (!scalar_check (count, 0))
5726 return false;
5728 if (!type_check (count, 0, BT_INTEGER))
5729 return false;
5731 if (count->ts.kind != gfc_default_integer_kind
5732 && !gfc_notify_std (GFC_STD_F2003, "COUNT argument to "
5733 "SYSTEM_CLOCK at %L has non-default kind",
5734 &count->where))
5735 return false;
5737 if (!variable_check (count, 0, false))
5738 return false;
5741 if (count_rate != NULL)
5743 if (!scalar_check (count_rate, 1))
5744 return false;
5746 if (!variable_check (count_rate, 1, false))
5747 return false;
5749 if (count_rate->ts.type == BT_REAL)
5751 if (!gfc_notify_std (GFC_STD_F2003, "Real COUNT_RATE argument to "
5752 "SYSTEM_CLOCK at %L", &count_rate->where))
5753 return false;
5755 else
5757 if (!type_check (count_rate, 1, BT_INTEGER))
5758 return false;
5760 if (count_rate->ts.kind != gfc_default_integer_kind
5761 && !gfc_notify_std (GFC_STD_F2003, "COUNT_RATE argument to "
5762 "SYSTEM_CLOCK at %L has non-default kind",
5763 &count_rate->where))
5764 return false;
5769 if (count_max != NULL)
5771 if (!scalar_check (count_max, 2))
5772 return false;
5774 if (!type_check (count_max, 2, BT_INTEGER))
5775 return false;
5777 if (count_max->ts.kind != gfc_default_integer_kind
5778 && !gfc_notify_std (GFC_STD_F2003, "COUNT_MAX argument to "
5779 "SYSTEM_CLOCK at %L has non-default kind",
5780 &count_max->where))
5781 return false;
5783 if (!variable_check (count_max, 2, false))
5784 return false;
5787 return true;
5791 bool
5792 gfc_check_irand (gfc_expr *x)
5794 if (x == NULL)
5795 return true;
5797 if (!scalar_check (x, 0))
5798 return false;
5800 if (!type_check (x, 0, BT_INTEGER))
5801 return false;
5803 if (!kind_value_check (x, 0, 4))
5804 return false;
5806 return true;
5810 bool
5811 gfc_check_alarm_sub (gfc_expr *seconds, gfc_expr *handler, gfc_expr *status)
5813 if (!scalar_check (seconds, 0))
5814 return false;
5815 if (!type_check (seconds, 0, BT_INTEGER))
5816 return false;
5818 if (!int_or_proc_check (handler, 1))
5819 return false;
5820 if (handler->ts.type == BT_INTEGER && !scalar_check (handler, 1))
5821 return false;
5823 if (status == NULL)
5824 return true;
5826 if (!scalar_check (status, 2))
5827 return false;
5828 if (!type_check (status, 2, BT_INTEGER))
5829 return false;
5830 if (!kind_value_check (status, 2, gfc_default_integer_kind))
5831 return false;
5833 return true;
5837 bool
5838 gfc_check_rand (gfc_expr *x)
5840 if (x == NULL)
5841 return true;
5843 if (!scalar_check (x, 0))
5844 return false;
5846 if (!type_check (x, 0, BT_INTEGER))
5847 return false;
5849 if (!kind_value_check (x, 0, 4))
5850 return false;
5852 return true;
5856 bool
5857 gfc_check_srand (gfc_expr *x)
5859 if (!scalar_check (x, 0))
5860 return false;
5862 if (!type_check (x, 0, BT_INTEGER))
5863 return false;
5865 if (!kind_value_check (x, 0, 4))
5866 return false;
5868 return true;
5872 bool
5873 gfc_check_ctime_sub (gfc_expr *time, gfc_expr *result)
5875 if (!scalar_check (time, 0))
5876 return false;
5877 if (!type_check (time, 0, BT_INTEGER))
5878 return false;
5880 if (!type_check (result, 1, BT_CHARACTER))
5881 return false;
5882 if (!kind_value_check (result, 1, gfc_default_character_kind))
5883 return false;
5885 return true;
5889 bool
5890 gfc_check_dtime_etime (gfc_expr *x)
5892 if (!array_check (x, 0))
5893 return false;
5895 if (!rank_check (x, 0, 1))
5896 return false;
5898 if (!variable_check (x, 0, false))
5899 return false;
5901 if (!type_check (x, 0, BT_REAL))
5902 return false;
5904 if (!kind_value_check (x, 0, 4))
5905 return false;
5907 return true;
5911 bool
5912 gfc_check_dtime_etime_sub (gfc_expr *values, gfc_expr *time)
5914 if (!array_check (values, 0))
5915 return false;
5917 if (!rank_check (values, 0, 1))
5918 return false;
5920 if (!variable_check (values, 0, false))
5921 return false;
5923 if (!type_check (values, 0, BT_REAL))
5924 return false;
5926 if (!kind_value_check (values, 0, 4))
5927 return false;
5929 if (!scalar_check (time, 1))
5930 return false;
5932 if (!type_check (time, 1, BT_REAL))
5933 return false;
5935 if (!kind_value_check (time, 1, 4))
5936 return false;
5938 return true;
5942 bool
5943 gfc_check_fdate_sub (gfc_expr *date)
5945 if (!type_check (date, 0, BT_CHARACTER))
5946 return false;
5947 if (!kind_value_check (date, 0, gfc_default_character_kind))
5948 return false;
5950 return true;
5954 bool
5955 gfc_check_gerror (gfc_expr *msg)
5957 if (!type_check (msg, 0, BT_CHARACTER))
5958 return false;
5959 if (!kind_value_check (msg, 0, gfc_default_character_kind))
5960 return false;
5962 return true;
5966 bool
5967 gfc_check_getcwd_sub (gfc_expr *cwd, gfc_expr *status)
5969 if (!type_check (cwd, 0, BT_CHARACTER))
5970 return false;
5971 if (!kind_value_check (cwd, 0, gfc_default_character_kind))
5972 return false;
5974 if (status == NULL)
5975 return true;
5977 if (!scalar_check (status, 1))
5978 return false;
5980 if (!type_check (status, 1, BT_INTEGER))
5981 return false;
5983 return true;
5987 bool
5988 gfc_check_getarg (gfc_expr *pos, gfc_expr *value)
5990 if (!type_check (pos, 0, BT_INTEGER))
5991 return false;
5993 if (pos->ts.kind > gfc_default_integer_kind)
5995 gfc_error ("%qs argument of %qs intrinsic at %L must be of a kind "
5996 "not wider than the default kind (%d)",
5997 gfc_current_intrinsic_arg[0]->name, gfc_current_intrinsic,
5998 &pos->where, gfc_default_integer_kind);
5999 return false;
6002 if (!type_check (value, 1, BT_CHARACTER))
6003 return false;
6004 if (!kind_value_check (value, 1, gfc_default_character_kind))
6005 return false;
6007 return true;
6011 bool
6012 gfc_check_getlog (gfc_expr *msg)
6014 if (!type_check (msg, 0, BT_CHARACTER))
6015 return false;
6016 if (!kind_value_check (msg, 0, gfc_default_character_kind))
6017 return false;
6019 return true;
6023 bool
6024 gfc_check_exit (gfc_expr *status)
6026 if (status == NULL)
6027 return true;
6029 if (!type_check (status, 0, BT_INTEGER))
6030 return false;
6032 if (!scalar_check (status, 0))
6033 return false;
6035 return true;
6039 bool
6040 gfc_check_flush (gfc_expr *unit)
6042 if (unit == NULL)
6043 return true;
6045 if (!type_check (unit, 0, BT_INTEGER))
6046 return false;
6048 if (!scalar_check (unit, 0))
6049 return false;
6051 return true;
6055 bool
6056 gfc_check_free (gfc_expr *i)
6058 if (!type_check (i, 0, BT_INTEGER))
6059 return false;
6061 if (!scalar_check (i, 0))
6062 return false;
6064 return true;
6068 bool
6069 gfc_check_hostnm (gfc_expr *name)
6071 if (!type_check (name, 0, BT_CHARACTER))
6072 return false;
6073 if (!kind_value_check (name, 0, gfc_default_character_kind))
6074 return false;
6076 return true;
6080 bool
6081 gfc_check_hostnm_sub (gfc_expr *name, gfc_expr *status)
6083 if (!type_check (name, 0, BT_CHARACTER))
6084 return false;
6085 if (!kind_value_check (name, 0, gfc_default_character_kind))
6086 return false;
6088 if (status == NULL)
6089 return true;
6091 if (!scalar_check (status, 1))
6092 return false;
6094 if (!type_check (status, 1, BT_INTEGER))
6095 return false;
6097 return true;
6101 bool
6102 gfc_check_itime_idate (gfc_expr *values)
6104 if (!array_check (values, 0))
6105 return false;
6107 if (!rank_check (values, 0, 1))
6108 return false;
6110 if (!variable_check (values, 0, false))
6111 return false;
6113 if (!type_check (values, 0, BT_INTEGER))
6114 return false;
6116 if (!kind_value_check (values, 0, gfc_default_integer_kind))
6117 return false;
6119 return true;
6123 bool
6124 gfc_check_ltime_gmtime (gfc_expr *time, gfc_expr *values)
6126 if (!type_check (time, 0, BT_INTEGER))
6127 return false;
6129 if (!kind_value_check (time, 0, gfc_default_integer_kind))
6130 return false;
6132 if (!scalar_check (time, 0))
6133 return false;
6135 if (!array_check (values, 1))
6136 return false;
6138 if (!rank_check (values, 1, 1))
6139 return false;
6141 if (!variable_check (values, 1, false))
6142 return false;
6144 if (!type_check (values, 1, BT_INTEGER))
6145 return false;
6147 if (!kind_value_check (values, 1, gfc_default_integer_kind))
6148 return false;
6150 return true;
6154 bool
6155 gfc_check_ttynam_sub (gfc_expr *unit, gfc_expr *name)
6157 if (!scalar_check (unit, 0))
6158 return false;
6160 if (!type_check (unit, 0, BT_INTEGER))
6161 return false;
6163 if (!type_check (name, 1, BT_CHARACTER))
6164 return false;
6165 if (!kind_value_check (name, 1, gfc_default_character_kind))
6166 return false;
6168 return true;
6172 bool
6173 gfc_check_isatty (gfc_expr *unit)
6175 if (unit == NULL)
6176 return false;
6178 if (!type_check (unit, 0, BT_INTEGER))
6179 return false;
6181 if (!scalar_check (unit, 0))
6182 return false;
6184 return true;
6188 bool
6189 gfc_check_isnan (gfc_expr *x)
6191 if (!type_check (x, 0, BT_REAL))
6192 return false;
6194 return true;
6198 bool
6199 gfc_check_perror (gfc_expr *string)
6201 if (!type_check (string, 0, BT_CHARACTER))
6202 return false;
6203 if (!kind_value_check (string, 0, gfc_default_character_kind))
6204 return false;
6206 return true;
6210 bool
6211 gfc_check_umask (gfc_expr *mask)
6213 if (!type_check (mask, 0, BT_INTEGER))
6214 return false;
6216 if (!scalar_check (mask, 0))
6217 return false;
6219 return true;
6223 bool
6224 gfc_check_umask_sub (gfc_expr *mask, gfc_expr *old)
6226 if (!type_check (mask, 0, BT_INTEGER))
6227 return false;
6229 if (!scalar_check (mask, 0))
6230 return false;
6232 if (old == NULL)
6233 return true;
6235 if (!scalar_check (old, 1))
6236 return false;
6238 if (!type_check (old, 1, BT_INTEGER))
6239 return false;
6241 return true;
6245 bool
6246 gfc_check_unlink (gfc_expr *name)
6248 if (!type_check (name, 0, BT_CHARACTER))
6249 return false;
6250 if (!kind_value_check (name, 0, gfc_default_character_kind))
6251 return false;
6253 return true;
6257 bool
6258 gfc_check_unlink_sub (gfc_expr *name, gfc_expr *status)
6260 if (!type_check (name, 0, BT_CHARACTER))
6261 return false;
6262 if (!kind_value_check (name, 0, gfc_default_character_kind))
6263 return false;
6265 if (status == NULL)
6266 return true;
6268 if (!scalar_check (status, 1))
6269 return false;
6271 if (!type_check (status, 1, BT_INTEGER))
6272 return false;
6274 return true;
6278 bool
6279 gfc_check_signal (gfc_expr *number, gfc_expr *handler)
6281 if (!scalar_check (number, 0))
6282 return false;
6283 if (!type_check (number, 0, BT_INTEGER))
6284 return false;
6286 if (!int_or_proc_check (handler, 1))
6287 return false;
6288 if (handler->ts.type == BT_INTEGER && !scalar_check (handler, 1))
6289 return false;
6291 return true;
6295 bool
6296 gfc_check_signal_sub (gfc_expr *number, gfc_expr *handler, gfc_expr *status)
6298 if (!scalar_check (number, 0))
6299 return false;
6300 if (!type_check (number, 0, BT_INTEGER))
6301 return false;
6303 if (!int_or_proc_check (handler, 1))
6304 return false;
6305 if (handler->ts.type == BT_INTEGER && !scalar_check (handler, 1))
6306 return false;
6308 if (status == NULL)
6309 return true;
6311 if (!type_check (status, 2, BT_INTEGER))
6312 return false;
6313 if (!scalar_check (status, 2))
6314 return false;
6316 return true;
6320 bool
6321 gfc_check_system_sub (gfc_expr *cmd, gfc_expr *status)
6323 if (!type_check (cmd, 0, BT_CHARACTER))
6324 return false;
6325 if (!kind_value_check (cmd, 0, gfc_default_character_kind))
6326 return false;
6328 if (!scalar_check (status, 1))
6329 return false;
6331 if (!type_check (status, 1, BT_INTEGER))
6332 return false;
6334 if (!kind_value_check (status, 1, gfc_default_integer_kind))
6335 return false;
6337 return true;
6341 /* This is used for the GNU intrinsics AND, OR and XOR. */
6342 bool
6343 gfc_check_and (gfc_expr *i, gfc_expr *j)
6345 if (i->ts.type != BT_INTEGER && i->ts.type != BT_LOGICAL)
6347 gfc_error ("%qs argument of %qs intrinsic at %L must be INTEGER "
6348 "or LOGICAL", gfc_current_intrinsic_arg[0]->name,
6349 gfc_current_intrinsic, &i->where);
6350 return false;
6353 if (j->ts.type != BT_INTEGER && j->ts.type != BT_LOGICAL)
6355 gfc_error ("%qs argument of %qs intrinsic at %L must be INTEGER "
6356 "or LOGICAL", gfc_current_intrinsic_arg[1]->name,
6357 gfc_current_intrinsic, &j->where);
6358 return false;
6361 if (i->ts.type != j->ts.type)
6363 gfc_error ("%qs and %qs arguments of %qs intrinsic at %L must "
6364 "have the same type", gfc_current_intrinsic_arg[0]->name,
6365 gfc_current_intrinsic_arg[1]->name, gfc_current_intrinsic,
6366 &j->where);
6367 return false;
6370 if (!scalar_check (i, 0))
6371 return false;
6373 if (!scalar_check (j, 1))
6374 return false;
6376 return true;
6380 bool
6381 gfc_check_storage_size (gfc_expr *a, gfc_expr *kind)
6384 if (a->expr_type == EXPR_NULL)
6386 gfc_error ("Intrinsic function NULL at %L cannot be an actual "
6387 "argument to STORAGE_SIZE, because it returns a "
6388 "disassociated pointer", &a->where);
6389 return false;
6392 if (a->ts.type == BT_ASSUMED)
6394 gfc_error ("%qs argument of %qs intrinsic at %L shall not be TYPE(*)",
6395 gfc_current_intrinsic_arg[0]->name, gfc_current_intrinsic,
6396 &a->where);
6397 return false;
6400 if (a->ts.type == BT_PROCEDURE)
6402 gfc_error ("%qs argument of %qs intrinsic at %L shall not be a "
6403 "procedure", gfc_current_intrinsic_arg[0]->name,
6404 gfc_current_intrinsic, &a->where);
6405 return false;
6408 if (kind == NULL)
6409 return true;
6411 if (!type_check (kind, 1, BT_INTEGER))
6412 return false;
6414 if (!scalar_check (kind, 1))
6415 return false;
6417 if (kind->expr_type != EXPR_CONSTANT)
6419 gfc_error ("%qs argument of %qs intrinsic at %L must be a constant",
6420 gfc_current_intrinsic_arg[1]->name, gfc_current_intrinsic,
6421 &kind->where);
6422 return false;
6425 return true;