PR rtl-optimization/82913
[official-gcc.git] / gcc / fortran / check.c
bloba147449bf707c768369e7dc43bd0aae35f9d2e1d
1 /* Check functions
2 Copyright (C) 2002-2017 Free Software Foundation, Inc.
3 Contributed by Andy Vaught & Katherine Holcomb
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
22 /* These functions check to see if an argument list is compatible with
23 a particular intrinsic function or subroutine. Presence of
24 required arguments has already been established, the argument list
25 has been sorted into the right order and has NULL arguments in the
26 correct places for missing optional arguments. */
28 #include "config.h"
29 #include "system.h"
30 #include "coretypes.h"
31 #include "options.h"
32 #include "gfortran.h"
33 #include "intrinsic.h"
34 #include "constructor.h"
35 #include "target-memory.h"
38 /* Make sure an expression is a scalar. */
40 static bool
41 scalar_check (gfc_expr *e, int n)
43 if (e->rank == 0)
44 return true;
46 gfc_error ("%qs argument of %qs intrinsic at %L must be a scalar",
47 gfc_current_intrinsic_arg[n]->name, gfc_current_intrinsic,
48 &e->where);
50 return false;
54 /* Check the type of an expression. */
56 static bool
57 type_check (gfc_expr *e, int n, bt type)
59 if (e->ts.type == type)
60 return true;
62 gfc_error ("%qs argument of %qs intrinsic at %L must be %s",
63 gfc_current_intrinsic_arg[n]->name, gfc_current_intrinsic,
64 &e->where, gfc_basic_typename (type));
66 return false;
70 /* Check that the expression is a numeric type. */
72 static bool
73 numeric_check (gfc_expr *e, int n)
75 /* Users sometime use a subroutine designator as an actual argument to
76 an intrinsic subprogram that expects an argument with a numeric type. */
77 if (e->symtree && e->symtree->n.sym->attr.subroutine)
78 goto error;
80 if (gfc_numeric_ts (&e->ts))
81 return true;
83 /* If the expression has not got a type, check if its namespace can
84 offer a default type. */
85 if ((e->expr_type == EXPR_VARIABLE || e->expr_type == EXPR_FUNCTION)
86 && e->symtree->n.sym->ts.type == BT_UNKNOWN
87 && gfc_set_default_type (e->symtree->n.sym, 0, e->symtree->n.sym->ns)
88 && gfc_numeric_ts (&e->symtree->n.sym->ts))
90 e->ts = e->symtree->n.sym->ts;
91 return true;
94 error:
96 gfc_error ("%qs argument of %qs intrinsic at %L must have a numeric type",
97 gfc_current_intrinsic_arg[n]->name, gfc_current_intrinsic,
98 &e->where);
100 return false;
104 /* Check that an expression is integer or real. */
106 static bool
107 int_or_real_check (gfc_expr *e, int n)
109 if (e->ts.type != BT_INTEGER && e->ts.type != BT_REAL)
111 gfc_error ("%qs argument of %qs intrinsic at %L must be INTEGER "
112 "or REAL", gfc_current_intrinsic_arg[n]->name,
113 gfc_current_intrinsic, &e->where);
114 return false;
117 return true;
121 /* Check that an expression is real or complex. */
123 static bool
124 real_or_complex_check (gfc_expr *e, int n)
126 if (e->ts.type != BT_REAL && e->ts.type != BT_COMPLEX)
128 gfc_error ("%qs argument of %qs intrinsic at %L must be REAL "
129 "or COMPLEX", gfc_current_intrinsic_arg[n]->name,
130 gfc_current_intrinsic, &e->where);
131 return false;
134 return true;
138 /* Check that an expression is INTEGER or PROCEDURE. */
140 static bool
141 int_or_proc_check (gfc_expr *e, int n)
143 if (e->ts.type != BT_INTEGER && e->ts.type != BT_PROCEDURE)
145 gfc_error ("%qs argument of %qs intrinsic at %L must be INTEGER "
146 "or PROCEDURE", gfc_current_intrinsic_arg[n]->name,
147 gfc_current_intrinsic, &e->where);
148 return false;
151 return true;
155 /* Check that the expression is an optional constant integer
156 and that it specifies a valid kind for that type. */
158 static bool
159 kind_check (gfc_expr *k, int n, bt type)
161 int kind;
163 if (k == NULL)
164 return true;
166 if (!type_check (k, n, BT_INTEGER))
167 return false;
169 if (!scalar_check (k, n))
170 return false;
172 if (!gfc_check_init_expr (k))
174 gfc_error ("%qs argument of %qs intrinsic at %L must be a constant",
175 gfc_current_intrinsic_arg[n]->name, gfc_current_intrinsic,
176 &k->where);
177 return false;
180 if (gfc_extract_int (k, &kind)
181 || gfc_validate_kind (type, kind, true) < 0)
183 gfc_error ("Invalid kind for %s at %L", gfc_basic_typename (type),
184 &k->where);
185 return false;
188 return true;
192 /* Make sure the expression is a double precision real. */
194 static bool
195 double_check (gfc_expr *d, int n)
197 if (!type_check (d, n, BT_REAL))
198 return false;
200 if (d->ts.kind != gfc_default_double_kind)
202 gfc_error ("%qs argument of %qs intrinsic at %L must be double "
203 "precision", gfc_current_intrinsic_arg[n]->name,
204 gfc_current_intrinsic, &d->where);
205 return false;
208 return true;
212 static bool
213 coarray_check (gfc_expr *e, int n)
215 if (e->ts.type == BT_CLASS && gfc_expr_attr (e).class_ok
216 && CLASS_DATA (e)->attr.codimension
217 && CLASS_DATA (e)->as->corank)
219 gfc_add_class_array_ref (e);
220 return true;
223 if (!gfc_is_coarray (e))
225 gfc_error ("Expected coarray variable as %qs argument to the %s "
226 "intrinsic at %L", gfc_current_intrinsic_arg[n]->name,
227 gfc_current_intrinsic, &e->where);
228 return false;
231 return true;
235 /* Make sure the expression is a logical array. */
237 static bool
238 logical_array_check (gfc_expr *array, int n)
240 if (array->ts.type != BT_LOGICAL || array->rank == 0)
242 gfc_error ("%qs argument of %qs intrinsic at %L must be a logical "
243 "array", gfc_current_intrinsic_arg[n]->name,
244 gfc_current_intrinsic, &array->where);
245 return false;
248 return true;
252 /* Make sure an expression is an array. */
254 static bool
255 array_check (gfc_expr *e, int n)
257 if (e->ts.type == BT_CLASS && gfc_expr_attr (e).class_ok
258 && CLASS_DATA (e)->attr.dimension
259 && CLASS_DATA (e)->as->rank)
261 gfc_add_class_array_ref (e);
262 return true;
265 if (e->rank != 0 && e->ts.type != BT_PROCEDURE)
266 return true;
268 gfc_error ("%qs argument of %qs intrinsic at %L must be an array",
269 gfc_current_intrinsic_arg[n]->name, gfc_current_intrinsic,
270 &e->where);
272 return false;
276 /* If expr is a constant, then check to ensure that it is greater than
277 of equal to zero. */
279 static bool
280 nonnegative_check (const char *arg, gfc_expr *expr)
282 int i;
284 if (expr->expr_type == EXPR_CONSTANT)
286 gfc_extract_int (expr, &i);
287 if (i < 0)
289 gfc_error ("%qs at %L must be nonnegative", arg, &expr->where);
290 return false;
294 return true;
298 /* If expr is a constant, then check to ensure that it is greater than zero. */
300 static bool
301 positive_check (int n, gfc_expr *expr)
303 int i;
305 if (expr->expr_type == EXPR_CONSTANT)
307 gfc_extract_int (expr, &i);
308 if (i <= 0)
310 gfc_error ("%qs argument of %qs intrinsic at %L must be positive",
311 gfc_current_intrinsic_arg[n]->name, gfc_current_intrinsic,
312 &expr->where);
313 return false;
317 return true;
321 /* If expr2 is constant, then check that the value is less than
322 (less than or equal to, if 'or_equal' is true) bit_size(expr1). */
324 static bool
325 less_than_bitsize1 (const char *arg1, gfc_expr *expr1, const char *arg2,
326 gfc_expr *expr2, bool or_equal)
328 int i2, i3;
330 if (expr2->expr_type == EXPR_CONSTANT)
332 gfc_extract_int (expr2, &i2);
333 i3 = gfc_validate_kind (BT_INTEGER, expr1->ts.kind, false);
335 /* For ISHFT[C], check that |shift| <= bit_size(i). */
336 if (arg2 == NULL)
338 if (i2 < 0)
339 i2 = -i2;
341 if (i2 > gfc_integer_kinds[i3].bit_size)
343 gfc_error ("The absolute value of SHIFT at %L must be less "
344 "than or equal to BIT_SIZE(%qs)",
345 &expr2->where, arg1);
346 return false;
350 if (or_equal)
352 if (i2 > gfc_integer_kinds[i3].bit_size)
354 gfc_error ("%qs at %L must be less than "
355 "or equal to BIT_SIZE(%qs)",
356 arg2, &expr2->where, arg1);
357 return false;
360 else
362 if (i2 >= gfc_integer_kinds[i3].bit_size)
364 gfc_error ("%qs at %L must be less than BIT_SIZE(%qs)",
365 arg2, &expr2->where, arg1);
366 return false;
371 return true;
375 /* If expr is constant, then check that the value is less than or equal
376 to the bit_size of the kind k. */
378 static bool
379 less_than_bitsizekind (const char *arg, gfc_expr *expr, int k)
381 int i, val;
383 if (expr->expr_type != EXPR_CONSTANT)
384 return true;
386 i = gfc_validate_kind (BT_INTEGER, k, false);
387 gfc_extract_int (expr, &val);
389 if (val > gfc_integer_kinds[i].bit_size)
391 gfc_error ("%qs at %L must be less than or equal to the BIT_SIZE of "
392 "INTEGER(KIND=%d)", arg, &expr->where, k);
393 return false;
396 return true;
400 /* If expr2 and expr3 are constants, then check that the value is less than
401 or equal to bit_size(expr1). */
403 static bool
404 less_than_bitsize2 (const char *arg1, gfc_expr *expr1, const char *arg2,
405 gfc_expr *expr2, const char *arg3, gfc_expr *expr3)
407 int i2, i3;
409 if (expr2->expr_type == EXPR_CONSTANT && expr3->expr_type == EXPR_CONSTANT)
411 gfc_extract_int (expr2, &i2);
412 gfc_extract_int (expr3, &i3);
413 i2 += i3;
414 i3 = gfc_validate_kind (BT_INTEGER, expr1->ts.kind, false);
415 if (i2 > gfc_integer_kinds[i3].bit_size)
417 gfc_error ("%<%s + %s%> at %L must be less than or equal "
418 "to BIT_SIZE(%qs)",
419 arg2, arg3, &expr2->where, arg1);
420 return false;
424 return true;
427 /* Make sure two expressions have the same type. */
429 static bool
430 same_type_check (gfc_expr *e, int n, gfc_expr *f, int m, bool assoc = false)
432 gfc_typespec *ets = &e->ts;
433 gfc_typespec *fts = &f->ts;
435 if (assoc)
437 /* Procedure pointer component expressions have the type of the interface
438 procedure. If they are being tested for association with a procedure
439 pointer (ie. not a component), the type of the procedure must be
440 determined. */
441 if (e->ts.type == BT_PROCEDURE && e->symtree->n.sym)
442 ets = &e->symtree->n.sym->ts;
443 if (f->ts.type == BT_PROCEDURE && f->symtree->n.sym)
444 fts = &f->symtree->n.sym->ts;
447 if (gfc_compare_types (ets, fts))
448 return true;
450 gfc_error ("%qs argument of %qs intrinsic at %L must be the same type "
451 "and kind as %qs", gfc_current_intrinsic_arg[m]->name,
452 gfc_current_intrinsic, &f->where,
453 gfc_current_intrinsic_arg[n]->name);
455 return false;
459 /* Make sure that an expression has a certain (nonzero) rank. */
461 static bool
462 rank_check (gfc_expr *e, int n, int rank)
464 if (e->rank == rank)
465 return true;
467 gfc_error ("%qs argument of %qs intrinsic at %L must be of rank %d",
468 gfc_current_intrinsic_arg[n]->name, gfc_current_intrinsic,
469 &e->where, rank);
471 return false;
475 /* Make sure a variable expression is not an optional dummy argument. */
477 static bool
478 nonoptional_check (gfc_expr *e, int n)
480 if (e->expr_type == EXPR_VARIABLE && e->symtree->n.sym->attr.optional)
482 gfc_error ("%qs argument of %qs intrinsic at %L must not be OPTIONAL",
483 gfc_current_intrinsic_arg[n]->name, gfc_current_intrinsic,
484 &e->where);
487 /* TODO: Recursive check on nonoptional variables? */
489 return true;
493 /* Check for ALLOCATABLE attribute. */
495 static bool
496 allocatable_check (gfc_expr *e, int n)
498 symbol_attribute attr;
500 attr = gfc_variable_attr (e, NULL);
501 if (!attr.allocatable || attr.associate_var)
503 gfc_error ("%qs argument of %qs intrinsic at %L must be ALLOCATABLE",
504 gfc_current_intrinsic_arg[n]->name, gfc_current_intrinsic,
505 &e->where);
506 return false;
509 return true;
513 /* Check that an expression has a particular kind. */
515 static bool
516 kind_value_check (gfc_expr *e, int n, int k)
518 if (e->ts.kind == k)
519 return true;
521 gfc_error ("%qs argument of %qs intrinsic at %L must be of kind %d",
522 gfc_current_intrinsic_arg[n]->name, gfc_current_intrinsic,
523 &e->where, k);
525 return false;
529 /* Make sure an expression is a variable. */
531 static bool
532 variable_check (gfc_expr *e, int n, bool allow_proc)
534 if (e->expr_type == EXPR_VARIABLE
535 && e->symtree->n.sym->attr.intent == INTENT_IN
536 && (gfc_current_intrinsic_arg[n]->intent == INTENT_OUT
537 || gfc_current_intrinsic_arg[n]->intent == INTENT_INOUT))
539 gfc_ref *ref;
540 bool pointer = e->symtree->n.sym->ts.type == BT_CLASS
541 && CLASS_DATA (e->symtree->n.sym)
542 ? CLASS_DATA (e->symtree->n.sym)->attr.class_pointer
543 : e->symtree->n.sym->attr.pointer;
545 for (ref = e->ref; ref; ref = ref->next)
547 if (pointer && ref->type == REF_COMPONENT)
548 break;
549 if (ref->type == REF_COMPONENT
550 && ((ref->u.c.component->ts.type == BT_CLASS
551 && CLASS_DATA (ref->u.c.component)->attr.class_pointer)
552 || (ref->u.c.component->ts.type != BT_CLASS
553 && ref->u.c.component->attr.pointer)))
554 break;
557 if (!ref)
559 gfc_error ("%qs argument of %qs intrinsic at %L cannot be "
560 "INTENT(IN)", gfc_current_intrinsic_arg[n]->name,
561 gfc_current_intrinsic, &e->where);
562 return false;
566 if (e->expr_type == EXPR_VARIABLE
567 && e->symtree->n.sym->attr.flavor != FL_PARAMETER
568 && (allow_proc || !e->symtree->n.sym->attr.function))
569 return true;
571 if (e->expr_type == EXPR_VARIABLE && e->symtree->n.sym->attr.function
572 && e->symtree->n.sym == e->symtree->n.sym->result)
574 gfc_namespace *ns;
575 for (ns = gfc_current_ns; ns; ns = ns->parent)
576 if (ns->proc_name == e->symtree->n.sym)
577 return true;
580 gfc_error ("%qs argument of %qs intrinsic at %L must be a variable",
581 gfc_current_intrinsic_arg[n]->name, gfc_current_intrinsic, &e->where);
583 return false;
587 /* Check the common DIM parameter for correctness. */
589 static bool
590 dim_check (gfc_expr *dim, int n, bool optional)
592 if (dim == NULL)
593 return true;
595 if (!type_check (dim, n, BT_INTEGER))
596 return false;
598 if (!scalar_check (dim, n))
599 return false;
601 if (!optional && !nonoptional_check (dim, n))
602 return false;
604 return true;
608 /* If a coarray DIM parameter is a constant, make sure that it is greater than
609 zero and less than or equal to the corank of the given array. */
611 static bool
612 dim_corank_check (gfc_expr *dim, gfc_expr *array)
614 int corank;
616 gcc_assert (array->expr_type == EXPR_VARIABLE);
618 if (dim->expr_type != EXPR_CONSTANT)
619 return true;
621 if (array->ts.type == BT_CLASS)
622 return true;
624 corank = gfc_get_corank (array);
626 if (mpz_cmp_ui (dim->value.integer, 1) < 0
627 || mpz_cmp_ui (dim->value.integer, corank) > 0)
629 gfc_error ("%<dim%> argument of %qs intrinsic at %L is not a valid "
630 "codimension index", gfc_current_intrinsic, &dim->where);
632 return false;
635 return true;
639 /* If a DIM parameter is a constant, make sure that it is greater than
640 zero and less than or equal to the rank of the given array. If
641 allow_assumed is zero then dim must be less than the rank of the array
642 for assumed size arrays. */
644 static bool
645 dim_rank_check (gfc_expr *dim, gfc_expr *array, int allow_assumed)
647 gfc_array_ref *ar;
648 int rank;
650 if (dim == NULL)
651 return true;
653 if (dim->expr_type != EXPR_CONSTANT)
654 return true;
656 if (array->expr_type == EXPR_FUNCTION && array->value.function.isym
657 && array->value.function.isym->id == GFC_ISYM_SPREAD)
658 rank = array->rank + 1;
659 else
660 rank = array->rank;
662 /* Assumed-rank array. */
663 if (rank == -1)
664 rank = GFC_MAX_DIMENSIONS;
666 if (array->expr_type == EXPR_VARIABLE)
668 ar = gfc_find_array_ref (array);
669 if (ar->as->type == AS_ASSUMED_SIZE
670 && !allow_assumed
671 && ar->type != AR_ELEMENT
672 && ar->type != AR_SECTION)
673 rank--;
676 if (mpz_cmp_ui (dim->value.integer, 1) < 0
677 || mpz_cmp_ui (dim->value.integer, rank) > 0)
679 gfc_error ("%<dim%> argument of %qs intrinsic at %L is not a valid "
680 "dimension index", gfc_current_intrinsic, &dim->where);
682 return false;
685 return true;
689 /* Compare the size of a along dimension ai with the size of b along
690 dimension bi, returning 0 if they are known not to be identical,
691 and 1 if they are identical, or if this cannot be determined. */
693 static int
694 identical_dimen_shape (gfc_expr *a, int ai, gfc_expr *b, int bi)
696 mpz_t a_size, b_size;
697 int ret;
699 gcc_assert (a->rank > ai);
700 gcc_assert (b->rank > bi);
702 ret = 1;
704 if (gfc_array_dimen_size (a, ai, &a_size))
706 if (gfc_array_dimen_size (b, bi, &b_size))
708 if (mpz_cmp (a_size, b_size) != 0)
709 ret = 0;
711 mpz_clear (b_size);
713 mpz_clear (a_size);
715 return ret;
718 /* Calculate the length of a character variable, including substrings.
719 Strip away parentheses if necessary. Return -1 if no length could
720 be determined. */
722 static long
723 gfc_var_strlen (const gfc_expr *a)
725 gfc_ref *ra;
727 while (a->expr_type == EXPR_OP && a->value.op.op == INTRINSIC_PARENTHESES)
728 a = a->value.op.op1;
730 for (ra = a->ref; ra != NULL && ra->type != REF_SUBSTRING; ra = ra->next)
733 if (ra)
735 long start_a, end_a;
737 if (!ra->u.ss.end)
738 return -1;
740 if ((!ra->u.ss.start || ra->u.ss.start->expr_type == EXPR_CONSTANT)
741 && ra->u.ss.end->expr_type == EXPR_CONSTANT)
743 start_a = ra->u.ss.start ? mpz_get_si (ra->u.ss.start->value.integer)
744 : 1;
745 end_a = mpz_get_si (ra->u.ss.end->value.integer);
746 return (end_a < start_a) ? 0 : end_a - start_a + 1;
748 else if (ra->u.ss.start
749 && gfc_dep_compare_expr (ra->u.ss.start, ra->u.ss.end) == 0)
750 return 1;
751 else
752 return -1;
755 if (a->ts.u.cl && a->ts.u.cl->length
756 && a->ts.u.cl->length->expr_type == EXPR_CONSTANT)
757 return mpz_get_si (a->ts.u.cl->length->value.integer);
758 else if (a->expr_type == EXPR_CONSTANT
759 && (a->ts.u.cl == NULL || a->ts.u.cl->length == NULL))
760 return a->value.character.length;
761 else
762 return -1;
766 /* Check whether two character expressions have the same length;
767 returns true if they have or if the length cannot be determined,
768 otherwise return false and raise a gfc_error. */
770 bool
771 gfc_check_same_strlen (const gfc_expr *a, const gfc_expr *b, const char *name)
773 long len_a, len_b;
775 len_a = gfc_var_strlen(a);
776 len_b = gfc_var_strlen(b);
778 if (len_a == -1 || len_b == -1 || len_a == len_b)
779 return true;
780 else
782 gfc_error ("Unequal character lengths (%ld/%ld) in %s at %L",
783 len_a, len_b, name, &a->where);
784 return false;
789 /***** Check functions *****/
791 /* Check subroutine suitable for intrinsics taking a real argument and
792 a kind argument for the result. */
794 static bool
795 check_a_kind (gfc_expr *a, gfc_expr *kind, bt type)
797 if (!type_check (a, 0, BT_REAL))
798 return false;
799 if (!kind_check (kind, 1, type))
800 return false;
802 return true;
806 /* Check subroutine suitable for ceiling, floor and nint. */
808 bool
809 gfc_check_a_ikind (gfc_expr *a, gfc_expr *kind)
811 return check_a_kind (a, kind, BT_INTEGER);
815 /* Check subroutine suitable for aint, anint. */
817 bool
818 gfc_check_a_xkind (gfc_expr *a, gfc_expr *kind)
820 return check_a_kind (a, kind, BT_REAL);
824 bool
825 gfc_check_abs (gfc_expr *a)
827 if (!numeric_check (a, 0))
828 return false;
830 return true;
834 bool
835 gfc_check_achar (gfc_expr *a, gfc_expr *kind)
837 if (!type_check (a, 0, BT_INTEGER))
838 return false;
839 if (!kind_check (kind, 1, BT_CHARACTER))
840 return false;
842 return true;
846 bool
847 gfc_check_access_func (gfc_expr *name, gfc_expr *mode)
849 if (!type_check (name, 0, BT_CHARACTER)
850 || !scalar_check (name, 0))
851 return false;
852 if (!kind_value_check (name, 0, gfc_default_character_kind))
853 return false;
855 if (!type_check (mode, 1, BT_CHARACTER)
856 || !scalar_check (mode, 1))
857 return false;
858 if (!kind_value_check (mode, 1, gfc_default_character_kind))
859 return false;
861 return true;
865 bool
866 gfc_check_all_any (gfc_expr *mask, gfc_expr *dim)
868 if (!logical_array_check (mask, 0))
869 return false;
871 if (!dim_check (dim, 1, false))
872 return false;
874 if (!dim_rank_check (dim, mask, 0))
875 return false;
877 return true;
881 bool
882 gfc_check_allocated (gfc_expr *array)
884 /* Tests on allocated components of coarrays need to detour the check to
885 argument of the _caf_get. */
886 if (flag_coarray == GFC_FCOARRAY_LIB && array->expr_type == EXPR_FUNCTION
887 && array->value.function.isym
888 && array->value.function.isym->id == GFC_ISYM_CAF_GET)
890 array = array->value.function.actual->expr;
891 if (!array->ref)
892 return false;
895 if (!variable_check (array, 0, false))
896 return false;
897 if (!allocatable_check (array, 0))
898 return false;
900 return true;
904 /* Common check function where the first argument must be real or
905 integer and the second argument must be the same as the first. */
907 bool
908 gfc_check_a_p (gfc_expr *a, gfc_expr *p)
910 if (!int_or_real_check (a, 0))
911 return false;
913 if (a->ts.type != p->ts.type)
915 gfc_error ("%qs and %qs arguments of %qs intrinsic at %L must "
916 "have the same type", gfc_current_intrinsic_arg[0]->name,
917 gfc_current_intrinsic_arg[1]->name, gfc_current_intrinsic,
918 &p->where);
919 return false;
922 if (a->ts.kind != p->ts.kind)
924 if (!gfc_notify_std (GFC_STD_GNU, "Different type kinds at %L",
925 &p->where))
926 return false;
929 return true;
933 bool
934 gfc_check_x_yd (gfc_expr *x, gfc_expr *y)
936 if (!double_check (x, 0) || !double_check (y, 1))
937 return false;
939 return true;
943 bool
944 gfc_check_associated (gfc_expr *pointer, gfc_expr *target)
946 symbol_attribute attr1, attr2;
947 int i;
948 bool t;
949 locus *where;
951 where = &pointer->where;
953 if (pointer->expr_type == EXPR_NULL)
954 goto null_arg;
956 attr1 = gfc_expr_attr (pointer);
958 if (!attr1.pointer && !attr1.proc_pointer)
960 gfc_error ("%qs argument of %qs intrinsic at %L must be a POINTER",
961 gfc_current_intrinsic_arg[0]->name, gfc_current_intrinsic,
962 &pointer->where);
963 return false;
966 /* F2008, C1242. */
967 if (attr1.pointer && gfc_is_coindexed (pointer))
969 gfc_error ("%qs argument of %qs intrinsic at %L shall not be "
970 "coindexed", gfc_current_intrinsic_arg[0]->name,
971 gfc_current_intrinsic, &pointer->where);
972 return false;
975 /* Target argument is optional. */
976 if (target == NULL)
977 return true;
979 where = &target->where;
980 if (target->expr_type == EXPR_NULL)
981 goto null_arg;
983 if (target->expr_type == EXPR_VARIABLE || target->expr_type == EXPR_FUNCTION)
984 attr2 = gfc_expr_attr (target);
985 else
987 gfc_error ("%qs argument of %qs intrinsic at %L must be a pointer "
988 "or target VARIABLE or FUNCTION",
989 gfc_current_intrinsic_arg[1]->name, gfc_current_intrinsic,
990 &target->where);
991 return false;
994 if (attr1.pointer && !attr2.pointer && !attr2.target)
996 gfc_error ("%qs argument of %qs intrinsic at %L must be a POINTER "
997 "or a TARGET", gfc_current_intrinsic_arg[1]->name,
998 gfc_current_intrinsic, &target->where);
999 return false;
1002 /* F2008, C1242. */
1003 if (attr1.pointer && gfc_is_coindexed (target))
1005 gfc_error ("%qs argument of %qs intrinsic at %L shall not be "
1006 "coindexed", gfc_current_intrinsic_arg[1]->name,
1007 gfc_current_intrinsic, &target->where);
1008 return false;
1011 t = true;
1012 if (!same_type_check (pointer, 0, target, 1, true))
1013 t = false;
1014 if (!rank_check (target, 0, pointer->rank))
1015 t = false;
1016 if (target->rank > 0)
1018 for (i = 0; i < target->rank; i++)
1019 if (target->ref->u.ar.dimen_type[i] == DIMEN_VECTOR)
1021 gfc_error ("Array section with a vector subscript at %L shall not "
1022 "be the target of a pointer",
1023 &target->where);
1024 t = false;
1025 break;
1028 return t;
1030 null_arg:
1032 gfc_error ("NULL pointer at %L is not permitted as actual argument "
1033 "of %qs intrinsic function", where, gfc_current_intrinsic);
1034 return false;
1039 bool
1040 gfc_check_atan_2 (gfc_expr *y, gfc_expr *x)
1042 /* gfc_notify_std would be a waste of time as the return value
1043 is seemingly used only for the generic resolution. The error
1044 will be: Too many arguments. */
1045 if ((gfc_option.allow_std & GFC_STD_F2008) == 0)
1046 return false;
1048 return gfc_check_atan2 (y, x);
1052 bool
1053 gfc_check_atan2 (gfc_expr *y, gfc_expr *x)
1055 if (!type_check (y, 0, BT_REAL))
1056 return false;
1057 if (!same_type_check (y, 0, x, 1))
1058 return false;
1060 return true;
1064 static bool
1065 gfc_check_atomic (gfc_expr *atom, int atom_no, gfc_expr *value, int val_no,
1066 gfc_expr *stat, int stat_no)
1068 if (!scalar_check (atom, atom_no) || !scalar_check (value, val_no))
1069 return false;
1071 if (!(atom->ts.type == BT_INTEGER && atom->ts.kind == gfc_atomic_int_kind)
1072 && !(atom->ts.type == BT_LOGICAL
1073 && atom->ts.kind == gfc_atomic_logical_kind))
1075 gfc_error ("ATOM argument at %L to intrinsic function %s shall be an "
1076 "integer of ATOMIC_INT_KIND or a logical of "
1077 "ATOMIC_LOGICAL_KIND", &atom->where, gfc_current_intrinsic);
1078 return false;
1081 if (!gfc_is_coarray (atom) && !gfc_is_coindexed (atom))
1083 gfc_error ("ATOM argument at %L of the %s intrinsic function shall be a "
1084 "coarray or coindexed", &atom->where, gfc_current_intrinsic);
1085 return false;
1088 if (atom->ts.type != value->ts.type)
1090 gfc_error ("%qs argument of %qs intrinsic at %L shall have the same "
1091 "type as %qs at %L", gfc_current_intrinsic_arg[val_no]->name,
1092 gfc_current_intrinsic, &value->where,
1093 gfc_current_intrinsic_arg[atom_no]->name, &atom->where);
1094 return false;
1097 if (stat != NULL)
1099 if (!type_check (stat, stat_no, BT_INTEGER))
1100 return false;
1101 if (!scalar_check (stat, stat_no))
1102 return false;
1103 if (!variable_check (stat, stat_no, false))
1104 return false;
1105 if (!kind_value_check (stat, stat_no, gfc_default_integer_kind))
1106 return false;
1108 if (!gfc_notify_std (GFC_STD_F2008_TS, "STAT= argument to %s at %L",
1109 gfc_current_intrinsic, &stat->where))
1110 return false;
1113 return true;
1117 bool
1118 gfc_check_atomic_def (gfc_expr *atom, gfc_expr *value, gfc_expr *stat)
1120 if (atom->expr_type == EXPR_FUNCTION
1121 && atom->value.function.isym
1122 && atom->value.function.isym->id == GFC_ISYM_CAF_GET)
1123 atom = atom->value.function.actual->expr;
1125 if (!gfc_check_vardef_context (atom, false, false, false, NULL))
1127 gfc_error ("ATOM argument of the %s intrinsic function at %L shall be "
1128 "definable", gfc_current_intrinsic, &atom->where);
1129 return false;
1132 return gfc_check_atomic (atom, 0, value, 1, stat, 2);
1136 bool
1137 gfc_check_atomic_op (gfc_expr *atom, gfc_expr *value, gfc_expr *stat)
1139 if (atom->ts.type != BT_INTEGER || atom->ts.kind != gfc_atomic_int_kind)
1141 gfc_error ("ATOM argument at %L to intrinsic function %s shall be an "
1142 "integer of ATOMIC_INT_KIND", &atom->where,
1143 gfc_current_intrinsic);
1144 return false;
1147 return gfc_check_atomic_def (atom, value, stat);
1151 bool
1152 gfc_check_atomic_ref (gfc_expr *value, gfc_expr *atom, gfc_expr *stat)
1154 if (atom->expr_type == EXPR_FUNCTION
1155 && atom->value.function.isym
1156 && atom->value.function.isym->id == GFC_ISYM_CAF_GET)
1157 atom = atom->value.function.actual->expr;
1159 if (!gfc_check_vardef_context (value, false, false, false, NULL))
1161 gfc_error ("VALUE argument of the %s intrinsic function at %L shall be "
1162 "definable", gfc_current_intrinsic, &value->where);
1163 return false;
1166 return gfc_check_atomic (atom, 1, value, 0, stat, 2);
1170 bool
1171 gfc_check_image_status (gfc_expr *image, gfc_expr *team)
1173 /* IMAGE has to be a positive, scalar integer. */
1174 if (!type_check (image, 0, BT_INTEGER) || !scalar_check (image, 0)
1175 || !positive_check (0, image))
1176 return false;
1178 if (team)
1180 gfc_error ("%qs argument of %qs intrinsic at %L not yet supported",
1181 gfc_current_intrinsic_arg[1]->name, gfc_current_intrinsic,
1182 &team->where);
1183 return false;
1185 return true;
1189 bool
1190 gfc_check_failed_or_stopped_images (gfc_expr *team, gfc_expr *kind)
1192 if (team)
1194 gfc_error ("%qs argument of %qs intrinsic at %L not yet supported",
1195 gfc_current_intrinsic_arg[0]->name, gfc_current_intrinsic,
1196 &team->where);
1197 return false;
1200 if (kind)
1202 int k;
1204 if (!type_check (kind, 1, BT_INTEGER) || !scalar_check (kind, 1)
1205 || !positive_check (1, kind))
1206 return false;
1208 /* Get the kind, reporting error on non-constant or overflow. */
1209 gfc_current_locus = kind->where;
1210 if (gfc_extract_int (kind, &k, 1))
1211 return false;
1212 if (gfc_validate_kind (BT_INTEGER, k, true) == -1)
1214 gfc_error ("%qs argument of %qs intrinsic at %L shall specify a "
1215 "valid integer kind", gfc_current_intrinsic_arg[1]->name,
1216 gfc_current_intrinsic, &kind->where);
1217 return false;
1220 return true;
1224 bool
1225 gfc_check_atomic_cas (gfc_expr *atom, gfc_expr *old, gfc_expr *compare,
1226 gfc_expr *new_val, gfc_expr *stat)
1228 if (atom->expr_type == EXPR_FUNCTION
1229 && atom->value.function.isym
1230 && atom->value.function.isym->id == GFC_ISYM_CAF_GET)
1231 atom = atom->value.function.actual->expr;
1233 if (!gfc_check_atomic (atom, 0, new_val, 3, stat, 4))
1234 return false;
1236 if (!scalar_check (old, 1) || !scalar_check (compare, 2))
1237 return false;
1239 if (!same_type_check (atom, 0, old, 1))
1240 return false;
1242 if (!same_type_check (atom, 0, compare, 2))
1243 return false;
1245 if (!gfc_check_vardef_context (atom, false, false, false, NULL))
1247 gfc_error ("ATOM argument of the %s intrinsic function at %L shall be "
1248 "definable", gfc_current_intrinsic, &atom->where);
1249 return false;
1252 if (!gfc_check_vardef_context (old, false, false, false, NULL))
1254 gfc_error ("OLD argument of the %s intrinsic function at %L shall be "
1255 "definable", gfc_current_intrinsic, &old->where);
1256 return false;
1259 return true;
1262 bool
1263 gfc_check_event_query (gfc_expr *event, gfc_expr *count, gfc_expr *stat)
1265 if (event->ts.type != BT_DERIVED
1266 || event->ts.u.derived->from_intmod != INTMOD_ISO_FORTRAN_ENV
1267 || event->ts.u.derived->intmod_sym_id != ISOFORTRAN_EVENT_TYPE)
1269 gfc_error ("EVENT argument at %L to the intrinsic EVENT_QUERY "
1270 "shall be of type EVENT_TYPE", &event->where);
1271 return false;
1274 if (!scalar_check (event, 0))
1275 return false;
1277 if (!gfc_check_vardef_context (count, false, false, false, NULL))
1279 gfc_error ("COUNT argument of the EVENT_QUERY intrinsic function at %L "
1280 "shall be definable", &count->where);
1281 return false;
1284 if (!type_check (count, 1, BT_INTEGER))
1285 return false;
1287 int i = gfc_validate_kind (BT_INTEGER, count->ts.kind, false);
1288 int j = gfc_validate_kind (BT_INTEGER, gfc_default_integer_kind, false);
1290 if (gfc_integer_kinds[i].range < gfc_integer_kinds[j].range)
1292 gfc_error ("COUNT argument of the EVENT_QUERY intrinsic function at %L "
1293 "shall have at least the range of the default integer",
1294 &count->where);
1295 return false;
1298 if (stat != NULL)
1300 if (!type_check (stat, 2, BT_INTEGER))
1301 return false;
1302 if (!scalar_check (stat, 2))
1303 return false;
1304 if (!variable_check (stat, 2, false))
1305 return false;
1307 if (!gfc_notify_std (GFC_STD_F2008_TS, "STAT= argument to %s at %L",
1308 gfc_current_intrinsic, &stat->where))
1309 return false;
1312 return true;
1316 bool
1317 gfc_check_atomic_fetch_op (gfc_expr *atom, gfc_expr *value, gfc_expr *old,
1318 gfc_expr *stat)
1320 if (atom->expr_type == EXPR_FUNCTION
1321 && atom->value.function.isym
1322 && atom->value.function.isym->id == GFC_ISYM_CAF_GET)
1323 atom = atom->value.function.actual->expr;
1325 if (atom->ts.type != BT_INTEGER || atom->ts.kind != gfc_atomic_int_kind)
1327 gfc_error ("ATOM argument at %L to intrinsic function %s shall be an "
1328 "integer of ATOMIC_INT_KIND", &atom->where,
1329 gfc_current_intrinsic);
1330 return false;
1333 if (!gfc_check_atomic (atom, 0, value, 1, stat, 3))
1334 return false;
1336 if (!scalar_check (old, 2))
1337 return false;
1339 if (!same_type_check (atom, 0, old, 2))
1340 return false;
1342 if (!gfc_check_vardef_context (atom, false, false, false, NULL))
1344 gfc_error ("ATOM argument of the %s intrinsic function at %L shall be "
1345 "definable", gfc_current_intrinsic, &atom->where);
1346 return false;
1349 if (!gfc_check_vardef_context (old, false, false, false, NULL))
1351 gfc_error ("OLD argument of the %s intrinsic function at %L shall be "
1352 "definable", gfc_current_intrinsic, &old->where);
1353 return false;
1356 return true;
1360 /* BESJN and BESYN functions. */
1362 bool
1363 gfc_check_besn (gfc_expr *n, gfc_expr *x)
1365 if (!type_check (n, 0, BT_INTEGER))
1366 return false;
1367 if (n->expr_type == EXPR_CONSTANT)
1369 int i;
1370 gfc_extract_int (n, &i);
1371 if (i < 0 && !gfc_notify_std (GFC_STD_GNU, "Negative argument "
1372 "N at %L", &n->where))
1373 return false;
1376 if (!type_check (x, 1, BT_REAL))
1377 return false;
1379 return true;
1383 /* Transformational version of the Bessel JN and YN functions. */
1385 bool
1386 gfc_check_bessel_n2 (gfc_expr *n1, gfc_expr *n2, gfc_expr *x)
1388 if (!type_check (n1, 0, BT_INTEGER))
1389 return false;
1390 if (!scalar_check (n1, 0))
1391 return false;
1392 if (!nonnegative_check ("N1", n1))
1393 return false;
1395 if (!type_check (n2, 1, BT_INTEGER))
1396 return false;
1397 if (!scalar_check (n2, 1))
1398 return false;
1399 if (!nonnegative_check ("N2", n2))
1400 return false;
1402 if (!type_check (x, 2, BT_REAL))
1403 return false;
1404 if (!scalar_check (x, 2))
1405 return false;
1407 return true;
1411 bool
1412 gfc_check_bge_bgt_ble_blt (gfc_expr *i, gfc_expr *j)
1414 if (!type_check (i, 0, BT_INTEGER))
1415 return false;
1417 if (!type_check (j, 1, BT_INTEGER))
1418 return false;
1420 return true;
1424 bool
1425 gfc_check_bitfcn (gfc_expr *i, gfc_expr *pos)
1427 if (!type_check (i, 0, BT_INTEGER))
1428 return false;
1430 if (!type_check (pos, 1, BT_INTEGER))
1431 return false;
1433 if (!nonnegative_check ("pos", pos))
1434 return false;
1436 if (!less_than_bitsize1 ("i", i, "pos", pos, false))
1437 return false;
1439 return true;
1443 bool
1444 gfc_check_char (gfc_expr *i, gfc_expr *kind)
1446 if (!type_check (i, 0, BT_INTEGER))
1447 return false;
1448 if (!kind_check (kind, 1, BT_CHARACTER))
1449 return false;
1451 return true;
1455 bool
1456 gfc_check_chdir (gfc_expr *dir)
1458 if (!type_check (dir, 0, BT_CHARACTER))
1459 return false;
1460 if (!kind_value_check (dir, 0, gfc_default_character_kind))
1461 return false;
1463 return true;
1467 bool
1468 gfc_check_chdir_sub (gfc_expr *dir, gfc_expr *status)
1470 if (!type_check (dir, 0, BT_CHARACTER))
1471 return false;
1472 if (!kind_value_check (dir, 0, gfc_default_character_kind))
1473 return false;
1475 if (status == NULL)
1476 return true;
1478 if (!type_check (status, 1, BT_INTEGER))
1479 return false;
1480 if (!scalar_check (status, 1))
1481 return false;
1483 return true;
1487 bool
1488 gfc_check_chmod (gfc_expr *name, gfc_expr *mode)
1490 if (!type_check (name, 0, BT_CHARACTER))
1491 return false;
1492 if (!kind_value_check (name, 0, gfc_default_character_kind))
1493 return false;
1495 if (!type_check (mode, 1, BT_CHARACTER))
1496 return false;
1497 if (!kind_value_check (mode, 1, gfc_default_character_kind))
1498 return false;
1500 return true;
1504 bool
1505 gfc_check_chmod_sub (gfc_expr *name, gfc_expr *mode, gfc_expr *status)
1507 if (!type_check (name, 0, BT_CHARACTER))
1508 return false;
1509 if (!kind_value_check (name, 0, gfc_default_character_kind))
1510 return false;
1512 if (!type_check (mode, 1, BT_CHARACTER))
1513 return false;
1514 if (!kind_value_check (mode, 1, gfc_default_character_kind))
1515 return false;
1517 if (status == NULL)
1518 return true;
1520 if (!type_check (status, 2, BT_INTEGER))
1521 return false;
1523 if (!scalar_check (status, 2))
1524 return false;
1526 return true;
1530 bool
1531 gfc_check_cmplx (gfc_expr *x, gfc_expr *y, gfc_expr *kind)
1533 if (!numeric_check (x, 0))
1534 return false;
1536 if (y != NULL)
1538 if (!numeric_check (y, 1))
1539 return false;
1541 if (x->ts.type == BT_COMPLEX)
1543 gfc_error ("%qs argument of %qs intrinsic at %L must not be "
1544 "present if %<x%> is COMPLEX",
1545 gfc_current_intrinsic_arg[1]->name, gfc_current_intrinsic,
1546 &y->where);
1547 return false;
1550 if (y->ts.type == BT_COMPLEX)
1552 gfc_error ("%qs argument of %qs intrinsic at %L must have a type "
1553 "of either REAL or INTEGER",
1554 gfc_current_intrinsic_arg[1]->name, gfc_current_intrinsic,
1555 &y->where);
1556 return false;
1561 if (!kind_check (kind, 2, BT_COMPLEX))
1562 return false;
1564 if (!kind && warn_conversion
1565 && x->ts.type == BT_REAL && x->ts.kind > gfc_default_real_kind)
1566 gfc_warning_now (OPT_Wconversion, "Conversion from %s to default-kind "
1567 "COMPLEX(%d) at %L might lose precision, consider using "
1568 "the KIND argument", gfc_typename (&x->ts),
1569 gfc_default_real_kind, &x->where);
1570 else if (y && !kind && warn_conversion
1571 && y->ts.type == BT_REAL && y->ts.kind > gfc_default_real_kind)
1572 gfc_warning_now (OPT_Wconversion, "Conversion from %s to default-kind "
1573 "COMPLEX(%d) at %L might lose precision, consider using "
1574 "the KIND argument", gfc_typename (&y->ts),
1575 gfc_default_real_kind, &y->where);
1576 return true;
1580 static bool
1581 check_co_collective (gfc_expr *a, gfc_expr *image_idx, gfc_expr *stat,
1582 gfc_expr *errmsg, bool co_reduce)
1584 if (!variable_check (a, 0, false))
1585 return false;
1587 if (!gfc_check_vardef_context (a, false, false, false, "argument 'A' with "
1588 "INTENT(INOUT)"))
1589 return false;
1591 /* Fortran 2008, 12.5.2.4, paragraph 18. */
1592 if (gfc_has_vector_subscript (a))
1594 gfc_error ("Argument %<A%> with INTENT(INOUT) at %L of the intrinsic "
1595 "subroutine %s shall not have a vector subscript",
1596 &a->where, gfc_current_intrinsic);
1597 return false;
1600 if (gfc_is_coindexed (a))
1602 gfc_error ("The A argument at %L to the intrinsic %s shall not be "
1603 "coindexed", &a->where, gfc_current_intrinsic);
1604 return false;
1607 if (image_idx != NULL)
1609 if (!type_check (image_idx, co_reduce ? 2 : 1, BT_INTEGER))
1610 return false;
1611 if (!scalar_check (image_idx, co_reduce ? 2 : 1))
1612 return false;
1615 if (stat != NULL)
1617 if (!type_check (stat, co_reduce ? 3 : 2, BT_INTEGER))
1618 return false;
1619 if (!scalar_check (stat, co_reduce ? 3 : 2))
1620 return false;
1621 if (!variable_check (stat, co_reduce ? 3 : 2, false))
1622 return false;
1623 if (stat->ts.kind != 4)
1625 gfc_error ("The stat= argument at %L must be a kind=4 integer "
1626 "variable", &stat->where);
1627 return false;
1631 if (errmsg != NULL)
1633 if (!type_check (errmsg, co_reduce ? 4 : 3, BT_CHARACTER))
1634 return false;
1635 if (!scalar_check (errmsg, co_reduce ? 4 : 3))
1636 return false;
1637 if (!variable_check (errmsg, co_reduce ? 4 : 3, false))
1638 return false;
1639 if (errmsg->ts.kind != 1)
1641 gfc_error ("The errmsg= argument at %L must be a default-kind "
1642 "character variable", &errmsg->where);
1643 return false;
1647 if (flag_coarray == GFC_FCOARRAY_NONE)
1649 gfc_fatal_error ("Coarrays disabled at %L, use %<-fcoarray=%> to enable",
1650 &a->where);
1651 return false;
1654 return true;
1658 bool
1659 gfc_check_co_broadcast (gfc_expr *a, gfc_expr *source_image, gfc_expr *stat,
1660 gfc_expr *errmsg)
1662 if (a->ts.type == BT_CLASS || gfc_expr_attr (a).alloc_comp)
1664 gfc_error ("Support for the A argument at %L which is polymorphic A "
1665 "argument or has allocatable components is not yet "
1666 "implemented", &a->where);
1667 return false;
1669 return check_co_collective (a, source_image, stat, errmsg, false);
1673 bool
1674 gfc_check_co_reduce (gfc_expr *a, gfc_expr *op, gfc_expr *result_image,
1675 gfc_expr *stat, gfc_expr *errmsg)
1677 symbol_attribute attr;
1678 gfc_formal_arglist *formal;
1679 gfc_symbol *sym;
1681 if (a->ts.type == BT_CLASS)
1683 gfc_error ("The A argument at %L of CO_REDUCE shall not be polymorphic",
1684 &a->where);
1685 return false;
1688 if (gfc_expr_attr (a).alloc_comp)
1690 gfc_error ("Support for the A argument at %L with allocatable components"
1691 " is not yet implemented", &a->where);
1692 return false;
1695 if (!check_co_collective (a, result_image, stat, errmsg, true))
1696 return false;
1698 if (!gfc_resolve_expr (op))
1699 return false;
1701 attr = gfc_expr_attr (op);
1702 if (!attr.pure || !attr.function)
1704 gfc_error ("OPERATOR argument at %L must be a PURE function",
1705 &op->where);
1706 return false;
1709 if (attr.intrinsic)
1711 /* None of the intrinsics fulfills the criteria of taking two arguments,
1712 returning the same type and kind as the arguments and being permitted
1713 as actual argument. */
1714 gfc_error ("Intrinsic function %s at %L is not permitted for CO_REDUCE",
1715 op->symtree->n.sym->name, &op->where);
1716 return false;
1719 if (gfc_is_proc_ptr_comp (op))
1721 gfc_component *comp = gfc_get_proc_ptr_comp (op);
1722 sym = comp->ts.interface;
1724 else
1725 sym = op->symtree->n.sym;
1727 formal = sym->formal;
1729 if (!formal || !formal->next || formal->next->next)
1731 gfc_error ("The function passed as OPERATOR at %L shall have two "
1732 "arguments", &op->where);
1733 return false;
1736 if (sym->result->ts.type == BT_UNKNOWN)
1737 gfc_set_default_type (sym->result, 0, NULL);
1739 if (!gfc_compare_types (&a->ts, &sym->result->ts))
1741 gfc_error ("The A argument at %L has type %s but the function passed as "
1742 "OPERATOR at %L returns %s",
1743 &a->where, gfc_typename (&a->ts), &op->where,
1744 gfc_typename (&sym->result->ts));
1745 return false;
1747 if (!gfc_compare_types (&a->ts, &formal->sym->ts)
1748 || !gfc_compare_types (&a->ts, &formal->next->sym->ts))
1750 gfc_error ("The function passed as OPERATOR at %L has arguments of type "
1751 "%s and %s but shall have type %s", &op->where,
1752 gfc_typename (&formal->sym->ts),
1753 gfc_typename (&formal->next->sym->ts), gfc_typename (&a->ts));
1754 return false;
1756 if (op->rank || attr.allocatable || attr.pointer || formal->sym->as
1757 || formal->next->sym->as || formal->sym->attr.allocatable
1758 || formal->next->sym->attr.allocatable || formal->sym->attr.pointer
1759 || formal->next->sym->attr.pointer)
1761 gfc_error ("The function passed as OPERATOR at %L shall have scalar "
1762 "nonallocatable nonpointer arguments and return a "
1763 "nonallocatable nonpointer scalar", &op->where);
1764 return false;
1767 if (formal->sym->attr.value != formal->next->sym->attr.value)
1769 gfc_error ("The function passed as OPERATOR at %L shall have the VALUE "
1770 "attribute either for none or both arguments", &op->where);
1771 return false;
1774 if (formal->sym->attr.target != formal->next->sym->attr.target)
1776 gfc_error ("The function passed as OPERATOR at %L shall have the TARGET "
1777 "attribute either for none or both arguments", &op->where);
1778 return false;
1781 if (formal->sym->attr.asynchronous != formal->next->sym->attr.asynchronous)
1783 gfc_error ("The function passed as OPERATOR at %L shall have the "
1784 "ASYNCHRONOUS attribute either for none or both arguments",
1785 &op->where);
1786 return false;
1789 if (formal->sym->attr.optional || formal->next->sym->attr.optional)
1791 gfc_error ("The function passed as OPERATOR at %L shall not have the "
1792 "OPTIONAL attribute for either of the arguments", &op->where);
1793 return false;
1796 if (a->ts.type == BT_CHARACTER)
1798 gfc_charlen *cl;
1799 unsigned long actual_size, formal_size1, formal_size2, result_size;
1801 cl = a->ts.u.cl;
1802 actual_size = cl && cl->length && cl->length->expr_type == EXPR_CONSTANT
1803 ? mpz_get_ui (cl->length->value.integer) : 0;
1805 cl = formal->sym->ts.u.cl;
1806 formal_size1 = cl && cl->length && cl->length->expr_type == EXPR_CONSTANT
1807 ? mpz_get_ui (cl->length->value.integer) : 0;
1809 cl = formal->next->sym->ts.u.cl;
1810 formal_size2 = cl && cl->length && cl->length->expr_type == EXPR_CONSTANT
1811 ? mpz_get_ui (cl->length->value.integer) : 0;
1813 cl = sym->ts.u.cl;
1814 result_size = cl && cl->length && cl->length->expr_type == EXPR_CONSTANT
1815 ? mpz_get_ui (cl->length->value.integer) : 0;
1817 if (actual_size
1818 && ((formal_size1 && actual_size != formal_size1)
1819 || (formal_size2 && actual_size != formal_size2)))
1821 gfc_error ("The character length of the A argument at %L and of the "
1822 "arguments of the OPERATOR at %L shall be the same",
1823 &a->where, &op->where);
1824 return false;
1826 if (actual_size && result_size && actual_size != result_size)
1828 gfc_error ("The character length of the A argument at %L and of the "
1829 "function result of the OPERATOR at %L shall be the same",
1830 &a->where, &op->where);
1831 return false;
1835 return true;
1839 bool
1840 gfc_check_co_minmax (gfc_expr *a, gfc_expr *result_image, gfc_expr *stat,
1841 gfc_expr *errmsg)
1843 if (a->ts.type != BT_INTEGER && a->ts.type != BT_REAL
1844 && a->ts.type != BT_CHARACTER)
1846 gfc_error ("%qs argument of %qs intrinsic at %L shall be of type "
1847 "integer, real or character",
1848 gfc_current_intrinsic_arg[0]->name, gfc_current_intrinsic,
1849 &a->where);
1850 return false;
1852 return check_co_collective (a, result_image, stat, errmsg, false);
1856 bool
1857 gfc_check_co_sum (gfc_expr *a, gfc_expr *result_image, gfc_expr *stat,
1858 gfc_expr *errmsg)
1860 if (!numeric_check (a, 0))
1861 return false;
1862 return check_co_collective (a, result_image, stat, errmsg, false);
1866 bool
1867 gfc_check_complex (gfc_expr *x, gfc_expr *y)
1869 if (!int_or_real_check (x, 0))
1870 return false;
1871 if (!scalar_check (x, 0))
1872 return false;
1874 if (!int_or_real_check (y, 1))
1875 return false;
1876 if (!scalar_check (y, 1))
1877 return false;
1879 return true;
1883 bool
1884 gfc_check_count (gfc_expr *mask, gfc_expr *dim, gfc_expr *kind)
1886 if (!logical_array_check (mask, 0))
1887 return false;
1888 if (!dim_check (dim, 1, false))
1889 return false;
1890 if (!dim_rank_check (dim, mask, 0))
1891 return false;
1892 if (!kind_check (kind, 2, BT_INTEGER))
1893 return false;
1894 if (kind && !gfc_notify_std (GFC_STD_F2003, "%qs intrinsic "
1895 "with KIND argument at %L",
1896 gfc_current_intrinsic, &kind->where))
1897 return false;
1899 return true;
1903 bool
1904 gfc_check_cshift (gfc_expr *array, gfc_expr *shift, gfc_expr *dim)
1906 if (!array_check (array, 0))
1907 return false;
1909 if (!type_check (shift, 1, BT_INTEGER))
1910 return false;
1912 if (!dim_check (dim, 2, true))
1913 return false;
1915 if (!dim_rank_check (dim, array, false))
1916 return false;
1918 if (array->rank == 1 || shift->rank == 0)
1920 if (!scalar_check (shift, 1))
1921 return false;
1923 else if (shift->rank == array->rank - 1)
1925 int d;
1926 if (!dim)
1927 d = 1;
1928 else if (dim->expr_type == EXPR_CONSTANT)
1929 gfc_extract_int (dim, &d);
1930 else
1931 d = -1;
1933 if (d > 0)
1935 int i, j;
1936 for (i = 0, j = 0; i < array->rank; i++)
1937 if (i != d - 1)
1939 if (!identical_dimen_shape (array, i, shift, j))
1941 gfc_error ("%qs argument of %qs intrinsic at %L has "
1942 "invalid shape in dimension %d (%ld/%ld)",
1943 gfc_current_intrinsic_arg[1]->name,
1944 gfc_current_intrinsic, &shift->where, i + 1,
1945 mpz_get_si (array->shape[i]),
1946 mpz_get_si (shift->shape[j]));
1947 return false;
1950 j += 1;
1954 else
1956 gfc_error ("%qs argument of intrinsic %qs at %L of must have rank "
1957 "%d or be a scalar", gfc_current_intrinsic_arg[1]->name,
1958 gfc_current_intrinsic, &shift->where, array->rank - 1);
1959 return false;
1962 return true;
1966 bool
1967 gfc_check_ctime (gfc_expr *time)
1969 if (!scalar_check (time, 0))
1970 return false;
1972 if (!type_check (time, 0, BT_INTEGER))
1973 return false;
1975 return true;
1979 bool gfc_check_datan2 (gfc_expr *y, gfc_expr *x)
1981 if (!double_check (y, 0) || !double_check (x, 1))
1982 return false;
1984 return true;
1987 bool
1988 gfc_check_dcmplx (gfc_expr *x, gfc_expr *y)
1990 if (!numeric_check (x, 0))
1991 return false;
1993 if (y != NULL)
1995 if (!numeric_check (y, 1))
1996 return false;
1998 if (x->ts.type == BT_COMPLEX)
2000 gfc_error ("%qs argument of %qs intrinsic at %L must not be "
2001 "present if %<x%> is COMPLEX",
2002 gfc_current_intrinsic_arg[1]->name, gfc_current_intrinsic,
2003 &y->where);
2004 return false;
2007 if (y->ts.type == BT_COMPLEX)
2009 gfc_error ("%qs argument of %qs intrinsic at %L must have a type "
2010 "of either REAL or INTEGER",
2011 gfc_current_intrinsic_arg[1]->name, gfc_current_intrinsic,
2012 &y->where);
2013 return false;
2017 return true;
2021 bool
2022 gfc_check_dble (gfc_expr *x)
2024 if (!numeric_check (x, 0))
2025 return false;
2027 return true;
2031 bool
2032 gfc_check_digits (gfc_expr *x)
2034 if (!int_or_real_check (x, 0))
2035 return false;
2037 return true;
2041 bool
2042 gfc_check_dot_product (gfc_expr *vector_a, gfc_expr *vector_b)
2044 switch (vector_a->ts.type)
2046 case BT_LOGICAL:
2047 if (!type_check (vector_b, 1, BT_LOGICAL))
2048 return false;
2049 break;
2051 case BT_INTEGER:
2052 case BT_REAL:
2053 case BT_COMPLEX:
2054 if (!numeric_check (vector_b, 1))
2055 return false;
2056 break;
2058 default:
2059 gfc_error ("%qs argument of %qs intrinsic at %L must be numeric "
2060 "or LOGICAL", gfc_current_intrinsic_arg[0]->name,
2061 gfc_current_intrinsic, &vector_a->where);
2062 return false;
2065 if (!rank_check (vector_a, 0, 1))
2066 return false;
2068 if (!rank_check (vector_b, 1, 1))
2069 return false;
2071 if (! identical_dimen_shape (vector_a, 0, vector_b, 0))
2073 gfc_error ("Different shape for arguments %qs and %qs at %L for "
2074 "intrinsic %<dot_product%>",
2075 gfc_current_intrinsic_arg[0]->name,
2076 gfc_current_intrinsic_arg[1]->name, &vector_a->where);
2077 return false;
2080 return true;
2084 bool
2085 gfc_check_dprod (gfc_expr *x, gfc_expr *y)
2087 if (!type_check (x, 0, BT_REAL)
2088 || !type_check (y, 1, BT_REAL))
2089 return false;
2091 if (x->ts.kind != gfc_default_real_kind)
2093 gfc_error ("%qs argument of %qs intrinsic at %L must be default "
2094 "real", gfc_current_intrinsic_arg[0]->name,
2095 gfc_current_intrinsic, &x->where);
2096 return false;
2099 if (y->ts.kind != gfc_default_real_kind)
2101 gfc_error ("%qs argument of %qs intrinsic at %L must be default "
2102 "real", gfc_current_intrinsic_arg[1]->name,
2103 gfc_current_intrinsic, &y->where);
2104 return false;
2107 return true;
2111 bool
2112 gfc_check_dshift (gfc_expr *i, gfc_expr *j, gfc_expr *shift)
2114 if (!type_check (i, 0, BT_INTEGER))
2115 return false;
2117 if (!type_check (j, 1, BT_INTEGER))
2118 return false;
2120 if (i->is_boz && j->is_boz)
2122 gfc_error ("%<I%> at %L and %<J%>' at %L cannot both be BOZ literal "
2123 "constants", &i->where, &j->where);
2124 return false;
2127 if (!i->is_boz && !j->is_boz && !same_type_check (i, 0, j, 1))
2128 return false;
2130 if (!type_check (shift, 2, BT_INTEGER))
2131 return false;
2133 if (!nonnegative_check ("SHIFT", shift))
2134 return false;
2136 if (i->is_boz)
2138 if (!less_than_bitsize1 ("J", j, "SHIFT", shift, true))
2139 return false;
2140 i->ts.kind = j->ts.kind;
2142 else
2144 if (!less_than_bitsize1 ("I", i, "SHIFT", shift, true))
2145 return false;
2146 j->ts.kind = i->ts.kind;
2149 return true;
2153 bool
2154 gfc_check_eoshift (gfc_expr *array, gfc_expr *shift, gfc_expr *boundary,
2155 gfc_expr *dim)
2157 if (!array_check (array, 0))
2158 return false;
2160 if (!type_check (shift, 1, BT_INTEGER))
2161 return false;
2163 if (!dim_check (dim, 3, true))
2164 return false;
2166 if (!dim_rank_check (dim, array, false))
2167 return false;
2169 if (array->rank == 1 || shift->rank == 0)
2171 if (!scalar_check (shift, 1))
2172 return false;
2174 else if (shift->rank == array->rank - 1)
2176 int d;
2177 if (!dim)
2178 d = 1;
2179 else if (dim->expr_type == EXPR_CONSTANT)
2180 gfc_extract_int (dim, &d);
2181 else
2182 d = -1;
2184 if (d > 0)
2186 int i, j;
2187 for (i = 0, j = 0; i < array->rank; i++)
2188 if (i != d - 1)
2190 if (!identical_dimen_shape (array, i, shift, j))
2192 gfc_error ("%qs argument of %qs intrinsic at %L has "
2193 "invalid shape in dimension %d (%ld/%ld)",
2194 gfc_current_intrinsic_arg[1]->name,
2195 gfc_current_intrinsic, &shift->where, i + 1,
2196 mpz_get_si (array->shape[i]),
2197 mpz_get_si (shift->shape[j]));
2198 return false;
2201 j += 1;
2205 else
2207 gfc_error ("%qs argument of intrinsic %qs at %L of must have rank "
2208 "%d or be a scalar", gfc_current_intrinsic_arg[1]->name,
2209 gfc_current_intrinsic, &shift->where, array->rank - 1);
2210 return false;
2213 if (boundary != NULL)
2215 if (!same_type_check (array, 0, boundary, 2))
2216 return false;
2218 if (array->rank == 1 || boundary->rank == 0)
2220 if (!scalar_check (boundary, 2))
2221 return false;
2223 else if (boundary->rank == array->rank - 1)
2225 if (!gfc_check_conformance (shift, boundary,
2226 "arguments '%s' and '%s' for "
2227 "intrinsic %s",
2228 gfc_current_intrinsic_arg[1]->name,
2229 gfc_current_intrinsic_arg[2]->name,
2230 gfc_current_intrinsic))
2231 return false;
2233 else
2235 gfc_error ("%qs argument of intrinsic %qs at %L of must have "
2236 "rank %d or be a scalar",
2237 gfc_current_intrinsic_arg[1]->name, gfc_current_intrinsic,
2238 &shift->where, array->rank - 1);
2239 return false;
2243 return true;
2246 bool
2247 gfc_check_float (gfc_expr *a)
2249 if (!type_check (a, 0, BT_INTEGER))
2250 return false;
2252 if ((a->ts.kind != gfc_default_integer_kind)
2253 && !gfc_notify_std (GFC_STD_GNU, "non-default INTEGER "
2254 "kind argument to %s intrinsic at %L",
2255 gfc_current_intrinsic, &a->where))
2256 return false;
2258 return true;
2261 /* A single complex argument. */
2263 bool
2264 gfc_check_fn_c (gfc_expr *a)
2266 if (!type_check (a, 0, BT_COMPLEX))
2267 return false;
2269 return true;
2273 /* A single real argument. */
2275 bool
2276 gfc_check_fn_r (gfc_expr *a)
2278 if (!type_check (a, 0, BT_REAL))
2279 return false;
2281 return true;
2284 /* A single double argument. */
2286 bool
2287 gfc_check_fn_d (gfc_expr *a)
2289 if (!double_check (a, 0))
2290 return false;
2292 return true;
2295 /* A single real or complex argument. */
2297 bool
2298 gfc_check_fn_rc (gfc_expr *a)
2300 if (!real_or_complex_check (a, 0))
2301 return false;
2303 return true;
2307 bool
2308 gfc_check_fn_rc2008 (gfc_expr *a)
2310 if (!real_or_complex_check (a, 0))
2311 return false;
2313 if (a->ts.type == BT_COMPLEX
2314 && !gfc_notify_std (GFC_STD_F2008, "COMPLEX argument %qs "
2315 "of %qs intrinsic at %L",
2316 gfc_current_intrinsic_arg[0]->name,
2317 gfc_current_intrinsic, &a->where))
2318 return false;
2320 return true;
2324 bool
2325 gfc_check_fnum (gfc_expr *unit)
2327 if (!type_check (unit, 0, BT_INTEGER))
2328 return false;
2330 if (!scalar_check (unit, 0))
2331 return false;
2333 return true;
2337 bool
2338 gfc_check_huge (gfc_expr *x)
2340 if (!int_or_real_check (x, 0))
2341 return false;
2343 return true;
2347 bool
2348 gfc_check_hypot (gfc_expr *x, gfc_expr *y)
2350 if (!type_check (x, 0, BT_REAL))
2351 return false;
2352 if (!same_type_check (x, 0, y, 1))
2353 return false;
2355 return true;
2359 /* Check that the single argument is an integer. */
2361 bool
2362 gfc_check_i (gfc_expr *i)
2364 if (!type_check (i, 0, BT_INTEGER))
2365 return false;
2367 return true;
2371 bool
2372 gfc_check_iand (gfc_expr *i, gfc_expr *j)
2374 if (!type_check (i, 0, BT_INTEGER))
2375 return false;
2377 if (!type_check (j, 1, BT_INTEGER))
2378 return false;
2380 if (i->ts.kind != j->ts.kind)
2382 if (!gfc_notify_std (GFC_STD_GNU, "Different type kinds at %L",
2383 &i->where))
2384 return false;
2387 return true;
2391 bool
2392 gfc_check_ibits (gfc_expr *i, gfc_expr *pos, gfc_expr *len)
2394 if (!type_check (i, 0, BT_INTEGER))
2395 return false;
2397 if (!type_check (pos, 1, BT_INTEGER))
2398 return false;
2400 if (!type_check (len, 2, BT_INTEGER))
2401 return false;
2403 if (!nonnegative_check ("pos", pos))
2404 return false;
2406 if (!nonnegative_check ("len", len))
2407 return false;
2409 if (!less_than_bitsize2 ("i", i, "pos", pos, "len", len))
2410 return false;
2412 return true;
2416 bool
2417 gfc_check_ichar_iachar (gfc_expr *c, gfc_expr *kind)
2419 int i;
2421 if (!type_check (c, 0, BT_CHARACTER))
2422 return false;
2424 if (!kind_check (kind, 1, BT_INTEGER))
2425 return false;
2427 if (kind && !gfc_notify_std (GFC_STD_F2003, "%qs intrinsic "
2428 "with KIND argument at %L",
2429 gfc_current_intrinsic, &kind->where))
2430 return false;
2432 if (c->expr_type == EXPR_VARIABLE || c->expr_type == EXPR_SUBSTRING)
2434 gfc_expr *start;
2435 gfc_expr *end;
2436 gfc_ref *ref;
2438 /* Substring references don't have the charlength set. */
2439 ref = c->ref;
2440 while (ref && ref->type != REF_SUBSTRING)
2441 ref = ref->next;
2443 gcc_assert (ref == NULL || ref->type == REF_SUBSTRING);
2445 if (!ref)
2447 /* Check that the argument is length one. Non-constant lengths
2448 can't be checked here, so assume they are ok. */
2449 if (c->ts.u.cl && c->ts.u.cl->length)
2451 /* If we already have a length for this expression then use it. */
2452 if (c->ts.u.cl->length->expr_type != EXPR_CONSTANT)
2453 return true;
2454 i = mpz_get_si (c->ts.u.cl->length->value.integer);
2456 else
2457 return true;
2459 else
2461 start = ref->u.ss.start;
2462 end = ref->u.ss.end;
2464 gcc_assert (start);
2465 if (end == NULL || end->expr_type != EXPR_CONSTANT
2466 || start->expr_type != EXPR_CONSTANT)
2467 return true;
2469 i = mpz_get_si (end->value.integer) + 1
2470 - mpz_get_si (start->value.integer);
2473 else
2474 return true;
2476 if (i != 1)
2478 gfc_error ("Argument of %s at %L must be of length one",
2479 gfc_current_intrinsic, &c->where);
2480 return false;
2483 return true;
2487 bool
2488 gfc_check_idnint (gfc_expr *a)
2490 if (!double_check (a, 0))
2491 return false;
2493 return true;
2497 bool
2498 gfc_check_ieor (gfc_expr *i, gfc_expr *j)
2500 if (!type_check (i, 0, BT_INTEGER))
2501 return false;
2503 if (!type_check (j, 1, BT_INTEGER))
2504 return false;
2506 if (i->ts.kind != j->ts.kind)
2508 if (!gfc_notify_std (GFC_STD_GNU, "Different type kinds at %L",
2509 &i->where))
2510 return false;
2513 return true;
2517 bool
2518 gfc_check_index (gfc_expr *string, gfc_expr *substring, gfc_expr *back,
2519 gfc_expr *kind)
2521 if (!type_check (string, 0, BT_CHARACTER)
2522 || !type_check (substring, 1, BT_CHARACTER))
2523 return false;
2525 if (back != NULL && !type_check (back, 2, BT_LOGICAL))
2526 return false;
2528 if (!kind_check (kind, 3, BT_INTEGER))
2529 return false;
2530 if (kind && !gfc_notify_std (GFC_STD_F2003, "%qs intrinsic "
2531 "with KIND argument at %L",
2532 gfc_current_intrinsic, &kind->where))
2533 return false;
2535 if (string->ts.kind != substring->ts.kind)
2537 gfc_error ("%qs argument of %qs intrinsic at %L must be the same "
2538 "kind as %qs", gfc_current_intrinsic_arg[1]->name,
2539 gfc_current_intrinsic, &substring->where,
2540 gfc_current_intrinsic_arg[0]->name);
2541 return false;
2544 return true;
2548 bool
2549 gfc_check_int (gfc_expr *x, gfc_expr *kind)
2551 if (!numeric_check (x, 0))
2552 return false;
2554 if (!kind_check (kind, 1, BT_INTEGER))
2555 return false;
2557 return true;
2561 bool
2562 gfc_check_intconv (gfc_expr *x)
2564 if (!numeric_check (x, 0))
2565 return false;
2567 return true;
2571 bool
2572 gfc_check_ior (gfc_expr *i, gfc_expr *j)
2574 if (!type_check (i, 0, BT_INTEGER))
2575 return false;
2577 if (!type_check (j, 1, BT_INTEGER))
2578 return false;
2580 if (i->ts.kind != j->ts.kind)
2582 if (!gfc_notify_std (GFC_STD_GNU, "Different type kinds at %L",
2583 &i->where))
2584 return false;
2587 return true;
2591 bool
2592 gfc_check_ishft (gfc_expr *i, gfc_expr *shift)
2594 if (!type_check (i, 0, BT_INTEGER)
2595 || !type_check (shift, 1, BT_INTEGER))
2596 return false;
2598 if (!less_than_bitsize1 ("I", i, NULL, shift, true))
2599 return false;
2601 return true;
2605 bool
2606 gfc_check_ishftc (gfc_expr *i, gfc_expr *shift, gfc_expr *size)
2608 if (!type_check (i, 0, BT_INTEGER)
2609 || !type_check (shift, 1, BT_INTEGER))
2610 return false;
2612 if (size != NULL)
2614 int i2, i3;
2616 if (!type_check (size, 2, BT_INTEGER))
2617 return false;
2619 if (!less_than_bitsize1 ("I", i, "SIZE", size, true))
2620 return false;
2622 if (size->expr_type == EXPR_CONSTANT)
2624 gfc_extract_int (size, &i3);
2625 if (i3 <= 0)
2627 gfc_error ("SIZE at %L must be positive", &size->where);
2628 return false;
2631 if (shift->expr_type == EXPR_CONSTANT)
2633 gfc_extract_int (shift, &i2);
2634 if (i2 < 0)
2635 i2 = -i2;
2637 if (i2 > i3)
2639 gfc_error ("The absolute value of SHIFT at %L must be less "
2640 "than or equal to SIZE at %L", &shift->where,
2641 &size->where);
2642 return false;
2647 else if (!less_than_bitsize1 ("I", i, NULL, shift, true))
2648 return false;
2650 return true;
2654 bool
2655 gfc_check_kill (gfc_expr *pid, gfc_expr *sig)
2657 if (!type_check (pid, 0, BT_INTEGER))
2658 return false;
2660 if (!type_check (sig, 1, BT_INTEGER))
2661 return false;
2663 return true;
2667 bool
2668 gfc_check_kill_sub (gfc_expr *pid, gfc_expr *sig, gfc_expr *status)
2670 if (!type_check (pid, 0, BT_INTEGER))
2671 return false;
2673 if (!scalar_check (pid, 0))
2674 return false;
2676 if (!type_check (sig, 1, BT_INTEGER))
2677 return false;
2679 if (!scalar_check (sig, 1))
2680 return false;
2682 if (status == NULL)
2683 return true;
2685 if (!type_check (status, 2, BT_INTEGER))
2686 return false;
2688 if (!scalar_check (status, 2))
2689 return false;
2691 return true;
2695 bool
2696 gfc_check_kind (gfc_expr *x)
2698 if (gfc_bt_struct (x->ts.type) || x->ts.type == BT_CLASS)
2700 gfc_error ("%qs argument of %qs intrinsic at %L must be of "
2701 "intrinsic type", gfc_current_intrinsic_arg[0]->name,
2702 gfc_current_intrinsic, &x->where);
2703 return false;
2705 if (x->ts.type == BT_PROCEDURE)
2707 gfc_error ("%qs argument of %qs intrinsic at %L must be a data entity",
2708 gfc_current_intrinsic_arg[0]->name, gfc_current_intrinsic,
2709 &x->where);
2710 return false;
2713 return true;
2717 bool
2718 gfc_check_lbound (gfc_expr *array, gfc_expr *dim, gfc_expr *kind)
2720 if (!array_check (array, 0))
2721 return false;
2723 if (!dim_check (dim, 1, false))
2724 return false;
2726 if (!dim_rank_check (dim, array, 1))
2727 return false;
2729 if (!kind_check (kind, 2, BT_INTEGER))
2730 return false;
2731 if (kind && !gfc_notify_std (GFC_STD_F2003, "%qs intrinsic "
2732 "with KIND argument at %L",
2733 gfc_current_intrinsic, &kind->where))
2734 return false;
2736 return true;
2740 bool
2741 gfc_check_lcobound (gfc_expr *coarray, gfc_expr *dim, gfc_expr *kind)
2743 if (flag_coarray == GFC_FCOARRAY_NONE)
2745 gfc_fatal_error ("Coarrays disabled at %C, use %<-fcoarray=%> to enable");
2746 return false;
2749 if (!coarray_check (coarray, 0))
2750 return false;
2752 if (dim != NULL)
2754 if (!dim_check (dim, 1, false))
2755 return false;
2757 if (!dim_corank_check (dim, coarray))
2758 return false;
2761 if (!kind_check (kind, 2, BT_INTEGER))
2762 return false;
2764 return true;
2768 bool
2769 gfc_check_len_lentrim (gfc_expr *s, gfc_expr *kind)
2771 if (!type_check (s, 0, BT_CHARACTER))
2772 return false;
2774 if (!kind_check (kind, 1, BT_INTEGER))
2775 return false;
2776 if (kind && !gfc_notify_std (GFC_STD_F2003, "%qs intrinsic "
2777 "with KIND argument at %L",
2778 gfc_current_intrinsic, &kind->where))
2779 return false;
2781 return true;
2785 bool
2786 gfc_check_lge_lgt_lle_llt (gfc_expr *a, gfc_expr *b)
2788 if (!type_check (a, 0, BT_CHARACTER))
2789 return false;
2790 if (!kind_value_check (a, 0, gfc_default_character_kind))
2791 return false;
2793 if (!type_check (b, 1, BT_CHARACTER))
2794 return false;
2795 if (!kind_value_check (b, 1, gfc_default_character_kind))
2796 return false;
2798 return true;
2802 bool
2803 gfc_check_link (gfc_expr *path1, gfc_expr *path2)
2805 if (!type_check (path1, 0, BT_CHARACTER))
2806 return false;
2807 if (!kind_value_check (path1, 0, gfc_default_character_kind))
2808 return false;
2810 if (!type_check (path2, 1, BT_CHARACTER))
2811 return false;
2812 if (!kind_value_check (path2, 1, gfc_default_character_kind))
2813 return false;
2815 return true;
2819 bool
2820 gfc_check_link_sub (gfc_expr *path1, gfc_expr *path2, gfc_expr *status)
2822 if (!type_check (path1, 0, BT_CHARACTER))
2823 return false;
2824 if (!kind_value_check (path1, 0, gfc_default_character_kind))
2825 return false;
2827 if (!type_check (path2, 1, BT_CHARACTER))
2828 return false;
2829 if (!kind_value_check (path2, 0, gfc_default_character_kind))
2830 return false;
2832 if (status == NULL)
2833 return true;
2835 if (!type_check (status, 2, BT_INTEGER))
2836 return false;
2838 if (!scalar_check (status, 2))
2839 return false;
2841 return true;
2845 bool
2846 gfc_check_loc (gfc_expr *expr)
2848 return variable_check (expr, 0, true);
2852 bool
2853 gfc_check_symlnk (gfc_expr *path1, gfc_expr *path2)
2855 if (!type_check (path1, 0, BT_CHARACTER))
2856 return false;
2857 if (!kind_value_check (path1, 0, gfc_default_character_kind))
2858 return false;
2860 if (!type_check (path2, 1, BT_CHARACTER))
2861 return false;
2862 if (!kind_value_check (path2, 1, gfc_default_character_kind))
2863 return false;
2865 return true;
2869 bool
2870 gfc_check_symlnk_sub (gfc_expr *path1, gfc_expr *path2, gfc_expr *status)
2872 if (!type_check (path1, 0, BT_CHARACTER))
2873 return false;
2874 if (!kind_value_check (path1, 0, gfc_default_character_kind))
2875 return false;
2877 if (!type_check (path2, 1, BT_CHARACTER))
2878 return false;
2879 if (!kind_value_check (path2, 1, gfc_default_character_kind))
2880 return false;
2882 if (status == NULL)
2883 return true;
2885 if (!type_check (status, 2, BT_INTEGER))
2886 return false;
2888 if (!scalar_check (status, 2))
2889 return false;
2891 return true;
2895 bool
2896 gfc_check_logical (gfc_expr *a, gfc_expr *kind)
2898 if (!type_check (a, 0, BT_LOGICAL))
2899 return false;
2900 if (!kind_check (kind, 1, BT_LOGICAL))
2901 return false;
2903 return true;
2907 /* Min/max family. */
2909 static bool
2910 min_max_args (gfc_actual_arglist *args)
2912 gfc_actual_arglist *arg;
2913 int i, j, nargs, *nlabels, nlabelless;
2914 bool a1 = false, a2 = false;
2916 if (args == NULL || args->next == NULL)
2918 gfc_error ("Intrinsic %qs at %L must have at least two arguments",
2919 gfc_current_intrinsic, gfc_current_intrinsic_where);
2920 return false;
2923 if (!args->name)
2924 a1 = true;
2926 if (!args->next->name)
2927 a2 = true;
2929 nargs = 0;
2930 for (arg = args; arg; arg = arg->next)
2931 if (arg->name)
2932 nargs++;
2934 if (nargs == 0)
2935 return true;
2937 /* Note: Having a keywordless argument after an "arg=" is checked before. */
2938 nlabelless = 0;
2939 nlabels = XALLOCAVEC (int, nargs);
2940 for (arg = args, i = 0; arg; arg = arg->next, i++)
2941 if (arg->name)
2943 int n;
2944 char *endp;
2946 if (arg->name[0] != 'a' || arg->name[1] < '1' || arg->name[1] > '9')
2947 goto unknown;
2948 n = strtol (&arg->name[1], &endp, 10);
2949 if (endp[0] != '\0')
2950 goto unknown;
2951 if (n <= 0)
2952 goto unknown;
2953 if (n <= nlabelless)
2954 goto duplicate;
2955 nlabels[i] = n;
2956 if (n == 1)
2957 a1 = true;
2958 if (n == 2)
2959 a2 = true;
2961 else
2962 nlabelless++;
2964 if (!a1 || !a2)
2966 gfc_error ("Missing %qs argument to the %s intrinsic at %L",
2967 !a1 ? "a1" : "a2", gfc_current_intrinsic,
2968 gfc_current_intrinsic_where);
2969 return false;
2972 /* Check for duplicates. */
2973 for (i = 0; i < nargs; i++)
2974 for (j = i + 1; j < nargs; j++)
2975 if (nlabels[i] == nlabels[j])
2976 goto duplicate;
2978 return true;
2980 duplicate:
2981 gfc_error ("Duplicate argument %qs at %L to intrinsic %s", arg->name,
2982 &arg->expr->where, gfc_current_intrinsic);
2983 return false;
2985 unknown:
2986 gfc_error ("Unknown argument %qs at %L to intrinsic %s", arg->name,
2987 &arg->expr->where, gfc_current_intrinsic);
2988 return false;
2992 static bool
2993 check_rest (bt type, int kind, gfc_actual_arglist *arglist)
2995 gfc_actual_arglist *arg, *tmp;
2996 gfc_expr *x;
2997 int m, n;
2999 if (!min_max_args (arglist))
3000 return false;
3002 for (arg = arglist, n=1; arg; arg = arg->next, n++)
3004 x = arg->expr;
3005 if (x->ts.type != type || x->ts.kind != kind)
3007 if (x->ts.type == type)
3009 if (!gfc_notify_std (GFC_STD_GNU, "Different type "
3010 "kinds at %L", &x->where))
3011 return false;
3013 else
3015 gfc_error ("%<a%d%> argument of %qs intrinsic at %L must be "
3016 "%s(%d)", n, gfc_current_intrinsic, &x->where,
3017 gfc_basic_typename (type), kind);
3018 return false;
3022 for (tmp = arglist, m=1; tmp != arg; tmp = tmp->next, m++)
3023 if (!gfc_check_conformance (tmp->expr, x,
3024 "arguments 'a%d' and 'a%d' for "
3025 "intrinsic '%s'", m, n,
3026 gfc_current_intrinsic))
3027 return false;
3030 return true;
3034 bool
3035 gfc_check_min_max (gfc_actual_arglist *arg)
3037 gfc_expr *x;
3039 if (!min_max_args (arg))
3040 return false;
3042 x = arg->expr;
3044 if (x->ts.type == BT_CHARACTER)
3046 if (!gfc_notify_std (GFC_STD_F2003, "%qs intrinsic "
3047 "with CHARACTER argument at %L",
3048 gfc_current_intrinsic, &x->where))
3049 return false;
3051 else if (x->ts.type != BT_INTEGER && x->ts.type != BT_REAL)
3053 gfc_error ("%<a1%> argument of %qs intrinsic at %L must be INTEGER, "
3054 "REAL or CHARACTER", gfc_current_intrinsic, &x->where);
3055 return false;
3058 return check_rest (x->ts.type, x->ts.kind, arg);
3062 bool
3063 gfc_check_min_max_integer (gfc_actual_arglist *arg)
3065 return check_rest (BT_INTEGER, gfc_default_integer_kind, arg);
3069 bool
3070 gfc_check_min_max_real (gfc_actual_arglist *arg)
3072 return check_rest (BT_REAL, gfc_default_real_kind, arg);
3076 bool
3077 gfc_check_min_max_double (gfc_actual_arglist *arg)
3079 return check_rest (BT_REAL, gfc_default_double_kind, arg);
3083 /* End of min/max family. */
3085 bool
3086 gfc_check_malloc (gfc_expr *size)
3088 if (!type_check (size, 0, BT_INTEGER))
3089 return false;
3091 if (!scalar_check (size, 0))
3092 return false;
3094 return true;
3098 bool
3099 gfc_check_matmul (gfc_expr *matrix_a, gfc_expr *matrix_b)
3101 if ((matrix_a->ts.type != BT_LOGICAL) && !gfc_numeric_ts (&matrix_a->ts))
3103 gfc_error ("%qs argument of %qs intrinsic at %L must be numeric "
3104 "or LOGICAL", gfc_current_intrinsic_arg[0]->name,
3105 gfc_current_intrinsic, &matrix_a->where);
3106 return false;
3109 if ((matrix_b->ts.type != BT_LOGICAL) && !gfc_numeric_ts (&matrix_b->ts))
3111 gfc_error ("%qs argument of %qs intrinsic at %L must be numeric "
3112 "or LOGICAL", gfc_current_intrinsic_arg[1]->name,
3113 gfc_current_intrinsic, &matrix_b->where);
3114 return false;
3117 if ((matrix_a->ts.type == BT_LOGICAL && gfc_numeric_ts (&matrix_b->ts))
3118 || (gfc_numeric_ts (&matrix_a->ts) && matrix_b->ts.type == BT_LOGICAL))
3120 gfc_error ("Argument types of %qs intrinsic at %L must match (%s/%s)",
3121 gfc_current_intrinsic, &matrix_a->where,
3122 gfc_typename(&matrix_a->ts), gfc_typename(&matrix_b->ts));
3123 return false;
3126 switch (matrix_a->rank)
3128 case 1:
3129 if (!rank_check (matrix_b, 1, 2))
3130 return false;
3131 /* Check for case matrix_a has shape(m), matrix_b has shape (m, k). */
3132 if (!identical_dimen_shape (matrix_a, 0, matrix_b, 0))
3134 gfc_error ("Different shape on dimension 1 for arguments %qs "
3135 "and %qs at %L for intrinsic matmul",
3136 gfc_current_intrinsic_arg[0]->name,
3137 gfc_current_intrinsic_arg[1]->name, &matrix_a->where);
3138 return false;
3140 break;
3142 case 2:
3143 if (matrix_b->rank != 2)
3145 if (!rank_check (matrix_b, 1, 1))
3146 return false;
3148 /* matrix_b has rank 1 or 2 here. Common check for the cases
3149 - matrix_a has shape (n,m) and matrix_b has shape (m, k)
3150 - matrix_a has shape (n,m) and matrix_b has shape (m). */
3151 if (!identical_dimen_shape (matrix_a, 1, matrix_b, 0))
3153 gfc_error ("Different shape on dimension 2 for argument %qs and "
3154 "dimension 1 for argument %qs at %L for intrinsic "
3155 "matmul", gfc_current_intrinsic_arg[0]->name,
3156 gfc_current_intrinsic_arg[1]->name, &matrix_a->where);
3157 return false;
3159 break;
3161 default:
3162 gfc_error ("%qs argument of %qs intrinsic at %L must be of rank "
3163 "1 or 2", gfc_current_intrinsic_arg[0]->name,
3164 gfc_current_intrinsic, &matrix_a->where);
3165 return false;
3168 return true;
3172 /* Whoever came up with this interface was probably on something.
3173 The possibilities for the occupation of the second and third
3174 parameters are:
3176 Arg #2 Arg #3
3177 NULL NULL
3178 DIM NULL
3179 MASK NULL
3180 NULL MASK minloc(array, mask=m)
3181 DIM MASK
3183 I.e. in the case of minloc(array,mask), mask will be in the second
3184 position of the argument list and we'll have to fix that up. */
3186 bool
3187 gfc_check_minloc_maxloc (gfc_actual_arglist *ap)
3189 gfc_expr *a, *m, *d, *k;
3191 a = ap->expr;
3192 if (!int_or_real_check (a, 0) || !array_check (a, 0))
3193 return false;
3195 d = ap->next->expr;
3196 m = ap->next->next->expr;
3197 k = ap->next->next->next->expr;
3199 if (m == NULL && d != NULL && d->ts.type == BT_LOGICAL
3200 && ap->next->name == NULL)
3202 m = d;
3203 d = NULL;
3204 ap->next->expr = NULL;
3205 ap->next->next->expr = m;
3208 if (!dim_check (d, 1, false))
3209 return false;
3211 if (!dim_rank_check (d, a, 0))
3212 return false;
3214 if (m != NULL && !type_check (m, 2, BT_LOGICAL))
3215 return false;
3217 if (m != NULL
3218 && !gfc_check_conformance (a, m,
3219 "arguments '%s' and '%s' for intrinsic %s",
3220 gfc_current_intrinsic_arg[0]->name,
3221 gfc_current_intrinsic_arg[2]->name,
3222 gfc_current_intrinsic))
3223 return false;
3225 if (!kind_check (k, 1, BT_INTEGER))
3226 return false;
3228 return true;
3232 /* Similar to minloc/maxloc, the argument list might need to be
3233 reordered for the MINVAL, MAXVAL, PRODUCT, and SUM intrinsics. The
3234 difference is that MINLOC/MAXLOC take an additional KIND argument.
3235 The possibilities are:
3237 Arg #2 Arg #3
3238 NULL NULL
3239 DIM NULL
3240 MASK NULL
3241 NULL MASK minval(array, mask=m)
3242 DIM MASK
3244 I.e. in the case of minval(array,mask), mask will be in the second
3245 position of the argument list and we'll have to fix that up. */
3247 static bool
3248 check_reduction (gfc_actual_arglist *ap)
3250 gfc_expr *a, *m, *d;
3252 a = ap->expr;
3253 d = ap->next->expr;
3254 m = ap->next->next->expr;
3256 if (m == NULL && d != NULL && d->ts.type == BT_LOGICAL
3257 && ap->next->name == NULL)
3259 m = d;
3260 d = NULL;
3261 ap->next->expr = NULL;
3262 ap->next->next->expr = m;
3265 if (!dim_check (d, 1, false))
3266 return false;
3268 if (!dim_rank_check (d, a, 0))
3269 return false;
3271 if (m != NULL && !type_check (m, 2, BT_LOGICAL))
3272 return false;
3274 if (m != NULL
3275 && !gfc_check_conformance (a, m,
3276 "arguments '%s' and '%s' for intrinsic %s",
3277 gfc_current_intrinsic_arg[0]->name,
3278 gfc_current_intrinsic_arg[2]->name,
3279 gfc_current_intrinsic))
3280 return false;
3282 return true;
3286 bool
3287 gfc_check_minval_maxval (gfc_actual_arglist *ap)
3289 if (!int_or_real_check (ap->expr, 0)
3290 || !array_check (ap->expr, 0))
3291 return false;
3293 return check_reduction (ap);
3297 bool
3298 gfc_check_product_sum (gfc_actual_arglist *ap)
3300 if (!numeric_check (ap->expr, 0)
3301 || !array_check (ap->expr, 0))
3302 return false;
3304 return check_reduction (ap);
3308 /* For IANY, IALL and IPARITY. */
3310 bool
3311 gfc_check_mask (gfc_expr *i, gfc_expr *kind)
3313 int k;
3315 if (!type_check (i, 0, BT_INTEGER))
3316 return false;
3318 if (!nonnegative_check ("I", i))
3319 return false;
3321 if (!kind_check (kind, 1, BT_INTEGER))
3322 return false;
3324 if (kind)
3325 gfc_extract_int (kind, &k);
3326 else
3327 k = gfc_default_integer_kind;
3329 if (!less_than_bitsizekind ("I", i, k))
3330 return false;
3332 return true;
3336 bool
3337 gfc_check_transf_bit_intrins (gfc_actual_arglist *ap)
3339 if (ap->expr->ts.type != BT_INTEGER)
3341 gfc_error ("%qs argument of %qs intrinsic at %L must be INTEGER",
3342 gfc_current_intrinsic_arg[0]->name,
3343 gfc_current_intrinsic, &ap->expr->where);
3344 return false;
3347 if (!array_check (ap->expr, 0))
3348 return false;
3350 return check_reduction (ap);
3354 bool
3355 gfc_check_merge (gfc_expr *tsource, gfc_expr *fsource, gfc_expr *mask)
3357 if (!same_type_check (tsource, 0, fsource, 1))
3358 return false;
3360 if (!type_check (mask, 2, BT_LOGICAL))
3361 return false;
3363 if (tsource->ts.type == BT_CHARACTER)
3364 return gfc_check_same_strlen (tsource, fsource, "MERGE intrinsic");
3366 return true;
3370 bool
3371 gfc_check_merge_bits (gfc_expr *i, gfc_expr *j, gfc_expr *mask)
3373 if (!type_check (i, 0, BT_INTEGER))
3374 return false;
3376 if (!type_check (j, 1, BT_INTEGER))
3377 return false;
3379 if (!type_check (mask, 2, BT_INTEGER))
3380 return false;
3382 if (!same_type_check (i, 0, j, 1))
3383 return false;
3385 if (!same_type_check (i, 0, mask, 2))
3386 return false;
3388 return true;
3392 bool
3393 gfc_check_move_alloc (gfc_expr *from, gfc_expr *to)
3395 if (!variable_check (from, 0, false))
3396 return false;
3397 if (!allocatable_check (from, 0))
3398 return false;
3399 if (gfc_is_coindexed (from))
3401 gfc_error ("The FROM argument to MOVE_ALLOC at %L shall not be "
3402 "coindexed", &from->where);
3403 return false;
3406 if (!variable_check (to, 1, false))
3407 return false;
3408 if (!allocatable_check (to, 1))
3409 return false;
3410 if (gfc_is_coindexed (to))
3412 gfc_error ("The TO argument to MOVE_ALLOC at %L shall not be "
3413 "coindexed", &to->where);
3414 return false;
3417 if (from->ts.type == BT_CLASS && to->ts.type == BT_DERIVED)
3419 gfc_error ("The TO arguments in MOVE_ALLOC at %L must be "
3420 "polymorphic if FROM is polymorphic",
3421 &to->where);
3422 return false;
3425 if (!same_type_check (to, 1, from, 0))
3426 return false;
3428 if (to->rank != from->rank)
3430 gfc_error ("The FROM and TO arguments of the MOVE_ALLOC intrinsic at %L "
3431 "must have the same rank %d/%d", &to->where, from->rank,
3432 to->rank);
3433 return false;
3436 /* IR F08/0040; cf. 12-006A. */
3437 if (gfc_get_corank (to) != gfc_get_corank (from))
3439 gfc_error ("The FROM and TO arguments of the MOVE_ALLOC intrinsic at %L "
3440 "must have the same corank %d/%d", &to->where,
3441 gfc_get_corank (from), gfc_get_corank (to));
3442 return false;
3445 /* This is based losely on F2003 12.4.1.7. It is intended to prevent
3446 the likes of to = sym->cmp1->cmp2 and from = sym->cmp1, where cmp1
3447 and cmp2 are allocatable. After the allocation is transferred,
3448 the 'to' chain is broken by the nullification of the 'from'. A bit
3449 of reflection reveals that this can only occur for derived types
3450 with recursive allocatable components. */
3451 if (to->expr_type == EXPR_VARIABLE && from->expr_type == EXPR_VARIABLE
3452 && !strcmp (to->symtree->n.sym->name, from->symtree->n.sym->name))
3454 gfc_ref *to_ref, *from_ref;
3455 to_ref = to->ref;
3456 from_ref = from->ref;
3457 bool aliasing = true;
3459 for (; from_ref && to_ref;
3460 from_ref = from_ref->next, to_ref = to_ref->next)
3462 if (to_ref->type != from->ref->type)
3463 aliasing = false;
3464 else if (to_ref->type == REF_ARRAY
3465 && to_ref->u.ar.type != AR_FULL
3466 && from_ref->u.ar.type != AR_FULL)
3467 /* Play safe; assume sections and elements are different. */
3468 aliasing = false;
3469 else if (to_ref->type == REF_COMPONENT
3470 && to_ref->u.c.component != from_ref->u.c.component)
3471 aliasing = false;
3473 if (!aliasing)
3474 break;
3477 if (aliasing)
3479 gfc_error ("The FROM and TO arguments at %L violate aliasing "
3480 "restrictions (F2003 12.4.1.7)", &to->where);
3481 return false;
3485 /* CLASS arguments: Make sure the vtab of from is present. */
3486 if (to->ts.type == BT_CLASS && !UNLIMITED_POLY (from))
3487 gfc_find_vtab (&from->ts);
3489 return true;
3493 bool
3494 gfc_check_nearest (gfc_expr *x, gfc_expr *s)
3496 if (!type_check (x, 0, BT_REAL))
3497 return false;
3499 if (!type_check (s, 1, BT_REAL))
3500 return false;
3502 if (s->expr_type == EXPR_CONSTANT)
3504 if (mpfr_sgn (s->value.real) == 0)
3506 gfc_error ("Argument %<S%> of NEAREST at %L shall not be zero",
3507 &s->where);
3508 return false;
3512 return true;
3516 bool
3517 gfc_check_new_line (gfc_expr *a)
3519 if (!type_check (a, 0, BT_CHARACTER))
3520 return false;
3522 return true;
3526 bool
3527 gfc_check_norm2 (gfc_expr *array, gfc_expr *dim)
3529 if (!type_check (array, 0, BT_REAL))
3530 return false;
3532 if (!array_check (array, 0))
3533 return false;
3535 if (!dim_rank_check (dim, array, false))
3536 return false;
3538 return true;
3541 bool
3542 gfc_check_null (gfc_expr *mold)
3544 symbol_attribute attr;
3546 if (mold == NULL)
3547 return true;
3549 if (!variable_check (mold, 0, true))
3550 return false;
3552 attr = gfc_variable_attr (mold, NULL);
3554 if (!attr.pointer && !attr.proc_pointer && !attr.allocatable)
3556 gfc_error ("%qs argument of %qs intrinsic at %L must be a POINTER, "
3557 "ALLOCATABLE or procedure pointer",
3558 gfc_current_intrinsic_arg[0]->name,
3559 gfc_current_intrinsic, &mold->where);
3560 return false;
3563 if (attr.allocatable
3564 && !gfc_notify_std (GFC_STD_F2003, "NULL intrinsic with "
3565 "allocatable MOLD at %L", &mold->where))
3566 return false;
3568 /* F2008, C1242. */
3569 if (gfc_is_coindexed (mold))
3571 gfc_error ("%qs argument of %qs intrinsic at %L shall not be "
3572 "coindexed", gfc_current_intrinsic_arg[0]->name,
3573 gfc_current_intrinsic, &mold->where);
3574 return false;
3577 return true;
3581 bool
3582 gfc_check_pack (gfc_expr *array, gfc_expr *mask, gfc_expr *vector)
3584 if (!array_check (array, 0))
3585 return false;
3587 if (!type_check (mask, 1, BT_LOGICAL))
3588 return false;
3590 if (!gfc_check_conformance (array, mask,
3591 "arguments '%s' and '%s' for intrinsic '%s'",
3592 gfc_current_intrinsic_arg[0]->name,
3593 gfc_current_intrinsic_arg[1]->name,
3594 gfc_current_intrinsic))
3595 return false;
3597 if (vector != NULL)
3599 mpz_t array_size, vector_size;
3600 bool have_array_size, have_vector_size;
3602 if (!same_type_check (array, 0, vector, 2))
3603 return false;
3605 if (!rank_check (vector, 2, 1))
3606 return false;
3608 /* VECTOR requires at least as many elements as MASK
3609 has .TRUE. values. */
3610 have_array_size = gfc_array_size(array, &array_size);
3611 have_vector_size = gfc_array_size(vector, &vector_size);
3613 if (have_vector_size
3614 && (mask->expr_type == EXPR_ARRAY
3615 || (mask->expr_type == EXPR_CONSTANT
3616 && have_array_size)))
3618 int mask_true_values = 0;
3620 if (mask->expr_type == EXPR_ARRAY)
3622 gfc_constructor *mask_ctor;
3623 mask_ctor = gfc_constructor_first (mask->value.constructor);
3624 while (mask_ctor)
3626 if (mask_ctor->expr->expr_type != EXPR_CONSTANT)
3628 mask_true_values = 0;
3629 break;
3632 if (mask_ctor->expr->value.logical)
3633 mask_true_values++;
3635 mask_ctor = gfc_constructor_next (mask_ctor);
3638 else if (mask->expr_type == EXPR_CONSTANT && mask->value.logical)
3639 mask_true_values = mpz_get_si (array_size);
3641 if (mpz_get_si (vector_size) < mask_true_values)
3643 gfc_error ("%qs argument of %qs intrinsic at %L must "
3644 "provide at least as many elements as there "
3645 "are .TRUE. values in %qs (%ld/%d)",
3646 gfc_current_intrinsic_arg[2]->name,
3647 gfc_current_intrinsic, &vector->where,
3648 gfc_current_intrinsic_arg[1]->name,
3649 mpz_get_si (vector_size), mask_true_values);
3650 return false;
3654 if (have_array_size)
3655 mpz_clear (array_size);
3656 if (have_vector_size)
3657 mpz_clear (vector_size);
3660 return true;
3664 bool
3665 gfc_check_parity (gfc_expr *mask, gfc_expr *dim)
3667 if (!type_check (mask, 0, BT_LOGICAL))
3668 return false;
3670 if (!array_check (mask, 0))
3671 return false;
3673 if (!dim_rank_check (dim, mask, false))
3674 return false;
3676 return true;
3680 bool
3681 gfc_check_precision (gfc_expr *x)
3683 if (!real_or_complex_check (x, 0))
3684 return false;
3686 return true;
3690 bool
3691 gfc_check_present (gfc_expr *a)
3693 gfc_symbol *sym;
3695 if (!variable_check (a, 0, true))
3696 return false;
3698 sym = a->symtree->n.sym;
3699 if (!sym->attr.dummy)
3701 gfc_error ("%qs argument of %qs intrinsic at %L must be of a "
3702 "dummy variable", gfc_current_intrinsic_arg[0]->name,
3703 gfc_current_intrinsic, &a->where);
3704 return false;
3707 if (!sym->attr.optional)
3709 gfc_error ("%qs argument of %qs intrinsic at %L must be of "
3710 "an OPTIONAL dummy variable",
3711 gfc_current_intrinsic_arg[0]->name, gfc_current_intrinsic,
3712 &a->where);
3713 return false;
3716 /* 13.14.82 PRESENT(A)
3717 ......
3718 Argument. A shall be the name of an optional dummy argument that is
3719 accessible in the subprogram in which the PRESENT function reference
3720 appears... */
3722 if (a->ref != NULL
3723 && !(a->ref->next == NULL && a->ref->type == REF_ARRAY
3724 && (a->ref->u.ar.type == AR_FULL
3725 || (a->ref->u.ar.type == AR_ELEMENT
3726 && a->ref->u.ar.as->rank == 0))))
3728 gfc_error ("%qs argument of %qs intrinsic at %L must not be a "
3729 "subobject of %qs", gfc_current_intrinsic_arg[0]->name,
3730 gfc_current_intrinsic, &a->where, sym->name);
3731 return false;
3734 return true;
3738 bool
3739 gfc_check_radix (gfc_expr *x)
3741 if (!int_or_real_check (x, 0))
3742 return false;
3744 return true;
3748 bool
3749 gfc_check_range (gfc_expr *x)
3751 if (!numeric_check (x, 0))
3752 return false;
3754 return true;
3758 bool
3759 gfc_check_rank (gfc_expr *a)
3761 /* Any data object is allowed; a "data object" is a "constant (4.1.3),
3762 variable (6), or subobject of a constant (2.4.3.2.3)" (F2008, 1.3.45). */
3764 bool is_variable = true;
3766 /* Functions returning pointers are regarded as variable, cf. F2008, R602. */
3767 if (a->expr_type == EXPR_FUNCTION)
3768 is_variable = a->value.function.esym
3769 ? a->value.function.esym->result->attr.pointer
3770 : a->symtree->n.sym->result->attr.pointer;
3772 if (a->expr_type == EXPR_OP || a->expr_type == EXPR_NULL
3773 || a->expr_type == EXPR_COMPCALL|| a->expr_type == EXPR_PPC
3774 || !is_variable)
3776 gfc_error ("The argument of the RANK intrinsic at %L must be a data "
3777 "object", &a->where);
3778 return false;
3781 return true;
3785 /* real, float, sngl. */
3786 bool
3787 gfc_check_real (gfc_expr *a, gfc_expr *kind)
3789 if (!numeric_check (a, 0))
3790 return false;
3792 if (!kind_check (kind, 1, BT_REAL))
3793 return false;
3795 return true;
3799 bool
3800 gfc_check_rename (gfc_expr *path1, gfc_expr *path2)
3802 if (!type_check (path1, 0, BT_CHARACTER))
3803 return false;
3804 if (!kind_value_check (path1, 0, gfc_default_character_kind))
3805 return false;
3807 if (!type_check (path2, 1, BT_CHARACTER))
3808 return false;
3809 if (!kind_value_check (path2, 1, gfc_default_character_kind))
3810 return false;
3812 return true;
3816 bool
3817 gfc_check_rename_sub (gfc_expr *path1, gfc_expr *path2, gfc_expr *status)
3819 if (!type_check (path1, 0, BT_CHARACTER))
3820 return false;
3821 if (!kind_value_check (path1, 0, gfc_default_character_kind))
3822 return false;
3824 if (!type_check (path2, 1, BT_CHARACTER))
3825 return false;
3826 if (!kind_value_check (path2, 1, gfc_default_character_kind))
3827 return false;
3829 if (status == NULL)
3830 return true;
3832 if (!type_check (status, 2, BT_INTEGER))
3833 return false;
3835 if (!scalar_check (status, 2))
3836 return false;
3838 return true;
3842 bool
3843 gfc_check_repeat (gfc_expr *x, gfc_expr *y)
3845 if (!type_check (x, 0, BT_CHARACTER))
3846 return false;
3848 if (!scalar_check (x, 0))
3849 return false;
3851 if (!type_check (y, 0, BT_INTEGER))
3852 return false;
3854 if (!scalar_check (y, 1))
3855 return false;
3857 return true;
3861 bool
3862 gfc_check_reshape (gfc_expr *source, gfc_expr *shape,
3863 gfc_expr *pad, gfc_expr *order)
3865 mpz_t size;
3866 mpz_t nelems;
3867 int shape_size;
3869 if (!array_check (source, 0))
3870 return false;
3872 if (!rank_check (shape, 1, 1))
3873 return false;
3875 if (!type_check (shape, 1, BT_INTEGER))
3876 return false;
3878 if (!gfc_array_size (shape, &size))
3880 gfc_error ("%<shape%> argument of %<reshape%> intrinsic at %L must be an "
3881 "array of constant size", &shape->where);
3882 return false;
3885 shape_size = mpz_get_ui (size);
3886 mpz_clear (size);
3888 if (shape_size <= 0)
3890 gfc_error ("%qs argument of %qs intrinsic at %L is empty",
3891 gfc_current_intrinsic_arg[1]->name, gfc_current_intrinsic,
3892 &shape->where);
3893 return false;
3895 else if (shape_size > GFC_MAX_DIMENSIONS)
3897 gfc_error ("%<shape%> argument of %<reshape%> intrinsic at %L has more "
3898 "than %d elements", &shape->where, GFC_MAX_DIMENSIONS);
3899 return false;
3901 else if (shape->expr_type == EXPR_ARRAY && gfc_is_constant_expr (shape))
3903 gfc_expr *e;
3904 int i, extent;
3905 for (i = 0; i < shape_size; ++i)
3907 e = gfc_constructor_lookup_expr (shape->value.constructor, i);
3908 if (e->expr_type != EXPR_CONSTANT)
3909 continue;
3911 gfc_extract_int (e, &extent);
3912 if (extent < 0)
3914 gfc_error ("%qs argument of %qs intrinsic at %L has "
3915 "negative element (%d)",
3916 gfc_current_intrinsic_arg[1]->name,
3917 gfc_current_intrinsic, &e->where, extent);
3918 return false;
3922 else if (shape->expr_type == EXPR_VARIABLE && shape->ref
3923 && shape->ref->u.ar.type == AR_FULL && shape->ref->u.ar.dimen == 1
3924 && shape->ref->u.ar.as
3925 && shape->ref->u.ar.as->lower[0]->expr_type == EXPR_CONSTANT
3926 && shape->ref->u.ar.as->lower[0]->ts.type == BT_INTEGER
3927 && shape->ref->u.ar.as->upper[0]->expr_type == EXPR_CONSTANT
3928 && shape->ref->u.ar.as->upper[0]->ts.type == BT_INTEGER
3929 && shape->symtree->n.sym->attr.flavor == FL_PARAMETER)
3931 int i, extent;
3932 gfc_expr *e, *v;
3934 v = shape->symtree->n.sym->value;
3936 for (i = 0; i < shape_size; i++)
3938 e = gfc_constructor_lookup_expr (v->value.constructor, i);
3939 if (e == NULL)
3940 break;
3942 gfc_extract_int (e, &extent);
3944 if (extent < 0)
3946 gfc_error ("Element %d of actual argument of RESHAPE at %L "
3947 "cannot be negative", i + 1, &shape->where);
3948 return false;
3953 if (pad != NULL)
3955 if (!same_type_check (source, 0, pad, 2))
3956 return false;
3958 if (!array_check (pad, 2))
3959 return false;
3962 if (order != NULL)
3964 if (!array_check (order, 3))
3965 return false;
3967 if (!type_check (order, 3, BT_INTEGER))
3968 return false;
3970 if (order->expr_type == EXPR_ARRAY && gfc_is_constant_expr (order))
3972 int i, order_size, dim, perm[GFC_MAX_DIMENSIONS];
3973 gfc_expr *e;
3975 for (i = 0; i < GFC_MAX_DIMENSIONS; ++i)
3976 perm[i] = 0;
3978 gfc_array_size (order, &size);
3979 order_size = mpz_get_ui (size);
3980 mpz_clear (size);
3982 if (order_size != shape_size)
3984 gfc_error ("%qs argument of %qs intrinsic at %L "
3985 "has wrong number of elements (%d/%d)",
3986 gfc_current_intrinsic_arg[3]->name,
3987 gfc_current_intrinsic, &order->where,
3988 order_size, shape_size);
3989 return false;
3992 for (i = 1; i <= order_size; ++i)
3994 e = gfc_constructor_lookup_expr (order->value.constructor, i-1);
3995 if (e->expr_type != EXPR_CONSTANT)
3996 continue;
3998 gfc_extract_int (e, &dim);
4000 if (dim < 1 || dim > order_size)
4002 gfc_error ("%qs argument of %qs intrinsic at %L "
4003 "has out-of-range dimension (%d)",
4004 gfc_current_intrinsic_arg[3]->name,
4005 gfc_current_intrinsic, &e->where, dim);
4006 return false;
4009 if (perm[dim-1] != 0)
4011 gfc_error ("%qs argument of %qs intrinsic at %L has "
4012 "invalid permutation of dimensions (dimension "
4013 "%qd duplicated)",
4014 gfc_current_intrinsic_arg[3]->name,
4015 gfc_current_intrinsic, &e->where, dim);
4016 return false;
4019 perm[dim-1] = 1;
4024 if (pad == NULL && shape->expr_type == EXPR_ARRAY
4025 && gfc_is_constant_expr (shape)
4026 && !(source->expr_type == EXPR_VARIABLE && source->symtree->n.sym->as
4027 && source->symtree->n.sym->as->type == AS_ASSUMED_SIZE))
4029 /* Check the match in size between source and destination. */
4030 if (gfc_array_size (source, &nelems))
4032 gfc_constructor *c;
4033 bool test;
4036 mpz_init_set_ui (size, 1);
4037 for (c = gfc_constructor_first (shape->value.constructor);
4038 c; c = gfc_constructor_next (c))
4039 mpz_mul (size, size, c->expr->value.integer);
4041 test = mpz_cmp (nelems, size) < 0 && mpz_cmp_ui (size, 0) > 0;
4042 mpz_clear (nelems);
4043 mpz_clear (size);
4045 if (test)
4047 gfc_error ("Without padding, there are not enough elements "
4048 "in the intrinsic RESHAPE source at %L to match "
4049 "the shape", &source->where);
4050 return false;
4055 return true;
4059 bool
4060 gfc_check_same_type_as (gfc_expr *a, gfc_expr *b)
4062 if (a->ts.type != BT_DERIVED && a->ts.type != BT_CLASS)
4064 gfc_error ("%qs argument of %qs intrinsic at %L "
4065 "cannot be of type %s",
4066 gfc_current_intrinsic_arg[0]->name,
4067 gfc_current_intrinsic,
4068 &a->where, gfc_typename (&a->ts));
4069 return false;
4072 if (!(gfc_type_is_extensible (a->ts.u.derived) || UNLIMITED_POLY (a)))
4074 gfc_error ("%qs argument of %qs intrinsic at %L "
4075 "must be of an extensible type",
4076 gfc_current_intrinsic_arg[0]->name, gfc_current_intrinsic,
4077 &a->where);
4078 return false;
4081 if (b->ts.type != BT_DERIVED && b->ts.type != BT_CLASS)
4083 gfc_error ("%qs argument of %qs intrinsic at %L "
4084 "cannot be of type %s",
4085 gfc_current_intrinsic_arg[0]->name,
4086 gfc_current_intrinsic,
4087 &b->where, gfc_typename (&b->ts));
4088 return false;
4091 if (!(gfc_type_is_extensible (b->ts.u.derived) || UNLIMITED_POLY (b)))
4093 gfc_error ("%qs argument of %qs intrinsic at %L "
4094 "must be of an extensible type",
4095 gfc_current_intrinsic_arg[1]->name, gfc_current_intrinsic,
4096 &b->where);
4097 return false;
4100 return true;
4104 bool
4105 gfc_check_scale (gfc_expr *x, gfc_expr *i)
4107 if (!type_check (x, 0, BT_REAL))
4108 return false;
4110 if (!type_check (i, 1, BT_INTEGER))
4111 return false;
4113 return true;
4117 bool
4118 gfc_check_scan (gfc_expr *x, gfc_expr *y, gfc_expr *z, gfc_expr *kind)
4120 if (!type_check (x, 0, BT_CHARACTER))
4121 return false;
4123 if (!type_check (y, 1, BT_CHARACTER))
4124 return false;
4126 if (z != NULL && !type_check (z, 2, BT_LOGICAL))
4127 return false;
4129 if (!kind_check (kind, 3, BT_INTEGER))
4130 return false;
4131 if (kind && !gfc_notify_std (GFC_STD_F2003, "%qs intrinsic "
4132 "with KIND argument at %L",
4133 gfc_current_intrinsic, &kind->where))
4134 return false;
4136 if (!same_type_check (x, 0, y, 1))
4137 return false;
4139 return true;
4143 bool
4144 gfc_check_secnds (gfc_expr *r)
4146 if (!type_check (r, 0, BT_REAL))
4147 return false;
4149 if (!kind_value_check (r, 0, 4))
4150 return false;
4152 if (!scalar_check (r, 0))
4153 return false;
4155 return true;
4159 bool
4160 gfc_check_selected_char_kind (gfc_expr *name)
4162 if (!type_check (name, 0, BT_CHARACTER))
4163 return false;
4165 if (!kind_value_check (name, 0, gfc_default_character_kind))
4166 return false;
4168 if (!scalar_check (name, 0))
4169 return false;
4171 return true;
4175 bool
4176 gfc_check_selected_int_kind (gfc_expr *r)
4178 if (!type_check (r, 0, BT_INTEGER))
4179 return false;
4181 if (!scalar_check (r, 0))
4182 return false;
4184 return true;
4188 bool
4189 gfc_check_selected_real_kind (gfc_expr *p, gfc_expr *r, gfc_expr *radix)
4191 if (p == NULL && r == NULL
4192 && !gfc_notify_std (GFC_STD_F2008, "SELECTED_REAL_KIND with"
4193 " neither %<P%> nor %<R%> argument at %L",
4194 gfc_current_intrinsic_where))
4195 return false;
4197 if (p)
4199 if (!type_check (p, 0, BT_INTEGER))
4200 return false;
4202 if (!scalar_check (p, 0))
4203 return false;
4206 if (r)
4208 if (!type_check (r, 1, BT_INTEGER))
4209 return false;
4211 if (!scalar_check (r, 1))
4212 return false;
4215 if (radix)
4217 if (!type_check (radix, 1, BT_INTEGER))
4218 return false;
4220 if (!scalar_check (radix, 1))
4221 return false;
4223 if (!gfc_notify_std (GFC_STD_F2008, "%qs intrinsic with "
4224 "RADIX argument at %L", gfc_current_intrinsic,
4225 &radix->where))
4226 return false;
4229 return true;
4233 bool
4234 gfc_check_set_exponent (gfc_expr *x, gfc_expr *i)
4236 if (!type_check (x, 0, BT_REAL))
4237 return false;
4239 if (!type_check (i, 1, BT_INTEGER))
4240 return false;
4242 return true;
4246 bool
4247 gfc_check_shape (gfc_expr *source, gfc_expr *kind)
4249 gfc_array_ref *ar;
4251 if (source->rank == 0 || source->expr_type != EXPR_VARIABLE)
4252 return true;
4254 ar = gfc_find_array_ref (source);
4256 if (ar->as && ar->as->type == AS_ASSUMED_SIZE && ar->type == AR_FULL)
4258 gfc_error ("%<source%> argument of %<shape%> intrinsic at %L must not be "
4259 "an assumed size array", &source->where);
4260 return false;
4263 if (!kind_check (kind, 1, BT_INTEGER))
4264 return false;
4265 if (kind && !gfc_notify_std (GFC_STD_F2003, "%qs intrinsic "
4266 "with KIND argument at %L",
4267 gfc_current_intrinsic, &kind->where))
4268 return false;
4270 return true;
4274 bool
4275 gfc_check_shift (gfc_expr *i, gfc_expr *shift)
4277 if (!type_check (i, 0, BT_INTEGER))
4278 return false;
4280 if (!type_check (shift, 0, BT_INTEGER))
4281 return false;
4283 if (!nonnegative_check ("SHIFT", shift))
4284 return false;
4286 if (!less_than_bitsize1 ("I", i, "SHIFT", shift, true))
4287 return false;
4289 return true;
4293 bool
4294 gfc_check_sign (gfc_expr *a, gfc_expr *b)
4296 if (!int_or_real_check (a, 0))
4297 return false;
4299 if (!same_type_check (a, 0, b, 1))
4300 return false;
4302 return true;
4306 bool
4307 gfc_check_size (gfc_expr *array, gfc_expr *dim, gfc_expr *kind)
4309 if (!array_check (array, 0))
4310 return false;
4312 if (!dim_check (dim, 1, true))
4313 return false;
4315 if (!dim_rank_check (dim, array, 0))
4316 return false;
4318 if (!kind_check (kind, 2, BT_INTEGER))
4319 return false;
4320 if (kind && !gfc_notify_std (GFC_STD_F2003, "%qs intrinsic "
4321 "with KIND argument at %L",
4322 gfc_current_intrinsic, &kind->where))
4323 return false;
4326 return true;
4330 bool
4331 gfc_check_sizeof (gfc_expr *arg)
4333 if (arg->ts.type == BT_PROCEDURE)
4335 gfc_error ("%qs argument of %qs intrinsic at %L shall not be a procedure",
4336 gfc_current_intrinsic_arg[0]->name, gfc_current_intrinsic,
4337 &arg->where);
4338 return false;
4341 /* TYPE(*) is acceptable if and only if it uses an array descriptor. */
4342 if (arg->ts.type == BT_ASSUMED
4343 && (arg->symtree->n.sym->as == NULL
4344 || (arg->symtree->n.sym->as->type != AS_ASSUMED_SHAPE
4345 && arg->symtree->n.sym->as->type != AS_DEFERRED
4346 && arg->symtree->n.sym->as->type != AS_ASSUMED_RANK)))
4348 gfc_error ("%qs argument of %qs intrinsic at %L shall not be TYPE(*)",
4349 gfc_current_intrinsic_arg[0]->name, gfc_current_intrinsic,
4350 &arg->where);
4351 return false;
4354 if (arg->rank && arg->expr_type == EXPR_VARIABLE
4355 && arg->symtree->n.sym->as != NULL
4356 && arg->symtree->n.sym->as->type == AS_ASSUMED_SIZE && arg->ref
4357 && arg->ref->type == REF_ARRAY && arg->ref->u.ar.type == AR_FULL)
4359 gfc_error ("%qs argument of %qs intrinsic at %L shall not be an "
4360 "assumed-size array", gfc_current_intrinsic_arg[0]->name,
4361 gfc_current_intrinsic, &arg->where);
4362 return false;
4365 return true;
4369 /* Check whether an expression is interoperable. When returning false,
4370 msg is set to a string telling why the expression is not interoperable,
4371 otherwise, it is set to NULL. The msg string can be used in diagnostics.
4372 If c_loc is true, character with len > 1 are allowed (cf. Fortran
4373 2003corr5); additionally, assumed-shape/assumed-rank/deferred-shape
4374 arrays are permitted. And if c_f_ptr is true, deferred-shape arrays
4375 are permitted. */
4377 static bool
4378 is_c_interoperable (gfc_expr *expr, const char **msg, bool c_loc, bool c_f_ptr)
4380 *msg = NULL;
4382 if (expr->ts.type == BT_CLASS)
4384 *msg = "Expression is polymorphic";
4385 return false;
4388 if (expr->ts.type == BT_DERIVED && !expr->ts.u.derived->attr.is_bind_c
4389 && !expr->ts.u.derived->ts.is_iso_c)
4391 *msg = "Expression is a noninteroperable derived type";
4392 return false;
4395 if (expr->ts.type == BT_PROCEDURE)
4397 *msg = "Procedure unexpected as argument";
4398 return false;
4401 if (gfc_notification_std (GFC_STD_GNU) && expr->ts.type == BT_LOGICAL)
4403 int i;
4404 for (i = 0; gfc_logical_kinds[i].kind; i++)
4405 if (gfc_logical_kinds[i].kind == expr->ts.kind)
4406 return true;
4407 *msg = "Extension to use a non-C_Bool-kind LOGICAL";
4408 return false;
4411 if (gfc_notification_std (GFC_STD_GNU) && expr->ts.type == BT_CHARACTER
4412 && expr->ts.kind != 1)
4414 *msg = "Extension to use a non-C_CHAR-kind CHARACTER";
4415 return false;
4418 if (expr->ts.type == BT_CHARACTER) {
4419 if (expr->ts.deferred)
4421 /* TS 29113 allows deferred-length strings as dummy arguments,
4422 but it is not an interoperable type. */
4423 *msg = "Expression shall not be a deferred-length string";
4424 return false;
4427 if (expr->ts.u.cl && expr->ts.u.cl->length
4428 && !gfc_simplify_expr (expr->ts.u.cl->length, 0))
4429 gfc_internal_error ("is_c_interoperable(): gfc_simplify_expr failed");
4431 if (!c_loc && expr->ts.u.cl
4432 && (!expr->ts.u.cl->length
4433 || expr->ts.u.cl->length->expr_type != EXPR_CONSTANT
4434 || mpz_cmp_si (expr->ts.u.cl->length->value.integer, 1) != 0))
4436 *msg = "Type shall have a character length of 1";
4437 return false;
4441 /* Note: The following checks are about interoperatable variables, Fortran
4442 15.3.5/15.3.6. In intrinsics like C_LOC or in procedure interface, more
4443 is allowed, e.g. assumed-shape arrays with TS 29113. */
4445 if (gfc_is_coarray (expr))
4447 *msg = "Coarrays are not interoperable";
4448 return false;
4451 if (!c_loc && expr->rank > 0 && expr->expr_type != EXPR_ARRAY)
4453 gfc_array_ref *ar = gfc_find_array_ref (expr);
4454 if (ar->type != AR_FULL)
4456 *msg = "Only whole-arrays are interoperable";
4457 return false;
4459 if (!c_f_ptr && ar->as->type != AS_EXPLICIT
4460 && ar->as->type != AS_ASSUMED_SIZE)
4462 *msg = "Only explicit-size and assumed-size arrays are interoperable";
4463 return false;
4467 return true;
4471 bool
4472 gfc_check_c_sizeof (gfc_expr *arg)
4474 const char *msg;
4476 if (!is_c_interoperable (arg, &msg, false, false))
4478 gfc_error ("%qs argument of %qs intrinsic at %L must be an "
4479 "interoperable data entity: %s",
4480 gfc_current_intrinsic_arg[0]->name, gfc_current_intrinsic,
4481 &arg->where, msg);
4482 return false;
4485 if (arg->ts.type == BT_ASSUMED)
4487 gfc_error ("%qs argument of %qs intrinsic at %L shall not be "
4488 "TYPE(*)",
4489 gfc_current_intrinsic_arg[0]->name, gfc_current_intrinsic,
4490 &arg->where);
4491 return false;
4494 if (arg->rank && arg->expr_type == EXPR_VARIABLE
4495 && arg->symtree->n.sym->as != NULL
4496 && arg->symtree->n.sym->as->type == AS_ASSUMED_SIZE && arg->ref
4497 && arg->ref->type == REF_ARRAY && arg->ref->u.ar.type == AR_FULL)
4499 gfc_error ("%qs argument of %qs intrinsic at %L shall not be an "
4500 "assumed-size array", gfc_current_intrinsic_arg[0]->name,
4501 gfc_current_intrinsic, &arg->where);
4502 return false;
4505 return true;
4509 bool
4510 gfc_check_c_associated (gfc_expr *c_ptr_1, gfc_expr *c_ptr_2)
4512 if (c_ptr_1->ts.type != BT_DERIVED
4513 || c_ptr_1->ts.u.derived->from_intmod != INTMOD_ISO_C_BINDING
4514 || (c_ptr_1->ts.u.derived->intmod_sym_id != ISOCBINDING_PTR
4515 && c_ptr_1->ts.u.derived->intmod_sym_id != ISOCBINDING_FUNPTR))
4517 gfc_error ("Argument C_PTR_1 at %L to C_ASSOCIATED shall have the "
4518 "type TYPE(C_PTR) or TYPE(C_FUNPTR)", &c_ptr_1->where);
4519 return false;
4522 if (!scalar_check (c_ptr_1, 0))
4523 return false;
4525 if (c_ptr_2
4526 && (c_ptr_2->ts.type != BT_DERIVED
4527 || c_ptr_2->ts.u.derived->from_intmod != INTMOD_ISO_C_BINDING
4528 || (c_ptr_1->ts.u.derived->intmod_sym_id
4529 != c_ptr_2->ts.u.derived->intmod_sym_id)))
4531 gfc_error ("Argument C_PTR_2 at %L to C_ASSOCIATED shall have the "
4532 "same type as C_PTR_1: %s instead of %s", &c_ptr_1->where,
4533 gfc_typename (&c_ptr_1->ts),
4534 gfc_typename (&c_ptr_2->ts));
4535 return false;
4538 if (c_ptr_2 && !scalar_check (c_ptr_2, 1))
4539 return false;
4541 return true;
4545 bool
4546 gfc_check_c_f_pointer (gfc_expr *cptr, gfc_expr *fptr, gfc_expr *shape)
4548 symbol_attribute attr;
4549 const char *msg;
4551 if (cptr->ts.type != BT_DERIVED
4552 || cptr->ts.u.derived->from_intmod != INTMOD_ISO_C_BINDING
4553 || cptr->ts.u.derived->intmod_sym_id != ISOCBINDING_PTR)
4555 gfc_error ("Argument CPTR at %L to C_F_POINTER shall have the "
4556 "type TYPE(C_PTR)", &cptr->where);
4557 return false;
4560 if (!scalar_check (cptr, 0))
4561 return false;
4563 attr = gfc_expr_attr (fptr);
4565 if (!attr.pointer)
4567 gfc_error ("Argument FPTR at %L to C_F_POINTER must be a pointer",
4568 &fptr->where);
4569 return false;
4572 if (fptr->ts.type == BT_CLASS)
4574 gfc_error ("FPTR argument at %L to C_F_POINTER shall not be polymorphic",
4575 &fptr->where);
4576 return false;
4579 if (gfc_is_coindexed (fptr))
4581 gfc_error ("Argument FPTR at %L to C_F_POINTER shall not be "
4582 "coindexed", &fptr->where);
4583 return false;
4586 if (fptr->rank == 0 && shape)
4588 gfc_error ("Unexpected SHAPE argument at %L to C_F_POINTER with scalar "
4589 "FPTR", &fptr->where);
4590 return false;
4592 else if (fptr->rank && !shape)
4594 gfc_error ("Expected SHAPE argument to C_F_POINTER with array "
4595 "FPTR at %L", &fptr->where);
4596 return false;
4599 if (shape && !rank_check (shape, 2, 1))
4600 return false;
4602 if (shape && !type_check (shape, 2, BT_INTEGER))
4603 return false;
4605 if (shape)
4607 mpz_t size;
4608 if (gfc_array_size (shape, &size))
4610 if (mpz_cmp_ui (size, fptr->rank) != 0)
4612 mpz_clear (size);
4613 gfc_error ("SHAPE argument at %L to C_F_POINTER must have the same "
4614 "size as the RANK of FPTR", &shape->where);
4615 return false;
4617 mpz_clear (size);
4621 if (fptr->ts.type == BT_CLASS)
4623 gfc_error ("Polymorphic FPTR at %L to C_F_POINTER", &fptr->where);
4624 return false;
4627 if (!is_c_interoperable (fptr, &msg, false, true))
4628 return gfc_notify_std (GFC_STD_F2008_TS, "Noninteroperable array FPTR "
4629 "at %L to C_F_POINTER: %s", &fptr->where, msg);
4631 return true;
4635 bool
4636 gfc_check_c_f_procpointer (gfc_expr *cptr, gfc_expr *fptr)
4638 symbol_attribute attr;
4640 if (cptr->ts.type != BT_DERIVED
4641 || cptr->ts.u.derived->from_intmod != INTMOD_ISO_C_BINDING
4642 || cptr->ts.u.derived->intmod_sym_id != ISOCBINDING_FUNPTR)
4644 gfc_error ("Argument CPTR at %L to C_F_PROCPOINTER shall have the "
4645 "type TYPE(C_FUNPTR)", &cptr->where);
4646 return false;
4649 if (!scalar_check (cptr, 0))
4650 return false;
4652 attr = gfc_expr_attr (fptr);
4654 if (!attr.proc_pointer)
4656 gfc_error ("Argument FPTR at %L to C_F_PROCPOINTER shall be a procedure "
4657 "pointer", &fptr->where);
4658 return false;
4661 if (gfc_is_coindexed (fptr))
4663 gfc_error ("Argument FPTR at %L to C_F_PROCPOINTER shall not be "
4664 "coindexed", &fptr->where);
4665 return false;
4668 if (!attr.is_bind_c)
4669 return gfc_notify_std (GFC_STD_F2008_TS, "Noninteroperable procedure "
4670 "pointer at %L to C_F_PROCPOINTER", &fptr->where);
4672 return true;
4676 bool
4677 gfc_check_c_funloc (gfc_expr *x)
4679 symbol_attribute attr;
4681 if (gfc_is_coindexed (x))
4683 gfc_error ("Argument X at %L to C_FUNLOC shall not be "
4684 "coindexed", &x->where);
4685 return false;
4688 attr = gfc_expr_attr (x);
4690 if (attr.function && !attr.proc_pointer && x->expr_type == EXPR_VARIABLE
4691 && x->symtree->n.sym == x->symtree->n.sym->result)
4693 gfc_namespace *ns = gfc_current_ns;
4695 for (ns = gfc_current_ns; ns; ns = ns->parent)
4696 if (x->symtree->n.sym == ns->proc_name)
4698 gfc_error ("Function result %qs at %L is invalid as X argument "
4699 "to C_FUNLOC", x->symtree->n.sym->name, &x->where);
4700 return false;
4704 if (attr.flavor != FL_PROCEDURE)
4706 gfc_error ("Argument X at %L to C_FUNLOC shall be a procedure "
4707 "or a procedure pointer", &x->where);
4708 return false;
4711 if (!attr.is_bind_c)
4712 return gfc_notify_std (GFC_STD_F2008_TS, "Noninteroperable procedure "
4713 "at %L to C_FUNLOC", &x->where);
4714 return true;
4718 bool
4719 gfc_check_c_loc (gfc_expr *x)
4721 symbol_attribute attr;
4722 const char *msg;
4724 if (gfc_is_coindexed (x))
4726 gfc_error ("Argument X at %L to C_LOC shall not be coindexed", &x->where);
4727 return false;
4730 if (x->ts.type == BT_CLASS)
4732 gfc_error ("X argument at %L to C_LOC shall not be polymorphic",
4733 &x->where);
4734 return false;
4737 attr = gfc_expr_attr (x);
4739 if (!attr.pointer
4740 && (x->expr_type != EXPR_VARIABLE || !attr.target
4741 || attr.flavor == FL_PARAMETER))
4743 gfc_error ("Argument X at %L to C_LOC shall have either "
4744 "the POINTER or the TARGET attribute", &x->where);
4745 return false;
4748 if (x->ts.type == BT_CHARACTER
4749 && gfc_var_strlen (x) == 0)
4751 gfc_error ("Argument X at %L to C_LOC shall be not be a zero-sized "
4752 "string", &x->where);
4753 return false;
4756 if (!is_c_interoperable (x, &msg, true, false))
4758 if (x->ts.type == BT_CLASS)
4760 gfc_error ("Argument at %L to C_LOC shall not be polymorphic",
4761 &x->where);
4762 return false;
4765 if (x->rank
4766 && !gfc_notify_std (GFC_STD_F2008_TS,
4767 "Noninteroperable array at %L as"
4768 " argument to C_LOC: %s", &x->where, msg))
4769 return false;
4771 else if (x->rank > 0 && gfc_notification_std (GFC_STD_F2008))
4773 gfc_array_ref *ar = gfc_find_array_ref (x);
4775 if (ar->as->type != AS_EXPLICIT && ar->as->type != AS_ASSUMED_SIZE
4776 && !attr.allocatable
4777 && !gfc_notify_std (GFC_STD_F2008,
4778 "Array of interoperable type at %L "
4779 "to C_LOC which is nonallocatable and neither "
4780 "assumed size nor explicit size", &x->where))
4781 return false;
4782 else if (ar->type != AR_FULL
4783 && !gfc_notify_std (GFC_STD_F2008, "Array section at %L "
4784 "to C_LOC", &x->where))
4785 return false;
4788 return true;
4792 bool
4793 gfc_check_sleep_sub (gfc_expr *seconds)
4795 if (!type_check (seconds, 0, BT_INTEGER))
4796 return false;
4798 if (!scalar_check (seconds, 0))
4799 return false;
4801 return true;
4804 bool
4805 gfc_check_sngl (gfc_expr *a)
4807 if (!type_check (a, 0, BT_REAL))
4808 return false;
4810 if ((a->ts.kind != gfc_default_double_kind)
4811 && !gfc_notify_std (GFC_STD_GNU, "non double precision "
4812 "REAL argument to %s intrinsic at %L",
4813 gfc_current_intrinsic, &a->where))
4814 return false;
4816 return true;
4819 bool
4820 gfc_check_spread (gfc_expr *source, gfc_expr *dim, gfc_expr *ncopies)
4822 if (source->rank >= GFC_MAX_DIMENSIONS)
4824 gfc_error ("%qs argument of %qs intrinsic at %L must be less "
4825 "than rank %d", gfc_current_intrinsic_arg[0]->name,
4826 gfc_current_intrinsic, &source->where, GFC_MAX_DIMENSIONS);
4828 return false;
4831 if (dim == NULL)
4832 return false;
4834 if (!dim_check (dim, 1, false))
4835 return false;
4837 /* dim_rank_check() does not apply here. */
4838 if (dim
4839 && dim->expr_type == EXPR_CONSTANT
4840 && (mpz_cmp_ui (dim->value.integer, 1) < 0
4841 || mpz_cmp_ui (dim->value.integer, source->rank + 1) > 0))
4843 gfc_error ("%qs argument of %qs intrinsic at %L is not a valid "
4844 "dimension index", gfc_current_intrinsic_arg[1]->name,
4845 gfc_current_intrinsic, &dim->where);
4846 return false;
4849 if (!type_check (ncopies, 2, BT_INTEGER))
4850 return false;
4852 if (!scalar_check (ncopies, 2))
4853 return false;
4855 return true;
4859 /* Functions for checking FGETC, FPUTC, FGET and FPUT (subroutines and
4860 functions). */
4862 bool
4863 gfc_check_fgetputc_sub (gfc_expr *unit, gfc_expr *c, gfc_expr *status)
4865 if (!type_check (unit, 0, BT_INTEGER))
4866 return false;
4868 if (!scalar_check (unit, 0))
4869 return false;
4871 if (!type_check (c, 1, BT_CHARACTER))
4872 return false;
4873 if (!kind_value_check (c, 1, gfc_default_character_kind))
4874 return false;
4876 if (status == NULL)
4877 return true;
4879 if (!type_check (status, 2, BT_INTEGER)
4880 || !kind_value_check (status, 2, gfc_default_integer_kind)
4881 || !scalar_check (status, 2))
4882 return false;
4884 return true;
4888 bool
4889 gfc_check_fgetputc (gfc_expr *unit, gfc_expr *c)
4891 return gfc_check_fgetputc_sub (unit, c, NULL);
4895 bool
4896 gfc_check_fgetput_sub (gfc_expr *c, gfc_expr *status)
4898 if (!type_check (c, 0, BT_CHARACTER))
4899 return false;
4900 if (!kind_value_check (c, 0, gfc_default_character_kind))
4901 return false;
4903 if (status == NULL)
4904 return true;
4906 if (!type_check (status, 1, BT_INTEGER)
4907 || !kind_value_check (status, 1, gfc_default_integer_kind)
4908 || !scalar_check (status, 1))
4909 return false;
4911 return true;
4915 bool
4916 gfc_check_fgetput (gfc_expr *c)
4918 return gfc_check_fgetput_sub (c, NULL);
4922 bool
4923 gfc_check_fseek_sub (gfc_expr *unit, gfc_expr *offset, gfc_expr *whence, gfc_expr *status)
4925 if (!type_check (unit, 0, BT_INTEGER))
4926 return false;
4928 if (!scalar_check (unit, 0))
4929 return false;
4931 if (!type_check (offset, 1, BT_INTEGER))
4932 return false;
4934 if (!scalar_check (offset, 1))
4935 return false;
4937 if (!type_check (whence, 2, BT_INTEGER))
4938 return false;
4940 if (!scalar_check (whence, 2))
4941 return false;
4943 if (status == NULL)
4944 return true;
4946 if (!type_check (status, 3, BT_INTEGER))
4947 return false;
4949 if (!kind_value_check (status, 3, 4))
4950 return false;
4952 if (!scalar_check (status, 3))
4953 return false;
4955 return true;
4960 bool
4961 gfc_check_fstat (gfc_expr *unit, gfc_expr *array)
4963 if (!type_check (unit, 0, BT_INTEGER))
4964 return false;
4966 if (!scalar_check (unit, 0))
4967 return false;
4969 if (!type_check (array, 1, BT_INTEGER)
4970 || !kind_value_check (unit, 0, gfc_default_integer_kind))
4971 return false;
4973 if (!array_check (array, 1))
4974 return false;
4976 return true;
4980 bool
4981 gfc_check_fstat_sub (gfc_expr *unit, gfc_expr *array, gfc_expr *status)
4983 if (!type_check (unit, 0, BT_INTEGER))
4984 return false;
4986 if (!scalar_check (unit, 0))
4987 return false;
4989 if (!type_check (array, 1, BT_INTEGER)
4990 || !kind_value_check (array, 1, gfc_default_integer_kind))
4991 return false;
4993 if (!array_check (array, 1))
4994 return false;
4996 if (status == NULL)
4997 return true;
4999 if (!type_check (status, 2, BT_INTEGER)
5000 || !kind_value_check (status, 2, gfc_default_integer_kind))
5001 return false;
5003 if (!scalar_check (status, 2))
5004 return false;
5006 return true;
5010 bool
5011 gfc_check_ftell (gfc_expr *unit)
5013 if (!type_check (unit, 0, BT_INTEGER))
5014 return false;
5016 if (!scalar_check (unit, 0))
5017 return false;
5019 return true;
5023 bool
5024 gfc_check_ftell_sub (gfc_expr *unit, gfc_expr *offset)
5026 if (!type_check (unit, 0, BT_INTEGER))
5027 return false;
5029 if (!scalar_check (unit, 0))
5030 return false;
5032 if (!type_check (offset, 1, BT_INTEGER))
5033 return false;
5035 if (!scalar_check (offset, 1))
5036 return false;
5038 return true;
5042 bool
5043 gfc_check_stat (gfc_expr *name, gfc_expr *array)
5045 if (!type_check (name, 0, BT_CHARACTER))
5046 return false;
5047 if (!kind_value_check (name, 0, gfc_default_character_kind))
5048 return false;
5050 if (!type_check (array, 1, BT_INTEGER)
5051 || !kind_value_check (array, 1, gfc_default_integer_kind))
5052 return false;
5054 if (!array_check (array, 1))
5055 return false;
5057 return true;
5061 bool
5062 gfc_check_stat_sub (gfc_expr *name, gfc_expr *array, gfc_expr *status)
5064 if (!type_check (name, 0, BT_CHARACTER))
5065 return false;
5066 if (!kind_value_check (name, 0, gfc_default_character_kind))
5067 return false;
5069 if (!type_check (array, 1, BT_INTEGER)
5070 || !kind_value_check (array, 1, gfc_default_integer_kind))
5071 return false;
5073 if (!array_check (array, 1))
5074 return false;
5076 if (status == NULL)
5077 return true;
5079 if (!type_check (status, 2, BT_INTEGER)
5080 || !kind_value_check (array, 1, gfc_default_integer_kind))
5081 return false;
5083 if (!scalar_check (status, 2))
5084 return false;
5086 return true;
5090 bool
5091 gfc_check_image_index (gfc_expr *coarray, gfc_expr *sub)
5093 mpz_t nelems;
5095 if (flag_coarray == GFC_FCOARRAY_NONE)
5097 gfc_fatal_error ("Coarrays disabled at %C, use %<-fcoarray=%> to enable");
5098 return false;
5101 if (!coarray_check (coarray, 0))
5102 return false;
5104 if (sub->rank != 1)
5106 gfc_error ("%s argument to IMAGE_INDEX must be a rank one array at %L",
5107 gfc_current_intrinsic_arg[1]->name, &sub->where);
5108 return false;
5111 if (gfc_array_size (sub, &nelems))
5113 int corank = gfc_get_corank (coarray);
5115 if (mpz_cmp_ui (nelems, corank) != 0)
5117 gfc_error ("The number of array elements of the SUB argument to "
5118 "IMAGE_INDEX at %L shall be %d (corank) not %d",
5119 &sub->where, corank, (int) mpz_get_si (nelems));
5120 mpz_clear (nelems);
5121 return false;
5123 mpz_clear (nelems);
5126 return true;
5130 bool
5131 gfc_check_num_images (gfc_expr *distance, gfc_expr *failed)
5133 if (flag_coarray == GFC_FCOARRAY_NONE)
5135 gfc_fatal_error ("Coarrays disabled at %C, use %<-fcoarray=%> to enable");
5136 return false;
5139 if (distance)
5141 if (!type_check (distance, 0, BT_INTEGER))
5142 return false;
5144 if (!nonnegative_check ("DISTANCE", distance))
5145 return false;
5147 if (!scalar_check (distance, 0))
5148 return false;
5150 if (!gfc_notify_std (GFC_STD_F2008_TS, "DISTANCE= argument to "
5151 "NUM_IMAGES at %L", &distance->where))
5152 return false;
5155 if (failed)
5157 if (!type_check (failed, 1, BT_LOGICAL))
5158 return false;
5160 if (!scalar_check (failed, 1))
5161 return false;
5163 if (!gfc_notify_std (GFC_STD_F2008_TS, "FAILED= argument to "
5164 "NUM_IMAGES at %L", &failed->where))
5165 return false;
5168 return true;
5172 bool
5173 gfc_check_this_image (gfc_expr *coarray, gfc_expr *dim, gfc_expr *distance)
5175 if (flag_coarray == GFC_FCOARRAY_NONE)
5177 gfc_fatal_error ("Coarrays disabled at %C, use %<-fcoarray=%> to enable");
5178 return false;
5181 if (coarray == NULL && dim == NULL && distance == NULL)
5182 return true;
5184 if (dim != NULL && coarray == NULL)
5186 gfc_error ("DIM argument without COARRAY argument not allowed for "
5187 "THIS_IMAGE intrinsic at %L", &dim->where);
5188 return false;
5191 if (distance && (coarray || dim))
5193 gfc_error ("The DISTANCE argument may not be specified together with the "
5194 "COARRAY or DIM argument in intrinsic at %L",
5195 &distance->where);
5196 return false;
5199 /* Assume that we have "this_image (distance)". */
5200 if (coarray && !gfc_is_coarray (coarray) && coarray->ts.type == BT_INTEGER)
5202 if (dim)
5204 gfc_error ("Unexpected DIM argument with noncoarray argument at %L",
5205 &coarray->where);
5206 return false;
5208 distance = coarray;
5211 if (distance)
5213 if (!type_check (distance, 2, BT_INTEGER))
5214 return false;
5216 if (!nonnegative_check ("DISTANCE", distance))
5217 return false;
5219 if (!scalar_check (distance, 2))
5220 return false;
5222 if (!gfc_notify_std (GFC_STD_F2008_TS, "DISTANCE= argument to "
5223 "THIS_IMAGE at %L", &distance->where))
5224 return false;
5226 return true;
5229 if (!coarray_check (coarray, 0))
5230 return false;
5232 if (dim != NULL)
5234 if (!dim_check (dim, 1, false))
5235 return false;
5237 if (!dim_corank_check (dim, coarray))
5238 return false;
5241 return true;
5244 /* Calculate the sizes for transfer, used by gfc_check_transfer and also
5245 by gfc_simplify_transfer. Return false if we cannot do so. */
5247 bool
5248 gfc_calculate_transfer_sizes (gfc_expr *source, gfc_expr *mold, gfc_expr *size,
5249 size_t *source_size, size_t *result_size,
5250 size_t *result_length_p)
5252 size_t result_elt_size;
5254 if (source->expr_type == EXPR_FUNCTION)
5255 return false;
5257 if (size && size->expr_type != EXPR_CONSTANT)
5258 return false;
5260 /* Calculate the size of the source. */
5261 *source_size = gfc_target_expr_size (source);
5262 if (*source_size == 0)
5263 return false;
5265 /* Determine the size of the element. */
5266 result_elt_size = gfc_element_size (mold);
5267 if (result_elt_size == 0)
5268 return false;
5270 if (mold->expr_type == EXPR_ARRAY || mold->rank || size)
5272 int result_length;
5274 if (size)
5275 result_length = (size_t)mpz_get_ui (size->value.integer);
5276 else
5278 result_length = *source_size / result_elt_size;
5279 if (result_length * result_elt_size < *source_size)
5280 result_length += 1;
5283 *result_size = result_length * result_elt_size;
5284 if (result_length_p)
5285 *result_length_p = result_length;
5287 else
5288 *result_size = result_elt_size;
5290 return true;
5294 bool
5295 gfc_check_transfer (gfc_expr *source, gfc_expr *mold, gfc_expr *size)
5297 size_t source_size;
5298 size_t result_size;
5300 if (mold->ts.type == BT_HOLLERITH)
5302 gfc_error ("%<MOLD%> argument of %<TRANSFER%> intrinsic at %L must not be"
5303 " %s", &mold->where, gfc_basic_typename (BT_HOLLERITH));
5304 return false;
5307 if (size != NULL)
5309 if (!type_check (size, 2, BT_INTEGER))
5310 return false;
5312 if (!scalar_check (size, 2))
5313 return false;
5315 if (!nonoptional_check (size, 2))
5316 return false;
5319 if (!warn_surprising)
5320 return true;
5322 /* If we can't calculate the sizes, we cannot check any more.
5323 Return true for that case. */
5325 if (!gfc_calculate_transfer_sizes (source, mold, size, &source_size,
5326 &result_size, NULL))
5327 return true;
5329 if (source_size < result_size)
5330 gfc_warning (OPT_Wsurprising,
5331 "Intrinsic TRANSFER at %L has partly undefined result: "
5332 "source size %ld < result size %ld", &source->where,
5333 (long) source_size, (long) result_size);
5335 return true;
5339 bool
5340 gfc_check_transpose (gfc_expr *matrix)
5342 if (!rank_check (matrix, 0, 2))
5343 return false;
5345 return true;
5349 bool
5350 gfc_check_ubound (gfc_expr *array, gfc_expr *dim, gfc_expr *kind)
5352 if (!array_check (array, 0))
5353 return false;
5355 if (!dim_check (dim, 1, false))
5356 return false;
5358 if (!dim_rank_check (dim, array, 0))
5359 return false;
5361 if (!kind_check (kind, 2, BT_INTEGER))
5362 return false;
5363 if (kind && !gfc_notify_std (GFC_STD_F2003, "%qs intrinsic "
5364 "with KIND argument at %L",
5365 gfc_current_intrinsic, &kind->where))
5366 return false;
5368 return true;
5372 bool
5373 gfc_check_ucobound (gfc_expr *coarray, gfc_expr *dim, gfc_expr *kind)
5375 if (flag_coarray == GFC_FCOARRAY_NONE)
5377 gfc_fatal_error ("Coarrays disabled at %C, use %<-fcoarray=%> to enable");
5378 return false;
5381 if (!coarray_check (coarray, 0))
5382 return false;
5384 if (dim != NULL)
5386 if (!dim_check (dim, 1, false))
5387 return false;
5389 if (!dim_corank_check (dim, coarray))
5390 return false;
5393 if (!kind_check (kind, 2, BT_INTEGER))
5394 return false;
5396 return true;
5400 bool
5401 gfc_check_unpack (gfc_expr *vector, gfc_expr *mask, gfc_expr *field)
5403 mpz_t vector_size;
5405 if (!rank_check (vector, 0, 1))
5406 return false;
5408 if (!array_check (mask, 1))
5409 return false;
5411 if (!type_check (mask, 1, BT_LOGICAL))
5412 return false;
5414 if (!same_type_check (vector, 0, field, 2))
5415 return false;
5417 if (mask->expr_type == EXPR_ARRAY
5418 && gfc_array_size (vector, &vector_size))
5420 int mask_true_count = 0;
5421 gfc_constructor *mask_ctor;
5422 mask_ctor = gfc_constructor_first (mask->value.constructor);
5423 while (mask_ctor)
5425 if (mask_ctor->expr->expr_type != EXPR_CONSTANT)
5427 mask_true_count = 0;
5428 break;
5431 if (mask_ctor->expr->value.logical)
5432 mask_true_count++;
5434 mask_ctor = gfc_constructor_next (mask_ctor);
5437 if (mpz_get_si (vector_size) < mask_true_count)
5439 gfc_error ("%qs argument of %qs intrinsic at %L must "
5440 "provide at least as many elements as there "
5441 "are .TRUE. values in %qs (%ld/%d)",
5442 gfc_current_intrinsic_arg[0]->name, gfc_current_intrinsic,
5443 &vector->where, gfc_current_intrinsic_arg[1]->name,
5444 mpz_get_si (vector_size), mask_true_count);
5445 return false;
5448 mpz_clear (vector_size);
5451 if (mask->rank != field->rank && field->rank != 0)
5453 gfc_error ("%qs argument of %qs intrinsic at %L must have "
5454 "the same rank as %qs or be a scalar",
5455 gfc_current_intrinsic_arg[2]->name, gfc_current_intrinsic,
5456 &field->where, gfc_current_intrinsic_arg[1]->name);
5457 return false;
5460 if (mask->rank == field->rank)
5462 int i;
5463 for (i = 0; i < field->rank; i++)
5464 if (! identical_dimen_shape (mask, i, field, i))
5466 gfc_error ("%qs and %qs arguments of %qs intrinsic at %L "
5467 "must have identical shape.",
5468 gfc_current_intrinsic_arg[2]->name,
5469 gfc_current_intrinsic_arg[1]->name, gfc_current_intrinsic,
5470 &field->where);
5474 return true;
5478 bool
5479 gfc_check_verify (gfc_expr *x, gfc_expr *y, gfc_expr *z, gfc_expr *kind)
5481 if (!type_check (x, 0, BT_CHARACTER))
5482 return false;
5484 if (!same_type_check (x, 0, y, 1))
5485 return false;
5487 if (z != NULL && !type_check (z, 2, BT_LOGICAL))
5488 return false;
5490 if (!kind_check (kind, 3, BT_INTEGER))
5491 return false;
5492 if (kind && !gfc_notify_std (GFC_STD_F2003, "%qs intrinsic "
5493 "with KIND argument at %L",
5494 gfc_current_intrinsic, &kind->where))
5495 return false;
5497 return true;
5501 bool
5502 gfc_check_trim (gfc_expr *x)
5504 if (!type_check (x, 0, BT_CHARACTER))
5505 return false;
5507 if (!scalar_check (x, 0))
5508 return false;
5510 return true;
5514 bool
5515 gfc_check_ttynam (gfc_expr *unit)
5517 if (!scalar_check (unit, 0))
5518 return false;
5520 if (!type_check (unit, 0, BT_INTEGER))
5521 return false;
5523 return true;
5527 /************* Check functions for intrinsic subroutines *************/
5529 bool
5530 gfc_check_cpu_time (gfc_expr *time)
5532 if (!scalar_check (time, 0))
5533 return false;
5535 if (!type_check (time, 0, BT_REAL))
5536 return false;
5538 if (!variable_check (time, 0, false))
5539 return false;
5541 return true;
5545 bool
5546 gfc_check_date_and_time (gfc_expr *date, gfc_expr *time,
5547 gfc_expr *zone, gfc_expr *values)
5549 if (date != NULL)
5551 if (!type_check (date, 0, BT_CHARACTER))
5552 return false;
5553 if (!kind_value_check (date, 0, gfc_default_character_kind))
5554 return false;
5555 if (!scalar_check (date, 0))
5556 return false;
5557 if (!variable_check (date, 0, false))
5558 return false;
5561 if (time != NULL)
5563 if (!type_check (time, 1, BT_CHARACTER))
5564 return false;
5565 if (!kind_value_check (time, 1, gfc_default_character_kind))
5566 return false;
5567 if (!scalar_check (time, 1))
5568 return false;
5569 if (!variable_check (time, 1, false))
5570 return false;
5573 if (zone != NULL)
5575 if (!type_check (zone, 2, BT_CHARACTER))
5576 return false;
5577 if (!kind_value_check (zone, 2, gfc_default_character_kind))
5578 return false;
5579 if (!scalar_check (zone, 2))
5580 return false;
5581 if (!variable_check (zone, 2, false))
5582 return false;
5585 if (values != NULL)
5587 if (!type_check (values, 3, BT_INTEGER))
5588 return false;
5589 if (!array_check (values, 3))
5590 return false;
5591 if (!rank_check (values, 3, 1))
5592 return false;
5593 if (!variable_check (values, 3, false))
5594 return false;
5597 return true;
5601 bool
5602 gfc_check_mvbits (gfc_expr *from, gfc_expr *frompos, gfc_expr *len,
5603 gfc_expr *to, gfc_expr *topos)
5605 if (!type_check (from, 0, BT_INTEGER))
5606 return false;
5608 if (!type_check (frompos, 1, BT_INTEGER))
5609 return false;
5611 if (!type_check (len, 2, BT_INTEGER))
5612 return false;
5614 if (!same_type_check (from, 0, to, 3))
5615 return false;
5617 if (!variable_check (to, 3, false))
5618 return false;
5620 if (!type_check (topos, 4, BT_INTEGER))
5621 return false;
5623 if (!nonnegative_check ("frompos", frompos))
5624 return false;
5626 if (!nonnegative_check ("topos", topos))
5627 return false;
5629 if (!nonnegative_check ("len", len))
5630 return false;
5632 if (!less_than_bitsize2 ("from", from, "frompos", frompos, "len", len))
5633 return false;
5635 if (!less_than_bitsize2 ("to", to, "topos", topos, "len", len))
5636 return false;
5638 return true;
5642 bool
5643 gfc_check_random_number (gfc_expr *harvest)
5645 if (!type_check (harvest, 0, BT_REAL))
5646 return false;
5648 if (!variable_check (harvest, 0, false))
5649 return false;
5651 return true;
5655 bool
5656 gfc_check_random_seed (gfc_expr *size, gfc_expr *put, gfc_expr *get)
5658 unsigned int nargs = 0, seed_size;
5659 locus *where = NULL;
5660 mpz_t put_size, get_size;
5662 /* Keep the number of bytes in sync with master_state in
5663 libgfortran/intrinsics/random.c. +1 due to the integer p which is
5664 part of the state too. */
5665 seed_size = 128 / gfc_default_integer_kind + 1;
5667 if (size != NULL)
5669 if (size->expr_type != EXPR_VARIABLE
5670 || !size->symtree->n.sym->attr.optional)
5671 nargs++;
5673 if (!scalar_check (size, 0))
5674 return false;
5676 if (!type_check (size, 0, BT_INTEGER))
5677 return false;
5679 if (!variable_check (size, 0, false))
5680 return false;
5682 if (!kind_value_check (size, 0, gfc_default_integer_kind))
5683 return false;
5686 if (put != NULL)
5688 if (put->expr_type != EXPR_VARIABLE
5689 || !put->symtree->n.sym->attr.optional)
5691 nargs++;
5692 where = &put->where;
5695 if (!array_check (put, 1))
5696 return false;
5698 if (!rank_check (put, 1, 1))
5699 return false;
5701 if (!type_check (put, 1, BT_INTEGER))
5702 return false;
5704 if (!kind_value_check (put, 1, gfc_default_integer_kind))
5705 return false;
5707 if (gfc_array_size (put, &put_size)
5708 && mpz_get_ui (put_size) < seed_size)
5709 gfc_error ("Size of %qs argument of %qs intrinsic at %L "
5710 "too small (%i/%i)",
5711 gfc_current_intrinsic_arg[1]->name, gfc_current_intrinsic,
5712 where, (int) mpz_get_ui (put_size), seed_size);
5715 if (get != NULL)
5717 if (get->expr_type != EXPR_VARIABLE
5718 || !get->symtree->n.sym->attr.optional)
5720 nargs++;
5721 where = &get->where;
5724 if (!array_check (get, 2))
5725 return false;
5727 if (!rank_check (get, 2, 1))
5728 return false;
5730 if (!type_check (get, 2, BT_INTEGER))
5731 return false;
5733 if (!variable_check (get, 2, false))
5734 return false;
5736 if (!kind_value_check (get, 2, gfc_default_integer_kind))
5737 return false;
5739 if (gfc_array_size (get, &get_size)
5740 && mpz_get_ui (get_size) < seed_size)
5741 gfc_error ("Size of %qs argument of %qs intrinsic at %L "
5742 "too small (%i/%i)",
5743 gfc_current_intrinsic_arg[2]->name, gfc_current_intrinsic,
5744 where, (int) mpz_get_ui (get_size), seed_size);
5747 /* RANDOM_SEED may not have more than one non-optional argument. */
5748 if (nargs > 1)
5749 gfc_error ("Too many arguments to %s at %L", gfc_current_intrinsic, where);
5751 return true;
5754 bool
5755 gfc_check_fe_runtime_error (gfc_actual_arglist *a)
5757 gfc_expr *e;
5758 int len, i;
5759 int num_percent, nargs;
5761 e = a->expr;
5762 if (e->expr_type != EXPR_CONSTANT)
5763 return true;
5765 len = e->value.character.length;
5766 if (e->value.character.string[len-1] != '\0')
5767 gfc_internal_error ("fe_runtime_error string must be null terminated");
5769 num_percent = 0;
5770 for (i=0; i<len-1; i++)
5771 if (e->value.character.string[i] == '%')
5772 num_percent ++;
5774 nargs = 0;
5775 for (; a; a = a->next)
5776 nargs ++;
5778 if (nargs -1 != num_percent)
5779 gfc_internal_error ("fe_runtime_error: Wrong number of arguments (%d instead of %d)",
5780 nargs, num_percent++);
5782 return true;
5785 bool
5786 gfc_check_second_sub (gfc_expr *time)
5788 if (!scalar_check (time, 0))
5789 return false;
5791 if (!type_check (time, 0, BT_REAL))
5792 return false;
5794 if (!kind_value_check (time, 0, 4))
5795 return false;
5797 return true;
5801 /* COUNT and COUNT_MAX of SYSTEM_CLOCK are scalar, default-kind integer
5802 variables in Fortran 95. In Fortran 2003 and later, they can be of any
5803 kind, and COUNT_RATE can be of type real. Note, count, count_rate, and
5804 count_max are all optional arguments */
5806 bool
5807 gfc_check_system_clock (gfc_expr *count, gfc_expr *count_rate,
5808 gfc_expr *count_max)
5810 if (count != NULL)
5812 if (!scalar_check (count, 0))
5813 return false;
5815 if (!type_check (count, 0, BT_INTEGER))
5816 return false;
5818 if (count->ts.kind != gfc_default_integer_kind
5819 && !gfc_notify_std (GFC_STD_F2003, "COUNT argument to "
5820 "SYSTEM_CLOCK at %L has non-default kind",
5821 &count->where))
5822 return false;
5824 if (!variable_check (count, 0, false))
5825 return false;
5828 if (count_rate != NULL)
5830 if (!scalar_check (count_rate, 1))
5831 return false;
5833 if (!variable_check (count_rate, 1, false))
5834 return false;
5836 if (count_rate->ts.type == BT_REAL)
5838 if (!gfc_notify_std (GFC_STD_F2003, "Real COUNT_RATE argument to "
5839 "SYSTEM_CLOCK at %L", &count_rate->where))
5840 return false;
5842 else
5844 if (!type_check (count_rate, 1, BT_INTEGER))
5845 return false;
5847 if (count_rate->ts.kind != gfc_default_integer_kind
5848 && !gfc_notify_std (GFC_STD_F2003, "COUNT_RATE argument to "
5849 "SYSTEM_CLOCK at %L has non-default kind",
5850 &count_rate->where))
5851 return false;
5856 if (count_max != NULL)
5858 if (!scalar_check (count_max, 2))
5859 return false;
5861 if (!type_check (count_max, 2, BT_INTEGER))
5862 return false;
5864 if (count_max->ts.kind != gfc_default_integer_kind
5865 && !gfc_notify_std (GFC_STD_F2003, "COUNT_MAX argument to "
5866 "SYSTEM_CLOCK at %L has non-default kind",
5867 &count_max->where))
5868 return false;
5870 if (!variable_check (count_max, 2, false))
5871 return false;
5874 return true;
5878 bool
5879 gfc_check_irand (gfc_expr *x)
5881 if (x == NULL)
5882 return true;
5884 if (!scalar_check (x, 0))
5885 return false;
5887 if (!type_check (x, 0, BT_INTEGER))
5888 return false;
5890 if (!kind_value_check (x, 0, 4))
5891 return false;
5893 return true;
5897 bool
5898 gfc_check_alarm_sub (gfc_expr *seconds, gfc_expr *handler, gfc_expr *status)
5900 if (!scalar_check (seconds, 0))
5901 return false;
5902 if (!type_check (seconds, 0, BT_INTEGER))
5903 return false;
5905 if (!int_or_proc_check (handler, 1))
5906 return false;
5907 if (handler->ts.type == BT_INTEGER && !scalar_check (handler, 1))
5908 return false;
5910 if (status == NULL)
5911 return true;
5913 if (!scalar_check (status, 2))
5914 return false;
5915 if (!type_check (status, 2, BT_INTEGER))
5916 return false;
5917 if (!kind_value_check (status, 2, gfc_default_integer_kind))
5918 return false;
5920 return true;
5924 bool
5925 gfc_check_rand (gfc_expr *x)
5927 if (x == NULL)
5928 return true;
5930 if (!scalar_check (x, 0))
5931 return false;
5933 if (!type_check (x, 0, BT_INTEGER))
5934 return false;
5936 if (!kind_value_check (x, 0, 4))
5937 return false;
5939 return true;
5943 bool
5944 gfc_check_srand (gfc_expr *x)
5946 if (!scalar_check (x, 0))
5947 return false;
5949 if (!type_check (x, 0, BT_INTEGER))
5950 return false;
5952 if (!kind_value_check (x, 0, 4))
5953 return false;
5955 return true;
5959 bool
5960 gfc_check_ctime_sub (gfc_expr *time, gfc_expr *result)
5962 if (!scalar_check (time, 0))
5963 return false;
5964 if (!type_check (time, 0, BT_INTEGER))
5965 return false;
5967 if (!type_check (result, 1, BT_CHARACTER))
5968 return false;
5969 if (!kind_value_check (result, 1, gfc_default_character_kind))
5970 return false;
5972 return true;
5976 bool
5977 gfc_check_dtime_etime (gfc_expr *x)
5979 if (!array_check (x, 0))
5980 return false;
5982 if (!rank_check (x, 0, 1))
5983 return false;
5985 if (!variable_check (x, 0, false))
5986 return false;
5988 if (!type_check (x, 0, BT_REAL))
5989 return false;
5991 if (!kind_value_check (x, 0, 4))
5992 return false;
5994 return true;
5998 bool
5999 gfc_check_dtime_etime_sub (gfc_expr *values, gfc_expr *time)
6001 if (!array_check (values, 0))
6002 return false;
6004 if (!rank_check (values, 0, 1))
6005 return false;
6007 if (!variable_check (values, 0, false))
6008 return false;
6010 if (!type_check (values, 0, BT_REAL))
6011 return false;
6013 if (!kind_value_check (values, 0, 4))
6014 return false;
6016 if (!scalar_check (time, 1))
6017 return false;
6019 if (!type_check (time, 1, BT_REAL))
6020 return false;
6022 if (!kind_value_check (time, 1, 4))
6023 return false;
6025 return true;
6029 bool
6030 gfc_check_fdate_sub (gfc_expr *date)
6032 if (!type_check (date, 0, BT_CHARACTER))
6033 return false;
6034 if (!kind_value_check (date, 0, gfc_default_character_kind))
6035 return false;
6037 return true;
6041 bool
6042 gfc_check_gerror (gfc_expr *msg)
6044 if (!type_check (msg, 0, BT_CHARACTER))
6045 return false;
6046 if (!kind_value_check (msg, 0, gfc_default_character_kind))
6047 return false;
6049 return true;
6053 bool
6054 gfc_check_getcwd_sub (gfc_expr *cwd, gfc_expr *status)
6056 if (!type_check (cwd, 0, BT_CHARACTER))
6057 return false;
6058 if (!kind_value_check (cwd, 0, gfc_default_character_kind))
6059 return false;
6061 if (status == NULL)
6062 return true;
6064 if (!scalar_check (status, 1))
6065 return false;
6067 if (!type_check (status, 1, BT_INTEGER))
6068 return false;
6070 return true;
6074 bool
6075 gfc_check_getarg (gfc_expr *pos, gfc_expr *value)
6077 if (!type_check (pos, 0, BT_INTEGER))
6078 return false;
6080 if (pos->ts.kind > gfc_default_integer_kind)
6082 gfc_error ("%qs argument of %qs intrinsic at %L must be of a kind "
6083 "not wider than the default kind (%d)",
6084 gfc_current_intrinsic_arg[0]->name, gfc_current_intrinsic,
6085 &pos->where, gfc_default_integer_kind);
6086 return false;
6089 if (!type_check (value, 1, BT_CHARACTER))
6090 return false;
6091 if (!kind_value_check (value, 1, gfc_default_character_kind))
6092 return false;
6094 return true;
6098 bool
6099 gfc_check_getlog (gfc_expr *msg)
6101 if (!type_check (msg, 0, BT_CHARACTER))
6102 return false;
6103 if (!kind_value_check (msg, 0, gfc_default_character_kind))
6104 return false;
6106 return true;
6110 bool
6111 gfc_check_exit (gfc_expr *status)
6113 if (status == NULL)
6114 return true;
6116 if (!type_check (status, 0, BT_INTEGER))
6117 return false;
6119 if (!scalar_check (status, 0))
6120 return false;
6122 return true;
6126 bool
6127 gfc_check_flush (gfc_expr *unit)
6129 if (unit == NULL)
6130 return true;
6132 if (!type_check (unit, 0, BT_INTEGER))
6133 return false;
6135 if (!scalar_check (unit, 0))
6136 return false;
6138 return true;
6142 bool
6143 gfc_check_free (gfc_expr *i)
6145 if (!type_check (i, 0, BT_INTEGER))
6146 return false;
6148 if (!scalar_check (i, 0))
6149 return false;
6151 return true;
6155 bool
6156 gfc_check_hostnm (gfc_expr *name)
6158 if (!type_check (name, 0, BT_CHARACTER))
6159 return false;
6160 if (!kind_value_check (name, 0, gfc_default_character_kind))
6161 return false;
6163 return true;
6167 bool
6168 gfc_check_hostnm_sub (gfc_expr *name, gfc_expr *status)
6170 if (!type_check (name, 0, BT_CHARACTER))
6171 return false;
6172 if (!kind_value_check (name, 0, gfc_default_character_kind))
6173 return false;
6175 if (status == NULL)
6176 return true;
6178 if (!scalar_check (status, 1))
6179 return false;
6181 if (!type_check (status, 1, BT_INTEGER))
6182 return false;
6184 return true;
6188 bool
6189 gfc_check_itime_idate (gfc_expr *values)
6191 if (!array_check (values, 0))
6192 return false;
6194 if (!rank_check (values, 0, 1))
6195 return false;
6197 if (!variable_check (values, 0, false))
6198 return false;
6200 if (!type_check (values, 0, BT_INTEGER))
6201 return false;
6203 if (!kind_value_check (values, 0, gfc_default_integer_kind))
6204 return false;
6206 return true;
6210 bool
6211 gfc_check_ltime_gmtime (gfc_expr *time, gfc_expr *values)
6213 if (!type_check (time, 0, BT_INTEGER))
6214 return false;
6216 if (!kind_value_check (time, 0, gfc_default_integer_kind))
6217 return false;
6219 if (!scalar_check (time, 0))
6220 return false;
6222 if (!array_check (values, 1))
6223 return false;
6225 if (!rank_check (values, 1, 1))
6226 return false;
6228 if (!variable_check (values, 1, false))
6229 return false;
6231 if (!type_check (values, 1, BT_INTEGER))
6232 return false;
6234 if (!kind_value_check (values, 1, gfc_default_integer_kind))
6235 return false;
6237 return true;
6241 bool
6242 gfc_check_ttynam_sub (gfc_expr *unit, gfc_expr *name)
6244 if (!scalar_check (unit, 0))
6245 return false;
6247 if (!type_check (unit, 0, BT_INTEGER))
6248 return false;
6250 if (!type_check (name, 1, BT_CHARACTER))
6251 return false;
6252 if (!kind_value_check (name, 1, gfc_default_character_kind))
6253 return false;
6255 return true;
6259 bool
6260 gfc_check_isatty (gfc_expr *unit)
6262 if (unit == NULL)
6263 return false;
6265 if (!type_check (unit, 0, BT_INTEGER))
6266 return false;
6268 if (!scalar_check (unit, 0))
6269 return false;
6271 return true;
6275 bool
6276 gfc_check_isnan (gfc_expr *x)
6278 if (!type_check (x, 0, BT_REAL))
6279 return false;
6281 return true;
6285 bool
6286 gfc_check_perror (gfc_expr *string)
6288 if (!type_check (string, 0, BT_CHARACTER))
6289 return false;
6290 if (!kind_value_check (string, 0, gfc_default_character_kind))
6291 return false;
6293 return true;
6297 bool
6298 gfc_check_umask (gfc_expr *mask)
6300 if (!type_check (mask, 0, BT_INTEGER))
6301 return false;
6303 if (!scalar_check (mask, 0))
6304 return false;
6306 return true;
6310 bool
6311 gfc_check_umask_sub (gfc_expr *mask, gfc_expr *old)
6313 if (!type_check (mask, 0, BT_INTEGER))
6314 return false;
6316 if (!scalar_check (mask, 0))
6317 return false;
6319 if (old == NULL)
6320 return true;
6322 if (!scalar_check (old, 1))
6323 return false;
6325 if (!type_check (old, 1, BT_INTEGER))
6326 return false;
6328 return true;
6332 bool
6333 gfc_check_unlink (gfc_expr *name)
6335 if (!type_check (name, 0, BT_CHARACTER))
6336 return false;
6337 if (!kind_value_check (name, 0, gfc_default_character_kind))
6338 return false;
6340 return true;
6344 bool
6345 gfc_check_unlink_sub (gfc_expr *name, gfc_expr *status)
6347 if (!type_check (name, 0, BT_CHARACTER))
6348 return false;
6349 if (!kind_value_check (name, 0, gfc_default_character_kind))
6350 return false;
6352 if (status == NULL)
6353 return true;
6355 if (!scalar_check (status, 1))
6356 return false;
6358 if (!type_check (status, 1, BT_INTEGER))
6359 return false;
6361 return true;
6365 bool
6366 gfc_check_signal (gfc_expr *number, gfc_expr *handler)
6368 if (!scalar_check (number, 0))
6369 return false;
6370 if (!type_check (number, 0, BT_INTEGER))
6371 return false;
6373 if (!int_or_proc_check (handler, 1))
6374 return false;
6375 if (handler->ts.type == BT_INTEGER && !scalar_check (handler, 1))
6376 return false;
6378 return true;
6382 bool
6383 gfc_check_signal_sub (gfc_expr *number, gfc_expr *handler, gfc_expr *status)
6385 if (!scalar_check (number, 0))
6386 return false;
6387 if (!type_check (number, 0, BT_INTEGER))
6388 return false;
6390 if (!int_or_proc_check (handler, 1))
6391 return false;
6392 if (handler->ts.type == BT_INTEGER && !scalar_check (handler, 1))
6393 return false;
6395 if (status == NULL)
6396 return true;
6398 if (!type_check (status, 2, BT_INTEGER))
6399 return false;
6400 if (!scalar_check (status, 2))
6401 return false;
6403 return true;
6407 bool
6408 gfc_check_system_sub (gfc_expr *cmd, gfc_expr *status)
6410 if (!type_check (cmd, 0, BT_CHARACTER))
6411 return false;
6412 if (!kind_value_check (cmd, 0, gfc_default_character_kind))
6413 return false;
6415 if (!scalar_check (status, 1))
6416 return false;
6418 if (!type_check (status, 1, BT_INTEGER))
6419 return false;
6421 if (!kind_value_check (status, 1, gfc_default_integer_kind))
6422 return false;
6424 return true;
6428 /* This is used for the GNU intrinsics AND, OR and XOR. */
6429 bool
6430 gfc_check_and (gfc_expr *i, gfc_expr *j)
6432 if (i->ts.type != BT_INTEGER && i->ts.type != BT_LOGICAL)
6434 gfc_error ("%qs argument of %qs intrinsic at %L must be INTEGER "
6435 "or LOGICAL", gfc_current_intrinsic_arg[0]->name,
6436 gfc_current_intrinsic, &i->where);
6437 return false;
6440 if (j->ts.type != BT_INTEGER && j->ts.type != BT_LOGICAL)
6442 gfc_error ("%qs argument of %qs intrinsic at %L must be INTEGER "
6443 "or LOGICAL", gfc_current_intrinsic_arg[1]->name,
6444 gfc_current_intrinsic, &j->where);
6445 return false;
6448 if (i->ts.type != j->ts.type)
6450 gfc_error ("%qs and %qs arguments of %qs intrinsic at %L must "
6451 "have the same type", gfc_current_intrinsic_arg[0]->name,
6452 gfc_current_intrinsic_arg[1]->name, gfc_current_intrinsic,
6453 &j->where);
6454 return false;
6457 if (!scalar_check (i, 0))
6458 return false;
6460 if (!scalar_check (j, 1))
6461 return false;
6463 return true;
6467 bool
6468 gfc_check_storage_size (gfc_expr *a, gfc_expr *kind)
6471 if (a->expr_type == EXPR_NULL)
6473 gfc_error ("Intrinsic function NULL at %L cannot be an actual "
6474 "argument to STORAGE_SIZE, because it returns a "
6475 "disassociated pointer", &a->where);
6476 return false;
6479 if (a->ts.type == BT_ASSUMED)
6481 gfc_error ("%qs argument of %qs intrinsic at %L shall not be TYPE(*)",
6482 gfc_current_intrinsic_arg[0]->name, gfc_current_intrinsic,
6483 &a->where);
6484 return false;
6487 if (a->ts.type == BT_PROCEDURE)
6489 gfc_error ("%qs argument of %qs intrinsic at %L shall not be a "
6490 "procedure", gfc_current_intrinsic_arg[0]->name,
6491 gfc_current_intrinsic, &a->where);
6492 return false;
6495 if (kind == NULL)
6496 return true;
6498 if (!type_check (kind, 1, BT_INTEGER))
6499 return false;
6501 if (!scalar_check (kind, 1))
6502 return false;
6504 if (kind->expr_type != EXPR_CONSTANT)
6506 gfc_error ("%qs argument of %qs intrinsic at %L must be a constant",
6507 gfc_current_intrinsic_arg[1]->name, gfc_current_intrinsic,
6508 &kind->where);
6509 return false;
6512 return true;