Fix build on sparc64-linux-gnu.
[official-gcc.git] / gcc / fortran / check.c
blob43b07132e0ed2c132ee6ddc05ec918a5b0f9a5f6
1 /* Check functions
2 Copyright (C) 2002-2018 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;
120 /* Check that an expression is integer or real; allow character for
121 F2003 or later. */
123 static bool
124 int_or_real_or_char_check_f2003 (gfc_expr *e, int n)
126 if (e->ts.type != BT_INTEGER && e->ts.type != BT_REAL)
128 if (e->ts.type == BT_CHARACTER)
129 return gfc_notify_std (GFC_STD_F2003, "Fortran 2003: Character for "
130 "%qs argument of %qs intrinsic at %L",
131 gfc_current_intrinsic_arg[n]->name,
132 gfc_current_intrinsic, &e->where);
133 else
135 if (gfc_option.allow_std & GFC_STD_F2003)
136 gfc_error ("%qs argument of %qs intrinsic at %L must be INTEGER "
137 "or REAL or CHARACTER",
138 gfc_current_intrinsic_arg[n]->name,
139 gfc_current_intrinsic, &e->where);
140 else
141 gfc_error ("%qs argument of %qs intrinsic at %L must be INTEGER "
142 "or REAL", gfc_current_intrinsic_arg[n]->name,
143 gfc_current_intrinsic, &e->where);
145 return false;
148 return true;
151 /* Check that an expression is an intrinsic type. */
152 static bool
153 intrinsic_type_check (gfc_expr *e, int n)
155 if (e->ts.type != BT_INTEGER && e->ts.type != BT_REAL
156 && e->ts.type != BT_COMPLEX && e->ts.type != BT_CHARACTER
157 && e->ts.type != BT_LOGICAL)
159 gfc_error ("%qs argument of %qs intrinsic at %L must be of intrinsic type",
160 gfc_current_intrinsic_arg[n]->name,
161 gfc_current_intrinsic, &e->where);
162 return false;
164 return true;
167 /* Check that an expression is real or complex. */
169 static bool
170 real_or_complex_check (gfc_expr *e, int n)
172 if (e->ts.type != BT_REAL && e->ts.type != BT_COMPLEX)
174 gfc_error ("%qs argument of %qs intrinsic at %L must be REAL "
175 "or COMPLEX", gfc_current_intrinsic_arg[n]->name,
176 gfc_current_intrinsic, &e->where);
177 return false;
180 return true;
184 /* Check that an expression is INTEGER or PROCEDURE. */
186 static bool
187 int_or_proc_check (gfc_expr *e, int n)
189 if (e->ts.type != BT_INTEGER && e->ts.type != BT_PROCEDURE)
191 gfc_error ("%qs argument of %qs intrinsic at %L must be INTEGER "
192 "or PROCEDURE", gfc_current_intrinsic_arg[n]->name,
193 gfc_current_intrinsic, &e->where);
194 return false;
197 return true;
201 /* Check that the expression is an optional constant integer
202 and that it specifies a valid kind for that type. */
204 static bool
205 kind_check (gfc_expr *k, int n, bt type)
207 int kind;
209 if (k == NULL)
210 return true;
212 if (!type_check (k, n, BT_INTEGER))
213 return false;
215 if (!scalar_check (k, n))
216 return false;
218 if (!gfc_check_init_expr (k))
220 gfc_error ("%qs argument of %qs intrinsic at %L must be a constant",
221 gfc_current_intrinsic_arg[n]->name, gfc_current_intrinsic,
222 &k->where);
223 return false;
226 if (gfc_extract_int (k, &kind)
227 || gfc_validate_kind (type, kind, true) < 0)
229 gfc_error ("Invalid kind for %s at %L", gfc_basic_typename (type),
230 &k->where);
231 return false;
234 return true;
238 /* Make sure the expression is a double precision real. */
240 static bool
241 double_check (gfc_expr *d, int n)
243 if (!type_check (d, n, BT_REAL))
244 return false;
246 if (d->ts.kind != gfc_default_double_kind)
248 gfc_error ("%qs argument of %qs intrinsic at %L must be double "
249 "precision", gfc_current_intrinsic_arg[n]->name,
250 gfc_current_intrinsic, &d->where);
251 return false;
254 return true;
258 static bool
259 coarray_check (gfc_expr *e, int n)
261 if (e->ts.type == BT_CLASS && gfc_expr_attr (e).class_ok
262 && CLASS_DATA (e)->attr.codimension
263 && CLASS_DATA (e)->as->corank)
265 gfc_add_class_array_ref (e);
266 return true;
269 if (!gfc_is_coarray (e))
271 gfc_error ("Expected coarray variable as %qs argument to the %s "
272 "intrinsic at %L", gfc_current_intrinsic_arg[n]->name,
273 gfc_current_intrinsic, &e->where);
274 return false;
277 return true;
281 /* Make sure the expression is a logical array. */
283 static bool
284 logical_array_check (gfc_expr *array, int n)
286 if (array->ts.type != BT_LOGICAL || array->rank == 0)
288 gfc_error ("%qs argument of %qs intrinsic at %L must be a logical "
289 "array", gfc_current_intrinsic_arg[n]->name,
290 gfc_current_intrinsic, &array->where);
291 return false;
294 return true;
298 /* Make sure an expression is an array. */
300 static bool
301 array_check (gfc_expr *e, int n)
303 if (e->ts.type == BT_CLASS && gfc_expr_attr (e).class_ok
304 && CLASS_DATA (e)->attr.dimension
305 && CLASS_DATA (e)->as->rank)
307 gfc_add_class_array_ref (e);
308 return true;
311 if (e->rank != 0 && e->ts.type != BT_PROCEDURE)
312 return true;
314 gfc_error ("%qs argument of %qs intrinsic at %L must be an array",
315 gfc_current_intrinsic_arg[n]->name, gfc_current_intrinsic,
316 &e->where);
318 return false;
322 /* If expr is a constant, then check to ensure that it is greater than
323 of equal to zero. */
325 static bool
326 nonnegative_check (const char *arg, gfc_expr *expr)
328 int i;
330 if (expr->expr_type == EXPR_CONSTANT)
332 gfc_extract_int (expr, &i);
333 if (i < 0)
335 gfc_error ("%qs at %L must be nonnegative", arg, &expr->where);
336 return false;
340 return true;
344 /* If expr is a constant, then check to ensure that it is greater than zero. */
346 static bool
347 positive_check (int n, gfc_expr *expr)
349 int i;
351 if (expr->expr_type == EXPR_CONSTANT)
353 gfc_extract_int (expr, &i);
354 if (i <= 0)
356 gfc_error ("%qs argument of %qs intrinsic at %L must be positive",
357 gfc_current_intrinsic_arg[n]->name, gfc_current_intrinsic,
358 &expr->where);
359 return false;
363 return true;
367 /* If expr2 is constant, then check that the value is less than
368 (less than or equal to, if 'or_equal' is true) bit_size(expr1). */
370 static bool
371 less_than_bitsize1 (const char *arg1, gfc_expr *expr1, const char *arg2,
372 gfc_expr *expr2, bool or_equal)
374 int i2, i3;
376 if (expr2->expr_type == EXPR_CONSTANT)
378 gfc_extract_int (expr2, &i2);
379 i3 = gfc_validate_kind (BT_INTEGER, expr1->ts.kind, false);
381 /* For ISHFT[C], check that |shift| <= bit_size(i). */
382 if (arg2 == NULL)
384 if (i2 < 0)
385 i2 = -i2;
387 if (i2 > gfc_integer_kinds[i3].bit_size)
389 gfc_error ("The absolute value of SHIFT at %L must be less "
390 "than or equal to BIT_SIZE(%qs)",
391 &expr2->where, arg1);
392 return false;
396 if (or_equal)
398 if (i2 > gfc_integer_kinds[i3].bit_size)
400 gfc_error ("%qs at %L must be less than "
401 "or equal to BIT_SIZE(%qs)",
402 arg2, &expr2->where, arg1);
403 return false;
406 else
408 if (i2 >= gfc_integer_kinds[i3].bit_size)
410 gfc_error ("%qs at %L must be less than BIT_SIZE(%qs)",
411 arg2, &expr2->where, arg1);
412 return false;
417 return true;
421 /* If expr is constant, then check that the value is less than or equal
422 to the bit_size of the kind k. */
424 static bool
425 less_than_bitsizekind (const char *arg, gfc_expr *expr, int k)
427 int i, val;
429 if (expr->expr_type != EXPR_CONSTANT)
430 return true;
432 i = gfc_validate_kind (BT_INTEGER, k, false);
433 gfc_extract_int (expr, &val);
435 if (val > gfc_integer_kinds[i].bit_size)
437 gfc_error ("%qs at %L must be less than or equal to the BIT_SIZE of "
438 "INTEGER(KIND=%d)", arg, &expr->where, k);
439 return false;
442 return true;
446 /* If expr2 and expr3 are constants, then check that the value is less than
447 or equal to bit_size(expr1). */
449 static bool
450 less_than_bitsize2 (const char *arg1, gfc_expr *expr1, const char *arg2,
451 gfc_expr *expr2, const char *arg3, gfc_expr *expr3)
453 int i2, i3;
455 if (expr2->expr_type == EXPR_CONSTANT && expr3->expr_type == EXPR_CONSTANT)
457 gfc_extract_int (expr2, &i2);
458 gfc_extract_int (expr3, &i3);
459 i2 += i3;
460 i3 = gfc_validate_kind (BT_INTEGER, expr1->ts.kind, false);
461 if (i2 > gfc_integer_kinds[i3].bit_size)
463 gfc_error ("%<%s + %s%> at %L must be less than or equal "
464 "to BIT_SIZE(%qs)",
465 arg2, arg3, &expr2->where, arg1);
466 return false;
470 return true;
473 /* Make sure two expressions have the same type. */
475 static bool
476 same_type_check (gfc_expr *e, int n, gfc_expr *f, int m, bool assoc = false)
478 gfc_typespec *ets = &e->ts;
479 gfc_typespec *fts = &f->ts;
481 if (assoc)
483 /* Procedure pointer component expressions have the type of the interface
484 procedure. If they are being tested for association with a procedure
485 pointer (ie. not a component), the type of the procedure must be
486 determined. */
487 if (e->ts.type == BT_PROCEDURE && e->symtree->n.sym)
488 ets = &e->symtree->n.sym->ts;
489 if (f->ts.type == BT_PROCEDURE && f->symtree->n.sym)
490 fts = &f->symtree->n.sym->ts;
493 if (gfc_compare_types (ets, fts))
494 return true;
496 gfc_error ("%qs argument of %qs intrinsic at %L must be the same type "
497 "and kind as %qs", gfc_current_intrinsic_arg[m]->name,
498 gfc_current_intrinsic, &f->where,
499 gfc_current_intrinsic_arg[n]->name);
501 return false;
505 /* Make sure that an expression has a certain (nonzero) rank. */
507 static bool
508 rank_check (gfc_expr *e, int n, int rank)
510 if (e->rank == rank)
511 return true;
513 gfc_error ("%qs argument of %qs intrinsic at %L must be of rank %d",
514 gfc_current_intrinsic_arg[n]->name, gfc_current_intrinsic,
515 &e->where, rank);
517 return false;
521 /* Make sure a variable expression is not an optional dummy argument. */
523 static bool
524 nonoptional_check (gfc_expr *e, int n)
526 if (e->expr_type == EXPR_VARIABLE && e->symtree->n.sym->attr.optional)
528 gfc_error ("%qs argument of %qs intrinsic at %L must not be OPTIONAL",
529 gfc_current_intrinsic_arg[n]->name, gfc_current_intrinsic,
530 &e->where);
533 /* TODO: Recursive check on nonoptional variables? */
535 return true;
539 /* Check for ALLOCATABLE attribute. */
541 static bool
542 allocatable_check (gfc_expr *e, int n)
544 symbol_attribute attr;
546 attr = gfc_variable_attr (e, NULL);
547 if (!attr.allocatable || attr.associate_var)
549 gfc_error ("%qs argument of %qs intrinsic at %L must be ALLOCATABLE",
550 gfc_current_intrinsic_arg[n]->name, gfc_current_intrinsic,
551 &e->where);
552 return false;
555 return true;
559 /* Check that an expression has a particular kind. */
561 static bool
562 kind_value_check (gfc_expr *e, int n, int k)
564 if (e->ts.kind == k)
565 return true;
567 gfc_error ("%qs argument of %qs intrinsic at %L must be of kind %d",
568 gfc_current_intrinsic_arg[n]->name, gfc_current_intrinsic,
569 &e->where, k);
571 return false;
575 /* Make sure an expression is a variable. */
577 static bool
578 variable_check (gfc_expr *e, int n, bool allow_proc)
580 if (e->expr_type == EXPR_VARIABLE
581 && e->symtree->n.sym->attr.intent == INTENT_IN
582 && (gfc_current_intrinsic_arg[n]->intent == INTENT_OUT
583 || gfc_current_intrinsic_arg[n]->intent == INTENT_INOUT))
585 gfc_ref *ref;
586 bool pointer = e->symtree->n.sym->ts.type == BT_CLASS
587 && CLASS_DATA (e->symtree->n.sym)
588 ? CLASS_DATA (e->symtree->n.sym)->attr.class_pointer
589 : e->symtree->n.sym->attr.pointer;
591 for (ref = e->ref; ref; ref = ref->next)
593 if (pointer && ref->type == REF_COMPONENT)
594 break;
595 if (ref->type == REF_COMPONENT
596 && ((ref->u.c.component->ts.type == BT_CLASS
597 && CLASS_DATA (ref->u.c.component)->attr.class_pointer)
598 || (ref->u.c.component->ts.type != BT_CLASS
599 && ref->u.c.component->attr.pointer)))
600 break;
603 if (!ref)
605 gfc_error ("%qs argument of %qs intrinsic at %L cannot be "
606 "INTENT(IN)", gfc_current_intrinsic_arg[n]->name,
607 gfc_current_intrinsic, &e->where);
608 return false;
612 if (e->expr_type == EXPR_VARIABLE
613 && e->symtree->n.sym->attr.flavor != FL_PARAMETER
614 && (allow_proc || !e->symtree->n.sym->attr.function))
615 return true;
617 if (e->expr_type == EXPR_VARIABLE && e->symtree->n.sym->attr.function
618 && e->symtree->n.sym == e->symtree->n.sym->result)
620 gfc_namespace *ns;
621 for (ns = gfc_current_ns; ns; ns = ns->parent)
622 if (ns->proc_name == e->symtree->n.sym)
623 return true;
626 gfc_error ("%qs argument of %qs intrinsic at %L must be a variable",
627 gfc_current_intrinsic_arg[n]->name, gfc_current_intrinsic, &e->where);
629 return false;
633 /* Check the common DIM parameter for correctness. */
635 static bool
636 dim_check (gfc_expr *dim, int n, bool optional)
638 if (dim == NULL)
639 return true;
641 if (!type_check (dim, n, BT_INTEGER))
642 return false;
644 if (!scalar_check (dim, n))
645 return false;
647 if (!optional && !nonoptional_check (dim, n))
648 return false;
650 return true;
654 /* If a coarray DIM parameter is a constant, make sure that it is greater than
655 zero and less than or equal to the corank of the given array. */
657 static bool
658 dim_corank_check (gfc_expr *dim, gfc_expr *array)
660 int corank;
662 gcc_assert (array->expr_type == EXPR_VARIABLE);
664 if (dim->expr_type != EXPR_CONSTANT)
665 return true;
667 if (array->ts.type == BT_CLASS)
668 return true;
670 corank = gfc_get_corank (array);
672 if (mpz_cmp_ui (dim->value.integer, 1) < 0
673 || mpz_cmp_ui (dim->value.integer, corank) > 0)
675 gfc_error ("%<dim%> argument of %qs intrinsic at %L is not a valid "
676 "codimension index", gfc_current_intrinsic, &dim->where);
678 return false;
681 return true;
685 /* If a DIM parameter is a constant, make sure that it is greater than
686 zero and less than or equal to the rank of the given array. If
687 allow_assumed is zero then dim must be less than the rank of the array
688 for assumed size arrays. */
690 static bool
691 dim_rank_check (gfc_expr *dim, gfc_expr *array, int allow_assumed)
693 gfc_array_ref *ar;
694 int rank;
696 if (dim == NULL)
697 return true;
699 if (dim->expr_type != EXPR_CONSTANT)
700 return true;
702 if (array->expr_type == EXPR_FUNCTION && array->value.function.isym
703 && array->value.function.isym->id == GFC_ISYM_SPREAD)
704 rank = array->rank + 1;
705 else
706 rank = array->rank;
708 /* Assumed-rank array. */
709 if (rank == -1)
710 rank = GFC_MAX_DIMENSIONS;
712 if (array->expr_type == EXPR_VARIABLE)
714 ar = gfc_find_array_ref (array);
715 if (ar->as->type == AS_ASSUMED_SIZE
716 && !allow_assumed
717 && ar->type != AR_ELEMENT
718 && ar->type != AR_SECTION)
719 rank--;
722 if (mpz_cmp_ui (dim->value.integer, 1) < 0
723 || mpz_cmp_ui (dim->value.integer, rank) > 0)
725 gfc_error ("%<dim%> argument of %qs intrinsic at %L is not a valid "
726 "dimension index", gfc_current_intrinsic, &dim->where);
728 return false;
731 return true;
735 /* Compare the size of a along dimension ai with the size of b along
736 dimension bi, returning 0 if they are known not to be identical,
737 and 1 if they are identical, or if this cannot be determined. */
739 static int
740 identical_dimen_shape (gfc_expr *a, int ai, gfc_expr *b, int bi)
742 mpz_t a_size, b_size;
743 int ret;
745 gcc_assert (a->rank > ai);
746 gcc_assert (b->rank > bi);
748 ret = 1;
750 if (gfc_array_dimen_size (a, ai, &a_size))
752 if (gfc_array_dimen_size (b, bi, &b_size))
754 if (mpz_cmp (a_size, b_size) != 0)
755 ret = 0;
757 mpz_clear (b_size);
759 mpz_clear (a_size);
761 return ret;
764 /* Calculate the length of a character variable, including substrings.
765 Strip away parentheses if necessary. Return -1 if no length could
766 be determined. */
768 static long
769 gfc_var_strlen (const gfc_expr *a)
771 gfc_ref *ra;
773 while (a->expr_type == EXPR_OP && a->value.op.op == INTRINSIC_PARENTHESES)
774 a = a->value.op.op1;
776 for (ra = a->ref; ra != NULL && ra->type != REF_SUBSTRING; ra = ra->next)
779 if (ra)
781 long start_a, end_a;
783 if (!ra->u.ss.end)
784 return -1;
786 if ((!ra->u.ss.start || ra->u.ss.start->expr_type == EXPR_CONSTANT)
787 && ra->u.ss.end->expr_type == EXPR_CONSTANT)
789 start_a = ra->u.ss.start ? mpz_get_si (ra->u.ss.start->value.integer)
790 : 1;
791 end_a = mpz_get_si (ra->u.ss.end->value.integer);
792 return (end_a < start_a) ? 0 : end_a - start_a + 1;
794 else if (ra->u.ss.start
795 && gfc_dep_compare_expr (ra->u.ss.start, ra->u.ss.end) == 0)
796 return 1;
797 else
798 return -1;
801 if (a->ts.u.cl && a->ts.u.cl->length
802 && a->ts.u.cl->length->expr_type == EXPR_CONSTANT)
803 return mpz_get_si (a->ts.u.cl->length->value.integer);
804 else if (a->expr_type == EXPR_CONSTANT
805 && (a->ts.u.cl == NULL || a->ts.u.cl->length == NULL))
806 return a->value.character.length;
807 else
808 return -1;
812 /* Check whether two character expressions have the same length;
813 returns true if they have or if the length cannot be determined,
814 otherwise return false and raise a gfc_error. */
816 bool
817 gfc_check_same_strlen (const gfc_expr *a, const gfc_expr *b, const char *name)
819 long len_a, len_b;
821 len_a = gfc_var_strlen(a);
822 len_b = gfc_var_strlen(b);
824 if (len_a == -1 || len_b == -1 || len_a == len_b)
825 return true;
826 else
828 gfc_error ("Unequal character lengths (%ld/%ld) in %s at %L",
829 len_a, len_b, name, &a->where);
830 return false;
835 /***** Check functions *****/
837 /* Check subroutine suitable for intrinsics taking a real argument and
838 a kind argument for the result. */
840 static bool
841 check_a_kind (gfc_expr *a, gfc_expr *kind, bt type)
843 if (!type_check (a, 0, BT_REAL))
844 return false;
845 if (!kind_check (kind, 1, type))
846 return false;
848 return true;
852 /* Check subroutine suitable for ceiling, floor and nint. */
854 bool
855 gfc_check_a_ikind (gfc_expr *a, gfc_expr *kind)
857 return check_a_kind (a, kind, BT_INTEGER);
861 /* Check subroutine suitable for aint, anint. */
863 bool
864 gfc_check_a_xkind (gfc_expr *a, gfc_expr *kind)
866 return check_a_kind (a, kind, BT_REAL);
870 bool
871 gfc_check_abs (gfc_expr *a)
873 if (!numeric_check (a, 0))
874 return false;
876 return true;
880 bool
881 gfc_check_achar (gfc_expr *a, gfc_expr *kind)
883 if (!type_check (a, 0, BT_INTEGER))
884 return false;
885 if (!kind_check (kind, 1, BT_CHARACTER))
886 return false;
888 return true;
892 bool
893 gfc_check_access_func (gfc_expr *name, gfc_expr *mode)
895 if (!type_check (name, 0, BT_CHARACTER)
896 || !scalar_check (name, 0))
897 return false;
898 if (!kind_value_check (name, 0, gfc_default_character_kind))
899 return false;
901 if (!type_check (mode, 1, BT_CHARACTER)
902 || !scalar_check (mode, 1))
903 return false;
904 if (!kind_value_check (mode, 1, gfc_default_character_kind))
905 return false;
907 return true;
911 bool
912 gfc_check_all_any (gfc_expr *mask, gfc_expr *dim)
914 if (!logical_array_check (mask, 0))
915 return false;
917 if (!dim_check (dim, 1, false))
918 return false;
920 if (!dim_rank_check (dim, mask, 0))
921 return false;
923 return true;
927 bool
928 gfc_check_allocated (gfc_expr *array)
930 /* Tests on allocated components of coarrays need to detour the check to
931 argument of the _caf_get. */
932 if (flag_coarray == GFC_FCOARRAY_LIB && array->expr_type == EXPR_FUNCTION
933 && array->value.function.isym
934 && array->value.function.isym->id == GFC_ISYM_CAF_GET)
936 array = array->value.function.actual->expr;
937 if (!array->ref)
938 return false;
941 if (!variable_check (array, 0, false))
942 return false;
943 if (!allocatable_check (array, 0))
944 return false;
946 return true;
950 /* Common check function where the first argument must be real or
951 integer and the second argument must be the same as the first. */
953 bool
954 gfc_check_a_p (gfc_expr *a, gfc_expr *p)
956 if (!int_or_real_check (a, 0))
957 return false;
959 if (a->ts.type != p->ts.type)
961 gfc_error ("%qs and %qs arguments of %qs intrinsic at %L must "
962 "have the same type", gfc_current_intrinsic_arg[0]->name,
963 gfc_current_intrinsic_arg[1]->name, gfc_current_intrinsic,
964 &p->where);
965 return false;
968 if (a->ts.kind != p->ts.kind)
970 if (!gfc_notify_std (GFC_STD_GNU, "Different type kinds at %L",
971 &p->where))
972 return false;
975 return true;
979 bool
980 gfc_check_x_yd (gfc_expr *x, gfc_expr *y)
982 if (!double_check (x, 0) || !double_check (y, 1))
983 return false;
985 return true;
989 bool
990 gfc_check_associated (gfc_expr *pointer, gfc_expr *target)
992 symbol_attribute attr1, attr2;
993 int i;
994 bool t;
995 locus *where;
997 where = &pointer->where;
999 if (pointer->expr_type == EXPR_NULL)
1000 goto null_arg;
1002 attr1 = gfc_expr_attr (pointer);
1004 if (!attr1.pointer && !attr1.proc_pointer)
1006 gfc_error ("%qs argument of %qs intrinsic at %L must be a POINTER",
1007 gfc_current_intrinsic_arg[0]->name, gfc_current_intrinsic,
1008 &pointer->where);
1009 return false;
1012 /* F2008, C1242. */
1013 if (attr1.pointer && gfc_is_coindexed (pointer))
1015 gfc_error ("%qs argument of %qs intrinsic at %L shall not be "
1016 "coindexed", gfc_current_intrinsic_arg[0]->name,
1017 gfc_current_intrinsic, &pointer->where);
1018 return false;
1021 /* Target argument is optional. */
1022 if (target == NULL)
1023 return true;
1025 where = &target->where;
1026 if (target->expr_type == EXPR_NULL)
1027 goto null_arg;
1029 if (target->expr_type == EXPR_VARIABLE || target->expr_type == EXPR_FUNCTION)
1030 attr2 = gfc_expr_attr (target);
1031 else
1033 gfc_error ("%qs argument of %qs intrinsic at %L must be a pointer "
1034 "or target VARIABLE or FUNCTION",
1035 gfc_current_intrinsic_arg[1]->name, gfc_current_intrinsic,
1036 &target->where);
1037 return false;
1040 if (attr1.pointer && !attr2.pointer && !attr2.target)
1042 gfc_error ("%qs argument of %qs intrinsic at %L must be a POINTER "
1043 "or a TARGET", gfc_current_intrinsic_arg[1]->name,
1044 gfc_current_intrinsic, &target->where);
1045 return false;
1048 /* F2008, C1242. */
1049 if (attr1.pointer && gfc_is_coindexed (target))
1051 gfc_error ("%qs argument of %qs intrinsic at %L shall not be "
1052 "coindexed", gfc_current_intrinsic_arg[1]->name,
1053 gfc_current_intrinsic, &target->where);
1054 return false;
1057 t = true;
1058 if (!same_type_check (pointer, 0, target, 1, true))
1059 t = false;
1060 if (!rank_check (target, 0, pointer->rank))
1061 t = false;
1062 if (target->rank > 0)
1064 for (i = 0; i < target->rank; i++)
1065 if (target->ref->u.ar.dimen_type[i] == DIMEN_VECTOR)
1067 gfc_error ("Array section with a vector subscript at %L shall not "
1068 "be the target of a pointer",
1069 &target->where);
1070 t = false;
1071 break;
1074 return t;
1076 null_arg:
1078 gfc_error ("NULL pointer at %L is not permitted as actual argument "
1079 "of %qs intrinsic function", where, gfc_current_intrinsic);
1080 return false;
1085 bool
1086 gfc_check_atan_2 (gfc_expr *y, gfc_expr *x)
1088 /* gfc_notify_std would be a waste of time as the return value
1089 is seemingly used only for the generic resolution. The error
1090 will be: Too many arguments. */
1091 if ((gfc_option.allow_std & GFC_STD_F2008) == 0)
1092 return false;
1094 return gfc_check_atan2 (y, x);
1098 bool
1099 gfc_check_atan2 (gfc_expr *y, gfc_expr *x)
1101 if (!type_check (y, 0, BT_REAL))
1102 return false;
1103 if (!same_type_check (y, 0, x, 1))
1104 return false;
1106 return true;
1110 static bool
1111 gfc_check_atomic (gfc_expr *atom, int atom_no, gfc_expr *value, int val_no,
1112 gfc_expr *stat, int stat_no)
1114 if (!scalar_check (atom, atom_no) || !scalar_check (value, val_no))
1115 return false;
1117 if (!(atom->ts.type == BT_INTEGER && atom->ts.kind == gfc_atomic_int_kind)
1118 && !(atom->ts.type == BT_LOGICAL
1119 && atom->ts.kind == gfc_atomic_logical_kind))
1121 gfc_error ("ATOM argument at %L to intrinsic function %s shall be an "
1122 "integer of ATOMIC_INT_KIND or a logical of "
1123 "ATOMIC_LOGICAL_KIND", &atom->where, gfc_current_intrinsic);
1124 return false;
1127 if (!gfc_is_coarray (atom) && !gfc_is_coindexed (atom))
1129 gfc_error ("ATOM argument at %L of the %s intrinsic function shall be a "
1130 "coarray or coindexed", &atom->where, gfc_current_intrinsic);
1131 return false;
1134 if (atom->ts.type != value->ts.type)
1136 gfc_error ("%qs argument of %qs intrinsic at %L shall have the same "
1137 "type as %qs at %L", gfc_current_intrinsic_arg[val_no]->name,
1138 gfc_current_intrinsic, &value->where,
1139 gfc_current_intrinsic_arg[atom_no]->name, &atom->where);
1140 return false;
1143 if (stat != NULL)
1145 if (!type_check (stat, stat_no, BT_INTEGER))
1146 return false;
1147 if (!scalar_check (stat, stat_no))
1148 return false;
1149 if (!variable_check (stat, stat_no, false))
1150 return false;
1151 if (!kind_value_check (stat, stat_no, gfc_default_integer_kind))
1152 return false;
1154 if (!gfc_notify_std (GFC_STD_F2018, "STAT= argument to %s at %L",
1155 gfc_current_intrinsic, &stat->where))
1156 return false;
1159 return true;
1163 bool
1164 gfc_check_atomic_def (gfc_expr *atom, gfc_expr *value, gfc_expr *stat)
1166 if (atom->expr_type == EXPR_FUNCTION
1167 && atom->value.function.isym
1168 && atom->value.function.isym->id == GFC_ISYM_CAF_GET)
1169 atom = atom->value.function.actual->expr;
1171 if (!gfc_check_vardef_context (atom, false, false, false, NULL))
1173 gfc_error ("ATOM argument of the %s intrinsic function at %L shall be "
1174 "definable", gfc_current_intrinsic, &atom->where);
1175 return false;
1178 return gfc_check_atomic (atom, 0, value, 1, stat, 2);
1182 bool
1183 gfc_check_atomic_op (gfc_expr *atom, gfc_expr *value, gfc_expr *stat)
1185 if (atom->ts.type != BT_INTEGER || atom->ts.kind != gfc_atomic_int_kind)
1187 gfc_error ("ATOM argument at %L to intrinsic function %s shall be an "
1188 "integer of ATOMIC_INT_KIND", &atom->where,
1189 gfc_current_intrinsic);
1190 return false;
1193 return gfc_check_atomic_def (atom, value, stat);
1197 bool
1198 gfc_check_atomic_ref (gfc_expr *value, gfc_expr *atom, gfc_expr *stat)
1200 if (atom->expr_type == EXPR_FUNCTION
1201 && atom->value.function.isym
1202 && atom->value.function.isym->id == GFC_ISYM_CAF_GET)
1203 atom = atom->value.function.actual->expr;
1205 if (!gfc_check_vardef_context (value, false, false, false, NULL))
1207 gfc_error ("VALUE argument of the %s intrinsic function at %L shall be "
1208 "definable", gfc_current_intrinsic, &value->where);
1209 return false;
1212 return gfc_check_atomic (atom, 1, value, 0, stat, 2);
1216 bool
1217 gfc_check_image_status (gfc_expr *image, gfc_expr *team)
1219 /* IMAGE has to be a positive, scalar integer. */
1220 if (!type_check (image, 0, BT_INTEGER) || !scalar_check (image, 0)
1221 || !positive_check (0, image))
1222 return false;
1224 if (team)
1226 gfc_error ("%qs argument of %qs intrinsic at %L not yet supported",
1227 gfc_current_intrinsic_arg[1]->name, gfc_current_intrinsic,
1228 &team->where);
1229 return false;
1231 return true;
1235 bool
1236 gfc_check_failed_or_stopped_images (gfc_expr *team, gfc_expr *kind)
1238 if (team)
1240 gfc_error ("%qs argument of %qs intrinsic at %L not yet supported",
1241 gfc_current_intrinsic_arg[0]->name, gfc_current_intrinsic,
1242 &team->where);
1243 return false;
1246 if (kind)
1248 int k;
1250 if (!type_check (kind, 1, BT_INTEGER) || !scalar_check (kind, 1)
1251 || !positive_check (1, kind))
1252 return false;
1254 /* Get the kind, reporting error on non-constant or overflow. */
1255 gfc_current_locus = kind->where;
1256 if (gfc_extract_int (kind, &k, 1))
1257 return false;
1258 if (gfc_validate_kind (BT_INTEGER, k, true) == -1)
1260 gfc_error ("%qs argument of %qs intrinsic at %L shall specify a "
1261 "valid integer kind", gfc_current_intrinsic_arg[1]->name,
1262 gfc_current_intrinsic, &kind->where);
1263 return false;
1266 return true;
1270 bool
1271 gfc_check_get_team (gfc_expr *level)
1273 if (level)
1275 gfc_error ("%qs argument of %qs intrinsic at %L not yet supported",
1276 gfc_current_intrinsic_arg[0]->name, gfc_current_intrinsic,
1277 &level->where);
1278 return false;
1280 return true;
1284 bool
1285 gfc_check_atomic_cas (gfc_expr *atom, gfc_expr *old, gfc_expr *compare,
1286 gfc_expr *new_val, gfc_expr *stat)
1288 if (atom->expr_type == EXPR_FUNCTION
1289 && atom->value.function.isym
1290 && atom->value.function.isym->id == GFC_ISYM_CAF_GET)
1291 atom = atom->value.function.actual->expr;
1293 if (!gfc_check_atomic (atom, 0, new_val, 3, stat, 4))
1294 return false;
1296 if (!scalar_check (old, 1) || !scalar_check (compare, 2))
1297 return false;
1299 if (!same_type_check (atom, 0, old, 1))
1300 return false;
1302 if (!same_type_check (atom, 0, compare, 2))
1303 return false;
1305 if (!gfc_check_vardef_context (atom, false, false, false, NULL))
1307 gfc_error ("ATOM argument of the %s intrinsic function at %L shall be "
1308 "definable", gfc_current_intrinsic, &atom->where);
1309 return false;
1312 if (!gfc_check_vardef_context (old, false, false, false, NULL))
1314 gfc_error ("OLD argument of the %s intrinsic function at %L shall be "
1315 "definable", gfc_current_intrinsic, &old->where);
1316 return false;
1319 return true;
1322 bool
1323 gfc_check_event_query (gfc_expr *event, gfc_expr *count, gfc_expr *stat)
1325 if (event->ts.type != BT_DERIVED
1326 || event->ts.u.derived->from_intmod != INTMOD_ISO_FORTRAN_ENV
1327 || event->ts.u.derived->intmod_sym_id != ISOFORTRAN_EVENT_TYPE)
1329 gfc_error ("EVENT argument at %L to the intrinsic EVENT_QUERY "
1330 "shall be of type EVENT_TYPE", &event->where);
1331 return false;
1334 if (!scalar_check (event, 0))
1335 return false;
1337 if (!gfc_check_vardef_context (count, false, false, false, NULL))
1339 gfc_error ("COUNT argument of the EVENT_QUERY intrinsic function at %L "
1340 "shall be definable", &count->where);
1341 return false;
1344 if (!type_check (count, 1, BT_INTEGER))
1345 return false;
1347 int i = gfc_validate_kind (BT_INTEGER, count->ts.kind, false);
1348 int j = gfc_validate_kind (BT_INTEGER, gfc_default_integer_kind, false);
1350 if (gfc_integer_kinds[i].range < gfc_integer_kinds[j].range)
1352 gfc_error ("COUNT argument of the EVENT_QUERY intrinsic function at %L "
1353 "shall have at least the range of the default integer",
1354 &count->where);
1355 return false;
1358 if (stat != NULL)
1360 if (!type_check (stat, 2, BT_INTEGER))
1361 return false;
1362 if (!scalar_check (stat, 2))
1363 return false;
1364 if (!variable_check (stat, 2, false))
1365 return false;
1367 if (!gfc_notify_std (GFC_STD_F2018, "STAT= argument to %s at %L",
1368 gfc_current_intrinsic, &stat->where))
1369 return false;
1372 return true;
1376 bool
1377 gfc_check_atomic_fetch_op (gfc_expr *atom, gfc_expr *value, gfc_expr *old,
1378 gfc_expr *stat)
1380 if (atom->expr_type == EXPR_FUNCTION
1381 && atom->value.function.isym
1382 && atom->value.function.isym->id == GFC_ISYM_CAF_GET)
1383 atom = atom->value.function.actual->expr;
1385 if (atom->ts.type != BT_INTEGER || atom->ts.kind != gfc_atomic_int_kind)
1387 gfc_error ("ATOM argument at %L to intrinsic function %s shall be an "
1388 "integer of ATOMIC_INT_KIND", &atom->where,
1389 gfc_current_intrinsic);
1390 return false;
1393 if (!gfc_check_atomic (atom, 0, value, 1, stat, 3))
1394 return false;
1396 if (!scalar_check (old, 2))
1397 return false;
1399 if (!same_type_check (atom, 0, old, 2))
1400 return false;
1402 if (!gfc_check_vardef_context (atom, false, false, false, NULL))
1404 gfc_error ("ATOM argument of the %s intrinsic function at %L shall be "
1405 "definable", gfc_current_intrinsic, &atom->where);
1406 return false;
1409 if (!gfc_check_vardef_context (old, false, false, false, NULL))
1411 gfc_error ("OLD argument of the %s intrinsic function at %L shall be "
1412 "definable", gfc_current_intrinsic, &old->where);
1413 return false;
1416 return true;
1420 /* BESJN and BESYN functions. */
1422 bool
1423 gfc_check_besn (gfc_expr *n, gfc_expr *x)
1425 if (!type_check (n, 0, BT_INTEGER))
1426 return false;
1427 if (n->expr_type == EXPR_CONSTANT)
1429 int i;
1430 gfc_extract_int (n, &i);
1431 if (i < 0 && !gfc_notify_std (GFC_STD_GNU, "Negative argument "
1432 "N at %L", &n->where))
1433 return false;
1436 if (!type_check (x, 1, BT_REAL))
1437 return false;
1439 return true;
1443 /* Transformational version of the Bessel JN and YN functions. */
1445 bool
1446 gfc_check_bessel_n2 (gfc_expr *n1, gfc_expr *n2, gfc_expr *x)
1448 if (!type_check (n1, 0, BT_INTEGER))
1449 return false;
1450 if (!scalar_check (n1, 0))
1451 return false;
1452 if (!nonnegative_check ("N1", n1))
1453 return false;
1455 if (!type_check (n2, 1, BT_INTEGER))
1456 return false;
1457 if (!scalar_check (n2, 1))
1458 return false;
1459 if (!nonnegative_check ("N2", n2))
1460 return false;
1462 if (!type_check (x, 2, BT_REAL))
1463 return false;
1464 if (!scalar_check (x, 2))
1465 return false;
1467 return true;
1471 bool
1472 gfc_check_bge_bgt_ble_blt (gfc_expr *i, gfc_expr *j)
1474 if (!type_check (i, 0, BT_INTEGER))
1475 return false;
1477 if (!type_check (j, 1, BT_INTEGER))
1478 return false;
1480 return true;
1484 bool
1485 gfc_check_bitfcn (gfc_expr *i, gfc_expr *pos)
1487 if (!type_check (i, 0, BT_INTEGER))
1488 return false;
1490 if (!type_check (pos, 1, BT_INTEGER))
1491 return false;
1493 if (!nonnegative_check ("pos", pos))
1494 return false;
1496 if (!less_than_bitsize1 ("i", i, "pos", pos, false))
1497 return false;
1499 return true;
1503 bool
1504 gfc_check_char (gfc_expr *i, gfc_expr *kind)
1506 if (!type_check (i, 0, BT_INTEGER))
1507 return false;
1508 if (!kind_check (kind, 1, BT_CHARACTER))
1509 return false;
1511 return true;
1515 bool
1516 gfc_check_chdir (gfc_expr *dir)
1518 if (!type_check (dir, 0, BT_CHARACTER))
1519 return false;
1520 if (!kind_value_check (dir, 0, gfc_default_character_kind))
1521 return false;
1523 return true;
1527 bool
1528 gfc_check_chdir_sub (gfc_expr *dir, gfc_expr *status)
1530 if (!type_check (dir, 0, BT_CHARACTER))
1531 return false;
1532 if (!kind_value_check (dir, 0, gfc_default_character_kind))
1533 return false;
1535 if (status == NULL)
1536 return true;
1538 if (!type_check (status, 1, BT_INTEGER))
1539 return false;
1540 if (!scalar_check (status, 1))
1541 return false;
1543 return true;
1547 bool
1548 gfc_check_chmod (gfc_expr *name, gfc_expr *mode)
1550 if (!type_check (name, 0, BT_CHARACTER))
1551 return false;
1552 if (!kind_value_check (name, 0, gfc_default_character_kind))
1553 return false;
1555 if (!type_check (mode, 1, BT_CHARACTER))
1556 return false;
1557 if (!kind_value_check (mode, 1, gfc_default_character_kind))
1558 return false;
1560 return true;
1564 bool
1565 gfc_check_chmod_sub (gfc_expr *name, gfc_expr *mode, gfc_expr *status)
1567 if (!type_check (name, 0, BT_CHARACTER))
1568 return false;
1569 if (!kind_value_check (name, 0, gfc_default_character_kind))
1570 return false;
1572 if (!type_check (mode, 1, BT_CHARACTER))
1573 return false;
1574 if (!kind_value_check (mode, 1, gfc_default_character_kind))
1575 return false;
1577 if (status == NULL)
1578 return true;
1580 if (!type_check (status, 2, BT_INTEGER))
1581 return false;
1583 if (!scalar_check (status, 2))
1584 return false;
1586 return true;
1590 bool
1591 gfc_check_cmplx (gfc_expr *x, gfc_expr *y, gfc_expr *kind)
1593 if (!numeric_check (x, 0))
1594 return false;
1596 if (y != NULL)
1598 if (!numeric_check (y, 1))
1599 return false;
1601 if (x->ts.type == BT_COMPLEX)
1603 gfc_error ("%qs argument of %qs intrinsic at %L must not be "
1604 "present if %<x%> is COMPLEX",
1605 gfc_current_intrinsic_arg[1]->name, gfc_current_intrinsic,
1606 &y->where);
1607 return false;
1610 if (y->ts.type == BT_COMPLEX)
1612 gfc_error ("%qs argument of %qs intrinsic at %L must have a type "
1613 "of either REAL or INTEGER",
1614 gfc_current_intrinsic_arg[1]->name, gfc_current_intrinsic,
1615 &y->where);
1616 return false;
1621 if (!kind_check (kind, 2, BT_COMPLEX))
1622 return false;
1624 if (!kind && warn_conversion
1625 && x->ts.type == BT_REAL && x->ts.kind > gfc_default_real_kind)
1626 gfc_warning_now (OPT_Wconversion, "Conversion from %s to default-kind "
1627 "COMPLEX(%d) at %L might lose precision, consider using "
1628 "the KIND argument", gfc_typename (&x->ts),
1629 gfc_default_real_kind, &x->where);
1630 else if (y && !kind && warn_conversion
1631 && y->ts.type == BT_REAL && y->ts.kind > gfc_default_real_kind)
1632 gfc_warning_now (OPT_Wconversion, "Conversion from %s to default-kind "
1633 "COMPLEX(%d) at %L might lose precision, consider using "
1634 "the KIND argument", gfc_typename (&y->ts),
1635 gfc_default_real_kind, &y->where);
1636 return true;
1640 static bool
1641 check_co_collective (gfc_expr *a, gfc_expr *image_idx, gfc_expr *stat,
1642 gfc_expr *errmsg, bool co_reduce)
1644 if (!variable_check (a, 0, false))
1645 return false;
1647 if (!gfc_check_vardef_context (a, false, false, false, "argument 'A' with "
1648 "INTENT(INOUT)"))
1649 return false;
1651 /* Fortran 2008, 12.5.2.4, paragraph 18. */
1652 if (gfc_has_vector_subscript (a))
1654 gfc_error ("Argument %<A%> with INTENT(INOUT) at %L of the intrinsic "
1655 "subroutine %s shall not have a vector subscript",
1656 &a->where, gfc_current_intrinsic);
1657 return false;
1660 if (gfc_is_coindexed (a))
1662 gfc_error ("The A argument at %L to the intrinsic %s shall not be "
1663 "coindexed", &a->where, gfc_current_intrinsic);
1664 return false;
1667 if (image_idx != NULL)
1669 if (!type_check (image_idx, co_reduce ? 2 : 1, BT_INTEGER))
1670 return false;
1671 if (!scalar_check (image_idx, co_reduce ? 2 : 1))
1672 return false;
1675 if (stat != NULL)
1677 if (!type_check (stat, co_reduce ? 3 : 2, BT_INTEGER))
1678 return false;
1679 if (!scalar_check (stat, co_reduce ? 3 : 2))
1680 return false;
1681 if (!variable_check (stat, co_reduce ? 3 : 2, false))
1682 return false;
1683 if (stat->ts.kind != 4)
1685 gfc_error ("The stat= argument at %L must be a kind=4 integer "
1686 "variable", &stat->where);
1687 return false;
1691 if (errmsg != NULL)
1693 if (!type_check (errmsg, co_reduce ? 4 : 3, BT_CHARACTER))
1694 return false;
1695 if (!scalar_check (errmsg, co_reduce ? 4 : 3))
1696 return false;
1697 if (!variable_check (errmsg, co_reduce ? 4 : 3, false))
1698 return false;
1699 if (errmsg->ts.kind != 1)
1701 gfc_error ("The errmsg= argument at %L must be a default-kind "
1702 "character variable", &errmsg->where);
1703 return false;
1707 if (flag_coarray == GFC_FCOARRAY_NONE)
1709 gfc_fatal_error ("Coarrays disabled at %L, use %<-fcoarray=%> to enable",
1710 &a->where);
1711 return false;
1714 return true;
1718 bool
1719 gfc_check_co_broadcast (gfc_expr *a, gfc_expr *source_image, gfc_expr *stat,
1720 gfc_expr *errmsg)
1722 if (a->ts.type == BT_CLASS || gfc_expr_attr (a).alloc_comp)
1724 gfc_error ("Support for the A argument at %L which is polymorphic A "
1725 "argument or has allocatable components is not yet "
1726 "implemented", &a->where);
1727 return false;
1729 return check_co_collective (a, source_image, stat, errmsg, false);
1733 bool
1734 gfc_check_co_reduce (gfc_expr *a, gfc_expr *op, gfc_expr *result_image,
1735 gfc_expr *stat, gfc_expr *errmsg)
1737 symbol_attribute attr;
1738 gfc_formal_arglist *formal;
1739 gfc_symbol *sym;
1741 if (a->ts.type == BT_CLASS)
1743 gfc_error ("The A argument at %L of CO_REDUCE shall not be polymorphic",
1744 &a->where);
1745 return false;
1748 if (gfc_expr_attr (a).alloc_comp)
1750 gfc_error ("Support for the A argument at %L with allocatable components"
1751 " is not yet implemented", &a->where);
1752 return false;
1755 if (!check_co_collective (a, result_image, stat, errmsg, true))
1756 return false;
1758 if (!gfc_resolve_expr (op))
1759 return false;
1761 attr = gfc_expr_attr (op);
1762 if (!attr.pure || !attr.function)
1764 gfc_error ("OPERATOR argument at %L must be a PURE function",
1765 &op->where);
1766 return false;
1769 if (attr.intrinsic)
1771 /* None of the intrinsics fulfills the criteria of taking two arguments,
1772 returning the same type and kind as the arguments and being permitted
1773 as actual argument. */
1774 gfc_error ("Intrinsic function %s at %L is not permitted for CO_REDUCE",
1775 op->symtree->n.sym->name, &op->where);
1776 return false;
1779 if (gfc_is_proc_ptr_comp (op))
1781 gfc_component *comp = gfc_get_proc_ptr_comp (op);
1782 sym = comp->ts.interface;
1784 else
1785 sym = op->symtree->n.sym;
1787 formal = sym->formal;
1789 if (!formal || !formal->next || formal->next->next)
1791 gfc_error ("The function passed as OPERATOR at %L shall have two "
1792 "arguments", &op->where);
1793 return false;
1796 if (sym->result->ts.type == BT_UNKNOWN)
1797 gfc_set_default_type (sym->result, 0, NULL);
1799 if (!gfc_compare_types (&a->ts, &sym->result->ts))
1801 gfc_error ("The A argument at %L has type %s but the function passed as "
1802 "OPERATOR at %L returns %s",
1803 &a->where, gfc_typename (&a->ts), &op->where,
1804 gfc_typename (&sym->result->ts));
1805 return false;
1807 if (!gfc_compare_types (&a->ts, &formal->sym->ts)
1808 || !gfc_compare_types (&a->ts, &formal->next->sym->ts))
1810 gfc_error ("The function passed as OPERATOR at %L has arguments of type "
1811 "%s and %s but shall have type %s", &op->where,
1812 gfc_typename (&formal->sym->ts),
1813 gfc_typename (&formal->next->sym->ts), gfc_typename (&a->ts));
1814 return false;
1816 if (op->rank || attr.allocatable || attr.pointer || formal->sym->as
1817 || formal->next->sym->as || formal->sym->attr.allocatable
1818 || formal->next->sym->attr.allocatable || formal->sym->attr.pointer
1819 || formal->next->sym->attr.pointer)
1821 gfc_error ("The function passed as OPERATOR at %L shall have scalar "
1822 "nonallocatable nonpointer arguments and return a "
1823 "nonallocatable nonpointer scalar", &op->where);
1824 return false;
1827 if (formal->sym->attr.value != formal->next->sym->attr.value)
1829 gfc_error ("The function passed as OPERATOR at %L shall have the VALUE "
1830 "attribute either for none or both arguments", &op->where);
1831 return false;
1834 if (formal->sym->attr.target != formal->next->sym->attr.target)
1836 gfc_error ("The function passed as OPERATOR at %L shall have the TARGET "
1837 "attribute either for none or both arguments", &op->where);
1838 return false;
1841 if (formal->sym->attr.asynchronous != formal->next->sym->attr.asynchronous)
1843 gfc_error ("The function passed as OPERATOR at %L shall have the "
1844 "ASYNCHRONOUS attribute either for none or both arguments",
1845 &op->where);
1846 return false;
1849 if (formal->sym->attr.optional || formal->next->sym->attr.optional)
1851 gfc_error ("The function passed as OPERATOR at %L shall not have the "
1852 "OPTIONAL attribute for either of the arguments", &op->where);
1853 return false;
1856 if (a->ts.type == BT_CHARACTER)
1858 gfc_charlen *cl;
1859 unsigned long actual_size, formal_size1, formal_size2, result_size;
1861 cl = a->ts.u.cl;
1862 actual_size = cl && cl->length && cl->length->expr_type == EXPR_CONSTANT
1863 ? mpz_get_ui (cl->length->value.integer) : 0;
1865 cl = formal->sym->ts.u.cl;
1866 formal_size1 = cl && cl->length && cl->length->expr_type == EXPR_CONSTANT
1867 ? mpz_get_ui (cl->length->value.integer) : 0;
1869 cl = formal->next->sym->ts.u.cl;
1870 formal_size2 = cl && cl->length && cl->length->expr_type == EXPR_CONSTANT
1871 ? mpz_get_ui (cl->length->value.integer) : 0;
1873 cl = sym->ts.u.cl;
1874 result_size = cl && cl->length && cl->length->expr_type == EXPR_CONSTANT
1875 ? mpz_get_ui (cl->length->value.integer) : 0;
1877 if (actual_size
1878 && ((formal_size1 && actual_size != formal_size1)
1879 || (formal_size2 && actual_size != formal_size2)))
1881 gfc_error ("The character length of the A argument at %L and of the "
1882 "arguments of the OPERATOR at %L shall be the same",
1883 &a->where, &op->where);
1884 return false;
1886 if (actual_size && result_size && actual_size != result_size)
1888 gfc_error ("The character length of the A argument at %L and of the "
1889 "function result of the OPERATOR at %L shall be the same",
1890 &a->where, &op->where);
1891 return false;
1895 return true;
1899 bool
1900 gfc_check_co_minmax (gfc_expr *a, gfc_expr *result_image, gfc_expr *stat,
1901 gfc_expr *errmsg)
1903 if (a->ts.type != BT_INTEGER && a->ts.type != BT_REAL
1904 && a->ts.type != BT_CHARACTER)
1906 gfc_error ("%qs argument of %qs intrinsic at %L shall be of type "
1907 "integer, real or character",
1908 gfc_current_intrinsic_arg[0]->name, gfc_current_intrinsic,
1909 &a->where);
1910 return false;
1912 return check_co_collective (a, result_image, stat, errmsg, false);
1916 bool
1917 gfc_check_co_sum (gfc_expr *a, gfc_expr *result_image, gfc_expr *stat,
1918 gfc_expr *errmsg)
1920 if (!numeric_check (a, 0))
1921 return false;
1922 return check_co_collective (a, result_image, stat, errmsg, false);
1926 bool
1927 gfc_check_complex (gfc_expr *x, gfc_expr *y)
1929 if (!int_or_real_check (x, 0))
1930 return false;
1931 if (!scalar_check (x, 0))
1932 return false;
1934 if (!int_or_real_check (y, 1))
1935 return false;
1936 if (!scalar_check (y, 1))
1937 return false;
1939 return true;
1943 bool
1944 gfc_check_count (gfc_expr *mask, gfc_expr *dim, gfc_expr *kind)
1946 if (!logical_array_check (mask, 0))
1947 return false;
1948 if (!dim_check (dim, 1, false))
1949 return false;
1950 if (!dim_rank_check (dim, mask, 0))
1951 return false;
1952 if (!kind_check (kind, 2, BT_INTEGER))
1953 return false;
1954 if (kind && !gfc_notify_std (GFC_STD_F2003, "%qs intrinsic "
1955 "with KIND argument at %L",
1956 gfc_current_intrinsic, &kind->where))
1957 return false;
1959 return true;
1963 bool
1964 gfc_check_cshift (gfc_expr *array, gfc_expr *shift, gfc_expr *dim)
1966 if (!array_check (array, 0))
1967 return false;
1969 if (!type_check (shift, 1, BT_INTEGER))
1970 return false;
1972 if (!dim_check (dim, 2, true))
1973 return false;
1975 if (!dim_rank_check (dim, array, false))
1976 return false;
1978 if (array->rank == 1 || shift->rank == 0)
1980 if (!scalar_check (shift, 1))
1981 return false;
1983 else if (shift->rank == array->rank - 1)
1985 int d;
1986 if (!dim)
1987 d = 1;
1988 else if (dim->expr_type == EXPR_CONSTANT)
1989 gfc_extract_int (dim, &d);
1990 else
1991 d = -1;
1993 if (d > 0)
1995 int i, j;
1996 for (i = 0, j = 0; i < array->rank; i++)
1997 if (i != d - 1)
1999 if (!identical_dimen_shape (array, i, shift, j))
2001 gfc_error ("%qs argument of %qs intrinsic at %L has "
2002 "invalid shape in dimension %d (%ld/%ld)",
2003 gfc_current_intrinsic_arg[1]->name,
2004 gfc_current_intrinsic, &shift->where, i + 1,
2005 mpz_get_si (array->shape[i]),
2006 mpz_get_si (shift->shape[j]));
2007 return false;
2010 j += 1;
2014 else
2016 gfc_error ("%qs argument of intrinsic %qs at %L of must have rank "
2017 "%d or be a scalar", gfc_current_intrinsic_arg[1]->name,
2018 gfc_current_intrinsic, &shift->where, array->rank - 1);
2019 return false;
2022 return true;
2026 bool
2027 gfc_check_ctime (gfc_expr *time)
2029 if (!scalar_check (time, 0))
2030 return false;
2032 if (!type_check (time, 0, BT_INTEGER))
2033 return false;
2035 return true;
2039 bool gfc_check_datan2 (gfc_expr *y, gfc_expr *x)
2041 if (!double_check (y, 0) || !double_check (x, 1))
2042 return false;
2044 return true;
2047 bool
2048 gfc_check_dcmplx (gfc_expr *x, gfc_expr *y)
2050 if (!numeric_check (x, 0))
2051 return false;
2053 if (y != NULL)
2055 if (!numeric_check (y, 1))
2056 return false;
2058 if (x->ts.type == BT_COMPLEX)
2060 gfc_error ("%qs argument of %qs intrinsic at %L must not be "
2061 "present if %<x%> is COMPLEX",
2062 gfc_current_intrinsic_arg[1]->name, gfc_current_intrinsic,
2063 &y->where);
2064 return false;
2067 if (y->ts.type == BT_COMPLEX)
2069 gfc_error ("%qs argument of %qs intrinsic at %L must have a type "
2070 "of either REAL or INTEGER",
2071 gfc_current_intrinsic_arg[1]->name, gfc_current_intrinsic,
2072 &y->where);
2073 return false;
2077 return true;
2081 bool
2082 gfc_check_dble (gfc_expr *x)
2084 if (!numeric_check (x, 0))
2085 return false;
2087 return true;
2091 bool
2092 gfc_check_digits (gfc_expr *x)
2094 if (!int_or_real_check (x, 0))
2095 return false;
2097 return true;
2101 bool
2102 gfc_check_dot_product (gfc_expr *vector_a, gfc_expr *vector_b)
2104 switch (vector_a->ts.type)
2106 case BT_LOGICAL:
2107 if (!type_check (vector_b, 1, BT_LOGICAL))
2108 return false;
2109 break;
2111 case BT_INTEGER:
2112 case BT_REAL:
2113 case BT_COMPLEX:
2114 if (!numeric_check (vector_b, 1))
2115 return false;
2116 break;
2118 default:
2119 gfc_error ("%qs argument of %qs intrinsic at %L must be numeric "
2120 "or LOGICAL", gfc_current_intrinsic_arg[0]->name,
2121 gfc_current_intrinsic, &vector_a->where);
2122 return false;
2125 if (!rank_check (vector_a, 0, 1))
2126 return false;
2128 if (!rank_check (vector_b, 1, 1))
2129 return false;
2131 if (! identical_dimen_shape (vector_a, 0, vector_b, 0))
2133 gfc_error ("Different shape for arguments %qs and %qs at %L for "
2134 "intrinsic %<dot_product%>",
2135 gfc_current_intrinsic_arg[0]->name,
2136 gfc_current_intrinsic_arg[1]->name, &vector_a->where);
2137 return false;
2140 return true;
2144 bool
2145 gfc_check_dprod (gfc_expr *x, gfc_expr *y)
2147 if (!type_check (x, 0, BT_REAL)
2148 || !type_check (y, 1, BT_REAL))
2149 return false;
2151 if (x->ts.kind != gfc_default_real_kind)
2153 gfc_error ("%qs argument of %qs intrinsic at %L must be default "
2154 "real", gfc_current_intrinsic_arg[0]->name,
2155 gfc_current_intrinsic, &x->where);
2156 return false;
2159 if (y->ts.kind != gfc_default_real_kind)
2161 gfc_error ("%qs argument of %qs intrinsic at %L must be default "
2162 "real", gfc_current_intrinsic_arg[1]->name,
2163 gfc_current_intrinsic, &y->where);
2164 return false;
2167 return true;
2171 bool
2172 gfc_check_dshift (gfc_expr *i, gfc_expr *j, gfc_expr *shift)
2174 if (!type_check (i, 0, BT_INTEGER))
2175 return false;
2177 if (!type_check (j, 1, BT_INTEGER))
2178 return false;
2180 if (i->is_boz && j->is_boz)
2182 gfc_error ("%<I%> at %L and %<J%>' at %L cannot both be BOZ literal "
2183 "constants", &i->where, &j->where);
2184 return false;
2187 if (!i->is_boz && !j->is_boz && !same_type_check (i, 0, j, 1))
2188 return false;
2190 if (!type_check (shift, 2, BT_INTEGER))
2191 return false;
2193 if (!nonnegative_check ("SHIFT", shift))
2194 return false;
2196 if (i->is_boz)
2198 if (!less_than_bitsize1 ("J", j, "SHIFT", shift, true))
2199 return false;
2200 i->ts.kind = j->ts.kind;
2202 else
2204 if (!less_than_bitsize1 ("I", i, "SHIFT", shift, true))
2205 return false;
2206 j->ts.kind = i->ts.kind;
2209 return true;
2213 bool
2214 gfc_check_eoshift (gfc_expr *array, gfc_expr *shift, gfc_expr *boundary,
2215 gfc_expr *dim)
2217 int d;
2219 if (!array_check (array, 0))
2220 return false;
2222 if (!type_check (shift, 1, BT_INTEGER))
2223 return false;
2225 if (!dim_check (dim, 3, true))
2226 return false;
2228 if (!dim_rank_check (dim, array, false))
2229 return false;
2231 if (!dim)
2232 d = 1;
2233 else if (dim->expr_type == EXPR_CONSTANT)
2234 gfc_extract_int (dim, &d);
2235 else
2236 d = -1;
2238 if (array->rank == 1 || shift->rank == 0)
2240 if (!scalar_check (shift, 1))
2241 return false;
2243 else if (shift->rank == array->rank - 1)
2245 if (d > 0)
2247 int i, j;
2248 for (i = 0, j = 0; i < array->rank; i++)
2249 if (i != d - 1)
2251 if (!identical_dimen_shape (array, i, shift, j))
2253 gfc_error ("%qs argument of %qs intrinsic at %L has "
2254 "invalid shape in dimension %d (%ld/%ld)",
2255 gfc_current_intrinsic_arg[1]->name,
2256 gfc_current_intrinsic, &shift->where, i + 1,
2257 mpz_get_si (array->shape[i]),
2258 mpz_get_si (shift->shape[j]));
2259 return false;
2262 j += 1;
2266 else
2268 gfc_error ("%qs argument of intrinsic %qs at %L of must have rank "
2269 "%d or be a scalar", gfc_current_intrinsic_arg[1]->name,
2270 gfc_current_intrinsic, &shift->where, array->rank - 1);
2271 return false;
2274 if (boundary != NULL)
2276 if (!same_type_check (array, 0, boundary, 2))
2277 return false;
2279 /* Reject unequal string lengths and emit a better error message than
2280 gfc_check_same_strlen would. */
2281 if (array->ts.type == BT_CHARACTER)
2283 ssize_t len_a, len_b;
2285 len_a = gfc_var_strlen (array);
2286 len_b = gfc_var_strlen (boundary);
2287 if (len_a != -1 && len_b != -1 && len_a != len_b)
2289 gfc_error ("%qs must be of same type and kind as %qs at %L in %qs",
2290 gfc_current_intrinsic_arg[2]->name,
2291 gfc_current_intrinsic_arg[0]->name,
2292 &boundary->where, gfc_current_intrinsic);
2293 return false;
2297 if (array->rank == 1 || boundary->rank == 0)
2299 if (!scalar_check (boundary, 2))
2300 return false;
2302 else if (boundary->rank == array->rank - 1)
2304 if (d > 0)
2306 int i,j;
2307 for (i = 0, j = 0; i < array->rank; i++)
2309 if (i != d - 1)
2311 if (!identical_dimen_shape (array, i, boundary, j))
2313 gfc_error ("%qs argument of %qs intrinsic at %L has "
2314 "invalid shape in dimension %d (%ld/%ld)",
2315 gfc_current_intrinsic_arg[2]->name,
2316 gfc_current_intrinsic, &shift->where, i+1,
2317 mpz_get_si (array->shape[i]),
2318 mpz_get_si (boundary->shape[j]));
2319 return false;
2321 j += 1;
2326 else
2328 gfc_error ("%qs argument of intrinsic %qs at %L of must have "
2329 "rank %d or be a scalar",
2330 gfc_current_intrinsic_arg[1]->name, gfc_current_intrinsic,
2331 &shift->where, array->rank - 1);
2332 return false;
2335 else
2337 switch (array->ts.type)
2339 case BT_INTEGER:
2340 case BT_LOGICAL:
2341 case BT_REAL:
2342 case BT_COMPLEX:
2343 case BT_CHARACTER:
2344 break;
2346 default:
2347 gfc_error ("Missing %qs argument to %qs intrinsic at %L for %qs "
2348 "of type %qs", gfc_current_intrinsic_arg[2]->name,
2349 gfc_current_intrinsic, &array->where,
2350 gfc_current_intrinsic_arg[0]->name,
2351 gfc_typename (&array->ts));
2352 return false;
2356 return true;
2359 bool
2360 gfc_check_float (gfc_expr *a)
2362 if (!type_check (a, 0, BT_INTEGER))
2363 return false;
2365 if ((a->ts.kind != gfc_default_integer_kind)
2366 && !gfc_notify_std (GFC_STD_GNU, "non-default INTEGER "
2367 "kind argument to %s intrinsic at %L",
2368 gfc_current_intrinsic, &a->where))
2369 return false;
2371 return true;
2374 /* A single complex argument. */
2376 bool
2377 gfc_check_fn_c (gfc_expr *a)
2379 if (!type_check (a, 0, BT_COMPLEX))
2380 return false;
2382 return true;
2386 /* A single real argument. */
2388 bool
2389 gfc_check_fn_r (gfc_expr *a)
2391 if (!type_check (a, 0, BT_REAL))
2392 return false;
2394 return true;
2397 /* A single double argument. */
2399 bool
2400 gfc_check_fn_d (gfc_expr *a)
2402 if (!double_check (a, 0))
2403 return false;
2405 return true;
2408 /* A single real or complex argument. */
2410 bool
2411 gfc_check_fn_rc (gfc_expr *a)
2413 if (!real_or_complex_check (a, 0))
2414 return false;
2416 return true;
2420 bool
2421 gfc_check_fn_rc2008 (gfc_expr *a)
2423 if (!real_or_complex_check (a, 0))
2424 return false;
2426 if (a->ts.type == BT_COMPLEX
2427 && !gfc_notify_std (GFC_STD_F2008, "COMPLEX argument %qs "
2428 "of %qs intrinsic at %L",
2429 gfc_current_intrinsic_arg[0]->name,
2430 gfc_current_intrinsic, &a->where))
2431 return false;
2433 return true;
2437 bool
2438 gfc_check_fnum (gfc_expr *unit)
2440 if (!type_check (unit, 0, BT_INTEGER))
2441 return false;
2443 if (!scalar_check (unit, 0))
2444 return false;
2446 return true;
2450 bool
2451 gfc_check_huge (gfc_expr *x)
2453 if (!int_or_real_check (x, 0))
2454 return false;
2456 return true;
2460 bool
2461 gfc_check_hypot (gfc_expr *x, gfc_expr *y)
2463 if (!type_check (x, 0, BT_REAL))
2464 return false;
2465 if (!same_type_check (x, 0, y, 1))
2466 return false;
2468 return true;
2472 /* Check that the single argument is an integer. */
2474 bool
2475 gfc_check_i (gfc_expr *i)
2477 if (!type_check (i, 0, BT_INTEGER))
2478 return false;
2480 return true;
2484 bool
2485 gfc_check_iand (gfc_expr *i, gfc_expr *j)
2487 if (!type_check (i, 0, BT_INTEGER))
2488 return false;
2490 if (!type_check (j, 1, BT_INTEGER))
2491 return false;
2493 if (i->ts.kind != j->ts.kind)
2495 if (!gfc_notify_std (GFC_STD_GNU, "Different type kinds at %L",
2496 &i->where))
2497 return false;
2500 return true;
2504 bool
2505 gfc_check_ibits (gfc_expr *i, gfc_expr *pos, gfc_expr *len)
2507 if (!type_check (i, 0, BT_INTEGER))
2508 return false;
2510 if (!type_check (pos, 1, BT_INTEGER))
2511 return false;
2513 if (!type_check (len, 2, BT_INTEGER))
2514 return false;
2516 if (!nonnegative_check ("pos", pos))
2517 return false;
2519 if (!nonnegative_check ("len", len))
2520 return false;
2522 if (!less_than_bitsize2 ("i", i, "pos", pos, "len", len))
2523 return false;
2525 return true;
2529 bool
2530 gfc_check_ichar_iachar (gfc_expr *c, gfc_expr *kind)
2532 int i;
2534 if (!type_check (c, 0, BT_CHARACTER))
2535 return false;
2537 if (!kind_check (kind, 1, BT_INTEGER))
2538 return false;
2540 if (kind && !gfc_notify_std (GFC_STD_F2003, "%qs intrinsic "
2541 "with KIND argument at %L",
2542 gfc_current_intrinsic, &kind->where))
2543 return false;
2545 if (c->expr_type == EXPR_VARIABLE || c->expr_type == EXPR_SUBSTRING)
2547 gfc_expr *start;
2548 gfc_expr *end;
2549 gfc_ref *ref;
2551 /* Substring references don't have the charlength set. */
2552 ref = c->ref;
2553 while (ref && ref->type != REF_SUBSTRING)
2554 ref = ref->next;
2556 gcc_assert (ref == NULL || ref->type == REF_SUBSTRING);
2558 if (!ref)
2560 /* Check that the argument is length one. Non-constant lengths
2561 can't be checked here, so assume they are ok. */
2562 if (c->ts.u.cl && c->ts.u.cl->length)
2564 /* If we already have a length for this expression then use it. */
2565 if (c->ts.u.cl->length->expr_type != EXPR_CONSTANT)
2566 return true;
2567 i = mpz_get_si (c->ts.u.cl->length->value.integer);
2569 else
2570 return true;
2572 else
2574 start = ref->u.ss.start;
2575 end = ref->u.ss.end;
2577 gcc_assert (start);
2578 if (end == NULL || end->expr_type != EXPR_CONSTANT
2579 || start->expr_type != EXPR_CONSTANT)
2580 return true;
2582 i = mpz_get_si (end->value.integer) + 1
2583 - mpz_get_si (start->value.integer);
2586 else
2587 return true;
2589 if (i != 1)
2591 gfc_error ("Argument of %s at %L must be of length one",
2592 gfc_current_intrinsic, &c->where);
2593 return false;
2596 return true;
2600 bool
2601 gfc_check_idnint (gfc_expr *a)
2603 if (!double_check (a, 0))
2604 return false;
2606 return true;
2610 bool
2611 gfc_check_ieor (gfc_expr *i, gfc_expr *j)
2613 if (!type_check (i, 0, BT_INTEGER))
2614 return false;
2616 if (!type_check (j, 1, BT_INTEGER))
2617 return false;
2619 if (i->ts.kind != j->ts.kind)
2621 if (!gfc_notify_std (GFC_STD_GNU, "Different type kinds at %L",
2622 &i->where))
2623 return false;
2626 return true;
2630 bool
2631 gfc_check_index (gfc_expr *string, gfc_expr *substring, gfc_expr *back,
2632 gfc_expr *kind)
2634 if (!type_check (string, 0, BT_CHARACTER)
2635 || !type_check (substring, 1, BT_CHARACTER))
2636 return false;
2638 if (back != NULL && !type_check (back, 2, BT_LOGICAL))
2639 return false;
2641 if (!kind_check (kind, 3, BT_INTEGER))
2642 return false;
2643 if (kind && !gfc_notify_std (GFC_STD_F2003, "%qs intrinsic "
2644 "with KIND argument at %L",
2645 gfc_current_intrinsic, &kind->where))
2646 return false;
2648 if (string->ts.kind != substring->ts.kind)
2650 gfc_error ("%qs argument of %qs intrinsic at %L must be the same "
2651 "kind as %qs", gfc_current_intrinsic_arg[1]->name,
2652 gfc_current_intrinsic, &substring->where,
2653 gfc_current_intrinsic_arg[0]->name);
2654 return false;
2657 return true;
2661 bool
2662 gfc_check_int (gfc_expr *x, gfc_expr *kind)
2664 if (!numeric_check (x, 0))
2665 return false;
2667 if (!kind_check (kind, 1, BT_INTEGER))
2668 return false;
2670 return true;
2674 bool
2675 gfc_check_intconv (gfc_expr *x)
2677 if (!numeric_check (x, 0))
2678 return false;
2680 return true;
2684 bool
2685 gfc_check_ior (gfc_expr *i, gfc_expr *j)
2687 if (!type_check (i, 0, BT_INTEGER))
2688 return false;
2690 if (!type_check (j, 1, BT_INTEGER))
2691 return false;
2693 if (i->ts.kind != j->ts.kind)
2695 if (!gfc_notify_std (GFC_STD_GNU, "Different type kinds at %L",
2696 &i->where))
2697 return false;
2700 return true;
2704 bool
2705 gfc_check_ishft (gfc_expr *i, gfc_expr *shift)
2707 if (!type_check (i, 0, BT_INTEGER)
2708 || !type_check (shift, 1, BT_INTEGER))
2709 return false;
2711 if (!less_than_bitsize1 ("I", i, NULL, shift, true))
2712 return false;
2714 return true;
2718 bool
2719 gfc_check_ishftc (gfc_expr *i, gfc_expr *shift, gfc_expr *size)
2721 if (!type_check (i, 0, BT_INTEGER)
2722 || !type_check (shift, 1, BT_INTEGER))
2723 return false;
2725 if (size != NULL)
2727 int i2, i3;
2729 if (!type_check (size, 2, BT_INTEGER))
2730 return false;
2732 if (!less_than_bitsize1 ("I", i, "SIZE", size, true))
2733 return false;
2735 if (size->expr_type == EXPR_CONSTANT)
2737 gfc_extract_int (size, &i3);
2738 if (i3 <= 0)
2740 gfc_error ("SIZE at %L must be positive", &size->where);
2741 return false;
2744 if (shift->expr_type == EXPR_CONSTANT)
2746 gfc_extract_int (shift, &i2);
2747 if (i2 < 0)
2748 i2 = -i2;
2750 if (i2 > i3)
2752 gfc_error ("The absolute value of SHIFT at %L must be less "
2753 "than or equal to SIZE at %L", &shift->where,
2754 &size->where);
2755 return false;
2760 else if (!less_than_bitsize1 ("I", i, NULL, shift, true))
2761 return false;
2763 return true;
2767 bool
2768 gfc_check_kill (gfc_expr *pid, gfc_expr *sig)
2770 if (!type_check (pid, 0, BT_INTEGER))
2771 return false;
2773 if (!scalar_check (pid, 0))
2774 return false;
2776 if (!type_check (sig, 1, BT_INTEGER))
2777 return false;
2779 if (!scalar_check (sig, 1))
2780 return false;
2782 return true;
2786 bool
2787 gfc_check_kill_sub (gfc_expr *pid, gfc_expr *sig, gfc_expr *status)
2789 if (!type_check (pid, 0, BT_INTEGER))
2790 return false;
2792 if (!scalar_check (pid, 0))
2793 return false;
2795 if (!type_check (sig, 1, BT_INTEGER))
2796 return false;
2798 if (!scalar_check (sig, 1))
2799 return false;
2801 if (status)
2803 if (!type_check (status, 2, BT_INTEGER))
2804 return false;
2806 if (!scalar_check (status, 2))
2807 return false;
2810 return true;
2814 bool
2815 gfc_check_kind (gfc_expr *x)
2817 if (gfc_bt_struct (x->ts.type) || x->ts.type == BT_CLASS)
2819 gfc_error ("%qs argument of %qs intrinsic at %L must be of "
2820 "intrinsic type", gfc_current_intrinsic_arg[0]->name,
2821 gfc_current_intrinsic, &x->where);
2822 return false;
2824 if (x->ts.type == BT_PROCEDURE)
2826 gfc_error ("%qs argument of %qs intrinsic at %L must be a data entity",
2827 gfc_current_intrinsic_arg[0]->name, gfc_current_intrinsic,
2828 &x->where);
2829 return false;
2832 return true;
2836 bool
2837 gfc_check_lbound (gfc_expr *array, gfc_expr *dim, gfc_expr *kind)
2839 if (!array_check (array, 0))
2840 return false;
2842 if (!dim_check (dim, 1, false))
2843 return false;
2845 if (!dim_rank_check (dim, array, 1))
2846 return false;
2848 if (!kind_check (kind, 2, BT_INTEGER))
2849 return false;
2850 if (kind && !gfc_notify_std (GFC_STD_F2003, "%qs intrinsic "
2851 "with KIND argument at %L",
2852 gfc_current_intrinsic, &kind->where))
2853 return false;
2855 return true;
2859 bool
2860 gfc_check_lcobound (gfc_expr *coarray, gfc_expr *dim, gfc_expr *kind)
2862 if (flag_coarray == GFC_FCOARRAY_NONE)
2864 gfc_fatal_error ("Coarrays disabled at %C, use %<-fcoarray=%> to enable");
2865 return false;
2868 if (!coarray_check (coarray, 0))
2869 return false;
2871 if (dim != NULL)
2873 if (!dim_check (dim, 1, false))
2874 return false;
2876 if (!dim_corank_check (dim, coarray))
2877 return false;
2880 if (!kind_check (kind, 2, BT_INTEGER))
2881 return false;
2883 return true;
2887 bool
2888 gfc_check_len_lentrim (gfc_expr *s, gfc_expr *kind)
2890 if (!type_check (s, 0, BT_CHARACTER))
2891 return false;
2893 if (!kind_check (kind, 1, BT_INTEGER))
2894 return false;
2895 if (kind && !gfc_notify_std (GFC_STD_F2003, "%qs intrinsic "
2896 "with KIND argument at %L",
2897 gfc_current_intrinsic, &kind->where))
2898 return false;
2900 return true;
2904 bool
2905 gfc_check_lge_lgt_lle_llt (gfc_expr *a, gfc_expr *b)
2907 if (!type_check (a, 0, BT_CHARACTER))
2908 return false;
2909 if (!kind_value_check (a, 0, gfc_default_character_kind))
2910 return false;
2912 if (!type_check (b, 1, BT_CHARACTER))
2913 return false;
2914 if (!kind_value_check (b, 1, gfc_default_character_kind))
2915 return false;
2917 return true;
2921 bool
2922 gfc_check_link (gfc_expr *path1, gfc_expr *path2)
2924 if (!type_check (path1, 0, BT_CHARACTER))
2925 return false;
2926 if (!kind_value_check (path1, 0, gfc_default_character_kind))
2927 return false;
2929 if (!type_check (path2, 1, BT_CHARACTER))
2930 return false;
2931 if (!kind_value_check (path2, 1, gfc_default_character_kind))
2932 return false;
2934 return true;
2938 bool
2939 gfc_check_link_sub (gfc_expr *path1, gfc_expr *path2, gfc_expr *status)
2941 if (!type_check (path1, 0, BT_CHARACTER))
2942 return false;
2943 if (!kind_value_check (path1, 0, gfc_default_character_kind))
2944 return false;
2946 if (!type_check (path2, 1, BT_CHARACTER))
2947 return false;
2948 if (!kind_value_check (path2, 0, gfc_default_character_kind))
2949 return false;
2951 if (status == NULL)
2952 return true;
2954 if (!type_check (status, 2, BT_INTEGER))
2955 return false;
2957 if (!scalar_check (status, 2))
2958 return false;
2960 return true;
2964 bool
2965 gfc_check_loc (gfc_expr *expr)
2967 return variable_check (expr, 0, true);
2971 bool
2972 gfc_check_symlnk (gfc_expr *path1, gfc_expr *path2)
2974 if (!type_check (path1, 0, BT_CHARACTER))
2975 return false;
2976 if (!kind_value_check (path1, 0, gfc_default_character_kind))
2977 return false;
2979 if (!type_check (path2, 1, BT_CHARACTER))
2980 return false;
2981 if (!kind_value_check (path2, 1, gfc_default_character_kind))
2982 return false;
2984 return true;
2988 bool
2989 gfc_check_symlnk_sub (gfc_expr *path1, gfc_expr *path2, gfc_expr *status)
2991 if (!type_check (path1, 0, BT_CHARACTER))
2992 return false;
2993 if (!kind_value_check (path1, 0, gfc_default_character_kind))
2994 return false;
2996 if (!type_check (path2, 1, BT_CHARACTER))
2997 return false;
2998 if (!kind_value_check (path2, 1, gfc_default_character_kind))
2999 return false;
3001 if (status == NULL)
3002 return true;
3004 if (!type_check (status, 2, BT_INTEGER))
3005 return false;
3007 if (!scalar_check (status, 2))
3008 return false;
3010 return true;
3014 bool
3015 gfc_check_logical (gfc_expr *a, gfc_expr *kind)
3017 if (!type_check (a, 0, BT_LOGICAL))
3018 return false;
3019 if (!kind_check (kind, 1, BT_LOGICAL))
3020 return false;
3022 return true;
3026 /* Min/max family. */
3028 static bool
3029 min_max_args (gfc_actual_arglist *args)
3031 gfc_actual_arglist *arg;
3032 int i, j, nargs, *nlabels, nlabelless;
3033 bool a1 = false, a2 = false;
3035 if (args == NULL || args->next == NULL)
3037 gfc_error ("Intrinsic %qs at %L must have at least two arguments",
3038 gfc_current_intrinsic, gfc_current_intrinsic_where);
3039 return false;
3042 if (!args->name)
3043 a1 = true;
3045 if (!args->next->name)
3046 a2 = true;
3048 nargs = 0;
3049 for (arg = args; arg; arg = arg->next)
3050 if (arg->name)
3051 nargs++;
3053 if (nargs == 0)
3054 return true;
3056 /* Note: Having a keywordless argument after an "arg=" is checked before. */
3057 nlabelless = 0;
3058 nlabels = XALLOCAVEC (int, nargs);
3059 for (arg = args, i = 0; arg; arg = arg->next, i++)
3060 if (arg->name)
3062 int n;
3063 char *endp;
3065 if (arg->name[0] != 'a' || arg->name[1] < '1' || arg->name[1] > '9')
3066 goto unknown;
3067 n = strtol (&arg->name[1], &endp, 10);
3068 if (endp[0] != '\0')
3069 goto unknown;
3070 if (n <= 0)
3071 goto unknown;
3072 if (n <= nlabelless)
3073 goto duplicate;
3074 nlabels[i] = n;
3075 if (n == 1)
3076 a1 = true;
3077 if (n == 2)
3078 a2 = true;
3080 else
3081 nlabelless++;
3083 if (!a1 || !a2)
3085 gfc_error ("Missing %qs argument to the %s intrinsic at %L",
3086 !a1 ? "a1" : "a2", gfc_current_intrinsic,
3087 gfc_current_intrinsic_where);
3088 return false;
3091 /* Check for duplicates. */
3092 for (i = 0; i < nargs; i++)
3093 for (j = i + 1; j < nargs; j++)
3094 if (nlabels[i] == nlabels[j])
3095 goto duplicate;
3097 return true;
3099 duplicate:
3100 gfc_error ("Duplicate argument %qs at %L to intrinsic %s", arg->name,
3101 &arg->expr->where, gfc_current_intrinsic);
3102 return false;
3104 unknown:
3105 gfc_error ("Unknown argument %qs at %L to intrinsic %s", arg->name,
3106 &arg->expr->where, gfc_current_intrinsic);
3107 return false;
3111 static bool
3112 check_rest (bt type, int kind, gfc_actual_arglist *arglist)
3114 gfc_actual_arglist *arg, *tmp;
3115 gfc_expr *x;
3116 int m, n;
3118 if (!min_max_args (arglist))
3119 return false;
3121 for (arg = arglist, n=1; arg; arg = arg->next, n++)
3123 x = arg->expr;
3124 if (x->ts.type != type || x->ts.kind != kind)
3126 if (x->ts.type == type)
3128 if (!gfc_notify_std (GFC_STD_GNU, "Different type "
3129 "kinds at %L", &x->where))
3130 return false;
3132 else
3134 gfc_error ("%<a%d%> argument of %qs intrinsic at %L must be "
3135 "%s(%d)", n, gfc_current_intrinsic, &x->where,
3136 gfc_basic_typename (type), kind);
3137 return false;
3141 for (tmp = arglist, m=1; tmp != arg; tmp = tmp->next, m++)
3142 if (!gfc_check_conformance (tmp->expr, x,
3143 "arguments 'a%d' and 'a%d' for "
3144 "intrinsic '%s'", m, n,
3145 gfc_current_intrinsic))
3146 return false;
3149 return true;
3153 bool
3154 gfc_check_min_max (gfc_actual_arglist *arg)
3156 gfc_expr *x;
3158 if (!min_max_args (arg))
3159 return false;
3161 x = arg->expr;
3163 if (x->ts.type == BT_CHARACTER)
3165 if (!gfc_notify_std (GFC_STD_F2003, "%qs intrinsic "
3166 "with CHARACTER argument at %L",
3167 gfc_current_intrinsic, &x->where))
3168 return false;
3170 else if (x->ts.type != BT_INTEGER && x->ts.type != BT_REAL)
3172 gfc_error ("%<a1%> argument of %qs intrinsic at %L must be INTEGER, "
3173 "REAL or CHARACTER", gfc_current_intrinsic, &x->where);
3174 return false;
3177 return check_rest (x->ts.type, x->ts.kind, arg);
3181 bool
3182 gfc_check_min_max_integer (gfc_actual_arglist *arg)
3184 return check_rest (BT_INTEGER, gfc_default_integer_kind, arg);
3188 bool
3189 gfc_check_min_max_real (gfc_actual_arglist *arg)
3191 return check_rest (BT_REAL, gfc_default_real_kind, arg);
3195 bool
3196 gfc_check_min_max_double (gfc_actual_arglist *arg)
3198 return check_rest (BT_REAL, gfc_default_double_kind, arg);
3202 /* End of min/max family. */
3204 bool
3205 gfc_check_malloc (gfc_expr *size)
3207 if (!type_check (size, 0, BT_INTEGER))
3208 return false;
3210 if (!scalar_check (size, 0))
3211 return false;
3213 return true;
3217 bool
3218 gfc_check_matmul (gfc_expr *matrix_a, gfc_expr *matrix_b)
3220 if ((matrix_a->ts.type != BT_LOGICAL) && !gfc_numeric_ts (&matrix_a->ts))
3222 gfc_error ("%qs argument of %qs intrinsic at %L must be numeric "
3223 "or LOGICAL", gfc_current_intrinsic_arg[0]->name,
3224 gfc_current_intrinsic, &matrix_a->where);
3225 return false;
3228 if ((matrix_b->ts.type != BT_LOGICAL) && !gfc_numeric_ts (&matrix_b->ts))
3230 gfc_error ("%qs argument of %qs intrinsic at %L must be numeric "
3231 "or LOGICAL", gfc_current_intrinsic_arg[1]->name,
3232 gfc_current_intrinsic, &matrix_b->where);
3233 return false;
3236 if ((matrix_a->ts.type == BT_LOGICAL && gfc_numeric_ts (&matrix_b->ts))
3237 || (gfc_numeric_ts (&matrix_a->ts) && matrix_b->ts.type == BT_LOGICAL))
3239 gfc_error ("Argument types of %qs intrinsic at %L must match (%s/%s)",
3240 gfc_current_intrinsic, &matrix_a->where,
3241 gfc_typename(&matrix_a->ts), gfc_typename(&matrix_b->ts));
3242 return false;
3245 switch (matrix_a->rank)
3247 case 1:
3248 if (!rank_check (matrix_b, 1, 2))
3249 return false;
3250 /* Check for case matrix_a has shape(m), matrix_b has shape (m, k). */
3251 if (!identical_dimen_shape (matrix_a, 0, matrix_b, 0))
3253 gfc_error ("Different shape on dimension 1 for arguments %qs "
3254 "and %qs at %L for intrinsic matmul",
3255 gfc_current_intrinsic_arg[0]->name,
3256 gfc_current_intrinsic_arg[1]->name, &matrix_a->where);
3257 return false;
3259 break;
3261 case 2:
3262 if (matrix_b->rank != 2)
3264 if (!rank_check (matrix_b, 1, 1))
3265 return false;
3267 /* matrix_b has rank 1 or 2 here. Common check for the cases
3268 - matrix_a has shape (n,m) and matrix_b has shape (m, k)
3269 - matrix_a has shape (n,m) and matrix_b has shape (m). */
3270 if (!identical_dimen_shape (matrix_a, 1, matrix_b, 0))
3272 gfc_error ("Different shape on dimension 2 for argument %qs and "
3273 "dimension 1 for argument %qs at %L for intrinsic "
3274 "matmul", gfc_current_intrinsic_arg[0]->name,
3275 gfc_current_intrinsic_arg[1]->name, &matrix_a->where);
3276 return false;
3278 break;
3280 default:
3281 gfc_error ("%qs argument of %qs intrinsic at %L must be of rank "
3282 "1 or 2", gfc_current_intrinsic_arg[0]->name,
3283 gfc_current_intrinsic, &matrix_a->where);
3284 return false;
3287 return true;
3291 /* Whoever came up with this interface was probably on something.
3292 The possibilities for the occupation of the second and third
3293 parameters are:
3295 Arg #2 Arg #3
3296 NULL NULL
3297 DIM NULL
3298 MASK NULL
3299 NULL MASK minloc(array, mask=m)
3300 DIM MASK
3302 I.e. in the case of minloc(array,mask), mask will be in the second
3303 position of the argument list and we'll have to fix that up. Also,
3304 add the BACK argument if that isn't present. */
3306 bool
3307 gfc_check_minloc_maxloc (gfc_actual_arglist *ap)
3309 gfc_expr *a, *m, *d, *k, *b;
3311 a = ap->expr;
3312 if (!int_or_real_or_char_check_f2003 (a, 0) || !array_check (a, 0))
3313 return false;
3315 d = ap->next->expr;
3316 m = ap->next->next->expr;
3317 k = ap->next->next->next->expr;
3318 b = ap->next->next->next->next->expr;
3320 if (b)
3322 if (!type_check (b, 4, BT_LOGICAL) || !scalar_check (b,4))
3323 return false;
3325 else
3327 b = gfc_get_logical_expr (gfc_logical_4_kind, NULL, 0);
3328 ap->next->next->next->next->expr = b;
3331 if (m == NULL && d != NULL && d->ts.type == BT_LOGICAL
3332 && ap->next->name == NULL)
3334 m = d;
3335 d = NULL;
3336 ap->next->expr = NULL;
3337 ap->next->next->expr = m;
3340 if (!dim_check (d, 1, false))
3341 return false;
3343 if (!dim_rank_check (d, a, 0))
3344 return false;
3346 if (m != NULL && !type_check (m, 2, BT_LOGICAL))
3347 return false;
3349 if (m != NULL
3350 && !gfc_check_conformance (a, m,
3351 "arguments '%s' and '%s' for intrinsic %s",
3352 gfc_current_intrinsic_arg[0]->name,
3353 gfc_current_intrinsic_arg[2]->name,
3354 gfc_current_intrinsic))
3355 return false;
3357 if (!kind_check (k, 1, BT_INTEGER))
3358 return false;
3360 return true;
3363 /* Check function for findloc. Mostly like gfc_check_minloc_maxloc
3364 above, with the additional "value" argument. */
3366 bool
3367 gfc_check_findloc (gfc_actual_arglist *ap)
3369 gfc_expr *a, *v, *m, *d, *k, *b;
3371 a = ap->expr;
3372 if (!intrinsic_type_check (a, 0) || !array_check (a, 0))
3373 return false;
3375 v = ap->next->expr;
3376 if (!scalar_check (v,1))
3377 return false;
3379 /* Check if the type is compatible. */
3381 if ((a->ts.type == BT_LOGICAL && v->ts.type != BT_LOGICAL)
3382 || (a->ts.type != BT_LOGICAL && v->ts.type == BT_LOGICAL))
3384 gfc_error ("Argument %qs of %qs intrinsic at %L must be in type "
3385 "conformance to argument %qs at %L",
3386 gfc_current_intrinsic_arg[0]->name,
3387 gfc_current_intrinsic, &a->where,
3388 gfc_current_intrinsic_arg[1]->name, &v->where);
3391 d = ap->next->next->expr;
3392 m = ap->next->next->next->expr;
3393 k = ap->next->next->next->next->expr;
3394 b = ap->next->next->next->next->next->expr;
3396 if (b)
3398 if (!type_check (b, 5, BT_LOGICAL) || !scalar_check (b,4))
3399 return false;
3401 else
3403 b = gfc_get_logical_expr (gfc_logical_4_kind, NULL, 0);
3404 ap->next->next->next->next->next->expr = b;
3407 if (m == NULL && d != NULL && d->ts.type == BT_LOGICAL
3408 && ap->next->name == NULL)
3410 m = d;
3411 d = NULL;
3412 ap->next->next->expr = NULL;
3413 ap->next->next->next->expr = m;
3416 if (!dim_check (d, 2, false))
3417 return false;
3419 if (!dim_rank_check (d, a, 0))
3420 return false;
3422 if (m != NULL && !type_check (m, 3, BT_LOGICAL))
3423 return false;
3425 if (m != NULL
3426 && !gfc_check_conformance (a, m,
3427 "arguments '%s' and '%s' for intrinsic %s",
3428 gfc_current_intrinsic_arg[0]->name,
3429 gfc_current_intrinsic_arg[3]->name,
3430 gfc_current_intrinsic))
3431 return false;
3433 if (!kind_check (k, 1, BT_INTEGER))
3434 return false;
3436 return true;
3440 /* Similar to minloc/maxloc, the argument list might need to be
3441 reordered for the MINVAL, MAXVAL, PRODUCT, and SUM intrinsics. The
3442 difference is that MINLOC/MAXLOC take an additional KIND argument.
3443 The possibilities are:
3445 Arg #2 Arg #3
3446 NULL NULL
3447 DIM NULL
3448 MASK NULL
3449 NULL MASK minval(array, mask=m)
3450 DIM MASK
3452 I.e. in the case of minval(array,mask), mask will be in the second
3453 position of the argument list and we'll have to fix that up. */
3455 static bool
3456 check_reduction (gfc_actual_arglist *ap)
3458 gfc_expr *a, *m, *d;
3460 a = ap->expr;
3461 d = ap->next->expr;
3462 m = ap->next->next->expr;
3464 if (m == NULL && d != NULL && d->ts.type == BT_LOGICAL
3465 && ap->next->name == NULL)
3467 m = d;
3468 d = NULL;
3469 ap->next->expr = NULL;
3470 ap->next->next->expr = m;
3473 if (!dim_check (d, 1, false))
3474 return false;
3476 if (!dim_rank_check (d, a, 0))
3477 return false;
3479 if (m != NULL && !type_check (m, 2, BT_LOGICAL))
3480 return false;
3482 if (m != NULL
3483 && !gfc_check_conformance (a, m,
3484 "arguments '%s' and '%s' for intrinsic %s",
3485 gfc_current_intrinsic_arg[0]->name,
3486 gfc_current_intrinsic_arg[2]->name,
3487 gfc_current_intrinsic))
3488 return false;
3490 return true;
3494 bool
3495 gfc_check_minval_maxval (gfc_actual_arglist *ap)
3497 if (!int_or_real_or_char_check_f2003 (ap->expr, 0)
3498 || !array_check (ap->expr, 0))
3499 return false;
3501 return check_reduction (ap);
3505 bool
3506 gfc_check_product_sum (gfc_actual_arglist *ap)
3508 if (!numeric_check (ap->expr, 0)
3509 || !array_check (ap->expr, 0))
3510 return false;
3512 return check_reduction (ap);
3516 /* For IANY, IALL and IPARITY. */
3518 bool
3519 gfc_check_mask (gfc_expr *i, gfc_expr *kind)
3521 int k;
3523 if (!type_check (i, 0, BT_INTEGER))
3524 return false;
3526 if (!nonnegative_check ("I", i))
3527 return false;
3529 if (!kind_check (kind, 1, BT_INTEGER))
3530 return false;
3532 if (kind)
3533 gfc_extract_int (kind, &k);
3534 else
3535 k = gfc_default_integer_kind;
3537 if (!less_than_bitsizekind ("I", i, k))
3538 return false;
3540 return true;
3544 bool
3545 gfc_check_transf_bit_intrins (gfc_actual_arglist *ap)
3547 if (ap->expr->ts.type != BT_INTEGER)
3549 gfc_error ("%qs argument of %qs intrinsic at %L must be INTEGER",
3550 gfc_current_intrinsic_arg[0]->name,
3551 gfc_current_intrinsic, &ap->expr->where);
3552 return false;
3555 if (!array_check (ap->expr, 0))
3556 return false;
3558 return check_reduction (ap);
3562 bool
3563 gfc_check_merge (gfc_expr *tsource, gfc_expr *fsource, gfc_expr *mask)
3565 if (!same_type_check (tsource, 0, fsource, 1))
3566 return false;
3568 if (!type_check (mask, 2, BT_LOGICAL))
3569 return false;
3571 if (tsource->ts.type == BT_CHARACTER)
3572 return gfc_check_same_strlen (tsource, fsource, "MERGE intrinsic");
3574 return true;
3578 bool
3579 gfc_check_merge_bits (gfc_expr *i, gfc_expr *j, gfc_expr *mask)
3581 if (!type_check (i, 0, BT_INTEGER))
3582 return false;
3584 if (!type_check (j, 1, BT_INTEGER))
3585 return false;
3587 if (!type_check (mask, 2, BT_INTEGER))
3588 return false;
3590 if (!same_type_check (i, 0, j, 1))
3591 return false;
3593 if (!same_type_check (i, 0, mask, 2))
3594 return false;
3596 return true;
3600 bool
3601 gfc_check_move_alloc (gfc_expr *from, gfc_expr *to)
3603 if (!variable_check (from, 0, false))
3604 return false;
3605 if (!allocatable_check (from, 0))
3606 return false;
3607 if (gfc_is_coindexed (from))
3609 gfc_error ("The FROM argument to MOVE_ALLOC at %L shall not be "
3610 "coindexed", &from->where);
3611 return false;
3614 if (!variable_check (to, 1, false))
3615 return false;
3616 if (!allocatable_check (to, 1))
3617 return false;
3618 if (gfc_is_coindexed (to))
3620 gfc_error ("The TO argument to MOVE_ALLOC at %L shall not be "
3621 "coindexed", &to->where);
3622 return false;
3625 if (from->ts.type == BT_CLASS && to->ts.type == BT_DERIVED)
3627 gfc_error ("The TO arguments in MOVE_ALLOC at %L must be "
3628 "polymorphic if FROM is polymorphic",
3629 &to->where);
3630 return false;
3633 if (!same_type_check (to, 1, from, 0))
3634 return false;
3636 if (to->rank != from->rank)
3638 gfc_error ("The FROM and TO arguments of the MOVE_ALLOC intrinsic at %L "
3639 "must have the same rank %d/%d", &to->where, from->rank,
3640 to->rank);
3641 return false;
3644 /* IR F08/0040; cf. 12-006A. */
3645 if (gfc_get_corank (to) != gfc_get_corank (from))
3647 gfc_error ("The FROM and TO arguments of the MOVE_ALLOC intrinsic at %L "
3648 "must have the same corank %d/%d", &to->where,
3649 gfc_get_corank (from), gfc_get_corank (to));
3650 return false;
3653 /* This is based losely on F2003 12.4.1.7. It is intended to prevent
3654 the likes of to = sym->cmp1->cmp2 and from = sym->cmp1, where cmp1
3655 and cmp2 are allocatable. After the allocation is transferred,
3656 the 'to' chain is broken by the nullification of the 'from'. A bit
3657 of reflection reveals that this can only occur for derived types
3658 with recursive allocatable components. */
3659 if (to->expr_type == EXPR_VARIABLE && from->expr_type == EXPR_VARIABLE
3660 && !strcmp (to->symtree->n.sym->name, from->symtree->n.sym->name))
3662 gfc_ref *to_ref, *from_ref;
3663 to_ref = to->ref;
3664 from_ref = from->ref;
3665 bool aliasing = true;
3667 for (; from_ref && to_ref;
3668 from_ref = from_ref->next, to_ref = to_ref->next)
3670 if (to_ref->type != from->ref->type)
3671 aliasing = false;
3672 else if (to_ref->type == REF_ARRAY
3673 && to_ref->u.ar.type != AR_FULL
3674 && from_ref->u.ar.type != AR_FULL)
3675 /* Play safe; assume sections and elements are different. */
3676 aliasing = false;
3677 else if (to_ref->type == REF_COMPONENT
3678 && to_ref->u.c.component != from_ref->u.c.component)
3679 aliasing = false;
3681 if (!aliasing)
3682 break;
3685 if (aliasing)
3687 gfc_error ("The FROM and TO arguments at %L violate aliasing "
3688 "restrictions (F2003 12.4.1.7)", &to->where);
3689 return false;
3693 /* CLASS arguments: Make sure the vtab of from is present. */
3694 if (to->ts.type == BT_CLASS && !UNLIMITED_POLY (from))
3695 gfc_find_vtab (&from->ts);
3697 return true;
3701 bool
3702 gfc_check_nearest (gfc_expr *x, gfc_expr *s)
3704 if (!type_check (x, 0, BT_REAL))
3705 return false;
3707 if (!type_check (s, 1, BT_REAL))
3708 return false;
3710 if (s->expr_type == EXPR_CONSTANT)
3712 if (mpfr_sgn (s->value.real) == 0)
3714 gfc_error ("Argument %<S%> of NEAREST at %L shall not be zero",
3715 &s->where);
3716 return false;
3720 return true;
3724 bool
3725 gfc_check_new_line (gfc_expr *a)
3727 if (!type_check (a, 0, BT_CHARACTER))
3728 return false;
3730 return true;
3734 bool
3735 gfc_check_norm2 (gfc_expr *array, gfc_expr *dim)
3737 if (!type_check (array, 0, BT_REAL))
3738 return false;
3740 if (!array_check (array, 0))
3741 return false;
3743 if (!dim_rank_check (dim, array, false))
3744 return false;
3746 return true;
3749 bool
3750 gfc_check_null (gfc_expr *mold)
3752 symbol_attribute attr;
3754 if (mold == NULL)
3755 return true;
3757 if (!variable_check (mold, 0, true))
3758 return false;
3760 attr = gfc_variable_attr (mold, NULL);
3762 if (!attr.pointer && !attr.proc_pointer && !attr.allocatable)
3764 gfc_error ("%qs argument of %qs intrinsic at %L must be a POINTER, "
3765 "ALLOCATABLE or procedure pointer",
3766 gfc_current_intrinsic_arg[0]->name,
3767 gfc_current_intrinsic, &mold->where);
3768 return false;
3771 if (attr.allocatable
3772 && !gfc_notify_std (GFC_STD_F2003, "NULL intrinsic with "
3773 "allocatable MOLD at %L", &mold->where))
3774 return false;
3776 /* F2008, C1242. */
3777 if (gfc_is_coindexed (mold))
3779 gfc_error ("%qs argument of %qs intrinsic at %L shall not be "
3780 "coindexed", gfc_current_intrinsic_arg[0]->name,
3781 gfc_current_intrinsic, &mold->where);
3782 return false;
3785 return true;
3789 bool
3790 gfc_check_pack (gfc_expr *array, gfc_expr *mask, gfc_expr *vector)
3792 if (!array_check (array, 0))
3793 return false;
3795 if (!type_check (mask, 1, BT_LOGICAL))
3796 return false;
3798 if (!gfc_check_conformance (array, mask,
3799 "arguments '%s' and '%s' for intrinsic '%s'",
3800 gfc_current_intrinsic_arg[0]->name,
3801 gfc_current_intrinsic_arg[1]->name,
3802 gfc_current_intrinsic))
3803 return false;
3805 if (vector != NULL)
3807 mpz_t array_size, vector_size;
3808 bool have_array_size, have_vector_size;
3810 if (!same_type_check (array, 0, vector, 2))
3811 return false;
3813 if (!rank_check (vector, 2, 1))
3814 return false;
3816 /* VECTOR requires at least as many elements as MASK
3817 has .TRUE. values. */
3818 have_array_size = gfc_array_size(array, &array_size);
3819 have_vector_size = gfc_array_size(vector, &vector_size);
3821 if (have_vector_size
3822 && (mask->expr_type == EXPR_ARRAY
3823 || (mask->expr_type == EXPR_CONSTANT
3824 && have_array_size)))
3826 int mask_true_values = 0;
3828 if (mask->expr_type == EXPR_ARRAY)
3830 gfc_constructor *mask_ctor;
3831 mask_ctor = gfc_constructor_first (mask->value.constructor);
3832 while (mask_ctor)
3834 if (mask_ctor->expr->expr_type != EXPR_CONSTANT)
3836 mask_true_values = 0;
3837 break;
3840 if (mask_ctor->expr->value.logical)
3841 mask_true_values++;
3843 mask_ctor = gfc_constructor_next (mask_ctor);
3846 else if (mask->expr_type == EXPR_CONSTANT && mask->value.logical)
3847 mask_true_values = mpz_get_si (array_size);
3849 if (mpz_get_si (vector_size) < mask_true_values)
3851 gfc_error ("%qs argument of %qs intrinsic at %L must "
3852 "provide at least as many elements as there "
3853 "are .TRUE. values in %qs (%ld/%d)",
3854 gfc_current_intrinsic_arg[2]->name,
3855 gfc_current_intrinsic, &vector->where,
3856 gfc_current_intrinsic_arg[1]->name,
3857 mpz_get_si (vector_size), mask_true_values);
3858 return false;
3862 if (have_array_size)
3863 mpz_clear (array_size);
3864 if (have_vector_size)
3865 mpz_clear (vector_size);
3868 return true;
3872 bool
3873 gfc_check_parity (gfc_expr *mask, gfc_expr *dim)
3875 if (!type_check (mask, 0, BT_LOGICAL))
3876 return false;
3878 if (!array_check (mask, 0))
3879 return false;
3881 if (!dim_rank_check (dim, mask, false))
3882 return false;
3884 return true;
3888 bool
3889 gfc_check_precision (gfc_expr *x)
3891 if (!real_or_complex_check (x, 0))
3892 return false;
3894 return true;
3898 bool
3899 gfc_check_present (gfc_expr *a)
3901 gfc_symbol *sym;
3903 if (!variable_check (a, 0, true))
3904 return false;
3906 sym = a->symtree->n.sym;
3907 if (!sym->attr.dummy)
3909 gfc_error ("%qs argument of %qs intrinsic at %L must be of a "
3910 "dummy variable", gfc_current_intrinsic_arg[0]->name,
3911 gfc_current_intrinsic, &a->where);
3912 return false;
3915 if (!sym->attr.optional)
3917 gfc_error ("%qs argument of %qs intrinsic at %L must be of "
3918 "an OPTIONAL dummy variable",
3919 gfc_current_intrinsic_arg[0]->name, gfc_current_intrinsic,
3920 &a->where);
3921 return false;
3924 /* 13.14.82 PRESENT(A)
3925 ......
3926 Argument. A shall be the name of an optional dummy argument that is
3927 accessible in the subprogram in which the PRESENT function reference
3928 appears... */
3930 if (a->ref != NULL
3931 && !(a->ref->next == NULL && a->ref->type == REF_ARRAY
3932 && (a->ref->u.ar.type == AR_FULL
3933 || (a->ref->u.ar.type == AR_ELEMENT
3934 && a->ref->u.ar.as->rank == 0))))
3936 gfc_error ("%qs argument of %qs intrinsic at %L must not be a "
3937 "subobject of %qs", gfc_current_intrinsic_arg[0]->name,
3938 gfc_current_intrinsic, &a->where, sym->name);
3939 return false;
3942 return true;
3946 bool
3947 gfc_check_radix (gfc_expr *x)
3949 if (!int_or_real_check (x, 0))
3950 return false;
3952 return true;
3956 bool
3957 gfc_check_range (gfc_expr *x)
3959 if (!numeric_check (x, 0))
3960 return false;
3962 return true;
3966 bool
3967 gfc_check_rank (gfc_expr *a)
3969 /* Any data object is allowed; a "data object" is a "constant (4.1.3),
3970 variable (6), or subobject of a constant (2.4.3.2.3)" (F2008, 1.3.45). */
3972 bool is_variable = true;
3974 /* Functions returning pointers are regarded as variable, cf. F2008, R602. */
3975 if (a->expr_type == EXPR_FUNCTION)
3976 is_variable = a->value.function.esym
3977 ? a->value.function.esym->result->attr.pointer
3978 : a->symtree->n.sym->result->attr.pointer;
3980 if (a->expr_type == EXPR_OP
3981 || a->expr_type == EXPR_NULL
3982 || a->expr_type == EXPR_COMPCALL
3983 || a->expr_type == EXPR_PPC
3984 || a->ts.type == BT_PROCEDURE
3985 || !is_variable)
3987 gfc_error ("The argument of the RANK intrinsic at %L must be a data "
3988 "object", &a->where);
3989 return false;
3992 return true;
3996 /* real, float, sngl. */
3997 bool
3998 gfc_check_real (gfc_expr *a, gfc_expr *kind)
4000 if (!numeric_check (a, 0))
4001 return false;
4003 if (!kind_check (kind, 1, BT_REAL))
4004 return false;
4006 return true;
4010 bool
4011 gfc_check_rename (gfc_expr *path1, gfc_expr *path2)
4013 if (!type_check (path1, 0, BT_CHARACTER))
4014 return false;
4015 if (!kind_value_check (path1, 0, gfc_default_character_kind))
4016 return false;
4018 if (!type_check (path2, 1, BT_CHARACTER))
4019 return false;
4020 if (!kind_value_check (path2, 1, gfc_default_character_kind))
4021 return false;
4023 return true;
4027 bool
4028 gfc_check_rename_sub (gfc_expr *path1, gfc_expr *path2, gfc_expr *status)
4030 if (!type_check (path1, 0, BT_CHARACTER))
4031 return false;
4032 if (!kind_value_check (path1, 0, gfc_default_character_kind))
4033 return false;
4035 if (!type_check (path2, 1, BT_CHARACTER))
4036 return false;
4037 if (!kind_value_check (path2, 1, gfc_default_character_kind))
4038 return false;
4040 if (status == NULL)
4041 return true;
4043 if (!type_check (status, 2, BT_INTEGER))
4044 return false;
4046 if (!scalar_check (status, 2))
4047 return false;
4049 return true;
4053 bool
4054 gfc_check_repeat (gfc_expr *x, gfc_expr *y)
4056 if (!type_check (x, 0, BT_CHARACTER))
4057 return false;
4059 if (!scalar_check (x, 0))
4060 return false;
4062 if (!type_check (y, 0, BT_INTEGER))
4063 return false;
4065 if (!scalar_check (y, 1))
4066 return false;
4068 return true;
4072 bool
4073 gfc_check_reshape (gfc_expr *source, gfc_expr *shape,
4074 gfc_expr *pad, gfc_expr *order)
4076 mpz_t size;
4077 mpz_t nelems;
4078 int shape_size;
4080 if (!array_check (source, 0))
4081 return false;
4083 if (!rank_check (shape, 1, 1))
4084 return false;
4086 if (!type_check (shape, 1, BT_INTEGER))
4087 return false;
4089 if (!gfc_array_size (shape, &size))
4091 gfc_error ("%<shape%> argument of %<reshape%> intrinsic at %L must be an "
4092 "array of constant size", &shape->where);
4093 return false;
4096 shape_size = mpz_get_ui (size);
4097 mpz_clear (size);
4099 if (shape_size <= 0)
4101 gfc_error ("%qs argument of %qs intrinsic at %L is empty",
4102 gfc_current_intrinsic_arg[1]->name, gfc_current_intrinsic,
4103 &shape->where);
4104 return false;
4106 else if (shape_size > GFC_MAX_DIMENSIONS)
4108 gfc_error ("%<shape%> argument of %<reshape%> intrinsic at %L has more "
4109 "than %d elements", &shape->where, GFC_MAX_DIMENSIONS);
4110 return false;
4112 else if (shape->expr_type == EXPR_ARRAY && gfc_is_constant_expr (shape))
4114 gfc_expr *e;
4115 int i, extent;
4116 for (i = 0; i < shape_size; ++i)
4118 e = gfc_constructor_lookup_expr (shape->value.constructor, i);
4119 if (e->expr_type != EXPR_CONSTANT)
4120 continue;
4122 gfc_extract_int (e, &extent);
4123 if (extent < 0)
4125 gfc_error ("%qs argument of %qs intrinsic at %L has "
4126 "negative element (%d)",
4127 gfc_current_intrinsic_arg[1]->name,
4128 gfc_current_intrinsic, &e->where, extent);
4129 return false;
4133 else if (shape->expr_type == EXPR_VARIABLE && shape->ref
4134 && shape->ref->u.ar.type == AR_FULL && shape->ref->u.ar.dimen == 1
4135 && shape->ref->u.ar.as
4136 && shape->ref->u.ar.as->lower[0]->expr_type == EXPR_CONSTANT
4137 && shape->ref->u.ar.as->lower[0]->ts.type == BT_INTEGER
4138 && shape->ref->u.ar.as->upper[0]->expr_type == EXPR_CONSTANT
4139 && shape->ref->u.ar.as->upper[0]->ts.type == BT_INTEGER
4140 && shape->symtree->n.sym->attr.flavor == FL_PARAMETER)
4142 int i, extent;
4143 gfc_expr *e, *v;
4145 v = shape->symtree->n.sym->value;
4147 for (i = 0; i < shape_size; i++)
4149 e = gfc_constructor_lookup_expr (v->value.constructor, i);
4150 if (e == NULL)
4151 break;
4153 gfc_extract_int (e, &extent);
4155 if (extent < 0)
4157 gfc_error ("Element %d of actual argument of RESHAPE at %L "
4158 "cannot be negative", i + 1, &shape->where);
4159 return false;
4164 if (pad != NULL)
4166 if (!same_type_check (source, 0, pad, 2))
4167 return false;
4169 if (!array_check (pad, 2))
4170 return false;
4173 if (order != NULL)
4175 if (!array_check (order, 3))
4176 return false;
4178 if (!type_check (order, 3, BT_INTEGER))
4179 return false;
4181 if (order->expr_type == EXPR_ARRAY && gfc_is_constant_expr (order))
4183 int i, order_size, dim, perm[GFC_MAX_DIMENSIONS];
4184 gfc_expr *e;
4186 for (i = 0; i < GFC_MAX_DIMENSIONS; ++i)
4187 perm[i] = 0;
4189 gfc_array_size (order, &size);
4190 order_size = mpz_get_ui (size);
4191 mpz_clear (size);
4193 if (order_size != shape_size)
4195 gfc_error ("%qs argument of %qs intrinsic at %L "
4196 "has wrong number of elements (%d/%d)",
4197 gfc_current_intrinsic_arg[3]->name,
4198 gfc_current_intrinsic, &order->where,
4199 order_size, shape_size);
4200 return false;
4203 for (i = 1; i <= order_size; ++i)
4205 e = gfc_constructor_lookup_expr (order->value.constructor, i-1);
4206 if (e->expr_type != EXPR_CONSTANT)
4207 continue;
4209 gfc_extract_int (e, &dim);
4211 if (dim < 1 || dim > order_size)
4213 gfc_error ("%qs argument of %qs intrinsic at %L "
4214 "has out-of-range dimension (%d)",
4215 gfc_current_intrinsic_arg[3]->name,
4216 gfc_current_intrinsic, &e->where, dim);
4217 return false;
4220 if (perm[dim-1] != 0)
4222 gfc_error ("%qs argument of %qs intrinsic at %L has "
4223 "invalid permutation of dimensions (dimension "
4224 "%qd duplicated)",
4225 gfc_current_intrinsic_arg[3]->name,
4226 gfc_current_intrinsic, &e->where, dim);
4227 return false;
4230 perm[dim-1] = 1;
4235 if (pad == NULL && shape->expr_type == EXPR_ARRAY
4236 && gfc_is_constant_expr (shape)
4237 && !(source->expr_type == EXPR_VARIABLE && source->symtree->n.sym->as
4238 && source->symtree->n.sym->as->type == AS_ASSUMED_SIZE))
4240 /* Check the match in size between source and destination. */
4241 if (gfc_array_size (source, &nelems))
4243 gfc_constructor *c;
4244 bool test;
4247 mpz_init_set_ui (size, 1);
4248 for (c = gfc_constructor_first (shape->value.constructor);
4249 c; c = gfc_constructor_next (c))
4250 mpz_mul (size, size, c->expr->value.integer);
4252 test = mpz_cmp (nelems, size) < 0 && mpz_cmp_ui (size, 0) > 0;
4253 mpz_clear (nelems);
4254 mpz_clear (size);
4256 if (test)
4258 gfc_error ("Without padding, there are not enough elements "
4259 "in the intrinsic RESHAPE source at %L to match "
4260 "the shape", &source->where);
4261 return false;
4266 return true;
4270 bool
4271 gfc_check_same_type_as (gfc_expr *a, gfc_expr *b)
4273 if (a->ts.type != BT_DERIVED && a->ts.type != BT_CLASS)
4275 gfc_error ("%qs argument of %qs intrinsic at %L "
4276 "cannot be of type %s",
4277 gfc_current_intrinsic_arg[0]->name,
4278 gfc_current_intrinsic,
4279 &a->where, gfc_typename (&a->ts));
4280 return false;
4283 if (!(gfc_type_is_extensible (a->ts.u.derived) || UNLIMITED_POLY (a)))
4285 gfc_error ("%qs argument of %qs intrinsic at %L "
4286 "must be of an extensible type",
4287 gfc_current_intrinsic_arg[0]->name, gfc_current_intrinsic,
4288 &a->where);
4289 return false;
4292 if (b->ts.type != BT_DERIVED && b->ts.type != BT_CLASS)
4294 gfc_error ("%qs argument of %qs intrinsic at %L "
4295 "cannot be of type %s",
4296 gfc_current_intrinsic_arg[0]->name,
4297 gfc_current_intrinsic,
4298 &b->where, gfc_typename (&b->ts));
4299 return false;
4302 if (!(gfc_type_is_extensible (b->ts.u.derived) || UNLIMITED_POLY (b)))
4304 gfc_error ("%qs argument of %qs intrinsic at %L "
4305 "must be of an extensible type",
4306 gfc_current_intrinsic_arg[1]->name, gfc_current_intrinsic,
4307 &b->where);
4308 return false;
4311 return true;
4315 bool
4316 gfc_check_scale (gfc_expr *x, gfc_expr *i)
4318 if (!type_check (x, 0, BT_REAL))
4319 return false;
4321 if (!type_check (i, 1, BT_INTEGER))
4322 return false;
4324 return true;
4328 bool
4329 gfc_check_scan (gfc_expr *x, gfc_expr *y, gfc_expr *z, gfc_expr *kind)
4331 if (!type_check (x, 0, BT_CHARACTER))
4332 return false;
4334 if (!type_check (y, 1, BT_CHARACTER))
4335 return false;
4337 if (z != NULL && !type_check (z, 2, BT_LOGICAL))
4338 return false;
4340 if (!kind_check (kind, 3, BT_INTEGER))
4341 return false;
4342 if (kind && !gfc_notify_std (GFC_STD_F2003, "%qs intrinsic "
4343 "with KIND argument at %L",
4344 gfc_current_intrinsic, &kind->where))
4345 return false;
4347 if (!same_type_check (x, 0, y, 1))
4348 return false;
4350 return true;
4354 bool
4355 gfc_check_secnds (gfc_expr *r)
4357 if (!type_check (r, 0, BT_REAL))
4358 return false;
4360 if (!kind_value_check (r, 0, 4))
4361 return false;
4363 if (!scalar_check (r, 0))
4364 return false;
4366 return true;
4370 bool
4371 gfc_check_selected_char_kind (gfc_expr *name)
4373 if (!type_check (name, 0, BT_CHARACTER))
4374 return false;
4376 if (!kind_value_check (name, 0, gfc_default_character_kind))
4377 return false;
4379 if (!scalar_check (name, 0))
4380 return false;
4382 return true;
4386 bool
4387 gfc_check_selected_int_kind (gfc_expr *r)
4389 if (!type_check (r, 0, BT_INTEGER))
4390 return false;
4392 if (!scalar_check (r, 0))
4393 return false;
4395 return true;
4399 bool
4400 gfc_check_selected_real_kind (gfc_expr *p, gfc_expr *r, gfc_expr *radix)
4402 if (p == NULL && r == NULL
4403 && !gfc_notify_std (GFC_STD_F2008, "SELECTED_REAL_KIND with"
4404 " neither %<P%> nor %<R%> argument at %L",
4405 gfc_current_intrinsic_where))
4406 return false;
4408 if (p)
4410 if (!type_check (p, 0, BT_INTEGER))
4411 return false;
4413 if (!scalar_check (p, 0))
4414 return false;
4417 if (r)
4419 if (!type_check (r, 1, BT_INTEGER))
4420 return false;
4422 if (!scalar_check (r, 1))
4423 return false;
4426 if (radix)
4428 if (!type_check (radix, 1, BT_INTEGER))
4429 return false;
4431 if (!scalar_check (radix, 1))
4432 return false;
4434 if (!gfc_notify_std (GFC_STD_F2008, "%qs intrinsic with "
4435 "RADIX argument at %L", gfc_current_intrinsic,
4436 &radix->where))
4437 return false;
4440 return true;
4444 bool
4445 gfc_check_set_exponent (gfc_expr *x, gfc_expr *i)
4447 if (!type_check (x, 0, BT_REAL))
4448 return false;
4450 if (!type_check (i, 1, BT_INTEGER))
4451 return false;
4453 return true;
4457 bool
4458 gfc_check_shape (gfc_expr *source, gfc_expr *kind)
4460 gfc_array_ref *ar;
4462 if (source->rank == 0 || source->expr_type != EXPR_VARIABLE)
4463 return true;
4465 ar = gfc_find_array_ref (source);
4467 if (ar->as && ar->as->type == AS_ASSUMED_SIZE && ar->type == AR_FULL)
4469 gfc_error ("%<source%> argument of %<shape%> intrinsic at %L must not be "
4470 "an assumed size array", &source->where);
4471 return false;
4474 if (!kind_check (kind, 1, BT_INTEGER))
4475 return false;
4476 if (kind && !gfc_notify_std (GFC_STD_F2003, "%qs intrinsic "
4477 "with KIND argument at %L",
4478 gfc_current_intrinsic, &kind->where))
4479 return false;
4481 return true;
4485 bool
4486 gfc_check_shift (gfc_expr *i, gfc_expr *shift)
4488 if (!type_check (i, 0, BT_INTEGER))
4489 return false;
4491 if (!type_check (shift, 0, BT_INTEGER))
4492 return false;
4494 if (!nonnegative_check ("SHIFT", shift))
4495 return false;
4497 if (!less_than_bitsize1 ("I", i, "SHIFT", shift, true))
4498 return false;
4500 return true;
4504 bool
4505 gfc_check_sign (gfc_expr *a, gfc_expr *b)
4507 if (!int_or_real_check (a, 0))
4508 return false;
4510 if (!same_type_check (a, 0, b, 1))
4511 return false;
4513 return true;
4517 bool
4518 gfc_check_size (gfc_expr *array, gfc_expr *dim, gfc_expr *kind)
4520 if (!array_check (array, 0))
4521 return false;
4523 if (!dim_check (dim, 1, true))
4524 return false;
4526 if (!dim_rank_check (dim, array, 0))
4527 return false;
4529 if (!kind_check (kind, 2, BT_INTEGER))
4530 return false;
4531 if (kind && !gfc_notify_std (GFC_STD_F2003, "%qs intrinsic "
4532 "with KIND argument at %L",
4533 gfc_current_intrinsic, &kind->where))
4534 return false;
4537 return true;
4541 bool
4542 gfc_check_sizeof (gfc_expr *arg)
4544 if (arg->ts.type == BT_PROCEDURE)
4546 gfc_error ("%qs argument of %qs intrinsic at %L shall not be a procedure",
4547 gfc_current_intrinsic_arg[0]->name, gfc_current_intrinsic,
4548 &arg->where);
4549 return false;
4552 /* TYPE(*) is acceptable if and only if it uses an array descriptor. */
4553 if (arg->ts.type == BT_ASSUMED
4554 && (arg->symtree->n.sym->as == NULL
4555 || (arg->symtree->n.sym->as->type != AS_ASSUMED_SHAPE
4556 && arg->symtree->n.sym->as->type != AS_DEFERRED
4557 && arg->symtree->n.sym->as->type != AS_ASSUMED_RANK)))
4559 gfc_error ("%qs argument of %qs intrinsic at %L shall not be TYPE(*)",
4560 gfc_current_intrinsic_arg[0]->name, gfc_current_intrinsic,
4561 &arg->where);
4562 return false;
4565 if (arg->rank && arg->expr_type == EXPR_VARIABLE
4566 && arg->symtree->n.sym->as != NULL
4567 && arg->symtree->n.sym->as->type == AS_ASSUMED_SIZE && arg->ref
4568 && arg->ref->type == REF_ARRAY && arg->ref->u.ar.type == AR_FULL)
4570 gfc_error ("%qs argument of %qs intrinsic at %L shall not be an "
4571 "assumed-size array", gfc_current_intrinsic_arg[0]->name,
4572 gfc_current_intrinsic, &arg->where);
4573 return false;
4576 return true;
4580 /* Check whether an expression is interoperable. When returning false,
4581 msg is set to a string telling why the expression is not interoperable,
4582 otherwise, it is set to NULL. The msg string can be used in diagnostics.
4583 If c_loc is true, character with len > 1 are allowed (cf. Fortran
4584 2003corr5); additionally, assumed-shape/assumed-rank/deferred-shape
4585 arrays are permitted. And if c_f_ptr is true, deferred-shape arrays
4586 are permitted. */
4588 static bool
4589 is_c_interoperable (gfc_expr *expr, const char **msg, bool c_loc, bool c_f_ptr)
4591 *msg = NULL;
4593 if (expr->ts.type == BT_CLASS)
4595 *msg = "Expression is polymorphic";
4596 return false;
4599 if (expr->ts.type == BT_DERIVED && !expr->ts.u.derived->attr.is_bind_c
4600 && !expr->ts.u.derived->ts.is_iso_c)
4602 *msg = "Expression is a noninteroperable derived type";
4603 return false;
4606 if (expr->ts.type == BT_PROCEDURE)
4608 *msg = "Procedure unexpected as argument";
4609 return false;
4612 if (gfc_notification_std (GFC_STD_GNU) && expr->ts.type == BT_LOGICAL)
4614 int i;
4615 for (i = 0; gfc_logical_kinds[i].kind; i++)
4616 if (gfc_logical_kinds[i].kind == expr->ts.kind)
4617 return true;
4618 *msg = "Extension to use a non-C_Bool-kind LOGICAL";
4619 return false;
4622 if (gfc_notification_std (GFC_STD_GNU) && expr->ts.type == BT_CHARACTER
4623 && expr->ts.kind != 1)
4625 *msg = "Extension to use a non-C_CHAR-kind CHARACTER";
4626 return false;
4629 if (expr->ts.type == BT_CHARACTER) {
4630 if (expr->ts.deferred)
4632 /* TS 29113 allows deferred-length strings as dummy arguments,
4633 but it is not an interoperable type. */
4634 *msg = "Expression shall not be a deferred-length string";
4635 return false;
4638 if (expr->ts.u.cl && expr->ts.u.cl->length
4639 && !gfc_simplify_expr (expr->ts.u.cl->length, 0))
4640 gfc_internal_error ("is_c_interoperable(): gfc_simplify_expr failed");
4642 if (!c_loc && expr->ts.u.cl
4643 && (!expr->ts.u.cl->length
4644 || expr->ts.u.cl->length->expr_type != EXPR_CONSTANT
4645 || mpz_cmp_si (expr->ts.u.cl->length->value.integer, 1) != 0))
4647 *msg = "Type shall have a character length of 1";
4648 return false;
4652 /* Note: The following checks are about interoperatable variables, Fortran
4653 15.3.5/15.3.6. In intrinsics like C_LOC or in procedure interface, more
4654 is allowed, e.g. assumed-shape arrays with TS 29113. */
4656 if (gfc_is_coarray (expr))
4658 *msg = "Coarrays are not interoperable";
4659 return false;
4662 if (!c_loc && expr->rank > 0 && expr->expr_type != EXPR_ARRAY)
4664 gfc_array_ref *ar = gfc_find_array_ref (expr);
4665 if (ar->type != AR_FULL)
4667 *msg = "Only whole-arrays are interoperable";
4668 return false;
4670 if (!c_f_ptr && ar->as->type != AS_EXPLICIT
4671 && ar->as->type != AS_ASSUMED_SIZE)
4673 *msg = "Only explicit-size and assumed-size arrays are interoperable";
4674 return false;
4678 return true;
4682 bool
4683 gfc_check_c_sizeof (gfc_expr *arg)
4685 const char *msg;
4687 if (!is_c_interoperable (arg, &msg, false, false))
4689 gfc_error ("%qs argument of %qs intrinsic at %L must be an "
4690 "interoperable data entity: %s",
4691 gfc_current_intrinsic_arg[0]->name, gfc_current_intrinsic,
4692 &arg->where, msg);
4693 return false;
4696 if (arg->ts.type == BT_ASSUMED)
4698 gfc_error ("%qs argument of %qs intrinsic at %L shall not be "
4699 "TYPE(*)",
4700 gfc_current_intrinsic_arg[0]->name, gfc_current_intrinsic,
4701 &arg->where);
4702 return false;
4705 if (arg->rank && arg->expr_type == EXPR_VARIABLE
4706 && arg->symtree->n.sym->as != NULL
4707 && arg->symtree->n.sym->as->type == AS_ASSUMED_SIZE && arg->ref
4708 && arg->ref->type == REF_ARRAY && arg->ref->u.ar.type == AR_FULL)
4710 gfc_error ("%qs argument of %qs intrinsic at %L shall not be an "
4711 "assumed-size array", gfc_current_intrinsic_arg[0]->name,
4712 gfc_current_intrinsic, &arg->where);
4713 return false;
4716 return true;
4720 bool
4721 gfc_check_c_associated (gfc_expr *c_ptr_1, gfc_expr *c_ptr_2)
4723 if (c_ptr_1->ts.type != BT_DERIVED
4724 || c_ptr_1->ts.u.derived->from_intmod != INTMOD_ISO_C_BINDING
4725 || (c_ptr_1->ts.u.derived->intmod_sym_id != ISOCBINDING_PTR
4726 && c_ptr_1->ts.u.derived->intmod_sym_id != ISOCBINDING_FUNPTR))
4728 gfc_error ("Argument C_PTR_1 at %L to C_ASSOCIATED shall have the "
4729 "type TYPE(C_PTR) or TYPE(C_FUNPTR)", &c_ptr_1->where);
4730 return false;
4733 if (!scalar_check (c_ptr_1, 0))
4734 return false;
4736 if (c_ptr_2
4737 && (c_ptr_2->ts.type != BT_DERIVED
4738 || c_ptr_2->ts.u.derived->from_intmod != INTMOD_ISO_C_BINDING
4739 || (c_ptr_1->ts.u.derived->intmod_sym_id
4740 != c_ptr_2->ts.u.derived->intmod_sym_id)))
4742 gfc_error ("Argument C_PTR_2 at %L to C_ASSOCIATED shall have the "
4743 "same type as C_PTR_1: %s instead of %s", &c_ptr_1->where,
4744 gfc_typename (&c_ptr_1->ts),
4745 gfc_typename (&c_ptr_2->ts));
4746 return false;
4749 if (c_ptr_2 && !scalar_check (c_ptr_2, 1))
4750 return false;
4752 return true;
4756 bool
4757 gfc_check_c_f_pointer (gfc_expr *cptr, gfc_expr *fptr, gfc_expr *shape)
4759 symbol_attribute attr;
4760 const char *msg;
4762 if (cptr->ts.type != BT_DERIVED
4763 || cptr->ts.u.derived->from_intmod != INTMOD_ISO_C_BINDING
4764 || cptr->ts.u.derived->intmod_sym_id != ISOCBINDING_PTR)
4766 gfc_error ("Argument CPTR at %L to C_F_POINTER shall have the "
4767 "type TYPE(C_PTR)", &cptr->where);
4768 return false;
4771 if (!scalar_check (cptr, 0))
4772 return false;
4774 attr = gfc_expr_attr (fptr);
4776 if (!attr.pointer)
4778 gfc_error ("Argument FPTR at %L to C_F_POINTER must be a pointer",
4779 &fptr->where);
4780 return false;
4783 if (fptr->ts.type == BT_CLASS)
4785 gfc_error ("FPTR argument at %L to C_F_POINTER shall not be polymorphic",
4786 &fptr->where);
4787 return false;
4790 if (gfc_is_coindexed (fptr))
4792 gfc_error ("Argument FPTR at %L to C_F_POINTER shall not be "
4793 "coindexed", &fptr->where);
4794 return false;
4797 if (fptr->rank == 0 && shape)
4799 gfc_error ("Unexpected SHAPE argument at %L to C_F_POINTER with scalar "
4800 "FPTR", &fptr->where);
4801 return false;
4803 else if (fptr->rank && !shape)
4805 gfc_error ("Expected SHAPE argument to C_F_POINTER with array "
4806 "FPTR at %L", &fptr->where);
4807 return false;
4810 if (shape && !rank_check (shape, 2, 1))
4811 return false;
4813 if (shape && !type_check (shape, 2, BT_INTEGER))
4814 return false;
4816 if (shape)
4818 mpz_t size;
4819 if (gfc_array_size (shape, &size))
4821 if (mpz_cmp_ui (size, fptr->rank) != 0)
4823 mpz_clear (size);
4824 gfc_error ("SHAPE argument at %L to C_F_POINTER must have the same "
4825 "size as the RANK of FPTR", &shape->where);
4826 return false;
4828 mpz_clear (size);
4832 if (fptr->ts.type == BT_CLASS)
4834 gfc_error ("Polymorphic FPTR at %L to C_F_POINTER", &fptr->where);
4835 return false;
4838 if (fptr->rank > 0 && !is_c_interoperable (fptr, &msg, false, true))
4839 return gfc_notify_std (GFC_STD_F2018, "Noninteroperable array FPTR "
4840 "at %L to C_F_POINTER: %s", &fptr->where, msg);
4842 return true;
4846 bool
4847 gfc_check_c_f_procpointer (gfc_expr *cptr, gfc_expr *fptr)
4849 symbol_attribute attr;
4851 if (cptr->ts.type != BT_DERIVED
4852 || cptr->ts.u.derived->from_intmod != INTMOD_ISO_C_BINDING
4853 || cptr->ts.u.derived->intmod_sym_id != ISOCBINDING_FUNPTR)
4855 gfc_error ("Argument CPTR at %L to C_F_PROCPOINTER shall have the "
4856 "type TYPE(C_FUNPTR)", &cptr->where);
4857 return false;
4860 if (!scalar_check (cptr, 0))
4861 return false;
4863 attr = gfc_expr_attr (fptr);
4865 if (!attr.proc_pointer)
4867 gfc_error ("Argument FPTR at %L to C_F_PROCPOINTER shall be a procedure "
4868 "pointer", &fptr->where);
4869 return false;
4872 if (gfc_is_coindexed (fptr))
4874 gfc_error ("Argument FPTR at %L to C_F_PROCPOINTER shall not be "
4875 "coindexed", &fptr->where);
4876 return false;
4879 if (!attr.is_bind_c)
4880 return gfc_notify_std (GFC_STD_F2018, "Noninteroperable procedure "
4881 "pointer at %L to C_F_PROCPOINTER", &fptr->where);
4883 return true;
4887 bool
4888 gfc_check_c_funloc (gfc_expr *x)
4890 symbol_attribute attr;
4892 if (gfc_is_coindexed (x))
4894 gfc_error ("Argument X at %L to C_FUNLOC shall not be "
4895 "coindexed", &x->where);
4896 return false;
4899 attr = gfc_expr_attr (x);
4901 if (attr.function && !attr.proc_pointer && x->expr_type == EXPR_VARIABLE
4902 && x->symtree->n.sym == x->symtree->n.sym->result)
4904 gfc_namespace *ns = gfc_current_ns;
4906 for (ns = gfc_current_ns; ns; ns = ns->parent)
4907 if (x->symtree->n.sym == ns->proc_name)
4909 gfc_error ("Function result %qs at %L is invalid as X argument "
4910 "to C_FUNLOC", x->symtree->n.sym->name, &x->where);
4911 return false;
4915 if (attr.flavor != FL_PROCEDURE)
4917 gfc_error ("Argument X at %L to C_FUNLOC shall be a procedure "
4918 "or a procedure pointer", &x->where);
4919 return false;
4922 if (!attr.is_bind_c)
4923 return gfc_notify_std (GFC_STD_F2018, "Noninteroperable procedure "
4924 "at %L to C_FUNLOC", &x->where);
4925 return true;
4929 bool
4930 gfc_check_c_loc (gfc_expr *x)
4932 symbol_attribute attr;
4933 const char *msg;
4935 if (gfc_is_coindexed (x))
4937 gfc_error ("Argument X at %L to C_LOC shall not be coindexed", &x->where);
4938 return false;
4941 if (x->ts.type == BT_CLASS)
4943 gfc_error ("X argument at %L to C_LOC shall not be polymorphic",
4944 &x->where);
4945 return false;
4948 attr = gfc_expr_attr (x);
4950 if (!attr.pointer
4951 && (x->expr_type != EXPR_VARIABLE || !attr.target
4952 || attr.flavor == FL_PARAMETER))
4954 gfc_error ("Argument X at %L to C_LOC shall have either "
4955 "the POINTER or the TARGET attribute", &x->where);
4956 return false;
4959 if (x->ts.type == BT_CHARACTER
4960 && gfc_var_strlen (x) == 0)
4962 gfc_error ("Argument X at %L to C_LOC shall be not be a zero-sized "
4963 "string", &x->where);
4964 return false;
4967 if (!is_c_interoperable (x, &msg, true, false))
4969 if (x->ts.type == BT_CLASS)
4971 gfc_error ("Argument at %L to C_LOC shall not be polymorphic",
4972 &x->where);
4973 return false;
4976 if (x->rank
4977 && !gfc_notify_std (GFC_STD_F2018,
4978 "Noninteroperable array at %L as"
4979 " argument to C_LOC: %s", &x->where, msg))
4980 return false;
4982 else if (x->rank > 0 && gfc_notification_std (GFC_STD_F2008))
4984 gfc_array_ref *ar = gfc_find_array_ref (x);
4986 if (ar->as->type != AS_EXPLICIT && ar->as->type != AS_ASSUMED_SIZE
4987 && !attr.allocatable
4988 && !gfc_notify_std (GFC_STD_F2008,
4989 "Array of interoperable type at %L "
4990 "to C_LOC which is nonallocatable and neither "
4991 "assumed size nor explicit size", &x->where))
4992 return false;
4993 else if (ar->type != AR_FULL
4994 && !gfc_notify_std (GFC_STD_F2008, "Array section at %L "
4995 "to C_LOC", &x->where))
4996 return false;
4999 return true;
5003 bool
5004 gfc_check_sleep_sub (gfc_expr *seconds)
5006 if (!type_check (seconds, 0, BT_INTEGER))
5007 return false;
5009 if (!scalar_check (seconds, 0))
5010 return false;
5012 return true;
5015 bool
5016 gfc_check_sngl (gfc_expr *a)
5018 if (!type_check (a, 0, BT_REAL))
5019 return false;
5021 if ((a->ts.kind != gfc_default_double_kind)
5022 && !gfc_notify_std (GFC_STD_GNU, "non double precision "
5023 "REAL argument to %s intrinsic at %L",
5024 gfc_current_intrinsic, &a->where))
5025 return false;
5027 return true;
5030 bool
5031 gfc_check_spread (gfc_expr *source, gfc_expr *dim, gfc_expr *ncopies)
5033 if (source->rank >= GFC_MAX_DIMENSIONS)
5035 gfc_error ("%qs argument of %qs intrinsic at %L must be less "
5036 "than rank %d", gfc_current_intrinsic_arg[0]->name,
5037 gfc_current_intrinsic, &source->where, GFC_MAX_DIMENSIONS);
5039 return false;
5042 if (dim == NULL)
5043 return false;
5045 if (!dim_check (dim, 1, false))
5046 return false;
5048 /* dim_rank_check() does not apply here. */
5049 if (dim
5050 && dim->expr_type == EXPR_CONSTANT
5051 && (mpz_cmp_ui (dim->value.integer, 1) < 0
5052 || mpz_cmp_ui (dim->value.integer, source->rank + 1) > 0))
5054 gfc_error ("%qs argument of %qs intrinsic at %L is not a valid "
5055 "dimension index", gfc_current_intrinsic_arg[1]->name,
5056 gfc_current_intrinsic, &dim->where);
5057 return false;
5060 if (!type_check (ncopies, 2, BT_INTEGER))
5061 return false;
5063 if (!scalar_check (ncopies, 2))
5064 return false;
5066 return true;
5070 /* Functions for checking FGETC, FPUTC, FGET and FPUT (subroutines and
5071 functions). */
5073 bool
5074 gfc_check_fgetputc_sub (gfc_expr *unit, gfc_expr *c, gfc_expr *status)
5076 if (!type_check (unit, 0, BT_INTEGER))
5077 return false;
5079 if (!scalar_check (unit, 0))
5080 return false;
5082 if (!type_check (c, 1, BT_CHARACTER))
5083 return false;
5084 if (!kind_value_check (c, 1, gfc_default_character_kind))
5085 return false;
5087 if (status == NULL)
5088 return true;
5090 if (!type_check (status, 2, BT_INTEGER)
5091 || !kind_value_check (status, 2, gfc_default_integer_kind)
5092 || !scalar_check (status, 2))
5093 return false;
5095 return true;
5099 bool
5100 gfc_check_fgetputc (gfc_expr *unit, gfc_expr *c)
5102 return gfc_check_fgetputc_sub (unit, c, NULL);
5106 bool
5107 gfc_check_fgetput_sub (gfc_expr *c, gfc_expr *status)
5109 if (!type_check (c, 0, BT_CHARACTER))
5110 return false;
5111 if (!kind_value_check (c, 0, gfc_default_character_kind))
5112 return false;
5114 if (status == NULL)
5115 return true;
5117 if (!type_check (status, 1, BT_INTEGER)
5118 || !kind_value_check (status, 1, gfc_default_integer_kind)
5119 || !scalar_check (status, 1))
5120 return false;
5122 return true;
5126 bool
5127 gfc_check_fgetput (gfc_expr *c)
5129 return gfc_check_fgetput_sub (c, NULL);
5133 bool
5134 gfc_check_fseek_sub (gfc_expr *unit, gfc_expr *offset, gfc_expr *whence, gfc_expr *status)
5136 if (!type_check (unit, 0, BT_INTEGER))
5137 return false;
5139 if (!scalar_check (unit, 0))
5140 return false;
5142 if (!type_check (offset, 1, BT_INTEGER))
5143 return false;
5145 if (!scalar_check (offset, 1))
5146 return false;
5148 if (!type_check (whence, 2, BT_INTEGER))
5149 return false;
5151 if (!scalar_check (whence, 2))
5152 return false;
5154 if (status == NULL)
5155 return true;
5157 if (!type_check (status, 3, BT_INTEGER))
5158 return false;
5160 if (!kind_value_check (status, 3, 4))
5161 return false;
5163 if (!scalar_check (status, 3))
5164 return false;
5166 return true;
5171 bool
5172 gfc_check_fstat (gfc_expr *unit, gfc_expr *array)
5174 if (!type_check (unit, 0, BT_INTEGER))
5175 return false;
5177 if (!scalar_check (unit, 0))
5178 return false;
5180 if (!type_check (array, 1, BT_INTEGER)
5181 || !kind_value_check (unit, 0, gfc_default_integer_kind))
5182 return false;
5184 if (!array_check (array, 1))
5185 return false;
5187 return true;
5191 bool
5192 gfc_check_fstat_sub (gfc_expr *unit, gfc_expr *array, gfc_expr *status)
5194 if (!type_check (unit, 0, BT_INTEGER))
5195 return false;
5197 if (!scalar_check (unit, 0))
5198 return false;
5200 if (!type_check (array, 1, BT_INTEGER)
5201 || !kind_value_check (array, 1, gfc_default_integer_kind))
5202 return false;
5204 if (!array_check (array, 1))
5205 return false;
5207 if (status == NULL)
5208 return true;
5210 if (!type_check (status, 2, BT_INTEGER)
5211 || !kind_value_check (status, 2, gfc_default_integer_kind))
5212 return false;
5214 if (!scalar_check (status, 2))
5215 return false;
5217 return true;
5221 bool
5222 gfc_check_ftell (gfc_expr *unit)
5224 if (!type_check (unit, 0, BT_INTEGER))
5225 return false;
5227 if (!scalar_check (unit, 0))
5228 return false;
5230 return true;
5234 bool
5235 gfc_check_ftell_sub (gfc_expr *unit, gfc_expr *offset)
5237 if (!type_check (unit, 0, BT_INTEGER))
5238 return false;
5240 if (!scalar_check (unit, 0))
5241 return false;
5243 if (!type_check (offset, 1, BT_INTEGER))
5244 return false;
5246 if (!scalar_check (offset, 1))
5247 return false;
5249 return true;
5253 bool
5254 gfc_check_stat (gfc_expr *name, gfc_expr *array)
5256 if (!type_check (name, 0, BT_CHARACTER))
5257 return false;
5258 if (!kind_value_check (name, 0, gfc_default_character_kind))
5259 return false;
5261 if (!type_check (array, 1, BT_INTEGER)
5262 || !kind_value_check (array, 1, gfc_default_integer_kind))
5263 return false;
5265 if (!array_check (array, 1))
5266 return false;
5268 return true;
5272 bool
5273 gfc_check_stat_sub (gfc_expr *name, gfc_expr *array, gfc_expr *status)
5275 if (!type_check (name, 0, BT_CHARACTER))
5276 return false;
5277 if (!kind_value_check (name, 0, gfc_default_character_kind))
5278 return false;
5280 if (!type_check (array, 1, BT_INTEGER)
5281 || !kind_value_check (array, 1, gfc_default_integer_kind))
5282 return false;
5284 if (!array_check (array, 1))
5285 return false;
5287 if (status == NULL)
5288 return true;
5290 if (!type_check (status, 2, BT_INTEGER)
5291 || !kind_value_check (array, 1, gfc_default_integer_kind))
5292 return false;
5294 if (!scalar_check (status, 2))
5295 return false;
5297 return true;
5301 bool
5302 gfc_check_image_index (gfc_expr *coarray, gfc_expr *sub)
5304 mpz_t nelems;
5306 if (flag_coarray == GFC_FCOARRAY_NONE)
5308 gfc_fatal_error ("Coarrays disabled at %C, use %<-fcoarray=%> to enable");
5309 return false;
5312 if (!coarray_check (coarray, 0))
5313 return false;
5315 if (sub->rank != 1)
5317 gfc_error ("%s argument to IMAGE_INDEX must be a rank one array at %L",
5318 gfc_current_intrinsic_arg[1]->name, &sub->where);
5319 return false;
5322 if (gfc_array_size (sub, &nelems))
5324 int corank = gfc_get_corank (coarray);
5326 if (mpz_cmp_ui (nelems, corank) != 0)
5328 gfc_error ("The number of array elements of the SUB argument to "
5329 "IMAGE_INDEX at %L shall be %d (corank) not %d",
5330 &sub->where, corank, (int) mpz_get_si (nelems));
5331 mpz_clear (nelems);
5332 return false;
5334 mpz_clear (nelems);
5337 return true;
5341 bool
5342 gfc_check_num_images (gfc_expr *distance, gfc_expr *failed)
5344 if (flag_coarray == GFC_FCOARRAY_NONE)
5346 gfc_fatal_error ("Coarrays disabled at %C, use %<-fcoarray=%> to enable");
5347 return false;
5350 if (distance)
5352 if (!type_check (distance, 0, BT_INTEGER))
5353 return false;
5355 if (!nonnegative_check ("DISTANCE", distance))
5356 return false;
5358 if (!scalar_check (distance, 0))
5359 return false;
5361 if (!gfc_notify_std (GFC_STD_F2018, "DISTANCE= argument to "
5362 "NUM_IMAGES at %L", &distance->where))
5363 return false;
5366 if (failed)
5368 if (!type_check (failed, 1, BT_LOGICAL))
5369 return false;
5371 if (!scalar_check (failed, 1))
5372 return false;
5374 if (!gfc_notify_std (GFC_STD_F2018, "FAILED= argument to "
5375 "NUM_IMAGES at %L", &failed->where))
5376 return false;
5379 return true;
5383 bool
5384 gfc_check_team_number (gfc_expr *team)
5386 if (flag_coarray == GFC_FCOARRAY_NONE)
5388 gfc_fatal_error ("Coarrays disabled at %C, use %<-fcoarray=%> to enable");
5389 return false;
5392 if (team)
5394 if (team->ts.type != BT_DERIVED
5395 || team->ts.u.derived->from_intmod != INTMOD_ISO_FORTRAN_ENV
5396 || team->ts.u.derived->intmod_sym_id != ISOFORTRAN_TEAM_TYPE)
5398 gfc_error ("TEAM argument at %L to the intrinsic TEAM_NUMBER "
5399 "shall be of type TEAM_TYPE", &team->where);
5400 return false;
5403 else
5404 return true;
5406 return true;
5410 bool
5411 gfc_check_this_image (gfc_expr *coarray, gfc_expr *dim, gfc_expr *distance)
5413 if (flag_coarray == GFC_FCOARRAY_NONE)
5415 gfc_fatal_error ("Coarrays disabled at %C, use %<-fcoarray=%> to enable");
5416 return false;
5419 if (coarray == NULL && dim == NULL && distance == NULL)
5420 return true;
5422 if (dim != NULL && coarray == NULL)
5424 gfc_error ("DIM argument without COARRAY argument not allowed for "
5425 "THIS_IMAGE intrinsic at %L", &dim->where);
5426 return false;
5429 if (distance && (coarray || dim))
5431 gfc_error ("The DISTANCE argument may not be specified together with the "
5432 "COARRAY or DIM argument in intrinsic at %L",
5433 &distance->where);
5434 return false;
5437 /* Assume that we have "this_image (distance)". */
5438 if (coarray && !gfc_is_coarray (coarray) && coarray->ts.type == BT_INTEGER)
5440 if (dim)
5442 gfc_error ("Unexpected DIM argument with noncoarray argument at %L",
5443 &coarray->where);
5444 return false;
5446 distance = coarray;
5449 if (distance)
5451 if (!type_check (distance, 2, BT_INTEGER))
5452 return false;
5454 if (!nonnegative_check ("DISTANCE", distance))
5455 return false;
5457 if (!scalar_check (distance, 2))
5458 return false;
5460 if (!gfc_notify_std (GFC_STD_F2018, "DISTANCE= argument to "
5461 "THIS_IMAGE at %L", &distance->where))
5462 return false;
5464 return true;
5467 if (!coarray_check (coarray, 0))
5468 return false;
5470 if (dim != NULL)
5472 if (!dim_check (dim, 1, false))
5473 return false;
5475 if (!dim_corank_check (dim, coarray))
5476 return false;
5479 return true;
5482 /* Calculate the sizes for transfer, used by gfc_check_transfer and also
5483 by gfc_simplify_transfer. Return false if we cannot do so. */
5485 bool
5486 gfc_calculate_transfer_sizes (gfc_expr *source, gfc_expr *mold, gfc_expr *size,
5487 size_t *source_size, size_t *result_size,
5488 size_t *result_length_p)
5490 size_t result_elt_size;
5492 if (source->expr_type == EXPR_FUNCTION)
5493 return false;
5495 if (size && size->expr_type != EXPR_CONSTANT)
5496 return false;
5498 /* Calculate the size of the source. */
5499 *source_size = gfc_target_expr_size (source);
5500 if (*source_size == 0)
5501 return false;
5503 /* Determine the size of the element. */
5504 result_elt_size = gfc_element_size (mold);
5505 if (result_elt_size == 0)
5506 return false;
5508 if (mold->expr_type == EXPR_ARRAY || mold->rank || size)
5510 int result_length;
5512 if (size)
5513 result_length = (size_t)mpz_get_ui (size->value.integer);
5514 else
5516 result_length = *source_size / result_elt_size;
5517 if (result_length * result_elt_size < *source_size)
5518 result_length += 1;
5521 *result_size = result_length * result_elt_size;
5522 if (result_length_p)
5523 *result_length_p = result_length;
5525 else
5526 *result_size = result_elt_size;
5528 return true;
5532 bool
5533 gfc_check_transfer (gfc_expr *source, gfc_expr *mold, gfc_expr *size)
5535 size_t source_size;
5536 size_t result_size;
5538 if (mold->ts.type == BT_HOLLERITH)
5540 gfc_error ("%<MOLD%> argument of %<TRANSFER%> intrinsic at %L must not be"
5541 " %s", &mold->where, gfc_basic_typename (BT_HOLLERITH));
5542 return false;
5545 if (size != NULL)
5547 if (!type_check (size, 2, BT_INTEGER))
5548 return false;
5550 if (!scalar_check (size, 2))
5551 return false;
5553 if (!nonoptional_check (size, 2))
5554 return false;
5557 if (!warn_surprising)
5558 return true;
5560 /* If we can't calculate the sizes, we cannot check any more.
5561 Return true for that case. */
5563 if (!gfc_calculate_transfer_sizes (source, mold, size, &source_size,
5564 &result_size, NULL))
5565 return true;
5567 if (source_size < result_size)
5568 gfc_warning (OPT_Wsurprising,
5569 "Intrinsic TRANSFER at %L has partly undefined result: "
5570 "source size %ld < result size %ld", &source->where,
5571 (long) source_size, (long) result_size);
5573 return true;
5577 bool
5578 gfc_check_transpose (gfc_expr *matrix)
5580 if (!rank_check (matrix, 0, 2))
5581 return false;
5583 return true;
5587 bool
5588 gfc_check_ubound (gfc_expr *array, gfc_expr *dim, gfc_expr *kind)
5590 if (!array_check (array, 0))
5591 return false;
5593 if (!dim_check (dim, 1, false))
5594 return false;
5596 if (!dim_rank_check (dim, array, 0))
5597 return false;
5599 if (!kind_check (kind, 2, BT_INTEGER))
5600 return false;
5601 if (kind && !gfc_notify_std (GFC_STD_F2003, "%qs intrinsic "
5602 "with KIND argument at %L",
5603 gfc_current_intrinsic, &kind->where))
5604 return false;
5606 return true;
5610 bool
5611 gfc_check_ucobound (gfc_expr *coarray, gfc_expr *dim, gfc_expr *kind)
5613 if (flag_coarray == GFC_FCOARRAY_NONE)
5615 gfc_fatal_error ("Coarrays disabled at %C, use %<-fcoarray=%> to enable");
5616 return false;
5619 if (!coarray_check (coarray, 0))
5620 return false;
5622 if (dim != NULL)
5624 if (!dim_check (dim, 1, false))
5625 return false;
5627 if (!dim_corank_check (dim, coarray))
5628 return false;
5631 if (!kind_check (kind, 2, BT_INTEGER))
5632 return false;
5634 return true;
5638 bool
5639 gfc_check_unpack (gfc_expr *vector, gfc_expr *mask, gfc_expr *field)
5641 mpz_t vector_size;
5643 if (!rank_check (vector, 0, 1))
5644 return false;
5646 if (!array_check (mask, 1))
5647 return false;
5649 if (!type_check (mask, 1, BT_LOGICAL))
5650 return false;
5652 if (!same_type_check (vector, 0, field, 2))
5653 return false;
5655 if (mask->expr_type == EXPR_ARRAY
5656 && gfc_array_size (vector, &vector_size))
5658 int mask_true_count = 0;
5659 gfc_constructor *mask_ctor;
5660 mask_ctor = gfc_constructor_first (mask->value.constructor);
5661 while (mask_ctor)
5663 if (mask_ctor->expr->expr_type != EXPR_CONSTANT)
5665 mask_true_count = 0;
5666 break;
5669 if (mask_ctor->expr->value.logical)
5670 mask_true_count++;
5672 mask_ctor = gfc_constructor_next (mask_ctor);
5675 if (mpz_get_si (vector_size) < mask_true_count)
5677 gfc_error ("%qs argument of %qs intrinsic at %L must "
5678 "provide at least as many elements as there "
5679 "are .TRUE. values in %qs (%ld/%d)",
5680 gfc_current_intrinsic_arg[0]->name, gfc_current_intrinsic,
5681 &vector->where, gfc_current_intrinsic_arg[1]->name,
5682 mpz_get_si (vector_size), mask_true_count);
5683 return false;
5686 mpz_clear (vector_size);
5689 if (mask->rank != field->rank && field->rank != 0)
5691 gfc_error ("%qs argument of %qs intrinsic at %L must have "
5692 "the same rank as %qs or be a scalar",
5693 gfc_current_intrinsic_arg[2]->name, gfc_current_intrinsic,
5694 &field->where, gfc_current_intrinsic_arg[1]->name);
5695 return false;
5698 if (mask->rank == field->rank)
5700 int i;
5701 for (i = 0; i < field->rank; i++)
5702 if (! identical_dimen_shape (mask, i, field, i))
5704 gfc_error ("%qs and %qs arguments of %qs intrinsic at %L "
5705 "must have identical shape.",
5706 gfc_current_intrinsic_arg[2]->name,
5707 gfc_current_intrinsic_arg[1]->name, gfc_current_intrinsic,
5708 &field->where);
5712 return true;
5716 bool
5717 gfc_check_verify (gfc_expr *x, gfc_expr *y, gfc_expr *z, gfc_expr *kind)
5719 if (!type_check (x, 0, BT_CHARACTER))
5720 return false;
5722 if (!same_type_check (x, 0, y, 1))
5723 return false;
5725 if (z != NULL && !type_check (z, 2, BT_LOGICAL))
5726 return false;
5728 if (!kind_check (kind, 3, BT_INTEGER))
5729 return false;
5730 if (kind && !gfc_notify_std (GFC_STD_F2003, "%qs intrinsic "
5731 "with KIND argument at %L",
5732 gfc_current_intrinsic, &kind->where))
5733 return false;
5735 return true;
5739 bool
5740 gfc_check_trim (gfc_expr *x)
5742 if (!type_check (x, 0, BT_CHARACTER))
5743 return false;
5745 if (!scalar_check (x, 0))
5746 return false;
5748 return true;
5752 bool
5753 gfc_check_ttynam (gfc_expr *unit)
5755 if (!scalar_check (unit, 0))
5756 return false;
5758 if (!type_check (unit, 0, BT_INTEGER))
5759 return false;
5761 return true;
5765 /************* Check functions for intrinsic subroutines *************/
5767 bool
5768 gfc_check_cpu_time (gfc_expr *time)
5770 if (!scalar_check (time, 0))
5771 return false;
5773 if (!type_check (time, 0, BT_REAL))
5774 return false;
5776 if (!variable_check (time, 0, false))
5777 return false;
5779 return true;
5783 bool
5784 gfc_check_date_and_time (gfc_expr *date, gfc_expr *time,
5785 gfc_expr *zone, gfc_expr *values)
5787 if (date != NULL)
5789 if (!type_check (date, 0, BT_CHARACTER))
5790 return false;
5791 if (!kind_value_check (date, 0, gfc_default_character_kind))
5792 return false;
5793 if (!scalar_check (date, 0))
5794 return false;
5795 if (!variable_check (date, 0, false))
5796 return false;
5799 if (time != NULL)
5801 if (!type_check (time, 1, BT_CHARACTER))
5802 return false;
5803 if (!kind_value_check (time, 1, gfc_default_character_kind))
5804 return false;
5805 if (!scalar_check (time, 1))
5806 return false;
5807 if (!variable_check (time, 1, false))
5808 return false;
5811 if (zone != NULL)
5813 if (!type_check (zone, 2, BT_CHARACTER))
5814 return false;
5815 if (!kind_value_check (zone, 2, gfc_default_character_kind))
5816 return false;
5817 if (!scalar_check (zone, 2))
5818 return false;
5819 if (!variable_check (zone, 2, false))
5820 return false;
5823 if (values != NULL)
5825 if (!type_check (values, 3, BT_INTEGER))
5826 return false;
5827 if (!array_check (values, 3))
5828 return false;
5829 if (!rank_check (values, 3, 1))
5830 return false;
5831 if (!variable_check (values, 3, false))
5832 return false;
5835 return true;
5839 bool
5840 gfc_check_mvbits (gfc_expr *from, gfc_expr *frompos, gfc_expr *len,
5841 gfc_expr *to, gfc_expr *topos)
5843 if (!type_check (from, 0, BT_INTEGER))
5844 return false;
5846 if (!type_check (frompos, 1, BT_INTEGER))
5847 return false;
5849 if (!type_check (len, 2, BT_INTEGER))
5850 return false;
5852 if (!same_type_check (from, 0, to, 3))
5853 return false;
5855 if (!variable_check (to, 3, false))
5856 return false;
5858 if (!type_check (topos, 4, BT_INTEGER))
5859 return false;
5861 if (!nonnegative_check ("frompos", frompos))
5862 return false;
5864 if (!nonnegative_check ("topos", topos))
5865 return false;
5867 if (!nonnegative_check ("len", len))
5868 return false;
5870 if (!less_than_bitsize2 ("from", from, "frompos", frompos, "len", len))
5871 return false;
5873 if (!less_than_bitsize2 ("to", to, "topos", topos, "len", len))
5874 return false;
5876 return true;
5880 /* Check the arguments for RANDOM_INIT. */
5882 bool
5883 gfc_check_random_init (gfc_expr *repeatable, gfc_expr *image_distinct)
5885 if (!type_check (repeatable, 0, BT_LOGICAL))
5886 return false;
5888 if (!scalar_check (repeatable, 0))
5889 return false;
5891 if (!type_check (image_distinct, 1, BT_LOGICAL))
5892 return false;
5894 if (!scalar_check (image_distinct, 1))
5895 return false;
5897 return true;
5901 bool
5902 gfc_check_random_number (gfc_expr *harvest)
5904 if (!type_check (harvest, 0, BT_REAL))
5905 return false;
5907 if (!variable_check (harvest, 0, false))
5908 return false;
5910 return true;
5914 bool
5915 gfc_check_random_seed (gfc_expr *size, gfc_expr *put, gfc_expr *get)
5917 unsigned int nargs = 0, seed_size;
5918 locus *where = NULL;
5919 mpz_t put_size, get_size;
5921 /* Keep the number of bytes in sync with master_state in
5922 libgfortran/intrinsics/random.c. +1 due to the integer p which is
5923 part of the state too. */
5924 seed_size = 128 / gfc_default_integer_kind + 1;
5926 if (size != NULL)
5928 if (size->expr_type != EXPR_VARIABLE
5929 || !size->symtree->n.sym->attr.optional)
5930 nargs++;
5932 if (!scalar_check (size, 0))
5933 return false;
5935 if (!type_check (size, 0, BT_INTEGER))
5936 return false;
5938 if (!variable_check (size, 0, false))
5939 return false;
5941 if (!kind_value_check (size, 0, gfc_default_integer_kind))
5942 return false;
5945 if (put != NULL)
5947 if (put->expr_type != EXPR_VARIABLE
5948 || !put->symtree->n.sym->attr.optional)
5950 nargs++;
5951 where = &put->where;
5954 if (!array_check (put, 1))
5955 return false;
5957 if (!rank_check (put, 1, 1))
5958 return false;
5960 if (!type_check (put, 1, BT_INTEGER))
5961 return false;
5963 if (!kind_value_check (put, 1, gfc_default_integer_kind))
5964 return false;
5966 if (gfc_array_size (put, &put_size)
5967 && mpz_get_ui (put_size) < seed_size)
5968 gfc_error ("Size of %qs argument of %qs intrinsic at %L "
5969 "too small (%i/%i)",
5970 gfc_current_intrinsic_arg[1]->name, gfc_current_intrinsic,
5971 where, (int) mpz_get_ui (put_size), seed_size);
5974 if (get != NULL)
5976 if (get->expr_type != EXPR_VARIABLE
5977 || !get->symtree->n.sym->attr.optional)
5979 nargs++;
5980 where = &get->where;
5983 if (!array_check (get, 2))
5984 return false;
5986 if (!rank_check (get, 2, 1))
5987 return false;
5989 if (!type_check (get, 2, BT_INTEGER))
5990 return false;
5992 if (!variable_check (get, 2, false))
5993 return false;
5995 if (!kind_value_check (get, 2, gfc_default_integer_kind))
5996 return false;
5998 if (gfc_array_size (get, &get_size)
5999 && mpz_get_ui (get_size) < seed_size)
6000 gfc_error ("Size of %qs argument of %qs intrinsic at %L "
6001 "too small (%i/%i)",
6002 gfc_current_intrinsic_arg[2]->name, gfc_current_intrinsic,
6003 where, (int) mpz_get_ui (get_size), seed_size);
6006 /* RANDOM_SEED may not have more than one non-optional argument. */
6007 if (nargs > 1)
6008 gfc_error ("Too many arguments to %s at %L", gfc_current_intrinsic, where);
6010 return true;
6013 bool
6014 gfc_check_fe_runtime_error (gfc_actual_arglist *a)
6016 gfc_expr *e;
6017 size_t len, i;
6018 int num_percent, nargs;
6020 e = a->expr;
6021 if (e->expr_type != EXPR_CONSTANT)
6022 return true;
6024 len = e->value.character.length;
6025 if (e->value.character.string[len-1] != '\0')
6026 gfc_internal_error ("fe_runtime_error string must be null terminated");
6028 num_percent = 0;
6029 for (i=0; i<len-1; i++)
6030 if (e->value.character.string[i] == '%')
6031 num_percent ++;
6033 nargs = 0;
6034 for (; a; a = a->next)
6035 nargs ++;
6037 if (nargs -1 != num_percent)
6038 gfc_internal_error ("fe_runtime_error: Wrong number of arguments (%d instead of %d)",
6039 nargs, num_percent++);
6041 return true;
6044 bool
6045 gfc_check_second_sub (gfc_expr *time)
6047 if (!scalar_check (time, 0))
6048 return false;
6050 if (!type_check (time, 0, BT_REAL))
6051 return false;
6053 if (!kind_value_check (time, 0, 4))
6054 return false;
6056 return true;
6060 /* COUNT and COUNT_MAX of SYSTEM_CLOCK are scalar, default-kind integer
6061 variables in Fortran 95. In Fortran 2003 and later, they can be of any
6062 kind, and COUNT_RATE can be of type real. Note, count, count_rate, and
6063 count_max are all optional arguments */
6065 bool
6066 gfc_check_system_clock (gfc_expr *count, gfc_expr *count_rate,
6067 gfc_expr *count_max)
6069 if (count != NULL)
6071 if (!scalar_check (count, 0))
6072 return false;
6074 if (!type_check (count, 0, BT_INTEGER))
6075 return false;
6077 if (count->ts.kind != gfc_default_integer_kind
6078 && !gfc_notify_std (GFC_STD_F2003, "COUNT argument to "
6079 "SYSTEM_CLOCK at %L has non-default kind",
6080 &count->where))
6081 return false;
6083 if (!variable_check (count, 0, false))
6084 return false;
6087 if (count_rate != NULL)
6089 if (!scalar_check (count_rate, 1))
6090 return false;
6092 if (!variable_check (count_rate, 1, false))
6093 return false;
6095 if (count_rate->ts.type == BT_REAL)
6097 if (!gfc_notify_std (GFC_STD_F2003, "Real COUNT_RATE argument to "
6098 "SYSTEM_CLOCK at %L", &count_rate->where))
6099 return false;
6101 else
6103 if (!type_check (count_rate, 1, BT_INTEGER))
6104 return false;
6106 if (count_rate->ts.kind != gfc_default_integer_kind
6107 && !gfc_notify_std (GFC_STD_F2003, "COUNT_RATE argument to "
6108 "SYSTEM_CLOCK at %L has non-default kind",
6109 &count_rate->where))
6110 return false;
6115 if (count_max != NULL)
6117 if (!scalar_check (count_max, 2))
6118 return false;
6120 if (!type_check (count_max, 2, BT_INTEGER))
6121 return false;
6123 if (count_max->ts.kind != gfc_default_integer_kind
6124 && !gfc_notify_std (GFC_STD_F2003, "COUNT_MAX argument to "
6125 "SYSTEM_CLOCK at %L has non-default kind",
6126 &count_max->where))
6127 return false;
6129 if (!variable_check (count_max, 2, false))
6130 return false;
6133 return true;
6137 bool
6138 gfc_check_irand (gfc_expr *x)
6140 if (x == NULL)
6141 return true;
6143 if (!scalar_check (x, 0))
6144 return false;
6146 if (!type_check (x, 0, BT_INTEGER))
6147 return false;
6149 if (!kind_value_check (x, 0, 4))
6150 return false;
6152 return true;
6156 bool
6157 gfc_check_alarm_sub (gfc_expr *seconds, gfc_expr *handler, gfc_expr *status)
6159 if (!scalar_check (seconds, 0))
6160 return false;
6161 if (!type_check (seconds, 0, BT_INTEGER))
6162 return false;
6164 if (!int_or_proc_check (handler, 1))
6165 return false;
6166 if (handler->ts.type == BT_INTEGER && !scalar_check (handler, 1))
6167 return false;
6169 if (status == NULL)
6170 return true;
6172 if (!scalar_check (status, 2))
6173 return false;
6174 if (!type_check (status, 2, BT_INTEGER))
6175 return false;
6176 if (!kind_value_check (status, 2, gfc_default_integer_kind))
6177 return false;
6179 return true;
6183 bool
6184 gfc_check_rand (gfc_expr *x)
6186 if (x == NULL)
6187 return true;
6189 if (!scalar_check (x, 0))
6190 return false;
6192 if (!type_check (x, 0, BT_INTEGER))
6193 return false;
6195 if (!kind_value_check (x, 0, 4))
6196 return false;
6198 return true;
6202 bool
6203 gfc_check_srand (gfc_expr *x)
6205 if (!scalar_check (x, 0))
6206 return false;
6208 if (!type_check (x, 0, BT_INTEGER))
6209 return false;
6211 if (!kind_value_check (x, 0, 4))
6212 return false;
6214 return true;
6218 bool
6219 gfc_check_ctime_sub (gfc_expr *time, gfc_expr *result)
6221 if (!scalar_check (time, 0))
6222 return false;
6223 if (!type_check (time, 0, BT_INTEGER))
6224 return false;
6226 if (!type_check (result, 1, BT_CHARACTER))
6227 return false;
6228 if (!kind_value_check (result, 1, gfc_default_character_kind))
6229 return false;
6231 return true;
6235 bool
6236 gfc_check_dtime_etime (gfc_expr *x)
6238 if (!array_check (x, 0))
6239 return false;
6241 if (!rank_check (x, 0, 1))
6242 return false;
6244 if (!variable_check (x, 0, false))
6245 return false;
6247 if (!type_check (x, 0, BT_REAL))
6248 return false;
6250 if (!kind_value_check (x, 0, 4))
6251 return false;
6253 return true;
6257 bool
6258 gfc_check_dtime_etime_sub (gfc_expr *values, gfc_expr *time)
6260 if (!array_check (values, 0))
6261 return false;
6263 if (!rank_check (values, 0, 1))
6264 return false;
6266 if (!variable_check (values, 0, false))
6267 return false;
6269 if (!type_check (values, 0, BT_REAL))
6270 return false;
6272 if (!kind_value_check (values, 0, 4))
6273 return false;
6275 if (!scalar_check (time, 1))
6276 return false;
6278 if (!type_check (time, 1, BT_REAL))
6279 return false;
6281 if (!kind_value_check (time, 1, 4))
6282 return false;
6284 return true;
6288 bool
6289 gfc_check_fdate_sub (gfc_expr *date)
6291 if (!type_check (date, 0, BT_CHARACTER))
6292 return false;
6293 if (!kind_value_check (date, 0, gfc_default_character_kind))
6294 return false;
6296 return true;
6300 bool
6301 gfc_check_gerror (gfc_expr *msg)
6303 if (!type_check (msg, 0, BT_CHARACTER))
6304 return false;
6305 if (!kind_value_check (msg, 0, gfc_default_character_kind))
6306 return false;
6308 return true;
6312 bool
6313 gfc_check_getcwd_sub (gfc_expr *cwd, gfc_expr *status)
6315 if (!type_check (cwd, 0, BT_CHARACTER))
6316 return false;
6317 if (!kind_value_check (cwd, 0, gfc_default_character_kind))
6318 return false;
6320 if (status == NULL)
6321 return true;
6323 if (!scalar_check (status, 1))
6324 return false;
6326 if (!type_check (status, 1, BT_INTEGER))
6327 return false;
6329 return true;
6333 bool
6334 gfc_check_getarg (gfc_expr *pos, gfc_expr *value)
6336 if (!type_check (pos, 0, BT_INTEGER))
6337 return false;
6339 if (pos->ts.kind > gfc_default_integer_kind)
6341 gfc_error ("%qs argument of %qs intrinsic at %L must be of a kind "
6342 "not wider than the default kind (%d)",
6343 gfc_current_intrinsic_arg[0]->name, gfc_current_intrinsic,
6344 &pos->where, gfc_default_integer_kind);
6345 return false;
6348 if (!type_check (value, 1, BT_CHARACTER))
6349 return false;
6350 if (!kind_value_check (value, 1, gfc_default_character_kind))
6351 return false;
6353 return true;
6357 bool
6358 gfc_check_getlog (gfc_expr *msg)
6360 if (!type_check (msg, 0, BT_CHARACTER))
6361 return false;
6362 if (!kind_value_check (msg, 0, gfc_default_character_kind))
6363 return false;
6365 return true;
6369 bool
6370 gfc_check_exit (gfc_expr *status)
6372 if (status == NULL)
6373 return true;
6375 if (!type_check (status, 0, BT_INTEGER))
6376 return false;
6378 if (!scalar_check (status, 0))
6379 return false;
6381 return true;
6385 bool
6386 gfc_check_flush (gfc_expr *unit)
6388 if (unit == NULL)
6389 return true;
6391 if (!type_check (unit, 0, BT_INTEGER))
6392 return false;
6394 if (!scalar_check (unit, 0))
6395 return false;
6397 return true;
6401 bool
6402 gfc_check_free (gfc_expr *i)
6404 if (!type_check (i, 0, BT_INTEGER))
6405 return false;
6407 if (!scalar_check (i, 0))
6408 return false;
6410 return true;
6414 bool
6415 gfc_check_hostnm (gfc_expr *name)
6417 if (!type_check (name, 0, BT_CHARACTER))
6418 return false;
6419 if (!kind_value_check (name, 0, gfc_default_character_kind))
6420 return false;
6422 return true;
6426 bool
6427 gfc_check_hostnm_sub (gfc_expr *name, gfc_expr *status)
6429 if (!type_check (name, 0, BT_CHARACTER))
6430 return false;
6431 if (!kind_value_check (name, 0, gfc_default_character_kind))
6432 return false;
6434 if (status == NULL)
6435 return true;
6437 if (!scalar_check (status, 1))
6438 return false;
6440 if (!type_check (status, 1, BT_INTEGER))
6441 return false;
6443 return true;
6447 bool
6448 gfc_check_itime_idate (gfc_expr *values)
6450 if (!array_check (values, 0))
6451 return false;
6453 if (!rank_check (values, 0, 1))
6454 return false;
6456 if (!variable_check (values, 0, false))
6457 return false;
6459 if (!type_check (values, 0, BT_INTEGER))
6460 return false;
6462 if (!kind_value_check (values, 0, gfc_default_integer_kind))
6463 return false;
6465 return true;
6469 bool
6470 gfc_check_ltime_gmtime (gfc_expr *time, gfc_expr *values)
6472 if (!type_check (time, 0, BT_INTEGER))
6473 return false;
6475 if (!kind_value_check (time, 0, gfc_default_integer_kind))
6476 return false;
6478 if (!scalar_check (time, 0))
6479 return false;
6481 if (!array_check (values, 1))
6482 return false;
6484 if (!rank_check (values, 1, 1))
6485 return false;
6487 if (!variable_check (values, 1, false))
6488 return false;
6490 if (!type_check (values, 1, BT_INTEGER))
6491 return false;
6493 if (!kind_value_check (values, 1, gfc_default_integer_kind))
6494 return false;
6496 return true;
6500 bool
6501 gfc_check_ttynam_sub (gfc_expr *unit, gfc_expr *name)
6503 if (!scalar_check (unit, 0))
6504 return false;
6506 if (!type_check (unit, 0, BT_INTEGER))
6507 return false;
6509 if (!type_check (name, 1, BT_CHARACTER))
6510 return false;
6511 if (!kind_value_check (name, 1, gfc_default_character_kind))
6512 return false;
6514 return true;
6518 bool
6519 gfc_check_isatty (gfc_expr *unit)
6521 if (unit == NULL)
6522 return false;
6524 if (!type_check (unit, 0, BT_INTEGER))
6525 return false;
6527 if (!scalar_check (unit, 0))
6528 return false;
6530 return true;
6534 bool
6535 gfc_check_isnan (gfc_expr *x)
6537 if (!type_check (x, 0, BT_REAL))
6538 return false;
6540 return true;
6544 bool
6545 gfc_check_perror (gfc_expr *string)
6547 if (!type_check (string, 0, BT_CHARACTER))
6548 return false;
6549 if (!kind_value_check (string, 0, gfc_default_character_kind))
6550 return false;
6552 return true;
6556 bool
6557 gfc_check_umask (gfc_expr *mask)
6559 if (!type_check (mask, 0, BT_INTEGER))
6560 return false;
6562 if (!scalar_check (mask, 0))
6563 return false;
6565 return true;
6569 bool
6570 gfc_check_umask_sub (gfc_expr *mask, gfc_expr *old)
6572 if (!type_check (mask, 0, BT_INTEGER))
6573 return false;
6575 if (!scalar_check (mask, 0))
6576 return false;
6578 if (old == NULL)
6579 return true;
6581 if (!scalar_check (old, 1))
6582 return false;
6584 if (!type_check (old, 1, BT_INTEGER))
6585 return false;
6587 return true;
6591 bool
6592 gfc_check_unlink (gfc_expr *name)
6594 if (!type_check (name, 0, BT_CHARACTER))
6595 return false;
6596 if (!kind_value_check (name, 0, gfc_default_character_kind))
6597 return false;
6599 return true;
6603 bool
6604 gfc_check_unlink_sub (gfc_expr *name, gfc_expr *status)
6606 if (!type_check (name, 0, BT_CHARACTER))
6607 return false;
6608 if (!kind_value_check (name, 0, gfc_default_character_kind))
6609 return false;
6611 if (status == NULL)
6612 return true;
6614 if (!scalar_check (status, 1))
6615 return false;
6617 if (!type_check (status, 1, BT_INTEGER))
6618 return false;
6620 return true;
6624 bool
6625 gfc_check_signal (gfc_expr *number, gfc_expr *handler)
6627 if (!scalar_check (number, 0))
6628 return false;
6629 if (!type_check (number, 0, BT_INTEGER))
6630 return false;
6632 if (!int_or_proc_check (handler, 1))
6633 return false;
6634 if (handler->ts.type == BT_INTEGER && !scalar_check (handler, 1))
6635 return false;
6637 return true;
6641 bool
6642 gfc_check_signal_sub (gfc_expr *number, gfc_expr *handler, gfc_expr *status)
6644 if (!scalar_check (number, 0))
6645 return false;
6646 if (!type_check (number, 0, BT_INTEGER))
6647 return false;
6649 if (!int_or_proc_check (handler, 1))
6650 return false;
6651 if (handler->ts.type == BT_INTEGER && !scalar_check (handler, 1))
6652 return false;
6654 if (status == NULL)
6655 return true;
6657 if (!type_check (status, 2, BT_INTEGER))
6658 return false;
6659 if (!scalar_check (status, 2))
6660 return false;
6662 return true;
6666 bool
6667 gfc_check_system_sub (gfc_expr *cmd, gfc_expr *status)
6669 if (!type_check (cmd, 0, BT_CHARACTER))
6670 return false;
6671 if (!kind_value_check (cmd, 0, gfc_default_character_kind))
6672 return false;
6674 if (!scalar_check (status, 1))
6675 return false;
6677 if (!type_check (status, 1, BT_INTEGER))
6678 return false;
6680 if (!kind_value_check (status, 1, gfc_default_integer_kind))
6681 return false;
6683 return true;
6687 /* This is used for the GNU intrinsics AND, OR and XOR. */
6688 bool
6689 gfc_check_and (gfc_expr *i, gfc_expr *j)
6691 if (i->ts.type != BT_INTEGER && i->ts.type != BT_LOGICAL)
6693 gfc_error ("%qs argument of %qs intrinsic at %L must be INTEGER "
6694 "or LOGICAL", gfc_current_intrinsic_arg[0]->name,
6695 gfc_current_intrinsic, &i->where);
6696 return false;
6699 if (j->ts.type != BT_INTEGER && j->ts.type != BT_LOGICAL)
6701 gfc_error ("%qs argument of %qs intrinsic at %L must be INTEGER "
6702 "or LOGICAL", gfc_current_intrinsic_arg[1]->name,
6703 gfc_current_intrinsic, &j->where);
6704 return false;
6707 if (i->ts.type != j->ts.type)
6709 gfc_error ("%qs and %qs arguments of %qs intrinsic at %L must "
6710 "have the same type", gfc_current_intrinsic_arg[0]->name,
6711 gfc_current_intrinsic_arg[1]->name, gfc_current_intrinsic,
6712 &j->where);
6713 return false;
6716 if (!scalar_check (i, 0))
6717 return false;
6719 if (!scalar_check (j, 1))
6720 return false;
6722 return true;
6726 bool
6727 gfc_check_storage_size (gfc_expr *a, gfc_expr *kind)
6730 if (a->expr_type == EXPR_NULL)
6732 gfc_error ("Intrinsic function NULL at %L cannot be an actual "
6733 "argument to STORAGE_SIZE, because it returns a "
6734 "disassociated pointer", &a->where);
6735 return false;
6738 if (a->ts.type == BT_ASSUMED)
6740 gfc_error ("%qs argument of %qs intrinsic at %L shall not be TYPE(*)",
6741 gfc_current_intrinsic_arg[0]->name, gfc_current_intrinsic,
6742 &a->where);
6743 return false;
6746 if (a->ts.type == BT_PROCEDURE)
6748 gfc_error ("%qs argument of %qs intrinsic at %L shall not be a "
6749 "procedure", gfc_current_intrinsic_arg[0]->name,
6750 gfc_current_intrinsic, &a->where);
6751 return false;
6754 if (kind == NULL)
6755 return true;
6757 if (!type_check (kind, 1, BT_INTEGER))
6758 return false;
6760 if (!scalar_check (kind, 1))
6761 return false;
6763 if (kind->expr_type != EXPR_CONSTANT)
6765 gfc_error ("%qs argument of %qs intrinsic at %L must be a constant",
6766 gfc_current_intrinsic_arg[1]->name, gfc_current_intrinsic,
6767 &kind->where);
6768 return false;
6771 return true;